binutils/testsuite/
[binutils.git] / bfd / elflink.c
blob1fcfce352d8989abc781b6dd48cd0dd40880cdfe
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 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 asection *s;
267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
269 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270 .rel[a].bss sections. */
271 flags = bed->dynamic_sec_flags;
273 pltflags = flags;
274 if (bed->plt_not_loaded)
275 /* We do not clear SEC_ALLOC here because we still want the OS to
276 allocate space for the section; it's just that there's nothing
277 to read in from the object file. */
278 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
279 else
280 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
281 if (bed->plt_readonly)
282 pltflags |= SEC_READONLY;
284 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
285 if (s == NULL
286 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
287 return FALSE;
289 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
290 .plt section. */
291 if (bed->want_plt_sym
292 && !_bfd_elf_define_linkage_sym (abfd, info, s,
293 "_PROCEDURE_LINKAGE_TABLE_"))
294 return FALSE;
296 s = bfd_make_section_with_flags (abfd,
297 (bed->default_use_rela_p
298 ? ".rela.plt" : ".rel.plt"),
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
304 if (! _bfd_elf_create_got_section (abfd, info))
305 return FALSE;
307 if (bed->want_dynbss)
309 /* The .dynbss section is a place to put symbols which are defined
310 by dynamic objects, are referenced by regular objects, and are
311 not functions. We must allocate space for them in the process
312 image and use a R_*_COPY reloc to tell the dynamic linker to
313 initialize them at run time. The linker script puts the .dynbss
314 section into the .bss section of the final image. */
315 s = bfd_make_section_with_flags (abfd, ".dynbss",
316 (SEC_ALLOC
317 | SEC_LINKER_CREATED));
318 if (s == NULL)
319 return FALSE;
321 /* The .rel[a].bss section holds copy relocs. This section is not
322 normally needed. We need to create it here, though, so that the
323 linker will map it to an output section. We can't just create it
324 only if we need it, because we will not know whether we need it
325 until we have seen all the input files, and the first time the
326 main linker code calls BFD after examining all the input files
327 (size_dynamic_sections) the input sections have already been
328 mapped to the output sections. If the section turns out not to
329 be needed, we can discard it later. We will never need this
330 section when generating a shared object, since they do not use
331 copy relocs. */
332 if (! info->shared)
334 s = bfd_make_section_with_flags (abfd,
335 (bed->default_use_rela_p
336 ? ".rela.bss" : ".rel.bss"),
337 flags | SEC_READONLY);
338 if (s == NULL
339 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
340 return FALSE;
344 return TRUE;
347 /* Record a new dynamic symbol. We record the dynamic symbols as we
348 read the input files, since we need to have a list of all of them
349 before we can determine the final sizes of the output sections.
350 Note that we may actually call this function even though we are not
351 going to output any dynamic symbols; in some cases we know that a
352 symbol should be in the dynamic symbol table, but only if there is
353 one. */
355 bfd_boolean
356 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
357 struct elf_link_hash_entry *h)
359 if (h->dynindx == -1)
361 struct elf_strtab_hash *dynstr;
362 char *p;
363 const char *name;
364 bfd_size_type indx;
366 /* XXX: The ABI draft says the linker must turn hidden and
367 internal symbols into STB_LOCAL symbols when producing the
368 DSO. However, if ld.so honors st_other in the dynamic table,
369 this would not be necessary. */
370 switch (ELF_ST_VISIBILITY (h->other))
372 case STV_INTERNAL:
373 case STV_HIDDEN:
374 if (h->root.type != bfd_link_hash_undefined
375 && h->root.type != bfd_link_hash_undefweak)
377 h->forced_local = 1;
378 if (!elf_hash_table (info)->is_relocatable_executable)
379 return TRUE;
382 default:
383 break;
386 h->dynindx = elf_hash_table (info)->dynsymcount;
387 ++elf_hash_table (info)->dynsymcount;
389 dynstr = elf_hash_table (info)->dynstr;
390 if (dynstr == NULL)
392 /* Create a strtab to hold the dynamic symbol names. */
393 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
394 if (dynstr == NULL)
395 return FALSE;
398 /* We don't put any version information in the dynamic string
399 table. */
400 name = h->root.root.string;
401 p = strchr (name, ELF_VER_CHR);
402 if (p != NULL)
403 /* We know that the p points into writable memory. In fact,
404 there are only a few symbols that have read-only names, being
405 those like _GLOBAL_OFFSET_TABLE_ that are created specially
406 by the backends. Most symbols will have names pointing into
407 an ELF string table read from a file, or to objalloc memory. */
408 *p = 0;
410 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
412 if (p != NULL)
413 *p = ELF_VER_CHR;
415 if (indx == (bfd_size_type) -1)
416 return FALSE;
417 h->dynstr_index = indx;
420 return TRUE;
423 /* Record an assignment to a symbol made by a linker script. We need
424 this in case some dynamic object refers to this symbol. */
426 bfd_boolean
427 bfd_elf_record_link_assignment (struct bfd_link_info *info,
428 const char *name,
429 bfd_boolean provide)
431 struct elf_link_hash_entry *h;
432 struct elf_link_hash_table *htab;
434 if (!is_elf_hash_table (info->hash))
435 return TRUE;
437 htab = elf_hash_table (info);
438 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
439 if (h == NULL)
440 return provide;
442 /* Since we're defining the symbol, don't let it seem to have not
443 been defined. record_dynamic_symbol and size_dynamic_sections
444 may depend on this. */
445 if (h->root.type == bfd_link_hash_undefweak
446 || h->root.type == bfd_link_hash_undefined)
448 h->root.type = bfd_link_hash_new;
449 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
450 bfd_link_repair_undef_list (&htab->root);
453 if (h->root.type == bfd_link_hash_new)
454 h->non_elf = 0;
456 /* If this symbol is being provided by the linker script, and it is
457 currently defined by a dynamic object, but not by a regular
458 object, then mark it as undefined so that the generic linker will
459 force the correct value. */
460 if (provide
461 && h->def_dynamic
462 && !h->def_regular)
463 h->root.type = bfd_link_hash_undefined;
465 /* If this symbol is not being provided by the linker script, and it is
466 currently defined by a dynamic object, but not by a regular object,
467 then clear out any version information because the symbol will not be
468 associated with the dynamic object any more. */
469 if (!provide
470 && h->def_dynamic
471 && !h->def_regular)
472 h->verinfo.verdef = NULL;
474 h->def_regular = 1;
476 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
477 and executables. */
478 if (!info->relocatable
479 && h->dynindx != -1
480 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
481 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
482 h->forced_local = 1;
484 if ((h->def_dynamic
485 || h->ref_dynamic
486 || info->shared
487 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
488 && h->dynindx == -1)
490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
491 return FALSE;
493 /* If this is a weak defined symbol, and we know a corresponding
494 real symbol from the same dynamic object, make sure the real
495 symbol is also made into a dynamic symbol. */
496 if (h->u.weakdef != NULL
497 && h->u.weakdef->dynindx == -1)
499 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
500 return FALSE;
504 return TRUE;
507 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
508 success, and 2 on a failure caused by attempting to record a symbol
509 in a discarded section, eg. a discarded link-once section symbol. */
512 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
513 bfd *input_bfd,
514 long input_indx)
516 bfd_size_type amt;
517 struct elf_link_local_dynamic_entry *entry;
518 struct elf_link_hash_table *eht;
519 struct elf_strtab_hash *dynstr;
520 unsigned long dynstr_index;
521 char *name;
522 Elf_External_Sym_Shndx eshndx;
523 char esym[sizeof (Elf64_External_Sym)];
525 if (! is_elf_hash_table (info->hash))
526 return 0;
528 /* See if the entry exists already. */
529 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
530 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
531 return 1;
533 amt = sizeof (*entry);
534 entry = bfd_alloc (input_bfd, amt);
535 if (entry == NULL)
536 return 0;
538 /* Go find the symbol, so that we can find it's name. */
539 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
540 1, input_indx, &entry->isym, esym, &eshndx))
542 bfd_release (input_bfd, entry);
543 return 0;
546 if (entry->isym.st_shndx != SHN_UNDEF
547 && (entry->isym.st_shndx < SHN_LORESERVE
548 || entry->isym.st_shndx > SHN_HIRESERVE))
550 asection *s;
552 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
553 if (s == NULL || bfd_is_abs_section (s->output_section))
555 /* We can still bfd_release here as nothing has done another
556 bfd_alloc. We can't do this later in this function. */
557 bfd_release (input_bfd, entry);
558 return 2;
562 name = (bfd_elf_string_from_elf_section
563 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
564 entry->isym.st_name));
566 dynstr = elf_hash_table (info)->dynstr;
567 if (dynstr == NULL)
569 /* Create a strtab to hold the dynamic symbol names. */
570 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
571 if (dynstr == NULL)
572 return 0;
575 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
576 if (dynstr_index == (unsigned long) -1)
577 return 0;
578 entry->isym.st_name = dynstr_index;
580 eht = elf_hash_table (info);
582 entry->next = eht->dynlocal;
583 eht->dynlocal = entry;
584 entry->input_bfd = input_bfd;
585 entry->input_indx = input_indx;
586 eht->dynsymcount++;
588 /* Whatever binding the symbol had before, it's now local. */
589 entry->isym.st_info
590 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
592 /* The dynindx will be set at the end of size_dynamic_sections. */
594 return 1;
597 /* Return the dynindex of a local dynamic symbol. */
599 long
600 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
601 bfd *input_bfd,
602 long input_indx)
604 struct elf_link_local_dynamic_entry *e;
606 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
607 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
608 return e->dynindx;
609 return -1;
612 /* This function is used to renumber the dynamic symbols, if some of
613 them are removed because they are marked as local. This is called
614 via elf_link_hash_traverse. */
616 static bfd_boolean
617 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
618 void *data)
620 size_t *count = data;
622 if (h->root.type == bfd_link_hash_warning)
623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
625 if (h->forced_local)
626 return TRUE;
628 if (h->dynindx != -1)
629 h->dynindx = ++(*count);
631 return TRUE;
635 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
636 STB_LOCAL binding. */
638 static bfd_boolean
639 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
640 void *data)
642 size_t *count = data;
644 if (h->root.type == bfd_link_hash_warning)
645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
647 if (!h->forced_local)
648 return TRUE;
650 if (h->dynindx != -1)
651 h->dynindx = ++(*count);
653 return TRUE;
656 /* Return true if the dynamic symbol for a given section should be
657 omitted when creating a shared library. */
658 bfd_boolean
659 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
660 struct bfd_link_info *info,
661 asection *p)
663 switch (elf_section_data (p)->this_hdr.sh_type)
665 case SHT_PROGBITS:
666 case SHT_NOBITS:
667 /* If sh_type is yet undecided, assume it could be
668 SHT_PROGBITS/SHT_NOBITS. */
669 case SHT_NULL:
670 if (strcmp (p->name, ".got") == 0
671 || strcmp (p->name, ".got.plt") == 0
672 || strcmp (p->name, ".plt") == 0)
674 asection *ip;
675 bfd *dynobj = elf_hash_table (info)->dynobj;
677 if (dynobj != NULL
678 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
679 && (ip->flags & SEC_LINKER_CREATED)
680 && ip->output_section == p)
681 return TRUE;
683 return FALSE;
685 /* There shouldn't be section relative relocations
686 against any other section. */
687 default:
688 return TRUE;
692 /* Assign dynsym indices. In a shared library we generate a section
693 symbol for each output section, which come first. Next come symbols
694 which have been forced to local binding. Then all of the back-end
695 allocated local dynamic syms, followed by the rest of the global
696 symbols. */
698 static unsigned long
699 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
700 struct bfd_link_info *info,
701 unsigned long *section_sym_count)
703 unsigned long dynsymcount = 0;
705 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
707 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
708 asection *p;
709 for (p = output_bfd->sections; p ; p = p->next)
710 if ((p->flags & SEC_EXCLUDE) == 0
711 && (p->flags & SEC_ALLOC) != 0
712 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
713 elf_section_data (p)->dynindx = ++dynsymcount;
715 *section_sym_count = dynsymcount;
717 elf_link_hash_traverse (elf_hash_table (info),
718 elf_link_renumber_local_hash_table_dynsyms,
719 &dynsymcount);
721 if (elf_hash_table (info)->dynlocal)
723 struct elf_link_local_dynamic_entry *p;
724 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
725 p->dynindx = ++dynsymcount;
728 elf_link_hash_traverse (elf_hash_table (info),
729 elf_link_renumber_hash_table_dynsyms,
730 &dynsymcount);
732 /* There is an unused NULL entry at the head of the table which
733 we must account for in our count. Unless there weren't any
734 symbols, which means we'll have no table at all. */
735 if (dynsymcount != 0)
736 ++dynsymcount;
738 elf_hash_table (info)->dynsymcount = dynsymcount;
739 return dynsymcount;
742 /* This function is called when we want to define a new symbol. It
743 handles the various cases which arise when we find a definition in
744 a dynamic object, or when there is already a definition in a
745 dynamic object. The new symbol is described by NAME, SYM, PSEC,
746 and PVALUE. We set SYM_HASH to the hash table entry. We set
747 OVERRIDE if the old symbol is overriding a new definition. We set
748 TYPE_CHANGE_OK if it is OK for the type to change. We set
749 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
750 change, we mean that we shouldn't warn if the type or size does
751 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
752 object is overridden by a regular object. */
754 bfd_boolean
755 _bfd_elf_merge_symbol (bfd *abfd,
756 struct bfd_link_info *info,
757 const char *name,
758 Elf_Internal_Sym *sym,
759 asection **psec,
760 bfd_vma *pvalue,
761 unsigned int *pold_alignment,
762 struct elf_link_hash_entry **sym_hash,
763 bfd_boolean *skip,
764 bfd_boolean *override,
765 bfd_boolean *type_change_ok,
766 bfd_boolean *size_change_ok)
768 asection *sec, *oldsec;
769 struct elf_link_hash_entry *h;
770 struct elf_link_hash_entry *flip;
771 int bind;
772 bfd *oldbfd;
773 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
774 bfd_boolean newweak, oldweak;
775 const struct elf_backend_data *bed;
777 *skip = FALSE;
778 *override = FALSE;
780 sec = *psec;
781 bind = ELF_ST_BIND (sym->st_info);
783 if (! bfd_is_und_section (sec))
784 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
785 else
786 h = ((struct elf_link_hash_entry *)
787 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
788 if (h == NULL)
789 return FALSE;
790 *sym_hash = h;
792 /* This code is for coping with dynamic objects, and is only useful
793 if we are doing an ELF link. */
794 if (info->hash->creator != abfd->xvec)
795 return TRUE;
797 /* For merging, we only care about real symbols. */
799 while (h->root.type == bfd_link_hash_indirect
800 || h->root.type == bfd_link_hash_warning)
801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
803 /* If we just created the symbol, mark it as being an ELF symbol.
804 Other than that, there is nothing to do--there is no merge issue
805 with a newly defined symbol--so we just return. */
807 if (h->root.type == bfd_link_hash_new)
809 h->non_elf = 0;
810 return TRUE;
813 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
814 existing symbol. */
816 switch (h->root.type)
818 default:
819 oldbfd = NULL;
820 oldsec = NULL;
821 break;
823 case bfd_link_hash_undefined:
824 case bfd_link_hash_undefweak:
825 oldbfd = h->root.u.undef.abfd;
826 oldsec = NULL;
827 break;
829 case bfd_link_hash_defined:
830 case bfd_link_hash_defweak:
831 oldbfd = h->root.u.def.section->owner;
832 oldsec = h->root.u.def.section;
833 break;
835 case bfd_link_hash_common:
836 oldbfd = h->root.u.c.p->section->owner;
837 oldsec = h->root.u.c.p->section;
838 break;
841 /* In cases involving weak versioned symbols, we may wind up trying
842 to merge a symbol with itself. Catch that here, to avoid the
843 confusion that results if we try to override a symbol with
844 itself. The additional tests catch cases like
845 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
846 dynamic object, which we do want to handle here. */
847 if (abfd == oldbfd
848 && ((abfd->flags & DYNAMIC) == 0
849 || !h->def_regular))
850 return TRUE;
852 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
853 respectively, is from a dynamic object. */
855 newdyn = (abfd->flags & DYNAMIC) != 0;
857 olddyn = FALSE;
858 if (oldbfd != NULL)
859 olddyn = (oldbfd->flags & DYNAMIC) != 0;
860 else if (oldsec != NULL)
862 /* This handles the special SHN_MIPS_{TEXT,DATA} section
863 indices used by MIPS ELF. */
864 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
867 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
868 respectively, appear to be a definition rather than reference. */
870 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
872 olddef = (h->root.type != bfd_link_hash_undefined
873 && h->root.type != bfd_link_hash_undefweak
874 && h->root.type != bfd_link_hash_common);
876 /* Check TLS symbol. We don't check undefined symbol introduced by
877 "ld -u". */
878 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
879 && ELF_ST_TYPE (sym->st_info) != h->type
880 && oldbfd != NULL)
882 bfd *ntbfd, *tbfd;
883 bfd_boolean ntdef, tdef;
884 asection *ntsec, *tsec;
886 if (h->type == STT_TLS)
888 ntbfd = abfd;
889 ntsec = sec;
890 ntdef = newdef;
891 tbfd = oldbfd;
892 tsec = oldsec;
893 tdef = olddef;
895 else
897 ntbfd = oldbfd;
898 ntsec = oldsec;
899 ntdef = olddef;
900 tbfd = abfd;
901 tsec = sec;
902 tdef = newdef;
905 if (tdef && ntdef)
906 (*_bfd_error_handler)
907 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
908 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
909 else if (!tdef && !ntdef)
910 (*_bfd_error_handler)
911 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
912 tbfd, ntbfd, h->root.root.string);
913 else if (tdef)
914 (*_bfd_error_handler)
915 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
916 tbfd, tsec, ntbfd, h->root.root.string);
917 else
918 (*_bfd_error_handler)
919 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
920 tbfd, ntbfd, ntsec, h->root.root.string);
922 bfd_set_error (bfd_error_bad_value);
923 return FALSE;
926 /* We need to remember if a symbol has a definition in a dynamic
927 object or is weak in all dynamic objects. Internal and hidden
928 visibility will make it unavailable to dynamic objects. */
929 if (newdyn && !h->dynamic_def)
931 if (!bfd_is_und_section (sec))
932 h->dynamic_def = 1;
933 else
935 /* Check if this symbol is weak in all dynamic objects. If it
936 is the first time we see it in a dynamic object, we mark
937 if it is weak. Otherwise, we clear it. */
938 if (!h->ref_dynamic)
940 if (bind == STB_WEAK)
941 h->dynamic_weak = 1;
943 else if (bind != STB_WEAK)
944 h->dynamic_weak = 0;
948 /* If the old symbol has non-default visibility, we ignore the new
949 definition from a dynamic object. */
950 if (newdyn
951 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
952 && !bfd_is_und_section (sec))
954 *skip = TRUE;
955 /* Make sure this symbol is dynamic. */
956 h->ref_dynamic = 1;
957 /* A protected symbol has external availability. Make sure it is
958 recorded as dynamic.
960 FIXME: Should we check type and size for protected symbol? */
961 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
962 return bfd_elf_link_record_dynamic_symbol (info, h);
963 else
964 return TRUE;
966 else if (!newdyn
967 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
968 && h->def_dynamic)
970 /* If the new symbol with non-default visibility comes from a
971 relocatable file and the old definition comes from a dynamic
972 object, we remove the old definition. */
973 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
974 h = *sym_hash;
976 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
977 && bfd_is_und_section (sec))
979 /* If the new symbol is undefined and the old symbol was
980 also undefined before, we need to make sure
981 _bfd_generic_link_add_one_symbol doesn't mess
982 up the linker hash table undefs list. Since the old
983 definition came from a dynamic object, it is still on the
984 undefs list. */
985 h->root.type = bfd_link_hash_undefined;
986 h->root.u.undef.abfd = abfd;
988 else
990 h->root.type = bfd_link_hash_new;
991 h->root.u.undef.abfd = NULL;
994 if (h->def_dynamic)
996 h->def_dynamic = 0;
997 h->ref_dynamic = 1;
998 h->dynamic_def = 1;
1000 /* FIXME: Should we check type and size for protected symbol? */
1001 h->size = 0;
1002 h->type = 0;
1003 return TRUE;
1006 /* Differentiate strong and weak symbols. */
1007 newweak = bind == STB_WEAK;
1008 oldweak = (h->root.type == bfd_link_hash_defweak
1009 || h->root.type == bfd_link_hash_undefweak);
1011 /* If a new weak symbol definition comes from a regular file and the
1012 old symbol comes from a dynamic library, we treat the new one as
1013 strong. Similarly, an old weak symbol definition from a regular
1014 file is treated as strong when the new symbol comes from a dynamic
1015 library. Further, an old weak symbol from a dynamic library is
1016 treated as strong if the new symbol is from a dynamic library.
1017 This reflects the way glibc's ld.so works.
1019 Do this before setting *type_change_ok or *size_change_ok so that
1020 we warn properly when dynamic library symbols are overridden. */
1022 if (newdef && !newdyn && olddyn)
1023 newweak = FALSE;
1024 if (olddef && newdyn)
1025 oldweak = FALSE;
1027 /* It's OK to change the type if either the existing symbol or the
1028 new symbol is weak. A type change is also OK if the old symbol
1029 is undefined and the new symbol is defined. */
1031 if (oldweak
1032 || newweak
1033 || (newdef
1034 && h->root.type == bfd_link_hash_undefined))
1035 *type_change_ok = TRUE;
1037 /* It's OK to change the size if either the existing symbol or the
1038 new symbol is weak, or if the old symbol is undefined. */
1040 if (*type_change_ok
1041 || h->root.type == bfd_link_hash_undefined)
1042 *size_change_ok = TRUE;
1044 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1045 symbol, respectively, appears to be a common symbol in a dynamic
1046 object. If a symbol appears in an uninitialized section, and is
1047 not weak, and is not a function, then it may be a common symbol
1048 which was resolved when the dynamic object was created. We want
1049 to treat such symbols specially, because they raise special
1050 considerations when setting the symbol size: if the symbol
1051 appears as a common symbol in a regular object, and the size in
1052 the regular object is larger, we must make sure that we use the
1053 larger size. This problematic case can always be avoided in C,
1054 but it must be handled correctly when using Fortran shared
1055 libraries.
1057 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1058 likewise for OLDDYNCOMMON and OLDDEF.
1060 Note that this test is just a heuristic, and that it is quite
1061 possible to have an uninitialized symbol in a shared object which
1062 is really a definition, rather than a common symbol. This could
1063 lead to some minor confusion when the symbol really is a common
1064 symbol in some regular object. However, I think it will be
1065 harmless. */
1067 if (newdyn
1068 && newdef
1069 && !newweak
1070 && (sec->flags & SEC_ALLOC) != 0
1071 && (sec->flags & SEC_LOAD) == 0
1072 && sym->st_size > 0
1073 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1074 newdyncommon = TRUE;
1075 else
1076 newdyncommon = FALSE;
1078 if (olddyn
1079 && olddef
1080 && h->root.type == bfd_link_hash_defined
1081 && h->def_dynamic
1082 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1083 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1084 && h->size > 0
1085 && h->type != STT_FUNC)
1086 olddyncommon = TRUE;
1087 else
1088 olddyncommon = FALSE;
1090 /* We now know everything about the old and new symbols. We ask the
1091 backend to check if we can merge them. */
1092 bed = get_elf_backend_data (abfd);
1093 if (bed->merge_symbol
1094 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1095 pold_alignment, skip, override,
1096 type_change_ok, size_change_ok,
1097 &newdyn, &newdef, &newdyncommon, &newweak,
1098 abfd, &sec,
1099 &olddyn, &olddef, &olddyncommon, &oldweak,
1100 oldbfd, &oldsec))
1101 return FALSE;
1103 /* If both the old and the new symbols look like common symbols in a
1104 dynamic object, set the size of the symbol to the larger of the
1105 two. */
1107 if (olddyncommon
1108 && newdyncommon
1109 && sym->st_size != h->size)
1111 /* Since we think we have two common symbols, issue a multiple
1112 common warning if desired. Note that we only warn if the
1113 size is different. If the size is the same, we simply let
1114 the old symbol override the new one as normally happens with
1115 symbols defined in dynamic objects. */
1117 if (! ((*info->callbacks->multiple_common)
1118 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1119 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1120 return FALSE;
1122 if (sym->st_size > h->size)
1123 h->size = sym->st_size;
1125 *size_change_ok = TRUE;
1128 /* If we are looking at a dynamic object, and we have found a
1129 definition, we need to see if the symbol was already defined by
1130 some other object. If so, we want to use the existing
1131 definition, and we do not want to report a multiple symbol
1132 definition error; we do this by clobbering *PSEC to be
1133 bfd_und_section_ptr.
1135 We treat a common symbol as a definition if the symbol in the
1136 shared library is a function, since common symbols always
1137 represent variables; this can cause confusion in principle, but
1138 any such confusion would seem to indicate an erroneous program or
1139 shared library. We also permit a common symbol in a regular
1140 object to override a weak symbol in a shared object. */
1142 if (newdyn
1143 && newdef
1144 && (olddef
1145 || (h->root.type == bfd_link_hash_common
1146 && (newweak
1147 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1149 *override = TRUE;
1150 newdef = FALSE;
1151 newdyncommon = FALSE;
1153 *psec = sec = bfd_und_section_ptr;
1154 *size_change_ok = TRUE;
1156 /* If we get here when the old symbol is a common symbol, then
1157 we are explicitly letting it override a weak symbol or
1158 function in a dynamic object, and we don't want to warn about
1159 a type change. If the old symbol is a defined symbol, a type
1160 change warning may still be appropriate. */
1162 if (h->root.type == bfd_link_hash_common)
1163 *type_change_ok = TRUE;
1166 /* Handle the special case of an old common symbol merging with a
1167 new symbol which looks like a common symbol in a shared object.
1168 We change *PSEC and *PVALUE to make the new symbol look like a
1169 common symbol, and let _bfd_generic_link_add_one_symbol do the
1170 right thing. */
1172 if (newdyncommon
1173 && h->root.type == bfd_link_hash_common)
1175 *override = TRUE;
1176 newdef = FALSE;
1177 newdyncommon = FALSE;
1178 *pvalue = sym->st_size;
1179 *psec = sec = bed->common_section (oldsec);
1180 *size_change_ok = TRUE;
1183 /* Skip weak definitions of symbols that are already defined. */
1184 if (newdef && olddef && newweak)
1185 *skip = TRUE;
1187 /* If the old symbol is from a dynamic object, and the new symbol is
1188 a definition which is not from a dynamic object, then the new
1189 symbol overrides the old symbol. Symbols from regular files
1190 always take precedence over symbols from dynamic objects, even if
1191 they are defined after the dynamic object in the link.
1193 As above, we again permit a common symbol in a regular object to
1194 override a definition in a shared object if the shared object
1195 symbol is a function or is weak. */
1197 flip = NULL;
1198 if (!newdyn
1199 && (newdef
1200 || (bfd_is_com_section (sec)
1201 && (oldweak
1202 || h->type == STT_FUNC)))
1203 && olddyn
1204 && olddef
1205 && h->def_dynamic)
1207 /* Change the hash table entry to undefined, and let
1208 _bfd_generic_link_add_one_symbol do the right thing with the
1209 new definition. */
1211 h->root.type = bfd_link_hash_undefined;
1212 h->root.u.undef.abfd = h->root.u.def.section->owner;
1213 *size_change_ok = TRUE;
1215 olddef = FALSE;
1216 olddyncommon = FALSE;
1218 /* We again permit a type change when a common symbol may be
1219 overriding a function. */
1221 if (bfd_is_com_section (sec))
1222 *type_change_ok = TRUE;
1224 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1225 flip = *sym_hash;
1226 else
1227 /* This union may have been set to be non-NULL when this symbol
1228 was seen in a dynamic object. We must force the union to be
1229 NULL, so that it is correct for a regular symbol. */
1230 h->verinfo.vertree = NULL;
1233 /* Handle the special case of a new common symbol merging with an
1234 old symbol that looks like it might be a common symbol defined in
1235 a shared object. Note that we have already handled the case in
1236 which a new common symbol should simply override the definition
1237 in the shared library. */
1239 if (! newdyn
1240 && bfd_is_com_section (sec)
1241 && olddyncommon)
1243 /* It would be best if we could set the hash table entry to a
1244 common symbol, but we don't know what to use for the section
1245 or the alignment. */
1246 if (! ((*info->callbacks->multiple_common)
1247 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1248 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1249 return FALSE;
1251 /* If the presumed common symbol in the dynamic object is
1252 larger, pretend that the new symbol has its size. */
1254 if (h->size > *pvalue)
1255 *pvalue = h->size;
1257 /* We need to remember the alignment required by the symbol
1258 in the dynamic object. */
1259 BFD_ASSERT (pold_alignment);
1260 *pold_alignment = h->root.u.def.section->alignment_power;
1262 olddef = FALSE;
1263 olddyncommon = FALSE;
1265 h->root.type = bfd_link_hash_undefined;
1266 h->root.u.undef.abfd = h->root.u.def.section->owner;
1268 *size_change_ok = TRUE;
1269 *type_change_ok = TRUE;
1271 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1272 flip = *sym_hash;
1273 else
1274 h->verinfo.vertree = NULL;
1277 if (flip != NULL)
1279 /* Handle the case where we had a versioned symbol in a dynamic
1280 library and now find a definition in a normal object. In this
1281 case, we make the versioned symbol point to the normal one. */
1282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1283 flip->root.type = h->root.type;
1284 h->root.type = bfd_link_hash_indirect;
1285 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1286 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1287 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1288 if (h->def_dynamic)
1290 h->def_dynamic = 0;
1291 flip->ref_dynamic = 1;
1295 return TRUE;
1298 /* This function is called to create an indirect symbol from the
1299 default for the symbol with the default version if needed. The
1300 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1301 set DYNSYM if the new indirect symbol is dynamic. */
1303 bfd_boolean
1304 _bfd_elf_add_default_symbol (bfd *abfd,
1305 struct bfd_link_info *info,
1306 struct elf_link_hash_entry *h,
1307 const char *name,
1308 Elf_Internal_Sym *sym,
1309 asection **psec,
1310 bfd_vma *value,
1311 bfd_boolean *dynsym,
1312 bfd_boolean override)
1314 bfd_boolean type_change_ok;
1315 bfd_boolean size_change_ok;
1316 bfd_boolean skip;
1317 char *shortname;
1318 struct elf_link_hash_entry *hi;
1319 struct bfd_link_hash_entry *bh;
1320 const struct elf_backend_data *bed;
1321 bfd_boolean collect;
1322 bfd_boolean dynamic;
1323 char *p;
1324 size_t len, shortlen;
1325 asection *sec;
1327 /* If this symbol has a version, and it is the default version, we
1328 create an indirect symbol from the default name to the fully
1329 decorated name. This will cause external references which do not
1330 specify a version to be bound to this version of the symbol. */
1331 p = strchr (name, ELF_VER_CHR);
1332 if (p == NULL || p[1] != ELF_VER_CHR)
1333 return TRUE;
1335 if (override)
1337 /* We are overridden by an old definition. We need to check if we
1338 need to create the indirect symbol from the default name. */
1339 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1340 FALSE, FALSE);
1341 BFD_ASSERT (hi != NULL);
1342 if (hi == h)
1343 return TRUE;
1344 while (hi->root.type == bfd_link_hash_indirect
1345 || hi->root.type == bfd_link_hash_warning)
1347 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1348 if (hi == h)
1349 return TRUE;
1353 bed = get_elf_backend_data (abfd);
1354 collect = bed->collect;
1355 dynamic = (abfd->flags & DYNAMIC) != 0;
1357 shortlen = p - name;
1358 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1359 if (shortname == NULL)
1360 return FALSE;
1361 memcpy (shortname, name, shortlen);
1362 shortname[shortlen] = '\0';
1364 /* We are going to create a new symbol. Merge it with any existing
1365 symbol with this name. For the purposes of the merge, act as
1366 though we were defining the symbol we just defined, although we
1367 actually going to define an indirect symbol. */
1368 type_change_ok = FALSE;
1369 size_change_ok = FALSE;
1370 sec = *psec;
1371 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1372 NULL, &hi, &skip, &override,
1373 &type_change_ok, &size_change_ok))
1374 return FALSE;
1376 if (skip)
1377 goto nondefault;
1379 if (! override)
1381 bh = &hi->root;
1382 if (! (_bfd_generic_link_add_one_symbol
1383 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1384 0, name, FALSE, collect, &bh)))
1385 return FALSE;
1386 hi = (struct elf_link_hash_entry *) bh;
1388 else
1390 /* In this case the symbol named SHORTNAME is overriding the
1391 indirect symbol we want to add. We were planning on making
1392 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1393 is the name without a version. NAME is the fully versioned
1394 name, and it is the default version.
1396 Overriding means that we already saw a definition for the
1397 symbol SHORTNAME in a regular object, and it is overriding
1398 the symbol defined in the dynamic object.
1400 When this happens, we actually want to change NAME, the
1401 symbol we just added, to refer to SHORTNAME. This will cause
1402 references to NAME in the shared object to become references
1403 to SHORTNAME in the regular object. This is what we expect
1404 when we override a function in a shared object: that the
1405 references in the shared object will be mapped to the
1406 definition in the regular object. */
1408 while (hi->root.type == bfd_link_hash_indirect
1409 || hi->root.type == bfd_link_hash_warning)
1410 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1412 h->root.type = bfd_link_hash_indirect;
1413 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1414 if (h->def_dynamic)
1416 h->def_dynamic = 0;
1417 hi->ref_dynamic = 1;
1418 if (hi->ref_regular
1419 || hi->def_regular)
1421 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1422 return FALSE;
1426 /* Now set HI to H, so that the following code will set the
1427 other fields correctly. */
1428 hi = h;
1431 /* If there is a duplicate definition somewhere, then HI may not
1432 point to an indirect symbol. We will have reported an error to
1433 the user in that case. */
1435 if (hi->root.type == bfd_link_hash_indirect)
1437 struct elf_link_hash_entry *ht;
1439 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1440 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1442 /* See if the new flags lead us to realize that the symbol must
1443 be dynamic. */
1444 if (! *dynsym)
1446 if (! dynamic)
1448 if (info->shared
1449 || hi->ref_dynamic)
1450 *dynsym = TRUE;
1452 else
1454 if (hi->ref_regular)
1455 *dynsym = TRUE;
1460 /* We also need to define an indirection from the nondefault version
1461 of the symbol. */
1463 nondefault:
1464 len = strlen (name);
1465 shortname = bfd_hash_allocate (&info->hash->table, len);
1466 if (shortname == NULL)
1467 return FALSE;
1468 memcpy (shortname, name, shortlen);
1469 memcpy (shortname + shortlen, p + 1, len - shortlen);
1471 /* Once again, merge with any existing symbol. */
1472 type_change_ok = FALSE;
1473 size_change_ok = FALSE;
1474 sec = *psec;
1475 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1476 NULL, &hi, &skip, &override,
1477 &type_change_ok, &size_change_ok))
1478 return FALSE;
1480 if (skip)
1481 return TRUE;
1483 if (override)
1485 /* Here SHORTNAME is a versioned name, so we don't expect to see
1486 the type of override we do in the case above unless it is
1487 overridden by a versioned definition. */
1488 if (hi->root.type != bfd_link_hash_defined
1489 && hi->root.type != bfd_link_hash_defweak)
1490 (*_bfd_error_handler)
1491 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1492 abfd, shortname);
1494 else
1496 bh = &hi->root;
1497 if (! (_bfd_generic_link_add_one_symbol
1498 (info, abfd, shortname, BSF_INDIRECT,
1499 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1500 return FALSE;
1501 hi = (struct elf_link_hash_entry *) bh;
1503 /* If there is a duplicate definition somewhere, then HI may not
1504 point to an indirect symbol. We will have reported an error
1505 to the user in that case. */
1507 if (hi->root.type == bfd_link_hash_indirect)
1509 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1511 /* See if the new flags lead us to realize that the symbol
1512 must be dynamic. */
1513 if (! *dynsym)
1515 if (! dynamic)
1517 if (info->shared
1518 || hi->ref_dynamic)
1519 *dynsym = TRUE;
1521 else
1523 if (hi->ref_regular)
1524 *dynsym = TRUE;
1530 return TRUE;
1533 /* This routine is used to export all defined symbols into the dynamic
1534 symbol table. It is called via elf_link_hash_traverse. */
1536 bfd_boolean
1537 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1539 struct elf_info_failed *eif = data;
1541 /* Ignore indirect symbols. These are added by the versioning code. */
1542 if (h->root.type == bfd_link_hash_indirect)
1543 return TRUE;
1545 if (h->root.type == bfd_link_hash_warning)
1546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1548 if (h->dynindx == -1
1549 && (h->def_regular
1550 || h->ref_regular))
1552 struct bfd_elf_version_tree *t;
1553 struct bfd_elf_version_expr *d;
1555 for (t = eif->verdefs; t != NULL; t = t->next)
1557 if (t->globals.list != NULL)
1559 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1560 if (d != NULL)
1561 goto doit;
1564 if (t->locals.list != NULL)
1566 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1567 if (d != NULL)
1568 return TRUE;
1572 if (!eif->verdefs)
1574 doit:
1575 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1577 eif->failed = TRUE;
1578 return FALSE;
1583 return TRUE;
1586 /* Look through the symbols which are defined in other shared
1587 libraries and referenced here. Update the list of version
1588 dependencies. This will be put into the .gnu.version_r section.
1589 This function is called via elf_link_hash_traverse. */
1591 bfd_boolean
1592 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1593 void *data)
1595 struct elf_find_verdep_info *rinfo = data;
1596 Elf_Internal_Verneed *t;
1597 Elf_Internal_Vernaux *a;
1598 bfd_size_type amt;
1600 if (h->root.type == bfd_link_hash_warning)
1601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1603 /* We only care about symbols defined in shared objects with version
1604 information. */
1605 if (!h->def_dynamic
1606 || h->def_regular
1607 || h->dynindx == -1
1608 || h->verinfo.verdef == NULL)
1609 return TRUE;
1611 /* See if we already know about this version. */
1612 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1614 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1615 continue;
1617 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1618 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1619 return TRUE;
1621 break;
1624 /* This is a new version. Add it to tree we are building. */
1626 if (t == NULL)
1628 amt = sizeof *t;
1629 t = bfd_zalloc (rinfo->output_bfd, amt);
1630 if (t == NULL)
1632 rinfo->failed = TRUE;
1633 return FALSE;
1636 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1637 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1638 elf_tdata (rinfo->output_bfd)->verref = t;
1641 amt = sizeof *a;
1642 a = bfd_zalloc (rinfo->output_bfd, amt);
1644 /* Note that we are copying a string pointer here, and testing it
1645 above. If bfd_elf_string_from_elf_section is ever changed to
1646 discard the string data when low in memory, this will have to be
1647 fixed. */
1648 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1650 a->vna_flags = h->verinfo.verdef->vd_flags;
1651 a->vna_nextptr = t->vn_auxptr;
1653 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1654 ++rinfo->vers;
1656 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1658 t->vn_auxptr = a;
1660 return TRUE;
1663 /* Figure out appropriate versions for all the symbols. We may not
1664 have the version number script until we have read all of the input
1665 files, so until that point we don't know which symbols should be
1666 local. This function is called via elf_link_hash_traverse. */
1668 bfd_boolean
1669 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1671 struct elf_assign_sym_version_info *sinfo;
1672 struct bfd_link_info *info;
1673 const struct elf_backend_data *bed;
1674 struct elf_info_failed eif;
1675 char *p;
1676 bfd_size_type amt;
1678 sinfo = data;
1679 info = sinfo->info;
1681 if (h->root.type == bfd_link_hash_warning)
1682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1684 /* Fix the symbol flags. */
1685 eif.failed = FALSE;
1686 eif.info = info;
1687 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1689 if (eif.failed)
1690 sinfo->failed = TRUE;
1691 return FALSE;
1694 /* We only need version numbers for symbols defined in regular
1695 objects. */
1696 if (!h->def_regular)
1697 return TRUE;
1699 bed = get_elf_backend_data (sinfo->output_bfd);
1700 p = strchr (h->root.root.string, ELF_VER_CHR);
1701 if (p != NULL && h->verinfo.vertree == NULL)
1703 struct bfd_elf_version_tree *t;
1704 bfd_boolean hidden;
1706 hidden = TRUE;
1708 /* There are two consecutive ELF_VER_CHR characters if this is
1709 not a hidden symbol. */
1710 ++p;
1711 if (*p == ELF_VER_CHR)
1713 hidden = FALSE;
1714 ++p;
1717 /* If there is no version string, we can just return out. */
1718 if (*p == '\0')
1720 if (hidden)
1721 h->hidden = 1;
1722 return TRUE;
1725 /* Look for the version. If we find it, it is no longer weak. */
1726 for (t = sinfo->verdefs; t != NULL; t = t->next)
1728 if (strcmp (t->name, p) == 0)
1730 size_t len;
1731 char *alc;
1732 struct bfd_elf_version_expr *d;
1734 len = p - h->root.root.string;
1735 alc = bfd_malloc (len);
1736 if (alc == NULL)
1737 return FALSE;
1738 memcpy (alc, h->root.root.string, len - 1);
1739 alc[len - 1] = '\0';
1740 if (alc[len - 2] == ELF_VER_CHR)
1741 alc[len - 2] = '\0';
1743 h->verinfo.vertree = t;
1744 t->used = TRUE;
1745 d = NULL;
1747 if (t->globals.list != NULL)
1748 d = (*t->match) (&t->globals, NULL, alc);
1750 /* See if there is anything to force this symbol to
1751 local scope. */
1752 if (d == NULL && t->locals.list != NULL)
1754 d = (*t->match) (&t->locals, NULL, alc);
1755 if (d != NULL
1756 && h->dynindx != -1
1757 && ! info->export_dynamic)
1758 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1761 free (alc);
1762 break;
1766 /* If we are building an application, we need to create a
1767 version node for this version. */
1768 if (t == NULL && info->executable)
1770 struct bfd_elf_version_tree **pp;
1771 int version_index;
1773 /* If we aren't going to export this symbol, we don't need
1774 to worry about it. */
1775 if (h->dynindx == -1)
1776 return TRUE;
1778 amt = sizeof *t;
1779 t = bfd_zalloc (sinfo->output_bfd, amt);
1780 if (t == NULL)
1782 sinfo->failed = TRUE;
1783 return FALSE;
1786 t->name = p;
1787 t->name_indx = (unsigned int) -1;
1788 t->used = TRUE;
1790 version_index = 1;
1791 /* Don't count anonymous version tag. */
1792 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1793 version_index = 0;
1794 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1795 ++version_index;
1796 t->vernum = version_index;
1798 *pp = t;
1800 h->verinfo.vertree = t;
1802 else if (t == NULL)
1804 /* We could not find the version for a symbol when
1805 generating a shared archive. Return an error. */
1806 (*_bfd_error_handler)
1807 (_("%B: undefined versioned symbol name %s"),
1808 sinfo->output_bfd, h->root.root.string);
1809 bfd_set_error (bfd_error_bad_value);
1810 sinfo->failed = TRUE;
1811 return FALSE;
1814 if (hidden)
1815 h->hidden = 1;
1818 /* If we don't have a version for this symbol, see if we can find
1819 something. */
1820 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1822 struct bfd_elf_version_tree *t;
1823 struct bfd_elf_version_tree *local_ver;
1824 struct bfd_elf_version_expr *d;
1826 /* See if can find what version this symbol is in. If the
1827 symbol is supposed to be local, then don't actually register
1828 it. */
1829 local_ver = NULL;
1830 for (t = sinfo->verdefs; t != NULL; t = t->next)
1832 if (t->globals.list != NULL)
1834 bfd_boolean matched;
1836 matched = FALSE;
1837 d = NULL;
1838 while ((d = (*t->match) (&t->globals, d,
1839 h->root.root.string)) != NULL)
1840 if (d->symver)
1841 matched = TRUE;
1842 else
1844 /* There is a version without definition. Make
1845 the symbol the default definition for this
1846 version. */
1847 h->verinfo.vertree = t;
1848 local_ver = NULL;
1849 d->script = 1;
1850 break;
1852 if (d != NULL)
1853 break;
1854 else if (matched)
1855 /* There is no undefined version for this symbol. Hide the
1856 default one. */
1857 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1860 if (t->locals.list != NULL)
1862 d = NULL;
1863 while ((d = (*t->match) (&t->locals, d,
1864 h->root.root.string)) != NULL)
1866 local_ver = t;
1867 /* If the match is "*", keep looking for a more
1868 explicit, perhaps even global, match.
1869 XXX: Shouldn't this be !d->wildcard instead? */
1870 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1871 break;
1874 if (d != NULL)
1875 break;
1879 if (local_ver != NULL)
1881 h->verinfo.vertree = local_ver;
1882 if (h->dynindx != -1
1883 && ! info->export_dynamic)
1885 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1890 return TRUE;
1893 /* Read and swap the relocs from the section indicated by SHDR. This
1894 may be either a REL or a RELA section. The relocations are
1895 translated into RELA relocations and stored in INTERNAL_RELOCS,
1896 which should have already been allocated to contain enough space.
1897 The EXTERNAL_RELOCS are a buffer where the external form of the
1898 relocations should be stored.
1900 Returns FALSE if something goes wrong. */
1902 static bfd_boolean
1903 elf_link_read_relocs_from_section (bfd *abfd,
1904 asection *sec,
1905 Elf_Internal_Shdr *shdr,
1906 void *external_relocs,
1907 Elf_Internal_Rela *internal_relocs)
1909 const struct elf_backend_data *bed;
1910 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1911 const bfd_byte *erela;
1912 const bfd_byte *erelaend;
1913 Elf_Internal_Rela *irela;
1914 Elf_Internal_Shdr *symtab_hdr;
1915 size_t nsyms;
1917 /* Position ourselves at the start of the section. */
1918 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1919 return FALSE;
1921 /* Read the relocations. */
1922 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1923 return FALSE;
1925 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1926 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1928 bed = get_elf_backend_data (abfd);
1930 /* Convert the external relocations to the internal format. */
1931 if (shdr->sh_entsize == bed->s->sizeof_rel)
1932 swap_in = bed->s->swap_reloc_in;
1933 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1934 swap_in = bed->s->swap_reloca_in;
1935 else
1937 bfd_set_error (bfd_error_wrong_format);
1938 return FALSE;
1941 erela = external_relocs;
1942 erelaend = erela + shdr->sh_size;
1943 irela = internal_relocs;
1944 while (erela < erelaend)
1946 bfd_vma r_symndx;
1948 (*swap_in) (abfd, erela, irela);
1949 r_symndx = ELF32_R_SYM (irela->r_info);
1950 if (bed->s->arch_size == 64)
1951 r_symndx >>= 24;
1952 if ((size_t) r_symndx >= nsyms)
1954 (*_bfd_error_handler)
1955 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1956 " for offset 0x%lx in section `%A'"),
1957 abfd, sec,
1958 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1959 bfd_set_error (bfd_error_bad_value);
1960 return FALSE;
1962 irela += bed->s->int_rels_per_ext_rel;
1963 erela += shdr->sh_entsize;
1966 return TRUE;
1969 /* Read and swap the relocs for a section O. They may have been
1970 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1971 not NULL, they are used as buffers to read into. They are known to
1972 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1973 the return value is allocated using either malloc or bfd_alloc,
1974 according to the KEEP_MEMORY argument. If O has two relocation
1975 sections (both REL and RELA relocations), then the REL_HDR
1976 relocations will appear first in INTERNAL_RELOCS, followed by the
1977 REL_HDR2 relocations. */
1979 Elf_Internal_Rela *
1980 _bfd_elf_link_read_relocs (bfd *abfd,
1981 asection *o,
1982 void *external_relocs,
1983 Elf_Internal_Rela *internal_relocs,
1984 bfd_boolean keep_memory)
1986 Elf_Internal_Shdr *rel_hdr;
1987 void *alloc1 = NULL;
1988 Elf_Internal_Rela *alloc2 = NULL;
1989 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1991 if (elf_section_data (o)->relocs != NULL)
1992 return elf_section_data (o)->relocs;
1994 if (o->reloc_count == 0)
1995 return NULL;
1997 rel_hdr = &elf_section_data (o)->rel_hdr;
1999 if (internal_relocs == NULL)
2001 bfd_size_type size;
2003 size = o->reloc_count;
2004 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2005 if (keep_memory)
2006 internal_relocs = bfd_alloc (abfd, size);
2007 else
2008 internal_relocs = alloc2 = bfd_malloc (size);
2009 if (internal_relocs == NULL)
2010 goto error_return;
2013 if (external_relocs == NULL)
2015 bfd_size_type size = rel_hdr->sh_size;
2017 if (elf_section_data (o)->rel_hdr2)
2018 size += elf_section_data (o)->rel_hdr2->sh_size;
2019 alloc1 = bfd_malloc (size);
2020 if (alloc1 == NULL)
2021 goto error_return;
2022 external_relocs = alloc1;
2025 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2026 external_relocs,
2027 internal_relocs))
2028 goto error_return;
2029 if (elf_section_data (o)->rel_hdr2
2030 && (!elf_link_read_relocs_from_section
2031 (abfd, o,
2032 elf_section_data (o)->rel_hdr2,
2033 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2034 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2035 * bed->s->int_rels_per_ext_rel))))
2036 goto error_return;
2038 /* Cache the results for next time, if we can. */
2039 if (keep_memory)
2040 elf_section_data (o)->relocs = internal_relocs;
2042 if (alloc1 != NULL)
2043 free (alloc1);
2045 /* Don't free alloc2, since if it was allocated we are passing it
2046 back (under the name of internal_relocs). */
2048 return internal_relocs;
2050 error_return:
2051 if (alloc1 != NULL)
2052 free (alloc1);
2053 if (alloc2 != NULL)
2054 free (alloc2);
2055 return NULL;
2058 /* Compute the size of, and allocate space for, REL_HDR which is the
2059 section header for a section containing relocations for O. */
2061 bfd_boolean
2062 _bfd_elf_link_size_reloc_section (bfd *abfd,
2063 Elf_Internal_Shdr *rel_hdr,
2064 asection *o)
2066 bfd_size_type reloc_count;
2067 bfd_size_type num_rel_hashes;
2069 /* Figure out how many relocations there will be. */
2070 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2071 reloc_count = elf_section_data (o)->rel_count;
2072 else
2073 reloc_count = elf_section_data (o)->rel_count2;
2075 num_rel_hashes = o->reloc_count;
2076 if (num_rel_hashes < reloc_count)
2077 num_rel_hashes = reloc_count;
2079 /* That allows us to calculate the size of the section. */
2080 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2082 /* The contents field must last into write_object_contents, so we
2083 allocate it with bfd_alloc rather than malloc. Also since we
2084 cannot be sure that the contents will actually be filled in,
2085 we zero the allocated space. */
2086 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2087 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2088 return FALSE;
2090 /* We only allocate one set of hash entries, so we only do it the
2091 first time we are called. */
2092 if (elf_section_data (o)->rel_hashes == NULL
2093 && num_rel_hashes)
2095 struct elf_link_hash_entry **p;
2097 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2098 if (p == NULL)
2099 return FALSE;
2101 elf_section_data (o)->rel_hashes = p;
2104 return TRUE;
2107 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2108 originated from the section given by INPUT_REL_HDR) to the
2109 OUTPUT_BFD. */
2111 bfd_boolean
2112 _bfd_elf_link_output_relocs (bfd *output_bfd,
2113 asection *input_section,
2114 Elf_Internal_Shdr *input_rel_hdr,
2115 Elf_Internal_Rela *internal_relocs,
2116 struct elf_link_hash_entry **rel_hash
2117 ATTRIBUTE_UNUSED)
2119 Elf_Internal_Rela *irela;
2120 Elf_Internal_Rela *irelaend;
2121 bfd_byte *erel;
2122 Elf_Internal_Shdr *output_rel_hdr;
2123 asection *output_section;
2124 unsigned int *rel_countp = NULL;
2125 const struct elf_backend_data *bed;
2126 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2128 output_section = input_section->output_section;
2129 output_rel_hdr = NULL;
2131 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2132 == input_rel_hdr->sh_entsize)
2134 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2135 rel_countp = &elf_section_data (output_section)->rel_count;
2137 else if (elf_section_data (output_section)->rel_hdr2
2138 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2139 == input_rel_hdr->sh_entsize))
2141 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2142 rel_countp = &elf_section_data (output_section)->rel_count2;
2144 else
2146 (*_bfd_error_handler)
2147 (_("%B: relocation size mismatch in %B section %A"),
2148 output_bfd, input_section->owner, input_section);
2149 bfd_set_error (bfd_error_wrong_object_format);
2150 return FALSE;
2153 bed = get_elf_backend_data (output_bfd);
2154 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2155 swap_out = bed->s->swap_reloc_out;
2156 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2157 swap_out = bed->s->swap_reloca_out;
2158 else
2159 abort ();
2161 erel = output_rel_hdr->contents;
2162 erel += *rel_countp * input_rel_hdr->sh_entsize;
2163 irela = internal_relocs;
2164 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2165 * bed->s->int_rels_per_ext_rel);
2166 while (irela < irelaend)
2168 (*swap_out) (output_bfd, irela, erel);
2169 irela += bed->s->int_rels_per_ext_rel;
2170 erel += input_rel_hdr->sh_entsize;
2173 /* Bump the counter, so that we know where to add the next set of
2174 relocations. */
2175 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2177 return TRUE;
2180 /* Fix up the flags for a symbol. This handles various cases which
2181 can only be fixed after all the input files are seen. This is
2182 currently called by both adjust_dynamic_symbol and
2183 assign_sym_version, which is unnecessary but perhaps more robust in
2184 the face of future changes. */
2186 bfd_boolean
2187 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2188 struct elf_info_failed *eif)
2190 /* If this symbol was mentioned in a non-ELF file, try to set
2191 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2192 permit a non-ELF file to correctly refer to a symbol defined in
2193 an ELF dynamic object. */
2194 if (h->non_elf)
2196 while (h->root.type == bfd_link_hash_indirect)
2197 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2199 if (h->root.type != bfd_link_hash_defined
2200 && h->root.type != bfd_link_hash_defweak)
2202 h->ref_regular = 1;
2203 h->ref_regular_nonweak = 1;
2205 else
2207 if (h->root.u.def.section->owner != NULL
2208 && (bfd_get_flavour (h->root.u.def.section->owner)
2209 == bfd_target_elf_flavour))
2211 h->ref_regular = 1;
2212 h->ref_regular_nonweak = 1;
2214 else
2215 h->def_regular = 1;
2218 if (h->dynindx == -1
2219 && (h->def_dynamic
2220 || h->ref_dynamic))
2222 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2224 eif->failed = TRUE;
2225 return FALSE;
2229 else
2231 /* Unfortunately, NON_ELF is only correct if the symbol
2232 was first seen in a non-ELF file. Fortunately, if the symbol
2233 was first seen in an ELF file, we're probably OK unless the
2234 symbol was defined in a non-ELF file. Catch that case here.
2235 FIXME: We're still in trouble if the symbol was first seen in
2236 a dynamic object, and then later in a non-ELF regular object. */
2237 if ((h->root.type == bfd_link_hash_defined
2238 || h->root.type == bfd_link_hash_defweak)
2239 && !h->def_regular
2240 && (h->root.u.def.section->owner != NULL
2241 ? (bfd_get_flavour (h->root.u.def.section->owner)
2242 != bfd_target_elf_flavour)
2243 : (bfd_is_abs_section (h->root.u.def.section)
2244 && !h->def_dynamic)))
2245 h->def_regular = 1;
2248 /* If this is a final link, and the symbol was defined as a common
2249 symbol in a regular object file, and there was no definition in
2250 any dynamic object, then the linker will have allocated space for
2251 the symbol in a common section but the DEF_REGULAR
2252 flag will not have been set. */
2253 if (h->root.type == bfd_link_hash_defined
2254 && !h->def_regular
2255 && h->ref_regular
2256 && !h->def_dynamic
2257 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2258 h->def_regular = 1;
2260 /* If -Bsymbolic was used (which means to bind references to global
2261 symbols to the definition within the shared object), and this
2262 symbol was defined in a regular object, then it actually doesn't
2263 need a PLT entry. Likewise, if the symbol has non-default
2264 visibility. If the symbol has hidden or internal visibility, we
2265 will force it local. */
2266 if (h->needs_plt
2267 && eif->info->shared
2268 && is_elf_hash_table (eif->info->hash)
2269 && (eif->info->symbolic
2270 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2271 && h->def_regular)
2273 const struct elf_backend_data *bed;
2274 bfd_boolean force_local;
2276 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2278 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2279 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2280 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2283 /* If a weak undefined symbol has non-default visibility, we also
2284 hide it from the dynamic linker. */
2285 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2286 && h->root.type == bfd_link_hash_undefweak)
2288 const struct elf_backend_data *bed;
2289 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2290 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2293 /* If this is a weak defined symbol in a dynamic object, and we know
2294 the real definition in the dynamic object, copy interesting flags
2295 over to the real definition. */
2296 if (h->u.weakdef != NULL)
2298 struct elf_link_hash_entry *weakdef;
2300 weakdef = h->u.weakdef;
2301 if (h->root.type == bfd_link_hash_indirect)
2302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2304 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2305 || h->root.type == bfd_link_hash_defweak);
2306 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2307 || weakdef->root.type == bfd_link_hash_defweak);
2308 BFD_ASSERT (weakdef->def_dynamic);
2310 /* If the real definition is defined by a regular object file,
2311 don't do anything special. See the longer description in
2312 _bfd_elf_adjust_dynamic_symbol, below. */
2313 if (weakdef->def_regular)
2314 h->u.weakdef = NULL;
2315 else
2317 const struct elf_backend_data *bed;
2319 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2320 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2324 return TRUE;
2327 /* Make the backend pick a good value for a dynamic symbol. This is
2328 called via elf_link_hash_traverse, and also calls itself
2329 recursively. */
2331 bfd_boolean
2332 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2334 struct elf_info_failed *eif = data;
2335 bfd *dynobj;
2336 const struct elf_backend_data *bed;
2338 if (! is_elf_hash_table (eif->info->hash))
2339 return FALSE;
2341 if (h->root.type == bfd_link_hash_warning)
2343 h->got = elf_hash_table (eif->info)->init_got_offset;
2344 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2346 /* When warning symbols are created, they **replace** the "real"
2347 entry in the hash table, thus we never get to see the real
2348 symbol in a hash traversal. So look at it now. */
2349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2352 /* Ignore indirect symbols. These are added by the versioning code. */
2353 if (h->root.type == bfd_link_hash_indirect)
2354 return TRUE;
2356 /* Fix the symbol flags. */
2357 if (! _bfd_elf_fix_symbol_flags (h, eif))
2358 return FALSE;
2360 /* If this symbol does not require a PLT entry, and it is not
2361 defined by a dynamic object, or is not referenced by a regular
2362 object, ignore it. We do have to handle a weak defined symbol,
2363 even if no regular object refers to it, if we decided to add it
2364 to the dynamic symbol table. FIXME: Do we normally need to worry
2365 about symbols which are defined by one dynamic object and
2366 referenced by another one? */
2367 if (!h->needs_plt
2368 && (h->def_regular
2369 || !h->def_dynamic
2370 || (!h->ref_regular
2371 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2373 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2374 return TRUE;
2377 /* If we've already adjusted this symbol, don't do it again. This
2378 can happen via a recursive call. */
2379 if (h->dynamic_adjusted)
2380 return TRUE;
2382 /* Don't look at this symbol again. Note that we must set this
2383 after checking the above conditions, because we may look at a
2384 symbol once, decide not to do anything, and then get called
2385 recursively later after REF_REGULAR is set below. */
2386 h->dynamic_adjusted = 1;
2388 /* If this is a weak definition, and we know a real definition, and
2389 the real symbol is not itself defined by a regular object file,
2390 then get a good value for the real definition. We handle the
2391 real symbol first, for the convenience of the backend routine.
2393 Note that there is a confusing case here. If the real definition
2394 is defined by a regular object file, we don't get the real symbol
2395 from the dynamic object, but we do get the weak symbol. If the
2396 processor backend uses a COPY reloc, then if some routine in the
2397 dynamic object changes the real symbol, we will not see that
2398 change in the corresponding weak symbol. This is the way other
2399 ELF linkers work as well, and seems to be a result of the shared
2400 library model.
2402 I will clarify this issue. Most SVR4 shared libraries define the
2403 variable _timezone and define timezone as a weak synonym. The
2404 tzset call changes _timezone. If you write
2405 extern int timezone;
2406 int _timezone = 5;
2407 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2408 you might expect that, since timezone is a synonym for _timezone,
2409 the same number will print both times. However, if the processor
2410 backend uses a COPY reloc, then actually timezone will be copied
2411 into your process image, and, since you define _timezone
2412 yourself, _timezone will not. Thus timezone and _timezone will
2413 wind up at different memory locations. The tzset call will set
2414 _timezone, leaving timezone unchanged. */
2416 if (h->u.weakdef != NULL)
2418 /* If we get to this point, we know there is an implicit
2419 reference by a regular object file via the weak symbol H.
2420 FIXME: Is this really true? What if the traversal finds
2421 H->U.WEAKDEF before it finds H? */
2422 h->u.weakdef->ref_regular = 1;
2424 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2425 return FALSE;
2428 /* If a symbol has no type and no size and does not require a PLT
2429 entry, then we are probably about to do the wrong thing here: we
2430 are probably going to create a COPY reloc for an empty object.
2431 This case can arise when a shared object is built with assembly
2432 code, and the assembly code fails to set the symbol type. */
2433 if (h->size == 0
2434 && h->type == STT_NOTYPE
2435 && !h->needs_plt)
2436 (*_bfd_error_handler)
2437 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2438 h->root.root.string);
2440 dynobj = elf_hash_table (eif->info)->dynobj;
2441 bed = get_elf_backend_data (dynobj);
2442 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2444 eif->failed = TRUE;
2445 return FALSE;
2448 return TRUE;
2451 /* Adjust all external symbols pointing into SEC_MERGE sections
2452 to reflect the object merging within the sections. */
2454 bfd_boolean
2455 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2457 asection *sec;
2459 if (h->root.type == bfd_link_hash_warning)
2460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2462 if ((h->root.type == bfd_link_hash_defined
2463 || h->root.type == bfd_link_hash_defweak)
2464 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2465 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2467 bfd *output_bfd = data;
2469 h->root.u.def.value =
2470 _bfd_merged_section_offset (output_bfd,
2471 &h->root.u.def.section,
2472 elf_section_data (sec)->sec_info,
2473 h->root.u.def.value);
2476 return TRUE;
2479 /* Returns false if the symbol referred to by H should be considered
2480 to resolve local to the current module, and true if it should be
2481 considered to bind dynamically. */
2483 bfd_boolean
2484 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2485 struct bfd_link_info *info,
2486 bfd_boolean ignore_protected)
2488 bfd_boolean binding_stays_local_p;
2490 if (h == NULL)
2491 return FALSE;
2493 while (h->root.type == bfd_link_hash_indirect
2494 || h->root.type == bfd_link_hash_warning)
2495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2497 /* If it was forced local, then clearly it's not dynamic. */
2498 if (h->dynindx == -1)
2499 return FALSE;
2500 if (h->forced_local)
2501 return FALSE;
2503 /* Identify the cases where name binding rules say that a
2504 visible symbol resolves locally. */
2505 binding_stays_local_p = info->executable || info->symbolic;
2507 switch (ELF_ST_VISIBILITY (h->other))
2509 case STV_INTERNAL:
2510 case STV_HIDDEN:
2511 return FALSE;
2513 case STV_PROTECTED:
2514 /* Proper resolution for function pointer equality may require
2515 that these symbols perhaps be resolved dynamically, even though
2516 we should be resolving them to the current module. */
2517 if (!ignore_protected || h->type != STT_FUNC)
2518 binding_stays_local_p = TRUE;
2519 break;
2521 default:
2522 break;
2525 /* If it isn't defined locally, then clearly it's dynamic. */
2526 if (!h->def_regular)
2527 return TRUE;
2529 /* Otherwise, the symbol is dynamic if binding rules don't tell
2530 us that it remains local. */
2531 return !binding_stays_local_p;
2534 /* Return true if the symbol referred to by H should be considered
2535 to resolve local to the current module, and false otherwise. Differs
2536 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2537 undefined symbols and weak symbols. */
2539 bfd_boolean
2540 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2541 struct bfd_link_info *info,
2542 bfd_boolean local_protected)
2544 /* If it's a local sym, of course we resolve locally. */
2545 if (h == NULL)
2546 return TRUE;
2548 /* Common symbols that become definitions don't get the DEF_REGULAR
2549 flag set, so test it first, and don't bail out. */
2550 if (ELF_COMMON_DEF_P (h))
2551 /* Do nothing. */;
2552 /* If we don't have a definition in a regular file, then we can't
2553 resolve locally. The sym is either undefined or dynamic. */
2554 else if (!h->def_regular)
2555 return FALSE;
2557 /* Forced local symbols resolve locally. */
2558 if (h->forced_local)
2559 return TRUE;
2561 /* As do non-dynamic symbols. */
2562 if (h->dynindx == -1)
2563 return TRUE;
2565 /* At this point, we know the symbol is defined and dynamic. In an
2566 executable it must resolve locally, likewise when building symbolic
2567 shared libraries. */
2568 if (info->executable || info->symbolic)
2569 return TRUE;
2571 /* Now deal with defined dynamic symbols in shared libraries. Ones
2572 with default visibility might not resolve locally. */
2573 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2574 return FALSE;
2576 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2577 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2578 return TRUE;
2580 /* STV_PROTECTED non-function symbols are local. */
2581 if (h->type != STT_FUNC)
2582 return TRUE;
2584 /* Function pointer equality tests may require that STV_PROTECTED
2585 symbols be treated as dynamic symbols, even when we know that the
2586 dynamic linker will resolve them locally. */
2587 return local_protected;
2590 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2591 aligned. Returns the first TLS output section. */
2593 struct bfd_section *
2594 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2596 struct bfd_section *sec, *tls;
2597 unsigned int align = 0;
2599 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2600 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2601 break;
2602 tls = sec;
2604 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2605 if (sec->alignment_power > align)
2606 align = sec->alignment_power;
2608 elf_hash_table (info)->tls_sec = tls;
2610 /* Ensure the alignment of the first section is the largest alignment,
2611 so that the tls segment starts aligned. */
2612 if (tls != NULL)
2613 tls->alignment_power = align;
2615 return tls;
2618 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2619 static bfd_boolean
2620 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2621 Elf_Internal_Sym *sym)
2623 const struct elf_backend_data *bed;
2625 /* Local symbols do not count, but target specific ones might. */
2626 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2627 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2628 return FALSE;
2630 /* Function symbols do not count. */
2631 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2632 return FALSE;
2634 /* If the section is undefined, then so is the symbol. */
2635 if (sym->st_shndx == SHN_UNDEF)
2636 return FALSE;
2638 /* If the symbol is defined in the common section, then
2639 it is a common definition and so does not count. */
2640 bed = get_elf_backend_data (abfd);
2641 if (bed->common_definition (sym))
2642 return FALSE;
2644 /* If the symbol is in a target specific section then we
2645 must rely upon the backend to tell us what it is. */
2646 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2647 /* FIXME - this function is not coded yet:
2649 return _bfd_is_global_symbol_definition (abfd, sym);
2651 Instead for now assume that the definition is not global,
2652 Even if this is wrong, at least the linker will behave
2653 in the same way that it used to do. */
2654 return FALSE;
2656 return TRUE;
2659 /* Search the symbol table of the archive element of the archive ABFD
2660 whose archive map contains a mention of SYMDEF, and determine if
2661 the symbol is defined in this element. */
2662 static bfd_boolean
2663 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2665 Elf_Internal_Shdr * hdr;
2666 bfd_size_type symcount;
2667 bfd_size_type extsymcount;
2668 bfd_size_type extsymoff;
2669 Elf_Internal_Sym *isymbuf;
2670 Elf_Internal_Sym *isym;
2671 Elf_Internal_Sym *isymend;
2672 bfd_boolean result;
2674 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2675 if (abfd == NULL)
2676 return FALSE;
2678 if (! bfd_check_format (abfd, bfd_object))
2679 return FALSE;
2681 /* If we have already included the element containing this symbol in the
2682 link then we do not need to include it again. Just claim that any symbol
2683 it contains is not a definition, so that our caller will not decide to
2684 (re)include this element. */
2685 if (abfd->archive_pass)
2686 return FALSE;
2688 /* Select the appropriate symbol table. */
2689 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2690 hdr = &elf_tdata (abfd)->symtab_hdr;
2691 else
2692 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2694 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2696 /* The sh_info field of the symtab header tells us where the
2697 external symbols start. We don't care about the local symbols. */
2698 if (elf_bad_symtab (abfd))
2700 extsymcount = symcount;
2701 extsymoff = 0;
2703 else
2705 extsymcount = symcount - hdr->sh_info;
2706 extsymoff = hdr->sh_info;
2709 if (extsymcount == 0)
2710 return FALSE;
2712 /* Read in the symbol table. */
2713 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2714 NULL, NULL, NULL);
2715 if (isymbuf == NULL)
2716 return FALSE;
2718 /* Scan the symbol table looking for SYMDEF. */
2719 result = FALSE;
2720 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2722 const char *name;
2724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2725 isym->st_name);
2726 if (name == NULL)
2727 break;
2729 if (strcmp (name, symdef->name) == 0)
2731 result = is_global_data_symbol_definition (abfd, isym);
2732 break;
2736 free (isymbuf);
2738 return result;
2741 /* Add an entry to the .dynamic table. */
2743 bfd_boolean
2744 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2745 bfd_vma tag,
2746 bfd_vma val)
2748 struct elf_link_hash_table *hash_table;
2749 const struct elf_backend_data *bed;
2750 asection *s;
2751 bfd_size_type newsize;
2752 bfd_byte *newcontents;
2753 Elf_Internal_Dyn dyn;
2755 hash_table = elf_hash_table (info);
2756 if (! is_elf_hash_table (hash_table))
2757 return FALSE;
2759 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2760 _bfd_error_handler
2761 (_("warning: creating a DT_TEXTREL in a shared object."));
2763 bed = get_elf_backend_data (hash_table->dynobj);
2764 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2765 BFD_ASSERT (s != NULL);
2767 newsize = s->size + bed->s->sizeof_dyn;
2768 newcontents = bfd_realloc (s->contents, newsize);
2769 if (newcontents == NULL)
2770 return FALSE;
2772 dyn.d_tag = tag;
2773 dyn.d_un.d_val = val;
2774 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2776 s->size = newsize;
2777 s->contents = newcontents;
2779 return TRUE;
2782 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2783 otherwise just check whether one already exists. Returns -1 on error,
2784 1 if a DT_NEEDED tag already exists, and 0 on success. */
2786 static int
2787 elf_add_dt_needed_tag (bfd *abfd,
2788 struct bfd_link_info *info,
2789 const char *soname,
2790 bfd_boolean do_it)
2792 struct elf_link_hash_table *hash_table;
2793 bfd_size_type oldsize;
2794 bfd_size_type strindex;
2796 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2797 return -1;
2799 hash_table = elf_hash_table (info);
2800 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2801 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2802 if (strindex == (bfd_size_type) -1)
2803 return -1;
2805 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2807 asection *sdyn;
2808 const struct elf_backend_data *bed;
2809 bfd_byte *extdyn;
2811 bed = get_elf_backend_data (hash_table->dynobj);
2812 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2813 if (sdyn != NULL)
2814 for (extdyn = sdyn->contents;
2815 extdyn < sdyn->contents + sdyn->size;
2816 extdyn += bed->s->sizeof_dyn)
2818 Elf_Internal_Dyn dyn;
2820 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2821 if (dyn.d_tag == DT_NEEDED
2822 && dyn.d_un.d_val == strindex)
2824 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2825 return 1;
2830 if (do_it)
2832 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2833 return -1;
2835 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2836 return -1;
2838 else
2839 /* We were just checking for existence of the tag. */
2840 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2842 return 0;
2845 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2846 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2847 references from regular objects to these symbols.
2849 ??? Should we do something about references from other dynamic
2850 obects? If not, we potentially lose some warnings about undefined
2851 symbols. But how can we recover the initial undefined / undefweak
2852 state? */
2854 struct elf_smash_syms_data
2856 bfd *not_needed;
2857 struct elf_link_hash_table *htab;
2858 bfd_boolean twiddled;
2861 static bfd_boolean
2862 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2864 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2865 struct bfd_link_hash_entry *bh;
2867 switch (h->root.type)
2869 default:
2870 case bfd_link_hash_new:
2871 return TRUE;
2873 case bfd_link_hash_undefined:
2874 if (h->root.u.undef.abfd != inf->not_needed)
2875 return TRUE;
2876 if (h->root.u.undef.weak != NULL
2877 && h->root.u.undef.weak != inf->not_needed)
2879 /* Symbol was undefweak in u.undef.weak bfd, and has become
2880 undefined in as-needed lib. Restore weak. */
2881 h->root.type = bfd_link_hash_undefweak;
2882 h->root.u.undef.abfd = h->root.u.undef.weak;
2883 if (h->root.u.undef.next != NULL
2884 || inf->htab->root.undefs_tail == &h->root)
2885 inf->twiddled = TRUE;
2886 return TRUE;
2888 break;
2890 case bfd_link_hash_undefweak:
2891 if (h->root.u.undef.abfd != inf->not_needed)
2892 return TRUE;
2893 break;
2895 case bfd_link_hash_defined:
2896 case bfd_link_hash_defweak:
2897 if (h->root.u.def.section->owner != inf->not_needed)
2898 return TRUE;
2899 break;
2901 case bfd_link_hash_common:
2902 if (h->root.u.c.p->section->owner != inf->not_needed)
2903 return TRUE;
2904 break;
2906 case bfd_link_hash_warning:
2907 case bfd_link_hash_indirect:
2908 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2909 if (h->root.u.i.link->type != bfd_link_hash_new)
2910 return TRUE;
2911 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2912 return TRUE;
2913 break;
2916 /* There is no way we can undo symbol table state from defined or
2917 defweak back to undefined. */
2918 if (h->ref_regular)
2919 abort ();
2921 /* Set sym back to newly created state, but keep undef.next if it is
2922 being used as a list pointer. */
2923 bh = h->root.u.undef.next;
2924 if (bh == &h->root)
2925 bh = NULL;
2926 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2927 inf->twiddled = TRUE;
2928 (*inf->htab->root.table.newfunc) (&h->root.root,
2929 &inf->htab->root.table,
2930 h->root.root.string);
2931 h->root.u.undef.next = bh;
2932 h->root.u.undef.abfd = inf->not_needed;
2933 h->non_elf = 0;
2934 return TRUE;
2937 /* Sort symbol by value and section. */
2938 static int
2939 elf_sort_symbol (const void *arg1, const void *arg2)
2941 const struct elf_link_hash_entry *h1;
2942 const struct elf_link_hash_entry *h2;
2943 bfd_signed_vma vdiff;
2945 h1 = *(const struct elf_link_hash_entry **) arg1;
2946 h2 = *(const struct elf_link_hash_entry **) arg2;
2947 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2948 if (vdiff != 0)
2949 return vdiff > 0 ? 1 : -1;
2950 else
2952 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2953 if (sdiff != 0)
2954 return sdiff > 0 ? 1 : -1;
2956 return 0;
2959 /* This function is used to adjust offsets into .dynstr for
2960 dynamic symbols. This is called via elf_link_hash_traverse. */
2962 static bfd_boolean
2963 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2965 struct elf_strtab_hash *dynstr = data;
2967 if (h->root.type == bfd_link_hash_warning)
2968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2970 if (h->dynindx != -1)
2971 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2972 return TRUE;
2975 /* Assign string offsets in .dynstr, update all structures referencing
2976 them. */
2978 static bfd_boolean
2979 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2981 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2982 struct elf_link_local_dynamic_entry *entry;
2983 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2984 bfd *dynobj = hash_table->dynobj;
2985 asection *sdyn;
2986 bfd_size_type size;
2987 const struct elf_backend_data *bed;
2988 bfd_byte *extdyn;
2990 _bfd_elf_strtab_finalize (dynstr);
2991 size = _bfd_elf_strtab_size (dynstr);
2993 bed = get_elf_backend_data (dynobj);
2994 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2995 BFD_ASSERT (sdyn != NULL);
2997 /* Update all .dynamic entries referencing .dynstr strings. */
2998 for (extdyn = sdyn->contents;
2999 extdyn < sdyn->contents + sdyn->size;
3000 extdyn += bed->s->sizeof_dyn)
3002 Elf_Internal_Dyn dyn;
3004 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3005 switch (dyn.d_tag)
3007 case DT_STRSZ:
3008 dyn.d_un.d_val = size;
3009 break;
3010 case DT_NEEDED:
3011 case DT_SONAME:
3012 case DT_RPATH:
3013 case DT_RUNPATH:
3014 case DT_FILTER:
3015 case DT_AUXILIARY:
3016 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3017 break;
3018 default:
3019 continue;
3021 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3024 /* Now update local dynamic symbols. */
3025 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3026 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3027 entry->isym.st_name);
3029 /* And the rest of dynamic symbols. */
3030 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3032 /* Adjust version definitions. */
3033 if (elf_tdata (output_bfd)->cverdefs)
3035 asection *s;
3036 bfd_byte *p;
3037 bfd_size_type i;
3038 Elf_Internal_Verdef def;
3039 Elf_Internal_Verdaux defaux;
3041 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3042 p = s->contents;
3045 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3046 &def);
3047 p += sizeof (Elf_External_Verdef);
3048 if (def.vd_aux != sizeof (Elf_External_Verdef))
3049 continue;
3050 for (i = 0; i < def.vd_cnt; ++i)
3052 _bfd_elf_swap_verdaux_in (output_bfd,
3053 (Elf_External_Verdaux *) p, &defaux);
3054 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3055 defaux.vda_name);
3056 _bfd_elf_swap_verdaux_out (output_bfd,
3057 &defaux, (Elf_External_Verdaux *) p);
3058 p += sizeof (Elf_External_Verdaux);
3061 while (def.vd_next);
3064 /* Adjust version references. */
3065 if (elf_tdata (output_bfd)->verref)
3067 asection *s;
3068 bfd_byte *p;
3069 bfd_size_type i;
3070 Elf_Internal_Verneed need;
3071 Elf_Internal_Vernaux needaux;
3073 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3074 p = s->contents;
3077 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3078 &need);
3079 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3080 _bfd_elf_swap_verneed_out (output_bfd, &need,
3081 (Elf_External_Verneed *) p);
3082 p += sizeof (Elf_External_Verneed);
3083 for (i = 0; i < need.vn_cnt; ++i)
3085 _bfd_elf_swap_vernaux_in (output_bfd,
3086 (Elf_External_Vernaux *) p, &needaux);
3087 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3088 needaux.vna_name);
3089 _bfd_elf_swap_vernaux_out (output_bfd,
3090 &needaux,
3091 (Elf_External_Vernaux *) p);
3092 p += sizeof (Elf_External_Vernaux);
3095 while (need.vn_next);
3098 return TRUE;
3101 /* Add symbols from an ELF object file to the linker hash table. */
3103 static bfd_boolean
3104 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3106 bfd_boolean (*add_symbol_hook)
3107 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3108 const char **, flagword *, asection **, bfd_vma *);
3109 bfd_boolean (*check_relocs)
3110 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3111 bfd_boolean (*check_directives)
3112 (bfd *, struct bfd_link_info *);
3113 bfd_boolean collect;
3114 Elf_Internal_Shdr *hdr;
3115 bfd_size_type symcount;
3116 bfd_size_type extsymcount;
3117 bfd_size_type extsymoff;
3118 struct elf_link_hash_entry **sym_hash;
3119 bfd_boolean dynamic;
3120 Elf_External_Versym *extversym = NULL;
3121 Elf_External_Versym *ever;
3122 struct elf_link_hash_entry *weaks;
3123 struct elf_link_hash_entry **nondeflt_vers = NULL;
3124 bfd_size_type nondeflt_vers_cnt = 0;
3125 Elf_Internal_Sym *isymbuf = NULL;
3126 Elf_Internal_Sym *isym;
3127 Elf_Internal_Sym *isymend;
3128 const struct elf_backend_data *bed;
3129 bfd_boolean add_needed;
3130 struct elf_link_hash_table * hash_table;
3131 bfd_size_type amt;
3133 hash_table = elf_hash_table (info);
3135 bed = get_elf_backend_data (abfd);
3136 add_symbol_hook = bed->elf_add_symbol_hook;
3137 collect = bed->collect;
3139 if ((abfd->flags & DYNAMIC) == 0)
3140 dynamic = FALSE;
3141 else
3143 dynamic = TRUE;
3145 /* You can't use -r against a dynamic object. Also, there's no
3146 hope of using a dynamic object which does not exactly match
3147 the format of the output file. */
3148 if (info->relocatable
3149 || !is_elf_hash_table (hash_table)
3150 || hash_table->root.creator != abfd->xvec)
3152 if (info->relocatable)
3153 bfd_set_error (bfd_error_invalid_operation);
3154 else
3155 bfd_set_error (bfd_error_wrong_format);
3156 goto error_return;
3160 /* As a GNU extension, any input sections which are named
3161 .gnu.warning.SYMBOL are treated as warning symbols for the given
3162 symbol. This differs from .gnu.warning sections, which generate
3163 warnings when they are included in an output file. */
3164 if (info->executable)
3166 asection *s;
3168 for (s = abfd->sections; s != NULL; s = s->next)
3170 const char *name;
3172 name = bfd_get_section_name (abfd, s);
3173 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3175 char *msg;
3176 bfd_size_type sz;
3178 name += sizeof ".gnu.warning." - 1;
3180 /* If this is a shared object, then look up the symbol
3181 in the hash table. If it is there, and it is already
3182 been defined, then we will not be using the entry
3183 from this shared object, so we don't need to warn.
3184 FIXME: If we see the definition in a regular object
3185 later on, we will warn, but we shouldn't. The only
3186 fix is to keep track of what warnings we are supposed
3187 to emit, and then handle them all at the end of the
3188 link. */
3189 if (dynamic)
3191 struct elf_link_hash_entry *h;
3193 h = elf_link_hash_lookup (hash_table, name,
3194 FALSE, FALSE, TRUE);
3196 /* FIXME: What about bfd_link_hash_common? */
3197 if (h != NULL
3198 && (h->root.type == bfd_link_hash_defined
3199 || h->root.type == bfd_link_hash_defweak))
3201 /* We don't want to issue this warning. Clobber
3202 the section size so that the warning does not
3203 get copied into the output file. */
3204 s->size = 0;
3205 continue;
3209 sz = s->size;
3210 msg = bfd_alloc (abfd, sz + 1);
3211 if (msg == NULL)
3212 goto error_return;
3214 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3215 goto error_return;
3217 msg[sz] = '\0';
3219 if (! (_bfd_generic_link_add_one_symbol
3220 (info, abfd, name, BSF_WARNING, s, 0, msg,
3221 FALSE, collect, NULL)))
3222 goto error_return;
3224 if (! info->relocatable)
3226 /* Clobber the section size so that the warning does
3227 not get copied into the output file. */
3228 s->size = 0;
3230 /* Also set SEC_EXCLUDE, so that symbols defined in
3231 the warning section don't get copied to the output. */
3232 s->flags |= SEC_EXCLUDE;
3238 add_needed = TRUE;
3239 if (! dynamic)
3241 /* If we are creating a shared library, create all the dynamic
3242 sections immediately. We need to attach them to something,
3243 so we attach them to this BFD, provided it is the right
3244 format. FIXME: If there are no input BFD's of the same
3245 format as the output, we can't make a shared library. */
3246 if (info->shared
3247 && is_elf_hash_table (hash_table)
3248 && hash_table->root.creator == abfd->xvec
3249 && ! hash_table->dynamic_sections_created)
3251 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3252 goto error_return;
3255 else if (!is_elf_hash_table (hash_table))
3256 goto error_return;
3257 else
3259 asection *s;
3260 const char *soname = NULL;
3261 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3262 int ret;
3264 /* ld --just-symbols and dynamic objects don't mix very well.
3265 ld shouldn't allow it. */
3266 if ((s = abfd->sections) != NULL
3267 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3268 abort ();
3270 /* If this dynamic lib was specified on the command line with
3271 --as-needed in effect, then we don't want to add a DT_NEEDED
3272 tag unless the lib is actually used. Similary for libs brought
3273 in by another lib's DT_NEEDED. When --no-add-needed is used
3274 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3275 any dynamic library in DT_NEEDED tags in the dynamic lib at
3276 all. */
3277 add_needed = (elf_dyn_lib_class (abfd)
3278 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3279 | DYN_NO_NEEDED)) == 0;
3281 s = bfd_get_section_by_name (abfd, ".dynamic");
3282 if (s != NULL)
3284 bfd_byte *dynbuf;
3285 bfd_byte *extdyn;
3286 int elfsec;
3287 unsigned long shlink;
3289 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3290 goto error_free_dyn;
3292 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3293 if (elfsec == -1)
3294 goto error_free_dyn;
3295 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3297 for (extdyn = dynbuf;
3298 extdyn < dynbuf + s->size;
3299 extdyn += bed->s->sizeof_dyn)
3301 Elf_Internal_Dyn dyn;
3303 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3304 if (dyn.d_tag == DT_SONAME)
3306 unsigned int tagv = dyn.d_un.d_val;
3307 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3308 if (soname == NULL)
3309 goto error_free_dyn;
3311 if (dyn.d_tag == DT_NEEDED)
3313 struct bfd_link_needed_list *n, **pn;
3314 char *fnm, *anm;
3315 unsigned int tagv = dyn.d_un.d_val;
3317 amt = sizeof (struct bfd_link_needed_list);
3318 n = bfd_alloc (abfd, amt);
3319 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3320 if (n == NULL || fnm == NULL)
3321 goto error_free_dyn;
3322 amt = strlen (fnm) + 1;
3323 anm = bfd_alloc (abfd, amt);
3324 if (anm == NULL)
3325 goto error_free_dyn;
3326 memcpy (anm, fnm, amt);
3327 n->name = anm;
3328 n->by = abfd;
3329 n->next = NULL;
3330 for (pn = & hash_table->needed;
3331 *pn != NULL;
3332 pn = &(*pn)->next)
3334 *pn = n;
3336 if (dyn.d_tag == DT_RUNPATH)
3338 struct bfd_link_needed_list *n, **pn;
3339 char *fnm, *anm;
3340 unsigned int tagv = dyn.d_un.d_val;
3342 amt = sizeof (struct bfd_link_needed_list);
3343 n = bfd_alloc (abfd, amt);
3344 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3345 if (n == NULL || fnm == NULL)
3346 goto error_free_dyn;
3347 amt = strlen (fnm) + 1;
3348 anm = bfd_alloc (abfd, amt);
3349 if (anm == NULL)
3350 goto error_free_dyn;
3351 memcpy (anm, fnm, amt);
3352 n->name = anm;
3353 n->by = abfd;
3354 n->next = NULL;
3355 for (pn = & runpath;
3356 *pn != NULL;
3357 pn = &(*pn)->next)
3359 *pn = n;
3361 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3362 if (!runpath && dyn.d_tag == DT_RPATH)
3364 struct bfd_link_needed_list *n, **pn;
3365 char *fnm, *anm;
3366 unsigned int tagv = dyn.d_un.d_val;
3368 amt = sizeof (struct bfd_link_needed_list);
3369 n = bfd_alloc (abfd, amt);
3370 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3371 if (n == NULL || fnm == NULL)
3372 goto error_free_dyn;
3373 amt = strlen (fnm) + 1;
3374 anm = bfd_alloc (abfd, amt);
3375 if (anm == NULL)
3377 error_free_dyn:
3378 free (dynbuf);
3379 goto error_return;
3381 memcpy (anm, fnm, amt);
3382 n->name = anm;
3383 n->by = abfd;
3384 n->next = NULL;
3385 for (pn = & rpath;
3386 *pn != NULL;
3387 pn = &(*pn)->next)
3389 *pn = n;
3393 free (dynbuf);
3396 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3397 frees all more recently bfd_alloc'd blocks as well. */
3398 if (runpath)
3399 rpath = runpath;
3401 if (rpath)
3403 struct bfd_link_needed_list **pn;
3404 for (pn = & hash_table->runpath;
3405 *pn != NULL;
3406 pn = &(*pn)->next)
3408 *pn = rpath;
3411 /* We do not want to include any of the sections in a dynamic
3412 object in the output file. We hack by simply clobbering the
3413 list of sections in the BFD. This could be handled more
3414 cleanly by, say, a new section flag; the existing
3415 SEC_NEVER_LOAD flag is not the one we want, because that one
3416 still implies that the section takes up space in the output
3417 file. */
3418 bfd_section_list_clear (abfd);
3420 /* Find the name to use in a DT_NEEDED entry that refers to this
3421 object. If the object has a DT_SONAME entry, we use it.
3422 Otherwise, if the generic linker stuck something in
3423 elf_dt_name, we use that. Otherwise, we just use the file
3424 name. */
3425 if (soname == NULL || *soname == '\0')
3427 soname = elf_dt_name (abfd);
3428 if (soname == NULL || *soname == '\0')
3429 soname = bfd_get_filename (abfd);
3432 /* Save the SONAME because sometimes the linker emulation code
3433 will need to know it. */
3434 elf_dt_name (abfd) = soname;
3436 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3437 if (ret < 0)
3438 goto error_return;
3440 /* If we have already included this dynamic object in the
3441 link, just ignore it. There is no reason to include a
3442 particular dynamic object more than once. */
3443 if (ret > 0)
3444 return TRUE;
3447 /* If this is a dynamic object, we always link against the .dynsym
3448 symbol table, not the .symtab symbol table. The dynamic linker
3449 will only see the .dynsym symbol table, so there is no reason to
3450 look at .symtab for a dynamic object. */
3452 if (! dynamic || elf_dynsymtab (abfd) == 0)
3453 hdr = &elf_tdata (abfd)->symtab_hdr;
3454 else
3455 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3457 symcount = hdr->sh_size / bed->s->sizeof_sym;
3459 /* The sh_info field of the symtab header tells us where the
3460 external symbols start. We don't care about the local symbols at
3461 this point. */
3462 if (elf_bad_symtab (abfd))
3464 extsymcount = symcount;
3465 extsymoff = 0;
3467 else
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3473 sym_hash = NULL;
3474 if (extsymcount != 0)
3476 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3477 NULL, NULL, NULL);
3478 if (isymbuf == NULL)
3479 goto error_return;
3481 /* We store a pointer to the hash table entry for each external
3482 symbol. */
3483 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3484 sym_hash = bfd_alloc (abfd, amt);
3485 if (sym_hash == NULL)
3486 goto error_free_sym;
3487 elf_sym_hashes (abfd) = sym_hash;
3490 if (dynamic)
3492 /* Read in any version definitions. */
3493 if (!_bfd_elf_slurp_version_tables (abfd,
3494 info->default_imported_symver))
3495 goto error_free_sym;
3497 /* Read in the symbol versions, but don't bother to convert them
3498 to internal format. */
3499 if (elf_dynversym (abfd) != 0)
3501 Elf_Internal_Shdr *versymhdr;
3503 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3504 extversym = bfd_malloc (versymhdr->sh_size);
3505 if (extversym == NULL)
3506 goto error_free_sym;
3507 amt = versymhdr->sh_size;
3508 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3509 || bfd_bread (extversym, amt, abfd) != amt)
3510 goto error_free_vers;
3514 weaks = NULL;
3516 ever = extversym != NULL ? extversym + extsymoff : NULL;
3517 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3518 isym < isymend;
3519 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3521 int bind;
3522 bfd_vma value;
3523 asection *sec, *new_sec;
3524 flagword flags;
3525 const char *name;
3526 struct elf_link_hash_entry *h;
3527 bfd_boolean definition;
3528 bfd_boolean size_change_ok;
3529 bfd_boolean type_change_ok;
3530 bfd_boolean new_weakdef;
3531 bfd_boolean override;
3532 bfd_boolean common;
3533 unsigned int old_alignment;
3534 bfd *old_bfd;
3536 override = FALSE;
3538 flags = BSF_NO_FLAGS;
3539 sec = NULL;
3540 value = isym->st_value;
3541 *sym_hash = NULL;
3542 common = bed->common_definition (isym);
3544 bind = ELF_ST_BIND (isym->st_info);
3545 if (bind == STB_LOCAL)
3547 /* This should be impossible, since ELF requires that all
3548 global symbols follow all local symbols, and that sh_info
3549 point to the first global symbol. Unfortunately, Irix 5
3550 screws this up. */
3551 continue;
3553 else if (bind == STB_GLOBAL)
3555 if (isym->st_shndx != SHN_UNDEF && !common)
3556 flags = BSF_GLOBAL;
3558 else if (bind == STB_WEAK)
3559 flags = BSF_WEAK;
3560 else
3562 /* Leave it up to the processor backend. */
3565 if (isym->st_shndx == SHN_UNDEF)
3566 sec = bfd_und_section_ptr;
3567 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3569 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3570 if (sec == NULL)
3571 sec = bfd_abs_section_ptr;
3572 else if (sec->kept_section)
3574 /* Symbols from discarded section are undefined, and have
3575 default visibility. */
3576 sec = bfd_und_section_ptr;
3577 isym->st_shndx = SHN_UNDEF;
3578 isym->st_other = STV_DEFAULT
3579 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3581 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3582 value -= sec->vma;
3584 else if (isym->st_shndx == SHN_ABS)
3585 sec = bfd_abs_section_ptr;
3586 else if (isym->st_shndx == SHN_COMMON)
3588 sec = bfd_com_section_ptr;
3589 /* What ELF calls the size we call the value. What ELF
3590 calls the value we call the alignment. */
3591 value = isym->st_size;
3593 else
3595 /* Leave it up to the processor backend. */
3598 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3599 isym->st_name);
3600 if (name == NULL)
3601 goto error_free_vers;
3603 if (isym->st_shndx == SHN_COMMON
3604 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3606 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3608 if (tcomm == NULL)
3610 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3611 (SEC_ALLOC
3612 | SEC_IS_COMMON
3613 | SEC_LINKER_CREATED
3614 | SEC_THREAD_LOCAL));
3615 if (tcomm == NULL)
3616 goto error_free_vers;
3618 sec = tcomm;
3620 else if (add_symbol_hook)
3622 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3623 &value))
3624 goto error_free_vers;
3626 /* The hook function sets the name to NULL if this symbol
3627 should be skipped for some reason. */
3628 if (name == NULL)
3629 continue;
3632 /* Sanity check that all possibilities were handled. */
3633 if (sec == NULL)
3635 bfd_set_error (bfd_error_bad_value);
3636 goto error_free_vers;
3639 if (bfd_is_und_section (sec)
3640 || bfd_is_com_section (sec))
3641 definition = FALSE;
3642 else
3643 definition = TRUE;
3645 size_change_ok = FALSE;
3646 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3647 old_alignment = 0;
3648 old_bfd = NULL;
3649 new_sec = sec;
3651 if (is_elf_hash_table (hash_table))
3653 Elf_Internal_Versym iver;
3654 unsigned int vernum = 0;
3655 bfd_boolean skip;
3657 if (ever == NULL)
3659 if (info->default_imported_symver)
3660 /* Use the default symbol version created earlier. */
3661 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3662 else
3663 iver.vs_vers = 0;
3665 else
3666 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3668 vernum = iver.vs_vers & VERSYM_VERSION;
3670 /* If this is a hidden symbol, or if it is not version
3671 1, we append the version name to the symbol name.
3672 However, we do not modify a non-hidden absolute symbol
3673 if it is not a function, because it might be the version
3674 symbol itself. FIXME: What if it isn't? */
3675 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3676 || (vernum > 1 && (! bfd_is_abs_section (sec)
3677 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3679 const char *verstr;
3680 size_t namelen, verlen, newlen;
3681 char *newname, *p;
3683 if (isym->st_shndx != SHN_UNDEF)
3685 if (vernum > elf_tdata (abfd)->cverdefs)
3686 verstr = NULL;
3687 else if (vernum > 1)
3688 verstr =
3689 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3690 else
3691 verstr = "";
3693 if (verstr == NULL)
3695 (*_bfd_error_handler)
3696 (_("%B: %s: invalid version %u (max %d)"),
3697 abfd, name, vernum,
3698 elf_tdata (abfd)->cverdefs);
3699 bfd_set_error (bfd_error_bad_value);
3700 goto error_free_vers;
3703 else
3705 /* We cannot simply test for the number of
3706 entries in the VERNEED section since the
3707 numbers for the needed versions do not start
3708 at 0. */
3709 Elf_Internal_Verneed *t;
3711 verstr = NULL;
3712 for (t = elf_tdata (abfd)->verref;
3713 t != NULL;
3714 t = t->vn_nextref)
3716 Elf_Internal_Vernaux *a;
3718 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3720 if (a->vna_other == vernum)
3722 verstr = a->vna_nodename;
3723 break;
3726 if (a != NULL)
3727 break;
3729 if (verstr == NULL)
3731 (*_bfd_error_handler)
3732 (_("%B: %s: invalid needed version %d"),
3733 abfd, name, vernum);
3734 bfd_set_error (bfd_error_bad_value);
3735 goto error_free_vers;
3739 namelen = strlen (name);
3740 verlen = strlen (verstr);
3741 newlen = namelen + verlen + 2;
3742 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3743 && isym->st_shndx != SHN_UNDEF)
3744 ++newlen;
3746 newname = bfd_alloc (abfd, newlen);
3747 if (newname == NULL)
3748 goto error_free_vers;
3749 memcpy (newname, name, namelen);
3750 p = newname + namelen;
3751 *p++ = ELF_VER_CHR;
3752 /* If this is a defined non-hidden version symbol,
3753 we add another @ to the name. This indicates the
3754 default version of the symbol. */
3755 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3756 && isym->st_shndx != SHN_UNDEF)
3757 *p++ = ELF_VER_CHR;
3758 memcpy (p, verstr, verlen + 1);
3760 name = newname;
3763 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3764 &value, &old_alignment,
3765 sym_hash, &skip, &override,
3766 &type_change_ok, &size_change_ok))
3767 goto error_free_vers;
3769 if (skip)
3770 continue;
3772 if (override)
3773 definition = FALSE;
3775 h = *sym_hash;
3776 while (h->root.type == bfd_link_hash_indirect
3777 || h->root.type == bfd_link_hash_warning)
3778 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3780 /* Remember the old alignment if this is a common symbol, so
3781 that we don't reduce the alignment later on. We can't
3782 check later, because _bfd_generic_link_add_one_symbol
3783 will set a default for the alignment which we want to
3784 override. We also remember the old bfd where the existing
3785 definition comes from. */
3786 switch (h->root.type)
3788 default:
3789 break;
3791 case bfd_link_hash_defined:
3792 case bfd_link_hash_defweak:
3793 old_bfd = h->root.u.def.section->owner;
3794 break;
3796 case bfd_link_hash_common:
3797 old_bfd = h->root.u.c.p->section->owner;
3798 old_alignment = h->root.u.c.p->alignment_power;
3799 break;
3802 if (elf_tdata (abfd)->verdef != NULL
3803 && ! override
3804 && vernum > 1
3805 && definition)
3806 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3809 if (! (_bfd_generic_link_add_one_symbol
3810 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3811 (struct bfd_link_hash_entry **) sym_hash)))
3812 goto error_free_vers;
3814 h = *sym_hash;
3815 while (h->root.type == bfd_link_hash_indirect
3816 || h->root.type == bfd_link_hash_warning)
3817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3818 *sym_hash = h;
3820 new_weakdef = FALSE;
3821 if (dynamic
3822 && definition
3823 && (flags & BSF_WEAK) != 0
3824 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3825 && is_elf_hash_table (hash_table)
3826 && h->u.weakdef == NULL)
3828 /* Keep a list of all weak defined non function symbols from
3829 a dynamic object, using the weakdef field. Later in this
3830 function we will set the weakdef field to the correct
3831 value. We only put non-function symbols from dynamic
3832 objects on this list, because that happens to be the only
3833 time we need to know the normal symbol corresponding to a
3834 weak symbol, and the information is time consuming to
3835 figure out. If the weakdef field is not already NULL,
3836 then this symbol was already defined by some previous
3837 dynamic object, and we will be using that previous
3838 definition anyhow. */
3840 h->u.weakdef = weaks;
3841 weaks = h;
3842 new_weakdef = TRUE;
3845 /* Set the alignment of a common symbol. */
3846 if ((common || bfd_is_com_section (sec))
3847 && h->root.type == bfd_link_hash_common)
3849 unsigned int align;
3851 if (common)
3852 align = bfd_log2 (isym->st_value);
3853 else
3855 /* The new symbol is a common symbol in a shared object.
3856 We need to get the alignment from the section. */
3857 align = new_sec->alignment_power;
3859 if (align > old_alignment
3860 /* Permit an alignment power of zero if an alignment of one
3861 is specified and no other alignments have been specified. */
3862 || (isym->st_value == 1 && old_alignment == 0))
3863 h->root.u.c.p->alignment_power = align;
3864 else
3865 h->root.u.c.p->alignment_power = old_alignment;
3868 if (is_elf_hash_table (hash_table))
3870 bfd_boolean dynsym;
3872 /* Check the alignment when a common symbol is involved. This
3873 can change when a common symbol is overridden by a normal
3874 definition or a common symbol is ignored due to the old
3875 normal definition. We need to make sure the maximum
3876 alignment is maintained. */
3877 if ((old_alignment || common)
3878 && h->root.type != bfd_link_hash_common)
3880 unsigned int common_align;
3881 unsigned int normal_align;
3882 unsigned int symbol_align;
3883 bfd *normal_bfd;
3884 bfd *common_bfd;
3886 symbol_align = ffs (h->root.u.def.value) - 1;
3887 if (h->root.u.def.section->owner != NULL
3888 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3890 normal_align = h->root.u.def.section->alignment_power;
3891 if (normal_align > symbol_align)
3892 normal_align = symbol_align;
3894 else
3895 normal_align = symbol_align;
3897 if (old_alignment)
3899 common_align = old_alignment;
3900 common_bfd = old_bfd;
3901 normal_bfd = abfd;
3903 else
3905 common_align = bfd_log2 (isym->st_value);
3906 common_bfd = abfd;
3907 normal_bfd = old_bfd;
3910 if (normal_align < common_align)
3911 (*_bfd_error_handler)
3912 (_("Warning: alignment %u of symbol `%s' in %B"
3913 " is smaller than %u in %B"),
3914 normal_bfd, common_bfd,
3915 1 << normal_align, name, 1 << common_align);
3918 /* Remember the symbol size and type. */
3919 if (isym->st_size != 0
3920 && (definition || h->size == 0))
3922 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3923 (*_bfd_error_handler)
3924 (_("Warning: size of symbol `%s' changed"
3925 " from %lu in %B to %lu in %B"),
3926 old_bfd, abfd,
3927 name, (unsigned long) h->size,
3928 (unsigned long) isym->st_size);
3930 h->size = isym->st_size;
3933 /* If this is a common symbol, then we always want H->SIZE
3934 to be the size of the common symbol. The code just above
3935 won't fix the size if a common symbol becomes larger. We
3936 don't warn about a size change here, because that is
3937 covered by --warn-common. */
3938 if (h->root.type == bfd_link_hash_common)
3939 h->size = h->root.u.c.size;
3941 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3942 && (definition || h->type == STT_NOTYPE))
3944 if (h->type != STT_NOTYPE
3945 && h->type != ELF_ST_TYPE (isym->st_info)
3946 && ! type_change_ok)
3947 (*_bfd_error_handler)
3948 (_("Warning: type of symbol `%s' changed"
3949 " from %d to %d in %B"),
3950 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3952 h->type = ELF_ST_TYPE (isym->st_info);
3955 /* If st_other has a processor-specific meaning, specific
3956 code might be needed here. We never merge the visibility
3957 attribute with the one from a dynamic object. */
3958 if (bed->elf_backend_merge_symbol_attribute)
3959 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3960 dynamic);
3962 /* If this symbol has default visibility and the user has requested
3963 we not re-export it, then mark it as hidden. */
3964 if (definition && !dynamic
3965 && (abfd->no_export
3966 || (abfd->my_archive && abfd->my_archive->no_export))
3967 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3968 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3970 if (isym->st_other != 0 && !dynamic)
3972 unsigned char hvis, symvis, other, nvis;
3974 /* Take the balance of OTHER from the definition. */
3975 other = (definition ? isym->st_other : h->other);
3976 other &= ~ ELF_ST_VISIBILITY (-1);
3978 /* Combine visibilities, using the most constraining one. */
3979 hvis = ELF_ST_VISIBILITY (h->other);
3980 symvis = ELF_ST_VISIBILITY (isym->st_other);
3981 if (! hvis)
3982 nvis = symvis;
3983 else if (! symvis)
3984 nvis = hvis;
3985 else
3986 nvis = hvis < symvis ? hvis : symvis;
3988 h->other = other | nvis;
3991 /* Set a flag in the hash table entry indicating the type of
3992 reference or definition we just found. Keep a count of
3993 the number of dynamic symbols we find. A dynamic symbol
3994 is one which is referenced or defined by both a regular
3995 object and a shared object. */
3996 dynsym = FALSE;
3997 if (! dynamic)
3999 if (! definition)
4001 h->ref_regular = 1;
4002 if (bind != STB_WEAK)
4003 h->ref_regular_nonweak = 1;
4005 else
4006 h->def_regular = 1;
4007 if (! info->executable
4008 || h->def_dynamic
4009 || h->ref_dynamic)
4010 dynsym = TRUE;
4012 else
4014 if (! definition)
4015 h->ref_dynamic = 1;
4016 else
4017 h->def_dynamic = 1;
4018 if (h->def_regular
4019 || h->ref_regular
4020 || (h->u.weakdef != NULL
4021 && ! new_weakdef
4022 && h->u.weakdef->dynindx != -1))
4023 dynsym = TRUE;
4026 /* Check to see if we need to add an indirect symbol for
4027 the default name. */
4028 if (definition || h->root.type == bfd_link_hash_common)
4029 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4030 &sec, &value, &dynsym,
4031 override))
4032 goto error_free_vers;
4034 if (definition && !dynamic)
4036 char *p = strchr (name, ELF_VER_CHR);
4037 if (p != NULL && p[1] != ELF_VER_CHR)
4039 /* Queue non-default versions so that .symver x, x@FOO
4040 aliases can be checked. */
4041 if (! nondeflt_vers)
4043 amt = (isymend - isym + 1)
4044 * sizeof (struct elf_link_hash_entry *);
4045 nondeflt_vers = bfd_malloc (amt);
4047 nondeflt_vers [nondeflt_vers_cnt++] = h;
4051 if (dynsym && h->dynindx == -1)
4053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4054 goto error_free_vers;
4055 if (h->u.weakdef != NULL
4056 && ! new_weakdef
4057 && h->u.weakdef->dynindx == -1)
4059 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4060 goto error_free_vers;
4063 else if (dynsym && h->dynindx != -1)
4064 /* If the symbol already has a dynamic index, but
4065 visibility says it should not be visible, turn it into
4066 a local symbol. */
4067 switch (ELF_ST_VISIBILITY (h->other))
4069 case STV_INTERNAL:
4070 case STV_HIDDEN:
4071 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4072 dynsym = FALSE;
4073 break;
4076 if (!add_needed
4077 && definition
4078 && dynsym
4079 && h->ref_regular)
4081 int ret;
4082 const char *soname = elf_dt_name (abfd);
4084 /* A symbol from a library loaded via DT_NEEDED of some
4085 other library is referenced by a regular object.
4086 Add a DT_NEEDED entry for it. Issue an error if
4087 --no-add-needed is used. */
4088 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4090 (*_bfd_error_handler)
4091 (_("%s: invalid DSO for symbol `%s' definition"),
4092 abfd, name);
4093 bfd_set_error (bfd_error_bad_value);
4094 goto error_free_vers;
4097 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4099 add_needed = TRUE;
4100 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4101 if (ret < 0)
4102 goto error_free_vers;
4104 BFD_ASSERT (ret == 0);
4109 /* Now that all the symbols from this input file are created, handle
4110 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4111 if (nondeflt_vers != NULL)
4113 bfd_size_type cnt, symidx;
4115 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4117 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4118 char *shortname, *p;
4120 p = strchr (h->root.root.string, ELF_VER_CHR);
4121 if (p == NULL
4122 || (h->root.type != bfd_link_hash_defined
4123 && h->root.type != bfd_link_hash_defweak))
4124 continue;
4126 amt = p - h->root.root.string;
4127 shortname = bfd_malloc (amt + 1);
4128 memcpy (shortname, h->root.root.string, amt);
4129 shortname[amt] = '\0';
4131 hi = (struct elf_link_hash_entry *)
4132 bfd_link_hash_lookup (&hash_table->root, shortname,
4133 FALSE, FALSE, FALSE);
4134 if (hi != NULL
4135 && hi->root.type == h->root.type
4136 && hi->root.u.def.value == h->root.u.def.value
4137 && hi->root.u.def.section == h->root.u.def.section)
4139 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4140 hi->root.type = bfd_link_hash_indirect;
4141 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4142 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4143 sym_hash = elf_sym_hashes (abfd);
4144 if (sym_hash)
4145 for (symidx = 0; symidx < extsymcount; ++symidx)
4146 if (sym_hash[symidx] == hi)
4148 sym_hash[symidx] = h;
4149 break;
4152 free (shortname);
4154 free (nondeflt_vers);
4155 nondeflt_vers = NULL;
4158 if (extversym != NULL)
4160 free (extversym);
4161 extversym = NULL;
4164 if (isymbuf != NULL)
4165 free (isymbuf);
4166 isymbuf = NULL;
4168 if (!add_needed
4169 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4171 /* Remove symbols defined in an as-needed shared lib that wasn't
4172 needed. */
4173 struct elf_smash_syms_data inf;
4174 inf.not_needed = abfd;
4175 inf.htab = hash_table;
4176 inf.twiddled = FALSE;
4177 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4178 if (inf.twiddled)
4179 bfd_link_repair_undef_list (&hash_table->root);
4180 weaks = NULL;
4183 /* Now set the weakdefs field correctly for all the weak defined
4184 symbols we found. The only way to do this is to search all the
4185 symbols. Since we only need the information for non functions in
4186 dynamic objects, that's the only time we actually put anything on
4187 the list WEAKS. We need this information so that if a regular
4188 object refers to a symbol defined weakly in a dynamic object, the
4189 real symbol in the dynamic object is also put in the dynamic
4190 symbols; we also must arrange for both symbols to point to the
4191 same memory location. We could handle the general case of symbol
4192 aliasing, but a general symbol alias can only be generated in
4193 assembler code, handling it correctly would be very time
4194 consuming, and other ELF linkers don't handle general aliasing
4195 either. */
4196 if (weaks != NULL)
4198 struct elf_link_hash_entry **hpp;
4199 struct elf_link_hash_entry **hppend;
4200 struct elf_link_hash_entry **sorted_sym_hash;
4201 struct elf_link_hash_entry *h;
4202 size_t sym_count;
4204 /* Since we have to search the whole symbol list for each weak
4205 defined symbol, search time for N weak defined symbols will be
4206 O(N^2). Binary search will cut it down to O(NlogN). */
4207 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4208 sorted_sym_hash = bfd_malloc (amt);
4209 if (sorted_sym_hash == NULL)
4210 goto error_return;
4211 sym_hash = sorted_sym_hash;
4212 hpp = elf_sym_hashes (abfd);
4213 hppend = hpp + extsymcount;
4214 sym_count = 0;
4215 for (; hpp < hppend; hpp++)
4217 h = *hpp;
4218 if (h != NULL
4219 && h->root.type == bfd_link_hash_defined
4220 && h->type != STT_FUNC)
4222 *sym_hash = h;
4223 sym_hash++;
4224 sym_count++;
4228 qsort (sorted_sym_hash, sym_count,
4229 sizeof (struct elf_link_hash_entry *),
4230 elf_sort_symbol);
4232 while (weaks != NULL)
4234 struct elf_link_hash_entry *hlook;
4235 asection *slook;
4236 bfd_vma vlook;
4237 long ilook;
4238 size_t i, j, idx;
4240 hlook = weaks;
4241 weaks = hlook->u.weakdef;
4242 hlook->u.weakdef = NULL;
4244 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4245 || hlook->root.type == bfd_link_hash_defweak
4246 || hlook->root.type == bfd_link_hash_common
4247 || hlook->root.type == bfd_link_hash_indirect);
4248 slook = hlook->root.u.def.section;
4249 vlook = hlook->root.u.def.value;
4251 ilook = -1;
4252 i = 0;
4253 j = sym_count;
4254 while (i < j)
4256 bfd_signed_vma vdiff;
4257 idx = (i + j) / 2;
4258 h = sorted_sym_hash [idx];
4259 vdiff = vlook - h->root.u.def.value;
4260 if (vdiff < 0)
4261 j = idx;
4262 else if (vdiff > 0)
4263 i = idx + 1;
4264 else
4266 long sdiff = slook->id - h->root.u.def.section->id;
4267 if (sdiff < 0)
4268 j = idx;
4269 else if (sdiff > 0)
4270 i = idx + 1;
4271 else
4273 ilook = idx;
4274 break;
4279 /* We didn't find a value/section match. */
4280 if (ilook == -1)
4281 continue;
4283 for (i = ilook; i < sym_count; i++)
4285 h = sorted_sym_hash [i];
4287 /* Stop if value or section doesn't match. */
4288 if (h->root.u.def.value != vlook
4289 || h->root.u.def.section != slook)
4290 break;
4291 else if (h != hlook)
4293 hlook->u.weakdef = h;
4295 /* If the weak definition is in the list of dynamic
4296 symbols, make sure the real definition is put
4297 there as well. */
4298 if (hlook->dynindx != -1 && h->dynindx == -1)
4300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4301 goto error_return;
4304 /* If the real definition is in the list of dynamic
4305 symbols, make sure the weak definition is put
4306 there as well. If we don't do this, then the
4307 dynamic loader might not merge the entries for the
4308 real definition and the weak definition. */
4309 if (h->dynindx != -1 && hlook->dynindx == -1)
4311 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4312 goto error_return;
4314 break;
4319 free (sorted_sym_hash);
4322 check_directives = get_elf_backend_data (abfd)->check_directives;
4323 if (check_directives)
4324 check_directives (abfd, info);
4326 /* If this object is the same format as the output object, and it is
4327 not a shared library, then let the backend look through the
4328 relocs.
4330 This is required to build global offset table entries and to
4331 arrange for dynamic relocs. It is not required for the
4332 particular common case of linking non PIC code, even when linking
4333 against shared libraries, but unfortunately there is no way of
4334 knowing whether an object file has been compiled PIC or not.
4335 Looking through the relocs is not particularly time consuming.
4336 The problem is that we must either (1) keep the relocs in memory,
4337 which causes the linker to require additional runtime memory or
4338 (2) read the relocs twice from the input file, which wastes time.
4339 This would be a good case for using mmap.
4341 I have no idea how to handle linking PIC code into a file of a
4342 different format. It probably can't be done. */
4343 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4344 if (! dynamic
4345 && is_elf_hash_table (hash_table)
4346 && hash_table->root.creator == abfd->xvec
4347 && check_relocs != NULL)
4349 asection *o;
4351 for (o = abfd->sections; o != NULL; o = o->next)
4353 Elf_Internal_Rela *internal_relocs;
4354 bfd_boolean ok;
4356 if ((o->flags & SEC_RELOC) == 0
4357 || o->reloc_count == 0
4358 || ((info->strip == strip_all || info->strip == strip_debugger)
4359 && (o->flags & SEC_DEBUGGING) != 0)
4360 || bfd_is_abs_section (o->output_section))
4361 continue;
4363 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4364 info->keep_memory);
4365 if (internal_relocs == NULL)
4366 goto error_return;
4368 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4370 if (elf_section_data (o)->relocs != internal_relocs)
4371 free (internal_relocs);
4373 if (! ok)
4374 goto error_return;
4378 /* If this is a non-traditional link, try to optimize the handling
4379 of the .stab/.stabstr sections. */
4380 if (! dynamic
4381 && ! info->traditional_format
4382 && is_elf_hash_table (hash_table)
4383 && (info->strip != strip_all && info->strip != strip_debugger))
4385 asection *stabstr;
4387 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4388 if (stabstr != NULL)
4390 bfd_size_type string_offset = 0;
4391 asection *stab;
4393 for (stab = abfd->sections; stab; stab = stab->next)
4394 if (strncmp (".stab", stab->name, 5) == 0
4395 && (!stab->name[5] ||
4396 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4397 && (stab->flags & SEC_MERGE) == 0
4398 && !bfd_is_abs_section (stab->output_section))
4400 struct bfd_elf_section_data *secdata;
4402 secdata = elf_section_data (stab);
4403 if (! _bfd_link_section_stabs (abfd,
4404 &hash_table->stab_info,
4405 stab, stabstr,
4406 &secdata->sec_info,
4407 &string_offset))
4408 goto error_return;
4409 if (secdata->sec_info)
4410 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4415 if (is_elf_hash_table (hash_table) && add_needed)
4417 /* Add this bfd to the loaded list. */
4418 struct elf_link_loaded_list *n;
4420 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4421 if (n == NULL)
4422 goto error_return;
4423 n->abfd = abfd;
4424 n->next = hash_table->loaded;
4425 hash_table->loaded = n;
4428 return TRUE;
4430 error_free_vers:
4431 if (nondeflt_vers != NULL)
4432 free (nondeflt_vers);
4433 if (extversym != NULL)
4434 free (extversym);
4435 error_free_sym:
4436 if (isymbuf != NULL)
4437 free (isymbuf);
4438 error_return:
4439 return FALSE;
4442 /* Return the linker hash table entry of a symbol that might be
4443 satisfied by an archive symbol. Return -1 on error. */
4445 struct elf_link_hash_entry *
4446 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4447 struct bfd_link_info *info,
4448 const char *name)
4450 struct elf_link_hash_entry *h;
4451 char *p, *copy;
4452 size_t len, first;
4454 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4455 if (h != NULL)
4456 return h;
4458 /* If this is a default version (the name contains @@), look up the
4459 symbol again with only one `@' as well as without the version.
4460 The effect is that references to the symbol with and without the
4461 version will be matched by the default symbol in the archive. */
4463 p = strchr (name, ELF_VER_CHR);
4464 if (p == NULL || p[1] != ELF_VER_CHR)
4465 return h;
4467 /* First check with only one `@'. */
4468 len = strlen (name);
4469 copy = bfd_alloc (abfd, len);
4470 if (copy == NULL)
4471 return (struct elf_link_hash_entry *) 0 - 1;
4473 first = p - name + 1;
4474 memcpy (copy, name, first);
4475 memcpy (copy + first, name + first + 1, len - first);
4477 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4478 if (h == NULL)
4480 /* We also need to check references to the symbol without the
4481 version. */
4482 copy[first - 1] = '\0';
4483 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4484 FALSE, FALSE, FALSE);
4487 bfd_release (abfd, copy);
4488 return h;
4491 /* Add symbols from an ELF archive file to the linker hash table. We
4492 don't use _bfd_generic_link_add_archive_symbols because of a
4493 problem which arises on UnixWare. The UnixWare libc.so is an
4494 archive which includes an entry libc.so.1 which defines a bunch of
4495 symbols. The libc.so archive also includes a number of other
4496 object files, which also define symbols, some of which are the same
4497 as those defined in libc.so.1. Correct linking requires that we
4498 consider each object file in turn, and include it if it defines any
4499 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4500 this; it looks through the list of undefined symbols, and includes
4501 any object file which defines them. When this algorithm is used on
4502 UnixWare, it winds up pulling in libc.so.1 early and defining a
4503 bunch of symbols. This means that some of the other objects in the
4504 archive are not included in the link, which is incorrect since they
4505 precede libc.so.1 in the archive.
4507 Fortunately, ELF archive handling is simpler than that done by
4508 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4509 oddities. In ELF, if we find a symbol in the archive map, and the
4510 symbol is currently undefined, we know that we must pull in that
4511 object file.
4513 Unfortunately, we do have to make multiple passes over the symbol
4514 table until nothing further is resolved. */
4516 static bfd_boolean
4517 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4519 symindex c;
4520 bfd_boolean *defined = NULL;
4521 bfd_boolean *included = NULL;
4522 carsym *symdefs;
4523 bfd_boolean loop;
4524 bfd_size_type amt;
4525 const struct elf_backend_data *bed;
4526 struct elf_link_hash_entry * (*archive_symbol_lookup)
4527 (bfd *, struct bfd_link_info *, const char *);
4529 if (! bfd_has_map (abfd))
4531 /* An empty archive is a special case. */
4532 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4533 return TRUE;
4534 bfd_set_error (bfd_error_no_armap);
4535 return FALSE;
4538 /* Keep track of all symbols we know to be already defined, and all
4539 files we know to be already included. This is to speed up the
4540 second and subsequent passes. */
4541 c = bfd_ardata (abfd)->symdef_count;
4542 if (c == 0)
4543 return TRUE;
4544 amt = c;
4545 amt *= sizeof (bfd_boolean);
4546 defined = bfd_zmalloc (amt);
4547 included = bfd_zmalloc (amt);
4548 if (defined == NULL || included == NULL)
4549 goto error_return;
4551 symdefs = bfd_ardata (abfd)->symdefs;
4552 bed = get_elf_backend_data (abfd);
4553 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4557 file_ptr last;
4558 symindex i;
4559 carsym *symdef;
4560 carsym *symdefend;
4562 loop = FALSE;
4563 last = -1;
4565 symdef = symdefs;
4566 symdefend = symdef + c;
4567 for (i = 0; symdef < symdefend; symdef++, i++)
4569 struct elf_link_hash_entry *h;
4570 bfd *element;
4571 struct bfd_link_hash_entry *undefs_tail;
4572 symindex mark;
4574 if (defined[i] || included[i])
4575 continue;
4576 if (symdef->file_offset == last)
4578 included[i] = TRUE;
4579 continue;
4582 h = archive_symbol_lookup (abfd, info, symdef->name);
4583 if (h == (struct elf_link_hash_entry *) 0 - 1)
4584 goto error_return;
4586 if (h == NULL)
4587 continue;
4589 if (h->root.type == bfd_link_hash_common)
4591 /* We currently have a common symbol. The archive map contains
4592 a reference to this symbol, so we may want to include it. We
4593 only want to include it however, if this archive element
4594 contains a definition of the symbol, not just another common
4595 declaration of it.
4597 Unfortunately some archivers (including GNU ar) will put
4598 declarations of common symbols into their archive maps, as
4599 well as real definitions, so we cannot just go by the archive
4600 map alone. Instead we must read in the element's symbol
4601 table and check that to see what kind of symbol definition
4602 this is. */
4603 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4604 continue;
4606 else if (h->root.type != bfd_link_hash_undefined)
4608 if (h->root.type != bfd_link_hash_undefweak)
4609 defined[i] = TRUE;
4610 continue;
4613 /* We need to include this archive member. */
4614 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4615 if (element == NULL)
4616 goto error_return;
4618 if (! bfd_check_format (element, bfd_object))
4619 goto error_return;
4621 /* Doublecheck that we have not included this object
4622 already--it should be impossible, but there may be
4623 something wrong with the archive. */
4624 if (element->archive_pass != 0)
4626 bfd_set_error (bfd_error_bad_value);
4627 goto error_return;
4629 element->archive_pass = 1;
4631 undefs_tail = info->hash->undefs_tail;
4633 if (! (*info->callbacks->add_archive_element) (info, element,
4634 symdef->name))
4635 goto error_return;
4636 if (! bfd_link_add_symbols (element, info))
4637 goto error_return;
4639 /* If there are any new undefined symbols, we need to make
4640 another pass through the archive in order to see whether
4641 they can be defined. FIXME: This isn't perfect, because
4642 common symbols wind up on undefs_tail and because an
4643 undefined symbol which is defined later on in this pass
4644 does not require another pass. This isn't a bug, but it
4645 does make the code less efficient than it could be. */
4646 if (undefs_tail != info->hash->undefs_tail)
4647 loop = TRUE;
4649 /* Look backward to mark all symbols from this object file
4650 which we have already seen in this pass. */
4651 mark = i;
4654 included[mark] = TRUE;
4655 if (mark == 0)
4656 break;
4657 --mark;
4659 while (symdefs[mark].file_offset == symdef->file_offset);
4661 /* We mark subsequent symbols from this object file as we go
4662 on through the loop. */
4663 last = symdef->file_offset;
4666 while (loop);
4668 free (defined);
4669 free (included);
4671 return TRUE;
4673 error_return:
4674 if (defined != NULL)
4675 free (defined);
4676 if (included != NULL)
4677 free (included);
4678 return FALSE;
4681 /* Given an ELF BFD, add symbols to the global hash table as
4682 appropriate. */
4684 bfd_boolean
4685 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4687 switch (bfd_get_format (abfd))
4689 case bfd_object:
4690 return elf_link_add_object_symbols (abfd, info);
4691 case bfd_archive:
4692 return elf_link_add_archive_symbols (abfd, info);
4693 default:
4694 bfd_set_error (bfd_error_wrong_format);
4695 return FALSE;
4699 /* This function will be called though elf_link_hash_traverse to store
4700 all hash value of the exported symbols in an array. */
4702 static bfd_boolean
4703 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4705 unsigned long **valuep = data;
4706 const char *name;
4707 char *p;
4708 unsigned long ha;
4709 char *alc = NULL;
4711 if (h->root.type == bfd_link_hash_warning)
4712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4714 /* Ignore indirect symbols. These are added by the versioning code. */
4715 if (h->dynindx == -1)
4716 return TRUE;
4718 name = h->root.root.string;
4719 p = strchr (name, ELF_VER_CHR);
4720 if (p != NULL)
4722 alc = bfd_malloc (p - name + 1);
4723 memcpy (alc, name, p - name);
4724 alc[p - name] = '\0';
4725 name = alc;
4728 /* Compute the hash value. */
4729 ha = bfd_elf_hash (name);
4731 /* Store the found hash value in the array given as the argument. */
4732 *(*valuep)++ = ha;
4734 /* And store it in the struct so that we can put it in the hash table
4735 later. */
4736 h->u.elf_hash_value = ha;
4738 if (alc != NULL)
4739 free (alc);
4741 return TRUE;
4744 /* Array used to determine the number of hash table buckets to use
4745 based on the number of symbols there are. If there are fewer than
4746 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4747 fewer than 37 we use 17 buckets, and so forth. We never use more
4748 than 32771 buckets. */
4750 static const size_t elf_buckets[] =
4752 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4753 16411, 32771, 0
4756 /* Compute bucket count for hashing table. We do not use a static set
4757 of possible tables sizes anymore. Instead we determine for all
4758 possible reasonable sizes of the table the outcome (i.e., the
4759 number of collisions etc) and choose the best solution. The
4760 weighting functions are not too simple to allow the table to grow
4761 without bounds. Instead one of the weighting factors is the size.
4762 Therefore the result is always a good payoff between few collisions
4763 (= short chain lengths) and table size. */
4764 static size_t
4765 compute_bucket_count (struct bfd_link_info *info)
4767 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4768 size_t best_size = 0;
4769 unsigned long int *hashcodes;
4770 unsigned long int *hashcodesp;
4771 unsigned long int i;
4772 bfd_size_type amt;
4774 /* Compute the hash values for all exported symbols. At the same
4775 time store the values in an array so that we could use them for
4776 optimizations. */
4777 amt = dynsymcount;
4778 amt *= sizeof (unsigned long int);
4779 hashcodes = bfd_malloc (amt);
4780 if (hashcodes == NULL)
4781 return 0;
4782 hashcodesp = hashcodes;
4784 /* Put all hash values in HASHCODES. */
4785 elf_link_hash_traverse (elf_hash_table (info),
4786 elf_collect_hash_codes, &hashcodesp);
4788 /* We have a problem here. The following code to optimize the table
4789 size requires an integer type with more the 32 bits. If
4790 BFD_HOST_U_64_BIT is set we know about such a type. */
4791 #ifdef BFD_HOST_U_64_BIT
4792 if (info->optimize)
4794 unsigned long int nsyms = hashcodesp - hashcodes;
4795 size_t minsize;
4796 size_t maxsize;
4797 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4798 unsigned long int *counts ;
4799 bfd *dynobj = elf_hash_table (info)->dynobj;
4800 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4802 /* Possible optimization parameters: if we have NSYMS symbols we say
4803 that the hashing table must at least have NSYMS/4 and at most
4804 2*NSYMS buckets. */
4805 minsize = nsyms / 4;
4806 if (minsize == 0)
4807 minsize = 1;
4808 best_size = maxsize = nsyms * 2;
4810 /* Create array where we count the collisions in. We must use bfd_malloc
4811 since the size could be large. */
4812 amt = maxsize;
4813 amt *= sizeof (unsigned long int);
4814 counts = bfd_malloc (amt);
4815 if (counts == NULL)
4817 free (hashcodes);
4818 return 0;
4821 /* Compute the "optimal" size for the hash table. The criteria is a
4822 minimal chain length. The minor criteria is (of course) the size
4823 of the table. */
4824 for (i = minsize; i < maxsize; ++i)
4826 /* Walk through the array of hashcodes and count the collisions. */
4827 BFD_HOST_U_64_BIT max;
4828 unsigned long int j;
4829 unsigned long int fact;
4831 memset (counts, '\0', i * sizeof (unsigned long int));
4833 /* Determine how often each hash bucket is used. */
4834 for (j = 0; j < nsyms; ++j)
4835 ++counts[hashcodes[j] % i];
4837 /* For the weight function we need some information about the
4838 pagesize on the target. This is information need not be 100%
4839 accurate. Since this information is not available (so far) we
4840 define it here to a reasonable default value. If it is crucial
4841 to have a better value some day simply define this value. */
4842 # ifndef BFD_TARGET_PAGESIZE
4843 # define BFD_TARGET_PAGESIZE (4096)
4844 # endif
4846 /* We in any case need 2 + NSYMS entries for the size values and
4847 the chains. */
4848 max = (2 + nsyms) * (bed->s->arch_size / 8);
4850 # if 1
4851 /* Variant 1: optimize for short chains. We add the squares
4852 of all the chain lengths (which favors many small chain
4853 over a few long chains). */
4854 for (j = 0; j < i; ++j)
4855 max += counts[j] * counts[j];
4857 /* This adds penalties for the overall size of the table. */
4858 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4859 max *= fact * fact;
4860 # else
4861 /* Variant 2: Optimize a lot more for small table. Here we
4862 also add squares of the size but we also add penalties for
4863 empty slots (the +1 term). */
4864 for (j = 0; j < i; ++j)
4865 max += (1 + counts[j]) * (1 + counts[j]);
4867 /* The overall size of the table is considered, but not as
4868 strong as in variant 1, where it is squared. */
4869 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4870 max *= fact;
4871 # endif
4873 /* Compare with current best results. */
4874 if (max < best_chlen)
4876 best_chlen = max;
4877 best_size = i;
4881 free (counts);
4883 else
4884 #endif /* defined (BFD_HOST_U_64_BIT) */
4886 /* This is the fallback solution if no 64bit type is available or if we
4887 are not supposed to spend much time on optimizations. We select the
4888 bucket count using a fixed set of numbers. */
4889 for (i = 0; elf_buckets[i] != 0; i++)
4891 best_size = elf_buckets[i];
4892 if (dynsymcount < elf_buckets[i + 1])
4893 break;
4897 /* Free the arrays we needed. */
4898 free (hashcodes);
4900 return best_size;
4903 /* Set up the sizes and contents of the ELF dynamic sections. This is
4904 called by the ELF linker emulation before_allocation routine. We
4905 must set the sizes of the sections before the linker sets the
4906 addresses of the various sections. */
4908 bfd_boolean
4909 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4910 const char *soname,
4911 const char *rpath,
4912 const char *filter_shlib,
4913 const char * const *auxiliary_filters,
4914 struct bfd_link_info *info,
4915 asection **sinterpptr,
4916 struct bfd_elf_version_tree *verdefs)
4918 bfd_size_type soname_indx;
4919 bfd *dynobj;
4920 const struct elf_backend_data *bed;
4921 struct elf_assign_sym_version_info asvinfo;
4923 *sinterpptr = NULL;
4925 soname_indx = (bfd_size_type) -1;
4927 if (!is_elf_hash_table (info->hash))
4928 return TRUE;
4930 elf_tdata (output_bfd)->relro = info->relro;
4931 if (info->execstack)
4932 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4933 else if (info->noexecstack)
4934 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4935 else
4937 bfd *inputobj;
4938 asection *notesec = NULL;
4939 int exec = 0;
4941 for (inputobj = info->input_bfds;
4942 inputobj;
4943 inputobj = inputobj->link_next)
4945 asection *s;
4947 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4948 continue;
4949 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4950 if (s)
4952 if (s->flags & SEC_CODE)
4953 exec = PF_X;
4954 notesec = s;
4956 else
4957 exec = PF_X;
4959 if (notesec)
4961 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4962 if (exec && info->relocatable
4963 && notesec->output_section != bfd_abs_section_ptr)
4964 notesec->output_section->flags |= SEC_CODE;
4968 /* Any syms created from now on start with -1 in
4969 got.refcount/offset and plt.refcount/offset. */
4970 elf_hash_table (info)->init_got_refcount
4971 = elf_hash_table (info)->init_got_offset;
4972 elf_hash_table (info)->init_plt_refcount
4973 = elf_hash_table (info)->init_plt_offset;
4975 /* The backend may have to create some sections regardless of whether
4976 we're dynamic or not. */
4977 bed = get_elf_backend_data (output_bfd);
4978 if (bed->elf_backend_always_size_sections
4979 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4980 return FALSE;
4982 dynobj = elf_hash_table (info)->dynobj;
4984 /* If there were no dynamic objects in the link, there is nothing to
4985 do here. */
4986 if (dynobj == NULL)
4987 return TRUE;
4989 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4990 return FALSE;
4992 if (elf_hash_table (info)->dynamic_sections_created)
4994 struct elf_info_failed eif;
4995 struct elf_link_hash_entry *h;
4996 asection *dynstr;
4997 struct bfd_elf_version_tree *t;
4998 struct bfd_elf_version_expr *d;
4999 asection *s;
5000 bfd_boolean all_defined;
5002 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5003 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5005 if (soname != NULL)
5007 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5008 soname, TRUE);
5009 if (soname_indx == (bfd_size_type) -1
5010 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5011 return FALSE;
5014 if (info->symbolic)
5016 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5017 return FALSE;
5018 info->flags |= DF_SYMBOLIC;
5021 if (rpath != NULL)
5023 bfd_size_type indx;
5025 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5026 TRUE);
5027 if (indx == (bfd_size_type) -1
5028 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5029 return FALSE;
5031 if (info->new_dtags)
5033 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5034 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5035 return FALSE;
5039 if (filter_shlib != NULL)
5041 bfd_size_type indx;
5043 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5044 filter_shlib, TRUE);
5045 if (indx == (bfd_size_type) -1
5046 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5047 return FALSE;
5050 if (auxiliary_filters != NULL)
5052 const char * const *p;
5054 for (p = auxiliary_filters; *p != NULL; p++)
5056 bfd_size_type indx;
5058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5059 *p, TRUE);
5060 if (indx == (bfd_size_type) -1
5061 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5062 return FALSE;
5066 eif.info = info;
5067 eif.verdefs = verdefs;
5068 eif.failed = FALSE;
5070 /* If we are supposed to export all symbols into the dynamic symbol
5071 table (this is not the normal case), then do so. */
5072 if (info->export_dynamic)
5074 elf_link_hash_traverse (elf_hash_table (info),
5075 _bfd_elf_export_symbol,
5076 &eif);
5077 if (eif.failed)
5078 return FALSE;
5081 /* Make all global versions with definition. */
5082 for (t = verdefs; t != NULL; t = t->next)
5083 for (d = t->globals.list; d != NULL; d = d->next)
5084 if (!d->symver && d->symbol)
5086 const char *verstr, *name;
5087 size_t namelen, verlen, newlen;
5088 char *newname, *p;
5089 struct elf_link_hash_entry *newh;
5091 name = d->symbol;
5092 namelen = strlen (name);
5093 verstr = t->name;
5094 verlen = strlen (verstr);
5095 newlen = namelen + verlen + 3;
5097 newname = bfd_malloc (newlen);
5098 if (newname == NULL)
5099 return FALSE;
5100 memcpy (newname, name, namelen);
5102 /* Check the hidden versioned definition. */
5103 p = newname + namelen;
5104 *p++ = ELF_VER_CHR;
5105 memcpy (p, verstr, verlen + 1);
5106 newh = elf_link_hash_lookup (elf_hash_table (info),
5107 newname, FALSE, FALSE,
5108 FALSE);
5109 if (newh == NULL
5110 || (newh->root.type != bfd_link_hash_defined
5111 && newh->root.type != bfd_link_hash_defweak))
5113 /* Check the default versioned definition. */
5114 *p++ = ELF_VER_CHR;
5115 memcpy (p, verstr, verlen + 1);
5116 newh = elf_link_hash_lookup (elf_hash_table (info),
5117 newname, FALSE, FALSE,
5118 FALSE);
5120 free (newname);
5122 /* Mark this version if there is a definition and it is
5123 not defined in a shared object. */
5124 if (newh != NULL
5125 && !newh->def_dynamic
5126 && (newh->root.type == bfd_link_hash_defined
5127 || newh->root.type == bfd_link_hash_defweak))
5128 d->symver = 1;
5131 /* Attach all the symbols to their version information. */
5132 asvinfo.output_bfd = output_bfd;
5133 asvinfo.info = info;
5134 asvinfo.verdefs = verdefs;
5135 asvinfo.failed = FALSE;
5137 elf_link_hash_traverse (elf_hash_table (info),
5138 _bfd_elf_link_assign_sym_version,
5139 &asvinfo);
5140 if (asvinfo.failed)
5141 return FALSE;
5143 if (!info->allow_undefined_version)
5145 /* Check if all global versions have a definition. */
5146 all_defined = TRUE;
5147 for (t = verdefs; t != NULL; t = t->next)
5148 for (d = t->globals.list; d != NULL; d = d->next)
5149 if (!d->symver && !d->script)
5151 (*_bfd_error_handler)
5152 (_("%s: undefined version: %s"),
5153 d->pattern, t->name);
5154 all_defined = FALSE;
5157 if (!all_defined)
5159 bfd_set_error (bfd_error_bad_value);
5160 return FALSE;
5164 /* Find all symbols which were defined in a dynamic object and make
5165 the backend pick a reasonable value for them. */
5166 elf_link_hash_traverse (elf_hash_table (info),
5167 _bfd_elf_adjust_dynamic_symbol,
5168 &eif);
5169 if (eif.failed)
5170 return FALSE;
5172 /* Add some entries to the .dynamic section. We fill in some of the
5173 values later, in bfd_elf_final_link, but we must add the entries
5174 now so that we know the final size of the .dynamic section. */
5176 /* If there are initialization and/or finalization functions to
5177 call then add the corresponding DT_INIT/DT_FINI entries. */
5178 h = (info->init_function
5179 ? elf_link_hash_lookup (elf_hash_table (info),
5180 info->init_function, FALSE,
5181 FALSE, FALSE)
5182 : NULL);
5183 if (h != NULL
5184 && (h->ref_regular
5185 || h->def_regular))
5187 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5188 return FALSE;
5190 h = (info->fini_function
5191 ? elf_link_hash_lookup (elf_hash_table (info),
5192 info->fini_function, FALSE,
5193 FALSE, FALSE)
5194 : NULL);
5195 if (h != NULL
5196 && (h->ref_regular
5197 || h->def_regular))
5199 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5200 return FALSE;
5203 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5204 if (s != NULL && s->linker_has_input)
5206 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5207 if (! info->executable)
5209 bfd *sub;
5210 asection *o;
5212 for (sub = info->input_bfds; sub != NULL;
5213 sub = sub->link_next)
5214 for (o = sub->sections; o != NULL; o = o->next)
5215 if (elf_section_data (o)->this_hdr.sh_type
5216 == SHT_PREINIT_ARRAY)
5218 (*_bfd_error_handler)
5219 (_("%B: .preinit_array section is not allowed in DSO"),
5220 sub);
5221 break;
5224 bfd_set_error (bfd_error_nonrepresentable_section);
5225 return FALSE;
5228 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5229 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5230 return FALSE;
5232 s = bfd_get_section_by_name (output_bfd, ".init_array");
5233 if (s != NULL && s->linker_has_input)
5235 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5236 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5237 return FALSE;
5239 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5240 if (s != NULL && s->linker_has_input)
5242 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5243 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5244 return FALSE;
5247 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5248 /* If .dynstr is excluded from the link, we don't want any of
5249 these tags. Strictly, we should be checking each section
5250 individually; This quick check covers for the case where
5251 someone does a /DISCARD/ : { *(*) }. */
5252 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5254 bfd_size_type strsize;
5256 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5257 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5258 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5259 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5260 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5261 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5262 bed->s->sizeof_sym))
5263 return FALSE;
5267 /* The backend must work out the sizes of all the other dynamic
5268 sections. */
5269 if (bed->elf_backend_size_dynamic_sections
5270 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5271 return FALSE;
5273 if (elf_hash_table (info)->dynamic_sections_created)
5275 unsigned long section_sym_count;
5276 asection *s;
5278 /* Set up the version definition section. */
5279 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5280 BFD_ASSERT (s != NULL);
5282 /* We may have created additional version definitions if we are
5283 just linking a regular application. */
5284 verdefs = asvinfo.verdefs;
5286 /* Skip anonymous version tag. */
5287 if (verdefs != NULL && verdefs->vernum == 0)
5288 verdefs = verdefs->next;
5290 if (verdefs == NULL && !info->create_default_symver)
5291 s->flags |= SEC_EXCLUDE;
5292 else
5294 unsigned int cdefs;
5295 bfd_size_type size;
5296 struct bfd_elf_version_tree *t;
5297 bfd_byte *p;
5298 Elf_Internal_Verdef def;
5299 Elf_Internal_Verdaux defaux;
5300 struct bfd_link_hash_entry *bh;
5301 struct elf_link_hash_entry *h;
5302 const char *name;
5304 cdefs = 0;
5305 size = 0;
5307 /* Make space for the base version. */
5308 size += sizeof (Elf_External_Verdef);
5309 size += sizeof (Elf_External_Verdaux);
5310 ++cdefs;
5312 /* Make space for the default version. */
5313 if (info->create_default_symver)
5315 size += sizeof (Elf_External_Verdef);
5316 ++cdefs;
5319 for (t = verdefs; t != NULL; t = t->next)
5321 struct bfd_elf_version_deps *n;
5323 size += sizeof (Elf_External_Verdef);
5324 size += sizeof (Elf_External_Verdaux);
5325 ++cdefs;
5327 for (n = t->deps; n != NULL; n = n->next)
5328 size += sizeof (Elf_External_Verdaux);
5331 s->size = size;
5332 s->contents = bfd_alloc (output_bfd, s->size);
5333 if (s->contents == NULL && s->size != 0)
5334 return FALSE;
5336 /* Fill in the version definition section. */
5338 p = s->contents;
5340 def.vd_version = VER_DEF_CURRENT;
5341 def.vd_flags = VER_FLG_BASE;
5342 def.vd_ndx = 1;
5343 def.vd_cnt = 1;
5344 if (info->create_default_symver)
5346 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5347 def.vd_next = sizeof (Elf_External_Verdef);
5349 else
5351 def.vd_aux = sizeof (Elf_External_Verdef);
5352 def.vd_next = (sizeof (Elf_External_Verdef)
5353 + sizeof (Elf_External_Verdaux));
5356 if (soname_indx != (bfd_size_type) -1)
5358 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5359 soname_indx);
5360 def.vd_hash = bfd_elf_hash (soname);
5361 defaux.vda_name = soname_indx;
5362 name = soname;
5364 else
5366 bfd_size_type indx;
5368 name = lbasename (output_bfd->filename);
5369 def.vd_hash = bfd_elf_hash (name);
5370 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5371 name, FALSE);
5372 if (indx == (bfd_size_type) -1)
5373 return FALSE;
5374 defaux.vda_name = indx;
5376 defaux.vda_next = 0;
5378 _bfd_elf_swap_verdef_out (output_bfd, &def,
5379 (Elf_External_Verdef *) p);
5380 p += sizeof (Elf_External_Verdef);
5381 if (info->create_default_symver)
5383 /* Add a symbol representing this version. */
5384 bh = NULL;
5385 if (! (_bfd_generic_link_add_one_symbol
5386 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5387 0, NULL, FALSE,
5388 get_elf_backend_data (dynobj)->collect, &bh)))
5389 return FALSE;
5390 h = (struct elf_link_hash_entry *) bh;
5391 h->non_elf = 0;
5392 h->def_regular = 1;
5393 h->type = STT_OBJECT;
5394 h->verinfo.vertree = NULL;
5396 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5397 return FALSE;
5399 /* Create a duplicate of the base version with the same
5400 aux block, but different flags. */
5401 def.vd_flags = 0;
5402 def.vd_ndx = 2;
5403 def.vd_aux = sizeof (Elf_External_Verdef);
5404 if (verdefs)
5405 def.vd_next = (sizeof (Elf_External_Verdef)
5406 + sizeof (Elf_External_Verdaux));
5407 else
5408 def.vd_next = 0;
5409 _bfd_elf_swap_verdef_out (output_bfd, &def,
5410 (Elf_External_Verdef *) p);
5411 p += sizeof (Elf_External_Verdef);
5413 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5414 (Elf_External_Verdaux *) p);
5415 p += sizeof (Elf_External_Verdaux);
5417 for (t = verdefs; t != NULL; t = t->next)
5419 unsigned int cdeps;
5420 struct bfd_elf_version_deps *n;
5422 cdeps = 0;
5423 for (n = t->deps; n != NULL; n = n->next)
5424 ++cdeps;
5426 /* Add a symbol representing this version. */
5427 bh = NULL;
5428 if (! (_bfd_generic_link_add_one_symbol
5429 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5430 0, NULL, FALSE,
5431 get_elf_backend_data (dynobj)->collect, &bh)))
5432 return FALSE;
5433 h = (struct elf_link_hash_entry *) bh;
5434 h->non_elf = 0;
5435 h->def_regular = 1;
5436 h->type = STT_OBJECT;
5437 h->verinfo.vertree = t;
5439 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5440 return FALSE;
5442 def.vd_version = VER_DEF_CURRENT;
5443 def.vd_flags = 0;
5444 if (t->globals.list == NULL
5445 && t->locals.list == NULL
5446 && ! t->used)
5447 def.vd_flags |= VER_FLG_WEAK;
5448 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5449 def.vd_cnt = cdeps + 1;
5450 def.vd_hash = bfd_elf_hash (t->name);
5451 def.vd_aux = sizeof (Elf_External_Verdef);
5452 def.vd_next = 0;
5453 if (t->next != NULL)
5454 def.vd_next = (sizeof (Elf_External_Verdef)
5455 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5457 _bfd_elf_swap_verdef_out (output_bfd, &def,
5458 (Elf_External_Verdef *) p);
5459 p += sizeof (Elf_External_Verdef);
5461 defaux.vda_name = h->dynstr_index;
5462 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5463 h->dynstr_index);
5464 defaux.vda_next = 0;
5465 if (t->deps != NULL)
5466 defaux.vda_next = sizeof (Elf_External_Verdaux);
5467 t->name_indx = defaux.vda_name;
5469 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5470 (Elf_External_Verdaux *) p);
5471 p += sizeof (Elf_External_Verdaux);
5473 for (n = t->deps; n != NULL; n = n->next)
5475 if (n->version_needed == NULL)
5477 /* This can happen if there was an error in the
5478 version script. */
5479 defaux.vda_name = 0;
5481 else
5483 defaux.vda_name = n->version_needed->name_indx;
5484 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5485 defaux.vda_name);
5487 if (n->next == NULL)
5488 defaux.vda_next = 0;
5489 else
5490 defaux.vda_next = sizeof (Elf_External_Verdaux);
5492 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5493 (Elf_External_Verdaux *) p);
5494 p += sizeof (Elf_External_Verdaux);
5498 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5499 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5500 return FALSE;
5502 elf_tdata (output_bfd)->cverdefs = cdefs;
5505 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5507 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5508 return FALSE;
5510 else if (info->flags & DF_BIND_NOW)
5512 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5513 return FALSE;
5516 if (info->flags_1)
5518 if (info->executable)
5519 info->flags_1 &= ~ (DF_1_INITFIRST
5520 | DF_1_NODELETE
5521 | DF_1_NOOPEN);
5522 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5523 return FALSE;
5526 /* Work out the size of the version reference section. */
5528 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5529 BFD_ASSERT (s != NULL);
5531 struct elf_find_verdep_info sinfo;
5533 sinfo.output_bfd = output_bfd;
5534 sinfo.info = info;
5535 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5536 if (sinfo.vers == 0)
5537 sinfo.vers = 1;
5538 sinfo.failed = FALSE;
5540 elf_link_hash_traverse (elf_hash_table (info),
5541 _bfd_elf_link_find_version_dependencies,
5542 &sinfo);
5544 if (elf_tdata (output_bfd)->verref == NULL)
5545 s->flags |= SEC_EXCLUDE;
5546 else
5548 Elf_Internal_Verneed *t;
5549 unsigned int size;
5550 unsigned int crefs;
5551 bfd_byte *p;
5553 /* Build the version definition section. */
5554 size = 0;
5555 crefs = 0;
5556 for (t = elf_tdata (output_bfd)->verref;
5557 t != NULL;
5558 t = t->vn_nextref)
5560 Elf_Internal_Vernaux *a;
5562 size += sizeof (Elf_External_Verneed);
5563 ++crefs;
5564 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5565 size += sizeof (Elf_External_Vernaux);
5568 s->size = size;
5569 s->contents = bfd_alloc (output_bfd, s->size);
5570 if (s->contents == NULL)
5571 return FALSE;
5573 p = s->contents;
5574 for (t = elf_tdata (output_bfd)->verref;
5575 t != NULL;
5576 t = t->vn_nextref)
5578 unsigned int caux;
5579 Elf_Internal_Vernaux *a;
5580 bfd_size_type indx;
5582 caux = 0;
5583 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5584 ++caux;
5586 t->vn_version = VER_NEED_CURRENT;
5587 t->vn_cnt = caux;
5588 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5589 elf_dt_name (t->vn_bfd) != NULL
5590 ? elf_dt_name (t->vn_bfd)
5591 : lbasename (t->vn_bfd->filename),
5592 FALSE);
5593 if (indx == (bfd_size_type) -1)
5594 return FALSE;
5595 t->vn_file = indx;
5596 t->vn_aux = sizeof (Elf_External_Verneed);
5597 if (t->vn_nextref == NULL)
5598 t->vn_next = 0;
5599 else
5600 t->vn_next = (sizeof (Elf_External_Verneed)
5601 + caux * sizeof (Elf_External_Vernaux));
5603 _bfd_elf_swap_verneed_out (output_bfd, t,
5604 (Elf_External_Verneed *) p);
5605 p += sizeof (Elf_External_Verneed);
5607 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5609 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5610 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5611 a->vna_nodename, FALSE);
5612 if (indx == (bfd_size_type) -1)
5613 return FALSE;
5614 a->vna_name = indx;
5615 if (a->vna_nextptr == NULL)
5616 a->vna_next = 0;
5617 else
5618 a->vna_next = sizeof (Elf_External_Vernaux);
5620 _bfd_elf_swap_vernaux_out (output_bfd, a,
5621 (Elf_External_Vernaux *) p);
5622 p += sizeof (Elf_External_Vernaux);
5626 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5627 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5628 return FALSE;
5630 elf_tdata (output_bfd)->cverrefs = crefs;
5634 if ((elf_tdata (output_bfd)->cverrefs == 0
5635 && elf_tdata (output_bfd)->cverdefs == 0)
5636 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5637 &section_sym_count) == 0)
5639 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5640 s->flags |= SEC_EXCLUDE;
5643 return TRUE;
5646 bfd_boolean
5647 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5649 if (!is_elf_hash_table (info->hash))
5650 return TRUE;
5652 if (elf_hash_table (info)->dynamic_sections_created)
5654 bfd *dynobj;
5655 const struct elf_backend_data *bed;
5656 asection *s;
5657 bfd_size_type dynsymcount;
5658 unsigned long section_sym_count;
5659 size_t bucketcount = 0;
5660 size_t hash_entry_size;
5661 unsigned int dtagcount;
5663 dynobj = elf_hash_table (info)->dynobj;
5665 /* Assign dynsym indicies. In a shared library we generate a
5666 section symbol for each output section, which come first.
5667 Next come all of the back-end allocated local dynamic syms,
5668 followed by the rest of the global symbols. */
5670 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5671 &section_sym_count);
5673 /* Work out the size of the symbol version section. */
5674 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5675 BFD_ASSERT (s != NULL);
5676 if (dynsymcount != 0
5677 && (s->flags & SEC_EXCLUDE) == 0)
5679 s->size = dynsymcount * sizeof (Elf_External_Versym);
5680 s->contents = bfd_zalloc (output_bfd, s->size);
5681 if (s->contents == NULL)
5682 return FALSE;
5684 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5685 return FALSE;
5688 /* Set the size of the .dynsym and .hash sections. We counted
5689 the number of dynamic symbols in elf_link_add_object_symbols.
5690 We will build the contents of .dynsym and .hash when we build
5691 the final symbol table, because until then we do not know the
5692 correct value to give the symbols. We built the .dynstr
5693 section as we went along in elf_link_add_object_symbols. */
5694 s = bfd_get_section_by_name (dynobj, ".dynsym");
5695 BFD_ASSERT (s != NULL);
5696 bed = get_elf_backend_data (output_bfd);
5697 s->size = dynsymcount * bed->s->sizeof_sym;
5699 if (dynsymcount != 0)
5701 s->contents = bfd_alloc (output_bfd, s->size);
5702 if (s->contents == NULL)
5703 return FALSE;
5705 /* The first entry in .dynsym is a dummy symbol.
5706 Clear all the section syms, in case we don't output them all. */
5707 ++section_sym_count;
5708 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5711 /* Compute the size of the hashing table. As a side effect this
5712 computes the hash values for all the names we export. */
5713 bucketcount = compute_bucket_count (info);
5715 s = bfd_get_section_by_name (dynobj, ".hash");
5716 BFD_ASSERT (s != NULL);
5717 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5718 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5719 s->contents = bfd_zalloc (output_bfd, s->size);
5720 if (s->contents == NULL)
5721 return FALSE;
5723 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5724 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5725 s->contents + hash_entry_size);
5727 elf_hash_table (info)->bucketcount = bucketcount;
5729 s = bfd_get_section_by_name (dynobj, ".dynstr");
5730 BFD_ASSERT (s != NULL);
5732 elf_finalize_dynstr (output_bfd, info);
5734 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5736 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5737 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5738 return FALSE;
5741 return TRUE;
5744 /* Final phase of ELF linker. */
5746 /* A structure we use to avoid passing large numbers of arguments. */
5748 struct elf_final_link_info
5750 /* General link information. */
5751 struct bfd_link_info *info;
5752 /* Output BFD. */
5753 bfd *output_bfd;
5754 /* Symbol string table. */
5755 struct bfd_strtab_hash *symstrtab;
5756 /* .dynsym section. */
5757 asection *dynsym_sec;
5758 /* .hash section. */
5759 asection *hash_sec;
5760 /* symbol version section (.gnu.version). */
5761 asection *symver_sec;
5762 /* Buffer large enough to hold contents of any section. */
5763 bfd_byte *contents;
5764 /* Buffer large enough to hold external relocs of any section. */
5765 void *external_relocs;
5766 /* Buffer large enough to hold internal relocs of any section. */
5767 Elf_Internal_Rela *internal_relocs;
5768 /* Buffer large enough to hold external local symbols of any input
5769 BFD. */
5770 bfd_byte *external_syms;
5771 /* And a buffer for symbol section indices. */
5772 Elf_External_Sym_Shndx *locsym_shndx;
5773 /* Buffer large enough to hold internal local symbols of any input
5774 BFD. */
5775 Elf_Internal_Sym *internal_syms;
5776 /* Array large enough to hold a symbol index for each local symbol
5777 of any input BFD. */
5778 long *indices;
5779 /* Array large enough to hold a section pointer for each local
5780 symbol of any input BFD. */
5781 asection **sections;
5782 /* Buffer to hold swapped out symbols. */
5783 bfd_byte *symbuf;
5784 /* And one for symbol section indices. */
5785 Elf_External_Sym_Shndx *symshndxbuf;
5786 /* Number of swapped out symbols in buffer. */
5787 size_t symbuf_count;
5788 /* Number of symbols which fit in symbuf. */
5789 size_t symbuf_size;
5790 /* And same for symshndxbuf. */
5791 size_t shndxbuf_size;
5794 /* This struct is used to pass information to elf_link_output_extsym. */
5796 struct elf_outext_info
5798 bfd_boolean failed;
5799 bfd_boolean localsyms;
5800 struct elf_final_link_info *finfo;
5803 /* When performing a relocatable link, the input relocations are
5804 preserved. But, if they reference global symbols, the indices
5805 referenced must be updated. Update all the relocations in
5806 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5808 static void
5809 elf_link_adjust_relocs (bfd *abfd,
5810 Elf_Internal_Shdr *rel_hdr,
5811 unsigned int count,
5812 struct elf_link_hash_entry **rel_hash)
5814 unsigned int i;
5815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5816 bfd_byte *erela;
5817 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5818 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5819 bfd_vma r_type_mask;
5820 int r_sym_shift;
5822 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5824 swap_in = bed->s->swap_reloc_in;
5825 swap_out = bed->s->swap_reloc_out;
5827 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5829 swap_in = bed->s->swap_reloca_in;
5830 swap_out = bed->s->swap_reloca_out;
5832 else
5833 abort ();
5835 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5836 abort ();
5838 if (bed->s->arch_size == 32)
5840 r_type_mask = 0xff;
5841 r_sym_shift = 8;
5843 else
5845 r_type_mask = 0xffffffff;
5846 r_sym_shift = 32;
5849 erela = rel_hdr->contents;
5850 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5852 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5853 unsigned int j;
5855 if (*rel_hash == NULL)
5856 continue;
5858 BFD_ASSERT ((*rel_hash)->indx >= 0);
5860 (*swap_in) (abfd, erela, irela);
5861 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5862 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5863 | (irela[j].r_info & r_type_mask));
5864 (*swap_out) (abfd, irela, erela);
5868 struct elf_link_sort_rela
5870 union {
5871 bfd_vma offset;
5872 bfd_vma sym_mask;
5873 } u;
5874 enum elf_reloc_type_class type;
5875 /* We use this as an array of size int_rels_per_ext_rel. */
5876 Elf_Internal_Rela rela[1];
5879 static int
5880 elf_link_sort_cmp1 (const void *A, const void *B)
5882 const struct elf_link_sort_rela *a = A;
5883 const struct elf_link_sort_rela *b = B;
5884 int relativea, relativeb;
5886 relativea = a->type == reloc_class_relative;
5887 relativeb = b->type == reloc_class_relative;
5889 if (relativea < relativeb)
5890 return 1;
5891 if (relativea > relativeb)
5892 return -1;
5893 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5894 return -1;
5895 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5896 return 1;
5897 if (a->rela->r_offset < b->rela->r_offset)
5898 return -1;
5899 if (a->rela->r_offset > b->rela->r_offset)
5900 return 1;
5901 return 0;
5904 static int
5905 elf_link_sort_cmp2 (const void *A, const void *B)
5907 const struct elf_link_sort_rela *a = A;
5908 const struct elf_link_sort_rela *b = B;
5909 int copya, copyb;
5911 if (a->u.offset < b->u.offset)
5912 return -1;
5913 if (a->u.offset > b->u.offset)
5914 return 1;
5915 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5916 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5917 if (copya < copyb)
5918 return -1;
5919 if (copya > copyb)
5920 return 1;
5921 if (a->rela->r_offset < b->rela->r_offset)
5922 return -1;
5923 if (a->rela->r_offset > b->rela->r_offset)
5924 return 1;
5925 return 0;
5928 static size_t
5929 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5931 asection *reldyn;
5932 bfd_size_type count, size;
5933 size_t i, ret, sort_elt, ext_size;
5934 bfd_byte *sort, *s_non_relative, *p;
5935 struct elf_link_sort_rela *sq;
5936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5937 int i2e = bed->s->int_rels_per_ext_rel;
5938 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5939 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5940 struct bfd_link_order *lo;
5941 bfd_vma r_sym_mask;
5943 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5944 if (reldyn == NULL || reldyn->size == 0)
5946 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5947 if (reldyn == NULL || reldyn->size == 0)
5948 return 0;
5949 ext_size = bed->s->sizeof_rel;
5950 swap_in = bed->s->swap_reloc_in;
5951 swap_out = bed->s->swap_reloc_out;
5953 else
5955 ext_size = bed->s->sizeof_rela;
5956 swap_in = bed->s->swap_reloca_in;
5957 swap_out = bed->s->swap_reloca_out;
5959 count = reldyn->size / ext_size;
5961 size = 0;
5962 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5963 if (lo->type == bfd_indirect_link_order)
5965 asection *o = lo->u.indirect.section;
5966 size += o->size;
5969 if (size != reldyn->size)
5970 return 0;
5972 sort_elt = (sizeof (struct elf_link_sort_rela)
5973 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5974 sort = bfd_zmalloc (sort_elt * count);
5975 if (sort == NULL)
5977 (*info->callbacks->warning)
5978 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5979 return 0;
5982 if (bed->s->arch_size == 32)
5983 r_sym_mask = ~(bfd_vma) 0xff;
5984 else
5985 r_sym_mask = ~(bfd_vma) 0xffffffff;
5987 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5988 if (lo->type == bfd_indirect_link_order)
5990 bfd_byte *erel, *erelend;
5991 asection *o = lo->u.indirect.section;
5993 if (o->contents == NULL && o->size != 0)
5995 /* This is a reloc section that is being handled as a normal
5996 section. See bfd_section_from_shdr. We can't combine
5997 relocs in this case. */
5998 free (sort);
5999 return 0;
6001 erel = o->contents;
6002 erelend = o->contents + o->size;
6003 p = sort + o->output_offset / ext_size * sort_elt;
6004 while (erel < erelend)
6006 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6007 (*swap_in) (abfd, erel, s->rela);
6008 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6009 s->u.sym_mask = r_sym_mask;
6010 p += sort_elt;
6011 erel += ext_size;
6015 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6017 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6019 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6020 if (s->type != reloc_class_relative)
6021 break;
6023 ret = i;
6024 s_non_relative = p;
6026 sq = (struct elf_link_sort_rela *) s_non_relative;
6027 for (; i < count; i++, p += sort_elt)
6029 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6030 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6031 sq = sp;
6032 sp->u.offset = sq->rela->r_offset;
6035 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6037 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6038 if (lo->type == bfd_indirect_link_order)
6040 bfd_byte *erel, *erelend;
6041 asection *o = lo->u.indirect.section;
6043 erel = o->contents;
6044 erelend = o->contents + o->size;
6045 p = sort + o->output_offset / ext_size * sort_elt;
6046 while (erel < erelend)
6048 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6049 (*swap_out) (abfd, s->rela, erel);
6050 p += sort_elt;
6051 erel += ext_size;
6055 free (sort);
6056 *psec = reldyn;
6057 return ret;
6060 /* Flush the output symbols to the file. */
6062 static bfd_boolean
6063 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6064 const struct elf_backend_data *bed)
6066 if (finfo->symbuf_count > 0)
6068 Elf_Internal_Shdr *hdr;
6069 file_ptr pos;
6070 bfd_size_type amt;
6072 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6073 pos = hdr->sh_offset + hdr->sh_size;
6074 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6075 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6076 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6077 return FALSE;
6079 hdr->sh_size += amt;
6080 finfo->symbuf_count = 0;
6083 return TRUE;
6086 /* Add a symbol to the output symbol table. */
6088 static bfd_boolean
6089 elf_link_output_sym (struct elf_final_link_info *finfo,
6090 const char *name,
6091 Elf_Internal_Sym *elfsym,
6092 asection *input_sec,
6093 struct elf_link_hash_entry *h)
6095 bfd_byte *dest;
6096 Elf_External_Sym_Shndx *destshndx;
6097 bfd_boolean (*output_symbol_hook)
6098 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6099 struct elf_link_hash_entry *);
6100 const struct elf_backend_data *bed;
6102 bed = get_elf_backend_data (finfo->output_bfd);
6103 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6104 if (output_symbol_hook != NULL)
6106 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6107 return FALSE;
6110 if (name == NULL || *name == '\0')
6111 elfsym->st_name = 0;
6112 else if (input_sec->flags & SEC_EXCLUDE)
6113 elfsym->st_name = 0;
6114 else
6116 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6117 name, TRUE, FALSE);
6118 if (elfsym->st_name == (unsigned long) -1)
6119 return FALSE;
6122 if (finfo->symbuf_count >= finfo->symbuf_size)
6124 if (! elf_link_flush_output_syms (finfo, bed))
6125 return FALSE;
6128 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6129 destshndx = finfo->symshndxbuf;
6130 if (destshndx != NULL)
6132 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6134 bfd_size_type amt;
6136 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6137 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6138 if (destshndx == NULL)
6139 return FALSE;
6140 memset ((char *) destshndx + amt, 0, amt);
6141 finfo->shndxbuf_size *= 2;
6143 destshndx += bfd_get_symcount (finfo->output_bfd);
6146 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6147 finfo->symbuf_count += 1;
6148 bfd_get_symcount (finfo->output_bfd) += 1;
6150 return TRUE;
6153 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6154 allowing an unsatisfied unversioned symbol in the DSO to match a
6155 versioned symbol that would normally require an explicit version.
6156 We also handle the case that a DSO references a hidden symbol
6157 which may be satisfied by a versioned symbol in another DSO. */
6159 static bfd_boolean
6160 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6161 const struct elf_backend_data *bed,
6162 struct elf_link_hash_entry *h)
6164 bfd *abfd;
6165 struct elf_link_loaded_list *loaded;
6167 if (!is_elf_hash_table (info->hash))
6168 return FALSE;
6170 switch (h->root.type)
6172 default:
6173 abfd = NULL;
6174 break;
6176 case bfd_link_hash_undefined:
6177 case bfd_link_hash_undefweak:
6178 abfd = h->root.u.undef.abfd;
6179 if ((abfd->flags & DYNAMIC) == 0
6180 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6181 return FALSE;
6182 break;
6184 case bfd_link_hash_defined:
6185 case bfd_link_hash_defweak:
6186 abfd = h->root.u.def.section->owner;
6187 break;
6189 case bfd_link_hash_common:
6190 abfd = h->root.u.c.p->section->owner;
6191 break;
6193 BFD_ASSERT (abfd != NULL);
6195 for (loaded = elf_hash_table (info)->loaded;
6196 loaded != NULL;
6197 loaded = loaded->next)
6199 bfd *input;
6200 Elf_Internal_Shdr *hdr;
6201 bfd_size_type symcount;
6202 bfd_size_type extsymcount;
6203 bfd_size_type extsymoff;
6204 Elf_Internal_Shdr *versymhdr;
6205 Elf_Internal_Sym *isym;
6206 Elf_Internal_Sym *isymend;
6207 Elf_Internal_Sym *isymbuf;
6208 Elf_External_Versym *ever;
6209 Elf_External_Versym *extversym;
6211 input = loaded->abfd;
6213 /* We check each DSO for a possible hidden versioned definition. */
6214 if (input == abfd
6215 || (input->flags & DYNAMIC) == 0
6216 || elf_dynversym (input) == 0)
6217 continue;
6219 hdr = &elf_tdata (input)->dynsymtab_hdr;
6221 symcount = hdr->sh_size / bed->s->sizeof_sym;
6222 if (elf_bad_symtab (input))
6224 extsymcount = symcount;
6225 extsymoff = 0;
6227 else
6229 extsymcount = symcount - hdr->sh_info;
6230 extsymoff = hdr->sh_info;
6233 if (extsymcount == 0)
6234 continue;
6236 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6237 NULL, NULL, NULL);
6238 if (isymbuf == NULL)
6239 return FALSE;
6241 /* Read in any version definitions. */
6242 versymhdr = &elf_tdata (input)->dynversym_hdr;
6243 extversym = bfd_malloc (versymhdr->sh_size);
6244 if (extversym == NULL)
6245 goto error_ret;
6247 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6248 || (bfd_bread (extversym, versymhdr->sh_size, input)
6249 != versymhdr->sh_size))
6251 free (extversym);
6252 error_ret:
6253 free (isymbuf);
6254 return FALSE;
6257 ever = extversym + extsymoff;
6258 isymend = isymbuf + extsymcount;
6259 for (isym = isymbuf; isym < isymend; isym++, ever++)
6261 const char *name;
6262 Elf_Internal_Versym iver;
6263 unsigned short version_index;
6265 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6266 || isym->st_shndx == SHN_UNDEF)
6267 continue;
6269 name = bfd_elf_string_from_elf_section (input,
6270 hdr->sh_link,
6271 isym->st_name);
6272 if (strcmp (name, h->root.root.string) != 0)
6273 continue;
6275 _bfd_elf_swap_versym_in (input, ever, &iver);
6277 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6279 /* If we have a non-hidden versioned sym, then it should
6280 have provided a definition for the undefined sym. */
6281 abort ();
6284 version_index = iver.vs_vers & VERSYM_VERSION;
6285 if (version_index == 1 || version_index == 2)
6287 /* This is the base or first version. We can use it. */
6288 free (extversym);
6289 free (isymbuf);
6290 return TRUE;
6294 free (extversym);
6295 free (isymbuf);
6298 return FALSE;
6301 /* Add an external symbol to the symbol table. This is called from
6302 the hash table traversal routine. When generating a shared object,
6303 we go through the symbol table twice. The first time we output
6304 anything that might have been forced to local scope in a version
6305 script. The second time we output the symbols that are still
6306 global symbols. */
6308 static bfd_boolean
6309 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6311 struct elf_outext_info *eoinfo = data;
6312 struct elf_final_link_info *finfo = eoinfo->finfo;
6313 bfd_boolean strip;
6314 Elf_Internal_Sym sym;
6315 asection *input_sec;
6316 const struct elf_backend_data *bed;
6318 if (h->root.type == bfd_link_hash_warning)
6320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6321 if (h->root.type == bfd_link_hash_new)
6322 return TRUE;
6325 /* Decide whether to output this symbol in this pass. */
6326 if (eoinfo->localsyms)
6328 if (!h->forced_local)
6329 return TRUE;
6331 else
6333 if (h->forced_local)
6334 return TRUE;
6337 bed = get_elf_backend_data (finfo->output_bfd);
6339 /* If we have an undefined symbol reference here then it must have
6340 come from a shared library that is being linked in. (Undefined
6341 references in regular files have already been handled). If we
6342 are reporting errors for this situation then do so now. */
6343 if (h->root.type == bfd_link_hash_undefined
6344 && h->ref_dynamic
6345 && !h->ref_regular
6346 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6347 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6349 if (! ((*finfo->info->callbacks->undefined_symbol)
6350 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6351 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6353 eoinfo->failed = TRUE;
6354 return FALSE;
6358 /* We should also warn if a forced local symbol is referenced from
6359 shared libraries. */
6360 if (! finfo->info->relocatable
6361 && (! finfo->info->shared)
6362 && h->forced_local
6363 && h->ref_dynamic
6364 && !h->dynamic_def
6365 && !h->dynamic_weak
6366 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6368 (*_bfd_error_handler)
6369 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6370 finfo->output_bfd,
6371 h->root.u.def.section == bfd_abs_section_ptr
6372 ? finfo->output_bfd : h->root.u.def.section->owner,
6373 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6374 ? "internal"
6375 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6376 ? "hidden" : "local",
6377 h->root.root.string);
6378 eoinfo->failed = TRUE;
6379 return FALSE;
6382 /* We don't want to output symbols that have never been mentioned by
6383 a regular file, or that we have been told to strip. However, if
6384 h->indx is set to -2, the symbol is used by a reloc and we must
6385 output it. */
6386 if (h->indx == -2)
6387 strip = FALSE;
6388 else if ((h->def_dynamic
6389 || h->ref_dynamic
6390 || h->root.type == bfd_link_hash_new)
6391 && !h->def_regular
6392 && !h->ref_regular)
6393 strip = TRUE;
6394 else if (finfo->info->strip == strip_all)
6395 strip = TRUE;
6396 else if (finfo->info->strip == strip_some
6397 && bfd_hash_lookup (finfo->info->keep_hash,
6398 h->root.root.string, FALSE, FALSE) == NULL)
6399 strip = TRUE;
6400 else if (finfo->info->strip_discarded
6401 && (h->root.type == bfd_link_hash_defined
6402 || h->root.type == bfd_link_hash_defweak)
6403 && elf_discarded_section (h->root.u.def.section))
6404 strip = TRUE;
6405 else
6406 strip = FALSE;
6408 /* If we're stripping it, and it's not a dynamic symbol, there's
6409 nothing else to do unless it is a forced local symbol. */
6410 if (strip
6411 && h->dynindx == -1
6412 && !h->forced_local)
6413 return TRUE;
6415 sym.st_value = 0;
6416 sym.st_size = h->size;
6417 sym.st_other = h->other;
6418 if (h->forced_local)
6419 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6420 else if (h->root.type == bfd_link_hash_undefweak
6421 || h->root.type == bfd_link_hash_defweak)
6422 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6423 else
6424 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6426 switch (h->root.type)
6428 default:
6429 case bfd_link_hash_new:
6430 case bfd_link_hash_warning:
6431 abort ();
6432 return FALSE;
6434 case bfd_link_hash_undefined:
6435 case bfd_link_hash_undefweak:
6436 input_sec = bfd_und_section_ptr;
6437 sym.st_shndx = SHN_UNDEF;
6438 break;
6440 case bfd_link_hash_defined:
6441 case bfd_link_hash_defweak:
6443 input_sec = h->root.u.def.section;
6444 if (input_sec->output_section != NULL)
6446 sym.st_shndx =
6447 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6448 input_sec->output_section);
6449 if (sym.st_shndx == SHN_BAD)
6451 (*_bfd_error_handler)
6452 (_("%B: could not find output section %A for input section %A"),
6453 finfo->output_bfd, input_sec->output_section, input_sec);
6454 eoinfo->failed = TRUE;
6455 return FALSE;
6458 /* ELF symbols in relocatable files are section relative,
6459 but in nonrelocatable files they are virtual
6460 addresses. */
6461 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6462 if (! finfo->info->relocatable)
6464 sym.st_value += input_sec->output_section->vma;
6465 if (h->type == STT_TLS)
6467 /* STT_TLS symbols are relative to PT_TLS segment
6468 base. */
6469 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6470 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6474 else
6476 BFD_ASSERT (input_sec->owner == NULL
6477 || (input_sec->owner->flags & DYNAMIC) != 0);
6478 sym.st_shndx = SHN_UNDEF;
6479 input_sec = bfd_und_section_ptr;
6482 break;
6484 case bfd_link_hash_common:
6485 input_sec = h->root.u.c.p->section;
6486 sym.st_shndx = bed->common_section_index (input_sec);
6487 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6488 break;
6490 case bfd_link_hash_indirect:
6491 /* These symbols are created by symbol versioning. They point
6492 to the decorated version of the name. For example, if the
6493 symbol foo@@GNU_1.2 is the default, which should be used when
6494 foo is used with no version, then we add an indirect symbol
6495 foo which points to foo@@GNU_1.2. We ignore these symbols,
6496 since the indirected symbol is already in the hash table. */
6497 return TRUE;
6500 /* Give the processor backend a chance to tweak the symbol value,
6501 and also to finish up anything that needs to be done for this
6502 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6503 forced local syms when non-shared is due to a historical quirk. */
6504 if ((h->dynindx != -1
6505 || h->forced_local)
6506 && ((finfo->info->shared
6507 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6508 || h->root.type != bfd_link_hash_undefweak))
6509 || !h->forced_local)
6510 && elf_hash_table (finfo->info)->dynamic_sections_created)
6512 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6513 (finfo->output_bfd, finfo->info, h, &sym)))
6515 eoinfo->failed = TRUE;
6516 return FALSE;
6520 /* If we are marking the symbol as undefined, and there are no
6521 non-weak references to this symbol from a regular object, then
6522 mark the symbol as weak undefined; if there are non-weak
6523 references, mark the symbol as strong. We can't do this earlier,
6524 because it might not be marked as undefined until the
6525 finish_dynamic_symbol routine gets through with it. */
6526 if (sym.st_shndx == SHN_UNDEF
6527 && h->ref_regular
6528 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6529 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6531 int bindtype;
6533 if (h->ref_regular_nonweak)
6534 bindtype = STB_GLOBAL;
6535 else
6536 bindtype = STB_WEAK;
6537 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6540 /* If a non-weak symbol with non-default visibility is not defined
6541 locally, it is a fatal error. */
6542 if (! finfo->info->relocatable
6543 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6544 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6545 && h->root.type == bfd_link_hash_undefined
6546 && !h->def_regular)
6548 (*_bfd_error_handler)
6549 (_("%B: %s symbol `%s' isn't defined"),
6550 finfo->output_bfd,
6551 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6552 ? "protected"
6553 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6554 ? "internal" : "hidden",
6555 h->root.root.string);
6556 eoinfo->failed = TRUE;
6557 return FALSE;
6560 /* If this symbol should be put in the .dynsym section, then put it
6561 there now. We already know the symbol index. We also fill in
6562 the entry in the .hash section. */
6563 if (h->dynindx != -1
6564 && elf_hash_table (finfo->info)->dynamic_sections_created)
6566 size_t bucketcount;
6567 size_t bucket;
6568 size_t hash_entry_size;
6569 bfd_byte *bucketpos;
6570 bfd_vma chain;
6571 bfd_byte *esym;
6573 sym.st_name = h->dynstr_index;
6574 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6575 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6577 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6578 bucket = h->u.elf_hash_value % bucketcount;
6579 hash_entry_size
6580 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6581 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6582 + (bucket + 2) * hash_entry_size);
6583 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6584 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6585 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6586 ((bfd_byte *) finfo->hash_sec->contents
6587 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6589 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6591 Elf_Internal_Versym iversym;
6592 Elf_External_Versym *eversym;
6594 if (!h->def_regular)
6596 if (h->verinfo.verdef == NULL)
6597 iversym.vs_vers = 0;
6598 else
6599 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6601 else
6603 if (h->verinfo.vertree == NULL)
6604 iversym.vs_vers = 1;
6605 else
6606 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6607 if (finfo->info->create_default_symver)
6608 iversym.vs_vers++;
6611 if (h->hidden)
6612 iversym.vs_vers |= VERSYM_HIDDEN;
6614 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6615 eversym += h->dynindx;
6616 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6620 /* If we're stripping it, then it was just a dynamic symbol, and
6621 there's nothing else to do. */
6622 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6623 return TRUE;
6625 h->indx = bfd_get_symcount (finfo->output_bfd);
6627 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6629 eoinfo->failed = TRUE;
6630 return FALSE;
6633 return TRUE;
6636 /* Return TRUE if special handling is done for relocs in SEC against
6637 symbols defined in discarded sections. */
6639 static bfd_boolean
6640 elf_section_ignore_discarded_relocs (asection *sec)
6642 const struct elf_backend_data *bed;
6644 switch (sec->sec_info_type)
6646 case ELF_INFO_TYPE_STABS:
6647 case ELF_INFO_TYPE_EH_FRAME:
6648 return TRUE;
6649 default:
6650 break;
6653 bed = get_elf_backend_data (sec->owner);
6654 if (bed->elf_backend_ignore_discarded_relocs != NULL
6655 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6656 return TRUE;
6658 return FALSE;
6661 /* Return a mask saying how ld should treat relocations in SEC against
6662 symbols defined in discarded sections. If this function returns
6663 COMPLAIN set, ld will issue a warning message. If this function
6664 returns PRETEND set, and the discarded section was link-once and the
6665 same size as the kept link-once section, ld will pretend that the
6666 symbol was actually defined in the kept section. Otherwise ld will
6667 zero the reloc (at least that is the intent, but some cooperation by
6668 the target dependent code is needed, particularly for REL targets). */
6670 unsigned int
6671 _bfd_elf_default_action_discarded (asection *sec)
6673 if (sec->flags & SEC_DEBUGGING)
6674 return PRETEND;
6676 if (strcmp (".eh_frame", sec->name) == 0)
6677 return 0;
6679 if (strcmp (".gcc_except_table", sec->name) == 0)
6680 return 0;
6682 return COMPLAIN | PRETEND;
6685 /* Find a match between a section and a member of a section group. */
6687 static asection *
6688 match_group_member (asection *sec, asection *group)
6690 asection *first = elf_next_in_group (group);
6691 asection *s = first;
6693 while (s != NULL)
6695 if (bfd_elf_match_symbols_in_sections (s, sec))
6696 return s;
6698 if (s == first)
6699 break;
6702 return NULL;
6705 /* Check if the kept section of a discarded section SEC can be used
6706 to replace it. Return the replacement if it is OK. Otherwise return
6707 NULL. */
6709 asection *
6710 _bfd_elf_check_kept_section (asection *sec)
6712 asection *kept;
6714 kept = sec->kept_section;
6715 if (kept != NULL)
6717 if (elf_sec_group (sec) != NULL)
6718 kept = match_group_member (sec, kept);
6719 if (kept != NULL && sec->size != kept->size)
6720 kept = NULL;
6722 return kept;
6725 /* Link an input file into the linker output file. This function
6726 handles all the sections and relocations of the input file at once.
6727 This is so that we only have to read the local symbols once, and
6728 don't have to keep them in memory. */
6730 static bfd_boolean
6731 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6733 bfd_boolean (*relocate_section)
6734 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6735 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6736 bfd *output_bfd;
6737 Elf_Internal_Shdr *symtab_hdr;
6738 size_t locsymcount;
6739 size_t extsymoff;
6740 Elf_Internal_Sym *isymbuf;
6741 Elf_Internal_Sym *isym;
6742 Elf_Internal_Sym *isymend;
6743 long *pindex;
6744 asection **ppsection;
6745 asection *o;
6746 const struct elf_backend_data *bed;
6747 bfd_boolean emit_relocs;
6748 struct elf_link_hash_entry **sym_hashes;
6750 output_bfd = finfo->output_bfd;
6751 bed = get_elf_backend_data (output_bfd);
6752 relocate_section = bed->elf_backend_relocate_section;
6754 /* If this is a dynamic object, we don't want to do anything here:
6755 we don't want the local symbols, and we don't want the section
6756 contents. */
6757 if ((input_bfd->flags & DYNAMIC) != 0)
6758 return TRUE;
6760 emit_relocs = (finfo->info->relocatable
6761 || finfo->info->emitrelocations);
6763 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6764 if (elf_bad_symtab (input_bfd))
6766 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6767 extsymoff = 0;
6769 else
6771 locsymcount = symtab_hdr->sh_info;
6772 extsymoff = symtab_hdr->sh_info;
6775 /* Read the local symbols. */
6776 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6777 if (isymbuf == NULL && locsymcount != 0)
6779 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6780 finfo->internal_syms,
6781 finfo->external_syms,
6782 finfo->locsym_shndx);
6783 if (isymbuf == NULL)
6784 return FALSE;
6787 /* Find local symbol sections and adjust values of symbols in
6788 SEC_MERGE sections. Write out those local symbols we know are
6789 going into the output file. */
6790 isymend = isymbuf + locsymcount;
6791 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6792 isym < isymend;
6793 isym++, pindex++, ppsection++)
6795 asection *isec;
6796 const char *name;
6797 Elf_Internal_Sym osym;
6799 *pindex = -1;
6801 if (elf_bad_symtab (input_bfd))
6803 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6805 *ppsection = NULL;
6806 continue;
6810 if (isym->st_shndx == SHN_UNDEF)
6811 isec = bfd_und_section_ptr;
6812 else if (isym->st_shndx < SHN_LORESERVE
6813 || isym->st_shndx > SHN_HIRESERVE)
6815 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6816 if (isec
6817 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6818 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6819 isym->st_value =
6820 _bfd_merged_section_offset (output_bfd, &isec,
6821 elf_section_data (isec)->sec_info,
6822 isym->st_value);
6824 else if (isym->st_shndx == SHN_ABS)
6825 isec = bfd_abs_section_ptr;
6826 else if (isym->st_shndx == SHN_COMMON)
6827 isec = bfd_com_section_ptr;
6828 else
6830 /* Don't attempt to output symbols with st_shnx in the
6831 reserved range other than SHN_ABS and SHN_COMMON. */
6832 *ppsection = NULL;
6833 continue;
6836 *ppsection = isec;
6838 /* Don't output the first, undefined, symbol. */
6839 if (ppsection == finfo->sections)
6840 continue;
6842 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6844 /* We never output section symbols. Instead, we use the
6845 section symbol of the corresponding section in the output
6846 file. */
6847 continue;
6850 /* If we are stripping all symbols, we don't want to output this
6851 one. */
6852 if (finfo->info->strip == strip_all)
6853 continue;
6855 /* If we are discarding all local symbols, we don't want to
6856 output this one. If we are generating a relocatable output
6857 file, then some of the local symbols may be required by
6858 relocs; we output them below as we discover that they are
6859 needed. */
6860 if (finfo->info->discard == discard_all)
6861 continue;
6863 /* If this symbol is defined in a section which we are
6864 discarding, we don't need to keep it. */
6865 if (isym->st_shndx != SHN_UNDEF
6866 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6867 && (isec == NULL
6868 || bfd_section_removed_from_list (output_bfd,
6869 isec->output_section)))
6870 continue;
6872 /* Get the name of the symbol. */
6873 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6874 isym->st_name);
6875 if (name == NULL)
6876 return FALSE;
6878 /* See if we are discarding symbols with this name. */
6879 if ((finfo->info->strip == strip_some
6880 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6881 == NULL))
6882 || (((finfo->info->discard == discard_sec_merge
6883 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6884 || finfo->info->discard == discard_l)
6885 && bfd_is_local_label_name (input_bfd, name)))
6886 continue;
6888 /* If we get here, we are going to output this symbol. */
6890 osym = *isym;
6892 /* Adjust the section index for the output file. */
6893 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6894 isec->output_section);
6895 if (osym.st_shndx == SHN_BAD)
6896 return FALSE;
6898 *pindex = bfd_get_symcount (output_bfd);
6900 /* ELF symbols in relocatable files are section relative, but
6901 in executable files they are virtual addresses. Note that
6902 this code assumes that all ELF sections have an associated
6903 BFD section with a reasonable value for output_offset; below
6904 we assume that they also have a reasonable value for
6905 output_section. Any special sections must be set up to meet
6906 these requirements. */
6907 osym.st_value += isec->output_offset;
6908 if (! finfo->info->relocatable)
6910 osym.st_value += isec->output_section->vma;
6911 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6913 /* STT_TLS symbols are relative to PT_TLS segment base. */
6914 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6915 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6919 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6920 return FALSE;
6923 /* Relocate the contents of each section. */
6924 sym_hashes = elf_sym_hashes (input_bfd);
6925 for (o = input_bfd->sections; o != NULL; o = o->next)
6927 bfd_byte *contents;
6929 if (! o->linker_mark)
6931 /* This section was omitted from the link. */
6932 continue;
6935 if ((o->flags & SEC_HAS_CONTENTS) == 0
6936 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6937 continue;
6939 if ((o->flags & SEC_LINKER_CREATED) != 0)
6941 /* Section was created by _bfd_elf_link_create_dynamic_sections
6942 or somesuch. */
6943 continue;
6946 /* Get the contents of the section. They have been cached by a
6947 relaxation routine. Note that o is a section in an input
6948 file, so the contents field will not have been set by any of
6949 the routines which work on output files. */
6950 if (elf_section_data (o)->this_hdr.contents != NULL)
6951 contents = elf_section_data (o)->this_hdr.contents;
6952 else
6954 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6956 contents = finfo->contents;
6957 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6958 return FALSE;
6961 if ((o->flags & SEC_RELOC) != 0)
6963 Elf_Internal_Rela *internal_relocs;
6964 bfd_vma r_type_mask;
6965 int r_sym_shift;
6967 /* Get the swapped relocs. */
6968 internal_relocs
6969 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6970 finfo->internal_relocs, FALSE);
6971 if (internal_relocs == NULL
6972 && o->reloc_count > 0)
6973 return FALSE;
6975 if (bed->s->arch_size == 32)
6977 r_type_mask = 0xff;
6978 r_sym_shift = 8;
6980 else
6982 r_type_mask = 0xffffffff;
6983 r_sym_shift = 32;
6986 /* Run through the relocs looking for any against symbols
6987 from discarded sections and section symbols from
6988 removed link-once sections. Complain about relocs
6989 against discarded sections. Zero relocs against removed
6990 link-once sections. Preserve debug information as much
6991 as we can. */
6992 if (!elf_section_ignore_discarded_relocs (o))
6994 Elf_Internal_Rela *rel, *relend;
6995 unsigned int action = (*bed->action_discarded) (o);
6997 rel = internal_relocs;
6998 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6999 for ( ; rel < relend; rel++)
7001 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7002 asection **ps, *sec;
7003 struct elf_link_hash_entry *h = NULL;
7004 const char *sym_name;
7006 if (r_symndx == STN_UNDEF)
7007 continue;
7009 if (r_symndx >= locsymcount
7010 || (elf_bad_symtab (input_bfd)
7011 && finfo->sections[r_symndx] == NULL))
7013 h = sym_hashes[r_symndx - extsymoff];
7015 /* Badly formatted input files can contain relocs that
7016 reference non-existant symbols. Check here so that
7017 we do not seg fault. */
7018 if (h == NULL)
7020 char buffer [32];
7022 sprintf_vma (buffer, rel->r_info);
7023 (*_bfd_error_handler)
7024 (_("error: %B contains a reloc (0x%s) for section %A "
7025 "that references a non-existent global symbol"),
7026 input_bfd, o, buffer);
7027 bfd_set_error (bfd_error_bad_value);
7028 return FALSE;
7031 while (h->root.type == bfd_link_hash_indirect
7032 || h->root.type == bfd_link_hash_warning)
7033 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7035 if (h->root.type != bfd_link_hash_defined
7036 && h->root.type != bfd_link_hash_defweak)
7037 continue;
7039 ps = &h->root.u.def.section;
7040 sym_name = h->root.root.string;
7042 else
7044 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7045 ps = &finfo->sections[r_symndx];
7046 sym_name = bfd_elf_sym_name (input_bfd,
7047 symtab_hdr,
7048 sym, *ps);
7051 /* Complain if the definition comes from a
7052 discarded section. */
7053 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7055 BFD_ASSERT (r_symndx != 0);
7056 if (action & COMPLAIN)
7057 (*finfo->info->callbacks->einfo)
7058 (_("%X`%s' referenced in section `%A' of %B: "
7059 "defined in discarded section `%A' of %B\n"),
7060 sym_name, o, input_bfd, sec, sec->owner);
7062 /* Try to do the best we can to support buggy old
7063 versions of gcc. If we've warned, or this is
7064 debugging info, pretend that the symbol is
7065 really defined in the kept linkonce section.
7066 FIXME: This is quite broken. Modifying the
7067 symbol here means we will be changing all later
7068 uses of the symbol, not just in this section.
7069 The only thing that makes this half reasonable
7070 is that we warn in non-debug sections, and
7071 debug sections tend to come after other
7072 sections. */
7073 if (action & PRETEND)
7075 asection *kept;
7077 kept = _bfd_elf_check_kept_section (sec);
7078 if (kept != NULL)
7080 *ps = kept;
7081 continue;
7085 /* Remove the symbol reference from the reloc, but
7086 don't kill the reloc completely. This is so that
7087 a zero value will be written into the section,
7088 which may have non-zero contents put there by the
7089 assembler. Zero in things like an eh_frame fde
7090 pc_begin allows stack unwinders to recognize the
7091 fde as bogus. */
7092 rel->r_info &= r_type_mask;
7093 rel->r_addend = 0;
7098 /* Relocate the section by invoking a back end routine.
7100 The back end routine is responsible for adjusting the
7101 section contents as necessary, and (if using Rela relocs
7102 and generating a relocatable output file) adjusting the
7103 reloc addend as necessary.
7105 The back end routine does not have to worry about setting
7106 the reloc address or the reloc symbol index.
7108 The back end routine is given a pointer to the swapped in
7109 internal symbols, and can access the hash table entries
7110 for the external symbols via elf_sym_hashes (input_bfd).
7112 When generating relocatable output, the back end routine
7113 must handle STB_LOCAL/STT_SECTION symbols specially. The
7114 output symbol is going to be a section symbol
7115 corresponding to the output section, which will require
7116 the addend to be adjusted. */
7118 if (! (*relocate_section) (output_bfd, finfo->info,
7119 input_bfd, o, contents,
7120 internal_relocs,
7121 isymbuf,
7122 finfo->sections))
7123 return FALSE;
7125 if (emit_relocs)
7127 Elf_Internal_Rela *irela;
7128 Elf_Internal_Rela *irelaend;
7129 bfd_vma last_offset;
7130 struct elf_link_hash_entry **rel_hash;
7131 struct elf_link_hash_entry **rel_hash_list;
7132 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7133 unsigned int next_erel;
7134 bfd_boolean rela_normal;
7136 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7137 rela_normal = (bed->rela_normal
7138 && (input_rel_hdr->sh_entsize
7139 == bed->s->sizeof_rela));
7141 /* Adjust the reloc addresses and symbol indices. */
7143 irela = internal_relocs;
7144 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7145 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7146 + elf_section_data (o->output_section)->rel_count
7147 + elf_section_data (o->output_section)->rel_count2);
7148 rel_hash_list = rel_hash;
7149 last_offset = o->output_offset;
7150 if (!finfo->info->relocatable)
7151 last_offset += o->output_section->vma;
7152 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7154 unsigned long r_symndx;
7155 asection *sec;
7156 Elf_Internal_Sym sym;
7158 if (next_erel == bed->s->int_rels_per_ext_rel)
7160 rel_hash++;
7161 next_erel = 0;
7164 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7165 finfo->info, o,
7166 irela->r_offset);
7167 if (irela->r_offset >= (bfd_vma) -2)
7169 /* This is a reloc for a deleted entry or somesuch.
7170 Turn it into an R_*_NONE reloc, at the same
7171 offset as the last reloc. elf_eh_frame.c and
7172 elf_bfd_discard_info rely on reloc offsets
7173 being ordered. */
7174 irela->r_offset = last_offset;
7175 irela->r_info = 0;
7176 irela->r_addend = 0;
7177 continue;
7180 irela->r_offset += o->output_offset;
7182 /* Relocs in an executable have to be virtual addresses. */
7183 if (!finfo->info->relocatable)
7184 irela->r_offset += o->output_section->vma;
7186 last_offset = irela->r_offset;
7188 r_symndx = irela->r_info >> r_sym_shift;
7189 if (r_symndx == STN_UNDEF)
7190 continue;
7192 if (r_symndx >= locsymcount
7193 || (elf_bad_symtab (input_bfd)
7194 && finfo->sections[r_symndx] == NULL))
7196 struct elf_link_hash_entry *rh;
7197 unsigned long indx;
7199 /* This is a reloc against a global symbol. We
7200 have not yet output all the local symbols, so
7201 we do not know the symbol index of any global
7202 symbol. We set the rel_hash entry for this
7203 reloc to point to the global hash table entry
7204 for this symbol. The symbol index is then
7205 set at the end of bfd_elf_final_link. */
7206 indx = r_symndx - extsymoff;
7207 rh = elf_sym_hashes (input_bfd)[indx];
7208 while (rh->root.type == bfd_link_hash_indirect
7209 || rh->root.type == bfd_link_hash_warning)
7210 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7212 /* Setting the index to -2 tells
7213 elf_link_output_extsym that this symbol is
7214 used by a reloc. */
7215 BFD_ASSERT (rh->indx < 0);
7216 rh->indx = -2;
7218 *rel_hash = rh;
7220 continue;
7223 /* This is a reloc against a local symbol. */
7225 *rel_hash = NULL;
7226 sym = isymbuf[r_symndx];
7227 sec = finfo->sections[r_symndx];
7228 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7230 /* I suppose the backend ought to fill in the
7231 section of any STT_SECTION symbol against a
7232 processor specific section. */
7233 r_symndx = 0;
7234 if (bfd_is_abs_section (sec))
7236 else if (sec == NULL || sec->owner == NULL)
7238 bfd_set_error (bfd_error_bad_value);
7239 return FALSE;
7241 else
7243 asection *osec = sec->output_section;
7245 /* If we have discarded a section, the output
7246 section will be the absolute section. In
7247 case of discarded link-once and discarded
7248 SEC_MERGE sections, use the kept section. */
7249 if (bfd_is_abs_section (osec)
7250 && sec->kept_section != NULL
7251 && sec->kept_section->output_section != NULL)
7253 osec = sec->kept_section->output_section;
7254 irela->r_addend -= osec->vma;
7257 if (!bfd_is_abs_section (osec))
7259 r_symndx = osec->target_index;
7260 BFD_ASSERT (r_symndx != 0);
7264 /* Adjust the addend according to where the
7265 section winds up in the output section. */
7266 if (rela_normal)
7267 irela->r_addend += sec->output_offset;
7269 else
7271 if (finfo->indices[r_symndx] == -1)
7273 unsigned long shlink;
7274 const char *name;
7275 asection *osec;
7277 if (finfo->info->strip == strip_all)
7279 /* You can't do ld -r -s. */
7280 bfd_set_error (bfd_error_invalid_operation);
7281 return FALSE;
7284 /* This symbol was skipped earlier, but
7285 since it is needed by a reloc, we
7286 must output it now. */
7287 shlink = symtab_hdr->sh_link;
7288 name = (bfd_elf_string_from_elf_section
7289 (input_bfd, shlink, sym.st_name));
7290 if (name == NULL)
7291 return FALSE;
7293 osec = sec->output_section;
7294 sym.st_shndx =
7295 _bfd_elf_section_from_bfd_section (output_bfd,
7296 osec);
7297 if (sym.st_shndx == SHN_BAD)
7298 return FALSE;
7300 sym.st_value += sec->output_offset;
7301 if (! finfo->info->relocatable)
7303 sym.st_value += osec->vma;
7304 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7306 /* STT_TLS symbols are relative to PT_TLS
7307 segment base. */
7308 BFD_ASSERT (elf_hash_table (finfo->info)
7309 ->tls_sec != NULL);
7310 sym.st_value -= (elf_hash_table (finfo->info)
7311 ->tls_sec->vma);
7315 finfo->indices[r_symndx]
7316 = bfd_get_symcount (output_bfd);
7318 if (! elf_link_output_sym (finfo, name, &sym, sec,
7319 NULL))
7320 return FALSE;
7323 r_symndx = finfo->indices[r_symndx];
7326 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7327 | (irela->r_info & r_type_mask));
7330 /* Swap out the relocs. */
7331 if (input_rel_hdr->sh_size != 0
7332 && !bed->elf_backend_emit_relocs (output_bfd, o,
7333 input_rel_hdr,
7334 internal_relocs,
7335 rel_hash_list))
7336 return FALSE;
7338 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7339 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7341 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7342 * bed->s->int_rels_per_ext_rel);
7343 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7344 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7345 input_rel_hdr2,
7346 internal_relocs,
7347 rel_hash_list))
7348 return FALSE;
7353 /* Write out the modified section contents. */
7354 if (bed->elf_backend_write_section
7355 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7357 /* Section written out. */
7359 else switch (o->sec_info_type)
7361 case ELF_INFO_TYPE_STABS:
7362 if (! (_bfd_write_section_stabs
7363 (output_bfd,
7364 &elf_hash_table (finfo->info)->stab_info,
7365 o, &elf_section_data (o)->sec_info, contents)))
7366 return FALSE;
7367 break;
7368 case ELF_INFO_TYPE_MERGE:
7369 if (! _bfd_write_merged_section (output_bfd, o,
7370 elf_section_data (o)->sec_info))
7371 return FALSE;
7372 break;
7373 case ELF_INFO_TYPE_EH_FRAME:
7375 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7376 o, contents))
7377 return FALSE;
7379 break;
7380 default:
7382 if (! (o->flags & SEC_EXCLUDE)
7383 && ! bfd_set_section_contents (output_bfd, o->output_section,
7384 contents,
7385 (file_ptr) o->output_offset,
7386 o->size))
7387 return FALSE;
7389 break;
7393 return TRUE;
7396 /* Generate a reloc when linking an ELF file. This is a reloc
7397 requested by the linker, and does not come from any input file. This
7398 is used to build constructor and destructor tables when linking
7399 with -Ur. */
7401 static bfd_boolean
7402 elf_reloc_link_order (bfd *output_bfd,
7403 struct bfd_link_info *info,
7404 asection *output_section,
7405 struct bfd_link_order *link_order)
7407 reloc_howto_type *howto;
7408 long indx;
7409 bfd_vma offset;
7410 bfd_vma addend;
7411 struct elf_link_hash_entry **rel_hash_ptr;
7412 Elf_Internal_Shdr *rel_hdr;
7413 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7414 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7415 bfd_byte *erel;
7416 unsigned int i;
7418 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7419 if (howto == NULL)
7421 bfd_set_error (bfd_error_bad_value);
7422 return FALSE;
7425 addend = link_order->u.reloc.p->addend;
7427 /* Figure out the symbol index. */
7428 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7429 + elf_section_data (output_section)->rel_count
7430 + elf_section_data (output_section)->rel_count2);
7431 if (link_order->type == bfd_section_reloc_link_order)
7433 indx = link_order->u.reloc.p->u.section->target_index;
7434 BFD_ASSERT (indx != 0);
7435 *rel_hash_ptr = NULL;
7437 else
7439 struct elf_link_hash_entry *h;
7441 /* Treat a reloc against a defined symbol as though it were
7442 actually against the section. */
7443 h = ((struct elf_link_hash_entry *)
7444 bfd_wrapped_link_hash_lookup (output_bfd, info,
7445 link_order->u.reloc.p->u.name,
7446 FALSE, FALSE, TRUE));
7447 if (h != NULL
7448 && (h->root.type == bfd_link_hash_defined
7449 || h->root.type == bfd_link_hash_defweak))
7451 asection *section;
7453 section = h->root.u.def.section;
7454 indx = section->output_section->target_index;
7455 *rel_hash_ptr = NULL;
7456 /* It seems that we ought to add the symbol value to the
7457 addend here, but in practice it has already been added
7458 because it was passed to constructor_callback. */
7459 addend += section->output_section->vma + section->output_offset;
7461 else if (h != NULL)
7463 /* Setting the index to -2 tells elf_link_output_extsym that
7464 this symbol is used by a reloc. */
7465 h->indx = -2;
7466 *rel_hash_ptr = h;
7467 indx = 0;
7469 else
7471 if (! ((*info->callbacks->unattached_reloc)
7472 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7473 return FALSE;
7474 indx = 0;
7478 /* If this is an inplace reloc, we must write the addend into the
7479 object file. */
7480 if (howto->partial_inplace && addend != 0)
7482 bfd_size_type size;
7483 bfd_reloc_status_type rstat;
7484 bfd_byte *buf;
7485 bfd_boolean ok;
7486 const char *sym_name;
7488 size = bfd_get_reloc_size (howto);
7489 buf = bfd_zmalloc (size);
7490 if (buf == NULL)
7491 return FALSE;
7492 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7493 switch (rstat)
7495 case bfd_reloc_ok:
7496 break;
7498 default:
7499 case bfd_reloc_outofrange:
7500 abort ();
7502 case bfd_reloc_overflow:
7503 if (link_order->type == bfd_section_reloc_link_order)
7504 sym_name = bfd_section_name (output_bfd,
7505 link_order->u.reloc.p->u.section);
7506 else
7507 sym_name = link_order->u.reloc.p->u.name;
7508 if (! ((*info->callbacks->reloc_overflow)
7509 (info, NULL, sym_name, howto->name, addend, NULL,
7510 NULL, (bfd_vma) 0)))
7512 free (buf);
7513 return FALSE;
7515 break;
7517 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7518 link_order->offset, size);
7519 free (buf);
7520 if (! ok)
7521 return FALSE;
7524 /* The address of a reloc is relative to the section in a
7525 relocatable file, and is a virtual address in an executable
7526 file. */
7527 offset = link_order->offset;
7528 if (! info->relocatable)
7529 offset += output_section->vma;
7531 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7533 irel[i].r_offset = offset;
7534 irel[i].r_info = 0;
7535 irel[i].r_addend = 0;
7537 if (bed->s->arch_size == 32)
7538 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7539 else
7540 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7542 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7543 erel = rel_hdr->contents;
7544 if (rel_hdr->sh_type == SHT_REL)
7546 erel += (elf_section_data (output_section)->rel_count
7547 * bed->s->sizeof_rel);
7548 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7550 else
7552 irel[0].r_addend = addend;
7553 erel += (elf_section_data (output_section)->rel_count
7554 * bed->s->sizeof_rela);
7555 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7558 ++elf_section_data (output_section)->rel_count;
7560 return TRUE;
7564 /* Get the output vma of the section pointed to by the sh_link field. */
7566 static bfd_vma
7567 elf_get_linked_section_vma (struct bfd_link_order *p)
7569 Elf_Internal_Shdr **elf_shdrp;
7570 asection *s;
7571 int elfsec;
7573 s = p->u.indirect.section;
7574 elf_shdrp = elf_elfsections (s->owner);
7575 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7576 elfsec = elf_shdrp[elfsec]->sh_link;
7577 /* PR 290:
7578 The Intel C compiler generates SHT_IA_64_UNWIND with
7579 SHF_LINK_ORDER. But it doesn't set the sh_link or
7580 sh_info fields. Hence we could get the situation
7581 where elfsec is 0. */
7582 if (elfsec == 0)
7584 const struct elf_backend_data *bed
7585 = get_elf_backend_data (s->owner);
7586 if (bed->link_order_error_handler)
7587 bed->link_order_error_handler
7588 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7589 return 0;
7591 else
7593 s = elf_shdrp[elfsec]->bfd_section;
7594 return s->output_section->vma + s->output_offset;
7599 /* Compare two sections based on the locations of the sections they are
7600 linked to. Used by elf_fixup_link_order. */
7602 static int
7603 compare_link_order (const void * a, const void * b)
7605 bfd_vma apos;
7606 bfd_vma bpos;
7608 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7609 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7610 if (apos < bpos)
7611 return -1;
7612 return apos > bpos;
7616 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7617 order as their linked sections. Returns false if this could not be done
7618 because an output section includes both ordered and unordered
7619 sections. Ideally we'd do this in the linker proper. */
7621 static bfd_boolean
7622 elf_fixup_link_order (bfd *abfd, asection *o)
7624 int seen_linkorder;
7625 int seen_other;
7626 int n;
7627 struct bfd_link_order *p;
7628 bfd *sub;
7629 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7630 int elfsec;
7631 struct bfd_link_order **sections;
7632 asection *s, *other_sec, *linkorder_sec;
7633 bfd_vma offset;
7635 other_sec = NULL;
7636 linkorder_sec = NULL;
7637 seen_other = 0;
7638 seen_linkorder = 0;
7639 for (p = o->map_head.link_order; p != NULL; p = p->next)
7641 if (p->type == bfd_indirect_link_order)
7643 s = p->u.indirect.section;
7644 sub = s->owner;
7645 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7646 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7647 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1
7648 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7650 seen_linkorder++;
7651 linkorder_sec = s;
7653 else
7655 seen_other++;
7656 other_sec = s;
7659 else
7660 seen_other++;
7662 if (seen_other && seen_linkorder)
7664 if (other_sec && linkorder_sec)
7665 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7666 o, linkorder_sec,
7667 linkorder_sec->owner, other_sec,
7668 other_sec->owner);
7669 else
7670 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7672 bfd_set_error (bfd_error_bad_value);
7673 return FALSE;
7677 if (!seen_linkorder)
7678 return TRUE;
7680 sections = (struct bfd_link_order **)
7681 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7682 seen_linkorder = 0;
7684 for (p = o->map_head.link_order; p != NULL; p = p->next)
7686 sections[seen_linkorder++] = p;
7688 /* Sort the input sections in the order of their linked section. */
7689 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7690 compare_link_order);
7692 /* Change the offsets of the sections. */
7693 offset = 0;
7694 for (n = 0; n < seen_linkorder; n++)
7696 s = sections[n]->u.indirect.section;
7697 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7698 s->output_offset = offset;
7699 sections[n]->offset = offset;
7700 offset += sections[n]->size;
7703 return TRUE;
7707 /* Do the final step of an ELF link. */
7709 bfd_boolean
7710 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7712 bfd_boolean dynamic;
7713 bfd_boolean emit_relocs;
7714 bfd *dynobj;
7715 struct elf_final_link_info finfo;
7716 register asection *o;
7717 register struct bfd_link_order *p;
7718 register bfd *sub;
7719 bfd_size_type max_contents_size;
7720 bfd_size_type max_external_reloc_size;
7721 bfd_size_type max_internal_reloc_count;
7722 bfd_size_type max_sym_count;
7723 bfd_size_type max_sym_shndx_count;
7724 file_ptr off;
7725 Elf_Internal_Sym elfsym;
7726 unsigned int i;
7727 Elf_Internal_Shdr *symtab_hdr;
7728 Elf_Internal_Shdr *symtab_shndx_hdr;
7729 Elf_Internal_Shdr *symstrtab_hdr;
7730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7731 struct elf_outext_info eoinfo;
7732 bfd_boolean merged;
7733 size_t relativecount = 0;
7734 asection *reldyn = 0;
7735 bfd_size_type amt;
7737 if (! is_elf_hash_table (info->hash))
7738 return FALSE;
7740 if (info->shared)
7741 abfd->flags |= DYNAMIC;
7743 dynamic = elf_hash_table (info)->dynamic_sections_created;
7744 dynobj = elf_hash_table (info)->dynobj;
7746 emit_relocs = (info->relocatable
7747 || info->emitrelocations
7748 || bed->elf_backend_emit_relocs);
7750 finfo.info = info;
7751 finfo.output_bfd = abfd;
7752 finfo.symstrtab = _bfd_elf_stringtab_init ();
7753 if (finfo.symstrtab == NULL)
7754 return FALSE;
7756 if (! dynamic)
7758 finfo.dynsym_sec = NULL;
7759 finfo.hash_sec = NULL;
7760 finfo.symver_sec = NULL;
7762 else
7764 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7765 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7766 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7767 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7768 /* Note that it is OK if symver_sec is NULL. */
7771 finfo.contents = NULL;
7772 finfo.external_relocs = NULL;
7773 finfo.internal_relocs = NULL;
7774 finfo.external_syms = NULL;
7775 finfo.locsym_shndx = NULL;
7776 finfo.internal_syms = NULL;
7777 finfo.indices = NULL;
7778 finfo.sections = NULL;
7779 finfo.symbuf = NULL;
7780 finfo.symshndxbuf = NULL;
7781 finfo.symbuf_count = 0;
7782 finfo.shndxbuf_size = 0;
7784 /* Count up the number of relocations we will output for each output
7785 section, so that we know the sizes of the reloc sections. We
7786 also figure out some maximum sizes. */
7787 max_contents_size = 0;
7788 max_external_reloc_size = 0;
7789 max_internal_reloc_count = 0;
7790 max_sym_count = 0;
7791 max_sym_shndx_count = 0;
7792 merged = FALSE;
7793 for (o = abfd->sections; o != NULL; o = o->next)
7795 struct bfd_elf_section_data *esdo = elf_section_data (o);
7796 o->reloc_count = 0;
7798 for (p = o->map_head.link_order; p != NULL; p = p->next)
7800 unsigned int reloc_count = 0;
7801 struct bfd_elf_section_data *esdi = NULL;
7802 unsigned int *rel_count1;
7804 if (p->type == bfd_section_reloc_link_order
7805 || p->type == bfd_symbol_reloc_link_order)
7806 reloc_count = 1;
7807 else if (p->type == bfd_indirect_link_order)
7809 asection *sec;
7811 sec = p->u.indirect.section;
7812 esdi = elf_section_data (sec);
7814 /* Mark all sections which are to be included in the
7815 link. This will normally be every section. We need
7816 to do this so that we can identify any sections which
7817 the linker has decided to not include. */
7818 sec->linker_mark = TRUE;
7820 if (sec->flags & SEC_MERGE)
7821 merged = TRUE;
7823 if (info->relocatable || info->emitrelocations)
7824 reloc_count = sec->reloc_count;
7825 else if (bed->elf_backend_count_relocs)
7827 Elf_Internal_Rela * relocs;
7829 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7830 info->keep_memory);
7832 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7834 if (elf_section_data (o)->relocs != relocs)
7835 free (relocs);
7838 if (sec->rawsize > max_contents_size)
7839 max_contents_size = sec->rawsize;
7840 if (sec->size > max_contents_size)
7841 max_contents_size = sec->size;
7843 /* We are interested in just local symbols, not all
7844 symbols. */
7845 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7846 && (sec->owner->flags & DYNAMIC) == 0)
7848 size_t sym_count;
7850 if (elf_bad_symtab (sec->owner))
7851 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7852 / bed->s->sizeof_sym);
7853 else
7854 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7856 if (sym_count > max_sym_count)
7857 max_sym_count = sym_count;
7859 if (sym_count > max_sym_shndx_count
7860 && elf_symtab_shndx (sec->owner) != 0)
7861 max_sym_shndx_count = sym_count;
7863 if ((sec->flags & SEC_RELOC) != 0)
7865 size_t ext_size;
7867 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7868 if (ext_size > max_external_reloc_size)
7869 max_external_reloc_size = ext_size;
7870 if (sec->reloc_count > max_internal_reloc_count)
7871 max_internal_reloc_count = sec->reloc_count;
7876 if (reloc_count == 0)
7877 continue;
7879 o->reloc_count += reloc_count;
7881 /* MIPS may have a mix of REL and RELA relocs on sections.
7882 To support this curious ABI we keep reloc counts in
7883 elf_section_data too. We must be careful to add the
7884 relocations from the input section to the right output
7885 count. FIXME: Get rid of one count. We have
7886 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7887 rel_count1 = &esdo->rel_count;
7888 if (esdi != NULL)
7890 bfd_boolean same_size;
7891 bfd_size_type entsize1;
7893 entsize1 = esdi->rel_hdr.sh_entsize;
7894 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7895 || entsize1 == bed->s->sizeof_rela);
7896 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7898 if (!same_size)
7899 rel_count1 = &esdo->rel_count2;
7901 if (esdi->rel_hdr2 != NULL)
7903 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7904 unsigned int alt_count;
7905 unsigned int *rel_count2;
7907 BFD_ASSERT (entsize2 != entsize1
7908 && (entsize2 == bed->s->sizeof_rel
7909 || entsize2 == bed->s->sizeof_rela));
7911 rel_count2 = &esdo->rel_count2;
7912 if (!same_size)
7913 rel_count2 = &esdo->rel_count;
7915 /* The following is probably too simplistic if the
7916 backend counts output relocs unusually. */
7917 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7918 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7919 *rel_count2 += alt_count;
7920 reloc_count -= alt_count;
7923 *rel_count1 += reloc_count;
7926 if (o->reloc_count > 0)
7927 o->flags |= SEC_RELOC;
7928 else
7930 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7931 set it (this is probably a bug) and if it is set
7932 assign_section_numbers will create a reloc section. */
7933 o->flags &=~ SEC_RELOC;
7936 /* If the SEC_ALLOC flag is not set, force the section VMA to
7937 zero. This is done in elf_fake_sections as well, but forcing
7938 the VMA to 0 here will ensure that relocs against these
7939 sections are handled correctly. */
7940 if ((o->flags & SEC_ALLOC) == 0
7941 && ! o->user_set_vma)
7942 o->vma = 0;
7945 if (! info->relocatable && merged)
7946 elf_link_hash_traverse (elf_hash_table (info),
7947 _bfd_elf_link_sec_merge_syms, abfd);
7949 /* Figure out the file positions for everything but the symbol table
7950 and the relocs. We set symcount to force assign_section_numbers
7951 to create a symbol table. */
7952 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7953 BFD_ASSERT (! abfd->output_has_begun);
7954 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7955 goto error_return;
7957 /* Set sizes, and assign file positions for reloc sections. */
7958 for (o = abfd->sections; o != NULL; o = o->next)
7960 if ((o->flags & SEC_RELOC) != 0)
7962 if (!(_bfd_elf_link_size_reloc_section
7963 (abfd, &elf_section_data (o)->rel_hdr, o)))
7964 goto error_return;
7966 if (elf_section_data (o)->rel_hdr2
7967 && !(_bfd_elf_link_size_reloc_section
7968 (abfd, elf_section_data (o)->rel_hdr2, o)))
7969 goto error_return;
7972 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7973 to count upwards while actually outputting the relocations. */
7974 elf_section_data (o)->rel_count = 0;
7975 elf_section_data (o)->rel_count2 = 0;
7978 _bfd_elf_assign_file_positions_for_relocs (abfd);
7980 /* We have now assigned file positions for all the sections except
7981 .symtab and .strtab. We start the .symtab section at the current
7982 file position, and write directly to it. We build the .strtab
7983 section in memory. */
7984 bfd_get_symcount (abfd) = 0;
7985 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7986 /* sh_name is set in prep_headers. */
7987 symtab_hdr->sh_type = SHT_SYMTAB;
7988 /* sh_flags, sh_addr and sh_size all start off zero. */
7989 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7990 /* sh_link is set in assign_section_numbers. */
7991 /* sh_info is set below. */
7992 /* sh_offset is set just below. */
7993 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7995 off = elf_tdata (abfd)->next_file_pos;
7996 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7998 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7999 incorrect. We do not yet know the size of the .symtab section.
8000 We correct next_file_pos below, after we do know the size. */
8002 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8003 continuously seeking to the right position in the file. */
8004 if (! info->keep_memory || max_sym_count < 20)
8005 finfo.symbuf_size = 20;
8006 else
8007 finfo.symbuf_size = max_sym_count;
8008 amt = finfo.symbuf_size;
8009 amt *= bed->s->sizeof_sym;
8010 finfo.symbuf = bfd_malloc (amt);
8011 if (finfo.symbuf == NULL)
8012 goto error_return;
8013 if (elf_numsections (abfd) > SHN_LORESERVE)
8015 /* Wild guess at number of output symbols. realloc'd as needed. */
8016 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8017 finfo.shndxbuf_size = amt;
8018 amt *= sizeof (Elf_External_Sym_Shndx);
8019 finfo.symshndxbuf = bfd_zmalloc (amt);
8020 if (finfo.symshndxbuf == NULL)
8021 goto error_return;
8024 /* Start writing out the symbol table. The first symbol is always a
8025 dummy symbol. */
8026 if (info->strip != strip_all
8027 || emit_relocs)
8029 elfsym.st_value = 0;
8030 elfsym.st_size = 0;
8031 elfsym.st_info = 0;
8032 elfsym.st_other = 0;
8033 elfsym.st_shndx = SHN_UNDEF;
8034 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8035 NULL))
8036 goto error_return;
8039 /* Output a symbol for each section. We output these even if we are
8040 discarding local symbols, since they are used for relocs. These
8041 symbols have no names. We store the index of each one in the
8042 index field of the section, so that we can find it again when
8043 outputting relocs. */
8044 if (info->strip != strip_all
8045 || emit_relocs)
8047 elfsym.st_size = 0;
8048 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8049 elfsym.st_other = 0;
8050 for (i = 1; i < elf_numsections (abfd); i++)
8052 o = bfd_section_from_elf_index (abfd, i);
8053 if (o != NULL)
8054 o->target_index = bfd_get_symcount (abfd);
8055 elfsym.st_shndx = i;
8056 if (info->relocatable || o == NULL)
8057 elfsym.st_value = 0;
8058 else
8059 elfsym.st_value = o->vma;
8060 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8061 goto error_return;
8062 if (i == SHN_LORESERVE - 1)
8063 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8067 /* Allocate some memory to hold information read in from the input
8068 files. */
8069 if (max_contents_size != 0)
8071 finfo.contents = bfd_malloc (max_contents_size);
8072 if (finfo.contents == NULL)
8073 goto error_return;
8076 if (max_external_reloc_size != 0)
8078 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8079 if (finfo.external_relocs == NULL)
8080 goto error_return;
8083 if (max_internal_reloc_count != 0)
8085 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8086 amt *= sizeof (Elf_Internal_Rela);
8087 finfo.internal_relocs = bfd_malloc (amt);
8088 if (finfo.internal_relocs == NULL)
8089 goto error_return;
8092 if (max_sym_count != 0)
8094 amt = max_sym_count * bed->s->sizeof_sym;
8095 finfo.external_syms = bfd_malloc (amt);
8096 if (finfo.external_syms == NULL)
8097 goto error_return;
8099 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8100 finfo.internal_syms = bfd_malloc (amt);
8101 if (finfo.internal_syms == NULL)
8102 goto error_return;
8104 amt = max_sym_count * sizeof (long);
8105 finfo.indices = bfd_malloc (amt);
8106 if (finfo.indices == NULL)
8107 goto error_return;
8109 amt = max_sym_count * sizeof (asection *);
8110 finfo.sections = bfd_malloc (amt);
8111 if (finfo.sections == NULL)
8112 goto error_return;
8115 if (max_sym_shndx_count != 0)
8117 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8118 finfo.locsym_shndx = bfd_malloc (amt);
8119 if (finfo.locsym_shndx == NULL)
8120 goto error_return;
8123 if (elf_hash_table (info)->tls_sec)
8125 bfd_vma base, end = 0;
8126 asection *sec;
8128 for (sec = elf_hash_table (info)->tls_sec;
8129 sec && (sec->flags & SEC_THREAD_LOCAL);
8130 sec = sec->next)
8132 bfd_size_type size = sec->size;
8134 if (size == 0
8135 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8137 struct bfd_link_order *o = sec->map_tail.link_order;
8138 if (o != NULL)
8139 size = o->offset + o->size;
8141 end = sec->vma + size;
8143 base = elf_hash_table (info)->tls_sec->vma;
8144 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8145 elf_hash_table (info)->tls_size = end - base;
8148 /* Reorder SHF_LINK_ORDER sections. */
8149 for (o = abfd->sections; o != NULL; o = o->next)
8151 if (!elf_fixup_link_order (abfd, o))
8152 return FALSE;
8155 /* Since ELF permits relocations to be against local symbols, we
8156 must have the local symbols available when we do the relocations.
8157 Since we would rather only read the local symbols once, and we
8158 would rather not keep them in memory, we handle all the
8159 relocations for a single input file at the same time.
8161 Unfortunately, there is no way to know the total number of local
8162 symbols until we have seen all of them, and the local symbol
8163 indices precede the global symbol indices. This means that when
8164 we are generating relocatable output, and we see a reloc against
8165 a global symbol, we can not know the symbol index until we have
8166 finished examining all the local symbols to see which ones we are
8167 going to output. To deal with this, we keep the relocations in
8168 memory, and don't output them until the end of the link. This is
8169 an unfortunate waste of memory, but I don't see a good way around
8170 it. Fortunately, it only happens when performing a relocatable
8171 link, which is not the common case. FIXME: If keep_memory is set
8172 we could write the relocs out and then read them again; I don't
8173 know how bad the memory loss will be. */
8175 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8176 sub->output_has_begun = FALSE;
8177 for (o = abfd->sections; o != NULL; o = o->next)
8179 for (p = o->map_head.link_order; p != NULL; p = p->next)
8181 if (p->type == bfd_indirect_link_order
8182 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8183 == bfd_target_elf_flavour)
8184 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8186 if (! sub->output_has_begun)
8188 if (! elf_link_input_bfd (&finfo, sub))
8189 goto error_return;
8190 sub->output_has_begun = TRUE;
8193 else if (p->type == bfd_section_reloc_link_order
8194 || p->type == bfd_symbol_reloc_link_order)
8196 if (! elf_reloc_link_order (abfd, info, o, p))
8197 goto error_return;
8199 else
8201 if (! _bfd_default_link_order (abfd, info, o, p))
8202 goto error_return;
8207 /* Output any global symbols that got converted to local in a
8208 version script or due to symbol visibility. We do this in a
8209 separate step since ELF requires all local symbols to appear
8210 prior to any global symbols. FIXME: We should only do this if
8211 some global symbols were, in fact, converted to become local.
8212 FIXME: Will this work correctly with the Irix 5 linker? */
8213 eoinfo.failed = FALSE;
8214 eoinfo.finfo = &finfo;
8215 eoinfo.localsyms = TRUE;
8216 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8217 &eoinfo);
8218 if (eoinfo.failed)
8219 return FALSE;
8221 /* That wrote out all the local symbols. Finish up the symbol table
8222 with the global symbols. Even if we want to strip everything we
8223 can, we still need to deal with those global symbols that got
8224 converted to local in a version script. */
8226 /* The sh_info field records the index of the first non local symbol. */
8227 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8229 if (dynamic
8230 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8232 Elf_Internal_Sym sym;
8233 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8234 long last_local = 0;
8236 /* Write out the section symbols for the output sections. */
8237 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8239 asection *s;
8241 sym.st_size = 0;
8242 sym.st_name = 0;
8243 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8244 sym.st_other = 0;
8246 for (s = abfd->sections; s != NULL; s = s->next)
8248 int indx;
8249 bfd_byte *dest;
8250 long dynindx;
8252 dynindx = elf_section_data (s)->dynindx;
8253 if (dynindx <= 0)
8254 continue;
8255 indx = elf_section_data (s)->this_idx;
8256 BFD_ASSERT (indx > 0);
8257 sym.st_shndx = indx;
8258 sym.st_value = s->vma;
8259 dest = dynsym + dynindx * bed->s->sizeof_sym;
8260 if (last_local < dynindx)
8261 last_local = dynindx;
8262 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8266 /* Write out the local dynsyms. */
8267 if (elf_hash_table (info)->dynlocal)
8269 struct elf_link_local_dynamic_entry *e;
8270 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8272 asection *s;
8273 bfd_byte *dest;
8275 sym.st_size = e->isym.st_size;
8276 sym.st_other = e->isym.st_other;
8278 /* Copy the internal symbol as is.
8279 Note that we saved a word of storage and overwrote
8280 the original st_name with the dynstr_index. */
8281 sym = e->isym;
8283 if (e->isym.st_shndx != SHN_UNDEF
8284 && (e->isym.st_shndx < SHN_LORESERVE
8285 || e->isym.st_shndx > SHN_HIRESERVE))
8287 s = bfd_section_from_elf_index (e->input_bfd,
8288 e->isym.st_shndx);
8290 sym.st_shndx =
8291 elf_section_data (s->output_section)->this_idx;
8292 sym.st_value = (s->output_section->vma
8293 + s->output_offset
8294 + e->isym.st_value);
8297 if (last_local < e->dynindx)
8298 last_local = e->dynindx;
8300 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8301 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8305 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8306 last_local + 1;
8309 /* We get the global symbols from the hash table. */
8310 eoinfo.failed = FALSE;
8311 eoinfo.localsyms = FALSE;
8312 eoinfo.finfo = &finfo;
8313 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8314 &eoinfo);
8315 if (eoinfo.failed)
8316 return FALSE;
8318 /* If backend needs to output some symbols not present in the hash
8319 table, do it now. */
8320 if (bed->elf_backend_output_arch_syms)
8322 typedef bfd_boolean (*out_sym_func)
8323 (void *, const char *, Elf_Internal_Sym *, asection *,
8324 struct elf_link_hash_entry *);
8326 if (! ((*bed->elf_backend_output_arch_syms)
8327 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8328 return FALSE;
8331 /* Flush all symbols to the file. */
8332 if (! elf_link_flush_output_syms (&finfo, bed))
8333 return FALSE;
8335 /* Now we know the size of the symtab section. */
8336 off += symtab_hdr->sh_size;
8338 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8339 if (symtab_shndx_hdr->sh_name != 0)
8341 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8342 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8343 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8344 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8345 symtab_shndx_hdr->sh_size = amt;
8347 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8348 off, TRUE);
8350 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8351 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8352 return FALSE;
8356 /* Finish up and write out the symbol string table (.strtab)
8357 section. */
8358 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8359 /* sh_name was set in prep_headers. */
8360 symstrtab_hdr->sh_type = SHT_STRTAB;
8361 symstrtab_hdr->sh_flags = 0;
8362 symstrtab_hdr->sh_addr = 0;
8363 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8364 symstrtab_hdr->sh_entsize = 0;
8365 symstrtab_hdr->sh_link = 0;
8366 symstrtab_hdr->sh_info = 0;
8367 /* sh_offset is set just below. */
8368 symstrtab_hdr->sh_addralign = 1;
8370 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8371 elf_tdata (abfd)->next_file_pos = off;
8373 if (bfd_get_symcount (abfd) > 0)
8375 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8376 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8377 return FALSE;
8380 /* Adjust the relocs to have the correct symbol indices. */
8381 for (o = abfd->sections; o != NULL; o = o->next)
8383 if ((o->flags & SEC_RELOC) == 0)
8384 continue;
8386 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8387 elf_section_data (o)->rel_count,
8388 elf_section_data (o)->rel_hashes);
8389 if (elf_section_data (o)->rel_hdr2 != NULL)
8390 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8391 elf_section_data (o)->rel_count2,
8392 (elf_section_data (o)->rel_hashes
8393 + elf_section_data (o)->rel_count));
8395 /* Set the reloc_count field to 0 to prevent write_relocs from
8396 trying to swap the relocs out itself. */
8397 o->reloc_count = 0;
8400 if (dynamic && info->combreloc && dynobj != NULL)
8401 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8403 /* If we are linking against a dynamic object, or generating a
8404 shared library, finish up the dynamic linking information. */
8405 if (dynamic)
8407 bfd_byte *dyncon, *dynconend;
8409 /* Fix up .dynamic entries. */
8410 o = bfd_get_section_by_name (dynobj, ".dynamic");
8411 BFD_ASSERT (o != NULL);
8413 dyncon = o->contents;
8414 dynconend = o->contents + o->size;
8415 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8417 Elf_Internal_Dyn dyn;
8418 const char *name;
8419 unsigned int type;
8421 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8423 switch (dyn.d_tag)
8425 default:
8426 continue;
8427 case DT_NULL:
8428 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8430 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8432 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8433 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8434 default: continue;
8436 dyn.d_un.d_val = relativecount;
8437 relativecount = 0;
8438 break;
8440 continue;
8442 case DT_INIT:
8443 name = info->init_function;
8444 goto get_sym;
8445 case DT_FINI:
8446 name = info->fini_function;
8447 get_sym:
8449 struct elf_link_hash_entry *h;
8451 h = elf_link_hash_lookup (elf_hash_table (info), name,
8452 FALSE, FALSE, TRUE);
8453 if (h != NULL
8454 && (h->root.type == bfd_link_hash_defined
8455 || h->root.type == bfd_link_hash_defweak))
8457 dyn.d_un.d_val = h->root.u.def.value;
8458 o = h->root.u.def.section;
8459 if (o->output_section != NULL)
8460 dyn.d_un.d_val += (o->output_section->vma
8461 + o->output_offset);
8462 else
8464 /* The symbol is imported from another shared
8465 library and does not apply to this one. */
8466 dyn.d_un.d_val = 0;
8468 break;
8471 continue;
8473 case DT_PREINIT_ARRAYSZ:
8474 name = ".preinit_array";
8475 goto get_size;
8476 case DT_INIT_ARRAYSZ:
8477 name = ".init_array";
8478 goto get_size;
8479 case DT_FINI_ARRAYSZ:
8480 name = ".fini_array";
8481 get_size:
8482 o = bfd_get_section_by_name (abfd, name);
8483 if (o == NULL)
8485 (*_bfd_error_handler)
8486 (_("%B: could not find output section %s"), abfd, name);
8487 goto error_return;
8489 if (o->size == 0)
8490 (*_bfd_error_handler)
8491 (_("warning: %s section has zero size"), name);
8492 dyn.d_un.d_val = o->size;
8493 break;
8495 case DT_PREINIT_ARRAY:
8496 name = ".preinit_array";
8497 goto get_vma;
8498 case DT_INIT_ARRAY:
8499 name = ".init_array";
8500 goto get_vma;
8501 case DT_FINI_ARRAY:
8502 name = ".fini_array";
8503 goto get_vma;
8505 case DT_HASH:
8506 name = ".hash";
8507 goto get_vma;
8508 case DT_STRTAB:
8509 name = ".dynstr";
8510 goto get_vma;
8511 case DT_SYMTAB:
8512 name = ".dynsym";
8513 goto get_vma;
8514 case DT_VERDEF:
8515 name = ".gnu.version_d";
8516 goto get_vma;
8517 case DT_VERNEED:
8518 name = ".gnu.version_r";
8519 goto get_vma;
8520 case DT_VERSYM:
8521 name = ".gnu.version";
8522 get_vma:
8523 o = bfd_get_section_by_name (abfd, name);
8524 if (o == NULL)
8526 (*_bfd_error_handler)
8527 (_("%B: could not find output section %s"), abfd, name);
8528 goto error_return;
8530 dyn.d_un.d_ptr = o->vma;
8531 break;
8533 case DT_REL:
8534 case DT_RELA:
8535 case DT_RELSZ:
8536 case DT_RELASZ:
8537 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8538 type = SHT_REL;
8539 else
8540 type = SHT_RELA;
8541 dyn.d_un.d_val = 0;
8542 for (i = 1; i < elf_numsections (abfd); i++)
8544 Elf_Internal_Shdr *hdr;
8546 hdr = elf_elfsections (abfd)[i];
8547 if (hdr->sh_type == type
8548 && (hdr->sh_flags & SHF_ALLOC) != 0)
8550 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8551 dyn.d_un.d_val += hdr->sh_size;
8552 else
8554 if (dyn.d_un.d_val == 0
8555 || hdr->sh_addr < dyn.d_un.d_val)
8556 dyn.d_un.d_val = hdr->sh_addr;
8560 break;
8562 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8566 /* If we have created any dynamic sections, then output them. */
8567 if (dynobj != NULL)
8569 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8570 goto error_return;
8572 for (o = dynobj->sections; o != NULL; o = o->next)
8574 if ((o->flags & SEC_HAS_CONTENTS) == 0
8575 || o->size == 0
8576 || o->output_section == bfd_abs_section_ptr)
8577 continue;
8578 if ((o->flags & SEC_LINKER_CREATED) == 0)
8580 /* At this point, we are only interested in sections
8581 created by _bfd_elf_link_create_dynamic_sections. */
8582 continue;
8584 if (elf_hash_table (info)->stab_info.stabstr == o)
8585 continue;
8586 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8587 continue;
8588 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8589 != SHT_STRTAB)
8590 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8592 if (! bfd_set_section_contents (abfd, o->output_section,
8593 o->contents,
8594 (file_ptr) o->output_offset,
8595 o->size))
8596 goto error_return;
8598 else
8600 /* The contents of the .dynstr section are actually in a
8601 stringtab. */
8602 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8603 if (bfd_seek (abfd, off, SEEK_SET) != 0
8604 || ! _bfd_elf_strtab_emit (abfd,
8605 elf_hash_table (info)->dynstr))
8606 goto error_return;
8611 if (info->relocatable)
8613 bfd_boolean failed = FALSE;
8615 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8616 if (failed)
8617 goto error_return;
8620 /* If we have optimized stabs strings, output them. */
8621 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8623 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8624 goto error_return;
8627 if (info->eh_frame_hdr)
8629 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8630 goto error_return;
8633 if (finfo.symstrtab != NULL)
8634 _bfd_stringtab_free (finfo.symstrtab);
8635 if (finfo.contents != NULL)
8636 free (finfo.contents);
8637 if (finfo.external_relocs != NULL)
8638 free (finfo.external_relocs);
8639 if (finfo.internal_relocs != NULL)
8640 free (finfo.internal_relocs);
8641 if (finfo.external_syms != NULL)
8642 free (finfo.external_syms);
8643 if (finfo.locsym_shndx != NULL)
8644 free (finfo.locsym_shndx);
8645 if (finfo.internal_syms != NULL)
8646 free (finfo.internal_syms);
8647 if (finfo.indices != NULL)
8648 free (finfo.indices);
8649 if (finfo.sections != NULL)
8650 free (finfo.sections);
8651 if (finfo.symbuf != NULL)
8652 free (finfo.symbuf);
8653 if (finfo.symshndxbuf != NULL)
8654 free (finfo.symshndxbuf);
8655 for (o = abfd->sections; o != NULL; o = o->next)
8657 if ((o->flags & SEC_RELOC) != 0
8658 && elf_section_data (o)->rel_hashes != NULL)
8659 free (elf_section_data (o)->rel_hashes);
8662 elf_tdata (abfd)->linker = TRUE;
8664 return TRUE;
8666 error_return:
8667 if (finfo.symstrtab != NULL)
8668 _bfd_stringtab_free (finfo.symstrtab);
8669 if (finfo.contents != NULL)
8670 free (finfo.contents);
8671 if (finfo.external_relocs != NULL)
8672 free (finfo.external_relocs);
8673 if (finfo.internal_relocs != NULL)
8674 free (finfo.internal_relocs);
8675 if (finfo.external_syms != NULL)
8676 free (finfo.external_syms);
8677 if (finfo.locsym_shndx != NULL)
8678 free (finfo.locsym_shndx);
8679 if (finfo.internal_syms != NULL)
8680 free (finfo.internal_syms);
8681 if (finfo.indices != NULL)
8682 free (finfo.indices);
8683 if (finfo.sections != NULL)
8684 free (finfo.sections);
8685 if (finfo.symbuf != NULL)
8686 free (finfo.symbuf);
8687 if (finfo.symshndxbuf != NULL)
8688 free (finfo.symshndxbuf);
8689 for (o = abfd->sections; o != NULL; o = o->next)
8691 if ((o->flags & SEC_RELOC) != 0
8692 && elf_section_data (o)->rel_hashes != NULL)
8693 free (elf_section_data (o)->rel_hashes);
8696 return FALSE;
8699 /* Garbage collect unused sections. */
8701 /* The mark phase of garbage collection. For a given section, mark
8702 it and any sections in this section's group, and all the sections
8703 which define symbols to which it refers. */
8705 typedef asection * (*gc_mark_hook_fn)
8706 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8707 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8709 bfd_boolean
8710 _bfd_elf_gc_mark (struct bfd_link_info *info,
8711 asection *sec,
8712 gc_mark_hook_fn gc_mark_hook)
8714 bfd_boolean ret;
8715 bfd_boolean is_eh;
8716 asection *group_sec;
8718 sec->gc_mark = 1;
8720 /* Mark all the sections in the group. */
8721 group_sec = elf_section_data (sec)->next_in_group;
8722 if (group_sec && !group_sec->gc_mark)
8723 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8724 return FALSE;
8726 /* Look through the section relocs. */
8727 ret = TRUE;
8728 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8729 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8731 Elf_Internal_Rela *relstart, *rel, *relend;
8732 Elf_Internal_Shdr *symtab_hdr;
8733 struct elf_link_hash_entry **sym_hashes;
8734 size_t nlocsyms;
8735 size_t extsymoff;
8736 bfd *input_bfd = sec->owner;
8737 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8738 Elf_Internal_Sym *isym = NULL;
8739 int r_sym_shift;
8741 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8742 sym_hashes = elf_sym_hashes (input_bfd);
8744 /* Read the local symbols. */
8745 if (elf_bad_symtab (input_bfd))
8747 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8748 extsymoff = 0;
8750 else
8751 extsymoff = nlocsyms = symtab_hdr->sh_info;
8753 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8754 if (isym == NULL && nlocsyms != 0)
8756 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8757 NULL, NULL, NULL);
8758 if (isym == NULL)
8759 return FALSE;
8762 /* Read the relocations. */
8763 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8764 info->keep_memory);
8765 if (relstart == NULL)
8767 ret = FALSE;
8768 goto out1;
8770 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8772 if (bed->s->arch_size == 32)
8773 r_sym_shift = 8;
8774 else
8775 r_sym_shift = 32;
8777 for (rel = relstart; rel < relend; rel++)
8779 unsigned long r_symndx;
8780 asection *rsec;
8781 struct elf_link_hash_entry *h;
8783 r_symndx = rel->r_info >> r_sym_shift;
8784 if (r_symndx == 0)
8785 continue;
8787 if (r_symndx >= nlocsyms
8788 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8790 h = sym_hashes[r_symndx - extsymoff];
8791 while (h->root.type == bfd_link_hash_indirect
8792 || h->root.type == bfd_link_hash_warning)
8793 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8794 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8796 else
8798 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8801 if (rsec && !rsec->gc_mark)
8803 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8804 rsec->gc_mark = 1;
8805 else if (is_eh)
8806 rsec->gc_mark_from_eh = 1;
8807 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8809 ret = FALSE;
8810 goto out2;
8815 out2:
8816 if (elf_section_data (sec)->relocs != relstart)
8817 free (relstart);
8818 out1:
8819 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8821 if (! info->keep_memory)
8822 free (isym);
8823 else
8824 symtab_hdr->contents = (unsigned char *) isym;
8828 return ret;
8831 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8833 struct elf_gc_sweep_symbol_info {
8834 struct bfd_link_info *info;
8835 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8836 bfd_boolean);
8839 static bfd_boolean
8840 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8842 if (h->root.type == bfd_link_hash_warning)
8843 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8845 if ((h->root.type == bfd_link_hash_defined
8846 || h->root.type == bfd_link_hash_defweak)
8847 && !h->root.u.def.section->gc_mark
8848 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8850 struct elf_gc_sweep_symbol_info *inf = data;
8851 (*inf->hide_symbol) (inf->info, h, TRUE);
8854 return TRUE;
8857 /* The sweep phase of garbage collection. Remove all garbage sections. */
8859 typedef bfd_boolean (*gc_sweep_hook_fn)
8860 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8862 static bfd_boolean
8863 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8865 bfd *sub;
8866 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8867 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8868 unsigned long section_sym_count;
8869 struct elf_gc_sweep_symbol_info sweep_info;
8871 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8873 asection *o;
8875 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8876 continue;
8878 for (o = sub->sections; o != NULL; o = o->next)
8880 /* Keep debug and special sections. */
8881 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8882 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8883 o->gc_mark = 1;
8885 if (o->gc_mark)
8886 continue;
8888 /* Skip sweeping sections already excluded. */
8889 if (o->flags & SEC_EXCLUDE)
8890 continue;
8892 /* Since this is early in the link process, it is simple
8893 to remove a section from the output. */
8894 o->flags |= SEC_EXCLUDE;
8896 /* But we also have to update some of the relocation
8897 info we collected before. */
8898 if (gc_sweep_hook
8899 && (o->flags & SEC_RELOC) != 0
8900 && o->reloc_count > 0
8901 && !bfd_is_abs_section (o->output_section))
8903 Elf_Internal_Rela *internal_relocs;
8904 bfd_boolean r;
8906 internal_relocs
8907 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8908 info->keep_memory);
8909 if (internal_relocs == NULL)
8910 return FALSE;
8912 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8914 if (elf_section_data (o)->relocs != internal_relocs)
8915 free (internal_relocs);
8917 if (!r)
8918 return FALSE;
8923 /* Remove the symbols that were in the swept sections from the dynamic
8924 symbol table. GCFIXME: Anyone know how to get them out of the
8925 static symbol table as well? */
8926 sweep_info.info = info;
8927 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8928 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8929 &sweep_info);
8931 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
8932 return TRUE;
8935 /* Propagate collected vtable information. This is called through
8936 elf_link_hash_traverse. */
8938 static bfd_boolean
8939 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8941 if (h->root.type == bfd_link_hash_warning)
8942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8944 /* Those that are not vtables. */
8945 if (h->vtable == NULL || h->vtable->parent == NULL)
8946 return TRUE;
8948 /* Those vtables that do not have parents, we cannot merge. */
8949 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8950 return TRUE;
8952 /* If we've already been done, exit. */
8953 if (h->vtable->used && h->vtable->used[-1])
8954 return TRUE;
8956 /* Make sure the parent's table is up to date. */
8957 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8959 if (h->vtable->used == NULL)
8961 /* None of this table's entries were referenced. Re-use the
8962 parent's table. */
8963 h->vtable->used = h->vtable->parent->vtable->used;
8964 h->vtable->size = h->vtable->parent->vtable->size;
8966 else
8968 size_t n;
8969 bfd_boolean *cu, *pu;
8971 /* Or the parent's entries into ours. */
8972 cu = h->vtable->used;
8973 cu[-1] = TRUE;
8974 pu = h->vtable->parent->vtable->used;
8975 if (pu != NULL)
8977 const struct elf_backend_data *bed;
8978 unsigned int log_file_align;
8980 bed = get_elf_backend_data (h->root.u.def.section->owner);
8981 log_file_align = bed->s->log_file_align;
8982 n = h->vtable->parent->vtable->size >> log_file_align;
8983 while (n--)
8985 if (*pu)
8986 *cu = TRUE;
8987 pu++;
8988 cu++;
8993 return TRUE;
8996 static bfd_boolean
8997 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8999 asection *sec;
9000 bfd_vma hstart, hend;
9001 Elf_Internal_Rela *relstart, *relend, *rel;
9002 const struct elf_backend_data *bed;
9003 unsigned int log_file_align;
9005 if (h->root.type == bfd_link_hash_warning)
9006 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9008 /* Take care of both those symbols that do not describe vtables as
9009 well as those that are not loaded. */
9010 if (h->vtable == NULL || h->vtable->parent == NULL)
9011 return TRUE;
9013 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9014 || h->root.type == bfd_link_hash_defweak);
9016 sec = h->root.u.def.section;
9017 hstart = h->root.u.def.value;
9018 hend = hstart + h->size;
9020 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9021 if (!relstart)
9022 return *(bfd_boolean *) okp = FALSE;
9023 bed = get_elf_backend_data (sec->owner);
9024 log_file_align = bed->s->log_file_align;
9026 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9028 for (rel = relstart; rel < relend; ++rel)
9029 if (rel->r_offset >= hstart && rel->r_offset < hend)
9031 /* If the entry is in use, do nothing. */
9032 if (h->vtable->used
9033 && (rel->r_offset - hstart) < h->vtable->size)
9035 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9036 if (h->vtable->used[entry])
9037 continue;
9039 /* Otherwise, kill it. */
9040 rel->r_offset = rel->r_info = rel->r_addend = 0;
9043 return TRUE;
9046 /* Mark sections containing dynamically referenced symbols. When
9047 building shared libraries, we must assume that any visible symbol is
9048 referenced. */
9050 static bfd_boolean
9051 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9053 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9055 if (h->root.type == bfd_link_hash_warning)
9056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9058 if ((h->root.type == bfd_link_hash_defined
9059 || h->root.type == bfd_link_hash_defweak)
9060 && (h->ref_dynamic
9061 || (info->shared
9062 && h->def_regular
9063 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9064 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9065 h->root.u.def.section->flags |= SEC_KEEP;
9067 return TRUE;
9070 /* Do mark and sweep of unused sections. */
9072 bfd_boolean
9073 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9075 bfd_boolean ok = TRUE;
9076 bfd *sub;
9077 asection * (*gc_mark_hook)
9078 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9079 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9081 if (!get_elf_backend_data (abfd)->can_gc_sections
9082 || info->relocatable
9083 || info->emitrelocations
9084 || !is_elf_hash_table (info->hash))
9086 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9087 return TRUE;
9090 /* Apply transitive closure to the vtable entry usage info. */
9091 elf_link_hash_traverse (elf_hash_table (info),
9092 elf_gc_propagate_vtable_entries_used,
9093 &ok);
9094 if (!ok)
9095 return FALSE;
9097 /* Kill the vtable relocations that were not used. */
9098 elf_link_hash_traverse (elf_hash_table (info),
9099 elf_gc_smash_unused_vtentry_relocs,
9100 &ok);
9101 if (!ok)
9102 return FALSE;
9104 /* Mark dynamically referenced symbols. */
9105 if (elf_hash_table (info)->dynamic_sections_created)
9106 elf_link_hash_traverse (elf_hash_table (info),
9107 elf_gc_mark_dynamic_ref_symbol,
9108 info);
9110 /* Grovel through relocs to find out who stays ... */
9111 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9112 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9114 asection *o;
9116 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9117 continue;
9119 for (o = sub->sections; o != NULL; o = o->next)
9120 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9121 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9122 return FALSE;
9125 /* ... again for sections marked from eh_frame. */
9126 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9128 asection *o;
9130 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9131 continue;
9133 /* Keep .gcc_except_table.* if the associated .text.* is
9134 marked. This isn't very nice, but the proper solution,
9135 splitting .eh_frame up and using comdat doesn't pan out
9136 easily due to needing special relocs to handle the
9137 difference of two symbols in separate sections.
9138 Don't keep code sections referenced by .eh_frame. */
9139 for (o = sub->sections; o != NULL; o = o->next)
9140 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9142 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9144 unsigned long len;
9145 char *fn_name;
9146 asection *fn_text;
9148 len = strlen (o->name + 18) + 1;
9149 fn_name = bfd_malloc (len + 6);
9150 if (fn_name == NULL)
9151 return FALSE;
9152 memcpy (fn_name, ".text.", 6);
9153 memcpy (fn_name + 6, o->name + 18, len);
9154 fn_text = bfd_get_section_by_name (sub, fn_name);
9155 free (fn_name);
9156 if (fn_text == NULL || !fn_text->gc_mark)
9157 continue;
9160 /* If not using specially named exception table section,
9161 then keep whatever we are using. */
9162 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9163 return FALSE;
9167 /* ... and mark SEC_EXCLUDE for those that go. */
9168 return elf_gc_sweep (abfd, info);
9171 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9173 bfd_boolean
9174 bfd_elf_gc_record_vtinherit (bfd *abfd,
9175 asection *sec,
9176 struct elf_link_hash_entry *h,
9177 bfd_vma offset)
9179 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9180 struct elf_link_hash_entry **search, *child;
9181 bfd_size_type extsymcount;
9182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9184 /* The sh_info field of the symtab header tells us where the
9185 external symbols start. We don't care about the local symbols at
9186 this point. */
9187 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9188 if (!elf_bad_symtab (abfd))
9189 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9191 sym_hashes = elf_sym_hashes (abfd);
9192 sym_hashes_end = sym_hashes + extsymcount;
9194 /* Hunt down the child symbol, which is in this section at the same
9195 offset as the relocation. */
9196 for (search = sym_hashes; search != sym_hashes_end; ++search)
9198 if ((child = *search) != NULL
9199 && (child->root.type == bfd_link_hash_defined
9200 || child->root.type == bfd_link_hash_defweak)
9201 && child->root.u.def.section == sec
9202 && child->root.u.def.value == offset)
9203 goto win;
9206 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9207 abfd, sec, (unsigned long) offset);
9208 bfd_set_error (bfd_error_invalid_operation);
9209 return FALSE;
9211 win:
9212 if (!child->vtable)
9214 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9215 if (!child->vtable)
9216 return FALSE;
9218 if (!h)
9220 /* This *should* only be the absolute section. It could potentially
9221 be that someone has defined a non-global vtable though, which
9222 would be bad. It isn't worth paging in the local symbols to be
9223 sure though; that case should simply be handled by the assembler. */
9225 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9227 else
9228 child->vtable->parent = h;
9230 return TRUE;
9233 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9235 bfd_boolean
9236 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9237 asection *sec ATTRIBUTE_UNUSED,
9238 struct elf_link_hash_entry *h,
9239 bfd_vma addend)
9241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9242 unsigned int log_file_align = bed->s->log_file_align;
9244 if (!h->vtable)
9246 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9247 if (!h->vtable)
9248 return FALSE;
9251 if (addend >= h->vtable->size)
9253 size_t size, bytes, file_align;
9254 bfd_boolean *ptr = h->vtable->used;
9256 /* While the symbol is undefined, we have to be prepared to handle
9257 a zero size. */
9258 file_align = 1 << log_file_align;
9259 if (h->root.type == bfd_link_hash_undefined)
9260 size = addend + file_align;
9261 else
9263 size = h->size;
9264 if (addend >= size)
9266 /* Oops! We've got a reference past the defined end of
9267 the table. This is probably a bug -- shall we warn? */
9268 size = addend + file_align;
9271 size = (size + file_align - 1) & -file_align;
9273 /* Allocate one extra entry for use as a "done" flag for the
9274 consolidation pass. */
9275 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9277 if (ptr)
9279 ptr = bfd_realloc (ptr - 1, bytes);
9281 if (ptr != NULL)
9283 size_t oldbytes;
9285 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9286 * sizeof (bfd_boolean));
9287 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9290 else
9291 ptr = bfd_zmalloc (bytes);
9293 if (ptr == NULL)
9294 return FALSE;
9296 /* And arrange for that done flag to be at index -1. */
9297 h->vtable->used = ptr + 1;
9298 h->vtable->size = size;
9301 h->vtable->used[addend >> log_file_align] = TRUE;
9303 return TRUE;
9306 struct alloc_got_off_arg {
9307 bfd_vma gotoff;
9308 unsigned int got_elt_size;
9311 /* We need a special top-level link routine to convert got reference counts
9312 to real got offsets. */
9314 static bfd_boolean
9315 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9317 struct alloc_got_off_arg *gofarg = arg;
9319 if (h->root.type == bfd_link_hash_warning)
9320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9322 if (h->got.refcount > 0)
9324 h->got.offset = gofarg->gotoff;
9325 gofarg->gotoff += gofarg->got_elt_size;
9327 else
9328 h->got.offset = (bfd_vma) -1;
9330 return TRUE;
9333 /* And an accompanying bit to work out final got entry offsets once
9334 we're done. Should be called from final_link. */
9336 bfd_boolean
9337 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9338 struct bfd_link_info *info)
9340 bfd *i;
9341 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9342 bfd_vma gotoff;
9343 unsigned int got_elt_size = bed->s->arch_size / 8;
9344 struct alloc_got_off_arg gofarg;
9346 if (! is_elf_hash_table (info->hash))
9347 return FALSE;
9349 /* The GOT offset is relative to the .got section, but the GOT header is
9350 put into the .got.plt section, if the backend uses it. */
9351 if (bed->want_got_plt)
9352 gotoff = 0;
9353 else
9354 gotoff = bed->got_header_size;
9356 /* Do the local .got entries first. */
9357 for (i = info->input_bfds; i; i = i->link_next)
9359 bfd_signed_vma *local_got;
9360 bfd_size_type j, locsymcount;
9361 Elf_Internal_Shdr *symtab_hdr;
9363 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9364 continue;
9366 local_got = elf_local_got_refcounts (i);
9367 if (!local_got)
9368 continue;
9370 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9371 if (elf_bad_symtab (i))
9372 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9373 else
9374 locsymcount = symtab_hdr->sh_info;
9376 for (j = 0; j < locsymcount; ++j)
9378 if (local_got[j] > 0)
9380 local_got[j] = gotoff;
9381 gotoff += got_elt_size;
9383 else
9384 local_got[j] = (bfd_vma) -1;
9388 /* Then the global .got entries. .plt refcounts are handled by
9389 adjust_dynamic_symbol */
9390 gofarg.gotoff = gotoff;
9391 gofarg.got_elt_size = got_elt_size;
9392 elf_link_hash_traverse (elf_hash_table (info),
9393 elf_gc_allocate_got_offsets,
9394 &gofarg);
9395 return TRUE;
9398 /* Many folk need no more in the way of final link than this, once
9399 got entry reference counting is enabled. */
9401 bfd_boolean
9402 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9404 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9405 return FALSE;
9407 /* Invoke the regular ELF backend linker to do all the work. */
9408 return bfd_elf_final_link (abfd, info);
9411 bfd_boolean
9412 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9414 struct elf_reloc_cookie *rcookie = cookie;
9416 if (rcookie->bad_symtab)
9417 rcookie->rel = rcookie->rels;
9419 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9421 unsigned long r_symndx;
9423 if (! rcookie->bad_symtab)
9424 if (rcookie->rel->r_offset > offset)
9425 return FALSE;
9426 if (rcookie->rel->r_offset != offset)
9427 continue;
9429 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9430 if (r_symndx == SHN_UNDEF)
9431 return TRUE;
9433 if (r_symndx >= rcookie->locsymcount
9434 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9436 struct elf_link_hash_entry *h;
9438 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9440 while (h->root.type == bfd_link_hash_indirect
9441 || h->root.type == bfd_link_hash_warning)
9442 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9444 if ((h->root.type == bfd_link_hash_defined
9445 || h->root.type == bfd_link_hash_defweak)
9446 && elf_discarded_section (h->root.u.def.section))
9447 return TRUE;
9448 else
9449 return FALSE;
9451 else
9453 /* It's not a relocation against a global symbol,
9454 but it could be a relocation against a local
9455 symbol for a discarded section. */
9456 asection *isec;
9457 Elf_Internal_Sym *isym;
9459 /* Need to: get the symbol; get the section. */
9460 isym = &rcookie->locsyms[r_symndx];
9461 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9463 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9464 if (isec != NULL && elf_discarded_section (isec))
9465 return TRUE;
9468 return FALSE;
9470 return FALSE;
9473 /* Discard unneeded references to discarded sections.
9474 Returns TRUE if any section's size was changed. */
9475 /* This function assumes that the relocations are in sorted order,
9476 which is true for all known assemblers. */
9478 bfd_boolean
9479 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9481 struct elf_reloc_cookie cookie;
9482 asection *stab, *eh;
9483 Elf_Internal_Shdr *symtab_hdr;
9484 const struct elf_backend_data *bed;
9485 bfd *abfd;
9486 unsigned int count;
9487 bfd_boolean ret = FALSE;
9489 if (info->traditional_format
9490 || !is_elf_hash_table (info->hash))
9491 return FALSE;
9493 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9495 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9496 continue;
9498 bed = get_elf_backend_data (abfd);
9500 if ((abfd->flags & DYNAMIC) != 0)
9501 continue;
9503 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9504 if (info->relocatable
9505 || (eh != NULL
9506 && (eh->size == 0
9507 || bfd_is_abs_section (eh->output_section))))
9508 eh = NULL;
9510 stab = bfd_get_section_by_name (abfd, ".stab");
9511 if (stab != NULL
9512 && (stab->size == 0
9513 || bfd_is_abs_section (stab->output_section)
9514 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9515 stab = NULL;
9517 if (stab == NULL
9518 && eh == NULL
9519 && bed->elf_backend_discard_info == NULL)
9520 continue;
9522 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9523 cookie.abfd = abfd;
9524 cookie.sym_hashes = elf_sym_hashes (abfd);
9525 cookie.bad_symtab = elf_bad_symtab (abfd);
9526 if (cookie.bad_symtab)
9528 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9529 cookie.extsymoff = 0;
9531 else
9533 cookie.locsymcount = symtab_hdr->sh_info;
9534 cookie.extsymoff = symtab_hdr->sh_info;
9537 if (bed->s->arch_size == 32)
9538 cookie.r_sym_shift = 8;
9539 else
9540 cookie.r_sym_shift = 32;
9542 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9543 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9545 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9546 cookie.locsymcount, 0,
9547 NULL, NULL, NULL);
9548 if (cookie.locsyms == NULL)
9549 return FALSE;
9552 if (stab != NULL)
9554 cookie.rels = NULL;
9555 count = stab->reloc_count;
9556 if (count != 0)
9557 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9558 info->keep_memory);
9559 if (cookie.rels != NULL)
9561 cookie.rel = cookie.rels;
9562 cookie.relend = cookie.rels;
9563 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9564 if (_bfd_discard_section_stabs (abfd, stab,
9565 elf_section_data (stab)->sec_info,
9566 bfd_elf_reloc_symbol_deleted_p,
9567 &cookie))
9568 ret = TRUE;
9569 if (elf_section_data (stab)->relocs != cookie.rels)
9570 free (cookie.rels);
9574 if (eh != NULL)
9576 cookie.rels = NULL;
9577 count = eh->reloc_count;
9578 if (count != 0)
9579 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9580 info->keep_memory);
9581 cookie.rel = cookie.rels;
9582 cookie.relend = cookie.rels;
9583 if (cookie.rels != NULL)
9584 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9586 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9587 bfd_elf_reloc_symbol_deleted_p,
9588 &cookie))
9589 ret = TRUE;
9591 if (cookie.rels != NULL
9592 && elf_section_data (eh)->relocs != cookie.rels)
9593 free (cookie.rels);
9596 if (bed->elf_backend_discard_info != NULL
9597 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9598 ret = TRUE;
9600 if (cookie.locsyms != NULL
9601 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9603 if (! info->keep_memory)
9604 free (cookie.locsyms);
9605 else
9606 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9610 if (info->eh_frame_hdr
9611 && !info->relocatable
9612 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9613 ret = TRUE;
9615 return ret;
9618 void
9619 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9621 flagword flags;
9622 const char *name, *p;
9623 struct bfd_section_already_linked *l;
9624 struct bfd_section_already_linked_hash_entry *already_linked_list;
9625 asection *group;
9627 /* A single member comdat group section may be discarded by a
9628 linkonce section. See below. */
9629 if (sec->output_section == bfd_abs_section_ptr)
9630 return;
9632 flags = sec->flags;
9634 /* Check if it belongs to a section group. */
9635 group = elf_sec_group (sec);
9637 /* Return if it isn't a linkonce section nor a member of a group. A
9638 comdat group section also has SEC_LINK_ONCE set. */
9639 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9640 return;
9642 if (group)
9644 /* If this is the member of a single member comdat group, check if
9645 the group should be discarded. */
9646 if (elf_next_in_group (sec) == sec
9647 && (group->flags & SEC_LINK_ONCE) != 0)
9648 sec = group;
9649 else
9650 return;
9653 /* FIXME: When doing a relocatable link, we may have trouble
9654 copying relocations in other sections that refer to local symbols
9655 in the section being discarded. Those relocations will have to
9656 be converted somehow; as of this writing I'm not sure that any of
9657 the backends handle that correctly.
9659 It is tempting to instead not discard link once sections when
9660 doing a relocatable link (technically, they should be discarded
9661 whenever we are building constructors). However, that fails,
9662 because the linker winds up combining all the link once sections
9663 into a single large link once section, which defeats the purpose
9664 of having link once sections in the first place.
9666 Also, not merging link once sections in a relocatable link
9667 causes trouble for MIPS ELF, which relies on link once semantics
9668 to handle the .reginfo section correctly. */
9670 name = bfd_get_section_name (abfd, sec);
9672 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9673 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9674 p++;
9675 else
9676 p = name;
9678 already_linked_list = bfd_section_already_linked_table_lookup (p);
9680 for (l = already_linked_list->entry; l != NULL; l = l->next)
9682 /* We may have 3 different sections on the list: group section,
9683 comdat section and linkonce section. SEC may be a linkonce or
9684 group section. We match a group section with a group section,
9685 a linkonce section with a linkonce section, and ignore comdat
9686 section. */
9687 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9688 && strcmp (name, l->sec->name) == 0
9689 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9691 /* The section has already been linked. See if we should
9692 issue a warning. */
9693 switch (flags & SEC_LINK_DUPLICATES)
9695 default:
9696 abort ();
9698 case SEC_LINK_DUPLICATES_DISCARD:
9699 break;
9701 case SEC_LINK_DUPLICATES_ONE_ONLY:
9702 (*_bfd_error_handler)
9703 (_("%B: ignoring duplicate section `%A'"),
9704 abfd, sec);
9705 break;
9707 case SEC_LINK_DUPLICATES_SAME_SIZE:
9708 if (sec->size != l->sec->size)
9709 (*_bfd_error_handler)
9710 (_("%B: duplicate section `%A' has different size"),
9711 abfd, sec);
9712 break;
9714 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9715 if (sec->size != l->sec->size)
9716 (*_bfd_error_handler)
9717 (_("%B: duplicate section `%A' has different size"),
9718 abfd, sec);
9719 else if (sec->size != 0)
9721 bfd_byte *sec_contents, *l_sec_contents;
9723 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9724 (*_bfd_error_handler)
9725 (_("%B: warning: could not read contents of section `%A'"),
9726 abfd, sec);
9727 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9728 &l_sec_contents))
9729 (*_bfd_error_handler)
9730 (_("%B: warning: could not read contents of section `%A'"),
9731 l->sec->owner, l->sec);
9732 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9733 (*_bfd_error_handler)
9734 (_("%B: warning: duplicate section `%A' has different contents"),
9735 abfd, sec);
9737 if (sec_contents)
9738 free (sec_contents);
9739 if (l_sec_contents)
9740 free (l_sec_contents);
9742 break;
9745 /* Set the output_section field so that lang_add_section
9746 does not create a lang_input_section structure for this
9747 section. Since there might be a symbol in the section
9748 being discarded, we must retain a pointer to the section
9749 which we are really going to use. */
9750 sec->output_section = bfd_abs_section_ptr;
9751 sec->kept_section = l->sec;
9753 if (flags & SEC_GROUP)
9755 asection *first = elf_next_in_group (sec);
9756 asection *s = first;
9758 while (s != NULL)
9760 s->output_section = bfd_abs_section_ptr;
9761 /* Record which group discards it. */
9762 s->kept_section = l->sec;
9763 s = elf_next_in_group (s);
9764 /* These lists are circular. */
9765 if (s == first)
9766 break;
9770 return;
9774 if (group)
9776 /* If this is the member of a single member comdat group and the
9777 group hasn't be discarded, we check if it matches a linkonce
9778 section. We only record the discarded comdat group. Otherwise
9779 the undiscarded group will be discarded incorrectly later since
9780 itself has been recorded. */
9781 for (l = already_linked_list->entry; l != NULL; l = l->next)
9782 if ((l->sec->flags & SEC_GROUP) == 0
9783 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9784 && bfd_elf_match_symbols_in_sections (l->sec,
9785 elf_next_in_group (sec)))
9787 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9788 elf_next_in_group (sec)->kept_section = l->sec;
9789 group->output_section = bfd_abs_section_ptr;
9790 break;
9792 if (l == NULL)
9793 return;
9795 else
9796 /* There is no direct match. But for linkonce section, we should
9797 check if there is a match with comdat group member. We always
9798 record the linkonce section, discarded or not. */
9799 for (l = already_linked_list->entry; l != NULL; l = l->next)
9800 if (l->sec->flags & SEC_GROUP)
9802 asection *first = elf_next_in_group (l->sec);
9804 if (first != NULL
9805 && elf_next_in_group (first) == first
9806 && bfd_elf_match_symbols_in_sections (first, sec))
9808 sec->output_section = bfd_abs_section_ptr;
9809 sec->kept_section = l->sec;
9810 break;
9814 /* This is the first section with this name. Record it. */
9815 bfd_section_already_linked_table_insert (already_linked_list, sec);
9818 bfd_boolean
9819 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9821 return sym->st_shndx == SHN_COMMON;
9824 unsigned int
9825 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9827 return SHN_COMMON;
9830 asection *
9831 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9833 return bfd_com_section_ptr;