PR ld/2267
[binutils.git] / bfd / elflink.c
blob88e56da54d43e1cf261e47de081bc0d16a4ba1b1
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 if (! info->traditional_format)
190 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
191 flags | SEC_READONLY);
192 if (s == NULL
193 || ! bfd_set_section_alignment (abfd, s, 2))
194 return FALSE;
195 elf_hash_table (info)->eh_info.hdr_sec = s;
198 /* Create sections to hold version informations. These are removed
199 if they are not needed. */
200 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
201 flags | SEC_READONLY);
202 if (s == NULL
203 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
204 return FALSE;
206 s = bfd_make_section_with_flags (abfd, ".gnu.version",
207 flags | SEC_READONLY);
208 if (s == NULL
209 || ! bfd_set_section_alignment (abfd, s, 1))
210 return FALSE;
212 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
213 flags | SEC_READONLY);
214 if (s == NULL
215 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
216 return FALSE;
218 s = bfd_make_section_with_flags (abfd, ".dynsym",
219 flags | SEC_READONLY);
220 if (s == NULL
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
224 s = bfd_make_section_with_flags (abfd, ".dynstr",
225 flags | SEC_READONLY);
226 if (s == NULL)
227 return FALSE;
229 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
230 if (s == NULL
231 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
232 return FALSE;
234 /* The special symbol _DYNAMIC is always set to the start of the
235 .dynamic section. We could set _DYNAMIC in a linker script, but we
236 only want to define it if we are, in fact, creating a .dynamic
237 section. We don't want to define it if there is no .dynamic
238 section, since on some ELF platforms the start up code examines it
239 to decide how to initialize the process. */
240 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
241 return FALSE;
243 s = bfd_make_section_with_flags (abfd, ".hash",
244 flags | SEC_READONLY);
245 if (s == NULL
246 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
247 return FALSE;
248 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
250 /* Let the backend create the rest of the sections. This lets the
251 backend set the right flags. The backend will normally create
252 the .got and .plt sections. */
253 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
254 return FALSE;
256 elf_hash_table (info)->dynamic_sections_created = TRUE;
258 return TRUE;
261 /* Create dynamic sections when linking against a dynamic object. */
263 bfd_boolean
264 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
266 flagword flags, pltflags;
267 struct elf_link_hash_entry *h;
268 asection *s;
269 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
271 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
272 .rel[a].bss sections. */
273 flags = bed->dynamic_sec_flags;
275 pltflags = flags;
276 if (bed->plt_not_loaded)
277 /* We do not clear SEC_ALLOC here because we still want the OS to
278 allocate space for the section; it's just that there's nothing
279 to read in from the object file. */
280 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
281 else
282 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
283 if (bed->plt_readonly)
284 pltflags |= SEC_READONLY;
286 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
289 return FALSE;
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
292 .plt section. */
293 if (bed->want_plt_sym)
295 h = _bfd_elf_define_linkage_sym (abfd, info, s,
296 "_PROCEDURE_LINKAGE_TABLE_");
297 elf_hash_table (info)->hplt = h;
298 if (h == NULL)
299 return FALSE;
302 s = bfd_make_section_with_flags (abfd,
303 (bed->default_use_rela_p
304 ? ".rela.plt" : ".rel.plt"),
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
310 if (! _bfd_elf_create_got_section (abfd, info))
311 return FALSE;
313 if (bed->want_dynbss)
315 /* The .dynbss section is a place to put symbols which are defined
316 by dynamic objects, are referenced by regular objects, and are
317 not functions. We must allocate space for them in the process
318 image and use a R_*_COPY reloc to tell the dynamic linker to
319 initialize them at run time. The linker script puts the .dynbss
320 section into the .bss section of the final image. */
321 s = bfd_make_section_with_flags (abfd, ".dynbss",
322 (SEC_ALLOC
323 | SEC_LINKER_CREATED));
324 if (s == NULL)
325 return FALSE;
327 /* The .rel[a].bss section holds copy relocs. This section is not
328 normally needed. We need to create it here, though, so that the
329 linker will map it to an output section. We can't just create it
330 only if we need it, because we will not know whether we need it
331 until we have seen all the input files, and the first time the
332 main linker code calls BFD after examining all the input files
333 (size_dynamic_sections) the input sections have already been
334 mapped to the output sections. If the section turns out not to
335 be needed, we can discard it later. We will never need this
336 section when generating a shared object, since they do not use
337 copy relocs. */
338 if (! info->shared)
340 s = bfd_make_section_with_flags (abfd,
341 (bed->default_use_rela_p
342 ? ".rela.bss" : ".rel.bss"),
343 flags | SEC_READONLY);
344 if (s == NULL
345 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
346 return FALSE;
350 return TRUE;
353 /* Record a new dynamic symbol. We record the dynamic symbols as we
354 read the input files, since we need to have a list of all of them
355 before we can determine the final sizes of the output sections.
356 Note that we may actually call this function even though we are not
357 going to output any dynamic symbols; in some cases we know that a
358 symbol should be in the dynamic symbol table, but only if there is
359 one. */
361 bfd_boolean
362 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
363 struct elf_link_hash_entry *h)
365 if (h->dynindx == -1)
367 struct elf_strtab_hash *dynstr;
368 char *p;
369 const char *name;
370 bfd_size_type indx;
372 /* XXX: The ABI draft says the linker must turn hidden and
373 internal symbols into STB_LOCAL symbols when producing the
374 DSO. However, if ld.so honors st_other in the dynamic table,
375 this would not be necessary. */
376 switch (ELF_ST_VISIBILITY (h->other))
378 case STV_INTERNAL:
379 case STV_HIDDEN:
380 if (h->root.type != bfd_link_hash_undefined
381 && h->root.type != bfd_link_hash_undefweak)
383 h->forced_local = 1;
384 if (!elf_hash_table (info)->is_relocatable_executable)
385 return TRUE;
388 default:
389 break;
392 h->dynindx = elf_hash_table (info)->dynsymcount;
393 ++elf_hash_table (info)->dynsymcount;
395 dynstr = elf_hash_table (info)->dynstr;
396 if (dynstr == NULL)
398 /* Create a strtab to hold the dynamic symbol names. */
399 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
400 if (dynstr == NULL)
401 return FALSE;
404 /* We don't put any version information in the dynamic string
405 table. */
406 name = h->root.root.string;
407 p = strchr (name, ELF_VER_CHR);
408 if (p != NULL)
409 /* We know that the p points into writable memory. In fact,
410 there are only a few symbols that have read-only names, being
411 those like _GLOBAL_OFFSET_TABLE_ that are created specially
412 by the backends. Most symbols will have names pointing into
413 an ELF string table read from a file, or to objalloc memory. */
414 *p = 0;
416 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
418 if (p != NULL)
419 *p = ELF_VER_CHR;
421 if (indx == (bfd_size_type) -1)
422 return FALSE;
423 h->dynstr_index = indx;
426 return TRUE;
429 /* Record an assignment to a symbol made by a linker script. We need
430 this in case some dynamic object refers to this symbol. */
432 bfd_boolean
433 bfd_elf_record_link_assignment (bfd *output_bfd,
434 struct bfd_link_info *info,
435 const char *name,
436 bfd_boolean provide,
437 bfd_boolean hidden)
439 struct elf_link_hash_entry *h;
440 struct elf_link_hash_table *htab;
442 if (!is_elf_hash_table (info->hash))
443 return TRUE;
445 htab = elf_hash_table (info);
446 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
447 if (h == NULL)
448 return provide;
450 /* Since we're defining the symbol, don't let it seem to have not
451 been defined. record_dynamic_symbol and size_dynamic_sections
452 may depend on this. */
453 if (h->root.type == bfd_link_hash_undefweak
454 || h->root.type == bfd_link_hash_undefined)
456 h->root.type = bfd_link_hash_new;
457 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
458 bfd_link_repair_undef_list (&htab->root);
461 if (h->root.type == bfd_link_hash_new)
462 h->non_elf = 0;
464 /* If this symbol is being provided by the linker script, and it is
465 currently defined by a dynamic object, but not by a regular
466 object, then mark it as undefined so that the generic linker will
467 force the correct value. */
468 if (provide
469 && h->def_dynamic
470 && !h->def_regular)
471 h->root.type = bfd_link_hash_undefined;
473 /* If this symbol is not being provided by the linker script, and it is
474 currently defined by a dynamic object, but not by a regular object,
475 then clear out any version information because the symbol will not be
476 associated with the dynamic object any more. */
477 if (!provide
478 && h->def_dynamic
479 && !h->def_regular)
480 h->verinfo.verdef = NULL;
482 h->def_regular = 1;
484 if (provide && hidden)
486 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
488 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
489 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
492 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
493 and executables. */
494 if (!info->relocatable
495 && h->dynindx != -1
496 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
497 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
498 h->forced_local = 1;
500 if ((h->def_dynamic
501 || h->ref_dynamic
502 || info->shared
503 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
504 && h->dynindx == -1)
506 if (! bfd_elf_link_record_dynamic_symbol (info, h))
507 return FALSE;
509 /* If this is a weak defined symbol, and we know a corresponding
510 real symbol from the same dynamic object, make sure the real
511 symbol is also made into a dynamic symbol. */
512 if (h->u.weakdef != NULL
513 && h->u.weakdef->dynindx == -1)
515 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
516 return FALSE;
520 return TRUE;
523 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
524 success, and 2 on a failure caused by attempting to record a symbol
525 in a discarded section, eg. a discarded link-once section symbol. */
528 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
529 bfd *input_bfd,
530 long input_indx)
532 bfd_size_type amt;
533 struct elf_link_local_dynamic_entry *entry;
534 struct elf_link_hash_table *eht;
535 struct elf_strtab_hash *dynstr;
536 unsigned long dynstr_index;
537 char *name;
538 Elf_External_Sym_Shndx eshndx;
539 char esym[sizeof (Elf64_External_Sym)];
541 if (! is_elf_hash_table (info->hash))
542 return 0;
544 /* See if the entry exists already. */
545 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
546 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
547 return 1;
549 amt = sizeof (*entry);
550 entry = bfd_alloc (input_bfd, amt);
551 if (entry == NULL)
552 return 0;
554 /* Go find the symbol, so that we can find it's name. */
555 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
556 1, input_indx, &entry->isym, esym, &eshndx))
558 bfd_release (input_bfd, entry);
559 return 0;
562 if (entry->isym.st_shndx != SHN_UNDEF
563 && (entry->isym.st_shndx < SHN_LORESERVE
564 || entry->isym.st_shndx > SHN_HIRESERVE))
566 asection *s;
568 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
569 if (s == NULL || bfd_is_abs_section (s->output_section))
571 /* We can still bfd_release here as nothing has done another
572 bfd_alloc. We can't do this later in this function. */
573 bfd_release (input_bfd, entry);
574 return 2;
578 name = (bfd_elf_string_from_elf_section
579 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
580 entry->isym.st_name));
582 dynstr = elf_hash_table (info)->dynstr;
583 if (dynstr == NULL)
585 /* Create a strtab to hold the dynamic symbol names. */
586 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
587 if (dynstr == NULL)
588 return 0;
591 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
592 if (dynstr_index == (unsigned long) -1)
593 return 0;
594 entry->isym.st_name = dynstr_index;
596 eht = elf_hash_table (info);
598 entry->next = eht->dynlocal;
599 eht->dynlocal = entry;
600 entry->input_bfd = input_bfd;
601 entry->input_indx = input_indx;
602 eht->dynsymcount++;
604 /* Whatever binding the symbol had before, it's now local. */
605 entry->isym.st_info
606 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
608 /* The dynindx will be set at the end of size_dynamic_sections. */
610 return 1;
613 /* Return the dynindex of a local dynamic symbol. */
615 long
616 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
617 bfd *input_bfd,
618 long input_indx)
620 struct elf_link_local_dynamic_entry *e;
622 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
623 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
624 return e->dynindx;
625 return -1;
628 /* This function is used to renumber the dynamic symbols, if some of
629 them are removed because they are marked as local. This is called
630 via elf_link_hash_traverse. */
632 static bfd_boolean
633 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
634 void *data)
636 size_t *count = data;
638 if (h->root.type == bfd_link_hash_warning)
639 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641 if (h->forced_local)
642 return TRUE;
644 if (h->dynindx != -1)
645 h->dynindx = ++(*count);
647 return TRUE;
651 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
652 STB_LOCAL binding. */
654 static bfd_boolean
655 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
656 void *data)
658 size_t *count = data;
660 if (h->root.type == bfd_link_hash_warning)
661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
663 if (!h->forced_local)
664 return TRUE;
666 if (h->dynindx != -1)
667 h->dynindx = ++(*count);
669 return TRUE;
672 /* Return true if the dynamic symbol for a given section should be
673 omitted when creating a shared library. */
674 bfd_boolean
675 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
676 struct bfd_link_info *info,
677 asection *p)
679 switch (elf_section_data (p)->this_hdr.sh_type)
681 case SHT_PROGBITS:
682 case SHT_NOBITS:
683 /* If sh_type is yet undecided, assume it could be
684 SHT_PROGBITS/SHT_NOBITS. */
685 case SHT_NULL:
686 if (strcmp (p->name, ".got") == 0
687 || strcmp (p->name, ".got.plt") == 0
688 || strcmp (p->name, ".plt") == 0)
690 asection *ip;
691 bfd *dynobj = elf_hash_table (info)->dynobj;
693 if (dynobj != NULL
694 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
695 && (ip->flags & SEC_LINKER_CREATED)
696 && ip->output_section == p)
697 return TRUE;
699 return FALSE;
701 /* There shouldn't be section relative relocations
702 against any other section. */
703 default:
704 return TRUE;
708 /* Assign dynsym indices. In a shared library we generate a section
709 symbol for each output section, which come first. Next come symbols
710 which have been forced to local binding. Then all of the back-end
711 allocated local dynamic syms, followed by the rest of the global
712 symbols. */
714 static unsigned long
715 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
716 struct bfd_link_info *info,
717 unsigned long *section_sym_count)
719 unsigned long dynsymcount = 0;
721 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
723 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
724 asection *p;
725 for (p = output_bfd->sections; p ; p = p->next)
726 if ((p->flags & SEC_EXCLUDE) == 0
727 && (p->flags & SEC_ALLOC) != 0
728 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
729 elf_section_data (p)->dynindx = ++dynsymcount;
731 *section_sym_count = dynsymcount;
733 elf_link_hash_traverse (elf_hash_table (info),
734 elf_link_renumber_local_hash_table_dynsyms,
735 &dynsymcount);
737 if (elf_hash_table (info)->dynlocal)
739 struct elf_link_local_dynamic_entry *p;
740 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
741 p->dynindx = ++dynsymcount;
744 elf_link_hash_traverse (elf_hash_table (info),
745 elf_link_renumber_hash_table_dynsyms,
746 &dynsymcount);
748 /* There is an unused NULL entry at the head of the table which
749 we must account for in our count. Unless there weren't any
750 symbols, which means we'll have no table at all. */
751 if (dynsymcount != 0)
752 ++dynsymcount;
754 elf_hash_table (info)->dynsymcount = dynsymcount;
755 return dynsymcount;
758 /* This function is called when we want to define a new symbol. It
759 handles the various cases which arise when we find a definition in
760 a dynamic object, or when there is already a definition in a
761 dynamic object. The new symbol is described by NAME, SYM, PSEC,
762 and PVALUE. We set SYM_HASH to the hash table entry. We set
763 OVERRIDE if the old symbol is overriding a new definition. We set
764 TYPE_CHANGE_OK if it is OK for the type to change. We set
765 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
766 change, we mean that we shouldn't warn if the type or size does
767 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
768 object is overridden by a regular object. */
770 bfd_boolean
771 _bfd_elf_merge_symbol (bfd *abfd,
772 struct bfd_link_info *info,
773 const char *name,
774 Elf_Internal_Sym *sym,
775 asection **psec,
776 bfd_vma *pvalue,
777 unsigned int *pold_alignment,
778 struct elf_link_hash_entry **sym_hash,
779 bfd_boolean *skip,
780 bfd_boolean *override,
781 bfd_boolean *type_change_ok,
782 bfd_boolean *size_change_ok)
784 asection *sec, *oldsec;
785 struct elf_link_hash_entry *h;
786 struct elf_link_hash_entry *flip;
787 int bind;
788 bfd *oldbfd;
789 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
790 bfd_boolean newweak, oldweak;
791 const struct elf_backend_data *bed;
793 *skip = FALSE;
794 *override = FALSE;
796 sec = *psec;
797 bind = ELF_ST_BIND (sym->st_info);
799 if (! bfd_is_und_section (sec))
800 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
801 else
802 h = ((struct elf_link_hash_entry *)
803 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
804 if (h == NULL)
805 return FALSE;
806 *sym_hash = h;
808 /* This code is for coping with dynamic objects, and is only useful
809 if we are doing an ELF link. */
810 if (info->hash->creator != abfd->xvec)
811 return TRUE;
813 /* For merging, we only care about real symbols. */
815 while (h->root.type == bfd_link_hash_indirect
816 || h->root.type == bfd_link_hash_warning)
817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
819 /* If we just created the symbol, mark it as being an ELF symbol.
820 Other than that, there is nothing to do--there is no merge issue
821 with a newly defined symbol--so we just return. */
823 if (h->root.type == bfd_link_hash_new)
825 h->non_elf = 0;
826 return TRUE;
829 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
830 existing symbol. */
832 switch (h->root.type)
834 default:
835 oldbfd = NULL;
836 oldsec = NULL;
837 break;
839 case bfd_link_hash_undefined:
840 case bfd_link_hash_undefweak:
841 oldbfd = h->root.u.undef.abfd;
842 oldsec = NULL;
843 break;
845 case bfd_link_hash_defined:
846 case bfd_link_hash_defweak:
847 oldbfd = h->root.u.def.section->owner;
848 oldsec = h->root.u.def.section;
849 break;
851 case bfd_link_hash_common:
852 oldbfd = h->root.u.c.p->section->owner;
853 oldsec = h->root.u.c.p->section;
854 break;
857 /* In cases involving weak versioned symbols, we may wind up trying
858 to merge a symbol with itself. Catch that here, to avoid the
859 confusion that results if we try to override a symbol with
860 itself. The additional tests catch cases like
861 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
862 dynamic object, which we do want to handle here. */
863 if (abfd == oldbfd
864 && ((abfd->flags & DYNAMIC) == 0
865 || !h->def_regular))
866 return TRUE;
868 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
869 respectively, is from a dynamic object. */
871 newdyn = (abfd->flags & DYNAMIC) != 0;
873 olddyn = FALSE;
874 if (oldbfd != NULL)
875 olddyn = (oldbfd->flags & DYNAMIC) != 0;
876 else if (oldsec != NULL)
878 /* This handles the special SHN_MIPS_{TEXT,DATA} section
879 indices used by MIPS ELF. */
880 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
883 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
884 respectively, appear to be a definition rather than reference. */
886 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
888 olddef = (h->root.type != bfd_link_hash_undefined
889 && h->root.type != bfd_link_hash_undefweak
890 && h->root.type != bfd_link_hash_common);
892 /* Check TLS symbol. We don't check undefined symbol introduced by
893 "ld -u". */
894 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
895 && ELF_ST_TYPE (sym->st_info) != h->type
896 && oldbfd != NULL)
898 bfd *ntbfd, *tbfd;
899 bfd_boolean ntdef, tdef;
900 asection *ntsec, *tsec;
902 if (h->type == STT_TLS)
904 ntbfd = abfd;
905 ntsec = sec;
906 ntdef = newdef;
907 tbfd = oldbfd;
908 tsec = oldsec;
909 tdef = olddef;
911 else
913 ntbfd = oldbfd;
914 ntsec = oldsec;
915 ntdef = olddef;
916 tbfd = abfd;
917 tsec = sec;
918 tdef = newdef;
921 if (tdef && ntdef)
922 (*_bfd_error_handler)
923 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
924 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
925 else if (!tdef && !ntdef)
926 (*_bfd_error_handler)
927 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
928 tbfd, ntbfd, h->root.root.string);
929 else if (tdef)
930 (*_bfd_error_handler)
931 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
932 tbfd, tsec, ntbfd, h->root.root.string);
933 else
934 (*_bfd_error_handler)
935 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
936 tbfd, ntbfd, ntsec, h->root.root.string);
938 bfd_set_error (bfd_error_bad_value);
939 return FALSE;
942 /* We need to remember if a symbol has a definition in a dynamic
943 object or is weak in all dynamic objects. Internal and hidden
944 visibility will make it unavailable to dynamic objects. */
945 if (newdyn && !h->dynamic_def)
947 if (!bfd_is_und_section (sec))
948 h->dynamic_def = 1;
949 else
951 /* Check if this symbol is weak in all dynamic objects. If it
952 is the first time we see it in a dynamic object, we mark
953 if it is weak. Otherwise, we clear it. */
954 if (!h->ref_dynamic)
956 if (bind == STB_WEAK)
957 h->dynamic_weak = 1;
959 else if (bind != STB_WEAK)
960 h->dynamic_weak = 0;
964 /* If the old symbol has non-default visibility, we ignore the new
965 definition from a dynamic object. */
966 if (newdyn
967 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
968 && !bfd_is_und_section (sec))
970 *skip = TRUE;
971 /* Make sure this symbol is dynamic. */
972 h->ref_dynamic = 1;
973 /* A protected symbol has external availability. Make sure it is
974 recorded as dynamic.
976 FIXME: Should we check type and size for protected symbol? */
977 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
978 return bfd_elf_link_record_dynamic_symbol (info, h);
979 else
980 return TRUE;
982 else if (!newdyn
983 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
984 && h->def_dynamic)
986 /* If the new symbol with non-default visibility comes from a
987 relocatable file and the old definition comes from a dynamic
988 object, we remove the old definition. */
989 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
990 h = *sym_hash;
992 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
993 && bfd_is_und_section (sec))
995 /* If the new symbol is undefined and the old symbol was
996 also undefined before, we need to make sure
997 _bfd_generic_link_add_one_symbol doesn't mess
998 up the linker hash table undefs list. Since the old
999 definition came from a dynamic object, it is still on the
1000 undefs list. */
1001 h->root.type = bfd_link_hash_undefined;
1002 h->root.u.undef.abfd = abfd;
1004 else
1006 h->root.type = bfd_link_hash_new;
1007 h->root.u.undef.abfd = NULL;
1010 if (h->def_dynamic)
1012 h->def_dynamic = 0;
1013 h->ref_dynamic = 1;
1014 h->dynamic_def = 1;
1016 /* FIXME: Should we check type and size for protected symbol? */
1017 h->size = 0;
1018 h->type = 0;
1019 return TRUE;
1022 /* Differentiate strong and weak symbols. */
1023 newweak = bind == STB_WEAK;
1024 oldweak = (h->root.type == bfd_link_hash_defweak
1025 || h->root.type == bfd_link_hash_undefweak);
1027 /* If a new weak symbol definition comes from a regular file and the
1028 old symbol comes from a dynamic library, we treat the new one as
1029 strong. Similarly, an old weak symbol definition from a regular
1030 file is treated as strong when the new symbol comes from a dynamic
1031 library. Further, an old weak symbol from a dynamic library is
1032 treated as strong if the new symbol is from a dynamic library.
1033 This reflects the way glibc's ld.so works.
1035 Do this before setting *type_change_ok or *size_change_ok so that
1036 we warn properly when dynamic library symbols are overridden. */
1038 if (newdef && !newdyn && olddyn)
1039 newweak = FALSE;
1040 if (olddef && newdyn)
1041 oldweak = FALSE;
1043 /* It's OK to change the type if either the existing symbol or the
1044 new symbol is weak. A type change is also OK if the old symbol
1045 is undefined and the new symbol is defined. */
1047 if (oldweak
1048 || newweak
1049 || (newdef
1050 && h->root.type == bfd_link_hash_undefined))
1051 *type_change_ok = TRUE;
1053 /* It's OK to change the size if either the existing symbol or the
1054 new symbol is weak, or if the old symbol is undefined. */
1056 if (*type_change_ok
1057 || h->root.type == bfd_link_hash_undefined)
1058 *size_change_ok = TRUE;
1060 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1061 symbol, respectively, appears to be a common symbol in a dynamic
1062 object. If a symbol appears in an uninitialized section, and is
1063 not weak, and is not a function, then it may be a common symbol
1064 which was resolved when the dynamic object was created. We want
1065 to treat such symbols specially, because they raise special
1066 considerations when setting the symbol size: if the symbol
1067 appears as a common symbol in a regular object, and the size in
1068 the regular object is larger, we must make sure that we use the
1069 larger size. This problematic case can always be avoided in C,
1070 but it must be handled correctly when using Fortran shared
1071 libraries.
1073 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1074 likewise for OLDDYNCOMMON and OLDDEF.
1076 Note that this test is just a heuristic, and that it is quite
1077 possible to have an uninitialized symbol in a shared object which
1078 is really a definition, rather than a common symbol. This could
1079 lead to some minor confusion when the symbol really is a common
1080 symbol in some regular object. However, I think it will be
1081 harmless. */
1083 if (newdyn
1084 && newdef
1085 && !newweak
1086 && (sec->flags & SEC_ALLOC) != 0
1087 && (sec->flags & SEC_LOAD) == 0
1088 && sym->st_size > 0
1089 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1090 newdyncommon = TRUE;
1091 else
1092 newdyncommon = FALSE;
1094 if (olddyn
1095 && olddef
1096 && h->root.type == bfd_link_hash_defined
1097 && h->def_dynamic
1098 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1099 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1100 && h->size > 0
1101 && h->type != STT_FUNC)
1102 olddyncommon = TRUE;
1103 else
1104 olddyncommon = FALSE;
1106 /* We now know everything about the old and new symbols. We ask the
1107 backend to check if we can merge them. */
1108 bed = get_elf_backend_data (abfd);
1109 if (bed->merge_symbol
1110 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1111 pold_alignment, skip, override,
1112 type_change_ok, size_change_ok,
1113 &newdyn, &newdef, &newdyncommon, &newweak,
1114 abfd, &sec,
1115 &olddyn, &olddef, &olddyncommon, &oldweak,
1116 oldbfd, &oldsec))
1117 return FALSE;
1119 /* If both the old and the new symbols look like common symbols in a
1120 dynamic object, set the size of the symbol to the larger of the
1121 two. */
1123 if (olddyncommon
1124 && newdyncommon
1125 && sym->st_size != h->size)
1127 /* Since we think we have two common symbols, issue a multiple
1128 common warning if desired. Note that we only warn if the
1129 size is different. If the size is the same, we simply let
1130 the old symbol override the new one as normally happens with
1131 symbols defined in dynamic objects. */
1133 if (! ((*info->callbacks->multiple_common)
1134 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1135 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1136 return FALSE;
1138 if (sym->st_size > h->size)
1139 h->size = sym->st_size;
1141 *size_change_ok = TRUE;
1144 /* If we are looking at a dynamic object, and we have found a
1145 definition, we need to see if the symbol was already defined by
1146 some other object. If so, we want to use the existing
1147 definition, and we do not want to report a multiple symbol
1148 definition error; we do this by clobbering *PSEC to be
1149 bfd_und_section_ptr.
1151 We treat a common symbol as a definition if the symbol in the
1152 shared library is a function, since common symbols always
1153 represent variables; this can cause confusion in principle, but
1154 any such confusion would seem to indicate an erroneous program or
1155 shared library. We also permit a common symbol in a regular
1156 object to override a weak symbol in a shared object. */
1158 if (newdyn
1159 && newdef
1160 && (olddef
1161 || (h->root.type == bfd_link_hash_common
1162 && (newweak
1163 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1165 *override = TRUE;
1166 newdef = FALSE;
1167 newdyncommon = FALSE;
1169 *psec = sec = bfd_und_section_ptr;
1170 *size_change_ok = TRUE;
1172 /* If we get here when the old symbol is a common symbol, then
1173 we are explicitly letting it override a weak symbol or
1174 function in a dynamic object, and we don't want to warn about
1175 a type change. If the old symbol is a defined symbol, a type
1176 change warning may still be appropriate. */
1178 if (h->root.type == bfd_link_hash_common)
1179 *type_change_ok = TRUE;
1182 /* Handle the special case of an old common symbol merging with a
1183 new symbol which looks like a common symbol in a shared object.
1184 We change *PSEC and *PVALUE to make the new symbol look like a
1185 common symbol, and let _bfd_generic_link_add_one_symbol do the
1186 right thing. */
1188 if (newdyncommon
1189 && h->root.type == bfd_link_hash_common)
1191 *override = TRUE;
1192 newdef = FALSE;
1193 newdyncommon = FALSE;
1194 *pvalue = sym->st_size;
1195 *psec = sec = bed->common_section (oldsec);
1196 *size_change_ok = TRUE;
1199 /* Skip weak definitions of symbols that are already defined. */
1200 if (newdef && olddef && newweak)
1201 *skip = TRUE;
1203 /* If the old symbol is from a dynamic object, and the new symbol is
1204 a definition which is not from a dynamic object, then the new
1205 symbol overrides the old symbol. Symbols from regular files
1206 always take precedence over symbols from dynamic objects, even if
1207 they are defined after the dynamic object in the link.
1209 As above, we again permit a common symbol in a regular object to
1210 override a definition in a shared object if the shared object
1211 symbol is a function or is weak. */
1213 flip = NULL;
1214 if (!newdyn
1215 && (newdef
1216 || (bfd_is_com_section (sec)
1217 && (oldweak
1218 || h->type == STT_FUNC)))
1219 && olddyn
1220 && olddef
1221 && h->def_dynamic)
1223 /* Change the hash table entry to undefined, and let
1224 _bfd_generic_link_add_one_symbol do the right thing with the
1225 new definition. */
1227 h->root.type = bfd_link_hash_undefined;
1228 h->root.u.undef.abfd = h->root.u.def.section->owner;
1229 *size_change_ok = TRUE;
1231 olddef = FALSE;
1232 olddyncommon = FALSE;
1234 /* We again permit a type change when a common symbol may be
1235 overriding a function. */
1237 if (bfd_is_com_section (sec))
1238 *type_change_ok = TRUE;
1240 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1241 flip = *sym_hash;
1242 else
1243 /* This union may have been set to be non-NULL when this symbol
1244 was seen in a dynamic object. We must force the union to be
1245 NULL, so that it is correct for a regular symbol. */
1246 h->verinfo.vertree = NULL;
1249 /* Handle the special case of a new common symbol merging with an
1250 old symbol that looks like it might be a common symbol defined in
1251 a shared object. Note that we have already handled the case in
1252 which a new common symbol should simply override the definition
1253 in the shared library. */
1255 if (! newdyn
1256 && bfd_is_com_section (sec)
1257 && olddyncommon)
1259 /* It would be best if we could set the hash table entry to a
1260 common symbol, but we don't know what to use for the section
1261 or the alignment. */
1262 if (! ((*info->callbacks->multiple_common)
1263 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1264 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1265 return FALSE;
1267 /* If the presumed common symbol in the dynamic object is
1268 larger, pretend that the new symbol has its size. */
1270 if (h->size > *pvalue)
1271 *pvalue = h->size;
1273 /* We need to remember the alignment required by the symbol
1274 in the dynamic object. */
1275 BFD_ASSERT (pold_alignment);
1276 *pold_alignment = h->root.u.def.section->alignment_power;
1278 olddef = FALSE;
1279 olddyncommon = FALSE;
1281 h->root.type = bfd_link_hash_undefined;
1282 h->root.u.undef.abfd = h->root.u.def.section->owner;
1284 *size_change_ok = TRUE;
1285 *type_change_ok = TRUE;
1287 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1288 flip = *sym_hash;
1289 else
1290 h->verinfo.vertree = NULL;
1293 if (flip != NULL)
1295 /* Handle the case where we had a versioned symbol in a dynamic
1296 library and now find a definition in a normal object. In this
1297 case, we make the versioned symbol point to the normal one. */
1298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1299 flip->root.type = h->root.type;
1300 h->root.type = bfd_link_hash_indirect;
1301 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1302 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1303 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1304 if (h->def_dynamic)
1306 h->def_dynamic = 0;
1307 flip->ref_dynamic = 1;
1311 return TRUE;
1314 /* This function is called to create an indirect symbol from the
1315 default for the symbol with the default version if needed. The
1316 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1317 set DYNSYM if the new indirect symbol is dynamic. */
1319 bfd_boolean
1320 _bfd_elf_add_default_symbol (bfd *abfd,
1321 struct bfd_link_info *info,
1322 struct elf_link_hash_entry *h,
1323 const char *name,
1324 Elf_Internal_Sym *sym,
1325 asection **psec,
1326 bfd_vma *value,
1327 bfd_boolean *dynsym,
1328 bfd_boolean override)
1330 bfd_boolean type_change_ok;
1331 bfd_boolean size_change_ok;
1332 bfd_boolean skip;
1333 char *shortname;
1334 struct elf_link_hash_entry *hi;
1335 struct bfd_link_hash_entry *bh;
1336 const struct elf_backend_data *bed;
1337 bfd_boolean collect;
1338 bfd_boolean dynamic;
1339 char *p;
1340 size_t len, shortlen;
1341 asection *sec;
1343 /* If this symbol has a version, and it is the default version, we
1344 create an indirect symbol from the default name to the fully
1345 decorated name. This will cause external references which do not
1346 specify a version to be bound to this version of the symbol. */
1347 p = strchr (name, ELF_VER_CHR);
1348 if (p == NULL || p[1] != ELF_VER_CHR)
1349 return TRUE;
1351 if (override)
1353 /* We are overridden by an old definition. We need to check if we
1354 need to create the indirect symbol from the default name. */
1355 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1356 FALSE, FALSE);
1357 BFD_ASSERT (hi != NULL);
1358 if (hi == h)
1359 return TRUE;
1360 while (hi->root.type == bfd_link_hash_indirect
1361 || hi->root.type == bfd_link_hash_warning)
1363 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1364 if (hi == h)
1365 return TRUE;
1369 bed = get_elf_backend_data (abfd);
1370 collect = bed->collect;
1371 dynamic = (abfd->flags & DYNAMIC) != 0;
1373 shortlen = p - name;
1374 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1375 if (shortname == NULL)
1376 return FALSE;
1377 memcpy (shortname, name, shortlen);
1378 shortname[shortlen] = '\0';
1380 /* We are going to create a new symbol. Merge it with any existing
1381 symbol with this name. For the purposes of the merge, act as
1382 though we were defining the symbol we just defined, although we
1383 actually going to define an indirect symbol. */
1384 type_change_ok = FALSE;
1385 size_change_ok = FALSE;
1386 sec = *psec;
1387 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1388 NULL, &hi, &skip, &override,
1389 &type_change_ok, &size_change_ok))
1390 return FALSE;
1392 if (skip)
1393 goto nondefault;
1395 if (! override)
1397 bh = &hi->root;
1398 if (! (_bfd_generic_link_add_one_symbol
1399 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1400 0, name, FALSE, collect, &bh)))
1401 return FALSE;
1402 hi = (struct elf_link_hash_entry *) bh;
1404 else
1406 /* In this case the symbol named SHORTNAME is overriding the
1407 indirect symbol we want to add. We were planning on making
1408 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1409 is the name without a version. NAME is the fully versioned
1410 name, and it is the default version.
1412 Overriding means that we already saw a definition for the
1413 symbol SHORTNAME in a regular object, and it is overriding
1414 the symbol defined in the dynamic object.
1416 When this happens, we actually want to change NAME, the
1417 symbol we just added, to refer to SHORTNAME. This will cause
1418 references to NAME in the shared object to become references
1419 to SHORTNAME in the regular object. This is what we expect
1420 when we override a function in a shared object: that the
1421 references in the shared object will be mapped to the
1422 definition in the regular object. */
1424 while (hi->root.type == bfd_link_hash_indirect
1425 || hi->root.type == bfd_link_hash_warning)
1426 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1428 h->root.type = bfd_link_hash_indirect;
1429 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1430 if (h->def_dynamic)
1432 h->def_dynamic = 0;
1433 hi->ref_dynamic = 1;
1434 if (hi->ref_regular
1435 || hi->def_regular)
1437 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1438 return FALSE;
1442 /* Now set HI to H, so that the following code will set the
1443 other fields correctly. */
1444 hi = h;
1447 /* If there is a duplicate definition somewhere, then HI may not
1448 point to an indirect symbol. We will have reported an error to
1449 the user in that case. */
1451 if (hi->root.type == bfd_link_hash_indirect)
1453 struct elf_link_hash_entry *ht;
1455 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1456 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1458 /* See if the new flags lead us to realize that the symbol must
1459 be dynamic. */
1460 if (! *dynsym)
1462 if (! dynamic)
1464 if (info->shared
1465 || hi->ref_dynamic)
1466 *dynsym = TRUE;
1468 else
1470 if (hi->ref_regular)
1471 *dynsym = TRUE;
1476 /* We also need to define an indirection from the nondefault version
1477 of the symbol. */
1479 nondefault:
1480 len = strlen (name);
1481 shortname = bfd_hash_allocate (&info->hash->table, len);
1482 if (shortname == NULL)
1483 return FALSE;
1484 memcpy (shortname, name, shortlen);
1485 memcpy (shortname + shortlen, p + 1, len - shortlen);
1487 /* Once again, merge with any existing symbol. */
1488 type_change_ok = FALSE;
1489 size_change_ok = FALSE;
1490 sec = *psec;
1491 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1492 NULL, &hi, &skip, &override,
1493 &type_change_ok, &size_change_ok))
1494 return FALSE;
1496 if (skip)
1497 return TRUE;
1499 if (override)
1501 /* Here SHORTNAME is a versioned name, so we don't expect to see
1502 the type of override we do in the case above unless it is
1503 overridden by a versioned definition. */
1504 if (hi->root.type != bfd_link_hash_defined
1505 && hi->root.type != bfd_link_hash_defweak)
1506 (*_bfd_error_handler)
1507 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1508 abfd, shortname);
1510 else
1512 bh = &hi->root;
1513 if (! (_bfd_generic_link_add_one_symbol
1514 (info, abfd, shortname, BSF_INDIRECT,
1515 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1516 return FALSE;
1517 hi = (struct elf_link_hash_entry *) bh;
1519 /* If there is a duplicate definition somewhere, then HI may not
1520 point to an indirect symbol. We will have reported an error
1521 to the user in that case. */
1523 if (hi->root.type == bfd_link_hash_indirect)
1525 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1527 /* See if the new flags lead us to realize that the symbol
1528 must be dynamic. */
1529 if (! *dynsym)
1531 if (! dynamic)
1533 if (info->shared
1534 || hi->ref_dynamic)
1535 *dynsym = TRUE;
1537 else
1539 if (hi->ref_regular)
1540 *dynsym = TRUE;
1546 return TRUE;
1549 /* This routine is used to export all defined symbols into the dynamic
1550 symbol table. It is called via elf_link_hash_traverse. */
1552 bfd_boolean
1553 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1555 struct elf_info_failed *eif = data;
1557 /* Ignore indirect symbols. These are added by the versioning code. */
1558 if (h->root.type == bfd_link_hash_indirect)
1559 return TRUE;
1561 if (h->root.type == bfd_link_hash_warning)
1562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1564 if (h->dynindx == -1
1565 && (h->def_regular
1566 || h->ref_regular))
1568 struct bfd_elf_version_tree *t;
1569 struct bfd_elf_version_expr *d;
1571 for (t = eif->verdefs; t != NULL; t = t->next)
1573 if (t->globals.list != NULL)
1575 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1576 if (d != NULL)
1577 goto doit;
1580 if (t->locals.list != NULL)
1582 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1583 if (d != NULL)
1584 return TRUE;
1588 if (!eif->verdefs)
1590 doit:
1591 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1593 eif->failed = TRUE;
1594 return FALSE;
1599 return TRUE;
1602 /* Look through the symbols which are defined in other shared
1603 libraries and referenced here. Update the list of version
1604 dependencies. This will be put into the .gnu.version_r section.
1605 This function is called via elf_link_hash_traverse. */
1607 bfd_boolean
1608 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1609 void *data)
1611 struct elf_find_verdep_info *rinfo = data;
1612 Elf_Internal_Verneed *t;
1613 Elf_Internal_Vernaux *a;
1614 bfd_size_type amt;
1616 if (h->root.type == bfd_link_hash_warning)
1617 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1619 /* We only care about symbols defined in shared objects with version
1620 information. */
1621 if (!h->def_dynamic
1622 || h->def_regular
1623 || h->dynindx == -1
1624 || h->verinfo.verdef == NULL)
1625 return TRUE;
1627 /* See if we already know about this version. */
1628 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1630 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1631 continue;
1633 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1634 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1635 return TRUE;
1637 break;
1640 /* This is a new version. Add it to tree we are building. */
1642 if (t == NULL)
1644 amt = sizeof *t;
1645 t = bfd_zalloc (rinfo->output_bfd, amt);
1646 if (t == NULL)
1648 rinfo->failed = TRUE;
1649 return FALSE;
1652 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1653 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1654 elf_tdata (rinfo->output_bfd)->verref = t;
1657 amt = sizeof *a;
1658 a = bfd_zalloc (rinfo->output_bfd, amt);
1660 /* Note that we are copying a string pointer here, and testing it
1661 above. If bfd_elf_string_from_elf_section is ever changed to
1662 discard the string data when low in memory, this will have to be
1663 fixed. */
1664 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1666 a->vna_flags = h->verinfo.verdef->vd_flags;
1667 a->vna_nextptr = t->vn_auxptr;
1669 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1670 ++rinfo->vers;
1672 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1674 t->vn_auxptr = a;
1676 return TRUE;
1679 /* Figure out appropriate versions for all the symbols. We may not
1680 have the version number script until we have read all of the input
1681 files, so until that point we don't know which symbols should be
1682 local. This function is called via elf_link_hash_traverse. */
1684 bfd_boolean
1685 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1687 struct elf_assign_sym_version_info *sinfo;
1688 struct bfd_link_info *info;
1689 const struct elf_backend_data *bed;
1690 struct elf_info_failed eif;
1691 char *p;
1692 bfd_size_type amt;
1694 sinfo = data;
1695 info = sinfo->info;
1697 if (h->root.type == bfd_link_hash_warning)
1698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1700 /* Fix the symbol flags. */
1701 eif.failed = FALSE;
1702 eif.info = info;
1703 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1705 if (eif.failed)
1706 sinfo->failed = TRUE;
1707 return FALSE;
1710 /* We only need version numbers for symbols defined in regular
1711 objects. */
1712 if (!h->def_regular)
1713 return TRUE;
1715 bed = get_elf_backend_data (sinfo->output_bfd);
1716 p = strchr (h->root.root.string, ELF_VER_CHR);
1717 if (p != NULL && h->verinfo.vertree == NULL)
1719 struct bfd_elf_version_tree *t;
1720 bfd_boolean hidden;
1722 hidden = TRUE;
1724 /* There are two consecutive ELF_VER_CHR characters if this is
1725 not a hidden symbol. */
1726 ++p;
1727 if (*p == ELF_VER_CHR)
1729 hidden = FALSE;
1730 ++p;
1733 /* If there is no version string, we can just return out. */
1734 if (*p == '\0')
1736 if (hidden)
1737 h->hidden = 1;
1738 return TRUE;
1741 /* Look for the version. If we find it, it is no longer weak. */
1742 for (t = sinfo->verdefs; t != NULL; t = t->next)
1744 if (strcmp (t->name, p) == 0)
1746 size_t len;
1747 char *alc;
1748 struct bfd_elf_version_expr *d;
1750 len = p - h->root.root.string;
1751 alc = bfd_malloc (len);
1752 if (alc == NULL)
1753 return FALSE;
1754 memcpy (alc, h->root.root.string, len - 1);
1755 alc[len - 1] = '\0';
1756 if (alc[len - 2] == ELF_VER_CHR)
1757 alc[len - 2] = '\0';
1759 h->verinfo.vertree = t;
1760 t->used = TRUE;
1761 d = NULL;
1763 if (t->globals.list != NULL)
1764 d = (*t->match) (&t->globals, NULL, alc);
1766 /* See if there is anything to force this symbol to
1767 local scope. */
1768 if (d == NULL && t->locals.list != NULL)
1770 d = (*t->match) (&t->locals, NULL, alc);
1771 if (d != NULL
1772 && h->dynindx != -1
1773 && ! info->export_dynamic)
1774 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1777 free (alc);
1778 break;
1782 /* If we are building an application, we need to create a
1783 version node for this version. */
1784 if (t == NULL && info->executable)
1786 struct bfd_elf_version_tree **pp;
1787 int version_index;
1789 /* If we aren't going to export this symbol, we don't need
1790 to worry about it. */
1791 if (h->dynindx == -1)
1792 return TRUE;
1794 amt = sizeof *t;
1795 t = bfd_zalloc (sinfo->output_bfd, amt);
1796 if (t == NULL)
1798 sinfo->failed = TRUE;
1799 return FALSE;
1802 t->name = p;
1803 t->name_indx = (unsigned int) -1;
1804 t->used = TRUE;
1806 version_index = 1;
1807 /* Don't count anonymous version tag. */
1808 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1809 version_index = 0;
1810 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1811 ++version_index;
1812 t->vernum = version_index;
1814 *pp = t;
1816 h->verinfo.vertree = t;
1818 else if (t == NULL)
1820 /* We could not find the version for a symbol when
1821 generating a shared archive. Return an error. */
1822 (*_bfd_error_handler)
1823 (_("%B: undefined versioned symbol name %s"),
1824 sinfo->output_bfd, h->root.root.string);
1825 bfd_set_error (bfd_error_bad_value);
1826 sinfo->failed = TRUE;
1827 return FALSE;
1830 if (hidden)
1831 h->hidden = 1;
1834 /* If we don't have a version for this symbol, see if we can find
1835 something. */
1836 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1838 struct bfd_elf_version_tree *t;
1839 struct bfd_elf_version_tree *local_ver;
1840 struct bfd_elf_version_expr *d;
1842 /* See if can find what version this symbol is in. If the
1843 symbol is supposed to be local, then don't actually register
1844 it. */
1845 local_ver = NULL;
1846 for (t = sinfo->verdefs; t != NULL; t = t->next)
1848 if (t->globals.list != NULL)
1850 bfd_boolean matched;
1852 matched = FALSE;
1853 d = NULL;
1854 while ((d = (*t->match) (&t->globals, d,
1855 h->root.root.string)) != NULL)
1856 if (d->symver)
1857 matched = TRUE;
1858 else
1860 /* There is a version without definition. Make
1861 the symbol the default definition for this
1862 version. */
1863 h->verinfo.vertree = t;
1864 local_ver = NULL;
1865 d->script = 1;
1866 break;
1868 if (d != NULL)
1869 break;
1870 else if (matched)
1871 /* There is no undefined version for this symbol. Hide the
1872 default one. */
1873 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1876 if (t->locals.list != NULL)
1878 d = NULL;
1879 while ((d = (*t->match) (&t->locals, d,
1880 h->root.root.string)) != NULL)
1882 local_ver = t;
1883 /* If the match is "*", keep looking for a more
1884 explicit, perhaps even global, match.
1885 XXX: Shouldn't this be !d->wildcard instead? */
1886 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1887 break;
1890 if (d != NULL)
1891 break;
1895 if (local_ver != NULL)
1897 h->verinfo.vertree = local_ver;
1898 if (h->dynindx != -1
1899 && ! info->export_dynamic)
1901 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1906 return TRUE;
1909 /* Read and swap the relocs from the section indicated by SHDR. This
1910 may be either a REL or a RELA section. The relocations are
1911 translated into RELA relocations and stored in INTERNAL_RELOCS,
1912 which should have already been allocated to contain enough space.
1913 The EXTERNAL_RELOCS are a buffer where the external form of the
1914 relocations should be stored.
1916 Returns FALSE if something goes wrong. */
1918 static bfd_boolean
1919 elf_link_read_relocs_from_section (bfd *abfd,
1920 asection *sec,
1921 Elf_Internal_Shdr *shdr,
1922 void *external_relocs,
1923 Elf_Internal_Rela *internal_relocs)
1925 const struct elf_backend_data *bed;
1926 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1927 const bfd_byte *erela;
1928 const bfd_byte *erelaend;
1929 Elf_Internal_Rela *irela;
1930 Elf_Internal_Shdr *symtab_hdr;
1931 size_t nsyms;
1933 /* Position ourselves at the start of the section. */
1934 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1935 return FALSE;
1937 /* Read the relocations. */
1938 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1939 return FALSE;
1941 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1942 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1944 bed = get_elf_backend_data (abfd);
1946 /* Convert the external relocations to the internal format. */
1947 if (shdr->sh_entsize == bed->s->sizeof_rel)
1948 swap_in = bed->s->swap_reloc_in;
1949 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1950 swap_in = bed->s->swap_reloca_in;
1951 else
1953 bfd_set_error (bfd_error_wrong_format);
1954 return FALSE;
1957 erela = external_relocs;
1958 erelaend = erela + shdr->sh_size;
1959 irela = internal_relocs;
1960 while (erela < erelaend)
1962 bfd_vma r_symndx;
1964 (*swap_in) (abfd, erela, irela);
1965 r_symndx = ELF32_R_SYM (irela->r_info);
1966 if (bed->s->arch_size == 64)
1967 r_symndx >>= 24;
1968 if ((size_t) r_symndx >= nsyms)
1970 (*_bfd_error_handler)
1971 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1972 " for offset 0x%lx in section `%A'"),
1973 abfd, sec,
1974 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1975 bfd_set_error (bfd_error_bad_value);
1976 return FALSE;
1978 irela += bed->s->int_rels_per_ext_rel;
1979 erela += shdr->sh_entsize;
1982 return TRUE;
1985 /* Read and swap the relocs for a section O. They may have been
1986 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1987 not NULL, they are used as buffers to read into. They are known to
1988 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1989 the return value is allocated using either malloc or bfd_alloc,
1990 according to the KEEP_MEMORY argument. If O has two relocation
1991 sections (both REL and RELA relocations), then the REL_HDR
1992 relocations will appear first in INTERNAL_RELOCS, followed by the
1993 REL_HDR2 relocations. */
1995 Elf_Internal_Rela *
1996 _bfd_elf_link_read_relocs (bfd *abfd,
1997 asection *o,
1998 void *external_relocs,
1999 Elf_Internal_Rela *internal_relocs,
2000 bfd_boolean keep_memory)
2002 Elf_Internal_Shdr *rel_hdr;
2003 void *alloc1 = NULL;
2004 Elf_Internal_Rela *alloc2 = NULL;
2005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2007 if (elf_section_data (o)->relocs != NULL)
2008 return elf_section_data (o)->relocs;
2010 if (o->reloc_count == 0)
2011 return NULL;
2013 rel_hdr = &elf_section_data (o)->rel_hdr;
2015 if (internal_relocs == NULL)
2017 bfd_size_type size;
2019 size = o->reloc_count;
2020 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2021 if (keep_memory)
2022 internal_relocs = bfd_alloc (abfd, size);
2023 else
2024 internal_relocs = alloc2 = bfd_malloc (size);
2025 if (internal_relocs == NULL)
2026 goto error_return;
2029 if (external_relocs == NULL)
2031 bfd_size_type size = rel_hdr->sh_size;
2033 if (elf_section_data (o)->rel_hdr2)
2034 size += elf_section_data (o)->rel_hdr2->sh_size;
2035 alloc1 = bfd_malloc (size);
2036 if (alloc1 == NULL)
2037 goto error_return;
2038 external_relocs = alloc1;
2041 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2042 external_relocs,
2043 internal_relocs))
2044 goto error_return;
2045 if (elf_section_data (o)->rel_hdr2
2046 && (!elf_link_read_relocs_from_section
2047 (abfd, o,
2048 elf_section_data (o)->rel_hdr2,
2049 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2050 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2051 * bed->s->int_rels_per_ext_rel))))
2052 goto error_return;
2054 /* Cache the results for next time, if we can. */
2055 if (keep_memory)
2056 elf_section_data (o)->relocs = internal_relocs;
2058 if (alloc1 != NULL)
2059 free (alloc1);
2061 /* Don't free alloc2, since if it was allocated we are passing it
2062 back (under the name of internal_relocs). */
2064 return internal_relocs;
2066 error_return:
2067 if (alloc1 != NULL)
2068 free (alloc1);
2069 if (alloc2 != NULL)
2070 free (alloc2);
2071 return NULL;
2074 /* Compute the size of, and allocate space for, REL_HDR which is the
2075 section header for a section containing relocations for O. */
2077 bfd_boolean
2078 _bfd_elf_link_size_reloc_section (bfd *abfd,
2079 Elf_Internal_Shdr *rel_hdr,
2080 asection *o)
2082 bfd_size_type reloc_count;
2083 bfd_size_type num_rel_hashes;
2085 /* Figure out how many relocations there will be. */
2086 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2087 reloc_count = elf_section_data (o)->rel_count;
2088 else
2089 reloc_count = elf_section_data (o)->rel_count2;
2091 num_rel_hashes = o->reloc_count;
2092 if (num_rel_hashes < reloc_count)
2093 num_rel_hashes = reloc_count;
2095 /* That allows us to calculate the size of the section. */
2096 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2098 /* The contents field must last into write_object_contents, so we
2099 allocate it with bfd_alloc rather than malloc. Also since we
2100 cannot be sure that the contents will actually be filled in,
2101 we zero the allocated space. */
2102 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2103 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2104 return FALSE;
2106 /* We only allocate one set of hash entries, so we only do it the
2107 first time we are called. */
2108 if (elf_section_data (o)->rel_hashes == NULL
2109 && num_rel_hashes)
2111 struct elf_link_hash_entry **p;
2113 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2114 if (p == NULL)
2115 return FALSE;
2117 elf_section_data (o)->rel_hashes = p;
2120 return TRUE;
2123 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2124 originated from the section given by INPUT_REL_HDR) to the
2125 OUTPUT_BFD. */
2127 bfd_boolean
2128 _bfd_elf_link_output_relocs (bfd *output_bfd,
2129 asection *input_section,
2130 Elf_Internal_Shdr *input_rel_hdr,
2131 Elf_Internal_Rela *internal_relocs,
2132 struct elf_link_hash_entry **rel_hash
2133 ATTRIBUTE_UNUSED)
2135 Elf_Internal_Rela *irela;
2136 Elf_Internal_Rela *irelaend;
2137 bfd_byte *erel;
2138 Elf_Internal_Shdr *output_rel_hdr;
2139 asection *output_section;
2140 unsigned int *rel_countp = NULL;
2141 const struct elf_backend_data *bed;
2142 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2144 output_section = input_section->output_section;
2145 output_rel_hdr = NULL;
2147 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2148 == input_rel_hdr->sh_entsize)
2150 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2151 rel_countp = &elf_section_data (output_section)->rel_count;
2153 else if (elf_section_data (output_section)->rel_hdr2
2154 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2155 == input_rel_hdr->sh_entsize))
2157 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2158 rel_countp = &elf_section_data (output_section)->rel_count2;
2160 else
2162 (*_bfd_error_handler)
2163 (_("%B: relocation size mismatch in %B section %A"),
2164 output_bfd, input_section->owner, input_section);
2165 bfd_set_error (bfd_error_wrong_object_format);
2166 return FALSE;
2169 bed = get_elf_backend_data (output_bfd);
2170 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2171 swap_out = bed->s->swap_reloc_out;
2172 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2173 swap_out = bed->s->swap_reloca_out;
2174 else
2175 abort ();
2177 erel = output_rel_hdr->contents;
2178 erel += *rel_countp * input_rel_hdr->sh_entsize;
2179 irela = internal_relocs;
2180 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2181 * bed->s->int_rels_per_ext_rel);
2182 while (irela < irelaend)
2184 (*swap_out) (output_bfd, irela, erel);
2185 irela += bed->s->int_rels_per_ext_rel;
2186 erel += input_rel_hdr->sh_entsize;
2189 /* Bump the counter, so that we know where to add the next set of
2190 relocations. */
2191 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2193 return TRUE;
2196 /* Make weak undefined symbols in PIE dynamic. */
2198 bfd_boolean
2199 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2200 struct elf_link_hash_entry *h)
2202 if (info->pie
2203 && h->dynindx == -1
2204 && h->root.type == bfd_link_hash_undefweak)
2205 return bfd_elf_link_record_dynamic_symbol (info, h);
2207 return TRUE;
2210 /* Fix up the flags for a symbol. This handles various cases which
2211 can only be fixed after all the input files are seen. This is
2212 currently called by both adjust_dynamic_symbol and
2213 assign_sym_version, which is unnecessary but perhaps more robust in
2214 the face of future changes. */
2216 bfd_boolean
2217 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2218 struct elf_info_failed *eif)
2220 const struct elf_backend_data *bed = NULL;
2222 /* If this symbol was mentioned in a non-ELF file, try to set
2223 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2224 permit a non-ELF file to correctly refer to a symbol defined in
2225 an ELF dynamic object. */
2226 if (h->non_elf)
2228 while (h->root.type == bfd_link_hash_indirect)
2229 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2231 if (h->root.type != bfd_link_hash_defined
2232 && h->root.type != bfd_link_hash_defweak)
2234 h->ref_regular = 1;
2235 h->ref_regular_nonweak = 1;
2237 else
2239 if (h->root.u.def.section->owner != NULL
2240 && (bfd_get_flavour (h->root.u.def.section->owner)
2241 == bfd_target_elf_flavour))
2243 h->ref_regular = 1;
2244 h->ref_regular_nonweak = 1;
2246 else
2247 h->def_regular = 1;
2250 if (h->dynindx == -1
2251 && (h->def_dynamic
2252 || h->ref_dynamic))
2254 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2256 eif->failed = TRUE;
2257 return FALSE;
2261 else
2263 /* Unfortunately, NON_ELF is only correct if the symbol
2264 was first seen in a non-ELF file. Fortunately, if the symbol
2265 was first seen in an ELF file, we're probably OK unless the
2266 symbol was defined in a non-ELF file. Catch that case here.
2267 FIXME: We're still in trouble if the symbol was first seen in
2268 a dynamic object, and then later in a non-ELF regular object. */
2269 if ((h->root.type == bfd_link_hash_defined
2270 || h->root.type == bfd_link_hash_defweak)
2271 && !h->def_regular
2272 && (h->root.u.def.section->owner != NULL
2273 ? (bfd_get_flavour (h->root.u.def.section->owner)
2274 != bfd_target_elf_flavour)
2275 : (bfd_is_abs_section (h->root.u.def.section)
2276 && !h->def_dynamic)))
2277 h->def_regular = 1;
2280 /* Backend specific symbol fixup. */
2281 if (elf_hash_table (eif->info)->dynobj)
2283 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2284 if (bed->elf_backend_fixup_symbol
2285 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2286 return FALSE;
2289 /* If this is a final link, and the symbol was defined as a common
2290 symbol in a regular object file, and there was no definition in
2291 any dynamic object, then the linker will have allocated space for
2292 the symbol in a common section but the DEF_REGULAR
2293 flag will not have been set. */
2294 if (h->root.type == bfd_link_hash_defined
2295 && !h->def_regular
2296 && h->ref_regular
2297 && !h->def_dynamic
2298 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2299 h->def_regular = 1;
2301 /* If -Bsymbolic was used (which means to bind references to global
2302 symbols to the definition within the shared object), and this
2303 symbol was defined in a regular object, then it actually doesn't
2304 need a PLT entry. Likewise, if the symbol has non-default
2305 visibility. If the symbol has hidden or internal visibility, we
2306 will force it local. */
2307 if (h->needs_plt
2308 && eif->info->shared
2309 && is_elf_hash_table (eif->info->hash)
2310 && (eif->info->symbolic
2311 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2312 && h->def_regular)
2314 bfd_boolean force_local;
2316 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2317 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2318 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2321 /* If a weak undefined symbol has non-default visibility, we also
2322 hide it from the dynamic linker. */
2323 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2324 && h->root.type == bfd_link_hash_undefweak)
2326 const struct elf_backend_data *bed;
2327 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2328 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2331 /* If this is a weak defined symbol in a dynamic object, and we know
2332 the real definition in the dynamic object, copy interesting flags
2333 over to the real definition. */
2334 if (h->u.weakdef != NULL)
2336 struct elf_link_hash_entry *weakdef;
2338 weakdef = h->u.weakdef;
2339 if (h->root.type == bfd_link_hash_indirect)
2340 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2342 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2343 || h->root.type == bfd_link_hash_defweak);
2344 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2345 || weakdef->root.type == bfd_link_hash_defweak);
2346 BFD_ASSERT (weakdef->def_dynamic);
2348 /* If the real definition is defined by a regular object file,
2349 don't do anything special. See the longer description in
2350 _bfd_elf_adjust_dynamic_symbol, below. */
2351 if (weakdef->def_regular)
2352 h->u.weakdef = NULL;
2353 else
2354 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2358 return TRUE;
2361 /* Make the backend pick a good value for a dynamic symbol. This is
2362 called via elf_link_hash_traverse, and also calls itself
2363 recursively. */
2365 bfd_boolean
2366 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2368 struct elf_info_failed *eif = data;
2369 bfd *dynobj;
2370 const struct elf_backend_data *bed;
2372 if (! is_elf_hash_table (eif->info->hash))
2373 return FALSE;
2375 if (h->root.type == bfd_link_hash_warning)
2377 h->got = elf_hash_table (eif->info)->init_got_offset;
2378 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2380 /* When warning symbols are created, they **replace** the "real"
2381 entry in the hash table, thus we never get to see the real
2382 symbol in a hash traversal. So look at it now. */
2383 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2386 /* Ignore indirect symbols. These are added by the versioning code. */
2387 if (h->root.type == bfd_link_hash_indirect)
2388 return TRUE;
2390 /* Fix the symbol flags. */
2391 if (! _bfd_elf_fix_symbol_flags (h, eif))
2392 return FALSE;
2394 /* If this symbol does not require a PLT entry, and it is not
2395 defined by a dynamic object, or is not referenced by a regular
2396 object, ignore it. We do have to handle a weak defined symbol,
2397 even if no regular object refers to it, if we decided to add it
2398 to the dynamic symbol table. FIXME: Do we normally need to worry
2399 about symbols which are defined by one dynamic object and
2400 referenced by another one? */
2401 if (!h->needs_plt
2402 && (h->def_regular
2403 || !h->def_dynamic
2404 || (!h->ref_regular
2405 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2407 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2408 return TRUE;
2411 /* If we've already adjusted this symbol, don't do it again. This
2412 can happen via a recursive call. */
2413 if (h->dynamic_adjusted)
2414 return TRUE;
2416 /* Don't look at this symbol again. Note that we must set this
2417 after checking the above conditions, because we may look at a
2418 symbol once, decide not to do anything, and then get called
2419 recursively later after REF_REGULAR is set below. */
2420 h->dynamic_adjusted = 1;
2422 /* If this is a weak definition, and we know a real definition, and
2423 the real symbol is not itself defined by a regular object file,
2424 then get a good value for the real definition. We handle the
2425 real symbol first, for the convenience of the backend routine.
2427 Note that there is a confusing case here. If the real definition
2428 is defined by a regular object file, we don't get the real symbol
2429 from the dynamic object, but we do get the weak symbol. If the
2430 processor backend uses a COPY reloc, then if some routine in the
2431 dynamic object changes the real symbol, we will not see that
2432 change in the corresponding weak symbol. This is the way other
2433 ELF linkers work as well, and seems to be a result of the shared
2434 library model.
2436 I will clarify this issue. Most SVR4 shared libraries define the
2437 variable _timezone and define timezone as a weak synonym. The
2438 tzset call changes _timezone. If you write
2439 extern int timezone;
2440 int _timezone = 5;
2441 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2442 you might expect that, since timezone is a synonym for _timezone,
2443 the same number will print both times. However, if the processor
2444 backend uses a COPY reloc, then actually timezone will be copied
2445 into your process image, and, since you define _timezone
2446 yourself, _timezone will not. Thus timezone and _timezone will
2447 wind up at different memory locations. The tzset call will set
2448 _timezone, leaving timezone unchanged. */
2450 if (h->u.weakdef != NULL)
2452 /* If we get to this point, we know there is an implicit
2453 reference by a regular object file via the weak symbol H.
2454 FIXME: Is this really true? What if the traversal finds
2455 H->U.WEAKDEF before it finds H? */
2456 h->u.weakdef->ref_regular = 1;
2458 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2459 return FALSE;
2462 /* If a symbol has no type and no size and does not require a PLT
2463 entry, then we are probably about to do the wrong thing here: we
2464 are probably going to create a COPY reloc for an empty object.
2465 This case can arise when a shared object is built with assembly
2466 code, and the assembly code fails to set the symbol type. */
2467 if (h->size == 0
2468 && h->type == STT_NOTYPE
2469 && !h->needs_plt)
2470 (*_bfd_error_handler)
2471 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2472 h->root.root.string);
2474 dynobj = elf_hash_table (eif->info)->dynobj;
2475 bed = get_elf_backend_data (dynobj);
2476 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2478 eif->failed = TRUE;
2479 return FALSE;
2482 return TRUE;
2485 /* Adjust all external symbols pointing into SEC_MERGE sections
2486 to reflect the object merging within the sections. */
2488 bfd_boolean
2489 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2491 asection *sec;
2493 if (h->root.type == bfd_link_hash_warning)
2494 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2496 if ((h->root.type == bfd_link_hash_defined
2497 || h->root.type == bfd_link_hash_defweak)
2498 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2499 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2501 bfd *output_bfd = data;
2503 h->root.u.def.value =
2504 _bfd_merged_section_offset (output_bfd,
2505 &h->root.u.def.section,
2506 elf_section_data (sec)->sec_info,
2507 h->root.u.def.value);
2510 return TRUE;
2513 /* Returns false if the symbol referred to by H should be considered
2514 to resolve local to the current module, and true if it should be
2515 considered to bind dynamically. */
2517 bfd_boolean
2518 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2519 struct bfd_link_info *info,
2520 bfd_boolean ignore_protected)
2522 bfd_boolean binding_stays_local_p;
2524 if (h == NULL)
2525 return FALSE;
2527 while (h->root.type == bfd_link_hash_indirect
2528 || h->root.type == bfd_link_hash_warning)
2529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2531 /* If it was forced local, then clearly it's not dynamic. */
2532 if (h->dynindx == -1)
2533 return FALSE;
2534 if (h->forced_local)
2535 return FALSE;
2537 /* Identify the cases where name binding rules say that a
2538 visible symbol resolves locally. */
2539 binding_stays_local_p = info->executable || info->symbolic;
2541 switch (ELF_ST_VISIBILITY (h->other))
2543 case STV_INTERNAL:
2544 case STV_HIDDEN:
2545 return FALSE;
2547 case STV_PROTECTED:
2548 /* Proper resolution for function pointer equality may require
2549 that these symbols perhaps be resolved dynamically, even though
2550 we should be resolving them to the current module. */
2551 if (!ignore_protected || h->type != STT_FUNC)
2552 binding_stays_local_p = TRUE;
2553 break;
2555 default:
2556 break;
2559 /* If it isn't defined locally, then clearly it's dynamic. */
2560 if (!h->def_regular)
2561 return TRUE;
2563 /* Otherwise, the symbol is dynamic if binding rules don't tell
2564 us that it remains local. */
2565 return !binding_stays_local_p;
2568 /* Return true if the symbol referred to by H should be considered
2569 to resolve local to the current module, and false otherwise. Differs
2570 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2571 undefined symbols and weak symbols. */
2573 bfd_boolean
2574 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2575 struct bfd_link_info *info,
2576 bfd_boolean local_protected)
2578 /* If it's a local sym, of course we resolve locally. */
2579 if (h == NULL)
2580 return TRUE;
2582 /* Common symbols that become definitions don't get the DEF_REGULAR
2583 flag set, so test it first, and don't bail out. */
2584 if (ELF_COMMON_DEF_P (h))
2585 /* Do nothing. */;
2586 /* If we don't have a definition in a regular file, then we can't
2587 resolve locally. The sym is either undefined or dynamic. */
2588 else if (!h->def_regular)
2589 return FALSE;
2591 /* Forced local symbols resolve locally. */
2592 if (h->forced_local)
2593 return TRUE;
2595 /* As do non-dynamic symbols. */
2596 if (h->dynindx == -1)
2597 return TRUE;
2599 /* At this point, we know the symbol is defined and dynamic. In an
2600 executable it must resolve locally, likewise when building symbolic
2601 shared libraries. */
2602 if (info->executable || info->symbolic)
2603 return TRUE;
2605 /* Now deal with defined dynamic symbols in shared libraries. Ones
2606 with default visibility might not resolve locally. */
2607 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2608 return FALSE;
2610 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2611 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2612 return TRUE;
2614 /* STV_PROTECTED non-function symbols are local. */
2615 if (h->type != STT_FUNC)
2616 return TRUE;
2618 /* Function pointer equality tests may require that STV_PROTECTED
2619 symbols be treated as dynamic symbols, even when we know that the
2620 dynamic linker will resolve them locally. */
2621 return local_protected;
2624 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2625 aligned. Returns the first TLS output section. */
2627 struct bfd_section *
2628 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2630 struct bfd_section *sec, *tls;
2631 unsigned int align = 0;
2633 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2634 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2635 break;
2636 tls = sec;
2638 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2639 if (sec->alignment_power > align)
2640 align = sec->alignment_power;
2642 elf_hash_table (info)->tls_sec = tls;
2644 /* Ensure the alignment of the first section is the largest alignment,
2645 so that the tls segment starts aligned. */
2646 if (tls != NULL)
2647 tls->alignment_power = align;
2649 return tls;
2652 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2653 static bfd_boolean
2654 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2655 Elf_Internal_Sym *sym)
2657 const struct elf_backend_data *bed;
2659 /* Local symbols do not count, but target specific ones might. */
2660 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2661 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2662 return FALSE;
2664 /* Function symbols do not count. */
2665 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2666 return FALSE;
2668 /* If the section is undefined, then so is the symbol. */
2669 if (sym->st_shndx == SHN_UNDEF)
2670 return FALSE;
2672 /* If the symbol is defined in the common section, then
2673 it is a common definition and so does not count. */
2674 bed = get_elf_backend_data (abfd);
2675 if (bed->common_definition (sym))
2676 return FALSE;
2678 /* If the symbol is in a target specific section then we
2679 must rely upon the backend to tell us what it is. */
2680 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2681 /* FIXME - this function is not coded yet:
2683 return _bfd_is_global_symbol_definition (abfd, sym);
2685 Instead for now assume that the definition is not global,
2686 Even if this is wrong, at least the linker will behave
2687 in the same way that it used to do. */
2688 return FALSE;
2690 return TRUE;
2693 /* Search the symbol table of the archive element of the archive ABFD
2694 whose archive map contains a mention of SYMDEF, and determine if
2695 the symbol is defined in this element. */
2696 static bfd_boolean
2697 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2699 Elf_Internal_Shdr * hdr;
2700 bfd_size_type symcount;
2701 bfd_size_type extsymcount;
2702 bfd_size_type extsymoff;
2703 Elf_Internal_Sym *isymbuf;
2704 Elf_Internal_Sym *isym;
2705 Elf_Internal_Sym *isymend;
2706 bfd_boolean result;
2708 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2709 if (abfd == NULL)
2710 return FALSE;
2712 if (! bfd_check_format (abfd, bfd_object))
2713 return FALSE;
2715 /* If we have already included the element containing this symbol in the
2716 link then we do not need to include it again. Just claim that any symbol
2717 it contains is not a definition, so that our caller will not decide to
2718 (re)include this element. */
2719 if (abfd->archive_pass)
2720 return FALSE;
2722 /* Select the appropriate symbol table. */
2723 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2724 hdr = &elf_tdata (abfd)->symtab_hdr;
2725 else
2726 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2728 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2730 /* The sh_info field of the symtab header tells us where the
2731 external symbols start. We don't care about the local symbols. */
2732 if (elf_bad_symtab (abfd))
2734 extsymcount = symcount;
2735 extsymoff = 0;
2737 else
2739 extsymcount = symcount - hdr->sh_info;
2740 extsymoff = hdr->sh_info;
2743 if (extsymcount == 0)
2744 return FALSE;
2746 /* Read in the symbol table. */
2747 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2748 NULL, NULL, NULL);
2749 if (isymbuf == NULL)
2750 return FALSE;
2752 /* Scan the symbol table looking for SYMDEF. */
2753 result = FALSE;
2754 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2756 const char *name;
2758 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2759 isym->st_name);
2760 if (name == NULL)
2761 break;
2763 if (strcmp (name, symdef->name) == 0)
2765 result = is_global_data_symbol_definition (abfd, isym);
2766 break;
2770 free (isymbuf);
2772 return result;
2775 /* Add an entry to the .dynamic table. */
2777 bfd_boolean
2778 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2779 bfd_vma tag,
2780 bfd_vma val)
2782 struct elf_link_hash_table *hash_table;
2783 const struct elf_backend_data *bed;
2784 asection *s;
2785 bfd_size_type newsize;
2786 bfd_byte *newcontents;
2787 Elf_Internal_Dyn dyn;
2789 hash_table = elf_hash_table (info);
2790 if (! is_elf_hash_table (hash_table))
2791 return FALSE;
2793 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2794 _bfd_error_handler
2795 (_("warning: creating a DT_TEXTREL in a shared object."));
2797 bed = get_elf_backend_data (hash_table->dynobj);
2798 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2799 BFD_ASSERT (s != NULL);
2801 newsize = s->size + bed->s->sizeof_dyn;
2802 newcontents = bfd_realloc (s->contents, newsize);
2803 if (newcontents == NULL)
2804 return FALSE;
2806 dyn.d_tag = tag;
2807 dyn.d_un.d_val = val;
2808 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2810 s->size = newsize;
2811 s->contents = newcontents;
2813 return TRUE;
2816 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2817 otherwise just check whether one already exists. Returns -1 on error,
2818 1 if a DT_NEEDED tag already exists, and 0 on success. */
2820 static int
2821 elf_add_dt_needed_tag (bfd *abfd,
2822 struct bfd_link_info *info,
2823 const char *soname,
2824 bfd_boolean do_it)
2826 struct elf_link_hash_table *hash_table;
2827 bfd_size_type oldsize;
2828 bfd_size_type strindex;
2830 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2831 return -1;
2833 hash_table = elf_hash_table (info);
2834 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2835 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2836 if (strindex == (bfd_size_type) -1)
2837 return -1;
2839 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2841 asection *sdyn;
2842 const struct elf_backend_data *bed;
2843 bfd_byte *extdyn;
2845 bed = get_elf_backend_data (hash_table->dynobj);
2846 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2847 if (sdyn != NULL)
2848 for (extdyn = sdyn->contents;
2849 extdyn < sdyn->contents + sdyn->size;
2850 extdyn += bed->s->sizeof_dyn)
2852 Elf_Internal_Dyn dyn;
2854 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2855 if (dyn.d_tag == DT_NEEDED
2856 && dyn.d_un.d_val == strindex)
2858 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2859 return 1;
2864 if (do_it)
2866 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2867 return -1;
2869 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2870 return -1;
2872 else
2873 /* We were just checking for existence of the tag. */
2874 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2876 return 0;
2879 /* Sort symbol by value and section. */
2880 static int
2881 elf_sort_symbol (const void *arg1, const void *arg2)
2883 const struct elf_link_hash_entry *h1;
2884 const struct elf_link_hash_entry *h2;
2885 bfd_signed_vma vdiff;
2887 h1 = *(const struct elf_link_hash_entry **) arg1;
2888 h2 = *(const struct elf_link_hash_entry **) arg2;
2889 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2890 if (vdiff != 0)
2891 return vdiff > 0 ? 1 : -1;
2892 else
2894 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2895 if (sdiff != 0)
2896 return sdiff > 0 ? 1 : -1;
2898 return 0;
2901 /* This function is used to adjust offsets into .dynstr for
2902 dynamic symbols. This is called via elf_link_hash_traverse. */
2904 static bfd_boolean
2905 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2907 struct elf_strtab_hash *dynstr = data;
2909 if (h->root.type == bfd_link_hash_warning)
2910 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2912 if (h->dynindx != -1)
2913 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2914 return TRUE;
2917 /* Assign string offsets in .dynstr, update all structures referencing
2918 them. */
2920 static bfd_boolean
2921 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2923 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2924 struct elf_link_local_dynamic_entry *entry;
2925 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2926 bfd *dynobj = hash_table->dynobj;
2927 asection *sdyn;
2928 bfd_size_type size;
2929 const struct elf_backend_data *bed;
2930 bfd_byte *extdyn;
2932 _bfd_elf_strtab_finalize (dynstr);
2933 size = _bfd_elf_strtab_size (dynstr);
2935 bed = get_elf_backend_data (dynobj);
2936 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2937 BFD_ASSERT (sdyn != NULL);
2939 /* Update all .dynamic entries referencing .dynstr strings. */
2940 for (extdyn = sdyn->contents;
2941 extdyn < sdyn->contents + sdyn->size;
2942 extdyn += bed->s->sizeof_dyn)
2944 Elf_Internal_Dyn dyn;
2946 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2947 switch (dyn.d_tag)
2949 case DT_STRSZ:
2950 dyn.d_un.d_val = size;
2951 break;
2952 case DT_NEEDED:
2953 case DT_SONAME:
2954 case DT_RPATH:
2955 case DT_RUNPATH:
2956 case DT_FILTER:
2957 case DT_AUXILIARY:
2958 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2959 break;
2960 default:
2961 continue;
2963 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2966 /* Now update local dynamic symbols. */
2967 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2968 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2969 entry->isym.st_name);
2971 /* And the rest of dynamic symbols. */
2972 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2974 /* Adjust version definitions. */
2975 if (elf_tdata (output_bfd)->cverdefs)
2977 asection *s;
2978 bfd_byte *p;
2979 bfd_size_type i;
2980 Elf_Internal_Verdef def;
2981 Elf_Internal_Verdaux defaux;
2983 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2984 p = s->contents;
2987 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2988 &def);
2989 p += sizeof (Elf_External_Verdef);
2990 if (def.vd_aux != sizeof (Elf_External_Verdef))
2991 continue;
2992 for (i = 0; i < def.vd_cnt; ++i)
2994 _bfd_elf_swap_verdaux_in (output_bfd,
2995 (Elf_External_Verdaux *) p, &defaux);
2996 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2997 defaux.vda_name);
2998 _bfd_elf_swap_verdaux_out (output_bfd,
2999 &defaux, (Elf_External_Verdaux *) p);
3000 p += sizeof (Elf_External_Verdaux);
3003 while (def.vd_next);
3006 /* Adjust version references. */
3007 if (elf_tdata (output_bfd)->verref)
3009 asection *s;
3010 bfd_byte *p;
3011 bfd_size_type i;
3012 Elf_Internal_Verneed need;
3013 Elf_Internal_Vernaux needaux;
3015 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3016 p = s->contents;
3019 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3020 &need);
3021 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3022 _bfd_elf_swap_verneed_out (output_bfd, &need,
3023 (Elf_External_Verneed *) p);
3024 p += sizeof (Elf_External_Verneed);
3025 for (i = 0; i < need.vn_cnt; ++i)
3027 _bfd_elf_swap_vernaux_in (output_bfd,
3028 (Elf_External_Vernaux *) p, &needaux);
3029 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3030 needaux.vna_name);
3031 _bfd_elf_swap_vernaux_out (output_bfd,
3032 &needaux,
3033 (Elf_External_Vernaux *) p);
3034 p += sizeof (Elf_External_Vernaux);
3037 while (need.vn_next);
3040 return TRUE;
3043 /* Add symbols from an ELF object file to the linker hash table. */
3045 static bfd_boolean
3046 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3048 Elf_Internal_Shdr *hdr;
3049 bfd_size_type symcount;
3050 bfd_size_type extsymcount;
3051 bfd_size_type extsymoff;
3052 struct elf_link_hash_entry **sym_hash;
3053 bfd_boolean dynamic;
3054 Elf_External_Versym *extversym = NULL;
3055 Elf_External_Versym *ever;
3056 struct elf_link_hash_entry *weaks;
3057 struct elf_link_hash_entry **nondeflt_vers = NULL;
3058 bfd_size_type nondeflt_vers_cnt = 0;
3059 Elf_Internal_Sym *isymbuf = NULL;
3060 Elf_Internal_Sym *isym;
3061 Elf_Internal_Sym *isymend;
3062 const struct elf_backend_data *bed;
3063 bfd_boolean add_needed;
3064 struct elf_link_hash_table *htab;
3065 bfd_size_type amt;
3066 void *alloc_mark = NULL;
3067 void *old_tab = NULL;
3068 void *old_hash;
3069 void *old_ent;
3070 struct bfd_link_hash_entry *old_undefs = NULL;
3071 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3072 long old_dynsymcount = 0;
3073 size_t tabsize = 0;
3074 size_t hashsize = 0;
3076 htab = elf_hash_table (info);
3077 bed = get_elf_backend_data (abfd);
3079 if ((abfd->flags & DYNAMIC) == 0)
3080 dynamic = FALSE;
3081 else
3083 dynamic = TRUE;
3085 /* You can't use -r against a dynamic object. Also, there's no
3086 hope of using a dynamic object which does not exactly match
3087 the format of the output file. */
3088 if (info->relocatable
3089 || !is_elf_hash_table (htab)
3090 || htab->root.creator != abfd->xvec)
3092 if (info->relocatable)
3093 bfd_set_error (bfd_error_invalid_operation);
3094 else
3095 bfd_set_error (bfd_error_wrong_format);
3096 goto error_return;
3100 /* As a GNU extension, any input sections which are named
3101 .gnu.warning.SYMBOL are treated as warning symbols for the given
3102 symbol. This differs from .gnu.warning sections, which generate
3103 warnings when they are included in an output file. */
3104 if (info->executable)
3106 asection *s;
3108 for (s = abfd->sections; s != NULL; s = s->next)
3110 const char *name;
3112 name = bfd_get_section_name (abfd, s);
3113 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3115 char *msg;
3116 bfd_size_type sz;
3118 name += sizeof ".gnu.warning." - 1;
3120 /* If this is a shared object, then look up the symbol
3121 in the hash table. If it is there, and it is already
3122 been defined, then we will not be using the entry
3123 from this shared object, so we don't need to warn.
3124 FIXME: If we see the definition in a regular object
3125 later on, we will warn, but we shouldn't. The only
3126 fix is to keep track of what warnings we are supposed
3127 to emit, and then handle them all at the end of the
3128 link. */
3129 if (dynamic)
3131 struct elf_link_hash_entry *h;
3133 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3135 /* FIXME: What about bfd_link_hash_common? */
3136 if (h != NULL
3137 && (h->root.type == bfd_link_hash_defined
3138 || h->root.type == bfd_link_hash_defweak))
3140 /* We don't want to issue this warning. Clobber
3141 the section size so that the warning does not
3142 get copied into the output file. */
3143 s->size = 0;
3144 continue;
3148 sz = s->size;
3149 msg = bfd_alloc (abfd, sz + 1);
3150 if (msg == NULL)
3151 goto error_return;
3153 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3154 goto error_return;
3156 msg[sz] = '\0';
3158 if (! (_bfd_generic_link_add_one_symbol
3159 (info, abfd, name, BSF_WARNING, s, 0, msg,
3160 FALSE, bed->collect, NULL)))
3161 goto error_return;
3163 if (! info->relocatable)
3165 /* Clobber the section size so that the warning does
3166 not get copied into the output file. */
3167 s->size = 0;
3169 /* Also set SEC_EXCLUDE, so that symbols defined in
3170 the warning section don't get copied to the output. */
3171 s->flags |= SEC_EXCLUDE;
3177 add_needed = TRUE;
3178 if (! dynamic)
3180 /* If we are creating a shared library, create all the dynamic
3181 sections immediately. We need to attach them to something,
3182 so we attach them to this BFD, provided it is the right
3183 format. FIXME: If there are no input BFD's of the same
3184 format as the output, we can't make a shared library. */
3185 if (info->shared
3186 && is_elf_hash_table (htab)
3187 && htab->root.creator == abfd->xvec
3188 && !htab->dynamic_sections_created)
3190 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3191 goto error_return;
3194 else if (!is_elf_hash_table (htab))
3195 goto error_return;
3196 else
3198 asection *s;
3199 const char *soname = NULL;
3200 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3201 int ret;
3203 /* ld --just-symbols and dynamic objects don't mix very well.
3204 ld shouldn't allow it. */
3205 if ((s = abfd->sections) != NULL
3206 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3207 abort ();
3209 /* If this dynamic lib was specified on the command line with
3210 --as-needed in effect, then we don't want to add a DT_NEEDED
3211 tag unless the lib is actually used. Similary for libs brought
3212 in by another lib's DT_NEEDED. When --no-add-needed is used
3213 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3214 any dynamic library in DT_NEEDED tags in the dynamic lib at
3215 all. */
3216 add_needed = (elf_dyn_lib_class (abfd)
3217 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3218 | DYN_NO_NEEDED)) == 0;
3220 s = bfd_get_section_by_name (abfd, ".dynamic");
3221 if (s != NULL)
3223 bfd_byte *dynbuf;
3224 bfd_byte *extdyn;
3225 int elfsec;
3226 unsigned long shlink;
3228 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3229 goto error_free_dyn;
3231 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3232 if (elfsec == -1)
3233 goto error_free_dyn;
3234 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3236 for (extdyn = dynbuf;
3237 extdyn < dynbuf + s->size;
3238 extdyn += bed->s->sizeof_dyn)
3240 Elf_Internal_Dyn dyn;
3242 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3243 if (dyn.d_tag == DT_SONAME)
3245 unsigned int tagv = dyn.d_un.d_val;
3246 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3247 if (soname == NULL)
3248 goto error_free_dyn;
3250 if (dyn.d_tag == DT_NEEDED)
3252 struct bfd_link_needed_list *n, **pn;
3253 char *fnm, *anm;
3254 unsigned int tagv = dyn.d_un.d_val;
3256 amt = sizeof (struct bfd_link_needed_list);
3257 n = bfd_alloc (abfd, amt);
3258 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3259 if (n == NULL || fnm == NULL)
3260 goto error_free_dyn;
3261 amt = strlen (fnm) + 1;
3262 anm = bfd_alloc (abfd, amt);
3263 if (anm == NULL)
3264 goto error_free_dyn;
3265 memcpy (anm, fnm, amt);
3266 n->name = anm;
3267 n->by = abfd;
3268 n->next = NULL;
3269 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3271 *pn = n;
3273 if (dyn.d_tag == DT_RUNPATH)
3275 struct bfd_link_needed_list *n, **pn;
3276 char *fnm, *anm;
3277 unsigned int tagv = dyn.d_un.d_val;
3279 amt = sizeof (struct bfd_link_needed_list);
3280 n = bfd_alloc (abfd, amt);
3281 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3282 if (n == NULL || fnm == NULL)
3283 goto error_free_dyn;
3284 amt = strlen (fnm) + 1;
3285 anm = bfd_alloc (abfd, amt);
3286 if (anm == NULL)
3287 goto error_free_dyn;
3288 memcpy (anm, fnm, amt);
3289 n->name = anm;
3290 n->by = abfd;
3291 n->next = NULL;
3292 for (pn = & runpath;
3293 *pn != NULL;
3294 pn = &(*pn)->next)
3296 *pn = n;
3298 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3299 if (!runpath && dyn.d_tag == DT_RPATH)
3301 struct bfd_link_needed_list *n, **pn;
3302 char *fnm, *anm;
3303 unsigned int tagv = dyn.d_un.d_val;
3305 amt = sizeof (struct bfd_link_needed_list);
3306 n = bfd_alloc (abfd, amt);
3307 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3308 if (n == NULL || fnm == NULL)
3309 goto error_free_dyn;
3310 amt = strlen (fnm) + 1;
3311 anm = bfd_alloc (abfd, amt);
3312 if (anm == NULL)
3314 error_free_dyn:
3315 free (dynbuf);
3316 goto error_return;
3318 memcpy (anm, fnm, amt);
3319 n->name = anm;
3320 n->by = abfd;
3321 n->next = NULL;
3322 for (pn = & rpath;
3323 *pn != NULL;
3324 pn = &(*pn)->next)
3326 *pn = n;
3330 free (dynbuf);
3333 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3334 frees all more recently bfd_alloc'd blocks as well. */
3335 if (runpath)
3336 rpath = runpath;
3338 if (rpath)
3340 struct bfd_link_needed_list **pn;
3341 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3343 *pn = rpath;
3346 /* We do not want to include any of the sections in a dynamic
3347 object in the output file. We hack by simply clobbering the
3348 list of sections in the BFD. This could be handled more
3349 cleanly by, say, a new section flag; the existing
3350 SEC_NEVER_LOAD flag is not the one we want, because that one
3351 still implies that the section takes up space in the output
3352 file. */
3353 bfd_section_list_clear (abfd);
3355 /* Find the name to use in a DT_NEEDED entry that refers to this
3356 object. If the object has a DT_SONAME entry, we use it.
3357 Otherwise, if the generic linker stuck something in
3358 elf_dt_name, we use that. Otherwise, we just use the file
3359 name. */
3360 if (soname == NULL || *soname == '\0')
3362 soname = elf_dt_name (abfd);
3363 if (soname == NULL || *soname == '\0')
3364 soname = bfd_get_filename (abfd);
3367 /* Save the SONAME because sometimes the linker emulation code
3368 will need to know it. */
3369 elf_dt_name (abfd) = soname;
3371 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3372 if (ret < 0)
3373 goto error_return;
3375 /* If we have already included this dynamic object in the
3376 link, just ignore it. There is no reason to include a
3377 particular dynamic object more than once. */
3378 if (ret > 0)
3379 return TRUE;
3382 /* If this is a dynamic object, we always link against the .dynsym
3383 symbol table, not the .symtab symbol table. The dynamic linker
3384 will only see the .dynsym symbol table, so there is no reason to
3385 look at .symtab for a dynamic object. */
3387 if (! dynamic || elf_dynsymtab (abfd) == 0)
3388 hdr = &elf_tdata (abfd)->symtab_hdr;
3389 else
3390 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3392 symcount = hdr->sh_size / bed->s->sizeof_sym;
3394 /* The sh_info field of the symtab header tells us where the
3395 external symbols start. We don't care about the local symbols at
3396 this point. */
3397 if (elf_bad_symtab (abfd))
3399 extsymcount = symcount;
3400 extsymoff = 0;
3402 else
3404 extsymcount = symcount - hdr->sh_info;
3405 extsymoff = hdr->sh_info;
3408 sym_hash = NULL;
3409 if (extsymcount != 0)
3411 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3412 NULL, NULL, NULL);
3413 if (isymbuf == NULL)
3414 goto error_return;
3416 /* We store a pointer to the hash table entry for each external
3417 symbol. */
3418 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3419 sym_hash = bfd_alloc (abfd, amt);
3420 if (sym_hash == NULL)
3421 goto error_free_sym;
3422 elf_sym_hashes (abfd) = sym_hash;
3425 if (dynamic)
3427 /* Read in any version definitions. */
3428 if (!_bfd_elf_slurp_version_tables (abfd,
3429 info->default_imported_symver))
3430 goto error_free_sym;
3432 /* Read in the symbol versions, but don't bother to convert them
3433 to internal format. */
3434 if (elf_dynversym (abfd) != 0)
3436 Elf_Internal_Shdr *versymhdr;
3438 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3439 extversym = bfd_malloc (versymhdr->sh_size);
3440 if (extversym == NULL)
3441 goto error_free_sym;
3442 amt = versymhdr->sh_size;
3443 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3444 || bfd_bread (extversym, amt, abfd) != amt)
3445 goto error_free_vers;
3449 /* If we are loading an as-needed shared lib, save the symbol table
3450 state before we start adding symbols. If the lib turns out
3451 to be unneeded, restore the state. */
3452 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3454 unsigned int i;
3455 size_t entsize;
3457 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3459 struct bfd_hash_entry *p;
3460 struct elf_link_hash_entry *h;
3462 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3464 h = (struct elf_link_hash_entry *) p;
3465 entsize += htab->root.table.entsize;
3466 if (h->root.type == bfd_link_hash_warning)
3467 entsize += htab->root.table.entsize;
3471 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3472 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3473 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3474 if (old_tab == NULL)
3475 goto error_free_vers;
3477 /* Remember the current objalloc pointer, so that all mem for
3478 symbols added can later be reclaimed. */
3479 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3480 if (alloc_mark == NULL)
3481 goto error_free_vers;
3483 /* Clone the symbol table and sym hashes. Remember some
3484 pointers into the symbol table, and dynamic symbol count. */
3485 old_hash = (char *) old_tab + tabsize;
3486 old_ent = (char *) old_hash + hashsize;
3487 memcpy (old_tab, htab->root.table.table, tabsize);
3488 memcpy (old_hash, sym_hash, hashsize);
3489 old_undefs = htab->root.undefs;
3490 old_undefs_tail = htab->root.undefs_tail;
3491 old_dynsymcount = htab->dynsymcount;
3493 for (i = 0; i < htab->root.table.size; i++)
3495 struct bfd_hash_entry *p;
3496 struct elf_link_hash_entry *h;
3498 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3500 memcpy (old_ent, p, htab->root.table.entsize);
3501 old_ent = (char *) old_ent + htab->root.table.entsize;
3502 h = (struct elf_link_hash_entry *) p;
3503 if (h->root.type == bfd_link_hash_warning)
3505 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3506 old_ent = (char *) old_ent + htab->root.table.entsize;
3512 weaks = NULL;
3513 ever = extversym != NULL ? extversym + extsymoff : NULL;
3514 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3515 isym < isymend;
3516 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3518 int bind;
3519 bfd_vma value;
3520 asection *sec, *new_sec;
3521 flagword flags;
3522 const char *name;
3523 struct elf_link_hash_entry *h;
3524 bfd_boolean definition;
3525 bfd_boolean size_change_ok;
3526 bfd_boolean type_change_ok;
3527 bfd_boolean new_weakdef;
3528 bfd_boolean override;
3529 bfd_boolean common;
3530 unsigned int old_alignment;
3531 bfd *old_bfd;
3533 override = FALSE;
3535 flags = BSF_NO_FLAGS;
3536 sec = NULL;
3537 value = isym->st_value;
3538 *sym_hash = NULL;
3539 common = bed->common_definition (isym);
3541 bind = ELF_ST_BIND (isym->st_info);
3542 if (bind == STB_LOCAL)
3544 /* This should be impossible, since ELF requires that all
3545 global symbols follow all local symbols, and that sh_info
3546 point to the first global symbol. Unfortunately, Irix 5
3547 screws this up. */
3548 continue;
3550 else if (bind == STB_GLOBAL)
3552 if (isym->st_shndx != SHN_UNDEF && !common)
3553 flags = BSF_GLOBAL;
3555 else if (bind == STB_WEAK)
3556 flags = BSF_WEAK;
3557 else
3559 /* Leave it up to the processor backend. */
3562 if (isym->st_shndx == SHN_UNDEF)
3563 sec = bfd_und_section_ptr;
3564 else if (isym->st_shndx < SHN_LORESERVE
3565 || isym->st_shndx > SHN_HIRESERVE)
3567 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3568 if (sec == NULL)
3569 sec = bfd_abs_section_ptr;
3570 else if (sec->kept_section)
3572 /* Symbols from discarded section are undefined, and have
3573 default visibility. */
3574 sec = bfd_und_section_ptr;
3575 isym->st_shndx = SHN_UNDEF;
3576 isym->st_other = (STV_DEFAULT
3577 | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)));
3579 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3580 value -= sec->vma;
3582 else if (isym->st_shndx == SHN_ABS)
3583 sec = bfd_abs_section_ptr;
3584 else if (isym->st_shndx == SHN_COMMON)
3586 sec = bfd_com_section_ptr;
3587 /* What ELF calls the size we call the value. What ELF
3588 calls the value we call the alignment. */
3589 value = isym->st_size;
3591 else
3593 /* Leave it up to the processor backend. */
3596 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3597 isym->st_name);
3598 if (name == NULL)
3599 goto error_free_vers;
3601 if (isym->st_shndx == SHN_COMMON
3602 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3604 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3606 if (tcomm == NULL)
3608 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3609 (SEC_ALLOC
3610 | SEC_IS_COMMON
3611 | SEC_LINKER_CREATED
3612 | SEC_THREAD_LOCAL));
3613 if (tcomm == NULL)
3614 goto error_free_vers;
3616 sec = tcomm;
3618 else if (bed->elf_add_symbol_hook)
3620 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3621 &sec, &value))
3622 goto error_free_vers;
3624 /* The hook function sets the name to NULL if this symbol
3625 should be skipped for some reason. */
3626 if (name == NULL)
3627 continue;
3630 /* Sanity check that all possibilities were handled. */
3631 if (sec == NULL)
3633 bfd_set_error (bfd_error_bad_value);
3634 goto error_free_vers;
3637 if (bfd_is_und_section (sec)
3638 || bfd_is_com_section (sec))
3639 definition = FALSE;
3640 else
3641 definition = TRUE;
3643 size_change_ok = FALSE;
3644 type_change_ok = bed->type_change_ok;
3645 old_alignment = 0;
3646 old_bfd = NULL;
3647 new_sec = sec;
3649 if (is_elf_hash_table (htab))
3651 Elf_Internal_Versym iver;
3652 unsigned int vernum = 0;
3653 bfd_boolean skip;
3655 if (ever == NULL)
3657 if (info->default_imported_symver)
3658 /* Use the default symbol version created earlier. */
3659 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3660 else
3661 iver.vs_vers = 0;
3663 else
3664 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3666 vernum = iver.vs_vers & VERSYM_VERSION;
3668 /* If this is a hidden symbol, or if it is not version
3669 1, we append the version name to the symbol name.
3670 However, we do not modify a non-hidden absolute symbol
3671 if it is not a function, because it might be the version
3672 symbol itself. FIXME: What if it isn't? */
3673 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3674 || (vernum > 1 && (! bfd_is_abs_section (sec)
3675 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3677 const char *verstr;
3678 size_t namelen, verlen, newlen;
3679 char *newname, *p;
3681 if (isym->st_shndx != SHN_UNDEF)
3683 if (vernum > elf_tdata (abfd)->cverdefs)
3684 verstr = NULL;
3685 else if (vernum > 1)
3686 verstr =
3687 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3688 else
3689 verstr = "";
3691 if (verstr == NULL)
3693 (*_bfd_error_handler)
3694 (_("%B: %s: invalid version %u (max %d)"),
3695 abfd, name, vernum,
3696 elf_tdata (abfd)->cverdefs);
3697 bfd_set_error (bfd_error_bad_value);
3698 goto error_free_vers;
3701 else
3703 /* We cannot simply test for the number of
3704 entries in the VERNEED section since the
3705 numbers for the needed versions do not start
3706 at 0. */
3707 Elf_Internal_Verneed *t;
3709 verstr = NULL;
3710 for (t = elf_tdata (abfd)->verref;
3711 t != NULL;
3712 t = t->vn_nextref)
3714 Elf_Internal_Vernaux *a;
3716 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3718 if (a->vna_other == vernum)
3720 verstr = a->vna_nodename;
3721 break;
3724 if (a != NULL)
3725 break;
3727 if (verstr == NULL)
3729 (*_bfd_error_handler)
3730 (_("%B: %s: invalid needed version %d"),
3731 abfd, name, vernum);
3732 bfd_set_error (bfd_error_bad_value);
3733 goto error_free_vers;
3737 namelen = strlen (name);
3738 verlen = strlen (verstr);
3739 newlen = namelen + verlen + 2;
3740 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3741 && isym->st_shndx != SHN_UNDEF)
3742 ++newlen;
3744 newname = bfd_hash_allocate (&htab->root.table, newlen);
3745 if (newname == NULL)
3746 goto error_free_vers;
3747 memcpy (newname, name, namelen);
3748 p = newname + namelen;
3749 *p++ = ELF_VER_CHR;
3750 /* If this is a defined non-hidden version symbol,
3751 we add another @ to the name. This indicates the
3752 default version of the symbol. */
3753 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3754 && isym->st_shndx != SHN_UNDEF)
3755 *p++ = ELF_VER_CHR;
3756 memcpy (p, verstr, verlen + 1);
3758 name = newname;
3761 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3762 &value, &old_alignment,
3763 sym_hash, &skip, &override,
3764 &type_change_ok, &size_change_ok))
3765 goto error_free_vers;
3767 if (skip)
3768 continue;
3770 if (override)
3771 definition = FALSE;
3773 h = *sym_hash;
3774 while (h->root.type == bfd_link_hash_indirect
3775 || h->root.type == bfd_link_hash_warning)
3776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3778 /* Remember the old alignment if this is a common symbol, so
3779 that we don't reduce the alignment later on. We can't
3780 check later, because _bfd_generic_link_add_one_symbol
3781 will set a default for the alignment which we want to
3782 override. We also remember the old bfd where the existing
3783 definition comes from. */
3784 switch (h->root.type)
3786 default:
3787 break;
3789 case bfd_link_hash_defined:
3790 case bfd_link_hash_defweak:
3791 old_bfd = h->root.u.def.section->owner;
3792 break;
3794 case bfd_link_hash_common:
3795 old_bfd = h->root.u.c.p->section->owner;
3796 old_alignment = h->root.u.c.p->alignment_power;
3797 break;
3800 if (elf_tdata (abfd)->verdef != NULL
3801 && ! override
3802 && vernum > 1
3803 && definition)
3804 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3807 if (! (_bfd_generic_link_add_one_symbol
3808 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3809 (struct bfd_link_hash_entry **) sym_hash)))
3810 goto error_free_vers;
3812 h = *sym_hash;
3813 while (h->root.type == bfd_link_hash_indirect
3814 || h->root.type == bfd_link_hash_warning)
3815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3816 *sym_hash = h;
3818 new_weakdef = FALSE;
3819 if (dynamic
3820 && definition
3821 && (flags & BSF_WEAK) != 0
3822 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3823 && is_elf_hash_table (htab)
3824 && h->u.weakdef == NULL)
3826 /* Keep a list of all weak defined non function symbols from
3827 a dynamic object, using the weakdef field. Later in this
3828 function we will set the weakdef field to the correct
3829 value. We only put non-function symbols from dynamic
3830 objects on this list, because that happens to be the only
3831 time we need to know the normal symbol corresponding to a
3832 weak symbol, and the information is time consuming to
3833 figure out. If the weakdef field is not already NULL,
3834 then this symbol was already defined by some previous
3835 dynamic object, and we will be using that previous
3836 definition anyhow. */
3838 h->u.weakdef = weaks;
3839 weaks = h;
3840 new_weakdef = TRUE;
3843 /* Set the alignment of a common symbol. */
3844 if ((common || bfd_is_com_section (sec))
3845 && h->root.type == bfd_link_hash_common)
3847 unsigned int align;
3849 if (common)
3850 align = bfd_log2 (isym->st_value);
3851 else
3853 /* The new symbol is a common symbol in a shared object.
3854 We need to get the alignment from the section. */
3855 align = new_sec->alignment_power;
3857 if (align > old_alignment
3858 /* Permit an alignment power of zero if an alignment of one
3859 is specified and no other alignments have been specified. */
3860 || (isym->st_value == 1 && old_alignment == 0))
3861 h->root.u.c.p->alignment_power = align;
3862 else
3863 h->root.u.c.p->alignment_power = old_alignment;
3866 if (is_elf_hash_table (htab))
3868 bfd_boolean dynsym;
3870 /* Check the alignment when a common symbol is involved. This
3871 can change when a common symbol is overridden by a normal
3872 definition or a common symbol is ignored due to the old
3873 normal definition. We need to make sure the maximum
3874 alignment is maintained. */
3875 if ((old_alignment || common)
3876 && h->root.type != bfd_link_hash_common)
3878 unsigned int common_align;
3879 unsigned int normal_align;
3880 unsigned int symbol_align;
3881 bfd *normal_bfd;
3882 bfd *common_bfd;
3884 symbol_align = ffs (h->root.u.def.value) - 1;
3885 if (h->root.u.def.section->owner != NULL
3886 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3888 normal_align = h->root.u.def.section->alignment_power;
3889 if (normal_align > symbol_align)
3890 normal_align = symbol_align;
3892 else
3893 normal_align = symbol_align;
3895 if (old_alignment)
3897 common_align = old_alignment;
3898 common_bfd = old_bfd;
3899 normal_bfd = abfd;
3901 else
3903 common_align = bfd_log2 (isym->st_value);
3904 common_bfd = abfd;
3905 normal_bfd = old_bfd;
3908 if (normal_align < common_align)
3909 (*_bfd_error_handler)
3910 (_("Warning: alignment %u of symbol `%s' in %B"
3911 " is smaller than %u in %B"),
3912 normal_bfd, common_bfd,
3913 1 << normal_align, name, 1 << common_align);
3916 /* Remember the symbol size and type. */
3917 if (isym->st_size != 0
3918 && (definition || h->size == 0))
3920 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3921 (*_bfd_error_handler)
3922 (_("Warning: size of symbol `%s' changed"
3923 " from %lu in %B to %lu in %B"),
3924 old_bfd, abfd,
3925 name, (unsigned long) h->size,
3926 (unsigned long) isym->st_size);
3928 h->size = isym->st_size;
3931 /* If this is a common symbol, then we always want H->SIZE
3932 to be the size of the common symbol. The code just above
3933 won't fix the size if a common symbol becomes larger. We
3934 don't warn about a size change here, because that is
3935 covered by --warn-common. */
3936 if (h->root.type == bfd_link_hash_common)
3937 h->size = h->root.u.c.size;
3939 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3940 && (definition || h->type == STT_NOTYPE))
3942 if (h->type != STT_NOTYPE
3943 && h->type != ELF_ST_TYPE (isym->st_info)
3944 && ! type_change_ok)
3945 (*_bfd_error_handler)
3946 (_("Warning: type of symbol `%s' changed"
3947 " from %d to %d in %B"),
3948 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3950 h->type = ELF_ST_TYPE (isym->st_info);
3953 /* If st_other has a processor-specific meaning, specific
3954 code might be needed here. We never merge the visibility
3955 attribute with the one from a dynamic object. */
3956 if (bed->elf_backend_merge_symbol_attribute)
3957 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3958 dynamic);
3960 /* If this symbol has default visibility and the user has requested
3961 we not re-export it, then mark it as hidden. */
3962 if (definition && !dynamic
3963 && (abfd->no_export
3964 || (abfd->my_archive && abfd->my_archive->no_export))
3965 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3966 isym->st_other = (STV_HIDDEN
3967 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
3969 if (isym->st_other != 0 && !dynamic)
3971 unsigned char hvis, symvis, other, nvis;
3973 /* Take the balance of OTHER from the definition. */
3974 other = (definition ? isym->st_other : h->other);
3975 other &= ~ ELF_ST_VISIBILITY (-1);
3977 /* Combine visibilities, using the most constraining one. */
3978 hvis = ELF_ST_VISIBILITY (h->other);
3979 symvis = ELF_ST_VISIBILITY (isym->st_other);
3980 if (! hvis)
3981 nvis = symvis;
3982 else if (! symvis)
3983 nvis = hvis;
3984 else
3985 nvis = hvis < symvis ? hvis : symvis;
3987 h->other = other | nvis;
3990 /* Set a flag in the hash table entry indicating the type of
3991 reference or definition we just found. Keep a count of
3992 the number of dynamic symbols we find. A dynamic symbol
3993 is one which is referenced or defined by both a regular
3994 object and a shared object. */
3995 dynsym = FALSE;
3996 if (! dynamic)
3998 if (! definition)
4000 h->ref_regular = 1;
4001 if (bind != STB_WEAK)
4002 h->ref_regular_nonweak = 1;
4004 else
4005 h->def_regular = 1;
4006 if (! info->executable
4007 || h->def_dynamic
4008 || h->ref_dynamic)
4009 dynsym = TRUE;
4011 else
4013 if (! definition)
4014 h->ref_dynamic = 1;
4015 else
4016 h->def_dynamic = 1;
4017 if (h->def_regular
4018 || h->ref_regular
4019 || (h->u.weakdef != NULL
4020 && ! new_weakdef
4021 && h->u.weakdef->dynindx != -1))
4022 dynsym = TRUE;
4025 /* Check to see if we need to add an indirect symbol for
4026 the default name. */
4027 if (definition || h->root.type == bfd_link_hash_common)
4028 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4029 &sec, &value, &dynsym,
4030 override))
4031 goto error_free_vers;
4033 if (definition && !dynamic)
4035 char *p = strchr (name, ELF_VER_CHR);
4036 if (p != NULL && p[1] != ELF_VER_CHR)
4038 /* Queue non-default versions so that .symver x, x@FOO
4039 aliases can be checked. */
4040 if (!nondeflt_vers)
4042 amt = ((isymend - isym + 1)
4043 * sizeof (struct elf_link_hash_entry *));
4044 nondeflt_vers = bfd_malloc (amt);
4046 nondeflt_vers[nondeflt_vers_cnt++] = h;
4050 if (dynsym && h->dynindx == -1)
4052 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4053 goto error_free_vers;
4054 if (h->u.weakdef != NULL
4055 && ! new_weakdef
4056 && h->u.weakdef->dynindx == -1)
4058 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4059 goto error_free_vers;
4062 else if (dynsym && h->dynindx != -1)
4063 /* If the symbol already has a dynamic index, but
4064 visibility says it should not be visible, turn it into
4065 a local symbol. */
4066 switch (ELF_ST_VISIBILITY (h->other))
4068 case STV_INTERNAL:
4069 case STV_HIDDEN:
4070 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4071 dynsym = FALSE;
4072 break;
4075 if (!add_needed
4076 && definition
4077 && dynsym
4078 && h->ref_regular)
4080 int ret;
4081 const char *soname = elf_dt_name (abfd);
4083 /* A symbol from a library loaded via DT_NEEDED of some
4084 other library is referenced by a regular object.
4085 Add a DT_NEEDED entry for it. Issue an error if
4086 --no-add-needed is used. */
4087 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4089 (*_bfd_error_handler)
4090 (_("%s: invalid DSO for symbol `%s' definition"),
4091 abfd, name);
4092 bfd_set_error (bfd_error_bad_value);
4093 goto error_free_vers;
4096 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4098 add_needed = TRUE;
4099 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4100 if (ret < 0)
4101 goto error_free_vers;
4103 BFD_ASSERT (ret == 0);
4108 if (extversym != NULL)
4110 free (extversym);
4111 extversym = NULL;
4114 if (isymbuf != NULL)
4116 free (isymbuf);
4117 isymbuf = NULL;
4120 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4122 unsigned int i;
4124 /* Restore the symbol table. */
4125 old_hash = (char *) old_tab + tabsize;
4126 old_ent = (char *) old_hash + hashsize;
4127 sym_hash = elf_sym_hashes (abfd);
4128 memcpy (htab->root.table.table, old_tab, tabsize);
4129 memcpy (sym_hash, old_hash, hashsize);
4130 htab->root.undefs = old_undefs;
4131 htab->root.undefs_tail = old_undefs_tail;
4132 for (i = 0; i < htab->root.table.size; i++)
4134 struct bfd_hash_entry *p;
4135 struct elf_link_hash_entry *h;
4137 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4139 h = (struct elf_link_hash_entry *) p;
4140 if (h->root.type == bfd_link_hash_warning)
4141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4142 if (h->dynindx >= old_dynsymcount)
4143 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4145 memcpy (p, old_ent, htab->root.table.entsize);
4146 old_ent = (char *) old_ent + htab->root.table.entsize;
4147 h = (struct elf_link_hash_entry *) p;
4148 if (h->root.type == bfd_link_hash_warning)
4150 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4151 old_ent = (char *) old_ent + htab->root.table.entsize;
4156 free (old_tab);
4157 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4158 alloc_mark);
4159 if (nondeflt_vers != NULL)
4160 free (nondeflt_vers);
4161 return TRUE;
4164 if (old_tab != NULL)
4166 free (old_tab);
4167 old_tab = NULL;
4170 /* Now that all the symbols from this input file are created, handle
4171 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4172 if (nondeflt_vers != NULL)
4174 bfd_size_type cnt, symidx;
4176 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4178 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4179 char *shortname, *p;
4181 p = strchr (h->root.root.string, ELF_VER_CHR);
4182 if (p == NULL
4183 || (h->root.type != bfd_link_hash_defined
4184 && h->root.type != bfd_link_hash_defweak))
4185 continue;
4187 amt = p - h->root.root.string;
4188 shortname = bfd_malloc (amt + 1);
4189 memcpy (shortname, h->root.root.string, amt);
4190 shortname[amt] = '\0';
4192 hi = (struct elf_link_hash_entry *)
4193 bfd_link_hash_lookup (&htab->root, shortname,
4194 FALSE, FALSE, FALSE);
4195 if (hi != NULL
4196 && hi->root.type == h->root.type
4197 && hi->root.u.def.value == h->root.u.def.value
4198 && hi->root.u.def.section == h->root.u.def.section)
4200 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4201 hi->root.type = bfd_link_hash_indirect;
4202 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4203 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4204 sym_hash = elf_sym_hashes (abfd);
4205 if (sym_hash)
4206 for (symidx = 0; symidx < extsymcount; ++symidx)
4207 if (sym_hash[symidx] == hi)
4209 sym_hash[symidx] = h;
4210 break;
4213 free (shortname);
4215 free (nondeflt_vers);
4216 nondeflt_vers = NULL;
4219 /* Now set the weakdefs field correctly for all the weak defined
4220 symbols we found. The only way to do this is to search all the
4221 symbols. Since we only need the information for non functions in
4222 dynamic objects, that's the only time we actually put anything on
4223 the list WEAKS. We need this information so that if a regular
4224 object refers to a symbol defined weakly in a dynamic object, the
4225 real symbol in the dynamic object is also put in the dynamic
4226 symbols; we also must arrange for both symbols to point to the
4227 same memory location. We could handle the general case of symbol
4228 aliasing, but a general symbol alias can only be generated in
4229 assembler code, handling it correctly would be very time
4230 consuming, and other ELF linkers don't handle general aliasing
4231 either. */
4232 if (weaks != NULL)
4234 struct elf_link_hash_entry **hpp;
4235 struct elf_link_hash_entry **hppend;
4236 struct elf_link_hash_entry **sorted_sym_hash;
4237 struct elf_link_hash_entry *h;
4238 size_t sym_count;
4240 /* Since we have to search the whole symbol list for each weak
4241 defined symbol, search time for N weak defined symbols will be
4242 O(N^2). Binary search will cut it down to O(NlogN). */
4243 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4244 sorted_sym_hash = bfd_malloc (amt);
4245 if (sorted_sym_hash == NULL)
4246 goto error_return;
4247 sym_hash = sorted_sym_hash;
4248 hpp = elf_sym_hashes (abfd);
4249 hppend = hpp + extsymcount;
4250 sym_count = 0;
4251 for (; hpp < hppend; hpp++)
4253 h = *hpp;
4254 if (h != NULL
4255 && h->root.type == bfd_link_hash_defined
4256 && h->type != STT_FUNC)
4258 *sym_hash = h;
4259 sym_hash++;
4260 sym_count++;
4264 qsort (sorted_sym_hash, sym_count,
4265 sizeof (struct elf_link_hash_entry *),
4266 elf_sort_symbol);
4268 while (weaks != NULL)
4270 struct elf_link_hash_entry *hlook;
4271 asection *slook;
4272 bfd_vma vlook;
4273 long ilook;
4274 size_t i, j, idx;
4276 hlook = weaks;
4277 weaks = hlook->u.weakdef;
4278 hlook->u.weakdef = NULL;
4280 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4281 || hlook->root.type == bfd_link_hash_defweak
4282 || hlook->root.type == bfd_link_hash_common
4283 || hlook->root.type == bfd_link_hash_indirect);
4284 slook = hlook->root.u.def.section;
4285 vlook = hlook->root.u.def.value;
4287 ilook = -1;
4288 i = 0;
4289 j = sym_count;
4290 while (i < j)
4292 bfd_signed_vma vdiff;
4293 idx = (i + j) / 2;
4294 h = sorted_sym_hash [idx];
4295 vdiff = vlook - h->root.u.def.value;
4296 if (vdiff < 0)
4297 j = idx;
4298 else if (vdiff > 0)
4299 i = idx + 1;
4300 else
4302 long sdiff = slook->id - h->root.u.def.section->id;
4303 if (sdiff < 0)
4304 j = idx;
4305 else if (sdiff > 0)
4306 i = idx + 1;
4307 else
4309 ilook = idx;
4310 break;
4315 /* We didn't find a value/section match. */
4316 if (ilook == -1)
4317 continue;
4319 for (i = ilook; i < sym_count; i++)
4321 h = sorted_sym_hash [i];
4323 /* Stop if value or section doesn't match. */
4324 if (h->root.u.def.value != vlook
4325 || h->root.u.def.section != slook)
4326 break;
4327 else if (h != hlook)
4329 hlook->u.weakdef = h;
4331 /* If the weak definition is in the list of dynamic
4332 symbols, make sure the real definition is put
4333 there as well. */
4334 if (hlook->dynindx != -1 && h->dynindx == -1)
4336 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4337 goto error_return;
4340 /* If the real definition is in the list of dynamic
4341 symbols, make sure the weak definition is put
4342 there as well. If we don't do this, then the
4343 dynamic loader might not merge the entries for the
4344 real definition and the weak definition. */
4345 if (h->dynindx != -1 && hlook->dynindx == -1)
4347 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4348 goto error_return;
4350 break;
4355 free (sorted_sym_hash);
4358 if (bed->check_directives)
4359 (*bed->check_directives) (abfd, info);
4361 /* If this object is the same format as the output object, and it is
4362 not a shared library, then let the backend look through the
4363 relocs.
4365 This is required to build global offset table entries and to
4366 arrange for dynamic relocs. It is not required for the
4367 particular common case of linking non PIC code, even when linking
4368 against shared libraries, but unfortunately there is no way of
4369 knowing whether an object file has been compiled PIC or not.
4370 Looking through the relocs is not particularly time consuming.
4371 The problem is that we must either (1) keep the relocs in memory,
4372 which causes the linker to require additional runtime memory or
4373 (2) read the relocs twice from the input file, which wastes time.
4374 This would be a good case for using mmap.
4376 I have no idea how to handle linking PIC code into a file of a
4377 different format. It probably can't be done. */
4378 if (! dynamic
4379 && is_elf_hash_table (htab)
4380 && htab->root.creator == abfd->xvec
4381 && bed->check_relocs != NULL)
4383 asection *o;
4385 for (o = abfd->sections; o != NULL; o = o->next)
4387 Elf_Internal_Rela *internal_relocs;
4388 bfd_boolean ok;
4390 if ((o->flags & SEC_RELOC) == 0
4391 || o->reloc_count == 0
4392 || ((info->strip == strip_all || info->strip == strip_debugger)
4393 && (o->flags & SEC_DEBUGGING) != 0)
4394 || bfd_is_abs_section (o->output_section))
4395 continue;
4397 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4398 info->keep_memory);
4399 if (internal_relocs == NULL)
4400 goto error_return;
4402 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4404 if (elf_section_data (o)->relocs != internal_relocs)
4405 free (internal_relocs);
4407 if (! ok)
4408 goto error_return;
4412 /* If this is a non-traditional link, try to optimize the handling
4413 of the .stab/.stabstr sections. */
4414 if (! dynamic
4415 && ! info->traditional_format
4416 && is_elf_hash_table (htab)
4417 && (info->strip != strip_all && info->strip != strip_debugger))
4419 asection *stabstr;
4421 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4422 if (stabstr != NULL)
4424 bfd_size_type string_offset = 0;
4425 asection *stab;
4427 for (stab = abfd->sections; stab; stab = stab->next)
4428 if (strncmp (".stab", stab->name, 5) == 0
4429 && (!stab->name[5] ||
4430 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4431 && (stab->flags & SEC_MERGE) == 0
4432 && !bfd_is_abs_section (stab->output_section))
4434 struct bfd_elf_section_data *secdata;
4436 secdata = elf_section_data (stab);
4437 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4438 stabstr, &secdata->sec_info,
4439 &string_offset))
4440 goto error_return;
4441 if (secdata->sec_info)
4442 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4447 if (is_elf_hash_table (htab) && add_needed)
4449 /* Add this bfd to the loaded list. */
4450 struct elf_link_loaded_list *n;
4452 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4453 if (n == NULL)
4454 goto error_return;
4455 n->abfd = abfd;
4456 n->next = htab->loaded;
4457 htab->loaded = n;
4460 return TRUE;
4462 error_free_vers:
4463 if (old_tab != NULL)
4464 free (old_tab);
4465 if (nondeflt_vers != NULL)
4466 free (nondeflt_vers);
4467 if (extversym != NULL)
4468 free (extversym);
4469 error_free_sym:
4470 if (isymbuf != NULL)
4471 free (isymbuf);
4472 error_return:
4473 return FALSE;
4476 /* Return the linker hash table entry of a symbol that might be
4477 satisfied by an archive symbol. Return -1 on error. */
4479 struct elf_link_hash_entry *
4480 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4481 struct bfd_link_info *info,
4482 const char *name)
4484 struct elf_link_hash_entry *h;
4485 char *p, *copy;
4486 size_t len, first;
4488 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4489 if (h != NULL)
4490 return h;
4492 /* If this is a default version (the name contains @@), look up the
4493 symbol again with only one `@' as well as without the version.
4494 The effect is that references to the symbol with and without the
4495 version will be matched by the default symbol in the archive. */
4497 p = strchr (name, ELF_VER_CHR);
4498 if (p == NULL || p[1] != ELF_VER_CHR)
4499 return h;
4501 /* First check with only one `@'. */
4502 len = strlen (name);
4503 copy = bfd_alloc (abfd, len);
4504 if (copy == NULL)
4505 return (struct elf_link_hash_entry *) 0 - 1;
4507 first = p - name + 1;
4508 memcpy (copy, name, first);
4509 memcpy (copy + first, name + first + 1, len - first);
4511 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4512 if (h == NULL)
4514 /* We also need to check references to the symbol without the
4515 version. */
4516 copy[first - 1] = '\0';
4517 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4518 FALSE, FALSE, FALSE);
4521 bfd_release (abfd, copy);
4522 return h;
4525 /* Add symbols from an ELF archive file to the linker hash table. We
4526 don't use _bfd_generic_link_add_archive_symbols because of a
4527 problem which arises on UnixWare. The UnixWare libc.so is an
4528 archive which includes an entry libc.so.1 which defines a bunch of
4529 symbols. The libc.so archive also includes a number of other
4530 object files, which also define symbols, some of which are the same
4531 as those defined in libc.so.1. Correct linking requires that we
4532 consider each object file in turn, and include it if it defines any
4533 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4534 this; it looks through the list of undefined symbols, and includes
4535 any object file which defines them. When this algorithm is used on
4536 UnixWare, it winds up pulling in libc.so.1 early and defining a
4537 bunch of symbols. This means that some of the other objects in the
4538 archive are not included in the link, which is incorrect since they
4539 precede libc.so.1 in the archive.
4541 Fortunately, ELF archive handling is simpler than that done by
4542 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4543 oddities. In ELF, if we find a symbol in the archive map, and the
4544 symbol is currently undefined, we know that we must pull in that
4545 object file.
4547 Unfortunately, we do have to make multiple passes over the symbol
4548 table until nothing further is resolved. */
4550 static bfd_boolean
4551 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4553 symindex c;
4554 bfd_boolean *defined = NULL;
4555 bfd_boolean *included = NULL;
4556 carsym *symdefs;
4557 bfd_boolean loop;
4558 bfd_size_type amt;
4559 const struct elf_backend_data *bed;
4560 struct elf_link_hash_entry * (*archive_symbol_lookup)
4561 (bfd *, struct bfd_link_info *, const char *);
4563 if (! bfd_has_map (abfd))
4565 /* An empty archive is a special case. */
4566 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4567 return TRUE;
4568 bfd_set_error (bfd_error_no_armap);
4569 return FALSE;
4572 /* Keep track of all symbols we know to be already defined, and all
4573 files we know to be already included. This is to speed up the
4574 second and subsequent passes. */
4575 c = bfd_ardata (abfd)->symdef_count;
4576 if (c == 0)
4577 return TRUE;
4578 amt = c;
4579 amt *= sizeof (bfd_boolean);
4580 defined = bfd_zmalloc (amt);
4581 included = bfd_zmalloc (amt);
4582 if (defined == NULL || included == NULL)
4583 goto error_return;
4585 symdefs = bfd_ardata (abfd)->symdefs;
4586 bed = get_elf_backend_data (abfd);
4587 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4591 file_ptr last;
4592 symindex i;
4593 carsym *symdef;
4594 carsym *symdefend;
4596 loop = FALSE;
4597 last = -1;
4599 symdef = symdefs;
4600 symdefend = symdef + c;
4601 for (i = 0; symdef < symdefend; symdef++, i++)
4603 struct elf_link_hash_entry *h;
4604 bfd *element;
4605 struct bfd_link_hash_entry *undefs_tail;
4606 symindex mark;
4608 if (defined[i] || included[i])
4609 continue;
4610 if (symdef->file_offset == last)
4612 included[i] = TRUE;
4613 continue;
4616 h = archive_symbol_lookup (abfd, info, symdef->name);
4617 if (h == (struct elf_link_hash_entry *) 0 - 1)
4618 goto error_return;
4620 if (h == NULL)
4621 continue;
4623 if (h->root.type == bfd_link_hash_common)
4625 /* We currently have a common symbol. The archive map contains
4626 a reference to this symbol, so we may want to include it. We
4627 only want to include it however, if this archive element
4628 contains a definition of the symbol, not just another common
4629 declaration of it.
4631 Unfortunately some archivers (including GNU ar) will put
4632 declarations of common symbols into their archive maps, as
4633 well as real definitions, so we cannot just go by the archive
4634 map alone. Instead we must read in the element's symbol
4635 table and check that to see what kind of symbol definition
4636 this is. */
4637 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4638 continue;
4640 else if (h->root.type != bfd_link_hash_undefined)
4642 if (h->root.type != bfd_link_hash_undefweak)
4643 defined[i] = TRUE;
4644 continue;
4647 /* We need to include this archive member. */
4648 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4649 if (element == NULL)
4650 goto error_return;
4652 if (! bfd_check_format (element, bfd_object))
4653 goto error_return;
4655 /* Doublecheck that we have not included this object
4656 already--it should be impossible, but there may be
4657 something wrong with the archive. */
4658 if (element->archive_pass != 0)
4660 bfd_set_error (bfd_error_bad_value);
4661 goto error_return;
4663 element->archive_pass = 1;
4665 undefs_tail = info->hash->undefs_tail;
4667 if (! (*info->callbacks->add_archive_element) (info, element,
4668 symdef->name))
4669 goto error_return;
4670 if (! bfd_link_add_symbols (element, info))
4671 goto error_return;
4673 /* If there are any new undefined symbols, we need to make
4674 another pass through the archive in order to see whether
4675 they can be defined. FIXME: This isn't perfect, because
4676 common symbols wind up on undefs_tail and because an
4677 undefined symbol which is defined later on in this pass
4678 does not require another pass. This isn't a bug, but it
4679 does make the code less efficient than it could be. */
4680 if (undefs_tail != info->hash->undefs_tail)
4681 loop = TRUE;
4683 /* Look backward to mark all symbols from this object file
4684 which we have already seen in this pass. */
4685 mark = i;
4688 included[mark] = TRUE;
4689 if (mark == 0)
4690 break;
4691 --mark;
4693 while (symdefs[mark].file_offset == symdef->file_offset);
4695 /* We mark subsequent symbols from this object file as we go
4696 on through the loop. */
4697 last = symdef->file_offset;
4700 while (loop);
4702 free (defined);
4703 free (included);
4705 return TRUE;
4707 error_return:
4708 if (defined != NULL)
4709 free (defined);
4710 if (included != NULL)
4711 free (included);
4712 return FALSE;
4715 /* Given an ELF BFD, add symbols to the global hash table as
4716 appropriate. */
4718 bfd_boolean
4719 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4721 switch (bfd_get_format (abfd))
4723 case bfd_object:
4724 return elf_link_add_object_symbols (abfd, info);
4725 case bfd_archive:
4726 return elf_link_add_archive_symbols (abfd, info);
4727 default:
4728 bfd_set_error (bfd_error_wrong_format);
4729 return FALSE;
4733 /* This function will be called though elf_link_hash_traverse to store
4734 all hash value of the exported symbols in an array. */
4736 static bfd_boolean
4737 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4739 unsigned long **valuep = data;
4740 const char *name;
4741 char *p;
4742 unsigned long ha;
4743 char *alc = NULL;
4745 if (h->root.type == bfd_link_hash_warning)
4746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4748 /* Ignore indirect symbols. These are added by the versioning code. */
4749 if (h->dynindx == -1)
4750 return TRUE;
4752 name = h->root.root.string;
4753 p = strchr (name, ELF_VER_CHR);
4754 if (p != NULL)
4756 alc = bfd_malloc (p - name + 1);
4757 memcpy (alc, name, p - name);
4758 alc[p - name] = '\0';
4759 name = alc;
4762 /* Compute the hash value. */
4763 ha = bfd_elf_hash (name);
4765 /* Store the found hash value in the array given as the argument. */
4766 *(*valuep)++ = ha;
4768 /* And store it in the struct so that we can put it in the hash table
4769 later. */
4770 h->u.elf_hash_value = ha;
4772 if (alc != NULL)
4773 free (alc);
4775 return TRUE;
4778 /* Array used to determine the number of hash table buckets to use
4779 based on the number of symbols there are. If there are fewer than
4780 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4781 fewer than 37 we use 17 buckets, and so forth. We never use more
4782 than 32771 buckets. */
4784 static const size_t elf_buckets[] =
4786 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4787 16411, 32771, 0
4790 /* Compute bucket count for hashing table. We do not use a static set
4791 of possible tables sizes anymore. Instead we determine for all
4792 possible reasonable sizes of the table the outcome (i.e., the
4793 number of collisions etc) and choose the best solution. The
4794 weighting functions are not too simple to allow the table to grow
4795 without bounds. Instead one of the weighting factors is the size.
4796 Therefore the result is always a good payoff between few collisions
4797 (= short chain lengths) and table size. */
4798 static size_t
4799 compute_bucket_count (struct bfd_link_info *info)
4801 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4802 size_t best_size = 0;
4803 unsigned long int *hashcodes;
4804 unsigned long int *hashcodesp;
4805 unsigned long int i;
4806 bfd_size_type amt;
4808 /* Compute the hash values for all exported symbols. At the same
4809 time store the values in an array so that we could use them for
4810 optimizations. */
4811 amt = dynsymcount;
4812 amt *= sizeof (unsigned long int);
4813 hashcodes = bfd_malloc (amt);
4814 if (hashcodes == NULL)
4815 return 0;
4816 hashcodesp = hashcodes;
4818 /* Put all hash values in HASHCODES. */
4819 elf_link_hash_traverse (elf_hash_table (info),
4820 elf_collect_hash_codes, &hashcodesp);
4822 /* We have a problem here. The following code to optimize the table
4823 size requires an integer type with more the 32 bits. If
4824 BFD_HOST_U_64_BIT is set we know about such a type. */
4825 #ifdef BFD_HOST_U_64_BIT
4826 if (info->optimize)
4828 unsigned long int nsyms = hashcodesp - hashcodes;
4829 size_t minsize;
4830 size_t maxsize;
4831 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4832 unsigned long int *counts ;
4833 bfd *dynobj = elf_hash_table (info)->dynobj;
4834 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4836 /* Possible optimization parameters: if we have NSYMS symbols we say
4837 that the hashing table must at least have NSYMS/4 and at most
4838 2*NSYMS buckets. */
4839 minsize = nsyms / 4;
4840 if (minsize == 0)
4841 minsize = 1;
4842 best_size = maxsize = nsyms * 2;
4844 /* Create array where we count the collisions in. We must use bfd_malloc
4845 since the size could be large. */
4846 amt = maxsize;
4847 amt *= sizeof (unsigned long int);
4848 counts = bfd_malloc (amt);
4849 if (counts == NULL)
4851 free (hashcodes);
4852 return 0;
4855 /* Compute the "optimal" size for the hash table. The criteria is a
4856 minimal chain length. The minor criteria is (of course) the size
4857 of the table. */
4858 for (i = minsize; i < maxsize; ++i)
4860 /* Walk through the array of hashcodes and count the collisions. */
4861 BFD_HOST_U_64_BIT max;
4862 unsigned long int j;
4863 unsigned long int fact;
4865 memset (counts, '\0', i * sizeof (unsigned long int));
4867 /* Determine how often each hash bucket is used. */
4868 for (j = 0; j < nsyms; ++j)
4869 ++counts[hashcodes[j] % i];
4871 /* For the weight function we need some information about the
4872 pagesize on the target. This is information need not be 100%
4873 accurate. Since this information is not available (so far) we
4874 define it here to a reasonable default value. If it is crucial
4875 to have a better value some day simply define this value. */
4876 # ifndef BFD_TARGET_PAGESIZE
4877 # define BFD_TARGET_PAGESIZE (4096)
4878 # endif
4880 /* We in any case need 2 + NSYMS entries for the size values and
4881 the chains. */
4882 max = (2 + nsyms) * (bed->s->arch_size / 8);
4884 # if 1
4885 /* Variant 1: optimize for short chains. We add the squares
4886 of all the chain lengths (which favors many small chain
4887 over a few long chains). */
4888 for (j = 0; j < i; ++j)
4889 max += counts[j] * counts[j];
4891 /* This adds penalties for the overall size of the table. */
4892 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4893 max *= fact * fact;
4894 # else
4895 /* Variant 2: Optimize a lot more for small table. Here we
4896 also add squares of the size but we also add penalties for
4897 empty slots (the +1 term). */
4898 for (j = 0; j < i; ++j)
4899 max += (1 + counts[j]) * (1 + counts[j]);
4901 /* The overall size of the table is considered, but not as
4902 strong as in variant 1, where it is squared. */
4903 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4904 max *= fact;
4905 # endif
4907 /* Compare with current best results. */
4908 if (max < best_chlen)
4910 best_chlen = max;
4911 best_size = i;
4915 free (counts);
4917 else
4918 #endif /* defined (BFD_HOST_U_64_BIT) */
4920 /* This is the fallback solution if no 64bit type is available or if we
4921 are not supposed to spend much time on optimizations. We select the
4922 bucket count using a fixed set of numbers. */
4923 for (i = 0; elf_buckets[i] != 0; i++)
4925 best_size = elf_buckets[i];
4926 if (dynsymcount < elf_buckets[i + 1])
4927 break;
4931 /* Free the arrays we needed. */
4932 free (hashcodes);
4934 return best_size;
4937 /* Set up the sizes and contents of the ELF dynamic sections. This is
4938 called by the ELF linker emulation before_allocation routine. We
4939 must set the sizes of the sections before the linker sets the
4940 addresses of the various sections. */
4942 bfd_boolean
4943 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4944 const char *soname,
4945 const char *rpath,
4946 const char *filter_shlib,
4947 const char * const *auxiliary_filters,
4948 struct bfd_link_info *info,
4949 asection **sinterpptr,
4950 struct bfd_elf_version_tree *verdefs)
4952 bfd_size_type soname_indx;
4953 bfd *dynobj;
4954 const struct elf_backend_data *bed;
4955 struct elf_assign_sym_version_info asvinfo;
4957 *sinterpptr = NULL;
4959 soname_indx = (bfd_size_type) -1;
4961 if (!is_elf_hash_table (info->hash))
4962 return TRUE;
4964 elf_tdata (output_bfd)->relro = info->relro;
4965 if (info->execstack)
4966 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4967 else if (info->noexecstack)
4968 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4969 else
4971 bfd *inputobj;
4972 asection *notesec = NULL;
4973 int exec = 0;
4975 for (inputobj = info->input_bfds;
4976 inputobj;
4977 inputobj = inputobj->link_next)
4979 asection *s;
4981 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4982 continue;
4983 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4984 if (s)
4986 if (s->flags & SEC_CODE)
4987 exec = PF_X;
4988 notesec = s;
4990 else
4991 exec = PF_X;
4993 if (notesec)
4995 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4996 if (exec && info->relocatable
4997 && notesec->output_section != bfd_abs_section_ptr)
4998 notesec->output_section->flags |= SEC_CODE;
5002 /* Any syms created from now on start with -1 in
5003 got.refcount/offset and plt.refcount/offset. */
5004 elf_hash_table (info)->init_got_refcount
5005 = elf_hash_table (info)->init_got_offset;
5006 elf_hash_table (info)->init_plt_refcount
5007 = elf_hash_table (info)->init_plt_offset;
5009 /* The backend may have to create some sections regardless of whether
5010 we're dynamic or not. */
5011 bed = get_elf_backend_data (output_bfd);
5012 if (bed->elf_backend_always_size_sections
5013 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5014 return FALSE;
5016 dynobj = elf_hash_table (info)->dynobj;
5018 /* If there were no dynamic objects in the link, there is nothing to
5019 do here. */
5020 if (dynobj == NULL)
5021 return TRUE;
5023 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5024 return FALSE;
5026 if (elf_hash_table (info)->dynamic_sections_created)
5028 struct elf_info_failed eif;
5029 struct elf_link_hash_entry *h;
5030 asection *dynstr;
5031 struct bfd_elf_version_tree *t;
5032 struct bfd_elf_version_expr *d;
5033 asection *s;
5034 bfd_boolean all_defined;
5036 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5037 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5039 if (soname != NULL)
5041 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5042 soname, TRUE);
5043 if (soname_indx == (bfd_size_type) -1
5044 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5045 return FALSE;
5048 if (info->symbolic)
5050 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5051 return FALSE;
5052 info->flags |= DF_SYMBOLIC;
5055 if (rpath != NULL)
5057 bfd_size_type indx;
5059 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5060 TRUE);
5061 if (indx == (bfd_size_type) -1
5062 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5063 return FALSE;
5065 if (info->new_dtags)
5067 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5068 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5069 return FALSE;
5073 if (filter_shlib != NULL)
5075 bfd_size_type indx;
5077 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5078 filter_shlib, TRUE);
5079 if (indx == (bfd_size_type) -1
5080 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5081 return FALSE;
5084 if (auxiliary_filters != NULL)
5086 const char * const *p;
5088 for (p = auxiliary_filters; *p != NULL; p++)
5090 bfd_size_type indx;
5092 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5093 *p, TRUE);
5094 if (indx == (bfd_size_type) -1
5095 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5096 return FALSE;
5100 eif.info = info;
5101 eif.verdefs = verdefs;
5102 eif.failed = FALSE;
5104 /* If we are supposed to export all symbols into the dynamic symbol
5105 table (this is not the normal case), then do so. */
5106 if (info->export_dynamic)
5108 elf_link_hash_traverse (elf_hash_table (info),
5109 _bfd_elf_export_symbol,
5110 &eif);
5111 if (eif.failed)
5112 return FALSE;
5115 /* Make all global versions with definition. */
5116 for (t = verdefs; t != NULL; t = t->next)
5117 for (d = t->globals.list; d != NULL; d = d->next)
5118 if (!d->symver && d->symbol)
5120 const char *verstr, *name;
5121 size_t namelen, verlen, newlen;
5122 char *newname, *p;
5123 struct elf_link_hash_entry *newh;
5125 name = d->symbol;
5126 namelen = strlen (name);
5127 verstr = t->name;
5128 verlen = strlen (verstr);
5129 newlen = namelen + verlen + 3;
5131 newname = bfd_malloc (newlen);
5132 if (newname == NULL)
5133 return FALSE;
5134 memcpy (newname, name, namelen);
5136 /* Check the hidden versioned definition. */
5137 p = newname + namelen;
5138 *p++ = ELF_VER_CHR;
5139 memcpy (p, verstr, verlen + 1);
5140 newh = elf_link_hash_lookup (elf_hash_table (info),
5141 newname, FALSE, FALSE,
5142 FALSE);
5143 if (newh == NULL
5144 || (newh->root.type != bfd_link_hash_defined
5145 && newh->root.type != bfd_link_hash_defweak))
5147 /* Check the default versioned definition. */
5148 *p++ = ELF_VER_CHR;
5149 memcpy (p, verstr, verlen + 1);
5150 newh = elf_link_hash_lookup (elf_hash_table (info),
5151 newname, FALSE, FALSE,
5152 FALSE);
5154 free (newname);
5156 /* Mark this version if there is a definition and it is
5157 not defined in a shared object. */
5158 if (newh != NULL
5159 && !newh->def_dynamic
5160 && (newh->root.type == bfd_link_hash_defined
5161 || newh->root.type == bfd_link_hash_defweak))
5162 d->symver = 1;
5165 /* Attach all the symbols to their version information. */
5166 asvinfo.output_bfd = output_bfd;
5167 asvinfo.info = info;
5168 asvinfo.verdefs = verdefs;
5169 asvinfo.failed = FALSE;
5171 elf_link_hash_traverse (elf_hash_table (info),
5172 _bfd_elf_link_assign_sym_version,
5173 &asvinfo);
5174 if (asvinfo.failed)
5175 return FALSE;
5177 if (!info->allow_undefined_version)
5179 /* Check if all global versions have a definition. */
5180 all_defined = TRUE;
5181 for (t = verdefs; t != NULL; t = t->next)
5182 for (d = t->globals.list; d != NULL; d = d->next)
5183 if (!d->symver && !d->script)
5185 (*_bfd_error_handler)
5186 (_("%s: undefined version: %s"),
5187 d->pattern, t->name);
5188 all_defined = FALSE;
5191 if (!all_defined)
5193 bfd_set_error (bfd_error_bad_value);
5194 return FALSE;
5198 /* Find all symbols which were defined in a dynamic object and make
5199 the backend pick a reasonable value for them. */
5200 elf_link_hash_traverse (elf_hash_table (info),
5201 _bfd_elf_adjust_dynamic_symbol,
5202 &eif);
5203 if (eif.failed)
5204 return FALSE;
5206 /* Add some entries to the .dynamic section. We fill in some of the
5207 values later, in bfd_elf_final_link, but we must add the entries
5208 now so that we know the final size of the .dynamic section. */
5210 /* If there are initialization and/or finalization functions to
5211 call then add the corresponding DT_INIT/DT_FINI entries. */
5212 h = (info->init_function
5213 ? elf_link_hash_lookup (elf_hash_table (info),
5214 info->init_function, FALSE,
5215 FALSE, FALSE)
5216 : NULL);
5217 if (h != NULL
5218 && (h->ref_regular
5219 || h->def_regular))
5221 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5222 return FALSE;
5224 h = (info->fini_function
5225 ? elf_link_hash_lookup (elf_hash_table (info),
5226 info->fini_function, FALSE,
5227 FALSE, FALSE)
5228 : NULL);
5229 if (h != NULL
5230 && (h->ref_regular
5231 || h->def_regular))
5233 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5234 return FALSE;
5237 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5238 if (s != NULL && s->linker_has_input)
5240 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5241 if (! info->executable)
5243 bfd *sub;
5244 asection *o;
5246 for (sub = info->input_bfds; sub != NULL;
5247 sub = sub->link_next)
5248 for (o = sub->sections; o != NULL; o = o->next)
5249 if (elf_section_data (o)->this_hdr.sh_type
5250 == SHT_PREINIT_ARRAY)
5252 (*_bfd_error_handler)
5253 (_("%B: .preinit_array section is not allowed in DSO"),
5254 sub);
5255 break;
5258 bfd_set_error (bfd_error_nonrepresentable_section);
5259 return FALSE;
5262 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5263 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5264 return FALSE;
5266 s = bfd_get_section_by_name (output_bfd, ".init_array");
5267 if (s != NULL && s->linker_has_input)
5269 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5270 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5271 return FALSE;
5273 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5274 if (s != NULL && s->linker_has_input)
5276 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5277 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5278 return FALSE;
5281 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5282 /* If .dynstr is excluded from the link, we don't want any of
5283 these tags. Strictly, we should be checking each section
5284 individually; This quick check covers for the case where
5285 someone does a /DISCARD/ : { *(*) }. */
5286 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5288 bfd_size_type strsize;
5290 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5291 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5292 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5293 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5294 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5295 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5296 bed->s->sizeof_sym))
5297 return FALSE;
5301 /* The backend must work out the sizes of all the other dynamic
5302 sections. */
5303 if (bed->elf_backend_size_dynamic_sections
5304 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5305 return FALSE;
5307 if (elf_hash_table (info)->dynamic_sections_created)
5309 unsigned long section_sym_count;
5310 asection *s;
5312 /* Set up the version definition section. */
5313 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5314 BFD_ASSERT (s != NULL);
5316 /* We may have created additional version definitions if we are
5317 just linking a regular application. */
5318 verdefs = asvinfo.verdefs;
5320 /* Skip anonymous version tag. */
5321 if (verdefs != NULL && verdefs->vernum == 0)
5322 verdefs = verdefs->next;
5324 if (verdefs == NULL && !info->create_default_symver)
5325 s->flags |= SEC_EXCLUDE;
5326 else
5328 unsigned int cdefs;
5329 bfd_size_type size;
5330 struct bfd_elf_version_tree *t;
5331 bfd_byte *p;
5332 Elf_Internal_Verdef def;
5333 Elf_Internal_Verdaux defaux;
5334 struct bfd_link_hash_entry *bh;
5335 struct elf_link_hash_entry *h;
5336 const char *name;
5338 cdefs = 0;
5339 size = 0;
5341 /* Make space for the base version. */
5342 size += sizeof (Elf_External_Verdef);
5343 size += sizeof (Elf_External_Verdaux);
5344 ++cdefs;
5346 /* Make space for the default version. */
5347 if (info->create_default_symver)
5349 size += sizeof (Elf_External_Verdef);
5350 ++cdefs;
5353 for (t = verdefs; t != NULL; t = t->next)
5355 struct bfd_elf_version_deps *n;
5357 size += sizeof (Elf_External_Verdef);
5358 size += sizeof (Elf_External_Verdaux);
5359 ++cdefs;
5361 for (n = t->deps; n != NULL; n = n->next)
5362 size += sizeof (Elf_External_Verdaux);
5365 s->size = size;
5366 s->contents = bfd_alloc (output_bfd, s->size);
5367 if (s->contents == NULL && s->size != 0)
5368 return FALSE;
5370 /* Fill in the version definition section. */
5372 p = s->contents;
5374 def.vd_version = VER_DEF_CURRENT;
5375 def.vd_flags = VER_FLG_BASE;
5376 def.vd_ndx = 1;
5377 def.vd_cnt = 1;
5378 if (info->create_default_symver)
5380 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5381 def.vd_next = sizeof (Elf_External_Verdef);
5383 else
5385 def.vd_aux = sizeof (Elf_External_Verdef);
5386 def.vd_next = (sizeof (Elf_External_Verdef)
5387 + sizeof (Elf_External_Verdaux));
5390 if (soname_indx != (bfd_size_type) -1)
5392 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5393 soname_indx);
5394 def.vd_hash = bfd_elf_hash (soname);
5395 defaux.vda_name = soname_indx;
5396 name = soname;
5398 else
5400 bfd_size_type indx;
5402 name = lbasename (output_bfd->filename);
5403 def.vd_hash = bfd_elf_hash (name);
5404 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5405 name, FALSE);
5406 if (indx == (bfd_size_type) -1)
5407 return FALSE;
5408 defaux.vda_name = indx;
5410 defaux.vda_next = 0;
5412 _bfd_elf_swap_verdef_out (output_bfd, &def,
5413 (Elf_External_Verdef *) p);
5414 p += sizeof (Elf_External_Verdef);
5415 if (info->create_default_symver)
5417 /* Add a symbol representing this version. */
5418 bh = NULL;
5419 if (! (_bfd_generic_link_add_one_symbol
5420 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5421 0, NULL, FALSE,
5422 get_elf_backend_data (dynobj)->collect, &bh)))
5423 return FALSE;
5424 h = (struct elf_link_hash_entry *) bh;
5425 h->non_elf = 0;
5426 h->def_regular = 1;
5427 h->type = STT_OBJECT;
5428 h->verinfo.vertree = NULL;
5430 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5431 return FALSE;
5433 /* Create a duplicate of the base version with the same
5434 aux block, but different flags. */
5435 def.vd_flags = 0;
5436 def.vd_ndx = 2;
5437 def.vd_aux = sizeof (Elf_External_Verdef);
5438 if (verdefs)
5439 def.vd_next = (sizeof (Elf_External_Verdef)
5440 + sizeof (Elf_External_Verdaux));
5441 else
5442 def.vd_next = 0;
5443 _bfd_elf_swap_verdef_out (output_bfd, &def,
5444 (Elf_External_Verdef *) p);
5445 p += sizeof (Elf_External_Verdef);
5447 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5448 (Elf_External_Verdaux *) p);
5449 p += sizeof (Elf_External_Verdaux);
5451 for (t = verdefs; t != NULL; t = t->next)
5453 unsigned int cdeps;
5454 struct bfd_elf_version_deps *n;
5456 cdeps = 0;
5457 for (n = t->deps; n != NULL; n = n->next)
5458 ++cdeps;
5460 /* Add a symbol representing this version. */
5461 bh = NULL;
5462 if (! (_bfd_generic_link_add_one_symbol
5463 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5464 0, NULL, FALSE,
5465 get_elf_backend_data (dynobj)->collect, &bh)))
5466 return FALSE;
5467 h = (struct elf_link_hash_entry *) bh;
5468 h->non_elf = 0;
5469 h->def_regular = 1;
5470 h->type = STT_OBJECT;
5471 h->verinfo.vertree = t;
5473 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5474 return FALSE;
5476 def.vd_version = VER_DEF_CURRENT;
5477 def.vd_flags = 0;
5478 if (t->globals.list == NULL
5479 && t->locals.list == NULL
5480 && ! t->used)
5481 def.vd_flags |= VER_FLG_WEAK;
5482 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5483 def.vd_cnt = cdeps + 1;
5484 def.vd_hash = bfd_elf_hash (t->name);
5485 def.vd_aux = sizeof (Elf_External_Verdef);
5486 def.vd_next = 0;
5487 if (t->next != NULL)
5488 def.vd_next = (sizeof (Elf_External_Verdef)
5489 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5491 _bfd_elf_swap_verdef_out (output_bfd, &def,
5492 (Elf_External_Verdef *) p);
5493 p += sizeof (Elf_External_Verdef);
5495 defaux.vda_name = h->dynstr_index;
5496 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5497 h->dynstr_index);
5498 defaux.vda_next = 0;
5499 if (t->deps != NULL)
5500 defaux.vda_next = sizeof (Elf_External_Verdaux);
5501 t->name_indx = defaux.vda_name;
5503 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5504 (Elf_External_Verdaux *) p);
5505 p += sizeof (Elf_External_Verdaux);
5507 for (n = t->deps; n != NULL; n = n->next)
5509 if (n->version_needed == NULL)
5511 /* This can happen if there was an error in the
5512 version script. */
5513 defaux.vda_name = 0;
5515 else
5517 defaux.vda_name = n->version_needed->name_indx;
5518 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5519 defaux.vda_name);
5521 if (n->next == NULL)
5522 defaux.vda_next = 0;
5523 else
5524 defaux.vda_next = sizeof (Elf_External_Verdaux);
5526 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5527 (Elf_External_Verdaux *) p);
5528 p += sizeof (Elf_External_Verdaux);
5532 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5533 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5534 return FALSE;
5536 elf_tdata (output_bfd)->cverdefs = cdefs;
5539 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5541 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5542 return FALSE;
5544 else if (info->flags & DF_BIND_NOW)
5546 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5547 return FALSE;
5550 if (info->flags_1)
5552 if (info->executable)
5553 info->flags_1 &= ~ (DF_1_INITFIRST
5554 | DF_1_NODELETE
5555 | DF_1_NOOPEN);
5556 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5557 return FALSE;
5560 /* Work out the size of the version reference section. */
5562 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5563 BFD_ASSERT (s != NULL);
5565 struct elf_find_verdep_info sinfo;
5567 sinfo.output_bfd = output_bfd;
5568 sinfo.info = info;
5569 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5570 if (sinfo.vers == 0)
5571 sinfo.vers = 1;
5572 sinfo.failed = FALSE;
5574 elf_link_hash_traverse (elf_hash_table (info),
5575 _bfd_elf_link_find_version_dependencies,
5576 &sinfo);
5578 if (elf_tdata (output_bfd)->verref == NULL)
5579 s->flags |= SEC_EXCLUDE;
5580 else
5582 Elf_Internal_Verneed *t;
5583 unsigned int size;
5584 unsigned int crefs;
5585 bfd_byte *p;
5587 /* Build the version definition section. */
5588 size = 0;
5589 crefs = 0;
5590 for (t = elf_tdata (output_bfd)->verref;
5591 t != NULL;
5592 t = t->vn_nextref)
5594 Elf_Internal_Vernaux *a;
5596 size += sizeof (Elf_External_Verneed);
5597 ++crefs;
5598 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5599 size += sizeof (Elf_External_Vernaux);
5602 s->size = size;
5603 s->contents = bfd_alloc (output_bfd, s->size);
5604 if (s->contents == NULL)
5605 return FALSE;
5607 p = s->contents;
5608 for (t = elf_tdata (output_bfd)->verref;
5609 t != NULL;
5610 t = t->vn_nextref)
5612 unsigned int caux;
5613 Elf_Internal_Vernaux *a;
5614 bfd_size_type indx;
5616 caux = 0;
5617 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5618 ++caux;
5620 t->vn_version = VER_NEED_CURRENT;
5621 t->vn_cnt = caux;
5622 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5623 elf_dt_name (t->vn_bfd) != NULL
5624 ? elf_dt_name (t->vn_bfd)
5625 : lbasename (t->vn_bfd->filename),
5626 FALSE);
5627 if (indx == (bfd_size_type) -1)
5628 return FALSE;
5629 t->vn_file = indx;
5630 t->vn_aux = sizeof (Elf_External_Verneed);
5631 if (t->vn_nextref == NULL)
5632 t->vn_next = 0;
5633 else
5634 t->vn_next = (sizeof (Elf_External_Verneed)
5635 + caux * sizeof (Elf_External_Vernaux));
5637 _bfd_elf_swap_verneed_out (output_bfd, t,
5638 (Elf_External_Verneed *) p);
5639 p += sizeof (Elf_External_Verneed);
5641 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5643 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5644 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5645 a->vna_nodename, FALSE);
5646 if (indx == (bfd_size_type) -1)
5647 return FALSE;
5648 a->vna_name = indx;
5649 if (a->vna_nextptr == NULL)
5650 a->vna_next = 0;
5651 else
5652 a->vna_next = sizeof (Elf_External_Vernaux);
5654 _bfd_elf_swap_vernaux_out (output_bfd, a,
5655 (Elf_External_Vernaux *) p);
5656 p += sizeof (Elf_External_Vernaux);
5660 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5661 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5662 return FALSE;
5664 elf_tdata (output_bfd)->cverrefs = crefs;
5668 if ((elf_tdata (output_bfd)->cverrefs == 0
5669 && elf_tdata (output_bfd)->cverdefs == 0)
5670 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5671 &section_sym_count) == 0)
5673 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5674 s->flags |= SEC_EXCLUDE;
5677 return TRUE;
5680 bfd_boolean
5681 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5683 if (!is_elf_hash_table (info->hash))
5684 return TRUE;
5686 if (elf_hash_table (info)->dynamic_sections_created)
5688 bfd *dynobj;
5689 const struct elf_backend_data *bed;
5690 asection *s;
5691 bfd_size_type dynsymcount;
5692 unsigned long section_sym_count;
5693 size_t bucketcount = 0;
5694 size_t hash_entry_size;
5695 unsigned int dtagcount;
5697 dynobj = elf_hash_table (info)->dynobj;
5699 /* Assign dynsym indicies. In a shared library we generate a
5700 section symbol for each output section, which come first.
5701 Next come all of the back-end allocated local dynamic syms,
5702 followed by the rest of the global symbols. */
5704 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5705 &section_sym_count);
5707 /* Work out the size of the symbol version section. */
5708 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5709 BFD_ASSERT (s != NULL);
5710 if (dynsymcount != 0
5711 && (s->flags & SEC_EXCLUDE) == 0)
5713 s->size = dynsymcount * sizeof (Elf_External_Versym);
5714 s->contents = bfd_zalloc (output_bfd, s->size);
5715 if (s->contents == NULL)
5716 return FALSE;
5718 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5719 return FALSE;
5722 /* Set the size of the .dynsym and .hash sections. We counted
5723 the number of dynamic symbols in elf_link_add_object_symbols.
5724 We will build the contents of .dynsym and .hash when we build
5725 the final symbol table, because until then we do not know the
5726 correct value to give the symbols. We built the .dynstr
5727 section as we went along in elf_link_add_object_symbols. */
5728 s = bfd_get_section_by_name (dynobj, ".dynsym");
5729 BFD_ASSERT (s != NULL);
5730 bed = get_elf_backend_data (output_bfd);
5731 s->size = dynsymcount * bed->s->sizeof_sym;
5733 if (dynsymcount != 0)
5735 s->contents = bfd_alloc (output_bfd, s->size);
5736 if (s->contents == NULL)
5737 return FALSE;
5739 /* The first entry in .dynsym is a dummy symbol.
5740 Clear all the section syms, in case we don't output them all. */
5741 ++section_sym_count;
5742 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5745 /* Compute the size of the hashing table. As a side effect this
5746 computes the hash values for all the names we export. */
5747 bucketcount = compute_bucket_count (info);
5749 s = bfd_get_section_by_name (dynobj, ".hash");
5750 BFD_ASSERT (s != NULL);
5751 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5752 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5753 s->contents = bfd_zalloc (output_bfd, s->size);
5754 if (s->contents == NULL)
5755 return FALSE;
5757 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5758 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5759 s->contents + hash_entry_size);
5761 elf_hash_table (info)->bucketcount = bucketcount;
5763 s = bfd_get_section_by_name (dynobj, ".dynstr");
5764 BFD_ASSERT (s != NULL);
5766 elf_finalize_dynstr (output_bfd, info);
5768 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5770 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5771 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5772 return FALSE;
5775 return TRUE;
5778 /* Final phase of ELF linker. */
5780 /* A structure we use to avoid passing large numbers of arguments. */
5782 struct elf_final_link_info
5784 /* General link information. */
5785 struct bfd_link_info *info;
5786 /* Output BFD. */
5787 bfd *output_bfd;
5788 /* Symbol string table. */
5789 struct bfd_strtab_hash *symstrtab;
5790 /* .dynsym section. */
5791 asection *dynsym_sec;
5792 /* .hash section. */
5793 asection *hash_sec;
5794 /* symbol version section (.gnu.version). */
5795 asection *symver_sec;
5796 /* Buffer large enough to hold contents of any section. */
5797 bfd_byte *contents;
5798 /* Buffer large enough to hold external relocs of any section. */
5799 void *external_relocs;
5800 /* Buffer large enough to hold internal relocs of any section. */
5801 Elf_Internal_Rela *internal_relocs;
5802 /* Buffer large enough to hold external local symbols of any input
5803 BFD. */
5804 bfd_byte *external_syms;
5805 /* And a buffer for symbol section indices. */
5806 Elf_External_Sym_Shndx *locsym_shndx;
5807 /* Buffer large enough to hold internal local symbols of any input
5808 BFD. */
5809 Elf_Internal_Sym *internal_syms;
5810 /* Array large enough to hold a symbol index for each local symbol
5811 of any input BFD. */
5812 long *indices;
5813 /* Array large enough to hold a section pointer for each local
5814 symbol of any input BFD. */
5815 asection **sections;
5816 /* Buffer to hold swapped out symbols. */
5817 bfd_byte *symbuf;
5818 /* And one for symbol section indices. */
5819 Elf_External_Sym_Shndx *symshndxbuf;
5820 /* Number of swapped out symbols in buffer. */
5821 size_t symbuf_count;
5822 /* Number of symbols which fit in symbuf. */
5823 size_t symbuf_size;
5824 /* And same for symshndxbuf. */
5825 size_t shndxbuf_size;
5828 /* This struct is used to pass information to elf_link_output_extsym. */
5830 struct elf_outext_info
5832 bfd_boolean failed;
5833 bfd_boolean localsyms;
5834 struct elf_final_link_info *finfo;
5837 /* When performing a relocatable link, the input relocations are
5838 preserved. But, if they reference global symbols, the indices
5839 referenced must be updated. Update all the relocations in
5840 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5842 static void
5843 elf_link_adjust_relocs (bfd *abfd,
5844 Elf_Internal_Shdr *rel_hdr,
5845 unsigned int count,
5846 struct elf_link_hash_entry **rel_hash)
5848 unsigned int i;
5849 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5850 bfd_byte *erela;
5851 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5852 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5853 bfd_vma r_type_mask;
5854 int r_sym_shift;
5856 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5858 swap_in = bed->s->swap_reloc_in;
5859 swap_out = bed->s->swap_reloc_out;
5861 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5863 swap_in = bed->s->swap_reloca_in;
5864 swap_out = bed->s->swap_reloca_out;
5866 else
5867 abort ();
5869 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5870 abort ();
5872 if (bed->s->arch_size == 32)
5874 r_type_mask = 0xff;
5875 r_sym_shift = 8;
5877 else
5879 r_type_mask = 0xffffffff;
5880 r_sym_shift = 32;
5883 erela = rel_hdr->contents;
5884 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5886 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5887 unsigned int j;
5889 if (*rel_hash == NULL)
5890 continue;
5892 BFD_ASSERT ((*rel_hash)->indx >= 0);
5894 (*swap_in) (abfd, erela, irela);
5895 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5896 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5897 | (irela[j].r_info & r_type_mask));
5898 (*swap_out) (abfd, irela, erela);
5902 struct elf_link_sort_rela
5904 union {
5905 bfd_vma offset;
5906 bfd_vma sym_mask;
5907 } u;
5908 enum elf_reloc_type_class type;
5909 /* We use this as an array of size int_rels_per_ext_rel. */
5910 Elf_Internal_Rela rela[1];
5913 static int
5914 elf_link_sort_cmp1 (const void *A, const void *B)
5916 const struct elf_link_sort_rela *a = A;
5917 const struct elf_link_sort_rela *b = B;
5918 int relativea, relativeb;
5920 relativea = a->type == reloc_class_relative;
5921 relativeb = b->type == reloc_class_relative;
5923 if (relativea < relativeb)
5924 return 1;
5925 if (relativea > relativeb)
5926 return -1;
5927 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5928 return -1;
5929 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5930 return 1;
5931 if (a->rela->r_offset < b->rela->r_offset)
5932 return -1;
5933 if (a->rela->r_offset > b->rela->r_offset)
5934 return 1;
5935 return 0;
5938 static int
5939 elf_link_sort_cmp2 (const void *A, const void *B)
5941 const struct elf_link_sort_rela *a = A;
5942 const struct elf_link_sort_rela *b = B;
5943 int copya, copyb;
5945 if (a->u.offset < b->u.offset)
5946 return -1;
5947 if (a->u.offset > b->u.offset)
5948 return 1;
5949 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5950 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5951 if (copya < copyb)
5952 return -1;
5953 if (copya > copyb)
5954 return 1;
5955 if (a->rela->r_offset < b->rela->r_offset)
5956 return -1;
5957 if (a->rela->r_offset > b->rela->r_offset)
5958 return 1;
5959 return 0;
5962 static size_t
5963 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5965 asection *reldyn;
5966 bfd_size_type count, size;
5967 size_t i, ret, sort_elt, ext_size;
5968 bfd_byte *sort, *s_non_relative, *p;
5969 struct elf_link_sort_rela *sq;
5970 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5971 int i2e = bed->s->int_rels_per_ext_rel;
5972 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5973 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5974 struct bfd_link_order *lo;
5975 bfd_vma r_sym_mask;
5977 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5978 if (reldyn == NULL || reldyn->size == 0)
5980 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5981 if (reldyn == NULL || reldyn->size == 0)
5982 return 0;
5983 ext_size = bed->s->sizeof_rel;
5984 swap_in = bed->s->swap_reloc_in;
5985 swap_out = bed->s->swap_reloc_out;
5987 else
5989 ext_size = bed->s->sizeof_rela;
5990 swap_in = bed->s->swap_reloca_in;
5991 swap_out = bed->s->swap_reloca_out;
5993 count = reldyn->size / ext_size;
5995 size = 0;
5996 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5997 if (lo->type == bfd_indirect_link_order)
5999 asection *o = lo->u.indirect.section;
6000 size += o->size;
6003 if (size != reldyn->size)
6004 return 0;
6006 sort_elt = (sizeof (struct elf_link_sort_rela)
6007 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6008 sort = bfd_zmalloc (sort_elt * count);
6009 if (sort == NULL)
6011 (*info->callbacks->warning)
6012 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6013 return 0;
6016 if (bed->s->arch_size == 32)
6017 r_sym_mask = ~(bfd_vma) 0xff;
6018 else
6019 r_sym_mask = ~(bfd_vma) 0xffffffff;
6021 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6022 if (lo->type == bfd_indirect_link_order)
6024 bfd_byte *erel, *erelend;
6025 asection *o = lo->u.indirect.section;
6027 if (o->contents == NULL && o->size != 0)
6029 /* This is a reloc section that is being handled as a normal
6030 section. See bfd_section_from_shdr. We can't combine
6031 relocs in this case. */
6032 free (sort);
6033 return 0;
6035 erel = o->contents;
6036 erelend = o->contents + o->size;
6037 p = sort + o->output_offset / ext_size * sort_elt;
6038 while (erel < erelend)
6040 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6041 (*swap_in) (abfd, erel, s->rela);
6042 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6043 s->u.sym_mask = r_sym_mask;
6044 p += sort_elt;
6045 erel += ext_size;
6049 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6051 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6053 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6054 if (s->type != reloc_class_relative)
6055 break;
6057 ret = i;
6058 s_non_relative = p;
6060 sq = (struct elf_link_sort_rela *) s_non_relative;
6061 for (; i < count; i++, p += sort_elt)
6063 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6064 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6065 sq = sp;
6066 sp->u.offset = sq->rela->r_offset;
6069 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6071 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6072 if (lo->type == bfd_indirect_link_order)
6074 bfd_byte *erel, *erelend;
6075 asection *o = lo->u.indirect.section;
6077 erel = o->contents;
6078 erelend = o->contents + o->size;
6079 p = sort + o->output_offset / ext_size * sort_elt;
6080 while (erel < erelend)
6082 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6083 (*swap_out) (abfd, s->rela, erel);
6084 p += sort_elt;
6085 erel += ext_size;
6089 free (sort);
6090 *psec = reldyn;
6091 return ret;
6094 /* Flush the output symbols to the file. */
6096 static bfd_boolean
6097 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6098 const struct elf_backend_data *bed)
6100 if (finfo->symbuf_count > 0)
6102 Elf_Internal_Shdr *hdr;
6103 file_ptr pos;
6104 bfd_size_type amt;
6106 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6107 pos = hdr->sh_offset + hdr->sh_size;
6108 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6109 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6110 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6111 return FALSE;
6113 hdr->sh_size += amt;
6114 finfo->symbuf_count = 0;
6117 return TRUE;
6120 /* Add a symbol to the output symbol table. */
6122 static bfd_boolean
6123 elf_link_output_sym (struct elf_final_link_info *finfo,
6124 const char *name,
6125 Elf_Internal_Sym *elfsym,
6126 asection *input_sec,
6127 struct elf_link_hash_entry *h)
6129 bfd_byte *dest;
6130 Elf_External_Sym_Shndx *destshndx;
6131 bfd_boolean (*output_symbol_hook)
6132 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6133 struct elf_link_hash_entry *);
6134 const struct elf_backend_data *bed;
6136 bed = get_elf_backend_data (finfo->output_bfd);
6137 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6138 if (output_symbol_hook != NULL)
6140 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6141 return FALSE;
6144 if (name == NULL || *name == '\0')
6145 elfsym->st_name = 0;
6146 else if (input_sec->flags & SEC_EXCLUDE)
6147 elfsym->st_name = 0;
6148 else
6150 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6151 name, TRUE, FALSE);
6152 if (elfsym->st_name == (unsigned long) -1)
6153 return FALSE;
6156 if (finfo->symbuf_count >= finfo->symbuf_size)
6158 if (! elf_link_flush_output_syms (finfo, bed))
6159 return FALSE;
6162 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6163 destshndx = finfo->symshndxbuf;
6164 if (destshndx != NULL)
6166 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6168 bfd_size_type amt;
6170 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6171 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6172 if (destshndx == NULL)
6173 return FALSE;
6174 memset ((char *) destshndx + amt, 0, amt);
6175 finfo->shndxbuf_size *= 2;
6177 destshndx += bfd_get_symcount (finfo->output_bfd);
6180 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6181 finfo->symbuf_count += 1;
6182 bfd_get_symcount (finfo->output_bfd) += 1;
6184 return TRUE;
6187 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6188 allowing an unsatisfied unversioned symbol in the DSO to match a
6189 versioned symbol that would normally require an explicit version.
6190 We also handle the case that a DSO references a hidden symbol
6191 which may be satisfied by a versioned symbol in another DSO. */
6193 static bfd_boolean
6194 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6195 const struct elf_backend_data *bed,
6196 struct elf_link_hash_entry *h)
6198 bfd *abfd;
6199 struct elf_link_loaded_list *loaded;
6201 if (!is_elf_hash_table (info->hash))
6202 return FALSE;
6204 switch (h->root.type)
6206 default:
6207 abfd = NULL;
6208 break;
6210 case bfd_link_hash_undefined:
6211 case bfd_link_hash_undefweak:
6212 abfd = h->root.u.undef.abfd;
6213 if ((abfd->flags & DYNAMIC) == 0
6214 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6215 return FALSE;
6216 break;
6218 case bfd_link_hash_defined:
6219 case bfd_link_hash_defweak:
6220 abfd = h->root.u.def.section->owner;
6221 break;
6223 case bfd_link_hash_common:
6224 abfd = h->root.u.c.p->section->owner;
6225 break;
6227 BFD_ASSERT (abfd != NULL);
6229 for (loaded = elf_hash_table (info)->loaded;
6230 loaded != NULL;
6231 loaded = loaded->next)
6233 bfd *input;
6234 Elf_Internal_Shdr *hdr;
6235 bfd_size_type symcount;
6236 bfd_size_type extsymcount;
6237 bfd_size_type extsymoff;
6238 Elf_Internal_Shdr *versymhdr;
6239 Elf_Internal_Sym *isym;
6240 Elf_Internal_Sym *isymend;
6241 Elf_Internal_Sym *isymbuf;
6242 Elf_External_Versym *ever;
6243 Elf_External_Versym *extversym;
6245 input = loaded->abfd;
6247 /* We check each DSO for a possible hidden versioned definition. */
6248 if (input == abfd
6249 || (input->flags & DYNAMIC) == 0
6250 || elf_dynversym (input) == 0)
6251 continue;
6253 hdr = &elf_tdata (input)->dynsymtab_hdr;
6255 symcount = hdr->sh_size / bed->s->sizeof_sym;
6256 if (elf_bad_symtab (input))
6258 extsymcount = symcount;
6259 extsymoff = 0;
6261 else
6263 extsymcount = symcount - hdr->sh_info;
6264 extsymoff = hdr->sh_info;
6267 if (extsymcount == 0)
6268 continue;
6270 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6271 NULL, NULL, NULL);
6272 if (isymbuf == NULL)
6273 return FALSE;
6275 /* Read in any version definitions. */
6276 versymhdr = &elf_tdata (input)->dynversym_hdr;
6277 extversym = bfd_malloc (versymhdr->sh_size);
6278 if (extversym == NULL)
6279 goto error_ret;
6281 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6282 || (bfd_bread (extversym, versymhdr->sh_size, input)
6283 != versymhdr->sh_size))
6285 free (extversym);
6286 error_ret:
6287 free (isymbuf);
6288 return FALSE;
6291 ever = extversym + extsymoff;
6292 isymend = isymbuf + extsymcount;
6293 for (isym = isymbuf; isym < isymend; isym++, ever++)
6295 const char *name;
6296 Elf_Internal_Versym iver;
6297 unsigned short version_index;
6299 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6300 || isym->st_shndx == SHN_UNDEF)
6301 continue;
6303 name = bfd_elf_string_from_elf_section (input,
6304 hdr->sh_link,
6305 isym->st_name);
6306 if (strcmp (name, h->root.root.string) != 0)
6307 continue;
6309 _bfd_elf_swap_versym_in (input, ever, &iver);
6311 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6313 /* If we have a non-hidden versioned sym, then it should
6314 have provided a definition for the undefined sym. */
6315 abort ();
6318 version_index = iver.vs_vers & VERSYM_VERSION;
6319 if (version_index == 1 || version_index == 2)
6321 /* This is the base or first version. We can use it. */
6322 free (extversym);
6323 free (isymbuf);
6324 return TRUE;
6328 free (extversym);
6329 free (isymbuf);
6332 return FALSE;
6335 /* Add an external symbol to the symbol table. This is called from
6336 the hash table traversal routine. When generating a shared object,
6337 we go through the symbol table twice. The first time we output
6338 anything that might have been forced to local scope in a version
6339 script. The second time we output the symbols that are still
6340 global symbols. */
6342 static bfd_boolean
6343 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6345 struct elf_outext_info *eoinfo = data;
6346 struct elf_final_link_info *finfo = eoinfo->finfo;
6347 bfd_boolean strip;
6348 Elf_Internal_Sym sym;
6349 asection *input_sec;
6350 const struct elf_backend_data *bed;
6352 if (h->root.type == bfd_link_hash_warning)
6354 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6355 if (h->root.type == bfd_link_hash_new)
6356 return TRUE;
6359 /* Decide whether to output this symbol in this pass. */
6360 if (eoinfo->localsyms)
6362 if (!h->forced_local)
6363 return TRUE;
6365 else
6367 if (h->forced_local)
6368 return TRUE;
6371 bed = get_elf_backend_data (finfo->output_bfd);
6373 if (h->root.type == bfd_link_hash_undefined)
6375 /* If we have an undefined symbol reference here then it must have
6376 come from a shared library that is being linked in. (Undefined
6377 references in regular files have already been handled). */
6378 bfd_boolean ignore_undef = FALSE;
6380 /* Some symbols may be special in that the fact that they're
6381 undefined can be safely ignored - let backend determine that. */
6382 if (bed->elf_backend_ignore_undef_symbol)
6383 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6385 /* If we are reporting errors for this situation then do so now. */
6386 if (ignore_undef == FALSE
6387 && h->ref_dynamic
6388 && ! h->ref_regular
6389 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6390 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6392 if (! (finfo->info->callbacks->undefined_symbol
6393 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6394 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6396 eoinfo->failed = TRUE;
6397 return FALSE;
6402 /* We should also warn if a forced local symbol is referenced from
6403 shared libraries. */
6404 if (! finfo->info->relocatable
6405 && (! finfo->info->shared)
6406 && h->forced_local
6407 && h->ref_dynamic
6408 && !h->dynamic_def
6409 && !h->dynamic_weak
6410 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6412 (*_bfd_error_handler)
6413 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6414 finfo->output_bfd,
6415 h->root.u.def.section == bfd_abs_section_ptr
6416 ? finfo->output_bfd : h->root.u.def.section->owner,
6417 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6418 ? "internal"
6419 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6420 ? "hidden" : "local",
6421 h->root.root.string);
6422 eoinfo->failed = TRUE;
6423 return FALSE;
6426 /* We don't want to output symbols that have never been mentioned by
6427 a regular file, or that we have been told to strip. However, if
6428 h->indx is set to -2, the symbol is used by a reloc and we must
6429 output it. */
6430 if (h->indx == -2)
6431 strip = FALSE;
6432 else if ((h->def_dynamic
6433 || h->ref_dynamic
6434 || h->root.type == bfd_link_hash_new)
6435 && !h->def_regular
6436 && !h->ref_regular)
6437 strip = TRUE;
6438 else if (finfo->info->strip == strip_all)
6439 strip = TRUE;
6440 else if (finfo->info->strip == strip_some
6441 && bfd_hash_lookup (finfo->info->keep_hash,
6442 h->root.root.string, FALSE, FALSE) == NULL)
6443 strip = TRUE;
6444 else if (finfo->info->strip_discarded
6445 && (h->root.type == bfd_link_hash_defined
6446 || h->root.type == bfd_link_hash_defweak)
6447 && elf_discarded_section (h->root.u.def.section))
6448 strip = TRUE;
6449 else
6450 strip = FALSE;
6452 /* If we're stripping it, and it's not a dynamic symbol, there's
6453 nothing else to do unless it is a forced local symbol. */
6454 if (strip
6455 && h->dynindx == -1
6456 && !h->forced_local)
6457 return TRUE;
6459 sym.st_value = 0;
6460 sym.st_size = h->size;
6461 sym.st_other = h->other;
6462 if (h->forced_local)
6463 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6464 else if (h->root.type == bfd_link_hash_undefweak
6465 || h->root.type == bfd_link_hash_defweak)
6466 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6467 else
6468 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6470 switch (h->root.type)
6472 default:
6473 case bfd_link_hash_new:
6474 case bfd_link_hash_warning:
6475 abort ();
6476 return FALSE;
6478 case bfd_link_hash_undefined:
6479 case bfd_link_hash_undefweak:
6480 input_sec = bfd_und_section_ptr;
6481 sym.st_shndx = SHN_UNDEF;
6482 break;
6484 case bfd_link_hash_defined:
6485 case bfd_link_hash_defweak:
6487 input_sec = h->root.u.def.section;
6488 if (input_sec->output_section != NULL)
6490 sym.st_shndx =
6491 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6492 input_sec->output_section);
6493 if (sym.st_shndx == SHN_BAD)
6495 (*_bfd_error_handler)
6496 (_("%B: could not find output section %A for input section %A"),
6497 finfo->output_bfd, input_sec->output_section, input_sec);
6498 eoinfo->failed = TRUE;
6499 return FALSE;
6502 /* ELF symbols in relocatable files are section relative,
6503 but in nonrelocatable files they are virtual
6504 addresses. */
6505 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6506 if (! finfo->info->relocatable)
6508 sym.st_value += input_sec->output_section->vma;
6509 if (h->type == STT_TLS)
6511 /* STT_TLS symbols are relative to PT_TLS segment
6512 base. */
6513 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6514 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6518 else
6520 BFD_ASSERT (input_sec->owner == NULL
6521 || (input_sec->owner->flags & DYNAMIC) != 0);
6522 sym.st_shndx = SHN_UNDEF;
6523 input_sec = bfd_und_section_ptr;
6526 break;
6528 case bfd_link_hash_common:
6529 input_sec = h->root.u.c.p->section;
6530 sym.st_shndx = bed->common_section_index (input_sec);
6531 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6532 break;
6534 case bfd_link_hash_indirect:
6535 /* These symbols are created by symbol versioning. They point
6536 to the decorated version of the name. For example, if the
6537 symbol foo@@GNU_1.2 is the default, which should be used when
6538 foo is used with no version, then we add an indirect symbol
6539 foo which points to foo@@GNU_1.2. We ignore these symbols,
6540 since the indirected symbol is already in the hash table. */
6541 return TRUE;
6544 /* Give the processor backend a chance to tweak the symbol value,
6545 and also to finish up anything that needs to be done for this
6546 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6547 forced local syms when non-shared is due to a historical quirk. */
6548 if ((h->dynindx != -1
6549 || h->forced_local)
6550 && ((finfo->info->shared
6551 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6552 || h->root.type != bfd_link_hash_undefweak))
6553 || !h->forced_local)
6554 && elf_hash_table (finfo->info)->dynamic_sections_created)
6556 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6557 (finfo->output_bfd, finfo->info, h, &sym)))
6559 eoinfo->failed = TRUE;
6560 return FALSE;
6564 /* If we are marking the symbol as undefined, and there are no
6565 non-weak references to this symbol from a regular object, then
6566 mark the symbol as weak undefined; if there are non-weak
6567 references, mark the symbol as strong. We can't do this earlier,
6568 because it might not be marked as undefined until the
6569 finish_dynamic_symbol routine gets through with it. */
6570 if (sym.st_shndx == SHN_UNDEF
6571 && h->ref_regular
6572 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6573 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6575 int bindtype;
6577 if (h->ref_regular_nonweak)
6578 bindtype = STB_GLOBAL;
6579 else
6580 bindtype = STB_WEAK;
6581 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6584 /* If a non-weak symbol with non-default visibility is not defined
6585 locally, it is a fatal error. */
6586 if (! finfo->info->relocatable
6587 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6588 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6589 && h->root.type == bfd_link_hash_undefined
6590 && !h->def_regular)
6592 (*_bfd_error_handler)
6593 (_("%B: %s symbol `%s' isn't defined"),
6594 finfo->output_bfd,
6595 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6596 ? "protected"
6597 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6598 ? "internal" : "hidden",
6599 h->root.root.string);
6600 eoinfo->failed = TRUE;
6601 return FALSE;
6604 /* If this symbol should be put in the .dynsym section, then put it
6605 there now. We already know the symbol index. We also fill in
6606 the entry in the .hash section. */
6607 if (h->dynindx != -1
6608 && elf_hash_table (finfo->info)->dynamic_sections_created)
6610 size_t bucketcount;
6611 size_t bucket;
6612 size_t hash_entry_size;
6613 bfd_byte *bucketpos;
6614 bfd_vma chain;
6615 bfd_byte *esym;
6617 sym.st_name = h->dynstr_index;
6618 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6619 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6621 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6622 bucket = h->u.elf_hash_value % bucketcount;
6623 hash_entry_size
6624 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6625 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6626 + (bucket + 2) * hash_entry_size);
6627 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6628 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6629 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6630 ((bfd_byte *) finfo->hash_sec->contents
6631 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6633 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6635 Elf_Internal_Versym iversym;
6636 Elf_External_Versym *eversym;
6638 if (!h->def_regular)
6640 if (h->verinfo.verdef == NULL)
6641 iversym.vs_vers = 0;
6642 else
6643 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6645 else
6647 if (h->verinfo.vertree == NULL)
6648 iversym.vs_vers = 1;
6649 else
6650 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6651 if (finfo->info->create_default_symver)
6652 iversym.vs_vers++;
6655 if (h->hidden)
6656 iversym.vs_vers |= VERSYM_HIDDEN;
6658 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6659 eversym += h->dynindx;
6660 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6664 /* If we're stripping it, then it was just a dynamic symbol, and
6665 there's nothing else to do. */
6666 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6667 return TRUE;
6669 h->indx = bfd_get_symcount (finfo->output_bfd);
6671 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6673 eoinfo->failed = TRUE;
6674 return FALSE;
6677 return TRUE;
6680 /* Return TRUE if special handling is done for relocs in SEC against
6681 symbols defined in discarded sections. */
6683 static bfd_boolean
6684 elf_section_ignore_discarded_relocs (asection *sec)
6686 const struct elf_backend_data *bed;
6688 switch (sec->sec_info_type)
6690 case ELF_INFO_TYPE_STABS:
6691 case ELF_INFO_TYPE_EH_FRAME:
6692 return TRUE;
6693 default:
6694 break;
6697 bed = get_elf_backend_data (sec->owner);
6698 if (bed->elf_backend_ignore_discarded_relocs != NULL
6699 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6700 return TRUE;
6702 return FALSE;
6705 /* Return a mask saying how ld should treat relocations in SEC against
6706 symbols defined in discarded sections. If this function returns
6707 COMPLAIN set, ld will issue a warning message. If this function
6708 returns PRETEND set, and the discarded section was link-once and the
6709 same size as the kept link-once section, ld will pretend that the
6710 symbol was actually defined in the kept section. Otherwise ld will
6711 zero the reloc (at least that is the intent, but some cooperation by
6712 the target dependent code is needed, particularly for REL targets). */
6714 unsigned int
6715 _bfd_elf_default_action_discarded (asection *sec)
6717 if (sec->flags & SEC_DEBUGGING)
6718 return 0;
6720 if (strcmp (".eh_frame", sec->name) == 0)
6721 return 0;
6723 if (strcmp (".gcc_except_table", sec->name) == 0)
6724 return 0;
6726 return COMPLAIN | PRETEND;
6729 /* Find a match between a section and a member of a section group. */
6731 static asection *
6732 match_group_member (asection *sec, asection *group)
6734 asection *first = elf_next_in_group (group);
6735 asection *s = first;
6737 while (s != NULL)
6739 if (bfd_elf_match_symbols_in_sections (s, sec))
6740 return s;
6742 if (s == first)
6743 break;
6746 return NULL;
6749 /* Check if the kept section of a discarded section SEC can be used
6750 to replace it. Return the replacement if it is OK. Otherwise return
6751 NULL. */
6753 asection *
6754 _bfd_elf_check_kept_section (asection *sec)
6756 asection *kept;
6758 kept = sec->kept_section;
6759 if (kept != NULL)
6761 if (elf_sec_group (sec) != NULL)
6762 kept = match_group_member (sec, kept);
6763 if (kept != NULL && sec->size != kept->size)
6764 kept = NULL;
6766 return kept;
6769 /* Link an input file into the linker output file. This function
6770 handles all the sections and relocations of the input file at once.
6771 This is so that we only have to read the local symbols once, and
6772 don't have to keep them in memory. */
6774 static bfd_boolean
6775 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6777 bfd_boolean (*relocate_section)
6778 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6779 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6780 bfd *output_bfd;
6781 Elf_Internal_Shdr *symtab_hdr;
6782 size_t locsymcount;
6783 size_t extsymoff;
6784 Elf_Internal_Sym *isymbuf;
6785 Elf_Internal_Sym *isym;
6786 Elf_Internal_Sym *isymend;
6787 long *pindex;
6788 asection **ppsection;
6789 asection *o;
6790 const struct elf_backend_data *bed;
6791 bfd_boolean emit_relocs;
6792 struct elf_link_hash_entry **sym_hashes;
6794 output_bfd = finfo->output_bfd;
6795 bed = get_elf_backend_data (output_bfd);
6796 relocate_section = bed->elf_backend_relocate_section;
6798 /* If this is a dynamic object, we don't want to do anything here:
6799 we don't want the local symbols, and we don't want the section
6800 contents. */
6801 if ((input_bfd->flags & DYNAMIC) != 0)
6802 return TRUE;
6804 emit_relocs = (finfo->info->relocatable
6805 || finfo->info->emitrelocations);
6807 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6808 if (elf_bad_symtab (input_bfd))
6810 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6811 extsymoff = 0;
6813 else
6815 locsymcount = symtab_hdr->sh_info;
6816 extsymoff = symtab_hdr->sh_info;
6819 /* Read the local symbols. */
6820 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6821 if (isymbuf == NULL && locsymcount != 0)
6823 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6824 finfo->internal_syms,
6825 finfo->external_syms,
6826 finfo->locsym_shndx);
6827 if (isymbuf == NULL)
6828 return FALSE;
6831 /* Find local symbol sections and adjust values of symbols in
6832 SEC_MERGE sections. Write out those local symbols we know are
6833 going into the output file. */
6834 isymend = isymbuf + locsymcount;
6835 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6836 isym < isymend;
6837 isym++, pindex++, ppsection++)
6839 asection *isec;
6840 const char *name;
6841 Elf_Internal_Sym osym;
6843 *pindex = -1;
6845 if (elf_bad_symtab (input_bfd))
6847 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6849 *ppsection = NULL;
6850 continue;
6854 if (isym->st_shndx == SHN_UNDEF)
6855 isec = bfd_und_section_ptr;
6856 else if (isym->st_shndx < SHN_LORESERVE
6857 || isym->st_shndx > SHN_HIRESERVE)
6859 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6860 if (isec
6861 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6862 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6863 isym->st_value =
6864 _bfd_merged_section_offset (output_bfd, &isec,
6865 elf_section_data (isec)->sec_info,
6866 isym->st_value);
6868 else if (isym->st_shndx == SHN_ABS)
6869 isec = bfd_abs_section_ptr;
6870 else if (isym->st_shndx == SHN_COMMON)
6871 isec = bfd_com_section_ptr;
6872 else
6874 /* Don't attempt to output symbols with st_shnx in the
6875 reserved range other than SHN_ABS and SHN_COMMON. */
6876 *ppsection = NULL;
6877 continue;
6880 *ppsection = isec;
6882 /* Don't output the first, undefined, symbol. */
6883 if (ppsection == finfo->sections)
6884 continue;
6886 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6888 /* We never output section symbols. Instead, we use the
6889 section symbol of the corresponding section in the output
6890 file. */
6891 continue;
6894 /* If we are stripping all symbols, we don't want to output this
6895 one. */
6896 if (finfo->info->strip == strip_all)
6897 continue;
6899 /* If we are discarding all local symbols, we don't want to
6900 output this one. If we are generating a relocatable output
6901 file, then some of the local symbols may be required by
6902 relocs; we output them below as we discover that they are
6903 needed. */
6904 if (finfo->info->discard == discard_all)
6905 continue;
6907 /* If this symbol is defined in a section which we are
6908 discarding, we don't need to keep it. */
6909 if (isym->st_shndx != SHN_UNDEF
6910 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6911 && (isec == NULL
6912 || bfd_section_removed_from_list (output_bfd,
6913 isec->output_section)))
6914 continue;
6916 /* Get the name of the symbol. */
6917 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6918 isym->st_name);
6919 if (name == NULL)
6920 return FALSE;
6922 /* See if we are discarding symbols with this name. */
6923 if ((finfo->info->strip == strip_some
6924 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6925 == NULL))
6926 || (((finfo->info->discard == discard_sec_merge
6927 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6928 || finfo->info->discard == discard_l)
6929 && bfd_is_local_label_name (input_bfd, name)))
6930 continue;
6932 /* If we get here, we are going to output this symbol. */
6934 osym = *isym;
6936 /* Adjust the section index for the output file. */
6937 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6938 isec->output_section);
6939 if (osym.st_shndx == SHN_BAD)
6940 return FALSE;
6942 *pindex = bfd_get_symcount (output_bfd);
6944 /* ELF symbols in relocatable files are section relative, but
6945 in executable files they are virtual addresses. Note that
6946 this code assumes that all ELF sections have an associated
6947 BFD section with a reasonable value for output_offset; below
6948 we assume that they also have a reasonable value for
6949 output_section. Any special sections must be set up to meet
6950 these requirements. */
6951 osym.st_value += isec->output_offset;
6952 if (! finfo->info->relocatable)
6954 osym.st_value += isec->output_section->vma;
6955 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6957 /* STT_TLS symbols are relative to PT_TLS segment base. */
6958 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6959 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6963 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6964 return FALSE;
6967 /* Relocate the contents of each section. */
6968 sym_hashes = elf_sym_hashes (input_bfd);
6969 for (o = input_bfd->sections; o != NULL; o = o->next)
6971 bfd_byte *contents;
6973 if (! o->linker_mark)
6975 /* This section was omitted from the link. */
6976 continue;
6979 if ((o->flags & SEC_HAS_CONTENTS) == 0
6980 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6981 continue;
6983 if ((o->flags & SEC_LINKER_CREATED) != 0)
6985 /* Section was created by _bfd_elf_link_create_dynamic_sections
6986 or somesuch. */
6987 continue;
6990 /* Get the contents of the section. They have been cached by a
6991 relaxation routine. Note that o is a section in an input
6992 file, so the contents field will not have been set by any of
6993 the routines which work on output files. */
6994 if (elf_section_data (o)->this_hdr.contents != NULL)
6995 contents = elf_section_data (o)->this_hdr.contents;
6996 else
6998 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7000 contents = finfo->contents;
7001 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7002 return FALSE;
7005 if ((o->flags & SEC_RELOC) != 0)
7007 Elf_Internal_Rela *internal_relocs;
7008 bfd_vma r_type_mask;
7009 int r_sym_shift;
7011 /* Get the swapped relocs. */
7012 internal_relocs
7013 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7014 finfo->internal_relocs, FALSE);
7015 if (internal_relocs == NULL
7016 && o->reloc_count > 0)
7017 return FALSE;
7019 if (bed->s->arch_size == 32)
7021 r_type_mask = 0xff;
7022 r_sym_shift = 8;
7024 else
7026 r_type_mask = 0xffffffff;
7027 r_sym_shift = 32;
7030 /* Run through the relocs looking for any against symbols
7031 from discarded sections and section symbols from
7032 removed link-once sections. Complain about relocs
7033 against discarded sections. Zero relocs against removed
7034 link-once sections. */
7035 if (!elf_section_ignore_discarded_relocs (o))
7037 Elf_Internal_Rela *rel, *relend;
7038 unsigned int action = (*bed->action_discarded) (o);
7040 rel = internal_relocs;
7041 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7042 for ( ; rel < relend; rel++)
7044 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7045 asection **ps, *sec;
7046 struct elf_link_hash_entry *h = NULL;
7047 const char *sym_name;
7049 if (r_symndx == STN_UNDEF)
7050 continue;
7052 if (r_symndx >= locsymcount
7053 || (elf_bad_symtab (input_bfd)
7054 && finfo->sections[r_symndx] == NULL))
7056 h = sym_hashes[r_symndx - extsymoff];
7058 /* Badly formatted input files can contain relocs that
7059 reference non-existant symbols. Check here so that
7060 we do not seg fault. */
7061 if (h == NULL)
7063 char buffer [32];
7065 sprintf_vma (buffer, rel->r_info);
7066 (*_bfd_error_handler)
7067 (_("error: %B contains a reloc (0x%s) for section %A "
7068 "that references a non-existent global symbol"),
7069 input_bfd, o, buffer);
7070 bfd_set_error (bfd_error_bad_value);
7071 return FALSE;
7074 while (h->root.type == bfd_link_hash_indirect
7075 || h->root.type == bfd_link_hash_warning)
7076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7078 if (h->root.type != bfd_link_hash_defined
7079 && h->root.type != bfd_link_hash_defweak)
7080 continue;
7082 ps = &h->root.u.def.section;
7083 sym_name = h->root.root.string;
7085 else
7087 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7088 ps = &finfo->sections[r_symndx];
7089 sym_name = bfd_elf_sym_name (input_bfd,
7090 symtab_hdr,
7091 sym, *ps);
7094 /* Complain if the definition comes from a
7095 discarded section. */
7096 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7098 BFD_ASSERT (r_symndx != 0);
7099 if (action & COMPLAIN)
7100 (*finfo->info->callbacks->einfo)
7101 (_("%X`%s' referenced in section `%A' of %B: "
7102 "defined in discarded section `%A' of %B\n"),
7103 sym_name, o, input_bfd, sec, sec->owner);
7105 /* Try to do the best we can to support buggy old
7106 versions of gcc. Pretend that the symbol is
7107 really defined in the kept linkonce section.
7108 FIXME: This is quite broken. Modifying the
7109 symbol here means we will be changing all later
7110 uses of the symbol, not just in this section. */
7111 if (action & PRETEND)
7113 asection *kept;
7115 kept = _bfd_elf_check_kept_section (sec);
7116 if (kept != NULL)
7118 *ps = kept;
7119 continue;
7123 /* Remove the symbol reference from the reloc, but
7124 don't kill the reloc completely. This is so that
7125 a zero value will be written into the section,
7126 which may have non-zero contents put there by the
7127 assembler. Zero in things like an eh_frame fde
7128 pc_begin allows stack unwinders to recognize the
7129 fde as bogus. */
7130 rel->r_info &= r_type_mask;
7131 rel->r_addend = 0;
7136 /* Relocate the section by invoking a back end routine.
7138 The back end routine is responsible for adjusting the
7139 section contents as necessary, and (if using Rela relocs
7140 and generating a relocatable output file) adjusting the
7141 reloc addend as necessary.
7143 The back end routine does not have to worry about setting
7144 the reloc address or the reloc symbol index.
7146 The back end routine is given a pointer to the swapped in
7147 internal symbols, and can access the hash table entries
7148 for the external symbols via elf_sym_hashes (input_bfd).
7150 When generating relocatable output, the back end routine
7151 must handle STB_LOCAL/STT_SECTION symbols specially. The
7152 output symbol is going to be a section symbol
7153 corresponding to the output section, which will require
7154 the addend to be adjusted. */
7156 if (! (*relocate_section) (output_bfd, finfo->info,
7157 input_bfd, o, contents,
7158 internal_relocs,
7159 isymbuf,
7160 finfo->sections))
7161 return FALSE;
7163 if (emit_relocs)
7165 Elf_Internal_Rela *irela;
7166 Elf_Internal_Rela *irelaend;
7167 bfd_vma last_offset;
7168 struct elf_link_hash_entry **rel_hash;
7169 struct elf_link_hash_entry **rel_hash_list;
7170 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7171 unsigned int next_erel;
7172 bfd_boolean rela_normal;
7174 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7175 rela_normal = (bed->rela_normal
7176 && (input_rel_hdr->sh_entsize
7177 == bed->s->sizeof_rela));
7179 /* Adjust the reloc addresses and symbol indices. */
7181 irela = internal_relocs;
7182 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7183 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7184 + elf_section_data (o->output_section)->rel_count
7185 + elf_section_data (o->output_section)->rel_count2);
7186 rel_hash_list = rel_hash;
7187 last_offset = o->output_offset;
7188 if (!finfo->info->relocatable)
7189 last_offset += o->output_section->vma;
7190 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7192 unsigned long r_symndx;
7193 asection *sec;
7194 Elf_Internal_Sym sym;
7196 if (next_erel == bed->s->int_rels_per_ext_rel)
7198 rel_hash++;
7199 next_erel = 0;
7202 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7203 finfo->info, o,
7204 irela->r_offset);
7205 if (irela->r_offset >= (bfd_vma) -2)
7207 /* This is a reloc for a deleted entry or somesuch.
7208 Turn it into an R_*_NONE reloc, at the same
7209 offset as the last reloc. elf_eh_frame.c and
7210 elf_bfd_discard_info rely on reloc offsets
7211 being ordered. */
7212 irela->r_offset = last_offset;
7213 irela->r_info = 0;
7214 irela->r_addend = 0;
7215 continue;
7218 irela->r_offset += o->output_offset;
7220 /* Relocs in an executable have to be virtual addresses. */
7221 if (!finfo->info->relocatable)
7222 irela->r_offset += o->output_section->vma;
7224 last_offset = irela->r_offset;
7226 r_symndx = irela->r_info >> r_sym_shift;
7227 if (r_symndx == STN_UNDEF)
7228 continue;
7230 if (r_symndx >= locsymcount
7231 || (elf_bad_symtab (input_bfd)
7232 && finfo->sections[r_symndx] == NULL))
7234 struct elf_link_hash_entry *rh;
7235 unsigned long indx;
7237 /* This is a reloc against a global symbol. We
7238 have not yet output all the local symbols, so
7239 we do not know the symbol index of any global
7240 symbol. We set the rel_hash entry for this
7241 reloc to point to the global hash table entry
7242 for this symbol. The symbol index is then
7243 set at the end of bfd_elf_final_link. */
7244 indx = r_symndx - extsymoff;
7245 rh = elf_sym_hashes (input_bfd)[indx];
7246 while (rh->root.type == bfd_link_hash_indirect
7247 || rh->root.type == bfd_link_hash_warning)
7248 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7250 /* Setting the index to -2 tells
7251 elf_link_output_extsym that this symbol is
7252 used by a reloc. */
7253 BFD_ASSERT (rh->indx < 0);
7254 rh->indx = -2;
7256 *rel_hash = rh;
7258 continue;
7261 /* This is a reloc against a local symbol. */
7263 *rel_hash = NULL;
7264 sym = isymbuf[r_symndx];
7265 sec = finfo->sections[r_symndx];
7266 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7268 /* I suppose the backend ought to fill in the
7269 section of any STT_SECTION symbol against a
7270 processor specific section. */
7271 r_symndx = 0;
7272 if (bfd_is_abs_section (sec))
7274 else if (sec == NULL || sec->owner == NULL)
7276 bfd_set_error (bfd_error_bad_value);
7277 return FALSE;
7279 else
7281 asection *osec = sec->output_section;
7283 /* If we have discarded a section, the output
7284 section will be the absolute section. In
7285 case of discarded link-once and discarded
7286 SEC_MERGE sections, use the kept section. */
7287 if (bfd_is_abs_section (osec)
7288 && sec->kept_section != NULL
7289 && sec->kept_section->output_section != NULL)
7291 osec = sec->kept_section->output_section;
7292 irela->r_addend -= osec->vma;
7295 if (!bfd_is_abs_section (osec))
7297 r_symndx = osec->target_index;
7298 BFD_ASSERT (r_symndx != 0);
7302 /* Adjust the addend according to where the
7303 section winds up in the output section. */
7304 if (rela_normal)
7305 irela->r_addend += sec->output_offset;
7307 else
7309 if (finfo->indices[r_symndx] == -1)
7311 unsigned long shlink;
7312 const char *name;
7313 asection *osec;
7315 if (finfo->info->strip == strip_all)
7317 /* You can't do ld -r -s. */
7318 bfd_set_error (bfd_error_invalid_operation);
7319 return FALSE;
7322 /* This symbol was skipped earlier, but
7323 since it is needed by a reloc, we
7324 must output it now. */
7325 shlink = symtab_hdr->sh_link;
7326 name = (bfd_elf_string_from_elf_section
7327 (input_bfd, shlink, sym.st_name));
7328 if (name == NULL)
7329 return FALSE;
7331 osec = sec->output_section;
7332 sym.st_shndx =
7333 _bfd_elf_section_from_bfd_section (output_bfd,
7334 osec);
7335 if (sym.st_shndx == SHN_BAD)
7336 return FALSE;
7338 sym.st_value += sec->output_offset;
7339 if (! finfo->info->relocatable)
7341 sym.st_value += osec->vma;
7342 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7344 /* STT_TLS symbols are relative to PT_TLS
7345 segment base. */
7346 BFD_ASSERT (elf_hash_table (finfo->info)
7347 ->tls_sec != NULL);
7348 sym.st_value -= (elf_hash_table (finfo->info)
7349 ->tls_sec->vma);
7353 finfo->indices[r_symndx]
7354 = bfd_get_symcount (output_bfd);
7356 if (! elf_link_output_sym (finfo, name, &sym, sec,
7357 NULL))
7358 return FALSE;
7361 r_symndx = finfo->indices[r_symndx];
7364 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7365 | (irela->r_info & r_type_mask));
7368 /* Swap out the relocs. */
7369 if (input_rel_hdr->sh_size != 0
7370 && !bed->elf_backend_emit_relocs (output_bfd, o,
7371 input_rel_hdr,
7372 internal_relocs,
7373 rel_hash_list))
7374 return FALSE;
7376 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7377 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7379 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7380 * bed->s->int_rels_per_ext_rel);
7381 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7382 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7383 input_rel_hdr2,
7384 internal_relocs,
7385 rel_hash_list))
7386 return FALSE;
7391 /* Write out the modified section contents. */
7392 if (bed->elf_backend_write_section
7393 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7395 /* Section written out. */
7397 else switch (o->sec_info_type)
7399 case ELF_INFO_TYPE_STABS:
7400 if (! (_bfd_write_section_stabs
7401 (output_bfd,
7402 &elf_hash_table (finfo->info)->stab_info,
7403 o, &elf_section_data (o)->sec_info, contents)))
7404 return FALSE;
7405 break;
7406 case ELF_INFO_TYPE_MERGE:
7407 if (! _bfd_write_merged_section (output_bfd, o,
7408 elf_section_data (o)->sec_info))
7409 return FALSE;
7410 break;
7411 case ELF_INFO_TYPE_EH_FRAME:
7413 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7414 o, contents))
7415 return FALSE;
7417 break;
7418 default:
7420 if (! (o->flags & SEC_EXCLUDE)
7421 && ! bfd_set_section_contents (output_bfd, o->output_section,
7422 contents,
7423 (file_ptr) o->output_offset,
7424 o->size))
7425 return FALSE;
7427 break;
7431 return TRUE;
7434 /* Generate a reloc when linking an ELF file. This is a reloc
7435 requested by the linker, and does not come from any input file. This
7436 is used to build constructor and destructor tables when linking
7437 with -Ur. */
7439 static bfd_boolean
7440 elf_reloc_link_order (bfd *output_bfd,
7441 struct bfd_link_info *info,
7442 asection *output_section,
7443 struct bfd_link_order *link_order)
7445 reloc_howto_type *howto;
7446 long indx;
7447 bfd_vma offset;
7448 bfd_vma addend;
7449 struct elf_link_hash_entry **rel_hash_ptr;
7450 Elf_Internal_Shdr *rel_hdr;
7451 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7452 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7453 bfd_byte *erel;
7454 unsigned int i;
7456 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7457 if (howto == NULL)
7459 bfd_set_error (bfd_error_bad_value);
7460 return FALSE;
7463 addend = link_order->u.reloc.p->addend;
7465 /* Figure out the symbol index. */
7466 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7467 + elf_section_data (output_section)->rel_count
7468 + elf_section_data (output_section)->rel_count2);
7469 if (link_order->type == bfd_section_reloc_link_order)
7471 indx = link_order->u.reloc.p->u.section->target_index;
7472 BFD_ASSERT (indx != 0);
7473 *rel_hash_ptr = NULL;
7475 else
7477 struct elf_link_hash_entry *h;
7479 /* Treat a reloc against a defined symbol as though it were
7480 actually against the section. */
7481 h = ((struct elf_link_hash_entry *)
7482 bfd_wrapped_link_hash_lookup (output_bfd, info,
7483 link_order->u.reloc.p->u.name,
7484 FALSE, FALSE, TRUE));
7485 if (h != NULL
7486 && (h->root.type == bfd_link_hash_defined
7487 || h->root.type == bfd_link_hash_defweak))
7489 asection *section;
7491 section = h->root.u.def.section;
7492 indx = section->output_section->target_index;
7493 *rel_hash_ptr = NULL;
7494 /* It seems that we ought to add the symbol value to the
7495 addend here, but in practice it has already been added
7496 because it was passed to constructor_callback. */
7497 addend += section->output_section->vma + section->output_offset;
7499 else if (h != NULL)
7501 /* Setting the index to -2 tells elf_link_output_extsym that
7502 this symbol is used by a reloc. */
7503 h->indx = -2;
7504 *rel_hash_ptr = h;
7505 indx = 0;
7507 else
7509 if (! ((*info->callbacks->unattached_reloc)
7510 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7511 return FALSE;
7512 indx = 0;
7516 /* If this is an inplace reloc, we must write the addend into the
7517 object file. */
7518 if (howto->partial_inplace && addend != 0)
7520 bfd_size_type size;
7521 bfd_reloc_status_type rstat;
7522 bfd_byte *buf;
7523 bfd_boolean ok;
7524 const char *sym_name;
7526 size = bfd_get_reloc_size (howto);
7527 buf = bfd_zmalloc (size);
7528 if (buf == NULL)
7529 return FALSE;
7530 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7531 switch (rstat)
7533 case bfd_reloc_ok:
7534 break;
7536 default:
7537 case bfd_reloc_outofrange:
7538 abort ();
7540 case bfd_reloc_overflow:
7541 if (link_order->type == bfd_section_reloc_link_order)
7542 sym_name = bfd_section_name (output_bfd,
7543 link_order->u.reloc.p->u.section);
7544 else
7545 sym_name = link_order->u.reloc.p->u.name;
7546 if (! ((*info->callbacks->reloc_overflow)
7547 (info, NULL, sym_name, howto->name, addend, NULL,
7548 NULL, (bfd_vma) 0)))
7550 free (buf);
7551 return FALSE;
7553 break;
7555 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7556 link_order->offset, size);
7557 free (buf);
7558 if (! ok)
7559 return FALSE;
7562 /* The address of a reloc is relative to the section in a
7563 relocatable file, and is a virtual address in an executable
7564 file. */
7565 offset = link_order->offset;
7566 if (! info->relocatable)
7567 offset += output_section->vma;
7569 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7571 irel[i].r_offset = offset;
7572 irel[i].r_info = 0;
7573 irel[i].r_addend = 0;
7575 if (bed->s->arch_size == 32)
7576 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7577 else
7578 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7580 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7581 erel = rel_hdr->contents;
7582 if (rel_hdr->sh_type == SHT_REL)
7584 erel += (elf_section_data (output_section)->rel_count
7585 * bed->s->sizeof_rel);
7586 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7588 else
7590 irel[0].r_addend = addend;
7591 erel += (elf_section_data (output_section)->rel_count
7592 * bed->s->sizeof_rela);
7593 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7596 ++elf_section_data (output_section)->rel_count;
7598 return TRUE;
7602 /* Get the output vma of the section pointed to by the sh_link field. */
7604 static bfd_vma
7605 elf_get_linked_section_vma (struct bfd_link_order *p)
7607 Elf_Internal_Shdr **elf_shdrp;
7608 asection *s;
7609 int elfsec;
7611 s = p->u.indirect.section;
7612 elf_shdrp = elf_elfsections (s->owner);
7613 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7614 elfsec = elf_shdrp[elfsec]->sh_link;
7615 /* PR 290:
7616 The Intel C compiler generates SHT_IA_64_UNWIND with
7617 SHF_LINK_ORDER. But it doesn't set the sh_link or
7618 sh_info fields. Hence we could get the situation
7619 where elfsec is 0. */
7620 if (elfsec == 0)
7622 const struct elf_backend_data *bed
7623 = get_elf_backend_data (s->owner);
7624 if (bed->link_order_error_handler)
7625 bed->link_order_error_handler
7626 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7627 return 0;
7629 else
7631 s = elf_shdrp[elfsec]->bfd_section;
7632 return s->output_section->vma + s->output_offset;
7637 /* Compare two sections based on the locations of the sections they are
7638 linked to. Used by elf_fixup_link_order. */
7640 static int
7641 compare_link_order (const void * a, const void * b)
7643 bfd_vma apos;
7644 bfd_vma bpos;
7646 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7647 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7648 if (apos < bpos)
7649 return -1;
7650 return apos > bpos;
7654 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7655 order as their linked sections. Returns false if this could not be done
7656 because an output section includes both ordered and unordered
7657 sections. Ideally we'd do this in the linker proper. */
7659 static bfd_boolean
7660 elf_fixup_link_order (bfd *abfd, asection *o)
7662 int seen_linkorder;
7663 int seen_other;
7664 int n;
7665 struct bfd_link_order *p;
7666 bfd *sub;
7667 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7668 unsigned elfsec;
7669 struct bfd_link_order **sections;
7670 asection *s, *other_sec, *linkorder_sec;
7671 bfd_vma offset;
7673 other_sec = NULL;
7674 linkorder_sec = NULL;
7675 seen_other = 0;
7676 seen_linkorder = 0;
7677 for (p = o->map_head.link_order; p != NULL; p = p->next)
7679 if (p->type == bfd_indirect_link_order)
7681 s = p->u.indirect.section;
7682 sub = s->owner;
7683 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7684 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7685 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
7686 && elfsec < elf_numsections (sub)
7687 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7689 seen_linkorder++;
7690 linkorder_sec = s;
7692 else
7694 seen_other++;
7695 other_sec = s;
7698 else
7699 seen_other++;
7701 if (seen_other && seen_linkorder)
7703 if (other_sec && linkorder_sec)
7704 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7705 o, linkorder_sec,
7706 linkorder_sec->owner, other_sec,
7707 other_sec->owner);
7708 else
7709 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7711 bfd_set_error (bfd_error_bad_value);
7712 return FALSE;
7716 if (!seen_linkorder)
7717 return TRUE;
7719 sections = (struct bfd_link_order **)
7720 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7721 seen_linkorder = 0;
7723 for (p = o->map_head.link_order; p != NULL; p = p->next)
7725 sections[seen_linkorder++] = p;
7727 /* Sort the input sections in the order of their linked section. */
7728 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7729 compare_link_order);
7731 /* Change the offsets of the sections. */
7732 offset = 0;
7733 for (n = 0; n < seen_linkorder; n++)
7735 s = sections[n]->u.indirect.section;
7736 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7737 s->output_offset = offset;
7738 sections[n]->offset = offset;
7739 offset += sections[n]->size;
7742 return TRUE;
7746 /* Do the final step of an ELF link. */
7748 bfd_boolean
7749 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7751 bfd_boolean dynamic;
7752 bfd_boolean emit_relocs;
7753 bfd *dynobj;
7754 struct elf_final_link_info finfo;
7755 register asection *o;
7756 register struct bfd_link_order *p;
7757 register bfd *sub;
7758 bfd_size_type max_contents_size;
7759 bfd_size_type max_external_reloc_size;
7760 bfd_size_type max_internal_reloc_count;
7761 bfd_size_type max_sym_count;
7762 bfd_size_type max_sym_shndx_count;
7763 file_ptr off;
7764 Elf_Internal_Sym elfsym;
7765 unsigned int i;
7766 Elf_Internal_Shdr *symtab_hdr;
7767 Elf_Internal_Shdr *symtab_shndx_hdr;
7768 Elf_Internal_Shdr *symstrtab_hdr;
7769 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7770 struct elf_outext_info eoinfo;
7771 bfd_boolean merged;
7772 size_t relativecount = 0;
7773 asection *reldyn = 0;
7774 bfd_size_type amt;
7776 if (! is_elf_hash_table (info->hash))
7777 return FALSE;
7779 if (info->shared)
7780 abfd->flags |= DYNAMIC;
7782 dynamic = elf_hash_table (info)->dynamic_sections_created;
7783 dynobj = elf_hash_table (info)->dynobj;
7785 emit_relocs = (info->relocatable
7786 || info->emitrelocations);
7788 finfo.info = info;
7789 finfo.output_bfd = abfd;
7790 finfo.symstrtab = _bfd_elf_stringtab_init ();
7791 if (finfo.symstrtab == NULL)
7792 return FALSE;
7794 if (! dynamic)
7796 finfo.dynsym_sec = NULL;
7797 finfo.hash_sec = NULL;
7798 finfo.symver_sec = NULL;
7800 else
7802 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7803 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7804 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7805 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7806 /* Note that it is OK if symver_sec is NULL. */
7809 finfo.contents = NULL;
7810 finfo.external_relocs = NULL;
7811 finfo.internal_relocs = NULL;
7812 finfo.external_syms = NULL;
7813 finfo.locsym_shndx = NULL;
7814 finfo.internal_syms = NULL;
7815 finfo.indices = NULL;
7816 finfo.sections = NULL;
7817 finfo.symbuf = NULL;
7818 finfo.symshndxbuf = NULL;
7819 finfo.symbuf_count = 0;
7820 finfo.shndxbuf_size = 0;
7822 /* Count up the number of relocations we will output for each output
7823 section, so that we know the sizes of the reloc sections. We
7824 also figure out some maximum sizes. */
7825 max_contents_size = 0;
7826 max_external_reloc_size = 0;
7827 max_internal_reloc_count = 0;
7828 max_sym_count = 0;
7829 max_sym_shndx_count = 0;
7830 merged = FALSE;
7831 for (o = abfd->sections; o != NULL; o = o->next)
7833 struct bfd_elf_section_data *esdo = elf_section_data (o);
7834 o->reloc_count = 0;
7836 for (p = o->map_head.link_order; p != NULL; p = p->next)
7838 unsigned int reloc_count = 0;
7839 struct bfd_elf_section_data *esdi = NULL;
7840 unsigned int *rel_count1;
7842 if (p->type == bfd_section_reloc_link_order
7843 || p->type == bfd_symbol_reloc_link_order)
7844 reloc_count = 1;
7845 else if (p->type == bfd_indirect_link_order)
7847 asection *sec;
7849 sec = p->u.indirect.section;
7850 esdi = elf_section_data (sec);
7852 /* Mark all sections which are to be included in the
7853 link. This will normally be every section. We need
7854 to do this so that we can identify any sections which
7855 the linker has decided to not include. */
7856 sec->linker_mark = TRUE;
7858 if (sec->flags & SEC_MERGE)
7859 merged = TRUE;
7861 if (info->relocatable || info->emitrelocations)
7862 reloc_count = sec->reloc_count;
7863 else if (bed->elf_backend_count_relocs)
7865 Elf_Internal_Rela * relocs;
7867 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7868 info->keep_memory);
7870 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7872 if (elf_section_data (o)->relocs != relocs)
7873 free (relocs);
7876 if (sec->rawsize > max_contents_size)
7877 max_contents_size = sec->rawsize;
7878 if (sec->size > max_contents_size)
7879 max_contents_size = sec->size;
7881 /* We are interested in just local symbols, not all
7882 symbols. */
7883 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7884 && (sec->owner->flags & DYNAMIC) == 0)
7886 size_t sym_count;
7888 if (elf_bad_symtab (sec->owner))
7889 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7890 / bed->s->sizeof_sym);
7891 else
7892 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7894 if (sym_count > max_sym_count)
7895 max_sym_count = sym_count;
7897 if (sym_count > max_sym_shndx_count
7898 && elf_symtab_shndx (sec->owner) != 0)
7899 max_sym_shndx_count = sym_count;
7901 if ((sec->flags & SEC_RELOC) != 0)
7903 size_t ext_size;
7905 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7906 if (ext_size > max_external_reloc_size)
7907 max_external_reloc_size = ext_size;
7908 if (sec->reloc_count > max_internal_reloc_count)
7909 max_internal_reloc_count = sec->reloc_count;
7914 if (reloc_count == 0)
7915 continue;
7917 o->reloc_count += reloc_count;
7919 /* MIPS may have a mix of REL and RELA relocs on sections.
7920 To support this curious ABI we keep reloc counts in
7921 elf_section_data too. We must be careful to add the
7922 relocations from the input section to the right output
7923 count. FIXME: Get rid of one count. We have
7924 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7925 rel_count1 = &esdo->rel_count;
7926 if (esdi != NULL)
7928 bfd_boolean same_size;
7929 bfd_size_type entsize1;
7931 entsize1 = esdi->rel_hdr.sh_entsize;
7932 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7933 || entsize1 == bed->s->sizeof_rela);
7934 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7936 if (!same_size)
7937 rel_count1 = &esdo->rel_count2;
7939 if (esdi->rel_hdr2 != NULL)
7941 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7942 unsigned int alt_count;
7943 unsigned int *rel_count2;
7945 BFD_ASSERT (entsize2 != entsize1
7946 && (entsize2 == bed->s->sizeof_rel
7947 || entsize2 == bed->s->sizeof_rela));
7949 rel_count2 = &esdo->rel_count2;
7950 if (!same_size)
7951 rel_count2 = &esdo->rel_count;
7953 /* The following is probably too simplistic if the
7954 backend counts output relocs unusually. */
7955 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7956 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7957 *rel_count2 += alt_count;
7958 reloc_count -= alt_count;
7961 *rel_count1 += reloc_count;
7964 if (o->reloc_count > 0)
7965 o->flags |= SEC_RELOC;
7966 else
7968 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7969 set it (this is probably a bug) and if it is set
7970 assign_section_numbers will create a reloc section. */
7971 o->flags &=~ SEC_RELOC;
7974 /* If the SEC_ALLOC flag is not set, force the section VMA to
7975 zero. This is done in elf_fake_sections as well, but forcing
7976 the VMA to 0 here will ensure that relocs against these
7977 sections are handled correctly. */
7978 if ((o->flags & SEC_ALLOC) == 0
7979 && ! o->user_set_vma)
7980 o->vma = 0;
7983 if (! info->relocatable && merged)
7984 elf_link_hash_traverse (elf_hash_table (info),
7985 _bfd_elf_link_sec_merge_syms, abfd);
7987 /* Figure out the file positions for everything but the symbol table
7988 and the relocs. We set symcount to force assign_section_numbers
7989 to create a symbol table. */
7990 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7991 BFD_ASSERT (! abfd->output_has_begun);
7992 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7993 goto error_return;
7995 /* Set sizes, and assign file positions for reloc sections. */
7996 for (o = abfd->sections; o != NULL; o = o->next)
7998 if ((o->flags & SEC_RELOC) != 0)
8000 if (!(_bfd_elf_link_size_reloc_section
8001 (abfd, &elf_section_data (o)->rel_hdr, o)))
8002 goto error_return;
8004 if (elf_section_data (o)->rel_hdr2
8005 && !(_bfd_elf_link_size_reloc_section
8006 (abfd, elf_section_data (o)->rel_hdr2, o)))
8007 goto error_return;
8010 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8011 to count upwards while actually outputting the relocations. */
8012 elf_section_data (o)->rel_count = 0;
8013 elf_section_data (o)->rel_count2 = 0;
8016 _bfd_elf_assign_file_positions_for_relocs (abfd);
8018 /* We have now assigned file positions for all the sections except
8019 .symtab and .strtab. We start the .symtab section at the current
8020 file position, and write directly to it. We build the .strtab
8021 section in memory. */
8022 bfd_get_symcount (abfd) = 0;
8023 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8024 /* sh_name is set in prep_headers. */
8025 symtab_hdr->sh_type = SHT_SYMTAB;
8026 /* sh_flags, sh_addr and sh_size all start off zero. */
8027 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8028 /* sh_link is set in assign_section_numbers. */
8029 /* sh_info is set below. */
8030 /* sh_offset is set just below. */
8031 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8033 off = elf_tdata (abfd)->next_file_pos;
8034 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8036 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8037 incorrect. We do not yet know the size of the .symtab section.
8038 We correct next_file_pos below, after we do know the size. */
8040 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8041 continuously seeking to the right position in the file. */
8042 if (! info->keep_memory || max_sym_count < 20)
8043 finfo.symbuf_size = 20;
8044 else
8045 finfo.symbuf_size = max_sym_count;
8046 amt = finfo.symbuf_size;
8047 amt *= bed->s->sizeof_sym;
8048 finfo.symbuf = bfd_malloc (amt);
8049 if (finfo.symbuf == NULL)
8050 goto error_return;
8051 if (elf_numsections (abfd) > SHN_LORESERVE)
8053 /* Wild guess at number of output symbols. realloc'd as needed. */
8054 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8055 finfo.shndxbuf_size = amt;
8056 amt *= sizeof (Elf_External_Sym_Shndx);
8057 finfo.symshndxbuf = bfd_zmalloc (amt);
8058 if (finfo.symshndxbuf == NULL)
8059 goto error_return;
8062 /* Start writing out the symbol table. The first symbol is always a
8063 dummy symbol. */
8064 if (info->strip != strip_all
8065 || emit_relocs)
8067 elfsym.st_value = 0;
8068 elfsym.st_size = 0;
8069 elfsym.st_info = 0;
8070 elfsym.st_other = 0;
8071 elfsym.st_shndx = SHN_UNDEF;
8072 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8073 NULL))
8074 goto error_return;
8077 /* Output a symbol for each section. We output these even if we are
8078 discarding local symbols, since they are used for relocs. These
8079 symbols have no names. We store the index of each one in the
8080 index field of the section, so that we can find it again when
8081 outputting relocs. */
8082 if (info->strip != strip_all
8083 || emit_relocs)
8085 elfsym.st_size = 0;
8086 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8087 elfsym.st_other = 0;
8088 for (i = 1; i < elf_numsections (abfd); i++)
8090 o = bfd_section_from_elf_index (abfd, i);
8091 if (o != NULL)
8092 o->target_index = bfd_get_symcount (abfd);
8093 elfsym.st_shndx = i;
8094 if (info->relocatable || o == NULL)
8095 elfsym.st_value = 0;
8096 else
8097 elfsym.st_value = o->vma;
8098 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8099 goto error_return;
8100 if (i == SHN_LORESERVE - 1)
8101 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8105 /* Allocate some memory to hold information read in from the input
8106 files. */
8107 if (max_contents_size != 0)
8109 finfo.contents = bfd_malloc (max_contents_size);
8110 if (finfo.contents == NULL)
8111 goto error_return;
8114 if (max_external_reloc_size != 0)
8116 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8117 if (finfo.external_relocs == NULL)
8118 goto error_return;
8121 if (max_internal_reloc_count != 0)
8123 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8124 amt *= sizeof (Elf_Internal_Rela);
8125 finfo.internal_relocs = bfd_malloc (amt);
8126 if (finfo.internal_relocs == NULL)
8127 goto error_return;
8130 if (max_sym_count != 0)
8132 amt = max_sym_count * bed->s->sizeof_sym;
8133 finfo.external_syms = bfd_malloc (amt);
8134 if (finfo.external_syms == NULL)
8135 goto error_return;
8137 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8138 finfo.internal_syms = bfd_malloc (amt);
8139 if (finfo.internal_syms == NULL)
8140 goto error_return;
8142 amt = max_sym_count * sizeof (long);
8143 finfo.indices = bfd_malloc (amt);
8144 if (finfo.indices == NULL)
8145 goto error_return;
8147 amt = max_sym_count * sizeof (asection *);
8148 finfo.sections = bfd_malloc (amt);
8149 if (finfo.sections == NULL)
8150 goto error_return;
8153 if (max_sym_shndx_count != 0)
8155 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8156 finfo.locsym_shndx = bfd_malloc (amt);
8157 if (finfo.locsym_shndx == NULL)
8158 goto error_return;
8161 if (elf_hash_table (info)->tls_sec)
8163 bfd_vma base, end = 0;
8164 asection *sec;
8166 for (sec = elf_hash_table (info)->tls_sec;
8167 sec && (sec->flags & SEC_THREAD_LOCAL);
8168 sec = sec->next)
8170 bfd_size_type size = sec->size;
8172 if (size == 0
8173 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8175 struct bfd_link_order *o = sec->map_tail.link_order;
8176 if (o != NULL)
8177 size = o->offset + o->size;
8179 end = sec->vma + size;
8181 base = elf_hash_table (info)->tls_sec->vma;
8182 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8183 elf_hash_table (info)->tls_size = end - base;
8186 /* Reorder SHF_LINK_ORDER sections. */
8187 for (o = abfd->sections; o != NULL; o = o->next)
8189 if (!elf_fixup_link_order (abfd, o))
8190 return FALSE;
8193 /* Since ELF permits relocations to be against local symbols, we
8194 must have the local symbols available when we do the relocations.
8195 Since we would rather only read the local symbols once, and we
8196 would rather not keep them in memory, we handle all the
8197 relocations for a single input file at the same time.
8199 Unfortunately, there is no way to know the total number of local
8200 symbols until we have seen all of them, and the local symbol
8201 indices precede the global symbol indices. This means that when
8202 we are generating relocatable output, and we see a reloc against
8203 a global symbol, we can not know the symbol index until we have
8204 finished examining all the local symbols to see which ones we are
8205 going to output. To deal with this, we keep the relocations in
8206 memory, and don't output them until the end of the link. This is
8207 an unfortunate waste of memory, but I don't see a good way around
8208 it. Fortunately, it only happens when performing a relocatable
8209 link, which is not the common case. FIXME: If keep_memory is set
8210 we could write the relocs out and then read them again; I don't
8211 know how bad the memory loss will be. */
8213 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8214 sub->output_has_begun = FALSE;
8215 for (o = abfd->sections; o != NULL; o = o->next)
8217 for (p = o->map_head.link_order; p != NULL; p = p->next)
8219 if (p->type == bfd_indirect_link_order
8220 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8221 == bfd_target_elf_flavour)
8222 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8224 if (! sub->output_has_begun)
8226 if (! elf_link_input_bfd (&finfo, sub))
8227 goto error_return;
8228 sub->output_has_begun = TRUE;
8231 else if (p->type == bfd_section_reloc_link_order
8232 || p->type == bfd_symbol_reloc_link_order)
8234 if (! elf_reloc_link_order (abfd, info, o, p))
8235 goto error_return;
8237 else
8239 if (! _bfd_default_link_order (abfd, info, o, p))
8240 goto error_return;
8245 /* Output any global symbols that got converted to local in a
8246 version script or due to symbol visibility. We do this in a
8247 separate step since ELF requires all local symbols to appear
8248 prior to any global symbols. FIXME: We should only do this if
8249 some global symbols were, in fact, converted to become local.
8250 FIXME: Will this work correctly with the Irix 5 linker? */
8251 eoinfo.failed = FALSE;
8252 eoinfo.finfo = &finfo;
8253 eoinfo.localsyms = TRUE;
8254 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8255 &eoinfo);
8256 if (eoinfo.failed)
8257 return FALSE;
8259 /* That wrote out all the local symbols. Finish up the symbol table
8260 with the global symbols. Even if we want to strip everything we
8261 can, we still need to deal with those global symbols that got
8262 converted to local in a version script. */
8264 /* The sh_info field records the index of the first non local symbol. */
8265 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8267 if (dynamic
8268 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8270 Elf_Internal_Sym sym;
8271 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8272 long last_local = 0;
8274 /* Write out the section symbols for the output sections. */
8275 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8277 asection *s;
8279 sym.st_size = 0;
8280 sym.st_name = 0;
8281 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8282 sym.st_other = 0;
8284 for (s = abfd->sections; s != NULL; s = s->next)
8286 int indx;
8287 bfd_byte *dest;
8288 long dynindx;
8290 dynindx = elf_section_data (s)->dynindx;
8291 if (dynindx <= 0)
8292 continue;
8293 indx = elf_section_data (s)->this_idx;
8294 BFD_ASSERT (indx > 0);
8295 sym.st_shndx = indx;
8296 sym.st_value = s->vma;
8297 dest = dynsym + dynindx * bed->s->sizeof_sym;
8298 if (last_local < dynindx)
8299 last_local = dynindx;
8300 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8304 /* Write out the local dynsyms. */
8305 if (elf_hash_table (info)->dynlocal)
8307 struct elf_link_local_dynamic_entry *e;
8308 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8310 asection *s;
8311 bfd_byte *dest;
8313 sym.st_size = e->isym.st_size;
8314 sym.st_other = e->isym.st_other;
8316 /* Copy the internal symbol as is.
8317 Note that we saved a word of storage and overwrote
8318 the original st_name with the dynstr_index. */
8319 sym = e->isym;
8321 if (e->isym.st_shndx != SHN_UNDEF
8322 && (e->isym.st_shndx < SHN_LORESERVE
8323 || e->isym.st_shndx > SHN_HIRESERVE))
8325 s = bfd_section_from_elf_index (e->input_bfd,
8326 e->isym.st_shndx);
8328 sym.st_shndx =
8329 elf_section_data (s->output_section)->this_idx;
8330 sym.st_value = (s->output_section->vma
8331 + s->output_offset
8332 + e->isym.st_value);
8335 if (last_local < e->dynindx)
8336 last_local = e->dynindx;
8338 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8339 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8343 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8344 last_local + 1;
8347 /* We get the global symbols from the hash table. */
8348 eoinfo.failed = FALSE;
8349 eoinfo.localsyms = FALSE;
8350 eoinfo.finfo = &finfo;
8351 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8352 &eoinfo);
8353 if (eoinfo.failed)
8354 return FALSE;
8356 /* If backend needs to output some symbols not present in the hash
8357 table, do it now. */
8358 if (bed->elf_backend_output_arch_syms)
8360 typedef bfd_boolean (*out_sym_func)
8361 (void *, const char *, Elf_Internal_Sym *, asection *,
8362 struct elf_link_hash_entry *);
8364 if (! ((*bed->elf_backend_output_arch_syms)
8365 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8366 return FALSE;
8369 /* Flush all symbols to the file. */
8370 if (! elf_link_flush_output_syms (&finfo, bed))
8371 return FALSE;
8373 /* Now we know the size of the symtab section. */
8374 off += symtab_hdr->sh_size;
8376 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8377 if (symtab_shndx_hdr->sh_name != 0)
8379 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8380 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8381 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8382 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8383 symtab_shndx_hdr->sh_size = amt;
8385 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8386 off, TRUE);
8388 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8389 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8390 return FALSE;
8394 /* Finish up and write out the symbol string table (.strtab)
8395 section. */
8396 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8397 /* sh_name was set in prep_headers. */
8398 symstrtab_hdr->sh_type = SHT_STRTAB;
8399 symstrtab_hdr->sh_flags = 0;
8400 symstrtab_hdr->sh_addr = 0;
8401 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8402 symstrtab_hdr->sh_entsize = 0;
8403 symstrtab_hdr->sh_link = 0;
8404 symstrtab_hdr->sh_info = 0;
8405 /* sh_offset is set just below. */
8406 symstrtab_hdr->sh_addralign = 1;
8408 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8409 elf_tdata (abfd)->next_file_pos = off;
8411 if (bfd_get_symcount (abfd) > 0)
8413 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8414 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8415 return FALSE;
8418 /* Adjust the relocs to have the correct symbol indices. */
8419 for (o = abfd->sections; o != NULL; o = o->next)
8421 if ((o->flags & SEC_RELOC) == 0)
8422 continue;
8424 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8425 elf_section_data (o)->rel_count,
8426 elf_section_data (o)->rel_hashes);
8427 if (elf_section_data (o)->rel_hdr2 != NULL)
8428 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8429 elf_section_data (o)->rel_count2,
8430 (elf_section_data (o)->rel_hashes
8431 + elf_section_data (o)->rel_count));
8433 /* Set the reloc_count field to 0 to prevent write_relocs from
8434 trying to swap the relocs out itself. */
8435 o->reloc_count = 0;
8438 if (dynamic && info->combreloc && dynobj != NULL)
8439 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8441 /* If we are linking against a dynamic object, or generating a
8442 shared library, finish up the dynamic linking information. */
8443 if (dynamic)
8445 bfd_byte *dyncon, *dynconend;
8447 /* Fix up .dynamic entries. */
8448 o = bfd_get_section_by_name (dynobj, ".dynamic");
8449 BFD_ASSERT (o != NULL);
8451 dyncon = o->contents;
8452 dynconend = o->contents + o->size;
8453 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8455 Elf_Internal_Dyn dyn;
8456 const char *name;
8457 unsigned int type;
8459 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8461 switch (dyn.d_tag)
8463 default:
8464 continue;
8465 case DT_NULL:
8466 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8468 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8470 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8471 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8472 default: continue;
8474 dyn.d_un.d_val = relativecount;
8475 relativecount = 0;
8476 break;
8478 continue;
8480 case DT_INIT:
8481 name = info->init_function;
8482 goto get_sym;
8483 case DT_FINI:
8484 name = info->fini_function;
8485 get_sym:
8487 struct elf_link_hash_entry *h;
8489 h = elf_link_hash_lookup (elf_hash_table (info), name,
8490 FALSE, FALSE, TRUE);
8491 if (h != NULL
8492 && (h->root.type == bfd_link_hash_defined
8493 || h->root.type == bfd_link_hash_defweak))
8495 dyn.d_un.d_val = h->root.u.def.value;
8496 o = h->root.u.def.section;
8497 if (o->output_section != NULL)
8498 dyn.d_un.d_val += (o->output_section->vma
8499 + o->output_offset);
8500 else
8502 /* The symbol is imported from another shared
8503 library and does not apply to this one. */
8504 dyn.d_un.d_val = 0;
8506 break;
8509 continue;
8511 case DT_PREINIT_ARRAYSZ:
8512 name = ".preinit_array";
8513 goto get_size;
8514 case DT_INIT_ARRAYSZ:
8515 name = ".init_array";
8516 goto get_size;
8517 case DT_FINI_ARRAYSZ:
8518 name = ".fini_array";
8519 get_size:
8520 o = bfd_get_section_by_name (abfd, name);
8521 if (o == NULL)
8523 (*_bfd_error_handler)
8524 (_("%B: could not find output section %s"), abfd, name);
8525 goto error_return;
8527 if (o->size == 0)
8528 (*_bfd_error_handler)
8529 (_("warning: %s section has zero size"), name);
8530 dyn.d_un.d_val = o->size;
8531 break;
8533 case DT_PREINIT_ARRAY:
8534 name = ".preinit_array";
8535 goto get_vma;
8536 case DT_INIT_ARRAY:
8537 name = ".init_array";
8538 goto get_vma;
8539 case DT_FINI_ARRAY:
8540 name = ".fini_array";
8541 goto get_vma;
8543 case DT_HASH:
8544 name = ".hash";
8545 goto get_vma;
8546 case DT_STRTAB:
8547 name = ".dynstr";
8548 goto get_vma;
8549 case DT_SYMTAB:
8550 name = ".dynsym";
8551 goto get_vma;
8552 case DT_VERDEF:
8553 name = ".gnu.version_d";
8554 goto get_vma;
8555 case DT_VERNEED:
8556 name = ".gnu.version_r";
8557 goto get_vma;
8558 case DT_VERSYM:
8559 name = ".gnu.version";
8560 get_vma:
8561 o = bfd_get_section_by_name (abfd, name);
8562 if (o == NULL)
8564 (*_bfd_error_handler)
8565 (_("%B: could not find output section %s"), abfd, name);
8566 goto error_return;
8568 dyn.d_un.d_ptr = o->vma;
8569 break;
8571 case DT_REL:
8572 case DT_RELA:
8573 case DT_RELSZ:
8574 case DT_RELASZ:
8575 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8576 type = SHT_REL;
8577 else
8578 type = SHT_RELA;
8579 dyn.d_un.d_val = 0;
8580 for (i = 1; i < elf_numsections (abfd); i++)
8582 Elf_Internal_Shdr *hdr;
8584 hdr = elf_elfsections (abfd)[i];
8585 if (hdr->sh_type == type
8586 && (hdr->sh_flags & SHF_ALLOC) != 0)
8588 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8589 dyn.d_un.d_val += hdr->sh_size;
8590 else
8592 if (dyn.d_un.d_val == 0
8593 || hdr->sh_addr < dyn.d_un.d_val)
8594 dyn.d_un.d_val = hdr->sh_addr;
8598 break;
8600 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8604 /* If we have created any dynamic sections, then output them. */
8605 if (dynobj != NULL)
8607 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8608 goto error_return;
8610 for (o = dynobj->sections; o != NULL; o = o->next)
8612 if ((o->flags & SEC_HAS_CONTENTS) == 0
8613 || o->size == 0
8614 || o->output_section == bfd_abs_section_ptr)
8615 continue;
8616 if ((o->flags & SEC_LINKER_CREATED) == 0)
8618 /* At this point, we are only interested in sections
8619 created by _bfd_elf_link_create_dynamic_sections. */
8620 continue;
8622 if (elf_hash_table (info)->stab_info.stabstr == o)
8623 continue;
8624 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8625 continue;
8626 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8627 != SHT_STRTAB)
8628 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8630 if (! bfd_set_section_contents (abfd, o->output_section,
8631 o->contents,
8632 (file_ptr) o->output_offset,
8633 o->size))
8634 goto error_return;
8636 else
8638 /* The contents of the .dynstr section are actually in a
8639 stringtab. */
8640 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8641 if (bfd_seek (abfd, off, SEEK_SET) != 0
8642 || ! _bfd_elf_strtab_emit (abfd,
8643 elf_hash_table (info)->dynstr))
8644 goto error_return;
8649 if (info->relocatable)
8651 bfd_boolean failed = FALSE;
8653 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8654 if (failed)
8655 goto error_return;
8658 /* If we have optimized stabs strings, output them. */
8659 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8661 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8662 goto error_return;
8665 if (info->eh_frame_hdr)
8667 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8668 goto error_return;
8671 if (finfo.symstrtab != NULL)
8672 _bfd_stringtab_free (finfo.symstrtab);
8673 if (finfo.contents != NULL)
8674 free (finfo.contents);
8675 if (finfo.external_relocs != NULL)
8676 free (finfo.external_relocs);
8677 if (finfo.internal_relocs != NULL)
8678 free (finfo.internal_relocs);
8679 if (finfo.external_syms != NULL)
8680 free (finfo.external_syms);
8681 if (finfo.locsym_shndx != NULL)
8682 free (finfo.locsym_shndx);
8683 if (finfo.internal_syms != NULL)
8684 free (finfo.internal_syms);
8685 if (finfo.indices != NULL)
8686 free (finfo.indices);
8687 if (finfo.sections != NULL)
8688 free (finfo.sections);
8689 if (finfo.symbuf != NULL)
8690 free (finfo.symbuf);
8691 if (finfo.symshndxbuf != NULL)
8692 free (finfo.symshndxbuf);
8693 for (o = abfd->sections; o != NULL; o = o->next)
8695 if ((o->flags & SEC_RELOC) != 0
8696 && elf_section_data (o)->rel_hashes != NULL)
8697 free (elf_section_data (o)->rel_hashes);
8700 elf_tdata (abfd)->linker = TRUE;
8702 return TRUE;
8704 error_return:
8705 if (finfo.symstrtab != NULL)
8706 _bfd_stringtab_free (finfo.symstrtab);
8707 if (finfo.contents != NULL)
8708 free (finfo.contents);
8709 if (finfo.external_relocs != NULL)
8710 free (finfo.external_relocs);
8711 if (finfo.internal_relocs != NULL)
8712 free (finfo.internal_relocs);
8713 if (finfo.external_syms != NULL)
8714 free (finfo.external_syms);
8715 if (finfo.locsym_shndx != NULL)
8716 free (finfo.locsym_shndx);
8717 if (finfo.internal_syms != NULL)
8718 free (finfo.internal_syms);
8719 if (finfo.indices != NULL)
8720 free (finfo.indices);
8721 if (finfo.sections != NULL)
8722 free (finfo.sections);
8723 if (finfo.symbuf != NULL)
8724 free (finfo.symbuf);
8725 if (finfo.symshndxbuf != NULL)
8726 free (finfo.symshndxbuf);
8727 for (o = abfd->sections; o != NULL; o = o->next)
8729 if ((o->flags & SEC_RELOC) != 0
8730 && elf_section_data (o)->rel_hashes != NULL)
8731 free (elf_section_data (o)->rel_hashes);
8734 return FALSE;
8737 /* Garbage collect unused sections. */
8739 /* The mark phase of garbage collection. For a given section, mark
8740 it and any sections in this section's group, and all the sections
8741 which define symbols to which it refers. */
8743 typedef asection * (*gc_mark_hook_fn)
8744 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8745 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8747 bfd_boolean
8748 _bfd_elf_gc_mark (struct bfd_link_info *info,
8749 asection *sec,
8750 gc_mark_hook_fn gc_mark_hook)
8752 bfd_boolean ret;
8753 bfd_boolean is_eh;
8754 asection *group_sec;
8756 sec->gc_mark = 1;
8758 /* Mark all the sections in the group. */
8759 group_sec = elf_section_data (sec)->next_in_group;
8760 if (group_sec && !group_sec->gc_mark)
8761 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8762 return FALSE;
8764 /* Look through the section relocs. */
8765 ret = TRUE;
8766 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8767 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8769 Elf_Internal_Rela *relstart, *rel, *relend;
8770 Elf_Internal_Shdr *symtab_hdr;
8771 struct elf_link_hash_entry **sym_hashes;
8772 size_t nlocsyms;
8773 size_t extsymoff;
8774 bfd *input_bfd = sec->owner;
8775 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8776 Elf_Internal_Sym *isym = NULL;
8777 int r_sym_shift;
8779 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8780 sym_hashes = elf_sym_hashes (input_bfd);
8782 /* Read the local symbols. */
8783 if (elf_bad_symtab (input_bfd))
8785 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8786 extsymoff = 0;
8788 else
8789 extsymoff = nlocsyms = symtab_hdr->sh_info;
8791 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8792 if (isym == NULL && nlocsyms != 0)
8794 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8795 NULL, NULL, NULL);
8796 if (isym == NULL)
8797 return FALSE;
8800 /* Read the relocations. */
8801 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8802 info->keep_memory);
8803 if (relstart == NULL)
8805 ret = FALSE;
8806 goto out1;
8808 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8810 if (bed->s->arch_size == 32)
8811 r_sym_shift = 8;
8812 else
8813 r_sym_shift = 32;
8815 for (rel = relstart; rel < relend; rel++)
8817 unsigned long r_symndx;
8818 asection *rsec;
8819 struct elf_link_hash_entry *h;
8821 r_symndx = rel->r_info >> r_sym_shift;
8822 if (r_symndx == 0)
8823 continue;
8825 if (r_symndx >= nlocsyms
8826 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8828 h = sym_hashes[r_symndx - extsymoff];
8829 while (h->root.type == bfd_link_hash_indirect
8830 || h->root.type == bfd_link_hash_warning)
8831 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8832 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8834 else
8836 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8839 if (rsec && !rsec->gc_mark)
8841 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8842 rsec->gc_mark = 1;
8843 else if (is_eh)
8844 rsec->gc_mark_from_eh = 1;
8845 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8847 ret = FALSE;
8848 goto out2;
8853 out2:
8854 if (elf_section_data (sec)->relocs != relstart)
8855 free (relstart);
8856 out1:
8857 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8859 if (! info->keep_memory)
8860 free (isym);
8861 else
8862 symtab_hdr->contents = (unsigned char *) isym;
8866 return ret;
8869 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8871 struct elf_gc_sweep_symbol_info {
8872 struct bfd_link_info *info;
8873 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8874 bfd_boolean);
8877 static bfd_boolean
8878 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8880 if (h->root.type == bfd_link_hash_warning)
8881 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8883 if ((h->root.type == bfd_link_hash_defined
8884 || h->root.type == bfd_link_hash_defweak)
8885 && !h->root.u.def.section->gc_mark
8886 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8888 struct elf_gc_sweep_symbol_info *inf = data;
8889 (*inf->hide_symbol) (inf->info, h, TRUE);
8892 return TRUE;
8895 /* The sweep phase of garbage collection. Remove all garbage sections. */
8897 typedef bfd_boolean (*gc_sweep_hook_fn)
8898 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8900 static bfd_boolean
8901 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8903 bfd *sub;
8904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8905 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8906 unsigned long section_sym_count;
8907 struct elf_gc_sweep_symbol_info sweep_info;
8909 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8911 asection *o;
8913 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8914 continue;
8916 for (o = sub->sections; o != NULL; o = o->next)
8918 /* Keep debug and special sections. */
8919 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8920 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8921 o->gc_mark = 1;
8923 if (o->gc_mark)
8924 continue;
8926 /* Skip sweeping sections already excluded. */
8927 if (o->flags & SEC_EXCLUDE)
8928 continue;
8930 /* Since this is early in the link process, it is simple
8931 to remove a section from the output. */
8932 o->flags |= SEC_EXCLUDE;
8934 /* But we also have to update some of the relocation
8935 info we collected before. */
8936 if (gc_sweep_hook
8937 && (o->flags & SEC_RELOC) != 0
8938 && o->reloc_count > 0
8939 && !bfd_is_abs_section (o->output_section))
8941 Elf_Internal_Rela *internal_relocs;
8942 bfd_boolean r;
8944 internal_relocs
8945 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8946 info->keep_memory);
8947 if (internal_relocs == NULL)
8948 return FALSE;
8950 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8952 if (elf_section_data (o)->relocs != internal_relocs)
8953 free (internal_relocs);
8955 if (!r)
8956 return FALSE;
8961 /* Remove the symbols that were in the swept sections from the dynamic
8962 symbol table. GCFIXME: Anyone know how to get them out of the
8963 static symbol table as well? */
8964 sweep_info.info = info;
8965 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8966 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8967 &sweep_info);
8969 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
8970 return TRUE;
8973 /* Propagate collected vtable information. This is called through
8974 elf_link_hash_traverse. */
8976 static bfd_boolean
8977 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8979 if (h->root.type == bfd_link_hash_warning)
8980 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8982 /* Those that are not vtables. */
8983 if (h->vtable == NULL || h->vtable->parent == NULL)
8984 return TRUE;
8986 /* Those vtables that do not have parents, we cannot merge. */
8987 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8988 return TRUE;
8990 /* If we've already been done, exit. */
8991 if (h->vtable->used && h->vtable->used[-1])
8992 return TRUE;
8994 /* Make sure the parent's table is up to date. */
8995 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8997 if (h->vtable->used == NULL)
8999 /* None of this table's entries were referenced. Re-use the
9000 parent's table. */
9001 h->vtable->used = h->vtable->parent->vtable->used;
9002 h->vtable->size = h->vtable->parent->vtable->size;
9004 else
9006 size_t n;
9007 bfd_boolean *cu, *pu;
9009 /* Or the parent's entries into ours. */
9010 cu = h->vtable->used;
9011 cu[-1] = TRUE;
9012 pu = h->vtable->parent->vtable->used;
9013 if (pu != NULL)
9015 const struct elf_backend_data *bed;
9016 unsigned int log_file_align;
9018 bed = get_elf_backend_data (h->root.u.def.section->owner);
9019 log_file_align = bed->s->log_file_align;
9020 n = h->vtable->parent->vtable->size >> log_file_align;
9021 while (n--)
9023 if (*pu)
9024 *cu = TRUE;
9025 pu++;
9026 cu++;
9031 return TRUE;
9034 static bfd_boolean
9035 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9037 asection *sec;
9038 bfd_vma hstart, hend;
9039 Elf_Internal_Rela *relstart, *relend, *rel;
9040 const struct elf_backend_data *bed;
9041 unsigned int log_file_align;
9043 if (h->root.type == bfd_link_hash_warning)
9044 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9046 /* Take care of both those symbols that do not describe vtables as
9047 well as those that are not loaded. */
9048 if (h->vtable == NULL || h->vtable->parent == NULL)
9049 return TRUE;
9051 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9052 || h->root.type == bfd_link_hash_defweak);
9054 sec = h->root.u.def.section;
9055 hstart = h->root.u.def.value;
9056 hend = hstart + h->size;
9058 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9059 if (!relstart)
9060 return *(bfd_boolean *) okp = FALSE;
9061 bed = get_elf_backend_data (sec->owner);
9062 log_file_align = bed->s->log_file_align;
9064 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9066 for (rel = relstart; rel < relend; ++rel)
9067 if (rel->r_offset >= hstart && rel->r_offset < hend)
9069 /* If the entry is in use, do nothing. */
9070 if (h->vtable->used
9071 && (rel->r_offset - hstart) < h->vtable->size)
9073 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9074 if (h->vtable->used[entry])
9075 continue;
9077 /* Otherwise, kill it. */
9078 rel->r_offset = rel->r_info = rel->r_addend = 0;
9081 return TRUE;
9084 /* Mark sections containing dynamically referenced symbols. When
9085 building shared libraries, we must assume that any visible symbol is
9086 referenced. */
9088 bfd_boolean
9089 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9091 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9093 if (h->root.type == bfd_link_hash_warning)
9094 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9096 if ((h->root.type == bfd_link_hash_defined
9097 || h->root.type == bfd_link_hash_defweak)
9098 && (h->ref_dynamic
9099 || (!info->executable
9100 && h->def_regular
9101 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9102 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9103 h->root.u.def.section->flags |= SEC_KEEP;
9105 return TRUE;
9108 /* Do mark and sweep of unused sections. */
9110 bfd_boolean
9111 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9113 bfd_boolean ok = TRUE;
9114 bfd *sub;
9115 asection * (*gc_mark_hook)
9116 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9117 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9118 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9120 if (!bed->can_gc_sections
9121 || info->relocatable
9122 || info->emitrelocations
9123 || !is_elf_hash_table (info->hash))
9125 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9126 return TRUE;
9129 /* Apply transitive closure to the vtable entry usage info. */
9130 elf_link_hash_traverse (elf_hash_table (info),
9131 elf_gc_propagate_vtable_entries_used,
9132 &ok);
9133 if (!ok)
9134 return FALSE;
9136 /* Kill the vtable relocations that were not used. */
9137 elf_link_hash_traverse (elf_hash_table (info),
9138 elf_gc_smash_unused_vtentry_relocs,
9139 &ok);
9140 if (!ok)
9141 return FALSE;
9143 /* Mark dynamically referenced symbols. */
9144 if (elf_hash_table (info)->dynamic_sections_created)
9145 elf_link_hash_traverse (elf_hash_table (info),
9146 bed->gc_mark_dynamic_ref,
9147 info);
9149 /* Grovel through relocs to find out who stays ... */
9150 gc_mark_hook = bed->gc_mark_hook;
9151 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9153 asection *o;
9155 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9156 continue;
9158 for (o = sub->sections; o != NULL; o = o->next)
9159 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9160 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9161 return FALSE;
9164 /* ... again for sections marked from eh_frame. */
9165 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9167 asection *o;
9169 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9170 continue;
9172 /* Keep .gcc_except_table.* if the associated .text.* is
9173 marked. This isn't very nice, but the proper solution,
9174 splitting .eh_frame up and using comdat doesn't pan out
9175 easily due to needing special relocs to handle the
9176 difference of two symbols in separate sections.
9177 Don't keep code sections referenced by .eh_frame. */
9178 for (o = sub->sections; o != NULL; o = o->next)
9179 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9181 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9183 unsigned long len;
9184 char *fn_name;
9185 asection *fn_text;
9187 len = strlen (o->name + 18) + 1;
9188 fn_name = bfd_malloc (len + 6);
9189 if (fn_name == NULL)
9190 return FALSE;
9191 memcpy (fn_name, ".text.", 6);
9192 memcpy (fn_name + 6, o->name + 18, len);
9193 fn_text = bfd_get_section_by_name (sub, fn_name);
9194 free (fn_name);
9195 if (fn_text == NULL || !fn_text->gc_mark)
9196 continue;
9199 /* If not using specially named exception table section,
9200 then keep whatever we are using. */
9201 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9202 return FALSE;
9206 /* ... and mark SEC_EXCLUDE for those that go. */
9207 return elf_gc_sweep (abfd, info);
9210 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9212 bfd_boolean
9213 bfd_elf_gc_record_vtinherit (bfd *abfd,
9214 asection *sec,
9215 struct elf_link_hash_entry *h,
9216 bfd_vma offset)
9218 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9219 struct elf_link_hash_entry **search, *child;
9220 bfd_size_type extsymcount;
9221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9223 /* The sh_info field of the symtab header tells us where the
9224 external symbols start. We don't care about the local symbols at
9225 this point. */
9226 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9227 if (!elf_bad_symtab (abfd))
9228 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9230 sym_hashes = elf_sym_hashes (abfd);
9231 sym_hashes_end = sym_hashes + extsymcount;
9233 /* Hunt down the child symbol, which is in this section at the same
9234 offset as the relocation. */
9235 for (search = sym_hashes; search != sym_hashes_end; ++search)
9237 if ((child = *search) != NULL
9238 && (child->root.type == bfd_link_hash_defined
9239 || child->root.type == bfd_link_hash_defweak)
9240 && child->root.u.def.section == sec
9241 && child->root.u.def.value == offset)
9242 goto win;
9245 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9246 abfd, sec, (unsigned long) offset);
9247 bfd_set_error (bfd_error_invalid_operation);
9248 return FALSE;
9250 win:
9251 if (!child->vtable)
9253 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9254 if (!child->vtable)
9255 return FALSE;
9257 if (!h)
9259 /* This *should* only be the absolute section. It could potentially
9260 be that someone has defined a non-global vtable though, which
9261 would be bad. It isn't worth paging in the local symbols to be
9262 sure though; that case should simply be handled by the assembler. */
9264 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9266 else
9267 child->vtable->parent = h;
9269 return TRUE;
9272 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9274 bfd_boolean
9275 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9276 asection *sec ATTRIBUTE_UNUSED,
9277 struct elf_link_hash_entry *h,
9278 bfd_vma addend)
9280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9281 unsigned int log_file_align = bed->s->log_file_align;
9283 if (!h->vtable)
9285 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9286 if (!h->vtable)
9287 return FALSE;
9290 if (addend >= h->vtable->size)
9292 size_t size, bytes, file_align;
9293 bfd_boolean *ptr = h->vtable->used;
9295 /* While the symbol is undefined, we have to be prepared to handle
9296 a zero size. */
9297 file_align = 1 << log_file_align;
9298 if (h->root.type == bfd_link_hash_undefined)
9299 size = addend + file_align;
9300 else
9302 size = h->size;
9303 if (addend >= size)
9305 /* Oops! We've got a reference past the defined end of
9306 the table. This is probably a bug -- shall we warn? */
9307 size = addend + file_align;
9310 size = (size + file_align - 1) & -file_align;
9312 /* Allocate one extra entry for use as a "done" flag for the
9313 consolidation pass. */
9314 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9316 if (ptr)
9318 ptr = bfd_realloc (ptr - 1, bytes);
9320 if (ptr != NULL)
9322 size_t oldbytes;
9324 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9325 * sizeof (bfd_boolean));
9326 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9329 else
9330 ptr = bfd_zmalloc (bytes);
9332 if (ptr == NULL)
9333 return FALSE;
9335 /* And arrange for that done flag to be at index -1. */
9336 h->vtable->used = ptr + 1;
9337 h->vtable->size = size;
9340 h->vtable->used[addend >> log_file_align] = TRUE;
9342 return TRUE;
9345 struct alloc_got_off_arg {
9346 bfd_vma gotoff;
9347 unsigned int got_elt_size;
9350 /* We need a special top-level link routine to convert got reference counts
9351 to real got offsets. */
9353 static bfd_boolean
9354 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9356 struct alloc_got_off_arg *gofarg = arg;
9358 if (h->root.type == bfd_link_hash_warning)
9359 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9361 if (h->got.refcount > 0)
9363 h->got.offset = gofarg->gotoff;
9364 gofarg->gotoff += gofarg->got_elt_size;
9366 else
9367 h->got.offset = (bfd_vma) -1;
9369 return TRUE;
9372 /* And an accompanying bit to work out final got entry offsets once
9373 we're done. Should be called from final_link. */
9375 bfd_boolean
9376 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9377 struct bfd_link_info *info)
9379 bfd *i;
9380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9381 bfd_vma gotoff;
9382 unsigned int got_elt_size = bed->s->arch_size / 8;
9383 struct alloc_got_off_arg gofarg;
9385 if (! is_elf_hash_table (info->hash))
9386 return FALSE;
9388 /* The GOT offset is relative to the .got section, but the GOT header is
9389 put into the .got.plt section, if the backend uses it. */
9390 if (bed->want_got_plt)
9391 gotoff = 0;
9392 else
9393 gotoff = bed->got_header_size;
9395 /* Do the local .got entries first. */
9396 for (i = info->input_bfds; i; i = i->link_next)
9398 bfd_signed_vma *local_got;
9399 bfd_size_type j, locsymcount;
9400 Elf_Internal_Shdr *symtab_hdr;
9402 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9403 continue;
9405 local_got = elf_local_got_refcounts (i);
9406 if (!local_got)
9407 continue;
9409 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9410 if (elf_bad_symtab (i))
9411 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9412 else
9413 locsymcount = symtab_hdr->sh_info;
9415 for (j = 0; j < locsymcount; ++j)
9417 if (local_got[j] > 0)
9419 local_got[j] = gotoff;
9420 gotoff += got_elt_size;
9422 else
9423 local_got[j] = (bfd_vma) -1;
9427 /* Then the global .got entries. .plt refcounts are handled by
9428 adjust_dynamic_symbol */
9429 gofarg.gotoff = gotoff;
9430 gofarg.got_elt_size = got_elt_size;
9431 elf_link_hash_traverse (elf_hash_table (info),
9432 elf_gc_allocate_got_offsets,
9433 &gofarg);
9434 return TRUE;
9437 /* Many folk need no more in the way of final link than this, once
9438 got entry reference counting is enabled. */
9440 bfd_boolean
9441 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9443 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9444 return FALSE;
9446 /* Invoke the regular ELF backend linker to do all the work. */
9447 return bfd_elf_final_link (abfd, info);
9450 bfd_boolean
9451 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9453 struct elf_reloc_cookie *rcookie = cookie;
9455 if (rcookie->bad_symtab)
9456 rcookie->rel = rcookie->rels;
9458 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9460 unsigned long r_symndx;
9462 if (! rcookie->bad_symtab)
9463 if (rcookie->rel->r_offset > offset)
9464 return FALSE;
9465 if (rcookie->rel->r_offset != offset)
9466 continue;
9468 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9469 if (r_symndx == SHN_UNDEF)
9470 return TRUE;
9472 if (r_symndx >= rcookie->locsymcount
9473 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9475 struct elf_link_hash_entry *h;
9477 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9479 while (h->root.type == bfd_link_hash_indirect
9480 || h->root.type == bfd_link_hash_warning)
9481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9483 if ((h->root.type == bfd_link_hash_defined
9484 || h->root.type == bfd_link_hash_defweak)
9485 && elf_discarded_section (h->root.u.def.section))
9486 return TRUE;
9487 else
9488 return FALSE;
9490 else
9492 /* It's not a relocation against a global symbol,
9493 but it could be a relocation against a local
9494 symbol for a discarded section. */
9495 asection *isec;
9496 Elf_Internal_Sym *isym;
9498 /* Need to: get the symbol; get the section. */
9499 isym = &rcookie->locsyms[r_symndx];
9500 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9502 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9503 if (isec != NULL && elf_discarded_section (isec))
9504 return TRUE;
9507 return FALSE;
9509 return FALSE;
9512 /* Discard unneeded references to discarded sections.
9513 Returns TRUE if any section's size was changed. */
9514 /* This function assumes that the relocations are in sorted order,
9515 which is true for all known assemblers. */
9517 bfd_boolean
9518 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9520 struct elf_reloc_cookie cookie;
9521 asection *stab, *eh;
9522 Elf_Internal_Shdr *symtab_hdr;
9523 const struct elf_backend_data *bed;
9524 bfd *abfd;
9525 unsigned int count;
9526 bfd_boolean ret = FALSE;
9528 if (info->traditional_format
9529 || !is_elf_hash_table (info->hash))
9530 return FALSE;
9532 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9534 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9535 continue;
9537 bed = get_elf_backend_data (abfd);
9539 if ((abfd->flags & DYNAMIC) != 0)
9540 continue;
9542 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9543 if (info->relocatable
9544 || (eh != NULL
9545 && (eh->size == 0
9546 || bfd_is_abs_section (eh->output_section))))
9547 eh = NULL;
9549 stab = bfd_get_section_by_name (abfd, ".stab");
9550 if (stab != NULL
9551 && (stab->size == 0
9552 || bfd_is_abs_section (stab->output_section)
9553 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9554 stab = NULL;
9556 if (stab == NULL
9557 && eh == NULL
9558 && bed->elf_backend_discard_info == NULL)
9559 continue;
9561 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9562 cookie.abfd = abfd;
9563 cookie.sym_hashes = elf_sym_hashes (abfd);
9564 cookie.bad_symtab = elf_bad_symtab (abfd);
9565 if (cookie.bad_symtab)
9567 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9568 cookie.extsymoff = 0;
9570 else
9572 cookie.locsymcount = symtab_hdr->sh_info;
9573 cookie.extsymoff = symtab_hdr->sh_info;
9576 if (bed->s->arch_size == 32)
9577 cookie.r_sym_shift = 8;
9578 else
9579 cookie.r_sym_shift = 32;
9581 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9582 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9584 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9585 cookie.locsymcount, 0,
9586 NULL, NULL, NULL);
9587 if (cookie.locsyms == NULL)
9588 return FALSE;
9591 if (stab != NULL)
9593 cookie.rels = NULL;
9594 count = stab->reloc_count;
9595 if (count != 0)
9596 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9597 info->keep_memory);
9598 if (cookie.rels != NULL)
9600 cookie.rel = cookie.rels;
9601 cookie.relend = cookie.rels;
9602 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9603 if (_bfd_discard_section_stabs (abfd, stab,
9604 elf_section_data (stab)->sec_info,
9605 bfd_elf_reloc_symbol_deleted_p,
9606 &cookie))
9607 ret = TRUE;
9608 if (elf_section_data (stab)->relocs != cookie.rels)
9609 free (cookie.rels);
9613 if (eh != NULL)
9615 cookie.rels = NULL;
9616 count = eh->reloc_count;
9617 if (count != 0)
9618 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9619 info->keep_memory);
9620 cookie.rel = cookie.rels;
9621 cookie.relend = cookie.rels;
9622 if (cookie.rels != NULL)
9623 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9625 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9626 bfd_elf_reloc_symbol_deleted_p,
9627 &cookie))
9628 ret = TRUE;
9630 if (cookie.rels != NULL
9631 && elf_section_data (eh)->relocs != cookie.rels)
9632 free (cookie.rels);
9635 if (bed->elf_backend_discard_info != NULL
9636 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9637 ret = TRUE;
9639 if (cookie.locsyms != NULL
9640 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9642 if (! info->keep_memory)
9643 free (cookie.locsyms);
9644 else
9645 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9649 if (info->eh_frame_hdr
9650 && !info->relocatable
9651 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9652 ret = TRUE;
9654 return ret;
9657 void
9658 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9660 flagword flags;
9661 const char *name, *p;
9662 struct bfd_section_already_linked *l;
9663 struct bfd_section_already_linked_hash_entry *already_linked_list;
9664 asection *group;
9666 /* A single member comdat group section may be discarded by a
9667 linkonce section. See below. */
9668 if (sec->output_section == bfd_abs_section_ptr)
9669 return;
9671 flags = sec->flags;
9673 /* Check if it belongs to a section group. */
9674 group = elf_sec_group (sec);
9676 /* Return if it isn't a linkonce section nor a member of a group. A
9677 comdat group section also has SEC_LINK_ONCE set. */
9678 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9679 return;
9681 if (group)
9683 /* If this is the member of a single member comdat group, check if
9684 the group should be discarded. */
9685 if (elf_next_in_group (sec) == sec
9686 && (group->flags & SEC_LINK_ONCE) != 0)
9687 sec = group;
9688 else
9689 return;
9692 /* FIXME: When doing a relocatable link, we may have trouble
9693 copying relocations in other sections that refer to local symbols
9694 in the section being discarded. Those relocations will have to
9695 be converted somehow; as of this writing I'm not sure that any of
9696 the backends handle that correctly.
9698 It is tempting to instead not discard link once sections when
9699 doing a relocatable link (technically, they should be discarded
9700 whenever we are building constructors). However, that fails,
9701 because the linker winds up combining all the link once sections
9702 into a single large link once section, which defeats the purpose
9703 of having link once sections in the first place.
9705 Also, not merging link once sections in a relocatable link
9706 causes trouble for MIPS ELF, which relies on link once semantics
9707 to handle the .reginfo section correctly. */
9709 name = bfd_get_section_name (abfd, sec);
9711 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9712 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9713 p++;
9714 else
9715 p = name;
9717 already_linked_list = bfd_section_already_linked_table_lookup (p);
9719 for (l = already_linked_list->entry; l != NULL; l = l->next)
9721 /* We may have 3 different sections on the list: group section,
9722 comdat section and linkonce section. SEC may be a linkonce or
9723 group section. We match a group section with a group section,
9724 a linkonce section with a linkonce section, and ignore comdat
9725 section. */
9726 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9727 && strcmp (name, l->sec->name) == 0
9728 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9730 /* The section has already been linked. See if we should
9731 issue a warning. */
9732 switch (flags & SEC_LINK_DUPLICATES)
9734 default:
9735 abort ();
9737 case SEC_LINK_DUPLICATES_DISCARD:
9738 break;
9740 case SEC_LINK_DUPLICATES_ONE_ONLY:
9741 (*_bfd_error_handler)
9742 (_("%B: ignoring duplicate section `%A'"),
9743 abfd, sec);
9744 break;
9746 case SEC_LINK_DUPLICATES_SAME_SIZE:
9747 if (sec->size != l->sec->size)
9748 (*_bfd_error_handler)
9749 (_("%B: duplicate section `%A' has different size"),
9750 abfd, sec);
9751 break;
9753 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9754 if (sec->size != l->sec->size)
9755 (*_bfd_error_handler)
9756 (_("%B: duplicate section `%A' has different size"),
9757 abfd, sec);
9758 else if (sec->size != 0)
9760 bfd_byte *sec_contents, *l_sec_contents;
9762 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9763 (*_bfd_error_handler)
9764 (_("%B: warning: could not read contents of section `%A'"),
9765 abfd, sec);
9766 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9767 &l_sec_contents))
9768 (*_bfd_error_handler)
9769 (_("%B: warning: could not read contents of section `%A'"),
9770 l->sec->owner, l->sec);
9771 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9772 (*_bfd_error_handler)
9773 (_("%B: warning: duplicate section `%A' has different contents"),
9774 abfd, sec);
9776 if (sec_contents)
9777 free (sec_contents);
9778 if (l_sec_contents)
9779 free (l_sec_contents);
9781 break;
9784 /* Set the output_section field so that lang_add_section
9785 does not create a lang_input_section structure for this
9786 section. Since there might be a symbol in the section
9787 being discarded, we must retain a pointer to the section
9788 which we are really going to use. */
9789 sec->output_section = bfd_abs_section_ptr;
9790 sec->kept_section = l->sec;
9792 if (flags & SEC_GROUP)
9794 asection *first = elf_next_in_group (sec);
9795 asection *s = first;
9797 while (s != NULL)
9799 s->output_section = bfd_abs_section_ptr;
9800 /* Record which group discards it. */
9801 s->kept_section = l->sec;
9802 s = elf_next_in_group (s);
9803 /* These lists are circular. */
9804 if (s == first)
9805 break;
9809 return;
9813 if (group)
9815 /* If this is the member of a single member comdat group and the
9816 group hasn't be discarded, we check if it matches a linkonce
9817 section. We only record the discarded comdat group. Otherwise
9818 the undiscarded group will be discarded incorrectly later since
9819 itself has been recorded. */
9820 for (l = already_linked_list->entry; l != NULL; l = l->next)
9821 if ((l->sec->flags & SEC_GROUP) == 0
9822 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9823 && bfd_elf_match_symbols_in_sections (l->sec,
9824 elf_next_in_group (sec)))
9826 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9827 elf_next_in_group (sec)->kept_section = l->sec;
9828 group->output_section = bfd_abs_section_ptr;
9829 break;
9831 if (l == NULL)
9832 return;
9834 else
9835 /* There is no direct match. But for linkonce section, we should
9836 check if there is a match with comdat group member. We always
9837 record the linkonce section, discarded or not. */
9838 for (l = already_linked_list->entry; l != NULL; l = l->next)
9839 if (l->sec->flags & SEC_GROUP)
9841 asection *first = elf_next_in_group (l->sec);
9843 if (first != NULL
9844 && elf_next_in_group (first) == first
9845 && bfd_elf_match_symbols_in_sections (first, sec))
9847 sec->output_section = bfd_abs_section_ptr;
9848 sec->kept_section = l->sec;
9849 break;
9853 /* This is the first section with this name. Record it. */
9854 bfd_section_already_linked_table_insert (already_linked_list, sec);
9857 bfd_boolean
9858 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9860 return sym->st_shndx == SHN_COMMON;
9863 unsigned int
9864 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9866 return SHN_COMMON;
9869 asection *
9870 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9872 return bfd_com_section_ptr;