Configury changes: update src repository (binutils, gdb, and rda) to use
[binutils.git] / bfd / elflink.c
blobdf2b7e9fb7a7222e0d22a6585ba62fb1e0e06c45
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 /* When we try to create a default indirect symbol from the dynamic
893 definition with the default version, we skip it if its type and
894 the type of existing regular definition mismatch. We only do it
895 if the existing regular definition won't be dynamic. */
896 if (pold_alignment == NULL
897 && !info->shared
898 && !info->export_dynamic
899 && !h->ref_dynamic
900 && newdyn
901 && newdef
902 && !olddyn
903 && (olddef || h->root.type == bfd_link_hash_common)
904 && ELF_ST_TYPE (sym->st_info) != h->type
905 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
906 && h->type != STT_NOTYPE)
908 *skip = TRUE;
909 return TRUE;
912 /* Check TLS symbol. We don't check undefined symbol introduced by
913 "ld -u". */
914 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
915 && ELF_ST_TYPE (sym->st_info) != h->type
916 && oldbfd != NULL)
918 bfd *ntbfd, *tbfd;
919 bfd_boolean ntdef, tdef;
920 asection *ntsec, *tsec;
922 if (h->type == STT_TLS)
924 ntbfd = abfd;
925 ntsec = sec;
926 ntdef = newdef;
927 tbfd = oldbfd;
928 tsec = oldsec;
929 tdef = olddef;
931 else
933 ntbfd = oldbfd;
934 ntsec = oldsec;
935 ntdef = olddef;
936 tbfd = abfd;
937 tsec = sec;
938 tdef = newdef;
941 if (tdef && ntdef)
942 (*_bfd_error_handler)
943 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
944 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
945 else if (!tdef && !ntdef)
946 (*_bfd_error_handler)
947 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
948 tbfd, ntbfd, h->root.root.string);
949 else if (tdef)
950 (*_bfd_error_handler)
951 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
952 tbfd, tsec, ntbfd, h->root.root.string);
953 else
954 (*_bfd_error_handler)
955 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
956 tbfd, ntbfd, ntsec, h->root.root.string);
958 bfd_set_error (bfd_error_bad_value);
959 return FALSE;
962 /* We need to remember if a symbol has a definition in a dynamic
963 object or is weak in all dynamic objects. Internal and hidden
964 visibility will make it unavailable to dynamic objects. */
965 if (newdyn && !h->dynamic_def)
967 if (!bfd_is_und_section (sec))
968 h->dynamic_def = 1;
969 else
971 /* Check if this symbol is weak in all dynamic objects. If it
972 is the first time we see it in a dynamic object, we mark
973 if it is weak. Otherwise, we clear it. */
974 if (!h->ref_dynamic)
976 if (bind == STB_WEAK)
977 h->dynamic_weak = 1;
979 else if (bind != STB_WEAK)
980 h->dynamic_weak = 0;
984 /* If the old symbol has non-default visibility, we ignore the new
985 definition from a dynamic object. */
986 if (newdyn
987 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
988 && !bfd_is_und_section (sec))
990 *skip = TRUE;
991 /* Make sure this symbol is dynamic. */
992 h->ref_dynamic = 1;
993 /* A protected symbol has external availability. Make sure it is
994 recorded as dynamic.
996 FIXME: Should we check type and size for protected symbol? */
997 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
998 return bfd_elf_link_record_dynamic_symbol (info, h);
999 else
1000 return TRUE;
1002 else if (!newdyn
1003 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1004 && h->def_dynamic)
1006 /* If the new symbol with non-default visibility comes from a
1007 relocatable file and the old definition comes from a dynamic
1008 object, we remove the old definition. */
1009 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1010 h = *sym_hash;
1012 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1013 && bfd_is_und_section (sec))
1015 /* If the new symbol is undefined and the old symbol was
1016 also undefined before, we need to make sure
1017 _bfd_generic_link_add_one_symbol doesn't mess
1018 up the linker hash table undefs list. Since the old
1019 definition came from a dynamic object, it is still on the
1020 undefs list. */
1021 h->root.type = bfd_link_hash_undefined;
1022 h->root.u.undef.abfd = abfd;
1024 else
1026 h->root.type = bfd_link_hash_new;
1027 h->root.u.undef.abfd = NULL;
1030 if (h->def_dynamic)
1032 h->def_dynamic = 0;
1033 h->ref_dynamic = 1;
1034 h->dynamic_def = 1;
1036 /* FIXME: Should we check type and size for protected symbol? */
1037 h->size = 0;
1038 h->type = 0;
1039 return TRUE;
1042 /* Differentiate strong and weak symbols. */
1043 newweak = bind == STB_WEAK;
1044 oldweak = (h->root.type == bfd_link_hash_defweak
1045 || h->root.type == bfd_link_hash_undefweak);
1047 /* If a new weak symbol definition comes from a regular file and the
1048 old symbol comes from a dynamic library, we treat the new one as
1049 strong. Similarly, an old weak symbol definition from a regular
1050 file is treated as strong when the new symbol comes from a dynamic
1051 library. Further, an old weak symbol from a dynamic library is
1052 treated as strong if the new symbol is from a dynamic library.
1053 This reflects the way glibc's ld.so works.
1055 Do this before setting *type_change_ok or *size_change_ok so that
1056 we warn properly when dynamic library symbols are overridden. */
1058 if (newdef && !newdyn && olddyn)
1059 newweak = FALSE;
1060 if (olddef && newdyn)
1061 oldweak = FALSE;
1063 /* It's OK to change the type if either the existing symbol or the
1064 new symbol is weak. A type change is also OK if the old symbol
1065 is undefined and the new symbol is defined. */
1067 if (oldweak
1068 || newweak
1069 || (newdef
1070 && h->root.type == bfd_link_hash_undefined))
1071 *type_change_ok = TRUE;
1073 /* It's OK to change the size if either the existing symbol or the
1074 new symbol is weak, or if the old symbol is undefined. */
1076 if (*type_change_ok
1077 || h->root.type == bfd_link_hash_undefined)
1078 *size_change_ok = TRUE;
1080 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1081 symbol, respectively, appears to be a common symbol in a dynamic
1082 object. If a symbol appears in an uninitialized section, and is
1083 not weak, and is not a function, then it may be a common symbol
1084 which was resolved when the dynamic object was created. We want
1085 to treat such symbols specially, because they raise special
1086 considerations when setting the symbol size: if the symbol
1087 appears as a common symbol in a regular object, and the size in
1088 the regular object is larger, we must make sure that we use the
1089 larger size. This problematic case can always be avoided in C,
1090 but it must be handled correctly when using Fortran shared
1091 libraries.
1093 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1094 likewise for OLDDYNCOMMON and OLDDEF.
1096 Note that this test is just a heuristic, and that it is quite
1097 possible to have an uninitialized symbol in a shared object which
1098 is really a definition, rather than a common symbol. This could
1099 lead to some minor confusion when the symbol really is a common
1100 symbol in some regular object. However, I think it will be
1101 harmless. */
1103 if (newdyn
1104 && newdef
1105 && !newweak
1106 && (sec->flags & SEC_ALLOC) != 0
1107 && (sec->flags & SEC_LOAD) == 0
1108 && sym->st_size > 0
1109 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1110 newdyncommon = TRUE;
1111 else
1112 newdyncommon = FALSE;
1114 if (olddyn
1115 && olddef
1116 && h->root.type == bfd_link_hash_defined
1117 && h->def_dynamic
1118 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1119 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1120 && h->size > 0
1121 && h->type != STT_FUNC)
1122 olddyncommon = TRUE;
1123 else
1124 olddyncommon = FALSE;
1126 /* We now know everything about the old and new symbols. We ask the
1127 backend to check if we can merge them. */
1128 bed = get_elf_backend_data (abfd);
1129 if (bed->merge_symbol
1130 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1131 pold_alignment, skip, override,
1132 type_change_ok, size_change_ok,
1133 &newdyn, &newdef, &newdyncommon, &newweak,
1134 abfd, &sec,
1135 &olddyn, &olddef, &olddyncommon, &oldweak,
1136 oldbfd, &oldsec))
1137 return FALSE;
1139 /* If both the old and the new symbols look like common symbols in a
1140 dynamic object, set the size of the symbol to the larger of the
1141 two. */
1143 if (olddyncommon
1144 && newdyncommon
1145 && sym->st_size != h->size)
1147 /* Since we think we have two common symbols, issue a multiple
1148 common warning if desired. Note that we only warn if the
1149 size is different. If the size is the same, we simply let
1150 the old symbol override the new one as normally happens with
1151 symbols defined in dynamic objects. */
1153 if (! ((*info->callbacks->multiple_common)
1154 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1155 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1156 return FALSE;
1158 if (sym->st_size > h->size)
1159 h->size = sym->st_size;
1161 *size_change_ok = TRUE;
1164 /* If we are looking at a dynamic object, and we have found a
1165 definition, we need to see if the symbol was already defined by
1166 some other object. If so, we want to use the existing
1167 definition, and we do not want to report a multiple symbol
1168 definition error; we do this by clobbering *PSEC to be
1169 bfd_und_section_ptr.
1171 We treat a common symbol as a definition if the symbol in the
1172 shared library is a function, since common symbols always
1173 represent variables; this can cause confusion in principle, but
1174 any such confusion would seem to indicate an erroneous program or
1175 shared library. We also permit a common symbol in a regular
1176 object to override a weak symbol in a shared object. */
1178 if (newdyn
1179 && newdef
1180 && (olddef
1181 || (h->root.type == bfd_link_hash_common
1182 && (newweak
1183 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1185 *override = TRUE;
1186 newdef = FALSE;
1187 newdyncommon = FALSE;
1189 *psec = sec = bfd_und_section_ptr;
1190 *size_change_ok = TRUE;
1192 /* If we get here when the old symbol is a common symbol, then
1193 we are explicitly letting it override a weak symbol or
1194 function in a dynamic object, and we don't want to warn about
1195 a type change. If the old symbol is a defined symbol, a type
1196 change warning may still be appropriate. */
1198 if (h->root.type == bfd_link_hash_common)
1199 *type_change_ok = TRUE;
1202 /* Handle the special case of an old common symbol merging with a
1203 new symbol which looks like a common symbol in a shared object.
1204 We change *PSEC and *PVALUE to make the new symbol look like a
1205 common symbol, and let _bfd_generic_link_add_one_symbol do the
1206 right thing. */
1208 if (newdyncommon
1209 && h->root.type == bfd_link_hash_common)
1211 *override = TRUE;
1212 newdef = FALSE;
1213 newdyncommon = FALSE;
1214 *pvalue = sym->st_size;
1215 *psec = sec = bed->common_section (oldsec);
1216 *size_change_ok = TRUE;
1219 /* Skip weak definitions of symbols that are already defined. */
1220 if (newdef && olddef && newweak)
1221 *skip = TRUE;
1223 /* If the old symbol is from a dynamic object, and the new symbol is
1224 a definition which is not from a dynamic object, then the new
1225 symbol overrides the old symbol. Symbols from regular files
1226 always take precedence over symbols from dynamic objects, even if
1227 they are defined after the dynamic object in the link.
1229 As above, we again permit a common symbol in a regular object to
1230 override a definition in a shared object if the shared object
1231 symbol is a function or is weak. */
1233 flip = NULL;
1234 if (!newdyn
1235 && (newdef
1236 || (bfd_is_com_section (sec)
1237 && (oldweak
1238 || h->type == STT_FUNC)))
1239 && olddyn
1240 && olddef
1241 && h->def_dynamic)
1243 /* Change the hash table entry to undefined, and let
1244 _bfd_generic_link_add_one_symbol do the right thing with the
1245 new definition. */
1247 h->root.type = bfd_link_hash_undefined;
1248 h->root.u.undef.abfd = h->root.u.def.section->owner;
1249 *size_change_ok = TRUE;
1251 olddef = FALSE;
1252 olddyncommon = FALSE;
1254 /* We again permit a type change when a common symbol may be
1255 overriding a function. */
1257 if (bfd_is_com_section (sec))
1258 *type_change_ok = TRUE;
1260 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1261 flip = *sym_hash;
1262 else
1263 /* This union may have been set to be non-NULL when this symbol
1264 was seen in a dynamic object. We must force the union to be
1265 NULL, so that it is correct for a regular symbol. */
1266 h->verinfo.vertree = NULL;
1269 /* Handle the special case of a new common symbol merging with an
1270 old symbol that looks like it might be a common symbol defined in
1271 a shared object. Note that we have already handled the case in
1272 which a new common symbol should simply override the definition
1273 in the shared library. */
1275 if (! newdyn
1276 && bfd_is_com_section (sec)
1277 && olddyncommon)
1279 /* It would be best if we could set the hash table entry to a
1280 common symbol, but we don't know what to use for the section
1281 or the alignment. */
1282 if (! ((*info->callbacks->multiple_common)
1283 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1284 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1285 return FALSE;
1287 /* If the presumed common symbol in the dynamic object is
1288 larger, pretend that the new symbol has its size. */
1290 if (h->size > *pvalue)
1291 *pvalue = h->size;
1293 /* We need to remember the alignment required by the symbol
1294 in the dynamic object. */
1295 BFD_ASSERT (pold_alignment);
1296 *pold_alignment = h->root.u.def.section->alignment_power;
1298 olddef = FALSE;
1299 olddyncommon = FALSE;
1301 h->root.type = bfd_link_hash_undefined;
1302 h->root.u.undef.abfd = h->root.u.def.section->owner;
1304 *size_change_ok = TRUE;
1305 *type_change_ok = TRUE;
1307 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1308 flip = *sym_hash;
1309 else
1310 h->verinfo.vertree = NULL;
1313 if (flip != NULL)
1315 /* Handle the case where we had a versioned symbol in a dynamic
1316 library and now find a definition in a normal object. In this
1317 case, we make the versioned symbol point to the normal one. */
1318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1319 flip->root.type = h->root.type;
1320 h->root.type = bfd_link_hash_indirect;
1321 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1322 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1323 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1324 if (h->def_dynamic)
1326 h->def_dynamic = 0;
1327 flip->ref_dynamic = 1;
1331 return TRUE;
1334 /* This function is called to create an indirect symbol from the
1335 default for the symbol with the default version if needed. The
1336 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1337 set DYNSYM if the new indirect symbol is dynamic. */
1339 bfd_boolean
1340 _bfd_elf_add_default_symbol (bfd *abfd,
1341 struct bfd_link_info *info,
1342 struct elf_link_hash_entry *h,
1343 const char *name,
1344 Elf_Internal_Sym *sym,
1345 asection **psec,
1346 bfd_vma *value,
1347 bfd_boolean *dynsym,
1348 bfd_boolean override)
1350 bfd_boolean type_change_ok;
1351 bfd_boolean size_change_ok;
1352 bfd_boolean skip;
1353 char *shortname;
1354 struct elf_link_hash_entry *hi;
1355 struct bfd_link_hash_entry *bh;
1356 const struct elf_backend_data *bed;
1357 bfd_boolean collect;
1358 bfd_boolean dynamic;
1359 char *p;
1360 size_t len, shortlen;
1361 asection *sec;
1363 /* If this symbol has a version, and it is the default version, we
1364 create an indirect symbol from the default name to the fully
1365 decorated name. This will cause external references which do not
1366 specify a version to be bound to this version of the symbol. */
1367 p = strchr (name, ELF_VER_CHR);
1368 if (p == NULL || p[1] != ELF_VER_CHR)
1369 return TRUE;
1371 if (override)
1373 /* We are overridden by an old definition. We need to check if we
1374 need to create the indirect symbol from the default name. */
1375 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1376 FALSE, FALSE);
1377 BFD_ASSERT (hi != NULL);
1378 if (hi == h)
1379 return TRUE;
1380 while (hi->root.type == bfd_link_hash_indirect
1381 || hi->root.type == bfd_link_hash_warning)
1383 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1384 if (hi == h)
1385 return TRUE;
1389 bed = get_elf_backend_data (abfd);
1390 collect = bed->collect;
1391 dynamic = (abfd->flags & DYNAMIC) != 0;
1393 shortlen = p - name;
1394 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1395 if (shortname == NULL)
1396 return FALSE;
1397 memcpy (shortname, name, shortlen);
1398 shortname[shortlen] = '\0';
1400 /* We are going to create a new symbol. Merge it with any existing
1401 symbol with this name. For the purposes of the merge, act as
1402 though we were defining the symbol we just defined, although we
1403 actually going to define an indirect symbol. */
1404 type_change_ok = FALSE;
1405 size_change_ok = FALSE;
1406 sec = *psec;
1407 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1408 NULL, &hi, &skip, &override,
1409 &type_change_ok, &size_change_ok))
1410 return FALSE;
1412 if (skip)
1413 goto nondefault;
1415 if (! override)
1417 bh = &hi->root;
1418 if (! (_bfd_generic_link_add_one_symbol
1419 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1420 0, name, FALSE, collect, &bh)))
1421 return FALSE;
1422 hi = (struct elf_link_hash_entry *) bh;
1424 else
1426 /* In this case the symbol named SHORTNAME is overriding the
1427 indirect symbol we want to add. We were planning on making
1428 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1429 is the name without a version. NAME is the fully versioned
1430 name, and it is the default version.
1432 Overriding means that we already saw a definition for the
1433 symbol SHORTNAME in a regular object, and it is overriding
1434 the symbol defined in the dynamic object.
1436 When this happens, we actually want to change NAME, the
1437 symbol we just added, to refer to SHORTNAME. This will cause
1438 references to NAME in the shared object to become references
1439 to SHORTNAME in the regular object. This is what we expect
1440 when we override a function in a shared object: that the
1441 references in the shared object will be mapped to the
1442 definition in the regular object. */
1444 while (hi->root.type == bfd_link_hash_indirect
1445 || hi->root.type == bfd_link_hash_warning)
1446 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1448 h->root.type = bfd_link_hash_indirect;
1449 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1450 if (h->def_dynamic)
1452 h->def_dynamic = 0;
1453 hi->ref_dynamic = 1;
1454 if (hi->ref_regular
1455 || hi->def_regular)
1457 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1458 return FALSE;
1462 /* Now set HI to H, so that the following code will set the
1463 other fields correctly. */
1464 hi = h;
1467 /* If there is a duplicate definition somewhere, then HI may not
1468 point to an indirect symbol. We will have reported an error to
1469 the user in that case. */
1471 if (hi->root.type == bfd_link_hash_indirect)
1473 struct elf_link_hash_entry *ht;
1475 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1476 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1478 /* See if the new flags lead us to realize that the symbol must
1479 be dynamic. */
1480 if (! *dynsym)
1482 if (! dynamic)
1484 if (info->shared
1485 || hi->ref_dynamic)
1486 *dynsym = TRUE;
1488 else
1490 if (hi->ref_regular)
1491 *dynsym = TRUE;
1496 /* We also need to define an indirection from the nondefault version
1497 of the symbol. */
1499 nondefault:
1500 len = strlen (name);
1501 shortname = bfd_hash_allocate (&info->hash->table, len);
1502 if (shortname == NULL)
1503 return FALSE;
1504 memcpy (shortname, name, shortlen);
1505 memcpy (shortname + shortlen, p + 1, len - shortlen);
1507 /* Once again, merge with any existing symbol. */
1508 type_change_ok = FALSE;
1509 size_change_ok = FALSE;
1510 sec = *psec;
1511 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1512 NULL, &hi, &skip, &override,
1513 &type_change_ok, &size_change_ok))
1514 return FALSE;
1516 if (skip)
1517 return TRUE;
1519 if (override)
1521 /* Here SHORTNAME is a versioned name, so we don't expect to see
1522 the type of override we do in the case above unless it is
1523 overridden by a versioned definition. */
1524 if (hi->root.type != bfd_link_hash_defined
1525 && hi->root.type != bfd_link_hash_defweak)
1526 (*_bfd_error_handler)
1527 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1528 abfd, shortname);
1530 else
1532 bh = &hi->root;
1533 if (! (_bfd_generic_link_add_one_symbol
1534 (info, abfd, shortname, BSF_INDIRECT,
1535 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1536 return FALSE;
1537 hi = (struct elf_link_hash_entry *) bh;
1539 /* If there is a duplicate definition somewhere, then HI may not
1540 point to an indirect symbol. We will have reported an error
1541 to the user in that case. */
1543 if (hi->root.type == bfd_link_hash_indirect)
1545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1547 /* See if the new flags lead us to realize that the symbol
1548 must be dynamic. */
1549 if (! *dynsym)
1551 if (! dynamic)
1553 if (info->shared
1554 || hi->ref_dynamic)
1555 *dynsym = TRUE;
1557 else
1559 if (hi->ref_regular)
1560 *dynsym = TRUE;
1566 return TRUE;
1569 /* This routine is used to export all defined symbols into the dynamic
1570 symbol table. It is called via elf_link_hash_traverse. */
1572 bfd_boolean
1573 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1575 struct elf_info_failed *eif = data;
1577 /* Ignore indirect symbols. These are added by the versioning code. */
1578 if (h->root.type == bfd_link_hash_indirect)
1579 return TRUE;
1581 if (h->root.type == bfd_link_hash_warning)
1582 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1584 if (h->dynindx == -1
1585 && (h->def_regular
1586 || h->ref_regular))
1588 struct bfd_elf_version_tree *t;
1589 struct bfd_elf_version_expr *d;
1591 for (t = eif->verdefs; t != NULL; t = t->next)
1593 if (t->globals.list != NULL)
1595 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1596 if (d != NULL)
1597 goto doit;
1600 if (t->locals.list != NULL)
1602 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1603 if (d != NULL)
1604 return TRUE;
1608 if (!eif->verdefs)
1610 doit:
1611 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1613 eif->failed = TRUE;
1614 return FALSE;
1619 return TRUE;
1622 /* Look through the symbols which are defined in other shared
1623 libraries and referenced here. Update the list of version
1624 dependencies. This will be put into the .gnu.version_r section.
1625 This function is called via elf_link_hash_traverse. */
1627 bfd_boolean
1628 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1629 void *data)
1631 struct elf_find_verdep_info *rinfo = data;
1632 Elf_Internal_Verneed *t;
1633 Elf_Internal_Vernaux *a;
1634 bfd_size_type amt;
1636 if (h->root.type == bfd_link_hash_warning)
1637 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1639 /* We only care about symbols defined in shared objects with version
1640 information. */
1641 if (!h->def_dynamic
1642 || h->def_regular
1643 || h->dynindx == -1
1644 || h->verinfo.verdef == NULL)
1645 return TRUE;
1647 /* See if we already know about this version. */
1648 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1650 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1651 continue;
1653 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1654 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1655 return TRUE;
1657 break;
1660 /* This is a new version. Add it to tree we are building. */
1662 if (t == NULL)
1664 amt = sizeof *t;
1665 t = bfd_zalloc (rinfo->output_bfd, amt);
1666 if (t == NULL)
1668 rinfo->failed = TRUE;
1669 return FALSE;
1672 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1673 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1674 elf_tdata (rinfo->output_bfd)->verref = t;
1677 amt = sizeof *a;
1678 a = bfd_zalloc (rinfo->output_bfd, amt);
1680 /* Note that we are copying a string pointer here, and testing it
1681 above. If bfd_elf_string_from_elf_section is ever changed to
1682 discard the string data when low in memory, this will have to be
1683 fixed. */
1684 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1686 a->vna_flags = h->verinfo.verdef->vd_flags;
1687 a->vna_nextptr = t->vn_auxptr;
1689 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1690 ++rinfo->vers;
1692 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1694 t->vn_auxptr = a;
1696 return TRUE;
1699 /* Figure out appropriate versions for all the symbols. We may not
1700 have the version number script until we have read all of the input
1701 files, so until that point we don't know which symbols should be
1702 local. This function is called via elf_link_hash_traverse. */
1704 bfd_boolean
1705 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1707 struct elf_assign_sym_version_info *sinfo;
1708 struct bfd_link_info *info;
1709 const struct elf_backend_data *bed;
1710 struct elf_info_failed eif;
1711 char *p;
1712 bfd_size_type amt;
1714 sinfo = data;
1715 info = sinfo->info;
1717 if (h->root.type == bfd_link_hash_warning)
1718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1720 /* Fix the symbol flags. */
1721 eif.failed = FALSE;
1722 eif.info = info;
1723 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1725 if (eif.failed)
1726 sinfo->failed = TRUE;
1727 return FALSE;
1730 /* We only need version numbers for symbols defined in regular
1731 objects. */
1732 if (!h->def_regular)
1733 return TRUE;
1735 bed = get_elf_backend_data (sinfo->output_bfd);
1736 p = strchr (h->root.root.string, ELF_VER_CHR);
1737 if (p != NULL && h->verinfo.vertree == NULL)
1739 struct bfd_elf_version_tree *t;
1740 bfd_boolean hidden;
1742 hidden = TRUE;
1744 /* There are two consecutive ELF_VER_CHR characters if this is
1745 not a hidden symbol. */
1746 ++p;
1747 if (*p == ELF_VER_CHR)
1749 hidden = FALSE;
1750 ++p;
1753 /* If there is no version string, we can just return out. */
1754 if (*p == '\0')
1756 if (hidden)
1757 h->hidden = 1;
1758 return TRUE;
1761 /* Look for the version. If we find it, it is no longer weak. */
1762 for (t = sinfo->verdefs; t != NULL; t = t->next)
1764 if (strcmp (t->name, p) == 0)
1766 size_t len;
1767 char *alc;
1768 struct bfd_elf_version_expr *d;
1770 len = p - h->root.root.string;
1771 alc = bfd_malloc (len);
1772 if (alc == NULL)
1773 return FALSE;
1774 memcpy (alc, h->root.root.string, len - 1);
1775 alc[len - 1] = '\0';
1776 if (alc[len - 2] == ELF_VER_CHR)
1777 alc[len - 2] = '\0';
1779 h->verinfo.vertree = t;
1780 t->used = TRUE;
1781 d = NULL;
1783 if (t->globals.list != NULL)
1784 d = (*t->match) (&t->globals, NULL, alc);
1786 /* See if there is anything to force this symbol to
1787 local scope. */
1788 if (d == NULL && t->locals.list != NULL)
1790 d = (*t->match) (&t->locals, NULL, alc);
1791 if (d != NULL
1792 && h->dynindx != -1
1793 && ! info->export_dynamic)
1794 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1797 free (alc);
1798 break;
1802 /* If we are building an application, we need to create a
1803 version node for this version. */
1804 if (t == NULL && info->executable)
1806 struct bfd_elf_version_tree **pp;
1807 int version_index;
1809 /* If we aren't going to export this symbol, we don't need
1810 to worry about it. */
1811 if (h->dynindx == -1)
1812 return TRUE;
1814 amt = sizeof *t;
1815 t = bfd_zalloc (sinfo->output_bfd, amt);
1816 if (t == NULL)
1818 sinfo->failed = TRUE;
1819 return FALSE;
1822 t->name = p;
1823 t->name_indx = (unsigned int) -1;
1824 t->used = TRUE;
1826 version_index = 1;
1827 /* Don't count anonymous version tag. */
1828 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1829 version_index = 0;
1830 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1831 ++version_index;
1832 t->vernum = version_index;
1834 *pp = t;
1836 h->verinfo.vertree = t;
1838 else if (t == NULL)
1840 /* We could not find the version for a symbol when
1841 generating a shared archive. Return an error. */
1842 (*_bfd_error_handler)
1843 (_("%B: undefined versioned symbol name %s"),
1844 sinfo->output_bfd, h->root.root.string);
1845 bfd_set_error (bfd_error_bad_value);
1846 sinfo->failed = TRUE;
1847 return FALSE;
1850 if (hidden)
1851 h->hidden = 1;
1854 /* If we don't have a version for this symbol, see if we can find
1855 something. */
1856 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1858 struct bfd_elf_version_tree *t;
1859 struct bfd_elf_version_tree *local_ver;
1860 struct bfd_elf_version_expr *d;
1862 /* See if can find what version this symbol is in. If the
1863 symbol is supposed to be local, then don't actually register
1864 it. */
1865 local_ver = NULL;
1866 for (t = sinfo->verdefs; t != NULL; t = t->next)
1868 if (t->globals.list != NULL)
1870 bfd_boolean matched;
1872 matched = FALSE;
1873 d = NULL;
1874 while ((d = (*t->match) (&t->globals, d,
1875 h->root.root.string)) != NULL)
1876 if (d->symver)
1877 matched = TRUE;
1878 else
1880 /* There is a version without definition. Make
1881 the symbol the default definition for this
1882 version. */
1883 h->verinfo.vertree = t;
1884 local_ver = NULL;
1885 d->script = 1;
1886 break;
1888 if (d != NULL)
1889 break;
1890 else if (matched)
1891 /* There is no undefined version for this symbol. Hide the
1892 default one. */
1893 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1896 if (t->locals.list != NULL)
1898 d = NULL;
1899 while ((d = (*t->match) (&t->locals, d,
1900 h->root.root.string)) != NULL)
1902 local_ver = t;
1903 /* If the match is "*", keep looking for a more
1904 explicit, perhaps even global, match.
1905 XXX: Shouldn't this be !d->wildcard instead? */
1906 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1907 break;
1910 if (d != NULL)
1911 break;
1915 if (local_ver != NULL)
1917 h->verinfo.vertree = local_ver;
1918 if (h->dynindx != -1
1919 && ! info->export_dynamic)
1921 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1926 return TRUE;
1929 /* Read and swap the relocs from the section indicated by SHDR. This
1930 may be either a REL or a RELA section. The relocations are
1931 translated into RELA relocations and stored in INTERNAL_RELOCS,
1932 which should have already been allocated to contain enough space.
1933 The EXTERNAL_RELOCS are a buffer where the external form of the
1934 relocations should be stored.
1936 Returns FALSE if something goes wrong. */
1938 static bfd_boolean
1939 elf_link_read_relocs_from_section (bfd *abfd,
1940 asection *sec,
1941 Elf_Internal_Shdr *shdr,
1942 void *external_relocs,
1943 Elf_Internal_Rela *internal_relocs)
1945 const struct elf_backend_data *bed;
1946 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1947 const bfd_byte *erela;
1948 const bfd_byte *erelaend;
1949 Elf_Internal_Rela *irela;
1950 Elf_Internal_Shdr *symtab_hdr;
1951 size_t nsyms;
1953 /* Position ourselves at the start of the section. */
1954 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1955 return FALSE;
1957 /* Read the relocations. */
1958 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1959 return FALSE;
1961 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1962 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1964 bed = get_elf_backend_data (abfd);
1966 /* Convert the external relocations to the internal format. */
1967 if (shdr->sh_entsize == bed->s->sizeof_rel)
1968 swap_in = bed->s->swap_reloc_in;
1969 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1970 swap_in = bed->s->swap_reloca_in;
1971 else
1973 bfd_set_error (bfd_error_wrong_format);
1974 return FALSE;
1977 erela = external_relocs;
1978 erelaend = erela + shdr->sh_size;
1979 irela = internal_relocs;
1980 while (erela < erelaend)
1982 bfd_vma r_symndx;
1984 (*swap_in) (abfd, erela, irela);
1985 r_symndx = ELF32_R_SYM (irela->r_info);
1986 if (bed->s->arch_size == 64)
1987 r_symndx >>= 24;
1988 if ((size_t) r_symndx >= nsyms)
1990 (*_bfd_error_handler)
1991 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1992 " for offset 0x%lx in section `%A'"),
1993 abfd, sec,
1994 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1995 bfd_set_error (bfd_error_bad_value);
1996 return FALSE;
1998 irela += bed->s->int_rels_per_ext_rel;
1999 erela += shdr->sh_entsize;
2002 return TRUE;
2005 /* Read and swap the relocs for a section O. They may have been
2006 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2007 not NULL, they are used as buffers to read into. They are known to
2008 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2009 the return value is allocated using either malloc or bfd_alloc,
2010 according to the KEEP_MEMORY argument. If O has two relocation
2011 sections (both REL and RELA relocations), then the REL_HDR
2012 relocations will appear first in INTERNAL_RELOCS, followed by the
2013 REL_HDR2 relocations. */
2015 Elf_Internal_Rela *
2016 _bfd_elf_link_read_relocs (bfd *abfd,
2017 asection *o,
2018 void *external_relocs,
2019 Elf_Internal_Rela *internal_relocs,
2020 bfd_boolean keep_memory)
2022 Elf_Internal_Shdr *rel_hdr;
2023 void *alloc1 = NULL;
2024 Elf_Internal_Rela *alloc2 = NULL;
2025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2027 if (elf_section_data (o)->relocs != NULL)
2028 return elf_section_data (o)->relocs;
2030 if (o->reloc_count == 0)
2031 return NULL;
2033 rel_hdr = &elf_section_data (o)->rel_hdr;
2035 if (internal_relocs == NULL)
2037 bfd_size_type size;
2039 size = o->reloc_count;
2040 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2041 if (keep_memory)
2042 internal_relocs = bfd_alloc (abfd, size);
2043 else
2044 internal_relocs = alloc2 = bfd_malloc (size);
2045 if (internal_relocs == NULL)
2046 goto error_return;
2049 if (external_relocs == NULL)
2051 bfd_size_type size = rel_hdr->sh_size;
2053 if (elf_section_data (o)->rel_hdr2)
2054 size += elf_section_data (o)->rel_hdr2->sh_size;
2055 alloc1 = bfd_malloc (size);
2056 if (alloc1 == NULL)
2057 goto error_return;
2058 external_relocs = alloc1;
2061 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2062 external_relocs,
2063 internal_relocs))
2064 goto error_return;
2065 if (elf_section_data (o)->rel_hdr2
2066 && (!elf_link_read_relocs_from_section
2067 (abfd, o,
2068 elf_section_data (o)->rel_hdr2,
2069 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2070 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2071 * bed->s->int_rels_per_ext_rel))))
2072 goto error_return;
2074 /* Cache the results for next time, if we can. */
2075 if (keep_memory)
2076 elf_section_data (o)->relocs = internal_relocs;
2078 if (alloc1 != NULL)
2079 free (alloc1);
2081 /* Don't free alloc2, since if it was allocated we are passing it
2082 back (under the name of internal_relocs). */
2084 return internal_relocs;
2086 error_return:
2087 if (alloc1 != NULL)
2088 free (alloc1);
2089 if (alloc2 != NULL)
2090 free (alloc2);
2091 return NULL;
2094 /* Compute the size of, and allocate space for, REL_HDR which is the
2095 section header for a section containing relocations for O. */
2097 bfd_boolean
2098 _bfd_elf_link_size_reloc_section (bfd *abfd,
2099 Elf_Internal_Shdr *rel_hdr,
2100 asection *o)
2102 bfd_size_type reloc_count;
2103 bfd_size_type num_rel_hashes;
2105 /* Figure out how many relocations there will be. */
2106 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2107 reloc_count = elf_section_data (o)->rel_count;
2108 else
2109 reloc_count = elf_section_data (o)->rel_count2;
2111 num_rel_hashes = o->reloc_count;
2112 if (num_rel_hashes < reloc_count)
2113 num_rel_hashes = reloc_count;
2115 /* That allows us to calculate the size of the section. */
2116 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2118 /* The contents field must last into write_object_contents, so we
2119 allocate it with bfd_alloc rather than malloc. Also since we
2120 cannot be sure that the contents will actually be filled in,
2121 we zero the allocated space. */
2122 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2123 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2124 return FALSE;
2126 /* We only allocate one set of hash entries, so we only do it the
2127 first time we are called. */
2128 if (elf_section_data (o)->rel_hashes == NULL
2129 && num_rel_hashes)
2131 struct elf_link_hash_entry **p;
2133 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2134 if (p == NULL)
2135 return FALSE;
2137 elf_section_data (o)->rel_hashes = p;
2140 return TRUE;
2143 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2144 originated from the section given by INPUT_REL_HDR) to the
2145 OUTPUT_BFD. */
2147 bfd_boolean
2148 _bfd_elf_link_output_relocs (bfd *output_bfd,
2149 asection *input_section,
2150 Elf_Internal_Shdr *input_rel_hdr,
2151 Elf_Internal_Rela *internal_relocs,
2152 struct elf_link_hash_entry **rel_hash
2153 ATTRIBUTE_UNUSED)
2155 Elf_Internal_Rela *irela;
2156 Elf_Internal_Rela *irelaend;
2157 bfd_byte *erel;
2158 Elf_Internal_Shdr *output_rel_hdr;
2159 asection *output_section;
2160 unsigned int *rel_countp = NULL;
2161 const struct elf_backend_data *bed;
2162 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2164 output_section = input_section->output_section;
2165 output_rel_hdr = NULL;
2167 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2168 == input_rel_hdr->sh_entsize)
2170 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2171 rel_countp = &elf_section_data (output_section)->rel_count;
2173 else if (elf_section_data (output_section)->rel_hdr2
2174 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2175 == input_rel_hdr->sh_entsize))
2177 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2178 rel_countp = &elf_section_data (output_section)->rel_count2;
2180 else
2182 (*_bfd_error_handler)
2183 (_("%B: relocation size mismatch in %B section %A"),
2184 output_bfd, input_section->owner, input_section);
2185 bfd_set_error (bfd_error_wrong_object_format);
2186 return FALSE;
2189 bed = get_elf_backend_data (output_bfd);
2190 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2191 swap_out = bed->s->swap_reloc_out;
2192 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2193 swap_out = bed->s->swap_reloca_out;
2194 else
2195 abort ();
2197 erel = output_rel_hdr->contents;
2198 erel += *rel_countp * input_rel_hdr->sh_entsize;
2199 irela = internal_relocs;
2200 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2201 * bed->s->int_rels_per_ext_rel);
2202 while (irela < irelaend)
2204 (*swap_out) (output_bfd, irela, erel);
2205 irela += bed->s->int_rels_per_ext_rel;
2206 erel += input_rel_hdr->sh_entsize;
2209 /* Bump the counter, so that we know where to add the next set of
2210 relocations. */
2211 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2213 return TRUE;
2216 /* Make weak undefined symbols in PIE dynamic. */
2218 bfd_boolean
2219 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2220 struct elf_link_hash_entry *h)
2222 if (info->pie
2223 && h->dynindx == -1
2224 && h->root.type == bfd_link_hash_undefweak)
2225 return bfd_elf_link_record_dynamic_symbol (info, h);
2227 return TRUE;
2230 /* Fix up the flags for a symbol. This handles various cases which
2231 can only be fixed after all the input files are seen. This is
2232 currently called by both adjust_dynamic_symbol and
2233 assign_sym_version, which is unnecessary but perhaps more robust in
2234 the face of future changes. */
2236 bfd_boolean
2237 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2238 struct elf_info_failed *eif)
2240 const struct elf_backend_data *bed = NULL;
2242 /* If this symbol was mentioned in a non-ELF file, try to set
2243 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2244 permit a non-ELF file to correctly refer to a symbol defined in
2245 an ELF dynamic object. */
2246 if (h->non_elf)
2248 while (h->root.type == bfd_link_hash_indirect)
2249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2251 if (h->root.type != bfd_link_hash_defined
2252 && h->root.type != bfd_link_hash_defweak)
2254 h->ref_regular = 1;
2255 h->ref_regular_nonweak = 1;
2257 else
2259 if (h->root.u.def.section->owner != NULL
2260 && (bfd_get_flavour (h->root.u.def.section->owner)
2261 == bfd_target_elf_flavour))
2263 h->ref_regular = 1;
2264 h->ref_regular_nonweak = 1;
2266 else
2267 h->def_regular = 1;
2270 if (h->dynindx == -1
2271 && (h->def_dynamic
2272 || h->ref_dynamic))
2274 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2276 eif->failed = TRUE;
2277 return FALSE;
2281 else
2283 /* Unfortunately, NON_ELF is only correct if the symbol
2284 was first seen in a non-ELF file. Fortunately, if the symbol
2285 was first seen in an ELF file, we're probably OK unless the
2286 symbol was defined in a non-ELF file. Catch that case here.
2287 FIXME: We're still in trouble if the symbol was first seen in
2288 a dynamic object, and then later in a non-ELF regular object. */
2289 if ((h->root.type == bfd_link_hash_defined
2290 || h->root.type == bfd_link_hash_defweak)
2291 && !h->def_regular
2292 && (h->root.u.def.section->owner != NULL
2293 ? (bfd_get_flavour (h->root.u.def.section->owner)
2294 != bfd_target_elf_flavour)
2295 : (bfd_is_abs_section (h->root.u.def.section)
2296 && !h->def_dynamic)))
2297 h->def_regular = 1;
2300 /* Backend specific symbol fixup. */
2301 if (elf_hash_table (eif->info)->dynobj)
2303 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2304 if (bed->elf_backend_fixup_symbol
2305 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2306 return FALSE;
2309 /* If this is a final link, and the symbol was defined as a common
2310 symbol in a regular object file, and there was no definition in
2311 any dynamic object, then the linker will have allocated space for
2312 the symbol in a common section but the DEF_REGULAR
2313 flag will not have been set. */
2314 if (h->root.type == bfd_link_hash_defined
2315 && !h->def_regular
2316 && h->ref_regular
2317 && !h->def_dynamic
2318 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2319 h->def_regular = 1;
2321 /* If -Bsymbolic was used (which means to bind references to global
2322 symbols to the definition within the shared object), and this
2323 symbol was defined in a regular object, then it actually doesn't
2324 need a PLT entry. Likewise, if the symbol has non-default
2325 visibility. If the symbol has hidden or internal visibility, we
2326 will force it local. */
2327 if (h->needs_plt
2328 && eif->info->shared
2329 && is_elf_hash_table (eif->info->hash)
2330 && (eif->info->symbolic
2331 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2332 && h->def_regular)
2334 bfd_boolean force_local;
2336 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2337 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2338 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2341 /* If a weak undefined symbol has non-default visibility, we also
2342 hide it from the dynamic linker. */
2343 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2344 && h->root.type == bfd_link_hash_undefweak)
2346 const struct elf_backend_data *bed;
2347 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2348 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2351 /* If this is a weak defined symbol in a dynamic object, and we know
2352 the real definition in the dynamic object, copy interesting flags
2353 over to the real definition. */
2354 if (h->u.weakdef != NULL)
2356 struct elf_link_hash_entry *weakdef;
2358 weakdef = h->u.weakdef;
2359 if (h->root.type == bfd_link_hash_indirect)
2360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2363 || h->root.type == bfd_link_hash_defweak);
2364 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2365 || weakdef->root.type == bfd_link_hash_defweak);
2366 BFD_ASSERT (weakdef->def_dynamic);
2368 /* If the real definition is defined by a regular object file,
2369 don't do anything special. See the longer description in
2370 _bfd_elf_adjust_dynamic_symbol, below. */
2371 if (weakdef->def_regular)
2372 h->u.weakdef = NULL;
2373 else
2374 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2378 return TRUE;
2381 /* Make the backend pick a good value for a dynamic symbol. This is
2382 called via elf_link_hash_traverse, and also calls itself
2383 recursively. */
2385 bfd_boolean
2386 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2388 struct elf_info_failed *eif = data;
2389 bfd *dynobj;
2390 const struct elf_backend_data *bed;
2392 if (! is_elf_hash_table (eif->info->hash))
2393 return FALSE;
2395 if (h->root.type == bfd_link_hash_warning)
2397 h->got = elf_hash_table (eif->info)->init_got_offset;
2398 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2400 /* When warning symbols are created, they **replace** the "real"
2401 entry in the hash table, thus we never get to see the real
2402 symbol in a hash traversal. So look at it now. */
2403 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2406 /* Ignore indirect symbols. These are added by the versioning code. */
2407 if (h->root.type == bfd_link_hash_indirect)
2408 return TRUE;
2410 /* Fix the symbol flags. */
2411 if (! _bfd_elf_fix_symbol_flags (h, eif))
2412 return FALSE;
2414 /* If this symbol does not require a PLT entry, and it is not
2415 defined by a dynamic object, or is not referenced by a regular
2416 object, ignore it. We do have to handle a weak defined symbol,
2417 even if no regular object refers to it, if we decided to add it
2418 to the dynamic symbol table. FIXME: Do we normally need to worry
2419 about symbols which are defined by one dynamic object and
2420 referenced by another one? */
2421 if (!h->needs_plt
2422 && (h->def_regular
2423 || !h->def_dynamic
2424 || (!h->ref_regular
2425 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2427 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2428 return TRUE;
2431 /* If we've already adjusted this symbol, don't do it again. This
2432 can happen via a recursive call. */
2433 if (h->dynamic_adjusted)
2434 return TRUE;
2436 /* Don't look at this symbol again. Note that we must set this
2437 after checking the above conditions, because we may look at a
2438 symbol once, decide not to do anything, and then get called
2439 recursively later after REF_REGULAR is set below. */
2440 h->dynamic_adjusted = 1;
2442 /* If this is a weak definition, and we know a real definition, and
2443 the real symbol is not itself defined by a regular object file,
2444 then get a good value for the real definition. We handle the
2445 real symbol first, for the convenience of the backend routine.
2447 Note that there is a confusing case here. If the real definition
2448 is defined by a regular object file, we don't get the real symbol
2449 from the dynamic object, but we do get the weak symbol. If the
2450 processor backend uses a COPY reloc, then if some routine in the
2451 dynamic object changes the real symbol, we will not see that
2452 change in the corresponding weak symbol. This is the way other
2453 ELF linkers work as well, and seems to be a result of the shared
2454 library model.
2456 I will clarify this issue. Most SVR4 shared libraries define the
2457 variable _timezone and define timezone as a weak synonym. The
2458 tzset call changes _timezone. If you write
2459 extern int timezone;
2460 int _timezone = 5;
2461 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2462 you might expect that, since timezone is a synonym for _timezone,
2463 the same number will print both times. However, if the processor
2464 backend uses a COPY reloc, then actually timezone will be copied
2465 into your process image, and, since you define _timezone
2466 yourself, _timezone will not. Thus timezone and _timezone will
2467 wind up at different memory locations. The tzset call will set
2468 _timezone, leaving timezone unchanged. */
2470 if (h->u.weakdef != NULL)
2472 /* If we get to this point, we know there is an implicit
2473 reference by a regular object file via the weak symbol H.
2474 FIXME: Is this really true? What if the traversal finds
2475 H->U.WEAKDEF before it finds H? */
2476 h->u.weakdef->ref_regular = 1;
2478 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2479 return FALSE;
2482 /* If a symbol has no type and no size and does not require a PLT
2483 entry, then we are probably about to do the wrong thing here: we
2484 are probably going to create a COPY reloc for an empty object.
2485 This case can arise when a shared object is built with assembly
2486 code, and the assembly code fails to set the symbol type. */
2487 if (h->size == 0
2488 && h->type == STT_NOTYPE
2489 && !h->needs_plt)
2490 (*_bfd_error_handler)
2491 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2492 h->root.root.string);
2494 dynobj = elf_hash_table (eif->info)->dynobj;
2495 bed = get_elf_backend_data (dynobj);
2496 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2498 eif->failed = TRUE;
2499 return FALSE;
2502 return TRUE;
2505 /* Adjust all external symbols pointing into SEC_MERGE sections
2506 to reflect the object merging within the sections. */
2508 bfd_boolean
2509 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2511 asection *sec;
2513 if (h->root.type == bfd_link_hash_warning)
2514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2516 if ((h->root.type == bfd_link_hash_defined
2517 || h->root.type == bfd_link_hash_defweak)
2518 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2519 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2521 bfd *output_bfd = data;
2523 h->root.u.def.value =
2524 _bfd_merged_section_offset (output_bfd,
2525 &h->root.u.def.section,
2526 elf_section_data (sec)->sec_info,
2527 h->root.u.def.value);
2530 return TRUE;
2533 /* Returns false if the symbol referred to by H should be considered
2534 to resolve local to the current module, and true if it should be
2535 considered to bind dynamically. */
2537 bfd_boolean
2538 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2539 struct bfd_link_info *info,
2540 bfd_boolean ignore_protected)
2542 bfd_boolean binding_stays_local_p;
2544 if (h == NULL)
2545 return FALSE;
2547 while (h->root.type == bfd_link_hash_indirect
2548 || h->root.type == bfd_link_hash_warning)
2549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2551 /* If it was forced local, then clearly it's not dynamic. */
2552 if (h->dynindx == -1)
2553 return FALSE;
2554 if (h->forced_local)
2555 return FALSE;
2557 /* Identify the cases where name binding rules say that a
2558 visible symbol resolves locally. */
2559 binding_stays_local_p = info->executable || info->symbolic;
2561 switch (ELF_ST_VISIBILITY (h->other))
2563 case STV_INTERNAL:
2564 case STV_HIDDEN:
2565 return FALSE;
2567 case STV_PROTECTED:
2568 /* Proper resolution for function pointer equality may require
2569 that these symbols perhaps be resolved dynamically, even though
2570 we should be resolving them to the current module. */
2571 if (!ignore_protected || h->type != STT_FUNC)
2572 binding_stays_local_p = TRUE;
2573 break;
2575 default:
2576 break;
2579 /* If it isn't defined locally, then clearly it's dynamic. */
2580 if (!h->def_regular)
2581 return TRUE;
2583 /* Otherwise, the symbol is dynamic if binding rules don't tell
2584 us that it remains local. */
2585 return !binding_stays_local_p;
2588 /* Return true if the symbol referred to by H should be considered
2589 to resolve local to the current module, and false otherwise. Differs
2590 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2591 undefined symbols and weak symbols. */
2593 bfd_boolean
2594 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2595 struct bfd_link_info *info,
2596 bfd_boolean local_protected)
2598 /* If it's a local sym, of course we resolve locally. */
2599 if (h == NULL)
2600 return TRUE;
2602 /* Common symbols that become definitions don't get the DEF_REGULAR
2603 flag set, so test it first, and don't bail out. */
2604 if (ELF_COMMON_DEF_P (h))
2605 /* Do nothing. */;
2606 /* If we don't have a definition in a regular file, then we can't
2607 resolve locally. The sym is either undefined or dynamic. */
2608 else if (!h->def_regular)
2609 return FALSE;
2611 /* Forced local symbols resolve locally. */
2612 if (h->forced_local)
2613 return TRUE;
2615 /* As do non-dynamic symbols. */
2616 if (h->dynindx == -1)
2617 return TRUE;
2619 /* At this point, we know the symbol is defined and dynamic. In an
2620 executable it must resolve locally, likewise when building symbolic
2621 shared libraries. */
2622 if (info->executable || info->symbolic)
2623 return TRUE;
2625 /* Now deal with defined dynamic symbols in shared libraries. Ones
2626 with default visibility might not resolve locally. */
2627 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2628 return FALSE;
2630 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2631 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2632 return TRUE;
2634 /* STV_PROTECTED non-function symbols are local. */
2635 if (h->type != STT_FUNC)
2636 return TRUE;
2638 /* Function pointer equality tests may require that STV_PROTECTED
2639 symbols be treated as dynamic symbols, even when we know that the
2640 dynamic linker will resolve them locally. */
2641 return local_protected;
2644 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2645 aligned. Returns the first TLS output section. */
2647 struct bfd_section *
2648 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2650 struct bfd_section *sec, *tls;
2651 unsigned int align = 0;
2653 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2654 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2655 break;
2656 tls = sec;
2658 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2659 if (sec->alignment_power > align)
2660 align = sec->alignment_power;
2662 elf_hash_table (info)->tls_sec = tls;
2664 /* Ensure the alignment of the first section is the largest alignment,
2665 so that the tls segment starts aligned. */
2666 if (tls != NULL)
2667 tls->alignment_power = align;
2669 return tls;
2672 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2673 static bfd_boolean
2674 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2675 Elf_Internal_Sym *sym)
2677 const struct elf_backend_data *bed;
2679 /* Local symbols do not count, but target specific ones might. */
2680 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2681 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2682 return FALSE;
2684 /* Function symbols do not count. */
2685 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2686 return FALSE;
2688 /* If the section is undefined, then so is the symbol. */
2689 if (sym->st_shndx == SHN_UNDEF)
2690 return FALSE;
2692 /* If the symbol is defined in the common section, then
2693 it is a common definition and so does not count. */
2694 bed = get_elf_backend_data (abfd);
2695 if (bed->common_definition (sym))
2696 return FALSE;
2698 /* If the symbol is in a target specific section then we
2699 must rely upon the backend to tell us what it is. */
2700 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2701 /* FIXME - this function is not coded yet:
2703 return _bfd_is_global_symbol_definition (abfd, sym);
2705 Instead for now assume that the definition is not global,
2706 Even if this is wrong, at least the linker will behave
2707 in the same way that it used to do. */
2708 return FALSE;
2710 return TRUE;
2713 /* Search the symbol table of the archive element of the archive ABFD
2714 whose archive map contains a mention of SYMDEF, and determine if
2715 the symbol is defined in this element. */
2716 static bfd_boolean
2717 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2719 Elf_Internal_Shdr * hdr;
2720 bfd_size_type symcount;
2721 bfd_size_type extsymcount;
2722 bfd_size_type extsymoff;
2723 Elf_Internal_Sym *isymbuf;
2724 Elf_Internal_Sym *isym;
2725 Elf_Internal_Sym *isymend;
2726 bfd_boolean result;
2728 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2729 if (abfd == NULL)
2730 return FALSE;
2732 if (! bfd_check_format (abfd, bfd_object))
2733 return FALSE;
2735 /* If we have already included the element containing this symbol in the
2736 link then we do not need to include it again. Just claim that any symbol
2737 it contains is not a definition, so that our caller will not decide to
2738 (re)include this element. */
2739 if (abfd->archive_pass)
2740 return FALSE;
2742 /* Select the appropriate symbol table. */
2743 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2744 hdr = &elf_tdata (abfd)->symtab_hdr;
2745 else
2746 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2748 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2750 /* The sh_info field of the symtab header tells us where the
2751 external symbols start. We don't care about the local symbols. */
2752 if (elf_bad_symtab (abfd))
2754 extsymcount = symcount;
2755 extsymoff = 0;
2757 else
2759 extsymcount = symcount - hdr->sh_info;
2760 extsymoff = hdr->sh_info;
2763 if (extsymcount == 0)
2764 return FALSE;
2766 /* Read in the symbol table. */
2767 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2768 NULL, NULL, NULL);
2769 if (isymbuf == NULL)
2770 return FALSE;
2772 /* Scan the symbol table looking for SYMDEF. */
2773 result = FALSE;
2774 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2776 const char *name;
2778 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2779 isym->st_name);
2780 if (name == NULL)
2781 break;
2783 if (strcmp (name, symdef->name) == 0)
2785 result = is_global_data_symbol_definition (abfd, isym);
2786 break;
2790 free (isymbuf);
2792 return result;
2795 /* Add an entry to the .dynamic table. */
2797 bfd_boolean
2798 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2799 bfd_vma tag,
2800 bfd_vma val)
2802 struct elf_link_hash_table *hash_table;
2803 const struct elf_backend_data *bed;
2804 asection *s;
2805 bfd_size_type newsize;
2806 bfd_byte *newcontents;
2807 Elf_Internal_Dyn dyn;
2809 hash_table = elf_hash_table (info);
2810 if (! is_elf_hash_table (hash_table))
2811 return FALSE;
2813 bed = get_elf_backend_data (hash_table->dynobj);
2814 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2815 BFD_ASSERT (s != NULL);
2817 newsize = s->size + bed->s->sizeof_dyn;
2818 newcontents = bfd_realloc (s->contents, newsize);
2819 if (newcontents == NULL)
2820 return FALSE;
2822 dyn.d_tag = tag;
2823 dyn.d_un.d_val = val;
2824 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2826 s->size = newsize;
2827 s->contents = newcontents;
2829 return TRUE;
2832 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2833 otherwise just check whether one already exists. Returns -1 on error,
2834 1 if a DT_NEEDED tag already exists, and 0 on success. */
2836 static int
2837 elf_add_dt_needed_tag (bfd *abfd,
2838 struct bfd_link_info *info,
2839 const char *soname,
2840 bfd_boolean do_it)
2842 struct elf_link_hash_table *hash_table;
2843 bfd_size_type oldsize;
2844 bfd_size_type strindex;
2846 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2847 return -1;
2849 hash_table = elf_hash_table (info);
2850 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2851 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2852 if (strindex == (bfd_size_type) -1)
2853 return -1;
2855 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2857 asection *sdyn;
2858 const struct elf_backend_data *bed;
2859 bfd_byte *extdyn;
2861 bed = get_elf_backend_data (hash_table->dynobj);
2862 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2863 if (sdyn != NULL)
2864 for (extdyn = sdyn->contents;
2865 extdyn < sdyn->contents + sdyn->size;
2866 extdyn += bed->s->sizeof_dyn)
2868 Elf_Internal_Dyn dyn;
2870 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2871 if (dyn.d_tag == DT_NEEDED
2872 && dyn.d_un.d_val == strindex)
2874 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2875 return 1;
2880 if (do_it)
2882 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2883 return -1;
2885 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2886 return -1;
2888 else
2889 /* We were just checking for existence of the tag. */
2890 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2892 return 0;
2895 /* Sort symbol by value and section. */
2896 static int
2897 elf_sort_symbol (const void *arg1, const void *arg2)
2899 const struct elf_link_hash_entry *h1;
2900 const struct elf_link_hash_entry *h2;
2901 bfd_signed_vma vdiff;
2903 h1 = *(const struct elf_link_hash_entry **) arg1;
2904 h2 = *(const struct elf_link_hash_entry **) arg2;
2905 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2906 if (vdiff != 0)
2907 return vdiff > 0 ? 1 : -1;
2908 else
2910 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2911 if (sdiff != 0)
2912 return sdiff > 0 ? 1 : -1;
2914 return 0;
2917 /* This function is used to adjust offsets into .dynstr for
2918 dynamic symbols. This is called via elf_link_hash_traverse. */
2920 static bfd_boolean
2921 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2923 struct elf_strtab_hash *dynstr = data;
2925 if (h->root.type == bfd_link_hash_warning)
2926 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2928 if (h->dynindx != -1)
2929 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2930 return TRUE;
2933 /* Assign string offsets in .dynstr, update all structures referencing
2934 them. */
2936 static bfd_boolean
2937 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2939 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2940 struct elf_link_local_dynamic_entry *entry;
2941 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2942 bfd *dynobj = hash_table->dynobj;
2943 asection *sdyn;
2944 bfd_size_type size;
2945 const struct elf_backend_data *bed;
2946 bfd_byte *extdyn;
2948 _bfd_elf_strtab_finalize (dynstr);
2949 size = _bfd_elf_strtab_size (dynstr);
2951 bed = get_elf_backend_data (dynobj);
2952 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2953 BFD_ASSERT (sdyn != NULL);
2955 /* Update all .dynamic entries referencing .dynstr strings. */
2956 for (extdyn = sdyn->contents;
2957 extdyn < sdyn->contents + sdyn->size;
2958 extdyn += bed->s->sizeof_dyn)
2960 Elf_Internal_Dyn dyn;
2962 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2963 switch (dyn.d_tag)
2965 case DT_STRSZ:
2966 dyn.d_un.d_val = size;
2967 break;
2968 case DT_NEEDED:
2969 case DT_SONAME:
2970 case DT_RPATH:
2971 case DT_RUNPATH:
2972 case DT_FILTER:
2973 case DT_AUXILIARY:
2974 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2975 break;
2976 default:
2977 continue;
2979 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2982 /* Now update local dynamic symbols. */
2983 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2984 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2985 entry->isym.st_name);
2987 /* And the rest of dynamic symbols. */
2988 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2990 /* Adjust version definitions. */
2991 if (elf_tdata (output_bfd)->cverdefs)
2993 asection *s;
2994 bfd_byte *p;
2995 bfd_size_type i;
2996 Elf_Internal_Verdef def;
2997 Elf_Internal_Verdaux defaux;
2999 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3000 p = s->contents;
3003 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3004 &def);
3005 p += sizeof (Elf_External_Verdef);
3006 if (def.vd_aux != sizeof (Elf_External_Verdef))
3007 continue;
3008 for (i = 0; i < def.vd_cnt; ++i)
3010 _bfd_elf_swap_verdaux_in (output_bfd,
3011 (Elf_External_Verdaux *) p, &defaux);
3012 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3013 defaux.vda_name);
3014 _bfd_elf_swap_verdaux_out (output_bfd,
3015 &defaux, (Elf_External_Verdaux *) p);
3016 p += sizeof (Elf_External_Verdaux);
3019 while (def.vd_next);
3022 /* Adjust version references. */
3023 if (elf_tdata (output_bfd)->verref)
3025 asection *s;
3026 bfd_byte *p;
3027 bfd_size_type i;
3028 Elf_Internal_Verneed need;
3029 Elf_Internal_Vernaux needaux;
3031 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3032 p = s->contents;
3035 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3036 &need);
3037 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3038 _bfd_elf_swap_verneed_out (output_bfd, &need,
3039 (Elf_External_Verneed *) p);
3040 p += sizeof (Elf_External_Verneed);
3041 for (i = 0; i < need.vn_cnt; ++i)
3043 _bfd_elf_swap_vernaux_in (output_bfd,
3044 (Elf_External_Vernaux *) p, &needaux);
3045 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3046 needaux.vna_name);
3047 _bfd_elf_swap_vernaux_out (output_bfd,
3048 &needaux,
3049 (Elf_External_Vernaux *) p);
3050 p += sizeof (Elf_External_Vernaux);
3053 while (need.vn_next);
3056 return TRUE;
3059 /* Add symbols from an ELF object file to the linker hash table. */
3061 static bfd_boolean
3062 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3064 Elf_Internal_Shdr *hdr;
3065 bfd_size_type symcount;
3066 bfd_size_type extsymcount;
3067 bfd_size_type extsymoff;
3068 struct elf_link_hash_entry **sym_hash;
3069 bfd_boolean dynamic;
3070 Elf_External_Versym *extversym = NULL;
3071 Elf_External_Versym *ever;
3072 struct elf_link_hash_entry *weaks;
3073 struct elf_link_hash_entry **nondeflt_vers = NULL;
3074 bfd_size_type nondeflt_vers_cnt = 0;
3075 Elf_Internal_Sym *isymbuf = NULL;
3076 Elf_Internal_Sym *isym;
3077 Elf_Internal_Sym *isymend;
3078 const struct elf_backend_data *bed;
3079 bfd_boolean add_needed;
3080 struct elf_link_hash_table *htab;
3081 bfd_size_type amt;
3082 void *alloc_mark = NULL;
3083 void *old_tab = NULL;
3084 void *old_hash;
3085 void *old_ent;
3086 struct bfd_link_hash_entry *old_undefs = NULL;
3087 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3088 long old_dynsymcount = 0;
3089 size_t tabsize = 0;
3090 size_t hashsize = 0;
3092 htab = elf_hash_table (info);
3093 bed = get_elf_backend_data (abfd);
3095 if ((abfd->flags & DYNAMIC) == 0)
3096 dynamic = FALSE;
3097 else
3099 dynamic = TRUE;
3101 /* You can't use -r against a dynamic object. Also, there's no
3102 hope of using a dynamic object which does not exactly match
3103 the format of the output file. */
3104 if (info->relocatable
3105 || !is_elf_hash_table (htab)
3106 || htab->root.creator != abfd->xvec)
3108 if (info->relocatable)
3109 bfd_set_error (bfd_error_invalid_operation);
3110 else
3111 bfd_set_error (bfd_error_wrong_format);
3112 goto error_return;
3116 /* As a GNU extension, any input sections which are named
3117 .gnu.warning.SYMBOL are treated as warning symbols for the given
3118 symbol. This differs from .gnu.warning sections, which generate
3119 warnings when they are included in an output file. */
3120 if (info->executable)
3122 asection *s;
3124 for (s = abfd->sections; s != NULL; s = s->next)
3126 const char *name;
3128 name = bfd_get_section_name (abfd, s);
3129 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3131 char *msg;
3132 bfd_size_type sz;
3134 name += sizeof ".gnu.warning." - 1;
3136 /* If this is a shared object, then look up the symbol
3137 in the hash table. If it is there, and it is already
3138 been defined, then we will not be using the entry
3139 from this shared object, so we don't need to warn.
3140 FIXME: If we see the definition in a regular object
3141 later on, we will warn, but we shouldn't. The only
3142 fix is to keep track of what warnings we are supposed
3143 to emit, and then handle them all at the end of the
3144 link. */
3145 if (dynamic)
3147 struct elf_link_hash_entry *h;
3149 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3151 /* FIXME: What about bfd_link_hash_common? */
3152 if (h != NULL
3153 && (h->root.type == bfd_link_hash_defined
3154 || h->root.type == bfd_link_hash_defweak))
3156 /* We don't want to issue this warning. Clobber
3157 the section size so that the warning does not
3158 get copied into the output file. */
3159 s->size = 0;
3160 continue;
3164 sz = s->size;
3165 msg = bfd_alloc (abfd, sz + 1);
3166 if (msg == NULL)
3167 goto error_return;
3169 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3170 goto error_return;
3172 msg[sz] = '\0';
3174 if (! (_bfd_generic_link_add_one_symbol
3175 (info, abfd, name, BSF_WARNING, s, 0, msg,
3176 FALSE, bed->collect, NULL)))
3177 goto error_return;
3179 if (! info->relocatable)
3181 /* Clobber the section size so that the warning does
3182 not get copied into the output file. */
3183 s->size = 0;
3185 /* Also set SEC_EXCLUDE, so that symbols defined in
3186 the warning section don't get copied to the output. */
3187 s->flags |= SEC_EXCLUDE;
3193 add_needed = TRUE;
3194 if (! dynamic)
3196 /* If we are creating a shared library, create all the dynamic
3197 sections immediately. We need to attach them to something,
3198 so we attach them to this BFD, provided it is the right
3199 format. FIXME: If there are no input BFD's of the same
3200 format as the output, we can't make a shared library. */
3201 if (info->shared
3202 && is_elf_hash_table (htab)
3203 && htab->root.creator == abfd->xvec
3204 && !htab->dynamic_sections_created)
3206 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3207 goto error_return;
3210 else if (!is_elf_hash_table (htab))
3211 goto error_return;
3212 else
3214 asection *s;
3215 const char *soname = NULL;
3216 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3217 int ret;
3219 /* ld --just-symbols and dynamic objects don't mix very well.
3220 ld shouldn't allow it. */
3221 if ((s = abfd->sections) != NULL
3222 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3223 abort ();
3225 /* If this dynamic lib was specified on the command line with
3226 --as-needed in effect, then we don't want to add a DT_NEEDED
3227 tag unless the lib is actually used. Similary for libs brought
3228 in by another lib's DT_NEEDED. When --no-add-needed is used
3229 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3230 any dynamic library in DT_NEEDED tags in the dynamic lib at
3231 all. */
3232 add_needed = (elf_dyn_lib_class (abfd)
3233 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3234 | DYN_NO_NEEDED)) == 0;
3236 s = bfd_get_section_by_name (abfd, ".dynamic");
3237 if (s != NULL)
3239 bfd_byte *dynbuf;
3240 bfd_byte *extdyn;
3241 int elfsec;
3242 unsigned long shlink;
3244 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3245 goto error_free_dyn;
3247 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3248 if (elfsec == -1)
3249 goto error_free_dyn;
3250 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3252 for (extdyn = dynbuf;
3253 extdyn < dynbuf + s->size;
3254 extdyn += bed->s->sizeof_dyn)
3256 Elf_Internal_Dyn dyn;
3258 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3259 if (dyn.d_tag == DT_SONAME)
3261 unsigned int tagv = dyn.d_un.d_val;
3262 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3263 if (soname == NULL)
3264 goto error_free_dyn;
3266 if (dyn.d_tag == DT_NEEDED)
3268 struct bfd_link_needed_list *n, **pn;
3269 char *fnm, *anm;
3270 unsigned int tagv = dyn.d_un.d_val;
3272 amt = sizeof (struct bfd_link_needed_list);
3273 n = bfd_alloc (abfd, amt);
3274 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3275 if (n == NULL || fnm == NULL)
3276 goto error_free_dyn;
3277 amt = strlen (fnm) + 1;
3278 anm = bfd_alloc (abfd, amt);
3279 if (anm == NULL)
3280 goto error_free_dyn;
3281 memcpy (anm, fnm, amt);
3282 n->name = anm;
3283 n->by = abfd;
3284 n->next = NULL;
3285 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3287 *pn = n;
3289 if (dyn.d_tag == DT_RUNPATH)
3291 struct bfd_link_needed_list *n, **pn;
3292 char *fnm, *anm;
3293 unsigned int tagv = dyn.d_un.d_val;
3295 amt = sizeof (struct bfd_link_needed_list);
3296 n = bfd_alloc (abfd, amt);
3297 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3298 if (n == NULL || fnm == NULL)
3299 goto error_free_dyn;
3300 amt = strlen (fnm) + 1;
3301 anm = bfd_alloc (abfd, amt);
3302 if (anm == NULL)
3303 goto error_free_dyn;
3304 memcpy (anm, fnm, amt);
3305 n->name = anm;
3306 n->by = abfd;
3307 n->next = NULL;
3308 for (pn = & runpath;
3309 *pn != NULL;
3310 pn = &(*pn)->next)
3312 *pn = n;
3314 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3315 if (!runpath && dyn.d_tag == DT_RPATH)
3317 struct bfd_link_needed_list *n, **pn;
3318 char *fnm, *anm;
3319 unsigned int tagv = dyn.d_un.d_val;
3321 amt = sizeof (struct bfd_link_needed_list);
3322 n = bfd_alloc (abfd, amt);
3323 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3324 if (n == NULL || fnm == NULL)
3325 goto error_free_dyn;
3326 amt = strlen (fnm) + 1;
3327 anm = bfd_alloc (abfd, amt);
3328 if (anm == NULL)
3330 error_free_dyn:
3331 free (dynbuf);
3332 goto error_return;
3334 memcpy (anm, fnm, amt);
3335 n->name = anm;
3336 n->by = abfd;
3337 n->next = NULL;
3338 for (pn = & rpath;
3339 *pn != NULL;
3340 pn = &(*pn)->next)
3342 *pn = n;
3346 free (dynbuf);
3349 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3350 frees all more recently bfd_alloc'd blocks as well. */
3351 if (runpath)
3352 rpath = runpath;
3354 if (rpath)
3356 struct bfd_link_needed_list **pn;
3357 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3359 *pn = rpath;
3362 /* We do not want to include any of the sections in a dynamic
3363 object in the output file. We hack by simply clobbering the
3364 list of sections in the BFD. This could be handled more
3365 cleanly by, say, a new section flag; the existing
3366 SEC_NEVER_LOAD flag is not the one we want, because that one
3367 still implies that the section takes up space in the output
3368 file. */
3369 bfd_section_list_clear (abfd);
3371 /* Find the name to use in a DT_NEEDED entry that refers to this
3372 object. If the object has a DT_SONAME entry, we use it.
3373 Otherwise, if the generic linker stuck something in
3374 elf_dt_name, we use that. Otherwise, we just use the file
3375 name. */
3376 if (soname == NULL || *soname == '\0')
3378 soname = elf_dt_name (abfd);
3379 if (soname == NULL || *soname == '\0')
3380 soname = bfd_get_filename (abfd);
3383 /* Save the SONAME because sometimes the linker emulation code
3384 will need to know it. */
3385 elf_dt_name (abfd) = soname;
3387 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3388 if (ret < 0)
3389 goto error_return;
3391 /* If we have already included this dynamic object in the
3392 link, just ignore it. There is no reason to include a
3393 particular dynamic object more than once. */
3394 if (ret > 0)
3395 return TRUE;
3398 /* If this is a dynamic object, we always link against the .dynsym
3399 symbol table, not the .symtab symbol table. The dynamic linker
3400 will only see the .dynsym symbol table, so there is no reason to
3401 look at .symtab for a dynamic object. */
3403 if (! dynamic || elf_dynsymtab (abfd) == 0)
3404 hdr = &elf_tdata (abfd)->symtab_hdr;
3405 else
3406 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3408 symcount = hdr->sh_size / bed->s->sizeof_sym;
3410 /* The sh_info field of the symtab header tells us where the
3411 external symbols start. We don't care about the local symbols at
3412 this point. */
3413 if (elf_bad_symtab (abfd))
3415 extsymcount = symcount;
3416 extsymoff = 0;
3418 else
3420 extsymcount = symcount - hdr->sh_info;
3421 extsymoff = hdr->sh_info;
3424 sym_hash = NULL;
3425 if (extsymcount != 0)
3427 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3428 NULL, NULL, NULL);
3429 if (isymbuf == NULL)
3430 goto error_return;
3432 /* We store a pointer to the hash table entry for each external
3433 symbol. */
3434 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3435 sym_hash = bfd_alloc (abfd, amt);
3436 if (sym_hash == NULL)
3437 goto error_free_sym;
3438 elf_sym_hashes (abfd) = sym_hash;
3441 if (dynamic)
3443 /* Read in any version definitions. */
3444 if (!_bfd_elf_slurp_version_tables (abfd,
3445 info->default_imported_symver))
3446 goto error_free_sym;
3448 /* Read in the symbol versions, but don't bother to convert them
3449 to internal format. */
3450 if (elf_dynversym (abfd) != 0)
3452 Elf_Internal_Shdr *versymhdr;
3454 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3455 extversym = bfd_malloc (versymhdr->sh_size);
3456 if (extversym == NULL)
3457 goto error_free_sym;
3458 amt = versymhdr->sh_size;
3459 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3460 || bfd_bread (extversym, amt, abfd) != amt)
3461 goto error_free_vers;
3465 /* If we are loading an as-needed shared lib, save the symbol table
3466 state before we start adding symbols. If the lib turns out
3467 to be unneeded, restore the state. */
3468 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3470 unsigned int i;
3471 size_t entsize;
3473 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3475 struct bfd_hash_entry *p;
3476 struct elf_link_hash_entry *h;
3478 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3480 h = (struct elf_link_hash_entry *) p;
3481 entsize += htab->root.table.entsize;
3482 if (h->root.type == bfd_link_hash_warning)
3483 entsize += htab->root.table.entsize;
3487 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3488 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3489 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3490 if (old_tab == NULL)
3491 goto error_free_vers;
3493 /* Remember the current objalloc pointer, so that all mem for
3494 symbols added can later be reclaimed. */
3495 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3496 if (alloc_mark == NULL)
3497 goto error_free_vers;
3499 /* Clone the symbol table and sym hashes. Remember some
3500 pointers into the symbol table, and dynamic symbol count. */
3501 old_hash = (char *) old_tab + tabsize;
3502 old_ent = (char *) old_hash + hashsize;
3503 memcpy (old_tab, htab->root.table.table, tabsize);
3504 memcpy (old_hash, sym_hash, hashsize);
3505 old_undefs = htab->root.undefs;
3506 old_undefs_tail = htab->root.undefs_tail;
3507 old_dynsymcount = htab->dynsymcount;
3509 for (i = 0; i < htab->root.table.size; i++)
3511 struct bfd_hash_entry *p;
3512 struct elf_link_hash_entry *h;
3514 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3516 memcpy (old_ent, p, htab->root.table.entsize);
3517 old_ent = (char *) old_ent + htab->root.table.entsize;
3518 h = (struct elf_link_hash_entry *) p;
3519 if (h->root.type == bfd_link_hash_warning)
3521 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3522 old_ent = (char *) old_ent + htab->root.table.entsize;
3528 weaks = NULL;
3529 ever = extversym != NULL ? extversym + extsymoff : NULL;
3530 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3531 isym < isymend;
3532 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3534 int bind;
3535 bfd_vma value;
3536 asection *sec, *new_sec;
3537 flagword flags;
3538 const char *name;
3539 struct elf_link_hash_entry *h;
3540 bfd_boolean definition;
3541 bfd_boolean size_change_ok;
3542 bfd_boolean type_change_ok;
3543 bfd_boolean new_weakdef;
3544 bfd_boolean override;
3545 bfd_boolean common;
3546 unsigned int old_alignment;
3547 bfd *old_bfd;
3549 override = FALSE;
3551 flags = BSF_NO_FLAGS;
3552 sec = NULL;
3553 value = isym->st_value;
3554 *sym_hash = NULL;
3555 common = bed->common_definition (isym);
3557 bind = ELF_ST_BIND (isym->st_info);
3558 if (bind == STB_LOCAL)
3560 /* This should be impossible, since ELF requires that all
3561 global symbols follow all local symbols, and that sh_info
3562 point to the first global symbol. Unfortunately, Irix 5
3563 screws this up. */
3564 continue;
3566 else if (bind == STB_GLOBAL)
3568 if (isym->st_shndx != SHN_UNDEF && !common)
3569 flags = BSF_GLOBAL;
3571 else if (bind == STB_WEAK)
3572 flags = BSF_WEAK;
3573 else
3575 /* Leave it up to the processor backend. */
3578 if (isym->st_shndx == SHN_UNDEF)
3579 sec = bfd_und_section_ptr;
3580 else if (isym->st_shndx < SHN_LORESERVE
3581 || isym->st_shndx > SHN_HIRESERVE)
3583 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3584 if (sec == NULL)
3585 sec = bfd_abs_section_ptr;
3586 else if (sec->kept_section)
3588 /* Symbols from discarded section are undefined, and have
3589 default visibility. */
3590 sec = bfd_und_section_ptr;
3591 isym->st_shndx = SHN_UNDEF;
3592 isym->st_other = (STV_DEFAULT
3593 | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)));
3595 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3596 value -= sec->vma;
3598 else if (isym->st_shndx == SHN_ABS)
3599 sec = bfd_abs_section_ptr;
3600 else if (isym->st_shndx == SHN_COMMON)
3602 sec = bfd_com_section_ptr;
3603 /* What ELF calls the size we call the value. What ELF
3604 calls the value we call the alignment. */
3605 value = isym->st_size;
3607 else
3609 /* Leave it up to the processor backend. */
3612 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3613 isym->st_name);
3614 if (name == NULL)
3615 goto error_free_vers;
3617 if (isym->st_shndx == SHN_COMMON
3618 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3620 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3622 if (tcomm == NULL)
3624 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3625 (SEC_ALLOC
3626 | SEC_IS_COMMON
3627 | SEC_LINKER_CREATED
3628 | SEC_THREAD_LOCAL));
3629 if (tcomm == NULL)
3630 goto error_free_vers;
3632 sec = tcomm;
3634 else if (bed->elf_add_symbol_hook)
3636 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3637 &sec, &value))
3638 goto error_free_vers;
3640 /* The hook function sets the name to NULL if this symbol
3641 should be skipped for some reason. */
3642 if (name == NULL)
3643 continue;
3646 /* Sanity check that all possibilities were handled. */
3647 if (sec == NULL)
3649 bfd_set_error (bfd_error_bad_value);
3650 goto error_free_vers;
3653 if (bfd_is_und_section (sec)
3654 || bfd_is_com_section (sec))
3655 definition = FALSE;
3656 else
3657 definition = TRUE;
3659 size_change_ok = FALSE;
3660 type_change_ok = bed->type_change_ok;
3661 old_alignment = 0;
3662 old_bfd = NULL;
3663 new_sec = sec;
3665 if (is_elf_hash_table (htab))
3667 Elf_Internal_Versym iver;
3668 unsigned int vernum = 0;
3669 bfd_boolean skip;
3671 if (ever == NULL)
3673 if (info->default_imported_symver)
3674 /* Use the default symbol version created earlier. */
3675 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3676 else
3677 iver.vs_vers = 0;
3679 else
3680 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3682 vernum = iver.vs_vers & VERSYM_VERSION;
3684 /* If this is a hidden symbol, or if it is not version
3685 1, we append the version name to the symbol name.
3686 However, we do not modify a non-hidden absolute symbol
3687 if it is not a function, because it might be the version
3688 symbol itself. FIXME: What if it isn't? */
3689 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3690 || (vernum > 1 && (! bfd_is_abs_section (sec)
3691 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3693 const char *verstr;
3694 size_t namelen, verlen, newlen;
3695 char *newname, *p;
3697 if (isym->st_shndx != SHN_UNDEF)
3699 if (vernum > elf_tdata (abfd)->cverdefs)
3700 verstr = NULL;
3701 else if (vernum > 1)
3702 verstr =
3703 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3704 else
3705 verstr = "";
3707 if (verstr == NULL)
3709 (*_bfd_error_handler)
3710 (_("%B: %s: invalid version %u (max %d)"),
3711 abfd, name, vernum,
3712 elf_tdata (abfd)->cverdefs);
3713 bfd_set_error (bfd_error_bad_value);
3714 goto error_free_vers;
3717 else
3719 /* We cannot simply test for the number of
3720 entries in the VERNEED section since the
3721 numbers for the needed versions do not start
3722 at 0. */
3723 Elf_Internal_Verneed *t;
3725 verstr = NULL;
3726 for (t = elf_tdata (abfd)->verref;
3727 t != NULL;
3728 t = t->vn_nextref)
3730 Elf_Internal_Vernaux *a;
3732 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3734 if (a->vna_other == vernum)
3736 verstr = a->vna_nodename;
3737 break;
3740 if (a != NULL)
3741 break;
3743 if (verstr == NULL)
3745 (*_bfd_error_handler)
3746 (_("%B: %s: invalid needed version %d"),
3747 abfd, name, vernum);
3748 bfd_set_error (bfd_error_bad_value);
3749 goto error_free_vers;
3753 namelen = strlen (name);
3754 verlen = strlen (verstr);
3755 newlen = namelen + verlen + 2;
3756 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3757 && isym->st_shndx != SHN_UNDEF)
3758 ++newlen;
3760 newname = bfd_hash_allocate (&htab->root.table, newlen);
3761 if (newname == NULL)
3762 goto error_free_vers;
3763 memcpy (newname, name, namelen);
3764 p = newname + namelen;
3765 *p++ = ELF_VER_CHR;
3766 /* If this is a defined non-hidden version symbol,
3767 we add another @ to the name. This indicates the
3768 default version of the symbol. */
3769 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3770 && isym->st_shndx != SHN_UNDEF)
3771 *p++ = ELF_VER_CHR;
3772 memcpy (p, verstr, verlen + 1);
3774 name = newname;
3777 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3778 &value, &old_alignment,
3779 sym_hash, &skip, &override,
3780 &type_change_ok, &size_change_ok))
3781 goto error_free_vers;
3783 if (skip)
3784 continue;
3786 if (override)
3787 definition = FALSE;
3789 h = *sym_hash;
3790 while (h->root.type == bfd_link_hash_indirect
3791 || h->root.type == bfd_link_hash_warning)
3792 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3794 /* Remember the old alignment if this is a common symbol, so
3795 that we don't reduce the alignment later on. We can't
3796 check later, because _bfd_generic_link_add_one_symbol
3797 will set a default for the alignment which we want to
3798 override. We also remember the old bfd where the existing
3799 definition comes from. */
3800 switch (h->root.type)
3802 default:
3803 break;
3805 case bfd_link_hash_defined:
3806 case bfd_link_hash_defweak:
3807 old_bfd = h->root.u.def.section->owner;
3808 break;
3810 case bfd_link_hash_common:
3811 old_bfd = h->root.u.c.p->section->owner;
3812 old_alignment = h->root.u.c.p->alignment_power;
3813 break;
3816 if (elf_tdata (abfd)->verdef != NULL
3817 && ! override
3818 && vernum > 1
3819 && definition)
3820 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3823 if (! (_bfd_generic_link_add_one_symbol
3824 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3825 (struct bfd_link_hash_entry **) sym_hash)))
3826 goto error_free_vers;
3828 h = *sym_hash;
3829 while (h->root.type == bfd_link_hash_indirect
3830 || h->root.type == bfd_link_hash_warning)
3831 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3832 *sym_hash = h;
3834 new_weakdef = FALSE;
3835 if (dynamic
3836 && definition
3837 && (flags & BSF_WEAK) != 0
3838 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3839 && is_elf_hash_table (htab)
3840 && h->u.weakdef == NULL)
3842 /* Keep a list of all weak defined non function symbols from
3843 a dynamic object, using the weakdef field. Later in this
3844 function we will set the weakdef field to the correct
3845 value. We only put non-function symbols from dynamic
3846 objects on this list, because that happens to be the only
3847 time we need to know the normal symbol corresponding to a
3848 weak symbol, and the information is time consuming to
3849 figure out. If the weakdef field is not already NULL,
3850 then this symbol was already defined by some previous
3851 dynamic object, and we will be using that previous
3852 definition anyhow. */
3854 h->u.weakdef = weaks;
3855 weaks = h;
3856 new_weakdef = TRUE;
3859 /* Set the alignment of a common symbol. */
3860 if ((common || bfd_is_com_section (sec))
3861 && h->root.type == bfd_link_hash_common)
3863 unsigned int align;
3865 if (common)
3866 align = bfd_log2 (isym->st_value);
3867 else
3869 /* The new symbol is a common symbol in a shared object.
3870 We need to get the alignment from the section. */
3871 align = new_sec->alignment_power;
3873 if (align > old_alignment
3874 /* Permit an alignment power of zero if an alignment of one
3875 is specified and no other alignments have been specified. */
3876 || (isym->st_value == 1 && old_alignment == 0))
3877 h->root.u.c.p->alignment_power = align;
3878 else
3879 h->root.u.c.p->alignment_power = old_alignment;
3882 if (is_elf_hash_table (htab))
3884 bfd_boolean dynsym;
3886 /* Check the alignment when a common symbol is involved. This
3887 can change when a common symbol is overridden by a normal
3888 definition or a common symbol is ignored due to the old
3889 normal definition. We need to make sure the maximum
3890 alignment is maintained. */
3891 if ((old_alignment || common)
3892 && h->root.type != bfd_link_hash_common)
3894 unsigned int common_align;
3895 unsigned int normal_align;
3896 unsigned int symbol_align;
3897 bfd *normal_bfd;
3898 bfd *common_bfd;
3900 symbol_align = ffs (h->root.u.def.value) - 1;
3901 if (h->root.u.def.section->owner != NULL
3902 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3904 normal_align = h->root.u.def.section->alignment_power;
3905 if (normal_align > symbol_align)
3906 normal_align = symbol_align;
3908 else
3909 normal_align = symbol_align;
3911 if (old_alignment)
3913 common_align = old_alignment;
3914 common_bfd = old_bfd;
3915 normal_bfd = abfd;
3917 else
3919 common_align = bfd_log2 (isym->st_value);
3920 common_bfd = abfd;
3921 normal_bfd = old_bfd;
3924 if (normal_align < common_align)
3925 (*_bfd_error_handler)
3926 (_("Warning: alignment %u of symbol `%s' in %B"
3927 " is smaller than %u in %B"),
3928 normal_bfd, common_bfd,
3929 1 << normal_align, name, 1 << common_align);
3932 /* Remember the symbol size and type. */
3933 if (isym->st_size != 0
3934 && (definition || h->size == 0))
3936 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3937 (*_bfd_error_handler)
3938 (_("Warning: size of symbol `%s' changed"
3939 " from %lu in %B to %lu in %B"),
3940 old_bfd, abfd,
3941 name, (unsigned long) h->size,
3942 (unsigned long) isym->st_size);
3944 h->size = isym->st_size;
3947 /* If this is a common symbol, then we always want H->SIZE
3948 to be the size of the common symbol. The code just above
3949 won't fix the size if a common symbol becomes larger. We
3950 don't warn about a size change here, because that is
3951 covered by --warn-common. */
3952 if (h->root.type == bfd_link_hash_common)
3953 h->size = h->root.u.c.size;
3955 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3956 && (definition || h->type == STT_NOTYPE))
3958 if (h->type != STT_NOTYPE
3959 && h->type != ELF_ST_TYPE (isym->st_info)
3960 && ! type_change_ok)
3961 (*_bfd_error_handler)
3962 (_("Warning: type of symbol `%s' changed"
3963 " from %d to %d in %B"),
3964 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3966 h->type = ELF_ST_TYPE (isym->st_info);
3969 /* If st_other has a processor-specific meaning, specific
3970 code might be needed here. We never merge the visibility
3971 attribute with the one from a dynamic object. */
3972 if (bed->elf_backend_merge_symbol_attribute)
3973 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3974 dynamic);
3976 /* If this symbol has default visibility and the user has requested
3977 we not re-export it, then mark it as hidden. */
3978 if (definition && !dynamic
3979 && (abfd->no_export
3980 || (abfd->my_archive && abfd->my_archive->no_export))
3981 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3982 isym->st_other = (STV_HIDDEN
3983 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
3985 if (isym->st_other != 0 && !dynamic)
3987 unsigned char hvis, symvis, other, nvis;
3989 /* Take the balance of OTHER from the definition. */
3990 other = (definition ? isym->st_other : h->other);
3991 other &= ~ ELF_ST_VISIBILITY (-1);
3993 /* Combine visibilities, using the most constraining one. */
3994 hvis = ELF_ST_VISIBILITY (h->other);
3995 symvis = ELF_ST_VISIBILITY (isym->st_other);
3996 if (! hvis)
3997 nvis = symvis;
3998 else if (! symvis)
3999 nvis = hvis;
4000 else
4001 nvis = hvis < symvis ? hvis : symvis;
4003 h->other = other | nvis;
4006 /* Set a flag in the hash table entry indicating the type of
4007 reference or definition we just found. Keep a count of
4008 the number of dynamic symbols we find. A dynamic symbol
4009 is one which is referenced or defined by both a regular
4010 object and a shared object. */
4011 dynsym = FALSE;
4012 if (! dynamic)
4014 if (! definition)
4016 h->ref_regular = 1;
4017 if (bind != STB_WEAK)
4018 h->ref_regular_nonweak = 1;
4020 else
4021 h->def_regular = 1;
4022 if (! info->executable
4023 || h->def_dynamic
4024 || h->ref_dynamic)
4025 dynsym = TRUE;
4027 else
4029 if (! definition)
4030 h->ref_dynamic = 1;
4031 else
4032 h->def_dynamic = 1;
4033 if (h->def_regular
4034 || h->ref_regular
4035 || (h->u.weakdef != NULL
4036 && ! new_weakdef
4037 && h->u.weakdef->dynindx != -1))
4038 dynsym = TRUE;
4041 /* Check to see if we need to add an indirect symbol for
4042 the default name. */
4043 if (definition || h->root.type == bfd_link_hash_common)
4044 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4045 &sec, &value, &dynsym,
4046 override))
4047 goto error_free_vers;
4049 if (definition && !dynamic)
4051 char *p = strchr (name, ELF_VER_CHR);
4052 if (p != NULL && p[1] != ELF_VER_CHR)
4054 /* Queue non-default versions so that .symver x, x@FOO
4055 aliases can be checked. */
4056 if (!nondeflt_vers)
4058 amt = ((isymend - isym + 1)
4059 * sizeof (struct elf_link_hash_entry *));
4060 nondeflt_vers = bfd_malloc (amt);
4062 nondeflt_vers[nondeflt_vers_cnt++] = h;
4066 if (dynsym && h->dynindx == -1)
4068 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4069 goto error_free_vers;
4070 if (h->u.weakdef != NULL
4071 && ! new_weakdef
4072 && h->u.weakdef->dynindx == -1)
4074 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4075 goto error_free_vers;
4078 else if (dynsym && h->dynindx != -1)
4079 /* If the symbol already has a dynamic index, but
4080 visibility says it should not be visible, turn it into
4081 a local symbol. */
4082 switch (ELF_ST_VISIBILITY (h->other))
4084 case STV_INTERNAL:
4085 case STV_HIDDEN:
4086 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4087 dynsym = FALSE;
4088 break;
4091 if (!add_needed
4092 && definition
4093 && dynsym
4094 && h->ref_regular)
4096 int ret;
4097 const char *soname = elf_dt_name (abfd);
4099 /* A symbol from a library loaded via DT_NEEDED of some
4100 other library is referenced by a regular object.
4101 Add a DT_NEEDED entry for it. Issue an error if
4102 --no-add-needed is used. */
4103 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4105 (*_bfd_error_handler)
4106 (_("%s: invalid DSO for symbol `%s' definition"),
4107 abfd, name);
4108 bfd_set_error (bfd_error_bad_value);
4109 goto error_free_vers;
4112 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4114 add_needed = TRUE;
4115 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4116 if (ret < 0)
4117 goto error_free_vers;
4119 BFD_ASSERT (ret == 0);
4124 if (extversym != NULL)
4126 free (extversym);
4127 extversym = NULL;
4130 if (isymbuf != NULL)
4132 free (isymbuf);
4133 isymbuf = NULL;
4136 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4138 unsigned int i;
4140 /* Restore the symbol table. */
4141 old_hash = (char *) old_tab + tabsize;
4142 old_ent = (char *) old_hash + hashsize;
4143 sym_hash = elf_sym_hashes (abfd);
4144 memcpy (htab->root.table.table, old_tab, tabsize);
4145 memcpy (sym_hash, old_hash, hashsize);
4146 htab->root.undefs = old_undefs;
4147 htab->root.undefs_tail = old_undefs_tail;
4148 for (i = 0; i < htab->root.table.size; i++)
4150 struct bfd_hash_entry *p;
4151 struct elf_link_hash_entry *h;
4153 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4155 h = (struct elf_link_hash_entry *) p;
4156 if (h->root.type == bfd_link_hash_warning)
4157 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4158 if (h->dynindx >= old_dynsymcount)
4159 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4161 memcpy (p, old_ent, htab->root.table.entsize);
4162 old_ent = (char *) old_ent + htab->root.table.entsize;
4163 h = (struct elf_link_hash_entry *) p;
4164 if (h->root.type == bfd_link_hash_warning)
4166 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4167 old_ent = (char *) old_ent + htab->root.table.entsize;
4172 free (old_tab);
4173 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4174 alloc_mark);
4175 if (nondeflt_vers != NULL)
4176 free (nondeflt_vers);
4177 return TRUE;
4180 if (old_tab != NULL)
4182 free (old_tab);
4183 old_tab = NULL;
4186 /* Now that all the symbols from this input file are created, handle
4187 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4188 if (nondeflt_vers != NULL)
4190 bfd_size_type cnt, symidx;
4192 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4194 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4195 char *shortname, *p;
4197 p = strchr (h->root.root.string, ELF_VER_CHR);
4198 if (p == NULL
4199 || (h->root.type != bfd_link_hash_defined
4200 && h->root.type != bfd_link_hash_defweak))
4201 continue;
4203 amt = p - h->root.root.string;
4204 shortname = bfd_malloc (amt + 1);
4205 memcpy (shortname, h->root.root.string, amt);
4206 shortname[amt] = '\0';
4208 hi = (struct elf_link_hash_entry *)
4209 bfd_link_hash_lookup (&htab->root, shortname,
4210 FALSE, FALSE, FALSE);
4211 if (hi != NULL
4212 && hi->root.type == h->root.type
4213 && hi->root.u.def.value == h->root.u.def.value
4214 && hi->root.u.def.section == h->root.u.def.section)
4216 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4217 hi->root.type = bfd_link_hash_indirect;
4218 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4219 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4220 sym_hash = elf_sym_hashes (abfd);
4221 if (sym_hash)
4222 for (symidx = 0; symidx < extsymcount; ++symidx)
4223 if (sym_hash[symidx] == hi)
4225 sym_hash[symidx] = h;
4226 break;
4229 free (shortname);
4231 free (nondeflt_vers);
4232 nondeflt_vers = NULL;
4235 /* Now set the weakdefs field correctly for all the weak defined
4236 symbols we found. The only way to do this is to search all the
4237 symbols. Since we only need the information for non functions in
4238 dynamic objects, that's the only time we actually put anything on
4239 the list WEAKS. We need this information so that if a regular
4240 object refers to a symbol defined weakly in a dynamic object, the
4241 real symbol in the dynamic object is also put in the dynamic
4242 symbols; we also must arrange for both symbols to point to the
4243 same memory location. We could handle the general case of symbol
4244 aliasing, but a general symbol alias can only be generated in
4245 assembler code, handling it correctly would be very time
4246 consuming, and other ELF linkers don't handle general aliasing
4247 either. */
4248 if (weaks != NULL)
4250 struct elf_link_hash_entry **hpp;
4251 struct elf_link_hash_entry **hppend;
4252 struct elf_link_hash_entry **sorted_sym_hash;
4253 struct elf_link_hash_entry *h;
4254 size_t sym_count;
4256 /* Since we have to search the whole symbol list for each weak
4257 defined symbol, search time for N weak defined symbols will be
4258 O(N^2). Binary search will cut it down to O(NlogN). */
4259 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4260 sorted_sym_hash = bfd_malloc (amt);
4261 if (sorted_sym_hash == NULL)
4262 goto error_return;
4263 sym_hash = sorted_sym_hash;
4264 hpp = elf_sym_hashes (abfd);
4265 hppend = hpp + extsymcount;
4266 sym_count = 0;
4267 for (; hpp < hppend; hpp++)
4269 h = *hpp;
4270 if (h != NULL
4271 && h->root.type == bfd_link_hash_defined
4272 && h->type != STT_FUNC)
4274 *sym_hash = h;
4275 sym_hash++;
4276 sym_count++;
4280 qsort (sorted_sym_hash, sym_count,
4281 sizeof (struct elf_link_hash_entry *),
4282 elf_sort_symbol);
4284 while (weaks != NULL)
4286 struct elf_link_hash_entry *hlook;
4287 asection *slook;
4288 bfd_vma vlook;
4289 long ilook;
4290 size_t i, j, idx;
4292 hlook = weaks;
4293 weaks = hlook->u.weakdef;
4294 hlook->u.weakdef = NULL;
4296 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4297 || hlook->root.type == bfd_link_hash_defweak
4298 || hlook->root.type == bfd_link_hash_common
4299 || hlook->root.type == bfd_link_hash_indirect);
4300 slook = hlook->root.u.def.section;
4301 vlook = hlook->root.u.def.value;
4303 ilook = -1;
4304 i = 0;
4305 j = sym_count;
4306 while (i < j)
4308 bfd_signed_vma vdiff;
4309 idx = (i + j) / 2;
4310 h = sorted_sym_hash [idx];
4311 vdiff = vlook - h->root.u.def.value;
4312 if (vdiff < 0)
4313 j = idx;
4314 else if (vdiff > 0)
4315 i = idx + 1;
4316 else
4318 long sdiff = slook->id - h->root.u.def.section->id;
4319 if (sdiff < 0)
4320 j = idx;
4321 else if (sdiff > 0)
4322 i = idx + 1;
4323 else
4325 ilook = idx;
4326 break;
4331 /* We didn't find a value/section match. */
4332 if (ilook == -1)
4333 continue;
4335 for (i = ilook; i < sym_count; i++)
4337 h = sorted_sym_hash [i];
4339 /* Stop if value or section doesn't match. */
4340 if (h->root.u.def.value != vlook
4341 || h->root.u.def.section != slook)
4342 break;
4343 else if (h != hlook)
4345 hlook->u.weakdef = h;
4347 /* If the weak definition is in the list of dynamic
4348 symbols, make sure the real definition is put
4349 there as well. */
4350 if (hlook->dynindx != -1 && h->dynindx == -1)
4352 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4353 goto error_return;
4356 /* If the real definition is in the list of dynamic
4357 symbols, make sure the weak definition is put
4358 there as well. If we don't do this, then the
4359 dynamic loader might not merge the entries for the
4360 real definition and the weak definition. */
4361 if (h->dynindx != -1 && hlook->dynindx == -1)
4363 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4364 goto error_return;
4366 break;
4371 free (sorted_sym_hash);
4374 if (bed->check_directives)
4375 (*bed->check_directives) (abfd, info);
4377 /* If this object is the same format as the output object, and it is
4378 not a shared library, then let the backend look through the
4379 relocs.
4381 This is required to build global offset table entries and to
4382 arrange for dynamic relocs. It is not required for the
4383 particular common case of linking non PIC code, even when linking
4384 against shared libraries, but unfortunately there is no way of
4385 knowing whether an object file has been compiled PIC or not.
4386 Looking through the relocs is not particularly time consuming.
4387 The problem is that we must either (1) keep the relocs in memory,
4388 which causes the linker to require additional runtime memory or
4389 (2) read the relocs twice from the input file, which wastes time.
4390 This would be a good case for using mmap.
4392 I have no idea how to handle linking PIC code into a file of a
4393 different format. It probably can't be done. */
4394 if (! dynamic
4395 && is_elf_hash_table (htab)
4396 && htab->root.creator == abfd->xvec
4397 && bed->check_relocs != NULL)
4399 asection *o;
4401 for (o = abfd->sections; o != NULL; o = o->next)
4403 Elf_Internal_Rela *internal_relocs;
4404 bfd_boolean ok;
4406 if ((o->flags & SEC_RELOC) == 0
4407 || o->reloc_count == 0
4408 || ((info->strip == strip_all || info->strip == strip_debugger)
4409 && (o->flags & SEC_DEBUGGING) != 0)
4410 || bfd_is_abs_section (o->output_section))
4411 continue;
4413 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4414 info->keep_memory);
4415 if (internal_relocs == NULL)
4416 goto error_return;
4418 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4420 if (elf_section_data (o)->relocs != internal_relocs)
4421 free (internal_relocs);
4423 if (! ok)
4424 goto error_return;
4428 /* If this is a non-traditional link, try to optimize the handling
4429 of the .stab/.stabstr sections. */
4430 if (! dynamic
4431 && ! info->traditional_format
4432 && is_elf_hash_table (htab)
4433 && (info->strip != strip_all && info->strip != strip_debugger))
4435 asection *stabstr;
4437 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4438 if (stabstr != NULL)
4440 bfd_size_type string_offset = 0;
4441 asection *stab;
4443 for (stab = abfd->sections; stab; stab = stab->next)
4444 if (strncmp (".stab", stab->name, 5) == 0
4445 && (!stab->name[5] ||
4446 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4447 && (stab->flags & SEC_MERGE) == 0
4448 && !bfd_is_abs_section (stab->output_section))
4450 struct bfd_elf_section_data *secdata;
4452 secdata = elf_section_data (stab);
4453 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4454 stabstr, &secdata->sec_info,
4455 &string_offset))
4456 goto error_return;
4457 if (secdata->sec_info)
4458 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4463 if (is_elf_hash_table (htab) && add_needed)
4465 /* Add this bfd to the loaded list. */
4466 struct elf_link_loaded_list *n;
4468 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4469 if (n == NULL)
4470 goto error_return;
4471 n->abfd = abfd;
4472 n->next = htab->loaded;
4473 htab->loaded = n;
4476 return TRUE;
4478 error_free_vers:
4479 if (old_tab != NULL)
4480 free (old_tab);
4481 if (nondeflt_vers != NULL)
4482 free (nondeflt_vers);
4483 if (extversym != NULL)
4484 free (extversym);
4485 error_free_sym:
4486 if (isymbuf != NULL)
4487 free (isymbuf);
4488 error_return:
4489 return FALSE;
4492 /* Return the linker hash table entry of a symbol that might be
4493 satisfied by an archive symbol. Return -1 on error. */
4495 struct elf_link_hash_entry *
4496 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4497 struct bfd_link_info *info,
4498 const char *name)
4500 struct elf_link_hash_entry *h;
4501 char *p, *copy;
4502 size_t len, first;
4504 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4505 if (h != NULL)
4506 return h;
4508 /* If this is a default version (the name contains @@), look up the
4509 symbol again with only one `@' as well as without the version.
4510 The effect is that references to the symbol with and without the
4511 version will be matched by the default symbol in the archive. */
4513 p = strchr (name, ELF_VER_CHR);
4514 if (p == NULL || p[1] != ELF_VER_CHR)
4515 return h;
4517 /* First check with only one `@'. */
4518 len = strlen (name);
4519 copy = bfd_alloc (abfd, len);
4520 if (copy == NULL)
4521 return (struct elf_link_hash_entry *) 0 - 1;
4523 first = p - name + 1;
4524 memcpy (copy, name, first);
4525 memcpy (copy + first, name + first + 1, len - first);
4527 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4528 if (h == NULL)
4530 /* We also need to check references to the symbol without the
4531 version. */
4532 copy[first - 1] = '\0';
4533 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4534 FALSE, FALSE, FALSE);
4537 bfd_release (abfd, copy);
4538 return h;
4541 /* Add symbols from an ELF archive file to the linker hash table. We
4542 don't use _bfd_generic_link_add_archive_symbols because of a
4543 problem which arises on UnixWare. The UnixWare libc.so is an
4544 archive which includes an entry libc.so.1 which defines a bunch of
4545 symbols. The libc.so archive also includes a number of other
4546 object files, which also define symbols, some of which are the same
4547 as those defined in libc.so.1. Correct linking requires that we
4548 consider each object file in turn, and include it if it defines any
4549 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4550 this; it looks through the list of undefined symbols, and includes
4551 any object file which defines them. When this algorithm is used on
4552 UnixWare, it winds up pulling in libc.so.1 early and defining a
4553 bunch of symbols. This means that some of the other objects in the
4554 archive are not included in the link, which is incorrect since they
4555 precede libc.so.1 in the archive.
4557 Fortunately, ELF archive handling is simpler than that done by
4558 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4559 oddities. In ELF, if we find a symbol in the archive map, and the
4560 symbol is currently undefined, we know that we must pull in that
4561 object file.
4563 Unfortunately, we do have to make multiple passes over the symbol
4564 table until nothing further is resolved. */
4566 static bfd_boolean
4567 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4569 symindex c;
4570 bfd_boolean *defined = NULL;
4571 bfd_boolean *included = NULL;
4572 carsym *symdefs;
4573 bfd_boolean loop;
4574 bfd_size_type amt;
4575 const struct elf_backend_data *bed;
4576 struct elf_link_hash_entry * (*archive_symbol_lookup)
4577 (bfd *, struct bfd_link_info *, const char *);
4579 if (! bfd_has_map (abfd))
4581 /* An empty archive is a special case. */
4582 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4583 return TRUE;
4584 bfd_set_error (bfd_error_no_armap);
4585 return FALSE;
4588 /* Keep track of all symbols we know to be already defined, and all
4589 files we know to be already included. This is to speed up the
4590 second and subsequent passes. */
4591 c = bfd_ardata (abfd)->symdef_count;
4592 if (c == 0)
4593 return TRUE;
4594 amt = c;
4595 amt *= sizeof (bfd_boolean);
4596 defined = bfd_zmalloc (amt);
4597 included = bfd_zmalloc (amt);
4598 if (defined == NULL || included == NULL)
4599 goto error_return;
4601 symdefs = bfd_ardata (abfd)->symdefs;
4602 bed = get_elf_backend_data (abfd);
4603 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4607 file_ptr last;
4608 symindex i;
4609 carsym *symdef;
4610 carsym *symdefend;
4612 loop = FALSE;
4613 last = -1;
4615 symdef = symdefs;
4616 symdefend = symdef + c;
4617 for (i = 0; symdef < symdefend; symdef++, i++)
4619 struct elf_link_hash_entry *h;
4620 bfd *element;
4621 struct bfd_link_hash_entry *undefs_tail;
4622 symindex mark;
4624 if (defined[i] || included[i])
4625 continue;
4626 if (symdef->file_offset == last)
4628 included[i] = TRUE;
4629 continue;
4632 h = archive_symbol_lookup (abfd, info, symdef->name);
4633 if (h == (struct elf_link_hash_entry *) 0 - 1)
4634 goto error_return;
4636 if (h == NULL)
4637 continue;
4639 if (h->root.type == bfd_link_hash_common)
4641 /* We currently have a common symbol. The archive map contains
4642 a reference to this symbol, so we may want to include it. We
4643 only want to include it however, if this archive element
4644 contains a definition of the symbol, not just another common
4645 declaration of it.
4647 Unfortunately some archivers (including GNU ar) will put
4648 declarations of common symbols into their archive maps, as
4649 well as real definitions, so we cannot just go by the archive
4650 map alone. Instead we must read in the element's symbol
4651 table and check that to see what kind of symbol definition
4652 this is. */
4653 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4654 continue;
4656 else if (h->root.type != bfd_link_hash_undefined)
4658 if (h->root.type != bfd_link_hash_undefweak)
4659 defined[i] = TRUE;
4660 continue;
4663 /* We need to include this archive member. */
4664 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4665 if (element == NULL)
4666 goto error_return;
4668 if (! bfd_check_format (element, bfd_object))
4669 goto error_return;
4671 /* Doublecheck that we have not included this object
4672 already--it should be impossible, but there may be
4673 something wrong with the archive. */
4674 if (element->archive_pass != 0)
4676 bfd_set_error (bfd_error_bad_value);
4677 goto error_return;
4679 element->archive_pass = 1;
4681 undefs_tail = info->hash->undefs_tail;
4683 if (! (*info->callbacks->add_archive_element) (info, element,
4684 symdef->name))
4685 goto error_return;
4686 if (! bfd_link_add_symbols (element, info))
4687 goto error_return;
4689 /* If there are any new undefined symbols, we need to make
4690 another pass through the archive in order to see whether
4691 they can be defined. FIXME: This isn't perfect, because
4692 common symbols wind up on undefs_tail and because an
4693 undefined symbol which is defined later on in this pass
4694 does not require another pass. This isn't a bug, but it
4695 does make the code less efficient than it could be. */
4696 if (undefs_tail != info->hash->undefs_tail)
4697 loop = TRUE;
4699 /* Look backward to mark all symbols from this object file
4700 which we have already seen in this pass. */
4701 mark = i;
4704 included[mark] = TRUE;
4705 if (mark == 0)
4706 break;
4707 --mark;
4709 while (symdefs[mark].file_offset == symdef->file_offset);
4711 /* We mark subsequent symbols from this object file as we go
4712 on through the loop. */
4713 last = symdef->file_offset;
4716 while (loop);
4718 free (defined);
4719 free (included);
4721 return TRUE;
4723 error_return:
4724 if (defined != NULL)
4725 free (defined);
4726 if (included != NULL)
4727 free (included);
4728 return FALSE;
4731 /* Given an ELF BFD, add symbols to the global hash table as
4732 appropriate. */
4734 bfd_boolean
4735 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4737 switch (bfd_get_format (abfd))
4739 case bfd_object:
4740 return elf_link_add_object_symbols (abfd, info);
4741 case bfd_archive:
4742 return elf_link_add_archive_symbols (abfd, info);
4743 default:
4744 bfd_set_error (bfd_error_wrong_format);
4745 return FALSE;
4749 /* This function will be called though elf_link_hash_traverse to store
4750 all hash value of the exported symbols in an array. */
4752 static bfd_boolean
4753 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4755 unsigned long **valuep = data;
4756 const char *name;
4757 char *p;
4758 unsigned long ha;
4759 char *alc = NULL;
4761 if (h->root.type == bfd_link_hash_warning)
4762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4764 /* Ignore indirect symbols. These are added by the versioning code. */
4765 if (h->dynindx == -1)
4766 return TRUE;
4768 name = h->root.root.string;
4769 p = strchr (name, ELF_VER_CHR);
4770 if (p != NULL)
4772 alc = bfd_malloc (p - name + 1);
4773 memcpy (alc, name, p - name);
4774 alc[p - name] = '\0';
4775 name = alc;
4778 /* Compute the hash value. */
4779 ha = bfd_elf_hash (name);
4781 /* Store the found hash value in the array given as the argument. */
4782 *(*valuep)++ = ha;
4784 /* And store it in the struct so that we can put it in the hash table
4785 later. */
4786 h->u.elf_hash_value = ha;
4788 if (alc != NULL)
4789 free (alc);
4791 return TRUE;
4794 /* Array used to determine the number of hash table buckets to use
4795 based on the number of symbols there are. If there are fewer than
4796 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4797 fewer than 37 we use 17 buckets, and so forth. We never use more
4798 than 32771 buckets. */
4800 static const size_t elf_buckets[] =
4802 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4803 16411, 32771, 0
4806 /* Compute bucket count for hashing table. We do not use a static set
4807 of possible tables sizes anymore. Instead we determine for all
4808 possible reasonable sizes of the table the outcome (i.e., the
4809 number of collisions etc) and choose the best solution. The
4810 weighting functions are not too simple to allow the table to grow
4811 without bounds. Instead one of the weighting factors is the size.
4812 Therefore the result is always a good payoff between few collisions
4813 (= short chain lengths) and table size. */
4814 static size_t
4815 compute_bucket_count (struct bfd_link_info *info)
4817 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4818 size_t best_size = 0;
4819 unsigned long int *hashcodes;
4820 unsigned long int *hashcodesp;
4821 unsigned long int i;
4822 bfd_size_type amt;
4824 /* Compute the hash values for all exported symbols. At the same
4825 time store the values in an array so that we could use them for
4826 optimizations. */
4827 amt = dynsymcount;
4828 amt *= sizeof (unsigned long int);
4829 hashcodes = bfd_malloc (amt);
4830 if (hashcodes == NULL)
4831 return 0;
4832 hashcodesp = hashcodes;
4834 /* Put all hash values in HASHCODES. */
4835 elf_link_hash_traverse (elf_hash_table (info),
4836 elf_collect_hash_codes, &hashcodesp);
4838 /* We have a problem here. The following code to optimize the table
4839 size requires an integer type with more the 32 bits. If
4840 BFD_HOST_U_64_BIT is set we know about such a type. */
4841 #ifdef BFD_HOST_U_64_BIT
4842 if (info->optimize)
4844 unsigned long int nsyms = hashcodesp - hashcodes;
4845 size_t minsize;
4846 size_t maxsize;
4847 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4848 unsigned long int *counts ;
4849 bfd *dynobj = elf_hash_table (info)->dynobj;
4850 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4852 /* Possible optimization parameters: if we have NSYMS symbols we say
4853 that the hashing table must at least have NSYMS/4 and at most
4854 2*NSYMS buckets. */
4855 minsize = nsyms / 4;
4856 if (minsize == 0)
4857 minsize = 1;
4858 best_size = maxsize = nsyms * 2;
4860 /* Create array where we count the collisions in. We must use bfd_malloc
4861 since the size could be large. */
4862 amt = maxsize;
4863 amt *= sizeof (unsigned long int);
4864 counts = bfd_malloc (amt);
4865 if (counts == NULL)
4867 free (hashcodes);
4868 return 0;
4871 /* Compute the "optimal" size for the hash table. The criteria is a
4872 minimal chain length. The minor criteria is (of course) the size
4873 of the table. */
4874 for (i = minsize; i < maxsize; ++i)
4876 /* Walk through the array of hashcodes and count the collisions. */
4877 BFD_HOST_U_64_BIT max;
4878 unsigned long int j;
4879 unsigned long int fact;
4881 memset (counts, '\0', i * sizeof (unsigned long int));
4883 /* Determine how often each hash bucket is used. */
4884 for (j = 0; j < nsyms; ++j)
4885 ++counts[hashcodes[j] % i];
4887 /* For the weight function we need some information about the
4888 pagesize on the target. This is information need not be 100%
4889 accurate. Since this information is not available (so far) we
4890 define it here to a reasonable default value. If it is crucial
4891 to have a better value some day simply define this value. */
4892 # ifndef BFD_TARGET_PAGESIZE
4893 # define BFD_TARGET_PAGESIZE (4096)
4894 # endif
4896 /* We in any case need 2 + NSYMS entries for the size values and
4897 the chains. */
4898 max = (2 + nsyms) * (bed->s->arch_size / 8);
4900 # if 1
4901 /* Variant 1: optimize for short chains. We add the squares
4902 of all the chain lengths (which favors many small chain
4903 over a few long chains). */
4904 for (j = 0; j < i; ++j)
4905 max += counts[j] * counts[j];
4907 /* This adds penalties for the overall size of the table. */
4908 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4909 max *= fact * fact;
4910 # else
4911 /* Variant 2: Optimize a lot more for small table. Here we
4912 also add squares of the size but we also add penalties for
4913 empty slots (the +1 term). */
4914 for (j = 0; j < i; ++j)
4915 max += (1 + counts[j]) * (1 + counts[j]);
4917 /* The overall size of the table is considered, but not as
4918 strong as in variant 1, where it is squared. */
4919 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4920 max *= fact;
4921 # endif
4923 /* Compare with current best results. */
4924 if (max < best_chlen)
4926 best_chlen = max;
4927 best_size = i;
4931 free (counts);
4933 else
4934 #endif /* defined (BFD_HOST_U_64_BIT) */
4936 /* This is the fallback solution if no 64bit type is available or if we
4937 are not supposed to spend much time on optimizations. We select the
4938 bucket count using a fixed set of numbers. */
4939 for (i = 0; elf_buckets[i] != 0; i++)
4941 best_size = elf_buckets[i];
4942 if (dynsymcount < elf_buckets[i + 1])
4943 break;
4947 /* Free the arrays we needed. */
4948 free (hashcodes);
4950 return best_size;
4953 /* Set up the sizes and contents of the ELF dynamic sections. This is
4954 called by the ELF linker emulation before_allocation routine. We
4955 must set the sizes of the sections before the linker sets the
4956 addresses of the various sections. */
4958 bfd_boolean
4959 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4960 const char *soname,
4961 const char *rpath,
4962 const char *filter_shlib,
4963 const char * const *auxiliary_filters,
4964 struct bfd_link_info *info,
4965 asection **sinterpptr,
4966 struct bfd_elf_version_tree *verdefs)
4968 bfd_size_type soname_indx;
4969 bfd *dynobj;
4970 const struct elf_backend_data *bed;
4971 struct elf_assign_sym_version_info asvinfo;
4973 *sinterpptr = NULL;
4975 soname_indx = (bfd_size_type) -1;
4977 if (!is_elf_hash_table (info->hash))
4978 return TRUE;
4980 elf_tdata (output_bfd)->relro = info->relro;
4981 if (info->execstack)
4982 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4983 else if (info->noexecstack)
4984 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4985 else
4987 bfd *inputobj;
4988 asection *notesec = NULL;
4989 int exec = 0;
4991 for (inputobj = info->input_bfds;
4992 inputobj;
4993 inputobj = inputobj->link_next)
4995 asection *s;
4997 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4998 continue;
4999 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5000 if (s)
5002 if (s->flags & SEC_CODE)
5003 exec = PF_X;
5004 notesec = s;
5006 else
5007 exec = PF_X;
5009 if (notesec)
5011 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5012 if (exec && info->relocatable
5013 && notesec->output_section != bfd_abs_section_ptr)
5014 notesec->output_section->flags |= SEC_CODE;
5018 /* Any syms created from now on start with -1 in
5019 got.refcount/offset and plt.refcount/offset. */
5020 elf_hash_table (info)->init_got_refcount
5021 = elf_hash_table (info)->init_got_offset;
5022 elf_hash_table (info)->init_plt_refcount
5023 = elf_hash_table (info)->init_plt_offset;
5025 /* The backend may have to create some sections regardless of whether
5026 we're dynamic or not. */
5027 bed = get_elf_backend_data (output_bfd);
5028 if (bed->elf_backend_always_size_sections
5029 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5030 return FALSE;
5032 dynobj = elf_hash_table (info)->dynobj;
5034 /* If there were no dynamic objects in the link, there is nothing to
5035 do here. */
5036 if (dynobj == NULL)
5037 return TRUE;
5039 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5040 return FALSE;
5042 if (elf_hash_table (info)->dynamic_sections_created)
5044 struct elf_info_failed eif;
5045 struct elf_link_hash_entry *h;
5046 asection *dynstr;
5047 struct bfd_elf_version_tree *t;
5048 struct bfd_elf_version_expr *d;
5049 asection *s;
5050 bfd_boolean all_defined;
5052 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5053 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5055 if (soname != NULL)
5057 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5058 soname, TRUE);
5059 if (soname_indx == (bfd_size_type) -1
5060 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5061 return FALSE;
5064 if (info->symbolic)
5066 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5067 return FALSE;
5068 info->flags |= DF_SYMBOLIC;
5071 if (rpath != NULL)
5073 bfd_size_type indx;
5075 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5076 TRUE);
5077 if (indx == (bfd_size_type) -1
5078 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5079 return FALSE;
5081 if (info->new_dtags)
5083 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5084 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5085 return FALSE;
5089 if (filter_shlib != NULL)
5091 bfd_size_type indx;
5093 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5094 filter_shlib, TRUE);
5095 if (indx == (bfd_size_type) -1
5096 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5097 return FALSE;
5100 if (auxiliary_filters != NULL)
5102 const char * const *p;
5104 for (p = auxiliary_filters; *p != NULL; p++)
5106 bfd_size_type indx;
5108 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5109 *p, TRUE);
5110 if (indx == (bfd_size_type) -1
5111 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5112 return FALSE;
5116 eif.info = info;
5117 eif.verdefs = verdefs;
5118 eif.failed = FALSE;
5120 /* If we are supposed to export all symbols into the dynamic symbol
5121 table (this is not the normal case), then do so. */
5122 if (info->export_dynamic)
5124 elf_link_hash_traverse (elf_hash_table (info),
5125 _bfd_elf_export_symbol,
5126 &eif);
5127 if (eif.failed)
5128 return FALSE;
5131 /* Make all global versions with definition. */
5132 for (t = verdefs; t != NULL; t = t->next)
5133 for (d = t->globals.list; d != NULL; d = d->next)
5134 if (!d->symver && d->symbol)
5136 const char *verstr, *name;
5137 size_t namelen, verlen, newlen;
5138 char *newname, *p;
5139 struct elf_link_hash_entry *newh;
5141 name = d->symbol;
5142 namelen = strlen (name);
5143 verstr = t->name;
5144 verlen = strlen (verstr);
5145 newlen = namelen + verlen + 3;
5147 newname = bfd_malloc (newlen);
5148 if (newname == NULL)
5149 return FALSE;
5150 memcpy (newname, name, namelen);
5152 /* Check the hidden versioned definition. */
5153 p = newname + namelen;
5154 *p++ = ELF_VER_CHR;
5155 memcpy (p, verstr, verlen + 1);
5156 newh = elf_link_hash_lookup (elf_hash_table (info),
5157 newname, FALSE, FALSE,
5158 FALSE);
5159 if (newh == NULL
5160 || (newh->root.type != bfd_link_hash_defined
5161 && newh->root.type != bfd_link_hash_defweak))
5163 /* Check the default versioned definition. */
5164 *p++ = ELF_VER_CHR;
5165 memcpy (p, verstr, verlen + 1);
5166 newh = elf_link_hash_lookup (elf_hash_table (info),
5167 newname, FALSE, FALSE,
5168 FALSE);
5170 free (newname);
5172 /* Mark this version if there is a definition and it is
5173 not defined in a shared object. */
5174 if (newh != NULL
5175 && !newh->def_dynamic
5176 && (newh->root.type == bfd_link_hash_defined
5177 || newh->root.type == bfd_link_hash_defweak))
5178 d->symver = 1;
5181 /* Attach all the symbols to their version information. */
5182 asvinfo.output_bfd = output_bfd;
5183 asvinfo.info = info;
5184 asvinfo.verdefs = verdefs;
5185 asvinfo.failed = FALSE;
5187 elf_link_hash_traverse (elf_hash_table (info),
5188 _bfd_elf_link_assign_sym_version,
5189 &asvinfo);
5190 if (asvinfo.failed)
5191 return FALSE;
5193 if (!info->allow_undefined_version)
5195 /* Check if all global versions have a definition. */
5196 all_defined = TRUE;
5197 for (t = verdefs; t != NULL; t = t->next)
5198 for (d = t->globals.list; d != NULL; d = d->next)
5199 if (!d->symver && !d->script)
5201 (*_bfd_error_handler)
5202 (_("%s: undefined version: %s"),
5203 d->pattern, t->name);
5204 all_defined = FALSE;
5207 if (!all_defined)
5209 bfd_set_error (bfd_error_bad_value);
5210 return FALSE;
5214 /* Find all symbols which were defined in a dynamic object and make
5215 the backend pick a reasonable value for them. */
5216 elf_link_hash_traverse (elf_hash_table (info),
5217 _bfd_elf_adjust_dynamic_symbol,
5218 &eif);
5219 if (eif.failed)
5220 return FALSE;
5222 /* Add some entries to the .dynamic section. We fill in some of the
5223 values later, in bfd_elf_final_link, but we must add the entries
5224 now so that we know the final size of the .dynamic section. */
5226 /* If there are initialization and/or finalization functions to
5227 call then add the corresponding DT_INIT/DT_FINI entries. */
5228 h = (info->init_function
5229 ? elf_link_hash_lookup (elf_hash_table (info),
5230 info->init_function, FALSE,
5231 FALSE, FALSE)
5232 : NULL);
5233 if (h != NULL
5234 && (h->ref_regular
5235 || h->def_regular))
5237 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5238 return FALSE;
5240 h = (info->fini_function
5241 ? elf_link_hash_lookup (elf_hash_table (info),
5242 info->fini_function, FALSE,
5243 FALSE, FALSE)
5244 : NULL);
5245 if (h != NULL
5246 && (h->ref_regular
5247 || h->def_regular))
5249 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5250 return FALSE;
5253 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5254 if (s != NULL && s->linker_has_input)
5256 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5257 if (! info->executable)
5259 bfd *sub;
5260 asection *o;
5262 for (sub = info->input_bfds; sub != NULL;
5263 sub = sub->link_next)
5264 for (o = sub->sections; o != NULL; o = o->next)
5265 if (elf_section_data (o)->this_hdr.sh_type
5266 == SHT_PREINIT_ARRAY)
5268 (*_bfd_error_handler)
5269 (_("%B: .preinit_array section is not allowed in DSO"),
5270 sub);
5271 break;
5274 bfd_set_error (bfd_error_nonrepresentable_section);
5275 return FALSE;
5278 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5279 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5280 return FALSE;
5282 s = bfd_get_section_by_name (output_bfd, ".init_array");
5283 if (s != NULL && s->linker_has_input)
5285 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5286 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5287 return FALSE;
5289 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5290 if (s != NULL && s->linker_has_input)
5292 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5293 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5294 return FALSE;
5297 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5298 /* If .dynstr is excluded from the link, we don't want any of
5299 these tags. Strictly, we should be checking each section
5300 individually; This quick check covers for the case where
5301 someone does a /DISCARD/ : { *(*) }. */
5302 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5304 bfd_size_type strsize;
5306 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5307 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5308 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5309 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5310 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5311 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5312 bed->s->sizeof_sym))
5313 return FALSE;
5317 /* The backend must work out the sizes of all the other dynamic
5318 sections. */
5319 if (bed->elf_backend_size_dynamic_sections
5320 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5321 return FALSE;
5323 if (elf_hash_table (info)->dynamic_sections_created)
5325 unsigned long section_sym_count;
5326 asection *s;
5328 /* Set up the version definition section. */
5329 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5330 BFD_ASSERT (s != NULL);
5332 /* We may have created additional version definitions if we are
5333 just linking a regular application. */
5334 verdefs = asvinfo.verdefs;
5336 /* Skip anonymous version tag. */
5337 if (verdefs != NULL && verdefs->vernum == 0)
5338 verdefs = verdefs->next;
5340 if (verdefs == NULL && !info->create_default_symver)
5341 s->flags |= SEC_EXCLUDE;
5342 else
5344 unsigned int cdefs;
5345 bfd_size_type size;
5346 struct bfd_elf_version_tree *t;
5347 bfd_byte *p;
5348 Elf_Internal_Verdef def;
5349 Elf_Internal_Verdaux defaux;
5350 struct bfd_link_hash_entry *bh;
5351 struct elf_link_hash_entry *h;
5352 const char *name;
5354 cdefs = 0;
5355 size = 0;
5357 /* Make space for the base version. */
5358 size += sizeof (Elf_External_Verdef);
5359 size += sizeof (Elf_External_Verdaux);
5360 ++cdefs;
5362 /* Make space for the default version. */
5363 if (info->create_default_symver)
5365 size += sizeof (Elf_External_Verdef);
5366 ++cdefs;
5369 for (t = verdefs; t != NULL; t = t->next)
5371 struct bfd_elf_version_deps *n;
5373 size += sizeof (Elf_External_Verdef);
5374 size += sizeof (Elf_External_Verdaux);
5375 ++cdefs;
5377 for (n = t->deps; n != NULL; n = n->next)
5378 size += sizeof (Elf_External_Verdaux);
5381 s->size = size;
5382 s->contents = bfd_alloc (output_bfd, s->size);
5383 if (s->contents == NULL && s->size != 0)
5384 return FALSE;
5386 /* Fill in the version definition section. */
5388 p = s->contents;
5390 def.vd_version = VER_DEF_CURRENT;
5391 def.vd_flags = VER_FLG_BASE;
5392 def.vd_ndx = 1;
5393 def.vd_cnt = 1;
5394 if (info->create_default_symver)
5396 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5397 def.vd_next = sizeof (Elf_External_Verdef);
5399 else
5401 def.vd_aux = sizeof (Elf_External_Verdef);
5402 def.vd_next = (sizeof (Elf_External_Verdef)
5403 + sizeof (Elf_External_Verdaux));
5406 if (soname_indx != (bfd_size_type) -1)
5408 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5409 soname_indx);
5410 def.vd_hash = bfd_elf_hash (soname);
5411 defaux.vda_name = soname_indx;
5412 name = soname;
5414 else
5416 bfd_size_type indx;
5418 name = lbasename (output_bfd->filename);
5419 def.vd_hash = bfd_elf_hash (name);
5420 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5421 name, FALSE);
5422 if (indx == (bfd_size_type) -1)
5423 return FALSE;
5424 defaux.vda_name = indx;
5426 defaux.vda_next = 0;
5428 _bfd_elf_swap_verdef_out (output_bfd, &def,
5429 (Elf_External_Verdef *) p);
5430 p += sizeof (Elf_External_Verdef);
5431 if (info->create_default_symver)
5433 /* Add a symbol representing this version. */
5434 bh = NULL;
5435 if (! (_bfd_generic_link_add_one_symbol
5436 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5437 0, NULL, FALSE,
5438 get_elf_backend_data (dynobj)->collect, &bh)))
5439 return FALSE;
5440 h = (struct elf_link_hash_entry *) bh;
5441 h->non_elf = 0;
5442 h->def_regular = 1;
5443 h->type = STT_OBJECT;
5444 h->verinfo.vertree = NULL;
5446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5447 return FALSE;
5449 /* Create a duplicate of the base version with the same
5450 aux block, but different flags. */
5451 def.vd_flags = 0;
5452 def.vd_ndx = 2;
5453 def.vd_aux = sizeof (Elf_External_Verdef);
5454 if (verdefs)
5455 def.vd_next = (sizeof (Elf_External_Verdef)
5456 + sizeof (Elf_External_Verdaux));
5457 else
5458 def.vd_next = 0;
5459 _bfd_elf_swap_verdef_out (output_bfd, &def,
5460 (Elf_External_Verdef *) p);
5461 p += sizeof (Elf_External_Verdef);
5463 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5464 (Elf_External_Verdaux *) p);
5465 p += sizeof (Elf_External_Verdaux);
5467 for (t = verdefs; t != NULL; t = t->next)
5469 unsigned int cdeps;
5470 struct bfd_elf_version_deps *n;
5472 cdeps = 0;
5473 for (n = t->deps; n != NULL; n = n->next)
5474 ++cdeps;
5476 /* Add a symbol representing this version. */
5477 bh = NULL;
5478 if (! (_bfd_generic_link_add_one_symbol
5479 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5480 0, NULL, FALSE,
5481 get_elf_backend_data (dynobj)->collect, &bh)))
5482 return FALSE;
5483 h = (struct elf_link_hash_entry *) bh;
5484 h->non_elf = 0;
5485 h->def_regular = 1;
5486 h->type = STT_OBJECT;
5487 h->verinfo.vertree = t;
5489 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5490 return FALSE;
5492 def.vd_version = VER_DEF_CURRENT;
5493 def.vd_flags = 0;
5494 if (t->globals.list == NULL
5495 && t->locals.list == NULL
5496 && ! t->used)
5497 def.vd_flags |= VER_FLG_WEAK;
5498 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5499 def.vd_cnt = cdeps + 1;
5500 def.vd_hash = bfd_elf_hash (t->name);
5501 def.vd_aux = sizeof (Elf_External_Verdef);
5502 def.vd_next = 0;
5503 if (t->next != NULL)
5504 def.vd_next = (sizeof (Elf_External_Verdef)
5505 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5507 _bfd_elf_swap_verdef_out (output_bfd, &def,
5508 (Elf_External_Verdef *) p);
5509 p += sizeof (Elf_External_Verdef);
5511 defaux.vda_name = h->dynstr_index;
5512 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5513 h->dynstr_index);
5514 defaux.vda_next = 0;
5515 if (t->deps != NULL)
5516 defaux.vda_next = sizeof (Elf_External_Verdaux);
5517 t->name_indx = defaux.vda_name;
5519 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5520 (Elf_External_Verdaux *) p);
5521 p += sizeof (Elf_External_Verdaux);
5523 for (n = t->deps; n != NULL; n = n->next)
5525 if (n->version_needed == NULL)
5527 /* This can happen if there was an error in the
5528 version script. */
5529 defaux.vda_name = 0;
5531 else
5533 defaux.vda_name = n->version_needed->name_indx;
5534 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5535 defaux.vda_name);
5537 if (n->next == NULL)
5538 defaux.vda_next = 0;
5539 else
5540 defaux.vda_next = sizeof (Elf_External_Verdaux);
5542 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5543 (Elf_External_Verdaux *) p);
5544 p += sizeof (Elf_External_Verdaux);
5548 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5549 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5550 return FALSE;
5552 elf_tdata (output_bfd)->cverdefs = cdefs;
5555 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5557 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5558 return FALSE;
5560 else if (info->flags & DF_BIND_NOW)
5562 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5563 return FALSE;
5566 if (info->flags_1)
5568 if (info->executable)
5569 info->flags_1 &= ~ (DF_1_INITFIRST
5570 | DF_1_NODELETE
5571 | DF_1_NOOPEN);
5572 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5573 return FALSE;
5576 /* Work out the size of the version reference section. */
5578 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5579 BFD_ASSERT (s != NULL);
5581 struct elf_find_verdep_info sinfo;
5583 sinfo.output_bfd = output_bfd;
5584 sinfo.info = info;
5585 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5586 if (sinfo.vers == 0)
5587 sinfo.vers = 1;
5588 sinfo.failed = FALSE;
5590 elf_link_hash_traverse (elf_hash_table (info),
5591 _bfd_elf_link_find_version_dependencies,
5592 &sinfo);
5594 if (elf_tdata (output_bfd)->verref == NULL)
5595 s->flags |= SEC_EXCLUDE;
5596 else
5598 Elf_Internal_Verneed *t;
5599 unsigned int size;
5600 unsigned int crefs;
5601 bfd_byte *p;
5603 /* Build the version definition section. */
5604 size = 0;
5605 crefs = 0;
5606 for (t = elf_tdata (output_bfd)->verref;
5607 t != NULL;
5608 t = t->vn_nextref)
5610 Elf_Internal_Vernaux *a;
5612 size += sizeof (Elf_External_Verneed);
5613 ++crefs;
5614 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5615 size += sizeof (Elf_External_Vernaux);
5618 s->size = size;
5619 s->contents = bfd_alloc (output_bfd, s->size);
5620 if (s->contents == NULL)
5621 return FALSE;
5623 p = s->contents;
5624 for (t = elf_tdata (output_bfd)->verref;
5625 t != NULL;
5626 t = t->vn_nextref)
5628 unsigned int caux;
5629 Elf_Internal_Vernaux *a;
5630 bfd_size_type indx;
5632 caux = 0;
5633 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5634 ++caux;
5636 t->vn_version = VER_NEED_CURRENT;
5637 t->vn_cnt = caux;
5638 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5639 elf_dt_name (t->vn_bfd) != NULL
5640 ? elf_dt_name (t->vn_bfd)
5641 : lbasename (t->vn_bfd->filename),
5642 FALSE);
5643 if (indx == (bfd_size_type) -1)
5644 return FALSE;
5645 t->vn_file = indx;
5646 t->vn_aux = sizeof (Elf_External_Verneed);
5647 if (t->vn_nextref == NULL)
5648 t->vn_next = 0;
5649 else
5650 t->vn_next = (sizeof (Elf_External_Verneed)
5651 + caux * sizeof (Elf_External_Vernaux));
5653 _bfd_elf_swap_verneed_out (output_bfd, t,
5654 (Elf_External_Verneed *) p);
5655 p += sizeof (Elf_External_Verneed);
5657 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5659 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5660 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5661 a->vna_nodename, FALSE);
5662 if (indx == (bfd_size_type) -1)
5663 return FALSE;
5664 a->vna_name = indx;
5665 if (a->vna_nextptr == NULL)
5666 a->vna_next = 0;
5667 else
5668 a->vna_next = sizeof (Elf_External_Vernaux);
5670 _bfd_elf_swap_vernaux_out (output_bfd, a,
5671 (Elf_External_Vernaux *) p);
5672 p += sizeof (Elf_External_Vernaux);
5676 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5677 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5678 return FALSE;
5680 elf_tdata (output_bfd)->cverrefs = crefs;
5684 if ((elf_tdata (output_bfd)->cverrefs == 0
5685 && elf_tdata (output_bfd)->cverdefs == 0)
5686 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5687 &section_sym_count) == 0)
5689 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5690 s->flags |= SEC_EXCLUDE;
5693 return TRUE;
5696 bfd_boolean
5697 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5699 if (!is_elf_hash_table (info->hash))
5700 return TRUE;
5702 if (elf_hash_table (info)->dynamic_sections_created)
5704 bfd *dynobj;
5705 const struct elf_backend_data *bed;
5706 asection *s;
5707 bfd_size_type dynsymcount;
5708 unsigned long section_sym_count;
5709 size_t bucketcount = 0;
5710 size_t hash_entry_size;
5711 unsigned int dtagcount;
5713 dynobj = elf_hash_table (info)->dynobj;
5715 /* Assign dynsym indicies. In a shared library we generate a
5716 section symbol for each output section, which come first.
5717 Next come all of the back-end allocated local dynamic syms,
5718 followed by the rest of the global symbols. */
5720 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5721 &section_sym_count);
5723 /* Work out the size of the symbol version section. */
5724 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5725 BFD_ASSERT (s != NULL);
5726 if (dynsymcount != 0
5727 && (s->flags & SEC_EXCLUDE) == 0)
5729 s->size = dynsymcount * sizeof (Elf_External_Versym);
5730 s->contents = bfd_zalloc (output_bfd, s->size);
5731 if (s->contents == NULL)
5732 return FALSE;
5734 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5735 return FALSE;
5738 /* Set the size of the .dynsym and .hash sections. We counted
5739 the number of dynamic symbols in elf_link_add_object_symbols.
5740 We will build the contents of .dynsym and .hash when we build
5741 the final symbol table, because until then we do not know the
5742 correct value to give the symbols. We built the .dynstr
5743 section as we went along in elf_link_add_object_symbols. */
5744 s = bfd_get_section_by_name (dynobj, ".dynsym");
5745 BFD_ASSERT (s != NULL);
5746 bed = get_elf_backend_data (output_bfd);
5747 s->size = dynsymcount * bed->s->sizeof_sym;
5749 if (dynsymcount != 0)
5751 s->contents = bfd_alloc (output_bfd, s->size);
5752 if (s->contents == NULL)
5753 return FALSE;
5755 /* The first entry in .dynsym is a dummy symbol.
5756 Clear all the section syms, in case we don't output them all. */
5757 ++section_sym_count;
5758 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5761 /* Compute the size of the hashing table. As a side effect this
5762 computes the hash values for all the names we export. */
5763 bucketcount = compute_bucket_count (info);
5765 s = bfd_get_section_by_name (dynobj, ".hash");
5766 BFD_ASSERT (s != NULL);
5767 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5768 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5769 s->contents = bfd_zalloc (output_bfd, s->size);
5770 if (s->contents == NULL)
5771 return FALSE;
5773 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5774 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5775 s->contents + hash_entry_size);
5777 elf_hash_table (info)->bucketcount = bucketcount;
5779 s = bfd_get_section_by_name (dynobj, ".dynstr");
5780 BFD_ASSERT (s != NULL);
5782 elf_finalize_dynstr (output_bfd, info);
5784 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5786 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5787 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5788 return FALSE;
5791 return TRUE;
5794 /* Final phase of ELF linker. */
5796 /* A structure we use to avoid passing large numbers of arguments. */
5798 struct elf_final_link_info
5800 /* General link information. */
5801 struct bfd_link_info *info;
5802 /* Output BFD. */
5803 bfd *output_bfd;
5804 /* Symbol string table. */
5805 struct bfd_strtab_hash *symstrtab;
5806 /* .dynsym section. */
5807 asection *dynsym_sec;
5808 /* .hash section. */
5809 asection *hash_sec;
5810 /* symbol version section (.gnu.version). */
5811 asection *symver_sec;
5812 /* Buffer large enough to hold contents of any section. */
5813 bfd_byte *contents;
5814 /* Buffer large enough to hold external relocs of any section. */
5815 void *external_relocs;
5816 /* Buffer large enough to hold internal relocs of any section. */
5817 Elf_Internal_Rela *internal_relocs;
5818 /* Buffer large enough to hold external local symbols of any input
5819 BFD. */
5820 bfd_byte *external_syms;
5821 /* And a buffer for symbol section indices. */
5822 Elf_External_Sym_Shndx *locsym_shndx;
5823 /* Buffer large enough to hold internal local symbols of any input
5824 BFD. */
5825 Elf_Internal_Sym *internal_syms;
5826 /* Array large enough to hold a symbol index for each local symbol
5827 of any input BFD. */
5828 long *indices;
5829 /* Array large enough to hold a section pointer for each local
5830 symbol of any input BFD. */
5831 asection **sections;
5832 /* Buffer to hold swapped out symbols. */
5833 bfd_byte *symbuf;
5834 /* And one for symbol section indices. */
5835 Elf_External_Sym_Shndx *symshndxbuf;
5836 /* Number of swapped out symbols in buffer. */
5837 size_t symbuf_count;
5838 /* Number of symbols which fit in symbuf. */
5839 size_t symbuf_size;
5840 /* And same for symshndxbuf. */
5841 size_t shndxbuf_size;
5844 /* This struct is used to pass information to elf_link_output_extsym. */
5846 struct elf_outext_info
5848 bfd_boolean failed;
5849 bfd_boolean localsyms;
5850 struct elf_final_link_info *finfo;
5853 /* When performing a relocatable link, the input relocations are
5854 preserved. But, if they reference global symbols, the indices
5855 referenced must be updated. Update all the relocations in
5856 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5858 static void
5859 elf_link_adjust_relocs (bfd *abfd,
5860 Elf_Internal_Shdr *rel_hdr,
5861 unsigned int count,
5862 struct elf_link_hash_entry **rel_hash)
5864 unsigned int i;
5865 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5866 bfd_byte *erela;
5867 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5868 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5869 bfd_vma r_type_mask;
5870 int r_sym_shift;
5872 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5874 swap_in = bed->s->swap_reloc_in;
5875 swap_out = bed->s->swap_reloc_out;
5877 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5879 swap_in = bed->s->swap_reloca_in;
5880 swap_out = bed->s->swap_reloca_out;
5882 else
5883 abort ();
5885 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5886 abort ();
5888 if (bed->s->arch_size == 32)
5890 r_type_mask = 0xff;
5891 r_sym_shift = 8;
5893 else
5895 r_type_mask = 0xffffffff;
5896 r_sym_shift = 32;
5899 erela = rel_hdr->contents;
5900 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5902 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5903 unsigned int j;
5905 if (*rel_hash == NULL)
5906 continue;
5908 BFD_ASSERT ((*rel_hash)->indx >= 0);
5910 (*swap_in) (abfd, erela, irela);
5911 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5912 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5913 | (irela[j].r_info & r_type_mask));
5914 (*swap_out) (abfd, irela, erela);
5918 struct elf_link_sort_rela
5920 union {
5921 bfd_vma offset;
5922 bfd_vma sym_mask;
5923 } u;
5924 enum elf_reloc_type_class type;
5925 /* We use this as an array of size int_rels_per_ext_rel. */
5926 Elf_Internal_Rela rela[1];
5929 static int
5930 elf_link_sort_cmp1 (const void *A, const void *B)
5932 const struct elf_link_sort_rela *a = A;
5933 const struct elf_link_sort_rela *b = B;
5934 int relativea, relativeb;
5936 relativea = a->type == reloc_class_relative;
5937 relativeb = b->type == reloc_class_relative;
5939 if (relativea < relativeb)
5940 return 1;
5941 if (relativea > relativeb)
5942 return -1;
5943 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5944 return -1;
5945 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5946 return 1;
5947 if (a->rela->r_offset < b->rela->r_offset)
5948 return -1;
5949 if (a->rela->r_offset > b->rela->r_offset)
5950 return 1;
5951 return 0;
5954 static int
5955 elf_link_sort_cmp2 (const void *A, const void *B)
5957 const struct elf_link_sort_rela *a = A;
5958 const struct elf_link_sort_rela *b = B;
5959 int copya, copyb;
5961 if (a->u.offset < b->u.offset)
5962 return -1;
5963 if (a->u.offset > b->u.offset)
5964 return 1;
5965 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5966 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5967 if (copya < copyb)
5968 return -1;
5969 if (copya > copyb)
5970 return 1;
5971 if (a->rela->r_offset < b->rela->r_offset)
5972 return -1;
5973 if (a->rela->r_offset > b->rela->r_offset)
5974 return 1;
5975 return 0;
5978 static size_t
5979 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5981 asection *reldyn;
5982 bfd_size_type count, size;
5983 size_t i, ret, sort_elt, ext_size;
5984 bfd_byte *sort, *s_non_relative, *p;
5985 struct elf_link_sort_rela *sq;
5986 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5987 int i2e = bed->s->int_rels_per_ext_rel;
5988 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5989 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5990 struct bfd_link_order *lo;
5991 bfd_vma r_sym_mask;
5993 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5994 if (reldyn == NULL || reldyn->size == 0)
5996 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5997 if (reldyn == NULL || reldyn->size == 0)
5998 return 0;
5999 ext_size = bed->s->sizeof_rel;
6000 swap_in = bed->s->swap_reloc_in;
6001 swap_out = bed->s->swap_reloc_out;
6003 else
6005 ext_size = bed->s->sizeof_rela;
6006 swap_in = bed->s->swap_reloca_in;
6007 swap_out = bed->s->swap_reloca_out;
6009 count = reldyn->size / ext_size;
6011 size = 0;
6012 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6013 if (lo->type == bfd_indirect_link_order)
6015 asection *o = lo->u.indirect.section;
6016 size += o->size;
6019 if (size != reldyn->size)
6020 return 0;
6022 sort_elt = (sizeof (struct elf_link_sort_rela)
6023 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6024 sort = bfd_zmalloc (sort_elt * count);
6025 if (sort == NULL)
6027 (*info->callbacks->warning)
6028 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6029 return 0;
6032 if (bed->s->arch_size == 32)
6033 r_sym_mask = ~(bfd_vma) 0xff;
6034 else
6035 r_sym_mask = ~(bfd_vma) 0xffffffff;
6037 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6038 if (lo->type == bfd_indirect_link_order)
6040 bfd_byte *erel, *erelend;
6041 asection *o = lo->u.indirect.section;
6043 if (o->contents == NULL && o->size != 0)
6045 /* This is a reloc section that is being handled as a normal
6046 section. See bfd_section_from_shdr. We can't combine
6047 relocs in this case. */
6048 free (sort);
6049 return 0;
6051 erel = o->contents;
6052 erelend = o->contents + o->size;
6053 p = sort + o->output_offset / ext_size * sort_elt;
6054 while (erel < erelend)
6056 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6057 (*swap_in) (abfd, erel, s->rela);
6058 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6059 s->u.sym_mask = r_sym_mask;
6060 p += sort_elt;
6061 erel += ext_size;
6065 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6067 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6069 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6070 if (s->type != reloc_class_relative)
6071 break;
6073 ret = i;
6074 s_non_relative = p;
6076 sq = (struct elf_link_sort_rela *) s_non_relative;
6077 for (; i < count; i++, p += sort_elt)
6079 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6080 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6081 sq = sp;
6082 sp->u.offset = sq->rela->r_offset;
6085 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6087 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6088 if (lo->type == bfd_indirect_link_order)
6090 bfd_byte *erel, *erelend;
6091 asection *o = lo->u.indirect.section;
6093 erel = o->contents;
6094 erelend = o->contents + o->size;
6095 p = sort + o->output_offset / ext_size * sort_elt;
6096 while (erel < erelend)
6098 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6099 (*swap_out) (abfd, s->rela, erel);
6100 p += sort_elt;
6101 erel += ext_size;
6105 free (sort);
6106 *psec = reldyn;
6107 return ret;
6110 /* Flush the output symbols to the file. */
6112 static bfd_boolean
6113 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6114 const struct elf_backend_data *bed)
6116 if (finfo->symbuf_count > 0)
6118 Elf_Internal_Shdr *hdr;
6119 file_ptr pos;
6120 bfd_size_type amt;
6122 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6123 pos = hdr->sh_offset + hdr->sh_size;
6124 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6125 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6126 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6127 return FALSE;
6129 hdr->sh_size += amt;
6130 finfo->symbuf_count = 0;
6133 return TRUE;
6136 /* Add a symbol to the output symbol table. */
6138 static bfd_boolean
6139 elf_link_output_sym (struct elf_final_link_info *finfo,
6140 const char *name,
6141 Elf_Internal_Sym *elfsym,
6142 asection *input_sec,
6143 struct elf_link_hash_entry *h)
6145 bfd_byte *dest;
6146 Elf_External_Sym_Shndx *destshndx;
6147 bfd_boolean (*output_symbol_hook)
6148 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6149 struct elf_link_hash_entry *);
6150 const struct elf_backend_data *bed;
6152 bed = get_elf_backend_data (finfo->output_bfd);
6153 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6154 if (output_symbol_hook != NULL)
6156 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6157 return FALSE;
6160 if (name == NULL || *name == '\0')
6161 elfsym->st_name = 0;
6162 else if (input_sec->flags & SEC_EXCLUDE)
6163 elfsym->st_name = 0;
6164 else
6166 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6167 name, TRUE, FALSE);
6168 if (elfsym->st_name == (unsigned long) -1)
6169 return FALSE;
6172 if (finfo->symbuf_count >= finfo->symbuf_size)
6174 if (! elf_link_flush_output_syms (finfo, bed))
6175 return FALSE;
6178 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6179 destshndx = finfo->symshndxbuf;
6180 if (destshndx != NULL)
6182 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6184 bfd_size_type amt;
6186 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6187 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6188 if (destshndx == NULL)
6189 return FALSE;
6190 memset ((char *) destshndx + amt, 0, amt);
6191 finfo->shndxbuf_size *= 2;
6193 destshndx += bfd_get_symcount (finfo->output_bfd);
6196 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6197 finfo->symbuf_count += 1;
6198 bfd_get_symcount (finfo->output_bfd) += 1;
6200 return TRUE;
6203 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6205 static bfd_boolean
6206 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
6208 if (sym->st_shndx > SHN_HIRESERVE)
6210 /* The gABI doesn't support dynamic symbols in output sections
6211 beyond 64k. */
6212 (*_bfd_error_handler)
6213 (_("%B: Too many sections: %d (>= %d)"),
6214 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
6215 bfd_set_error (bfd_error_nonrepresentable_section);
6216 return FALSE;
6218 return TRUE;
6221 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6222 allowing an unsatisfied unversioned symbol in the DSO to match a
6223 versioned symbol that would normally require an explicit version.
6224 We also handle the case that a DSO references a hidden symbol
6225 which may be satisfied by a versioned symbol in another DSO. */
6227 static bfd_boolean
6228 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6229 const struct elf_backend_data *bed,
6230 struct elf_link_hash_entry *h)
6232 bfd *abfd;
6233 struct elf_link_loaded_list *loaded;
6235 if (!is_elf_hash_table (info->hash))
6236 return FALSE;
6238 switch (h->root.type)
6240 default:
6241 abfd = NULL;
6242 break;
6244 case bfd_link_hash_undefined:
6245 case bfd_link_hash_undefweak:
6246 abfd = h->root.u.undef.abfd;
6247 if ((abfd->flags & DYNAMIC) == 0
6248 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6249 return FALSE;
6250 break;
6252 case bfd_link_hash_defined:
6253 case bfd_link_hash_defweak:
6254 abfd = h->root.u.def.section->owner;
6255 break;
6257 case bfd_link_hash_common:
6258 abfd = h->root.u.c.p->section->owner;
6259 break;
6261 BFD_ASSERT (abfd != NULL);
6263 for (loaded = elf_hash_table (info)->loaded;
6264 loaded != NULL;
6265 loaded = loaded->next)
6267 bfd *input;
6268 Elf_Internal_Shdr *hdr;
6269 bfd_size_type symcount;
6270 bfd_size_type extsymcount;
6271 bfd_size_type extsymoff;
6272 Elf_Internal_Shdr *versymhdr;
6273 Elf_Internal_Sym *isym;
6274 Elf_Internal_Sym *isymend;
6275 Elf_Internal_Sym *isymbuf;
6276 Elf_External_Versym *ever;
6277 Elf_External_Versym *extversym;
6279 input = loaded->abfd;
6281 /* We check each DSO for a possible hidden versioned definition. */
6282 if (input == abfd
6283 || (input->flags & DYNAMIC) == 0
6284 || elf_dynversym (input) == 0)
6285 continue;
6287 hdr = &elf_tdata (input)->dynsymtab_hdr;
6289 symcount = hdr->sh_size / bed->s->sizeof_sym;
6290 if (elf_bad_symtab (input))
6292 extsymcount = symcount;
6293 extsymoff = 0;
6295 else
6297 extsymcount = symcount - hdr->sh_info;
6298 extsymoff = hdr->sh_info;
6301 if (extsymcount == 0)
6302 continue;
6304 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6305 NULL, NULL, NULL);
6306 if (isymbuf == NULL)
6307 return FALSE;
6309 /* Read in any version definitions. */
6310 versymhdr = &elf_tdata (input)->dynversym_hdr;
6311 extversym = bfd_malloc (versymhdr->sh_size);
6312 if (extversym == NULL)
6313 goto error_ret;
6315 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6316 || (bfd_bread (extversym, versymhdr->sh_size, input)
6317 != versymhdr->sh_size))
6319 free (extversym);
6320 error_ret:
6321 free (isymbuf);
6322 return FALSE;
6325 ever = extversym + extsymoff;
6326 isymend = isymbuf + extsymcount;
6327 for (isym = isymbuf; isym < isymend; isym++, ever++)
6329 const char *name;
6330 Elf_Internal_Versym iver;
6331 unsigned short version_index;
6333 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6334 || isym->st_shndx == SHN_UNDEF)
6335 continue;
6337 name = bfd_elf_string_from_elf_section (input,
6338 hdr->sh_link,
6339 isym->st_name);
6340 if (strcmp (name, h->root.root.string) != 0)
6341 continue;
6343 _bfd_elf_swap_versym_in (input, ever, &iver);
6345 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6347 /* If we have a non-hidden versioned sym, then it should
6348 have provided a definition for the undefined sym. */
6349 abort ();
6352 version_index = iver.vs_vers & VERSYM_VERSION;
6353 if (version_index == 1 || version_index == 2)
6355 /* This is the base or first version. We can use it. */
6356 free (extversym);
6357 free (isymbuf);
6358 return TRUE;
6362 free (extversym);
6363 free (isymbuf);
6366 return FALSE;
6369 /* Add an external symbol to the symbol table. This is called from
6370 the hash table traversal routine. When generating a shared object,
6371 we go through the symbol table twice. The first time we output
6372 anything that might have been forced to local scope in a version
6373 script. The second time we output the symbols that are still
6374 global symbols. */
6376 static bfd_boolean
6377 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6379 struct elf_outext_info *eoinfo = data;
6380 struct elf_final_link_info *finfo = eoinfo->finfo;
6381 bfd_boolean strip;
6382 Elf_Internal_Sym sym;
6383 asection *input_sec;
6384 const struct elf_backend_data *bed;
6386 if (h->root.type == bfd_link_hash_warning)
6388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6389 if (h->root.type == bfd_link_hash_new)
6390 return TRUE;
6393 /* Decide whether to output this symbol in this pass. */
6394 if (eoinfo->localsyms)
6396 if (!h->forced_local)
6397 return TRUE;
6399 else
6401 if (h->forced_local)
6402 return TRUE;
6405 bed = get_elf_backend_data (finfo->output_bfd);
6407 if (h->root.type == bfd_link_hash_undefined)
6409 /* If we have an undefined symbol reference here then it must have
6410 come from a shared library that is being linked in. (Undefined
6411 references in regular files have already been handled). */
6412 bfd_boolean ignore_undef = FALSE;
6414 /* Some symbols may be special in that the fact that they're
6415 undefined can be safely ignored - let backend determine that. */
6416 if (bed->elf_backend_ignore_undef_symbol)
6417 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6419 /* If we are reporting errors for this situation then do so now. */
6420 if (ignore_undef == FALSE
6421 && h->ref_dynamic
6422 && ! h->ref_regular
6423 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6424 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6426 if (! (finfo->info->callbacks->undefined_symbol
6427 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6428 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6430 eoinfo->failed = TRUE;
6431 return FALSE;
6436 /* We should also warn if a forced local symbol is referenced from
6437 shared libraries. */
6438 if (! finfo->info->relocatable
6439 && (! finfo->info->shared)
6440 && h->forced_local
6441 && h->ref_dynamic
6442 && !h->dynamic_def
6443 && !h->dynamic_weak
6444 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6446 (*_bfd_error_handler)
6447 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6448 finfo->output_bfd,
6449 h->root.u.def.section == bfd_abs_section_ptr
6450 ? finfo->output_bfd : h->root.u.def.section->owner,
6451 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6452 ? "internal"
6453 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6454 ? "hidden" : "local",
6455 h->root.root.string);
6456 eoinfo->failed = TRUE;
6457 return FALSE;
6460 /* We don't want to output symbols that have never been mentioned by
6461 a regular file, or that we have been told to strip. However, if
6462 h->indx is set to -2, the symbol is used by a reloc and we must
6463 output it. */
6464 if (h->indx == -2)
6465 strip = FALSE;
6466 else if ((h->def_dynamic
6467 || h->ref_dynamic
6468 || h->root.type == bfd_link_hash_new)
6469 && !h->def_regular
6470 && !h->ref_regular)
6471 strip = TRUE;
6472 else if (finfo->info->strip == strip_all)
6473 strip = TRUE;
6474 else if (finfo->info->strip == strip_some
6475 && bfd_hash_lookup (finfo->info->keep_hash,
6476 h->root.root.string, FALSE, FALSE) == NULL)
6477 strip = TRUE;
6478 else if (finfo->info->strip_discarded
6479 && (h->root.type == bfd_link_hash_defined
6480 || h->root.type == bfd_link_hash_defweak)
6481 && elf_discarded_section (h->root.u.def.section))
6482 strip = TRUE;
6483 else
6484 strip = FALSE;
6486 /* If we're stripping it, and it's not a dynamic symbol, there's
6487 nothing else to do unless it is a forced local symbol. */
6488 if (strip
6489 && h->dynindx == -1
6490 && !h->forced_local)
6491 return TRUE;
6493 sym.st_value = 0;
6494 sym.st_size = h->size;
6495 sym.st_other = h->other;
6496 if (h->forced_local)
6497 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6498 else if (h->root.type == bfd_link_hash_undefweak
6499 || h->root.type == bfd_link_hash_defweak)
6500 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6501 else
6502 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6504 switch (h->root.type)
6506 default:
6507 case bfd_link_hash_new:
6508 case bfd_link_hash_warning:
6509 abort ();
6510 return FALSE;
6512 case bfd_link_hash_undefined:
6513 case bfd_link_hash_undefweak:
6514 input_sec = bfd_und_section_ptr;
6515 sym.st_shndx = SHN_UNDEF;
6516 break;
6518 case bfd_link_hash_defined:
6519 case bfd_link_hash_defweak:
6521 input_sec = h->root.u.def.section;
6522 if (input_sec->output_section != NULL)
6524 sym.st_shndx =
6525 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6526 input_sec->output_section);
6527 if (sym.st_shndx == SHN_BAD)
6529 (*_bfd_error_handler)
6530 (_("%B: could not find output section %A for input section %A"),
6531 finfo->output_bfd, input_sec->output_section, input_sec);
6532 eoinfo->failed = TRUE;
6533 return FALSE;
6536 /* ELF symbols in relocatable files are section relative,
6537 but in nonrelocatable files they are virtual
6538 addresses. */
6539 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6540 if (! finfo->info->relocatable)
6542 sym.st_value += input_sec->output_section->vma;
6543 if (h->type == STT_TLS)
6545 /* STT_TLS symbols are relative to PT_TLS segment
6546 base. */
6547 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6548 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6552 else
6554 BFD_ASSERT (input_sec->owner == NULL
6555 || (input_sec->owner->flags & DYNAMIC) != 0);
6556 sym.st_shndx = SHN_UNDEF;
6557 input_sec = bfd_und_section_ptr;
6560 break;
6562 case bfd_link_hash_common:
6563 input_sec = h->root.u.c.p->section;
6564 sym.st_shndx = bed->common_section_index (input_sec);
6565 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6566 break;
6568 case bfd_link_hash_indirect:
6569 /* These symbols are created by symbol versioning. They point
6570 to the decorated version of the name. For example, if the
6571 symbol foo@@GNU_1.2 is the default, which should be used when
6572 foo is used with no version, then we add an indirect symbol
6573 foo which points to foo@@GNU_1.2. We ignore these symbols,
6574 since the indirected symbol is already in the hash table. */
6575 return TRUE;
6578 /* Give the processor backend a chance to tweak the symbol value,
6579 and also to finish up anything that needs to be done for this
6580 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6581 forced local syms when non-shared is due to a historical quirk. */
6582 if ((h->dynindx != -1
6583 || h->forced_local)
6584 && ((finfo->info->shared
6585 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6586 || h->root.type != bfd_link_hash_undefweak))
6587 || !h->forced_local)
6588 && elf_hash_table (finfo->info)->dynamic_sections_created)
6590 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6591 (finfo->output_bfd, finfo->info, h, &sym)))
6593 eoinfo->failed = TRUE;
6594 return FALSE;
6598 /* If we are marking the symbol as undefined, and there are no
6599 non-weak references to this symbol from a regular object, then
6600 mark the symbol as weak undefined; if there are non-weak
6601 references, mark the symbol as strong. We can't do this earlier,
6602 because it might not be marked as undefined until the
6603 finish_dynamic_symbol routine gets through with it. */
6604 if (sym.st_shndx == SHN_UNDEF
6605 && h->ref_regular
6606 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6607 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6609 int bindtype;
6611 if (h->ref_regular_nonweak)
6612 bindtype = STB_GLOBAL;
6613 else
6614 bindtype = STB_WEAK;
6615 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6618 /* If a non-weak symbol with non-default visibility is not defined
6619 locally, it is a fatal error. */
6620 if (! finfo->info->relocatable
6621 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6622 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6623 && h->root.type == bfd_link_hash_undefined
6624 && !h->def_regular)
6626 (*_bfd_error_handler)
6627 (_("%B: %s symbol `%s' isn't defined"),
6628 finfo->output_bfd,
6629 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6630 ? "protected"
6631 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6632 ? "internal" : "hidden",
6633 h->root.root.string);
6634 eoinfo->failed = TRUE;
6635 return FALSE;
6638 /* If this symbol should be put in the .dynsym section, then put it
6639 there now. We already know the symbol index. We also fill in
6640 the entry in the .hash section. */
6641 if (h->dynindx != -1
6642 && elf_hash_table (finfo->info)->dynamic_sections_created)
6644 size_t bucketcount;
6645 size_t bucket;
6646 size_t hash_entry_size;
6647 bfd_byte *bucketpos;
6648 bfd_vma chain;
6649 bfd_byte *esym;
6651 sym.st_name = h->dynstr_index;
6652 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6653 if (! check_dynsym (finfo->output_bfd, &sym))
6655 eoinfo->failed = TRUE;
6656 return FALSE;
6658 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6660 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6661 bucket = h->u.elf_hash_value % bucketcount;
6662 hash_entry_size
6663 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6664 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6665 + (bucket + 2) * hash_entry_size);
6666 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6667 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6668 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6669 ((bfd_byte *) finfo->hash_sec->contents
6670 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6672 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6674 Elf_Internal_Versym iversym;
6675 Elf_External_Versym *eversym;
6677 if (!h->def_regular)
6679 if (h->verinfo.verdef == NULL)
6680 iversym.vs_vers = 0;
6681 else
6682 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6684 else
6686 if (h->verinfo.vertree == NULL)
6687 iversym.vs_vers = 1;
6688 else
6689 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6690 if (finfo->info->create_default_symver)
6691 iversym.vs_vers++;
6694 if (h->hidden)
6695 iversym.vs_vers |= VERSYM_HIDDEN;
6697 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6698 eversym += h->dynindx;
6699 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6703 /* If we're stripping it, then it was just a dynamic symbol, and
6704 there's nothing else to do. */
6705 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6706 return TRUE;
6708 h->indx = bfd_get_symcount (finfo->output_bfd);
6710 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6712 eoinfo->failed = TRUE;
6713 return FALSE;
6716 return TRUE;
6719 /* Return TRUE if special handling is done for relocs in SEC against
6720 symbols defined in discarded sections. */
6722 static bfd_boolean
6723 elf_section_ignore_discarded_relocs (asection *sec)
6725 const struct elf_backend_data *bed;
6727 switch (sec->sec_info_type)
6729 case ELF_INFO_TYPE_STABS:
6730 case ELF_INFO_TYPE_EH_FRAME:
6731 return TRUE;
6732 default:
6733 break;
6736 bed = get_elf_backend_data (sec->owner);
6737 if (bed->elf_backend_ignore_discarded_relocs != NULL
6738 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6739 return TRUE;
6741 return FALSE;
6744 /* Return a mask saying how ld should treat relocations in SEC against
6745 symbols defined in discarded sections. If this function returns
6746 COMPLAIN set, ld will issue a warning message. If this function
6747 returns PRETEND set, and the discarded section was link-once and the
6748 same size as the kept link-once section, ld will pretend that the
6749 symbol was actually defined in the kept section. Otherwise ld will
6750 zero the reloc (at least that is the intent, but some cooperation by
6751 the target dependent code is needed, particularly for REL targets). */
6753 unsigned int
6754 _bfd_elf_default_action_discarded (asection *sec)
6756 if (sec->flags & SEC_DEBUGGING)
6757 return PRETEND;
6759 if (strcmp (".eh_frame", sec->name) == 0)
6760 return 0;
6762 if (strcmp (".gcc_except_table", sec->name) == 0)
6763 return 0;
6765 return COMPLAIN | PRETEND;
6768 /* Find a match between a section and a member of a section group. */
6770 static asection *
6771 match_group_member (asection *sec, asection *group)
6773 asection *first = elf_next_in_group (group);
6774 asection *s = first;
6776 while (s != NULL)
6778 if (bfd_elf_match_symbols_in_sections (s, sec))
6779 return s;
6781 s = elf_next_in_group (s);
6782 if (s == first)
6783 break;
6786 return NULL;
6789 /* Check if the kept section of a discarded section SEC can be used
6790 to replace it. Return the replacement if it is OK. Otherwise return
6791 NULL. */
6793 asection *
6794 _bfd_elf_check_kept_section (asection *sec)
6796 asection *kept;
6798 kept = sec->kept_section;
6799 if (kept != NULL)
6801 if (elf_sec_group (sec) != NULL)
6802 kept = match_group_member (sec, kept);
6803 if (kept != NULL && sec->size != kept->size)
6804 kept = NULL;
6806 return kept;
6809 /* Link an input file into the linker output file. This function
6810 handles all the sections and relocations of the input file at once.
6811 This is so that we only have to read the local symbols once, and
6812 don't have to keep them in memory. */
6814 static bfd_boolean
6815 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6817 bfd_boolean (*relocate_section)
6818 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6819 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6820 bfd *output_bfd;
6821 Elf_Internal_Shdr *symtab_hdr;
6822 size_t locsymcount;
6823 size_t extsymoff;
6824 Elf_Internal_Sym *isymbuf;
6825 Elf_Internal_Sym *isym;
6826 Elf_Internal_Sym *isymend;
6827 long *pindex;
6828 asection **ppsection;
6829 asection *o;
6830 const struct elf_backend_data *bed;
6831 bfd_boolean emit_relocs;
6832 struct elf_link_hash_entry **sym_hashes;
6834 output_bfd = finfo->output_bfd;
6835 bed = get_elf_backend_data (output_bfd);
6836 relocate_section = bed->elf_backend_relocate_section;
6838 /* If this is a dynamic object, we don't want to do anything here:
6839 we don't want the local symbols, and we don't want the section
6840 contents. */
6841 if ((input_bfd->flags & DYNAMIC) != 0)
6842 return TRUE;
6844 emit_relocs = (finfo->info->relocatable
6845 || finfo->info->emitrelocations);
6847 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6848 if (elf_bad_symtab (input_bfd))
6850 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6851 extsymoff = 0;
6853 else
6855 locsymcount = symtab_hdr->sh_info;
6856 extsymoff = symtab_hdr->sh_info;
6859 /* Read the local symbols. */
6860 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6861 if (isymbuf == NULL && locsymcount != 0)
6863 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6864 finfo->internal_syms,
6865 finfo->external_syms,
6866 finfo->locsym_shndx);
6867 if (isymbuf == NULL)
6868 return FALSE;
6871 /* Find local symbol sections and adjust values of symbols in
6872 SEC_MERGE sections. Write out those local symbols we know are
6873 going into the output file. */
6874 isymend = isymbuf + locsymcount;
6875 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6876 isym < isymend;
6877 isym++, pindex++, ppsection++)
6879 asection *isec;
6880 const char *name;
6881 Elf_Internal_Sym osym;
6883 *pindex = -1;
6885 if (elf_bad_symtab (input_bfd))
6887 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6889 *ppsection = NULL;
6890 continue;
6894 if (isym->st_shndx == SHN_UNDEF)
6895 isec = bfd_und_section_ptr;
6896 else if (isym->st_shndx < SHN_LORESERVE
6897 || isym->st_shndx > SHN_HIRESERVE)
6899 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6900 if (isec
6901 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6902 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6903 isym->st_value =
6904 _bfd_merged_section_offset (output_bfd, &isec,
6905 elf_section_data (isec)->sec_info,
6906 isym->st_value);
6908 else if (isym->st_shndx == SHN_ABS)
6909 isec = bfd_abs_section_ptr;
6910 else if (isym->st_shndx == SHN_COMMON)
6911 isec = bfd_com_section_ptr;
6912 else
6914 /* Don't attempt to output symbols with st_shnx in the
6915 reserved range other than SHN_ABS and SHN_COMMON. */
6916 *ppsection = NULL;
6917 continue;
6920 *ppsection = isec;
6922 /* Don't output the first, undefined, symbol. */
6923 if (ppsection == finfo->sections)
6924 continue;
6926 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6928 /* We never output section symbols. Instead, we use the
6929 section symbol of the corresponding section in the output
6930 file. */
6931 continue;
6934 /* If we are stripping all symbols, we don't want to output this
6935 one. */
6936 if (finfo->info->strip == strip_all)
6937 continue;
6939 /* If we are discarding all local symbols, we don't want to
6940 output this one. If we are generating a relocatable output
6941 file, then some of the local symbols may be required by
6942 relocs; we output them below as we discover that they are
6943 needed. */
6944 if (finfo->info->discard == discard_all)
6945 continue;
6947 /* If this symbol is defined in a section which we are
6948 discarding, we don't need to keep it. */
6949 if (isym->st_shndx != SHN_UNDEF
6950 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6951 && (isec == NULL
6952 || bfd_section_removed_from_list (output_bfd,
6953 isec->output_section)))
6954 continue;
6956 /* Get the name of the symbol. */
6957 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6958 isym->st_name);
6959 if (name == NULL)
6960 return FALSE;
6962 /* See if we are discarding symbols with this name. */
6963 if ((finfo->info->strip == strip_some
6964 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6965 == NULL))
6966 || (((finfo->info->discard == discard_sec_merge
6967 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6968 || finfo->info->discard == discard_l)
6969 && bfd_is_local_label_name (input_bfd, name)))
6970 continue;
6972 /* If we get here, we are going to output this symbol. */
6974 osym = *isym;
6976 /* Adjust the section index for the output file. */
6977 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6978 isec->output_section);
6979 if (osym.st_shndx == SHN_BAD)
6980 return FALSE;
6982 *pindex = bfd_get_symcount (output_bfd);
6984 /* ELF symbols in relocatable files are section relative, but
6985 in executable files they are virtual addresses. Note that
6986 this code assumes that all ELF sections have an associated
6987 BFD section with a reasonable value for output_offset; below
6988 we assume that they also have a reasonable value for
6989 output_section. Any special sections must be set up to meet
6990 these requirements. */
6991 osym.st_value += isec->output_offset;
6992 if (! finfo->info->relocatable)
6994 osym.st_value += isec->output_section->vma;
6995 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6997 /* STT_TLS symbols are relative to PT_TLS segment base. */
6998 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6999 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7003 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
7004 return FALSE;
7007 /* Relocate the contents of each section. */
7008 sym_hashes = elf_sym_hashes (input_bfd);
7009 for (o = input_bfd->sections; o != NULL; o = o->next)
7011 bfd_byte *contents;
7013 if (! o->linker_mark)
7015 /* This section was omitted from the link. */
7016 continue;
7019 if ((o->flags & SEC_HAS_CONTENTS) == 0
7020 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
7021 continue;
7023 if ((o->flags & SEC_LINKER_CREATED) != 0)
7025 /* Section was created by _bfd_elf_link_create_dynamic_sections
7026 or somesuch. */
7027 continue;
7030 /* Get the contents of the section. They have been cached by a
7031 relaxation routine. Note that o is a section in an input
7032 file, so the contents field will not have been set by any of
7033 the routines which work on output files. */
7034 if (elf_section_data (o)->this_hdr.contents != NULL)
7035 contents = elf_section_data (o)->this_hdr.contents;
7036 else
7038 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7040 contents = finfo->contents;
7041 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7042 return FALSE;
7045 if ((o->flags & SEC_RELOC) != 0)
7047 Elf_Internal_Rela *internal_relocs;
7048 bfd_vma r_type_mask;
7049 int r_sym_shift;
7051 /* Get the swapped relocs. */
7052 internal_relocs
7053 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7054 finfo->internal_relocs, FALSE);
7055 if (internal_relocs == NULL
7056 && o->reloc_count > 0)
7057 return FALSE;
7059 if (bed->s->arch_size == 32)
7061 r_type_mask = 0xff;
7062 r_sym_shift = 8;
7064 else
7066 r_type_mask = 0xffffffff;
7067 r_sym_shift = 32;
7070 /* Run through the relocs looking for any against symbols
7071 from discarded sections and section symbols from
7072 removed link-once sections. Complain about relocs
7073 against discarded sections. Zero relocs against removed
7074 link-once sections. */
7075 if (!elf_section_ignore_discarded_relocs (o))
7077 Elf_Internal_Rela *rel, *relend;
7078 unsigned int action = (*bed->action_discarded) (o);
7080 rel = internal_relocs;
7081 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7082 for ( ; rel < relend; rel++)
7084 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7085 asection **ps, *sec;
7086 struct elf_link_hash_entry *h = NULL;
7087 const char *sym_name;
7089 if (r_symndx == STN_UNDEF)
7090 continue;
7092 if (r_symndx >= locsymcount
7093 || (elf_bad_symtab (input_bfd)
7094 && finfo->sections[r_symndx] == NULL))
7096 h = sym_hashes[r_symndx - extsymoff];
7098 /* Badly formatted input files can contain relocs that
7099 reference non-existant symbols. Check here so that
7100 we do not seg fault. */
7101 if (h == NULL)
7103 char buffer [32];
7105 sprintf_vma (buffer, rel->r_info);
7106 (*_bfd_error_handler)
7107 (_("error: %B contains a reloc (0x%s) for section %A "
7108 "that references a non-existent global symbol"),
7109 input_bfd, o, buffer);
7110 bfd_set_error (bfd_error_bad_value);
7111 return FALSE;
7114 while (h->root.type == bfd_link_hash_indirect
7115 || h->root.type == bfd_link_hash_warning)
7116 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7118 if (h->root.type != bfd_link_hash_defined
7119 && h->root.type != bfd_link_hash_defweak)
7120 continue;
7122 ps = &h->root.u.def.section;
7123 sym_name = h->root.root.string;
7125 else
7127 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7128 ps = &finfo->sections[r_symndx];
7129 sym_name = bfd_elf_sym_name (input_bfd,
7130 symtab_hdr,
7131 sym, *ps);
7134 /* Complain if the definition comes from a
7135 discarded section. */
7136 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7138 BFD_ASSERT (r_symndx != 0);
7139 if (action & COMPLAIN)
7140 (*finfo->info->callbacks->einfo)
7141 (_("%X`%s' referenced in section `%A' of %B: "
7142 "defined in discarded section `%A' of %B\n"),
7143 sym_name, o, input_bfd, sec, sec->owner);
7145 /* Try to do the best we can to support buggy old
7146 versions of gcc. Pretend that the symbol is
7147 really defined in the kept linkonce section.
7148 FIXME: This is quite broken. Modifying the
7149 symbol here means we will be changing all later
7150 uses of the symbol, not just in this section. */
7151 if (action & PRETEND)
7153 asection *kept;
7155 kept = _bfd_elf_check_kept_section (sec);
7156 if (kept != NULL)
7158 *ps = kept;
7159 continue;
7163 /* Remove the symbol reference from the reloc, but
7164 don't kill the reloc completely. This is so that
7165 a zero value will be written into the section,
7166 which may have non-zero contents put there by the
7167 assembler. Zero in things like an eh_frame fde
7168 pc_begin allows stack unwinders to recognize the
7169 fde as bogus. */
7170 rel->r_info &= r_type_mask;
7171 rel->r_addend = 0;
7176 /* Relocate the section by invoking a back end routine.
7178 The back end routine is responsible for adjusting the
7179 section contents as necessary, and (if using Rela relocs
7180 and generating a relocatable output file) adjusting the
7181 reloc addend as necessary.
7183 The back end routine does not have to worry about setting
7184 the reloc address or the reloc symbol index.
7186 The back end routine is given a pointer to the swapped in
7187 internal symbols, and can access the hash table entries
7188 for the external symbols via elf_sym_hashes (input_bfd).
7190 When generating relocatable output, the back end routine
7191 must handle STB_LOCAL/STT_SECTION symbols specially. The
7192 output symbol is going to be a section symbol
7193 corresponding to the output section, which will require
7194 the addend to be adjusted. */
7196 if (! (*relocate_section) (output_bfd, finfo->info,
7197 input_bfd, o, contents,
7198 internal_relocs,
7199 isymbuf,
7200 finfo->sections))
7201 return FALSE;
7203 if (emit_relocs)
7205 Elf_Internal_Rela *irela;
7206 Elf_Internal_Rela *irelaend;
7207 bfd_vma last_offset;
7208 struct elf_link_hash_entry **rel_hash;
7209 struct elf_link_hash_entry **rel_hash_list;
7210 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7211 unsigned int next_erel;
7212 bfd_boolean rela_normal;
7214 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7215 rela_normal = (bed->rela_normal
7216 && (input_rel_hdr->sh_entsize
7217 == bed->s->sizeof_rela));
7219 /* Adjust the reloc addresses and symbol indices. */
7221 irela = internal_relocs;
7222 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7223 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7224 + elf_section_data (o->output_section)->rel_count
7225 + elf_section_data (o->output_section)->rel_count2);
7226 rel_hash_list = rel_hash;
7227 last_offset = o->output_offset;
7228 if (!finfo->info->relocatable)
7229 last_offset += o->output_section->vma;
7230 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7232 unsigned long r_symndx;
7233 asection *sec;
7234 Elf_Internal_Sym sym;
7236 if (next_erel == bed->s->int_rels_per_ext_rel)
7238 rel_hash++;
7239 next_erel = 0;
7242 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7243 finfo->info, o,
7244 irela->r_offset);
7245 if (irela->r_offset >= (bfd_vma) -2)
7247 /* This is a reloc for a deleted entry or somesuch.
7248 Turn it into an R_*_NONE reloc, at the same
7249 offset as the last reloc. elf_eh_frame.c and
7250 bfd_elf_discard_info rely on reloc offsets
7251 being ordered. */
7252 irela->r_offset = last_offset;
7253 irela->r_info = 0;
7254 irela->r_addend = 0;
7255 continue;
7258 irela->r_offset += o->output_offset;
7260 /* Relocs in an executable have to be virtual addresses. */
7261 if (!finfo->info->relocatable)
7262 irela->r_offset += o->output_section->vma;
7264 last_offset = irela->r_offset;
7266 r_symndx = irela->r_info >> r_sym_shift;
7267 if (r_symndx == STN_UNDEF)
7268 continue;
7270 if (r_symndx >= locsymcount
7271 || (elf_bad_symtab (input_bfd)
7272 && finfo->sections[r_symndx] == NULL))
7274 struct elf_link_hash_entry *rh;
7275 unsigned long indx;
7277 /* This is a reloc against a global symbol. We
7278 have not yet output all the local symbols, so
7279 we do not know the symbol index of any global
7280 symbol. We set the rel_hash entry for this
7281 reloc to point to the global hash table entry
7282 for this symbol. The symbol index is then
7283 set at the end of bfd_elf_final_link. */
7284 indx = r_symndx - extsymoff;
7285 rh = elf_sym_hashes (input_bfd)[indx];
7286 while (rh->root.type == bfd_link_hash_indirect
7287 || rh->root.type == bfd_link_hash_warning)
7288 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7290 /* Setting the index to -2 tells
7291 elf_link_output_extsym that this symbol is
7292 used by a reloc. */
7293 BFD_ASSERT (rh->indx < 0);
7294 rh->indx = -2;
7296 *rel_hash = rh;
7298 continue;
7301 /* This is a reloc against a local symbol. */
7303 *rel_hash = NULL;
7304 sym = isymbuf[r_symndx];
7305 sec = finfo->sections[r_symndx];
7306 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7308 /* I suppose the backend ought to fill in the
7309 section of any STT_SECTION symbol against a
7310 processor specific section. */
7311 r_symndx = 0;
7312 if (bfd_is_abs_section (sec))
7314 else if (sec == NULL || sec->owner == NULL)
7316 bfd_set_error (bfd_error_bad_value);
7317 return FALSE;
7319 else
7321 asection *osec = sec->output_section;
7323 /* If we have discarded a section, the output
7324 section will be the absolute section. In
7325 case of discarded link-once and discarded
7326 SEC_MERGE sections, use the kept section. */
7327 if (bfd_is_abs_section (osec)
7328 && sec->kept_section != NULL
7329 && sec->kept_section->output_section != NULL)
7331 osec = sec->kept_section->output_section;
7332 irela->r_addend -= osec->vma;
7335 if (!bfd_is_abs_section (osec))
7337 r_symndx = osec->target_index;
7338 BFD_ASSERT (r_symndx != 0);
7342 /* Adjust the addend according to where the
7343 section winds up in the output section. */
7344 if (rela_normal)
7345 irela->r_addend += sec->output_offset;
7347 else
7349 if (finfo->indices[r_symndx] == -1)
7351 unsigned long shlink;
7352 const char *name;
7353 asection *osec;
7355 if (finfo->info->strip == strip_all)
7357 /* You can't do ld -r -s. */
7358 bfd_set_error (bfd_error_invalid_operation);
7359 return FALSE;
7362 /* This symbol was skipped earlier, but
7363 since it is needed by a reloc, we
7364 must output it now. */
7365 shlink = symtab_hdr->sh_link;
7366 name = (bfd_elf_string_from_elf_section
7367 (input_bfd, shlink, sym.st_name));
7368 if (name == NULL)
7369 return FALSE;
7371 osec = sec->output_section;
7372 sym.st_shndx =
7373 _bfd_elf_section_from_bfd_section (output_bfd,
7374 osec);
7375 if (sym.st_shndx == SHN_BAD)
7376 return FALSE;
7378 sym.st_value += sec->output_offset;
7379 if (! finfo->info->relocatable)
7381 sym.st_value += osec->vma;
7382 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7384 /* STT_TLS symbols are relative to PT_TLS
7385 segment base. */
7386 BFD_ASSERT (elf_hash_table (finfo->info)
7387 ->tls_sec != NULL);
7388 sym.st_value -= (elf_hash_table (finfo->info)
7389 ->tls_sec->vma);
7393 finfo->indices[r_symndx]
7394 = bfd_get_symcount (output_bfd);
7396 if (! elf_link_output_sym (finfo, name, &sym, sec,
7397 NULL))
7398 return FALSE;
7401 r_symndx = finfo->indices[r_symndx];
7404 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7405 | (irela->r_info & r_type_mask));
7408 /* Swap out the relocs. */
7409 if (input_rel_hdr->sh_size != 0
7410 && !bed->elf_backend_emit_relocs (output_bfd, o,
7411 input_rel_hdr,
7412 internal_relocs,
7413 rel_hash_list))
7414 return FALSE;
7416 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7417 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7419 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7420 * bed->s->int_rels_per_ext_rel);
7421 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7422 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7423 input_rel_hdr2,
7424 internal_relocs,
7425 rel_hash_list))
7426 return FALSE;
7431 /* Write out the modified section contents. */
7432 if (bed->elf_backend_write_section
7433 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7435 /* Section written out. */
7437 else switch (o->sec_info_type)
7439 case ELF_INFO_TYPE_STABS:
7440 if (! (_bfd_write_section_stabs
7441 (output_bfd,
7442 &elf_hash_table (finfo->info)->stab_info,
7443 o, &elf_section_data (o)->sec_info, contents)))
7444 return FALSE;
7445 break;
7446 case ELF_INFO_TYPE_MERGE:
7447 if (! _bfd_write_merged_section (output_bfd, o,
7448 elf_section_data (o)->sec_info))
7449 return FALSE;
7450 break;
7451 case ELF_INFO_TYPE_EH_FRAME:
7453 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7454 o, contents))
7455 return FALSE;
7457 break;
7458 default:
7460 if (! (o->flags & SEC_EXCLUDE)
7461 && ! bfd_set_section_contents (output_bfd, o->output_section,
7462 contents,
7463 (file_ptr) o->output_offset,
7464 o->size))
7465 return FALSE;
7467 break;
7471 return TRUE;
7474 /* Generate a reloc when linking an ELF file. This is a reloc
7475 requested by the linker, and does not come from any input file. This
7476 is used to build constructor and destructor tables when linking
7477 with -Ur. */
7479 static bfd_boolean
7480 elf_reloc_link_order (bfd *output_bfd,
7481 struct bfd_link_info *info,
7482 asection *output_section,
7483 struct bfd_link_order *link_order)
7485 reloc_howto_type *howto;
7486 long indx;
7487 bfd_vma offset;
7488 bfd_vma addend;
7489 struct elf_link_hash_entry **rel_hash_ptr;
7490 Elf_Internal_Shdr *rel_hdr;
7491 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7492 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7493 bfd_byte *erel;
7494 unsigned int i;
7496 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7497 if (howto == NULL)
7499 bfd_set_error (bfd_error_bad_value);
7500 return FALSE;
7503 addend = link_order->u.reloc.p->addend;
7505 /* Figure out the symbol index. */
7506 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7507 + elf_section_data (output_section)->rel_count
7508 + elf_section_data (output_section)->rel_count2);
7509 if (link_order->type == bfd_section_reloc_link_order)
7511 indx = link_order->u.reloc.p->u.section->target_index;
7512 BFD_ASSERT (indx != 0);
7513 *rel_hash_ptr = NULL;
7515 else
7517 struct elf_link_hash_entry *h;
7519 /* Treat a reloc against a defined symbol as though it were
7520 actually against the section. */
7521 h = ((struct elf_link_hash_entry *)
7522 bfd_wrapped_link_hash_lookup (output_bfd, info,
7523 link_order->u.reloc.p->u.name,
7524 FALSE, FALSE, TRUE));
7525 if (h != NULL
7526 && (h->root.type == bfd_link_hash_defined
7527 || h->root.type == bfd_link_hash_defweak))
7529 asection *section;
7531 section = h->root.u.def.section;
7532 indx = section->output_section->target_index;
7533 *rel_hash_ptr = NULL;
7534 /* It seems that we ought to add the symbol value to the
7535 addend here, but in practice it has already been added
7536 because it was passed to constructor_callback. */
7537 addend += section->output_section->vma + section->output_offset;
7539 else if (h != NULL)
7541 /* Setting the index to -2 tells elf_link_output_extsym that
7542 this symbol is used by a reloc. */
7543 h->indx = -2;
7544 *rel_hash_ptr = h;
7545 indx = 0;
7547 else
7549 if (! ((*info->callbacks->unattached_reloc)
7550 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7551 return FALSE;
7552 indx = 0;
7556 /* If this is an inplace reloc, we must write the addend into the
7557 object file. */
7558 if (howto->partial_inplace && addend != 0)
7560 bfd_size_type size;
7561 bfd_reloc_status_type rstat;
7562 bfd_byte *buf;
7563 bfd_boolean ok;
7564 const char *sym_name;
7566 size = bfd_get_reloc_size (howto);
7567 buf = bfd_zmalloc (size);
7568 if (buf == NULL)
7569 return FALSE;
7570 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7571 switch (rstat)
7573 case bfd_reloc_ok:
7574 break;
7576 default:
7577 case bfd_reloc_outofrange:
7578 abort ();
7580 case bfd_reloc_overflow:
7581 if (link_order->type == bfd_section_reloc_link_order)
7582 sym_name = bfd_section_name (output_bfd,
7583 link_order->u.reloc.p->u.section);
7584 else
7585 sym_name = link_order->u.reloc.p->u.name;
7586 if (! ((*info->callbacks->reloc_overflow)
7587 (info, NULL, sym_name, howto->name, addend, NULL,
7588 NULL, (bfd_vma) 0)))
7590 free (buf);
7591 return FALSE;
7593 break;
7595 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7596 link_order->offset, size);
7597 free (buf);
7598 if (! ok)
7599 return FALSE;
7602 /* The address of a reloc is relative to the section in a
7603 relocatable file, and is a virtual address in an executable
7604 file. */
7605 offset = link_order->offset;
7606 if (! info->relocatable)
7607 offset += output_section->vma;
7609 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7611 irel[i].r_offset = offset;
7612 irel[i].r_info = 0;
7613 irel[i].r_addend = 0;
7615 if (bed->s->arch_size == 32)
7616 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7617 else
7618 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7620 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7621 erel = rel_hdr->contents;
7622 if (rel_hdr->sh_type == SHT_REL)
7624 erel += (elf_section_data (output_section)->rel_count
7625 * bed->s->sizeof_rel);
7626 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7628 else
7630 irel[0].r_addend = addend;
7631 erel += (elf_section_data (output_section)->rel_count
7632 * bed->s->sizeof_rela);
7633 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7636 ++elf_section_data (output_section)->rel_count;
7638 return TRUE;
7642 /* Get the output vma of the section pointed to by the sh_link field. */
7644 static bfd_vma
7645 elf_get_linked_section_vma (struct bfd_link_order *p)
7647 Elf_Internal_Shdr **elf_shdrp;
7648 asection *s;
7649 int elfsec;
7651 s = p->u.indirect.section;
7652 elf_shdrp = elf_elfsections (s->owner);
7653 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7654 elfsec = elf_shdrp[elfsec]->sh_link;
7655 /* PR 290:
7656 The Intel C compiler generates SHT_IA_64_UNWIND with
7657 SHF_LINK_ORDER. But it doesn't set the sh_link or
7658 sh_info fields. Hence we could get the situation
7659 where elfsec is 0. */
7660 if (elfsec == 0)
7662 const struct elf_backend_data *bed
7663 = get_elf_backend_data (s->owner);
7664 if (bed->link_order_error_handler)
7665 bed->link_order_error_handler
7666 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7667 return 0;
7669 else
7671 s = elf_shdrp[elfsec]->bfd_section;
7672 return s->output_section->vma + s->output_offset;
7677 /* Compare two sections based on the locations of the sections they are
7678 linked to. Used by elf_fixup_link_order. */
7680 static int
7681 compare_link_order (const void * a, const void * b)
7683 bfd_vma apos;
7684 bfd_vma bpos;
7686 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7687 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7688 if (apos < bpos)
7689 return -1;
7690 return apos > bpos;
7694 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7695 order as their linked sections. Returns false if this could not be done
7696 because an output section includes both ordered and unordered
7697 sections. Ideally we'd do this in the linker proper. */
7699 static bfd_boolean
7700 elf_fixup_link_order (bfd *abfd, asection *o)
7702 int seen_linkorder;
7703 int seen_other;
7704 int n;
7705 struct bfd_link_order *p;
7706 bfd *sub;
7707 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7708 unsigned elfsec;
7709 struct bfd_link_order **sections;
7710 asection *s, *other_sec, *linkorder_sec;
7711 bfd_vma offset;
7713 other_sec = NULL;
7714 linkorder_sec = NULL;
7715 seen_other = 0;
7716 seen_linkorder = 0;
7717 for (p = o->map_head.link_order; p != NULL; p = p->next)
7719 if (p->type == bfd_indirect_link_order)
7721 s = p->u.indirect.section;
7722 sub = s->owner;
7723 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7724 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7725 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
7726 && elfsec < elf_numsections (sub)
7727 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7729 seen_linkorder++;
7730 linkorder_sec = s;
7732 else
7734 seen_other++;
7735 other_sec = s;
7738 else
7739 seen_other++;
7741 if (seen_other && seen_linkorder)
7743 if (other_sec && linkorder_sec)
7744 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7745 o, linkorder_sec,
7746 linkorder_sec->owner, other_sec,
7747 other_sec->owner);
7748 else
7749 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7751 bfd_set_error (bfd_error_bad_value);
7752 return FALSE;
7756 if (!seen_linkorder)
7757 return TRUE;
7759 sections = (struct bfd_link_order **)
7760 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7761 seen_linkorder = 0;
7763 for (p = o->map_head.link_order; p != NULL; p = p->next)
7765 sections[seen_linkorder++] = p;
7767 /* Sort the input sections in the order of their linked section. */
7768 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7769 compare_link_order);
7771 /* Change the offsets of the sections. */
7772 offset = 0;
7773 for (n = 0; n < seen_linkorder; n++)
7775 s = sections[n]->u.indirect.section;
7776 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7777 s->output_offset = offset;
7778 sections[n]->offset = offset;
7779 offset += sections[n]->size;
7782 return TRUE;
7786 /* Do the final step of an ELF link. */
7788 bfd_boolean
7789 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7791 bfd_boolean dynamic;
7792 bfd_boolean emit_relocs;
7793 bfd *dynobj;
7794 struct elf_final_link_info finfo;
7795 register asection *o;
7796 register struct bfd_link_order *p;
7797 register bfd *sub;
7798 bfd_size_type max_contents_size;
7799 bfd_size_type max_external_reloc_size;
7800 bfd_size_type max_internal_reloc_count;
7801 bfd_size_type max_sym_count;
7802 bfd_size_type max_sym_shndx_count;
7803 file_ptr off;
7804 Elf_Internal_Sym elfsym;
7805 unsigned int i;
7806 Elf_Internal_Shdr *symtab_hdr;
7807 Elf_Internal_Shdr *symtab_shndx_hdr;
7808 Elf_Internal_Shdr *symstrtab_hdr;
7809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7810 struct elf_outext_info eoinfo;
7811 bfd_boolean merged;
7812 size_t relativecount = 0;
7813 asection *reldyn = 0;
7814 bfd_size_type amt;
7816 if (! is_elf_hash_table (info->hash))
7817 return FALSE;
7819 if (info->shared)
7820 abfd->flags |= DYNAMIC;
7822 dynamic = elf_hash_table (info)->dynamic_sections_created;
7823 dynobj = elf_hash_table (info)->dynobj;
7825 emit_relocs = (info->relocatable
7826 || info->emitrelocations);
7828 finfo.info = info;
7829 finfo.output_bfd = abfd;
7830 finfo.symstrtab = _bfd_elf_stringtab_init ();
7831 if (finfo.symstrtab == NULL)
7832 return FALSE;
7834 if (! dynamic)
7836 finfo.dynsym_sec = NULL;
7837 finfo.hash_sec = NULL;
7838 finfo.symver_sec = NULL;
7840 else
7842 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7843 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7844 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7845 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7846 /* Note that it is OK if symver_sec is NULL. */
7849 finfo.contents = NULL;
7850 finfo.external_relocs = NULL;
7851 finfo.internal_relocs = NULL;
7852 finfo.external_syms = NULL;
7853 finfo.locsym_shndx = NULL;
7854 finfo.internal_syms = NULL;
7855 finfo.indices = NULL;
7856 finfo.sections = NULL;
7857 finfo.symbuf = NULL;
7858 finfo.symshndxbuf = NULL;
7859 finfo.symbuf_count = 0;
7860 finfo.shndxbuf_size = 0;
7862 /* Count up the number of relocations we will output for each output
7863 section, so that we know the sizes of the reloc sections. We
7864 also figure out some maximum sizes. */
7865 max_contents_size = 0;
7866 max_external_reloc_size = 0;
7867 max_internal_reloc_count = 0;
7868 max_sym_count = 0;
7869 max_sym_shndx_count = 0;
7870 merged = FALSE;
7871 for (o = abfd->sections; o != NULL; o = o->next)
7873 struct bfd_elf_section_data *esdo = elf_section_data (o);
7874 o->reloc_count = 0;
7876 for (p = o->map_head.link_order; p != NULL; p = p->next)
7878 unsigned int reloc_count = 0;
7879 struct bfd_elf_section_data *esdi = NULL;
7880 unsigned int *rel_count1;
7882 if (p->type == bfd_section_reloc_link_order
7883 || p->type == bfd_symbol_reloc_link_order)
7884 reloc_count = 1;
7885 else if (p->type == bfd_indirect_link_order)
7887 asection *sec;
7889 sec = p->u.indirect.section;
7890 esdi = elf_section_data (sec);
7892 /* Mark all sections which are to be included in the
7893 link. This will normally be every section. We need
7894 to do this so that we can identify any sections which
7895 the linker has decided to not include. */
7896 sec->linker_mark = TRUE;
7898 if (sec->flags & SEC_MERGE)
7899 merged = TRUE;
7901 if (info->relocatable || info->emitrelocations)
7902 reloc_count = sec->reloc_count;
7903 else if (bed->elf_backend_count_relocs)
7905 Elf_Internal_Rela * relocs;
7907 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7908 info->keep_memory);
7910 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7912 if (elf_section_data (o)->relocs != relocs)
7913 free (relocs);
7916 if (sec->rawsize > max_contents_size)
7917 max_contents_size = sec->rawsize;
7918 if (sec->size > max_contents_size)
7919 max_contents_size = sec->size;
7921 /* We are interested in just local symbols, not all
7922 symbols. */
7923 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7924 && (sec->owner->flags & DYNAMIC) == 0)
7926 size_t sym_count;
7928 if (elf_bad_symtab (sec->owner))
7929 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7930 / bed->s->sizeof_sym);
7931 else
7932 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7934 if (sym_count > max_sym_count)
7935 max_sym_count = sym_count;
7937 if (sym_count > max_sym_shndx_count
7938 && elf_symtab_shndx (sec->owner) != 0)
7939 max_sym_shndx_count = sym_count;
7941 if ((sec->flags & SEC_RELOC) != 0)
7943 size_t ext_size;
7945 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7946 if (ext_size > max_external_reloc_size)
7947 max_external_reloc_size = ext_size;
7948 if (sec->reloc_count > max_internal_reloc_count)
7949 max_internal_reloc_count = sec->reloc_count;
7954 if (reloc_count == 0)
7955 continue;
7957 o->reloc_count += reloc_count;
7959 /* MIPS may have a mix of REL and RELA relocs on sections.
7960 To support this curious ABI we keep reloc counts in
7961 elf_section_data too. We must be careful to add the
7962 relocations from the input section to the right output
7963 count. FIXME: Get rid of one count. We have
7964 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7965 rel_count1 = &esdo->rel_count;
7966 if (esdi != NULL)
7968 bfd_boolean same_size;
7969 bfd_size_type entsize1;
7971 entsize1 = esdi->rel_hdr.sh_entsize;
7972 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7973 || entsize1 == bed->s->sizeof_rela);
7974 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7976 if (!same_size)
7977 rel_count1 = &esdo->rel_count2;
7979 if (esdi->rel_hdr2 != NULL)
7981 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7982 unsigned int alt_count;
7983 unsigned int *rel_count2;
7985 BFD_ASSERT (entsize2 != entsize1
7986 && (entsize2 == bed->s->sizeof_rel
7987 || entsize2 == bed->s->sizeof_rela));
7989 rel_count2 = &esdo->rel_count2;
7990 if (!same_size)
7991 rel_count2 = &esdo->rel_count;
7993 /* The following is probably too simplistic if the
7994 backend counts output relocs unusually. */
7995 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7996 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7997 *rel_count2 += alt_count;
7998 reloc_count -= alt_count;
8001 *rel_count1 += reloc_count;
8004 if (o->reloc_count > 0)
8005 o->flags |= SEC_RELOC;
8006 else
8008 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8009 set it (this is probably a bug) and if it is set
8010 assign_section_numbers will create a reloc section. */
8011 o->flags &=~ SEC_RELOC;
8014 /* If the SEC_ALLOC flag is not set, force the section VMA to
8015 zero. This is done in elf_fake_sections as well, but forcing
8016 the VMA to 0 here will ensure that relocs against these
8017 sections are handled correctly. */
8018 if ((o->flags & SEC_ALLOC) == 0
8019 && ! o->user_set_vma)
8020 o->vma = 0;
8023 if (! info->relocatable && merged)
8024 elf_link_hash_traverse (elf_hash_table (info),
8025 _bfd_elf_link_sec_merge_syms, abfd);
8027 /* Figure out the file positions for everything but the symbol table
8028 and the relocs. We set symcount to force assign_section_numbers
8029 to create a symbol table. */
8030 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
8031 BFD_ASSERT (! abfd->output_has_begun);
8032 if (! _bfd_elf_compute_section_file_positions (abfd, info))
8033 goto error_return;
8035 /* Set sizes, and assign file positions for reloc sections. */
8036 for (o = abfd->sections; o != NULL; o = o->next)
8038 if ((o->flags & SEC_RELOC) != 0)
8040 if (!(_bfd_elf_link_size_reloc_section
8041 (abfd, &elf_section_data (o)->rel_hdr, o)))
8042 goto error_return;
8044 if (elf_section_data (o)->rel_hdr2
8045 && !(_bfd_elf_link_size_reloc_section
8046 (abfd, elf_section_data (o)->rel_hdr2, o)))
8047 goto error_return;
8050 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8051 to count upwards while actually outputting the relocations. */
8052 elf_section_data (o)->rel_count = 0;
8053 elf_section_data (o)->rel_count2 = 0;
8056 _bfd_elf_assign_file_positions_for_relocs (abfd);
8058 /* We have now assigned file positions for all the sections except
8059 .symtab and .strtab. We start the .symtab section at the current
8060 file position, and write directly to it. We build the .strtab
8061 section in memory. */
8062 bfd_get_symcount (abfd) = 0;
8063 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8064 /* sh_name is set in prep_headers. */
8065 symtab_hdr->sh_type = SHT_SYMTAB;
8066 /* sh_flags, sh_addr and sh_size all start off zero. */
8067 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8068 /* sh_link is set in assign_section_numbers. */
8069 /* sh_info is set below. */
8070 /* sh_offset is set just below. */
8071 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8073 off = elf_tdata (abfd)->next_file_pos;
8074 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8076 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8077 incorrect. We do not yet know the size of the .symtab section.
8078 We correct next_file_pos below, after we do know the size. */
8080 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8081 continuously seeking to the right position in the file. */
8082 if (! info->keep_memory || max_sym_count < 20)
8083 finfo.symbuf_size = 20;
8084 else
8085 finfo.symbuf_size = max_sym_count;
8086 amt = finfo.symbuf_size;
8087 amt *= bed->s->sizeof_sym;
8088 finfo.symbuf = bfd_malloc (amt);
8089 if (finfo.symbuf == NULL)
8090 goto error_return;
8091 if (elf_numsections (abfd) > SHN_LORESERVE)
8093 /* Wild guess at number of output symbols. realloc'd as needed. */
8094 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8095 finfo.shndxbuf_size = amt;
8096 amt *= sizeof (Elf_External_Sym_Shndx);
8097 finfo.symshndxbuf = bfd_zmalloc (amt);
8098 if (finfo.symshndxbuf == NULL)
8099 goto error_return;
8102 /* Start writing out the symbol table. The first symbol is always a
8103 dummy symbol. */
8104 if (info->strip != strip_all
8105 || emit_relocs)
8107 elfsym.st_value = 0;
8108 elfsym.st_size = 0;
8109 elfsym.st_info = 0;
8110 elfsym.st_other = 0;
8111 elfsym.st_shndx = SHN_UNDEF;
8112 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8113 NULL))
8114 goto error_return;
8117 /* Output a symbol for each section. We output these even if we are
8118 discarding local symbols, since they are used for relocs. These
8119 symbols have no names. We store the index of each one in the
8120 index field of the section, so that we can find it again when
8121 outputting relocs. */
8122 if (info->strip != strip_all
8123 || emit_relocs)
8125 elfsym.st_size = 0;
8126 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8127 elfsym.st_other = 0;
8128 for (i = 1; i < elf_numsections (abfd); i++)
8130 o = bfd_section_from_elf_index (abfd, i);
8131 if (o != NULL)
8132 o->target_index = bfd_get_symcount (abfd);
8133 elfsym.st_shndx = i;
8134 if (info->relocatable || o == NULL)
8135 elfsym.st_value = 0;
8136 else
8137 elfsym.st_value = o->vma;
8138 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8139 goto error_return;
8140 if (i == SHN_LORESERVE - 1)
8141 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8145 /* Allocate some memory to hold information read in from the input
8146 files. */
8147 if (max_contents_size != 0)
8149 finfo.contents = bfd_malloc (max_contents_size);
8150 if (finfo.contents == NULL)
8151 goto error_return;
8154 if (max_external_reloc_size != 0)
8156 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8157 if (finfo.external_relocs == NULL)
8158 goto error_return;
8161 if (max_internal_reloc_count != 0)
8163 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8164 amt *= sizeof (Elf_Internal_Rela);
8165 finfo.internal_relocs = bfd_malloc (amt);
8166 if (finfo.internal_relocs == NULL)
8167 goto error_return;
8170 if (max_sym_count != 0)
8172 amt = max_sym_count * bed->s->sizeof_sym;
8173 finfo.external_syms = bfd_malloc (amt);
8174 if (finfo.external_syms == NULL)
8175 goto error_return;
8177 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8178 finfo.internal_syms = bfd_malloc (amt);
8179 if (finfo.internal_syms == NULL)
8180 goto error_return;
8182 amt = max_sym_count * sizeof (long);
8183 finfo.indices = bfd_malloc (amt);
8184 if (finfo.indices == NULL)
8185 goto error_return;
8187 amt = max_sym_count * sizeof (asection *);
8188 finfo.sections = bfd_malloc (amt);
8189 if (finfo.sections == NULL)
8190 goto error_return;
8193 if (max_sym_shndx_count != 0)
8195 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8196 finfo.locsym_shndx = bfd_malloc (amt);
8197 if (finfo.locsym_shndx == NULL)
8198 goto error_return;
8201 if (elf_hash_table (info)->tls_sec)
8203 bfd_vma base, end = 0;
8204 asection *sec;
8206 for (sec = elf_hash_table (info)->tls_sec;
8207 sec && (sec->flags & SEC_THREAD_LOCAL);
8208 sec = sec->next)
8210 bfd_size_type size = sec->size;
8212 if (size == 0
8213 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8215 struct bfd_link_order *o = sec->map_tail.link_order;
8216 if (o != NULL)
8217 size = o->offset + o->size;
8219 end = sec->vma + size;
8221 base = elf_hash_table (info)->tls_sec->vma;
8222 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8223 elf_hash_table (info)->tls_size = end - base;
8226 /* Reorder SHF_LINK_ORDER sections. */
8227 for (o = abfd->sections; o != NULL; o = o->next)
8229 if (!elf_fixup_link_order (abfd, o))
8230 return FALSE;
8233 /* Since ELF permits relocations to be against local symbols, we
8234 must have the local symbols available when we do the relocations.
8235 Since we would rather only read the local symbols once, and we
8236 would rather not keep them in memory, we handle all the
8237 relocations for a single input file at the same time.
8239 Unfortunately, there is no way to know the total number of local
8240 symbols until we have seen all of them, and the local symbol
8241 indices precede the global symbol indices. This means that when
8242 we are generating relocatable output, and we see a reloc against
8243 a global symbol, we can not know the symbol index until we have
8244 finished examining all the local symbols to see which ones we are
8245 going to output. To deal with this, we keep the relocations in
8246 memory, and don't output them until the end of the link. This is
8247 an unfortunate waste of memory, but I don't see a good way around
8248 it. Fortunately, it only happens when performing a relocatable
8249 link, which is not the common case. FIXME: If keep_memory is set
8250 we could write the relocs out and then read them again; I don't
8251 know how bad the memory loss will be. */
8253 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8254 sub->output_has_begun = FALSE;
8255 for (o = abfd->sections; o != NULL; o = o->next)
8257 for (p = o->map_head.link_order; p != NULL; p = p->next)
8259 if (p->type == bfd_indirect_link_order
8260 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8261 == bfd_target_elf_flavour)
8262 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8264 if (! sub->output_has_begun)
8266 if (! elf_link_input_bfd (&finfo, sub))
8267 goto error_return;
8268 sub->output_has_begun = TRUE;
8271 else if (p->type == bfd_section_reloc_link_order
8272 || p->type == bfd_symbol_reloc_link_order)
8274 if (! elf_reloc_link_order (abfd, info, o, p))
8275 goto error_return;
8277 else
8279 if (! _bfd_default_link_order (abfd, info, o, p))
8280 goto error_return;
8285 /* Output any global symbols that got converted to local in a
8286 version script or due to symbol visibility. We do this in a
8287 separate step since ELF requires all local symbols to appear
8288 prior to any global symbols. FIXME: We should only do this if
8289 some global symbols were, in fact, converted to become local.
8290 FIXME: Will this work correctly with the Irix 5 linker? */
8291 eoinfo.failed = FALSE;
8292 eoinfo.finfo = &finfo;
8293 eoinfo.localsyms = TRUE;
8294 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8295 &eoinfo);
8296 if (eoinfo.failed)
8297 return FALSE;
8299 /* If backend needs to output some local symbols not present in the hash
8300 table, do it now. */
8301 if (bed->elf_backend_output_arch_local_syms)
8303 typedef bfd_boolean (*out_sym_func)
8304 (void *, const char *, Elf_Internal_Sym *, asection *,
8305 struct elf_link_hash_entry *);
8307 if (! ((*bed->elf_backend_output_arch_local_syms)
8308 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8309 return FALSE;
8312 /* That wrote out all the local symbols. Finish up the symbol table
8313 with the global symbols. Even if we want to strip everything we
8314 can, we still need to deal with those global symbols that got
8315 converted to local in a version script. */
8317 /* The sh_info field records the index of the first non local symbol. */
8318 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8320 if (dynamic
8321 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8323 Elf_Internal_Sym sym;
8324 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8325 long last_local = 0;
8327 /* Write out the section symbols for the output sections. */
8328 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8330 asection *s;
8332 sym.st_size = 0;
8333 sym.st_name = 0;
8334 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8335 sym.st_other = 0;
8337 for (s = abfd->sections; s != NULL; s = s->next)
8339 int indx;
8340 bfd_byte *dest;
8341 long dynindx;
8343 dynindx = elf_section_data (s)->dynindx;
8344 if (dynindx <= 0)
8345 continue;
8346 indx = elf_section_data (s)->this_idx;
8347 BFD_ASSERT (indx > 0);
8348 sym.st_shndx = indx;
8349 if (! check_dynsym (abfd, &sym))
8350 return FALSE;
8351 sym.st_value = s->vma;
8352 dest = dynsym + dynindx * bed->s->sizeof_sym;
8353 if (last_local < dynindx)
8354 last_local = dynindx;
8355 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8359 /* Write out the local dynsyms. */
8360 if (elf_hash_table (info)->dynlocal)
8362 struct elf_link_local_dynamic_entry *e;
8363 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8365 asection *s;
8366 bfd_byte *dest;
8368 sym.st_size = e->isym.st_size;
8369 sym.st_other = e->isym.st_other;
8371 /* Copy the internal symbol as is.
8372 Note that we saved a word of storage and overwrote
8373 the original st_name with the dynstr_index. */
8374 sym = e->isym;
8376 if (e->isym.st_shndx != SHN_UNDEF
8377 && (e->isym.st_shndx < SHN_LORESERVE
8378 || e->isym.st_shndx > SHN_HIRESERVE))
8380 s = bfd_section_from_elf_index (e->input_bfd,
8381 e->isym.st_shndx);
8383 sym.st_shndx =
8384 elf_section_data (s->output_section)->this_idx;
8385 if (! check_dynsym (abfd, &sym))
8386 return FALSE;
8387 sym.st_value = (s->output_section->vma
8388 + s->output_offset
8389 + e->isym.st_value);
8392 if (last_local < e->dynindx)
8393 last_local = e->dynindx;
8395 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8396 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8400 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8401 last_local + 1;
8404 /* We get the global symbols from the hash table. */
8405 eoinfo.failed = FALSE;
8406 eoinfo.localsyms = FALSE;
8407 eoinfo.finfo = &finfo;
8408 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8409 &eoinfo);
8410 if (eoinfo.failed)
8411 return FALSE;
8413 /* If backend needs to output some symbols not present in the hash
8414 table, do it now. */
8415 if (bed->elf_backend_output_arch_syms)
8417 typedef bfd_boolean (*out_sym_func)
8418 (void *, const char *, Elf_Internal_Sym *, asection *,
8419 struct elf_link_hash_entry *);
8421 if (! ((*bed->elf_backend_output_arch_syms)
8422 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8423 return FALSE;
8426 /* Flush all symbols to the file. */
8427 if (! elf_link_flush_output_syms (&finfo, bed))
8428 return FALSE;
8430 /* Now we know the size of the symtab section. */
8431 off += symtab_hdr->sh_size;
8433 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8434 if (symtab_shndx_hdr->sh_name != 0)
8436 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8437 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8438 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8439 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8440 symtab_shndx_hdr->sh_size = amt;
8442 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8443 off, TRUE);
8445 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8446 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8447 return FALSE;
8451 /* Finish up and write out the symbol string table (.strtab)
8452 section. */
8453 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8454 /* sh_name was set in prep_headers. */
8455 symstrtab_hdr->sh_type = SHT_STRTAB;
8456 symstrtab_hdr->sh_flags = 0;
8457 symstrtab_hdr->sh_addr = 0;
8458 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8459 symstrtab_hdr->sh_entsize = 0;
8460 symstrtab_hdr->sh_link = 0;
8461 symstrtab_hdr->sh_info = 0;
8462 /* sh_offset is set just below. */
8463 symstrtab_hdr->sh_addralign = 1;
8465 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8466 elf_tdata (abfd)->next_file_pos = off;
8468 if (bfd_get_symcount (abfd) > 0)
8470 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8471 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8472 return FALSE;
8475 /* Adjust the relocs to have the correct symbol indices. */
8476 for (o = abfd->sections; o != NULL; o = o->next)
8478 if ((o->flags & SEC_RELOC) == 0)
8479 continue;
8481 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8482 elf_section_data (o)->rel_count,
8483 elf_section_data (o)->rel_hashes);
8484 if (elf_section_data (o)->rel_hdr2 != NULL)
8485 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8486 elf_section_data (o)->rel_count2,
8487 (elf_section_data (o)->rel_hashes
8488 + elf_section_data (o)->rel_count));
8490 /* Set the reloc_count field to 0 to prevent write_relocs from
8491 trying to swap the relocs out itself. */
8492 o->reloc_count = 0;
8495 if (dynamic && info->combreloc && dynobj != NULL)
8496 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8498 /* If we are linking against a dynamic object, or generating a
8499 shared library, finish up the dynamic linking information. */
8500 if (dynamic)
8502 bfd_byte *dyncon, *dynconend;
8504 /* Fix up .dynamic entries. */
8505 o = bfd_get_section_by_name (dynobj, ".dynamic");
8506 BFD_ASSERT (o != NULL);
8508 dyncon = o->contents;
8509 dynconend = o->contents + o->size;
8510 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8512 Elf_Internal_Dyn dyn;
8513 const char *name;
8514 unsigned int type;
8516 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8518 switch (dyn.d_tag)
8520 default:
8521 continue;
8522 case DT_NULL:
8523 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8525 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8527 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8528 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8529 default: continue;
8531 dyn.d_un.d_val = relativecount;
8532 relativecount = 0;
8533 break;
8535 continue;
8537 case DT_INIT:
8538 name = info->init_function;
8539 goto get_sym;
8540 case DT_FINI:
8541 name = info->fini_function;
8542 get_sym:
8544 struct elf_link_hash_entry *h;
8546 h = elf_link_hash_lookup (elf_hash_table (info), name,
8547 FALSE, FALSE, TRUE);
8548 if (h != NULL
8549 && (h->root.type == bfd_link_hash_defined
8550 || h->root.type == bfd_link_hash_defweak))
8552 dyn.d_un.d_val = h->root.u.def.value;
8553 o = h->root.u.def.section;
8554 if (o->output_section != NULL)
8555 dyn.d_un.d_val += (o->output_section->vma
8556 + o->output_offset);
8557 else
8559 /* The symbol is imported from another shared
8560 library and does not apply to this one. */
8561 dyn.d_un.d_val = 0;
8563 break;
8566 continue;
8568 case DT_PREINIT_ARRAYSZ:
8569 name = ".preinit_array";
8570 goto get_size;
8571 case DT_INIT_ARRAYSZ:
8572 name = ".init_array";
8573 goto get_size;
8574 case DT_FINI_ARRAYSZ:
8575 name = ".fini_array";
8576 get_size:
8577 o = bfd_get_section_by_name (abfd, name);
8578 if (o == NULL)
8580 (*_bfd_error_handler)
8581 (_("%B: could not find output section %s"), abfd, name);
8582 goto error_return;
8584 if (o->size == 0)
8585 (*_bfd_error_handler)
8586 (_("warning: %s section has zero size"), name);
8587 dyn.d_un.d_val = o->size;
8588 break;
8590 case DT_PREINIT_ARRAY:
8591 name = ".preinit_array";
8592 goto get_vma;
8593 case DT_INIT_ARRAY:
8594 name = ".init_array";
8595 goto get_vma;
8596 case DT_FINI_ARRAY:
8597 name = ".fini_array";
8598 goto get_vma;
8600 case DT_HASH:
8601 name = ".hash";
8602 goto get_vma;
8603 case DT_STRTAB:
8604 name = ".dynstr";
8605 goto get_vma;
8606 case DT_SYMTAB:
8607 name = ".dynsym";
8608 goto get_vma;
8609 case DT_VERDEF:
8610 name = ".gnu.version_d";
8611 goto get_vma;
8612 case DT_VERNEED:
8613 name = ".gnu.version_r";
8614 goto get_vma;
8615 case DT_VERSYM:
8616 name = ".gnu.version";
8617 get_vma:
8618 o = bfd_get_section_by_name (abfd, name);
8619 if (o == NULL)
8621 (*_bfd_error_handler)
8622 (_("%B: could not find output section %s"), abfd, name);
8623 goto error_return;
8625 dyn.d_un.d_ptr = o->vma;
8626 break;
8628 case DT_REL:
8629 case DT_RELA:
8630 case DT_RELSZ:
8631 case DT_RELASZ:
8632 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8633 type = SHT_REL;
8634 else
8635 type = SHT_RELA;
8636 dyn.d_un.d_val = 0;
8637 for (i = 1; i < elf_numsections (abfd); i++)
8639 Elf_Internal_Shdr *hdr;
8641 hdr = elf_elfsections (abfd)[i];
8642 if (hdr->sh_type == type
8643 && (hdr->sh_flags & SHF_ALLOC) != 0)
8645 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8646 dyn.d_un.d_val += hdr->sh_size;
8647 else
8649 if (dyn.d_un.d_val == 0
8650 || hdr->sh_addr < dyn.d_un.d_val)
8651 dyn.d_un.d_val = hdr->sh_addr;
8655 break;
8657 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8661 /* If we have created any dynamic sections, then output them. */
8662 if (dynobj != NULL)
8664 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8665 goto error_return;
8667 /* Check for DT_TEXTREL (late, in case the backend removes it). */
8668 if (info->warn_shared_textrel && info->shared)
8670 bfd_byte *dyncon, *dynconend;
8672 /* Fix up .dynamic entries. */
8673 o = bfd_get_section_by_name (dynobj, ".dynamic");
8674 BFD_ASSERT (o != NULL);
8676 dyncon = o->contents;
8677 dynconend = o->contents + o->size;
8678 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8680 Elf_Internal_Dyn dyn;
8682 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8684 if (dyn.d_tag == DT_TEXTREL)
8686 _bfd_error_handler
8687 (_("warning: creating a DT_TEXTREL in a shared object."));
8688 break;
8693 for (o = dynobj->sections; o != NULL; o = o->next)
8695 if ((o->flags & SEC_HAS_CONTENTS) == 0
8696 || o->size == 0
8697 || o->output_section == bfd_abs_section_ptr)
8698 continue;
8699 if ((o->flags & SEC_LINKER_CREATED) == 0)
8701 /* At this point, we are only interested in sections
8702 created by _bfd_elf_link_create_dynamic_sections. */
8703 continue;
8705 if (elf_hash_table (info)->stab_info.stabstr == o)
8706 continue;
8707 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8708 continue;
8709 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8710 != SHT_STRTAB)
8711 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8713 if (! bfd_set_section_contents (abfd, o->output_section,
8714 o->contents,
8715 (file_ptr) o->output_offset,
8716 o->size))
8717 goto error_return;
8719 else
8721 /* The contents of the .dynstr section are actually in a
8722 stringtab. */
8723 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8724 if (bfd_seek (abfd, off, SEEK_SET) != 0
8725 || ! _bfd_elf_strtab_emit (abfd,
8726 elf_hash_table (info)->dynstr))
8727 goto error_return;
8732 if (info->relocatable)
8734 bfd_boolean failed = FALSE;
8736 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8737 if (failed)
8738 goto error_return;
8741 /* If we have optimized stabs strings, output them. */
8742 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8744 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8745 goto error_return;
8748 if (info->eh_frame_hdr)
8750 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8751 goto error_return;
8754 if (finfo.symstrtab != NULL)
8755 _bfd_stringtab_free (finfo.symstrtab);
8756 if (finfo.contents != NULL)
8757 free (finfo.contents);
8758 if (finfo.external_relocs != NULL)
8759 free (finfo.external_relocs);
8760 if (finfo.internal_relocs != NULL)
8761 free (finfo.internal_relocs);
8762 if (finfo.external_syms != NULL)
8763 free (finfo.external_syms);
8764 if (finfo.locsym_shndx != NULL)
8765 free (finfo.locsym_shndx);
8766 if (finfo.internal_syms != NULL)
8767 free (finfo.internal_syms);
8768 if (finfo.indices != NULL)
8769 free (finfo.indices);
8770 if (finfo.sections != NULL)
8771 free (finfo.sections);
8772 if (finfo.symbuf != NULL)
8773 free (finfo.symbuf);
8774 if (finfo.symshndxbuf != NULL)
8775 free (finfo.symshndxbuf);
8776 for (o = abfd->sections; o != NULL; o = o->next)
8778 if ((o->flags & SEC_RELOC) != 0
8779 && elf_section_data (o)->rel_hashes != NULL)
8780 free (elf_section_data (o)->rel_hashes);
8783 elf_tdata (abfd)->linker = TRUE;
8785 return TRUE;
8787 error_return:
8788 if (finfo.symstrtab != NULL)
8789 _bfd_stringtab_free (finfo.symstrtab);
8790 if (finfo.contents != NULL)
8791 free (finfo.contents);
8792 if (finfo.external_relocs != NULL)
8793 free (finfo.external_relocs);
8794 if (finfo.internal_relocs != NULL)
8795 free (finfo.internal_relocs);
8796 if (finfo.external_syms != NULL)
8797 free (finfo.external_syms);
8798 if (finfo.locsym_shndx != NULL)
8799 free (finfo.locsym_shndx);
8800 if (finfo.internal_syms != NULL)
8801 free (finfo.internal_syms);
8802 if (finfo.indices != NULL)
8803 free (finfo.indices);
8804 if (finfo.sections != NULL)
8805 free (finfo.sections);
8806 if (finfo.symbuf != NULL)
8807 free (finfo.symbuf);
8808 if (finfo.symshndxbuf != NULL)
8809 free (finfo.symshndxbuf);
8810 for (o = abfd->sections; o != NULL; o = o->next)
8812 if ((o->flags & SEC_RELOC) != 0
8813 && elf_section_data (o)->rel_hashes != NULL)
8814 free (elf_section_data (o)->rel_hashes);
8817 return FALSE;
8820 /* Garbage collect unused sections. */
8822 /* The mark phase of garbage collection. For a given section, mark
8823 it and any sections in this section's group, and all the sections
8824 which define symbols to which it refers. */
8826 typedef asection * (*gc_mark_hook_fn)
8827 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8828 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8830 bfd_boolean
8831 _bfd_elf_gc_mark (struct bfd_link_info *info,
8832 asection *sec,
8833 gc_mark_hook_fn gc_mark_hook)
8835 bfd_boolean ret;
8836 bfd_boolean is_eh;
8837 asection *group_sec;
8839 sec->gc_mark = 1;
8841 /* Mark all the sections in the group. */
8842 group_sec = elf_section_data (sec)->next_in_group;
8843 if (group_sec && !group_sec->gc_mark)
8844 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8845 return FALSE;
8847 /* Look through the section relocs. */
8848 ret = TRUE;
8849 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8850 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8852 Elf_Internal_Rela *relstart, *rel, *relend;
8853 Elf_Internal_Shdr *symtab_hdr;
8854 struct elf_link_hash_entry **sym_hashes;
8855 size_t nlocsyms;
8856 size_t extsymoff;
8857 bfd *input_bfd = sec->owner;
8858 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8859 Elf_Internal_Sym *isym = NULL;
8860 int r_sym_shift;
8862 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8863 sym_hashes = elf_sym_hashes (input_bfd);
8865 /* Read the local symbols. */
8866 if (elf_bad_symtab (input_bfd))
8868 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8869 extsymoff = 0;
8871 else
8872 extsymoff = nlocsyms = symtab_hdr->sh_info;
8874 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8875 if (isym == NULL && nlocsyms != 0)
8877 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8878 NULL, NULL, NULL);
8879 if (isym == NULL)
8880 return FALSE;
8883 /* Read the relocations. */
8884 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8885 info->keep_memory);
8886 if (relstart == NULL)
8888 ret = FALSE;
8889 goto out1;
8891 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8893 if (bed->s->arch_size == 32)
8894 r_sym_shift = 8;
8895 else
8896 r_sym_shift = 32;
8898 for (rel = relstart; rel < relend; rel++)
8900 unsigned long r_symndx;
8901 asection *rsec;
8902 struct elf_link_hash_entry *h;
8904 r_symndx = rel->r_info >> r_sym_shift;
8905 if (r_symndx == 0)
8906 continue;
8908 if (r_symndx >= nlocsyms
8909 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8911 h = sym_hashes[r_symndx - extsymoff];
8912 while (h->root.type == bfd_link_hash_indirect
8913 || h->root.type == bfd_link_hash_warning)
8914 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8915 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8917 else
8919 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8922 if (rsec && !rsec->gc_mark)
8924 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8925 rsec->gc_mark = 1;
8926 else if (is_eh)
8927 rsec->gc_mark_from_eh = 1;
8928 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8930 ret = FALSE;
8931 goto out2;
8936 out2:
8937 if (elf_section_data (sec)->relocs != relstart)
8938 free (relstart);
8939 out1:
8940 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8942 if (! info->keep_memory)
8943 free (isym);
8944 else
8945 symtab_hdr->contents = (unsigned char *) isym;
8949 return ret;
8952 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8954 struct elf_gc_sweep_symbol_info {
8955 struct bfd_link_info *info;
8956 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8957 bfd_boolean);
8960 static bfd_boolean
8961 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8963 if (h->root.type == bfd_link_hash_warning)
8964 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8966 if ((h->root.type == bfd_link_hash_defined
8967 || h->root.type == bfd_link_hash_defweak)
8968 && !h->root.u.def.section->gc_mark
8969 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8971 struct elf_gc_sweep_symbol_info *inf = data;
8972 (*inf->hide_symbol) (inf->info, h, TRUE);
8975 return TRUE;
8978 /* The sweep phase of garbage collection. Remove all garbage sections. */
8980 typedef bfd_boolean (*gc_sweep_hook_fn)
8981 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8983 static bfd_boolean
8984 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8986 bfd *sub;
8987 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8988 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8989 unsigned long section_sym_count;
8990 struct elf_gc_sweep_symbol_info sweep_info;
8992 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8994 asection *o;
8996 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8997 continue;
8999 for (o = sub->sections; o != NULL; o = o->next)
9001 /* Keep debug and special sections. */
9002 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
9003 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
9004 o->gc_mark = 1;
9006 if (o->gc_mark)
9007 continue;
9009 /* Skip sweeping sections already excluded. */
9010 if (o->flags & SEC_EXCLUDE)
9011 continue;
9013 /* Since this is early in the link process, it is simple
9014 to remove a section from the output. */
9015 o->flags |= SEC_EXCLUDE;
9017 /* But we also have to update some of the relocation
9018 info we collected before. */
9019 if (gc_sweep_hook
9020 && (o->flags & SEC_RELOC) != 0
9021 && o->reloc_count > 0
9022 && !bfd_is_abs_section (o->output_section))
9024 Elf_Internal_Rela *internal_relocs;
9025 bfd_boolean r;
9027 internal_relocs
9028 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
9029 info->keep_memory);
9030 if (internal_relocs == NULL)
9031 return FALSE;
9033 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
9035 if (elf_section_data (o)->relocs != internal_relocs)
9036 free (internal_relocs);
9038 if (!r)
9039 return FALSE;
9044 /* Remove the symbols that were in the swept sections from the dynamic
9045 symbol table. GCFIXME: Anyone know how to get them out of the
9046 static symbol table as well? */
9047 sweep_info.info = info;
9048 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
9049 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
9050 &sweep_info);
9052 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
9053 return TRUE;
9056 /* Propagate collected vtable information. This is called through
9057 elf_link_hash_traverse. */
9059 static bfd_boolean
9060 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
9062 if (h->root.type == bfd_link_hash_warning)
9063 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9065 /* Those that are not vtables. */
9066 if (h->vtable == NULL || h->vtable->parent == NULL)
9067 return TRUE;
9069 /* Those vtables that do not have parents, we cannot merge. */
9070 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
9071 return TRUE;
9073 /* If we've already been done, exit. */
9074 if (h->vtable->used && h->vtable->used[-1])
9075 return TRUE;
9077 /* Make sure the parent's table is up to date. */
9078 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9080 if (h->vtable->used == NULL)
9082 /* None of this table's entries were referenced. Re-use the
9083 parent's table. */
9084 h->vtable->used = h->vtable->parent->vtable->used;
9085 h->vtable->size = h->vtable->parent->vtable->size;
9087 else
9089 size_t n;
9090 bfd_boolean *cu, *pu;
9092 /* Or the parent's entries into ours. */
9093 cu = h->vtable->used;
9094 cu[-1] = TRUE;
9095 pu = h->vtable->parent->vtable->used;
9096 if (pu != NULL)
9098 const struct elf_backend_data *bed;
9099 unsigned int log_file_align;
9101 bed = get_elf_backend_data (h->root.u.def.section->owner);
9102 log_file_align = bed->s->log_file_align;
9103 n = h->vtable->parent->vtable->size >> log_file_align;
9104 while (n--)
9106 if (*pu)
9107 *cu = TRUE;
9108 pu++;
9109 cu++;
9114 return TRUE;
9117 static bfd_boolean
9118 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9120 asection *sec;
9121 bfd_vma hstart, hend;
9122 Elf_Internal_Rela *relstart, *relend, *rel;
9123 const struct elf_backend_data *bed;
9124 unsigned int log_file_align;
9126 if (h->root.type == bfd_link_hash_warning)
9127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9129 /* Take care of both those symbols that do not describe vtables as
9130 well as those that are not loaded. */
9131 if (h->vtable == NULL || h->vtable->parent == NULL)
9132 return TRUE;
9134 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9135 || h->root.type == bfd_link_hash_defweak);
9137 sec = h->root.u.def.section;
9138 hstart = h->root.u.def.value;
9139 hend = hstart + h->size;
9141 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9142 if (!relstart)
9143 return *(bfd_boolean *) okp = FALSE;
9144 bed = get_elf_backend_data (sec->owner);
9145 log_file_align = bed->s->log_file_align;
9147 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9149 for (rel = relstart; rel < relend; ++rel)
9150 if (rel->r_offset >= hstart && rel->r_offset < hend)
9152 /* If the entry is in use, do nothing. */
9153 if (h->vtable->used
9154 && (rel->r_offset - hstart) < h->vtable->size)
9156 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9157 if (h->vtable->used[entry])
9158 continue;
9160 /* Otherwise, kill it. */
9161 rel->r_offset = rel->r_info = rel->r_addend = 0;
9164 return TRUE;
9167 /* Mark sections containing dynamically referenced symbols. When
9168 building shared libraries, we must assume that any visible symbol is
9169 referenced. */
9171 bfd_boolean
9172 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9174 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9176 if (h->root.type == bfd_link_hash_warning)
9177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9179 if ((h->root.type == bfd_link_hash_defined
9180 || h->root.type == bfd_link_hash_defweak)
9181 && (h->ref_dynamic
9182 || (!info->executable
9183 && h->def_regular
9184 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9185 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9186 h->root.u.def.section->flags |= SEC_KEEP;
9188 return TRUE;
9191 /* Do mark and sweep of unused sections. */
9193 bfd_boolean
9194 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9196 bfd_boolean ok = TRUE;
9197 bfd *sub;
9198 asection * (*gc_mark_hook)
9199 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9200 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9203 if (!bed->can_gc_sections
9204 || info->relocatable
9205 || info->emitrelocations
9206 || !is_elf_hash_table (info->hash))
9208 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9209 return TRUE;
9212 /* Apply transitive closure to the vtable entry usage info. */
9213 elf_link_hash_traverse (elf_hash_table (info),
9214 elf_gc_propagate_vtable_entries_used,
9215 &ok);
9216 if (!ok)
9217 return FALSE;
9219 /* Kill the vtable relocations that were not used. */
9220 elf_link_hash_traverse (elf_hash_table (info),
9221 elf_gc_smash_unused_vtentry_relocs,
9222 &ok);
9223 if (!ok)
9224 return FALSE;
9226 /* Mark dynamically referenced symbols. */
9227 if (elf_hash_table (info)->dynamic_sections_created)
9228 elf_link_hash_traverse (elf_hash_table (info),
9229 bed->gc_mark_dynamic_ref,
9230 info);
9232 /* Grovel through relocs to find out who stays ... */
9233 gc_mark_hook = bed->gc_mark_hook;
9234 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9236 asection *o;
9238 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9239 continue;
9241 for (o = sub->sections; o != NULL; o = o->next)
9242 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9243 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9244 return FALSE;
9247 /* ... again for sections marked from eh_frame. */
9248 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9250 asection *o;
9252 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9253 continue;
9255 /* Keep .gcc_except_table.* if the associated .text.* is
9256 marked. This isn't very nice, but the proper solution,
9257 splitting .eh_frame up and using comdat doesn't pan out
9258 easily due to needing special relocs to handle the
9259 difference of two symbols in separate sections.
9260 Don't keep code sections referenced by .eh_frame. */
9261 for (o = sub->sections; o != NULL; o = o->next)
9262 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9264 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9266 unsigned long len;
9267 char *fn_name;
9268 asection *fn_text;
9270 len = strlen (o->name + 18) + 1;
9271 fn_name = bfd_malloc (len + 6);
9272 if (fn_name == NULL)
9273 return FALSE;
9274 memcpy (fn_name, ".text.", 6);
9275 memcpy (fn_name + 6, o->name + 18, len);
9276 fn_text = bfd_get_section_by_name (sub, fn_name);
9277 free (fn_name);
9278 if (fn_text == NULL || !fn_text->gc_mark)
9279 continue;
9282 /* If not using specially named exception table section,
9283 then keep whatever we are using. */
9284 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9285 return FALSE;
9289 /* ... and mark SEC_EXCLUDE for those that go. */
9290 return elf_gc_sweep (abfd, info);
9293 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9295 bfd_boolean
9296 bfd_elf_gc_record_vtinherit (bfd *abfd,
9297 asection *sec,
9298 struct elf_link_hash_entry *h,
9299 bfd_vma offset)
9301 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9302 struct elf_link_hash_entry **search, *child;
9303 bfd_size_type extsymcount;
9304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9306 /* The sh_info field of the symtab header tells us where the
9307 external symbols start. We don't care about the local symbols at
9308 this point. */
9309 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9310 if (!elf_bad_symtab (abfd))
9311 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9313 sym_hashes = elf_sym_hashes (abfd);
9314 sym_hashes_end = sym_hashes + extsymcount;
9316 /* Hunt down the child symbol, which is in this section at the same
9317 offset as the relocation. */
9318 for (search = sym_hashes; search != sym_hashes_end; ++search)
9320 if ((child = *search) != NULL
9321 && (child->root.type == bfd_link_hash_defined
9322 || child->root.type == bfd_link_hash_defweak)
9323 && child->root.u.def.section == sec
9324 && child->root.u.def.value == offset)
9325 goto win;
9328 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9329 abfd, sec, (unsigned long) offset);
9330 bfd_set_error (bfd_error_invalid_operation);
9331 return FALSE;
9333 win:
9334 if (!child->vtable)
9336 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9337 if (!child->vtable)
9338 return FALSE;
9340 if (!h)
9342 /* This *should* only be the absolute section. It could potentially
9343 be that someone has defined a non-global vtable though, which
9344 would be bad. It isn't worth paging in the local symbols to be
9345 sure though; that case should simply be handled by the assembler. */
9347 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9349 else
9350 child->vtable->parent = h;
9352 return TRUE;
9355 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9357 bfd_boolean
9358 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9359 asection *sec ATTRIBUTE_UNUSED,
9360 struct elf_link_hash_entry *h,
9361 bfd_vma addend)
9363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9364 unsigned int log_file_align = bed->s->log_file_align;
9366 if (!h->vtable)
9368 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9369 if (!h->vtable)
9370 return FALSE;
9373 if (addend >= h->vtable->size)
9375 size_t size, bytes, file_align;
9376 bfd_boolean *ptr = h->vtable->used;
9378 /* While the symbol is undefined, we have to be prepared to handle
9379 a zero size. */
9380 file_align = 1 << log_file_align;
9381 if (h->root.type == bfd_link_hash_undefined)
9382 size = addend + file_align;
9383 else
9385 size = h->size;
9386 if (addend >= size)
9388 /* Oops! We've got a reference past the defined end of
9389 the table. This is probably a bug -- shall we warn? */
9390 size = addend + file_align;
9393 size = (size + file_align - 1) & -file_align;
9395 /* Allocate one extra entry for use as a "done" flag for the
9396 consolidation pass. */
9397 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9399 if (ptr)
9401 ptr = bfd_realloc (ptr - 1, bytes);
9403 if (ptr != NULL)
9405 size_t oldbytes;
9407 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9408 * sizeof (bfd_boolean));
9409 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9412 else
9413 ptr = bfd_zmalloc (bytes);
9415 if (ptr == NULL)
9416 return FALSE;
9418 /* And arrange for that done flag to be at index -1. */
9419 h->vtable->used = ptr + 1;
9420 h->vtable->size = size;
9423 h->vtable->used[addend >> log_file_align] = TRUE;
9425 return TRUE;
9428 struct alloc_got_off_arg {
9429 bfd_vma gotoff;
9430 unsigned int got_elt_size;
9433 /* We need a special top-level link routine to convert got reference counts
9434 to real got offsets. */
9436 static bfd_boolean
9437 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9439 struct alloc_got_off_arg *gofarg = arg;
9441 if (h->root.type == bfd_link_hash_warning)
9442 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9444 if (h->got.refcount > 0)
9446 h->got.offset = gofarg->gotoff;
9447 gofarg->gotoff += gofarg->got_elt_size;
9449 else
9450 h->got.offset = (bfd_vma) -1;
9452 return TRUE;
9455 /* And an accompanying bit to work out final got entry offsets once
9456 we're done. Should be called from final_link. */
9458 bfd_boolean
9459 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9460 struct bfd_link_info *info)
9462 bfd *i;
9463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9464 bfd_vma gotoff;
9465 unsigned int got_elt_size = bed->s->arch_size / 8;
9466 struct alloc_got_off_arg gofarg;
9468 if (! is_elf_hash_table (info->hash))
9469 return FALSE;
9471 /* The GOT offset is relative to the .got section, but the GOT header is
9472 put into the .got.plt section, if the backend uses it. */
9473 if (bed->want_got_plt)
9474 gotoff = 0;
9475 else
9476 gotoff = bed->got_header_size;
9478 /* Do the local .got entries first. */
9479 for (i = info->input_bfds; i; i = i->link_next)
9481 bfd_signed_vma *local_got;
9482 bfd_size_type j, locsymcount;
9483 Elf_Internal_Shdr *symtab_hdr;
9485 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9486 continue;
9488 local_got = elf_local_got_refcounts (i);
9489 if (!local_got)
9490 continue;
9492 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9493 if (elf_bad_symtab (i))
9494 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9495 else
9496 locsymcount = symtab_hdr->sh_info;
9498 for (j = 0; j < locsymcount; ++j)
9500 if (local_got[j] > 0)
9502 local_got[j] = gotoff;
9503 gotoff += got_elt_size;
9505 else
9506 local_got[j] = (bfd_vma) -1;
9510 /* Then the global .got entries. .plt refcounts are handled by
9511 adjust_dynamic_symbol */
9512 gofarg.gotoff = gotoff;
9513 gofarg.got_elt_size = got_elt_size;
9514 elf_link_hash_traverse (elf_hash_table (info),
9515 elf_gc_allocate_got_offsets,
9516 &gofarg);
9517 return TRUE;
9520 /* Many folk need no more in the way of final link than this, once
9521 got entry reference counting is enabled. */
9523 bfd_boolean
9524 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9526 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9527 return FALSE;
9529 /* Invoke the regular ELF backend linker to do all the work. */
9530 return bfd_elf_final_link (abfd, info);
9533 bfd_boolean
9534 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9536 struct elf_reloc_cookie *rcookie = cookie;
9538 if (rcookie->bad_symtab)
9539 rcookie->rel = rcookie->rels;
9541 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9543 unsigned long r_symndx;
9545 if (! rcookie->bad_symtab)
9546 if (rcookie->rel->r_offset > offset)
9547 return FALSE;
9548 if (rcookie->rel->r_offset != offset)
9549 continue;
9551 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9552 if (r_symndx == SHN_UNDEF)
9553 return TRUE;
9555 if (r_symndx >= rcookie->locsymcount
9556 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9558 struct elf_link_hash_entry *h;
9560 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9562 while (h->root.type == bfd_link_hash_indirect
9563 || h->root.type == bfd_link_hash_warning)
9564 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9566 if ((h->root.type == bfd_link_hash_defined
9567 || h->root.type == bfd_link_hash_defweak)
9568 && elf_discarded_section (h->root.u.def.section))
9569 return TRUE;
9570 else
9571 return FALSE;
9573 else
9575 /* It's not a relocation against a global symbol,
9576 but it could be a relocation against a local
9577 symbol for a discarded section. */
9578 asection *isec;
9579 Elf_Internal_Sym *isym;
9581 /* Need to: get the symbol; get the section. */
9582 isym = &rcookie->locsyms[r_symndx];
9583 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9585 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9586 if (isec != NULL && elf_discarded_section (isec))
9587 return TRUE;
9590 return FALSE;
9592 return FALSE;
9595 /* Discard unneeded references to discarded sections.
9596 Returns TRUE if any section's size was changed. */
9597 /* This function assumes that the relocations are in sorted order,
9598 which is true for all known assemblers. */
9600 bfd_boolean
9601 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9603 struct elf_reloc_cookie cookie;
9604 asection *stab, *eh;
9605 Elf_Internal_Shdr *symtab_hdr;
9606 const struct elf_backend_data *bed;
9607 bfd *abfd;
9608 unsigned int count;
9609 bfd_boolean ret = FALSE;
9611 if (info->traditional_format
9612 || !is_elf_hash_table (info->hash))
9613 return FALSE;
9615 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9617 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9618 continue;
9620 bed = get_elf_backend_data (abfd);
9622 if ((abfd->flags & DYNAMIC) != 0)
9623 continue;
9625 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9626 if (info->relocatable
9627 || (eh != NULL
9628 && (eh->size == 0
9629 || bfd_is_abs_section (eh->output_section))))
9630 eh = NULL;
9632 stab = bfd_get_section_by_name (abfd, ".stab");
9633 if (stab != NULL
9634 && (stab->size == 0
9635 || bfd_is_abs_section (stab->output_section)
9636 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9637 stab = NULL;
9639 if (stab == NULL
9640 && eh == NULL
9641 && bed->elf_backend_discard_info == NULL)
9642 continue;
9644 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9645 cookie.abfd = abfd;
9646 cookie.sym_hashes = elf_sym_hashes (abfd);
9647 cookie.bad_symtab = elf_bad_symtab (abfd);
9648 if (cookie.bad_symtab)
9650 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9651 cookie.extsymoff = 0;
9653 else
9655 cookie.locsymcount = symtab_hdr->sh_info;
9656 cookie.extsymoff = symtab_hdr->sh_info;
9659 if (bed->s->arch_size == 32)
9660 cookie.r_sym_shift = 8;
9661 else
9662 cookie.r_sym_shift = 32;
9664 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9665 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9667 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9668 cookie.locsymcount, 0,
9669 NULL, NULL, NULL);
9670 if (cookie.locsyms == NULL)
9671 return FALSE;
9674 if (stab != NULL)
9676 cookie.rels = NULL;
9677 count = stab->reloc_count;
9678 if (count != 0)
9679 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9680 info->keep_memory);
9681 if (cookie.rels != NULL)
9683 cookie.rel = cookie.rels;
9684 cookie.relend = cookie.rels;
9685 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9686 if (_bfd_discard_section_stabs (abfd, stab,
9687 elf_section_data (stab)->sec_info,
9688 bfd_elf_reloc_symbol_deleted_p,
9689 &cookie))
9690 ret = TRUE;
9691 if (elf_section_data (stab)->relocs != cookie.rels)
9692 free (cookie.rels);
9696 if (eh != NULL)
9698 cookie.rels = NULL;
9699 count = eh->reloc_count;
9700 if (count != 0)
9701 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9702 info->keep_memory);
9703 cookie.rel = cookie.rels;
9704 cookie.relend = cookie.rels;
9705 if (cookie.rels != NULL)
9706 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9708 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9709 bfd_elf_reloc_symbol_deleted_p,
9710 &cookie))
9711 ret = TRUE;
9713 if (cookie.rels != NULL
9714 && elf_section_data (eh)->relocs != cookie.rels)
9715 free (cookie.rels);
9718 if (bed->elf_backend_discard_info != NULL
9719 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9720 ret = TRUE;
9722 if (cookie.locsyms != NULL
9723 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9725 if (! info->keep_memory)
9726 free (cookie.locsyms);
9727 else
9728 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9732 if (info->eh_frame_hdr
9733 && !info->relocatable
9734 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9735 ret = TRUE;
9737 return ret;
9740 void
9741 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9743 flagword flags;
9744 const char *name, *p;
9745 struct bfd_section_already_linked *l;
9746 struct bfd_section_already_linked_hash_entry *already_linked_list;
9747 asection *group;
9749 /* A single member comdat group section may be discarded by a
9750 linkonce section. See below. */
9751 if (sec->output_section == bfd_abs_section_ptr)
9752 return;
9754 flags = sec->flags;
9756 /* Check if it belongs to a section group. */
9757 group = elf_sec_group (sec);
9759 /* Return if it isn't a linkonce section nor a member of a group. A
9760 comdat group section also has SEC_LINK_ONCE set. */
9761 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9762 return;
9764 if (group)
9766 /* If this is the member of a single member comdat group, check if
9767 the group should be discarded. */
9768 if (elf_next_in_group (sec) == sec
9769 && (group->flags & SEC_LINK_ONCE) != 0)
9770 sec = group;
9771 else
9772 return;
9775 /* FIXME: When doing a relocatable link, we may have trouble
9776 copying relocations in other sections that refer to local symbols
9777 in the section being discarded. Those relocations will have to
9778 be converted somehow; as of this writing I'm not sure that any of
9779 the backends handle that correctly.
9781 It is tempting to instead not discard link once sections when
9782 doing a relocatable link (technically, they should be discarded
9783 whenever we are building constructors). However, that fails,
9784 because the linker winds up combining all the link once sections
9785 into a single large link once section, which defeats the purpose
9786 of having link once sections in the first place.
9788 Also, not merging link once sections in a relocatable link
9789 causes trouble for MIPS ELF, which relies on link once semantics
9790 to handle the .reginfo section correctly. */
9792 name = bfd_get_section_name (abfd, sec);
9794 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9795 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9796 p++;
9797 else
9798 p = name;
9800 already_linked_list = bfd_section_already_linked_table_lookup (p);
9802 for (l = already_linked_list->entry; l != NULL; l = l->next)
9804 /* We may have 3 different sections on the list: group section,
9805 comdat section and linkonce section. SEC may be a linkonce or
9806 group section. We match a group section with a group section,
9807 a linkonce section with a linkonce section, and ignore comdat
9808 section. */
9809 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9810 && strcmp (name, l->sec->name) == 0
9811 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9813 /* The section has already been linked. See if we should
9814 issue a warning. */
9815 switch (flags & SEC_LINK_DUPLICATES)
9817 default:
9818 abort ();
9820 case SEC_LINK_DUPLICATES_DISCARD:
9821 break;
9823 case SEC_LINK_DUPLICATES_ONE_ONLY:
9824 (*_bfd_error_handler)
9825 (_("%B: ignoring duplicate section `%A'"),
9826 abfd, sec);
9827 break;
9829 case SEC_LINK_DUPLICATES_SAME_SIZE:
9830 if (sec->size != l->sec->size)
9831 (*_bfd_error_handler)
9832 (_("%B: duplicate section `%A' has different size"),
9833 abfd, sec);
9834 break;
9836 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9837 if (sec->size != l->sec->size)
9838 (*_bfd_error_handler)
9839 (_("%B: duplicate section `%A' has different size"),
9840 abfd, sec);
9841 else if (sec->size != 0)
9843 bfd_byte *sec_contents, *l_sec_contents;
9845 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9846 (*_bfd_error_handler)
9847 (_("%B: warning: could not read contents of section `%A'"),
9848 abfd, sec);
9849 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9850 &l_sec_contents))
9851 (*_bfd_error_handler)
9852 (_("%B: warning: could not read contents of section `%A'"),
9853 l->sec->owner, l->sec);
9854 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9855 (*_bfd_error_handler)
9856 (_("%B: warning: duplicate section `%A' has different contents"),
9857 abfd, sec);
9859 if (sec_contents)
9860 free (sec_contents);
9861 if (l_sec_contents)
9862 free (l_sec_contents);
9864 break;
9867 /* Set the output_section field so that lang_add_section
9868 does not create a lang_input_section structure for this
9869 section. Since there might be a symbol in the section
9870 being discarded, we must retain a pointer to the section
9871 which we are really going to use. */
9872 sec->output_section = bfd_abs_section_ptr;
9873 sec->kept_section = l->sec;
9875 if (flags & SEC_GROUP)
9877 asection *first = elf_next_in_group (sec);
9878 asection *s = first;
9880 while (s != NULL)
9882 s->output_section = bfd_abs_section_ptr;
9883 /* Record which group discards it. */
9884 s->kept_section = l->sec;
9885 s = elf_next_in_group (s);
9886 /* These lists are circular. */
9887 if (s == first)
9888 break;
9892 return;
9896 if (group)
9898 /* If this is the member of a single member comdat group and the
9899 group hasn't be discarded, we check if it matches a linkonce
9900 section. We only record the discarded comdat group. Otherwise
9901 the undiscarded group will be discarded incorrectly later since
9902 itself has been recorded. */
9903 for (l = already_linked_list->entry; l != NULL; l = l->next)
9904 if ((l->sec->flags & SEC_GROUP) == 0
9905 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9906 && bfd_elf_match_symbols_in_sections (l->sec,
9907 elf_next_in_group (sec)))
9909 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9910 elf_next_in_group (sec)->kept_section = l->sec;
9911 group->output_section = bfd_abs_section_ptr;
9912 break;
9914 if (l == NULL)
9915 return;
9917 else
9918 /* There is no direct match. But for linkonce section, we should
9919 check if there is a match with comdat group member. We always
9920 record the linkonce section, discarded or not. */
9921 for (l = already_linked_list->entry; l != NULL; l = l->next)
9922 if (l->sec->flags & SEC_GROUP)
9924 asection *first = elf_next_in_group (l->sec);
9926 if (first != NULL
9927 && elf_next_in_group (first) == first
9928 && bfd_elf_match_symbols_in_sections (first, sec))
9930 sec->output_section = bfd_abs_section_ptr;
9931 sec->kept_section = l->sec;
9932 break;
9936 /* This is the first section with this name. Record it. */
9937 bfd_section_already_linked_table_insert (already_linked_list, sec);
9940 bfd_boolean
9941 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9943 return sym->st_shndx == SHN_COMMON;
9946 unsigned int
9947 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9949 return SHN_COMMON;
9952 asection *
9953 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9955 return bfd_com_section_ptr;