daily update
[binutils.git] / bfd / elflink.c
bloba95ac44c0e9fb404aca658c618fedad5f81d97dc
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"
30 /* Define a symbol in a dynamic linkage section. */
32 struct elf_link_hash_entry *
33 _bfd_elf_define_linkage_sym (bfd *abfd,
34 struct bfd_link_info *info,
35 asection *sec,
36 const char *name)
38 struct elf_link_hash_entry *h;
39 struct bfd_link_hash_entry *bh;
40 const struct elf_backend_data *bed;
42 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
43 if (h != NULL)
45 /* Zap symbol defined in an as-needed lib that wasn't linked.
46 This is a symptom of a larger problem: Absolute symbols
47 defined in shared libraries can't be overridden, because we
48 lose the link to the bfd which is via the symbol section. */
49 h->root.type = bfd_link_hash_new;
52 bh = &h->root;
53 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
54 sec, 0, NULL, FALSE,
55 get_elf_backend_data (abfd)->collect,
56 &bh))
57 return NULL;
58 h = (struct elf_link_hash_entry *) bh;
59 h->def_regular = 1;
60 h->type = STT_OBJECT;
61 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
63 bed = get_elf_backend_data (abfd);
64 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
65 return h;
68 bfd_boolean
69 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
71 flagword flags;
72 asection *s;
73 struct elf_link_hash_entry *h;
74 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
75 int ptralign;
77 /* This function may be called more than once. */
78 s = bfd_get_section_by_name (abfd, ".got");
79 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
80 return TRUE;
82 switch (bed->s->arch_size)
84 case 32:
85 ptralign = 2;
86 break;
88 case 64:
89 ptralign = 3;
90 break;
92 default:
93 bfd_set_error (bfd_error_bad_value);
94 return FALSE;
97 flags = bed->dynamic_sec_flags;
99 s = bfd_make_section_with_flags (abfd, ".got", flags);
100 if (s == NULL
101 || !bfd_set_section_alignment (abfd, s, ptralign))
102 return FALSE;
104 if (bed->want_got_plt)
106 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
107 if (s == NULL
108 || !bfd_set_section_alignment (abfd, s, ptralign))
109 return FALSE;
112 if (bed->want_got_sym)
114 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
115 (or .got.plt) section. We don't do this in the linker script
116 because we don't want to define the symbol if we are not creating
117 a global offset table. */
118 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
119 elf_hash_table (info)->hgot = h;
120 if (h == NULL)
121 return FALSE;
124 /* The first bit of the global offset table is the header. */
125 s->size += bed->got_header_size;
127 return TRUE;
130 /* Create a strtab to hold the dynamic symbol names. */
131 static bfd_boolean
132 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
134 struct elf_link_hash_table *hash_table;
136 hash_table = elf_hash_table (info);
137 if (hash_table->dynobj == NULL)
138 hash_table->dynobj = abfd;
140 if (hash_table->dynstr == NULL)
142 hash_table->dynstr = _bfd_elf_strtab_init ();
143 if (hash_table->dynstr == NULL)
144 return FALSE;
146 return TRUE;
149 /* Create some sections which will be filled in with dynamic linking
150 information. ABFD is an input file which requires dynamic sections
151 to be created. The dynamic sections take up virtual memory space
152 when the final executable is run, so we need to create them before
153 addresses are assigned to the output sections. We work out the
154 actual contents and size of these sections later. */
156 bfd_boolean
157 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
159 flagword flags;
160 register asection *s;
161 const struct elf_backend_data *bed;
163 if (! is_elf_hash_table (info->hash))
164 return FALSE;
166 if (elf_hash_table (info)->dynamic_sections_created)
167 return TRUE;
169 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
170 return FALSE;
172 abfd = elf_hash_table (info)->dynobj;
173 bed = get_elf_backend_data (abfd);
175 flags = bed->dynamic_sec_flags;
177 /* A dynamically linked executable has a .interp section, but a
178 shared library does not. */
179 if (info->executable)
181 s = bfd_make_section_with_flags (abfd, ".interp",
182 flags | SEC_READONLY);
183 if (s == NULL)
184 return FALSE;
187 if (! info->traditional_format)
189 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
190 flags | SEC_READONLY);
191 if (s == NULL
192 || ! bfd_set_section_alignment (abfd, s, 2))
193 return FALSE;
194 elf_hash_table (info)->eh_info.hdr_sec = s;
197 /* Create sections to hold version informations. These are removed
198 if they are not needed. */
199 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
200 flags | SEC_READONLY);
201 if (s == NULL
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 return FALSE;
205 s = bfd_make_section_with_flags (abfd, ".gnu.version",
206 flags | SEC_READONLY);
207 if (s == NULL
208 || ! bfd_set_section_alignment (abfd, s, 1))
209 return FALSE;
211 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
212 flags | SEC_READONLY);
213 if (s == NULL
214 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 return FALSE;
217 s = bfd_make_section_with_flags (abfd, ".dynsym",
218 flags | SEC_READONLY);
219 if (s == NULL
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
223 s = bfd_make_section_with_flags (abfd, ".dynstr",
224 flags | SEC_READONLY);
225 if (s == NULL)
226 return FALSE;
228 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
229 if (s == NULL
230 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
231 return FALSE;
233 /* The special symbol _DYNAMIC is always set to the start of the
234 .dynamic section. We could set _DYNAMIC in a linker script, but we
235 only want to define it if we are, in fact, creating a .dynamic
236 section. We don't want to define it if there is no .dynamic
237 section, since on some ELF platforms the start up code examines it
238 to decide how to initialize the process. */
239 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
240 return FALSE;
242 s = bfd_make_section_with_flags (abfd, ".hash",
243 flags | SEC_READONLY);
244 if (s == NULL
245 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
246 return FALSE;
247 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
249 /* Let the backend create the rest of the sections. This lets the
250 backend set the right flags. The backend will normally create
251 the .got and .plt sections. */
252 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
253 return FALSE;
255 elf_hash_table (info)->dynamic_sections_created = TRUE;
257 return TRUE;
260 /* Create dynamic sections when linking against a dynamic object. */
262 bfd_boolean
263 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
265 flagword flags, pltflags;
266 struct elf_link_hash_entry *h;
267 asection *s;
268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
270 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
271 .rel[a].bss sections. */
272 flags = bed->dynamic_sec_flags;
274 pltflags = flags;
275 if (bed->plt_not_loaded)
276 /* We do not clear SEC_ALLOC here because we still want the OS to
277 allocate space for the section; it's just that there's nothing
278 to read in from the object file. */
279 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
280 else
281 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
282 if (bed->plt_readonly)
283 pltflags |= SEC_READONLY;
285 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
286 if (s == NULL
287 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
288 return FALSE;
290 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
291 .plt section. */
292 if (bed->want_plt_sym)
294 h = _bfd_elf_define_linkage_sym (abfd, info, s,
295 "_PROCEDURE_LINKAGE_TABLE_");
296 elf_hash_table (info)->hplt = h;
297 if (h == NULL)
298 return FALSE;
301 s = bfd_make_section_with_flags (abfd,
302 (bed->default_use_rela_p
303 ? ".rela.plt" : ".rel.plt"),
304 flags | SEC_READONLY);
305 if (s == NULL
306 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
307 return FALSE;
309 if (! _bfd_elf_create_got_section (abfd, info))
310 return FALSE;
312 if (bed->want_dynbss)
314 /* The .dynbss section is a place to put symbols which are defined
315 by dynamic objects, are referenced by regular objects, and are
316 not functions. We must allocate space for them in the process
317 image and use a R_*_COPY reloc to tell the dynamic linker to
318 initialize them at run time. The linker script puts the .dynbss
319 section into the .bss section of the final image. */
320 s = bfd_make_section_with_flags (abfd, ".dynbss",
321 (SEC_ALLOC
322 | SEC_LINKER_CREATED));
323 if (s == NULL)
324 return FALSE;
326 /* The .rel[a].bss section holds copy relocs. This section is not
327 normally needed. We need to create it here, though, so that the
328 linker will map it to an output section. We can't just create it
329 only if we need it, because we will not know whether we need it
330 until we have seen all the input files, and the first time the
331 main linker code calls BFD after examining all the input files
332 (size_dynamic_sections) the input sections have already been
333 mapped to the output sections. If the section turns out not to
334 be needed, we can discard it later. We will never need this
335 section when generating a shared object, since they do not use
336 copy relocs. */
337 if (! info->shared)
339 s = bfd_make_section_with_flags (abfd,
340 (bed->default_use_rela_p
341 ? ".rela.bss" : ".rel.bss"),
342 flags | SEC_READONLY);
343 if (s == NULL
344 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
345 return FALSE;
349 return TRUE;
352 /* Record a new dynamic symbol. We record the dynamic symbols as we
353 read the input files, since we need to have a list of all of them
354 before we can determine the final sizes of the output sections.
355 Note that we may actually call this function even though we are not
356 going to output any dynamic symbols; in some cases we know that a
357 symbol should be in the dynamic symbol table, but only if there is
358 one. */
360 bfd_boolean
361 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
362 struct elf_link_hash_entry *h)
364 if (h->dynindx == -1)
366 struct elf_strtab_hash *dynstr;
367 char *p;
368 const char *name;
369 bfd_size_type indx;
371 /* XXX: The ABI draft says the linker must turn hidden and
372 internal symbols into STB_LOCAL symbols when producing the
373 DSO. However, if ld.so honors st_other in the dynamic table,
374 this would not be necessary. */
375 switch (ELF_ST_VISIBILITY (h->other))
377 case STV_INTERNAL:
378 case STV_HIDDEN:
379 if (h->root.type != bfd_link_hash_undefined
380 && h->root.type != bfd_link_hash_undefweak)
382 h->forced_local = 1;
383 if (!elf_hash_table (info)->is_relocatable_executable)
384 return TRUE;
387 default:
388 break;
391 h->dynindx = elf_hash_table (info)->dynsymcount;
392 ++elf_hash_table (info)->dynsymcount;
394 dynstr = elf_hash_table (info)->dynstr;
395 if (dynstr == NULL)
397 /* Create a strtab to hold the dynamic symbol names. */
398 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
399 if (dynstr == NULL)
400 return FALSE;
403 /* We don't put any version information in the dynamic string
404 table. */
405 name = h->root.root.string;
406 p = strchr (name, ELF_VER_CHR);
407 if (p != NULL)
408 /* We know that the p points into writable memory. In fact,
409 there are only a few symbols that have read-only names, being
410 those like _GLOBAL_OFFSET_TABLE_ that are created specially
411 by the backends. Most symbols will have names pointing into
412 an ELF string table read from a file, or to objalloc memory. */
413 *p = 0;
415 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
417 if (p != NULL)
418 *p = ELF_VER_CHR;
420 if (indx == (bfd_size_type) -1)
421 return FALSE;
422 h->dynstr_index = indx;
425 return TRUE;
428 /* Record an assignment to a symbol made by a linker script. We need
429 this in case some dynamic object refers to this symbol. */
431 bfd_boolean
432 bfd_elf_record_link_assignment (bfd *output_bfd,
433 struct bfd_link_info *info,
434 const char *name,
435 bfd_boolean provide,
436 bfd_boolean hidden)
438 struct elf_link_hash_entry *h;
439 struct elf_link_hash_table *htab;
441 if (!is_elf_hash_table (info->hash))
442 return TRUE;
444 htab = elf_hash_table (info);
445 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
446 if (h == NULL)
447 return provide;
449 /* Since we're defining the symbol, don't let it seem to have not
450 been defined. record_dynamic_symbol and size_dynamic_sections
451 may depend on this. */
452 if (h->root.type == bfd_link_hash_undefweak
453 || h->root.type == bfd_link_hash_undefined)
455 h->root.type = bfd_link_hash_new;
456 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
457 bfd_link_repair_undef_list (&htab->root);
460 if (h->root.type == bfd_link_hash_new)
461 h->non_elf = 0;
463 /* If this symbol is being provided by the linker script, and it is
464 currently defined by a dynamic object, but not by a regular
465 object, then mark it as undefined so that the generic linker will
466 force the correct value. */
467 if (provide
468 && h->def_dynamic
469 && !h->def_regular)
470 h->root.type = bfd_link_hash_undefined;
472 /* If this symbol is not being provided by the linker script, and it is
473 currently defined by a dynamic object, but not by a regular object,
474 then clear out any version information because the symbol will not be
475 associated with the dynamic object any more. */
476 if (!provide
477 && h->def_dynamic
478 && !h->def_regular)
479 h->verinfo.verdef = NULL;
481 h->def_regular = 1;
483 if (provide && hidden)
485 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
487 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
491 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
492 and executables. */
493 if (!info->relocatable
494 && h->dynindx != -1
495 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
496 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
497 h->forced_local = 1;
499 if ((h->def_dynamic
500 || h->ref_dynamic
501 || info->shared
502 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
503 && h->dynindx == -1)
505 if (! bfd_elf_link_record_dynamic_symbol (info, h))
506 return FALSE;
508 /* If this is a weak defined symbol, and we know a corresponding
509 real symbol from the same dynamic object, make sure the real
510 symbol is also made into a dynamic symbol. */
511 if (h->u.weakdef != NULL
512 && h->u.weakdef->dynindx == -1)
514 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
515 return FALSE;
519 return TRUE;
522 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
523 success, and 2 on a failure caused by attempting to record a symbol
524 in a discarded section, eg. a discarded link-once section symbol. */
527 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
528 bfd *input_bfd,
529 long input_indx)
531 bfd_size_type amt;
532 struct elf_link_local_dynamic_entry *entry;
533 struct elf_link_hash_table *eht;
534 struct elf_strtab_hash *dynstr;
535 unsigned long dynstr_index;
536 char *name;
537 Elf_External_Sym_Shndx eshndx;
538 char esym[sizeof (Elf64_External_Sym)];
540 if (! is_elf_hash_table (info->hash))
541 return 0;
543 /* See if the entry exists already. */
544 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
545 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
546 return 1;
548 amt = sizeof (*entry);
549 entry = bfd_alloc (input_bfd, amt);
550 if (entry == NULL)
551 return 0;
553 /* Go find the symbol, so that we can find it's name. */
554 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
555 1, input_indx, &entry->isym, esym, &eshndx))
557 bfd_release (input_bfd, entry);
558 return 0;
561 if (entry->isym.st_shndx != SHN_UNDEF
562 && (entry->isym.st_shndx < SHN_LORESERVE
563 || entry->isym.st_shndx > SHN_HIRESERVE))
565 asection *s;
567 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
568 if (s == NULL || bfd_is_abs_section (s->output_section))
570 /* We can still bfd_release here as nothing has done another
571 bfd_alloc. We can't do this later in this function. */
572 bfd_release (input_bfd, entry);
573 return 2;
577 name = (bfd_elf_string_from_elf_section
578 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
579 entry->isym.st_name));
581 dynstr = elf_hash_table (info)->dynstr;
582 if (dynstr == NULL)
584 /* Create a strtab to hold the dynamic symbol names. */
585 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
586 if (dynstr == NULL)
587 return 0;
590 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
591 if (dynstr_index == (unsigned long) -1)
592 return 0;
593 entry->isym.st_name = dynstr_index;
595 eht = elf_hash_table (info);
597 entry->next = eht->dynlocal;
598 eht->dynlocal = entry;
599 entry->input_bfd = input_bfd;
600 entry->input_indx = input_indx;
601 eht->dynsymcount++;
603 /* Whatever binding the symbol had before, it's now local. */
604 entry->isym.st_info
605 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
607 /* The dynindx will be set at the end of size_dynamic_sections. */
609 return 1;
612 /* Return the dynindex of a local dynamic symbol. */
614 long
615 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
616 bfd *input_bfd,
617 long input_indx)
619 struct elf_link_local_dynamic_entry *e;
621 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
622 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
623 return e->dynindx;
624 return -1;
627 /* This function is used to renumber the dynamic symbols, if some of
628 them are removed because they are marked as local. This is called
629 via elf_link_hash_traverse. */
631 static bfd_boolean
632 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
633 void *data)
635 size_t *count = data;
637 if (h->root.type == bfd_link_hash_warning)
638 h = (struct elf_link_hash_entry *) h->root.u.i.link;
640 if (h->forced_local)
641 return TRUE;
643 if (h->dynindx != -1)
644 h->dynindx = ++(*count);
646 return TRUE;
650 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
651 STB_LOCAL binding. */
653 static bfd_boolean
654 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
655 void *data)
657 size_t *count = data;
659 if (h->root.type == bfd_link_hash_warning)
660 h = (struct elf_link_hash_entry *) h->root.u.i.link;
662 if (!h->forced_local)
663 return TRUE;
665 if (h->dynindx != -1)
666 h->dynindx = ++(*count);
668 return TRUE;
671 /* Return true if the dynamic symbol for a given section should be
672 omitted when creating a shared library. */
673 bfd_boolean
674 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
675 struct bfd_link_info *info,
676 asection *p)
678 switch (elf_section_data (p)->this_hdr.sh_type)
680 case SHT_PROGBITS:
681 case SHT_NOBITS:
682 /* If sh_type is yet undecided, assume it could be
683 SHT_PROGBITS/SHT_NOBITS. */
684 case SHT_NULL:
685 if (strcmp (p->name, ".got") == 0
686 || strcmp (p->name, ".got.plt") == 0
687 || strcmp (p->name, ".plt") == 0)
689 asection *ip;
690 bfd *dynobj = elf_hash_table (info)->dynobj;
692 if (dynobj != NULL
693 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
694 && (ip->flags & SEC_LINKER_CREATED)
695 && ip->output_section == p)
696 return TRUE;
698 return FALSE;
700 /* There shouldn't be section relative relocations
701 against any other section. */
702 default:
703 return TRUE;
707 /* Assign dynsym indices. In a shared library we generate a section
708 symbol for each output section, which come first. Next come symbols
709 which have been forced to local binding. Then all of the back-end
710 allocated local dynamic syms, followed by the rest of the global
711 symbols. */
713 static unsigned long
714 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
715 struct bfd_link_info *info,
716 unsigned long *section_sym_count)
718 unsigned long dynsymcount = 0;
720 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
722 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
723 asection *p;
724 for (p = output_bfd->sections; p ; p = p->next)
725 if ((p->flags & SEC_EXCLUDE) == 0
726 && (p->flags & SEC_ALLOC) != 0
727 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
728 elf_section_data (p)->dynindx = ++dynsymcount;
730 *section_sym_count = dynsymcount;
732 elf_link_hash_traverse (elf_hash_table (info),
733 elf_link_renumber_local_hash_table_dynsyms,
734 &dynsymcount);
736 if (elf_hash_table (info)->dynlocal)
738 struct elf_link_local_dynamic_entry *p;
739 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
740 p->dynindx = ++dynsymcount;
743 elf_link_hash_traverse (elf_hash_table (info),
744 elf_link_renumber_hash_table_dynsyms,
745 &dynsymcount);
747 /* There is an unused NULL entry at the head of the table which
748 we must account for in our count. Unless there weren't any
749 symbols, which means we'll have no table at all. */
750 if (dynsymcount != 0)
751 ++dynsymcount;
753 elf_hash_table (info)->dynsymcount = dynsymcount;
754 return dynsymcount;
757 /* This function is called when we want to define a new symbol. It
758 handles the various cases which arise when we find a definition in
759 a dynamic object, or when there is already a definition in a
760 dynamic object. The new symbol is described by NAME, SYM, PSEC,
761 and PVALUE. We set SYM_HASH to the hash table entry. We set
762 OVERRIDE if the old symbol is overriding a new definition. We set
763 TYPE_CHANGE_OK if it is OK for the type to change. We set
764 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
765 change, we mean that we shouldn't warn if the type or size does
766 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
767 object is overridden by a regular object. */
769 bfd_boolean
770 _bfd_elf_merge_symbol (bfd *abfd,
771 struct bfd_link_info *info,
772 const char *name,
773 Elf_Internal_Sym *sym,
774 asection **psec,
775 bfd_vma *pvalue,
776 unsigned int *pold_alignment,
777 struct elf_link_hash_entry **sym_hash,
778 bfd_boolean *skip,
779 bfd_boolean *override,
780 bfd_boolean *type_change_ok,
781 bfd_boolean *size_change_ok)
783 asection *sec, *oldsec;
784 struct elf_link_hash_entry *h;
785 struct elf_link_hash_entry *flip;
786 int bind;
787 bfd *oldbfd;
788 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
789 bfd_boolean newweak, oldweak;
790 const struct elf_backend_data *bed;
792 *skip = FALSE;
793 *override = FALSE;
795 sec = *psec;
796 bind = ELF_ST_BIND (sym->st_info);
798 if (! bfd_is_und_section (sec))
799 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
800 else
801 h = ((struct elf_link_hash_entry *)
802 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
803 if (h == NULL)
804 return FALSE;
805 *sym_hash = h;
807 /* This code is for coping with dynamic objects, and is only useful
808 if we are doing an ELF link. */
809 if (info->hash->creator != abfd->xvec)
810 return TRUE;
812 /* For merging, we only care about real symbols. */
814 while (h->root.type == bfd_link_hash_indirect
815 || h->root.type == bfd_link_hash_warning)
816 h = (struct elf_link_hash_entry *) h->root.u.i.link;
818 /* If we just created the symbol, mark it as being an ELF symbol.
819 Other than that, there is nothing to do--there is no merge issue
820 with a newly defined symbol--so we just return. */
822 if (h->root.type == bfd_link_hash_new)
824 h->non_elf = 0;
825 return TRUE;
828 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
829 existing symbol. */
831 switch (h->root.type)
833 default:
834 oldbfd = NULL;
835 oldsec = NULL;
836 break;
838 case bfd_link_hash_undefined:
839 case bfd_link_hash_undefweak:
840 oldbfd = h->root.u.undef.abfd;
841 oldsec = NULL;
842 break;
844 case bfd_link_hash_defined:
845 case bfd_link_hash_defweak:
846 oldbfd = h->root.u.def.section->owner;
847 oldsec = h->root.u.def.section;
848 break;
850 case bfd_link_hash_common:
851 oldbfd = h->root.u.c.p->section->owner;
852 oldsec = h->root.u.c.p->section;
853 break;
856 /* In cases involving weak versioned symbols, we may wind up trying
857 to merge a symbol with itself. Catch that here, to avoid the
858 confusion that results if we try to override a symbol with
859 itself. The additional tests catch cases like
860 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
861 dynamic object, which we do want to handle here. */
862 if (abfd == oldbfd
863 && ((abfd->flags & DYNAMIC) == 0
864 || !h->def_regular))
865 return TRUE;
867 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
868 respectively, is from a dynamic object. */
870 newdyn = (abfd->flags & DYNAMIC) != 0;
872 olddyn = FALSE;
873 if (oldbfd != NULL)
874 olddyn = (oldbfd->flags & DYNAMIC) != 0;
875 else if (oldsec != NULL)
877 /* This handles the special SHN_MIPS_{TEXT,DATA} section
878 indices used by MIPS ELF. */
879 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
882 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
883 respectively, appear to be a definition rather than reference. */
885 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
887 olddef = (h->root.type != bfd_link_hash_undefined
888 && h->root.type != bfd_link_hash_undefweak
889 && h->root.type != bfd_link_hash_common);
891 /* Check TLS symbol. We don't check undefined symbol introduced by
892 "ld -u". */
893 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
894 && ELF_ST_TYPE (sym->st_info) != h->type
895 && oldbfd != NULL)
897 bfd *ntbfd, *tbfd;
898 bfd_boolean ntdef, tdef;
899 asection *ntsec, *tsec;
901 if (h->type == STT_TLS)
903 ntbfd = abfd;
904 ntsec = sec;
905 ntdef = newdef;
906 tbfd = oldbfd;
907 tsec = oldsec;
908 tdef = olddef;
910 else
912 ntbfd = oldbfd;
913 ntsec = oldsec;
914 ntdef = olddef;
915 tbfd = abfd;
916 tsec = sec;
917 tdef = newdef;
920 if (tdef && ntdef)
921 (*_bfd_error_handler)
922 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
923 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
924 else if (!tdef && !ntdef)
925 (*_bfd_error_handler)
926 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
927 tbfd, ntbfd, h->root.root.string);
928 else if (tdef)
929 (*_bfd_error_handler)
930 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
931 tbfd, tsec, ntbfd, h->root.root.string);
932 else
933 (*_bfd_error_handler)
934 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
935 tbfd, ntbfd, ntsec, h->root.root.string);
937 bfd_set_error (bfd_error_bad_value);
938 return FALSE;
941 /* We need to remember if a symbol has a definition in a dynamic
942 object or is weak in all dynamic objects. Internal and hidden
943 visibility will make it unavailable to dynamic objects. */
944 if (newdyn && !h->dynamic_def)
946 if (!bfd_is_und_section (sec))
947 h->dynamic_def = 1;
948 else
950 /* Check if this symbol is weak in all dynamic objects. If it
951 is the first time we see it in a dynamic object, we mark
952 if it is weak. Otherwise, we clear it. */
953 if (!h->ref_dynamic)
955 if (bind == STB_WEAK)
956 h->dynamic_weak = 1;
958 else if (bind != STB_WEAK)
959 h->dynamic_weak = 0;
963 /* If the old symbol has non-default visibility, we ignore the new
964 definition from a dynamic object. */
965 if (newdyn
966 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
967 && !bfd_is_und_section (sec))
969 *skip = TRUE;
970 /* Make sure this symbol is dynamic. */
971 h->ref_dynamic = 1;
972 /* A protected symbol has external availability. Make sure it is
973 recorded as dynamic.
975 FIXME: Should we check type and size for protected symbol? */
976 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
977 return bfd_elf_link_record_dynamic_symbol (info, h);
978 else
979 return TRUE;
981 else if (!newdyn
982 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
983 && h->def_dynamic)
985 /* If the new symbol with non-default visibility comes from a
986 relocatable file and the old definition comes from a dynamic
987 object, we remove the old definition. */
988 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
989 h = *sym_hash;
991 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
992 && bfd_is_und_section (sec))
994 /* If the new symbol is undefined and the old symbol was
995 also undefined before, we need to make sure
996 _bfd_generic_link_add_one_symbol doesn't mess
997 up the linker hash table undefs list. Since the old
998 definition came from a dynamic object, it is still on the
999 undefs list. */
1000 h->root.type = bfd_link_hash_undefined;
1001 h->root.u.undef.abfd = abfd;
1003 else
1005 h->root.type = bfd_link_hash_new;
1006 h->root.u.undef.abfd = NULL;
1009 if (h->def_dynamic)
1011 h->def_dynamic = 0;
1012 h->ref_dynamic = 1;
1013 h->dynamic_def = 1;
1015 /* FIXME: Should we check type and size for protected symbol? */
1016 h->size = 0;
1017 h->type = 0;
1018 return TRUE;
1021 /* Differentiate strong and weak symbols. */
1022 newweak = bind == STB_WEAK;
1023 oldweak = (h->root.type == bfd_link_hash_defweak
1024 || h->root.type == bfd_link_hash_undefweak);
1026 /* If a new weak symbol definition comes from a regular file and the
1027 old symbol comes from a dynamic library, we treat the new one as
1028 strong. Similarly, an old weak symbol definition from a regular
1029 file is treated as strong when the new symbol comes from a dynamic
1030 library. Further, an old weak symbol from a dynamic library is
1031 treated as strong if the new symbol is from a dynamic library.
1032 This reflects the way glibc's ld.so works.
1034 Do this before setting *type_change_ok or *size_change_ok so that
1035 we warn properly when dynamic library symbols are overridden. */
1037 if (newdef && !newdyn && olddyn)
1038 newweak = FALSE;
1039 if (olddef && newdyn)
1040 oldweak = FALSE;
1042 /* It's OK to change the type if either the existing symbol or the
1043 new symbol is weak. A type change is also OK if the old symbol
1044 is undefined and the new symbol is defined. */
1046 if (oldweak
1047 || newweak
1048 || (newdef
1049 && h->root.type == bfd_link_hash_undefined))
1050 *type_change_ok = TRUE;
1052 /* It's OK to change the size if either the existing symbol or the
1053 new symbol is weak, or if the old symbol is undefined. */
1055 if (*type_change_ok
1056 || h->root.type == bfd_link_hash_undefined)
1057 *size_change_ok = TRUE;
1059 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1060 symbol, respectively, appears to be a common symbol in a dynamic
1061 object. If a symbol appears in an uninitialized section, and is
1062 not weak, and is not a function, then it may be a common symbol
1063 which was resolved when the dynamic object was created. We want
1064 to treat such symbols specially, because they raise special
1065 considerations when setting the symbol size: if the symbol
1066 appears as a common symbol in a regular object, and the size in
1067 the regular object is larger, we must make sure that we use the
1068 larger size. This problematic case can always be avoided in C,
1069 but it must be handled correctly when using Fortran shared
1070 libraries.
1072 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1073 likewise for OLDDYNCOMMON and OLDDEF.
1075 Note that this test is just a heuristic, and that it is quite
1076 possible to have an uninitialized symbol in a shared object which
1077 is really a definition, rather than a common symbol. This could
1078 lead to some minor confusion when the symbol really is a common
1079 symbol in some regular object. However, I think it will be
1080 harmless. */
1082 if (newdyn
1083 && newdef
1084 && !newweak
1085 && (sec->flags & SEC_ALLOC) != 0
1086 && (sec->flags & SEC_LOAD) == 0
1087 && sym->st_size > 0
1088 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1089 newdyncommon = TRUE;
1090 else
1091 newdyncommon = FALSE;
1093 if (olddyn
1094 && olddef
1095 && h->root.type == bfd_link_hash_defined
1096 && h->def_dynamic
1097 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1098 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1099 && h->size > 0
1100 && h->type != STT_FUNC)
1101 olddyncommon = TRUE;
1102 else
1103 olddyncommon = FALSE;
1105 /* We now know everything about the old and new symbols. We ask the
1106 backend to check if we can merge them. */
1107 bed = get_elf_backend_data (abfd);
1108 if (bed->merge_symbol
1109 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1110 pold_alignment, skip, override,
1111 type_change_ok, size_change_ok,
1112 &newdyn, &newdef, &newdyncommon, &newweak,
1113 abfd, &sec,
1114 &olddyn, &olddef, &olddyncommon, &oldweak,
1115 oldbfd, &oldsec))
1116 return FALSE;
1118 /* If both the old and the new symbols look like common symbols in a
1119 dynamic object, set the size of the symbol to the larger of the
1120 two. */
1122 if (olddyncommon
1123 && newdyncommon
1124 && sym->st_size != h->size)
1126 /* Since we think we have two common symbols, issue a multiple
1127 common warning if desired. Note that we only warn if the
1128 size is different. If the size is the same, we simply let
1129 the old symbol override the new one as normally happens with
1130 symbols defined in dynamic objects. */
1132 if (! ((*info->callbacks->multiple_common)
1133 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1134 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1135 return FALSE;
1137 if (sym->st_size > h->size)
1138 h->size = sym->st_size;
1140 *size_change_ok = TRUE;
1143 /* If we are looking at a dynamic object, and we have found a
1144 definition, we need to see if the symbol was already defined by
1145 some other object. If so, we want to use the existing
1146 definition, and we do not want to report a multiple symbol
1147 definition error; we do this by clobbering *PSEC to be
1148 bfd_und_section_ptr.
1150 We treat a common symbol as a definition if the symbol in the
1151 shared library is a function, since common symbols always
1152 represent variables; this can cause confusion in principle, but
1153 any such confusion would seem to indicate an erroneous program or
1154 shared library. We also permit a common symbol in a regular
1155 object to override a weak symbol in a shared object. */
1157 if (newdyn
1158 && newdef
1159 && (olddef
1160 || (h->root.type == bfd_link_hash_common
1161 && (newweak
1162 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1164 *override = TRUE;
1165 newdef = FALSE;
1166 newdyncommon = FALSE;
1168 *psec = sec = bfd_und_section_ptr;
1169 *size_change_ok = TRUE;
1171 /* If we get here when the old symbol is a common symbol, then
1172 we are explicitly letting it override a weak symbol or
1173 function in a dynamic object, and we don't want to warn about
1174 a type change. If the old symbol is a defined symbol, a type
1175 change warning may still be appropriate. */
1177 if (h->root.type == bfd_link_hash_common)
1178 *type_change_ok = TRUE;
1181 /* Handle the special case of an old common symbol merging with a
1182 new symbol which looks like a common symbol in a shared object.
1183 We change *PSEC and *PVALUE to make the new symbol look like a
1184 common symbol, and let _bfd_generic_link_add_one_symbol do the
1185 right thing. */
1187 if (newdyncommon
1188 && h->root.type == bfd_link_hash_common)
1190 *override = TRUE;
1191 newdef = FALSE;
1192 newdyncommon = FALSE;
1193 *pvalue = sym->st_size;
1194 *psec = sec = bed->common_section (oldsec);
1195 *size_change_ok = TRUE;
1198 /* Skip weak definitions of symbols that are already defined. */
1199 if (newdef && olddef && newweak)
1200 *skip = TRUE;
1202 /* If the old symbol is from a dynamic object, and the new symbol is
1203 a definition which is not from a dynamic object, then the new
1204 symbol overrides the old symbol. Symbols from regular files
1205 always take precedence over symbols from dynamic objects, even if
1206 they are defined after the dynamic object in the link.
1208 As above, we again permit a common symbol in a regular object to
1209 override a definition in a shared object if the shared object
1210 symbol is a function or is weak. */
1212 flip = NULL;
1213 if (!newdyn
1214 && (newdef
1215 || (bfd_is_com_section (sec)
1216 && (oldweak
1217 || h->type == STT_FUNC)))
1218 && olddyn
1219 && olddef
1220 && h->def_dynamic)
1222 /* Change the hash table entry to undefined, and let
1223 _bfd_generic_link_add_one_symbol do the right thing with the
1224 new definition. */
1226 h->root.type = bfd_link_hash_undefined;
1227 h->root.u.undef.abfd = h->root.u.def.section->owner;
1228 *size_change_ok = TRUE;
1230 olddef = FALSE;
1231 olddyncommon = FALSE;
1233 /* We again permit a type change when a common symbol may be
1234 overriding a function. */
1236 if (bfd_is_com_section (sec))
1237 *type_change_ok = TRUE;
1239 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1240 flip = *sym_hash;
1241 else
1242 /* This union may have been set to be non-NULL when this symbol
1243 was seen in a dynamic object. We must force the union to be
1244 NULL, so that it is correct for a regular symbol. */
1245 h->verinfo.vertree = NULL;
1248 /* Handle the special case of a new common symbol merging with an
1249 old symbol that looks like it might be a common symbol defined in
1250 a shared object. Note that we have already handled the case in
1251 which a new common symbol should simply override the definition
1252 in the shared library. */
1254 if (! newdyn
1255 && bfd_is_com_section (sec)
1256 && olddyncommon)
1258 /* It would be best if we could set the hash table entry to a
1259 common symbol, but we don't know what to use for the section
1260 or the alignment. */
1261 if (! ((*info->callbacks->multiple_common)
1262 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1263 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1264 return FALSE;
1266 /* If the presumed common symbol in the dynamic object is
1267 larger, pretend that the new symbol has its size. */
1269 if (h->size > *pvalue)
1270 *pvalue = h->size;
1272 /* We need to remember the alignment required by the symbol
1273 in the dynamic object. */
1274 BFD_ASSERT (pold_alignment);
1275 *pold_alignment = h->root.u.def.section->alignment_power;
1277 olddef = FALSE;
1278 olddyncommon = FALSE;
1280 h->root.type = bfd_link_hash_undefined;
1281 h->root.u.undef.abfd = h->root.u.def.section->owner;
1283 *size_change_ok = TRUE;
1284 *type_change_ok = TRUE;
1286 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1287 flip = *sym_hash;
1288 else
1289 h->verinfo.vertree = NULL;
1292 if (flip != NULL)
1294 /* Handle the case where we had a versioned symbol in a dynamic
1295 library and now find a definition in a normal object. In this
1296 case, we make the versioned symbol point to the normal one. */
1297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1298 flip->root.type = h->root.type;
1299 h->root.type = bfd_link_hash_indirect;
1300 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1301 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1302 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1303 if (h->def_dynamic)
1305 h->def_dynamic = 0;
1306 flip->ref_dynamic = 1;
1310 return TRUE;
1313 /* This function is called to create an indirect symbol from the
1314 default for the symbol with the default version if needed. The
1315 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1316 set DYNSYM if the new indirect symbol is dynamic. */
1318 bfd_boolean
1319 _bfd_elf_add_default_symbol (bfd *abfd,
1320 struct bfd_link_info *info,
1321 struct elf_link_hash_entry *h,
1322 const char *name,
1323 Elf_Internal_Sym *sym,
1324 asection **psec,
1325 bfd_vma *value,
1326 bfd_boolean *dynsym,
1327 bfd_boolean override)
1329 bfd_boolean type_change_ok;
1330 bfd_boolean size_change_ok;
1331 bfd_boolean skip;
1332 char *shortname;
1333 struct elf_link_hash_entry *hi;
1334 struct bfd_link_hash_entry *bh;
1335 const struct elf_backend_data *bed;
1336 bfd_boolean collect;
1337 bfd_boolean dynamic;
1338 char *p;
1339 size_t len, shortlen;
1340 asection *sec;
1342 /* If this symbol has a version, and it is the default version, we
1343 create an indirect symbol from the default name to the fully
1344 decorated name. This will cause external references which do not
1345 specify a version to be bound to this version of the symbol. */
1346 p = strchr (name, ELF_VER_CHR);
1347 if (p == NULL || p[1] != ELF_VER_CHR)
1348 return TRUE;
1350 if (override)
1352 /* We are overridden by an old definition. We need to check if we
1353 need to create the indirect symbol from the default name. */
1354 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1355 FALSE, FALSE);
1356 BFD_ASSERT (hi != NULL);
1357 if (hi == h)
1358 return TRUE;
1359 while (hi->root.type == bfd_link_hash_indirect
1360 || hi->root.type == bfd_link_hash_warning)
1362 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1363 if (hi == h)
1364 return TRUE;
1368 bed = get_elf_backend_data (abfd);
1369 collect = bed->collect;
1370 dynamic = (abfd->flags & DYNAMIC) != 0;
1372 shortlen = p - name;
1373 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1374 if (shortname == NULL)
1375 return FALSE;
1376 memcpy (shortname, name, shortlen);
1377 shortname[shortlen] = '\0';
1379 /* We are going to create a new symbol. Merge it with any existing
1380 symbol with this name. For the purposes of the merge, act as
1381 though we were defining the symbol we just defined, although we
1382 actually going to define an indirect symbol. */
1383 type_change_ok = FALSE;
1384 size_change_ok = FALSE;
1385 sec = *psec;
1386 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1387 NULL, &hi, &skip, &override,
1388 &type_change_ok, &size_change_ok))
1389 return FALSE;
1391 if (skip)
1392 goto nondefault;
1394 if (! override)
1396 bh = &hi->root;
1397 if (! (_bfd_generic_link_add_one_symbol
1398 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1399 0, name, FALSE, collect, &bh)))
1400 return FALSE;
1401 hi = (struct elf_link_hash_entry *) bh;
1403 else
1405 /* In this case the symbol named SHORTNAME is overriding the
1406 indirect symbol we want to add. We were planning on making
1407 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1408 is the name without a version. NAME is the fully versioned
1409 name, and it is the default version.
1411 Overriding means that we already saw a definition for the
1412 symbol SHORTNAME in a regular object, and it is overriding
1413 the symbol defined in the dynamic object.
1415 When this happens, we actually want to change NAME, the
1416 symbol we just added, to refer to SHORTNAME. This will cause
1417 references to NAME in the shared object to become references
1418 to SHORTNAME in the regular object. This is what we expect
1419 when we override a function in a shared object: that the
1420 references in the shared object will be mapped to the
1421 definition in the regular object. */
1423 while (hi->root.type == bfd_link_hash_indirect
1424 || hi->root.type == bfd_link_hash_warning)
1425 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1427 h->root.type = bfd_link_hash_indirect;
1428 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1429 if (h->def_dynamic)
1431 h->def_dynamic = 0;
1432 hi->ref_dynamic = 1;
1433 if (hi->ref_regular
1434 || hi->def_regular)
1436 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1437 return FALSE;
1441 /* Now set HI to H, so that the following code will set the
1442 other fields correctly. */
1443 hi = h;
1446 /* If there is a duplicate definition somewhere, then HI may not
1447 point to an indirect symbol. We will have reported an error to
1448 the user in that case. */
1450 if (hi->root.type == bfd_link_hash_indirect)
1452 struct elf_link_hash_entry *ht;
1454 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1455 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1457 /* See if the new flags lead us to realize that the symbol must
1458 be dynamic. */
1459 if (! *dynsym)
1461 if (! dynamic)
1463 if (info->shared
1464 || hi->ref_dynamic)
1465 *dynsym = TRUE;
1467 else
1469 if (hi->ref_regular)
1470 *dynsym = TRUE;
1475 /* We also need to define an indirection from the nondefault version
1476 of the symbol. */
1478 nondefault:
1479 len = strlen (name);
1480 shortname = bfd_hash_allocate (&info->hash->table, len);
1481 if (shortname == NULL)
1482 return FALSE;
1483 memcpy (shortname, name, shortlen);
1484 memcpy (shortname + shortlen, p + 1, len - shortlen);
1486 /* Once again, merge with any existing symbol. */
1487 type_change_ok = FALSE;
1488 size_change_ok = FALSE;
1489 sec = *psec;
1490 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1491 NULL, &hi, &skip, &override,
1492 &type_change_ok, &size_change_ok))
1493 return FALSE;
1495 if (skip)
1496 return TRUE;
1498 if (override)
1500 /* Here SHORTNAME is a versioned name, so we don't expect to see
1501 the type of override we do in the case above unless it is
1502 overridden by a versioned definition. */
1503 if (hi->root.type != bfd_link_hash_defined
1504 && hi->root.type != bfd_link_hash_defweak)
1505 (*_bfd_error_handler)
1506 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1507 abfd, shortname);
1509 else
1511 bh = &hi->root;
1512 if (! (_bfd_generic_link_add_one_symbol
1513 (info, abfd, shortname, BSF_INDIRECT,
1514 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1515 return FALSE;
1516 hi = (struct elf_link_hash_entry *) bh;
1518 /* If there is a duplicate definition somewhere, then HI may not
1519 point to an indirect symbol. We will have reported an error
1520 to the user in that case. */
1522 if (hi->root.type == bfd_link_hash_indirect)
1524 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1526 /* See if the new flags lead us to realize that the symbol
1527 must be dynamic. */
1528 if (! *dynsym)
1530 if (! dynamic)
1532 if (info->shared
1533 || hi->ref_dynamic)
1534 *dynsym = TRUE;
1536 else
1538 if (hi->ref_regular)
1539 *dynsym = TRUE;
1545 return TRUE;
1548 /* This routine is used to export all defined symbols into the dynamic
1549 symbol table. It is called via elf_link_hash_traverse. */
1551 bfd_boolean
1552 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1554 struct elf_info_failed *eif = data;
1556 /* Ignore indirect symbols. These are added by the versioning code. */
1557 if (h->root.type == bfd_link_hash_indirect)
1558 return TRUE;
1560 if (h->root.type == bfd_link_hash_warning)
1561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1563 if (h->dynindx == -1
1564 && (h->def_regular
1565 || h->ref_regular))
1567 struct bfd_elf_version_tree *t;
1568 struct bfd_elf_version_expr *d;
1570 for (t = eif->verdefs; t != NULL; t = t->next)
1572 if (t->globals.list != NULL)
1574 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1575 if (d != NULL)
1576 goto doit;
1579 if (t->locals.list != NULL)
1581 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1582 if (d != NULL)
1583 return TRUE;
1587 if (!eif->verdefs)
1589 doit:
1590 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1592 eif->failed = TRUE;
1593 return FALSE;
1598 return TRUE;
1601 /* Look through the symbols which are defined in other shared
1602 libraries and referenced here. Update the list of version
1603 dependencies. This will be put into the .gnu.version_r section.
1604 This function is called via elf_link_hash_traverse. */
1606 bfd_boolean
1607 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1608 void *data)
1610 struct elf_find_verdep_info *rinfo = data;
1611 Elf_Internal_Verneed *t;
1612 Elf_Internal_Vernaux *a;
1613 bfd_size_type amt;
1615 if (h->root.type == bfd_link_hash_warning)
1616 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1618 /* We only care about symbols defined in shared objects with version
1619 information. */
1620 if (!h->def_dynamic
1621 || h->def_regular
1622 || h->dynindx == -1
1623 || h->verinfo.verdef == NULL)
1624 return TRUE;
1626 /* See if we already know about this version. */
1627 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1629 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1630 continue;
1632 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1633 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1634 return TRUE;
1636 break;
1639 /* This is a new version. Add it to tree we are building. */
1641 if (t == NULL)
1643 amt = sizeof *t;
1644 t = bfd_zalloc (rinfo->output_bfd, amt);
1645 if (t == NULL)
1647 rinfo->failed = TRUE;
1648 return FALSE;
1651 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1652 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1653 elf_tdata (rinfo->output_bfd)->verref = t;
1656 amt = sizeof *a;
1657 a = bfd_zalloc (rinfo->output_bfd, amt);
1659 /* Note that we are copying a string pointer here, and testing it
1660 above. If bfd_elf_string_from_elf_section is ever changed to
1661 discard the string data when low in memory, this will have to be
1662 fixed. */
1663 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1665 a->vna_flags = h->verinfo.verdef->vd_flags;
1666 a->vna_nextptr = t->vn_auxptr;
1668 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1669 ++rinfo->vers;
1671 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1673 t->vn_auxptr = a;
1675 return TRUE;
1678 /* Figure out appropriate versions for all the symbols. We may not
1679 have the version number script until we have read all of the input
1680 files, so until that point we don't know which symbols should be
1681 local. This function is called via elf_link_hash_traverse. */
1683 bfd_boolean
1684 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1686 struct elf_assign_sym_version_info *sinfo;
1687 struct bfd_link_info *info;
1688 const struct elf_backend_data *bed;
1689 struct elf_info_failed eif;
1690 char *p;
1691 bfd_size_type amt;
1693 sinfo = data;
1694 info = sinfo->info;
1696 if (h->root.type == bfd_link_hash_warning)
1697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1699 /* Fix the symbol flags. */
1700 eif.failed = FALSE;
1701 eif.info = info;
1702 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1704 if (eif.failed)
1705 sinfo->failed = TRUE;
1706 return FALSE;
1709 /* We only need version numbers for symbols defined in regular
1710 objects. */
1711 if (!h->def_regular)
1712 return TRUE;
1714 bed = get_elf_backend_data (sinfo->output_bfd);
1715 p = strchr (h->root.root.string, ELF_VER_CHR);
1716 if (p != NULL && h->verinfo.vertree == NULL)
1718 struct bfd_elf_version_tree *t;
1719 bfd_boolean hidden;
1721 hidden = TRUE;
1723 /* There are two consecutive ELF_VER_CHR characters if this is
1724 not a hidden symbol. */
1725 ++p;
1726 if (*p == ELF_VER_CHR)
1728 hidden = FALSE;
1729 ++p;
1732 /* If there is no version string, we can just return out. */
1733 if (*p == '\0')
1735 if (hidden)
1736 h->hidden = 1;
1737 return TRUE;
1740 /* Look for the version. If we find it, it is no longer weak. */
1741 for (t = sinfo->verdefs; t != NULL; t = t->next)
1743 if (strcmp (t->name, p) == 0)
1745 size_t len;
1746 char *alc;
1747 struct bfd_elf_version_expr *d;
1749 len = p - h->root.root.string;
1750 alc = bfd_malloc (len);
1751 if (alc == NULL)
1752 return FALSE;
1753 memcpy (alc, h->root.root.string, len - 1);
1754 alc[len - 1] = '\0';
1755 if (alc[len - 2] == ELF_VER_CHR)
1756 alc[len - 2] = '\0';
1758 h->verinfo.vertree = t;
1759 t->used = TRUE;
1760 d = NULL;
1762 if (t->globals.list != NULL)
1763 d = (*t->match) (&t->globals, NULL, alc);
1765 /* See if there is anything to force this symbol to
1766 local scope. */
1767 if (d == NULL && t->locals.list != NULL)
1769 d = (*t->match) (&t->locals, NULL, alc);
1770 if (d != NULL
1771 && h->dynindx != -1
1772 && ! info->export_dynamic)
1773 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1776 free (alc);
1777 break;
1781 /* If we are building an application, we need to create a
1782 version node for this version. */
1783 if (t == NULL && info->executable)
1785 struct bfd_elf_version_tree **pp;
1786 int version_index;
1788 /* If we aren't going to export this symbol, we don't need
1789 to worry about it. */
1790 if (h->dynindx == -1)
1791 return TRUE;
1793 amt = sizeof *t;
1794 t = bfd_zalloc (sinfo->output_bfd, amt);
1795 if (t == NULL)
1797 sinfo->failed = TRUE;
1798 return FALSE;
1801 t->name = p;
1802 t->name_indx = (unsigned int) -1;
1803 t->used = TRUE;
1805 version_index = 1;
1806 /* Don't count anonymous version tag. */
1807 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1808 version_index = 0;
1809 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1810 ++version_index;
1811 t->vernum = version_index;
1813 *pp = t;
1815 h->verinfo.vertree = t;
1817 else if (t == NULL)
1819 /* We could not find the version for a symbol when
1820 generating a shared archive. Return an error. */
1821 (*_bfd_error_handler)
1822 (_("%B: undefined versioned symbol name %s"),
1823 sinfo->output_bfd, h->root.root.string);
1824 bfd_set_error (bfd_error_bad_value);
1825 sinfo->failed = TRUE;
1826 return FALSE;
1829 if (hidden)
1830 h->hidden = 1;
1833 /* If we don't have a version for this symbol, see if we can find
1834 something. */
1835 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1837 struct bfd_elf_version_tree *t;
1838 struct bfd_elf_version_tree *local_ver;
1839 struct bfd_elf_version_expr *d;
1841 /* See if can find what version this symbol is in. If the
1842 symbol is supposed to be local, then don't actually register
1843 it. */
1844 local_ver = NULL;
1845 for (t = sinfo->verdefs; t != NULL; t = t->next)
1847 if (t->globals.list != NULL)
1849 bfd_boolean matched;
1851 matched = FALSE;
1852 d = NULL;
1853 while ((d = (*t->match) (&t->globals, d,
1854 h->root.root.string)) != NULL)
1855 if (d->symver)
1856 matched = TRUE;
1857 else
1859 /* There is a version without definition. Make
1860 the symbol the default definition for this
1861 version. */
1862 h->verinfo.vertree = t;
1863 local_ver = NULL;
1864 d->script = 1;
1865 break;
1867 if (d != NULL)
1868 break;
1869 else if (matched)
1870 /* There is no undefined version for this symbol. Hide the
1871 default one. */
1872 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1875 if (t->locals.list != NULL)
1877 d = NULL;
1878 while ((d = (*t->match) (&t->locals, d,
1879 h->root.root.string)) != NULL)
1881 local_ver = t;
1882 /* If the match is "*", keep looking for a more
1883 explicit, perhaps even global, match.
1884 XXX: Shouldn't this be !d->wildcard instead? */
1885 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1886 break;
1889 if (d != NULL)
1890 break;
1894 if (local_ver != NULL)
1896 h->verinfo.vertree = local_ver;
1897 if (h->dynindx != -1
1898 && ! info->export_dynamic)
1900 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1905 return TRUE;
1908 /* Read and swap the relocs from the section indicated by SHDR. This
1909 may be either a REL or a RELA section. The relocations are
1910 translated into RELA relocations and stored in INTERNAL_RELOCS,
1911 which should have already been allocated to contain enough space.
1912 The EXTERNAL_RELOCS are a buffer where the external form of the
1913 relocations should be stored.
1915 Returns FALSE if something goes wrong. */
1917 static bfd_boolean
1918 elf_link_read_relocs_from_section (bfd *abfd,
1919 asection *sec,
1920 Elf_Internal_Shdr *shdr,
1921 void *external_relocs,
1922 Elf_Internal_Rela *internal_relocs)
1924 const struct elf_backend_data *bed;
1925 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1926 const bfd_byte *erela;
1927 const bfd_byte *erelaend;
1928 Elf_Internal_Rela *irela;
1929 Elf_Internal_Shdr *symtab_hdr;
1930 size_t nsyms;
1932 /* Position ourselves at the start of the section. */
1933 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1934 return FALSE;
1936 /* Read the relocations. */
1937 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1938 return FALSE;
1940 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1941 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1943 bed = get_elf_backend_data (abfd);
1945 /* Convert the external relocations to the internal format. */
1946 if (shdr->sh_entsize == bed->s->sizeof_rel)
1947 swap_in = bed->s->swap_reloc_in;
1948 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1949 swap_in = bed->s->swap_reloca_in;
1950 else
1952 bfd_set_error (bfd_error_wrong_format);
1953 return FALSE;
1956 erela = external_relocs;
1957 erelaend = erela + shdr->sh_size;
1958 irela = internal_relocs;
1959 while (erela < erelaend)
1961 bfd_vma r_symndx;
1963 (*swap_in) (abfd, erela, irela);
1964 r_symndx = ELF32_R_SYM (irela->r_info);
1965 if (bed->s->arch_size == 64)
1966 r_symndx >>= 24;
1967 if ((size_t) r_symndx >= nsyms)
1969 (*_bfd_error_handler)
1970 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1971 " for offset 0x%lx in section `%A'"),
1972 abfd, sec,
1973 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1974 bfd_set_error (bfd_error_bad_value);
1975 return FALSE;
1977 irela += bed->s->int_rels_per_ext_rel;
1978 erela += shdr->sh_entsize;
1981 return TRUE;
1984 /* Read and swap the relocs for a section O. They may have been
1985 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1986 not NULL, they are used as buffers to read into. They are known to
1987 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1988 the return value is allocated using either malloc or bfd_alloc,
1989 according to the KEEP_MEMORY argument. If O has two relocation
1990 sections (both REL and RELA relocations), then the REL_HDR
1991 relocations will appear first in INTERNAL_RELOCS, followed by the
1992 REL_HDR2 relocations. */
1994 Elf_Internal_Rela *
1995 _bfd_elf_link_read_relocs (bfd *abfd,
1996 asection *o,
1997 void *external_relocs,
1998 Elf_Internal_Rela *internal_relocs,
1999 bfd_boolean keep_memory)
2001 Elf_Internal_Shdr *rel_hdr;
2002 void *alloc1 = NULL;
2003 Elf_Internal_Rela *alloc2 = NULL;
2004 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2006 if (elf_section_data (o)->relocs != NULL)
2007 return elf_section_data (o)->relocs;
2009 if (o->reloc_count == 0)
2010 return NULL;
2012 rel_hdr = &elf_section_data (o)->rel_hdr;
2014 if (internal_relocs == NULL)
2016 bfd_size_type size;
2018 size = o->reloc_count;
2019 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2020 if (keep_memory)
2021 internal_relocs = bfd_alloc (abfd, size);
2022 else
2023 internal_relocs = alloc2 = bfd_malloc (size);
2024 if (internal_relocs == NULL)
2025 goto error_return;
2028 if (external_relocs == NULL)
2030 bfd_size_type size = rel_hdr->sh_size;
2032 if (elf_section_data (o)->rel_hdr2)
2033 size += elf_section_data (o)->rel_hdr2->sh_size;
2034 alloc1 = bfd_malloc (size);
2035 if (alloc1 == NULL)
2036 goto error_return;
2037 external_relocs = alloc1;
2040 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2041 external_relocs,
2042 internal_relocs))
2043 goto error_return;
2044 if (elf_section_data (o)->rel_hdr2
2045 && (!elf_link_read_relocs_from_section
2046 (abfd, o,
2047 elf_section_data (o)->rel_hdr2,
2048 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2049 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2050 * bed->s->int_rels_per_ext_rel))))
2051 goto error_return;
2053 /* Cache the results for next time, if we can. */
2054 if (keep_memory)
2055 elf_section_data (o)->relocs = internal_relocs;
2057 if (alloc1 != NULL)
2058 free (alloc1);
2060 /* Don't free alloc2, since if it was allocated we are passing it
2061 back (under the name of internal_relocs). */
2063 return internal_relocs;
2065 error_return:
2066 if (alloc1 != NULL)
2067 free (alloc1);
2068 if (alloc2 != NULL)
2069 free (alloc2);
2070 return NULL;
2073 /* Compute the size of, and allocate space for, REL_HDR which is the
2074 section header for a section containing relocations for O. */
2076 bfd_boolean
2077 _bfd_elf_link_size_reloc_section (bfd *abfd,
2078 Elf_Internal_Shdr *rel_hdr,
2079 asection *o)
2081 bfd_size_type reloc_count;
2082 bfd_size_type num_rel_hashes;
2084 /* Figure out how many relocations there will be. */
2085 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2086 reloc_count = elf_section_data (o)->rel_count;
2087 else
2088 reloc_count = elf_section_data (o)->rel_count2;
2090 num_rel_hashes = o->reloc_count;
2091 if (num_rel_hashes < reloc_count)
2092 num_rel_hashes = reloc_count;
2094 /* That allows us to calculate the size of the section. */
2095 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2097 /* The contents field must last into write_object_contents, so we
2098 allocate it with bfd_alloc rather than malloc. Also since we
2099 cannot be sure that the contents will actually be filled in,
2100 we zero the allocated space. */
2101 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2102 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2103 return FALSE;
2105 /* We only allocate one set of hash entries, so we only do it the
2106 first time we are called. */
2107 if (elf_section_data (o)->rel_hashes == NULL
2108 && num_rel_hashes)
2110 struct elf_link_hash_entry **p;
2112 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2113 if (p == NULL)
2114 return FALSE;
2116 elf_section_data (o)->rel_hashes = p;
2119 return TRUE;
2122 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2123 originated from the section given by INPUT_REL_HDR) to the
2124 OUTPUT_BFD. */
2126 bfd_boolean
2127 _bfd_elf_link_output_relocs (bfd *output_bfd,
2128 asection *input_section,
2129 Elf_Internal_Shdr *input_rel_hdr,
2130 Elf_Internal_Rela *internal_relocs,
2131 struct elf_link_hash_entry **rel_hash
2132 ATTRIBUTE_UNUSED)
2134 Elf_Internal_Rela *irela;
2135 Elf_Internal_Rela *irelaend;
2136 bfd_byte *erel;
2137 Elf_Internal_Shdr *output_rel_hdr;
2138 asection *output_section;
2139 unsigned int *rel_countp = NULL;
2140 const struct elf_backend_data *bed;
2141 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2143 output_section = input_section->output_section;
2144 output_rel_hdr = NULL;
2146 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2147 == input_rel_hdr->sh_entsize)
2149 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2150 rel_countp = &elf_section_data (output_section)->rel_count;
2152 else if (elf_section_data (output_section)->rel_hdr2
2153 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2154 == input_rel_hdr->sh_entsize))
2156 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2157 rel_countp = &elf_section_data (output_section)->rel_count2;
2159 else
2161 (*_bfd_error_handler)
2162 (_("%B: relocation size mismatch in %B section %A"),
2163 output_bfd, input_section->owner, input_section);
2164 bfd_set_error (bfd_error_wrong_object_format);
2165 return FALSE;
2168 bed = get_elf_backend_data (output_bfd);
2169 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2170 swap_out = bed->s->swap_reloc_out;
2171 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2172 swap_out = bed->s->swap_reloca_out;
2173 else
2174 abort ();
2176 erel = output_rel_hdr->contents;
2177 erel += *rel_countp * input_rel_hdr->sh_entsize;
2178 irela = internal_relocs;
2179 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2180 * bed->s->int_rels_per_ext_rel);
2181 while (irela < irelaend)
2183 (*swap_out) (output_bfd, irela, erel);
2184 irela += bed->s->int_rels_per_ext_rel;
2185 erel += input_rel_hdr->sh_entsize;
2188 /* Bump the counter, so that we know where to add the next set of
2189 relocations. */
2190 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2192 return TRUE;
2195 /* Make weak undefined symbols in PIE dynamic. */
2197 bfd_boolean
2198 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2199 struct elf_link_hash_entry *h)
2201 if (info->pie
2202 && h->dynindx == -1
2203 && h->root.type == bfd_link_hash_undefweak)
2204 return bfd_elf_link_record_dynamic_symbol (info, h);
2206 return TRUE;
2209 /* Fix up the flags for a symbol. This handles various cases which
2210 can only be fixed after all the input files are seen. This is
2211 currently called by both adjust_dynamic_symbol and
2212 assign_sym_version, which is unnecessary but perhaps more robust in
2213 the face of future changes. */
2215 bfd_boolean
2216 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2217 struct elf_info_failed *eif)
2219 const struct elf_backend_data *bed = NULL;
2221 /* If this symbol was mentioned in a non-ELF file, try to set
2222 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2223 permit a non-ELF file to correctly refer to a symbol defined in
2224 an ELF dynamic object. */
2225 if (h->non_elf)
2227 while (h->root.type == bfd_link_hash_indirect)
2228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2230 if (h->root.type != bfd_link_hash_defined
2231 && h->root.type != bfd_link_hash_defweak)
2233 h->ref_regular = 1;
2234 h->ref_regular_nonweak = 1;
2236 else
2238 if (h->root.u.def.section->owner != NULL
2239 && (bfd_get_flavour (h->root.u.def.section->owner)
2240 == bfd_target_elf_flavour))
2242 h->ref_regular = 1;
2243 h->ref_regular_nonweak = 1;
2245 else
2246 h->def_regular = 1;
2249 if (h->dynindx == -1
2250 && (h->def_dynamic
2251 || h->ref_dynamic))
2253 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2255 eif->failed = TRUE;
2256 return FALSE;
2260 else
2262 /* Unfortunately, NON_ELF is only correct if the symbol
2263 was first seen in a non-ELF file. Fortunately, if the symbol
2264 was first seen in an ELF file, we're probably OK unless the
2265 symbol was defined in a non-ELF file. Catch that case here.
2266 FIXME: We're still in trouble if the symbol was first seen in
2267 a dynamic object, and then later in a non-ELF regular object. */
2268 if ((h->root.type == bfd_link_hash_defined
2269 || h->root.type == bfd_link_hash_defweak)
2270 && !h->def_regular
2271 && (h->root.u.def.section->owner != NULL
2272 ? (bfd_get_flavour (h->root.u.def.section->owner)
2273 != bfd_target_elf_flavour)
2274 : (bfd_is_abs_section (h->root.u.def.section)
2275 && !h->def_dynamic)))
2276 h->def_regular = 1;
2279 /* Backend specific symbol fixup. */
2280 if (elf_hash_table (eif->info)->dynobj)
2282 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2283 if (bed->elf_backend_fixup_symbol
2284 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2285 return FALSE;
2288 /* If this is a final link, and the symbol was defined as a common
2289 symbol in a regular object file, and there was no definition in
2290 any dynamic object, then the linker will have allocated space for
2291 the symbol in a common section but the DEF_REGULAR
2292 flag will not have been set. */
2293 if (h->root.type == bfd_link_hash_defined
2294 && !h->def_regular
2295 && h->ref_regular
2296 && !h->def_dynamic
2297 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2298 h->def_regular = 1;
2300 /* If -Bsymbolic was used (which means to bind references to global
2301 symbols to the definition within the shared object), and this
2302 symbol was defined in a regular object, then it actually doesn't
2303 need a PLT entry. Likewise, if the symbol has non-default
2304 visibility. If the symbol has hidden or internal visibility, we
2305 will force it local. */
2306 if (h->needs_plt
2307 && eif->info->shared
2308 && is_elf_hash_table (eif->info->hash)
2309 && (eif->info->symbolic
2310 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2311 && h->def_regular)
2313 bfd_boolean force_local;
2315 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2316 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2317 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2320 /* If a weak undefined symbol has non-default visibility, we also
2321 hide it from the dynamic linker. */
2322 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2323 && h->root.type == bfd_link_hash_undefweak)
2325 const struct elf_backend_data *bed;
2326 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2327 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2330 /* If this is a weak defined symbol in a dynamic object, and we know
2331 the real definition in the dynamic object, copy interesting flags
2332 over to the real definition. */
2333 if (h->u.weakdef != NULL)
2335 struct elf_link_hash_entry *weakdef;
2337 weakdef = h->u.weakdef;
2338 if (h->root.type == bfd_link_hash_indirect)
2339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2341 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2342 || h->root.type == bfd_link_hash_defweak);
2343 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2344 || weakdef->root.type == bfd_link_hash_defweak);
2345 BFD_ASSERT (weakdef->def_dynamic);
2347 /* If the real definition is defined by a regular object file,
2348 don't do anything special. See the longer description in
2349 _bfd_elf_adjust_dynamic_symbol, below. */
2350 if (weakdef->def_regular)
2351 h->u.weakdef = NULL;
2352 else
2353 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2357 return TRUE;
2360 /* Make the backend pick a good value for a dynamic symbol. This is
2361 called via elf_link_hash_traverse, and also calls itself
2362 recursively. */
2364 bfd_boolean
2365 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2367 struct elf_info_failed *eif = data;
2368 bfd *dynobj;
2369 const struct elf_backend_data *bed;
2371 if (! is_elf_hash_table (eif->info->hash))
2372 return FALSE;
2374 if (h->root.type == bfd_link_hash_warning)
2376 h->got = elf_hash_table (eif->info)->init_got_offset;
2377 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2379 /* When warning symbols are created, they **replace** the "real"
2380 entry in the hash table, thus we never get to see the real
2381 symbol in a hash traversal. So look at it now. */
2382 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2385 /* Ignore indirect symbols. These are added by the versioning code. */
2386 if (h->root.type == bfd_link_hash_indirect)
2387 return TRUE;
2389 /* Fix the symbol flags. */
2390 if (! _bfd_elf_fix_symbol_flags (h, eif))
2391 return FALSE;
2393 /* If this symbol does not require a PLT entry, and it is not
2394 defined by a dynamic object, or is not referenced by a regular
2395 object, ignore it. We do have to handle a weak defined symbol,
2396 even if no regular object refers to it, if we decided to add it
2397 to the dynamic symbol table. FIXME: Do we normally need to worry
2398 about symbols which are defined by one dynamic object and
2399 referenced by another one? */
2400 if (!h->needs_plt
2401 && (h->def_regular
2402 || !h->def_dynamic
2403 || (!h->ref_regular
2404 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2406 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2407 return TRUE;
2410 /* If we've already adjusted this symbol, don't do it again. This
2411 can happen via a recursive call. */
2412 if (h->dynamic_adjusted)
2413 return TRUE;
2415 /* Don't look at this symbol again. Note that we must set this
2416 after checking the above conditions, because we may look at a
2417 symbol once, decide not to do anything, and then get called
2418 recursively later after REF_REGULAR is set below. */
2419 h->dynamic_adjusted = 1;
2421 /* If this is a weak definition, and we know a real definition, and
2422 the real symbol is not itself defined by a regular object file,
2423 then get a good value for the real definition. We handle the
2424 real symbol first, for the convenience of the backend routine.
2426 Note that there is a confusing case here. If the real definition
2427 is defined by a regular object file, we don't get the real symbol
2428 from the dynamic object, but we do get the weak symbol. If the
2429 processor backend uses a COPY reloc, then if some routine in the
2430 dynamic object changes the real symbol, we will not see that
2431 change in the corresponding weak symbol. This is the way other
2432 ELF linkers work as well, and seems to be a result of the shared
2433 library model.
2435 I will clarify this issue. Most SVR4 shared libraries define the
2436 variable _timezone and define timezone as a weak synonym. The
2437 tzset call changes _timezone. If you write
2438 extern int timezone;
2439 int _timezone = 5;
2440 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2441 you might expect that, since timezone is a synonym for _timezone,
2442 the same number will print both times. However, if the processor
2443 backend uses a COPY reloc, then actually timezone will be copied
2444 into your process image, and, since you define _timezone
2445 yourself, _timezone will not. Thus timezone and _timezone will
2446 wind up at different memory locations. The tzset call will set
2447 _timezone, leaving timezone unchanged. */
2449 if (h->u.weakdef != NULL)
2451 /* If we get to this point, we know there is an implicit
2452 reference by a regular object file via the weak symbol H.
2453 FIXME: Is this really true? What if the traversal finds
2454 H->U.WEAKDEF before it finds H? */
2455 h->u.weakdef->ref_regular = 1;
2457 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2458 return FALSE;
2461 /* If a symbol has no type and no size and does not require a PLT
2462 entry, then we are probably about to do the wrong thing here: we
2463 are probably going to create a COPY reloc for an empty object.
2464 This case can arise when a shared object is built with assembly
2465 code, and the assembly code fails to set the symbol type. */
2466 if (h->size == 0
2467 && h->type == STT_NOTYPE
2468 && !h->needs_plt)
2469 (*_bfd_error_handler)
2470 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2471 h->root.root.string);
2473 dynobj = elf_hash_table (eif->info)->dynobj;
2474 bed = get_elf_backend_data (dynobj);
2475 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2477 eif->failed = TRUE;
2478 return FALSE;
2481 return TRUE;
2484 /* Adjust all external symbols pointing into SEC_MERGE sections
2485 to reflect the object merging within the sections. */
2487 bfd_boolean
2488 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2490 asection *sec;
2492 if (h->root.type == bfd_link_hash_warning)
2493 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2495 if ((h->root.type == bfd_link_hash_defined
2496 || h->root.type == bfd_link_hash_defweak)
2497 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2498 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2500 bfd *output_bfd = data;
2502 h->root.u.def.value =
2503 _bfd_merged_section_offset (output_bfd,
2504 &h->root.u.def.section,
2505 elf_section_data (sec)->sec_info,
2506 h->root.u.def.value);
2509 return TRUE;
2512 /* Returns false if the symbol referred to by H should be considered
2513 to resolve local to the current module, and true if it should be
2514 considered to bind dynamically. */
2516 bfd_boolean
2517 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2518 struct bfd_link_info *info,
2519 bfd_boolean ignore_protected)
2521 bfd_boolean binding_stays_local_p;
2523 if (h == NULL)
2524 return FALSE;
2526 while (h->root.type == bfd_link_hash_indirect
2527 || h->root.type == bfd_link_hash_warning)
2528 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2530 /* If it was forced local, then clearly it's not dynamic. */
2531 if (h->dynindx == -1)
2532 return FALSE;
2533 if (h->forced_local)
2534 return FALSE;
2536 /* Identify the cases where name binding rules say that a
2537 visible symbol resolves locally. */
2538 binding_stays_local_p = info->executable || info->symbolic;
2540 switch (ELF_ST_VISIBILITY (h->other))
2542 case STV_INTERNAL:
2543 case STV_HIDDEN:
2544 return FALSE;
2546 case STV_PROTECTED:
2547 /* Proper resolution for function pointer equality may require
2548 that these symbols perhaps be resolved dynamically, even though
2549 we should be resolving them to the current module. */
2550 if (!ignore_protected || h->type != STT_FUNC)
2551 binding_stays_local_p = TRUE;
2552 break;
2554 default:
2555 break;
2558 /* If it isn't defined locally, then clearly it's dynamic. */
2559 if (!h->def_regular)
2560 return TRUE;
2562 /* Otherwise, the symbol is dynamic if binding rules don't tell
2563 us that it remains local. */
2564 return !binding_stays_local_p;
2567 /* Return true if the symbol referred to by H should be considered
2568 to resolve local to the current module, and false otherwise. Differs
2569 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2570 undefined symbols and weak symbols. */
2572 bfd_boolean
2573 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2574 struct bfd_link_info *info,
2575 bfd_boolean local_protected)
2577 /* If it's a local sym, of course we resolve locally. */
2578 if (h == NULL)
2579 return TRUE;
2581 /* Common symbols that become definitions don't get the DEF_REGULAR
2582 flag set, so test it first, and don't bail out. */
2583 if (ELF_COMMON_DEF_P (h))
2584 /* Do nothing. */;
2585 /* If we don't have a definition in a regular file, then we can't
2586 resolve locally. The sym is either undefined or dynamic. */
2587 else if (!h->def_regular)
2588 return FALSE;
2590 /* Forced local symbols resolve locally. */
2591 if (h->forced_local)
2592 return TRUE;
2594 /* As do non-dynamic symbols. */
2595 if (h->dynindx == -1)
2596 return TRUE;
2598 /* At this point, we know the symbol is defined and dynamic. In an
2599 executable it must resolve locally, likewise when building symbolic
2600 shared libraries. */
2601 if (info->executable || info->symbolic)
2602 return TRUE;
2604 /* Now deal with defined dynamic symbols in shared libraries. Ones
2605 with default visibility might not resolve locally. */
2606 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2607 return FALSE;
2609 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2610 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2611 return TRUE;
2613 /* STV_PROTECTED non-function symbols are local. */
2614 if (h->type != STT_FUNC)
2615 return TRUE;
2617 /* Function pointer equality tests may require that STV_PROTECTED
2618 symbols be treated as dynamic symbols, even when we know that the
2619 dynamic linker will resolve them locally. */
2620 return local_protected;
2623 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2624 aligned. Returns the first TLS output section. */
2626 struct bfd_section *
2627 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2629 struct bfd_section *sec, *tls;
2630 unsigned int align = 0;
2632 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2633 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2634 break;
2635 tls = sec;
2637 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2638 if (sec->alignment_power > align)
2639 align = sec->alignment_power;
2641 elf_hash_table (info)->tls_sec = tls;
2643 /* Ensure the alignment of the first section is the largest alignment,
2644 so that the tls segment starts aligned. */
2645 if (tls != NULL)
2646 tls->alignment_power = align;
2648 return tls;
2651 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2652 static bfd_boolean
2653 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2654 Elf_Internal_Sym *sym)
2656 const struct elf_backend_data *bed;
2658 /* Local symbols do not count, but target specific ones might. */
2659 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2660 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2661 return FALSE;
2663 /* Function symbols do not count. */
2664 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2665 return FALSE;
2667 /* If the section is undefined, then so is the symbol. */
2668 if (sym->st_shndx == SHN_UNDEF)
2669 return FALSE;
2671 /* If the symbol is defined in the common section, then
2672 it is a common definition and so does not count. */
2673 bed = get_elf_backend_data (abfd);
2674 if (bed->common_definition (sym))
2675 return FALSE;
2677 /* If the symbol is in a target specific section then we
2678 must rely upon the backend to tell us what it is. */
2679 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2680 /* FIXME - this function is not coded yet:
2682 return _bfd_is_global_symbol_definition (abfd, sym);
2684 Instead for now assume that the definition is not global,
2685 Even if this is wrong, at least the linker will behave
2686 in the same way that it used to do. */
2687 return FALSE;
2689 return TRUE;
2692 /* Search the symbol table of the archive element of the archive ABFD
2693 whose archive map contains a mention of SYMDEF, and determine if
2694 the symbol is defined in this element. */
2695 static bfd_boolean
2696 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2698 Elf_Internal_Shdr * hdr;
2699 bfd_size_type symcount;
2700 bfd_size_type extsymcount;
2701 bfd_size_type extsymoff;
2702 Elf_Internal_Sym *isymbuf;
2703 Elf_Internal_Sym *isym;
2704 Elf_Internal_Sym *isymend;
2705 bfd_boolean result;
2707 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2708 if (abfd == NULL)
2709 return FALSE;
2711 if (! bfd_check_format (abfd, bfd_object))
2712 return FALSE;
2714 /* If we have already included the element containing this symbol in the
2715 link then we do not need to include it again. Just claim that any symbol
2716 it contains is not a definition, so that our caller will not decide to
2717 (re)include this element. */
2718 if (abfd->archive_pass)
2719 return FALSE;
2721 /* Select the appropriate symbol table. */
2722 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2723 hdr = &elf_tdata (abfd)->symtab_hdr;
2724 else
2725 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2727 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2729 /* The sh_info field of the symtab header tells us where the
2730 external symbols start. We don't care about the local symbols. */
2731 if (elf_bad_symtab (abfd))
2733 extsymcount = symcount;
2734 extsymoff = 0;
2736 else
2738 extsymcount = symcount - hdr->sh_info;
2739 extsymoff = hdr->sh_info;
2742 if (extsymcount == 0)
2743 return FALSE;
2745 /* Read in the symbol table. */
2746 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2747 NULL, NULL, NULL);
2748 if (isymbuf == NULL)
2749 return FALSE;
2751 /* Scan the symbol table looking for SYMDEF. */
2752 result = FALSE;
2753 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2755 const char *name;
2757 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2758 isym->st_name);
2759 if (name == NULL)
2760 break;
2762 if (strcmp (name, symdef->name) == 0)
2764 result = is_global_data_symbol_definition (abfd, isym);
2765 break;
2769 free (isymbuf);
2771 return result;
2774 /* Add an entry to the .dynamic table. */
2776 bfd_boolean
2777 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2778 bfd_vma tag,
2779 bfd_vma val)
2781 struct elf_link_hash_table *hash_table;
2782 const struct elf_backend_data *bed;
2783 asection *s;
2784 bfd_size_type newsize;
2785 bfd_byte *newcontents;
2786 Elf_Internal_Dyn dyn;
2788 hash_table = elf_hash_table (info);
2789 if (! is_elf_hash_table (hash_table))
2790 return FALSE;
2792 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2793 _bfd_error_handler
2794 (_("warning: creating a DT_TEXTREL in a shared object."));
2796 bed = get_elf_backend_data (hash_table->dynobj);
2797 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2798 BFD_ASSERT (s != NULL);
2800 newsize = s->size + bed->s->sizeof_dyn;
2801 newcontents = bfd_realloc (s->contents, newsize);
2802 if (newcontents == NULL)
2803 return FALSE;
2805 dyn.d_tag = tag;
2806 dyn.d_un.d_val = val;
2807 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2809 s->size = newsize;
2810 s->contents = newcontents;
2812 return TRUE;
2815 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2816 otherwise just check whether one already exists. Returns -1 on error,
2817 1 if a DT_NEEDED tag already exists, and 0 on success. */
2819 static int
2820 elf_add_dt_needed_tag (bfd *abfd,
2821 struct bfd_link_info *info,
2822 const char *soname,
2823 bfd_boolean do_it)
2825 struct elf_link_hash_table *hash_table;
2826 bfd_size_type oldsize;
2827 bfd_size_type strindex;
2829 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2830 return -1;
2832 hash_table = elf_hash_table (info);
2833 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2834 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2835 if (strindex == (bfd_size_type) -1)
2836 return -1;
2838 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2840 asection *sdyn;
2841 const struct elf_backend_data *bed;
2842 bfd_byte *extdyn;
2844 bed = get_elf_backend_data (hash_table->dynobj);
2845 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2846 if (sdyn != NULL)
2847 for (extdyn = sdyn->contents;
2848 extdyn < sdyn->contents + sdyn->size;
2849 extdyn += bed->s->sizeof_dyn)
2851 Elf_Internal_Dyn dyn;
2853 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2854 if (dyn.d_tag == DT_NEEDED
2855 && dyn.d_un.d_val == strindex)
2857 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2858 return 1;
2863 if (do_it)
2865 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2866 return -1;
2868 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2869 return -1;
2871 else
2872 /* We were just checking for existence of the tag. */
2873 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2875 return 0;
2878 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2879 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2880 references from regular objects to these symbols.
2882 ??? Should we do something about references from other dynamic
2883 obects? If not, we potentially lose some warnings about undefined
2884 symbols. But how can we recover the initial undefined / undefweak
2885 state? */
2887 struct elf_smash_syms_data
2889 bfd *not_needed;
2890 struct elf_link_hash_table *htab;
2891 bfd_boolean twiddled;
2894 static bfd_boolean
2895 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2897 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2898 struct bfd_link_hash_entry *bh;
2900 switch (h->root.type)
2902 default:
2903 case bfd_link_hash_new:
2904 return TRUE;
2906 case bfd_link_hash_undefined:
2907 if (h->root.u.undef.abfd != inf->not_needed)
2908 return TRUE;
2909 if (h->root.u.undef.weak != NULL
2910 && h->root.u.undef.weak != inf->not_needed)
2912 /* Symbol was undefweak in u.undef.weak bfd, and has become
2913 undefined in as-needed lib. Restore weak. */
2914 h->root.type = bfd_link_hash_undefweak;
2915 h->root.u.undef.abfd = h->root.u.undef.weak;
2916 if (h->root.u.undef.next != NULL
2917 || inf->htab->root.undefs_tail == &h->root)
2918 inf->twiddled = TRUE;
2919 return TRUE;
2921 break;
2923 case bfd_link_hash_undefweak:
2924 if (h->root.u.undef.abfd != inf->not_needed)
2925 return TRUE;
2926 break;
2928 case bfd_link_hash_defined:
2929 case bfd_link_hash_defweak:
2930 if (h->root.u.def.section->owner != inf->not_needed)
2931 return TRUE;
2932 break;
2934 case bfd_link_hash_common:
2935 if (h->root.u.c.p->section->owner != inf->not_needed)
2936 return TRUE;
2937 break;
2939 case bfd_link_hash_warning:
2940 case bfd_link_hash_indirect:
2941 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2942 if (h->root.u.i.link->type != bfd_link_hash_new)
2943 return TRUE;
2944 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2945 return TRUE;
2946 break;
2949 /* There is no way we can undo symbol table state from defined or
2950 defweak back to undefined. */
2951 if (h->ref_regular)
2952 abort ();
2954 /* Set sym back to newly created state, but keep undef.next if it is
2955 being used as a list pointer. */
2956 bh = h->root.u.undef.next;
2957 if (bh == &h->root)
2958 bh = NULL;
2959 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2960 inf->twiddled = TRUE;
2961 (*inf->htab->root.table.newfunc) (&h->root.root,
2962 &inf->htab->root.table,
2963 h->root.root.string);
2964 h->root.u.undef.next = bh;
2965 h->root.u.undef.abfd = inf->not_needed;
2966 h->non_elf = 0;
2967 return TRUE;
2970 /* Sort symbol by value and section. */
2971 static int
2972 elf_sort_symbol (const void *arg1, const void *arg2)
2974 const struct elf_link_hash_entry *h1;
2975 const struct elf_link_hash_entry *h2;
2976 bfd_signed_vma vdiff;
2978 h1 = *(const struct elf_link_hash_entry **) arg1;
2979 h2 = *(const struct elf_link_hash_entry **) arg2;
2980 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2981 if (vdiff != 0)
2982 return vdiff > 0 ? 1 : -1;
2983 else
2985 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2986 if (sdiff != 0)
2987 return sdiff > 0 ? 1 : -1;
2989 return 0;
2992 /* This function is used to adjust offsets into .dynstr for
2993 dynamic symbols. This is called via elf_link_hash_traverse. */
2995 static bfd_boolean
2996 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2998 struct elf_strtab_hash *dynstr = data;
3000 if (h->root.type == bfd_link_hash_warning)
3001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3003 if (h->dynindx != -1)
3004 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3005 return TRUE;
3008 /* Assign string offsets in .dynstr, update all structures referencing
3009 them. */
3011 static bfd_boolean
3012 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3014 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3015 struct elf_link_local_dynamic_entry *entry;
3016 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3017 bfd *dynobj = hash_table->dynobj;
3018 asection *sdyn;
3019 bfd_size_type size;
3020 const struct elf_backend_data *bed;
3021 bfd_byte *extdyn;
3023 _bfd_elf_strtab_finalize (dynstr);
3024 size = _bfd_elf_strtab_size (dynstr);
3026 bed = get_elf_backend_data (dynobj);
3027 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3028 BFD_ASSERT (sdyn != NULL);
3030 /* Update all .dynamic entries referencing .dynstr strings. */
3031 for (extdyn = sdyn->contents;
3032 extdyn < sdyn->contents + sdyn->size;
3033 extdyn += bed->s->sizeof_dyn)
3035 Elf_Internal_Dyn dyn;
3037 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3038 switch (dyn.d_tag)
3040 case DT_STRSZ:
3041 dyn.d_un.d_val = size;
3042 break;
3043 case DT_NEEDED:
3044 case DT_SONAME:
3045 case DT_RPATH:
3046 case DT_RUNPATH:
3047 case DT_FILTER:
3048 case DT_AUXILIARY:
3049 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3050 break;
3051 default:
3052 continue;
3054 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3057 /* Now update local dynamic symbols. */
3058 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3059 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3060 entry->isym.st_name);
3062 /* And the rest of dynamic symbols. */
3063 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3065 /* Adjust version definitions. */
3066 if (elf_tdata (output_bfd)->cverdefs)
3068 asection *s;
3069 bfd_byte *p;
3070 bfd_size_type i;
3071 Elf_Internal_Verdef def;
3072 Elf_Internal_Verdaux defaux;
3074 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3075 p = s->contents;
3078 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3079 &def);
3080 p += sizeof (Elf_External_Verdef);
3081 if (def.vd_aux != sizeof (Elf_External_Verdef))
3082 continue;
3083 for (i = 0; i < def.vd_cnt; ++i)
3085 _bfd_elf_swap_verdaux_in (output_bfd,
3086 (Elf_External_Verdaux *) p, &defaux);
3087 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3088 defaux.vda_name);
3089 _bfd_elf_swap_verdaux_out (output_bfd,
3090 &defaux, (Elf_External_Verdaux *) p);
3091 p += sizeof (Elf_External_Verdaux);
3094 while (def.vd_next);
3097 /* Adjust version references. */
3098 if (elf_tdata (output_bfd)->verref)
3100 asection *s;
3101 bfd_byte *p;
3102 bfd_size_type i;
3103 Elf_Internal_Verneed need;
3104 Elf_Internal_Vernaux needaux;
3106 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3107 p = s->contents;
3110 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3111 &need);
3112 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3113 _bfd_elf_swap_verneed_out (output_bfd, &need,
3114 (Elf_External_Verneed *) p);
3115 p += sizeof (Elf_External_Verneed);
3116 for (i = 0; i < need.vn_cnt; ++i)
3118 _bfd_elf_swap_vernaux_in (output_bfd,
3119 (Elf_External_Vernaux *) p, &needaux);
3120 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3121 needaux.vna_name);
3122 _bfd_elf_swap_vernaux_out (output_bfd,
3123 &needaux,
3124 (Elf_External_Vernaux *) p);
3125 p += sizeof (Elf_External_Vernaux);
3128 while (need.vn_next);
3131 return TRUE;
3134 /* Add symbols from an ELF object file to the linker hash table. */
3136 static bfd_boolean
3137 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3139 bfd_boolean (*add_symbol_hook)
3140 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3141 const char **, flagword *, asection **, bfd_vma *);
3142 bfd_boolean (*check_relocs)
3143 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3144 bfd_boolean (*check_directives)
3145 (bfd *, struct bfd_link_info *);
3146 bfd_boolean collect;
3147 Elf_Internal_Shdr *hdr;
3148 bfd_size_type symcount;
3149 bfd_size_type extsymcount;
3150 bfd_size_type extsymoff;
3151 struct elf_link_hash_entry **sym_hash;
3152 bfd_boolean dynamic;
3153 Elf_External_Versym *extversym = NULL;
3154 Elf_External_Versym *ever;
3155 struct elf_link_hash_entry *weaks;
3156 struct elf_link_hash_entry **nondeflt_vers = NULL;
3157 bfd_size_type nondeflt_vers_cnt = 0;
3158 Elf_Internal_Sym *isymbuf = NULL;
3159 Elf_Internal_Sym *isym;
3160 Elf_Internal_Sym *isymend;
3161 const struct elf_backend_data *bed;
3162 bfd_boolean add_needed;
3163 struct elf_link_hash_table * hash_table;
3164 bfd_size_type amt;
3166 hash_table = elf_hash_table (info);
3168 bed = get_elf_backend_data (abfd);
3169 add_symbol_hook = bed->elf_add_symbol_hook;
3170 collect = bed->collect;
3172 if ((abfd->flags & DYNAMIC) == 0)
3173 dynamic = FALSE;
3174 else
3176 dynamic = TRUE;
3178 /* You can't use -r against a dynamic object. Also, there's no
3179 hope of using a dynamic object which does not exactly match
3180 the format of the output file. */
3181 if (info->relocatable
3182 || !is_elf_hash_table (hash_table)
3183 || hash_table->root.creator != abfd->xvec)
3185 if (info->relocatable)
3186 bfd_set_error (bfd_error_invalid_operation);
3187 else
3188 bfd_set_error (bfd_error_wrong_format);
3189 goto error_return;
3193 /* As a GNU extension, any input sections which are named
3194 .gnu.warning.SYMBOL are treated as warning symbols for the given
3195 symbol. This differs from .gnu.warning sections, which generate
3196 warnings when they are included in an output file. */
3197 if (info->executable)
3199 asection *s;
3201 for (s = abfd->sections; s != NULL; s = s->next)
3203 const char *name;
3205 name = bfd_get_section_name (abfd, s);
3206 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3208 char *msg;
3209 bfd_size_type sz;
3211 name += sizeof ".gnu.warning." - 1;
3213 /* If this is a shared object, then look up the symbol
3214 in the hash table. If it is there, and it is already
3215 been defined, then we will not be using the entry
3216 from this shared object, so we don't need to warn.
3217 FIXME: If we see the definition in a regular object
3218 later on, we will warn, but we shouldn't. The only
3219 fix is to keep track of what warnings we are supposed
3220 to emit, and then handle them all at the end of the
3221 link. */
3222 if (dynamic)
3224 struct elf_link_hash_entry *h;
3226 h = elf_link_hash_lookup (hash_table, name,
3227 FALSE, FALSE, TRUE);
3229 /* FIXME: What about bfd_link_hash_common? */
3230 if (h != NULL
3231 && (h->root.type == bfd_link_hash_defined
3232 || h->root.type == bfd_link_hash_defweak))
3234 /* We don't want to issue this warning. Clobber
3235 the section size so that the warning does not
3236 get copied into the output file. */
3237 s->size = 0;
3238 continue;
3242 sz = s->size;
3243 msg = bfd_alloc (abfd, sz + 1);
3244 if (msg == NULL)
3245 goto error_return;
3247 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3248 goto error_return;
3250 msg[sz] = '\0';
3252 if (! (_bfd_generic_link_add_one_symbol
3253 (info, abfd, name, BSF_WARNING, s, 0, msg,
3254 FALSE, collect, NULL)))
3255 goto error_return;
3257 if (! info->relocatable)
3259 /* Clobber the section size so that the warning does
3260 not get copied into the output file. */
3261 s->size = 0;
3263 /* Also set SEC_EXCLUDE, so that symbols defined in
3264 the warning section don't get copied to the output. */
3265 s->flags |= SEC_EXCLUDE;
3271 add_needed = TRUE;
3272 if (! dynamic)
3274 /* If we are creating a shared library, create all the dynamic
3275 sections immediately. We need to attach them to something,
3276 so we attach them to this BFD, provided it is the right
3277 format. FIXME: If there are no input BFD's of the same
3278 format as the output, we can't make a shared library. */
3279 if (info->shared
3280 && is_elf_hash_table (hash_table)
3281 && hash_table->root.creator == abfd->xvec
3282 && ! hash_table->dynamic_sections_created)
3284 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3285 goto error_return;
3288 else if (!is_elf_hash_table (hash_table))
3289 goto error_return;
3290 else
3292 asection *s;
3293 const char *soname = NULL;
3294 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3295 int ret;
3297 /* ld --just-symbols and dynamic objects don't mix very well.
3298 ld shouldn't allow it. */
3299 if ((s = abfd->sections) != NULL
3300 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3301 abort ();
3303 /* If this dynamic lib was specified on the command line with
3304 --as-needed in effect, then we don't want to add a DT_NEEDED
3305 tag unless the lib is actually used. Similary for libs brought
3306 in by another lib's DT_NEEDED. When --no-add-needed is used
3307 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3308 any dynamic library in DT_NEEDED tags in the dynamic lib at
3309 all. */
3310 add_needed = (elf_dyn_lib_class (abfd)
3311 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3312 | DYN_NO_NEEDED)) == 0;
3314 s = bfd_get_section_by_name (abfd, ".dynamic");
3315 if (s != NULL)
3317 bfd_byte *dynbuf;
3318 bfd_byte *extdyn;
3319 int elfsec;
3320 unsigned long shlink;
3322 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3323 goto error_free_dyn;
3325 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3326 if (elfsec == -1)
3327 goto error_free_dyn;
3328 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3330 for (extdyn = dynbuf;
3331 extdyn < dynbuf + s->size;
3332 extdyn += bed->s->sizeof_dyn)
3334 Elf_Internal_Dyn dyn;
3336 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3337 if (dyn.d_tag == DT_SONAME)
3339 unsigned int tagv = dyn.d_un.d_val;
3340 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3341 if (soname == NULL)
3342 goto error_free_dyn;
3344 if (dyn.d_tag == DT_NEEDED)
3346 struct bfd_link_needed_list *n, **pn;
3347 char *fnm, *anm;
3348 unsigned int tagv = dyn.d_un.d_val;
3350 amt = sizeof (struct bfd_link_needed_list);
3351 n = bfd_alloc (abfd, amt);
3352 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3353 if (n == NULL || fnm == NULL)
3354 goto error_free_dyn;
3355 amt = strlen (fnm) + 1;
3356 anm = bfd_alloc (abfd, amt);
3357 if (anm == NULL)
3358 goto error_free_dyn;
3359 memcpy (anm, fnm, amt);
3360 n->name = anm;
3361 n->by = abfd;
3362 n->next = NULL;
3363 for (pn = & hash_table->needed;
3364 *pn != NULL;
3365 pn = &(*pn)->next)
3367 *pn = n;
3369 if (dyn.d_tag == DT_RUNPATH)
3371 struct bfd_link_needed_list *n, **pn;
3372 char *fnm, *anm;
3373 unsigned int tagv = dyn.d_un.d_val;
3375 amt = sizeof (struct bfd_link_needed_list);
3376 n = bfd_alloc (abfd, amt);
3377 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3378 if (n == NULL || fnm == NULL)
3379 goto error_free_dyn;
3380 amt = strlen (fnm) + 1;
3381 anm = bfd_alloc (abfd, amt);
3382 if (anm == NULL)
3383 goto error_free_dyn;
3384 memcpy (anm, fnm, amt);
3385 n->name = anm;
3386 n->by = abfd;
3387 n->next = NULL;
3388 for (pn = & runpath;
3389 *pn != NULL;
3390 pn = &(*pn)->next)
3392 *pn = n;
3394 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3395 if (!runpath && dyn.d_tag == DT_RPATH)
3397 struct bfd_link_needed_list *n, **pn;
3398 char *fnm, *anm;
3399 unsigned int tagv = dyn.d_un.d_val;
3401 amt = sizeof (struct bfd_link_needed_list);
3402 n = bfd_alloc (abfd, amt);
3403 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3404 if (n == NULL || fnm == NULL)
3405 goto error_free_dyn;
3406 amt = strlen (fnm) + 1;
3407 anm = bfd_alloc (abfd, amt);
3408 if (anm == NULL)
3410 error_free_dyn:
3411 free (dynbuf);
3412 goto error_return;
3414 memcpy (anm, fnm, amt);
3415 n->name = anm;
3416 n->by = abfd;
3417 n->next = NULL;
3418 for (pn = & rpath;
3419 *pn != NULL;
3420 pn = &(*pn)->next)
3422 *pn = n;
3426 free (dynbuf);
3429 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3430 frees all more recently bfd_alloc'd blocks as well. */
3431 if (runpath)
3432 rpath = runpath;
3434 if (rpath)
3436 struct bfd_link_needed_list **pn;
3437 for (pn = & hash_table->runpath;
3438 *pn != NULL;
3439 pn = &(*pn)->next)
3441 *pn = rpath;
3444 /* We do not want to include any of the sections in a dynamic
3445 object in the output file. We hack by simply clobbering the
3446 list of sections in the BFD. This could be handled more
3447 cleanly by, say, a new section flag; the existing
3448 SEC_NEVER_LOAD flag is not the one we want, because that one
3449 still implies that the section takes up space in the output
3450 file. */
3451 bfd_section_list_clear (abfd);
3453 /* Find the name to use in a DT_NEEDED entry that refers to this
3454 object. If the object has a DT_SONAME entry, we use it.
3455 Otherwise, if the generic linker stuck something in
3456 elf_dt_name, we use that. Otherwise, we just use the file
3457 name. */
3458 if (soname == NULL || *soname == '\0')
3460 soname = elf_dt_name (abfd);
3461 if (soname == NULL || *soname == '\0')
3462 soname = bfd_get_filename (abfd);
3465 /* Save the SONAME because sometimes the linker emulation code
3466 will need to know it. */
3467 elf_dt_name (abfd) = soname;
3469 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3470 if (ret < 0)
3471 goto error_return;
3473 /* If we have already included this dynamic object in the
3474 link, just ignore it. There is no reason to include a
3475 particular dynamic object more than once. */
3476 if (ret > 0)
3477 return TRUE;
3480 /* If this is a dynamic object, we always link against the .dynsym
3481 symbol table, not the .symtab symbol table. The dynamic linker
3482 will only see the .dynsym symbol table, so there is no reason to
3483 look at .symtab for a dynamic object. */
3485 if (! dynamic || elf_dynsymtab (abfd) == 0)
3486 hdr = &elf_tdata (abfd)->symtab_hdr;
3487 else
3488 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3490 symcount = hdr->sh_size / bed->s->sizeof_sym;
3492 /* The sh_info field of the symtab header tells us where the
3493 external symbols start. We don't care about the local symbols at
3494 this point. */
3495 if (elf_bad_symtab (abfd))
3497 extsymcount = symcount;
3498 extsymoff = 0;
3500 else
3502 extsymcount = symcount - hdr->sh_info;
3503 extsymoff = hdr->sh_info;
3506 sym_hash = NULL;
3507 if (extsymcount != 0)
3509 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3510 NULL, NULL, NULL);
3511 if (isymbuf == NULL)
3512 goto error_return;
3514 /* We store a pointer to the hash table entry for each external
3515 symbol. */
3516 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3517 sym_hash = bfd_alloc (abfd, amt);
3518 if (sym_hash == NULL)
3519 goto error_free_sym;
3520 elf_sym_hashes (abfd) = sym_hash;
3523 if (dynamic)
3525 /* Read in any version definitions. */
3526 if (!_bfd_elf_slurp_version_tables (abfd,
3527 info->default_imported_symver))
3528 goto error_free_sym;
3530 /* Read in the symbol versions, but don't bother to convert them
3531 to internal format. */
3532 if (elf_dynversym (abfd) != 0)
3534 Elf_Internal_Shdr *versymhdr;
3536 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3537 extversym = bfd_malloc (versymhdr->sh_size);
3538 if (extversym == NULL)
3539 goto error_free_sym;
3540 amt = versymhdr->sh_size;
3541 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3542 || bfd_bread (extversym, amt, abfd) != amt)
3543 goto error_free_vers;
3547 weaks = NULL;
3549 ever = extversym != NULL ? extversym + extsymoff : NULL;
3550 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3551 isym < isymend;
3552 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3554 int bind;
3555 bfd_vma value;
3556 asection *sec, *new_sec;
3557 flagword flags;
3558 const char *name;
3559 struct elf_link_hash_entry *h;
3560 bfd_boolean definition;
3561 bfd_boolean size_change_ok;
3562 bfd_boolean type_change_ok;
3563 bfd_boolean new_weakdef;
3564 bfd_boolean override;
3565 bfd_boolean common;
3566 unsigned int old_alignment;
3567 bfd *old_bfd;
3569 override = FALSE;
3571 flags = BSF_NO_FLAGS;
3572 sec = NULL;
3573 value = isym->st_value;
3574 *sym_hash = NULL;
3575 common = bed->common_definition (isym);
3577 bind = ELF_ST_BIND (isym->st_info);
3578 if (bind == STB_LOCAL)
3580 /* This should be impossible, since ELF requires that all
3581 global symbols follow all local symbols, and that sh_info
3582 point to the first global symbol. Unfortunately, Irix 5
3583 screws this up. */
3584 continue;
3586 else if (bind == STB_GLOBAL)
3588 if (isym->st_shndx != SHN_UNDEF && !common)
3589 flags = BSF_GLOBAL;
3591 else if (bind == STB_WEAK)
3592 flags = BSF_WEAK;
3593 else
3595 /* Leave it up to the processor backend. */
3598 if (isym->st_shndx == SHN_UNDEF)
3599 sec = bfd_und_section_ptr;
3600 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3602 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3603 if (sec == NULL)
3604 sec = bfd_abs_section_ptr;
3605 else if (sec->kept_section)
3607 /* Symbols from discarded section are undefined, and have
3608 default visibility. */
3609 sec = bfd_und_section_ptr;
3610 isym->st_shndx = SHN_UNDEF;
3611 isym->st_other = STV_DEFAULT
3612 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3614 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3615 value -= sec->vma;
3617 else if (isym->st_shndx == SHN_ABS)
3618 sec = bfd_abs_section_ptr;
3619 else if (isym->st_shndx == SHN_COMMON)
3621 sec = bfd_com_section_ptr;
3622 /* What ELF calls the size we call the value. What ELF
3623 calls the value we call the alignment. */
3624 value = isym->st_size;
3626 else
3628 /* Leave it up to the processor backend. */
3631 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3632 isym->st_name);
3633 if (name == NULL)
3634 goto error_free_vers;
3636 if (isym->st_shndx == SHN_COMMON
3637 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3639 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3641 if (tcomm == NULL)
3643 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3644 (SEC_ALLOC
3645 | SEC_IS_COMMON
3646 | SEC_LINKER_CREATED
3647 | SEC_THREAD_LOCAL));
3648 if (tcomm == NULL)
3649 goto error_free_vers;
3651 sec = tcomm;
3653 else if (add_symbol_hook)
3655 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3656 &value))
3657 goto error_free_vers;
3659 /* The hook function sets the name to NULL if this symbol
3660 should be skipped for some reason. */
3661 if (name == NULL)
3662 continue;
3665 /* Sanity check that all possibilities were handled. */
3666 if (sec == NULL)
3668 bfd_set_error (bfd_error_bad_value);
3669 goto error_free_vers;
3672 if (bfd_is_und_section (sec)
3673 || bfd_is_com_section (sec))
3674 definition = FALSE;
3675 else
3676 definition = TRUE;
3678 size_change_ok = FALSE;
3679 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3680 old_alignment = 0;
3681 old_bfd = NULL;
3682 new_sec = sec;
3684 if (is_elf_hash_table (hash_table))
3686 Elf_Internal_Versym iver;
3687 unsigned int vernum = 0;
3688 bfd_boolean skip;
3690 if (ever == NULL)
3692 if (info->default_imported_symver)
3693 /* Use the default symbol version created earlier. */
3694 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3695 else
3696 iver.vs_vers = 0;
3698 else
3699 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3701 vernum = iver.vs_vers & VERSYM_VERSION;
3703 /* If this is a hidden symbol, or if it is not version
3704 1, we append the version name to the symbol name.
3705 However, we do not modify a non-hidden absolute symbol
3706 if it is not a function, because it might be the version
3707 symbol itself. FIXME: What if it isn't? */
3708 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3709 || (vernum > 1 && (! bfd_is_abs_section (sec)
3710 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3712 const char *verstr;
3713 size_t namelen, verlen, newlen;
3714 char *newname, *p;
3716 if (isym->st_shndx != SHN_UNDEF)
3718 if (vernum > elf_tdata (abfd)->cverdefs)
3719 verstr = NULL;
3720 else if (vernum > 1)
3721 verstr =
3722 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3723 else
3724 verstr = "";
3726 if (verstr == NULL)
3728 (*_bfd_error_handler)
3729 (_("%B: %s: invalid version %u (max %d)"),
3730 abfd, name, vernum,
3731 elf_tdata (abfd)->cverdefs);
3732 bfd_set_error (bfd_error_bad_value);
3733 goto error_free_vers;
3736 else
3738 /* We cannot simply test for the number of
3739 entries in the VERNEED section since the
3740 numbers for the needed versions do not start
3741 at 0. */
3742 Elf_Internal_Verneed *t;
3744 verstr = NULL;
3745 for (t = elf_tdata (abfd)->verref;
3746 t != NULL;
3747 t = t->vn_nextref)
3749 Elf_Internal_Vernaux *a;
3751 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3753 if (a->vna_other == vernum)
3755 verstr = a->vna_nodename;
3756 break;
3759 if (a != NULL)
3760 break;
3762 if (verstr == NULL)
3764 (*_bfd_error_handler)
3765 (_("%B: %s: invalid needed version %d"),
3766 abfd, name, vernum);
3767 bfd_set_error (bfd_error_bad_value);
3768 goto error_free_vers;
3772 namelen = strlen (name);
3773 verlen = strlen (verstr);
3774 newlen = namelen + verlen + 2;
3775 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3776 && isym->st_shndx != SHN_UNDEF)
3777 ++newlen;
3779 newname = bfd_alloc (abfd, newlen);
3780 if (newname == NULL)
3781 goto error_free_vers;
3782 memcpy (newname, name, namelen);
3783 p = newname + namelen;
3784 *p++ = ELF_VER_CHR;
3785 /* If this is a defined non-hidden version symbol,
3786 we add another @ to the name. This indicates the
3787 default version of the symbol. */
3788 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3789 && isym->st_shndx != SHN_UNDEF)
3790 *p++ = ELF_VER_CHR;
3791 memcpy (p, verstr, verlen + 1);
3793 name = newname;
3796 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3797 &value, &old_alignment,
3798 sym_hash, &skip, &override,
3799 &type_change_ok, &size_change_ok))
3800 goto error_free_vers;
3802 if (skip)
3803 continue;
3805 if (override)
3806 definition = FALSE;
3808 h = *sym_hash;
3809 while (h->root.type == bfd_link_hash_indirect
3810 || h->root.type == bfd_link_hash_warning)
3811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3813 /* Remember the old alignment if this is a common symbol, so
3814 that we don't reduce the alignment later on. We can't
3815 check later, because _bfd_generic_link_add_one_symbol
3816 will set a default for the alignment which we want to
3817 override. We also remember the old bfd where the existing
3818 definition comes from. */
3819 switch (h->root.type)
3821 default:
3822 break;
3824 case bfd_link_hash_defined:
3825 case bfd_link_hash_defweak:
3826 old_bfd = h->root.u.def.section->owner;
3827 break;
3829 case bfd_link_hash_common:
3830 old_bfd = h->root.u.c.p->section->owner;
3831 old_alignment = h->root.u.c.p->alignment_power;
3832 break;
3835 if (elf_tdata (abfd)->verdef != NULL
3836 && ! override
3837 && vernum > 1
3838 && definition)
3839 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3842 if (! (_bfd_generic_link_add_one_symbol
3843 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3844 (struct bfd_link_hash_entry **) sym_hash)))
3845 goto error_free_vers;
3847 h = *sym_hash;
3848 while (h->root.type == bfd_link_hash_indirect
3849 || h->root.type == bfd_link_hash_warning)
3850 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3851 *sym_hash = h;
3853 new_weakdef = FALSE;
3854 if (dynamic
3855 && definition
3856 && (flags & BSF_WEAK) != 0
3857 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3858 && is_elf_hash_table (hash_table)
3859 && h->u.weakdef == NULL)
3861 /* Keep a list of all weak defined non function symbols from
3862 a dynamic object, using the weakdef field. Later in this
3863 function we will set the weakdef field to the correct
3864 value. We only put non-function symbols from dynamic
3865 objects on this list, because that happens to be the only
3866 time we need to know the normal symbol corresponding to a
3867 weak symbol, and the information is time consuming to
3868 figure out. If the weakdef field is not already NULL,
3869 then this symbol was already defined by some previous
3870 dynamic object, and we will be using that previous
3871 definition anyhow. */
3873 h->u.weakdef = weaks;
3874 weaks = h;
3875 new_weakdef = TRUE;
3878 /* Set the alignment of a common symbol. */
3879 if ((common || bfd_is_com_section (sec))
3880 && h->root.type == bfd_link_hash_common)
3882 unsigned int align;
3884 if (common)
3885 align = bfd_log2 (isym->st_value);
3886 else
3888 /* The new symbol is a common symbol in a shared object.
3889 We need to get the alignment from the section. */
3890 align = new_sec->alignment_power;
3892 if (align > old_alignment
3893 /* Permit an alignment power of zero if an alignment of one
3894 is specified and no other alignments have been specified. */
3895 || (isym->st_value == 1 && old_alignment == 0))
3896 h->root.u.c.p->alignment_power = align;
3897 else
3898 h->root.u.c.p->alignment_power = old_alignment;
3901 if (is_elf_hash_table (hash_table))
3903 bfd_boolean dynsym;
3905 /* Check the alignment when a common symbol is involved. This
3906 can change when a common symbol is overridden by a normal
3907 definition or a common symbol is ignored due to the old
3908 normal definition. We need to make sure the maximum
3909 alignment is maintained. */
3910 if ((old_alignment || common)
3911 && h->root.type != bfd_link_hash_common)
3913 unsigned int common_align;
3914 unsigned int normal_align;
3915 unsigned int symbol_align;
3916 bfd *normal_bfd;
3917 bfd *common_bfd;
3919 symbol_align = ffs (h->root.u.def.value) - 1;
3920 if (h->root.u.def.section->owner != NULL
3921 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3923 normal_align = h->root.u.def.section->alignment_power;
3924 if (normal_align > symbol_align)
3925 normal_align = symbol_align;
3927 else
3928 normal_align = symbol_align;
3930 if (old_alignment)
3932 common_align = old_alignment;
3933 common_bfd = old_bfd;
3934 normal_bfd = abfd;
3936 else
3938 common_align = bfd_log2 (isym->st_value);
3939 common_bfd = abfd;
3940 normal_bfd = old_bfd;
3943 if (normal_align < common_align)
3944 (*_bfd_error_handler)
3945 (_("Warning: alignment %u of symbol `%s' in %B"
3946 " is smaller than %u in %B"),
3947 normal_bfd, common_bfd,
3948 1 << normal_align, name, 1 << common_align);
3951 /* Remember the symbol size and type. */
3952 if (isym->st_size != 0
3953 && (definition || h->size == 0))
3955 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3956 (*_bfd_error_handler)
3957 (_("Warning: size of symbol `%s' changed"
3958 " from %lu in %B to %lu in %B"),
3959 old_bfd, abfd,
3960 name, (unsigned long) h->size,
3961 (unsigned long) isym->st_size);
3963 h->size = isym->st_size;
3966 /* If this is a common symbol, then we always want H->SIZE
3967 to be the size of the common symbol. The code just above
3968 won't fix the size if a common symbol becomes larger. We
3969 don't warn about a size change here, because that is
3970 covered by --warn-common. */
3971 if (h->root.type == bfd_link_hash_common)
3972 h->size = h->root.u.c.size;
3974 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3975 && (definition || h->type == STT_NOTYPE))
3977 if (h->type != STT_NOTYPE
3978 && h->type != ELF_ST_TYPE (isym->st_info)
3979 && ! type_change_ok)
3980 (*_bfd_error_handler)
3981 (_("Warning: type of symbol `%s' changed"
3982 " from %d to %d in %B"),
3983 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3985 h->type = ELF_ST_TYPE (isym->st_info);
3988 /* If st_other has a processor-specific meaning, specific
3989 code might be needed here. We never merge the visibility
3990 attribute with the one from a dynamic object. */
3991 if (bed->elf_backend_merge_symbol_attribute)
3992 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3993 dynamic);
3995 /* If this symbol has default visibility and the user has requested
3996 we not re-export it, then mark it as hidden. */
3997 if (definition && !dynamic
3998 && (abfd->no_export
3999 || (abfd->my_archive && abfd->my_archive->no_export))
4000 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4001 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
4003 if (isym->st_other != 0 && !dynamic)
4005 unsigned char hvis, symvis, other, nvis;
4007 /* Take the balance of OTHER from the definition. */
4008 other = (definition ? isym->st_other : h->other);
4009 other &= ~ ELF_ST_VISIBILITY (-1);
4011 /* Combine visibilities, using the most constraining one. */
4012 hvis = ELF_ST_VISIBILITY (h->other);
4013 symvis = ELF_ST_VISIBILITY (isym->st_other);
4014 if (! hvis)
4015 nvis = symvis;
4016 else if (! symvis)
4017 nvis = hvis;
4018 else
4019 nvis = hvis < symvis ? hvis : symvis;
4021 h->other = other | nvis;
4024 /* Set a flag in the hash table entry indicating the type of
4025 reference or definition we just found. Keep a count of
4026 the number of dynamic symbols we find. A dynamic symbol
4027 is one which is referenced or defined by both a regular
4028 object and a shared object. */
4029 dynsym = FALSE;
4030 if (! dynamic)
4032 if (! definition)
4034 h->ref_regular = 1;
4035 if (bind != STB_WEAK)
4036 h->ref_regular_nonweak = 1;
4038 else
4039 h->def_regular = 1;
4040 if (! info->executable
4041 || h->def_dynamic
4042 || h->ref_dynamic)
4043 dynsym = TRUE;
4045 else
4047 if (! definition)
4048 h->ref_dynamic = 1;
4049 else
4050 h->def_dynamic = 1;
4051 if (h->def_regular
4052 || h->ref_regular
4053 || (h->u.weakdef != NULL
4054 && ! new_weakdef
4055 && h->u.weakdef->dynindx != -1))
4056 dynsym = TRUE;
4059 /* Check to see if we need to add an indirect symbol for
4060 the default name. */
4061 if (definition || h->root.type == bfd_link_hash_common)
4062 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4063 &sec, &value, &dynsym,
4064 override))
4065 goto error_free_vers;
4067 if (definition && !dynamic)
4069 char *p = strchr (name, ELF_VER_CHR);
4070 if (p != NULL && p[1] != ELF_VER_CHR)
4072 /* Queue non-default versions so that .symver x, x@FOO
4073 aliases can be checked. */
4074 if (! nondeflt_vers)
4076 amt = (isymend - isym + 1)
4077 * sizeof (struct elf_link_hash_entry *);
4078 nondeflt_vers = bfd_malloc (amt);
4080 nondeflt_vers [nondeflt_vers_cnt++] = h;
4084 if (dynsym && h->dynindx == -1)
4086 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4087 goto error_free_vers;
4088 if (h->u.weakdef != NULL
4089 && ! new_weakdef
4090 && h->u.weakdef->dynindx == -1)
4092 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4093 goto error_free_vers;
4096 else if (dynsym && h->dynindx != -1)
4097 /* If the symbol already has a dynamic index, but
4098 visibility says it should not be visible, turn it into
4099 a local symbol. */
4100 switch (ELF_ST_VISIBILITY (h->other))
4102 case STV_INTERNAL:
4103 case STV_HIDDEN:
4104 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4105 dynsym = FALSE;
4106 break;
4109 if (!add_needed
4110 && definition
4111 && dynsym
4112 && h->ref_regular)
4114 int ret;
4115 const char *soname = elf_dt_name (abfd);
4117 /* A symbol from a library loaded via DT_NEEDED of some
4118 other library is referenced by a regular object.
4119 Add a DT_NEEDED entry for it. Issue an error if
4120 --no-add-needed is used. */
4121 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4123 (*_bfd_error_handler)
4124 (_("%s: invalid DSO for symbol `%s' definition"),
4125 abfd, name);
4126 bfd_set_error (bfd_error_bad_value);
4127 goto error_free_vers;
4130 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4132 add_needed = TRUE;
4133 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4134 if (ret < 0)
4135 goto error_free_vers;
4137 BFD_ASSERT (ret == 0);
4142 /* Now that all the symbols from this input file are created, handle
4143 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4144 if (nondeflt_vers != NULL)
4146 bfd_size_type cnt, symidx;
4148 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4150 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4151 char *shortname, *p;
4153 p = strchr (h->root.root.string, ELF_VER_CHR);
4154 if (p == NULL
4155 || (h->root.type != bfd_link_hash_defined
4156 && h->root.type != bfd_link_hash_defweak))
4157 continue;
4159 amt = p - h->root.root.string;
4160 shortname = bfd_malloc (amt + 1);
4161 memcpy (shortname, h->root.root.string, amt);
4162 shortname[amt] = '\0';
4164 hi = (struct elf_link_hash_entry *)
4165 bfd_link_hash_lookup (&hash_table->root, shortname,
4166 FALSE, FALSE, FALSE);
4167 if (hi != NULL
4168 && hi->root.type == h->root.type
4169 && hi->root.u.def.value == h->root.u.def.value
4170 && hi->root.u.def.section == h->root.u.def.section)
4172 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4173 hi->root.type = bfd_link_hash_indirect;
4174 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4175 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4176 sym_hash = elf_sym_hashes (abfd);
4177 if (sym_hash)
4178 for (symidx = 0; symidx < extsymcount; ++symidx)
4179 if (sym_hash[symidx] == hi)
4181 sym_hash[symidx] = h;
4182 break;
4185 free (shortname);
4187 free (nondeflt_vers);
4188 nondeflt_vers = NULL;
4191 if (extversym != NULL)
4193 free (extversym);
4194 extversym = NULL;
4197 if (isymbuf != NULL)
4198 free (isymbuf);
4199 isymbuf = NULL;
4201 if (!add_needed
4202 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4204 /* Remove symbols defined in an as-needed shared lib that wasn't
4205 needed. */
4206 struct elf_smash_syms_data inf;
4207 inf.not_needed = abfd;
4208 inf.htab = hash_table;
4209 inf.twiddled = FALSE;
4210 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4211 if (inf.twiddled)
4212 bfd_link_repair_undef_list (&hash_table->root);
4213 weaks = NULL;
4216 /* Now set the weakdefs field correctly for all the weak defined
4217 symbols we found. The only way to do this is to search all the
4218 symbols. Since we only need the information for non functions in
4219 dynamic objects, that's the only time we actually put anything on
4220 the list WEAKS. We need this information so that if a regular
4221 object refers to a symbol defined weakly in a dynamic object, the
4222 real symbol in the dynamic object is also put in the dynamic
4223 symbols; we also must arrange for both symbols to point to the
4224 same memory location. We could handle the general case of symbol
4225 aliasing, but a general symbol alias can only be generated in
4226 assembler code, handling it correctly would be very time
4227 consuming, and other ELF linkers don't handle general aliasing
4228 either. */
4229 if (weaks != NULL)
4231 struct elf_link_hash_entry **hpp;
4232 struct elf_link_hash_entry **hppend;
4233 struct elf_link_hash_entry **sorted_sym_hash;
4234 struct elf_link_hash_entry *h;
4235 size_t sym_count;
4237 /* Since we have to search the whole symbol list for each weak
4238 defined symbol, search time for N weak defined symbols will be
4239 O(N^2). Binary search will cut it down to O(NlogN). */
4240 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4241 sorted_sym_hash = bfd_malloc (amt);
4242 if (sorted_sym_hash == NULL)
4243 goto error_return;
4244 sym_hash = sorted_sym_hash;
4245 hpp = elf_sym_hashes (abfd);
4246 hppend = hpp + extsymcount;
4247 sym_count = 0;
4248 for (; hpp < hppend; hpp++)
4250 h = *hpp;
4251 if (h != NULL
4252 && h->root.type == bfd_link_hash_defined
4253 && h->type != STT_FUNC)
4255 *sym_hash = h;
4256 sym_hash++;
4257 sym_count++;
4261 qsort (sorted_sym_hash, sym_count,
4262 sizeof (struct elf_link_hash_entry *),
4263 elf_sort_symbol);
4265 while (weaks != NULL)
4267 struct elf_link_hash_entry *hlook;
4268 asection *slook;
4269 bfd_vma vlook;
4270 long ilook;
4271 size_t i, j, idx;
4273 hlook = weaks;
4274 weaks = hlook->u.weakdef;
4275 hlook->u.weakdef = NULL;
4277 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4278 || hlook->root.type == bfd_link_hash_defweak
4279 || hlook->root.type == bfd_link_hash_common
4280 || hlook->root.type == bfd_link_hash_indirect);
4281 slook = hlook->root.u.def.section;
4282 vlook = hlook->root.u.def.value;
4284 ilook = -1;
4285 i = 0;
4286 j = sym_count;
4287 while (i < j)
4289 bfd_signed_vma vdiff;
4290 idx = (i + j) / 2;
4291 h = sorted_sym_hash [idx];
4292 vdiff = vlook - h->root.u.def.value;
4293 if (vdiff < 0)
4294 j = idx;
4295 else if (vdiff > 0)
4296 i = idx + 1;
4297 else
4299 long sdiff = slook->id - h->root.u.def.section->id;
4300 if (sdiff < 0)
4301 j = idx;
4302 else if (sdiff > 0)
4303 i = idx + 1;
4304 else
4306 ilook = idx;
4307 break;
4312 /* We didn't find a value/section match. */
4313 if (ilook == -1)
4314 continue;
4316 for (i = ilook; i < sym_count; i++)
4318 h = sorted_sym_hash [i];
4320 /* Stop if value or section doesn't match. */
4321 if (h->root.u.def.value != vlook
4322 || h->root.u.def.section != slook)
4323 break;
4324 else if (h != hlook)
4326 hlook->u.weakdef = h;
4328 /* If the weak definition is in the list of dynamic
4329 symbols, make sure the real definition is put
4330 there as well. */
4331 if (hlook->dynindx != -1 && h->dynindx == -1)
4333 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4334 goto error_return;
4337 /* If the real definition is in the list of dynamic
4338 symbols, make sure the weak definition is put
4339 there as well. If we don't do this, then the
4340 dynamic loader might not merge the entries for the
4341 real definition and the weak definition. */
4342 if (h->dynindx != -1 && hlook->dynindx == -1)
4344 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4345 goto error_return;
4347 break;
4352 free (sorted_sym_hash);
4355 check_directives = get_elf_backend_data (abfd)->check_directives;
4356 if (check_directives)
4357 check_directives (abfd, info);
4359 /* If this object is the same format as the output object, and it is
4360 not a shared library, then let the backend look through the
4361 relocs.
4363 This is required to build global offset table entries and to
4364 arrange for dynamic relocs. It is not required for the
4365 particular common case of linking non PIC code, even when linking
4366 against shared libraries, but unfortunately there is no way of
4367 knowing whether an object file has been compiled PIC or not.
4368 Looking through the relocs is not particularly time consuming.
4369 The problem is that we must either (1) keep the relocs in memory,
4370 which causes the linker to require additional runtime memory or
4371 (2) read the relocs twice from the input file, which wastes time.
4372 This would be a good case for using mmap.
4374 I have no idea how to handle linking PIC code into a file of a
4375 different format. It probably can't be done. */
4376 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4377 if (! dynamic
4378 && is_elf_hash_table (hash_table)
4379 && hash_table->root.creator == abfd->xvec
4380 && check_relocs != NULL)
4382 asection *o;
4384 for (o = abfd->sections; o != NULL; o = o->next)
4386 Elf_Internal_Rela *internal_relocs;
4387 bfd_boolean ok;
4389 if ((o->flags & SEC_RELOC) == 0
4390 || o->reloc_count == 0
4391 || ((info->strip == strip_all || info->strip == strip_debugger)
4392 && (o->flags & SEC_DEBUGGING) != 0)
4393 || bfd_is_abs_section (o->output_section))
4394 continue;
4396 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4397 info->keep_memory);
4398 if (internal_relocs == NULL)
4399 goto error_return;
4401 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4403 if (elf_section_data (o)->relocs != internal_relocs)
4404 free (internal_relocs);
4406 if (! ok)
4407 goto error_return;
4411 /* If this is a non-traditional link, try to optimize the handling
4412 of the .stab/.stabstr sections. */
4413 if (! dynamic
4414 && ! info->traditional_format
4415 && is_elf_hash_table (hash_table)
4416 && (info->strip != strip_all && info->strip != strip_debugger))
4418 asection *stabstr;
4420 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4421 if (stabstr != NULL)
4423 bfd_size_type string_offset = 0;
4424 asection *stab;
4426 for (stab = abfd->sections; stab; stab = stab->next)
4427 if (strncmp (".stab", stab->name, 5) == 0
4428 && (!stab->name[5] ||
4429 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4430 && (stab->flags & SEC_MERGE) == 0
4431 && !bfd_is_abs_section (stab->output_section))
4433 struct bfd_elf_section_data *secdata;
4435 secdata = elf_section_data (stab);
4436 if (! _bfd_link_section_stabs (abfd,
4437 &hash_table->stab_info,
4438 stab, stabstr,
4439 &secdata->sec_info,
4440 &string_offset))
4441 goto error_return;
4442 if (secdata->sec_info)
4443 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4448 if (is_elf_hash_table (hash_table) && add_needed)
4450 /* Add this bfd to the loaded list. */
4451 struct elf_link_loaded_list *n;
4453 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4454 if (n == NULL)
4455 goto error_return;
4456 n->abfd = abfd;
4457 n->next = hash_table->loaded;
4458 hash_table->loaded = n;
4461 return TRUE;
4463 error_free_vers:
4464 if (nondeflt_vers != NULL)
4465 free (nondeflt_vers);
4466 if (extversym != NULL)
4467 free (extversym);
4468 error_free_sym:
4469 if (isymbuf != NULL)
4470 free (isymbuf);
4471 error_return:
4472 return FALSE;
4475 /* Return the linker hash table entry of a symbol that might be
4476 satisfied by an archive symbol. Return -1 on error. */
4478 struct elf_link_hash_entry *
4479 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4480 struct bfd_link_info *info,
4481 const char *name)
4483 struct elf_link_hash_entry *h;
4484 char *p, *copy;
4485 size_t len, first;
4487 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4488 if (h != NULL)
4489 return h;
4491 /* If this is a default version (the name contains @@), look up the
4492 symbol again with only one `@' as well as without the version.
4493 The effect is that references to the symbol with and without the
4494 version will be matched by the default symbol in the archive. */
4496 p = strchr (name, ELF_VER_CHR);
4497 if (p == NULL || p[1] != ELF_VER_CHR)
4498 return h;
4500 /* First check with only one `@'. */
4501 len = strlen (name);
4502 copy = bfd_alloc (abfd, len);
4503 if (copy == NULL)
4504 return (struct elf_link_hash_entry *) 0 - 1;
4506 first = p - name + 1;
4507 memcpy (copy, name, first);
4508 memcpy (copy + first, name + first + 1, len - first);
4510 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4511 if (h == NULL)
4513 /* We also need to check references to the symbol without the
4514 version. */
4515 copy[first - 1] = '\0';
4516 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4517 FALSE, FALSE, FALSE);
4520 bfd_release (abfd, copy);
4521 return h;
4524 /* Add symbols from an ELF archive file to the linker hash table. We
4525 don't use _bfd_generic_link_add_archive_symbols because of a
4526 problem which arises on UnixWare. The UnixWare libc.so is an
4527 archive which includes an entry libc.so.1 which defines a bunch of
4528 symbols. The libc.so archive also includes a number of other
4529 object files, which also define symbols, some of which are the same
4530 as those defined in libc.so.1. Correct linking requires that we
4531 consider each object file in turn, and include it if it defines any
4532 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4533 this; it looks through the list of undefined symbols, and includes
4534 any object file which defines them. When this algorithm is used on
4535 UnixWare, it winds up pulling in libc.so.1 early and defining a
4536 bunch of symbols. This means that some of the other objects in the
4537 archive are not included in the link, which is incorrect since they
4538 precede libc.so.1 in the archive.
4540 Fortunately, ELF archive handling is simpler than that done by
4541 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4542 oddities. In ELF, if we find a symbol in the archive map, and the
4543 symbol is currently undefined, we know that we must pull in that
4544 object file.
4546 Unfortunately, we do have to make multiple passes over the symbol
4547 table until nothing further is resolved. */
4549 static bfd_boolean
4550 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4552 symindex c;
4553 bfd_boolean *defined = NULL;
4554 bfd_boolean *included = NULL;
4555 carsym *symdefs;
4556 bfd_boolean loop;
4557 bfd_size_type amt;
4558 const struct elf_backend_data *bed;
4559 struct elf_link_hash_entry * (*archive_symbol_lookup)
4560 (bfd *, struct bfd_link_info *, const char *);
4562 if (! bfd_has_map (abfd))
4564 /* An empty archive is a special case. */
4565 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4566 return TRUE;
4567 bfd_set_error (bfd_error_no_armap);
4568 return FALSE;
4571 /* Keep track of all symbols we know to be already defined, and all
4572 files we know to be already included. This is to speed up the
4573 second and subsequent passes. */
4574 c = bfd_ardata (abfd)->symdef_count;
4575 if (c == 0)
4576 return TRUE;
4577 amt = c;
4578 amt *= sizeof (bfd_boolean);
4579 defined = bfd_zmalloc (amt);
4580 included = bfd_zmalloc (amt);
4581 if (defined == NULL || included == NULL)
4582 goto error_return;
4584 symdefs = bfd_ardata (abfd)->symdefs;
4585 bed = get_elf_backend_data (abfd);
4586 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4590 file_ptr last;
4591 symindex i;
4592 carsym *symdef;
4593 carsym *symdefend;
4595 loop = FALSE;
4596 last = -1;
4598 symdef = symdefs;
4599 symdefend = symdef + c;
4600 for (i = 0; symdef < symdefend; symdef++, i++)
4602 struct elf_link_hash_entry *h;
4603 bfd *element;
4604 struct bfd_link_hash_entry *undefs_tail;
4605 symindex mark;
4607 if (defined[i] || included[i])
4608 continue;
4609 if (symdef->file_offset == last)
4611 included[i] = TRUE;
4612 continue;
4615 h = archive_symbol_lookup (abfd, info, symdef->name);
4616 if (h == (struct elf_link_hash_entry *) 0 - 1)
4617 goto error_return;
4619 if (h == NULL)
4620 continue;
4622 if (h->root.type == bfd_link_hash_common)
4624 /* We currently have a common symbol. The archive map contains
4625 a reference to this symbol, so we may want to include it. We
4626 only want to include it however, if this archive element
4627 contains a definition of the symbol, not just another common
4628 declaration of it.
4630 Unfortunately some archivers (including GNU ar) will put
4631 declarations of common symbols into their archive maps, as
4632 well as real definitions, so we cannot just go by the archive
4633 map alone. Instead we must read in the element's symbol
4634 table and check that to see what kind of symbol definition
4635 this is. */
4636 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4637 continue;
4639 else if (h->root.type != bfd_link_hash_undefined)
4641 if (h->root.type != bfd_link_hash_undefweak)
4642 defined[i] = TRUE;
4643 continue;
4646 /* We need to include this archive member. */
4647 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4648 if (element == NULL)
4649 goto error_return;
4651 if (! bfd_check_format (element, bfd_object))
4652 goto error_return;
4654 /* Doublecheck that we have not included this object
4655 already--it should be impossible, but there may be
4656 something wrong with the archive. */
4657 if (element->archive_pass != 0)
4659 bfd_set_error (bfd_error_bad_value);
4660 goto error_return;
4662 element->archive_pass = 1;
4664 undefs_tail = info->hash->undefs_tail;
4666 if (! (*info->callbacks->add_archive_element) (info, element,
4667 symdef->name))
4668 goto error_return;
4669 if (! bfd_link_add_symbols (element, info))
4670 goto error_return;
4672 /* If there are any new undefined symbols, we need to make
4673 another pass through the archive in order to see whether
4674 they can be defined. FIXME: This isn't perfect, because
4675 common symbols wind up on undefs_tail and because an
4676 undefined symbol which is defined later on in this pass
4677 does not require another pass. This isn't a bug, but it
4678 does make the code less efficient than it could be. */
4679 if (undefs_tail != info->hash->undefs_tail)
4680 loop = TRUE;
4682 /* Look backward to mark all symbols from this object file
4683 which we have already seen in this pass. */
4684 mark = i;
4687 included[mark] = TRUE;
4688 if (mark == 0)
4689 break;
4690 --mark;
4692 while (symdefs[mark].file_offset == symdef->file_offset);
4694 /* We mark subsequent symbols from this object file as we go
4695 on through the loop. */
4696 last = symdef->file_offset;
4699 while (loop);
4701 free (defined);
4702 free (included);
4704 return TRUE;
4706 error_return:
4707 if (defined != NULL)
4708 free (defined);
4709 if (included != NULL)
4710 free (included);
4711 return FALSE;
4714 /* Given an ELF BFD, add symbols to the global hash table as
4715 appropriate. */
4717 bfd_boolean
4718 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4720 switch (bfd_get_format (abfd))
4722 case bfd_object:
4723 return elf_link_add_object_symbols (abfd, info);
4724 case bfd_archive:
4725 return elf_link_add_archive_symbols (abfd, info);
4726 default:
4727 bfd_set_error (bfd_error_wrong_format);
4728 return FALSE;
4732 /* This function will be called though elf_link_hash_traverse to store
4733 all hash value of the exported symbols in an array. */
4735 static bfd_boolean
4736 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4738 unsigned long **valuep = data;
4739 const char *name;
4740 char *p;
4741 unsigned long ha;
4742 char *alc = NULL;
4744 if (h->root.type == bfd_link_hash_warning)
4745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4747 /* Ignore indirect symbols. These are added by the versioning code. */
4748 if (h->dynindx == -1)
4749 return TRUE;
4751 name = h->root.root.string;
4752 p = strchr (name, ELF_VER_CHR);
4753 if (p != NULL)
4755 alc = bfd_malloc (p - name + 1);
4756 memcpy (alc, name, p - name);
4757 alc[p - name] = '\0';
4758 name = alc;
4761 /* Compute the hash value. */
4762 ha = bfd_elf_hash (name);
4764 /* Store the found hash value in the array given as the argument. */
4765 *(*valuep)++ = ha;
4767 /* And store it in the struct so that we can put it in the hash table
4768 later. */
4769 h->u.elf_hash_value = ha;
4771 if (alc != NULL)
4772 free (alc);
4774 return TRUE;
4777 /* Array used to determine the number of hash table buckets to use
4778 based on the number of symbols there are. If there are fewer than
4779 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4780 fewer than 37 we use 17 buckets, and so forth. We never use more
4781 than 32771 buckets. */
4783 static const size_t elf_buckets[] =
4785 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4786 16411, 32771, 0
4789 /* Compute bucket count for hashing table. We do not use a static set
4790 of possible tables sizes anymore. Instead we determine for all
4791 possible reasonable sizes of the table the outcome (i.e., the
4792 number of collisions etc) and choose the best solution. The
4793 weighting functions are not too simple to allow the table to grow
4794 without bounds. Instead one of the weighting factors is the size.
4795 Therefore the result is always a good payoff between few collisions
4796 (= short chain lengths) and table size. */
4797 static size_t
4798 compute_bucket_count (struct bfd_link_info *info)
4800 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4801 size_t best_size = 0;
4802 unsigned long int *hashcodes;
4803 unsigned long int *hashcodesp;
4804 unsigned long int i;
4805 bfd_size_type amt;
4807 /* Compute the hash values for all exported symbols. At the same
4808 time store the values in an array so that we could use them for
4809 optimizations. */
4810 amt = dynsymcount;
4811 amt *= sizeof (unsigned long int);
4812 hashcodes = bfd_malloc (amt);
4813 if (hashcodes == NULL)
4814 return 0;
4815 hashcodesp = hashcodes;
4817 /* Put all hash values in HASHCODES. */
4818 elf_link_hash_traverse (elf_hash_table (info),
4819 elf_collect_hash_codes, &hashcodesp);
4821 /* We have a problem here. The following code to optimize the table
4822 size requires an integer type with more the 32 bits. If
4823 BFD_HOST_U_64_BIT is set we know about such a type. */
4824 #ifdef BFD_HOST_U_64_BIT
4825 if (info->optimize)
4827 unsigned long int nsyms = hashcodesp - hashcodes;
4828 size_t minsize;
4829 size_t maxsize;
4830 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4831 unsigned long int *counts ;
4832 bfd *dynobj = elf_hash_table (info)->dynobj;
4833 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4835 /* Possible optimization parameters: if we have NSYMS symbols we say
4836 that the hashing table must at least have NSYMS/4 and at most
4837 2*NSYMS buckets. */
4838 minsize = nsyms / 4;
4839 if (minsize == 0)
4840 minsize = 1;
4841 best_size = maxsize = nsyms * 2;
4843 /* Create array where we count the collisions in. We must use bfd_malloc
4844 since the size could be large. */
4845 amt = maxsize;
4846 amt *= sizeof (unsigned long int);
4847 counts = bfd_malloc (amt);
4848 if (counts == NULL)
4850 free (hashcodes);
4851 return 0;
4854 /* Compute the "optimal" size for the hash table. The criteria is a
4855 minimal chain length. The minor criteria is (of course) the size
4856 of the table. */
4857 for (i = minsize; i < maxsize; ++i)
4859 /* Walk through the array of hashcodes and count the collisions. */
4860 BFD_HOST_U_64_BIT max;
4861 unsigned long int j;
4862 unsigned long int fact;
4864 memset (counts, '\0', i * sizeof (unsigned long int));
4866 /* Determine how often each hash bucket is used. */
4867 for (j = 0; j < nsyms; ++j)
4868 ++counts[hashcodes[j] % i];
4870 /* For the weight function we need some information about the
4871 pagesize on the target. This is information need not be 100%
4872 accurate. Since this information is not available (so far) we
4873 define it here to a reasonable default value. If it is crucial
4874 to have a better value some day simply define this value. */
4875 # ifndef BFD_TARGET_PAGESIZE
4876 # define BFD_TARGET_PAGESIZE (4096)
4877 # endif
4879 /* We in any case need 2 + NSYMS entries for the size values and
4880 the chains. */
4881 max = (2 + nsyms) * (bed->s->arch_size / 8);
4883 # if 1
4884 /* Variant 1: optimize for short chains. We add the squares
4885 of all the chain lengths (which favors many small chain
4886 over a few long chains). */
4887 for (j = 0; j < i; ++j)
4888 max += counts[j] * counts[j];
4890 /* This adds penalties for the overall size of the table. */
4891 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4892 max *= fact * fact;
4893 # else
4894 /* Variant 2: Optimize a lot more for small table. Here we
4895 also add squares of the size but we also add penalties for
4896 empty slots (the +1 term). */
4897 for (j = 0; j < i; ++j)
4898 max += (1 + counts[j]) * (1 + counts[j]);
4900 /* The overall size of the table is considered, but not as
4901 strong as in variant 1, where it is squared. */
4902 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4903 max *= fact;
4904 # endif
4906 /* Compare with current best results. */
4907 if (max < best_chlen)
4909 best_chlen = max;
4910 best_size = i;
4914 free (counts);
4916 else
4917 #endif /* defined (BFD_HOST_U_64_BIT) */
4919 /* This is the fallback solution if no 64bit type is available or if we
4920 are not supposed to spend much time on optimizations. We select the
4921 bucket count using a fixed set of numbers. */
4922 for (i = 0; elf_buckets[i] != 0; i++)
4924 best_size = elf_buckets[i];
4925 if (dynsymcount < elf_buckets[i + 1])
4926 break;
4930 /* Free the arrays we needed. */
4931 free (hashcodes);
4933 return best_size;
4936 /* Set up the sizes and contents of the ELF dynamic sections. This is
4937 called by the ELF linker emulation before_allocation routine. We
4938 must set the sizes of the sections before the linker sets the
4939 addresses of the various sections. */
4941 bfd_boolean
4942 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4943 const char *soname,
4944 const char *rpath,
4945 const char *filter_shlib,
4946 const char * const *auxiliary_filters,
4947 struct bfd_link_info *info,
4948 asection **sinterpptr,
4949 struct bfd_elf_version_tree *verdefs)
4951 bfd_size_type soname_indx;
4952 bfd *dynobj;
4953 const struct elf_backend_data *bed;
4954 struct elf_assign_sym_version_info asvinfo;
4956 *sinterpptr = NULL;
4958 soname_indx = (bfd_size_type) -1;
4960 if (!is_elf_hash_table (info->hash))
4961 return TRUE;
4963 elf_tdata (output_bfd)->relro = info->relro;
4964 if (info->execstack)
4965 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4966 else if (info->noexecstack)
4967 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4968 else
4970 bfd *inputobj;
4971 asection *notesec = NULL;
4972 int exec = 0;
4974 for (inputobj = info->input_bfds;
4975 inputobj;
4976 inputobj = inputobj->link_next)
4978 asection *s;
4980 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4981 continue;
4982 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4983 if (s)
4985 if (s->flags & SEC_CODE)
4986 exec = PF_X;
4987 notesec = s;
4989 else
4990 exec = PF_X;
4992 if (notesec)
4994 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4995 if (exec && info->relocatable
4996 && notesec->output_section != bfd_abs_section_ptr)
4997 notesec->output_section->flags |= SEC_CODE;
5001 /* Any syms created from now on start with -1 in
5002 got.refcount/offset and plt.refcount/offset. */
5003 elf_hash_table (info)->init_got_refcount
5004 = elf_hash_table (info)->init_got_offset;
5005 elf_hash_table (info)->init_plt_refcount
5006 = elf_hash_table (info)->init_plt_offset;
5008 /* The backend may have to create some sections regardless of whether
5009 we're dynamic or not. */
5010 bed = get_elf_backend_data (output_bfd);
5011 if (bed->elf_backend_always_size_sections
5012 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5013 return FALSE;
5015 dynobj = elf_hash_table (info)->dynobj;
5017 /* If there were no dynamic objects in the link, there is nothing to
5018 do here. */
5019 if (dynobj == NULL)
5020 return TRUE;
5022 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5023 return FALSE;
5025 if (elf_hash_table (info)->dynamic_sections_created)
5027 struct elf_info_failed eif;
5028 struct elf_link_hash_entry *h;
5029 asection *dynstr;
5030 struct bfd_elf_version_tree *t;
5031 struct bfd_elf_version_expr *d;
5032 asection *s;
5033 bfd_boolean all_defined;
5035 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5036 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5038 if (soname != NULL)
5040 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5041 soname, TRUE);
5042 if (soname_indx == (bfd_size_type) -1
5043 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5044 return FALSE;
5047 if (info->symbolic)
5049 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5050 return FALSE;
5051 info->flags |= DF_SYMBOLIC;
5054 if (rpath != NULL)
5056 bfd_size_type indx;
5058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5059 TRUE);
5060 if (indx == (bfd_size_type) -1
5061 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5062 return FALSE;
5064 if (info->new_dtags)
5066 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5067 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5068 return FALSE;
5072 if (filter_shlib != NULL)
5074 bfd_size_type indx;
5076 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5077 filter_shlib, TRUE);
5078 if (indx == (bfd_size_type) -1
5079 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5080 return FALSE;
5083 if (auxiliary_filters != NULL)
5085 const char * const *p;
5087 for (p = auxiliary_filters; *p != NULL; p++)
5089 bfd_size_type indx;
5091 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5092 *p, TRUE);
5093 if (indx == (bfd_size_type) -1
5094 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5095 return FALSE;
5099 eif.info = info;
5100 eif.verdefs = verdefs;
5101 eif.failed = FALSE;
5103 /* If we are supposed to export all symbols into the dynamic symbol
5104 table (this is not the normal case), then do so. */
5105 if (info->export_dynamic)
5107 elf_link_hash_traverse (elf_hash_table (info),
5108 _bfd_elf_export_symbol,
5109 &eif);
5110 if (eif.failed)
5111 return FALSE;
5114 /* Make all global versions with definition. */
5115 for (t = verdefs; t != NULL; t = t->next)
5116 for (d = t->globals.list; d != NULL; d = d->next)
5117 if (!d->symver && d->symbol)
5119 const char *verstr, *name;
5120 size_t namelen, verlen, newlen;
5121 char *newname, *p;
5122 struct elf_link_hash_entry *newh;
5124 name = d->symbol;
5125 namelen = strlen (name);
5126 verstr = t->name;
5127 verlen = strlen (verstr);
5128 newlen = namelen + verlen + 3;
5130 newname = bfd_malloc (newlen);
5131 if (newname == NULL)
5132 return FALSE;
5133 memcpy (newname, name, namelen);
5135 /* Check the hidden versioned definition. */
5136 p = newname + namelen;
5137 *p++ = ELF_VER_CHR;
5138 memcpy (p, verstr, verlen + 1);
5139 newh = elf_link_hash_lookup (elf_hash_table (info),
5140 newname, FALSE, FALSE,
5141 FALSE);
5142 if (newh == NULL
5143 || (newh->root.type != bfd_link_hash_defined
5144 && newh->root.type != bfd_link_hash_defweak))
5146 /* Check the default versioned definition. */
5147 *p++ = ELF_VER_CHR;
5148 memcpy (p, verstr, verlen + 1);
5149 newh = elf_link_hash_lookup (elf_hash_table (info),
5150 newname, FALSE, FALSE,
5151 FALSE);
5153 free (newname);
5155 /* Mark this version if there is a definition and it is
5156 not defined in a shared object. */
5157 if (newh != NULL
5158 && !newh->def_dynamic
5159 && (newh->root.type == bfd_link_hash_defined
5160 || newh->root.type == bfd_link_hash_defweak))
5161 d->symver = 1;
5164 /* Attach all the symbols to their version information. */
5165 asvinfo.output_bfd = output_bfd;
5166 asvinfo.info = info;
5167 asvinfo.verdefs = verdefs;
5168 asvinfo.failed = FALSE;
5170 elf_link_hash_traverse (elf_hash_table (info),
5171 _bfd_elf_link_assign_sym_version,
5172 &asvinfo);
5173 if (asvinfo.failed)
5174 return FALSE;
5176 if (!info->allow_undefined_version)
5178 /* Check if all global versions have a definition. */
5179 all_defined = TRUE;
5180 for (t = verdefs; t != NULL; t = t->next)
5181 for (d = t->globals.list; d != NULL; d = d->next)
5182 if (!d->symver && !d->script)
5184 (*_bfd_error_handler)
5185 (_("%s: undefined version: %s"),
5186 d->pattern, t->name);
5187 all_defined = FALSE;
5190 if (!all_defined)
5192 bfd_set_error (bfd_error_bad_value);
5193 return FALSE;
5197 /* Find all symbols which were defined in a dynamic object and make
5198 the backend pick a reasonable value for them. */
5199 elf_link_hash_traverse (elf_hash_table (info),
5200 _bfd_elf_adjust_dynamic_symbol,
5201 &eif);
5202 if (eif.failed)
5203 return FALSE;
5205 /* Add some entries to the .dynamic section. We fill in some of the
5206 values later, in bfd_elf_final_link, but we must add the entries
5207 now so that we know the final size of the .dynamic section. */
5209 /* If there are initialization and/or finalization functions to
5210 call then add the corresponding DT_INIT/DT_FINI entries. */
5211 h = (info->init_function
5212 ? elf_link_hash_lookup (elf_hash_table (info),
5213 info->init_function, FALSE,
5214 FALSE, FALSE)
5215 : NULL);
5216 if (h != NULL
5217 && (h->ref_regular
5218 || h->def_regular))
5220 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5221 return FALSE;
5223 h = (info->fini_function
5224 ? elf_link_hash_lookup (elf_hash_table (info),
5225 info->fini_function, FALSE,
5226 FALSE, FALSE)
5227 : NULL);
5228 if (h != NULL
5229 && (h->ref_regular
5230 || h->def_regular))
5232 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5233 return FALSE;
5236 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5237 if (s != NULL && s->linker_has_input)
5239 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5240 if (! info->executable)
5242 bfd *sub;
5243 asection *o;
5245 for (sub = info->input_bfds; sub != NULL;
5246 sub = sub->link_next)
5247 for (o = sub->sections; o != NULL; o = o->next)
5248 if (elf_section_data (o)->this_hdr.sh_type
5249 == SHT_PREINIT_ARRAY)
5251 (*_bfd_error_handler)
5252 (_("%B: .preinit_array section is not allowed in DSO"),
5253 sub);
5254 break;
5257 bfd_set_error (bfd_error_nonrepresentable_section);
5258 return FALSE;
5261 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5262 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5263 return FALSE;
5265 s = bfd_get_section_by_name (output_bfd, ".init_array");
5266 if (s != NULL && s->linker_has_input)
5268 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5269 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5270 return FALSE;
5272 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5273 if (s != NULL && s->linker_has_input)
5275 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5276 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5277 return FALSE;
5280 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5281 /* If .dynstr is excluded from the link, we don't want any of
5282 these tags. Strictly, we should be checking each section
5283 individually; This quick check covers for the case where
5284 someone does a /DISCARD/ : { *(*) }. */
5285 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5287 bfd_size_type strsize;
5289 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5290 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5291 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5292 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5293 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5294 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5295 bed->s->sizeof_sym))
5296 return FALSE;
5300 /* The backend must work out the sizes of all the other dynamic
5301 sections. */
5302 if (bed->elf_backend_size_dynamic_sections
5303 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5304 return FALSE;
5306 if (elf_hash_table (info)->dynamic_sections_created)
5308 unsigned long section_sym_count;
5309 asection *s;
5311 /* Set up the version definition section. */
5312 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5313 BFD_ASSERT (s != NULL);
5315 /* We may have created additional version definitions if we are
5316 just linking a regular application. */
5317 verdefs = asvinfo.verdefs;
5319 /* Skip anonymous version tag. */
5320 if (verdefs != NULL && verdefs->vernum == 0)
5321 verdefs = verdefs->next;
5323 if (verdefs == NULL && !info->create_default_symver)
5324 s->flags |= SEC_EXCLUDE;
5325 else
5327 unsigned int cdefs;
5328 bfd_size_type size;
5329 struct bfd_elf_version_tree *t;
5330 bfd_byte *p;
5331 Elf_Internal_Verdef def;
5332 Elf_Internal_Verdaux defaux;
5333 struct bfd_link_hash_entry *bh;
5334 struct elf_link_hash_entry *h;
5335 const char *name;
5337 cdefs = 0;
5338 size = 0;
5340 /* Make space for the base version. */
5341 size += sizeof (Elf_External_Verdef);
5342 size += sizeof (Elf_External_Verdaux);
5343 ++cdefs;
5345 /* Make space for the default version. */
5346 if (info->create_default_symver)
5348 size += sizeof (Elf_External_Verdef);
5349 ++cdefs;
5352 for (t = verdefs; t != NULL; t = t->next)
5354 struct bfd_elf_version_deps *n;
5356 size += sizeof (Elf_External_Verdef);
5357 size += sizeof (Elf_External_Verdaux);
5358 ++cdefs;
5360 for (n = t->deps; n != NULL; n = n->next)
5361 size += sizeof (Elf_External_Verdaux);
5364 s->size = size;
5365 s->contents = bfd_alloc (output_bfd, s->size);
5366 if (s->contents == NULL && s->size != 0)
5367 return FALSE;
5369 /* Fill in the version definition section. */
5371 p = s->contents;
5373 def.vd_version = VER_DEF_CURRENT;
5374 def.vd_flags = VER_FLG_BASE;
5375 def.vd_ndx = 1;
5376 def.vd_cnt = 1;
5377 if (info->create_default_symver)
5379 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5380 def.vd_next = sizeof (Elf_External_Verdef);
5382 else
5384 def.vd_aux = sizeof (Elf_External_Verdef);
5385 def.vd_next = (sizeof (Elf_External_Verdef)
5386 + sizeof (Elf_External_Verdaux));
5389 if (soname_indx != (bfd_size_type) -1)
5391 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5392 soname_indx);
5393 def.vd_hash = bfd_elf_hash (soname);
5394 defaux.vda_name = soname_indx;
5395 name = soname;
5397 else
5399 bfd_size_type indx;
5401 name = lbasename (output_bfd->filename);
5402 def.vd_hash = bfd_elf_hash (name);
5403 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5404 name, FALSE);
5405 if (indx == (bfd_size_type) -1)
5406 return FALSE;
5407 defaux.vda_name = indx;
5409 defaux.vda_next = 0;
5411 _bfd_elf_swap_verdef_out (output_bfd, &def,
5412 (Elf_External_Verdef *) p);
5413 p += sizeof (Elf_External_Verdef);
5414 if (info->create_default_symver)
5416 /* Add a symbol representing this version. */
5417 bh = NULL;
5418 if (! (_bfd_generic_link_add_one_symbol
5419 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5420 0, NULL, FALSE,
5421 get_elf_backend_data (dynobj)->collect, &bh)))
5422 return FALSE;
5423 h = (struct elf_link_hash_entry *) bh;
5424 h->non_elf = 0;
5425 h->def_regular = 1;
5426 h->type = STT_OBJECT;
5427 h->verinfo.vertree = NULL;
5429 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5430 return FALSE;
5432 /* Create a duplicate of the base version with the same
5433 aux block, but different flags. */
5434 def.vd_flags = 0;
5435 def.vd_ndx = 2;
5436 def.vd_aux = sizeof (Elf_External_Verdef);
5437 if (verdefs)
5438 def.vd_next = (sizeof (Elf_External_Verdef)
5439 + sizeof (Elf_External_Verdaux));
5440 else
5441 def.vd_next = 0;
5442 _bfd_elf_swap_verdef_out (output_bfd, &def,
5443 (Elf_External_Verdef *) p);
5444 p += sizeof (Elf_External_Verdef);
5446 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5447 (Elf_External_Verdaux *) p);
5448 p += sizeof (Elf_External_Verdaux);
5450 for (t = verdefs; t != NULL; t = t->next)
5452 unsigned int cdeps;
5453 struct bfd_elf_version_deps *n;
5455 cdeps = 0;
5456 for (n = t->deps; n != NULL; n = n->next)
5457 ++cdeps;
5459 /* Add a symbol representing this version. */
5460 bh = NULL;
5461 if (! (_bfd_generic_link_add_one_symbol
5462 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5463 0, NULL, FALSE,
5464 get_elf_backend_data (dynobj)->collect, &bh)))
5465 return FALSE;
5466 h = (struct elf_link_hash_entry *) bh;
5467 h->non_elf = 0;
5468 h->def_regular = 1;
5469 h->type = STT_OBJECT;
5470 h->verinfo.vertree = t;
5472 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5473 return FALSE;
5475 def.vd_version = VER_DEF_CURRENT;
5476 def.vd_flags = 0;
5477 if (t->globals.list == NULL
5478 && t->locals.list == NULL
5479 && ! t->used)
5480 def.vd_flags |= VER_FLG_WEAK;
5481 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5482 def.vd_cnt = cdeps + 1;
5483 def.vd_hash = bfd_elf_hash (t->name);
5484 def.vd_aux = sizeof (Elf_External_Verdef);
5485 def.vd_next = 0;
5486 if (t->next != NULL)
5487 def.vd_next = (sizeof (Elf_External_Verdef)
5488 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5490 _bfd_elf_swap_verdef_out (output_bfd, &def,
5491 (Elf_External_Verdef *) p);
5492 p += sizeof (Elf_External_Verdef);
5494 defaux.vda_name = h->dynstr_index;
5495 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5496 h->dynstr_index);
5497 defaux.vda_next = 0;
5498 if (t->deps != NULL)
5499 defaux.vda_next = sizeof (Elf_External_Verdaux);
5500 t->name_indx = defaux.vda_name;
5502 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5503 (Elf_External_Verdaux *) p);
5504 p += sizeof (Elf_External_Verdaux);
5506 for (n = t->deps; n != NULL; n = n->next)
5508 if (n->version_needed == NULL)
5510 /* This can happen if there was an error in the
5511 version script. */
5512 defaux.vda_name = 0;
5514 else
5516 defaux.vda_name = n->version_needed->name_indx;
5517 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5518 defaux.vda_name);
5520 if (n->next == NULL)
5521 defaux.vda_next = 0;
5522 else
5523 defaux.vda_next = sizeof (Elf_External_Verdaux);
5525 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5526 (Elf_External_Verdaux *) p);
5527 p += sizeof (Elf_External_Verdaux);
5531 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5532 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5533 return FALSE;
5535 elf_tdata (output_bfd)->cverdefs = cdefs;
5538 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5540 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5541 return FALSE;
5543 else if (info->flags & DF_BIND_NOW)
5545 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5546 return FALSE;
5549 if (info->flags_1)
5551 if (info->executable)
5552 info->flags_1 &= ~ (DF_1_INITFIRST
5553 | DF_1_NODELETE
5554 | DF_1_NOOPEN);
5555 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5556 return FALSE;
5559 /* Work out the size of the version reference section. */
5561 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5562 BFD_ASSERT (s != NULL);
5564 struct elf_find_verdep_info sinfo;
5566 sinfo.output_bfd = output_bfd;
5567 sinfo.info = info;
5568 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5569 if (sinfo.vers == 0)
5570 sinfo.vers = 1;
5571 sinfo.failed = FALSE;
5573 elf_link_hash_traverse (elf_hash_table (info),
5574 _bfd_elf_link_find_version_dependencies,
5575 &sinfo);
5577 if (elf_tdata (output_bfd)->verref == NULL)
5578 s->flags |= SEC_EXCLUDE;
5579 else
5581 Elf_Internal_Verneed *t;
5582 unsigned int size;
5583 unsigned int crefs;
5584 bfd_byte *p;
5586 /* Build the version definition section. */
5587 size = 0;
5588 crefs = 0;
5589 for (t = elf_tdata (output_bfd)->verref;
5590 t != NULL;
5591 t = t->vn_nextref)
5593 Elf_Internal_Vernaux *a;
5595 size += sizeof (Elf_External_Verneed);
5596 ++crefs;
5597 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5598 size += sizeof (Elf_External_Vernaux);
5601 s->size = size;
5602 s->contents = bfd_alloc (output_bfd, s->size);
5603 if (s->contents == NULL)
5604 return FALSE;
5606 p = s->contents;
5607 for (t = elf_tdata (output_bfd)->verref;
5608 t != NULL;
5609 t = t->vn_nextref)
5611 unsigned int caux;
5612 Elf_Internal_Vernaux *a;
5613 bfd_size_type indx;
5615 caux = 0;
5616 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5617 ++caux;
5619 t->vn_version = VER_NEED_CURRENT;
5620 t->vn_cnt = caux;
5621 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5622 elf_dt_name (t->vn_bfd) != NULL
5623 ? elf_dt_name (t->vn_bfd)
5624 : lbasename (t->vn_bfd->filename),
5625 FALSE);
5626 if (indx == (bfd_size_type) -1)
5627 return FALSE;
5628 t->vn_file = indx;
5629 t->vn_aux = sizeof (Elf_External_Verneed);
5630 if (t->vn_nextref == NULL)
5631 t->vn_next = 0;
5632 else
5633 t->vn_next = (sizeof (Elf_External_Verneed)
5634 + caux * sizeof (Elf_External_Vernaux));
5636 _bfd_elf_swap_verneed_out (output_bfd, t,
5637 (Elf_External_Verneed *) p);
5638 p += sizeof (Elf_External_Verneed);
5640 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5642 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5643 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5644 a->vna_nodename, FALSE);
5645 if (indx == (bfd_size_type) -1)
5646 return FALSE;
5647 a->vna_name = indx;
5648 if (a->vna_nextptr == NULL)
5649 a->vna_next = 0;
5650 else
5651 a->vna_next = sizeof (Elf_External_Vernaux);
5653 _bfd_elf_swap_vernaux_out (output_bfd, a,
5654 (Elf_External_Vernaux *) p);
5655 p += sizeof (Elf_External_Vernaux);
5659 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5660 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5661 return FALSE;
5663 elf_tdata (output_bfd)->cverrefs = crefs;
5667 if ((elf_tdata (output_bfd)->cverrefs == 0
5668 && elf_tdata (output_bfd)->cverdefs == 0)
5669 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5670 &section_sym_count) == 0)
5672 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5673 s->flags |= SEC_EXCLUDE;
5676 return TRUE;
5679 bfd_boolean
5680 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5682 if (!is_elf_hash_table (info->hash))
5683 return TRUE;
5685 if (elf_hash_table (info)->dynamic_sections_created)
5687 bfd *dynobj;
5688 const struct elf_backend_data *bed;
5689 asection *s;
5690 bfd_size_type dynsymcount;
5691 unsigned long section_sym_count;
5692 size_t bucketcount = 0;
5693 size_t hash_entry_size;
5694 unsigned int dtagcount;
5696 dynobj = elf_hash_table (info)->dynobj;
5698 /* Assign dynsym indicies. In a shared library we generate a
5699 section symbol for each output section, which come first.
5700 Next come all of the back-end allocated local dynamic syms,
5701 followed by the rest of the global symbols. */
5703 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5704 &section_sym_count);
5706 /* Work out the size of the symbol version section. */
5707 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5708 BFD_ASSERT (s != NULL);
5709 if (dynsymcount != 0
5710 && (s->flags & SEC_EXCLUDE) == 0)
5712 s->size = dynsymcount * sizeof (Elf_External_Versym);
5713 s->contents = bfd_zalloc (output_bfd, s->size);
5714 if (s->contents == NULL)
5715 return FALSE;
5717 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5718 return FALSE;
5721 /* Set the size of the .dynsym and .hash sections. We counted
5722 the number of dynamic symbols in elf_link_add_object_symbols.
5723 We will build the contents of .dynsym and .hash when we build
5724 the final symbol table, because until then we do not know the
5725 correct value to give the symbols. We built the .dynstr
5726 section as we went along in elf_link_add_object_symbols. */
5727 s = bfd_get_section_by_name (dynobj, ".dynsym");
5728 BFD_ASSERT (s != NULL);
5729 bed = get_elf_backend_data (output_bfd);
5730 s->size = dynsymcount * bed->s->sizeof_sym;
5732 if (dynsymcount != 0)
5734 s->contents = bfd_alloc (output_bfd, s->size);
5735 if (s->contents == NULL)
5736 return FALSE;
5738 /* The first entry in .dynsym is a dummy symbol.
5739 Clear all the section syms, in case we don't output them all. */
5740 ++section_sym_count;
5741 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5744 /* Compute the size of the hashing table. As a side effect this
5745 computes the hash values for all the names we export. */
5746 bucketcount = compute_bucket_count (info);
5748 s = bfd_get_section_by_name (dynobj, ".hash");
5749 BFD_ASSERT (s != NULL);
5750 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5751 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5752 s->contents = bfd_zalloc (output_bfd, s->size);
5753 if (s->contents == NULL)
5754 return FALSE;
5756 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5757 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5758 s->contents + hash_entry_size);
5760 elf_hash_table (info)->bucketcount = bucketcount;
5762 s = bfd_get_section_by_name (dynobj, ".dynstr");
5763 BFD_ASSERT (s != NULL);
5765 elf_finalize_dynstr (output_bfd, info);
5767 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5769 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5770 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5771 return FALSE;
5774 return TRUE;
5777 /* Final phase of ELF linker. */
5779 /* A structure we use to avoid passing large numbers of arguments. */
5781 struct elf_final_link_info
5783 /* General link information. */
5784 struct bfd_link_info *info;
5785 /* Output BFD. */
5786 bfd *output_bfd;
5787 /* Symbol string table. */
5788 struct bfd_strtab_hash *symstrtab;
5789 /* .dynsym section. */
5790 asection *dynsym_sec;
5791 /* .hash section. */
5792 asection *hash_sec;
5793 /* symbol version section (.gnu.version). */
5794 asection *symver_sec;
5795 /* Buffer large enough to hold contents of any section. */
5796 bfd_byte *contents;
5797 /* Buffer large enough to hold external relocs of any section. */
5798 void *external_relocs;
5799 /* Buffer large enough to hold internal relocs of any section. */
5800 Elf_Internal_Rela *internal_relocs;
5801 /* Buffer large enough to hold external local symbols of any input
5802 BFD. */
5803 bfd_byte *external_syms;
5804 /* And a buffer for symbol section indices. */
5805 Elf_External_Sym_Shndx *locsym_shndx;
5806 /* Buffer large enough to hold internal local symbols of any input
5807 BFD. */
5808 Elf_Internal_Sym *internal_syms;
5809 /* Array large enough to hold a symbol index for each local symbol
5810 of any input BFD. */
5811 long *indices;
5812 /* Array large enough to hold a section pointer for each local
5813 symbol of any input BFD. */
5814 asection **sections;
5815 /* Buffer to hold swapped out symbols. */
5816 bfd_byte *symbuf;
5817 /* And one for symbol section indices. */
5818 Elf_External_Sym_Shndx *symshndxbuf;
5819 /* Number of swapped out symbols in buffer. */
5820 size_t symbuf_count;
5821 /* Number of symbols which fit in symbuf. */
5822 size_t symbuf_size;
5823 /* And same for symshndxbuf. */
5824 size_t shndxbuf_size;
5827 /* This struct is used to pass information to elf_link_output_extsym. */
5829 struct elf_outext_info
5831 bfd_boolean failed;
5832 bfd_boolean localsyms;
5833 struct elf_final_link_info *finfo;
5836 /* When performing a relocatable link, the input relocations are
5837 preserved. But, if they reference global symbols, the indices
5838 referenced must be updated. Update all the relocations in
5839 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5841 static void
5842 elf_link_adjust_relocs (bfd *abfd,
5843 Elf_Internal_Shdr *rel_hdr,
5844 unsigned int count,
5845 struct elf_link_hash_entry **rel_hash)
5847 unsigned int i;
5848 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5849 bfd_byte *erela;
5850 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5851 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5852 bfd_vma r_type_mask;
5853 int r_sym_shift;
5855 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5857 swap_in = bed->s->swap_reloc_in;
5858 swap_out = bed->s->swap_reloc_out;
5860 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5862 swap_in = bed->s->swap_reloca_in;
5863 swap_out = bed->s->swap_reloca_out;
5865 else
5866 abort ();
5868 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5869 abort ();
5871 if (bed->s->arch_size == 32)
5873 r_type_mask = 0xff;
5874 r_sym_shift = 8;
5876 else
5878 r_type_mask = 0xffffffff;
5879 r_sym_shift = 32;
5882 erela = rel_hdr->contents;
5883 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5885 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5886 unsigned int j;
5888 if (*rel_hash == NULL)
5889 continue;
5891 BFD_ASSERT ((*rel_hash)->indx >= 0);
5893 (*swap_in) (abfd, erela, irela);
5894 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5895 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5896 | (irela[j].r_info & r_type_mask));
5897 (*swap_out) (abfd, irela, erela);
5901 struct elf_link_sort_rela
5903 union {
5904 bfd_vma offset;
5905 bfd_vma sym_mask;
5906 } u;
5907 enum elf_reloc_type_class type;
5908 /* We use this as an array of size int_rels_per_ext_rel. */
5909 Elf_Internal_Rela rela[1];
5912 static int
5913 elf_link_sort_cmp1 (const void *A, const void *B)
5915 const struct elf_link_sort_rela *a = A;
5916 const struct elf_link_sort_rela *b = B;
5917 int relativea, relativeb;
5919 relativea = a->type == reloc_class_relative;
5920 relativeb = b->type == reloc_class_relative;
5922 if (relativea < relativeb)
5923 return 1;
5924 if (relativea > relativeb)
5925 return -1;
5926 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5927 return -1;
5928 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5929 return 1;
5930 if (a->rela->r_offset < b->rela->r_offset)
5931 return -1;
5932 if (a->rela->r_offset > b->rela->r_offset)
5933 return 1;
5934 return 0;
5937 static int
5938 elf_link_sort_cmp2 (const void *A, const void *B)
5940 const struct elf_link_sort_rela *a = A;
5941 const struct elf_link_sort_rela *b = B;
5942 int copya, copyb;
5944 if (a->u.offset < b->u.offset)
5945 return -1;
5946 if (a->u.offset > b->u.offset)
5947 return 1;
5948 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5949 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5950 if (copya < copyb)
5951 return -1;
5952 if (copya > copyb)
5953 return 1;
5954 if (a->rela->r_offset < b->rela->r_offset)
5955 return -1;
5956 if (a->rela->r_offset > b->rela->r_offset)
5957 return 1;
5958 return 0;
5961 static size_t
5962 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5964 asection *reldyn;
5965 bfd_size_type count, size;
5966 size_t i, ret, sort_elt, ext_size;
5967 bfd_byte *sort, *s_non_relative, *p;
5968 struct elf_link_sort_rela *sq;
5969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5970 int i2e = bed->s->int_rels_per_ext_rel;
5971 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5972 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5973 struct bfd_link_order *lo;
5974 bfd_vma r_sym_mask;
5976 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5977 if (reldyn == NULL || reldyn->size == 0)
5979 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5980 if (reldyn == NULL || reldyn->size == 0)
5981 return 0;
5982 ext_size = bed->s->sizeof_rel;
5983 swap_in = bed->s->swap_reloc_in;
5984 swap_out = bed->s->swap_reloc_out;
5986 else
5988 ext_size = bed->s->sizeof_rela;
5989 swap_in = bed->s->swap_reloca_in;
5990 swap_out = bed->s->swap_reloca_out;
5992 count = reldyn->size / ext_size;
5994 size = 0;
5995 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5996 if (lo->type == bfd_indirect_link_order)
5998 asection *o = lo->u.indirect.section;
5999 size += o->size;
6002 if (size != reldyn->size)
6003 return 0;
6005 sort_elt = (sizeof (struct elf_link_sort_rela)
6006 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6007 sort = bfd_zmalloc (sort_elt * count);
6008 if (sort == NULL)
6010 (*info->callbacks->warning)
6011 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6012 return 0;
6015 if (bed->s->arch_size == 32)
6016 r_sym_mask = ~(bfd_vma) 0xff;
6017 else
6018 r_sym_mask = ~(bfd_vma) 0xffffffff;
6020 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6021 if (lo->type == bfd_indirect_link_order)
6023 bfd_byte *erel, *erelend;
6024 asection *o = lo->u.indirect.section;
6026 if (o->contents == NULL && o->size != 0)
6028 /* This is a reloc section that is being handled as a normal
6029 section. See bfd_section_from_shdr. We can't combine
6030 relocs in this case. */
6031 free (sort);
6032 return 0;
6034 erel = o->contents;
6035 erelend = o->contents + o->size;
6036 p = sort + o->output_offset / ext_size * sort_elt;
6037 while (erel < erelend)
6039 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6040 (*swap_in) (abfd, erel, s->rela);
6041 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6042 s->u.sym_mask = r_sym_mask;
6043 p += sort_elt;
6044 erel += ext_size;
6048 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6050 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6052 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6053 if (s->type != reloc_class_relative)
6054 break;
6056 ret = i;
6057 s_non_relative = p;
6059 sq = (struct elf_link_sort_rela *) s_non_relative;
6060 for (; i < count; i++, p += sort_elt)
6062 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6063 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6064 sq = sp;
6065 sp->u.offset = sq->rela->r_offset;
6068 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6070 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6071 if (lo->type == bfd_indirect_link_order)
6073 bfd_byte *erel, *erelend;
6074 asection *o = lo->u.indirect.section;
6076 erel = o->contents;
6077 erelend = o->contents + o->size;
6078 p = sort + o->output_offset / ext_size * sort_elt;
6079 while (erel < erelend)
6081 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6082 (*swap_out) (abfd, s->rela, erel);
6083 p += sort_elt;
6084 erel += ext_size;
6088 free (sort);
6089 *psec = reldyn;
6090 return ret;
6093 /* Flush the output symbols to the file. */
6095 static bfd_boolean
6096 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6097 const struct elf_backend_data *bed)
6099 if (finfo->symbuf_count > 0)
6101 Elf_Internal_Shdr *hdr;
6102 file_ptr pos;
6103 bfd_size_type amt;
6105 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6106 pos = hdr->sh_offset + hdr->sh_size;
6107 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6108 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6109 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6110 return FALSE;
6112 hdr->sh_size += amt;
6113 finfo->symbuf_count = 0;
6116 return TRUE;
6119 /* Add a symbol to the output symbol table. */
6121 static bfd_boolean
6122 elf_link_output_sym (struct elf_final_link_info *finfo,
6123 const char *name,
6124 Elf_Internal_Sym *elfsym,
6125 asection *input_sec,
6126 struct elf_link_hash_entry *h)
6128 bfd_byte *dest;
6129 Elf_External_Sym_Shndx *destshndx;
6130 bfd_boolean (*output_symbol_hook)
6131 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6132 struct elf_link_hash_entry *);
6133 const struct elf_backend_data *bed;
6135 bed = get_elf_backend_data (finfo->output_bfd);
6136 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6137 if (output_symbol_hook != NULL)
6139 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6140 return FALSE;
6143 if (name == NULL || *name == '\0')
6144 elfsym->st_name = 0;
6145 else if (input_sec->flags & SEC_EXCLUDE)
6146 elfsym->st_name = 0;
6147 else
6149 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6150 name, TRUE, FALSE);
6151 if (elfsym->st_name == (unsigned long) -1)
6152 return FALSE;
6155 if (finfo->symbuf_count >= finfo->symbuf_size)
6157 if (! elf_link_flush_output_syms (finfo, bed))
6158 return FALSE;
6161 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6162 destshndx = finfo->symshndxbuf;
6163 if (destshndx != NULL)
6165 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6167 bfd_size_type amt;
6169 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6170 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6171 if (destshndx == NULL)
6172 return FALSE;
6173 memset ((char *) destshndx + amt, 0, amt);
6174 finfo->shndxbuf_size *= 2;
6176 destshndx += bfd_get_symcount (finfo->output_bfd);
6179 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6180 finfo->symbuf_count += 1;
6181 bfd_get_symcount (finfo->output_bfd) += 1;
6183 return TRUE;
6186 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6187 allowing an unsatisfied unversioned symbol in the DSO to match a
6188 versioned symbol that would normally require an explicit version.
6189 We also handle the case that a DSO references a hidden symbol
6190 which may be satisfied by a versioned symbol in another DSO. */
6192 static bfd_boolean
6193 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6194 const struct elf_backend_data *bed,
6195 struct elf_link_hash_entry *h)
6197 bfd *abfd;
6198 struct elf_link_loaded_list *loaded;
6200 if (!is_elf_hash_table (info->hash))
6201 return FALSE;
6203 switch (h->root.type)
6205 default:
6206 abfd = NULL;
6207 break;
6209 case bfd_link_hash_undefined:
6210 case bfd_link_hash_undefweak:
6211 abfd = h->root.u.undef.abfd;
6212 if ((abfd->flags & DYNAMIC) == 0
6213 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6214 return FALSE;
6215 break;
6217 case bfd_link_hash_defined:
6218 case bfd_link_hash_defweak:
6219 abfd = h->root.u.def.section->owner;
6220 break;
6222 case bfd_link_hash_common:
6223 abfd = h->root.u.c.p->section->owner;
6224 break;
6226 BFD_ASSERT (abfd != NULL);
6228 for (loaded = elf_hash_table (info)->loaded;
6229 loaded != NULL;
6230 loaded = loaded->next)
6232 bfd *input;
6233 Elf_Internal_Shdr *hdr;
6234 bfd_size_type symcount;
6235 bfd_size_type extsymcount;
6236 bfd_size_type extsymoff;
6237 Elf_Internal_Shdr *versymhdr;
6238 Elf_Internal_Sym *isym;
6239 Elf_Internal_Sym *isymend;
6240 Elf_Internal_Sym *isymbuf;
6241 Elf_External_Versym *ever;
6242 Elf_External_Versym *extversym;
6244 input = loaded->abfd;
6246 /* We check each DSO for a possible hidden versioned definition. */
6247 if (input == abfd
6248 || (input->flags & DYNAMIC) == 0
6249 || elf_dynversym (input) == 0)
6250 continue;
6252 hdr = &elf_tdata (input)->dynsymtab_hdr;
6254 symcount = hdr->sh_size / bed->s->sizeof_sym;
6255 if (elf_bad_symtab (input))
6257 extsymcount = symcount;
6258 extsymoff = 0;
6260 else
6262 extsymcount = symcount - hdr->sh_info;
6263 extsymoff = hdr->sh_info;
6266 if (extsymcount == 0)
6267 continue;
6269 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6270 NULL, NULL, NULL);
6271 if (isymbuf == NULL)
6272 return FALSE;
6274 /* Read in any version definitions. */
6275 versymhdr = &elf_tdata (input)->dynversym_hdr;
6276 extversym = bfd_malloc (versymhdr->sh_size);
6277 if (extversym == NULL)
6278 goto error_ret;
6280 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6281 || (bfd_bread (extversym, versymhdr->sh_size, input)
6282 != versymhdr->sh_size))
6284 free (extversym);
6285 error_ret:
6286 free (isymbuf);
6287 return FALSE;
6290 ever = extversym + extsymoff;
6291 isymend = isymbuf + extsymcount;
6292 for (isym = isymbuf; isym < isymend; isym++, ever++)
6294 const char *name;
6295 Elf_Internal_Versym iver;
6296 unsigned short version_index;
6298 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6299 || isym->st_shndx == SHN_UNDEF)
6300 continue;
6302 name = bfd_elf_string_from_elf_section (input,
6303 hdr->sh_link,
6304 isym->st_name);
6305 if (strcmp (name, h->root.root.string) != 0)
6306 continue;
6308 _bfd_elf_swap_versym_in (input, ever, &iver);
6310 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6312 /* If we have a non-hidden versioned sym, then it should
6313 have provided a definition for the undefined sym. */
6314 abort ();
6317 version_index = iver.vs_vers & VERSYM_VERSION;
6318 if (version_index == 1 || version_index == 2)
6320 /* This is the base or first version. We can use it. */
6321 free (extversym);
6322 free (isymbuf);
6323 return TRUE;
6327 free (extversym);
6328 free (isymbuf);
6331 return FALSE;
6334 /* Add an external symbol to the symbol table. This is called from
6335 the hash table traversal routine. When generating a shared object,
6336 we go through the symbol table twice. The first time we output
6337 anything that might have been forced to local scope in a version
6338 script. The second time we output the symbols that are still
6339 global symbols. */
6341 static bfd_boolean
6342 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6344 struct elf_outext_info *eoinfo = data;
6345 struct elf_final_link_info *finfo = eoinfo->finfo;
6346 bfd_boolean strip;
6347 Elf_Internal_Sym sym;
6348 asection *input_sec;
6349 const struct elf_backend_data *bed;
6351 if (h->root.type == bfd_link_hash_warning)
6353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6354 if (h->root.type == bfd_link_hash_new)
6355 return TRUE;
6358 /* Decide whether to output this symbol in this pass. */
6359 if (eoinfo->localsyms)
6361 if (!h->forced_local)
6362 return TRUE;
6364 else
6366 if (h->forced_local)
6367 return TRUE;
6370 bed = get_elf_backend_data (finfo->output_bfd);
6372 if (h->root.type == bfd_link_hash_undefined)
6374 /* If we have an undefined symbol reference here then it must have
6375 come from a shared library that is being linked in. (Undefined
6376 references in regular files have already been handled). */
6377 bfd_boolean ignore_undef = FALSE;
6379 /* Some symbols may be special in that the fact that they're
6380 undefined can be safely ignored - let backend determine that. */
6381 if (bed->elf_backend_ignore_undef_symbol)
6382 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6384 /* If we are reporting errors for this situation then do so now. */
6385 if (ignore_undef == FALSE
6386 && h->ref_dynamic
6387 && ! h->ref_regular
6388 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6389 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6391 if (! (finfo->info->callbacks->undefined_symbol
6392 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6393 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6395 eoinfo->failed = TRUE;
6396 return FALSE;
6401 /* We should also warn if a forced local symbol is referenced from
6402 shared libraries. */
6403 if (! finfo->info->relocatable
6404 && (! finfo->info->shared)
6405 && h->forced_local
6406 && h->ref_dynamic
6407 && !h->dynamic_def
6408 && !h->dynamic_weak
6409 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6411 (*_bfd_error_handler)
6412 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6413 finfo->output_bfd,
6414 h->root.u.def.section == bfd_abs_section_ptr
6415 ? finfo->output_bfd : h->root.u.def.section->owner,
6416 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6417 ? "internal"
6418 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6419 ? "hidden" : "local",
6420 h->root.root.string);
6421 eoinfo->failed = TRUE;
6422 return FALSE;
6425 /* We don't want to output symbols that have never been mentioned by
6426 a regular file, or that we have been told to strip. However, if
6427 h->indx is set to -2, the symbol is used by a reloc and we must
6428 output it. */
6429 if (h->indx == -2)
6430 strip = FALSE;
6431 else if ((h->def_dynamic
6432 || h->ref_dynamic
6433 || h->root.type == bfd_link_hash_new)
6434 && !h->def_regular
6435 && !h->ref_regular)
6436 strip = TRUE;
6437 else if (finfo->info->strip == strip_all)
6438 strip = TRUE;
6439 else if (finfo->info->strip == strip_some
6440 && bfd_hash_lookup (finfo->info->keep_hash,
6441 h->root.root.string, FALSE, FALSE) == NULL)
6442 strip = TRUE;
6443 else if (finfo->info->strip_discarded
6444 && (h->root.type == bfd_link_hash_defined
6445 || h->root.type == bfd_link_hash_defweak)
6446 && elf_discarded_section (h->root.u.def.section))
6447 strip = TRUE;
6448 else
6449 strip = FALSE;
6451 /* If we're stripping it, and it's not a dynamic symbol, there's
6452 nothing else to do unless it is a forced local symbol. */
6453 if (strip
6454 && h->dynindx == -1
6455 && !h->forced_local)
6456 return TRUE;
6458 sym.st_value = 0;
6459 sym.st_size = h->size;
6460 sym.st_other = h->other;
6461 if (h->forced_local)
6462 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6463 else if (h->root.type == bfd_link_hash_undefweak
6464 || h->root.type == bfd_link_hash_defweak)
6465 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6466 else
6467 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6469 switch (h->root.type)
6471 default:
6472 case bfd_link_hash_new:
6473 case bfd_link_hash_warning:
6474 abort ();
6475 return FALSE;
6477 case bfd_link_hash_undefined:
6478 case bfd_link_hash_undefweak:
6479 input_sec = bfd_und_section_ptr;
6480 sym.st_shndx = SHN_UNDEF;
6481 break;
6483 case bfd_link_hash_defined:
6484 case bfd_link_hash_defweak:
6486 input_sec = h->root.u.def.section;
6487 if (input_sec->output_section != NULL)
6489 sym.st_shndx =
6490 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6491 input_sec->output_section);
6492 if (sym.st_shndx == SHN_BAD)
6494 (*_bfd_error_handler)
6495 (_("%B: could not find output section %A for input section %A"),
6496 finfo->output_bfd, input_sec->output_section, input_sec);
6497 eoinfo->failed = TRUE;
6498 return FALSE;
6501 /* ELF symbols in relocatable files are section relative,
6502 but in nonrelocatable files they are virtual
6503 addresses. */
6504 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6505 if (! finfo->info->relocatable)
6507 sym.st_value += input_sec->output_section->vma;
6508 if (h->type == STT_TLS)
6510 /* STT_TLS symbols are relative to PT_TLS segment
6511 base. */
6512 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6513 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6517 else
6519 BFD_ASSERT (input_sec->owner == NULL
6520 || (input_sec->owner->flags & DYNAMIC) != 0);
6521 sym.st_shndx = SHN_UNDEF;
6522 input_sec = bfd_und_section_ptr;
6525 break;
6527 case bfd_link_hash_common:
6528 input_sec = h->root.u.c.p->section;
6529 sym.st_shndx = bed->common_section_index (input_sec);
6530 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6531 break;
6533 case bfd_link_hash_indirect:
6534 /* These symbols are created by symbol versioning. They point
6535 to the decorated version of the name. For example, if the
6536 symbol foo@@GNU_1.2 is the default, which should be used when
6537 foo is used with no version, then we add an indirect symbol
6538 foo which points to foo@@GNU_1.2. We ignore these symbols,
6539 since the indirected symbol is already in the hash table. */
6540 return TRUE;
6543 /* Give the processor backend a chance to tweak the symbol value,
6544 and also to finish up anything that needs to be done for this
6545 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6546 forced local syms when non-shared is due to a historical quirk. */
6547 if ((h->dynindx != -1
6548 || h->forced_local)
6549 && ((finfo->info->shared
6550 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6551 || h->root.type != bfd_link_hash_undefweak))
6552 || !h->forced_local)
6553 && elf_hash_table (finfo->info)->dynamic_sections_created)
6555 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6556 (finfo->output_bfd, finfo->info, h, &sym)))
6558 eoinfo->failed = TRUE;
6559 return FALSE;
6563 /* If we are marking the symbol as undefined, and there are no
6564 non-weak references to this symbol from a regular object, then
6565 mark the symbol as weak undefined; if there are non-weak
6566 references, mark the symbol as strong. We can't do this earlier,
6567 because it might not be marked as undefined until the
6568 finish_dynamic_symbol routine gets through with it. */
6569 if (sym.st_shndx == SHN_UNDEF
6570 && h->ref_regular
6571 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6572 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6574 int bindtype;
6576 if (h->ref_regular_nonweak)
6577 bindtype = STB_GLOBAL;
6578 else
6579 bindtype = STB_WEAK;
6580 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6583 /* If a non-weak symbol with non-default visibility is not defined
6584 locally, it is a fatal error. */
6585 if (! finfo->info->relocatable
6586 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6587 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6588 && h->root.type == bfd_link_hash_undefined
6589 && !h->def_regular)
6591 (*_bfd_error_handler)
6592 (_("%B: %s symbol `%s' isn't defined"),
6593 finfo->output_bfd,
6594 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6595 ? "protected"
6596 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6597 ? "internal" : "hidden",
6598 h->root.root.string);
6599 eoinfo->failed = TRUE;
6600 return FALSE;
6603 /* If this symbol should be put in the .dynsym section, then put it
6604 there now. We already know the symbol index. We also fill in
6605 the entry in the .hash section. */
6606 if (h->dynindx != -1
6607 && elf_hash_table (finfo->info)->dynamic_sections_created)
6609 size_t bucketcount;
6610 size_t bucket;
6611 size_t hash_entry_size;
6612 bfd_byte *bucketpos;
6613 bfd_vma chain;
6614 bfd_byte *esym;
6616 sym.st_name = h->dynstr_index;
6617 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6618 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6620 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6621 bucket = h->u.elf_hash_value % bucketcount;
6622 hash_entry_size
6623 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6624 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6625 + (bucket + 2) * hash_entry_size);
6626 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6627 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6628 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6629 ((bfd_byte *) finfo->hash_sec->contents
6630 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6632 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6634 Elf_Internal_Versym iversym;
6635 Elf_External_Versym *eversym;
6637 if (!h->def_regular)
6639 if (h->verinfo.verdef == NULL)
6640 iversym.vs_vers = 0;
6641 else
6642 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6644 else
6646 if (h->verinfo.vertree == NULL)
6647 iversym.vs_vers = 1;
6648 else
6649 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6650 if (finfo->info->create_default_symver)
6651 iversym.vs_vers++;
6654 if (h->hidden)
6655 iversym.vs_vers |= VERSYM_HIDDEN;
6657 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6658 eversym += h->dynindx;
6659 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6663 /* If we're stripping it, then it was just a dynamic symbol, and
6664 there's nothing else to do. */
6665 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6666 return TRUE;
6668 h->indx = bfd_get_symcount (finfo->output_bfd);
6670 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6672 eoinfo->failed = TRUE;
6673 return FALSE;
6676 return TRUE;
6679 /* Return TRUE if special handling is done for relocs in SEC against
6680 symbols defined in discarded sections. */
6682 static bfd_boolean
6683 elf_section_ignore_discarded_relocs (asection *sec)
6685 const struct elf_backend_data *bed;
6687 switch (sec->sec_info_type)
6689 case ELF_INFO_TYPE_STABS:
6690 case ELF_INFO_TYPE_EH_FRAME:
6691 return TRUE;
6692 default:
6693 break;
6696 bed = get_elf_backend_data (sec->owner);
6697 if (bed->elf_backend_ignore_discarded_relocs != NULL
6698 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6699 return TRUE;
6701 return FALSE;
6704 /* Return a mask saying how ld should treat relocations in SEC against
6705 symbols defined in discarded sections. If this function returns
6706 COMPLAIN set, ld will issue a warning message. If this function
6707 returns PRETEND set, and the discarded section was link-once and the
6708 same size as the kept link-once section, ld will pretend that the
6709 symbol was actually defined in the kept section. Otherwise ld will
6710 zero the reloc (at least that is the intent, but some cooperation by
6711 the target dependent code is needed, particularly for REL targets). */
6713 unsigned int
6714 _bfd_elf_default_action_discarded (asection *sec)
6716 if (sec->flags & SEC_DEBUGGING)
6717 return 0;
6719 if (strcmp (".eh_frame", sec->name) == 0)
6720 return 0;
6722 if (strcmp (".gcc_except_table", sec->name) == 0)
6723 return 0;
6725 return COMPLAIN | PRETEND;
6728 /* Find a match between a section and a member of a section group. */
6730 static asection *
6731 match_group_member (asection *sec, asection *group)
6733 asection *first = elf_next_in_group (group);
6734 asection *s = first;
6736 while (s != NULL)
6738 if (bfd_elf_match_symbols_in_sections (s, sec))
6739 return s;
6741 if (s == first)
6742 break;
6745 return NULL;
6748 /* Check if the kept section of a discarded section SEC can be used
6749 to replace it. Return the replacement if it is OK. Otherwise return
6750 NULL. */
6752 asection *
6753 _bfd_elf_check_kept_section (asection *sec)
6755 asection *kept;
6757 kept = sec->kept_section;
6758 if (kept != NULL)
6760 if (elf_sec_group (sec) != NULL)
6761 kept = match_group_member (sec, kept);
6762 if (kept != NULL && sec->size != kept->size)
6763 kept = NULL;
6765 return kept;
6768 /* Link an input file into the linker output file. This function
6769 handles all the sections and relocations of the input file at once.
6770 This is so that we only have to read the local symbols once, and
6771 don't have to keep them in memory. */
6773 static bfd_boolean
6774 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6776 bfd_boolean (*relocate_section)
6777 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6778 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6779 bfd *output_bfd;
6780 Elf_Internal_Shdr *symtab_hdr;
6781 size_t locsymcount;
6782 size_t extsymoff;
6783 Elf_Internal_Sym *isymbuf;
6784 Elf_Internal_Sym *isym;
6785 Elf_Internal_Sym *isymend;
6786 long *pindex;
6787 asection **ppsection;
6788 asection *o;
6789 const struct elf_backend_data *bed;
6790 bfd_boolean emit_relocs;
6791 struct elf_link_hash_entry **sym_hashes;
6793 output_bfd = finfo->output_bfd;
6794 bed = get_elf_backend_data (output_bfd);
6795 relocate_section = bed->elf_backend_relocate_section;
6797 /* If this is a dynamic object, we don't want to do anything here:
6798 we don't want the local symbols, and we don't want the section
6799 contents. */
6800 if ((input_bfd->flags & DYNAMIC) != 0)
6801 return TRUE;
6803 emit_relocs = (finfo->info->relocatable
6804 || finfo->info->emitrelocations);
6806 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6807 if (elf_bad_symtab (input_bfd))
6809 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6810 extsymoff = 0;
6812 else
6814 locsymcount = symtab_hdr->sh_info;
6815 extsymoff = symtab_hdr->sh_info;
6818 /* Read the local symbols. */
6819 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6820 if (isymbuf == NULL && locsymcount != 0)
6822 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6823 finfo->internal_syms,
6824 finfo->external_syms,
6825 finfo->locsym_shndx);
6826 if (isymbuf == NULL)
6827 return FALSE;
6830 /* Find local symbol sections and adjust values of symbols in
6831 SEC_MERGE sections. Write out those local symbols we know are
6832 going into the output file. */
6833 isymend = isymbuf + locsymcount;
6834 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6835 isym < isymend;
6836 isym++, pindex++, ppsection++)
6838 asection *isec;
6839 const char *name;
6840 Elf_Internal_Sym osym;
6842 *pindex = -1;
6844 if (elf_bad_symtab (input_bfd))
6846 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6848 *ppsection = NULL;
6849 continue;
6853 if (isym->st_shndx == SHN_UNDEF)
6854 isec = bfd_und_section_ptr;
6855 else if (isym->st_shndx < SHN_LORESERVE
6856 || isym->st_shndx > SHN_HIRESERVE)
6858 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6859 if (isec
6860 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6861 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6862 isym->st_value =
6863 _bfd_merged_section_offset (output_bfd, &isec,
6864 elf_section_data (isec)->sec_info,
6865 isym->st_value);
6867 else if (isym->st_shndx == SHN_ABS)
6868 isec = bfd_abs_section_ptr;
6869 else if (isym->st_shndx == SHN_COMMON)
6870 isec = bfd_com_section_ptr;
6871 else
6873 /* Don't attempt to output symbols with st_shnx in the
6874 reserved range other than SHN_ABS and SHN_COMMON. */
6875 *ppsection = NULL;
6876 continue;
6879 *ppsection = isec;
6881 /* Don't output the first, undefined, symbol. */
6882 if (ppsection == finfo->sections)
6883 continue;
6885 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6887 /* We never output section symbols. Instead, we use the
6888 section symbol of the corresponding section in the output
6889 file. */
6890 continue;
6893 /* If we are stripping all symbols, we don't want to output this
6894 one. */
6895 if (finfo->info->strip == strip_all)
6896 continue;
6898 /* If we are discarding all local symbols, we don't want to
6899 output this one. If we are generating a relocatable output
6900 file, then some of the local symbols may be required by
6901 relocs; we output them below as we discover that they are
6902 needed. */
6903 if (finfo->info->discard == discard_all)
6904 continue;
6906 /* If this symbol is defined in a section which we are
6907 discarding, we don't need to keep it. */
6908 if (isym->st_shndx != SHN_UNDEF
6909 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6910 && (isec == NULL
6911 || bfd_section_removed_from_list (output_bfd,
6912 isec->output_section)))
6913 continue;
6915 /* Get the name of the symbol. */
6916 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6917 isym->st_name);
6918 if (name == NULL)
6919 return FALSE;
6921 /* See if we are discarding symbols with this name. */
6922 if ((finfo->info->strip == strip_some
6923 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6924 == NULL))
6925 || (((finfo->info->discard == discard_sec_merge
6926 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6927 || finfo->info->discard == discard_l)
6928 && bfd_is_local_label_name (input_bfd, name)))
6929 continue;
6931 /* If we get here, we are going to output this symbol. */
6933 osym = *isym;
6935 /* Adjust the section index for the output file. */
6936 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6937 isec->output_section);
6938 if (osym.st_shndx == SHN_BAD)
6939 return FALSE;
6941 *pindex = bfd_get_symcount (output_bfd);
6943 /* ELF symbols in relocatable files are section relative, but
6944 in executable files they are virtual addresses. Note that
6945 this code assumes that all ELF sections have an associated
6946 BFD section with a reasonable value for output_offset; below
6947 we assume that they also have a reasonable value for
6948 output_section. Any special sections must be set up to meet
6949 these requirements. */
6950 osym.st_value += isec->output_offset;
6951 if (! finfo->info->relocatable)
6953 osym.st_value += isec->output_section->vma;
6954 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6956 /* STT_TLS symbols are relative to PT_TLS segment base. */
6957 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6958 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6962 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6963 return FALSE;
6966 /* Relocate the contents of each section. */
6967 sym_hashes = elf_sym_hashes (input_bfd);
6968 for (o = input_bfd->sections; o != NULL; o = o->next)
6970 bfd_byte *contents;
6972 if (! o->linker_mark)
6974 /* This section was omitted from the link. */
6975 continue;
6978 if ((o->flags & SEC_HAS_CONTENTS) == 0
6979 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6980 continue;
6982 if ((o->flags & SEC_LINKER_CREATED) != 0)
6984 /* Section was created by _bfd_elf_link_create_dynamic_sections
6985 or somesuch. */
6986 continue;
6989 /* Get the contents of the section. They have been cached by a
6990 relaxation routine. Note that o is a section in an input
6991 file, so the contents field will not have been set by any of
6992 the routines which work on output files. */
6993 if (elf_section_data (o)->this_hdr.contents != NULL)
6994 contents = elf_section_data (o)->this_hdr.contents;
6995 else
6997 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6999 contents = finfo->contents;
7000 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7001 return FALSE;
7004 if ((o->flags & SEC_RELOC) != 0)
7006 Elf_Internal_Rela *internal_relocs;
7007 bfd_vma r_type_mask;
7008 int r_sym_shift;
7010 /* Get the swapped relocs. */
7011 internal_relocs
7012 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7013 finfo->internal_relocs, FALSE);
7014 if (internal_relocs == NULL
7015 && o->reloc_count > 0)
7016 return FALSE;
7018 if (bed->s->arch_size == 32)
7020 r_type_mask = 0xff;
7021 r_sym_shift = 8;
7023 else
7025 r_type_mask = 0xffffffff;
7026 r_sym_shift = 32;
7029 /* Run through the relocs looking for any against symbols
7030 from discarded sections and section symbols from
7031 removed link-once sections. Complain about relocs
7032 against discarded sections. Zero relocs against removed
7033 link-once sections. */
7034 if (!elf_section_ignore_discarded_relocs (o))
7036 Elf_Internal_Rela *rel, *relend;
7037 unsigned int action = (*bed->action_discarded) (o);
7039 rel = internal_relocs;
7040 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7041 for ( ; rel < relend; rel++)
7043 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7044 asection **ps, *sec;
7045 struct elf_link_hash_entry *h = NULL;
7046 const char *sym_name;
7048 if (r_symndx == STN_UNDEF)
7049 continue;
7051 if (r_symndx >= locsymcount
7052 || (elf_bad_symtab (input_bfd)
7053 && finfo->sections[r_symndx] == NULL))
7055 h = sym_hashes[r_symndx - extsymoff];
7057 /* Badly formatted input files can contain relocs that
7058 reference non-existant symbols. Check here so that
7059 we do not seg fault. */
7060 if (h == NULL)
7062 char buffer [32];
7064 sprintf_vma (buffer, rel->r_info);
7065 (*_bfd_error_handler)
7066 (_("error: %B contains a reloc (0x%s) for section %A "
7067 "that references a non-existent global symbol"),
7068 input_bfd, o, buffer);
7069 bfd_set_error (bfd_error_bad_value);
7070 return FALSE;
7073 while (h->root.type == bfd_link_hash_indirect
7074 || h->root.type == bfd_link_hash_warning)
7075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7077 if (h->root.type != bfd_link_hash_defined
7078 && h->root.type != bfd_link_hash_defweak)
7079 continue;
7081 ps = &h->root.u.def.section;
7082 sym_name = h->root.root.string;
7084 else
7086 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7087 ps = &finfo->sections[r_symndx];
7088 sym_name = bfd_elf_sym_name (input_bfd,
7089 symtab_hdr,
7090 sym, *ps);
7093 /* Complain if the definition comes from a
7094 discarded section. */
7095 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7097 BFD_ASSERT (r_symndx != 0);
7098 if (action & COMPLAIN)
7099 (*finfo->info->callbacks->einfo)
7100 (_("%X`%s' referenced in section `%A' of %B: "
7101 "defined in discarded section `%A' of %B\n"),
7102 sym_name, o, input_bfd, sec, sec->owner);
7104 /* Try to do the best we can to support buggy old
7105 versions of gcc. Pretend that the symbol is
7106 really defined in the kept linkonce section.
7107 FIXME: This is quite broken. Modifying the
7108 symbol here means we will be changing all later
7109 uses of the symbol, not just in this section. */
7110 if (action & PRETEND)
7112 asection *kept;
7114 kept = _bfd_elf_check_kept_section (sec);
7115 if (kept != NULL)
7117 *ps = kept;
7118 continue;
7122 /* Remove the symbol reference from the reloc, but
7123 don't kill the reloc completely. This is so that
7124 a zero value will be written into the section,
7125 which may have non-zero contents put there by the
7126 assembler. Zero in things like an eh_frame fde
7127 pc_begin allows stack unwinders to recognize the
7128 fde as bogus. */
7129 rel->r_info &= r_type_mask;
7130 rel->r_addend = 0;
7135 /* Relocate the section by invoking a back end routine.
7137 The back end routine is responsible for adjusting the
7138 section contents as necessary, and (if using Rela relocs
7139 and generating a relocatable output file) adjusting the
7140 reloc addend as necessary.
7142 The back end routine does not have to worry about setting
7143 the reloc address or the reloc symbol index.
7145 The back end routine is given a pointer to the swapped in
7146 internal symbols, and can access the hash table entries
7147 for the external symbols via elf_sym_hashes (input_bfd).
7149 When generating relocatable output, the back end routine
7150 must handle STB_LOCAL/STT_SECTION symbols specially. The
7151 output symbol is going to be a section symbol
7152 corresponding to the output section, which will require
7153 the addend to be adjusted. */
7155 if (! (*relocate_section) (output_bfd, finfo->info,
7156 input_bfd, o, contents,
7157 internal_relocs,
7158 isymbuf,
7159 finfo->sections))
7160 return FALSE;
7162 if (emit_relocs)
7164 Elf_Internal_Rela *irela;
7165 Elf_Internal_Rela *irelaend;
7166 bfd_vma last_offset;
7167 struct elf_link_hash_entry **rel_hash;
7168 struct elf_link_hash_entry **rel_hash_list;
7169 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7170 unsigned int next_erel;
7171 bfd_boolean rela_normal;
7173 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7174 rela_normal = (bed->rela_normal
7175 && (input_rel_hdr->sh_entsize
7176 == bed->s->sizeof_rela));
7178 /* Adjust the reloc addresses and symbol indices. */
7180 irela = internal_relocs;
7181 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7182 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7183 + elf_section_data (o->output_section)->rel_count
7184 + elf_section_data (o->output_section)->rel_count2);
7185 rel_hash_list = rel_hash;
7186 last_offset = o->output_offset;
7187 if (!finfo->info->relocatable)
7188 last_offset += o->output_section->vma;
7189 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7191 unsigned long r_symndx;
7192 asection *sec;
7193 Elf_Internal_Sym sym;
7195 if (next_erel == bed->s->int_rels_per_ext_rel)
7197 rel_hash++;
7198 next_erel = 0;
7201 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7202 finfo->info, o,
7203 irela->r_offset);
7204 if (irela->r_offset >= (bfd_vma) -2)
7206 /* This is a reloc for a deleted entry or somesuch.
7207 Turn it into an R_*_NONE reloc, at the same
7208 offset as the last reloc. elf_eh_frame.c and
7209 elf_bfd_discard_info rely on reloc offsets
7210 being ordered. */
7211 irela->r_offset = last_offset;
7212 irela->r_info = 0;
7213 irela->r_addend = 0;
7214 continue;
7217 irela->r_offset += o->output_offset;
7219 /* Relocs in an executable have to be virtual addresses. */
7220 if (!finfo->info->relocatable)
7221 irela->r_offset += o->output_section->vma;
7223 last_offset = irela->r_offset;
7225 r_symndx = irela->r_info >> r_sym_shift;
7226 if (r_symndx == STN_UNDEF)
7227 continue;
7229 if (r_symndx >= locsymcount
7230 || (elf_bad_symtab (input_bfd)
7231 && finfo->sections[r_symndx] == NULL))
7233 struct elf_link_hash_entry *rh;
7234 unsigned long indx;
7236 /* This is a reloc against a global symbol. We
7237 have not yet output all the local symbols, so
7238 we do not know the symbol index of any global
7239 symbol. We set the rel_hash entry for this
7240 reloc to point to the global hash table entry
7241 for this symbol. The symbol index is then
7242 set at the end of bfd_elf_final_link. */
7243 indx = r_symndx - extsymoff;
7244 rh = elf_sym_hashes (input_bfd)[indx];
7245 while (rh->root.type == bfd_link_hash_indirect
7246 || rh->root.type == bfd_link_hash_warning)
7247 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7249 /* Setting the index to -2 tells
7250 elf_link_output_extsym that this symbol is
7251 used by a reloc. */
7252 BFD_ASSERT (rh->indx < 0);
7253 rh->indx = -2;
7255 *rel_hash = rh;
7257 continue;
7260 /* This is a reloc against a local symbol. */
7262 *rel_hash = NULL;
7263 sym = isymbuf[r_symndx];
7264 sec = finfo->sections[r_symndx];
7265 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7267 /* I suppose the backend ought to fill in the
7268 section of any STT_SECTION symbol against a
7269 processor specific section. */
7270 r_symndx = 0;
7271 if (bfd_is_abs_section (sec))
7273 else if (sec == NULL || sec->owner == NULL)
7275 bfd_set_error (bfd_error_bad_value);
7276 return FALSE;
7278 else
7280 asection *osec = sec->output_section;
7282 /* If we have discarded a section, the output
7283 section will be the absolute section. In
7284 case of discarded link-once and discarded
7285 SEC_MERGE sections, use the kept section. */
7286 if (bfd_is_abs_section (osec)
7287 && sec->kept_section != NULL
7288 && sec->kept_section->output_section != NULL)
7290 osec = sec->kept_section->output_section;
7291 irela->r_addend -= osec->vma;
7294 if (!bfd_is_abs_section (osec))
7296 r_symndx = osec->target_index;
7297 BFD_ASSERT (r_symndx != 0);
7301 /* Adjust the addend according to where the
7302 section winds up in the output section. */
7303 if (rela_normal)
7304 irela->r_addend += sec->output_offset;
7306 else
7308 if (finfo->indices[r_symndx] == -1)
7310 unsigned long shlink;
7311 const char *name;
7312 asection *osec;
7314 if (finfo->info->strip == strip_all)
7316 /* You can't do ld -r -s. */
7317 bfd_set_error (bfd_error_invalid_operation);
7318 return FALSE;
7321 /* This symbol was skipped earlier, but
7322 since it is needed by a reloc, we
7323 must output it now. */
7324 shlink = symtab_hdr->sh_link;
7325 name = (bfd_elf_string_from_elf_section
7326 (input_bfd, shlink, sym.st_name));
7327 if (name == NULL)
7328 return FALSE;
7330 osec = sec->output_section;
7331 sym.st_shndx =
7332 _bfd_elf_section_from_bfd_section (output_bfd,
7333 osec);
7334 if (sym.st_shndx == SHN_BAD)
7335 return FALSE;
7337 sym.st_value += sec->output_offset;
7338 if (! finfo->info->relocatable)
7340 sym.st_value += osec->vma;
7341 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7343 /* STT_TLS symbols are relative to PT_TLS
7344 segment base. */
7345 BFD_ASSERT (elf_hash_table (finfo->info)
7346 ->tls_sec != NULL);
7347 sym.st_value -= (elf_hash_table (finfo->info)
7348 ->tls_sec->vma);
7352 finfo->indices[r_symndx]
7353 = bfd_get_symcount (output_bfd);
7355 if (! elf_link_output_sym (finfo, name, &sym, sec,
7356 NULL))
7357 return FALSE;
7360 r_symndx = finfo->indices[r_symndx];
7363 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7364 | (irela->r_info & r_type_mask));
7367 /* Swap out the relocs. */
7368 if (input_rel_hdr->sh_size != 0
7369 && !bed->elf_backend_emit_relocs (output_bfd, o,
7370 input_rel_hdr,
7371 internal_relocs,
7372 rel_hash_list))
7373 return FALSE;
7375 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7376 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7378 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7379 * bed->s->int_rels_per_ext_rel);
7380 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7381 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7382 input_rel_hdr2,
7383 internal_relocs,
7384 rel_hash_list))
7385 return FALSE;
7390 /* Write out the modified section contents. */
7391 if (bed->elf_backend_write_section
7392 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7394 /* Section written out. */
7396 else switch (o->sec_info_type)
7398 case ELF_INFO_TYPE_STABS:
7399 if (! (_bfd_write_section_stabs
7400 (output_bfd,
7401 &elf_hash_table (finfo->info)->stab_info,
7402 o, &elf_section_data (o)->sec_info, contents)))
7403 return FALSE;
7404 break;
7405 case ELF_INFO_TYPE_MERGE:
7406 if (! _bfd_write_merged_section (output_bfd, o,
7407 elf_section_data (o)->sec_info))
7408 return FALSE;
7409 break;
7410 case ELF_INFO_TYPE_EH_FRAME:
7412 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7413 o, contents))
7414 return FALSE;
7416 break;
7417 default:
7419 if (! (o->flags & SEC_EXCLUDE)
7420 && ! bfd_set_section_contents (output_bfd, o->output_section,
7421 contents,
7422 (file_ptr) o->output_offset,
7423 o->size))
7424 return FALSE;
7426 break;
7430 return TRUE;
7433 /* Generate a reloc when linking an ELF file. This is a reloc
7434 requested by the linker, and does not come from any input file. This
7435 is used to build constructor and destructor tables when linking
7436 with -Ur. */
7438 static bfd_boolean
7439 elf_reloc_link_order (bfd *output_bfd,
7440 struct bfd_link_info *info,
7441 asection *output_section,
7442 struct bfd_link_order *link_order)
7444 reloc_howto_type *howto;
7445 long indx;
7446 bfd_vma offset;
7447 bfd_vma addend;
7448 struct elf_link_hash_entry **rel_hash_ptr;
7449 Elf_Internal_Shdr *rel_hdr;
7450 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7451 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7452 bfd_byte *erel;
7453 unsigned int i;
7455 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7456 if (howto == NULL)
7458 bfd_set_error (bfd_error_bad_value);
7459 return FALSE;
7462 addend = link_order->u.reloc.p->addend;
7464 /* Figure out the symbol index. */
7465 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7466 + elf_section_data (output_section)->rel_count
7467 + elf_section_data (output_section)->rel_count2);
7468 if (link_order->type == bfd_section_reloc_link_order)
7470 indx = link_order->u.reloc.p->u.section->target_index;
7471 BFD_ASSERT (indx != 0);
7472 *rel_hash_ptr = NULL;
7474 else
7476 struct elf_link_hash_entry *h;
7478 /* Treat a reloc against a defined symbol as though it were
7479 actually against the section. */
7480 h = ((struct elf_link_hash_entry *)
7481 bfd_wrapped_link_hash_lookup (output_bfd, info,
7482 link_order->u.reloc.p->u.name,
7483 FALSE, FALSE, TRUE));
7484 if (h != NULL
7485 && (h->root.type == bfd_link_hash_defined
7486 || h->root.type == bfd_link_hash_defweak))
7488 asection *section;
7490 section = h->root.u.def.section;
7491 indx = section->output_section->target_index;
7492 *rel_hash_ptr = NULL;
7493 /* It seems that we ought to add the symbol value to the
7494 addend here, but in practice it has already been added
7495 because it was passed to constructor_callback. */
7496 addend += section->output_section->vma + section->output_offset;
7498 else if (h != NULL)
7500 /* Setting the index to -2 tells elf_link_output_extsym that
7501 this symbol is used by a reloc. */
7502 h->indx = -2;
7503 *rel_hash_ptr = h;
7504 indx = 0;
7506 else
7508 if (! ((*info->callbacks->unattached_reloc)
7509 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7510 return FALSE;
7511 indx = 0;
7515 /* If this is an inplace reloc, we must write the addend into the
7516 object file. */
7517 if (howto->partial_inplace && addend != 0)
7519 bfd_size_type size;
7520 bfd_reloc_status_type rstat;
7521 bfd_byte *buf;
7522 bfd_boolean ok;
7523 const char *sym_name;
7525 size = bfd_get_reloc_size (howto);
7526 buf = bfd_zmalloc (size);
7527 if (buf == NULL)
7528 return FALSE;
7529 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7530 switch (rstat)
7532 case bfd_reloc_ok:
7533 break;
7535 default:
7536 case bfd_reloc_outofrange:
7537 abort ();
7539 case bfd_reloc_overflow:
7540 if (link_order->type == bfd_section_reloc_link_order)
7541 sym_name = bfd_section_name (output_bfd,
7542 link_order->u.reloc.p->u.section);
7543 else
7544 sym_name = link_order->u.reloc.p->u.name;
7545 if (! ((*info->callbacks->reloc_overflow)
7546 (info, NULL, sym_name, howto->name, addend, NULL,
7547 NULL, (bfd_vma) 0)))
7549 free (buf);
7550 return FALSE;
7552 break;
7554 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7555 link_order->offset, size);
7556 free (buf);
7557 if (! ok)
7558 return FALSE;
7561 /* The address of a reloc is relative to the section in a
7562 relocatable file, and is a virtual address in an executable
7563 file. */
7564 offset = link_order->offset;
7565 if (! info->relocatable)
7566 offset += output_section->vma;
7568 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7570 irel[i].r_offset = offset;
7571 irel[i].r_info = 0;
7572 irel[i].r_addend = 0;
7574 if (bed->s->arch_size == 32)
7575 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7576 else
7577 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7579 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7580 erel = rel_hdr->contents;
7581 if (rel_hdr->sh_type == SHT_REL)
7583 erel += (elf_section_data (output_section)->rel_count
7584 * bed->s->sizeof_rel);
7585 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7587 else
7589 irel[0].r_addend = addend;
7590 erel += (elf_section_data (output_section)->rel_count
7591 * bed->s->sizeof_rela);
7592 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7595 ++elf_section_data (output_section)->rel_count;
7597 return TRUE;
7601 /* Get the output vma of the section pointed to by the sh_link field. */
7603 static bfd_vma
7604 elf_get_linked_section_vma (struct bfd_link_order *p)
7606 Elf_Internal_Shdr **elf_shdrp;
7607 asection *s;
7608 int elfsec;
7610 s = p->u.indirect.section;
7611 elf_shdrp = elf_elfsections (s->owner);
7612 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7613 elfsec = elf_shdrp[elfsec]->sh_link;
7614 /* PR 290:
7615 The Intel C compiler generates SHT_IA_64_UNWIND with
7616 SHF_LINK_ORDER. But it doesn't set the sh_link or
7617 sh_info fields. Hence we could get the situation
7618 where elfsec is 0. */
7619 if (elfsec == 0)
7621 const struct elf_backend_data *bed
7622 = get_elf_backend_data (s->owner);
7623 if (bed->link_order_error_handler)
7624 bed->link_order_error_handler
7625 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7626 return 0;
7628 else
7630 s = elf_shdrp[elfsec]->bfd_section;
7631 return s->output_section->vma + s->output_offset;
7636 /* Compare two sections based on the locations of the sections they are
7637 linked to. Used by elf_fixup_link_order. */
7639 static int
7640 compare_link_order (const void * a, const void * b)
7642 bfd_vma apos;
7643 bfd_vma bpos;
7645 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7646 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7647 if (apos < bpos)
7648 return -1;
7649 return apos > bpos;
7653 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7654 order as their linked sections. Returns false if this could not be done
7655 because an output section includes both ordered and unordered
7656 sections. Ideally we'd do this in the linker proper. */
7658 static bfd_boolean
7659 elf_fixup_link_order (bfd *abfd, asection *o)
7661 int seen_linkorder;
7662 int seen_other;
7663 int n;
7664 struct bfd_link_order *p;
7665 bfd *sub;
7666 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7667 int elfsec;
7668 struct bfd_link_order **sections;
7669 asection *s, *other_sec, *linkorder_sec;
7670 bfd_vma offset;
7672 other_sec = NULL;
7673 linkorder_sec = NULL;
7674 seen_other = 0;
7675 seen_linkorder = 0;
7676 for (p = o->map_head.link_order; p != NULL; p = p->next)
7678 if (p->type == bfd_indirect_link_order)
7680 s = p->u.indirect.section;
7681 sub = s->owner;
7682 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7683 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7684 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1
7685 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7687 seen_linkorder++;
7688 linkorder_sec = s;
7690 else
7692 seen_other++;
7693 other_sec = s;
7696 else
7697 seen_other++;
7699 if (seen_other && seen_linkorder)
7701 if (other_sec && linkorder_sec)
7702 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7703 o, linkorder_sec,
7704 linkorder_sec->owner, other_sec,
7705 other_sec->owner);
7706 else
7707 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7709 bfd_set_error (bfd_error_bad_value);
7710 return FALSE;
7714 if (!seen_linkorder)
7715 return TRUE;
7717 sections = (struct bfd_link_order **)
7718 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7719 seen_linkorder = 0;
7721 for (p = o->map_head.link_order; p != NULL; p = p->next)
7723 sections[seen_linkorder++] = p;
7725 /* Sort the input sections in the order of their linked section. */
7726 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7727 compare_link_order);
7729 /* Change the offsets of the sections. */
7730 offset = 0;
7731 for (n = 0; n < seen_linkorder; n++)
7733 s = sections[n]->u.indirect.section;
7734 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7735 s->output_offset = offset;
7736 sections[n]->offset = offset;
7737 offset += sections[n]->size;
7740 return TRUE;
7744 /* Do the final step of an ELF link. */
7746 bfd_boolean
7747 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7749 bfd_boolean dynamic;
7750 bfd_boolean emit_relocs;
7751 bfd *dynobj;
7752 struct elf_final_link_info finfo;
7753 register asection *o;
7754 register struct bfd_link_order *p;
7755 register bfd *sub;
7756 bfd_size_type max_contents_size;
7757 bfd_size_type max_external_reloc_size;
7758 bfd_size_type max_internal_reloc_count;
7759 bfd_size_type max_sym_count;
7760 bfd_size_type max_sym_shndx_count;
7761 file_ptr off;
7762 Elf_Internal_Sym elfsym;
7763 unsigned int i;
7764 Elf_Internal_Shdr *symtab_hdr;
7765 Elf_Internal_Shdr *symtab_shndx_hdr;
7766 Elf_Internal_Shdr *symstrtab_hdr;
7767 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7768 struct elf_outext_info eoinfo;
7769 bfd_boolean merged;
7770 size_t relativecount = 0;
7771 asection *reldyn = 0;
7772 bfd_size_type amt;
7774 if (! is_elf_hash_table (info->hash))
7775 return FALSE;
7777 if (info->shared)
7778 abfd->flags |= DYNAMIC;
7780 dynamic = elf_hash_table (info)->dynamic_sections_created;
7781 dynobj = elf_hash_table (info)->dynobj;
7783 emit_relocs = (info->relocatable
7784 || info->emitrelocations
7785 || bed->elf_backend_emit_relocs);
7787 finfo.info = info;
7788 finfo.output_bfd = abfd;
7789 finfo.symstrtab = _bfd_elf_stringtab_init ();
7790 if (finfo.symstrtab == NULL)
7791 return FALSE;
7793 if (! dynamic)
7795 finfo.dynsym_sec = NULL;
7796 finfo.hash_sec = NULL;
7797 finfo.symver_sec = NULL;
7799 else
7801 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7802 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7803 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7804 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7805 /* Note that it is OK if symver_sec is NULL. */
7808 finfo.contents = NULL;
7809 finfo.external_relocs = NULL;
7810 finfo.internal_relocs = NULL;
7811 finfo.external_syms = NULL;
7812 finfo.locsym_shndx = NULL;
7813 finfo.internal_syms = NULL;
7814 finfo.indices = NULL;
7815 finfo.sections = NULL;
7816 finfo.symbuf = NULL;
7817 finfo.symshndxbuf = NULL;
7818 finfo.symbuf_count = 0;
7819 finfo.shndxbuf_size = 0;
7821 /* Count up the number of relocations we will output for each output
7822 section, so that we know the sizes of the reloc sections. We
7823 also figure out some maximum sizes. */
7824 max_contents_size = 0;
7825 max_external_reloc_size = 0;
7826 max_internal_reloc_count = 0;
7827 max_sym_count = 0;
7828 max_sym_shndx_count = 0;
7829 merged = FALSE;
7830 for (o = abfd->sections; o != NULL; o = o->next)
7832 struct bfd_elf_section_data *esdo = elf_section_data (o);
7833 o->reloc_count = 0;
7835 for (p = o->map_head.link_order; p != NULL; p = p->next)
7837 unsigned int reloc_count = 0;
7838 struct bfd_elf_section_data *esdi = NULL;
7839 unsigned int *rel_count1;
7841 if (p->type == bfd_section_reloc_link_order
7842 || p->type == bfd_symbol_reloc_link_order)
7843 reloc_count = 1;
7844 else if (p->type == bfd_indirect_link_order)
7846 asection *sec;
7848 sec = p->u.indirect.section;
7849 esdi = elf_section_data (sec);
7851 /* Mark all sections which are to be included in the
7852 link. This will normally be every section. We need
7853 to do this so that we can identify any sections which
7854 the linker has decided to not include. */
7855 sec->linker_mark = TRUE;
7857 if (sec->flags & SEC_MERGE)
7858 merged = TRUE;
7860 if (info->relocatable || info->emitrelocations)
7861 reloc_count = sec->reloc_count;
7862 else if (bed->elf_backend_count_relocs)
7864 Elf_Internal_Rela * relocs;
7866 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7867 info->keep_memory);
7869 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7871 if (elf_section_data (o)->relocs != relocs)
7872 free (relocs);
7875 if (sec->rawsize > max_contents_size)
7876 max_contents_size = sec->rawsize;
7877 if (sec->size > max_contents_size)
7878 max_contents_size = sec->size;
7880 /* We are interested in just local symbols, not all
7881 symbols. */
7882 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7883 && (sec->owner->flags & DYNAMIC) == 0)
7885 size_t sym_count;
7887 if (elf_bad_symtab (sec->owner))
7888 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7889 / bed->s->sizeof_sym);
7890 else
7891 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7893 if (sym_count > max_sym_count)
7894 max_sym_count = sym_count;
7896 if (sym_count > max_sym_shndx_count
7897 && elf_symtab_shndx (sec->owner) != 0)
7898 max_sym_shndx_count = sym_count;
7900 if ((sec->flags & SEC_RELOC) != 0)
7902 size_t ext_size;
7904 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7905 if (ext_size > max_external_reloc_size)
7906 max_external_reloc_size = ext_size;
7907 if (sec->reloc_count > max_internal_reloc_count)
7908 max_internal_reloc_count = sec->reloc_count;
7913 if (reloc_count == 0)
7914 continue;
7916 o->reloc_count += reloc_count;
7918 /* MIPS may have a mix of REL and RELA relocs on sections.
7919 To support this curious ABI we keep reloc counts in
7920 elf_section_data too. We must be careful to add the
7921 relocations from the input section to the right output
7922 count. FIXME: Get rid of one count. We have
7923 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7924 rel_count1 = &esdo->rel_count;
7925 if (esdi != NULL)
7927 bfd_boolean same_size;
7928 bfd_size_type entsize1;
7930 entsize1 = esdi->rel_hdr.sh_entsize;
7931 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7932 || entsize1 == bed->s->sizeof_rela);
7933 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7935 if (!same_size)
7936 rel_count1 = &esdo->rel_count2;
7938 if (esdi->rel_hdr2 != NULL)
7940 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7941 unsigned int alt_count;
7942 unsigned int *rel_count2;
7944 BFD_ASSERT (entsize2 != entsize1
7945 && (entsize2 == bed->s->sizeof_rel
7946 || entsize2 == bed->s->sizeof_rela));
7948 rel_count2 = &esdo->rel_count2;
7949 if (!same_size)
7950 rel_count2 = &esdo->rel_count;
7952 /* The following is probably too simplistic if the
7953 backend counts output relocs unusually. */
7954 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7955 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7956 *rel_count2 += alt_count;
7957 reloc_count -= alt_count;
7960 *rel_count1 += reloc_count;
7963 if (o->reloc_count > 0)
7964 o->flags |= SEC_RELOC;
7965 else
7967 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7968 set it (this is probably a bug) and if it is set
7969 assign_section_numbers will create a reloc section. */
7970 o->flags &=~ SEC_RELOC;
7973 /* If the SEC_ALLOC flag is not set, force the section VMA to
7974 zero. This is done in elf_fake_sections as well, but forcing
7975 the VMA to 0 here will ensure that relocs against these
7976 sections are handled correctly. */
7977 if ((o->flags & SEC_ALLOC) == 0
7978 && ! o->user_set_vma)
7979 o->vma = 0;
7982 if (! info->relocatable && merged)
7983 elf_link_hash_traverse (elf_hash_table (info),
7984 _bfd_elf_link_sec_merge_syms, abfd);
7986 /* Figure out the file positions for everything but the symbol table
7987 and the relocs. We set symcount to force assign_section_numbers
7988 to create a symbol table. */
7989 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7990 BFD_ASSERT (! abfd->output_has_begun);
7991 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7992 goto error_return;
7994 /* Set sizes, and assign file positions for reloc sections. */
7995 for (o = abfd->sections; o != NULL; o = o->next)
7997 if ((o->flags & SEC_RELOC) != 0)
7999 if (!(_bfd_elf_link_size_reloc_section
8000 (abfd, &elf_section_data (o)->rel_hdr, o)))
8001 goto error_return;
8003 if (elf_section_data (o)->rel_hdr2
8004 && !(_bfd_elf_link_size_reloc_section
8005 (abfd, elf_section_data (o)->rel_hdr2, o)))
8006 goto error_return;
8009 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8010 to count upwards while actually outputting the relocations. */
8011 elf_section_data (o)->rel_count = 0;
8012 elf_section_data (o)->rel_count2 = 0;
8015 _bfd_elf_assign_file_positions_for_relocs (abfd);
8017 /* We have now assigned file positions for all the sections except
8018 .symtab and .strtab. We start the .symtab section at the current
8019 file position, and write directly to it. We build the .strtab
8020 section in memory. */
8021 bfd_get_symcount (abfd) = 0;
8022 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8023 /* sh_name is set in prep_headers. */
8024 symtab_hdr->sh_type = SHT_SYMTAB;
8025 /* sh_flags, sh_addr and sh_size all start off zero. */
8026 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8027 /* sh_link is set in assign_section_numbers. */
8028 /* sh_info is set below. */
8029 /* sh_offset is set just below. */
8030 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8032 off = elf_tdata (abfd)->next_file_pos;
8033 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8035 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8036 incorrect. We do not yet know the size of the .symtab section.
8037 We correct next_file_pos below, after we do know the size. */
8039 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8040 continuously seeking to the right position in the file. */
8041 if (! info->keep_memory || max_sym_count < 20)
8042 finfo.symbuf_size = 20;
8043 else
8044 finfo.symbuf_size = max_sym_count;
8045 amt = finfo.symbuf_size;
8046 amt *= bed->s->sizeof_sym;
8047 finfo.symbuf = bfd_malloc (amt);
8048 if (finfo.symbuf == NULL)
8049 goto error_return;
8050 if (elf_numsections (abfd) > SHN_LORESERVE)
8052 /* Wild guess at number of output symbols. realloc'd as needed. */
8053 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8054 finfo.shndxbuf_size = amt;
8055 amt *= sizeof (Elf_External_Sym_Shndx);
8056 finfo.symshndxbuf = bfd_zmalloc (amt);
8057 if (finfo.symshndxbuf == NULL)
8058 goto error_return;
8061 /* Start writing out the symbol table. The first symbol is always a
8062 dummy symbol. */
8063 if (info->strip != strip_all
8064 || emit_relocs)
8066 elfsym.st_value = 0;
8067 elfsym.st_size = 0;
8068 elfsym.st_info = 0;
8069 elfsym.st_other = 0;
8070 elfsym.st_shndx = SHN_UNDEF;
8071 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8072 NULL))
8073 goto error_return;
8076 /* Output a symbol for each section. We output these even if we are
8077 discarding local symbols, since they are used for relocs. These
8078 symbols have no names. We store the index of each one in the
8079 index field of the section, so that we can find it again when
8080 outputting relocs. */
8081 if (info->strip != strip_all
8082 || emit_relocs)
8084 elfsym.st_size = 0;
8085 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8086 elfsym.st_other = 0;
8087 for (i = 1; i < elf_numsections (abfd); i++)
8089 o = bfd_section_from_elf_index (abfd, i);
8090 if (o != NULL)
8091 o->target_index = bfd_get_symcount (abfd);
8092 elfsym.st_shndx = i;
8093 if (info->relocatable || o == NULL)
8094 elfsym.st_value = 0;
8095 else
8096 elfsym.st_value = o->vma;
8097 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8098 goto error_return;
8099 if (i == SHN_LORESERVE - 1)
8100 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8104 /* Allocate some memory to hold information read in from the input
8105 files. */
8106 if (max_contents_size != 0)
8108 finfo.contents = bfd_malloc (max_contents_size);
8109 if (finfo.contents == NULL)
8110 goto error_return;
8113 if (max_external_reloc_size != 0)
8115 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8116 if (finfo.external_relocs == NULL)
8117 goto error_return;
8120 if (max_internal_reloc_count != 0)
8122 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8123 amt *= sizeof (Elf_Internal_Rela);
8124 finfo.internal_relocs = bfd_malloc (amt);
8125 if (finfo.internal_relocs == NULL)
8126 goto error_return;
8129 if (max_sym_count != 0)
8131 amt = max_sym_count * bed->s->sizeof_sym;
8132 finfo.external_syms = bfd_malloc (amt);
8133 if (finfo.external_syms == NULL)
8134 goto error_return;
8136 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8137 finfo.internal_syms = bfd_malloc (amt);
8138 if (finfo.internal_syms == NULL)
8139 goto error_return;
8141 amt = max_sym_count * sizeof (long);
8142 finfo.indices = bfd_malloc (amt);
8143 if (finfo.indices == NULL)
8144 goto error_return;
8146 amt = max_sym_count * sizeof (asection *);
8147 finfo.sections = bfd_malloc (amt);
8148 if (finfo.sections == NULL)
8149 goto error_return;
8152 if (max_sym_shndx_count != 0)
8154 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8155 finfo.locsym_shndx = bfd_malloc (amt);
8156 if (finfo.locsym_shndx == NULL)
8157 goto error_return;
8160 if (elf_hash_table (info)->tls_sec)
8162 bfd_vma base, end = 0;
8163 asection *sec;
8165 for (sec = elf_hash_table (info)->tls_sec;
8166 sec && (sec->flags & SEC_THREAD_LOCAL);
8167 sec = sec->next)
8169 bfd_size_type size = sec->size;
8171 if (size == 0
8172 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8174 struct bfd_link_order *o = sec->map_tail.link_order;
8175 if (o != NULL)
8176 size = o->offset + o->size;
8178 end = sec->vma + size;
8180 base = elf_hash_table (info)->tls_sec->vma;
8181 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8182 elf_hash_table (info)->tls_size = end - base;
8185 /* Reorder SHF_LINK_ORDER sections. */
8186 for (o = abfd->sections; o != NULL; o = o->next)
8188 if (!elf_fixup_link_order (abfd, o))
8189 return FALSE;
8192 /* Since ELF permits relocations to be against local symbols, we
8193 must have the local symbols available when we do the relocations.
8194 Since we would rather only read the local symbols once, and we
8195 would rather not keep them in memory, we handle all the
8196 relocations for a single input file at the same time.
8198 Unfortunately, there is no way to know the total number of local
8199 symbols until we have seen all of them, and the local symbol
8200 indices precede the global symbol indices. This means that when
8201 we are generating relocatable output, and we see a reloc against
8202 a global symbol, we can not know the symbol index until we have
8203 finished examining all the local symbols to see which ones we are
8204 going to output. To deal with this, we keep the relocations in
8205 memory, and don't output them until the end of the link. This is
8206 an unfortunate waste of memory, but I don't see a good way around
8207 it. Fortunately, it only happens when performing a relocatable
8208 link, which is not the common case. FIXME: If keep_memory is set
8209 we could write the relocs out and then read them again; I don't
8210 know how bad the memory loss will be. */
8212 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8213 sub->output_has_begun = FALSE;
8214 for (o = abfd->sections; o != NULL; o = o->next)
8216 for (p = o->map_head.link_order; p != NULL; p = p->next)
8218 if (p->type == bfd_indirect_link_order
8219 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8220 == bfd_target_elf_flavour)
8221 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8223 if (! sub->output_has_begun)
8225 if (! elf_link_input_bfd (&finfo, sub))
8226 goto error_return;
8227 sub->output_has_begun = TRUE;
8230 else if (p->type == bfd_section_reloc_link_order
8231 || p->type == bfd_symbol_reloc_link_order)
8233 if (! elf_reloc_link_order (abfd, info, o, p))
8234 goto error_return;
8236 else
8238 if (! _bfd_default_link_order (abfd, info, o, p))
8239 goto error_return;
8244 /* Output any global symbols that got converted to local in a
8245 version script or due to symbol visibility. We do this in a
8246 separate step since ELF requires all local symbols to appear
8247 prior to any global symbols. FIXME: We should only do this if
8248 some global symbols were, in fact, converted to become local.
8249 FIXME: Will this work correctly with the Irix 5 linker? */
8250 eoinfo.failed = FALSE;
8251 eoinfo.finfo = &finfo;
8252 eoinfo.localsyms = TRUE;
8253 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8254 &eoinfo);
8255 if (eoinfo.failed)
8256 return FALSE;
8258 /* That wrote out all the local symbols. Finish up the symbol table
8259 with the global symbols. Even if we want to strip everything we
8260 can, we still need to deal with those global symbols that got
8261 converted to local in a version script. */
8263 /* The sh_info field records the index of the first non local symbol. */
8264 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8266 if (dynamic
8267 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8269 Elf_Internal_Sym sym;
8270 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8271 long last_local = 0;
8273 /* Write out the section symbols for the output sections. */
8274 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8276 asection *s;
8278 sym.st_size = 0;
8279 sym.st_name = 0;
8280 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8281 sym.st_other = 0;
8283 for (s = abfd->sections; s != NULL; s = s->next)
8285 int indx;
8286 bfd_byte *dest;
8287 long dynindx;
8289 dynindx = elf_section_data (s)->dynindx;
8290 if (dynindx <= 0)
8291 continue;
8292 indx = elf_section_data (s)->this_idx;
8293 BFD_ASSERT (indx > 0);
8294 sym.st_shndx = indx;
8295 sym.st_value = s->vma;
8296 dest = dynsym + dynindx * bed->s->sizeof_sym;
8297 if (last_local < dynindx)
8298 last_local = dynindx;
8299 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8303 /* Write out the local dynsyms. */
8304 if (elf_hash_table (info)->dynlocal)
8306 struct elf_link_local_dynamic_entry *e;
8307 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8309 asection *s;
8310 bfd_byte *dest;
8312 sym.st_size = e->isym.st_size;
8313 sym.st_other = e->isym.st_other;
8315 /* Copy the internal symbol as is.
8316 Note that we saved a word of storage and overwrote
8317 the original st_name with the dynstr_index. */
8318 sym = e->isym;
8320 if (e->isym.st_shndx != SHN_UNDEF
8321 && (e->isym.st_shndx < SHN_LORESERVE
8322 || e->isym.st_shndx > SHN_HIRESERVE))
8324 s = bfd_section_from_elf_index (e->input_bfd,
8325 e->isym.st_shndx);
8327 sym.st_shndx =
8328 elf_section_data (s->output_section)->this_idx;
8329 sym.st_value = (s->output_section->vma
8330 + s->output_offset
8331 + e->isym.st_value);
8334 if (last_local < e->dynindx)
8335 last_local = e->dynindx;
8337 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8338 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8342 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8343 last_local + 1;
8346 /* We get the global symbols from the hash table. */
8347 eoinfo.failed = FALSE;
8348 eoinfo.localsyms = FALSE;
8349 eoinfo.finfo = &finfo;
8350 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8351 &eoinfo);
8352 if (eoinfo.failed)
8353 return FALSE;
8355 /* If backend needs to output some symbols not present in the hash
8356 table, do it now. */
8357 if (bed->elf_backend_output_arch_syms)
8359 typedef bfd_boolean (*out_sym_func)
8360 (void *, const char *, Elf_Internal_Sym *, asection *,
8361 struct elf_link_hash_entry *);
8363 if (! ((*bed->elf_backend_output_arch_syms)
8364 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8365 return FALSE;
8368 /* Flush all symbols to the file. */
8369 if (! elf_link_flush_output_syms (&finfo, bed))
8370 return FALSE;
8372 /* Now we know the size of the symtab section. */
8373 off += symtab_hdr->sh_size;
8375 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8376 if (symtab_shndx_hdr->sh_name != 0)
8378 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8379 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8380 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8381 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8382 symtab_shndx_hdr->sh_size = amt;
8384 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8385 off, TRUE);
8387 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8388 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8389 return FALSE;
8393 /* Finish up and write out the symbol string table (.strtab)
8394 section. */
8395 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8396 /* sh_name was set in prep_headers. */
8397 symstrtab_hdr->sh_type = SHT_STRTAB;
8398 symstrtab_hdr->sh_flags = 0;
8399 symstrtab_hdr->sh_addr = 0;
8400 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8401 symstrtab_hdr->sh_entsize = 0;
8402 symstrtab_hdr->sh_link = 0;
8403 symstrtab_hdr->sh_info = 0;
8404 /* sh_offset is set just below. */
8405 symstrtab_hdr->sh_addralign = 1;
8407 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8408 elf_tdata (abfd)->next_file_pos = off;
8410 if (bfd_get_symcount (abfd) > 0)
8412 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8413 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8414 return FALSE;
8417 /* Adjust the relocs to have the correct symbol indices. */
8418 for (o = abfd->sections; o != NULL; o = o->next)
8420 if ((o->flags & SEC_RELOC) == 0)
8421 continue;
8423 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8424 elf_section_data (o)->rel_count,
8425 elf_section_data (o)->rel_hashes);
8426 if (elf_section_data (o)->rel_hdr2 != NULL)
8427 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8428 elf_section_data (o)->rel_count2,
8429 (elf_section_data (o)->rel_hashes
8430 + elf_section_data (o)->rel_count));
8432 /* Set the reloc_count field to 0 to prevent write_relocs from
8433 trying to swap the relocs out itself. */
8434 o->reloc_count = 0;
8437 if (dynamic && info->combreloc && dynobj != NULL)
8438 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8440 /* If we are linking against a dynamic object, or generating a
8441 shared library, finish up the dynamic linking information. */
8442 if (dynamic)
8444 bfd_byte *dyncon, *dynconend;
8446 /* Fix up .dynamic entries. */
8447 o = bfd_get_section_by_name (dynobj, ".dynamic");
8448 BFD_ASSERT (o != NULL);
8450 dyncon = o->contents;
8451 dynconend = o->contents + o->size;
8452 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8454 Elf_Internal_Dyn dyn;
8455 const char *name;
8456 unsigned int type;
8458 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8460 switch (dyn.d_tag)
8462 default:
8463 continue;
8464 case DT_NULL:
8465 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8467 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8469 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8470 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8471 default: continue;
8473 dyn.d_un.d_val = relativecount;
8474 relativecount = 0;
8475 break;
8477 continue;
8479 case DT_INIT:
8480 name = info->init_function;
8481 goto get_sym;
8482 case DT_FINI:
8483 name = info->fini_function;
8484 get_sym:
8486 struct elf_link_hash_entry *h;
8488 h = elf_link_hash_lookup (elf_hash_table (info), name,
8489 FALSE, FALSE, TRUE);
8490 if (h != NULL
8491 && (h->root.type == bfd_link_hash_defined
8492 || h->root.type == bfd_link_hash_defweak))
8494 dyn.d_un.d_val = h->root.u.def.value;
8495 o = h->root.u.def.section;
8496 if (o->output_section != NULL)
8497 dyn.d_un.d_val += (o->output_section->vma
8498 + o->output_offset);
8499 else
8501 /* The symbol is imported from another shared
8502 library and does not apply to this one. */
8503 dyn.d_un.d_val = 0;
8505 break;
8508 continue;
8510 case DT_PREINIT_ARRAYSZ:
8511 name = ".preinit_array";
8512 goto get_size;
8513 case DT_INIT_ARRAYSZ:
8514 name = ".init_array";
8515 goto get_size;
8516 case DT_FINI_ARRAYSZ:
8517 name = ".fini_array";
8518 get_size:
8519 o = bfd_get_section_by_name (abfd, name);
8520 if (o == NULL)
8522 (*_bfd_error_handler)
8523 (_("%B: could not find output section %s"), abfd, name);
8524 goto error_return;
8526 if (o->size == 0)
8527 (*_bfd_error_handler)
8528 (_("warning: %s section has zero size"), name);
8529 dyn.d_un.d_val = o->size;
8530 break;
8532 case DT_PREINIT_ARRAY:
8533 name = ".preinit_array";
8534 goto get_vma;
8535 case DT_INIT_ARRAY:
8536 name = ".init_array";
8537 goto get_vma;
8538 case DT_FINI_ARRAY:
8539 name = ".fini_array";
8540 goto get_vma;
8542 case DT_HASH:
8543 name = ".hash";
8544 goto get_vma;
8545 case DT_STRTAB:
8546 name = ".dynstr";
8547 goto get_vma;
8548 case DT_SYMTAB:
8549 name = ".dynsym";
8550 goto get_vma;
8551 case DT_VERDEF:
8552 name = ".gnu.version_d";
8553 goto get_vma;
8554 case DT_VERNEED:
8555 name = ".gnu.version_r";
8556 goto get_vma;
8557 case DT_VERSYM:
8558 name = ".gnu.version";
8559 get_vma:
8560 o = bfd_get_section_by_name (abfd, name);
8561 if (o == NULL)
8563 (*_bfd_error_handler)
8564 (_("%B: could not find output section %s"), abfd, name);
8565 goto error_return;
8567 dyn.d_un.d_ptr = o->vma;
8568 break;
8570 case DT_REL:
8571 case DT_RELA:
8572 case DT_RELSZ:
8573 case DT_RELASZ:
8574 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8575 type = SHT_REL;
8576 else
8577 type = SHT_RELA;
8578 dyn.d_un.d_val = 0;
8579 for (i = 1; i < elf_numsections (abfd); i++)
8581 Elf_Internal_Shdr *hdr;
8583 hdr = elf_elfsections (abfd)[i];
8584 if (hdr->sh_type == type
8585 && (hdr->sh_flags & SHF_ALLOC) != 0)
8587 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8588 dyn.d_un.d_val += hdr->sh_size;
8589 else
8591 if (dyn.d_un.d_val == 0
8592 || hdr->sh_addr < dyn.d_un.d_val)
8593 dyn.d_un.d_val = hdr->sh_addr;
8597 break;
8599 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8603 /* If we have created any dynamic sections, then output them. */
8604 if (dynobj != NULL)
8606 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8607 goto error_return;
8609 for (o = dynobj->sections; o != NULL; o = o->next)
8611 if ((o->flags & SEC_HAS_CONTENTS) == 0
8612 || o->size == 0
8613 || o->output_section == bfd_abs_section_ptr)
8614 continue;
8615 if ((o->flags & SEC_LINKER_CREATED) == 0)
8617 /* At this point, we are only interested in sections
8618 created by _bfd_elf_link_create_dynamic_sections. */
8619 continue;
8621 if (elf_hash_table (info)->stab_info.stabstr == o)
8622 continue;
8623 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8624 continue;
8625 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8626 != SHT_STRTAB)
8627 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8629 if (! bfd_set_section_contents (abfd, o->output_section,
8630 o->contents,
8631 (file_ptr) o->output_offset,
8632 o->size))
8633 goto error_return;
8635 else
8637 /* The contents of the .dynstr section are actually in a
8638 stringtab. */
8639 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8640 if (bfd_seek (abfd, off, SEEK_SET) != 0
8641 || ! _bfd_elf_strtab_emit (abfd,
8642 elf_hash_table (info)->dynstr))
8643 goto error_return;
8648 if (info->relocatable)
8650 bfd_boolean failed = FALSE;
8652 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8653 if (failed)
8654 goto error_return;
8657 /* If we have optimized stabs strings, output them. */
8658 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8660 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8661 goto error_return;
8664 if (info->eh_frame_hdr)
8666 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8667 goto error_return;
8670 if (finfo.symstrtab != NULL)
8671 _bfd_stringtab_free (finfo.symstrtab);
8672 if (finfo.contents != NULL)
8673 free (finfo.contents);
8674 if (finfo.external_relocs != NULL)
8675 free (finfo.external_relocs);
8676 if (finfo.internal_relocs != NULL)
8677 free (finfo.internal_relocs);
8678 if (finfo.external_syms != NULL)
8679 free (finfo.external_syms);
8680 if (finfo.locsym_shndx != NULL)
8681 free (finfo.locsym_shndx);
8682 if (finfo.internal_syms != NULL)
8683 free (finfo.internal_syms);
8684 if (finfo.indices != NULL)
8685 free (finfo.indices);
8686 if (finfo.sections != NULL)
8687 free (finfo.sections);
8688 if (finfo.symbuf != NULL)
8689 free (finfo.symbuf);
8690 if (finfo.symshndxbuf != NULL)
8691 free (finfo.symshndxbuf);
8692 for (o = abfd->sections; o != NULL; o = o->next)
8694 if ((o->flags & SEC_RELOC) != 0
8695 && elf_section_data (o)->rel_hashes != NULL)
8696 free (elf_section_data (o)->rel_hashes);
8699 elf_tdata (abfd)->linker = TRUE;
8701 return TRUE;
8703 error_return:
8704 if (finfo.symstrtab != NULL)
8705 _bfd_stringtab_free (finfo.symstrtab);
8706 if (finfo.contents != NULL)
8707 free (finfo.contents);
8708 if (finfo.external_relocs != NULL)
8709 free (finfo.external_relocs);
8710 if (finfo.internal_relocs != NULL)
8711 free (finfo.internal_relocs);
8712 if (finfo.external_syms != NULL)
8713 free (finfo.external_syms);
8714 if (finfo.locsym_shndx != NULL)
8715 free (finfo.locsym_shndx);
8716 if (finfo.internal_syms != NULL)
8717 free (finfo.internal_syms);
8718 if (finfo.indices != NULL)
8719 free (finfo.indices);
8720 if (finfo.sections != NULL)
8721 free (finfo.sections);
8722 if (finfo.symbuf != NULL)
8723 free (finfo.symbuf);
8724 if (finfo.symshndxbuf != NULL)
8725 free (finfo.symshndxbuf);
8726 for (o = abfd->sections; o != NULL; o = o->next)
8728 if ((o->flags & SEC_RELOC) != 0
8729 && elf_section_data (o)->rel_hashes != NULL)
8730 free (elf_section_data (o)->rel_hashes);
8733 return FALSE;
8736 /* Garbage collect unused sections. */
8738 /* The mark phase of garbage collection. For a given section, mark
8739 it and any sections in this section's group, and all the sections
8740 which define symbols to which it refers. */
8742 typedef asection * (*gc_mark_hook_fn)
8743 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8744 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8746 bfd_boolean
8747 _bfd_elf_gc_mark (struct bfd_link_info *info,
8748 asection *sec,
8749 gc_mark_hook_fn gc_mark_hook)
8751 bfd_boolean ret;
8752 bfd_boolean is_eh;
8753 asection *group_sec;
8755 sec->gc_mark = 1;
8757 /* Mark all the sections in the group. */
8758 group_sec = elf_section_data (sec)->next_in_group;
8759 if (group_sec && !group_sec->gc_mark)
8760 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8761 return FALSE;
8763 /* Look through the section relocs. */
8764 ret = TRUE;
8765 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8766 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8768 Elf_Internal_Rela *relstart, *rel, *relend;
8769 Elf_Internal_Shdr *symtab_hdr;
8770 struct elf_link_hash_entry **sym_hashes;
8771 size_t nlocsyms;
8772 size_t extsymoff;
8773 bfd *input_bfd = sec->owner;
8774 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8775 Elf_Internal_Sym *isym = NULL;
8776 int r_sym_shift;
8778 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8779 sym_hashes = elf_sym_hashes (input_bfd);
8781 /* Read the local symbols. */
8782 if (elf_bad_symtab (input_bfd))
8784 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8785 extsymoff = 0;
8787 else
8788 extsymoff = nlocsyms = symtab_hdr->sh_info;
8790 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8791 if (isym == NULL && nlocsyms != 0)
8793 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8794 NULL, NULL, NULL);
8795 if (isym == NULL)
8796 return FALSE;
8799 /* Read the relocations. */
8800 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8801 info->keep_memory);
8802 if (relstart == NULL)
8804 ret = FALSE;
8805 goto out1;
8807 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8809 if (bed->s->arch_size == 32)
8810 r_sym_shift = 8;
8811 else
8812 r_sym_shift = 32;
8814 for (rel = relstart; rel < relend; rel++)
8816 unsigned long r_symndx;
8817 asection *rsec;
8818 struct elf_link_hash_entry *h;
8820 r_symndx = rel->r_info >> r_sym_shift;
8821 if (r_symndx == 0)
8822 continue;
8824 if (r_symndx >= nlocsyms
8825 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8827 h = sym_hashes[r_symndx - extsymoff];
8828 while (h->root.type == bfd_link_hash_indirect
8829 || h->root.type == bfd_link_hash_warning)
8830 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8831 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8833 else
8835 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8838 if (rsec && !rsec->gc_mark)
8840 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8841 rsec->gc_mark = 1;
8842 else if (is_eh)
8843 rsec->gc_mark_from_eh = 1;
8844 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8846 ret = FALSE;
8847 goto out2;
8852 out2:
8853 if (elf_section_data (sec)->relocs != relstart)
8854 free (relstart);
8855 out1:
8856 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8858 if (! info->keep_memory)
8859 free (isym);
8860 else
8861 symtab_hdr->contents = (unsigned char *) isym;
8865 return ret;
8868 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8870 struct elf_gc_sweep_symbol_info {
8871 struct bfd_link_info *info;
8872 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8873 bfd_boolean);
8876 static bfd_boolean
8877 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8879 if (h->root.type == bfd_link_hash_warning)
8880 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8882 if ((h->root.type == bfd_link_hash_defined
8883 || h->root.type == bfd_link_hash_defweak)
8884 && !h->root.u.def.section->gc_mark
8885 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8887 struct elf_gc_sweep_symbol_info *inf = data;
8888 (*inf->hide_symbol) (inf->info, h, TRUE);
8891 return TRUE;
8894 /* The sweep phase of garbage collection. Remove all garbage sections. */
8896 typedef bfd_boolean (*gc_sweep_hook_fn)
8897 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8899 static bfd_boolean
8900 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8902 bfd *sub;
8903 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8904 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8905 unsigned long section_sym_count;
8906 struct elf_gc_sweep_symbol_info sweep_info;
8908 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8910 asection *o;
8912 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8913 continue;
8915 for (o = sub->sections; o != NULL; o = o->next)
8917 /* Keep debug and special sections. */
8918 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8919 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8920 o->gc_mark = 1;
8922 if (o->gc_mark)
8923 continue;
8925 /* Skip sweeping sections already excluded. */
8926 if (o->flags & SEC_EXCLUDE)
8927 continue;
8929 /* Since this is early in the link process, it is simple
8930 to remove a section from the output. */
8931 o->flags |= SEC_EXCLUDE;
8933 /* But we also have to update some of the relocation
8934 info we collected before. */
8935 if (gc_sweep_hook
8936 && (o->flags & SEC_RELOC) != 0
8937 && o->reloc_count > 0
8938 && !bfd_is_abs_section (o->output_section))
8940 Elf_Internal_Rela *internal_relocs;
8941 bfd_boolean r;
8943 internal_relocs
8944 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8945 info->keep_memory);
8946 if (internal_relocs == NULL)
8947 return FALSE;
8949 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8951 if (elf_section_data (o)->relocs != internal_relocs)
8952 free (internal_relocs);
8954 if (!r)
8955 return FALSE;
8960 /* Remove the symbols that were in the swept sections from the dynamic
8961 symbol table. GCFIXME: Anyone know how to get them out of the
8962 static symbol table as well? */
8963 sweep_info.info = info;
8964 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8965 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8966 &sweep_info);
8968 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
8969 return TRUE;
8972 /* Propagate collected vtable information. This is called through
8973 elf_link_hash_traverse. */
8975 static bfd_boolean
8976 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8978 if (h->root.type == bfd_link_hash_warning)
8979 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8981 /* Those that are not vtables. */
8982 if (h->vtable == NULL || h->vtable->parent == NULL)
8983 return TRUE;
8985 /* Those vtables that do not have parents, we cannot merge. */
8986 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8987 return TRUE;
8989 /* If we've already been done, exit. */
8990 if (h->vtable->used && h->vtable->used[-1])
8991 return TRUE;
8993 /* Make sure the parent's table is up to date. */
8994 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8996 if (h->vtable->used == NULL)
8998 /* None of this table's entries were referenced. Re-use the
8999 parent's table. */
9000 h->vtable->used = h->vtable->parent->vtable->used;
9001 h->vtable->size = h->vtable->parent->vtable->size;
9003 else
9005 size_t n;
9006 bfd_boolean *cu, *pu;
9008 /* Or the parent's entries into ours. */
9009 cu = h->vtable->used;
9010 cu[-1] = TRUE;
9011 pu = h->vtable->parent->vtable->used;
9012 if (pu != NULL)
9014 const struct elf_backend_data *bed;
9015 unsigned int log_file_align;
9017 bed = get_elf_backend_data (h->root.u.def.section->owner);
9018 log_file_align = bed->s->log_file_align;
9019 n = h->vtable->parent->vtable->size >> log_file_align;
9020 while (n--)
9022 if (*pu)
9023 *cu = TRUE;
9024 pu++;
9025 cu++;
9030 return TRUE;
9033 static bfd_boolean
9034 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9036 asection *sec;
9037 bfd_vma hstart, hend;
9038 Elf_Internal_Rela *relstart, *relend, *rel;
9039 const struct elf_backend_data *bed;
9040 unsigned int log_file_align;
9042 if (h->root.type == bfd_link_hash_warning)
9043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9045 /* Take care of both those symbols that do not describe vtables as
9046 well as those that are not loaded. */
9047 if (h->vtable == NULL || h->vtable->parent == NULL)
9048 return TRUE;
9050 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9051 || h->root.type == bfd_link_hash_defweak);
9053 sec = h->root.u.def.section;
9054 hstart = h->root.u.def.value;
9055 hend = hstart + h->size;
9057 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9058 if (!relstart)
9059 return *(bfd_boolean *) okp = FALSE;
9060 bed = get_elf_backend_data (sec->owner);
9061 log_file_align = bed->s->log_file_align;
9063 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9065 for (rel = relstart; rel < relend; ++rel)
9066 if (rel->r_offset >= hstart && rel->r_offset < hend)
9068 /* If the entry is in use, do nothing. */
9069 if (h->vtable->used
9070 && (rel->r_offset - hstart) < h->vtable->size)
9072 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9073 if (h->vtable->used[entry])
9074 continue;
9076 /* Otherwise, kill it. */
9077 rel->r_offset = rel->r_info = rel->r_addend = 0;
9080 return TRUE;
9083 /* Mark sections containing dynamically referenced symbols. When
9084 building shared libraries, we must assume that any visible symbol is
9085 referenced. */
9087 bfd_boolean
9088 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9090 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9092 if (h->root.type == bfd_link_hash_warning)
9093 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9095 if ((h->root.type == bfd_link_hash_defined
9096 || h->root.type == bfd_link_hash_defweak)
9097 && (h->ref_dynamic
9098 || (!info->executable
9099 && h->def_regular
9100 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9101 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9102 h->root.u.def.section->flags |= SEC_KEEP;
9104 return TRUE;
9107 /* Do mark and sweep of unused sections. */
9109 bfd_boolean
9110 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9112 bfd_boolean ok = TRUE;
9113 bfd *sub;
9114 asection * (*gc_mark_hook)
9115 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9116 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9117 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9119 if (!bed->can_gc_sections
9120 || info->relocatable
9121 || info->emitrelocations
9122 || !is_elf_hash_table (info->hash))
9124 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9125 return TRUE;
9128 /* Apply transitive closure to the vtable entry usage info. */
9129 elf_link_hash_traverse (elf_hash_table (info),
9130 elf_gc_propagate_vtable_entries_used,
9131 &ok);
9132 if (!ok)
9133 return FALSE;
9135 /* Kill the vtable relocations that were not used. */
9136 elf_link_hash_traverse (elf_hash_table (info),
9137 elf_gc_smash_unused_vtentry_relocs,
9138 &ok);
9139 if (!ok)
9140 return FALSE;
9142 /* Mark dynamically referenced symbols. */
9143 if (elf_hash_table (info)->dynamic_sections_created)
9144 elf_link_hash_traverse (elf_hash_table (info),
9145 bed->gc_mark_dynamic_ref,
9146 info);
9148 /* Grovel through relocs to find out who stays ... */
9149 gc_mark_hook = bed->gc_mark_hook;
9150 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9152 asection *o;
9154 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9155 continue;
9157 for (o = sub->sections; o != NULL; o = o->next)
9158 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9159 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9160 return FALSE;
9163 /* ... again for sections marked from eh_frame. */
9164 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9166 asection *o;
9168 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9169 continue;
9171 /* Keep .gcc_except_table.* if the associated .text.* is
9172 marked. This isn't very nice, but the proper solution,
9173 splitting .eh_frame up and using comdat doesn't pan out
9174 easily due to needing special relocs to handle the
9175 difference of two symbols in separate sections.
9176 Don't keep code sections referenced by .eh_frame. */
9177 for (o = sub->sections; o != NULL; o = o->next)
9178 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9180 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9182 unsigned long len;
9183 char *fn_name;
9184 asection *fn_text;
9186 len = strlen (o->name + 18) + 1;
9187 fn_name = bfd_malloc (len + 6);
9188 if (fn_name == NULL)
9189 return FALSE;
9190 memcpy (fn_name, ".text.", 6);
9191 memcpy (fn_name + 6, o->name + 18, len);
9192 fn_text = bfd_get_section_by_name (sub, fn_name);
9193 free (fn_name);
9194 if (fn_text == NULL || !fn_text->gc_mark)
9195 continue;
9198 /* If not using specially named exception table section,
9199 then keep whatever we are using. */
9200 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9201 return FALSE;
9205 /* ... and mark SEC_EXCLUDE for those that go. */
9206 return elf_gc_sweep (abfd, info);
9209 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9211 bfd_boolean
9212 bfd_elf_gc_record_vtinherit (bfd *abfd,
9213 asection *sec,
9214 struct elf_link_hash_entry *h,
9215 bfd_vma offset)
9217 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9218 struct elf_link_hash_entry **search, *child;
9219 bfd_size_type extsymcount;
9220 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9222 /* The sh_info field of the symtab header tells us where the
9223 external symbols start. We don't care about the local symbols at
9224 this point. */
9225 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9226 if (!elf_bad_symtab (abfd))
9227 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9229 sym_hashes = elf_sym_hashes (abfd);
9230 sym_hashes_end = sym_hashes + extsymcount;
9232 /* Hunt down the child symbol, which is in this section at the same
9233 offset as the relocation. */
9234 for (search = sym_hashes; search != sym_hashes_end; ++search)
9236 if ((child = *search) != NULL
9237 && (child->root.type == bfd_link_hash_defined
9238 || child->root.type == bfd_link_hash_defweak)
9239 && child->root.u.def.section == sec
9240 && child->root.u.def.value == offset)
9241 goto win;
9244 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9245 abfd, sec, (unsigned long) offset);
9246 bfd_set_error (bfd_error_invalid_operation);
9247 return FALSE;
9249 win:
9250 if (!child->vtable)
9252 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9253 if (!child->vtable)
9254 return FALSE;
9256 if (!h)
9258 /* This *should* only be the absolute section. It could potentially
9259 be that someone has defined a non-global vtable though, which
9260 would be bad. It isn't worth paging in the local symbols to be
9261 sure though; that case should simply be handled by the assembler. */
9263 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9265 else
9266 child->vtable->parent = h;
9268 return TRUE;
9271 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9273 bfd_boolean
9274 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9275 asection *sec ATTRIBUTE_UNUSED,
9276 struct elf_link_hash_entry *h,
9277 bfd_vma addend)
9279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9280 unsigned int log_file_align = bed->s->log_file_align;
9282 if (!h->vtable)
9284 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9285 if (!h->vtable)
9286 return FALSE;
9289 if (addend >= h->vtable->size)
9291 size_t size, bytes, file_align;
9292 bfd_boolean *ptr = h->vtable->used;
9294 /* While the symbol is undefined, we have to be prepared to handle
9295 a zero size. */
9296 file_align = 1 << log_file_align;
9297 if (h->root.type == bfd_link_hash_undefined)
9298 size = addend + file_align;
9299 else
9301 size = h->size;
9302 if (addend >= size)
9304 /* Oops! We've got a reference past the defined end of
9305 the table. This is probably a bug -- shall we warn? */
9306 size = addend + file_align;
9309 size = (size + file_align - 1) & -file_align;
9311 /* Allocate one extra entry for use as a "done" flag for the
9312 consolidation pass. */
9313 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9315 if (ptr)
9317 ptr = bfd_realloc (ptr - 1, bytes);
9319 if (ptr != NULL)
9321 size_t oldbytes;
9323 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9324 * sizeof (bfd_boolean));
9325 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9328 else
9329 ptr = bfd_zmalloc (bytes);
9331 if (ptr == NULL)
9332 return FALSE;
9334 /* And arrange for that done flag to be at index -1. */
9335 h->vtable->used = ptr + 1;
9336 h->vtable->size = size;
9339 h->vtable->used[addend >> log_file_align] = TRUE;
9341 return TRUE;
9344 struct alloc_got_off_arg {
9345 bfd_vma gotoff;
9346 unsigned int got_elt_size;
9349 /* We need a special top-level link routine to convert got reference counts
9350 to real got offsets. */
9352 static bfd_boolean
9353 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9355 struct alloc_got_off_arg *gofarg = arg;
9357 if (h->root.type == bfd_link_hash_warning)
9358 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9360 if (h->got.refcount > 0)
9362 h->got.offset = gofarg->gotoff;
9363 gofarg->gotoff += gofarg->got_elt_size;
9365 else
9366 h->got.offset = (bfd_vma) -1;
9368 return TRUE;
9371 /* And an accompanying bit to work out final got entry offsets once
9372 we're done. Should be called from final_link. */
9374 bfd_boolean
9375 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9376 struct bfd_link_info *info)
9378 bfd *i;
9379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9380 bfd_vma gotoff;
9381 unsigned int got_elt_size = bed->s->arch_size / 8;
9382 struct alloc_got_off_arg gofarg;
9384 if (! is_elf_hash_table (info->hash))
9385 return FALSE;
9387 /* The GOT offset is relative to the .got section, but the GOT header is
9388 put into the .got.plt section, if the backend uses it. */
9389 if (bed->want_got_plt)
9390 gotoff = 0;
9391 else
9392 gotoff = bed->got_header_size;
9394 /* Do the local .got entries first. */
9395 for (i = info->input_bfds; i; i = i->link_next)
9397 bfd_signed_vma *local_got;
9398 bfd_size_type j, locsymcount;
9399 Elf_Internal_Shdr *symtab_hdr;
9401 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9402 continue;
9404 local_got = elf_local_got_refcounts (i);
9405 if (!local_got)
9406 continue;
9408 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9409 if (elf_bad_symtab (i))
9410 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9411 else
9412 locsymcount = symtab_hdr->sh_info;
9414 for (j = 0; j < locsymcount; ++j)
9416 if (local_got[j] > 0)
9418 local_got[j] = gotoff;
9419 gotoff += got_elt_size;
9421 else
9422 local_got[j] = (bfd_vma) -1;
9426 /* Then the global .got entries. .plt refcounts are handled by
9427 adjust_dynamic_symbol */
9428 gofarg.gotoff = gotoff;
9429 gofarg.got_elt_size = got_elt_size;
9430 elf_link_hash_traverse (elf_hash_table (info),
9431 elf_gc_allocate_got_offsets,
9432 &gofarg);
9433 return TRUE;
9436 /* Many folk need no more in the way of final link than this, once
9437 got entry reference counting is enabled. */
9439 bfd_boolean
9440 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9442 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9443 return FALSE;
9445 /* Invoke the regular ELF backend linker to do all the work. */
9446 return bfd_elf_final_link (abfd, info);
9449 bfd_boolean
9450 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9452 struct elf_reloc_cookie *rcookie = cookie;
9454 if (rcookie->bad_symtab)
9455 rcookie->rel = rcookie->rels;
9457 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9459 unsigned long r_symndx;
9461 if (! rcookie->bad_symtab)
9462 if (rcookie->rel->r_offset > offset)
9463 return FALSE;
9464 if (rcookie->rel->r_offset != offset)
9465 continue;
9467 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9468 if (r_symndx == SHN_UNDEF)
9469 return TRUE;
9471 if (r_symndx >= rcookie->locsymcount
9472 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9474 struct elf_link_hash_entry *h;
9476 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9478 while (h->root.type == bfd_link_hash_indirect
9479 || h->root.type == bfd_link_hash_warning)
9480 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9482 if ((h->root.type == bfd_link_hash_defined
9483 || h->root.type == bfd_link_hash_defweak)
9484 && elf_discarded_section (h->root.u.def.section))
9485 return TRUE;
9486 else
9487 return FALSE;
9489 else
9491 /* It's not a relocation against a global symbol,
9492 but it could be a relocation against a local
9493 symbol for a discarded section. */
9494 asection *isec;
9495 Elf_Internal_Sym *isym;
9497 /* Need to: get the symbol; get the section. */
9498 isym = &rcookie->locsyms[r_symndx];
9499 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9501 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9502 if (isec != NULL && elf_discarded_section (isec))
9503 return TRUE;
9506 return FALSE;
9508 return FALSE;
9511 /* Discard unneeded references to discarded sections.
9512 Returns TRUE if any section's size was changed. */
9513 /* This function assumes that the relocations are in sorted order,
9514 which is true for all known assemblers. */
9516 bfd_boolean
9517 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9519 struct elf_reloc_cookie cookie;
9520 asection *stab, *eh;
9521 Elf_Internal_Shdr *symtab_hdr;
9522 const struct elf_backend_data *bed;
9523 bfd *abfd;
9524 unsigned int count;
9525 bfd_boolean ret = FALSE;
9527 if (info->traditional_format
9528 || !is_elf_hash_table (info->hash))
9529 return FALSE;
9531 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9533 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9534 continue;
9536 bed = get_elf_backend_data (abfd);
9538 if ((abfd->flags & DYNAMIC) != 0)
9539 continue;
9541 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9542 if (info->relocatable
9543 || (eh != NULL
9544 && (eh->size == 0
9545 || bfd_is_abs_section (eh->output_section))))
9546 eh = NULL;
9548 stab = bfd_get_section_by_name (abfd, ".stab");
9549 if (stab != NULL
9550 && (stab->size == 0
9551 || bfd_is_abs_section (stab->output_section)
9552 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9553 stab = NULL;
9555 if (stab == NULL
9556 && eh == NULL
9557 && bed->elf_backend_discard_info == NULL)
9558 continue;
9560 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9561 cookie.abfd = abfd;
9562 cookie.sym_hashes = elf_sym_hashes (abfd);
9563 cookie.bad_symtab = elf_bad_symtab (abfd);
9564 if (cookie.bad_symtab)
9566 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9567 cookie.extsymoff = 0;
9569 else
9571 cookie.locsymcount = symtab_hdr->sh_info;
9572 cookie.extsymoff = symtab_hdr->sh_info;
9575 if (bed->s->arch_size == 32)
9576 cookie.r_sym_shift = 8;
9577 else
9578 cookie.r_sym_shift = 32;
9580 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9581 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9583 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9584 cookie.locsymcount, 0,
9585 NULL, NULL, NULL);
9586 if (cookie.locsyms == NULL)
9587 return FALSE;
9590 if (stab != NULL)
9592 cookie.rels = NULL;
9593 count = stab->reloc_count;
9594 if (count != 0)
9595 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9596 info->keep_memory);
9597 if (cookie.rels != NULL)
9599 cookie.rel = cookie.rels;
9600 cookie.relend = cookie.rels;
9601 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9602 if (_bfd_discard_section_stabs (abfd, stab,
9603 elf_section_data (stab)->sec_info,
9604 bfd_elf_reloc_symbol_deleted_p,
9605 &cookie))
9606 ret = TRUE;
9607 if (elf_section_data (stab)->relocs != cookie.rels)
9608 free (cookie.rels);
9612 if (eh != NULL)
9614 cookie.rels = NULL;
9615 count = eh->reloc_count;
9616 if (count != 0)
9617 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9618 info->keep_memory);
9619 cookie.rel = cookie.rels;
9620 cookie.relend = cookie.rels;
9621 if (cookie.rels != NULL)
9622 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9624 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9625 bfd_elf_reloc_symbol_deleted_p,
9626 &cookie))
9627 ret = TRUE;
9629 if (cookie.rels != NULL
9630 && elf_section_data (eh)->relocs != cookie.rels)
9631 free (cookie.rels);
9634 if (bed->elf_backend_discard_info != NULL
9635 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9636 ret = TRUE;
9638 if (cookie.locsyms != NULL
9639 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9641 if (! info->keep_memory)
9642 free (cookie.locsyms);
9643 else
9644 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9648 if (info->eh_frame_hdr
9649 && !info->relocatable
9650 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9651 ret = TRUE;
9653 return ret;
9656 void
9657 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9659 flagword flags;
9660 const char *name, *p;
9661 struct bfd_section_already_linked *l;
9662 struct bfd_section_already_linked_hash_entry *already_linked_list;
9663 asection *group;
9665 /* A single member comdat group section may be discarded by a
9666 linkonce section. See below. */
9667 if (sec->output_section == bfd_abs_section_ptr)
9668 return;
9670 flags = sec->flags;
9672 /* Check if it belongs to a section group. */
9673 group = elf_sec_group (sec);
9675 /* Return if it isn't a linkonce section nor a member of a group. A
9676 comdat group section also has SEC_LINK_ONCE set. */
9677 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9678 return;
9680 if (group)
9682 /* If this is the member of a single member comdat group, check if
9683 the group should be discarded. */
9684 if (elf_next_in_group (sec) == sec
9685 && (group->flags & SEC_LINK_ONCE) != 0)
9686 sec = group;
9687 else
9688 return;
9691 /* FIXME: When doing a relocatable link, we may have trouble
9692 copying relocations in other sections that refer to local symbols
9693 in the section being discarded. Those relocations will have to
9694 be converted somehow; as of this writing I'm not sure that any of
9695 the backends handle that correctly.
9697 It is tempting to instead not discard link once sections when
9698 doing a relocatable link (technically, they should be discarded
9699 whenever we are building constructors). However, that fails,
9700 because the linker winds up combining all the link once sections
9701 into a single large link once section, which defeats the purpose
9702 of having link once sections in the first place.
9704 Also, not merging link once sections in a relocatable link
9705 causes trouble for MIPS ELF, which relies on link once semantics
9706 to handle the .reginfo section correctly. */
9708 name = bfd_get_section_name (abfd, sec);
9710 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9711 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9712 p++;
9713 else
9714 p = name;
9716 already_linked_list = bfd_section_already_linked_table_lookup (p);
9718 for (l = already_linked_list->entry; l != NULL; l = l->next)
9720 /* We may have 3 different sections on the list: group section,
9721 comdat section and linkonce section. SEC may be a linkonce or
9722 group section. We match a group section with a group section,
9723 a linkonce section with a linkonce section, and ignore comdat
9724 section. */
9725 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9726 && strcmp (name, l->sec->name) == 0
9727 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9729 /* The section has already been linked. See if we should
9730 issue a warning. */
9731 switch (flags & SEC_LINK_DUPLICATES)
9733 default:
9734 abort ();
9736 case SEC_LINK_DUPLICATES_DISCARD:
9737 break;
9739 case SEC_LINK_DUPLICATES_ONE_ONLY:
9740 (*_bfd_error_handler)
9741 (_("%B: ignoring duplicate section `%A'"),
9742 abfd, sec);
9743 break;
9745 case SEC_LINK_DUPLICATES_SAME_SIZE:
9746 if (sec->size != l->sec->size)
9747 (*_bfd_error_handler)
9748 (_("%B: duplicate section `%A' has different size"),
9749 abfd, sec);
9750 break;
9752 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9753 if (sec->size != l->sec->size)
9754 (*_bfd_error_handler)
9755 (_("%B: duplicate section `%A' has different size"),
9756 abfd, sec);
9757 else if (sec->size != 0)
9759 bfd_byte *sec_contents, *l_sec_contents;
9761 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9762 (*_bfd_error_handler)
9763 (_("%B: warning: could not read contents of section `%A'"),
9764 abfd, sec);
9765 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9766 &l_sec_contents))
9767 (*_bfd_error_handler)
9768 (_("%B: warning: could not read contents of section `%A'"),
9769 l->sec->owner, l->sec);
9770 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9771 (*_bfd_error_handler)
9772 (_("%B: warning: duplicate section `%A' has different contents"),
9773 abfd, sec);
9775 if (sec_contents)
9776 free (sec_contents);
9777 if (l_sec_contents)
9778 free (l_sec_contents);
9780 break;
9783 /* Set the output_section field so that lang_add_section
9784 does not create a lang_input_section structure for this
9785 section. Since there might be a symbol in the section
9786 being discarded, we must retain a pointer to the section
9787 which we are really going to use. */
9788 sec->output_section = bfd_abs_section_ptr;
9789 sec->kept_section = l->sec;
9791 if (flags & SEC_GROUP)
9793 asection *first = elf_next_in_group (sec);
9794 asection *s = first;
9796 while (s != NULL)
9798 s->output_section = bfd_abs_section_ptr;
9799 /* Record which group discards it. */
9800 s->kept_section = l->sec;
9801 s = elf_next_in_group (s);
9802 /* These lists are circular. */
9803 if (s == first)
9804 break;
9808 return;
9812 if (group)
9814 /* If this is the member of a single member comdat group and the
9815 group hasn't be discarded, we check if it matches a linkonce
9816 section. We only record the discarded comdat group. Otherwise
9817 the undiscarded group will be discarded incorrectly later since
9818 itself has been recorded. */
9819 for (l = already_linked_list->entry; l != NULL; l = l->next)
9820 if ((l->sec->flags & SEC_GROUP) == 0
9821 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9822 && bfd_elf_match_symbols_in_sections (l->sec,
9823 elf_next_in_group (sec)))
9825 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9826 elf_next_in_group (sec)->kept_section = l->sec;
9827 group->output_section = bfd_abs_section_ptr;
9828 break;
9830 if (l == NULL)
9831 return;
9833 else
9834 /* There is no direct match. But for linkonce section, we should
9835 check if there is a match with comdat group member. We always
9836 record the linkonce section, discarded or not. */
9837 for (l = already_linked_list->entry; l != NULL; l = l->next)
9838 if (l->sec->flags & SEC_GROUP)
9840 asection *first = elf_next_in_group (l->sec);
9842 if (first != NULL
9843 && elf_next_in_group (first) == first
9844 && bfd_elf_match_symbols_in_sections (first, sec))
9846 sec->output_section = bfd_abs_section_ptr;
9847 sec->kept_section = l->sec;
9848 break;
9852 /* This is the first section with this name. Record it. */
9853 bfd_section_already_linked_table_insert (already_linked_list, sec);
9856 bfd_boolean
9857 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9859 return sym->st_shndx == SHN_COMMON;
9862 unsigned int
9863 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9865 return SHN_COMMON;
9868 asection *
9869 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9871 return bfd_com_section_ptr;