bfd/
[binutils.git] / bfd / elflink.c
blobe7cc0620f42ee7bada7d48635ccdfcb80037e055
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.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,
36 asection *sec,
37 const char *name)
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);
44 if (h != NULL)
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;
53 bh = &h->root;
54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
55 sec, 0, NULL, FALSE,
56 get_elf_backend_data (abfd)->collect,
57 &bh))
58 return NULL;
59 h = (struct elf_link_hash_entry *) bh;
60 h->def_regular = 1;
61 h->type = STT_OBJECT;
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);
66 return h;
69 bfd_boolean
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
72 flagword flags;
73 asection *s;
74 struct elf_link_hash_entry *h;
75 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
76 int ptralign;
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)
81 return TRUE;
83 switch (bed->s->arch_size)
85 case 32:
86 ptralign = 2;
87 break;
89 case 64:
90 ptralign = 3;
91 break;
93 default:
94 bfd_set_error (bfd_error_bad_value);
95 return FALSE;
98 flags = bed->dynamic_sec_flags;
100 s = bfd_make_section_with_flags (abfd, ".got", flags);
101 if (s == NULL
102 || !bfd_set_section_alignment (abfd, s, ptralign))
103 return FALSE;
105 if (bed->want_got_plt)
107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
108 if (s == NULL
109 || !bfd_set_section_alignment (abfd, s, ptralign))
110 return FALSE;
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;
121 if (h == NULL)
122 return FALSE;
125 /* The first bit of the global offset table is the header. */
126 s->size += bed->got_header_size;
128 return TRUE;
131 /* Create a strtab to hold the dynamic symbol names. */
132 static bfd_boolean
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)
145 return FALSE;
147 return TRUE;
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. */
157 bfd_boolean
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
160 flagword flags;
161 register asection *s;
162 const struct elf_backend_data *bed;
164 if (! is_elf_hash_table (info->hash))
165 return FALSE;
167 if (elf_hash_table (info)->dynamic_sections_created)
168 return TRUE;
170 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
171 return FALSE;
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);
184 if (s == NULL)
185 return FALSE;
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);
192 if (s == NULL
193 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
194 return FALSE;
196 s = bfd_make_section_with_flags (abfd, ".gnu.version",
197 flags | SEC_READONLY);
198 if (s == NULL
199 || ! bfd_set_section_alignment (abfd, s, 1))
200 return FALSE;
202 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
203 flags | SEC_READONLY);
204 if (s == NULL
205 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
206 return FALSE;
208 s = bfd_make_section_with_flags (abfd, ".dynsym",
209 flags | SEC_READONLY);
210 if (s == NULL
211 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
212 return FALSE;
214 s = bfd_make_section_with_flags (abfd, ".dynstr",
215 flags | SEC_READONLY);
216 if (s == NULL)
217 return FALSE;
219 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
220 if (s == NULL
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
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"))
231 return FALSE;
233 if (info->emit_hash)
235 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
236 if (s == NULL
237 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
238 return FALSE;
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);
246 if (s == NULL
247 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
248 return FALSE;
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;
254 else
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))
262 return FALSE;
264 elf_hash_table (info)->dynamic_sections_created = TRUE;
266 return TRUE;
269 /* Create dynamic sections when linking against a dynamic object. */
271 bfd_boolean
272 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
274 flagword flags, pltflags;
275 struct elf_link_hash_entry *h;
276 asection *s;
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;
283 pltflags = 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);
289 else
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);
295 if (s == NULL
296 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
297 return FALSE;
299 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
300 .plt section. */
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;
306 if (h == NULL)
307 return FALSE;
310 s = bfd_make_section_with_flags (abfd,
311 (bed->default_use_rela_p
312 ? ".rela.plt" : ".rel.plt"),
313 flags | SEC_READONLY);
314 if (s == NULL
315 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
316 return FALSE;
318 if (! _bfd_elf_create_got_section (abfd, info))
319 return FALSE;
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",
330 (SEC_ALLOC
331 | SEC_LINKER_CREATED));
332 if (s == NULL)
333 return FALSE;
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
345 copy relocs. */
346 if (! info->shared)
348 s = bfd_make_section_with_flags (abfd,
349 (bed->default_use_rela_p
350 ? ".rela.bss" : ".rel.bss"),
351 flags | SEC_READONLY);
352 if (s == NULL
353 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
354 return FALSE;
358 return TRUE;
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
367 one. */
369 bfd_boolean
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;
376 char *p;
377 const char *name;
378 bfd_size_type indx;
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))
386 case STV_INTERNAL:
387 case STV_HIDDEN:
388 if (h->root.type != bfd_link_hash_undefined
389 && h->root.type != bfd_link_hash_undefweak)
391 h->forced_local = 1;
392 if (!elf_hash_table (info)->is_relocatable_executable)
393 return TRUE;
396 default:
397 break;
400 h->dynindx = elf_hash_table (info)->dynsymcount;
401 ++elf_hash_table (info)->dynsymcount;
403 dynstr = elf_hash_table (info)->dynstr;
404 if (dynstr == NULL)
406 /* Create a strtab to hold the dynamic symbol names. */
407 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
408 if (dynstr == NULL)
409 return FALSE;
412 /* We don't put any version information in the dynamic string
413 table. */
414 name = h->root.root.string;
415 p = strchr (name, ELF_VER_CHR);
416 if (p != NULL)
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. */
422 *p = 0;
424 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
426 if (p != NULL)
427 *p = ELF_VER_CHR;
429 if (indx == (bfd_size_type) -1)
430 return FALSE;
431 h->dynstr_index = indx;
434 return TRUE;
437 /* Mark a symbol dynamic. */
439 void
440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
441 struct elf_link_hash_entry *h)
443 struct bfd_elf_dynamic_list *d = info->dynamic;
445 if (d == NULL || info->relocatable)
446 return;
448 if ((*d->match) (&d->head, NULL, h->root.root.string))
449 h->dynamic = 1;
452 /* Record an assignment to a symbol made by a linker script. We need
453 this in case some dynamic object refers to this symbol. */
455 bfd_boolean
456 bfd_elf_record_link_assignment (bfd *output_bfd,
457 struct bfd_link_info *info,
458 const char *name,
459 bfd_boolean provide,
460 bfd_boolean hidden)
462 struct elf_link_hash_entry *h;
463 struct elf_link_hash_table *htab;
465 if (!is_elf_hash_table (info->hash))
466 return TRUE;
468 htab = elf_hash_table (info);
469 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
470 if (h == NULL)
471 return provide;
473 /* Since we're defining the symbol, don't let it seem to have not
474 been defined. record_dynamic_symbol and size_dynamic_sections
475 may depend on this. */
476 if (h->root.type == bfd_link_hash_undefweak
477 || h->root.type == bfd_link_hash_undefined)
479 h->root.type = bfd_link_hash_new;
480 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
481 bfd_link_repair_undef_list (&htab->root);
484 if (h->root.type == bfd_link_hash_new)
486 bfd_elf_link_mark_dynamic_symbol (info, h);
487 h->non_elf = 0;
490 /* If this symbol is being provided by the linker script, and it is
491 currently defined by a dynamic object, but not by a regular
492 object, then mark it as undefined so that the generic linker will
493 force the correct value. */
494 if (provide
495 && h->def_dynamic
496 && !h->def_regular)
497 h->root.type = bfd_link_hash_undefined;
499 /* If this symbol is not being provided by the linker script, and it is
500 currently defined by a dynamic object, but not by a regular object,
501 then clear out any version information because the symbol will not be
502 associated with the dynamic object any more. */
503 if (!provide
504 && h->def_dynamic
505 && !h->def_regular)
506 h->verinfo.verdef = NULL;
508 h->def_regular = 1;
510 if (provide && hidden)
512 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
514 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
515 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
518 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
519 and executables. */
520 if (!info->relocatable
521 && h->dynindx != -1
522 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
523 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
524 h->forced_local = 1;
526 if ((h->def_dynamic
527 || h->ref_dynamic
528 || info->shared
529 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
530 && h->dynindx == -1)
532 if (! bfd_elf_link_record_dynamic_symbol (info, h))
533 return FALSE;
535 /* If this is a weak defined symbol, and we know a corresponding
536 real symbol from the same dynamic object, make sure the real
537 symbol is also made into a dynamic symbol. */
538 if (h->u.weakdef != NULL
539 && h->u.weakdef->dynindx == -1)
541 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
542 return FALSE;
546 return TRUE;
549 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
550 success, and 2 on a failure caused by attempting to record a symbol
551 in a discarded section, eg. a discarded link-once section symbol. */
554 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
555 bfd *input_bfd,
556 long input_indx)
558 bfd_size_type amt;
559 struct elf_link_local_dynamic_entry *entry;
560 struct elf_link_hash_table *eht;
561 struct elf_strtab_hash *dynstr;
562 unsigned long dynstr_index;
563 char *name;
564 Elf_External_Sym_Shndx eshndx;
565 char esym[sizeof (Elf64_External_Sym)];
567 if (! is_elf_hash_table (info->hash))
568 return 0;
570 /* See if the entry exists already. */
571 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
572 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
573 return 1;
575 amt = sizeof (*entry);
576 entry = bfd_alloc (input_bfd, amt);
577 if (entry == NULL)
578 return 0;
580 /* Go find the symbol, so that we can find it's name. */
581 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
582 1, input_indx, &entry->isym, esym, &eshndx))
584 bfd_release (input_bfd, entry);
585 return 0;
588 if (entry->isym.st_shndx != SHN_UNDEF
589 && (entry->isym.st_shndx < SHN_LORESERVE
590 || entry->isym.st_shndx > SHN_HIRESERVE))
592 asection *s;
594 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
595 if (s == NULL || bfd_is_abs_section (s->output_section))
597 /* We can still bfd_release here as nothing has done another
598 bfd_alloc. We can't do this later in this function. */
599 bfd_release (input_bfd, entry);
600 return 2;
604 name = (bfd_elf_string_from_elf_section
605 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
606 entry->isym.st_name));
608 dynstr = elf_hash_table (info)->dynstr;
609 if (dynstr == NULL)
611 /* Create a strtab to hold the dynamic symbol names. */
612 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
613 if (dynstr == NULL)
614 return 0;
617 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
618 if (dynstr_index == (unsigned long) -1)
619 return 0;
620 entry->isym.st_name = dynstr_index;
622 eht = elf_hash_table (info);
624 entry->next = eht->dynlocal;
625 eht->dynlocal = entry;
626 entry->input_bfd = input_bfd;
627 entry->input_indx = input_indx;
628 eht->dynsymcount++;
630 /* Whatever binding the symbol had before, it's now local. */
631 entry->isym.st_info
632 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
634 /* The dynindx will be set at the end of size_dynamic_sections. */
636 return 1;
639 /* Return the dynindex of a local dynamic symbol. */
641 long
642 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
643 bfd *input_bfd,
644 long input_indx)
646 struct elf_link_local_dynamic_entry *e;
648 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
649 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
650 return e->dynindx;
651 return -1;
654 /* This function is used to renumber the dynamic symbols, if some of
655 them are removed because they are marked as local. This is called
656 via elf_link_hash_traverse. */
658 static bfd_boolean
659 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
660 void *data)
662 size_t *count = data;
664 if (h->root.type == bfd_link_hash_warning)
665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
667 if (h->forced_local)
668 return TRUE;
670 if (h->dynindx != -1)
671 h->dynindx = ++(*count);
673 return TRUE;
677 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
678 STB_LOCAL binding. */
680 static bfd_boolean
681 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
682 void *data)
684 size_t *count = data;
686 if (h->root.type == bfd_link_hash_warning)
687 h = (struct elf_link_hash_entry *) h->root.u.i.link;
689 if (!h->forced_local)
690 return TRUE;
692 if (h->dynindx != -1)
693 h->dynindx = ++(*count);
695 return TRUE;
698 /* Return true if the dynamic symbol for a given section should be
699 omitted when creating a shared library. */
700 bfd_boolean
701 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
702 struct bfd_link_info *info,
703 asection *p)
705 struct elf_link_hash_table *htab;
707 switch (elf_section_data (p)->this_hdr.sh_type)
709 case SHT_PROGBITS:
710 case SHT_NOBITS:
711 /* If sh_type is yet undecided, assume it could be
712 SHT_PROGBITS/SHT_NOBITS. */
713 case SHT_NULL:
714 htab = elf_hash_table (info);
715 if (p == htab->tls_sec)
716 return FALSE;
718 if (htab->text_index_section != NULL)
719 return p != htab->text_index_section && p != htab->data_index_section;
721 if (strcmp (p->name, ".got") == 0
722 || strcmp (p->name, ".got.plt") == 0
723 || strcmp (p->name, ".plt") == 0)
725 asection *ip;
727 if (htab->dynobj != NULL
728 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
729 && (ip->flags & SEC_LINKER_CREATED)
730 && ip->output_section == p)
731 return TRUE;
733 return FALSE;
735 /* There shouldn't be section relative relocations
736 against any other section. */
737 default:
738 return TRUE;
742 /* Assign dynsym indices. In a shared library we generate a section
743 symbol for each output section, which come first. Next come symbols
744 which have been forced to local binding. Then all of the back-end
745 allocated local dynamic syms, followed by the rest of the global
746 symbols. */
748 static unsigned long
749 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
750 struct bfd_link_info *info,
751 unsigned long *section_sym_count)
753 unsigned long dynsymcount = 0;
755 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
757 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
758 asection *p;
759 for (p = output_bfd->sections; p ; p = p->next)
760 if ((p->flags & SEC_EXCLUDE) == 0
761 && (p->flags & SEC_ALLOC) != 0
762 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
763 elf_section_data (p)->dynindx = ++dynsymcount;
764 else
765 elf_section_data (p)->dynindx = 0;
767 *section_sym_count = dynsymcount;
769 elf_link_hash_traverse (elf_hash_table (info),
770 elf_link_renumber_local_hash_table_dynsyms,
771 &dynsymcount);
773 if (elf_hash_table (info)->dynlocal)
775 struct elf_link_local_dynamic_entry *p;
776 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
777 p->dynindx = ++dynsymcount;
780 elf_link_hash_traverse (elf_hash_table (info),
781 elf_link_renumber_hash_table_dynsyms,
782 &dynsymcount);
784 /* There is an unused NULL entry at the head of the table which
785 we must account for in our count. Unless there weren't any
786 symbols, which means we'll have no table at all. */
787 if (dynsymcount != 0)
788 ++dynsymcount;
790 elf_hash_table (info)->dynsymcount = dynsymcount;
791 return dynsymcount;
794 /* This function is called when we want to define a new symbol. It
795 handles the various cases which arise when we find a definition in
796 a dynamic object, or when there is already a definition in a
797 dynamic object. The new symbol is described by NAME, SYM, PSEC,
798 and PVALUE. We set SYM_HASH to the hash table entry. We set
799 OVERRIDE if the old symbol is overriding a new definition. We set
800 TYPE_CHANGE_OK if it is OK for the type to change. We set
801 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
802 change, we mean that we shouldn't warn if the type or size does
803 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
804 object is overridden by a regular object. */
806 bfd_boolean
807 _bfd_elf_merge_symbol (bfd *abfd,
808 struct bfd_link_info *info,
809 const char *name,
810 Elf_Internal_Sym *sym,
811 asection **psec,
812 bfd_vma *pvalue,
813 unsigned int *pold_alignment,
814 struct elf_link_hash_entry **sym_hash,
815 bfd_boolean *skip,
816 bfd_boolean *override,
817 bfd_boolean *type_change_ok,
818 bfd_boolean *size_change_ok)
820 asection *sec, *oldsec;
821 struct elf_link_hash_entry *h;
822 struct elf_link_hash_entry *flip;
823 int bind;
824 bfd *oldbfd;
825 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
826 bfd_boolean newweak, oldweak;
827 const struct elf_backend_data *bed;
829 *skip = FALSE;
830 *override = FALSE;
832 sec = *psec;
833 bind = ELF_ST_BIND (sym->st_info);
835 /* Silently discard TLS symbols from --just-syms. There's no way to
836 combine a static TLS block with a new TLS block for this executable. */
837 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
838 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
840 *skip = TRUE;
841 return TRUE;
844 if (! bfd_is_und_section (sec))
845 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
846 else
847 h = ((struct elf_link_hash_entry *)
848 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
849 if (h == NULL)
850 return FALSE;
851 *sym_hash = h;
853 /* This code is for coping with dynamic objects, and is only useful
854 if we are doing an ELF link. */
855 if (info->hash->creator != abfd->xvec)
856 return TRUE;
858 /* For merging, we only care about real symbols. */
860 while (h->root.type == bfd_link_hash_indirect
861 || h->root.type == bfd_link_hash_warning)
862 h = (struct elf_link_hash_entry *) h->root.u.i.link;
864 /* If we just created the symbol, mark it as being an ELF symbol.
865 Other than that, there is nothing to do--there is no merge issue
866 with a newly defined symbol--so we just return. */
868 if (h->root.type == bfd_link_hash_new)
870 bfd_elf_link_mark_dynamic_symbol (info, h);
871 h->non_elf = 0;
872 return TRUE;
875 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
876 existing symbol. */
878 switch (h->root.type)
880 default:
881 oldbfd = NULL;
882 oldsec = NULL;
883 break;
885 case bfd_link_hash_undefined:
886 case bfd_link_hash_undefweak:
887 oldbfd = h->root.u.undef.abfd;
888 oldsec = NULL;
889 break;
891 case bfd_link_hash_defined:
892 case bfd_link_hash_defweak:
893 oldbfd = h->root.u.def.section->owner;
894 oldsec = h->root.u.def.section;
895 break;
897 case bfd_link_hash_common:
898 oldbfd = h->root.u.c.p->section->owner;
899 oldsec = h->root.u.c.p->section;
900 break;
903 /* In cases involving weak versioned symbols, we may wind up trying
904 to merge a symbol with itself. Catch that here, to avoid the
905 confusion that results if we try to override a symbol with
906 itself. The additional tests catch cases like
907 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
908 dynamic object, which we do want to handle here. */
909 if (abfd == oldbfd
910 && ((abfd->flags & DYNAMIC) == 0
911 || !h->def_regular))
912 return TRUE;
914 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
915 respectively, is from a dynamic object. */
917 newdyn = (abfd->flags & DYNAMIC) != 0;
919 olddyn = FALSE;
920 if (oldbfd != NULL)
921 olddyn = (oldbfd->flags & DYNAMIC) != 0;
922 else if (oldsec != NULL)
924 /* This handles the special SHN_MIPS_{TEXT,DATA} section
925 indices used by MIPS ELF. */
926 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
929 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
930 respectively, appear to be a definition rather than reference. */
932 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
934 olddef = (h->root.type != bfd_link_hash_undefined
935 && h->root.type != bfd_link_hash_undefweak
936 && h->root.type != bfd_link_hash_common);
938 /* When we try to create a default indirect symbol from the dynamic
939 definition with the default version, we skip it if its type and
940 the type of existing regular definition mismatch. We only do it
941 if the existing regular definition won't be dynamic. */
942 if (pold_alignment == NULL
943 && !info->shared
944 && !info->export_dynamic
945 && !h->ref_dynamic
946 && newdyn
947 && newdef
948 && !olddyn
949 && (olddef || h->root.type == bfd_link_hash_common)
950 && ELF_ST_TYPE (sym->st_info) != h->type
951 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
952 && h->type != STT_NOTYPE)
954 *skip = TRUE;
955 return TRUE;
958 /* Check TLS symbol. We don't check undefined symbol introduced by
959 "ld -u". */
960 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
961 && ELF_ST_TYPE (sym->st_info) != h->type
962 && oldbfd != NULL)
964 bfd *ntbfd, *tbfd;
965 bfd_boolean ntdef, tdef;
966 asection *ntsec, *tsec;
968 if (h->type == STT_TLS)
970 ntbfd = abfd;
971 ntsec = sec;
972 ntdef = newdef;
973 tbfd = oldbfd;
974 tsec = oldsec;
975 tdef = olddef;
977 else
979 ntbfd = oldbfd;
980 ntsec = oldsec;
981 ntdef = olddef;
982 tbfd = abfd;
983 tsec = sec;
984 tdef = newdef;
987 if (tdef && ntdef)
988 (*_bfd_error_handler)
989 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
990 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
991 else if (!tdef && !ntdef)
992 (*_bfd_error_handler)
993 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
994 tbfd, ntbfd, h->root.root.string);
995 else if (tdef)
996 (*_bfd_error_handler)
997 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
998 tbfd, tsec, ntbfd, h->root.root.string);
999 else
1000 (*_bfd_error_handler)
1001 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1002 tbfd, ntbfd, ntsec, h->root.root.string);
1004 bfd_set_error (bfd_error_bad_value);
1005 return FALSE;
1008 /* We need to remember if a symbol has a definition in a dynamic
1009 object or is weak in all dynamic objects. Internal and hidden
1010 visibility will make it unavailable to dynamic objects. */
1011 if (newdyn && !h->dynamic_def)
1013 if (!bfd_is_und_section (sec))
1014 h->dynamic_def = 1;
1015 else
1017 /* Check if this symbol is weak in all dynamic objects. If it
1018 is the first time we see it in a dynamic object, we mark
1019 if it is weak. Otherwise, we clear it. */
1020 if (!h->ref_dynamic)
1022 if (bind == STB_WEAK)
1023 h->dynamic_weak = 1;
1025 else if (bind != STB_WEAK)
1026 h->dynamic_weak = 0;
1030 /* If the old symbol has non-default visibility, we ignore the new
1031 definition from a dynamic object. */
1032 if (newdyn
1033 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1034 && !bfd_is_und_section (sec))
1036 *skip = TRUE;
1037 /* Make sure this symbol is dynamic. */
1038 h->ref_dynamic = 1;
1039 /* A protected symbol has external availability. Make sure it is
1040 recorded as dynamic.
1042 FIXME: Should we check type and size for protected symbol? */
1043 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1044 return bfd_elf_link_record_dynamic_symbol (info, h);
1045 else
1046 return TRUE;
1048 else if (!newdyn
1049 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1050 && h->def_dynamic)
1052 /* If the new symbol with non-default visibility comes from a
1053 relocatable file and the old definition comes from a dynamic
1054 object, we remove the old definition. */
1055 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1057 /* Handle the case where the old dynamic definition is
1058 default versioned. We need to copy the symbol info from
1059 the symbol with default version to the normal one if it
1060 was referenced before. */
1061 if (h->ref_regular)
1063 const struct elf_backend_data *bed
1064 = get_elf_backend_data (abfd);
1065 struct elf_link_hash_entry *vh = *sym_hash;
1066 vh->root.type = h->root.type;
1067 h->root.type = bfd_link_hash_indirect;
1068 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1069 /* Protected symbols will override the dynamic definition
1070 with default version. */
1071 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1073 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1074 vh->dynamic_def = 1;
1075 vh->ref_dynamic = 1;
1077 else
1079 h->root.type = vh->root.type;
1080 vh->ref_dynamic = 0;
1081 /* We have to hide it here since it was made dynamic
1082 global with extra bits when the symbol info was
1083 copied from the old dynamic definition. */
1084 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1086 h = vh;
1088 else
1089 h = *sym_hash;
1092 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1093 && bfd_is_und_section (sec))
1095 /* If the new symbol is undefined and the old symbol was
1096 also undefined before, we need to make sure
1097 _bfd_generic_link_add_one_symbol doesn't mess
1098 up the linker hash table undefs list. Since the old
1099 definition came from a dynamic object, it is still on the
1100 undefs list. */
1101 h->root.type = bfd_link_hash_undefined;
1102 h->root.u.undef.abfd = abfd;
1104 else
1106 h->root.type = bfd_link_hash_new;
1107 h->root.u.undef.abfd = NULL;
1110 if (h->def_dynamic)
1112 h->def_dynamic = 0;
1113 h->ref_dynamic = 1;
1114 h->dynamic_def = 1;
1116 /* FIXME: Should we check type and size for protected symbol? */
1117 h->size = 0;
1118 h->type = 0;
1119 return TRUE;
1122 /* Differentiate strong and weak symbols. */
1123 newweak = bind == STB_WEAK;
1124 oldweak = (h->root.type == bfd_link_hash_defweak
1125 || h->root.type == bfd_link_hash_undefweak);
1127 /* If a new weak symbol definition comes from a regular file and the
1128 old symbol comes from a dynamic library, we treat the new one as
1129 strong. Similarly, an old weak symbol definition from a regular
1130 file is treated as strong when the new symbol comes from a dynamic
1131 library. Further, an old weak symbol from a dynamic library is
1132 treated as strong if the new symbol is from a dynamic library.
1133 This reflects the way glibc's ld.so works.
1135 Do this before setting *type_change_ok or *size_change_ok so that
1136 we warn properly when dynamic library symbols are overridden. */
1138 if (newdef && !newdyn && olddyn)
1139 newweak = FALSE;
1140 if (olddef && newdyn)
1141 oldweak = FALSE;
1143 /* It's OK to change the type if either the existing symbol or the
1144 new symbol is weak. A type change is also OK if the old symbol
1145 is undefined and the new symbol is defined. */
1147 if (oldweak
1148 || newweak
1149 || (newdef
1150 && h->root.type == bfd_link_hash_undefined))
1151 *type_change_ok = TRUE;
1153 /* It's OK to change the size if either the existing symbol or the
1154 new symbol is weak, or if the old symbol is undefined. */
1156 if (*type_change_ok
1157 || h->root.type == bfd_link_hash_undefined)
1158 *size_change_ok = TRUE;
1160 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1161 symbol, respectively, appears to be a common symbol in a dynamic
1162 object. If a symbol appears in an uninitialized section, and is
1163 not weak, and is not a function, then it may be a common symbol
1164 which was resolved when the dynamic object was created. We want
1165 to treat such symbols specially, because they raise special
1166 considerations when setting the symbol size: if the symbol
1167 appears as a common symbol in a regular object, and the size in
1168 the regular object is larger, we must make sure that we use the
1169 larger size. This problematic case can always be avoided in C,
1170 but it must be handled correctly when using Fortran shared
1171 libraries.
1173 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1174 likewise for OLDDYNCOMMON and OLDDEF.
1176 Note that this test is just a heuristic, and that it is quite
1177 possible to have an uninitialized symbol in a shared object which
1178 is really a definition, rather than a common symbol. This could
1179 lead to some minor confusion when the symbol really is a common
1180 symbol in some regular object. However, I think it will be
1181 harmless. */
1183 if (newdyn
1184 && newdef
1185 && !newweak
1186 && (sec->flags & SEC_ALLOC) != 0
1187 && (sec->flags & SEC_LOAD) == 0
1188 && sym->st_size > 0
1189 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1190 newdyncommon = TRUE;
1191 else
1192 newdyncommon = FALSE;
1194 if (olddyn
1195 && olddef
1196 && h->root.type == bfd_link_hash_defined
1197 && h->def_dynamic
1198 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1199 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1200 && h->size > 0
1201 && h->type != STT_FUNC)
1202 olddyncommon = TRUE;
1203 else
1204 olddyncommon = FALSE;
1206 /* We now know everything about the old and new symbols. We ask the
1207 backend to check if we can merge them. */
1208 bed = get_elf_backend_data (abfd);
1209 if (bed->merge_symbol
1210 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1211 pold_alignment, skip, override,
1212 type_change_ok, size_change_ok,
1213 &newdyn, &newdef, &newdyncommon, &newweak,
1214 abfd, &sec,
1215 &olddyn, &olddef, &olddyncommon, &oldweak,
1216 oldbfd, &oldsec))
1217 return FALSE;
1219 /* If both the old and the new symbols look like common symbols in a
1220 dynamic object, set the size of the symbol to the larger of the
1221 two. */
1223 if (olddyncommon
1224 && newdyncommon
1225 && sym->st_size != h->size)
1227 /* Since we think we have two common symbols, issue a multiple
1228 common warning if desired. Note that we only warn if the
1229 size is different. If the size is the same, we simply let
1230 the old symbol override the new one as normally happens with
1231 symbols defined in dynamic objects. */
1233 if (! ((*info->callbacks->multiple_common)
1234 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1235 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1236 return FALSE;
1238 if (sym->st_size > h->size)
1239 h->size = sym->st_size;
1241 *size_change_ok = TRUE;
1244 /* If we are looking at a dynamic object, and we have found a
1245 definition, we need to see if the symbol was already defined by
1246 some other object. If so, we want to use the existing
1247 definition, and we do not want to report a multiple symbol
1248 definition error; we do this by clobbering *PSEC to be
1249 bfd_und_section_ptr.
1251 We treat a common symbol as a definition if the symbol in the
1252 shared library is a function, since common symbols always
1253 represent variables; this can cause confusion in principle, but
1254 any such confusion would seem to indicate an erroneous program or
1255 shared library. We also permit a common symbol in a regular
1256 object to override a weak symbol in a shared object. */
1258 if (newdyn
1259 && newdef
1260 && (olddef
1261 || (h->root.type == bfd_link_hash_common
1262 && (newweak
1263 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1265 *override = TRUE;
1266 newdef = FALSE;
1267 newdyncommon = FALSE;
1269 *psec = sec = bfd_und_section_ptr;
1270 *size_change_ok = TRUE;
1272 /* If we get here when the old symbol is a common symbol, then
1273 we are explicitly letting it override a weak symbol or
1274 function in a dynamic object, and we don't want to warn about
1275 a type change. If the old symbol is a defined symbol, a type
1276 change warning may still be appropriate. */
1278 if (h->root.type == bfd_link_hash_common)
1279 *type_change_ok = TRUE;
1282 /* Handle the special case of an old common symbol merging with a
1283 new symbol which looks like a common symbol in a shared object.
1284 We change *PSEC and *PVALUE to make the new symbol look like a
1285 common symbol, and let _bfd_generic_link_add_one_symbol do the
1286 right thing. */
1288 if (newdyncommon
1289 && h->root.type == bfd_link_hash_common)
1291 *override = TRUE;
1292 newdef = FALSE;
1293 newdyncommon = FALSE;
1294 *pvalue = sym->st_size;
1295 *psec = sec = bed->common_section (oldsec);
1296 *size_change_ok = TRUE;
1299 /* Skip weak definitions of symbols that are already defined. */
1300 if (newdef && olddef && newweak)
1301 *skip = TRUE;
1303 /* If the old symbol is from a dynamic object, and the new symbol is
1304 a definition which is not from a dynamic object, then the new
1305 symbol overrides the old symbol. Symbols from regular files
1306 always take precedence over symbols from dynamic objects, even if
1307 they are defined after the dynamic object in the link.
1309 As above, we again permit a common symbol in a regular object to
1310 override a definition in a shared object if the shared object
1311 symbol is a function or is weak. */
1313 flip = NULL;
1314 if (!newdyn
1315 && (newdef
1316 || (bfd_is_com_section (sec)
1317 && (oldweak
1318 || h->type == STT_FUNC)))
1319 && olddyn
1320 && olddef
1321 && h->def_dynamic)
1323 /* Change the hash table entry to undefined, and let
1324 _bfd_generic_link_add_one_symbol do the right thing with the
1325 new definition. */
1327 h->root.type = bfd_link_hash_undefined;
1328 h->root.u.undef.abfd = h->root.u.def.section->owner;
1329 *size_change_ok = TRUE;
1331 olddef = FALSE;
1332 olddyncommon = FALSE;
1334 /* We again permit a type change when a common symbol may be
1335 overriding a function. */
1337 if (bfd_is_com_section (sec))
1338 *type_change_ok = TRUE;
1340 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1341 flip = *sym_hash;
1342 else
1343 /* This union may have been set to be non-NULL when this symbol
1344 was seen in a dynamic object. We must force the union to be
1345 NULL, so that it is correct for a regular symbol. */
1346 h->verinfo.vertree = NULL;
1349 /* Handle the special case of a new common symbol merging with an
1350 old symbol that looks like it might be a common symbol defined in
1351 a shared object. Note that we have already handled the case in
1352 which a new common symbol should simply override the definition
1353 in the shared library. */
1355 if (! newdyn
1356 && bfd_is_com_section (sec)
1357 && olddyncommon)
1359 /* It would be best if we could set the hash table entry to a
1360 common symbol, but we don't know what to use for the section
1361 or the alignment. */
1362 if (! ((*info->callbacks->multiple_common)
1363 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1364 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1365 return FALSE;
1367 /* If the presumed common symbol in the dynamic object is
1368 larger, pretend that the new symbol has its size. */
1370 if (h->size > *pvalue)
1371 *pvalue = h->size;
1373 /* We need to remember the alignment required by the symbol
1374 in the dynamic object. */
1375 BFD_ASSERT (pold_alignment);
1376 *pold_alignment = h->root.u.def.section->alignment_power;
1378 olddef = FALSE;
1379 olddyncommon = FALSE;
1381 h->root.type = bfd_link_hash_undefined;
1382 h->root.u.undef.abfd = h->root.u.def.section->owner;
1384 *size_change_ok = TRUE;
1385 *type_change_ok = TRUE;
1387 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1388 flip = *sym_hash;
1389 else
1390 h->verinfo.vertree = NULL;
1393 if (flip != NULL)
1395 /* Handle the case where we had a versioned symbol in a dynamic
1396 library and now find a definition in a normal object. In this
1397 case, we make the versioned symbol point to the normal one. */
1398 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1399 flip->root.type = h->root.type;
1400 h->root.type = bfd_link_hash_indirect;
1401 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1402 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1403 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1404 if (h->def_dynamic)
1406 h->def_dynamic = 0;
1407 flip->ref_dynamic = 1;
1411 return TRUE;
1414 /* This function is called to create an indirect symbol from the
1415 default for the symbol with the default version if needed. The
1416 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1417 set DYNSYM if the new indirect symbol is dynamic. */
1419 bfd_boolean
1420 _bfd_elf_add_default_symbol (bfd *abfd,
1421 struct bfd_link_info *info,
1422 struct elf_link_hash_entry *h,
1423 const char *name,
1424 Elf_Internal_Sym *sym,
1425 asection **psec,
1426 bfd_vma *value,
1427 bfd_boolean *dynsym,
1428 bfd_boolean override)
1430 bfd_boolean type_change_ok;
1431 bfd_boolean size_change_ok;
1432 bfd_boolean skip;
1433 char *shortname;
1434 struct elf_link_hash_entry *hi;
1435 struct bfd_link_hash_entry *bh;
1436 const struct elf_backend_data *bed;
1437 bfd_boolean collect;
1438 bfd_boolean dynamic;
1439 char *p;
1440 size_t len, shortlen;
1441 asection *sec;
1443 /* If this symbol has a version, and it is the default version, we
1444 create an indirect symbol from the default name to the fully
1445 decorated name. This will cause external references which do not
1446 specify a version to be bound to this version of the symbol. */
1447 p = strchr (name, ELF_VER_CHR);
1448 if (p == NULL || p[1] != ELF_VER_CHR)
1449 return TRUE;
1451 if (override)
1453 /* We are overridden by an old definition. We need to check if we
1454 need to create the indirect symbol from the default name. */
1455 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1456 FALSE, FALSE);
1457 BFD_ASSERT (hi != NULL);
1458 if (hi == h)
1459 return TRUE;
1460 while (hi->root.type == bfd_link_hash_indirect
1461 || hi->root.type == bfd_link_hash_warning)
1463 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1464 if (hi == h)
1465 return TRUE;
1469 bed = get_elf_backend_data (abfd);
1470 collect = bed->collect;
1471 dynamic = (abfd->flags & DYNAMIC) != 0;
1473 shortlen = p - name;
1474 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1475 if (shortname == NULL)
1476 return FALSE;
1477 memcpy (shortname, name, shortlen);
1478 shortname[shortlen] = '\0';
1480 /* We are going to create a new symbol. Merge it with any existing
1481 symbol with this name. For the purposes of the merge, act as
1482 though we were defining the symbol we just defined, although we
1483 actually going to define an indirect symbol. */
1484 type_change_ok = FALSE;
1485 size_change_ok = FALSE;
1486 sec = *psec;
1487 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1488 NULL, &hi, &skip, &override,
1489 &type_change_ok, &size_change_ok))
1490 return FALSE;
1492 if (skip)
1493 goto nondefault;
1495 if (! override)
1497 bh = &hi->root;
1498 if (! (_bfd_generic_link_add_one_symbol
1499 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1500 0, name, FALSE, collect, &bh)))
1501 return FALSE;
1502 hi = (struct elf_link_hash_entry *) bh;
1504 else
1506 /* In this case the symbol named SHORTNAME is overriding the
1507 indirect symbol we want to add. We were planning on making
1508 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1509 is the name without a version. NAME is the fully versioned
1510 name, and it is the default version.
1512 Overriding means that we already saw a definition for the
1513 symbol SHORTNAME in a regular object, and it is overriding
1514 the symbol defined in the dynamic object.
1516 When this happens, we actually want to change NAME, the
1517 symbol we just added, to refer to SHORTNAME. This will cause
1518 references to NAME in the shared object to become references
1519 to SHORTNAME in the regular object. This is what we expect
1520 when we override a function in a shared object: that the
1521 references in the shared object will be mapped to the
1522 definition in the regular object. */
1524 while (hi->root.type == bfd_link_hash_indirect
1525 || hi->root.type == bfd_link_hash_warning)
1526 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1528 h->root.type = bfd_link_hash_indirect;
1529 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1530 if (h->def_dynamic)
1532 h->def_dynamic = 0;
1533 hi->ref_dynamic = 1;
1534 if (hi->ref_regular
1535 || hi->def_regular)
1537 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1538 return FALSE;
1542 /* Now set HI to H, so that the following code will set the
1543 other fields correctly. */
1544 hi = h;
1547 /* If there is a duplicate definition somewhere, then HI may not
1548 point to an indirect symbol. We will have reported an error to
1549 the user in that case. */
1551 if (hi->root.type == bfd_link_hash_indirect)
1553 struct elf_link_hash_entry *ht;
1555 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1556 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1558 /* See if the new flags lead us to realize that the symbol must
1559 be dynamic. */
1560 if (! *dynsym)
1562 if (! dynamic)
1564 if (info->shared
1565 || hi->ref_dynamic)
1566 *dynsym = TRUE;
1568 else
1570 if (hi->ref_regular)
1571 *dynsym = TRUE;
1576 /* We also need to define an indirection from the nondefault version
1577 of the symbol. */
1579 nondefault:
1580 len = strlen (name);
1581 shortname = bfd_hash_allocate (&info->hash->table, len);
1582 if (shortname == NULL)
1583 return FALSE;
1584 memcpy (shortname, name, shortlen);
1585 memcpy (shortname + shortlen, p + 1, len - shortlen);
1587 /* Once again, merge with any existing symbol. */
1588 type_change_ok = FALSE;
1589 size_change_ok = FALSE;
1590 sec = *psec;
1591 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1592 NULL, &hi, &skip, &override,
1593 &type_change_ok, &size_change_ok))
1594 return FALSE;
1596 if (skip)
1597 return TRUE;
1599 if (override)
1601 /* Here SHORTNAME is a versioned name, so we don't expect to see
1602 the type of override we do in the case above unless it is
1603 overridden by a versioned definition. */
1604 if (hi->root.type != bfd_link_hash_defined
1605 && hi->root.type != bfd_link_hash_defweak)
1606 (*_bfd_error_handler)
1607 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1608 abfd, shortname);
1610 else
1612 bh = &hi->root;
1613 if (! (_bfd_generic_link_add_one_symbol
1614 (info, abfd, shortname, BSF_INDIRECT,
1615 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1616 return FALSE;
1617 hi = (struct elf_link_hash_entry *) bh;
1619 /* If there is a duplicate definition somewhere, then HI may not
1620 point to an indirect symbol. We will have reported an error
1621 to the user in that case. */
1623 if (hi->root.type == bfd_link_hash_indirect)
1625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1627 /* See if the new flags lead us to realize that the symbol
1628 must be dynamic. */
1629 if (! *dynsym)
1631 if (! dynamic)
1633 if (info->shared
1634 || hi->ref_dynamic)
1635 *dynsym = TRUE;
1637 else
1639 if (hi->ref_regular)
1640 *dynsym = TRUE;
1646 return TRUE;
1649 /* This routine is used to export all defined symbols into the dynamic
1650 symbol table. It is called via elf_link_hash_traverse. */
1652 bfd_boolean
1653 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1655 struct elf_info_failed *eif = data;
1657 /* Ignore this if we won't export it. */
1658 if (!eif->info->export_dynamic && !h->dynamic)
1659 return TRUE;
1661 /* Ignore indirect symbols. These are added by the versioning code. */
1662 if (h->root.type == bfd_link_hash_indirect)
1663 return TRUE;
1665 if (h->root.type == bfd_link_hash_warning)
1666 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1668 if (h->dynindx == -1
1669 && (h->def_regular
1670 || h->ref_regular))
1672 struct bfd_elf_version_tree *t;
1673 struct bfd_elf_version_expr *d;
1675 for (t = eif->verdefs; t != NULL; t = t->next)
1677 if (t->globals.list != NULL)
1679 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1680 if (d != NULL)
1681 goto doit;
1684 if (t->locals.list != NULL)
1686 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1687 if (d != NULL)
1688 return TRUE;
1692 if (!eif->verdefs)
1694 doit:
1695 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1697 eif->failed = TRUE;
1698 return FALSE;
1703 return TRUE;
1706 /* Look through the symbols which are defined in other shared
1707 libraries and referenced here. Update the list of version
1708 dependencies. This will be put into the .gnu.version_r section.
1709 This function is called via elf_link_hash_traverse. */
1711 bfd_boolean
1712 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1713 void *data)
1715 struct elf_find_verdep_info *rinfo = data;
1716 Elf_Internal_Verneed *t;
1717 Elf_Internal_Vernaux *a;
1718 bfd_size_type amt;
1720 if (h->root.type == bfd_link_hash_warning)
1721 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1723 /* We only care about symbols defined in shared objects with version
1724 information. */
1725 if (!h->def_dynamic
1726 || h->def_regular
1727 || h->dynindx == -1
1728 || h->verinfo.verdef == NULL)
1729 return TRUE;
1731 /* See if we already know about this version. */
1732 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1734 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1735 continue;
1737 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1738 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1739 return TRUE;
1741 break;
1744 /* This is a new version. Add it to tree we are building. */
1746 if (t == NULL)
1748 amt = sizeof *t;
1749 t = bfd_zalloc (rinfo->output_bfd, amt);
1750 if (t == NULL)
1752 rinfo->failed = TRUE;
1753 return FALSE;
1756 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1757 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1758 elf_tdata (rinfo->output_bfd)->verref = t;
1761 amt = sizeof *a;
1762 a = bfd_zalloc (rinfo->output_bfd, amt);
1764 /* Note that we are copying a string pointer here, and testing it
1765 above. If bfd_elf_string_from_elf_section is ever changed to
1766 discard the string data when low in memory, this will have to be
1767 fixed. */
1768 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1770 a->vna_flags = h->verinfo.verdef->vd_flags;
1771 a->vna_nextptr = t->vn_auxptr;
1773 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1774 ++rinfo->vers;
1776 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1778 t->vn_auxptr = a;
1780 return TRUE;
1783 /* Figure out appropriate versions for all the symbols. We may not
1784 have the version number script until we have read all of the input
1785 files, so until that point we don't know which symbols should be
1786 local. This function is called via elf_link_hash_traverse. */
1788 bfd_boolean
1789 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1791 struct elf_assign_sym_version_info *sinfo;
1792 struct bfd_link_info *info;
1793 const struct elf_backend_data *bed;
1794 struct elf_info_failed eif;
1795 char *p;
1796 bfd_size_type amt;
1798 sinfo = data;
1799 info = sinfo->info;
1801 if (h->root.type == bfd_link_hash_warning)
1802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1804 /* Fix the symbol flags. */
1805 eif.failed = FALSE;
1806 eif.info = info;
1807 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1809 if (eif.failed)
1810 sinfo->failed = TRUE;
1811 return FALSE;
1814 /* We only need version numbers for symbols defined in regular
1815 objects. */
1816 if (!h->def_regular)
1817 return TRUE;
1819 bed = get_elf_backend_data (sinfo->output_bfd);
1820 p = strchr (h->root.root.string, ELF_VER_CHR);
1821 if (p != NULL && h->verinfo.vertree == NULL)
1823 struct bfd_elf_version_tree *t;
1824 bfd_boolean hidden;
1826 hidden = TRUE;
1828 /* There are two consecutive ELF_VER_CHR characters if this is
1829 not a hidden symbol. */
1830 ++p;
1831 if (*p == ELF_VER_CHR)
1833 hidden = FALSE;
1834 ++p;
1837 /* If there is no version string, we can just return out. */
1838 if (*p == '\0')
1840 if (hidden)
1841 h->hidden = 1;
1842 return TRUE;
1845 /* Look for the version. If we find it, it is no longer weak. */
1846 for (t = sinfo->verdefs; t != NULL; t = t->next)
1848 if (strcmp (t->name, p) == 0)
1850 size_t len;
1851 char *alc;
1852 struct bfd_elf_version_expr *d;
1854 len = p - h->root.root.string;
1855 alc = bfd_malloc (len);
1856 if (alc == NULL)
1857 return FALSE;
1858 memcpy (alc, h->root.root.string, len - 1);
1859 alc[len - 1] = '\0';
1860 if (alc[len - 2] == ELF_VER_CHR)
1861 alc[len - 2] = '\0';
1863 h->verinfo.vertree = t;
1864 t->used = TRUE;
1865 d = NULL;
1867 if (t->globals.list != NULL)
1868 d = (*t->match) (&t->globals, NULL, alc);
1870 /* See if there is anything to force this symbol to
1871 local scope. */
1872 if (d == NULL && t->locals.list != NULL)
1874 d = (*t->match) (&t->locals, NULL, alc);
1875 if (d != NULL
1876 && h->dynindx != -1
1877 && ! info->export_dynamic)
1878 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1881 free (alc);
1882 break;
1886 /* If we are building an application, we need to create a
1887 version node for this version. */
1888 if (t == NULL && info->executable)
1890 struct bfd_elf_version_tree **pp;
1891 int version_index;
1893 /* If we aren't going to export this symbol, we don't need
1894 to worry about it. */
1895 if (h->dynindx == -1)
1896 return TRUE;
1898 amt = sizeof *t;
1899 t = bfd_zalloc (sinfo->output_bfd, amt);
1900 if (t == NULL)
1902 sinfo->failed = TRUE;
1903 return FALSE;
1906 t->name = p;
1907 t->name_indx = (unsigned int) -1;
1908 t->used = TRUE;
1910 version_index = 1;
1911 /* Don't count anonymous version tag. */
1912 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1913 version_index = 0;
1914 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1915 ++version_index;
1916 t->vernum = version_index;
1918 *pp = t;
1920 h->verinfo.vertree = t;
1922 else if (t == NULL)
1924 /* We could not find the version for a symbol when
1925 generating a shared archive. Return an error. */
1926 (*_bfd_error_handler)
1927 (_("%B: undefined versioned symbol name %s"),
1928 sinfo->output_bfd, h->root.root.string);
1929 bfd_set_error (bfd_error_bad_value);
1930 sinfo->failed = TRUE;
1931 return FALSE;
1934 if (hidden)
1935 h->hidden = 1;
1938 /* If we don't have a version for this symbol, see if we can find
1939 something. */
1940 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1942 struct bfd_elf_version_tree *t;
1943 struct bfd_elf_version_tree *local_ver;
1944 struct bfd_elf_version_expr *d;
1946 /* See if can find what version this symbol is in. If the
1947 symbol is supposed to be local, then don't actually register
1948 it. */
1949 local_ver = NULL;
1950 for (t = sinfo->verdefs; t != NULL; t = t->next)
1952 if (t->globals.list != NULL)
1954 bfd_boolean matched;
1956 matched = FALSE;
1957 d = NULL;
1958 while ((d = (*t->match) (&t->globals, d,
1959 h->root.root.string)) != NULL)
1960 if (d->symver)
1961 matched = TRUE;
1962 else
1964 /* There is a version without definition. Make
1965 the symbol the default definition for this
1966 version. */
1967 h->verinfo.vertree = t;
1968 local_ver = NULL;
1969 d->script = 1;
1970 break;
1972 if (d != NULL)
1973 break;
1974 else if (matched)
1975 /* There is no undefined version for this symbol. Hide the
1976 default one. */
1977 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1980 if (t->locals.list != NULL)
1982 d = NULL;
1983 while ((d = (*t->match) (&t->locals, d,
1984 h->root.root.string)) != NULL)
1986 local_ver = t;
1987 /* If the match is "*", keep looking for a more
1988 explicit, perhaps even global, match.
1989 XXX: Shouldn't this be !d->wildcard instead? */
1990 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1991 break;
1994 if (d != NULL)
1995 break;
1999 if (local_ver != NULL)
2001 h->verinfo.vertree = local_ver;
2002 if (h->dynindx != -1
2003 && ! info->export_dynamic)
2005 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2010 return TRUE;
2013 /* Read and swap the relocs from the section indicated by SHDR. This
2014 may be either a REL or a RELA section. The relocations are
2015 translated into RELA relocations and stored in INTERNAL_RELOCS,
2016 which should have already been allocated to contain enough space.
2017 The EXTERNAL_RELOCS are a buffer where the external form of the
2018 relocations should be stored.
2020 Returns FALSE if something goes wrong. */
2022 static bfd_boolean
2023 elf_link_read_relocs_from_section (bfd *abfd,
2024 asection *sec,
2025 Elf_Internal_Shdr *shdr,
2026 void *external_relocs,
2027 Elf_Internal_Rela *internal_relocs)
2029 const struct elf_backend_data *bed;
2030 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2031 const bfd_byte *erela;
2032 const bfd_byte *erelaend;
2033 Elf_Internal_Rela *irela;
2034 Elf_Internal_Shdr *symtab_hdr;
2035 size_t nsyms;
2037 /* Position ourselves at the start of the section. */
2038 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2039 return FALSE;
2041 /* Read the relocations. */
2042 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2043 return FALSE;
2045 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2046 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2048 bed = get_elf_backend_data (abfd);
2050 /* Convert the external relocations to the internal format. */
2051 if (shdr->sh_entsize == bed->s->sizeof_rel)
2052 swap_in = bed->s->swap_reloc_in;
2053 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2054 swap_in = bed->s->swap_reloca_in;
2055 else
2057 bfd_set_error (bfd_error_wrong_format);
2058 return FALSE;
2061 erela = external_relocs;
2062 erelaend = erela + shdr->sh_size;
2063 irela = internal_relocs;
2064 while (erela < erelaend)
2066 bfd_vma r_symndx;
2068 (*swap_in) (abfd, erela, irela);
2069 r_symndx = ELF32_R_SYM (irela->r_info);
2070 if (bed->s->arch_size == 64)
2071 r_symndx >>= 24;
2072 if ((size_t) r_symndx >= nsyms)
2074 (*_bfd_error_handler)
2075 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2076 " for offset 0x%lx in section `%A'"),
2077 abfd, sec,
2078 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2079 bfd_set_error (bfd_error_bad_value);
2080 return FALSE;
2082 irela += bed->s->int_rels_per_ext_rel;
2083 erela += shdr->sh_entsize;
2086 return TRUE;
2089 /* Read and swap the relocs for a section O. They may have been
2090 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2091 not NULL, they are used as buffers to read into. They are known to
2092 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2093 the return value is allocated using either malloc or bfd_alloc,
2094 according to the KEEP_MEMORY argument. If O has two relocation
2095 sections (both REL and RELA relocations), then the REL_HDR
2096 relocations will appear first in INTERNAL_RELOCS, followed by the
2097 REL_HDR2 relocations. */
2099 Elf_Internal_Rela *
2100 _bfd_elf_link_read_relocs (bfd *abfd,
2101 asection *o,
2102 void *external_relocs,
2103 Elf_Internal_Rela *internal_relocs,
2104 bfd_boolean keep_memory)
2106 Elf_Internal_Shdr *rel_hdr;
2107 void *alloc1 = NULL;
2108 Elf_Internal_Rela *alloc2 = NULL;
2109 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2111 if (elf_section_data (o)->relocs != NULL)
2112 return elf_section_data (o)->relocs;
2114 if (o->reloc_count == 0)
2115 return NULL;
2117 rel_hdr = &elf_section_data (o)->rel_hdr;
2119 if (internal_relocs == NULL)
2121 bfd_size_type size;
2123 size = o->reloc_count;
2124 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2125 if (keep_memory)
2126 internal_relocs = bfd_alloc (abfd, size);
2127 else
2128 internal_relocs = alloc2 = bfd_malloc (size);
2129 if (internal_relocs == NULL)
2130 goto error_return;
2133 if (external_relocs == NULL)
2135 bfd_size_type size = rel_hdr->sh_size;
2137 if (elf_section_data (o)->rel_hdr2)
2138 size += elf_section_data (o)->rel_hdr2->sh_size;
2139 alloc1 = bfd_malloc (size);
2140 if (alloc1 == NULL)
2141 goto error_return;
2142 external_relocs = alloc1;
2145 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2146 external_relocs,
2147 internal_relocs))
2148 goto error_return;
2149 if (elf_section_data (o)->rel_hdr2
2150 && (!elf_link_read_relocs_from_section
2151 (abfd, o,
2152 elf_section_data (o)->rel_hdr2,
2153 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2154 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2155 * bed->s->int_rels_per_ext_rel))))
2156 goto error_return;
2158 /* Cache the results for next time, if we can. */
2159 if (keep_memory)
2160 elf_section_data (o)->relocs = internal_relocs;
2162 if (alloc1 != NULL)
2163 free (alloc1);
2165 /* Don't free alloc2, since if it was allocated we are passing it
2166 back (under the name of internal_relocs). */
2168 return internal_relocs;
2170 error_return:
2171 if (alloc1 != NULL)
2172 free (alloc1);
2173 if (alloc2 != NULL)
2174 free (alloc2);
2175 return NULL;
2178 /* Compute the size of, and allocate space for, REL_HDR which is the
2179 section header for a section containing relocations for O. */
2181 bfd_boolean
2182 _bfd_elf_link_size_reloc_section (bfd *abfd,
2183 Elf_Internal_Shdr *rel_hdr,
2184 asection *o)
2186 bfd_size_type reloc_count;
2187 bfd_size_type num_rel_hashes;
2189 /* Figure out how many relocations there will be. */
2190 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2191 reloc_count = elf_section_data (o)->rel_count;
2192 else
2193 reloc_count = elf_section_data (o)->rel_count2;
2195 num_rel_hashes = o->reloc_count;
2196 if (num_rel_hashes < reloc_count)
2197 num_rel_hashes = reloc_count;
2199 /* That allows us to calculate the size of the section. */
2200 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2202 /* The contents field must last into write_object_contents, so we
2203 allocate it with bfd_alloc rather than malloc. Also since we
2204 cannot be sure that the contents will actually be filled in,
2205 we zero the allocated space. */
2206 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2207 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2208 return FALSE;
2210 /* We only allocate one set of hash entries, so we only do it the
2211 first time we are called. */
2212 if (elf_section_data (o)->rel_hashes == NULL
2213 && num_rel_hashes)
2215 struct elf_link_hash_entry **p;
2217 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2218 if (p == NULL)
2219 return FALSE;
2221 elf_section_data (o)->rel_hashes = p;
2224 return TRUE;
2227 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2228 originated from the section given by INPUT_REL_HDR) to the
2229 OUTPUT_BFD. */
2231 bfd_boolean
2232 _bfd_elf_link_output_relocs (bfd *output_bfd,
2233 asection *input_section,
2234 Elf_Internal_Shdr *input_rel_hdr,
2235 Elf_Internal_Rela *internal_relocs,
2236 struct elf_link_hash_entry **rel_hash
2237 ATTRIBUTE_UNUSED)
2239 Elf_Internal_Rela *irela;
2240 Elf_Internal_Rela *irelaend;
2241 bfd_byte *erel;
2242 Elf_Internal_Shdr *output_rel_hdr;
2243 asection *output_section;
2244 unsigned int *rel_countp = NULL;
2245 const struct elf_backend_data *bed;
2246 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2248 output_section = input_section->output_section;
2249 output_rel_hdr = NULL;
2251 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2252 == input_rel_hdr->sh_entsize)
2254 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2255 rel_countp = &elf_section_data (output_section)->rel_count;
2257 else if (elf_section_data (output_section)->rel_hdr2
2258 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2259 == input_rel_hdr->sh_entsize))
2261 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2262 rel_countp = &elf_section_data (output_section)->rel_count2;
2264 else
2266 (*_bfd_error_handler)
2267 (_("%B: relocation size mismatch in %B section %A"),
2268 output_bfd, input_section->owner, input_section);
2269 bfd_set_error (bfd_error_wrong_object_format);
2270 return FALSE;
2273 bed = get_elf_backend_data (output_bfd);
2274 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2275 swap_out = bed->s->swap_reloc_out;
2276 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2277 swap_out = bed->s->swap_reloca_out;
2278 else
2279 abort ();
2281 erel = output_rel_hdr->contents;
2282 erel += *rel_countp * input_rel_hdr->sh_entsize;
2283 irela = internal_relocs;
2284 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2285 * bed->s->int_rels_per_ext_rel);
2286 while (irela < irelaend)
2288 (*swap_out) (output_bfd, irela, erel);
2289 irela += bed->s->int_rels_per_ext_rel;
2290 erel += input_rel_hdr->sh_entsize;
2293 /* Bump the counter, so that we know where to add the next set of
2294 relocations. */
2295 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2297 return TRUE;
2300 /* Make weak undefined symbols in PIE dynamic. */
2302 bfd_boolean
2303 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2304 struct elf_link_hash_entry *h)
2306 if (info->pie
2307 && h->dynindx == -1
2308 && h->root.type == bfd_link_hash_undefweak)
2309 return bfd_elf_link_record_dynamic_symbol (info, h);
2311 return TRUE;
2314 /* Fix up the flags for a symbol. This handles various cases which
2315 can only be fixed after all the input files are seen. This is
2316 currently called by both adjust_dynamic_symbol and
2317 assign_sym_version, which is unnecessary but perhaps more robust in
2318 the face of future changes. */
2320 bfd_boolean
2321 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2322 struct elf_info_failed *eif)
2324 const struct elf_backend_data *bed = NULL;
2326 /* If this symbol was mentioned in a non-ELF file, try to set
2327 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2328 permit a non-ELF file to correctly refer to a symbol defined in
2329 an ELF dynamic object. */
2330 if (h->non_elf)
2332 while (h->root.type == bfd_link_hash_indirect)
2333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2335 if (h->root.type != bfd_link_hash_defined
2336 && h->root.type != bfd_link_hash_defweak)
2338 h->ref_regular = 1;
2339 h->ref_regular_nonweak = 1;
2341 else
2343 if (h->root.u.def.section->owner != NULL
2344 && (bfd_get_flavour (h->root.u.def.section->owner)
2345 == bfd_target_elf_flavour))
2347 h->ref_regular = 1;
2348 h->ref_regular_nonweak = 1;
2350 else
2351 h->def_regular = 1;
2354 if (h->dynindx == -1
2355 && (h->def_dynamic
2356 || h->ref_dynamic))
2358 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2360 eif->failed = TRUE;
2361 return FALSE;
2365 else
2367 /* Unfortunately, NON_ELF is only correct if the symbol
2368 was first seen in a non-ELF file. Fortunately, if the symbol
2369 was first seen in an ELF file, we're probably OK unless the
2370 symbol was defined in a non-ELF file. Catch that case here.
2371 FIXME: We're still in trouble if the symbol was first seen in
2372 a dynamic object, and then later in a non-ELF regular object. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && !h->def_regular
2376 && (h->root.u.def.section->owner != NULL
2377 ? (bfd_get_flavour (h->root.u.def.section->owner)
2378 != bfd_target_elf_flavour)
2379 : (bfd_is_abs_section (h->root.u.def.section)
2380 && !h->def_dynamic)))
2381 h->def_regular = 1;
2384 /* Backend specific symbol fixup. */
2385 if (elf_hash_table (eif->info)->dynobj)
2387 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2388 if (bed->elf_backend_fixup_symbol
2389 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2390 return FALSE;
2393 /* If this is a final link, and the symbol was defined as a common
2394 symbol in a regular object file, and there was no definition in
2395 any dynamic object, then the linker will have allocated space for
2396 the symbol in a common section but the DEF_REGULAR
2397 flag will not have been set. */
2398 if (h->root.type == bfd_link_hash_defined
2399 && !h->def_regular
2400 && h->ref_regular
2401 && !h->def_dynamic
2402 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2403 h->def_regular = 1;
2405 /* If -Bsymbolic was used (which means to bind references to global
2406 symbols to the definition within the shared object), and this
2407 symbol was defined in a regular object, then it actually doesn't
2408 need a PLT entry. Likewise, if the symbol has non-default
2409 visibility. If the symbol has hidden or internal visibility, we
2410 will force it local. */
2411 if (h->needs_plt
2412 && eif->info->shared
2413 && is_elf_hash_table (eif->info->hash)
2414 && (SYMBOLIC_BIND (eif->info, h)
2415 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2416 && h->def_regular)
2418 bfd_boolean force_local;
2420 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2421 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2422 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2425 /* If a weak undefined symbol has non-default visibility, we also
2426 hide it from the dynamic linker. */
2427 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2428 && h->root.type == bfd_link_hash_undefweak)
2430 const struct elf_backend_data *bed;
2431 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2432 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2435 /* If this is a weak defined symbol in a dynamic object, and we know
2436 the real definition in the dynamic object, copy interesting flags
2437 over to the real definition. */
2438 if (h->u.weakdef != NULL)
2440 struct elf_link_hash_entry *weakdef;
2442 weakdef = h->u.weakdef;
2443 if (h->root.type == bfd_link_hash_indirect)
2444 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2446 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2447 || h->root.type == bfd_link_hash_defweak);
2448 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2449 || weakdef->root.type == bfd_link_hash_defweak);
2450 BFD_ASSERT (weakdef->def_dynamic);
2452 /* If the real definition is defined by a regular object file,
2453 don't do anything special. See the longer description in
2454 _bfd_elf_adjust_dynamic_symbol, below. */
2455 if (weakdef->def_regular)
2456 h->u.weakdef = NULL;
2457 else
2458 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2462 return TRUE;
2465 /* Make the backend pick a good value for a dynamic symbol. This is
2466 called via elf_link_hash_traverse, and also calls itself
2467 recursively. */
2469 bfd_boolean
2470 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2472 struct elf_info_failed *eif = data;
2473 bfd *dynobj;
2474 const struct elf_backend_data *bed;
2476 if (! is_elf_hash_table (eif->info->hash))
2477 return FALSE;
2479 if (h->root.type == bfd_link_hash_warning)
2481 h->got = elf_hash_table (eif->info)->init_got_offset;
2482 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2484 /* When warning symbols are created, they **replace** the "real"
2485 entry in the hash table, thus we never get to see the real
2486 symbol in a hash traversal. So look at it now. */
2487 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2490 /* Ignore indirect symbols. These are added by the versioning code. */
2491 if (h->root.type == bfd_link_hash_indirect)
2492 return TRUE;
2494 /* Fix the symbol flags. */
2495 if (! _bfd_elf_fix_symbol_flags (h, eif))
2496 return FALSE;
2498 /* If this symbol does not require a PLT entry, and it is not
2499 defined by a dynamic object, or is not referenced by a regular
2500 object, ignore it. We do have to handle a weak defined symbol,
2501 even if no regular object refers to it, if we decided to add it
2502 to the dynamic symbol table. FIXME: Do we normally need to worry
2503 about symbols which are defined by one dynamic object and
2504 referenced by another one? */
2505 if (!h->needs_plt
2506 && (h->def_regular
2507 || !h->def_dynamic
2508 || (!h->ref_regular
2509 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2511 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2512 return TRUE;
2515 /* If we've already adjusted this symbol, don't do it again. This
2516 can happen via a recursive call. */
2517 if (h->dynamic_adjusted)
2518 return TRUE;
2520 /* Don't look at this symbol again. Note that we must set this
2521 after checking the above conditions, because we may look at a
2522 symbol once, decide not to do anything, and then get called
2523 recursively later after REF_REGULAR is set below. */
2524 h->dynamic_adjusted = 1;
2526 /* If this is a weak definition, and we know a real definition, and
2527 the real symbol is not itself defined by a regular object file,
2528 then get a good value for the real definition. We handle the
2529 real symbol first, for the convenience of the backend routine.
2531 Note that there is a confusing case here. If the real definition
2532 is defined by a regular object file, we don't get the real symbol
2533 from the dynamic object, but we do get the weak symbol. If the
2534 processor backend uses a COPY reloc, then if some routine in the
2535 dynamic object changes the real symbol, we will not see that
2536 change in the corresponding weak symbol. This is the way other
2537 ELF linkers work as well, and seems to be a result of the shared
2538 library model.
2540 I will clarify this issue. Most SVR4 shared libraries define the
2541 variable _timezone and define timezone as a weak synonym. The
2542 tzset call changes _timezone. If you write
2543 extern int timezone;
2544 int _timezone = 5;
2545 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2546 you might expect that, since timezone is a synonym for _timezone,
2547 the same number will print both times. However, if the processor
2548 backend uses a COPY reloc, then actually timezone will be copied
2549 into your process image, and, since you define _timezone
2550 yourself, _timezone will not. Thus timezone and _timezone will
2551 wind up at different memory locations. The tzset call will set
2552 _timezone, leaving timezone unchanged. */
2554 if (h->u.weakdef != NULL)
2556 /* If we get to this point, we know there is an implicit
2557 reference by a regular object file via the weak symbol H.
2558 FIXME: Is this really true? What if the traversal finds
2559 H->U.WEAKDEF before it finds H? */
2560 h->u.weakdef->ref_regular = 1;
2562 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2563 return FALSE;
2566 /* If a symbol has no type and no size and does not require a PLT
2567 entry, then we are probably about to do the wrong thing here: we
2568 are probably going to create a COPY reloc for an empty object.
2569 This case can arise when a shared object is built with assembly
2570 code, and the assembly code fails to set the symbol type. */
2571 if (h->size == 0
2572 && h->type == STT_NOTYPE
2573 && !h->needs_plt)
2574 (*_bfd_error_handler)
2575 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2576 h->root.root.string);
2578 dynobj = elf_hash_table (eif->info)->dynobj;
2579 bed = get_elf_backend_data (dynobj);
2580 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2582 eif->failed = TRUE;
2583 return FALSE;
2586 return TRUE;
2589 /* Adjust all external symbols pointing into SEC_MERGE sections
2590 to reflect the object merging within the sections. */
2592 bfd_boolean
2593 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2595 asection *sec;
2597 if (h->root.type == bfd_link_hash_warning)
2598 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2600 if ((h->root.type == bfd_link_hash_defined
2601 || h->root.type == bfd_link_hash_defweak)
2602 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2603 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2605 bfd *output_bfd = data;
2607 h->root.u.def.value =
2608 _bfd_merged_section_offset (output_bfd,
2609 &h->root.u.def.section,
2610 elf_section_data (sec)->sec_info,
2611 h->root.u.def.value);
2614 return TRUE;
2617 /* Returns false if the symbol referred to by H should be considered
2618 to resolve local to the current module, and true if it should be
2619 considered to bind dynamically. */
2621 bfd_boolean
2622 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2623 struct bfd_link_info *info,
2624 bfd_boolean ignore_protected)
2626 bfd_boolean binding_stays_local_p;
2628 if (h == NULL)
2629 return FALSE;
2631 while (h->root.type == bfd_link_hash_indirect
2632 || h->root.type == bfd_link_hash_warning)
2633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2635 /* If it was forced local, then clearly it's not dynamic. */
2636 if (h->dynindx == -1)
2637 return FALSE;
2638 if (h->forced_local)
2639 return FALSE;
2641 /* Identify the cases where name binding rules say that a
2642 visible symbol resolves locally. */
2643 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2645 switch (ELF_ST_VISIBILITY (h->other))
2647 case STV_INTERNAL:
2648 case STV_HIDDEN:
2649 return FALSE;
2651 case STV_PROTECTED:
2652 /* Proper resolution for function pointer equality may require
2653 that these symbols perhaps be resolved dynamically, even though
2654 we should be resolving them to the current module. */
2655 if (!ignore_protected || h->type != STT_FUNC)
2656 binding_stays_local_p = TRUE;
2657 break;
2659 default:
2660 break;
2663 /* If it isn't defined locally, then clearly it's dynamic. */
2664 if (!h->def_regular)
2665 return TRUE;
2667 /* Otherwise, the symbol is dynamic if binding rules don't tell
2668 us that it remains local. */
2669 return !binding_stays_local_p;
2672 /* Return true if the symbol referred to by H should be considered
2673 to resolve local to the current module, and false otherwise. Differs
2674 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2675 undefined symbols and weak symbols. */
2677 bfd_boolean
2678 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2679 struct bfd_link_info *info,
2680 bfd_boolean local_protected)
2682 /* If it's a local sym, of course we resolve locally. */
2683 if (h == NULL)
2684 return TRUE;
2686 /* Common symbols that become definitions don't get the DEF_REGULAR
2687 flag set, so test it first, and don't bail out. */
2688 if (ELF_COMMON_DEF_P (h))
2689 /* Do nothing. */;
2690 /* If we don't have a definition in a regular file, then we can't
2691 resolve locally. The sym is either undefined or dynamic. */
2692 else if (!h->def_regular)
2693 return FALSE;
2695 /* Forced local symbols resolve locally. */
2696 if (h->forced_local)
2697 return TRUE;
2699 /* As do non-dynamic symbols. */
2700 if (h->dynindx == -1)
2701 return TRUE;
2703 /* At this point, we know the symbol is defined and dynamic. In an
2704 executable it must resolve locally, likewise when building symbolic
2705 shared libraries. */
2706 if (info->executable || SYMBOLIC_BIND (info, h))
2707 return TRUE;
2709 /* Now deal with defined dynamic symbols in shared libraries. Ones
2710 with default visibility might not resolve locally. */
2711 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2712 return FALSE;
2714 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2715 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2716 return TRUE;
2718 /* STV_PROTECTED non-function symbols are local. */
2719 if (h->type != STT_FUNC)
2720 return TRUE;
2722 /* Function pointer equality tests may require that STV_PROTECTED
2723 symbols be treated as dynamic symbols, even when we know that the
2724 dynamic linker will resolve them locally. */
2725 return local_protected;
2728 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2729 aligned. Returns the first TLS output section. */
2731 struct bfd_section *
2732 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2734 struct bfd_section *sec, *tls;
2735 unsigned int align = 0;
2737 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2738 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2739 break;
2740 tls = sec;
2742 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2743 if (sec->alignment_power > align)
2744 align = sec->alignment_power;
2746 elf_hash_table (info)->tls_sec = tls;
2748 /* Ensure the alignment of the first section is the largest alignment,
2749 so that the tls segment starts aligned. */
2750 if (tls != NULL)
2751 tls->alignment_power = align;
2753 return tls;
2756 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2757 static bfd_boolean
2758 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2759 Elf_Internal_Sym *sym)
2761 const struct elf_backend_data *bed;
2763 /* Local symbols do not count, but target specific ones might. */
2764 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2765 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2766 return FALSE;
2768 /* Function symbols do not count. */
2769 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2770 return FALSE;
2772 /* If the section is undefined, then so is the symbol. */
2773 if (sym->st_shndx == SHN_UNDEF)
2774 return FALSE;
2776 /* If the symbol is defined in the common section, then
2777 it is a common definition and so does not count. */
2778 bed = get_elf_backend_data (abfd);
2779 if (bed->common_definition (sym))
2780 return FALSE;
2782 /* If the symbol is in a target specific section then we
2783 must rely upon the backend to tell us what it is. */
2784 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2785 /* FIXME - this function is not coded yet:
2787 return _bfd_is_global_symbol_definition (abfd, sym);
2789 Instead for now assume that the definition is not global,
2790 Even if this is wrong, at least the linker will behave
2791 in the same way that it used to do. */
2792 return FALSE;
2794 return TRUE;
2797 /* Search the symbol table of the archive element of the archive ABFD
2798 whose archive map contains a mention of SYMDEF, and determine if
2799 the symbol is defined in this element. */
2800 static bfd_boolean
2801 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2803 Elf_Internal_Shdr * hdr;
2804 bfd_size_type symcount;
2805 bfd_size_type extsymcount;
2806 bfd_size_type extsymoff;
2807 Elf_Internal_Sym *isymbuf;
2808 Elf_Internal_Sym *isym;
2809 Elf_Internal_Sym *isymend;
2810 bfd_boolean result;
2812 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2813 if (abfd == NULL)
2814 return FALSE;
2816 if (! bfd_check_format (abfd, bfd_object))
2817 return FALSE;
2819 /* If we have already included the element containing this symbol in the
2820 link then we do not need to include it again. Just claim that any symbol
2821 it contains is not a definition, so that our caller will not decide to
2822 (re)include this element. */
2823 if (abfd->archive_pass)
2824 return FALSE;
2826 /* Select the appropriate symbol table. */
2827 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2828 hdr = &elf_tdata (abfd)->symtab_hdr;
2829 else
2830 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2832 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2834 /* The sh_info field of the symtab header tells us where the
2835 external symbols start. We don't care about the local symbols. */
2836 if (elf_bad_symtab (abfd))
2838 extsymcount = symcount;
2839 extsymoff = 0;
2841 else
2843 extsymcount = symcount - hdr->sh_info;
2844 extsymoff = hdr->sh_info;
2847 if (extsymcount == 0)
2848 return FALSE;
2850 /* Read in the symbol table. */
2851 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2852 NULL, NULL, NULL);
2853 if (isymbuf == NULL)
2854 return FALSE;
2856 /* Scan the symbol table looking for SYMDEF. */
2857 result = FALSE;
2858 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2860 const char *name;
2862 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2863 isym->st_name);
2864 if (name == NULL)
2865 break;
2867 if (strcmp (name, symdef->name) == 0)
2869 result = is_global_data_symbol_definition (abfd, isym);
2870 break;
2874 free (isymbuf);
2876 return result;
2879 /* Add an entry to the .dynamic table. */
2881 bfd_boolean
2882 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2883 bfd_vma tag,
2884 bfd_vma val)
2886 struct elf_link_hash_table *hash_table;
2887 const struct elf_backend_data *bed;
2888 asection *s;
2889 bfd_size_type newsize;
2890 bfd_byte *newcontents;
2891 Elf_Internal_Dyn dyn;
2893 hash_table = elf_hash_table (info);
2894 if (! is_elf_hash_table (hash_table))
2895 return FALSE;
2897 bed = get_elf_backend_data (hash_table->dynobj);
2898 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2899 BFD_ASSERT (s != NULL);
2901 newsize = s->size + bed->s->sizeof_dyn;
2902 newcontents = bfd_realloc (s->contents, newsize);
2903 if (newcontents == NULL)
2904 return FALSE;
2906 dyn.d_tag = tag;
2907 dyn.d_un.d_val = val;
2908 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2910 s->size = newsize;
2911 s->contents = newcontents;
2913 return TRUE;
2916 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2917 otherwise just check whether one already exists. Returns -1 on error,
2918 1 if a DT_NEEDED tag already exists, and 0 on success. */
2920 static int
2921 elf_add_dt_needed_tag (bfd *abfd,
2922 struct bfd_link_info *info,
2923 const char *soname,
2924 bfd_boolean do_it)
2926 struct elf_link_hash_table *hash_table;
2927 bfd_size_type oldsize;
2928 bfd_size_type strindex;
2930 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2931 return -1;
2933 hash_table = elf_hash_table (info);
2934 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2935 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2936 if (strindex == (bfd_size_type) -1)
2937 return -1;
2939 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2941 asection *sdyn;
2942 const struct elf_backend_data *bed;
2943 bfd_byte *extdyn;
2945 bed = get_elf_backend_data (hash_table->dynobj);
2946 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2947 if (sdyn != NULL)
2948 for (extdyn = sdyn->contents;
2949 extdyn < sdyn->contents + sdyn->size;
2950 extdyn += bed->s->sizeof_dyn)
2952 Elf_Internal_Dyn dyn;
2954 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2955 if (dyn.d_tag == DT_NEEDED
2956 && dyn.d_un.d_val == strindex)
2958 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2959 return 1;
2964 if (do_it)
2966 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2967 return -1;
2969 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2970 return -1;
2972 else
2973 /* We were just checking for existence of the tag. */
2974 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2976 return 0;
2979 /* Sort symbol by value and section. */
2980 static int
2981 elf_sort_symbol (const void *arg1, const void *arg2)
2983 const struct elf_link_hash_entry *h1;
2984 const struct elf_link_hash_entry *h2;
2985 bfd_signed_vma vdiff;
2987 h1 = *(const struct elf_link_hash_entry **) arg1;
2988 h2 = *(const struct elf_link_hash_entry **) arg2;
2989 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2990 if (vdiff != 0)
2991 return vdiff > 0 ? 1 : -1;
2992 else
2994 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2995 if (sdiff != 0)
2996 return sdiff > 0 ? 1 : -1;
2998 return 0;
3001 /* This function is used to adjust offsets into .dynstr for
3002 dynamic symbols. This is called via elf_link_hash_traverse. */
3004 static bfd_boolean
3005 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3007 struct elf_strtab_hash *dynstr = data;
3009 if (h->root.type == bfd_link_hash_warning)
3010 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3012 if (h->dynindx != -1)
3013 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3014 return TRUE;
3017 /* Assign string offsets in .dynstr, update all structures referencing
3018 them. */
3020 static bfd_boolean
3021 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3023 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3024 struct elf_link_local_dynamic_entry *entry;
3025 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3026 bfd *dynobj = hash_table->dynobj;
3027 asection *sdyn;
3028 bfd_size_type size;
3029 const struct elf_backend_data *bed;
3030 bfd_byte *extdyn;
3032 _bfd_elf_strtab_finalize (dynstr);
3033 size = _bfd_elf_strtab_size (dynstr);
3035 bed = get_elf_backend_data (dynobj);
3036 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3037 BFD_ASSERT (sdyn != NULL);
3039 /* Update all .dynamic entries referencing .dynstr strings. */
3040 for (extdyn = sdyn->contents;
3041 extdyn < sdyn->contents + sdyn->size;
3042 extdyn += bed->s->sizeof_dyn)
3044 Elf_Internal_Dyn dyn;
3046 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3047 switch (dyn.d_tag)
3049 case DT_STRSZ:
3050 dyn.d_un.d_val = size;
3051 break;
3052 case DT_NEEDED:
3053 case DT_SONAME:
3054 case DT_RPATH:
3055 case DT_RUNPATH:
3056 case DT_FILTER:
3057 case DT_AUXILIARY:
3058 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3059 break;
3060 default:
3061 continue;
3063 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3066 /* Now update local dynamic symbols. */
3067 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3068 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3069 entry->isym.st_name);
3071 /* And the rest of dynamic symbols. */
3072 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3074 /* Adjust version definitions. */
3075 if (elf_tdata (output_bfd)->cverdefs)
3077 asection *s;
3078 bfd_byte *p;
3079 bfd_size_type i;
3080 Elf_Internal_Verdef def;
3081 Elf_Internal_Verdaux defaux;
3083 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3084 p = s->contents;
3087 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3088 &def);
3089 p += sizeof (Elf_External_Verdef);
3090 if (def.vd_aux != sizeof (Elf_External_Verdef))
3091 continue;
3092 for (i = 0; i < def.vd_cnt; ++i)
3094 _bfd_elf_swap_verdaux_in (output_bfd,
3095 (Elf_External_Verdaux *) p, &defaux);
3096 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3097 defaux.vda_name);
3098 _bfd_elf_swap_verdaux_out (output_bfd,
3099 &defaux, (Elf_External_Verdaux *) p);
3100 p += sizeof (Elf_External_Verdaux);
3103 while (def.vd_next);
3106 /* Adjust version references. */
3107 if (elf_tdata (output_bfd)->verref)
3109 asection *s;
3110 bfd_byte *p;
3111 bfd_size_type i;
3112 Elf_Internal_Verneed need;
3113 Elf_Internal_Vernaux needaux;
3115 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3116 p = s->contents;
3119 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3120 &need);
3121 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3122 _bfd_elf_swap_verneed_out (output_bfd, &need,
3123 (Elf_External_Verneed *) p);
3124 p += sizeof (Elf_External_Verneed);
3125 for (i = 0; i < need.vn_cnt; ++i)
3127 _bfd_elf_swap_vernaux_in (output_bfd,
3128 (Elf_External_Vernaux *) p, &needaux);
3129 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3130 needaux.vna_name);
3131 _bfd_elf_swap_vernaux_out (output_bfd,
3132 &needaux,
3133 (Elf_External_Vernaux *) p);
3134 p += sizeof (Elf_External_Vernaux);
3137 while (need.vn_next);
3140 return TRUE;
3143 /* Add symbols from an ELF object file to the linker hash table. */
3145 static bfd_boolean
3146 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3148 Elf_Internal_Shdr *hdr;
3149 bfd_size_type symcount;
3150 bfd_size_type extsymcount;
3151 bfd_size_type extsymoff;
3152 struct elf_link_hash_entry **sym_hash;
3153 bfd_boolean dynamic;
3154 Elf_External_Versym *extversym = NULL;
3155 Elf_External_Versym *ever;
3156 struct elf_link_hash_entry *weaks;
3157 struct elf_link_hash_entry **nondeflt_vers = NULL;
3158 bfd_size_type nondeflt_vers_cnt = 0;
3159 Elf_Internal_Sym *isymbuf = NULL;
3160 Elf_Internal_Sym *isym;
3161 Elf_Internal_Sym *isymend;
3162 const struct elf_backend_data *bed;
3163 bfd_boolean add_needed;
3164 struct elf_link_hash_table *htab;
3165 bfd_size_type amt;
3166 void *alloc_mark = NULL;
3167 struct bfd_hash_entry **old_table = NULL;
3168 unsigned int old_size = 0;
3169 unsigned int old_count = 0;
3170 void *old_tab = NULL;
3171 void *old_hash;
3172 void *old_ent;
3173 struct bfd_link_hash_entry *old_undefs = NULL;
3174 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3175 long old_dynsymcount = 0;
3176 size_t tabsize = 0;
3177 size_t hashsize = 0;
3179 htab = elf_hash_table (info);
3180 bed = get_elf_backend_data (abfd);
3182 if ((abfd->flags & DYNAMIC) == 0)
3183 dynamic = FALSE;
3184 else
3186 dynamic = TRUE;
3188 /* You can't use -r against a dynamic object. Also, there's no
3189 hope of using a dynamic object which does not exactly match
3190 the format of the output file. */
3191 if (info->relocatable
3192 || !is_elf_hash_table (htab)
3193 || htab->root.creator != abfd->xvec)
3195 if (info->relocatable)
3196 bfd_set_error (bfd_error_invalid_operation);
3197 else
3198 bfd_set_error (bfd_error_wrong_format);
3199 goto error_return;
3203 /* As a GNU extension, any input sections which are named
3204 .gnu.warning.SYMBOL are treated as warning symbols for the given
3205 symbol. This differs from .gnu.warning sections, which generate
3206 warnings when they are included in an output file. */
3207 if (info->executable)
3209 asection *s;
3211 for (s = abfd->sections; s != NULL; s = s->next)
3213 const char *name;
3215 name = bfd_get_section_name (abfd, s);
3216 if (CONST_STRNEQ (name, ".gnu.warning."))
3218 char *msg;
3219 bfd_size_type sz;
3221 name += sizeof ".gnu.warning." - 1;
3223 /* If this is a shared object, then look up the symbol
3224 in the hash table. If it is there, and it is already
3225 been defined, then we will not be using the entry
3226 from this shared object, so we don't need to warn.
3227 FIXME: If we see the definition in a regular object
3228 later on, we will warn, but we shouldn't. The only
3229 fix is to keep track of what warnings we are supposed
3230 to emit, and then handle them all at the end of the
3231 link. */
3232 if (dynamic)
3234 struct elf_link_hash_entry *h;
3236 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3238 /* FIXME: What about bfd_link_hash_common? */
3239 if (h != NULL
3240 && (h->root.type == bfd_link_hash_defined
3241 || h->root.type == bfd_link_hash_defweak))
3243 /* We don't want to issue this warning. Clobber
3244 the section size so that the warning does not
3245 get copied into the output file. */
3246 s->size = 0;
3247 continue;
3251 sz = s->size;
3252 msg = bfd_alloc (abfd, sz + 1);
3253 if (msg == NULL)
3254 goto error_return;
3256 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3257 goto error_return;
3259 msg[sz] = '\0';
3261 if (! (_bfd_generic_link_add_one_symbol
3262 (info, abfd, name, BSF_WARNING, s, 0, msg,
3263 FALSE, bed->collect, NULL)))
3264 goto error_return;
3266 if (! info->relocatable)
3268 /* Clobber the section size so that the warning does
3269 not get copied into the output file. */
3270 s->size = 0;
3272 /* Also set SEC_EXCLUDE, so that symbols defined in
3273 the warning section don't get copied to the output. */
3274 s->flags |= SEC_EXCLUDE;
3280 add_needed = TRUE;
3281 if (! dynamic)
3283 /* If we are creating a shared library, create all the dynamic
3284 sections immediately. We need to attach them to something,
3285 so we attach them to this BFD, provided it is the right
3286 format. FIXME: If there are no input BFD's of the same
3287 format as the output, we can't make a shared library. */
3288 if (info->shared
3289 && is_elf_hash_table (htab)
3290 && htab->root.creator == abfd->xvec
3291 && !htab->dynamic_sections_created)
3293 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3294 goto error_return;
3297 else if (!is_elf_hash_table (htab))
3298 goto error_return;
3299 else
3301 asection *s;
3302 const char *soname = NULL;
3303 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3304 int ret;
3306 /* ld --just-symbols and dynamic objects don't mix very well.
3307 ld shouldn't allow it. */
3308 if ((s = abfd->sections) != NULL
3309 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3310 abort ();
3312 /* If this dynamic lib was specified on the command line with
3313 --as-needed in effect, then we don't want to add a DT_NEEDED
3314 tag unless the lib is actually used. Similary for libs brought
3315 in by another lib's DT_NEEDED. When --no-add-needed is used
3316 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3317 any dynamic library in DT_NEEDED tags in the dynamic lib at
3318 all. */
3319 add_needed = (elf_dyn_lib_class (abfd)
3320 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3321 | DYN_NO_NEEDED)) == 0;
3323 s = bfd_get_section_by_name (abfd, ".dynamic");
3324 if (s != NULL)
3326 bfd_byte *dynbuf;
3327 bfd_byte *extdyn;
3328 int elfsec;
3329 unsigned long shlink;
3331 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3332 goto error_free_dyn;
3334 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3335 if (elfsec == -1)
3336 goto error_free_dyn;
3337 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3339 for (extdyn = dynbuf;
3340 extdyn < dynbuf + s->size;
3341 extdyn += bed->s->sizeof_dyn)
3343 Elf_Internal_Dyn dyn;
3345 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3346 if (dyn.d_tag == DT_SONAME)
3348 unsigned int tagv = dyn.d_un.d_val;
3349 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3350 if (soname == NULL)
3351 goto error_free_dyn;
3353 if (dyn.d_tag == DT_NEEDED)
3355 struct bfd_link_needed_list *n, **pn;
3356 char *fnm, *anm;
3357 unsigned int tagv = dyn.d_un.d_val;
3359 amt = sizeof (struct bfd_link_needed_list);
3360 n = bfd_alloc (abfd, amt);
3361 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3362 if (n == NULL || fnm == NULL)
3363 goto error_free_dyn;
3364 amt = strlen (fnm) + 1;
3365 anm = bfd_alloc (abfd, amt);
3366 if (anm == NULL)
3367 goto error_free_dyn;
3368 memcpy (anm, fnm, amt);
3369 n->name = anm;
3370 n->by = abfd;
3371 n->next = NULL;
3372 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3374 *pn = n;
3376 if (dyn.d_tag == DT_RUNPATH)
3378 struct bfd_link_needed_list *n, **pn;
3379 char *fnm, *anm;
3380 unsigned int tagv = dyn.d_un.d_val;
3382 amt = sizeof (struct bfd_link_needed_list);
3383 n = bfd_alloc (abfd, amt);
3384 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3385 if (n == NULL || fnm == NULL)
3386 goto error_free_dyn;
3387 amt = strlen (fnm) + 1;
3388 anm = bfd_alloc (abfd, amt);
3389 if (anm == NULL)
3390 goto error_free_dyn;
3391 memcpy (anm, fnm, amt);
3392 n->name = anm;
3393 n->by = abfd;
3394 n->next = NULL;
3395 for (pn = & runpath;
3396 *pn != NULL;
3397 pn = &(*pn)->next)
3399 *pn = n;
3401 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3402 if (!runpath && dyn.d_tag == DT_RPATH)
3404 struct bfd_link_needed_list *n, **pn;
3405 char *fnm, *anm;
3406 unsigned int tagv = dyn.d_un.d_val;
3408 amt = sizeof (struct bfd_link_needed_list);
3409 n = bfd_alloc (abfd, amt);
3410 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3411 if (n == NULL || fnm == NULL)
3412 goto error_free_dyn;
3413 amt = strlen (fnm) + 1;
3414 anm = bfd_alloc (abfd, amt);
3415 if (anm == NULL)
3417 error_free_dyn:
3418 free (dynbuf);
3419 goto error_return;
3421 memcpy (anm, fnm, amt);
3422 n->name = anm;
3423 n->by = abfd;
3424 n->next = NULL;
3425 for (pn = & rpath;
3426 *pn != NULL;
3427 pn = &(*pn)->next)
3429 *pn = n;
3433 free (dynbuf);
3436 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3437 frees all more recently bfd_alloc'd blocks as well. */
3438 if (runpath)
3439 rpath = runpath;
3441 if (rpath)
3443 struct bfd_link_needed_list **pn;
3444 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3446 *pn = rpath;
3449 /* We do not want to include any of the sections in a dynamic
3450 object in the output file. We hack by simply clobbering the
3451 list of sections in the BFD. This could be handled more
3452 cleanly by, say, a new section flag; the existing
3453 SEC_NEVER_LOAD flag is not the one we want, because that one
3454 still implies that the section takes up space in the output
3455 file. */
3456 bfd_section_list_clear (abfd);
3458 /* Find the name to use in a DT_NEEDED entry that refers to this
3459 object. If the object has a DT_SONAME entry, we use it.
3460 Otherwise, if the generic linker stuck something in
3461 elf_dt_name, we use that. Otherwise, we just use the file
3462 name. */
3463 if (soname == NULL || *soname == '\0')
3465 soname = elf_dt_name (abfd);
3466 if (soname == NULL || *soname == '\0')
3467 soname = bfd_get_filename (abfd);
3470 /* Save the SONAME because sometimes the linker emulation code
3471 will need to know it. */
3472 elf_dt_name (abfd) = soname;
3474 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3475 if (ret < 0)
3476 goto error_return;
3478 /* If we have already included this dynamic object in the
3479 link, just ignore it. There is no reason to include a
3480 particular dynamic object more than once. */
3481 if (ret > 0)
3482 return TRUE;
3485 /* If this is a dynamic object, we always link against the .dynsym
3486 symbol table, not the .symtab symbol table. The dynamic linker
3487 will only see the .dynsym symbol table, so there is no reason to
3488 look at .symtab for a dynamic object. */
3490 if (! dynamic || elf_dynsymtab (abfd) == 0)
3491 hdr = &elf_tdata (abfd)->symtab_hdr;
3492 else
3493 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3495 symcount = hdr->sh_size / bed->s->sizeof_sym;
3497 /* The sh_info field of the symtab header tells us where the
3498 external symbols start. We don't care about the local symbols at
3499 this point. */
3500 if (elf_bad_symtab (abfd))
3502 extsymcount = symcount;
3503 extsymoff = 0;
3505 else
3507 extsymcount = symcount - hdr->sh_info;
3508 extsymoff = hdr->sh_info;
3511 sym_hash = NULL;
3512 if (extsymcount != 0)
3514 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3515 NULL, NULL, NULL);
3516 if (isymbuf == NULL)
3517 goto error_return;
3519 /* We store a pointer to the hash table entry for each external
3520 symbol. */
3521 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3522 sym_hash = bfd_alloc (abfd, amt);
3523 if (sym_hash == NULL)
3524 goto error_free_sym;
3525 elf_sym_hashes (abfd) = sym_hash;
3528 if (dynamic)
3530 /* Read in any version definitions. */
3531 if (!_bfd_elf_slurp_version_tables (abfd,
3532 info->default_imported_symver))
3533 goto error_free_sym;
3535 /* Read in the symbol versions, but don't bother to convert them
3536 to internal format. */
3537 if (elf_dynversym (abfd) != 0)
3539 Elf_Internal_Shdr *versymhdr;
3541 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3542 extversym = bfd_malloc (versymhdr->sh_size);
3543 if (extversym == NULL)
3544 goto error_free_sym;
3545 amt = versymhdr->sh_size;
3546 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3547 || bfd_bread (extversym, amt, abfd) != amt)
3548 goto error_free_vers;
3552 /* If we are loading an as-needed shared lib, save the symbol table
3553 state before we start adding symbols. If the lib turns out
3554 to be unneeded, restore the state. */
3555 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3557 unsigned int i;
3558 size_t entsize;
3560 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3562 struct bfd_hash_entry *p;
3563 struct elf_link_hash_entry *h;
3565 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3567 h = (struct elf_link_hash_entry *) p;
3568 entsize += htab->root.table.entsize;
3569 if (h->root.type == bfd_link_hash_warning)
3570 entsize += htab->root.table.entsize;
3574 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3575 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3576 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3577 if (old_tab == NULL)
3578 goto error_free_vers;
3580 /* Remember the current objalloc pointer, so that all mem for
3581 symbols added can later be reclaimed. */
3582 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3583 if (alloc_mark == NULL)
3584 goto error_free_vers;
3586 /* Make a special call to the linker "notice" function to
3587 tell it that we are about to handle an as-needed lib. */
3588 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3589 notice_as_needed))
3590 return FALSE;
3593 /* Clone the symbol table and sym hashes. Remember some
3594 pointers into the symbol table, and dynamic symbol count. */
3595 old_hash = (char *) old_tab + tabsize;
3596 old_ent = (char *) old_hash + hashsize;
3597 memcpy (old_tab, htab->root.table.table, tabsize);
3598 memcpy (old_hash, sym_hash, hashsize);
3599 old_undefs = htab->root.undefs;
3600 old_undefs_tail = htab->root.undefs_tail;
3601 old_table = htab->root.table.table;
3602 old_size = htab->root.table.size;
3603 old_count = htab->root.table.count;
3604 old_dynsymcount = htab->dynsymcount;
3606 for (i = 0; i < htab->root.table.size; i++)
3608 struct bfd_hash_entry *p;
3609 struct elf_link_hash_entry *h;
3611 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3613 memcpy (old_ent, p, htab->root.table.entsize);
3614 old_ent = (char *) old_ent + htab->root.table.entsize;
3615 h = (struct elf_link_hash_entry *) p;
3616 if (h->root.type == bfd_link_hash_warning)
3618 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3619 old_ent = (char *) old_ent + htab->root.table.entsize;
3625 weaks = NULL;
3626 ever = extversym != NULL ? extversym + extsymoff : NULL;
3627 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3628 isym < isymend;
3629 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3631 int bind;
3632 bfd_vma value;
3633 asection *sec, *new_sec;
3634 flagword flags;
3635 const char *name;
3636 struct elf_link_hash_entry *h;
3637 bfd_boolean definition;
3638 bfd_boolean size_change_ok;
3639 bfd_boolean type_change_ok;
3640 bfd_boolean new_weakdef;
3641 bfd_boolean override;
3642 bfd_boolean common;
3643 unsigned int old_alignment;
3644 bfd *old_bfd;
3646 override = FALSE;
3648 flags = BSF_NO_FLAGS;
3649 sec = NULL;
3650 value = isym->st_value;
3651 *sym_hash = NULL;
3652 common = bed->common_definition (isym);
3654 bind = ELF_ST_BIND (isym->st_info);
3655 if (bind == STB_LOCAL)
3657 /* This should be impossible, since ELF requires that all
3658 global symbols follow all local symbols, and that sh_info
3659 point to the first global symbol. Unfortunately, Irix 5
3660 screws this up. */
3661 continue;
3663 else if (bind == STB_GLOBAL)
3665 if (isym->st_shndx != SHN_UNDEF && !common)
3666 flags = BSF_GLOBAL;
3668 else if (bind == STB_WEAK)
3669 flags = BSF_WEAK;
3670 else
3672 /* Leave it up to the processor backend. */
3675 if (isym->st_shndx == SHN_UNDEF)
3676 sec = bfd_und_section_ptr;
3677 else if (isym->st_shndx < SHN_LORESERVE
3678 || isym->st_shndx > SHN_HIRESERVE)
3680 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3681 if (sec == NULL)
3682 sec = bfd_abs_section_ptr;
3683 else if (sec->kept_section)
3685 /* Symbols from discarded section are undefined. We keep
3686 its visibility. */
3687 sec = bfd_und_section_ptr;
3688 isym->st_shndx = SHN_UNDEF;
3690 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3691 value -= sec->vma;
3693 else if (isym->st_shndx == SHN_ABS)
3694 sec = bfd_abs_section_ptr;
3695 else if (isym->st_shndx == SHN_COMMON)
3697 sec = bfd_com_section_ptr;
3698 /* What ELF calls the size we call the value. What ELF
3699 calls the value we call the alignment. */
3700 value = isym->st_size;
3702 else
3704 /* Leave it up to the processor backend. */
3707 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3708 isym->st_name);
3709 if (name == NULL)
3710 goto error_free_vers;
3712 if (isym->st_shndx == SHN_COMMON
3713 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3714 && !info->relocatable)
3716 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3718 if (tcomm == NULL)
3720 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3721 (SEC_ALLOC
3722 | SEC_IS_COMMON
3723 | SEC_LINKER_CREATED
3724 | SEC_THREAD_LOCAL));
3725 if (tcomm == NULL)
3726 goto error_free_vers;
3728 sec = tcomm;
3730 else if (bed->elf_add_symbol_hook)
3732 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3733 &sec, &value))
3734 goto error_free_vers;
3736 /* The hook function sets the name to NULL if this symbol
3737 should be skipped for some reason. */
3738 if (name == NULL)
3739 continue;
3742 /* Sanity check that all possibilities were handled. */
3743 if (sec == NULL)
3745 bfd_set_error (bfd_error_bad_value);
3746 goto error_free_vers;
3749 if (bfd_is_und_section (sec)
3750 || bfd_is_com_section (sec))
3751 definition = FALSE;
3752 else
3753 definition = TRUE;
3755 size_change_ok = FALSE;
3756 type_change_ok = bed->type_change_ok;
3757 old_alignment = 0;
3758 old_bfd = NULL;
3759 new_sec = sec;
3761 if (is_elf_hash_table (htab))
3763 Elf_Internal_Versym iver;
3764 unsigned int vernum = 0;
3765 bfd_boolean skip;
3767 if (ever == NULL)
3769 if (info->default_imported_symver)
3770 /* Use the default symbol version created earlier. */
3771 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3772 else
3773 iver.vs_vers = 0;
3775 else
3776 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3778 vernum = iver.vs_vers & VERSYM_VERSION;
3780 /* If this is a hidden symbol, or if it is not version
3781 1, we append the version name to the symbol name.
3782 However, we do not modify a non-hidden absolute symbol
3783 if it is not a function, because it might be the version
3784 symbol itself. FIXME: What if it isn't? */
3785 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3786 || (vernum > 1 && (! bfd_is_abs_section (sec)
3787 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3789 const char *verstr;
3790 size_t namelen, verlen, newlen;
3791 char *newname, *p;
3793 if (isym->st_shndx != SHN_UNDEF)
3795 if (vernum > elf_tdata (abfd)->cverdefs)
3796 verstr = NULL;
3797 else if (vernum > 1)
3798 verstr =
3799 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3800 else
3801 verstr = "";
3803 if (verstr == NULL)
3805 (*_bfd_error_handler)
3806 (_("%B: %s: invalid version %u (max %d)"),
3807 abfd, name, vernum,
3808 elf_tdata (abfd)->cverdefs);
3809 bfd_set_error (bfd_error_bad_value);
3810 goto error_free_vers;
3813 else
3815 /* We cannot simply test for the number of
3816 entries in the VERNEED section since the
3817 numbers for the needed versions do not start
3818 at 0. */
3819 Elf_Internal_Verneed *t;
3821 verstr = NULL;
3822 for (t = elf_tdata (abfd)->verref;
3823 t != NULL;
3824 t = t->vn_nextref)
3826 Elf_Internal_Vernaux *a;
3828 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3830 if (a->vna_other == vernum)
3832 verstr = a->vna_nodename;
3833 break;
3836 if (a != NULL)
3837 break;
3839 if (verstr == NULL)
3841 (*_bfd_error_handler)
3842 (_("%B: %s: invalid needed version %d"),
3843 abfd, name, vernum);
3844 bfd_set_error (bfd_error_bad_value);
3845 goto error_free_vers;
3849 namelen = strlen (name);
3850 verlen = strlen (verstr);
3851 newlen = namelen + verlen + 2;
3852 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3853 && isym->st_shndx != SHN_UNDEF)
3854 ++newlen;
3856 newname = bfd_hash_allocate (&htab->root.table, newlen);
3857 if (newname == NULL)
3858 goto error_free_vers;
3859 memcpy (newname, name, namelen);
3860 p = newname + namelen;
3861 *p++ = ELF_VER_CHR;
3862 /* If this is a defined non-hidden version symbol,
3863 we add another @ to the name. This indicates the
3864 default version of the symbol. */
3865 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3866 && isym->st_shndx != SHN_UNDEF)
3867 *p++ = ELF_VER_CHR;
3868 memcpy (p, verstr, verlen + 1);
3870 name = newname;
3873 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3874 &value, &old_alignment,
3875 sym_hash, &skip, &override,
3876 &type_change_ok, &size_change_ok))
3877 goto error_free_vers;
3879 if (skip)
3880 continue;
3882 if (override)
3883 definition = FALSE;
3885 h = *sym_hash;
3886 while (h->root.type == bfd_link_hash_indirect
3887 || h->root.type == bfd_link_hash_warning)
3888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3890 /* Remember the old alignment if this is a common symbol, so
3891 that we don't reduce the alignment later on. We can't
3892 check later, because _bfd_generic_link_add_one_symbol
3893 will set a default for the alignment which we want to
3894 override. We also remember the old bfd where the existing
3895 definition comes from. */
3896 switch (h->root.type)
3898 default:
3899 break;
3901 case bfd_link_hash_defined:
3902 case bfd_link_hash_defweak:
3903 old_bfd = h->root.u.def.section->owner;
3904 break;
3906 case bfd_link_hash_common:
3907 old_bfd = h->root.u.c.p->section->owner;
3908 old_alignment = h->root.u.c.p->alignment_power;
3909 break;
3912 if (elf_tdata (abfd)->verdef != NULL
3913 && ! override
3914 && vernum > 1
3915 && definition)
3916 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3919 if (! (_bfd_generic_link_add_one_symbol
3920 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3921 (struct bfd_link_hash_entry **) sym_hash)))
3922 goto error_free_vers;
3924 h = *sym_hash;
3925 while (h->root.type == bfd_link_hash_indirect
3926 || h->root.type == bfd_link_hash_warning)
3927 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3928 *sym_hash = h;
3930 new_weakdef = FALSE;
3931 if (dynamic
3932 && definition
3933 && (flags & BSF_WEAK) != 0
3934 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3935 && is_elf_hash_table (htab)
3936 && h->u.weakdef == NULL)
3938 /* Keep a list of all weak defined non function symbols from
3939 a dynamic object, using the weakdef field. Later in this
3940 function we will set the weakdef field to the correct
3941 value. We only put non-function symbols from dynamic
3942 objects on this list, because that happens to be the only
3943 time we need to know the normal symbol corresponding to a
3944 weak symbol, and the information is time consuming to
3945 figure out. If the weakdef field is not already NULL,
3946 then this symbol was already defined by some previous
3947 dynamic object, and we will be using that previous
3948 definition anyhow. */
3950 h->u.weakdef = weaks;
3951 weaks = h;
3952 new_weakdef = TRUE;
3955 /* Set the alignment of a common symbol. */
3956 if ((common || bfd_is_com_section (sec))
3957 && h->root.type == bfd_link_hash_common)
3959 unsigned int align;
3961 if (common)
3962 align = bfd_log2 (isym->st_value);
3963 else
3965 /* The new symbol is a common symbol in a shared object.
3966 We need to get the alignment from the section. */
3967 align = new_sec->alignment_power;
3969 if (align > old_alignment
3970 /* Permit an alignment power of zero if an alignment of one
3971 is specified and no other alignments have been specified. */
3972 || (isym->st_value == 1 && old_alignment == 0))
3973 h->root.u.c.p->alignment_power = align;
3974 else
3975 h->root.u.c.p->alignment_power = old_alignment;
3978 if (is_elf_hash_table (htab))
3980 bfd_boolean dynsym;
3982 /* Check the alignment when a common symbol is involved. This
3983 can change when a common symbol is overridden by a normal
3984 definition or a common symbol is ignored due to the old
3985 normal definition. We need to make sure the maximum
3986 alignment is maintained. */
3987 if ((old_alignment || common)
3988 && h->root.type != bfd_link_hash_common)
3990 unsigned int common_align;
3991 unsigned int normal_align;
3992 unsigned int symbol_align;
3993 bfd *normal_bfd;
3994 bfd *common_bfd;
3996 symbol_align = ffs (h->root.u.def.value) - 1;
3997 if (h->root.u.def.section->owner != NULL
3998 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4000 normal_align = h->root.u.def.section->alignment_power;
4001 if (normal_align > symbol_align)
4002 normal_align = symbol_align;
4004 else
4005 normal_align = symbol_align;
4007 if (old_alignment)
4009 common_align = old_alignment;
4010 common_bfd = old_bfd;
4011 normal_bfd = abfd;
4013 else
4015 common_align = bfd_log2 (isym->st_value);
4016 common_bfd = abfd;
4017 normal_bfd = old_bfd;
4020 if (normal_align < common_align)
4022 /* PR binutils/2735 */
4023 if (normal_bfd == NULL)
4024 (*_bfd_error_handler)
4025 (_("Warning: alignment %u of common symbol `%s' in %B"
4026 " is greater than the alignment (%u) of its section %A"),
4027 common_bfd, h->root.u.def.section,
4028 1 << common_align, name, 1 << normal_align);
4029 else
4030 (*_bfd_error_handler)
4031 (_("Warning: alignment %u of symbol `%s' in %B"
4032 " is smaller than %u in %B"),
4033 normal_bfd, common_bfd,
4034 1 << normal_align, name, 1 << common_align);
4038 /* Remember the symbol size and type. */
4039 if (isym->st_size != 0
4040 && (definition || h->size == 0))
4042 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
4043 (*_bfd_error_handler)
4044 (_("Warning: size of symbol `%s' changed"
4045 " from %lu in %B to %lu in %B"),
4046 old_bfd, abfd,
4047 name, (unsigned long) h->size,
4048 (unsigned long) isym->st_size);
4050 h->size = isym->st_size;
4053 /* If this is a common symbol, then we always want H->SIZE
4054 to be the size of the common symbol. The code just above
4055 won't fix the size if a common symbol becomes larger. We
4056 don't warn about a size change here, because that is
4057 covered by --warn-common. */
4058 if (h->root.type == bfd_link_hash_common)
4059 h->size = h->root.u.c.size;
4061 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4062 && (definition || h->type == STT_NOTYPE))
4064 if (h->type != STT_NOTYPE
4065 && h->type != ELF_ST_TYPE (isym->st_info)
4066 && ! type_change_ok)
4067 (*_bfd_error_handler)
4068 (_("Warning: type of symbol `%s' changed"
4069 " from %d to %d in %B"),
4070 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4072 h->type = ELF_ST_TYPE (isym->st_info);
4075 /* If st_other has a processor-specific meaning, specific
4076 code might be needed here. We never merge the visibility
4077 attribute with the one from a dynamic object. */
4078 if (bed->elf_backend_merge_symbol_attribute)
4079 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4080 dynamic);
4082 /* If this symbol has default visibility and the user has requested
4083 we not re-export it, then mark it as hidden. */
4084 if (definition && !dynamic
4085 && (abfd->no_export
4086 || (abfd->my_archive && abfd->my_archive->no_export))
4087 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4088 isym->st_other = (STV_HIDDEN
4089 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4091 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4093 unsigned char hvis, symvis, other, nvis;
4095 /* Only merge the visibility. Leave the remainder of the
4096 st_other field to elf_backend_merge_symbol_attribute. */
4097 other = h->other & ~ELF_ST_VISIBILITY (-1);
4099 /* Combine visibilities, using the most constraining one. */
4100 hvis = ELF_ST_VISIBILITY (h->other);
4101 symvis = ELF_ST_VISIBILITY (isym->st_other);
4102 if (! hvis)
4103 nvis = symvis;
4104 else if (! symvis)
4105 nvis = hvis;
4106 else
4107 nvis = hvis < symvis ? hvis : symvis;
4109 h->other = other | nvis;
4112 /* Set a flag in the hash table entry indicating the type of
4113 reference or definition we just found. Keep a count of
4114 the number of dynamic symbols we find. A dynamic symbol
4115 is one which is referenced or defined by both a regular
4116 object and a shared object. */
4117 dynsym = FALSE;
4118 if (! dynamic)
4120 if (! definition)
4122 h->ref_regular = 1;
4123 if (bind != STB_WEAK)
4124 h->ref_regular_nonweak = 1;
4126 else
4127 h->def_regular = 1;
4128 if (! info->executable
4129 || h->def_dynamic
4130 || h->ref_dynamic)
4131 dynsym = TRUE;
4133 else
4135 if (! definition)
4136 h->ref_dynamic = 1;
4137 else
4138 h->def_dynamic = 1;
4139 if (h->def_regular
4140 || h->ref_regular
4141 || (h->u.weakdef != NULL
4142 && ! new_weakdef
4143 && h->u.weakdef->dynindx != -1))
4144 dynsym = TRUE;
4147 if (definition && (sec->flags & SEC_DEBUGGING))
4149 /* We don't want to make debug symbol dynamic. */
4150 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4151 dynsym = FALSE;
4154 /* Check to see if we need to add an indirect symbol for
4155 the default name. */
4156 if (definition || h->root.type == bfd_link_hash_common)
4157 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4158 &sec, &value, &dynsym,
4159 override))
4160 goto error_free_vers;
4162 if (definition && !dynamic)
4164 char *p = strchr (name, ELF_VER_CHR);
4165 if (p != NULL && p[1] != ELF_VER_CHR)
4167 /* Queue non-default versions so that .symver x, x@FOO
4168 aliases can be checked. */
4169 if (!nondeflt_vers)
4171 amt = ((isymend - isym + 1)
4172 * sizeof (struct elf_link_hash_entry *));
4173 nondeflt_vers = bfd_malloc (amt);
4175 nondeflt_vers[nondeflt_vers_cnt++] = h;
4179 if (dynsym && h->dynindx == -1)
4181 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4182 goto error_free_vers;
4183 if (h->u.weakdef != NULL
4184 && ! new_weakdef
4185 && h->u.weakdef->dynindx == -1)
4187 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4188 goto error_free_vers;
4191 else if (dynsym && h->dynindx != -1)
4192 /* If the symbol already has a dynamic index, but
4193 visibility says it should not be visible, turn it into
4194 a local symbol. */
4195 switch (ELF_ST_VISIBILITY (h->other))
4197 case STV_INTERNAL:
4198 case STV_HIDDEN:
4199 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4200 dynsym = FALSE;
4201 break;
4204 if (!add_needed
4205 && definition
4206 && dynsym
4207 && h->ref_regular)
4209 int ret;
4210 const char *soname = elf_dt_name (abfd);
4212 /* A symbol from a library loaded via DT_NEEDED of some
4213 other library is referenced by a regular object.
4214 Add a DT_NEEDED entry for it. Issue an error if
4215 --no-add-needed is used. */
4216 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4218 (*_bfd_error_handler)
4219 (_("%s: invalid DSO for symbol `%s' definition"),
4220 abfd, name);
4221 bfd_set_error (bfd_error_bad_value);
4222 goto error_free_vers;
4225 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4227 add_needed = TRUE;
4228 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4229 if (ret < 0)
4230 goto error_free_vers;
4232 BFD_ASSERT (ret == 0);
4237 if (extversym != NULL)
4239 free (extversym);
4240 extversym = NULL;
4243 if (isymbuf != NULL)
4245 free (isymbuf);
4246 isymbuf = NULL;
4249 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4251 unsigned int i;
4253 /* Restore the symbol table. */
4254 old_hash = (char *) old_tab + tabsize;
4255 old_ent = (char *) old_hash + hashsize;
4256 sym_hash = elf_sym_hashes (abfd);
4257 htab->root.table.table = old_table;
4258 htab->root.table.size = old_size;
4259 htab->root.table.count = old_count;
4260 memcpy (htab->root.table.table, old_tab, tabsize);
4261 memcpy (sym_hash, old_hash, hashsize);
4262 htab->root.undefs = old_undefs;
4263 htab->root.undefs_tail = old_undefs_tail;
4264 for (i = 0; i < htab->root.table.size; i++)
4266 struct bfd_hash_entry *p;
4267 struct elf_link_hash_entry *h;
4269 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4271 h = (struct elf_link_hash_entry *) p;
4272 if (h->root.type == bfd_link_hash_warning)
4273 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4274 if (h->dynindx >= old_dynsymcount)
4275 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4277 memcpy (p, old_ent, htab->root.table.entsize);
4278 old_ent = (char *) old_ent + htab->root.table.entsize;
4279 h = (struct elf_link_hash_entry *) p;
4280 if (h->root.type == bfd_link_hash_warning)
4282 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4283 old_ent = (char *) old_ent + htab->root.table.entsize;
4288 /* Make a special call to the linker "notice" function to
4289 tell it that symbols added for crefs may need to be removed. */
4290 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4291 notice_not_needed))
4292 return FALSE;
4294 free (old_tab);
4295 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4296 alloc_mark);
4297 if (nondeflt_vers != NULL)
4298 free (nondeflt_vers);
4299 return TRUE;
4302 if (old_tab != NULL)
4304 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4305 notice_needed))
4306 return FALSE;
4307 free (old_tab);
4308 old_tab = NULL;
4311 /* Now that all the symbols from this input file are created, handle
4312 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4313 if (nondeflt_vers != NULL)
4315 bfd_size_type cnt, symidx;
4317 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4319 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4320 char *shortname, *p;
4322 p = strchr (h->root.root.string, ELF_VER_CHR);
4323 if (p == NULL
4324 || (h->root.type != bfd_link_hash_defined
4325 && h->root.type != bfd_link_hash_defweak))
4326 continue;
4328 amt = p - h->root.root.string;
4329 shortname = bfd_malloc (amt + 1);
4330 memcpy (shortname, h->root.root.string, amt);
4331 shortname[amt] = '\0';
4333 hi = (struct elf_link_hash_entry *)
4334 bfd_link_hash_lookup (&htab->root, shortname,
4335 FALSE, FALSE, FALSE);
4336 if (hi != NULL
4337 && hi->root.type == h->root.type
4338 && hi->root.u.def.value == h->root.u.def.value
4339 && hi->root.u.def.section == h->root.u.def.section)
4341 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4342 hi->root.type = bfd_link_hash_indirect;
4343 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4344 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4345 sym_hash = elf_sym_hashes (abfd);
4346 if (sym_hash)
4347 for (symidx = 0; symidx < extsymcount; ++symidx)
4348 if (sym_hash[symidx] == hi)
4350 sym_hash[symidx] = h;
4351 break;
4354 free (shortname);
4356 free (nondeflt_vers);
4357 nondeflt_vers = NULL;
4360 /* Now set the weakdefs field correctly for all the weak defined
4361 symbols we found. The only way to do this is to search all the
4362 symbols. Since we only need the information for non functions in
4363 dynamic objects, that's the only time we actually put anything on
4364 the list WEAKS. We need this information so that if a regular
4365 object refers to a symbol defined weakly in a dynamic object, the
4366 real symbol in the dynamic object is also put in the dynamic
4367 symbols; we also must arrange for both symbols to point to the
4368 same memory location. We could handle the general case of symbol
4369 aliasing, but a general symbol alias can only be generated in
4370 assembler code, handling it correctly would be very time
4371 consuming, and other ELF linkers don't handle general aliasing
4372 either. */
4373 if (weaks != NULL)
4375 struct elf_link_hash_entry **hpp;
4376 struct elf_link_hash_entry **hppend;
4377 struct elf_link_hash_entry **sorted_sym_hash;
4378 struct elf_link_hash_entry *h;
4379 size_t sym_count;
4381 /* Since we have to search the whole symbol list for each weak
4382 defined symbol, search time for N weak defined symbols will be
4383 O(N^2). Binary search will cut it down to O(NlogN). */
4384 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4385 sorted_sym_hash = bfd_malloc (amt);
4386 if (sorted_sym_hash == NULL)
4387 goto error_return;
4388 sym_hash = sorted_sym_hash;
4389 hpp = elf_sym_hashes (abfd);
4390 hppend = hpp + extsymcount;
4391 sym_count = 0;
4392 for (; hpp < hppend; hpp++)
4394 h = *hpp;
4395 if (h != NULL
4396 && h->root.type == bfd_link_hash_defined
4397 && h->type != STT_FUNC)
4399 *sym_hash = h;
4400 sym_hash++;
4401 sym_count++;
4405 qsort (sorted_sym_hash, sym_count,
4406 sizeof (struct elf_link_hash_entry *),
4407 elf_sort_symbol);
4409 while (weaks != NULL)
4411 struct elf_link_hash_entry *hlook;
4412 asection *slook;
4413 bfd_vma vlook;
4414 long ilook;
4415 size_t i, j, idx;
4417 hlook = weaks;
4418 weaks = hlook->u.weakdef;
4419 hlook->u.weakdef = NULL;
4421 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4422 || hlook->root.type == bfd_link_hash_defweak
4423 || hlook->root.type == bfd_link_hash_common
4424 || hlook->root.type == bfd_link_hash_indirect);
4425 slook = hlook->root.u.def.section;
4426 vlook = hlook->root.u.def.value;
4428 ilook = -1;
4429 i = 0;
4430 j = sym_count;
4431 while (i < j)
4433 bfd_signed_vma vdiff;
4434 idx = (i + j) / 2;
4435 h = sorted_sym_hash [idx];
4436 vdiff = vlook - h->root.u.def.value;
4437 if (vdiff < 0)
4438 j = idx;
4439 else if (vdiff > 0)
4440 i = idx + 1;
4441 else
4443 long sdiff = slook->id - h->root.u.def.section->id;
4444 if (sdiff < 0)
4445 j = idx;
4446 else if (sdiff > 0)
4447 i = idx + 1;
4448 else
4450 ilook = idx;
4451 break;
4456 /* We didn't find a value/section match. */
4457 if (ilook == -1)
4458 continue;
4460 for (i = ilook; i < sym_count; i++)
4462 h = sorted_sym_hash [i];
4464 /* Stop if value or section doesn't match. */
4465 if (h->root.u.def.value != vlook
4466 || h->root.u.def.section != slook)
4467 break;
4468 else if (h != hlook)
4470 hlook->u.weakdef = h;
4472 /* If the weak definition is in the list of dynamic
4473 symbols, make sure the real definition is put
4474 there as well. */
4475 if (hlook->dynindx != -1 && h->dynindx == -1)
4477 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4478 goto error_return;
4481 /* If the real definition is in the list of dynamic
4482 symbols, make sure the weak definition is put
4483 there as well. If we don't do this, then the
4484 dynamic loader might not merge the entries for the
4485 real definition and the weak definition. */
4486 if (h->dynindx != -1 && hlook->dynindx == -1)
4488 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4489 goto error_return;
4491 break;
4496 free (sorted_sym_hash);
4499 if (bed->check_directives)
4500 (*bed->check_directives) (abfd, info);
4502 /* If this object is the same format as the output object, and it is
4503 not a shared library, then let the backend look through the
4504 relocs.
4506 This is required to build global offset table entries and to
4507 arrange for dynamic relocs. It is not required for the
4508 particular common case of linking non PIC code, even when linking
4509 against shared libraries, but unfortunately there is no way of
4510 knowing whether an object file has been compiled PIC or not.
4511 Looking through the relocs is not particularly time consuming.
4512 The problem is that we must either (1) keep the relocs in memory,
4513 which causes the linker to require additional runtime memory or
4514 (2) read the relocs twice from the input file, which wastes time.
4515 This would be a good case for using mmap.
4517 I have no idea how to handle linking PIC code into a file of a
4518 different format. It probably can't be done. */
4519 if (! dynamic
4520 && is_elf_hash_table (htab)
4521 && htab->root.creator == abfd->xvec
4522 && bed->check_relocs != NULL)
4524 asection *o;
4526 for (o = abfd->sections; o != NULL; o = o->next)
4528 Elf_Internal_Rela *internal_relocs;
4529 bfd_boolean ok;
4531 if ((o->flags & SEC_RELOC) == 0
4532 || o->reloc_count == 0
4533 || ((info->strip == strip_all || info->strip == strip_debugger)
4534 && (o->flags & SEC_DEBUGGING) != 0)
4535 || bfd_is_abs_section (o->output_section))
4536 continue;
4538 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4539 info->keep_memory);
4540 if (internal_relocs == NULL)
4541 goto error_return;
4543 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4545 if (elf_section_data (o)->relocs != internal_relocs)
4546 free (internal_relocs);
4548 if (! ok)
4549 goto error_return;
4553 /* If this is a non-traditional link, try to optimize the handling
4554 of the .stab/.stabstr sections. */
4555 if (! dynamic
4556 && ! info->traditional_format
4557 && is_elf_hash_table (htab)
4558 && (info->strip != strip_all && info->strip != strip_debugger))
4560 asection *stabstr;
4562 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4563 if (stabstr != NULL)
4565 bfd_size_type string_offset = 0;
4566 asection *stab;
4568 for (stab = abfd->sections; stab; stab = stab->next)
4569 if (CONST_STRNEQ (stab->name, ".stab")
4570 && (!stab->name[5] ||
4571 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4572 && (stab->flags & SEC_MERGE) == 0
4573 && !bfd_is_abs_section (stab->output_section))
4575 struct bfd_elf_section_data *secdata;
4577 secdata = elf_section_data (stab);
4578 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4579 stabstr, &secdata->sec_info,
4580 &string_offset))
4581 goto error_return;
4582 if (secdata->sec_info)
4583 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4588 if (is_elf_hash_table (htab) && add_needed)
4590 /* Add this bfd to the loaded list. */
4591 struct elf_link_loaded_list *n;
4593 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4594 if (n == NULL)
4595 goto error_return;
4596 n->abfd = abfd;
4597 n->next = htab->loaded;
4598 htab->loaded = n;
4601 return TRUE;
4603 error_free_vers:
4604 if (old_tab != NULL)
4605 free (old_tab);
4606 if (nondeflt_vers != NULL)
4607 free (nondeflt_vers);
4608 if (extversym != NULL)
4609 free (extversym);
4610 error_free_sym:
4611 if (isymbuf != NULL)
4612 free (isymbuf);
4613 error_return:
4614 return FALSE;
4617 /* Return the linker hash table entry of a symbol that might be
4618 satisfied by an archive symbol. Return -1 on error. */
4620 struct elf_link_hash_entry *
4621 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4622 struct bfd_link_info *info,
4623 const char *name)
4625 struct elf_link_hash_entry *h;
4626 char *p, *copy;
4627 size_t len, first;
4629 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4630 if (h != NULL)
4631 return h;
4633 /* If this is a default version (the name contains @@), look up the
4634 symbol again with only one `@' as well as without the version.
4635 The effect is that references to the symbol with and without the
4636 version will be matched by the default symbol in the archive. */
4638 p = strchr (name, ELF_VER_CHR);
4639 if (p == NULL || p[1] != ELF_VER_CHR)
4640 return h;
4642 /* First check with only one `@'. */
4643 len = strlen (name);
4644 copy = bfd_alloc (abfd, len);
4645 if (copy == NULL)
4646 return (struct elf_link_hash_entry *) 0 - 1;
4648 first = p - name + 1;
4649 memcpy (copy, name, first);
4650 memcpy (copy + first, name + first + 1, len - first);
4652 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4653 if (h == NULL)
4655 /* We also need to check references to the symbol without the
4656 version. */
4657 copy[first - 1] = '\0';
4658 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4659 FALSE, FALSE, FALSE);
4662 bfd_release (abfd, copy);
4663 return h;
4666 /* Add symbols from an ELF archive file to the linker hash table. We
4667 don't use _bfd_generic_link_add_archive_symbols because of a
4668 problem which arises on UnixWare. The UnixWare libc.so is an
4669 archive which includes an entry libc.so.1 which defines a bunch of
4670 symbols. The libc.so archive also includes a number of other
4671 object files, which also define symbols, some of which are the same
4672 as those defined in libc.so.1. Correct linking requires that we
4673 consider each object file in turn, and include it if it defines any
4674 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4675 this; it looks through the list of undefined symbols, and includes
4676 any object file which defines them. When this algorithm is used on
4677 UnixWare, it winds up pulling in libc.so.1 early and defining a
4678 bunch of symbols. This means that some of the other objects in the
4679 archive are not included in the link, which is incorrect since they
4680 precede libc.so.1 in the archive.
4682 Fortunately, ELF archive handling is simpler than that done by
4683 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4684 oddities. In ELF, if we find a symbol in the archive map, and the
4685 symbol is currently undefined, we know that we must pull in that
4686 object file.
4688 Unfortunately, we do have to make multiple passes over the symbol
4689 table until nothing further is resolved. */
4691 static bfd_boolean
4692 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4694 symindex c;
4695 bfd_boolean *defined = NULL;
4696 bfd_boolean *included = NULL;
4697 carsym *symdefs;
4698 bfd_boolean loop;
4699 bfd_size_type amt;
4700 const struct elf_backend_data *bed;
4701 struct elf_link_hash_entry * (*archive_symbol_lookup)
4702 (bfd *, struct bfd_link_info *, const char *);
4704 if (! bfd_has_map (abfd))
4706 /* An empty archive is a special case. */
4707 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4708 return TRUE;
4709 bfd_set_error (bfd_error_no_armap);
4710 return FALSE;
4713 /* Keep track of all symbols we know to be already defined, and all
4714 files we know to be already included. This is to speed up the
4715 second and subsequent passes. */
4716 c = bfd_ardata (abfd)->symdef_count;
4717 if (c == 0)
4718 return TRUE;
4719 amt = c;
4720 amt *= sizeof (bfd_boolean);
4721 defined = bfd_zmalloc (amt);
4722 included = bfd_zmalloc (amt);
4723 if (defined == NULL || included == NULL)
4724 goto error_return;
4726 symdefs = bfd_ardata (abfd)->symdefs;
4727 bed = get_elf_backend_data (abfd);
4728 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4732 file_ptr last;
4733 symindex i;
4734 carsym *symdef;
4735 carsym *symdefend;
4737 loop = FALSE;
4738 last = -1;
4740 symdef = symdefs;
4741 symdefend = symdef + c;
4742 for (i = 0; symdef < symdefend; symdef++, i++)
4744 struct elf_link_hash_entry *h;
4745 bfd *element;
4746 struct bfd_link_hash_entry *undefs_tail;
4747 symindex mark;
4749 if (defined[i] || included[i])
4750 continue;
4751 if (symdef->file_offset == last)
4753 included[i] = TRUE;
4754 continue;
4757 h = archive_symbol_lookup (abfd, info, symdef->name);
4758 if (h == (struct elf_link_hash_entry *) 0 - 1)
4759 goto error_return;
4761 if (h == NULL)
4762 continue;
4764 if (h->root.type == bfd_link_hash_common)
4766 /* We currently have a common symbol. The archive map contains
4767 a reference to this symbol, so we may want to include it. We
4768 only want to include it however, if this archive element
4769 contains a definition of the symbol, not just another common
4770 declaration of it.
4772 Unfortunately some archivers (including GNU ar) will put
4773 declarations of common symbols into their archive maps, as
4774 well as real definitions, so we cannot just go by the archive
4775 map alone. Instead we must read in the element's symbol
4776 table and check that to see what kind of symbol definition
4777 this is. */
4778 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4779 continue;
4781 else if (h->root.type != bfd_link_hash_undefined)
4783 if (h->root.type != bfd_link_hash_undefweak)
4784 defined[i] = TRUE;
4785 continue;
4788 /* We need to include this archive member. */
4789 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4790 if (element == NULL)
4791 goto error_return;
4793 if (! bfd_check_format (element, bfd_object))
4794 goto error_return;
4796 /* Doublecheck that we have not included this object
4797 already--it should be impossible, but there may be
4798 something wrong with the archive. */
4799 if (element->archive_pass != 0)
4801 bfd_set_error (bfd_error_bad_value);
4802 goto error_return;
4804 element->archive_pass = 1;
4806 undefs_tail = info->hash->undefs_tail;
4808 if (! (*info->callbacks->add_archive_element) (info, element,
4809 symdef->name))
4810 goto error_return;
4811 if (! bfd_link_add_symbols (element, info))
4812 goto error_return;
4814 /* If there are any new undefined symbols, we need to make
4815 another pass through the archive in order to see whether
4816 they can be defined. FIXME: This isn't perfect, because
4817 common symbols wind up on undefs_tail and because an
4818 undefined symbol which is defined later on in this pass
4819 does not require another pass. This isn't a bug, but it
4820 does make the code less efficient than it could be. */
4821 if (undefs_tail != info->hash->undefs_tail)
4822 loop = TRUE;
4824 /* Look backward to mark all symbols from this object file
4825 which we have already seen in this pass. */
4826 mark = i;
4829 included[mark] = TRUE;
4830 if (mark == 0)
4831 break;
4832 --mark;
4834 while (symdefs[mark].file_offset == symdef->file_offset);
4836 /* We mark subsequent symbols from this object file as we go
4837 on through the loop. */
4838 last = symdef->file_offset;
4841 while (loop);
4843 free (defined);
4844 free (included);
4846 return TRUE;
4848 error_return:
4849 if (defined != NULL)
4850 free (defined);
4851 if (included != NULL)
4852 free (included);
4853 return FALSE;
4856 /* Given an ELF BFD, add symbols to the global hash table as
4857 appropriate. */
4859 bfd_boolean
4860 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4862 switch (bfd_get_format (abfd))
4864 case bfd_object:
4865 return elf_link_add_object_symbols (abfd, info);
4866 case bfd_archive:
4867 return elf_link_add_archive_symbols (abfd, info);
4868 default:
4869 bfd_set_error (bfd_error_wrong_format);
4870 return FALSE;
4874 /* This function will be called though elf_link_hash_traverse to store
4875 all hash value of the exported symbols in an array. */
4877 static bfd_boolean
4878 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4880 unsigned long **valuep = data;
4881 const char *name;
4882 char *p;
4883 unsigned long ha;
4884 char *alc = NULL;
4886 if (h->root.type == bfd_link_hash_warning)
4887 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4889 /* Ignore indirect symbols. These are added by the versioning code. */
4890 if (h->dynindx == -1)
4891 return TRUE;
4893 name = h->root.root.string;
4894 p = strchr (name, ELF_VER_CHR);
4895 if (p != NULL)
4897 alc = bfd_malloc (p - name + 1);
4898 memcpy (alc, name, p - name);
4899 alc[p - name] = '\0';
4900 name = alc;
4903 /* Compute the hash value. */
4904 ha = bfd_elf_hash (name);
4906 /* Store the found hash value in the array given as the argument. */
4907 *(*valuep)++ = ha;
4909 /* And store it in the struct so that we can put it in the hash table
4910 later. */
4911 h->u.elf_hash_value = ha;
4913 if (alc != NULL)
4914 free (alc);
4916 return TRUE;
4919 struct collect_gnu_hash_codes
4921 bfd *output_bfd;
4922 const struct elf_backend_data *bed;
4923 unsigned long int nsyms;
4924 unsigned long int maskbits;
4925 unsigned long int *hashcodes;
4926 unsigned long int *hashval;
4927 unsigned long int *indx;
4928 unsigned long int *counts;
4929 bfd_vma *bitmask;
4930 bfd_byte *contents;
4931 long int min_dynindx;
4932 unsigned long int bucketcount;
4933 unsigned long int symindx;
4934 long int local_indx;
4935 long int shift1, shift2;
4936 unsigned long int mask;
4939 /* This function will be called though elf_link_hash_traverse to store
4940 all hash value of the exported symbols in an array. */
4942 static bfd_boolean
4943 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
4945 struct collect_gnu_hash_codes *s = data;
4946 const char *name;
4947 char *p;
4948 unsigned long ha;
4949 char *alc = NULL;
4951 if (h->root.type == bfd_link_hash_warning)
4952 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4954 /* Ignore indirect symbols. These are added by the versioning code. */
4955 if (h->dynindx == -1)
4956 return TRUE;
4958 /* Ignore also local symbols and undefined symbols. */
4959 if (! (*s->bed->elf_hash_symbol) (h))
4960 return TRUE;
4962 name = h->root.root.string;
4963 p = strchr (name, ELF_VER_CHR);
4964 if (p != NULL)
4966 alc = bfd_malloc (p - name + 1);
4967 memcpy (alc, name, p - name);
4968 alc[p - name] = '\0';
4969 name = alc;
4972 /* Compute the hash value. */
4973 ha = bfd_elf_gnu_hash (name);
4975 /* Store the found hash value in the array for compute_bucket_count,
4976 and also for .dynsym reordering purposes. */
4977 s->hashcodes[s->nsyms] = ha;
4978 s->hashval[h->dynindx] = ha;
4979 ++s->nsyms;
4980 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
4981 s->min_dynindx = h->dynindx;
4983 if (alc != NULL)
4984 free (alc);
4986 return TRUE;
4989 /* This function will be called though elf_link_hash_traverse to do
4990 final dynaminc symbol renumbering. */
4992 static bfd_boolean
4993 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
4995 struct collect_gnu_hash_codes *s = data;
4996 unsigned long int bucket;
4997 unsigned long int val;
4999 if (h->root.type == bfd_link_hash_warning)
5000 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5002 /* Ignore indirect symbols. */
5003 if (h->dynindx == -1)
5004 return TRUE;
5006 /* Ignore also local symbols and undefined symbols. */
5007 if (! (*s->bed->elf_hash_symbol) (h))
5009 if (h->dynindx >= s->min_dynindx)
5010 h->dynindx = s->local_indx++;
5011 return TRUE;
5014 bucket = s->hashval[h->dynindx] % s->bucketcount;
5015 val = (s->hashval[h->dynindx] >> s->shift1)
5016 & ((s->maskbits >> s->shift1) - 1);
5017 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5018 s->bitmask[val]
5019 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5020 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5021 if (s->counts[bucket] == 1)
5022 /* Last element terminates the chain. */
5023 val |= 1;
5024 bfd_put_32 (s->output_bfd, val,
5025 s->contents + (s->indx[bucket] - s->symindx) * 4);
5026 --s->counts[bucket];
5027 h->dynindx = s->indx[bucket]++;
5028 return TRUE;
5031 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5033 bfd_boolean
5034 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5036 return !(h->forced_local
5037 || h->root.type == bfd_link_hash_undefined
5038 || h->root.type == bfd_link_hash_undefweak
5039 || ((h->root.type == bfd_link_hash_defined
5040 || h->root.type == bfd_link_hash_defweak)
5041 && h->root.u.def.section->output_section == NULL));
5044 /* Array used to determine the number of hash table buckets to use
5045 based on the number of symbols there are. If there are fewer than
5046 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5047 fewer than 37 we use 17 buckets, and so forth. We never use more
5048 than 32771 buckets. */
5050 static const size_t elf_buckets[] =
5052 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5053 16411, 32771, 0
5056 /* Compute bucket count for hashing table. We do not use a static set
5057 of possible tables sizes anymore. Instead we determine for all
5058 possible reasonable sizes of the table the outcome (i.e., the
5059 number of collisions etc) and choose the best solution. The
5060 weighting functions are not too simple to allow the table to grow
5061 without bounds. Instead one of the weighting factors is the size.
5062 Therefore the result is always a good payoff between few collisions
5063 (= short chain lengths) and table size. */
5064 static size_t
5065 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5066 unsigned long int nsyms, int gnu_hash)
5068 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5069 size_t best_size = 0;
5070 unsigned long int i;
5071 bfd_size_type amt;
5073 /* We have a problem here. The following code to optimize the table
5074 size requires an integer type with more the 32 bits. If
5075 BFD_HOST_U_64_BIT is set we know about such a type. */
5076 #ifdef BFD_HOST_U_64_BIT
5077 if (info->optimize)
5079 size_t minsize;
5080 size_t maxsize;
5081 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5082 bfd *dynobj = elf_hash_table (info)->dynobj;
5083 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5084 unsigned long int *counts;
5086 /* Possible optimization parameters: if we have NSYMS symbols we say
5087 that the hashing table must at least have NSYMS/4 and at most
5088 2*NSYMS buckets. */
5089 minsize = nsyms / 4;
5090 if (minsize == 0)
5091 minsize = 1;
5092 best_size = maxsize = nsyms * 2;
5093 if (gnu_hash)
5095 if (minsize < 2)
5096 minsize = 2;
5097 if ((best_size & 31) == 0)
5098 ++best_size;
5101 /* Create array where we count the collisions in. We must use bfd_malloc
5102 since the size could be large. */
5103 amt = maxsize;
5104 amt *= sizeof (unsigned long int);
5105 counts = bfd_malloc (amt);
5106 if (counts == NULL)
5107 return 0;
5109 /* Compute the "optimal" size for the hash table. The criteria is a
5110 minimal chain length. The minor criteria is (of course) the size
5111 of the table. */
5112 for (i = minsize; i < maxsize; ++i)
5114 /* Walk through the array of hashcodes and count the collisions. */
5115 BFD_HOST_U_64_BIT max;
5116 unsigned long int j;
5117 unsigned long int fact;
5119 if (gnu_hash && (i & 31) == 0)
5120 continue;
5122 memset (counts, '\0', i * sizeof (unsigned long int));
5124 /* Determine how often each hash bucket is used. */
5125 for (j = 0; j < nsyms; ++j)
5126 ++counts[hashcodes[j] % i];
5128 /* For the weight function we need some information about the
5129 pagesize on the target. This is information need not be 100%
5130 accurate. Since this information is not available (so far) we
5131 define it here to a reasonable default value. If it is crucial
5132 to have a better value some day simply define this value. */
5133 # ifndef BFD_TARGET_PAGESIZE
5134 # define BFD_TARGET_PAGESIZE (4096)
5135 # endif
5137 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5138 and the chains. */
5139 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5141 # if 1
5142 /* Variant 1: optimize for short chains. We add the squares
5143 of all the chain lengths (which favors many small chain
5144 over a few long chains). */
5145 for (j = 0; j < i; ++j)
5146 max += counts[j] * counts[j];
5148 /* This adds penalties for the overall size of the table. */
5149 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5150 max *= fact * fact;
5151 # else
5152 /* Variant 2: Optimize a lot more for small table. Here we
5153 also add squares of the size but we also add penalties for
5154 empty slots (the +1 term). */
5155 for (j = 0; j < i; ++j)
5156 max += (1 + counts[j]) * (1 + counts[j]);
5158 /* The overall size of the table is considered, but not as
5159 strong as in variant 1, where it is squared. */
5160 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5161 max *= fact;
5162 # endif
5164 /* Compare with current best results. */
5165 if (max < best_chlen)
5167 best_chlen = max;
5168 best_size = i;
5172 free (counts);
5174 else
5175 #endif /* defined (BFD_HOST_U_64_BIT) */
5177 /* This is the fallback solution if no 64bit type is available or if we
5178 are not supposed to spend much time on optimizations. We select the
5179 bucket count using a fixed set of numbers. */
5180 for (i = 0; elf_buckets[i] != 0; i++)
5182 best_size = elf_buckets[i];
5183 if (nsyms < elf_buckets[i + 1])
5184 break;
5186 if (gnu_hash && best_size < 2)
5187 best_size = 2;
5190 return best_size;
5193 /* Set up the sizes and contents of the ELF dynamic sections. This is
5194 called by the ELF linker emulation before_allocation routine. We
5195 must set the sizes of the sections before the linker sets the
5196 addresses of the various sections. */
5198 bfd_boolean
5199 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5200 const char *soname,
5201 const char *rpath,
5202 const char *filter_shlib,
5203 const char * const *auxiliary_filters,
5204 struct bfd_link_info *info,
5205 asection **sinterpptr,
5206 struct bfd_elf_version_tree *verdefs)
5208 bfd_size_type soname_indx;
5209 bfd *dynobj;
5210 const struct elf_backend_data *bed;
5211 struct elf_assign_sym_version_info asvinfo;
5213 *sinterpptr = NULL;
5215 soname_indx = (bfd_size_type) -1;
5217 if (!is_elf_hash_table (info->hash))
5218 return TRUE;
5220 elf_tdata (output_bfd)->relro = info->relro;
5221 if (info->execstack)
5222 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5223 else if (info->noexecstack)
5224 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5225 else
5227 bfd *inputobj;
5228 asection *notesec = NULL;
5229 int exec = 0;
5231 for (inputobj = info->input_bfds;
5232 inputobj;
5233 inputobj = inputobj->link_next)
5235 asection *s;
5237 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5238 continue;
5239 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5240 if (s)
5242 if (s->flags & SEC_CODE)
5243 exec = PF_X;
5244 notesec = s;
5246 else
5247 exec = PF_X;
5249 if (notesec)
5251 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5252 if (exec && info->relocatable
5253 && notesec->output_section != bfd_abs_section_ptr)
5254 notesec->output_section->flags |= SEC_CODE;
5258 /* Any syms created from now on start with -1 in
5259 got.refcount/offset and plt.refcount/offset. */
5260 elf_hash_table (info)->init_got_refcount
5261 = elf_hash_table (info)->init_got_offset;
5262 elf_hash_table (info)->init_plt_refcount
5263 = elf_hash_table (info)->init_plt_offset;
5265 /* The backend may have to create some sections regardless of whether
5266 we're dynamic or not. */
5267 bed = get_elf_backend_data (output_bfd);
5268 if (bed->elf_backend_always_size_sections
5269 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5270 return FALSE;
5272 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5273 return FALSE;
5275 dynobj = elf_hash_table (info)->dynobj;
5277 /* If there were no dynamic objects in the link, there is nothing to
5278 do here. */
5279 if (dynobj == NULL)
5280 return TRUE;
5282 if (elf_hash_table (info)->dynamic_sections_created)
5284 struct elf_info_failed eif;
5285 struct elf_link_hash_entry *h;
5286 asection *dynstr;
5287 struct bfd_elf_version_tree *t;
5288 struct bfd_elf_version_expr *d;
5289 asection *s;
5290 bfd_boolean all_defined;
5292 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5293 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5295 if (soname != NULL)
5297 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5298 soname, TRUE);
5299 if (soname_indx == (bfd_size_type) -1
5300 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5301 return FALSE;
5304 if (info->symbolic)
5306 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5307 return FALSE;
5308 info->flags |= DF_SYMBOLIC;
5311 if (rpath != NULL)
5313 bfd_size_type indx;
5315 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5316 TRUE);
5317 if (indx == (bfd_size_type) -1
5318 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5319 return FALSE;
5321 if (info->new_dtags)
5323 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5324 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5325 return FALSE;
5329 if (filter_shlib != NULL)
5331 bfd_size_type indx;
5333 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5334 filter_shlib, TRUE);
5335 if (indx == (bfd_size_type) -1
5336 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5337 return FALSE;
5340 if (auxiliary_filters != NULL)
5342 const char * const *p;
5344 for (p = auxiliary_filters; *p != NULL; p++)
5346 bfd_size_type indx;
5348 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5349 *p, TRUE);
5350 if (indx == (bfd_size_type) -1
5351 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5352 return FALSE;
5356 eif.info = info;
5357 eif.verdefs = verdefs;
5358 eif.failed = FALSE;
5360 /* If we are supposed to export all symbols into the dynamic symbol
5361 table (this is not the normal case), then do so. */
5362 if (info->export_dynamic
5363 || (info->executable && info->dynamic))
5365 elf_link_hash_traverse (elf_hash_table (info),
5366 _bfd_elf_export_symbol,
5367 &eif);
5368 if (eif.failed)
5369 return FALSE;
5372 /* Make all global versions with definition. */
5373 for (t = verdefs; t != NULL; t = t->next)
5374 for (d = t->globals.list; d != NULL; d = d->next)
5375 if (!d->symver && d->symbol)
5377 const char *verstr, *name;
5378 size_t namelen, verlen, newlen;
5379 char *newname, *p;
5380 struct elf_link_hash_entry *newh;
5382 name = d->symbol;
5383 namelen = strlen (name);
5384 verstr = t->name;
5385 verlen = strlen (verstr);
5386 newlen = namelen + verlen + 3;
5388 newname = bfd_malloc (newlen);
5389 if (newname == NULL)
5390 return FALSE;
5391 memcpy (newname, name, namelen);
5393 /* Check the hidden versioned definition. */
5394 p = newname + namelen;
5395 *p++ = ELF_VER_CHR;
5396 memcpy (p, verstr, verlen + 1);
5397 newh = elf_link_hash_lookup (elf_hash_table (info),
5398 newname, FALSE, FALSE,
5399 FALSE);
5400 if (newh == NULL
5401 || (newh->root.type != bfd_link_hash_defined
5402 && newh->root.type != bfd_link_hash_defweak))
5404 /* Check the default versioned definition. */
5405 *p++ = ELF_VER_CHR;
5406 memcpy (p, verstr, verlen + 1);
5407 newh = elf_link_hash_lookup (elf_hash_table (info),
5408 newname, FALSE, FALSE,
5409 FALSE);
5411 free (newname);
5413 /* Mark this version if there is a definition and it is
5414 not defined in a shared object. */
5415 if (newh != NULL
5416 && !newh->def_dynamic
5417 && (newh->root.type == bfd_link_hash_defined
5418 || newh->root.type == bfd_link_hash_defweak))
5419 d->symver = 1;
5422 /* Attach all the symbols to their version information. */
5423 asvinfo.output_bfd = output_bfd;
5424 asvinfo.info = info;
5425 asvinfo.verdefs = verdefs;
5426 asvinfo.failed = FALSE;
5428 elf_link_hash_traverse (elf_hash_table (info),
5429 _bfd_elf_link_assign_sym_version,
5430 &asvinfo);
5431 if (asvinfo.failed)
5432 return FALSE;
5434 if (!info->allow_undefined_version)
5436 /* Check if all global versions have a definition. */
5437 all_defined = TRUE;
5438 for (t = verdefs; t != NULL; t = t->next)
5439 for (d = t->globals.list; d != NULL; d = d->next)
5440 if (!d->symver && !d->script)
5442 (*_bfd_error_handler)
5443 (_("%s: undefined version: %s"),
5444 d->pattern, t->name);
5445 all_defined = FALSE;
5448 if (!all_defined)
5450 bfd_set_error (bfd_error_bad_value);
5451 return FALSE;
5455 /* Find all symbols which were defined in a dynamic object and make
5456 the backend pick a reasonable value for them. */
5457 elf_link_hash_traverse (elf_hash_table (info),
5458 _bfd_elf_adjust_dynamic_symbol,
5459 &eif);
5460 if (eif.failed)
5461 return FALSE;
5463 /* Add some entries to the .dynamic section. We fill in some of the
5464 values later, in bfd_elf_final_link, but we must add the entries
5465 now so that we know the final size of the .dynamic section. */
5467 /* If there are initialization and/or finalization functions to
5468 call then add the corresponding DT_INIT/DT_FINI entries. */
5469 h = (info->init_function
5470 ? elf_link_hash_lookup (elf_hash_table (info),
5471 info->init_function, FALSE,
5472 FALSE, FALSE)
5473 : NULL);
5474 if (h != NULL
5475 && (h->ref_regular
5476 || h->def_regular))
5478 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5479 return FALSE;
5481 h = (info->fini_function
5482 ? elf_link_hash_lookup (elf_hash_table (info),
5483 info->fini_function, FALSE,
5484 FALSE, FALSE)
5485 : NULL);
5486 if (h != NULL
5487 && (h->ref_regular
5488 || h->def_regular))
5490 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5491 return FALSE;
5494 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5495 if (s != NULL && s->linker_has_input)
5497 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5498 if (! info->executable)
5500 bfd *sub;
5501 asection *o;
5503 for (sub = info->input_bfds; sub != NULL;
5504 sub = sub->link_next)
5505 for (o = sub->sections; o != NULL; o = o->next)
5506 if (elf_section_data (o)->this_hdr.sh_type
5507 == SHT_PREINIT_ARRAY)
5509 (*_bfd_error_handler)
5510 (_("%B: .preinit_array section is not allowed in DSO"),
5511 sub);
5512 break;
5515 bfd_set_error (bfd_error_nonrepresentable_section);
5516 return FALSE;
5519 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5520 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5521 return FALSE;
5523 s = bfd_get_section_by_name (output_bfd, ".init_array");
5524 if (s != NULL && s->linker_has_input)
5526 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5527 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5528 return FALSE;
5530 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5531 if (s != NULL && s->linker_has_input)
5533 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5534 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5535 return FALSE;
5538 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5539 /* If .dynstr is excluded from the link, we don't want any of
5540 these tags. Strictly, we should be checking each section
5541 individually; This quick check covers for the case where
5542 someone does a /DISCARD/ : { *(*) }. */
5543 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5545 bfd_size_type strsize;
5547 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5548 if ((info->emit_hash
5549 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5550 || (info->emit_gnu_hash
5551 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5552 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5553 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5554 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5555 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5556 bed->s->sizeof_sym))
5557 return FALSE;
5561 /* The backend must work out the sizes of all the other dynamic
5562 sections. */
5563 if (bed->elf_backend_size_dynamic_sections
5564 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5565 return FALSE;
5567 if (elf_hash_table (info)->dynamic_sections_created)
5569 unsigned long section_sym_count;
5570 asection *s;
5572 /* Set up the version definition section. */
5573 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5574 BFD_ASSERT (s != NULL);
5576 /* We may have created additional version definitions if we are
5577 just linking a regular application. */
5578 verdefs = asvinfo.verdefs;
5580 /* Skip anonymous version tag. */
5581 if (verdefs != NULL && verdefs->vernum == 0)
5582 verdefs = verdefs->next;
5584 if (verdefs == NULL && !info->create_default_symver)
5585 s->flags |= SEC_EXCLUDE;
5586 else
5588 unsigned int cdefs;
5589 bfd_size_type size;
5590 struct bfd_elf_version_tree *t;
5591 bfd_byte *p;
5592 Elf_Internal_Verdef def;
5593 Elf_Internal_Verdaux defaux;
5594 struct bfd_link_hash_entry *bh;
5595 struct elf_link_hash_entry *h;
5596 const char *name;
5598 cdefs = 0;
5599 size = 0;
5601 /* Make space for the base version. */
5602 size += sizeof (Elf_External_Verdef);
5603 size += sizeof (Elf_External_Verdaux);
5604 ++cdefs;
5606 /* Make space for the default version. */
5607 if (info->create_default_symver)
5609 size += sizeof (Elf_External_Verdef);
5610 ++cdefs;
5613 for (t = verdefs; t != NULL; t = t->next)
5615 struct bfd_elf_version_deps *n;
5617 size += sizeof (Elf_External_Verdef);
5618 size += sizeof (Elf_External_Verdaux);
5619 ++cdefs;
5621 for (n = t->deps; n != NULL; n = n->next)
5622 size += sizeof (Elf_External_Verdaux);
5625 s->size = size;
5626 s->contents = bfd_alloc (output_bfd, s->size);
5627 if (s->contents == NULL && s->size != 0)
5628 return FALSE;
5630 /* Fill in the version definition section. */
5632 p = s->contents;
5634 def.vd_version = VER_DEF_CURRENT;
5635 def.vd_flags = VER_FLG_BASE;
5636 def.vd_ndx = 1;
5637 def.vd_cnt = 1;
5638 if (info->create_default_symver)
5640 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5641 def.vd_next = sizeof (Elf_External_Verdef);
5643 else
5645 def.vd_aux = sizeof (Elf_External_Verdef);
5646 def.vd_next = (sizeof (Elf_External_Verdef)
5647 + sizeof (Elf_External_Verdaux));
5650 if (soname_indx != (bfd_size_type) -1)
5652 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5653 soname_indx);
5654 def.vd_hash = bfd_elf_hash (soname);
5655 defaux.vda_name = soname_indx;
5656 name = soname;
5658 else
5660 bfd_size_type indx;
5662 name = lbasename (output_bfd->filename);
5663 def.vd_hash = bfd_elf_hash (name);
5664 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5665 name, FALSE);
5666 if (indx == (bfd_size_type) -1)
5667 return FALSE;
5668 defaux.vda_name = indx;
5670 defaux.vda_next = 0;
5672 _bfd_elf_swap_verdef_out (output_bfd, &def,
5673 (Elf_External_Verdef *) p);
5674 p += sizeof (Elf_External_Verdef);
5675 if (info->create_default_symver)
5677 /* Add a symbol representing this version. */
5678 bh = NULL;
5679 if (! (_bfd_generic_link_add_one_symbol
5680 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5681 0, NULL, FALSE,
5682 get_elf_backend_data (dynobj)->collect, &bh)))
5683 return FALSE;
5684 h = (struct elf_link_hash_entry *) bh;
5685 h->non_elf = 0;
5686 h->def_regular = 1;
5687 h->type = STT_OBJECT;
5688 h->verinfo.vertree = NULL;
5690 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5691 return FALSE;
5693 /* Create a duplicate of the base version with the same
5694 aux block, but different flags. */
5695 def.vd_flags = 0;
5696 def.vd_ndx = 2;
5697 def.vd_aux = sizeof (Elf_External_Verdef);
5698 if (verdefs)
5699 def.vd_next = (sizeof (Elf_External_Verdef)
5700 + sizeof (Elf_External_Verdaux));
5701 else
5702 def.vd_next = 0;
5703 _bfd_elf_swap_verdef_out (output_bfd, &def,
5704 (Elf_External_Verdef *) p);
5705 p += sizeof (Elf_External_Verdef);
5707 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5708 (Elf_External_Verdaux *) p);
5709 p += sizeof (Elf_External_Verdaux);
5711 for (t = verdefs; t != NULL; t = t->next)
5713 unsigned int cdeps;
5714 struct bfd_elf_version_deps *n;
5716 cdeps = 0;
5717 for (n = t->deps; n != NULL; n = n->next)
5718 ++cdeps;
5720 /* Add a symbol representing this version. */
5721 bh = NULL;
5722 if (! (_bfd_generic_link_add_one_symbol
5723 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5724 0, NULL, FALSE,
5725 get_elf_backend_data (dynobj)->collect, &bh)))
5726 return FALSE;
5727 h = (struct elf_link_hash_entry *) bh;
5728 h->non_elf = 0;
5729 h->def_regular = 1;
5730 h->type = STT_OBJECT;
5731 h->verinfo.vertree = t;
5733 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5734 return FALSE;
5736 def.vd_version = VER_DEF_CURRENT;
5737 def.vd_flags = 0;
5738 if (t->globals.list == NULL
5739 && t->locals.list == NULL
5740 && ! t->used)
5741 def.vd_flags |= VER_FLG_WEAK;
5742 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5743 def.vd_cnt = cdeps + 1;
5744 def.vd_hash = bfd_elf_hash (t->name);
5745 def.vd_aux = sizeof (Elf_External_Verdef);
5746 def.vd_next = 0;
5747 if (t->next != NULL)
5748 def.vd_next = (sizeof (Elf_External_Verdef)
5749 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5751 _bfd_elf_swap_verdef_out (output_bfd, &def,
5752 (Elf_External_Verdef *) p);
5753 p += sizeof (Elf_External_Verdef);
5755 defaux.vda_name = h->dynstr_index;
5756 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5757 h->dynstr_index);
5758 defaux.vda_next = 0;
5759 if (t->deps != NULL)
5760 defaux.vda_next = sizeof (Elf_External_Verdaux);
5761 t->name_indx = defaux.vda_name;
5763 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5764 (Elf_External_Verdaux *) p);
5765 p += sizeof (Elf_External_Verdaux);
5767 for (n = t->deps; n != NULL; n = n->next)
5769 if (n->version_needed == NULL)
5771 /* This can happen if there was an error in the
5772 version script. */
5773 defaux.vda_name = 0;
5775 else
5777 defaux.vda_name = n->version_needed->name_indx;
5778 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5779 defaux.vda_name);
5781 if (n->next == NULL)
5782 defaux.vda_next = 0;
5783 else
5784 defaux.vda_next = sizeof (Elf_External_Verdaux);
5786 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5787 (Elf_External_Verdaux *) p);
5788 p += sizeof (Elf_External_Verdaux);
5792 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5793 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5794 return FALSE;
5796 elf_tdata (output_bfd)->cverdefs = cdefs;
5799 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5801 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5802 return FALSE;
5804 else if (info->flags & DF_BIND_NOW)
5806 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5807 return FALSE;
5810 if (info->flags_1)
5812 if (info->executable)
5813 info->flags_1 &= ~ (DF_1_INITFIRST
5814 | DF_1_NODELETE
5815 | DF_1_NOOPEN);
5816 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5817 return FALSE;
5820 /* Work out the size of the version reference section. */
5822 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5823 BFD_ASSERT (s != NULL);
5825 struct elf_find_verdep_info sinfo;
5827 sinfo.output_bfd = output_bfd;
5828 sinfo.info = info;
5829 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5830 if (sinfo.vers == 0)
5831 sinfo.vers = 1;
5832 sinfo.failed = FALSE;
5834 elf_link_hash_traverse (elf_hash_table (info),
5835 _bfd_elf_link_find_version_dependencies,
5836 &sinfo);
5838 if (elf_tdata (output_bfd)->verref == NULL)
5839 s->flags |= SEC_EXCLUDE;
5840 else
5842 Elf_Internal_Verneed *t;
5843 unsigned int size;
5844 unsigned int crefs;
5845 bfd_byte *p;
5847 /* Build the version definition section. */
5848 size = 0;
5849 crefs = 0;
5850 for (t = elf_tdata (output_bfd)->verref;
5851 t != NULL;
5852 t = t->vn_nextref)
5854 Elf_Internal_Vernaux *a;
5856 size += sizeof (Elf_External_Verneed);
5857 ++crefs;
5858 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5859 size += sizeof (Elf_External_Vernaux);
5862 s->size = size;
5863 s->contents = bfd_alloc (output_bfd, s->size);
5864 if (s->contents == NULL)
5865 return FALSE;
5867 p = s->contents;
5868 for (t = elf_tdata (output_bfd)->verref;
5869 t != NULL;
5870 t = t->vn_nextref)
5872 unsigned int caux;
5873 Elf_Internal_Vernaux *a;
5874 bfd_size_type indx;
5876 caux = 0;
5877 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5878 ++caux;
5880 t->vn_version = VER_NEED_CURRENT;
5881 t->vn_cnt = caux;
5882 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5883 elf_dt_name (t->vn_bfd) != NULL
5884 ? elf_dt_name (t->vn_bfd)
5885 : lbasename (t->vn_bfd->filename),
5886 FALSE);
5887 if (indx == (bfd_size_type) -1)
5888 return FALSE;
5889 t->vn_file = indx;
5890 t->vn_aux = sizeof (Elf_External_Verneed);
5891 if (t->vn_nextref == NULL)
5892 t->vn_next = 0;
5893 else
5894 t->vn_next = (sizeof (Elf_External_Verneed)
5895 + caux * sizeof (Elf_External_Vernaux));
5897 _bfd_elf_swap_verneed_out (output_bfd, t,
5898 (Elf_External_Verneed *) p);
5899 p += sizeof (Elf_External_Verneed);
5901 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5903 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5904 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5905 a->vna_nodename, FALSE);
5906 if (indx == (bfd_size_type) -1)
5907 return FALSE;
5908 a->vna_name = indx;
5909 if (a->vna_nextptr == NULL)
5910 a->vna_next = 0;
5911 else
5912 a->vna_next = sizeof (Elf_External_Vernaux);
5914 _bfd_elf_swap_vernaux_out (output_bfd, a,
5915 (Elf_External_Vernaux *) p);
5916 p += sizeof (Elf_External_Vernaux);
5920 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5921 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5922 return FALSE;
5924 elf_tdata (output_bfd)->cverrefs = crefs;
5928 if ((elf_tdata (output_bfd)->cverrefs == 0
5929 && elf_tdata (output_bfd)->cverdefs == 0)
5930 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5931 &section_sym_count) == 0)
5933 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5934 s->flags |= SEC_EXCLUDE;
5937 return TRUE;
5940 /* Find the first non-excluded output section. We'll use its
5941 section symbol for some emitted relocs. */
5942 void
5943 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
5945 asection *s;
5947 for (s = output_bfd->sections; s != NULL; s = s->next)
5948 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
5949 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5951 elf_hash_table (info)->text_index_section = s;
5952 break;
5956 /* Find two non-excluded output sections, one for code, one for data.
5957 We'll use their section symbols for some emitted relocs. */
5958 void
5959 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
5961 asection *s;
5963 for (s = output_bfd->sections; s != NULL; s = s->next)
5964 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
5965 == (SEC_ALLOC | SEC_READONLY))
5966 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5968 elf_hash_table (info)->text_index_section = s;
5969 break;
5972 for (s = output_bfd->sections; s != NULL; s = s->next)
5973 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
5974 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5976 elf_hash_table (info)->data_index_section = s;
5977 break;
5980 if (elf_hash_table (info)->text_index_section == NULL)
5981 elf_hash_table (info)->text_index_section
5982 = elf_hash_table (info)->data_index_section;
5985 bfd_boolean
5986 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5988 const struct elf_backend_data *bed;
5990 if (!is_elf_hash_table (info->hash))
5991 return TRUE;
5993 bed = get_elf_backend_data (output_bfd);
5994 (*bed->elf_backend_init_index_section) (output_bfd, info);
5996 if (elf_hash_table (info)->dynamic_sections_created)
5998 bfd *dynobj;
5999 asection *s;
6000 bfd_size_type dynsymcount;
6001 unsigned long section_sym_count;
6002 unsigned int dtagcount;
6004 dynobj = elf_hash_table (info)->dynobj;
6006 /* Assign dynsym indicies. In a shared library we generate a
6007 section symbol for each output section, which come first.
6008 Next come all of the back-end allocated local dynamic syms,
6009 followed by the rest of the global symbols. */
6011 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6012 &section_sym_count);
6014 /* Work out the size of the symbol version section. */
6015 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6016 BFD_ASSERT (s != NULL);
6017 if (dynsymcount != 0
6018 && (s->flags & SEC_EXCLUDE) == 0)
6020 s->size = dynsymcount * sizeof (Elf_External_Versym);
6021 s->contents = bfd_zalloc (output_bfd, s->size);
6022 if (s->contents == NULL)
6023 return FALSE;
6025 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6026 return FALSE;
6029 /* Set the size of the .dynsym and .hash sections. We counted
6030 the number of dynamic symbols in elf_link_add_object_symbols.
6031 We will build the contents of .dynsym and .hash when we build
6032 the final symbol table, because until then we do not know the
6033 correct value to give the symbols. We built the .dynstr
6034 section as we went along in elf_link_add_object_symbols. */
6035 s = bfd_get_section_by_name (dynobj, ".dynsym");
6036 BFD_ASSERT (s != NULL);
6037 s->size = dynsymcount * bed->s->sizeof_sym;
6039 if (dynsymcount != 0)
6041 s->contents = bfd_alloc (output_bfd, s->size);
6042 if (s->contents == NULL)
6043 return FALSE;
6045 /* The first entry in .dynsym is a dummy symbol.
6046 Clear all the section syms, in case we don't output them all. */
6047 ++section_sym_count;
6048 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6051 elf_hash_table (info)->bucketcount = 0;
6053 /* Compute the size of the hashing table. As a side effect this
6054 computes the hash values for all the names we export. */
6055 if (info->emit_hash)
6057 unsigned long int *hashcodes;
6058 unsigned long int *hashcodesp;
6059 bfd_size_type amt;
6060 unsigned long int nsyms;
6061 size_t bucketcount;
6062 size_t hash_entry_size;
6064 /* Compute the hash values for all exported symbols. At the same
6065 time store the values in an array so that we could use them for
6066 optimizations. */
6067 amt = dynsymcount * sizeof (unsigned long int);
6068 hashcodes = bfd_malloc (amt);
6069 if (hashcodes == NULL)
6070 return FALSE;
6071 hashcodesp = hashcodes;
6073 /* Put all hash values in HASHCODES. */
6074 elf_link_hash_traverse (elf_hash_table (info),
6075 elf_collect_hash_codes, &hashcodesp);
6077 nsyms = hashcodesp - hashcodes;
6078 bucketcount
6079 = compute_bucket_count (info, hashcodes, nsyms, 0);
6080 free (hashcodes);
6082 if (bucketcount == 0)
6083 return FALSE;
6085 elf_hash_table (info)->bucketcount = bucketcount;
6087 s = bfd_get_section_by_name (dynobj, ".hash");
6088 BFD_ASSERT (s != NULL);
6089 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6090 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6091 s->contents = bfd_zalloc (output_bfd, s->size);
6092 if (s->contents == NULL)
6093 return FALSE;
6095 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6096 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6097 s->contents + hash_entry_size);
6100 if (info->emit_gnu_hash)
6102 size_t i, cnt;
6103 unsigned char *contents;
6104 struct collect_gnu_hash_codes cinfo;
6105 bfd_size_type amt;
6106 size_t bucketcount;
6108 memset (&cinfo, 0, sizeof (cinfo));
6110 /* Compute the hash values for all exported symbols. At the same
6111 time store the values in an array so that we could use them for
6112 optimizations. */
6113 amt = dynsymcount * 2 * sizeof (unsigned long int);
6114 cinfo.hashcodes = bfd_malloc (amt);
6115 if (cinfo.hashcodes == NULL)
6116 return FALSE;
6118 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6119 cinfo.min_dynindx = -1;
6120 cinfo.output_bfd = output_bfd;
6121 cinfo.bed = bed;
6123 /* Put all hash values in HASHCODES. */
6124 elf_link_hash_traverse (elf_hash_table (info),
6125 elf_collect_gnu_hash_codes, &cinfo);
6127 bucketcount
6128 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6130 if (bucketcount == 0)
6132 free (cinfo.hashcodes);
6133 return FALSE;
6136 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6137 BFD_ASSERT (s != NULL);
6139 if (cinfo.nsyms == 0)
6141 /* Empty .gnu.hash section is special. */
6142 BFD_ASSERT (cinfo.min_dynindx == -1);
6143 free (cinfo.hashcodes);
6144 s->size = 5 * 4 + bed->s->arch_size / 8;
6145 contents = bfd_zalloc (output_bfd, s->size);
6146 if (contents == NULL)
6147 return FALSE;
6148 s->contents = contents;
6149 /* 1 empty bucket. */
6150 bfd_put_32 (output_bfd, 1, contents);
6151 /* SYMIDX above the special symbol 0. */
6152 bfd_put_32 (output_bfd, 1, contents + 4);
6153 /* Just one word for bitmask. */
6154 bfd_put_32 (output_bfd, 1, contents + 8);
6155 /* Only hash fn bloom filter. */
6156 bfd_put_32 (output_bfd, 0, contents + 12);
6157 /* No hashes are valid - empty bitmask. */
6158 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6159 /* No hashes in the only bucket. */
6160 bfd_put_32 (output_bfd, 0,
6161 contents + 16 + bed->s->arch_size / 8);
6163 else
6165 unsigned long int maskwords, maskbitslog2;
6166 BFD_ASSERT (cinfo.min_dynindx != -1);
6168 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6169 if (maskbitslog2 < 3)
6170 maskbitslog2 = 5;
6171 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6172 maskbitslog2 = maskbitslog2 + 3;
6173 else
6174 maskbitslog2 = maskbitslog2 + 2;
6175 if (bed->s->arch_size == 64)
6177 if (maskbitslog2 == 5)
6178 maskbitslog2 = 6;
6179 cinfo.shift1 = 6;
6181 else
6182 cinfo.shift1 = 5;
6183 cinfo.mask = (1 << cinfo.shift1) - 1;
6184 cinfo.shift2 = maskbitslog2;
6185 cinfo.maskbits = 1 << maskbitslog2;
6186 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6187 amt = bucketcount * sizeof (unsigned long int) * 2;
6188 amt += maskwords * sizeof (bfd_vma);
6189 cinfo.bitmask = bfd_malloc (amt);
6190 if (cinfo.bitmask == NULL)
6192 free (cinfo.hashcodes);
6193 return FALSE;
6196 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6197 cinfo.indx = cinfo.counts + bucketcount;
6198 cinfo.symindx = dynsymcount - cinfo.nsyms;
6199 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6201 /* Determine how often each hash bucket is used. */
6202 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6203 for (i = 0; i < cinfo.nsyms; ++i)
6204 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6206 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6207 if (cinfo.counts[i] != 0)
6209 cinfo.indx[i] = cnt;
6210 cnt += cinfo.counts[i];
6212 BFD_ASSERT (cnt == dynsymcount);
6213 cinfo.bucketcount = bucketcount;
6214 cinfo.local_indx = cinfo.min_dynindx;
6216 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6217 s->size += cinfo.maskbits / 8;
6218 contents = bfd_zalloc (output_bfd, s->size);
6219 if (contents == NULL)
6221 free (cinfo.bitmask);
6222 free (cinfo.hashcodes);
6223 return FALSE;
6226 s->contents = contents;
6227 bfd_put_32 (output_bfd, bucketcount, contents);
6228 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6229 bfd_put_32 (output_bfd, maskwords, contents + 8);
6230 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6231 contents += 16 + cinfo.maskbits / 8;
6233 for (i = 0; i < bucketcount; ++i)
6235 if (cinfo.counts[i] == 0)
6236 bfd_put_32 (output_bfd, 0, contents);
6237 else
6238 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6239 contents += 4;
6242 cinfo.contents = contents;
6244 /* Renumber dynamic symbols, populate .gnu.hash section. */
6245 elf_link_hash_traverse (elf_hash_table (info),
6246 elf_renumber_gnu_hash_syms, &cinfo);
6248 contents = s->contents + 16;
6249 for (i = 0; i < maskwords; ++i)
6251 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6252 contents);
6253 contents += bed->s->arch_size / 8;
6256 free (cinfo.bitmask);
6257 free (cinfo.hashcodes);
6261 s = bfd_get_section_by_name (dynobj, ".dynstr");
6262 BFD_ASSERT (s != NULL);
6264 elf_finalize_dynstr (output_bfd, info);
6266 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6268 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6269 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6270 return FALSE;
6273 return TRUE;
6276 /* Final phase of ELF linker. */
6278 /* A structure we use to avoid passing large numbers of arguments. */
6280 struct elf_final_link_info
6282 /* General link information. */
6283 struct bfd_link_info *info;
6284 /* Output BFD. */
6285 bfd *output_bfd;
6286 /* Symbol string table. */
6287 struct bfd_strtab_hash *symstrtab;
6288 /* .dynsym section. */
6289 asection *dynsym_sec;
6290 /* .hash section. */
6291 asection *hash_sec;
6292 /* symbol version section (.gnu.version). */
6293 asection *symver_sec;
6294 /* Buffer large enough to hold contents of any section. */
6295 bfd_byte *contents;
6296 /* Buffer large enough to hold external relocs of any section. */
6297 void *external_relocs;
6298 /* Buffer large enough to hold internal relocs of any section. */
6299 Elf_Internal_Rela *internal_relocs;
6300 /* Buffer large enough to hold external local symbols of any input
6301 BFD. */
6302 bfd_byte *external_syms;
6303 /* And a buffer for symbol section indices. */
6304 Elf_External_Sym_Shndx *locsym_shndx;
6305 /* Buffer large enough to hold internal local symbols of any input
6306 BFD. */
6307 Elf_Internal_Sym *internal_syms;
6308 /* Array large enough to hold a symbol index for each local symbol
6309 of any input BFD. */
6310 long *indices;
6311 /* Array large enough to hold a section pointer for each local
6312 symbol of any input BFD. */
6313 asection **sections;
6314 /* Buffer to hold swapped out symbols. */
6315 bfd_byte *symbuf;
6316 /* And one for symbol section indices. */
6317 Elf_External_Sym_Shndx *symshndxbuf;
6318 /* Number of swapped out symbols in buffer. */
6319 size_t symbuf_count;
6320 /* Number of symbols which fit in symbuf. */
6321 size_t symbuf_size;
6322 /* And same for symshndxbuf. */
6323 size_t shndxbuf_size;
6326 /* This struct is used to pass information to elf_link_output_extsym. */
6328 struct elf_outext_info
6330 bfd_boolean failed;
6331 bfd_boolean localsyms;
6332 struct elf_final_link_info *finfo;
6335 /* When performing a relocatable link, the input relocations are
6336 preserved. But, if they reference global symbols, the indices
6337 referenced must be updated. Update all the relocations in
6338 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
6340 static void
6341 elf_link_adjust_relocs (bfd *abfd,
6342 Elf_Internal_Shdr *rel_hdr,
6343 unsigned int count,
6344 struct elf_link_hash_entry **rel_hash)
6346 unsigned int i;
6347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6348 bfd_byte *erela;
6349 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6350 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6351 bfd_vma r_type_mask;
6352 int r_sym_shift;
6354 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
6356 swap_in = bed->s->swap_reloc_in;
6357 swap_out = bed->s->swap_reloc_out;
6359 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
6361 swap_in = bed->s->swap_reloca_in;
6362 swap_out = bed->s->swap_reloca_out;
6364 else
6365 abort ();
6367 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
6368 abort ();
6370 if (bed->s->arch_size == 32)
6372 r_type_mask = 0xff;
6373 r_sym_shift = 8;
6375 else
6377 r_type_mask = 0xffffffff;
6378 r_sym_shift = 32;
6381 erela = rel_hdr->contents;
6382 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
6384 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
6385 unsigned int j;
6387 if (*rel_hash == NULL)
6388 continue;
6390 BFD_ASSERT ((*rel_hash)->indx >= 0);
6392 (*swap_in) (abfd, erela, irela);
6393 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
6394 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
6395 | (irela[j].r_info & r_type_mask));
6396 (*swap_out) (abfd, irela, erela);
6400 struct elf_link_sort_rela
6402 union {
6403 bfd_vma offset;
6404 bfd_vma sym_mask;
6405 } u;
6406 enum elf_reloc_type_class type;
6407 /* We use this as an array of size int_rels_per_ext_rel. */
6408 Elf_Internal_Rela rela[1];
6411 static int
6412 elf_link_sort_cmp1 (const void *A, const void *B)
6414 const struct elf_link_sort_rela *a = A;
6415 const struct elf_link_sort_rela *b = B;
6416 int relativea, relativeb;
6418 relativea = a->type == reloc_class_relative;
6419 relativeb = b->type == reloc_class_relative;
6421 if (relativea < relativeb)
6422 return 1;
6423 if (relativea > relativeb)
6424 return -1;
6425 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
6426 return -1;
6427 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
6428 return 1;
6429 if (a->rela->r_offset < b->rela->r_offset)
6430 return -1;
6431 if (a->rela->r_offset > b->rela->r_offset)
6432 return 1;
6433 return 0;
6436 static int
6437 elf_link_sort_cmp2 (const void *A, const void *B)
6439 const struct elf_link_sort_rela *a = A;
6440 const struct elf_link_sort_rela *b = B;
6441 int copya, copyb;
6443 if (a->u.offset < b->u.offset)
6444 return -1;
6445 if (a->u.offset > b->u.offset)
6446 return 1;
6447 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
6448 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
6449 if (copya < copyb)
6450 return -1;
6451 if (copya > copyb)
6452 return 1;
6453 if (a->rela->r_offset < b->rela->r_offset)
6454 return -1;
6455 if (a->rela->r_offset > b->rela->r_offset)
6456 return 1;
6457 return 0;
6460 static size_t
6461 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
6463 asection *reldyn;
6464 bfd_size_type count, size;
6465 size_t i, ret, sort_elt, ext_size;
6466 bfd_byte *sort, *s_non_relative, *p;
6467 struct elf_link_sort_rela *sq;
6468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6469 int i2e = bed->s->int_rels_per_ext_rel;
6470 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6471 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6472 struct bfd_link_order *lo;
6473 bfd_vma r_sym_mask;
6475 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
6476 if (reldyn == NULL || reldyn->size == 0)
6478 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
6479 if (reldyn == NULL || reldyn->size == 0)
6480 return 0;
6481 ext_size = bed->s->sizeof_rel;
6482 swap_in = bed->s->swap_reloc_in;
6483 swap_out = bed->s->swap_reloc_out;
6485 else
6487 ext_size = bed->s->sizeof_rela;
6488 swap_in = bed->s->swap_reloca_in;
6489 swap_out = bed->s->swap_reloca_out;
6491 count = reldyn->size / ext_size;
6493 size = 0;
6494 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6495 if (lo->type == bfd_indirect_link_order)
6497 asection *o = lo->u.indirect.section;
6498 size += o->size;
6501 if (size != reldyn->size)
6502 return 0;
6504 sort_elt = (sizeof (struct elf_link_sort_rela)
6505 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6506 sort = bfd_zmalloc (sort_elt * count);
6507 if (sort == NULL)
6509 (*info->callbacks->warning)
6510 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6511 return 0;
6514 if (bed->s->arch_size == 32)
6515 r_sym_mask = ~(bfd_vma) 0xff;
6516 else
6517 r_sym_mask = ~(bfd_vma) 0xffffffff;
6519 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6520 if (lo->type == bfd_indirect_link_order)
6522 bfd_byte *erel, *erelend;
6523 asection *o = lo->u.indirect.section;
6525 if (o->contents == NULL && o->size != 0)
6527 /* This is a reloc section that is being handled as a normal
6528 section. See bfd_section_from_shdr. We can't combine
6529 relocs in this case. */
6530 free (sort);
6531 return 0;
6533 erel = o->contents;
6534 erelend = o->contents + o->size;
6535 p = sort + o->output_offset / ext_size * sort_elt;
6536 while (erel < erelend)
6538 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6539 (*swap_in) (abfd, erel, s->rela);
6540 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6541 s->u.sym_mask = r_sym_mask;
6542 p += sort_elt;
6543 erel += ext_size;
6547 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6549 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6551 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6552 if (s->type != reloc_class_relative)
6553 break;
6555 ret = i;
6556 s_non_relative = p;
6558 sq = (struct elf_link_sort_rela *) s_non_relative;
6559 for (; i < count; i++, p += sort_elt)
6561 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6562 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6563 sq = sp;
6564 sp->u.offset = sq->rela->r_offset;
6567 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6569 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6570 if (lo->type == bfd_indirect_link_order)
6572 bfd_byte *erel, *erelend;
6573 asection *o = lo->u.indirect.section;
6575 erel = o->contents;
6576 erelend = o->contents + o->size;
6577 p = sort + o->output_offset / ext_size * sort_elt;
6578 while (erel < erelend)
6580 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6581 (*swap_out) (abfd, s->rela, erel);
6582 p += sort_elt;
6583 erel += ext_size;
6587 free (sort);
6588 *psec = reldyn;
6589 return ret;
6592 /* Flush the output symbols to the file. */
6594 static bfd_boolean
6595 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6596 const struct elf_backend_data *bed)
6598 if (finfo->symbuf_count > 0)
6600 Elf_Internal_Shdr *hdr;
6601 file_ptr pos;
6602 bfd_size_type amt;
6604 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6605 pos = hdr->sh_offset + hdr->sh_size;
6606 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6607 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6608 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6609 return FALSE;
6611 hdr->sh_size += amt;
6612 finfo->symbuf_count = 0;
6615 return TRUE;
6618 /* Add a symbol to the output symbol table. */
6620 static bfd_boolean
6621 elf_link_output_sym (struct elf_final_link_info *finfo,
6622 const char *name,
6623 Elf_Internal_Sym *elfsym,
6624 asection *input_sec,
6625 struct elf_link_hash_entry *h)
6627 bfd_byte *dest;
6628 Elf_External_Sym_Shndx *destshndx;
6629 bfd_boolean (*output_symbol_hook)
6630 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6631 struct elf_link_hash_entry *);
6632 const struct elf_backend_data *bed;
6634 bed = get_elf_backend_data (finfo->output_bfd);
6635 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6636 if (output_symbol_hook != NULL)
6638 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6639 return FALSE;
6642 if (name == NULL || *name == '\0')
6643 elfsym->st_name = 0;
6644 else if (input_sec->flags & SEC_EXCLUDE)
6645 elfsym->st_name = 0;
6646 else
6648 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6649 name, TRUE, FALSE);
6650 if (elfsym->st_name == (unsigned long) -1)
6651 return FALSE;
6654 if (finfo->symbuf_count >= finfo->symbuf_size)
6656 if (! elf_link_flush_output_syms (finfo, bed))
6657 return FALSE;
6660 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6661 destshndx = finfo->symshndxbuf;
6662 if (destshndx != NULL)
6664 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6666 bfd_size_type amt;
6668 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6669 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6670 if (destshndx == NULL)
6671 return FALSE;
6672 memset ((char *) destshndx + amt, 0, amt);
6673 finfo->shndxbuf_size *= 2;
6675 destshndx += bfd_get_symcount (finfo->output_bfd);
6678 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6679 finfo->symbuf_count += 1;
6680 bfd_get_symcount (finfo->output_bfd) += 1;
6682 return TRUE;
6685 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6687 static bfd_boolean
6688 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
6690 if (sym->st_shndx > SHN_HIRESERVE)
6692 /* The gABI doesn't support dynamic symbols in output sections
6693 beyond 64k. */
6694 (*_bfd_error_handler)
6695 (_("%B: Too many sections: %d (>= %d)"),
6696 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
6697 bfd_set_error (bfd_error_nonrepresentable_section);
6698 return FALSE;
6700 return TRUE;
6703 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6704 allowing an unsatisfied unversioned symbol in the DSO to match a
6705 versioned symbol that would normally require an explicit version.
6706 We also handle the case that a DSO references a hidden symbol
6707 which may be satisfied by a versioned symbol in another DSO. */
6709 static bfd_boolean
6710 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6711 const struct elf_backend_data *bed,
6712 struct elf_link_hash_entry *h)
6714 bfd *abfd;
6715 struct elf_link_loaded_list *loaded;
6717 if (!is_elf_hash_table (info->hash))
6718 return FALSE;
6720 switch (h->root.type)
6722 default:
6723 abfd = NULL;
6724 break;
6726 case bfd_link_hash_undefined:
6727 case bfd_link_hash_undefweak:
6728 abfd = h->root.u.undef.abfd;
6729 if ((abfd->flags & DYNAMIC) == 0
6730 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6731 return FALSE;
6732 break;
6734 case bfd_link_hash_defined:
6735 case bfd_link_hash_defweak:
6736 abfd = h->root.u.def.section->owner;
6737 break;
6739 case bfd_link_hash_common:
6740 abfd = h->root.u.c.p->section->owner;
6741 break;
6743 BFD_ASSERT (abfd != NULL);
6745 for (loaded = elf_hash_table (info)->loaded;
6746 loaded != NULL;
6747 loaded = loaded->next)
6749 bfd *input;
6750 Elf_Internal_Shdr *hdr;
6751 bfd_size_type symcount;
6752 bfd_size_type extsymcount;
6753 bfd_size_type extsymoff;
6754 Elf_Internal_Shdr *versymhdr;
6755 Elf_Internal_Sym *isym;
6756 Elf_Internal_Sym *isymend;
6757 Elf_Internal_Sym *isymbuf;
6758 Elf_External_Versym *ever;
6759 Elf_External_Versym *extversym;
6761 input = loaded->abfd;
6763 /* We check each DSO for a possible hidden versioned definition. */
6764 if (input == abfd
6765 || (input->flags & DYNAMIC) == 0
6766 || elf_dynversym (input) == 0)
6767 continue;
6769 hdr = &elf_tdata (input)->dynsymtab_hdr;
6771 symcount = hdr->sh_size / bed->s->sizeof_sym;
6772 if (elf_bad_symtab (input))
6774 extsymcount = symcount;
6775 extsymoff = 0;
6777 else
6779 extsymcount = symcount - hdr->sh_info;
6780 extsymoff = hdr->sh_info;
6783 if (extsymcount == 0)
6784 continue;
6786 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6787 NULL, NULL, NULL);
6788 if (isymbuf == NULL)
6789 return FALSE;
6791 /* Read in any version definitions. */
6792 versymhdr = &elf_tdata (input)->dynversym_hdr;
6793 extversym = bfd_malloc (versymhdr->sh_size);
6794 if (extversym == NULL)
6795 goto error_ret;
6797 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6798 || (bfd_bread (extversym, versymhdr->sh_size, input)
6799 != versymhdr->sh_size))
6801 free (extversym);
6802 error_ret:
6803 free (isymbuf);
6804 return FALSE;
6807 ever = extversym + extsymoff;
6808 isymend = isymbuf + extsymcount;
6809 for (isym = isymbuf; isym < isymend; isym++, ever++)
6811 const char *name;
6812 Elf_Internal_Versym iver;
6813 unsigned short version_index;
6815 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6816 || isym->st_shndx == SHN_UNDEF)
6817 continue;
6819 name = bfd_elf_string_from_elf_section (input,
6820 hdr->sh_link,
6821 isym->st_name);
6822 if (strcmp (name, h->root.root.string) != 0)
6823 continue;
6825 _bfd_elf_swap_versym_in (input, ever, &iver);
6827 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6829 /* If we have a non-hidden versioned sym, then it should
6830 have provided a definition for the undefined sym. */
6831 abort ();
6834 version_index = iver.vs_vers & VERSYM_VERSION;
6835 if (version_index == 1 || version_index == 2)
6837 /* This is the base or first version. We can use it. */
6838 free (extversym);
6839 free (isymbuf);
6840 return TRUE;
6844 free (extversym);
6845 free (isymbuf);
6848 return FALSE;
6851 /* Add an external symbol to the symbol table. This is called from
6852 the hash table traversal routine. When generating a shared object,
6853 we go through the symbol table twice. The first time we output
6854 anything that might have been forced to local scope in a version
6855 script. The second time we output the symbols that are still
6856 global symbols. */
6858 static bfd_boolean
6859 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6861 struct elf_outext_info *eoinfo = data;
6862 struct elf_final_link_info *finfo = eoinfo->finfo;
6863 bfd_boolean strip;
6864 Elf_Internal_Sym sym;
6865 asection *input_sec;
6866 const struct elf_backend_data *bed;
6868 if (h->root.type == bfd_link_hash_warning)
6870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6871 if (h->root.type == bfd_link_hash_new)
6872 return TRUE;
6875 /* Decide whether to output this symbol in this pass. */
6876 if (eoinfo->localsyms)
6878 if (!h->forced_local)
6879 return TRUE;
6881 else
6883 if (h->forced_local)
6884 return TRUE;
6887 bed = get_elf_backend_data (finfo->output_bfd);
6889 if (h->root.type == bfd_link_hash_undefined)
6891 /* If we have an undefined symbol reference here then it must have
6892 come from a shared library that is being linked in. (Undefined
6893 references in regular files have already been handled). */
6894 bfd_boolean ignore_undef = FALSE;
6896 /* Some symbols may be special in that the fact that they're
6897 undefined can be safely ignored - let backend determine that. */
6898 if (bed->elf_backend_ignore_undef_symbol)
6899 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6901 /* If we are reporting errors for this situation then do so now. */
6902 if (ignore_undef == FALSE
6903 && h->ref_dynamic
6904 && ! h->ref_regular
6905 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6906 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6908 if (! (finfo->info->callbacks->undefined_symbol
6909 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6910 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6912 eoinfo->failed = TRUE;
6913 return FALSE;
6918 /* We should also warn if a forced local symbol is referenced from
6919 shared libraries. */
6920 if (! finfo->info->relocatable
6921 && (! finfo->info->shared)
6922 && h->forced_local
6923 && h->ref_dynamic
6924 && !h->dynamic_def
6925 && !h->dynamic_weak
6926 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6928 (*_bfd_error_handler)
6929 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6930 finfo->output_bfd,
6931 h->root.u.def.section == bfd_abs_section_ptr
6932 ? finfo->output_bfd : h->root.u.def.section->owner,
6933 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6934 ? "internal"
6935 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6936 ? "hidden" : "local",
6937 h->root.root.string);
6938 eoinfo->failed = TRUE;
6939 return FALSE;
6942 /* We don't want to output symbols that have never been mentioned by
6943 a regular file, or that we have been told to strip. However, if
6944 h->indx is set to -2, the symbol is used by a reloc and we must
6945 output it. */
6946 if (h->indx == -2)
6947 strip = FALSE;
6948 else if ((h->def_dynamic
6949 || h->ref_dynamic
6950 || h->root.type == bfd_link_hash_new)
6951 && !h->def_regular
6952 && !h->ref_regular)
6953 strip = TRUE;
6954 else if (finfo->info->strip == strip_all)
6955 strip = TRUE;
6956 else if (finfo->info->strip == strip_some
6957 && bfd_hash_lookup (finfo->info->keep_hash,
6958 h->root.root.string, FALSE, FALSE) == NULL)
6959 strip = TRUE;
6960 else if (finfo->info->strip_discarded
6961 && (h->root.type == bfd_link_hash_defined
6962 || h->root.type == bfd_link_hash_defweak)
6963 && elf_discarded_section (h->root.u.def.section))
6964 strip = TRUE;
6965 else
6966 strip = FALSE;
6968 /* If we're stripping it, and it's not a dynamic symbol, there's
6969 nothing else to do unless it is a forced local symbol. */
6970 if (strip
6971 && h->dynindx == -1
6972 && !h->forced_local)
6973 return TRUE;
6975 sym.st_value = 0;
6976 sym.st_size = h->size;
6977 sym.st_other = h->other;
6978 if (h->forced_local)
6979 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6980 else if (h->root.type == bfd_link_hash_undefweak
6981 || h->root.type == bfd_link_hash_defweak)
6982 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6983 else
6984 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6986 switch (h->root.type)
6988 default:
6989 case bfd_link_hash_new:
6990 case bfd_link_hash_warning:
6991 abort ();
6992 return FALSE;
6994 case bfd_link_hash_undefined:
6995 case bfd_link_hash_undefweak:
6996 input_sec = bfd_und_section_ptr;
6997 sym.st_shndx = SHN_UNDEF;
6998 break;
7000 case bfd_link_hash_defined:
7001 case bfd_link_hash_defweak:
7003 input_sec = h->root.u.def.section;
7004 if (input_sec->output_section != NULL)
7006 sym.st_shndx =
7007 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
7008 input_sec->output_section);
7009 if (sym.st_shndx == SHN_BAD)
7011 (*_bfd_error_handler)
7012 (_("%B: could not find output section %A for input section %A"),
7013 finfo->output_bfd, input_sec->output_section, input_sec);
7014 eoinfo->failed = TRUE;
7015 return FALSE;
7018 /* ELF symbols in relocatable files are section relative,
7019 but in nonrelocatable files they are virtual
7020 addresses. */
7021 sym.st_value = h->root.u.def.value + input_sec->output_offset;
7022 if (! finfo->info->relocatable)
7024 sym.st_value += input_sec->output_section->vma;
7025 if (h->type == STT_TLS)
7027 /* STT_TLS symbols are relative to PT_TLS segment
7028 base. */
7029 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7030 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7034 else
7036 BFD_ASSERT (input_sec->owner == NULL
7037 || (input_sec->owner->flags & DYNAMIC) != 0);
7038 sym.st_shndx = SHN_UNDEF;
7039 input_sec = bfd_und_section_ptr;
7042 break;
7044 case bfd_link_hash_common:
7045 input_sec = h->root.u.c.p->section;
7046 sym.st_shndx = bed->common_section_index (input_sec);
7047 sym.st_value = 1 << h->root.u.c.p->alignment_power;
7048 break;
7050 case bfd_link_hash_indirect:
7051 /* These symbols are created by symbol versioning. They point
7052 to the decorated version of the name. For example, if the
7053 symbol foo@@GNU_1.2 is the default, which should be used when
7054 foo is used with no version, then we add an indirect symbol
7055 foo which points to foo@@GNU_1.2. We ignore these symbols,
7056 since the indirected symbol is already in the hash table. */
7057 return TRUE;
7060 /* Give the processor backend a chance to tweak the symbol value,
7061 and also to finish up anything that needs to be done for this
7062 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7063 forced local syms when non-shared is due to a historical quirk. */
7064 if ((h->dynindx != -1
7065 || h->forced_local)
7066 && ((finfo->info->shared
7067 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7068 || h->root.type != bfd_link_hash_undefweak))
7069 || !h->forced_local)
7070 && elf_hash_table (finfo->info)->dynamic_sections_created)
7072 if (! ((*bed->elf_backend_finish_dynamic_symbol)
7073 (finfo->output_bfd, finfo->info, h, &sym)))
7075 eoinfo->failed = TRUE;
7076 return FALSE;
7080 /* If we are marking the symbol as undefined, and there are no
7081 non-weak references to this symbol from a regular object, then
7082 mark the symbol as weak undefined; if there are non-weak
7083 references, mark the symbol as strong. We can't do this earlier,
7084 because it might not be marked as undefined until the
7085 finish_dynamic_symbol routine gets through with it. */
7086 if (sym.st_shndx == SHN_UNDEF
7087 && h->ref_regular
7088 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
7089 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
7091 int bindtype;
7093 if (h->ref_regular_nonweak)
7094 bindtype = STB_GLOBAL;
7095 else
7096 bindtype = STB_WEAK;
7097 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
7100 /* If a non-weak symbol with non-default visibility is not defined
7101 locally, it is a fatal error. */
7102 if (! finfo->info->relocatable
7103 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
7104 && ELF_ST_BIND (sym.st_info) != STB_WEAK
7105 && h->root.type == bfd_link_hash_undefined
7106 && !h->def_regular)
7108 (*_bfd_error_handler)
7109 (_("%B: %s symbol `%s' isn't defined"),
7110 finfo->output_bfd,
7111 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
7112 ? "protected"
7113 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
7114 ? "internal" : "hidden",
7115 h->root.root.string);
7116 eoinfo->failed = TRUE;
7117 return FALSE;
7120 /* If this symbol should be put in the .dynsym section, then put it
7121 there now. We already know the symbol index. We also fill in
7122 the entry in the .hash section. */
7123 if (h->dynindx != -1
7124 && elf_hash_table (finfo->info)->dynamic_sections_created)
7126 bfd_byte *esym;
7128 sym.st_name = h->dynstr_index;
7129 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
7130 if (! check_dynsym (finfo->output_bfd, &sym))
7132 eoinfo->failed = TRUE;
7133 return FALSE;
7135 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
7137 if (finfo->hash_sec != NULL)
7139 size_t hash_entry_size;
7140 bfd_byte *bucketpos;
7141 bfd_vma chain;
7142 size_t bucketcount;
7143 size_t bucket;
7145 bucketcount = elf_hash_table (finfo->info)->bucketcount;
7146 bucket = h->u.elf_hash_value % bucketcount;
7148 hash_entry_size
7149 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
7150 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
7151 + (bucket + 2) * hash_entry_size);
7152 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
7153 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
7154 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
7155 ((bfd_byte *) finfo->hash_sec->contents
7156 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
7159 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
7161 Elf_Internal_Versym iversym;
7162 Elf_External_Versym *eversym;
7164 if (!h->def_regular)
7166 if (h->verinfo.verdef == NULL)
7167 iversym.vs_vers = 0;
7168 else
7169 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
7171 else
7173 if (h->verinfo.vertree == NULL)
7174 iversym.vs_vers = 1;
7175 else
7176 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
7177 if (finfo->info->create_default_symver)
7178 iversym.vs_vers++;
7181 if (h->hidden)
7182 iversym.vs_vers |= VERSYM_HIDDEN;
7184 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
7185 eversym += h->dynindx;
7186 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
7190 /* If we're stripping it, then it was just a dynamic symbol, and
7191 there's nothing else to do. */
7192 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
7193 return TRUE;
7195 h->indx = bfd_get_symcount (finfo->output_bfd);
7197 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
7199 eoinfo->failed = TRUE;
7200 return FALSE;
7203 return TRUE;
7206 /* Return TRUE if special handling is done for relocs in SEC against
7207 symbols defined in discarded sections. */
7209 static bfd_boolean
7210 elf_section_ignore_discarded_relocs (asection *sec)
7212 const struct elf_backend_data *bed;
7214 switch (sec->sec_info_type)
7216 case ELF_INFO_TYPE_STABS:
7217 case ELF_INFO_TYPE_EH_FRAME:
7218 return TRUE;
7219 default:
7220 break;
7223 bed = get_elf_backend_data (sec->owner);
7224 if (bed->elf_backend_ignore_discarded_relocs != NULL
7225 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
7226 return TRUE;
7228 return FALSE;
7231 /* Return a mask saying how ld should treat relocations in SEC against
7232 symbols defined in discarded sections. If this function returns
7233 COMPLAIN set, ld will issue a warning message. If this function
7234 returns PRETEND set, and the discarded section was link-once and the
7235 same size as the kept link-once section, ld will pretend that the
7236 symbol was actually defined in the kept section. Otherwise ld will
7237 zero the reloc (at least that is the intent, but some cooperation by
7238 the target dependent code is needed, particularly for REL targets). */
7240 unsigned int
7241 _bfd_elf_default_action_discarded (asection *sec)
7243 if (sec->flags & SEC_DEBUGGING)
7244 return PRETEND;
7246 if (strcmp (".eh_frame", sec->name) == 0)
7247 return 0;
7249 if (strcmp (".gcc_except_table", sec->name) == 0)
7250 return 0;
7252 return COMPLAIN | PRETEND;
7255 /* Find a match between a section and a member of a section group. */
7257 static asection *
7258 match_group_member (asection *sec, asection *group,
7259 struct bfd_link_info *info)
7261 asection *first = elf_next_in_group (group);
7262 asection *s = first;
7264 while (s != NULL)
7266 if (bfd_elf_match_symbols_in_sections (s, sec, info))
7267 return s;
7269 s = elf_next_in_group (s);
7270 if (s == first)
7271 break;
7274 return NULL;
7277 /* Check if the kept section of a discarded section SEC can be used
7278 to replace it. Return the replacement if it is OK. Otherwise return
7279 NULL. */
7281 asection *
7282 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
7284 asection *kept;
7286 kept = sec->kept_section;
7287 if (kept != NULL)
7289 if ((kept->flags & SEC_GROUP) != 0)
7290 kept = match_group_member (sec, kept, info);
7291 if (kept != NULL && sec->size != kept->size)
7292 kept = NULL;
7293 sec->kept_section = kept;
7295 return kept;
7298 /* Link an input file into the linker output file. This function
7299 handles all the sections and relocations of the input file at once.
7300 This is so that we only have to read the local symbols once, and
7301 don't have to keep them in memory. */
7303 static bfd_boolean
7304 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
7306 bfd_boolean (*relocate_section)
7307 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
7308 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
7309 bfd *output_bfd;
7310 Elf_Internal_Shdr *symtab_hdr;
7311 size_t locsymcount;
7312 size_t extsymoff;
7313 Elf_Internal_Sym *isymbuf;
7314 Elf_Internal_Sym *isym;
7315 Elf_Internal_Sym *isymend;
7316 long *pindex;
7317 asection **ppsection;
7318 asection *o;
7319 const struct elf_backend_data *bed;
7320 bfd_boolean emit_relocs;
7321 struct elf_link_hash_entry **sym_hashes;
7323 output_bfd = finfo->output_bfd;
7324 bed = get_elf_backend_data (output_bfd);
7325 relocate_section = bed->elf_backend_relocate_section;
7327 /* If this is a dynamic object, we don't want to do anything here:
7328 we don't want the local symbols, and we don't want the section
7329 contents. */
7330 if ((input_bfd->flags & DYNAMIC) != 0)
7331 return TRUE;
7333 emit_relocs = (finfo->info->relocatable
7334 || finfo->info->emitrelocations);
7336 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7337 if (elf_bad_symtab (input_bfd))
7339 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
7340 extsymoff = 0;
7342 else
7344 locsymcount = symtab_hdr->sh_info;
7345 extsymoff = symtab_hdr->sh_info;
7348 /* Read the local symbols. */
7349 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7350 if (isymbuf == NULL && locsymcount != 0)
7352 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
7353 finfo->internal_syms,
7354 finfo->external_syms,
7355 finfo->locsym_shndx);
7356 if (isymbuf == NULL)
7357 return FALSE;
7360 /* Find local symbol sections and adjust values of symbols in
7361 SEC_MERGE sections. Write out those local symbols we know are
7362 going into the output file. */
7363 isymend = isymbuf + locsymcount;
7364 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
7365 isym < isymend;
7366 isym++, pindex++, ppsection++)
7368 asection *isec;
7369 const char *name;
7370 Elf_Internal_Sym osym;
7372 *pindex = -1;
7374 if (elf_bad_symtab (input_bfd))
7376 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
7378 *ppsection = NULL;
7379 continue;
7383 if (isym->st_shndx == SHN_UNDEF)
7384 isec = bfd_und_section_ptr;
7385 else if (isym->st_shndx < SHN_LORESERVE
7386 || isym->st_shndx > SHN_HIRESERVE)
7388 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
7389 if (isec
7390 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
7391 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
7392 isym->st_value =
7393 _bfd_merged_section_offset (output_bfd, &isec,
7394 elf_section_data (isec)->sec_info,
7395 isym->st_value);
7397 else if (isym->st_shndx == SHN_ABS)
7398 isec = bfd_abs_section_ptr;
7399 else if (isym->st_shndx == SHN_COMMON)
7400 isec = bfd_com_section_ptr;
7401 else
7403 /* Don't attempt to output symbols with st_shnx in the
7404 reserved range other than SHN_ABS and SHN_COMMON. */
7405 *ppsection = NULL;
7406 continue;
7409 *ppsection = isec;
7411 /* Don't output the first, undefined, symbol. */
7412 if (ppsection == finfo->sections)
7413 continue;
7415 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
7417 /* We never output section symbols. Instead, we use the
7418 section symbol of the corresponding section in the output
7419 file. */
7420 continue;
7423 /* If we are stripping all symbols, we don't want to output this
7424 one. */
7425 if (finfo->info->strip == strip_all)
7426 continue;
7428 /* If we are discarding all local symbols, we don't want to
7429 output this one. If we are generating a relocatable output
7430 file, then some of the local symbols may be required by
7431 relocs; we output them below as we discover that they are
7432 needed. */
7433 if (finfo->info->discard == discard_all)
7434 continue;
7436 /* If this symbol is defined in a section which we are
7437 discarding, we don't need to keep it. */
7438 if (isym->st_shndx != SHN_UNDEF
7439 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
7440 && (isec == NULL
7441 || bfd_section_removed_from_list (output_bfd,
7442 isec->output_section)))
7443 continue;
7445 /* Get the name of the symbol. */
7446 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
7447 isym->st_name);
7448 if (name == NULL)
7449 return FALSE;
7451 /* See if we are discarding symbols with this name. */
7452 if ((finfo->info->strip == strip_some
7453 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
7454 == NULL))
7455 || (((finfo->info->discard == discard_sec_merge
7456 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
7457 || finfo->info->discard == discard_l)
7458 && bfd_is_local_label_name (input_bfd, name)))
7459 continue;
7461 /* If we get here, we are going to output this symbol. */
7463 osym = *isym;
7465 /* Adjust the section index for the output file. */
7466 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
7467 isec->output_section);
7468 if (osym.st_shndx == SHN_BAD)
7469 return FALSE;
7471 *pindex = bfd_get_symcount (output_bfd);
7473 /* ELF symbols in relocatable files are section relative, but
7474 in executable files they are virtual addresses. Note that
7475 this code assumes that all ELF sections have an associated
7476 BFD section with a reasonable value for output_offset; below
7477 we assume that they also have a reasonable value for
7478 output_section. Any special sections must be set up to meet
7479 these requirements. */
7480 osym.st_value += isec->output_offset;
7481 if (! finfo->info->relocatable)
7483 osym.st_value += isec->output_section->vma;
7484 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
7486 /* STT_TLS symbols are relative to PT_TLS segment base. */
7487 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7488 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7492 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
7493 return FALSE;
7496 /* Relocate the contents of each section. */
7497 sym_hashes = elf_sym_hashes (input_bfd);
7498 for (o = input_bfd->sections; o != NULL; o = o->next)
7500 bfd_byte *contents;
7502 if (! o->linker_mark)
7504 /* This section was omitted from the link. */
7505 continue;
7508 if ((o->flags & SEC_HAS_CONTENTS) == 0
7509 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
7510 continue;
7512 if ((o->flags & SEC_LINKER_CREATED) != 0)
7514 /* Section was created by _bfd_elf_link_create_dynamic_sections
7515 or somesuch. */
7516 continue;
7519 /* Get the contents of the section. They have been cached by a
7520 relaxation routine. Note that o is a section in an input
7521 file, so the contents field will not have been set by any of
7522 the routines which work on output files. */
7523 if (elf_section_data (o)->this_hdr.contents != NULL)
7524 contents = elf_section_data (o)->this_hdr.contents;
7525 else
7527 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7529 contents = finfo->contents;
7530 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7531 return FALSE;
7534 if ((o->flags & SEC_RELOC) != 0)
7536 Elf_Internal_Rela *internal_relocs;
7537 bfd_vma r_type_mask;
7538 int r_sym_shift;
7540 /* Get the swapped relocs. */
7541 internal_relocs
7542 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7543 finfo->internal_relocs, FALSE);
7544 if (internal_relocs == NULL
7545 && o->reloc_count > 0)
7546 return FALSE;
7548 if (bed->s->arch_size == 32)
7550 r_type_mask = 0xff;
7551 r_sym_shift = 8;
7553 else
7555 r_type_mask = 0xffffffff;
7556 r_sym_shift = 32;
7559 /* Run through the relocs looking for any against symbols
7560 from discarded sections and section symbols from
7561 removed link-once sections. Complain about relocs
7562 against discarded sections. Zero relocs against removed
7563 link-once sections. */
7564 if (!elf_section_ignore_discarded_relocs (o))
7566 Elf_Internal_Rela *rel, *relend;
7567 unsigned int action = (*bed->action_discarded) (o);
7569 rel = internal_relocs;
7570 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7571 for ( ; rel < relend; rel++)
7573 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7574 asection **ps, *sec;
7575 struct elf_link_hash_entry *h = NULL;
7576 const char *sym_name;
7578 if (r_symndx == STN_UNDEF)
7579 continue;
7581 if (r_symndx >= locsymcount
7582 || (elf_bad_symtab (input_bfd)
7583 && finfo->sections[r_symndx] == NULL))
7585 h = sym_hashes[r_symndx - extsymoff];
7587 /* Badly formatted input files can contain relocs that
7588 reference non-existant symbols. Check here so that
7589 we do not seg fault. */
7590 if (h == NULL)
7592 char buffer [32];
7594 sprintf_vma (buffer, rel->r_info);
7595 (*_bfd_error_handler)
7596 (_("error: %B contains a reloc (0x%s) for section %A "
7597 "that references a non-existent global symbol"),
7598 input_bfd, o, buffer);
7599 bfd_set_error (bfd_error_bad_value);
7600 return FALSE;
7603 while (h->root.type == bfd_link_hash_indirect
7604 || h->root.type == bfd_link_hash_warning)
7605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7607 if (h->root.type != bfd_link_hash_defined
7608 && h->root.type != bfd_link_hash_defweak)
7609 continue;
7611 ps = &h->root.u.def.section;
7612 sym_name = h->root.root.string;
7614 else
7616 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7617 ps = &finfo->sections[r_symndx];
7618 sym_name = bfd_elf_sym_name (input_bfd,
7619 symtab_hdr,
7620 sym, *ps);
7623 /* Complain if the definition comes from a
7624 discarded section. */
7625 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7627 BFD_ASSERT (r_symndx != 0);
7628 if (action & COMPLAIN)
7629 (*finfo->info->callbacks->einfo)
7630 (_("%X`%s' referenced in section `%A' of %B: "
7631 "defined in discarded section `%A' of %B\n"),
7632 sym_name, o, input_bfd, sec, sec->owner);
7634 /* Try to do the best we can to support buggy old
7635 versions of gcc. Pretend that the symbol is
7636 really defined in the kept linkonce section.
7637 FIXME: This is quite broken. Modifying the
7638 symbol here means we will be changing all later
7639 uses of the symbol, not just in this section. */
7640 if (action & PRETEND)
7642 asection *kept;
7644 kept = _bfd_elf_check_kept_section (sec,
7645 finfo->info);
7646 if (kept != NULL)
7648 *ps = kept;
7649 continue;
7653 /* Remove the symbol reference from the reloc, but
7654 don't kill the reloc completely. This is so that
7655 a zero value will be written into the section,
7656 which may have non-zero contents put there by the
7657 assembler. Zero in things like an eh_frame fde
7658 pc_begin allows stack unwinders to recognize the
7659 fde as bogus. */
7660 rel->r_info &= r_type_mask;
7661 rel->r_addend = 0;
7666 /* Relocate the section by invoking a back end routine.
7668 The back end routine is responsible for adjusting the
7669 section contents as necessary, and (if using Rela relocs
7670 and generating a relocatable output file) adjusting the
7671 reloc addend as necessary.
7673 The back end routine does not have to worry about setting
7674 the reloc address or the reloc symbol index.
7676 The back end routine is given a pointer to the swapped in
7677 internal symbols, and can access the hash table entries
7678 for the external symbols via elf_sym_hashes (input_bfd).
7680 When generating relocatable output, the back end routine
7681 must handle STB_LOCAL/STT_SECTION symbols specially. The
7682 output symbol is going to be a section symbol
7683 corresponding to the output section, which will require
7684 the addend to be adjusted. */
7686 if (! (*relocate_section) (output_bfd, finfo->info,
7687 input_bfd, o, contents,
7688 internal_relocs,
7689 isymbuf,
7690 finfo->sections))
7691 return FALSE;
7693 if (emit_relocs)
7695 Elf_Internal_Rela *irela;
7696 Elf_Internal_Rela *irelaend;
7697 bfd_vma last_offset;
7698 struct elf_link_hash_entry **rel_hash;
7699 struct elf_link_hash_entry **rel_hash_list;
7700 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7701 unsigned int next_erel;
7702 bfd_boolean rela_normal;
7704 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7705 rela_normal = (bed->rela_normal
7706 && (input_rel_hdr->sh_entsize
7707 == bed->s->sizeof_rela));
7709 /* Adjust the reloc addresses and symbol indices. */
7711 irela = internal_relocs;
7712 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7713 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7714 + elf_section_data (o->output_section)->rel_count
7715 + elf_section_data (o->output_section)->rel_count2);
7716 rel_hash_list = rel_hash;
7717 last_offset = o->output_offset;
7718 if (!finfo->info->relocatable)
7719 last_offset += o->output_section->vma;
7720 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7722 unsigned long r_symndx;
7723 asection *sec;
7724 Elf_Internal_Sym sym;
7726 if (next_erel == bed->s->int_rels_per_ext_rel)
7728 rel_hash++;
7729 next_erel = 0;
7732 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7733 finfo->info, o,
7734 irela->r_offset);
7735 if (irela->r_offset >= (bfd_vma) -2)
7737 /* This is a reloc for a deleted entry or somesuch.
7738 Turn it into an R_*_NONE reloc, at the same
7739 offset as the last reloc. elf_eh_frame.c and
7740 bfd_elf_discard_info rely on reloc offsets
7741 being ordered. */
7742 irela->r_offset = last_offset;
7743 irela->r_info = 0;
7744 irela->r_addend = 0;
7745 continue;
7748 irela->r_offset += o->output_offset;
7750 /* Relocs in an executable have to be virtual addresses. */
7751 if (!finfo->info->relocatable)
7752 irela->r_offset += o->output_section->vma;
7754 last_offset = irela->r_offset;
7756 r_symndx = irela->r_info >> r_sym_shift;
7757 if (r_symndx == STN_UNDEF)
7758 continue;
7760 if (r_symndx >= locsymcount
7761 || (elf_bad_symtab (input_bfd)
7762 && finfo->sections[r_symndx] == NULL))
7764 struct elf_link_hash_entry *rh;
7765 unsigned long indx;
7767 /* This is a reloc against a global symbol. We
7768 have not yet output all the local symbols, so
7769 we do not know the symbol index of any global
7770 symbol. We set the rel_hash entry for this
7771 reloc to point to the global hash table entry
7772 for this symbol. The symbol index is then
7773 set at the end of bfd_elf_final_link. */
7774 indx = r_symndx - extsymoff;
7775 rh = elf_sym_hashes (input_bfd)[indx];
7776 while (rh->root.type == bfd_link_hash_indirect
7777 || rh->root.type == bfd_link_hash_warning)
7778 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7780 /* Setting the index to -2 tells
7781 elf_link_output_extsym that this symbol is
7782 used by a reloc. */
7783 BFD_ASSERT (rh->indx < 0);
7784 rh->indx = -2;
7786 *rel_hash = rh;
7788 continue;
7791 /* This is a reloc against a local symbol. */
7793 *rel_hash = NULL;
7794 sym = isymbuf[r_symndx];
7795 sec = finfo->sections[r_symndx];
7796 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7798 /* I suppose the backend ought to fill in the
7799 section of any STT_SECTION symbol against a
7800 processor specific section. */
7801 r_symndx = 0;
7802 if (bfd_is_abs_section (sec))
7804 else if (sec == NULL || sec->owner == NULL)
7806 bfd_set_error (bfd_error_bad_value);
7807 return FALSE;
7809 else
7811 asection *osec = sec->output_section;
7813 /* If we have discarded a section, the output
7814 section will be the absolute section. In
7815 case of discarded link-once and discarded
7816 SEC_MERGE sections, use the kept section. */
7817 if (bfd_is_abs_section (osec)
7818 && sec->kept_section != NULL
7819 && sec->kept_section->output_section != NULL)
7821 osec = sec->kept_section->output_section;
7822 irela->r_addend -= osec->vma;
7825 if (!bfd_is_abs_section (osec))
7827 r_symndx = osec->target_index;
7828 if (r_symndx == 0)
7830 struct elf_link_hash_table *htab;
7831 asection *oi;
7833 htab = elf_hash_table (finfo->info);
7834 oi = htab->text_index_section;
7835 if ((osec->flags & SEC_READONLY) == 0
7836 && htab->data_index_section != NULL)
7837 oi = htab->data_index_section;
7839 if (oi != NULL)
7841 irela->r_addend += osec->vma - oi->vma;
7842 r_symndx = oi->target_index;
7846 BFD_ASSERT (r_symndx != 0);
7850 /* Adjust the addend according to where the
7851 section winds up in the output section. */
7852 if (rela_normal)
7853 irela->r_addend += sec->output_offset;
7855 else
7857 if (finfo->indices[r_symndx] == -1)
7859 unsigned long shlink;
7860 const char *name;
7861 asection *osec;
7863 if (finfo->info->strip == strip_all)
7865 /* You can't do ld -r -s. */
7866 bfd_set_error (bfd_error_invalid_operation);
7867 return FALSE;
7870 /* This symbol was skipped earlier, but
7871 since it is needed by a reloc, we
7872 must output it now. */
7873 shlink = symtab_hdr->sh_link;
7874 name = (bfd_elf_string_from_elf_section
7875 (input_bfd, shlink, sym.st_name));
7876 if (name == NULL)
7877 return FALSE;
7879 osec = sec->output_section;
7880 sym.st_shndx =
7881 _bfd_elf_section_from_bfd_section (output_bfd,
7882 osec);
7883 if (sym.st_shndx == SHN_BAD)
7884 return FALSE;
7886 sym.st_value += sec->output_offset;
7887 if (! finfo->info->relocatable)
7889 sym.st_value += osec->vma;
7890 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7892 /* STT_TLS symbols are relative to PT_TLS
7893 segment base. */
7894 BFD_ASSERT (elf_hash_table (finfo->info)
7895 ->tls_sec != NULL);
7896 sym.st_value -= (elf_hash_table (finfo->info)
7897 ->tls_sec->vma);
7901 finfo->indices[r_symndx]
7902 = bfd_get_symcount (output_bfd);
7904 if (! elf_link_output_sym (finfo, name, &sym, sec,
7905 NULL))
7906 return FALSE;
7909 r_symndx = finfo->indices[r_symndx];
7912 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7913 | (irela->r_info & r_type_mask));
7916 /* Swap out the relocs. */
7917 if (input_rel_hdr->sh_size != 0
7918 && !bed->elf_backend_emit_relocs (output_bfd, o,
7919 input_rel_hdr,
7920 internal_relocs,
7921 rel_hash_list))
7922 return FALSE;
7924 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7925 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7927 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7928 * bed->s->int_rels_per_ext_rel);
7929 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7930 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7931 input_rel_hdr2,
7932 internal_relocs,
7933 rel_hash_list))
7934 return FALSE;
7939 /* Write out the modified section contents. */
7940 if (bed->elf_backend_write_section
7941 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7943 /* Section written out. */
7945 else switch (o->sec_info_type)
7947 case ELF_INFO_TYPE_STABS:
7948 if (! (_bfd_write_section_stabs
7949 (output_bfd,
7950 &elf_hash_table (finfo->info)->stab_info,
7951 o, &elf_section_data (o)->sec_info, contents)))
7952 return FALSE;
7953 break;
7954 case ELF_INFO_TYPE_MERGE:
7955 if (! _bfd_write_merged_section (output_bfd, o,
7956 elf_section_data (o)->sec_info))
7957 return FALSE;
7958 break;
7959 case ELF_INFO_TYPE_EH_FRAME:
7961 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7962 o, contents))
7963 return FALSE;
7965 break;
7966 default:
7968 if (! (o->flags & SEC_EXCLUDE)
7969 && ! bfd_set_section_contents (output_bfd, o->output_section,
7970 contents,
7971 (file_ptr) o->output_offset,
7972 o->size))
7973 return FALSE;
7975 break;
7979 return TRUE;
7982 /* Generate a reloc when linking an ELF file. This is a reloc
7983 requested by the linker, and does not come from any input file. This
7984 is used to build constructor and destructor tables when linking
7985 with -Ur. */
7987 static bfd_boolean
7988 elf_reloc_link_order (bfd *output_bfd,
7989 struct bfd_link_info *info,
7990 asection *output_section,
7991 struct bfd_link_order *link_order)
7993 reloc_howto_type *howto;
7994 long indx;
7995 bfd_vma offset;
7996 bfd_vma addend;
7997 struct elf_link_hash_entry **rel_hash_ptr;
7998 Elf_Internal_Shdr *rel_hdr;
7999 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
8000 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
8001 bfd_byte *erel;
8002 unsigned int i;
8004 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
8005 if (howto == NULL)
8007 bfd_set_error (bfd_error_bad_value);
8008 return FALSE;
8011 addend = link_order->u.reloc.p->addend;
8013 /* Figure out the symbol index. */
8014 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
8015 + elf_section_data (output_section)->rel_count
8016 + elf_section_data (output_section)->rel_count2);
8017 if (link_order->type == bfd_section_reloc_link_order)
8019 indx = link_order->u.reloc.p->u.section->target_index;
8020 BFD_ASSERT (indx != 0);
8021 *rel_hash_ptr = NULL;
8023 else
8025 struct elf_link_hash_entry *h;
8027 /* Treat a reloc against a defined symbol as though it were
8028 actually against the section. */
8029 h = ((struct elf_link_hash_entry *)
8030 bfd_wrapped_link_hash_lookup (output_bfd, info,
8031 link_order->u.reloc.p->u.name,
8032 FALSE, FALSE, TRUE));
8033 if (h != NULL
8034 && (h->root.type == bfd_link_hash_defined
8035 || h->root.type == bfd_link_hash_defweak))
8037 asection *section;
8039 section = h->root.u.def.section;
8040 indx = section->output_section->target_index;
8041 *rel_hash_ptr = NULL;
8042 /* It seems that we ought to add the symbol value to the
8043 addend here, but in practice it has already been added
8044 because it was passed to constructor_callback. */
8045 addend += section->output_section->vma + section->output_offset;
8047 else if (h != NULL)
8049 /* Setting the index to -2 tells elf_link_output_extsym that
8050 this symbol is used by a reloc. */
8051 h->indx = -2;
8052 *rel_hash_ptr = h;
8053 indx = 0;
8055 else
8057 if (! ((*info->callbacks->unattached_reloc)
8058 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
8059 return FALSE;
8060 indx = 0;
8064 /* If this is an inplace reloc, we must write the addend into the
8065 object file. */
8066 if (howto->partial_inplace && addend != 0)
8068 bfd_size_type size;
8069 bfd_reloc_status_type rstat;
8070 bfd_byte *buf;
8071 bfd_boolean ok;
8072 const char *sym_name;
8074 size = bfd_get_reloc_size (howto);
8075 buf = bfd_zmalloc (size);
8076 if (buf == NULL)
8077 return FALSE;
8078 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
8079 switch (rstat)
8081 case bfd_reloc_ok:
8082 break;
8084 default:
8085 case bfd_reloc_outofrange:
8086 abort ();
8088 case bfd_reloc_overflow:
8089 if (link_order->type == bfd_section_reloc_link_order)
8090 sym_name = bfd_section_name (output_bfd,
8091 link_order->u.reloc.p->u.section);
8092 else
8093 sym_name = link_order->u.reloc.p->u.name;
8094 if (! ((*info->callbacks->reloc_overflow)
8095 (info, NULL, sym_name, howto->name, addend, NULL,
8096 NULL, (bfd_vma) 0)))
8098 free (buf);
8099 return FALSE;
8101 break;
8103 ok = bfd_set_section_contents (output_bfd, output_section, buf,
8104 link_order->offset, size);
8105 free (buf);
8106 if (! ok)
8107 return FALSE;
8110 /* The address of a reloc is relative to the section in a
8111 relocatable file, and is a virtual address in an executable
8112 file. */
8113 offset = link_order->offset;
8114 if (! info->relocatable)
8115 offset += output_section->vma;
8117 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
8119 irel[i].r_offset = offset;
8120 irel[i].r_info = 0;
8121 irel[i].r_addend = 0;
8123 if (bed->s->arch_size == 32)
8124 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
8125 else
8126 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
8128 rel_hdr = &elf_section_data (output_section)->rel_hdr;
8129 erel = rel_hdr->contents;
8130 if (rel_hdr->sh_type == SHT_REL)
8132 erel += (elf_section_data (output_section)->rel_count
8133 * bed->s->sizeof_rel);
8134 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
8136 else
8138 irel[0].r_addend = addend;
8139 erel += (elf_section_data (output_section)->rel_count
8140 * bed->s->sizeof_rela);
8141 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
8144 ++elf_section_data (output_section)->rel_count;
8146 return TRUE;
8150 /* Get the output vma of the section pointed to by the sh_link field. */
8152 static bfd_vma
8153 elf_get_linked_section_vma (struct bfd_link_order *p)
8155 Elf_Internal_Shdr **elf_shdrp;
8156 asection *s;
8157 int elfsec;
8159 s = p->u.indirect.section;
8160 elf_shdrp = elf_elfsections (s->owner);
8161 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
8162 elfsec = elf_shdrp[elfsec]->sh_link;
8163 /* PR 290:
8164 The Intel C compiler generates SHT_IA_64_UNWIND with
8165 SHF_LINK_ORDER. But it doesn't set the sh_link or
8166 sh_info fields. Hence we could get the situation
8167 where elfsec is 0. */
8168 if (elfsec == 0)
8170 const struct elf_backend_data *bed
8171 = get_elf_backend_data (s->owner);
8172 if (bed->link_order_error_handler)
8173 bed->link_order_error_handler
8174 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
8175 return 0;
8177 else
8179 s = elf_shdrp[elfsec]->bfd_section;
8180 return s->output_section->vma + s->output_offset;
8185 /* Compare two sections based on the locations of the sections they are
8186 linked to. Used by elf_fixup_link_order. */
8188 static int
8189 compare_link_order (const void * a, const void * b)
8191 bfd_vma apos;
8192 bfd_vma bpos;
8194 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
8195 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
8196 if (apos < bpos)
8197 return -1;
8198 return apos > bpos;
8202 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8203 order as their linked sections. Returns false if this could not be done
8204 because an output section includes both ordered and unordered
8205 sections. Ideally we'd do this in the linker proper. */
8207 static bfd_boolean
8208 elf_fixup_link_order (bfd *abfd, asection *o)
8210 int seen_linkorder;
8211 int seen_other;
8212 int n;
8213 struct bfd_link_order *p;
8214 bfd *sub;
8215 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8216 unsigned elfsec;
8217 struct bfd_link_order **sections;
8218 asection *s, *other_sec, *linkorder_sec;
8219 bfd_vma offset;
8221 other_sec = NULL;
8222 linkorder_sec = NULL;
8223 seen_other = 0;
8224 seen_linkorder = 0;
8225 for (p = o->map_head.link_order; p != NULL; p = p->next)
8227 if (p->type == bfd_indirect_link_order)
8229 s = p->u.indirect.section;
8230 sub = s->owner;
8231 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
8232 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
8233 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
8234 && elfsec < elf_numsections (sub)
8235 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
8237 seen_linkorder++;
8238 linkorder_sec = s;
8240 else
8242 seen_other++;
8243 other_sec = s;
8246 else
8247 seen_other++;
8249 if (seen_other && seen_linkorder)
8251 if (other_sec && linkorder_sec)
8252 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8253 o, linkorder_sec,
8254 linkorder_sec->owner, other_sec,
8255 other_sec->owner);
8256 else
8257 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
8259 bfd_set_error (bfd_error_bad_value);
8260 return FALSE;
8264 if (!seen_linkorder)
8265 return TRUE;
8267 sections = (struct bfd_link_order **)
8268 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
8269 seen_linkorder = 0;
8271 for (p = o->map_head.link_order; p != NULL; p = p->next)
8273 sections[seen_linkorder++] = p;
8275 /* Sort the input sections in the order of their linked section. */
8276 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
8277 compare_link_order);
8279 /* Change the offsets of the sections. */
8280 offset = 0;
8281 for (n = 0; n < seen_linkorder; n++)
8283 s = sections[n]->u.indirect.section;
8284 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
8285 s->output_offset = offset;
8286 sections[n]->offset = offset;
8287 offset += sections[n]->size;
8290 return TRUE;
8294 /* Do the final step of an ELF link. */
8296 bfd_boolean
8297 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8299 bfd_boolean dynamic;
8300 bfd_boolean emit_relocs;
8301 bfd *dynobj;
8302 struct elf_final_link_info finfo;
8303 register asection *o;
8304 register struct bfd_link_order *p;
8305 register bfd *sub;
8306 bfd_size_type max_contents_size;
8307 bfd_size_type max_external_reloc_size;
8308 bfd_size_type max_internal_reloc_count;
8309 bfd_size_type max_sym_count;
8310 bfd_size_type max_sym_shndx_count;
8311 file_ptr off;
8312 Elf_Internal_Sym elfsym;
8313 unsigned int i;
8314 Elf_Internal_Shdr *symtab_hdr;
8315 Elf_Internal_Shdr *symtab_shndx_hdr;
8316 Elf_Internal_Shdr *symstrtab_hdr;
8317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8318 struct elf_outext_info eoinfo;
8319 bfd_boolean merged;
8320 size_t relativecount = 0;
8321 asection *reldyn = 0;
8322 bfd_size_type amt;
8324 if (! is_elf_hash_table (info->hash))
8325 return FALSE;
8327 if (info->shared)
8328 abfd->flags |= DYNAMIC;
8330 dynamic = elf_hash_table (info)->dynamic_sections_created;
8331 dynobj = elf_hash_table (info)->dynobj;
8333 emit_relocs = (info->relocatable
8334 || info->emitrelocations);
8336 finfo.info = info;
8337 finfo.output_bfd = abfd;
8338 finfo.symstrtab = _bfd_elf_stringtab_init ();
8339 if (finfo.symstrtab == NULL)
8340 return FALSE;
8342 if (! dynamic)
8344 finfo.dynsym_sec = NULL;
8345 finfo.hash_sec = NULL;
8346 finfo.symver_sec = NULL;
8348 else
8350 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
8351 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
8352 BFD_ASSERT (finfo.dynsym_sec != NULL);
8353 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
8354 /* Note that it is OK if symver_sec is NULL. */
8357 finfo.contents = NULL;
8358 finfo.external_relocs = NULL;
8359 finfo.internal_relocs = NULL;
8360 finfo.external_syms = NULL;
8361 finfo.locsym_shndx = NULL;
8362 finfo.internal_syms = NULL;
8363 finfo.indices = NULL;
8364 finfo.sections = NULL;
8365 finfo.symbuf = NULL;
8366 finfo.symshndxbuf = NULL;
8367 finfo.symbuf_count = 0;
8368 finfo.shndxbuf_size = 0;
8370 /* Count up the number of relocations we will output for each output
8371 section, so that we know the sizes of the reloc sections. We
8372 also figure out some maximum sizes. */
8373 max_contents_size = 0;
8374 max_external_reloc_size = 0;
8375 max_internal_reloc_count = 0;
8376 max_sym_count = 0;
8377 max_sym_shndx_count = 0;
8378 merged = FALSE;
8379 for (o = abfd->sections; o != NULL; o = o->next)
8381 struct bfd_elf_section_data *esdo = elf_section_data (o);
8382 o->reloc_count = 0;
8384 for (p = o->map_head.link_order; p != NULL; p = p->next)
8386 unsigned int reloc_count = 0;
8387 struct bfd_elf_section_data *esdi = NULL;
8388 unsigned int *rel_count1;
8390 if (p->type == bfd_section_reloc_link_order
8391 || p->type == bfd_symbol_reloc_link_order)
8392 reloc_count = 1;
8393 else if (p->type == bfd_indirect_link_order)
8395 asection *sec;
8397 sec = p->u.indirect.section;
8398 esdi = elf_section_data (sec);
8400 /* Mark all sections which are to be included in the
8401 link. This will normally be every section. We need
8402 to do this so that we can identify any sections which
8403 the linker has decided to not include. */
8404 sec->linker_mark = TRUE;
8406 if (sec->flags & SEC_MERGE)
8407 merged = TRUE;
8409 if (info->relocatable || info->emitrelocations)
8410 reloc_count = sec->reloc_count;
8411 else if (bed->elf_backend_count_relocs)
8413 Elf_Internal_Rela * relocs;
8415 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8416 info->keep_memory);
8418 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
8420 if (elf_section_data (o)->relocs != relocs)
8421 free (relocs);
8424 if (sec->rawsize > max_contents_size)
8425 max_contents_size = sec->rawsize;
8426 if (sec->size > max_contents_size)
8427 max_contents_size = sec->size;
8429 /* We are interested in just local symbols, not all
8430 symbols. */
8431 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
8432 && (sec->owner->flags & DYNAMIC) == 0)
8434 size_t sym_count;
8436 if (elf_bad_symtab (sec->owner))
8437 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
8438 / bed->s->sizeof_sym);
8439 else
8440 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
8442 if (sym_count > max_sym_count)
8443 max_sym_count = sym_count;
8445 if (sym_count > max_sym_shndx_count
8446 && elf_symtab_shndx (sec->owner) != 0)
8447 max_sym_shndx_count = sym_count;
8449 if ((sec->flags & SEC_RELOC) != 0)
8451 size_t ext_size;
8453 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
8454 if (ext_size > max_external_reloc_size)
8455 max_external_reloc_size = ext_size;
8456 if (sec->reloc_count > max_internal_reloc_count)
8457 max_internal_reloc_count = sec->reloc_count;
8462 if (reloc_count == 0)
8463 continue;
8465 o->reloc_count += reloc_count;
8467 /* MIPS may have a mix of REL and RELA relocs on sections.
8468 To support this curious ABI we keep reloc counts in
8469 elf_section_data too. We must be careful to add the
8470 relocations from the input section to the right output
8471 count. FIXME: Get rid of one count. We have
8472 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
8473 rel_count1 = &esdo->rel_count;
8474 if (esdi != NULL)
8476 bfd_boolean same_size;
8477 bfd_size_type entsize1;
8479 entsize1 = esdi->rel_hdr.sh_entsize;
8480 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
8481 || entsize1 == bed->s->sizeof_rela);
8482 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
8484 if (!same_size)
8485 rel_count1 = &esdo->rel_count2;
8487 if (esdi->rel_hdr2 != NULL)
8489 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
8490 unsigned int alt_count;
8491 unsigned int *rel_count2;
8493 BFD_ASSERT (entsize2 != entsize1
8494 && (entsize2 == bed->s->sizeof_rel
8495 || entsize2 == bed->s->sizeof_rela));
8497 rel_count2 = &esdo->rel_count2;
8498 if (!same_size)
8499 rel_count2 = &esdo->rel_count;
8501 /* The following is probably too simplistic if the
8502 backend counts output relocs unusually. */
8503 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
8504 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
8505 *rel_count2 += alt_count;
8506 reloc_count -= alt_count;
8509 *rel_count1 += reloc_count;
8512 if (o->reloc_count > 0)
8513 o->flags |= SEC_RELOC;
8514 else
8516 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8517 set it (this is probably a bug) and if it is set
8518 assign_section_numbers will create a reloc section. */
8519 o->flags &=~ SEC_RELOC;
8522 /* If the SEC_ALLOC flag is not set, force the section VMA to
8523 zero. This is done in elf_fake_sections as well, but forcing
8524 the VMA to 0 here will ensure that relocs against these
8525 sections are handled correctly. */
8526 if ((o->flags & SEC_ALLOC) == 0
8527 && ! o->user_set_vma)
8528 o->vma = 0;
8531 if (! info->relocatable && merged)
8532 elf_link_hash_traverse (elf_hash_table (info),
8533 _bfd_elf_link_sec_merge_syms, abfd);
8535 /* Figure out the file positions for everything but the symbol table
8536 and the relocs. We set symcount to force assign_section_numbers
8537 to create a symbol table. */
8538 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
8539 BFD_ASSERT (! abfd->output_has_begun);
8540 if (! _bfd_elf_compute_section_file_positions (abfd, info))
8541 goto error_return;
8543 /* Set sizes, and assign file positions for reloc sections. */
8544 for (o = abfd->sections; o != NULL; o = o->next)
8546 if ((o->flags & SEC_RELOC) != 0)
8548 if (!(_bfd_elf_link_size_reloc_section
8549 (abfd, &elf_section_data (o)->rel_hdr, o)))
8550 goto error_return;
8552 if (elf_section_data (o)->rel_hdr2
8553 && !(_bfd_elf_link_size_reloc_section
8554 (abfd, elf_section_data (o)->rel_hdr2, o)))
8555 goto error_return;
8558 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8559 to count upwards while actually outputting the relocations. */
8560 elf_section_data (o)->rel_count = 0;
8561 elf_section_data (o)->rel_count2 = 0;
8564 _bfd_elf_assign_file_positions_for_relocs (abfd);
8566 /* We have now assigned file positions for all the sections except
8567 .symtab and .strtab. We start the .symtab section at the current
8568 file position, and write directly to it. We build the .strtab
8569 section in memory. */
8570 bfd_get_symcount (abfd) = 0;
8571 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8572 /* sh_name is set in prep_headers. */
8573 symtab_hdr->sh_type = SHT_SYMTAB;
8574 /* sh_flags, sh_addr and sh_size all start off zero. */
8575 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8576 /* sh_link is set in assign_section_numbers. */
8577 /* sh_info is set below. */
8578 /* sh_offset is set just below. */
8579 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8581 off = elf_tdata (abfd)->next_file_pos;
8582 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8584 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8585 incorrect. We do not yet know the size of the .symtab section.
8586 We correct next_file_pos below, after we do know the size. */
8588 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8589 continuously seeking to the right position in the file. */
8590 if (! info->keep_memory || max_sym_count < 20)
8591 finfo.symbuf_size = 20;
8592 else
8593 finfo.symbuf_size = max_sym_count;
8594 amt = finfo.symbuf_size;
8595 amt *= bed->s->sizeof_sym;
8596 finfo.symbuf = bfd_malloc (amt);
8597 if (finfo.symbuf == NULL)
8598 goto error_return;
8599 if (elf_numsections (abfd) > SHN_LORESERVE)
8601 /* Wild guess at number of output symbols. realloc'd as needed. */
8602 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8603 finfo.shndxbuf_size = amt;
8604 amt *= sizeof (Elf_External_Sym_Shndx);
8605 finfo.symshndxbuf = bfd_zmalloc (amt);
8606 if (finfo.symshndxbuf == NULL)
8607 goto error_return;
8610 /* Start writing out the symbol table. The first symbol is always a
8611 dummy symbol. */
8612 if (info->strip != strip_all
8613 || emit_relocs)
8615 elfsym.st_value = 0;
8616 elfsym.st_size = 0;
8617 elfsym.st_info = 0;
8618 elfsym.st_other = 0;
8619 elfsym.st_shndx = SHN_UNDEF;
8620 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8621 NULL))
8622 goto error_return;
8625 /* Output a symbol for each section. We output these even if we are
8626 discarding local symbols, since they are used for relocs. These
8627 symbols have no names. We store the index of each one in the
8628 index field of the section, so that we can find it again when
8629 outputting relocs. */
8630 if (info->strip != strip_all
8631 || emit_relocs)
8633 elfsym.st_size = 0;
8634 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8635 elfsym.st_other = 0;
8636 elfsym.st_value = 0;
8637 for (i = 1; i < elf_numsections (abfd); i++)
8639 o = bfd_section_from_elf_index (abfd, i);
8640 if (o != NULL)
8642 o->target_index = bfd_get_symcount (abfd);
8643 elfsym.st_shndx = i;
8644 if (!info->relocatable)
8645 elfsym.st_value = o->vma;
8646 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8647 goto error_return;
8649 if (i == SHN_LORESERVE - 1)
8650 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8654 /* Allocate some memory to hold information read in from the input
8655 files. */
8656 if (max_contents_size != 0)
8658 finfo.contents = bfd_malloc (max_contents_size);
8659 if (finfo.contents == NULL)
8660 goto error_return;
8663 if (max_external_reloc_size != 0)
8665 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8666 if (finfo.external_relocs == NULL)
8667 goto error_return;
8670 if (max_internal_reloc_count != 0)
8672 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8673 amt *= sizeof (Elf_Internal_Rela);
8674 finfo.internal_relocs = bfd_malloc (amt);
8675 if (finfo.internal_relocs == NULL)
8676 goto error_return;
8679 if (max_sym_count != 0)
8681 amt = max_sym_count * bed->s->sizeof_sym;
8682 finfo.external_syms = bfd_malloc (amt);
8683 if (finfo.external_syms == NULL)
8684 goto error_return;
8686 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8687 finfo.internal_syms = bfd_malloc (amt);
8688 if (finfo.internal_syms == NULL)
8689 goto error_return;
8691 amt = max_sym_count * sizeof (long);
8692 finfo.indices = bfd_malloc (amt);
8693 if (finfo.indices == NULL)
8694 goto error_return;
8696 amt = max_sym_count * sizeof (asection *);
8697 finfo.sections = bfd_malloc (amt);
8698 if (finfo.sections == NULL)
8699 goto error_return;
8702 if (max_sym_shndx_count != 0)
8704 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8705 finfo.locsym_shndx = bfd_malloc (amt);
8706 if (finfo.locsym_shndx == NULL)
8707 goto error_return;
8710 if (elf_hash_table (info)->tls_sec)
8712 bfd_vma base, end = 0;
8713 asection *sec;
8715 for (sec = elf_hash_table (info)->tls_sec;
8716 sec && (sec->flags & SEC_THREAD_LOCAL);
8717 sec = sec->next)
8719 bfd_size_type size = sec->size;
8721 if (size == 0
8722 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8724 struct bfd_link_order *o = sec->map_tail.link_order;
8725 if (o != NULL)
8726 size = o->offset + o->size;
8728 end = sec->vma + size;
8730 base = elf_hash_table (info)->tls_sec->vma;
8731 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8732 elf_hash_table (info)->tls_size = end - base;
8735 /* Reorder SHF_LINK_ORDER sections. */
8736 for (o = abfd->sections; o != NULL; o = o->next)
8738 if (!elf_fixup_link_order (abfd, o))
8739 return FALSE;
8742 /* Since ELF permits relocations to be against local symbols, we
8743 must have the local symbols available when we do the relocations.
8744 Since we would rather only read the local symbols once, and we
8745 would rather not keep them in memory, we handle all the
8746 relocations for a single input file at the same time.
8748 Unfortunately, there is no way to know the total number of local
8749 symbols until we have seen all of them, and the local symbol
8750 indices precede the global symbol indices. This means that when
8751 we are generating relocatable output, and we see a reloc against
8752 a global symbol, we can not know the symbol index until we have
8753 finished examining all the local symbols to see which ones we are
8754 going to output. To deal with this, we keep the relocations in
8755 memory, and don't output them until the end of the link. This is
8756 an unfortunate waste of memory, but I don't see a good way around
8757 it. Fortunately, it only happens when performing a relocatable
8758 link, which is not the common case. FIXME: If keep_memory is set
8759 we could write the relocs out and then read them again; I don't
8760 know how bad the memory loss will be. */
8762 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8763 sub->output_has_begun = FALSE;
8764 for (o = abfd->sections; o != NULL; o = o->next)
8766 for (p = o->map_head.link_order; p != NULL; p = p->next)
8768 if (p->type == bfd_indirect_link_order
8769 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8770 == bfd_target_elf_flavour)
8771 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8773 if (! sub->output_has_begun)
8775 if (! elf_link_input_bfd (&finfo, sub))
8776 goto error_return;
8777 sub->output_has_begun = TRUE;
8780 else if (p->type == bfd_section_reloc_link_order
8781 || p->type == bfd_symbol_reloc_link_order)
8783 if (! elf_reloc_link_order (abfd, info, o, p))
8784 goto error_return;
8786 else
8788 if (! _bfd_default_link_order (abfd, info, o, p))
8789 goto error_return;
8794 /* Free symbol buffer if needed. */
8795 if (!info->reduce_memory_overheads)
8797 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8798 if (elf_tdata (sub)->symbuf)
8800 free (elf_tdata (sub)->symbuf);
8801 elf_tdata (sub)->symbuf = NULL;
8805 /* Output any global symbols that got converted to local in a
8806 version script or due to symbol visibility. We do this in a
8807 separate step since ELF requires all local symbols to appear
8808 prior to any global symbols. FIXME: We should only do this if
8809 some global symbols were, in fact, converted to become local.
8810 FIXME: Will this work correctly with the Irix 5 linker? */
8811 eoinfo.failed = FALSE;
8812 eoinfo.finfo = &finfo;
8813 eoinfo.localsyms = TRUE;
8814 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8815 &eoinfo);
8816 if (eoinfo.failed)
8817 return FALSE;
8819 /* If backend needs to output some local symbols not present in the hash
8820 table, do it now. */
8821 if (bed->elf_backend_output_arch_local_syms)
8823 typedef bfd_boolean (*out_sym_func)
8824 (void *, const char *, Elf_Internal_Sym *, asection *,
8825 struct elf_link_hash_entry *);
8827 if (! ((*bed->elf_backend_output_arch_local_syms)
8828 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8829 return FALSE;
8832 /* That wrote out all the local symbols. Finish up the symbol table
8833 with the global symbols. Even if we want to strip everything we
8834 can, we still need to deal with those global symbols that got
8835 converted to local in a version script. */
8837 /* The sh_info field records the index of the first non local symbol. */
8838 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8840 if (dynamic
8841 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8843 Elf_Internal_Sym sym;
8844 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8845 long last_local = 0;
8847 /* Write out the section symbols for the output sections. */
8848 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8850 asection *s;
8852 sym.st_size = 0;
8853 sym.st_name = 0;
8854 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8855 sym.st_other = 0;
8857 for (s = abfd->sections; s != NULL; s = s->next)
8859 int indx;
8860 bfd_byte *dest;
8861 long dynindx;
8863 dynindx = elf_section_data (s)->dynindx;
8864 if (dynindx <= 0)
8865 continue;
8866 indx = elf_section_data (s)->this_idx;
8867 BFD_ASSERT (indx > 0);
8868 sym.st_shndx = indx;
8869 if (! check_dynsym (abfd, &sym))
8870 return FALSE;
8871 sym.st_value = s->vma;
8872 dest = dynsym + dynindx * bed->s->sizeof_sym;
8873 if (last_local < dynindx)
8874 last_local = dynindx;
8875 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8879 /* Write out the local dynsyms. */
8880 if (elf_hash_table (info)->dynlocal)
8882 struct elf_link_local_dynamic_entry *e;
8883 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8885 asection *s;
8886 bfd_byte *dest;
8888 sym.st_size = e->isym.st_size;
8889 sym.st_other = e->isym.st_other;
8891 /* Copy the internal symbol as is.
8892 Note that we saved a word of storage and overwrote
8893 the original st_name with the dynstr_index. */
8894 sym = e->isym;
8896 if (e->isym.st_shndx != SHN_UNDEF
8897 && (e->isym.st_shndx < SHN_LORESERVE
8898 || e->isym.st_shndx > SHN_HIRESERVE))
8900 s = bfd_section_from_elf_index (e->input_bfd,
8901 e->isym.st_shndx);
8903 sym.st_shndx =
8904 elf_section_data (s->output_section)->this_idx;
8905 if (! check_dynsym (abfd, &sym))
8906 return FALSE;
8907 sym.st_value = (s->output_section->vma
8908 + s->output_offset
8909 + e->isym.st_value);
8912 if (last_local < e->dynindx)
8913 last_local = e->dynindx;
8915 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8916 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8920 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8921 last_local + 1;
8924 /* We get the global symbols from the hash table. */
8925 eoinfo.failed = FALSE;
8926 eoinfo.localsyms = FALSE;
8927 eoinfo.finfo = &finfo;
8928 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8929 &eoinfo);
8930 if (eoinfo.failed)
8931 return FALSE;
8933 /* If backend needs to output some symbols not present in the hash
8934 table, do it now. */
8935 if (bed->elf_backend_output_arch_syms)
8937 typedef bfd_boolean (*out_sym_func)
8938 (void *, const char *, Elf_Internal_Sym *, asection *,
8939 struct elf_link_hash_entry *);
8941 if (! ((*bed->elf_backend_output_arch_syms)
8942 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8943 return FALSE;
8946 /* Flush all symbols to the file. */
8947 if (! elf_link_flush_output_syms (&finfo, bed))
8948 return FALSE;
8950 /* Now we know the size of the symtab section. */
8951 off += symtab_hdr->sh_size;
8953 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8954 if (symtab_shndx_hdr->sh_name != 0)
8956 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8957 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8958 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8959 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8960 symtab_shndx_hdr->sh_size = amt;
8962 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8963 off, TRUE);
8965 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8966 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8967 return FALSE;
8971 /* Finish up and write out the symbol string table (.strtab)
8972 section. */
8973 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8974 /* sh_name was set in prep_headers. */
8975 symstrtab_hdr->sh_type = SHT_STRTAB;
8976 symstrtab_hdr->sh_flags = 0;
8977 symstrtab_hdr->sh_addr = 0;
8978 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8979 symstrtab_hdr->sh_entsize = 0;
8980 symstrtab_hdr->sh_link = 0;
8981 symstrtab_hdr->sh_info = 0;
8982 /* sh_offset is set just below. */
8983 symstrtab_hdr->sh_addralign = 1;
8985 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8986 elf_tdata (abfd)->next_file_pos = off;
8988 if (bfd_get_symcount (abfd) > 0)
8990 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8991 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8992 return FALSE;
8995 /* Adjust the relocs to have the correct symbol indices. */
8996 for (o = abfd->sections; o != NULL; o = o->next)
8998 if ((o->flags & SEC_RELOC) == 0)
8999 continue;
9001 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
9002 elf_section_data (o)->rel_count,
9003 elf_section_data (o)->rel_hashes);
9004 if (elf_section_data (o)->rel_hdr2 != NULL)
9005 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
9006 elf_section_data (o)->rel_count2,
9007 (elf_section_data (o)->rel_hashes
9008 + elf_section_data (o)->rel_count));
9010 /* Set the reloc_count field to 0 to prevent write_relocs from
9011 trying to swap the relocs out itself. */
9012 o->reloc_count = 0;
9015 if (dynamic && info->combreloc && dynobj != NULL)
9016 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
9018 /* If we are linking against a dynamic object, or generating a
9019 shared library, finish up the dynamic linking information. */
9020 if (dynamic)
9022 bfd_byte *dyncon, *dynconend;
9024 /* Fix up .dynamic entries. */
9025 o = bfd_get_section_by_name (dynobj, ".dynamic");
9026 BFD_ASSERT (o != NULL);
9028 dyncon = o->contents;
9029 dynconend = o->contents + o->size;
9030 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9032 Elf_Internal_Dyn dyn;
9033 const char *name;
9034 unsigned int type;
9036 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9038 switch (dyn.d_tag)
9040 default:
9041 continue;
9042 case DT_NULL:
9043 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
9045 switch (elf_section_data (reldyn)->this_hdr.sh_type)
9047 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
9048 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
9049 default: continue;
9051 dyn.d_un.d_val = relativecount;
9052 relativecount = 0;
9053 break;
9055 continue;
9057 case DT_INIT:
9058 name = info->init_function;
9059 goto get_sym;
9060 case DT_FINI:
9061 name = info->fini_function;
9062 get_sym:
9064 struct elf_link_hash_entry *h;
9066 h = elf_link_hash_lookup (elf_hash_table (info), name,
9067 FALSE, FALSE, TRUE);
9068 if (h != NULL
9069 && (h->root.type == bfd_link_hash_defined
9070 || h->root.type == bfd_link_hash_defweak))
9072 dyn.d_un.d_val = h->root.u.def.value;
9073 o = h->root.u.def.section;
9074 if (o->output_section != NULL)
9075 dyn.d_un.d_val += (o->output_section->vma
9076 + o->output_offset);
9077 else
9079 /* The symbol is imported from another shared
9080 library and does not apply to this one. */
9081 dyn.d_un.d_val = 0;
9083 break;
9086 continue;
9088 case DT_PREINIT_ARRAYSZ:
9089 name = ".preinit_array";
9090 goto get_size;
9091 case DT_INIT_ARRAYSZ:
9092 name = ".init_array";
9093 goto get_size;
9094 case DT_FINI_ARRAYSZ:
9095 name = ".fini_array";
9096 get_size:
9097 o = bfd_get_section_by_name (abfd, name);
9098 if (o == NULL)
9100 (*_bfd_error_handler)
9101 (_("%B: could not find output section %s"), abfd, name);
9102 goto error_return;
9104 if (o->size == 0)
9105 (*_bfd_error_handler)
9106 (_("warning: %s section has zero size"), name);
9107 dyn.d_un.d_val = o->size;
9108 break;
9110 case DT_PREINIT_ARRAY:
9111 name = ".preinit_array";
9112 goto get_vma;
9113 case DT_INIT_ARRAY:
9114 name = ".init_array";
9115 goto get_vma;
9116 case DT_FINI_ARRAY:
9117 name = ".fini_array";
9118 goto get_vma;
9120 case DT_HASH:
9121 name = ".hash";
9122 goto get_vma;
9123 case DT_GNU_HASH:
9124 name = ".gnu.hash";
9125 goto get_vma;
9126 case DT_STRTAB:
9127 name = ".dynstr";
9128 goto get_vma;
9129 case DT_SYMTAB:
9130 name = ".dynsym";
9131 goto get_vma;
9132 case DT_VERDEF:
9133 name = ".gnu.version_d";
9134 goto get_vma;
9135 case DT_VERNEED:
9136 name = ".gnu.version_r";
9137 goto get_vma;
9138 case DT_VERSYM:
9139 name = ".gnu.version";
9140 get_vma:
9141 o = bfd_get_section_by_name (abfd, name);
9142 if (o == NULL)
9144 (*_bfd_error_handler)
9145 (_("%B: could not find output section %s"), abfd, name);
9146 goto error_return;
9148 dyn.d_un.d_ptr = o->vma;
9149 break;
9151 case DT_REL:
9152 case DT_RELA:
9153 case DT_RELSZ:
9154 case DT_RELASZ:
9155 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
9156 type = SHT_REL;
9157 else
9158 type = SHT_RELA;
9159 dyn.d_un.d_val = 0;
9160 for (i = 1; i < elf_numsections (abfd); i++)
9162 Elf_Internal_Shdr *hdr;
9164 hdr = elf_elfsections (abfd)[i];
9165 if (hdr->sh_type == type
9166 && (hdr->sh_flags & SHF_ALLOC) != 0)
9168 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
9169 dyn.d_un.d_val += hdr->sh_size;
9170 else
9172 if (dyn.d_un.d_val == 0
9173 || hdr->sh_addr < dyn.d_un.d_val)
9174 dyn.d_un.d_val = hdr->sh_addr;
9178 break;
9180 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
9184 /* If we have created any dynamic sections, then output them. */
9185 if (dynobj != NULL)
9187 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
9188 goto error_return;
9190 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9191 if (info->warn_shared_textrel && info->shared)
9193 bfd_byte *dyncon, *dynconend;
9195 /* Fix up .dynamic entries. */
9196 o = bfd_get_section_by_name (dynobj, ".dynamic");
9197 BFD_ASSERT (o != NULL);
9199 dyncon = o->contents;
9200 dynconend = o->contents + o->size;
9201 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9203 Elf_Internal_Dyn dyn;
9205 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9207 if (dyn.d_tag == DT_TEXTREL)
9209 _bfd_error_handler
9210 (_("warning: creating a DT_TEXTREL in a shared object."));
9211 break;
9216 for (o = dynobj->sections; o != NULL; o = o->next)
9218 if ((o->flags & SEC_HAS_CONTENTS) == 0
9219 || o->size == 0
9220 || o->output_section == bfd_abs_section_ptr)
9221 continue;
9222 if ((o->flags & SEC_LINKER_CREATED) == 0)
9224 /* At this point, we are only interested in sections
9225 created by _bfd_elf_link_create_dynamic_sections. */
9226 continue;
9228 if (elf_hash_table (info)->stab_info.stabstr == o)
9229 continue;
9230 if (elf_hash_table (info)->eh_info.hdr_sec == o)
9231 continue;
9232 if ((elf_section_data (o->output_section)->this_hdr.sh_type
9233 != SHT_STRTAB)
9234 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
9236 if (! bfd_set_section_contents (abfd, o->output_section,
9237 o->contents,
9238 (file_ptr) o->output_offset,
9239 o->size))
9240 goto error_return;
9242 else
9244 /* The contents of the .dynstr section are actually in a
9245 stringtab. */
9246 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
9247 if (bfd_seek (abfd, off, SEEK_SET) != 0
9248 || ! _bfd_elf_strtab_emit (abfd,
9249 elf_hash_table (info)->dynstr))
9250 goto error_return;
9255 if (info->relocatable)
9257 bfd_boolean failed = FALSE;
9259 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
9260 if (failed)
9261 goto error_return;
9264 /* If we have optimized stabs strings, output them. */
9265 if (elf_hash_table (info)->stab_info.stabstr != NULL)
9267 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
9268 goto error_return;
9271 if (info->eh_frame_hdr)
9273 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
9274 goto error_return;
9277 if (finfo.symstrtab != NULL)
9278 _bfd_stringtab_free (finfo.symstrtab);
9279 if (finfo.contents != NULL)
9280 free (finfo.contents);
9281 if (finfo.external_relocs != NULL)
9282 free (finfo.external_relocs);
9283 if (finfo.internal_relocs != NULL)
9284 free (finfo.internal_relocs);
9285 if (finfo.external_syms != NULL)
9286 free (finfo.external_syms);
9287 if (finfo.locsym_shndx != NULL)
9288 free (finfo.locsym_shndx);
9289 if (finfo.internal_syms != NULL)
9290 free (finfo.internal_syms);
9291 if (finfo.indices != NULL)
9292 free (finfo.indices);
9293 if (finfo.sections != NULL)
9294 free (finfo.sections);
9295 if (finfo.symbuf != NULL)
9296 free (finfo.symbuf);
9297 if (finfo.symshndxbuf != NULL)
9298 free (finfo.symshndxbuf);
9299 for (o = abfd->sections; o != NULL; o = o->next)
9301 if ((o->flags & SEC_RELOC) != 0
9302 && elf_section_data (o)->rel_hashes != NULL)
9303 free (elf_section_data (o)->rel_hashes);
9306 elf_tdata (abfd)->linker = TRUE;
9308 return TRUE;
9310 error_return:
9311 if (finfo.symstrtab != NULL)
9312 _bfd_stringtab_free (finfo.symstrtab);
9313 if (finfo.contents != NULL)
9314 free (finfo.contents);
9315 if (finfo.external_relocs != NULL)
9316 free (finfo.external_relocs);
9317 if (finfo.internal_relocs != NULL)
9318 free (finfo.internal_relocs);
9319 if (finfo.external_syms != NULL)
9320 free (finfo.external_syms);
9321 if (finfo.locsym_shndx != NULL)
9322 free (finfo.locsym_shndx);
9323 if (finfo.internal_syms != NULL)
9324 free (finfo.internal_syms);
9325 if (finfo.indices != NULL)
9326 free (finfo.indices);
9327 if (finfo.sections != NULL)
9328 free (finfo.sections);
9329 if (finfo.symbuf != NULL)
9330 free (finfo.symbuf);
9331 if (finfo.symshndxbuf != NULL)
9332 free (finfo.symshndxbuf);
9333 for (o = abfd->sections; o != NULL; o = o->next)
9335 if ((o->flags & SEC_RELOC) != 0
9336 && elf_section_data (o)->rel_hashes != NULL)
9337 free (elf_section_data (o)->rel_hashes);
9340 return FALSE;
9343 /* Garbage collect unused sections. */
9345 typedef asection * (*gc_mark_hook_fn)
9346 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9347 struct elf_link_hash_entry *, Elf_Internal_Sym *);
9349 /* Default gc_mark_hook. */
9351 asection *
9352 _bfd_elf_gc_mark_hook (asection *sec,
9353 struct bfd_link_info *info ATTRIBUTE_UNUSED,
9354 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
9355 struct elf_link_hash_entry *h,
9356 Elf_Internal_Sym *sym)
9358 if (h != NULL)
9360 switch (h->root.type)
9362 case bfd_link_hash_defined:
9363 case bfd_link_hash_defweak:
9364 return h->root.u.def.section;
9366 case bfd_link_hash_common:
9367 return h->root.u.c.p->section;
9369 default:
9370 break;
9373 else
9374 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9376 return NULL;
9379 /* The mark phase of garbage collection. For a given section, mark
9380 it and any sections in this section's group, and all the sections
9381 which define symbols to which it refers. */
9383 bfd_boolean
9384 _bfd_elf_gc_mark (struct bfd_link_info *info,
9385 asection *sec,
9386 gc_mark_hook_fn gc_mark_hook)
9388 bfd_boolean ret;
9389 bfd_boolean is_eh;
9390 asection *group_sec;
9392 sec->gc_mark = 1;
9394 /* Mark all the sections in the group. */
9395 group_sec = elf_section_data (sec)->next_in_group;
9396 if (group_sec && !group_sec->gc_mark)
9397 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
9398 return FALSE;
9400 /* Look through the section relocs. */
9401 ret = TRUE;
9402 is_eh = strcmp (sec->name, ".eh_frame") == 0;
9403 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
9405 Elf_Internal_Rela *relstart, *rel, *relend;
9406 Elf_Internal_Shdr *symtab_hdr;
9407 struct elf_link_hash_entry **sym_hashes;
9408 size_t nlocsyms;
9409 size_t extsymoff;
9410 bfd *input_bfd = sec->owner;
9411 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
9412 Elf_Internal_Sym *isym = NULL;
9413 int r_sym_shift;
9415 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9416 sym_hashes = elf_sym_hashes (input_bfd);
9418 /* Read the local symbols. */
9419 if (elf_bad_symtab (input_bfd))
9421 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
9422 extsymoff = 0;
9424 else
9425 extsymoff = nlocsyms = symtab_hdr->sh_info;
9427 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
9428 if (isym == NULL && nlocsyms != 0)
9430 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
9431 NULL, NULL, NULL);
9432 if (isym == NULL)
9433 return FALSE;
9436 /* Read the relocations. */
9437 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
9438 info->keep_memory);
9439 if (relstart == NULL)
9441 ret = FALSE;
9442 goto out1;
9444 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9446 if (bed->s->arch_size == 32)
9447 r_sym_shift = 8;
9448 else
9449 r_sym_shift = 32;
9451 for (rel = relstart; rel < relend; rel++)
9453 unsigned long r_symndx;
9454 asection *rsec;
9455 struct elf_link_hash_entry *h;
9457 r_symndx = rel->r_info >> r_sym_shift;
9458 if (r_symndx == 0)
9459 continue;
9461 if (r_symndx >= nlocsyms
9462 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
9464 h = sym_hashes[r_symndx - extsymoff];
9465 while (h->root.type == bfd_link_hash_indirect
9466 || h->root.type == bfd_link_hash_warning)
9467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9468 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
9470 else
9472 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
9475 if (rsec && !rsec->gc_mark)
9477 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
9478 rsec->gc_mark = 1;
9479 else if (is_eh)
9480 rsec->gc_mark_from_eh = 1;
9481 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
9483 ret = FALSE;
9484 goto out2;
9489 out2:
9490 if (elf_section_data (sec)->relocs != relstart)
9491 free (relstart);
9492 out1:
9493 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
9495 if (! info->keep_memory)
9496 free (isym);
9497 else
9498 symtab_hdr->contents = (unsigned char *) isym;
9502 return ret;
9505 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
9507 struct elf_gc_sweep_symbol_info
9509 struct bfd_link_info *info;
9510 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
9511 bfd_boolean);
9514 static bfd_boolean
9515 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
9517 if (h->root.type == bfd_link_hash_warning)
9518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9520 if ((h->root.type == bfd_link_hash_defined
9521 || h->root.type == bfd_link_hash_defweak)
9522 && !h->root.u.def.section->gc_mark
9523 && !(h->root.u.def.section->owner->flags & DYNAMIC))
9525 struct elf_gc_sweep_symbol_info *inf = data;
9526 (*inf->hide_symbol) (inf->info, h, TRUE);
9529 return TRUE;
9532 /* The sweep phase of garbage collection. Remove all garbage sections. */
9534 typedef bfd_boolean (*gc_sweep_hook_fn)
9535 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
9537 static bfd_boolean
9538 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
9540 bfd *sub;
9541 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9542 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
9543 unsigned long section_sym_count;
9544 struct elf_gc_sweep_symbol_info sweep_info;
9546 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9548 asection *o;
9550 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9551 continue;
9553 for (o = sub->sections; o != NULL; o = o->next)
9555 /* Keep debug and special sections. */
9556 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
9557 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
9558 o->gc_mark = 1;
9560 if (o->gc_mark)
9561 continue;
9563 /* Skip sweeping sections already excluded. */
9564 if (o->flags & SEC_EXCLUDE)
9565 continue;
9567 /* Since this is early in the link process, it is simple
9568 to remove a section from the output. */
9569 o->flags |= SEC_EXCLUDE;
9571 if (info->print_gc_sections == TRUE)
9572 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
9574 /* But we also have to update some of the relocation
9575 info we collected before. */
9576 if (gc_sweep_hook
9577 && (o->flags & SEC_RELOC) != 0
9578 && o->reloc_count > 0
9579 && !bfd_is_abs_section (o->output_section))
9581 Elf_Internal_Rela *internal_relocs;
9582 bfd_boolean r;
9584 internal_relocs
9585 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
9586 info->keep_memory);
9587 if (internal_relocs == NULL)
9588 return FALSE;
9590 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
9592 if (elf_section_data (o)->relocs != internal_relocs)
9593 free (internal_relocs);
9595 if (!r)
9596 return FALSE;
9601 /* Remove the symbols that were in the swept sections from the dynamic
9602 symbol table. GCFIXME: Anyone know how to get them out of the
9603 static symbol table as well? */
9604 sweep_info.info = info;
9605 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
9606 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
9607 &sweep_info);
9609 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
9610 return TRUE;
9613 /* Propagate collected vtable information. This is called through
9614 elf_link_hash_traverse. */
9616 static bfd_boolean
9617 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
9619 if (h->root.type == bfd_link_hash_warning)
9620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9622 /* Those that are not vtables. */
9623 if (h->vtable == NULL || h->vtable->parent == NULL)
9624 return TRUE;
9626 /* Those vtables that do not have parents, we cannot merge. */
9627 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
9628 return TRUE;
9630 /* If we've already been done, exit. */
9631 if (h->vtable->used && h->vtable->used[-1])
9632 return TRUE;
9634 /* Make sure the parent's table is up to date. */
9635 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9637 if (h->vtable->used == NULL)
9639 /* None of this table's entries were referenced. Re-use the
9640 parent's table. */
9641 h->vtable->used = h->vtable->parent->vtable->used;
9642 h->vtable->size = h->vtable->parent->vtable->size;
9644 else
9646 size_t n;
9647 bfd_boolean *cu, *pu;
9649 /* Or the parent's entries into ours. */
9650 cu = h->vtable->used;
9651 cu[-1] = TRUE;
9652 pu = h->vtable->parent->vtable->used;
9653 if (pu != NULL)
9655 const struct elf_backend_data *bed;
9656 unsigned int log_file_align;
9658 bed = get_elf_backend_data (h->root.u.def.section->owner);
9659 log_file_align = bed->s->log_file_align;
9660 n = h->vtable->parent->vtable->size >> log_file_align;
9661 while (n--)
9663 if (*pu)
9664 *cu = TRUE;
9665 pu++;
9666 cu++;
9671 return TRUE;
9674 static bfd_boolean
9675 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9677 asection *sec;
9678 bfd_vma hstart, hend;
9679 Elf_Internal_Rela *relstart, *relend, *rel;
9680 const struct elf_backend_data *bed;
9681 unsigned int log_file_align;
9683 if (h->root.type == bfd_link_hash_warning)
9684 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9686 /* Take care of both those symbols that do not describe vtables as
9687 well as those that are not loaded. */
9688 if (h->vtable == NULL || h->vtable->parent == NULL)
9689 return TRUE;
9691 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9692 || h->root.type == bfd_link_hash_defweak);
9694 sec = h->root.u.def.section;
9695 hstart = h->root.u.def.value;
9696 hend = hstart + h->size;
9698 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9699 if (!relstart)
9700 return *(bfd_boolean *) okp = FALSE;
9701 bed = get_elf_backend_data (sec->owner);
9702 log_file_align = bed->s->log_file_align;
9704 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9706 for (rel = relstart; rel < relend; ++rel)
9707 if (rel->r_offset >= hstart && rel->r_offset < hend)
9709 /* If the entry is in use, do nothing. */
9710 if (h->vtable->used
9711 && (rel->r_offset - hstart) < h->vtable->size)
9713 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9714 if (h->vtable->used[entry])
9715 continue;
9717 /* Otherwise, kill it. */
9718 rel->r_offset = rel->r_info = rel->r_addend = 0;
9721 return TRUE;
9724 /* Mark sections containing dynamically referenced symbols. When
9725 building shared libraries, we must assume that any visible symbol is
9726 referenced. */
9728 bfd_boolean
9729 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9731 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9733 if (h->root.type == bfd_link_hash_warning)
9734 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9736 if ((h->root.type == bfd_link_hash_defined
9737 || h->root.type == bfd_link_hash_defweak)
9738 && (h->ref_dynamic
9739 || (!info->executable
9740 && h->def_regular
9741 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9742 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9743 h->root.u.def.section->flags |= SEC_KEEP;
9745 return TRUE;
9748 /* Do mark and sweep of unused sections. */
9750 bfd_boolean
9751 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9753 bfd_boolean ok = TRUE;
9754 bfd *sub;
9755 asection * (*gc_mark_hook)
9756 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9757 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9758 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9760 if (!bed->can_gc_sections
9761 || info->relocatable
9762 || info->emitrelocations
9763 || !is_elf_hash_table (info->hash))
9765 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9766 return TRUE;
9769 /* Apply transitive closure to the vtable entry usage info. */
9770 elf_link_hash_traverse (elf_hash_table (info),
9771 elf_gc_propagate_vtable_entries_used,
9772 &ok);
9773 if (!ok)
9774 return FALSE;
9776 /* Kill the vtable relocations that were not used. */
9777 elf_link_hash_traverse (elf_hash_table (info),
9778 elf_gc_smash_unused_vtentry_relocs,
9779 &ok);
9780 if (!ok)
9781 return FALSE;
9783 /* Mark dynamically referenced symbols. */
9784 if (elf_hash_table (info)->dynamic_sections_created)
9785 elf_link_hash_traverse (elf_hash_table (info),
9786 bed->gc_mark_dynamic_ref,
9787 info);
9789 /* Grovel through relocs to find out who stays ... */
9790 gc_mark_hook = bed->gc_mark_hook;
9791 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9793 asection *o;
9795 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9796 continue;
9798 for (o = sub->sections; o != NULL; o = o->next)
9799 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
9800 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9801 return FALSE;
9804 /* ... again for sections marked from eh_frame. */
9805 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9807 asection *o;
9809 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9810 continue;
9812 /* Keep .gcc_except_table.* if the associated .text.* (or the
9813 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
9814 marked. This isn't very nice, but the proper solution,
9815 splitting .eh_frame up and using comdat doesn't pan out
9816 easily due to needing special relocs to handle the
9817 difference of two symbols in separate sections.
9818 Don't keep code sections referenced by .eh_frame. */
9819 #define TEXT_PREFIX ".text."
9820 #define TEXT_PREFIX2 ".gnu.linkonce.t."
9821 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
9822 for (o = sub->sections; o != NULL; o = o->next)
9823 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9825 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
9827 char *fn_name;
9828 const char *sec_name;
9829 asection *fn_text;
9830 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
9832 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
9833 sec_name = o->name + o_name_prefix_len;
9834 fn_name_prefix_len = strlen (TEXT_PREFIX);
9835 tmp = strlen (TEXT_PREFIX2);
9836 if (tmp > fn_name_prefix_len)
9837 fn_name_prefix_len = tmp;
9838 fn_name
9839 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
9840 if (fn_name == NULL)
9841 return FALSE;
9843 /* Try the first prefix. */
9844 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
9845 fn_text = bfd_get_section_by_name (sub, fn_name);
9847 /* Try the second prefix. */
9848 if (fn_text == NULL)
9850 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
9851 fn_text = bfd_get_section_by_name (sub, fn_name);
9854 free (fn_name);
9855 if (fn_text == NULL || !fn_text->gc_mark)
9856 continue;
9859 /* If not using specially named exception table section,
9860 then keep whatever we are using. */
9861 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9862 return FALSE;
9866 /* ... and mark SEC_EXCLUDE for those that go. */
9867 return elf_gc_sweep (abfd, info);
9870 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9872 bfd_boolean
9873 bfd_elf_gc_record_vtinherit (bfd *abfd,
9874 asection *sec,
9875 struct elf_link_hash_entry *h,
9876 bfd_vma offset)
9878 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9879 struct elf_link_hash_entry **search, *child;
9880 bfd_size_type extsymcount;
9881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9883 /* The sh_info field of the symtab header tells us where the
9884 external symbols start. We don't care about the local symbols at
9885 this point. */
9886 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9887 if (!elf_bad_symtab (abfd))
9888 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9890 sym_hashes = elf_sym_hashes (abfd);
9891 sym_hashes_end = sym_hashes + extsymcount;
9893 /* Hunt down the child symbol, which is in this section at the same
9894 offset as the relocation. */
9895 for (search = sym_hashes; search != sym_hashes_end; ++search)
9897 if ((child = *search) != NULL
9898 && (child->root.type == bfd_link_hash_defined
9899 || child->root.type == bfd_link_hash_defweak)
9900 && child->root.u.def.section == sec
9901 && child->root.u.def.value == offset)
9902 goto win;
9905 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9906 abfd, sec, (unsigned long) offset);
9907 bfd_set_error (bfd_error_invalid_operation);
9908 return FALSE;
9910 win:
9911 if (!child->vtable)
9913 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9914 if (!child->vtable)
9915 return FALSE;
9917 if (!h)
9919 /* This *should* only be the absolute section. It could potentially
9920 be that someone has defined a non-global vtable though, which
9921 would be bad. It isn't worth paging in the local symbols to be
9922 sure though; that case should simply be handled by the assembler. */
9924 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9926 else
9927 child->vtable->parent = h;
9929 return TRUE;
9932 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9934 bfd_boolean
9935 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9936 asection *sec ATTRIBUTE_UNUSED,
9937 struct elf_link_hash_entry *h,
9938 bfd_vma addend)
9940 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9941 unsigned int log_file_align = bed->s->log_file_align;
9943 if (!h->vtable)
9945 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9946 if (!h->vtable)
9947 return FALSE;
9950 if (addend >= h->vtable->size)
9952 size_t size, bytes, file_align;
9953 bfd_boolean *ptr = h->vtable->used;
9955 /* While the symbol is undefined, we have to be prepared to handle
9956 a zero size. */
9957 file_align = 1 << log_file_align;
9958 if (h->root.type == bfd_link_hash_undefined)
9959 size = addend + file_align;
9960 else
9962 size = h->size;
9963 if (addend >= size)
9965 /* Oops! We've got a reference past the defined end of
9966 the table. This is probably a bug -- shall we warn? */
9967 size = addend + file_align;
9970 size = (size + file_align - 1) & -file_align;
9972 /* Allocate one extra entry for use as a "done" flag for the
9973 consolidation pass. */
9974 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9976 if (ptr)
9978 ptr = bfd_realloc (ptr - 1, bytes);
9980 if (ptr != NULL)
9982 size_t oldbytes;
9984 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9985 * sizeof (bfd_boolean));
9986 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9989 else
9990 ptr = bfd_zmalloc (bytes);
9992 if (ptr == NULL)
9993 return FALSE;
9995 /* And arrange for that done flag to be at index -1. */
9996 h->vtable->used = ptr + 1;
9997 h->vtable->size = size;
10000 h->vtable->used[addend >> log_file_align] = TRUE;
10002 return TRUE;
10005 struct alloc_got_off_arg {
10006 bfd_vma gotoff;
10007 unsigned int got_elt_size;
10010 /* We need a special top-level link routine to convert got reference counts
10011 to real got offsets. */
10013 static bfd_boolean
10014 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
10016 struct alloc_got_off_arg *gofarg = arg;
10018 if (h->root.type == bfd_link_hash_warning)
10019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10021 if (h->got.refcount > 0)
10023 h->got.offset = gofarg->gotoff;
10024 gofarg->gotoff += gofarg->got_elt_size;
10026 else
10027 h->got.offset = (bfd_vma) -1;
10029 return TRUE;
10032 /* And an accompanying bit to work out final got entry offsets once
10033 we're done. Should be called from final_link. */
10035 bfd_boolean
10036 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
10037 struct bfd_link_info *info)
10039 bfd *i;
10040 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10041 bfd_vma gotoff;
10042 unsigned int got_elt_size = bed->s->arch_size / 8;
10043 struct alloc_got_off_arg gofarg;
10045 if (! is_elf_hash_table (info->hash))
10046 return FALSE;
10048 /* The GOT offset is relative to the .got section, but the GOT header is
10049 put into the .got.plt section, if the backend uses it. */
10050 if (bed->want_got_plt)
10051 gotoff = 0;
10052 else
10053 gotoff = bed->got_header_size;
10055 /* Do the local .got entries first. */
10056 for (i = info->input_bfds; i; i = i->link_next)
10058 bfd_signed_vma *local_got;
10059 bfd_size_type j, locsymcount;
10060 Elf_Internal_Shdr *symtab_hdr;
10062 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
10063 continue;
10065 local_got = elf_local_got_refcounts (i);
10066 if (!local_got)
10067 continue;
10069 symtab_hdr = &elf_tdata (i)->symtab_hdr;
10070 if (elf_bad_symtab (i))
10071 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10072 else
10073 locsymcount = symtab_hdr->sh_info;
10075 for (j = 0; j < locsymcount; ++j)
10077 if (local_got[j] > 0)
10079 local_got[j] = gotoff;
10080 gotoff += got_elt_size;
10082 else
10083 local_got[j] = (bfd_vma) -1;
10087 /* Then the global .got entries. .plt refcounts are handled by
10088 adjust_dynamic_symbol */
10089 gofarg.gotoff = gotoff;
10090 gofarg.got_elt_size = got_elt_size;
10091 elf_link_hash_traverse (elf_hash_table (info),
10092 elf_gc_allocate_got_offsets,
10093 &gofarg);
10094 return TRUE;
10097 /* Many folk need no more in the way of final link than this, once
10098 got entry reference counting is enabled. */
10100 bfd_boolean
10101 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
10103 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
10104 return FALSE;
10106 /* Invoke the regular ELF backend linker to do all the work. */
10107 return bfd_elf_final_link (abfd, info);
10110 bfd_boolean
10111 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
10113 struct elf_reloc_cookie *rcookie = cookie;
10115 if (rcookie->bad_symtab)
10116 rcookie->rel = rcookie->rels;
10118 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
10120 unsigned long r_symndx;
10122 if (! rcookie->bad_symtab)
10123 if (rcookie->rel->r_offset > offset)
10124 return FALSE;
10125 if (rcookie->rel->r_offset != offset)
10126 continue;
10128 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
10129 if (r_symndx == SHN_UNDEF)
10130 return TRUE;
10132 if (r_symndx >= rcookie->locsymcount
10133 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
10135 struct elf_link_hash_entry *h;
10137 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
10139 while (h->root.type == bfd_link_hash_indirect
10140 || h->root.type == bfd_link_hash_warning)
10141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10143 if ((h->root.type == bfd_link_hash_defined
10144 || h->root.type == bfd_link_hash_defweak)
10145 && elf_discarded_section (h->root.u.def.section))
10146 return TRUE;
10147 else
10148 return FALSE;
10150 else
10152 /* It's not a relocation against a global symbol,
10153 but it could be a relocation against a local
10154 symbol for a discarded section. */
10155 asection *isec;
10156 Elf_Internal_Sym *isym;
10158 /* Need to: get the symbol; get the section. */
10159 isym = &rcookie->locsyms[r_symndx];
10160 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
10162 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
10163 if (isec != NULL && elf_discarded_section (isec))
10164 return TRUE;
10167 return FALSE;
10169 return FALSE;
10172 /* Discard unneeded references to discarded sections.
10173 Returns TRUE if any section's size was changed. */
10174 /* This function assumes that the relocations are in sorted order,
10175 which is true for all known assemblers. */
10177 bfd_boolean
10178 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
10180 struct elf_reloc_cookie cookie;
10181 asection *stab, *eh;
10182 Elf_Internal_Shdr *symtab_hdr;
10183 const struct elf_backend_data *bed;
10184 bfd *abfd;
10185 unsigned int count;
10186 bfd_boolean ret = FALSE;
10188 if (info->traditional_format
10189 || !is_elf_hash_table (info->hash))
10190 return FALSE;
10192 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
10194 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
10195 continue;
10197 bed = get_elf_backend_data (abfd);
10199 if ((abfd->flags & DYNAMIC) != 0)
10200 continue;
10202 eh = bfd_get_section_by_name (abfd, ".eh_frame");
10203 if (info->relocatable
10204 || (eh != NULL
10205 && (eh->size == 0
10206 || bfd_is_abs_section (eh->output_section))))
10207 eh = NULL;
10209 stab = bfd_get_section_by_name (abfd, ".stab");
10210 if (stab != NULL
10211 && (stab->size == 0
10212 || bfd_is_abs_section (stab->output_section)
10213 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
10214 stab = NULL;
10216 if (stab == NULL
10217 && eh == NULL
10218 && bed->elf_backend_discard_info == NULL)
10219 continue;
10221 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10222 cookie.abfd = abfd;
10223 cookie.sym_hashes = elf_sym_hashes (abfd);
10224 cookie.bad_symtab = elf_bad_symtab (abfd);
10225 if (cookie.bad_symtab)
10227 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10228 cookie.extsymoff = 0;
10230 else
10232 cookie.locsymcount = symtab_hdr->sh_info;
10233 cookie.extsymoff = symtab_hdr->sh_info;
10236 if (bed->s->arch_size == 32)
10237 cookie.r_sym_shift = 8;
10238 else
10239 cookie.r_sym_shift = 32;
10241 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10242 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
10244 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
10245 cookie.locsymcount, 0,
10246 NULL, NULL, NULL);
10247 if (cookie.locsyms == NULL)
10248 return FALSE;
10251 if (stab != NULL)
10253 cookie.rels = NULL;
10254 count = stab->reloc_count;
10255 if (count != 0)
10256 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
10257 info->keep_memory);
10258 if (cookie.rels != NULL)
10260 cookie.rel = cookie.rels;
10261 cookie.relend = cookie.rels;
10262 cookie.relend += count * bed->s->int_rels_per_ext_rel;
10263 if (_bfd_discard_section_stabs (abfd, stab,
10264 elf_section_data (stab)->sec_info,
10265 bfd_elf_reloc_symbol_deleted_p,
10266 &cookie))
10267 ret = TRUE;
10268 if (elf_section_data (stab)->relocs != cookie.rels)
10269 free (cookie.rels);
10273 if (eh != NULL)
10275 cookie.rels = NULL;
10276 count = eh->reloc_count;
10277 if (count != 0)
10278 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
10279 info->keep_memory);
10280 cookie.rel = cookie.rels;
10281 cookie.relend = cookie.rels;
10282 if (cookie.rels != NULL)
10283 cookie.relend += count * bed->s->int_rels_per_ext_rel;
10285 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
10286 bfd_elf_reloc_symbol_deleted_p,
10287 &cookie))
10288 ret = TRUE;
10290 if (cookie.rels != NULL
10291 && elf_section_data (eh)->relocs != cookie.rels)
10292 free (cookie.rels);
10295 if (bed->elf_backend_discard_info != NULL
10296 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
10297 ret = TRUE;
10299 if (cookie.locsyms != NULL
10300 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
10302 if (! info->keep_memory)
10303 free (cookie.locsyms);
10304 else
10305 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
10309 if (info->eh_frame_hdr
10310 && !info->relocatable
10311 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
10312 ret = TRUE;
10314 return ret;
10317 void
10318 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
10319 struct bfd_link_info *info)
10321 flagword flags;
10322 const char *name, *p;
10323 struct bfd_section_already_linked *l;
10324 struct bfd_section_already_linked_hash_entry *already_linked_list;
10326 if (sec->output_section == bfd_abs_section_ptr)
10327 return;
10329 flags = sec->flags;
10331 /* Return if it isn't a linkonce section. A comdat group section
10332 also has SEC_LINK_ONCE set. */
10333 if ((flags & SEC_LINK_ONCE) == 0)
10334 return;
10336 /* Don't put group member sections on our list of already linked
10337 sections. They are handled as a group via their group section. */
10338 if (elf_sec_group (sec) != NULL)
10339 return;
10341 /* FIXME: When doing a relocatable link, we may have trouble
10342 copying relocations in other sections that refer to local symbols
10343 in the section being discarded. Those relocations will have to
10344 be converted somehow; as of this writing I'm not sure that any of
10345 the backends handle that correctly.
10347 It is tempting to instead not discard link once sections when
10348 doing a relocatable link (technically, they should be discarded
10349 whenever we are building constructors). However, that fails,
10350 because the linker winds up combining all the link once sections
10351 into a single large link once section, which defeats the purpose
10352 of having link once sections in the first place.
10354 Also, not merging link once sections in a relocatable link
10355 causes trouble for MIPS ELF, which relies on link once semantics
10356 to handle the .reginfo section correctly. */
10358 name = bfd_get_section_name (abfd, sec);
10360 if (CONST_STRNEQ (name, ".gnu.linkonce.")
10361 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
10362 p++;
10363 else
10364 p = name;
10366 already_linked_list = bfd_section_already_linked_table_lookup (p);
10368 for (l = already_linked_list->entry; l != NULL; l = l->next)
10370 /* We may have 2 different types of sections on the list: group
10371 sections and linkonce sections. Match like sections. */
10372 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
10373 && strcmp (name, l->sec->name) == 0
10374 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
10376 /* The section has already been linked. See if we should
10377 issue a warning. */
10378 switch (flags & SEC_LINK_DUPLICATES)
10380 default:
10381 abort ();
10383 case SEC_LINK_DUPLICATES_DISCARD:
10384 break;
10386 case SEC_LINK_DUPLICATES_ONE_ONLY:
10387 (*_bfd_error_handler)
10388 (_("%B: ignoring duplicate section `%A'"),
10389 abfd, sec);
10390 break;
10392 case SEC_LINK_DUPLICATES_SAME_SIZE:
10393 if (sec->size != l->sec->size)
10394 (*_bfd_error_handler)
10395 (_("%B: duplicate section `%A' has different size"),
10396 abfd, sec);
10397 break;
10399 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
10400 if (sec->size != l->sec->size)
10401 (*_bfd_error_handler)
10402 (_("%B: duplicate section `%A' has different size"),
10403 abfd, sec);
10404 else if (sec->size != 0)
10406 bfd_byte *sec_contents, *l_sec_contents;
10408 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
10409 (*_bfd_error_handler)
10410 (_("%B: warning: could not read contents of section `%A'"),
10411 abfd, sec);
10412 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
10413 &l_sec_contents))
10414 (*_bfd_error_handler)
10415 (_("%B: warning: could not read contents of section `%A'"),
10416 l->sec->owner, l->sec);
10417 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
10418 (*_bfd_error_handler)
10419 (_("%B: warning: duplicate section `%A' has different contents"),
10420 abfd, sec);
10422 if (sec_contents)
10423 free (sec_contents);
10424 if (l_sec_contents)
10425 free (l_sec_contents);
10427 break;
10430 /* Set the output_section field so that lang_add_section
10431 does not create a lang_input_section structure for this
10432 section. Since there might be a symbol in the section
10433 being discarded, we must retain a pointer to the section
10434 which we are really going to use. */
10435 sec->output_section = bfd_abs_section_ptr;
10436 sec->kept_section = l->sec;
10438 if (flags & SEC_GROUP)
10440 asection *first = elf_next_in_group (sec);
10441 asection *s = first;
10443 while (s != NULL)
10445 s->output_section = bfd_abs_section_ptr;
10446 /* Record which group discards it. */
10447 s->kept_section = l->sec;
10448 s = elf_next_in_group (s);
10449 /* These lists are circular. */
10450 if (s == first)
10451 break;
10455 return;
10459 /* A single member comdat group section may be discarded by a
10460 linkonce section and vice versa. */
10462 if ((flags & SEC_GROUP) != 0)
10464 asection *first = elf_next_in_group (sec);
10466 if (first != NULL && elf_next_in_group (first) == first)
10467 /* Check this single member group against linkonce sections. */
10468 for (l = already_linked_list->entry; l != NULL; l = l->next)
10469 if ((l->sec->flags & SEC_GROUP) == 0
10470 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
10471 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
10473 first->output_section = bfd_abs_section_ptr;
10474 first->kept_section = l->sec;
10475 sec->output_section = bfd_abs_section_ptr;
10476 break;
10479 else
10480 /* Check this linkonce section against single member groups. */
10481 for (l = already_linked_list->entry; l != NULL; l = l->next)
10482 if (l->sec->flags & SEC_GROUP)
10484 asection *first = elf_next_in_group (l->sec);
10486 if (first != NULL
10487 && elf_next_in_group (first) == first
10488 && bfd_elf_match_symbols_in_sections (first, sec, info))
10490 sec->output_section = bfd_abs_section_ptr;
10491 sec->kept_section = first;
10492 break;
10496 /* This is the first section with this name. Record it. */
10497 bfd_section_already_linked_table_insert (already_linked_list, sec);
10500 bfd_boolean
10501 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
10503 return sym->st_shndx == SHN_COMMON;
10506 unsigned int
10507 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
10509 return SHN_COMMON;
10512 asection *
10513 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
10515 return bfd_com_section_ptr;