ld/
[binutils.git] / bfd / elflink.c
blob4d3c58a806cbfaab2c7c9bd1b992761ca35f42ed
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
31 /* Define a symbol in a dynamic linkage section. */
33 struct elf_link_hash_entry *
34 _bfd_elf_define_linkage_sym (bfd *abfd,
35 struct bfd_link_info *info,
36 asection *sec,
37 const char *name)
39 struct elf_link_hash_entry *h;
40 struct bfd_link_hash_entry *bh;
41 const struct elf_backend_data *bed;
43 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
44 if (h != NULL)
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h->root.type = bfd_link_hash_new;
53 bh = &h->root;
54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
55 sec, 0, NULL, FALSE,
56 get_elf_backend_data (abfd)->collect,
57 &bh))
58 return NULL;
59 h = (struct elf_link_hash_entry *) bh;
60 h->def_regular = 1;
61 h->type = STT_OBJECT;
62 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64 bed = get_elf_backend_data (abfd);
65 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
66 return h;
69 bfd_boolean
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
72 flagword flags;
73 asection *s;
74 struct elf_link_hash_entry *h;
75 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
76 int ptralign;
78 /* This function may be called more than once. */
79 s = bfd_get_section_by_name (abfd, ".got");
80 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
81 return TRUE;
83 switch (bed->s->arch_size)
85 case 32:
86 ptralign = 2;
87 break;
89 case 64:
90 ptralign = 3;
91 break;
93 default:
94 bfd_set_error (bfd_error_bad_value);
95 return FALSE;
98 flags = bed->dynamic_sec_flags;
100 s = bfd_make_section_with_flags (abfd, ".got", flags);
101 if (s == NULL
102 || !bfd_set_section_alignment (abfd, s, ptralign))
103 return FALSE;
105 if (bed->want_got_plt)
107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
108 if (s == NULL
109 || !bfd_set_section_alignment (abfd, s, ptralign))
110 return FALSE;
113 if (bed->want_got_sym)
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info)->hgot = h;
121 if (h == NULL)
122 return FALSE;
125 /* The first bit of the global offset table is the header. */
126 s->size += bed->got_header_size;
128 return TRUE;
131 /* Create a strtab to hold the dynamic symbol names. */
132 static bfd_boolean
133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135 struct elf_link_hash_table *hash_table;
137 hash_table = elf_hash_table (info);
138 if (hash_table->dynobj == NULL)
139 hash_table->dynobj = abfd;
141 if (hash_table->dynstr == NULL)
143 hash_table->dynstr = _bfd_elf_strtab_init ();
144 if (hash_table->dynstr == NULL)
145 return FALSE;
147 return TRUE;
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
157 bfd_boolean
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
160 flagword flags;
161 register asection *s;
162 const struct elf_backend_data *bed;
164 if (! is_elf_hash_table (info->hash))
165 return FALSE;
167 if (elf_hash_table (info)->dynamic_sections_created)
168 return TRUE;
170 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
171 return FALSE;
173 abfd = elf_hash_table (info)->dynobj;
174 bed = get_elf_backend_data (abfd);
176 flags = bed->dynamic_sec_flags;
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info->executable)
182 s = bfd_make_section_with_flags (abfd, ".interp",
183 flags | SEC_READONLY);
184 if (s == NULL)
185 return FALSE;
188 /* Create sections to hold version informations. These are removed
189 if they are not needed. */
190 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
191 flags | SEC_READONLY);
192 if (s == NULL
193 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
194 return FALSE;
196 s = bfd_make_section_with_flags (abfd, ".gnu.version",
197 flags | SEC_READONLY);
198 if (s == NULL
199 || ! bfd_set_section_alignment (abfd, s, 1))
200 return FALSE;
202 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
203 flags | SEC_READONLY);
204 if (s == NULL
205 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
206 return FALSE;
208 s = bfd_make_section_with_flags (abfd, ".dynsym",
209 flags | SEC_READONLY);
210 if (s == NULL
211 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
212 return FALSE;
214 s = bfd_make_section_with_flags (abfd, ".dynstr",
215 flags | SEC_READONLY);
216 if (s == NULL)
217 return FALSE;
219 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
220 if (s == NULL
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
224 /* The special symbol _DYNAMIC is always set to the start of the
225 .dynamic section. We could set _DYNAMIC in a linker script, but we
226 only want to define it if we are, in fact, creating a .dynamic
227 section. We don't want to define it if there is no .dynamic
228 section, since on some ELF platforms the start up code examines it
229 to decide how to initialize the process. */
230 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
231 return FALSE;
233 if (info->emit_hash)
235 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
236 if (s == NULL
237 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
238 return FALSE;
239 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
242 if (info->emit_gnu_hash)
244 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
245 flags | SEC_READONLY);
246 if (s == NULL
247 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
248 return FALSE;
249 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
250 4 32-bit words followed by variable count of 64-bit words, then
251 variable count of 32-bit words. */
252 if (bed->s->arch_size == 64)
253 elf_section_data (s)->this_hdr.sh_entsize = 0;
254 else
255 elf_section_data (s)->this_hdr.sh_entsize = 4;
258 /* Let the backend create the rest of the sections. This lets the
259 backend set the right flags. The backend will normally create
260 the .got and .plt sections. */
261 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
262 return FALSE;
264 elf_hash_table (info)->dynamic_sections_created = TRUE;
266 return TRUE;
269 /* Create dynamic sections when linking against a dynamic object. */
271 bfd_boolean
272 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
274 flagword flags, pltflags;
275 struct elf_link_hash_entry *h;
276 asection *s;
277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
279 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
280 .rel[a].bss sections. */
281 flags = bed->dynamic_sec_flags;
283 pltflags = flags;
284 if (bed->plt_not_loaded)
285 /* We do not clear SEC_ALLOC here because we still want the OS to
286 allocate space for the section; it's just that there's nothing
287 to read in from the object file. */
288 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
289 else
290 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
291 if (bed->plt_readonly)
292 pltflags |= SEC_READONLY;
294 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
295 if (s == NULL
296 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
297 return FALSE;
299 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
300 .plt section. */
301 if (bed->want_plt_sym)
303 h = _bfd_elf_define_linkage_sym (abfd, info, s,
304 "_PROCEDURE_LINKAGE_TABLE_");
305 elf_hash_table (info)->hplt = h;
306 if (h == NULL)
307 return FALSE;
310 s = bfd_make_section_with_flags (abfd,
311 (bed->default_use_rela_p
312 ? ".rela.plt" : ".rel.plt"),
313 flags | SEC_READONLY);
314 if (s == NULL
315 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
316 return FALSE;
318 if (! _bfd_elf_create_got_section (abfd, info))
319 return FALSE;
321 if (bed->want_dynbss)
323 /* The .dynbss section is a place to put symbols which are defined
324 by dynamic objects, are referenced by regular objects, and are
325 not functions. We must allocate space for them in the process
326 image and use a R_*_COPY reloc to tell the dynamic linker to
327 initialize them at run time. The linker script puts the .dynbss
328 section into the .bss section of the final image. */
329 s = bfd_make_section_with_flags (abfd, ".dynbss",
330 (SEC_ALLOC
331 | SEC_LINKER_CREATED));
332 if (s == NULL)
333 return FALSE;
335 /* The .rel[a].bss section holds copy relocs. This section is not
336 normally needed. We need to create it here, though, so that the
337 linker will map it to an output section. We can't just create it
338 only if we need it, because we will not know whether we need it
339 until we have seen all the input files, and the first time the
340 main linker code calls BFD after examining all the input files
341 (size_dynamic_sections) the input sections have already been
342 mapped to the output sections. If the section turns out not to
343 be needed, we can discard it later. We will never need this
344 section when generating a shared object, since they do not use
345 copy relocs. */
346 if (! info->shared)
348 s = bfd_make_section_with_flags (abfd,
349 (bed->default_use_rela_p
350 ? ".rela.bss" : ".rel.bss"),
351 flags | SEC_READONLY);
352 if (s == NULL
353 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
354 return FALSE;
358 return TRUE;
361 /* Record a new dynamic symbol. We record the dynamic symbols as we
362 read the input files, since we need to have a list of all of them
363 before we can determine the final sizes of the output sections.
364 Note that we may actually call this function even though we are not
365 going to output any dynamic symbols; in some cases we know that a
366 symbol should be in the dynamic symbol table, but only if there is
367 one. */
369 bfd_boolean
370 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
371 struct elf_link_hash_entry *h)
373 if (h->dynindx == -1)
375 struct elf_strtab_hash *dynstr;
376 char *p;
377 const char *name;
378 bfd_size_type indx;
380 /* XXX: The ABI draft says the linker must turn hidden and
381 internal symbols into STB_LOCAL symbols when producing the
382 DSO. However, if ld.so honors st_other in the dynamic table,
383 this would not be necessary. */
384 switch (ELF_ST_VISIBILITY (h->other))
386 case STV_INTERNAL:
387 case STV_HIDDEN:
388 if (h->root.type != bfd_link_hash_undefined
389 && h->root.type != bfd_link_hash_undefweak)
391 h->forced_local = 1;
392 if (!elf_hash_table (info)->is_relocatable_executable)
393 return TRUE;
396 default:
397 break;
400 h->dynindx = elf_hash_table (info)->dynsymcount;
401 ++elf_hash_table (info)->dynsymcount;
403 dynstr = elf_hash_table (info)->dynstr;
404 if (dynstr == NULL)
406 /* Create a strtab to hold the dynamic symbol names. */
407 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
408 if (dynstr == NULL)
409 return FALSE;
412 /* We don't put any version information in the dynamic string
413 table. */
414 name = h->root.root.string;
415 p = strchr (name, ELF_VER_CHR);
416 if (p != NULL)
417 /* We know that the p points into writable memory. In fact,
418 there are only a few symbols that have read-only names, being
419 those like _GLOBAL_OFFSET_TABLE_ that are created specially
420 by the backends. Most symbols will have names pointing into
421 an ELF string table read from a file, or to objalloc memory. */
422 *p = 0;
424 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
426 if (p != NULL)
427 *p = ELF_VER_CHR;
429 if (indx == (bfd_size_type) -1)
430 return FALSE;
431 h->dynstr_index = indx;
434 return TRUE;
437 /* Mark a symbol dynamic. */
439 void
440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
441 struct elf_link_hash_entry *h,
442 Elf_Internal_Sym *sym)
444 struct bfd_elf_dynamic_list *d = info->dynamic_list;
446 /* It may be called more than once on the same H. */
447 if(h->dynamic || info->relocatable)
448 return;
450 if ((info->dynamic_data
451 && (h->type == STT_OBJECT
452 || (sym != NULL
453 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
454 || (d != NULL
455 && h->root.type == bfd_link_hash_new
456 && (*d->match) (&d->head, NULL, h->root.root.string)))
457 h->dynamic = 1;
460 /* Record an assignment to a symbol made by a linker script. We need
461 this in case some dynamic object refers to this symbol. */
463 bfd_boolean
464 bfd_elf_record_link_assignment (bfd *output_bfd,
465 struct bfd_link_info *info,
466 const char *name,
467 bfd_boolean provide,
468 bfd_boolean hidden)
470 struct elf_link_hash_entry *h;
471 struct elf_link_hash_table *htab;
473 if (!is_elf_hash_table (info->hash))
474 return TRUE;
476 htab = elf_hash_table (info);
477 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
478 if (h == NULL)
479 return provide;
481 /* Since we're defining the symbol, don't let it seem to have not
482 been defined. record_dynamic_symbol and size_dynamic_sections
483 may depend on this. */
484 if (h->root.type == bfd_link_hash_undefweak
485 || h->root.type == bfd_link_hash_undefined)
487 h->root.type = bfd_link_hash_new;
488 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
489 bfd_link_repair_undef_list (&htab->root);
492 if (h->root.type == bfd_link_hash_new)
494 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
495 h->non_elf = 0;
498 /* If this symbol is being provided by the linker script, and it is
499 currently defined by a dynamic object, but not by a regular
500 object, then mark it as undefined so that the generic linker will
501 force the correct value. */
502 if (provide
503 && h->def_dynamic
504 && !h->def_regular)
505 h->root.type = bfd_link_hash_undefined;
507 /* If this symbol is not being provided by the linker script, and it is
508 currently defined by a dynamic object, but not by a regular object,
509 then clear out any version information because the symbol will not be
510 associated with the dynamic object any more. */
511 if (!provide
512 && h->def_dynamic
513 && !h->def_regular)
514 h->verinfo.verdef = NULL;
516 h->def_regular = 1;
518 if (provide && hidden)
520 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
522 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
523 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
526 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
527 and executables. */
528 if (!info->relocatable
529 && h->dynindx != -1
530 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
531 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
532 h->forced_local = 1;
534 if ((h->def_dynamic
535 || h->ref_dynamic
536 || info->shared
537 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
538 && h->dynindx == -1)
540 if (! bfd_elf_link_record_dynamic_symbol (info, h))
541 return FALSE;
543 /* If this is a weak defined symbol, and we know a corresponding
544 real symbol from the same dynamic object, make sure the real
545 symbol is also made into a dynamic symbol. */
546 if (h->u.weakdef != NULL
547 && h->u.weakdef->dynindx == -1)
549 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
550 return FALSE;
554 return TRUE;
557 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
558 success, and 2 on a failure caused by attempting to record a symbol
559 in a discarded section, eg. a discarded link-once section symbol. */
562 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
563 bfd *input_bfd,
564 long input_indx)
566 bfd_size_type amt;
567 struct elf_link_local_dynamic_entry *entry;
568 struct elf_link_hash_table *eht;
569 struct elf_strtab_hash *dynstr;
570 unsigned long dynstr_index;
571 char *name;
572 Elf_External_Sym_Shndx eshndx;
573 char esym[sizeof (Elf64_External_Sym)];
575 if (! is_elf_hash_table (info->hash))
576 return 0;
578 /* See if the entry exists already. */
579 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
580 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
581 return 1;
583 amt = sizeof (*entry);
584 entry = bfd_alloc (input_bfd, amt);
585 if (entry == NULL)
586 return 0;
588 /* Go find the symbol, so that we can find it's name. */
589 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
590 1, input_indx, &entry->isym, esym, &eshndx))
592 bfd_release (input_bfd, entry);
593 return 0;
596 if (entry->isym.st_shndx != SHN_UNDEF
597 && (entry->isym.st_shndx < SHN_LORESERVE
598 || entry->isym.st_shndx > SHN_HIRESERVE))
600 asection *s;
602 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
603 if (s == NULL || bfd_is_abs_section (s->output_section))
605 /* We can still bfd_release here as nothing has done another
606 bfd_alloc. We can't do this later in this function. */
607 bfd_release (input_bfd, entry);
608 return 2;
612 name = (bfd_elf_string_from_elf_section
613 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
614 entry->isym.st_name));
616 dynstr = elf_hash_table (info)->dynstr;
617 if (dynstr == NULL)
619 /* Create a strtab to hold the dynamic symbol names. */
620 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
621 if (dynstr == NULL)
622 return 0;
625 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
626 if (dynstr_index == (unsigned long) -1)
627 return 0;
628 entry->isym.st_name = dynstr_index;
630 eht = elf_hash_table (info);
632 entry->next = eht->dynlocal;
633 eht->dynlocal = entry;
634 entry->input_bfd = input_bfd;
635 entry->input_indx = input_indx;
636 eht->dynsymcount++;
638 /* Whatever binding the symbol had before, it's now local. */
639 entry->isym.st_info
640 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
642 /* The dynindx will be set at the end of size_dynamic_sections. */
644 return 1;
647 /* Return the dynindex of a local dynamic symbol. */
649 long
650 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
651 bfd *input_bfd,
652 long input_indx)
654 struct elf_link_local_dynamic_entry *e;
656 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
657 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
658 return e->dynindx;
659 return -1;
662 /* This function is used to renumber the dynamic symbols, if some of
663 them are removed because they are marked as local. This is called
664 via elf_link_hash_traverse. */
666 static bfd_boolean
667 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
668 void *data)
670 size_t *count = data;
672 if (h->root.type == bfd_link_hash_warning)
673 h = (struct elf_link_hash_entry *) h->root.u.i.link;
675 if (h->forced_local)
676 return TRUE;
678 if (h->dynindx != -1)
679 h->dynindx = ++(*count);
681 return TRUE;
685 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
686 STB_LOCAL binding. */
688 static bfd_boolean
689 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
690 void *data)
692 size_t *count = data;
694 if (h->root.type == bfd_link_hash_warning)
695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
697 if (!h->forced_local)
698 return TRUE;
700 if (h->dynindx != -1)
701 h->dynindx = ++(*count);
703 return TRUE;
706 /* Return true if the dynamic symbol for a given section should be
707 omitted when creating a shared library. */
708 bfd_boolean
709 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
710 struct bfd_link_info *info,
711 asection *p)
713 struct elf_link_hash_table *htab;
715 switch (elf_section_data (p)->this_hdr.sh_type)
717 case SHT_PROGBITS:
718 case SHT_NOBITS:
719 /* If sh_type is yet undecided, assume it could be
720 SHT_PROGBITS/SHT_NOBITS. */
721 case SHT_NULL:
722 htab = elf_hash_table (info);
723 if (p == htab->tls_sec)
724 return FALSE;
726 if (htab->text_index_section != NULL)
727 return p != htab->text_index_section && p != htab->data_index_section;
729 if (strcmp (p->name, ".got") == 0
730 || strcmp (p->name, ".got.plt") == 0
731 || strcmp (p->name, ".plt") == 0)
733 asection *ip;
735 if (htab->dynobj != NULL
736 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
737 && (ip->flags & SEC_LINKER_CREATED)
738 && ip->output_section == p)
739 return TRUE;
741 return FALSE;
743 /* There shouldn't be section relative relocations
744 against any other section. */
745 default:
746 return TRUE;
750 /* Assign dynsym indices. In a shared library we generate a section
751 symbol for each output section, which come first. Next come symbols
752 which have been forced to local binding. Then all of the back-end
753 allocated local dynamic syms, followed by the rest of the global
754 symbols. */
756 static unsigned long
757 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
758 struct bfd_link_info *info,
759 unsigned long *section_sym_count)
761 unsigned long dynsymcount = 0;
763 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
765 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
766 asection *p;
767 for (p = output_bfd->sections; p ; p = p->next)
768 if ((p->flags & SEC_EXCLUDE) == 0
769 && (p->flags & SEC_ALLOC) != 0
770 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
771 elf_section_data (p)->dynindx = ++dynsymcount;
772 else
773 elf_section_data (p)->dynindx = 0;
775 *section_sym_count = dynsymcount;
777 elf_link_hash_traverse (elf_hash_table (info),
778 elf_link_renumber_local_hash_table_dynsyms,
779 &dynsymcount);
781 if (elf_hash_table (info)->dynlocal)
783 struct elf_link_local_dynamic_entry *p;
784 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
785 p->dynindx = ++dynsymcount;
788 elf_link_hash_traverse (elf_hash_table (info),
789 elf_link_renumber_hash_table_dynsyms,
790 &dynsymcount);
792 /* There is an unused NULL entry at the head of the table which
793 we must account for in our count. Unless there weren't any
794 symbols, which means we'll have no table at all. */
795 if (dynsymcount != 0)
796 ++dynsymcount;
798 elf_hash_table (info)->dynsymcount = dynsymcount;
799 return dynsymcount;
802 /* This function is called when we want to define a new symbol. It
803 handles the various cases which arise when we find a definition in
804 a dynamic object, or when there is already a definition in a
805 dynamic object. The new symbol is described by NAME, SYM, PSEC,
806 and PVALUE. We set SYM_HASH to the hash table entry. We set
807 OVERRIDE if the old symbol is overriding a new definition. We set
808 TYPE_CHANGE_OK if it is OK for the type to change. We set
809 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
810 change, we mean that we shouldn't warn if the type or size does
811 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
812 object is overridden by a regular object. */
814 bfd_boolean
815 _bfd_elf_merge_symbol (bfd *abfd,
816 struct bfd_link_info *info,
817 const char *name,
818 Elf_Internal_Sym *sym,
819 asection **psec,
820 bfd_vma *pvalue,
821 unsigned int *pold_alignment,
822 struct elf_link_hash_entry **sym_hash,
823 bfd_boolean *skip,
824 bfd_boolean *override,
825 bfd_boolean *type_change_ok,
826 bfd_boolean *size_change_ok)
828 asection *sec, *oldsec;
829 struct elf_link_hash_entry *h;
830 struct elf_link_hash_entry *flip;
831 int bind;
832 bfd *oldbfd;
833 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
834 bfd_boolean newweak, oldweak;
835 const struct elf_backend_data *bed;
837 *skip = FALSE;
838 *override = FALSE;
840 sec = *psec;
841 bind = ELF_ST_BIND (sym->st_info);
843 /* Silently discard TLS symbols from --just-syms. There's no way to
844 combine a static TLS block with a new TLS block for this executable. */
845 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
846 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
848 *skip = TRUE;
849 return TRUE;
852 if (! bfd_is_und_section (sec))
853 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
854 else
855 h = ((struct elf_link_hash_entry *)
856 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
857 if (h == NULL)
858 return FALSE;
859 *sym_hash = h;
861 /* This code is for coping with dynamic objects, and is only useful
862 if we are doing an ELF link. */
863 if (info->hash->creator != abfd->xvec)
864 return TRUE;
866 /* For merging, we only care about real symbols. */
868 while (h->root.type == bfd_link_hash_indirect
869 || h->root.type == bfd_link_hash_warning)
870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
872 /* We have to check it for every instance since the first few may be
873 refereences and not all compilers emit symbol type for undefined
874 symbols. */
875 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
877 /* If we just created the symbol, mark it as being an ELF symbol.
878 Other than that, there is nothing to do--there is no merge issue
879 with a newly defined symbol--so we just return. */
881 if (h->root.type == bfd_link_hash_new)
883 h->non_elf = 0;
884 return TRUE;
887 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
888 existing symbol. */
890 switch (h->root.type)
892 default:
893 oldbfd = NULL;
894 oldsec = NULL;
895 break;
897 case bfd_link_hash_undefined:
898 case bfd_link_hash_undefweak:
899 oldbfd = h->root.u.undef.abfd;
900 oldsec = NULL;
901 break;
903 case bfd_link_hash_defined:
904 case bfd_link_hash_defweak:
905 oldbfd = h->root.u.def.section->owner;
906 oldsec = h->root.u.def.section;
907 break;
909 case bfd_link_hash_common:
910 oldbfd = h->root.u.c.p->section->owner;
911 oldsec = h->root.u.c.p->section;
912 break;
915 /* In cases involving weak versioned symbols, we may wind up trying
916 to merge a symbol with itself. Catch that here, to avoid the
917 confusion that results if we try to override a symbol with
918 itself. The additional tests catch cases like
919 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
920 dynamic object, which we do want to handle here. */
921 if (abfd == oldbfd
922 && ((abfd->flags & DYNAMIC) == 0
923 || !h->def_regular))
924 return TRUE;
926 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
927 respectively, is from a dynamic object. */
929 newdyn = (abfd->flags & DYNAMIC) != 0;
931 olddyn = FALSE;
932 if (oldbfd != NULL)
933 olddyn = (oldbfd->flags & DYNAMIC) != 0;
934 else if (oldsec != NULL)
936 /* This handles the special SHN_MIPS_{TEXT,DATA} section
937 indices used by MIPS ELF. */
938 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
941 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
942 respectively, appear to be a definition rather than reference. */
944 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
946 olddef = (h->root.type != bfd_link_hash_undefined
947 && h->root.type != bfd_link_hash_undefweak
948 && h->root.type != bfd_link_hash_common);
950 /* When we try to create a default indirect symbol from the dynamic
951 definition with the default version, we skip it if its type and
952 the type of existing regular definition mismatch. We only do it
953 if the existing regular definition won't be dynamic. */
954 if (pold_alignment == NULL
955 && !info->shared
956 && !info->export_dynamic
957 && !h->ref_dynamic
958 && newdyn
959 && newdef
960 && !olddyn
961 && (olddef || h->root.type == bfd_link_hash_common)
962 && ELF_ST_TYPE (sym->st_info) != h->type
963 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
964 && h->type != STT_NOTYPE)
966 *skip = TRUE;
967 return TRUE;
970 /* Check TLS symbol. We don't check undefined symbol introduced by
971 "ld -u". */
972 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
973 && ELF_ST_TYPE (sym->st_info) != h->type
974 && oldbfd != NULL)
976 bfd *ntbfd, *tbfd;
977 bfd_boolean ntdef, tdef;
978 asection *ntsec, *tsec;
980 if (h->type == STT_TLS)
982 ntbfd = abfd;
983 ntsec = sec;
984 ntdef = newdef;
985 tbfd = oldbfd;
986 tsec = oldsec;
987 tdef = olddef;
989 else
991 ntbfd = oldbfd;
992 ntsec = oldsec;
993 ntdef = olddef;
994 tbfd = abfd;
995 tsec = sec;
996 tdef = newdef;
999 if (tdef && ntdef)
1000 (*_bfd_error_handler)
1001 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1002 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1003 else if (!tdef && !ntdef)
1004 (*_bfd_error_handler)
1005 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1006 tbfd, ntbfd, h->root.root.string);
1007 else if (tdef)
1008 (*_bfd_error_handler)
1009 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1010 tbfd, tsec, ntbfd, h->root.root.string);
1011 else
1012 (*_bfd_error_handler)
1013 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1014 tbfd, ntbfd, ntsec, h->root.root.string);
1016 bfd_set_error (bfd_error_bad_value);
1017 return FALSE;
1020 /* We need to remember if a symbol has a definition in a dynamic
1021 object or is weak in all dynamic objects. Internal and hidden
1022 visibility will make it unavailable to dynamic objects. */
1023 if (newdyn && !h->dynamic_def)
1025 if (!bfd_is_und_section (sec))
1026 h->dynamic_def = 1;
1027 else
1029 /* Check if this symbol is weak in all dynamic objects. If it
1030 is the first time we see it in a dynamic object, we mark
1031 if it is weak. Otherwise, we clear it. */
1032 if (!h->ref_dynamic)
1034 if (bind == STB_WEAK)
1035 h->dynamic_weak = 1;
1037 else if (bind != STB_WEAK)
1038 h->dynamic_weak = 0;
1042 /* If the old symbol has non-default visibility, we ignore the new
1043 definition from a dynamic object. */
1044 if (newdyn
1045 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1046 && !bfd_is_und_section (sec))
1048 *skip = TRUE;
1049 /* Make sure this symbol is dynamic. */
1050 h->ref_dynamic = 1;
1051 /* A protected symbol has external availability. Make sure it is
1052 recorded as dynamic.
1054 FIXME: Should we check type and size for protected symbol? */
1055 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1056 return bfd_elf_link_record_dynamic_symbol (info, h);
1057 else
1058 return TRUE;
1060 else if (!newdyn
1061 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1062 && h->def_dynamic)
1064 /* If the new symbol with non-default visibility comes from a
1065 relocatable file and the old definition comes from a dynamic
1066 object, we remove the old definition. */
1067 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1069 /* Handle the case where the old dynamic definition is
1070 default versioned. We need to copy the symbol info from
1071 the symbol with default version to the normal one if it
1072 was referenced before. */
1073 if (h->ref_regular)
1075 const struct elf_backend_data *bed
1076 = get_elf_backend_data (abfd);
1077 struct elf_link_hash_entry *vh = *sym_hash;
1078 vh->root.type = h->root.type;
1079 h->root.type = bfd_link_hash_indirect;
1080 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1081 /* Protected symbols will override the dynamic definition
1082 with default version. */
1083 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1085 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1086 vh->dynamic_def = 1;
1087 vh->ref_dynamic = 1;
1089 else
1091 h->root.type = vh->root.type;
1092 vh->ref_dynamic = 0;
1093 /* We have to hide it here since it was made dynamic
1094 global with extra bits when the symbol info was
1095 copied from the old dynamic definition. */
1096 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1098 h = vh;
1100 else
1101 h = *sym_hash;
1104 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1105 && bfd_is_und_section (sec))
1107 /* If the new symbol is undefined and the old symbol was
1108 also undefined before, we need to make sure
1109 _bfd_generic_link_add_one_symbol doesn't mess
1110 up the linker hash table undefs list. Since the old
1111 definition came from a dynamic object, it is still on the
1112 undefs list. */
1113 h->root.type = bfd_link_hash_undefined;
1114 h->root.u.undef.abfd = abfd;
1116 else
1118 h->root.type = bfd_link_hash_new;
1119 h->root.u.undef.abfd = NULL;
1122 if (h->def_dynamic)
1124 h->def_dynamic = 0;
1125 h->ref_dynamic = 1;
1126 h->dynamic_def = 1;
1128 /* FIXME: Should we check type and size for protected symbol? */
1129 h->size = 0;
1130 h->type = 0;
1131 return TRUE;
1134 /* Differentiate strong and weak symbols. */
1135 newweak = bind == STB_WEAK;
1136 oldweak = (h->root.type == bfd_link_hash_defweak
1137 || h->root.type == bfd_link_hash_undefweak);
1139 /* If a new weak symbol definition comes from a regular file and the
1140 old symbol comes from a dynamic library, we treat the new one as
1141 strong. Similarly, an old weak symbol definition from a regular
1142 file is treated as strong when the new symbol comes from a dynamic
1143 library. Further, an old weak symbol from a dynamic library is
1144 treated as strong if the new symbol is from a dynamic library.
1145 This reflects the way glibc's ld.so works.
1147 Do this before setting *type_change_ok or *size_change_ok so that
1148 we warn properly when dynamic library symbols are overridden. */
1150 if (newdef && !newdyn && olddyn)
1151 newweak = FALSE;
1152 if (olddef && newdyn)
1153 oldweak = FALSE;
1155 /* It's OK to change the type if either the existing symbol or the
1156 new symbol is weak. A type change is also OK if the old symbol
1157 is undefined and the new symbol is defined. */
1159 if (oldweak
1160 || newweak
1161 || (newdef
1162 && h->root.type == bfd_link_hash_undefined))
1163 *type_change_ok = TRUE;
1165 /* It's OK to change the size if either the existing symbol or the
1166 new symbol is weak, or if the old symbol is undefined. */
1168 if (*type_change_ok
1169 || h->root.type == bfd_link_hash_undefined)
1170 *size_change_ok = TRUE;
1172 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1173 symbol, respectively, appears to be a common symbol in a dynamic
1174 object. If a symbol appears in an uninitialized section, and is
1175 not weak, and is not a function, then it may be a common symbol
1176 which was resolved when the dynamic object was created. We want
1177 to treat such symbols specially, because they raise special
1178 considerations when setting the symbol size: if the symbol
1179 appears as a common symbol in a regular object, and the size in
1180 the regular object is larger, we must make sure that we use the
1181 larger size. This problematic case can always be avoided in C,
1182 but it must be handled correctly when using Fortran shared
1183 libraries.
1185 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1186 likewise for OLDDYNCOMMON and OLDDEF.
1188 Note that this test is just a heuristic, and that it is quite
1189 possible to have an uninitialized symbol in a shared object which
1190 is really a definition, rather than a common symbol. This could
1191 lead to some minor confusion when the symbol really is a common
1192 symbol in some regular object. However, I think it will be
1193 harmless. */
1195 if (newdyn
1196 && newdef
1197 && !newweak
1198 && (sec->flags & SEC_ALLOC) != 0
1199 && (sec->flags & SEC_LOAD) == 0
1200 && sym->st_size > 0
1201 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1202 newdyncommon = TRUE;
1203 else
1204 newdyncommon = FALSE;
1206 if (olddyn
1207 && olddef
1208 && h->root.type == bfd_link_hash_defined
1209 && h->def_dynamic
1210 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1211 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1212 && h->size > 0
1213 && h->type != STT_FUNC)
1214 olddyncommon = TRUE;
1215 else
1216 olddyncommon = FALSE;
1218 /* We now know everything about the old and new symbols. We ask the
1219 backend to check if we can merge them. */
1220 bed = get_elf_backend_data (abfd);
1221 if (bed->merge_symbol
1222 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1223 pold_alignment, skip, override,
1224 type_change_ok, size_change_ok,
1225 &newdyn, &newdef, &newdyncommon, &newweak,
1226 abfd, &sec,
1227 &olddyn, &olddef, &olddyncommon, &oldweak,
1228 oldbfd, &oldsec))
1229 return FALSE;
1231 /* If both the old and the new symbols look like common symbols in a
1232 dynamic object, set the size of the symbol to the larger of the
1233 two. */
1235 if (olddyncommon
1236 && newdyncommon
1237 && sym->st_size != h->size)
1239 /* Since we think we have two common symbols, issue a multiple
1240 common warning if desired. Note that we only warn if the
1241 size is different. If the size is the same, we simply let
1242 the old symbol override the new one as normally happens with
1243 symbols defined in dynamic objects. */
1245 if (! ((*info->callbacks->multiple_common)
1246 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1247 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1248 return FALSE;
1250 if (sym->st_size > h->size)
1251 h->size = sym->st_size;
1253 *size_change_ok = TRUE;
1256 /* If we are looking at a dynamic object, and we have found a
1257 definition, we need to see if the symbol was already defined by
1258 some other object. If so, we want to use the existing
1259 definition, and we do not want to report a multiple symbol
1260 definition error; we do this by clobbering *PSEC to be
1261 bfd_und_section_ptr.
1263 We treat a common symbol as a definition if the symbol in the
1264 shared library is a function, since common symbols always
1265 represent variables; this can cause confusion in principle, but
1266 any such confusion would seem to indicate an erroneous program or
1267 shared library. We also permit a common symbol in a regular
1268 object to override a weak symbol in a shared object. */
1270 if (newdyn
1271 && newdef
1272 && (olddef
1273 || (h->root.type == bfd_link_hash_common
1274 && (newweak
1275 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1277 *override = TRUE;
1278 newdef = FALSE;
1279 newdyncommon = FALSE;
1281 *psec = sec = bfd_und_section_ptr;
1282 *size_change_ok = TRUE;
1284 /* If we get here when the old symbol is a common symbol, then
1285 we are explicitly letting it override a weak symbol or
1286 function in a dynamic object, and we don't want to warn about
1287 a type change. If the old symbol is a defined symbol, a type
1288 change warning may still be appropriate. */
1290 if (h->root.type == bfd_link_hash_common)
1291 *type_change_ok = TRUE;
1294 /* Handle the special case of an old common symbol merging with a
1295 new symbol which looks like a common symbol in a shared object.
1296 We change *PSEC and *PVALUE to make the new symbol look like a
1297 common symbol, and let _bfd_generic_link_add_one_symbol do the
1298 right thing. */
1300 if (newdyncommon
1301 && h->root.type == bfd_link_hash_common)
1303 *override = TRUE;
1304 newdef = FALSE;
1305 newdyncommon = FALSE;
1306 *pvalue = sym->st_size;
1307 *psec = sec = bed->common_section (oldsec);
1308 *size_change_ok = TRUE;
1311 /* Skip weak definitions of symbols that are already defined. */
1312 if (newdef && olddef && newweak)
1313 *skip = TRUE;
1315 /* If the old symbol is from a dynamic object, and the new symbol is
1316 a definition which is not from a dynamic object, then the new
1317 symbol overrides the old symbol. Symbols from regular files
1318 always take precedence over symbols from dynamic objects, even if
1319 they are defined after the dynamic object in the link.
1321 As above, we again permit a common symbol in a regular object to
1322 override a definition in a shared object if the shared object
1323 symbol is a function or is weak. */
1325 flip = NULL;
1326 if (!newdyn
1327 && (newdef
1328 || (bfd_is_com_section (sec)
1329 && (oldweak
1330 || h->type == STT_FUNC)))
1331 && olddyn
1332 && olddef
1333 && h->def_dynamic)
1335 /* Change the hash table entry to undefined, and let
1336 _bfd_generic_link_add_one_symbol do the right thing with the
1337 new definition. */
1339 h->root.type = bfd_link_hash_undefined;
1340 h->root.u.undef.abfd = h->root.u.def.section->owner;
1341 *size_change_ok = TRUE;
1343 olddef = FALSE;
1344 olddyncommon = FALSE;
1346 /* We again permit a type change when a common symbol may be
1347 overriding a function. */
1349 if (bfd_is_com_section (sec))
1350 *type_change_ok = TRUE;
1352 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1353 flip = *sym_hash;
1354 else
1355 /* This union may have been set to be non-NULL when this symbol
1356 was seen in a dynamic object. We must force the union to be
1357 NULL, so that it is correct for a regular symbol. */
1358 h->verinfo.vertree = NULL;
1361 /* Handle the special case of a new common symbol merging with an
1362 old symbol that looks like it might be a common symbol defined in
1363 a shared object. Note that we have already handled the case in
1364 which a new common symbol should simply override the definition
1365 in the shared library. */
1367 if (! newdyn
1368 && bfd_is_com_section (sec)
1369 && olddyncommon)
1371 /* It would be best if we could set the hash table entry to a
1372 common symbol, but we don't know what to use for the section
1373 or the alignment. */
1374 if (! ((*info->callbacks->multiple_common)
1375 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1376 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1377 return FALSE;
1379 /* If the presumed common symbol in the dynamic object is
1380 larger, pretend that the new symbol has its size. */
1382 if (h->size > *pvalue)
1383 *pvalue = h->size;
1385 /* We need to remember the alignment required by the symbol
1386 in the dynamic object. */
1387 BFD_ASSERT (pold_alignment);
1388 *pold_alignment = h->root.u.def.section->alignment_power;
1390 olddef = FALSE;
1391 olddyncommon = FALSE;
1393 h->root.type = bfd_link_hash_undefined;
1394 h->root.u.undef.abfd = h->root.u.def.section->owner;
1396 *size_change_ok = TRUE;
1397 *type_change_ok = TRUE;
1399 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1400 flip = *sym_hash;
1401 else
1402 h->verinfo.vertree = NULL;
1405 if (flip != NULL)
1407 /* Handle the case where we had a versioned symbol in a dynamic
1408 library and now find a definition in a normal object. In this
1409 case, we make the versioned symbol point to the normal one. */
1410 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1411 flip->root.type = h->root.type;
1412 h->root.type = bfd_link_hash_indirect;
1413 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1414 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1415 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1416 if (h->def_dynamic)
1418 h->def_dynamic = 0;
1419 flip->ref_dynamic = 1;
1423 return TRUE;
1426 /* This function is called to create an indirect symbol from the
1427 default for the symbol with the default version if needed. The
1428 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1429 set DYNSYM if the new indirect symbol is dynamic. */
1431 bfd_boolean
1432 _bfd_elf_add_default_symbol (bfd *abfd,
1433 struct bfd_link_info *info,
1434 struct elf_link_hash_entry *h,
1435 const char *name,
1436 Elf_Internal_Sym *sym,
1437 asection **psec,
1438 bfd_vma *value,
1439 bfd_boolean *dynsym,
1440 bfd_boolean override)
1442 bfd_boolean type_change_ok;
1443 bfd_boolean size_change_ok;
1444 bfd_boolean skip;
1445 char *shortname;
1446 struct elf_link_hash_entry *hi;
1447 struct bfd_link_hash_entry *bh;
1448 const struct elf_backend_data *bed;
1449 bfd_boolean collect;
1450 bfd_boolean dynamic;
1451 char *p;
1452 size_t len, shortlen;
1453 asection *sec;
1455 /* If this symbol has a version, and it is the default version, we
1456 create an indirect symbol from the default name to the fully
1457 decorated name. This will cause external references which do not
1458 specify a version to be bound to this version of the symbol. */
1459 p = strchr (name, ELF_VER_CHR);
1460 if (p == NULL || p[1] != ELF_VER_CHR)
1461 return TRUE;
1463 if (override)
1465 /* We are overridden by an old definition. We need to check if we
1466 need to create the indirect symbol from the default name. */
1467 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1468 FALSE, FALSE);
1469 BFD_ASSERT (hi != NULL);
1470 if (hi == h)
1471 return TRUE;
1472 while (hi->root.type == bfd_link_hash_indirect
1473 || hi->root.type == bfd_link_hash_warning)
1475 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1476 if (hi == h)
1477 return TRUE;
1481 bed = get_elf_backend_data (abfd);
1482 collect = bed->collect;
1483 dynamic = (abfd->flags & DYNAMIC) != 0;
1485 shortlen = p - name;
1486 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1487 if (shortname == NULL)
1488 return FALSE;
1489 memcpy (shortname, name, shortlen);
1490 shortname[shortlen] = '\0';
1492 /* We are going to create a new symbol. Merge it with any existing
1493 symbol with this name. For the purposes of the merge, act as
1494 though we were defining the symbol we just defined, although we
1495 actually going to define an indirect symbol. */
1496 type_change_ok = FALSE;
1497 size_change_ok = FALSE;
1498 sec = *psec;
1499 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1500 NULL, &hi, &skip, &override,
1501 &type_change_ok, &size_change_ok))
1502 return FALSE;
1504 if (skip)
1505 goto nondefault;
1507 if (! override)
1509 bh = &hi->root;
1510 if (! (_bfd_generic_link_add_one_symbol
1511 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1512 0, name, FALSE, collect, &bh)))
1513 return FALSE;
1514 hi = (struct elf_link_hash_entry *) bh;
1516 else
1518 /* In this case the symbol named SHORTNAME is overriding the
1519 indirect symbol we want to add. We were planning on making
1520 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1521 is the name without a version. NAME is the fully versioned
1522 name, and it is the default version.
1524 Overriding means that we already saw a definition for the
1525 symbol SHORTNAME in a regular object, and it is overriding
1526 the symbol defined in the dynamic object.
1528 When this happens, we actually want to change NAME, the
1529 symbol we just added, to refer to SHORTNAME. This will cause
1530 references to NAME in the shared object to become references
1531 to SHORTNAME in the regular object. This is what we expect
1532 when we override a function in a shared object: that the
1533 references in the shared object will be mapped to the
1534 definition in the regular object. */
1536 while (hi->root.type == bfd_link_hash_indirect
1537 || hi->root.type == bfd_link_hash_warning)
1538 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1540 h->root.type = bfd_link_hash_indirect;
1541 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1542 if (h->def_dynamic)
1544 h->def_dynamic = 0;
1545 hi->ref_dynamic = 1;
1546 if (hi->ref_regular
1547 || hi->def_regular)
1549 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1550 return FALSE;
1554 /* Now set HI to H, so that the following code will set the
1555 other fields correctly. */
1556 hi = h;
1559 /* Check if HI is a warning symbol. */
1560 if (hi->root.type == bfd_link_hash_warning)
1561 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1563 /* If there is a duplicate definition somewhere, then HI may not
1564 point to an indirect symbol. We will have reported an error to
1565 the user in that case. */
1567 if (hi->root.type == bfd_link_hash_indirect)
1569 struct elf_link_hash_entry *ht;
1571 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1572 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1574 /* See if the new flags lead us to realize that the symbol must
1575 be dynamic. */
1576 if (! *dynsym)
1578 if (! dynamic)
1580 if (info->shared
1581 || hi->ref_dynamic)
1582 *dynsym = TRUE;
1584 else
1586 if (hi->ref_regular)
1587 *dynsym = TRUE;
1592 /* We also need to define an indirection from the nondefault version
1593 of the symbol. */
1595 nondefault:
1596 len = strlen (name);
1597 shortname = bfd_hash_allocate (&info->hash->table, len);
1598 if (shortname == NULL)
1599 return FALSE;
1600 memcpy (shortname, name, shortlen);
1601 memcpy (shortname + shortlen, p + 1, len - shortlen);
1603 /* Once again, merge with any existing symbol. */
1604 type_change_ok = FALSE;
1605 size_change_ok = FALSE;
1606 sec = *psec;
1607 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1608 NULL, &hi, &skip, &override,
1609 &type_change_ok, &size_change_ok))
1610 return FALSE;
1612 if (skip)
1613 return TRUE;
1615 if (override)
1617 /* Here SHORTNAME is a versioned name, so we don't expect to see
1618 the type of override we do in the case above unless it is
1619 overridden by a versioned definition. */
1620 if (hi->root.type != bfd_link_hash_defined
1621 && hi->root.type != bfd_link_hash_defweak)
1622 (*_bfd_error_handler)
1623 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1624 abfd, shortname);
1626 else
1628 bh = &hi->root;
1629 if (! (_bfd_generic_link_add_one_symbol
1630 (info, abfd, shortname, BSF_INDIRECT,
1631 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1632 return FALSE;
1633 hi = (struct elf_link_hash_entry *) bh;
1635 /* If there is a duplicate definition somewhere, then HI may not
1636 point to an indirect symbol. We will have reported an error
1637 to the user in that case. */
1639 if (hi->root.type == bfd_link_hash_indirect)
1641 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1643 /* See if the new flags lead us to realize that the symbol
1644 must be dynamic. */
1645 if (! *dynsym)
1647 if (! dynamic)
1649 if (info->shared
1650 || hi->ref_dynamic)
1651 *dynsym = TRUE;
1653 else
1655 if (hi->ref_regular)
1656 *dynsym = TRUE;
1662 return TRUE;
1665 /* This routine is used to export all defined symbols into the dynamic
1666 symbol table. It is called via elf_link_hash_traverse. */
1668 bfd_boolean
1669 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1671 struct elf_info_failed *eif = data;
1673 /* Ignore this if we won't export it. */
1674 if (!eif->info->export_dynamic && !h->dynamic)
1675 return TRUE;
1677 /* Ignore indirect symbols. These are added by the versioning code. */
1678 if (h->root.type == bfd_link_hash_indirect)
1679 return TRUE;
1681 if (h->root.type == bfd_link_hash_warning)
1682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1684 if (h->dynindx == -1
1685 && (h->def_regular
1686 || h->ref_regular))
1688 struct bfd_elf_version_tree *t;
1689 struct bfd_elf_version_expr *d;
1691 for (t = eif->verdefs; t != NULL; t = t->next)
1693 if (t->globals.list != NULL)
1695 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1696 if (d != NULL)
1697 goto doit;
1700 if (t->locals.list != NULL)
1702 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1703 if (d != NULL)
1704 return TRUE;
1708 if (!eif->verdefs)
1710 doit:
1711 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1713 eif->failed = TRUE;
1714 return FALSE;
1719 return TRUE;
1722 /* Look through the symbols which are defined in other shared
1723 libraries and referenced here. Update the list of version
1724 dependencies. This will be put into the .gnu.version_r section.
1725 This function is called via elf_link_hash_traverse. */
1727 bfd_boolean
1728 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1729 void *data)
1731 struct elf_find_verdep_info *rinfo = data;
1732 Elf_Internal_Verneed *t;
1733 Elf_Internal_Vernaux *a;
1734 bfd_size_type amt;
1736 if (h->root.type == bfd_link_hash_warning)
1737 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1739 /* We only care about symbols defined in shared objects with version
1740 information. */
1741 if (!h->def_dynamic
1742 || h->def_regular
1743 || h->dynindx == -1
1744 || h->verinfo.verdef == NULL)
1745 return TRUE;
1747 /* See if we already know about this version. */
1748 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1750 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1751 continue;
1753 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1754 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1755 return TRUE;
1757 break;
1760 /* This is a new version. Add it to tree we are building. */
1762 if (t == NULL)
1764 amt = sizeof *t;
1765 t = bfd_zalloc (rinfo->output_bfd, amt);
1766 if (t == NULL)
1768 rinfo->failed = TRUE;
1769 return FALSE;
1772 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1773 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1774 elf_tdata (rinfo->output_bfd)->verref = t;
1777 amt = sizeof *a;
1778 a = bfd_zalloc (rinfo->output_bfd, amt);
1780 /* Note that we are copying a string pointer here, and testing it
1781 above. If bfd_elf_string_from_elf_section is ever changed to
1782 discard the string data when low in memory, this will have to be
1783 fixed. */
1784 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1786 a->vna_flags = h->verinfo.verdef->vd_flags;
1787 a->vna_nextptr = t->vn_auxptr;
1789 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1790 ++rinfo->vers;
1792 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1794 t->vn_auxptr = a;
1796 return TRUE;
1799 /* Figure out appropriate versions for all the symbols. We may not
1800 have the version number script until we have read all of the input
1801 files, so until that point we don't know which symbols should be
1802 local. This function is called via elf_link_hash_traverse. */
1804 bfd_boolean
1805 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1807 struct elf_assign_sym_version_info *sinfo;
1808 struct bfd_link_info *info;
1809 const struct elf_backend_data *bed;
1810 struct elf_info_failed eif;
1811 char *p;
1812 bfd_size_type amt;
1814 sinfo = data;
1815 info = sinfo->info;
1817 if (h->root.type == bfd_link_hash_warning)
1818 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1820 /* Fix the symbol flags. */
1821 eif.failed = FALSE;
1822 eif.info = info;
1823 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1825 if (eif.failed)
1826 sinfo->failed = TRUE;
1827 return FALSE;
1830 /* We only need version numbers for symbols defined in regular
1831 objects. */
1832 if (!h->def_regular)
1833 return TRUE;
1835 bed = get_elf_backend_data (sinfo->output_bfd);
1836 p = strchr (h->root.root.string, ELF_VER_CHR);
1837 if (p != NULL && h->verinfo.vertree == NULL)
1839 struct bfd_elf_version_tree *t;
1840 bfd_boolean hidden;
1842 hidden = TRUE;
1844 /* There are two consecutive ELF_VER_CHR characters if this is
1845 not a hidden symbol. */
1846 ++p;
1847 if (*p == ELF_VER_CHR)
1849 hidden = FALSE;
1850 ++p;
1853 /* If there is no version string, we can just return out. */
1854 if (*p == '\0')
1856 if (hidden)
1857 h->hidden = 1;
1858 return TRUE;
1861 /* Look for the version. If we find it, it is no longer weak. */
1862 for (t = sinfo->verdefs; t != NULL; t = t->next)
1864 if (strcmp (t->name, p) == 0)
1866 size_t len;
1867 char *alc;
1868 struct bfd_elf_version_expr *d;
1870 len = p - h->root.root.string;
1871 alc = bfd_malloc (len);
1872 if (alc == NULL)
1873 return FALSE;
1874 memcpy (alc, h->root.root.string, len - 1);
1875 alc[len - 1] = '\0';
1876 if (alc[len - 2] == ELF_VER_CHR)
1877 alc[len - 2] = '\0';
1879 h->verinfo.vertree = t;
1880 t->used = TRUE;
1881 d = NULL;
1883 if (t->globals.list != NULL)
1884 d = (*t->match) (&t->globals, NULL, alc);
1886 /* See if there is anything to force this symbol to
1887 local scope. */
1888 if (d == NULL && t->locals.list != NULL)
1890 d = (*t->match) (&t->locals, NULL, alc);
1891 if (d != NULL
1892 && h->dynindx != -1
1893 && ! info->export_dynamic)
1894 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1897 free (alc);
1898 break;
1902 /* If we are building an application, we need to create a
1903 version node for this version. */
1904 if (t == NULL && info->executable)
1906 struct bfd_elf_version_tree **pp;
1907 int version_index;
1909 /* If we aren't going to export this symbol, we don't need
1910 to worry about it. */
1911 if (h->dynindx == -1)
1912 return TRUE;
1914 amt = sizeof *t;
1915 t = bfd_zalloc (sinfo->output_bfd, amt);
1916 if (t == NULL)
1918 sinfo->failed = TRUE;
1919 return FALSE;
1922 t->name = p;
1923 t->name_indx = (unsigned int) -1;
1924 t->used = TRUE;
1926 version_index = 1;
1927 /* Don't count anonymous version tag. */
1928 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1929 version_index = 0;
1930 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1931 ++version_index;
1932 t->vernum = version_index;
1934 *pp = t;
1936 h->verinfo.vertree = t;
1938 else if (t == NULL)
1940 /* We could not find the version for a symbol when
1941 generating a shared archive. Return an error. */
1942 (*_bfd_error_handler)
1943 (_("%B: undefined versioned symbol name %s"),
1944 sinfo->output_bfd, h->root.root.string);
1945 bfd_set_error (bfd_error_bad_value);
1946 sinfo->failed = TRUE;
1947 return FALSE;
1950 if (hidden)
1951 h->hidden = 1;
1954 /* If we don't have a version for this symbol, see if we can find
1955 something. */
1956 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1958 struct bfd_elf_version_tree *t;
1959 struct bfd_elf_version_tree *local_ver;
1960 struct bfd_elf_version_expr *d;
1962 /* See if can find what version this symbol is in. If the
1963 symbol is supposed to be local, then don't actually register
1964 it. */
1965 local_ver = NULL;
1966 for (t = sinfo->verdefs; t != NULL; t = t->next)
1968 if (t->globals.list != NULL)
1970 bfd_boolean matched;
1972 matched = FALSE;
1973 d = NULL;
1974 while ((d = (*t->match) (&t->globals, d,
1975 h->root.root.string)) != NULL)
1976 if (d->symver)
1977 matched = TRUE;
1978 else
1980 /* There is a version without definition. Make
1981 the symbol the default definition for this
1982 version. */
1983 h->verinfo.vertree = t;
1984 local_ver = NULL;
1985 d->script = 1;
1986 break;
1988 if (d != NULL)
1989 break;
1990 else if (matched)
1991 /* There is no undefined version for this symbol. Hide the
1992 default one. */
1993 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1996 if (t->locals.list != NULL)
1998 d = NULL;
1999 while ((d = (*t->match) (&t->locals, d,
2000 h->root.root.string)) != NULL)
2002 local_ver = t;
2003 /* If the match is "*", keep looking for a more
2004 explicit, perhaps even global, match.
2005 XXX: Shouldn't this be !d->wildcard instead? */
2006 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2007 break;
2010 if (d != NULL)
2011 break;
2015 if (local_ver != NULL)
2017 h->verinfo.vertree = local_ver;
2018 if (h->dynindx != -1
2019 && ! info->export_dynamic)
2021 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2026 return TRUE;
2029 /* Read and swap the relocs from the section indicated by SHDR. This
2030 may be either a REL or a RELA section. The relocations are
2031 translated into RELA relocations and stored in INTERNAL_RELOCS,
2032 which should have already been allocated to contain enough space.
2033 The EXTERNAL_RELOCS are a buffer where the external form of the
2034 relocations should be stored.
2036 Returns FALSE if something goes wrong. */
2038 static bfd_boolean
2039 elf_link_read_relocs_from_section (bfd *abfd,
2040 asection *sec,
2041 Elf_Internal_Shdr *shdr,
2042 void *external_relocs,
2043 Elf_Internal_Rela *internal_relocs)
2045 const struct elf_backend_data *bed;
2046 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2047 const bfd_byte *erela;
2048 const bfd_byte *erelaend;
2049 Elf_Internal_Rela *irela;
2050 Elf_Internal_Shdr *symtab_hdr;
2051 size_t nsyms;
2053 /* Position ourselves at the start of the section. */
2054 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2055 return FALSE;
2057 /* Read the relocations. */
2058 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2059 return FALSE;
2061 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2062 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2064 bed = get_elf_backend_data (abfd);
2066 /* Convert the external relocations to the internal format. */
2067 if (shdr->sh_entsize == bed->s->sizeof_rel)
2068 swap_in = bed->s->swap_reloc_in;
2069 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2070 swap_in = bed->s->swap_reloca_in;
2071 else
2073 bfd_set_error (bfd_error_wrong_format);
2074 return FALSE;
2077 erela = external_relocs;
2078 erelaend = erela + shdr->sh_size;
2079 irela = internal_relocs;
2080 while (erela < erelaend)
2082 bfd_vma r_symndx;
2084 (*swap_in) (abfd, erela, irela);
2085 r_symndx = ELF32_R_SYM (irela->r_info);
2086 if (bed->s->arch_size == 64)
2087 r_symndx >>= 24;
2088 if ((size_t) r_symndx >= nsyms)
2090 (*_bfd_error_handler)
2091 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2092 " for offset 0x%lx in section `%A'"),
2093 abfd, sec,
2094 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2095 bfd_set_error (bfd_error_bad_value);
2096 return FALSE;
2098 irela += bed->s->int_rels_per_ext_rel;
2099 erela += shdr->sh_entsize;
2102 return TRUE;
2105 /* Read and swap the relocs for a section O. They may have been
2106 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2107 not NULL, they are used as buffers to read into. They are known to
2108 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2109 the return value is allocated using either malloc or bfd_alloc,
2110 according to the KEEP_MEMORY argument. If O has two relocation
2111 sections (both REL and RELA relocations), then the REL_HDR
2112 relocations will appear first in INTERNAL_RELOCS, followed by the
2113 REL_HDR2 relocations. */
2115 Elf_Internal_Rela *
2116 _bfd_elf_link_read_relocs (bfd *abfd,
2117 asection *o,
2118 void *external_relocs,
2119 Elf_Internal_Rela *internal_relocs,
2120 bfd_boolean keep_memory)
2122 Elf_Internal_Shdr *rel_hdr;
2123 void *alloc1 = NULL;
2124 Elf_Internal_Rela *alloc2 = NULL;
2125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2127 if (elf_section_data (o)->relocs != NULL)
2128 return elf_section_data (o)->relocs;
2130 if (o->reloc_count == 0)
2131 return NULL;
2133 rel_hdr = &elf_section_data (o)->rel_hdr;
2135 if (internal_relocs == NULL)
2137 bfd_size_type size;
2139 size = o->reloc_count;
2140 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2141 if (keep_memory)
2142 internal_relocs = bfd_alloc (abfd, size);
2143 else
2144 internal_relocs = alloc2 = bfd_malloc (size);
2145 if (internal_relocs == NULL)
2146 goto error_return;
2149 if (external_relocs == NULL)
2151 bfd_size_type size = rel_hdr->sh_size;
2153 if (elf_section_data (o)->rel_hdr2)
2154 size += elf_section_data (o)->rel_hdr2->sh_size;
2155 alloc1 = bfd_malloc (size);
2156 if (alloc1 == NULL)
2157 goto error_return;
2158 external_relocs = alloc1;
2161 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2162 external_relocs,
2163 internal_relocs))
2164 goto error_return;
2165 if (elf_section_data (o)->rel_hdr2
2166 && (!elf_link_read_relocs_from_section
2167 (abfd, o,
2168 elf_section_data (o)->rel_hdr2,
2169 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2170 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2171 * bed->s->int_rels_per_ext_rel))))
2172 goto error_return;
2174 /* Cache the results for next time, if we can. */
2175 if (keep_memory)
2176 elf_section_data (o)->relocs = internal_relocs;
2178 if (alloc1 != NULL)
2179 free (alloc1);
2181 /* Don't free alloc2, since if it was allocated we are passing it
2182 back (under the name of internal_relocs). */
2184 return internal_relocs;
2186 error_return:
2187 if (alloc1 != NULL)
2188 free (alloc1);
2189 if (alloc2 != NULL)
2190 free (alloc2);
2191 return NULL;
2194 /* Compute the size of, and allocate space for, REL_HDR which is the
2195 section header for a section containing relocations for O. */
2197 bfd_boolean
2198 _bfd_elf_link_size_reloc_section (bfd *abfd,
2199 Elf_Internal_Shdr *rel_hdr,
2200 asection *o)
2202 bfd_size_type reloc_count;
2203 bfd_size_type num_rel_hashes;
2205 /* Figure out how many relocations there will be. */
2206 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2207 reloc_count = elf_section_data (o)->rel_count;
2208 else
2209 reloc_count = elf_section_data (o)->rel_count2;
2211 num_rel_hashes = o->reloc_count;
2212 if (num_rel_hashes < reloc_count)
2213 num_rel_hashes = reloc_count;
2215 /* That allows us to calculate the size of the section. */
2216 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2218 /* The contents field must last into write_object_contents, so we
2219 allocate it with bfd_alloc rather than malloc. Also since we
2220 cannot be sure that the contents will actually be filled in,
2221 we zero the allocated space. */
2222 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2223 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2224 return FALSE;
2226 /* We only allocate one set of hash entries, so we only do it the
2227 first time we are called. */
2228 if (elf_section_data (o)->rel_hashes == NULL
2229 && num_rel_hashes)
2231 struct elf_link_hash_entry **p;
2233 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2234 if (p == NULL)
2235 return FALSE;
2237 elf_section_data (o)->rel_hashes = p;
2240 return TRUE;
2243 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2244 originated from the section given by INPUT_REL_HDR) to the
2245 OUTPUT_BFD. */
2247 bfd_boolean
2248 _bfd_elf_link_output_relocs (bfd *output_bfd,
2249 asection *input_section,
2250 Elf_Internal_Shdr *input_rel_hdr,
2251 Elf_Internal_Rela *internal_relocs,
2252 struct elf_link_hash_entry **rel_hash
2253 ATTRIBUTE_UNUSED)
2255 Elf_Internal_Rela *irela;
2256 Elf_Internal_Rela *irelaend;
2257 bfd_byte *erel;
2258 Elf_Internal_Shdr *output_rel_hdr;
2259 asection *output_section;
2260 unsigned int *rel_countp = NULL;
2261 const struct elf_backend_data *bed;
2262 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2264 output_section = input_section->output_section;
2265 output_rel_hdr = NULL;
2267 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2268 == input_rel_hdr->sh_entsize)
2270 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2271 rel_countp = &elf_section_data (output_section)->rel_count;
2273 else if (elf_section_data (output_section)->rel_hdr2
2274 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2275 == input_rel_hdr->sh_entsize))
2277 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2278 rel_countp = &elf_section_data (output_section)->rel_count2;
2280 else
2282 (*_bfd_error_handler)
2283 (_("%B: relocation size mismatch in %B section %A"),
2284 output_bfd, input_section->owner, input_section);
2285 bfd_set_error (bfd_error_wrong_object_format);
2286 return FALSE;
2289 bed = get_elf_backend_data (output_bfd);
2290 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2291 swap_out = bed->s->swap_reloc_out;
2292 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2293 swap_out = bed->s->swap_reloca_out;
2294 else
2295 abort ();
2297 erel = output_rel_hdr->contents;
2298 erel += *rel_countp * input_rel_hdr->sh_entsize;
2299 irela = internal_relocs;
2300 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2301 * bed->s->int_rels_per_ext_rel);
2302 while (irela < irelaend)
2304 (*swap_out) (output_bfd, irela, erel);
2305 irela += bed->s->int_rels_per_ext_rel;
2306 erel += input_rel_hdr->sh_entsize;
2309 /* Bump the counter, so that we know where to add the next set of
2310 relocations. */
2311 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2313 return TRUE;
2316 /* Make weak undefined symbols in PIE dynamic. */
2318 bfd_boolean
2319 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2320 struct elf_link_hash_entry *h)
2322 if (info->pie
2323 && h->dynindx == -1
2324 && h->root.type == bfd_link_hash_undefweak)
2325 return bfd_elf_link_record_dynamic_symbol (info, h);
2327 return TRUE;
2330 /* Fix up the flags for a symbol. This handles various cases which
2331 can only be fixed after all the input files are seen. This is
2332 currently called by both adjust_dynamic_symbol and
2333 assign_sym_version, which is unnecessary but perhaps more robust in
2334 the face of future changes. */
2336 bfd_boolean
2337 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2338 struct elf_info_failed *eif)
2340 const struct elf_backend_data *bed = NULL;
2342 /* If this symbol was mentioned in a non-ELF file, try to set
2343 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2344 permit a non-ELF file to correctly refer to a symbol defined in
2345 an ELF dynamic object. */
2346 if (h->non_elf)
2348 while (h->root.type == bfd_link_hash_indirect)
2349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2351 if (h->root.type != bfd_link_hash_defined
2352 && h->root.type != bfd_link_hash_defweak)
2354 h->ref_regular = 1;
2355 h->ref_regular_nonweak = 1;
2357 else
2359 if (h->root.u.def.section->owner != NULL
2360 && (bfd_get_flavour (h->root.u.def.section->owner)
2361 == bfd_target_elf_flavour))
2363 h->ref_regular = 1;
2364 h->ref_regular_nonweak = 1;
2366 else
2367 h->def_regular = 1;
2370 if (h->dynindx == -1
2371 && (h->def_dynamic
2372 || h->ref_dynamic))
2374 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2376 eif->failed = TRUE;
2377 return FALSE;
2381 else
2383 /* Unfortunately, NON_ELF is only correct if the symbol
2384 was first seen in a non-ELF file. Fortunately, if the symbol
2385 was first seen in an ELF file, we're probably OK unless the
2386 symbol was defined in a non-ELF file. Catch that case here.
2387 FIXME: We're still in trouble if the symbol was first seen in
2388 a dynamic object, and then later in a non-ELF regular object. */
2389 if ((h->root.type == bfd_link_hash_defined
2390 || h->root.type == bfd_link_hash_defweak)
2391 && !h->def_regular
2392 && (h->root.u.def.section->owner != NULL
2393 ? (bfd_get_flavour (h->root.u.def.section->owner)
2394 != bfd_target_elf_flavour)
2395 : (bfd_is_abs_section (h->root.u.def.section)
2396 && !h->def_dynamic)))
2397 h->def_regular = 1;
2400 /* Backend specific symbol fixup. */
2401 if (elf_hash_table (eif->info)->dynobj)
2403 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2404 if (bed->elf_backend_fixup_symbol
2405 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2406 return FALSE;
2409 /* If this is a final link, and the symbol was defined as a common
2410 symbol in a regular object file, and there was no definition in
2411 any dynamic object, then the linker will have allocated space for
2412 the symbol in a common section but the DEF_REGULAR
2413 flag will not have been set. */
2414 if (h->root.type == bfd_link_hash_defined
2415 && !h->def_regular
2416 && h->ref_regular
2417 && !h->def_dynamic
2418 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2419 h->def_regular = 1;
2421 /* If -Bsymbolic was used (which means to bind references to global
2422 symbols to the definition within the shared object), and this
2423 symbol was defined in a regular object, then it actually doesn't
2424 need a PLT entry. Likewise, if the symbol has non-default
2425 visibility. If the symbol has hidden or internal visibility, we
2426 will force it local. */
2427 if (h->needs_plt
2428 && eif->info->shared
2429 && is_elf_hash_table (eif->info->hash)
2430 && (SYMBOLIC_BIND (eif->info, h)
2431 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2432 && h->def_regular)
2434 bfd_boolean force_local;
2436 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2437 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2438 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2441 /* If a weak undefined symbol has non-default visibility, we also
2442 hide it from the dynamic linker. */
2443 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2444 && h->root.type == bfd_link_hash_undefweak)
2446 const struct elf_backend_data *bed;
2447 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2448 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2451 /* If this is a weak defined symbol in a dynamic object, and we know
2452 the real definition in the dynamic object, copy interesting flags
2453 over to the real definition. */
2454 if (h->u.weakdef != NULL)
2456 struct elf_link_hash_entry *weakdef;
2458 weakdef = h->u.weakdef;
2459 if (h->root.type == bfd_link_hash_indirect)
2460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2462 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2463 || h->root.type == bfd_link_hash_defweak);
2464 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2465 || weakdef->root.type == bfd_link_hash_defweak);
2466 BFD_ASSERT (weakdef->def_dynamic);
2468 /* If the real definition is defined by a regular object file,
2469 don't do anything special. See the longer description in
2470 _bfd_elf_adjust_dynamic_symbol, below. */
2471 if (weakdef->def_regular)
2472 h->u.weakdef = NULL;
2473 else
2474 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2478 return TRUE;
2481 /* Make the backend pick a good value for a dynamic symbol. This is
2482 called via elf_link_hash_traverse, and also calls itself
2483 recursively. */
2485 bfd_boolean
2486 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2488 struct elf_info_failed *eif = data;
2489 bfd *dynobj;
2490 const struct elf_backend_data *bed;
2492 if (! is_elf_hash_table (eif->info->hash))
2493 return FALSE;
2495 if (h->root.type == bfd_link_hash_warning)
2497 h->got = elf_hash_table (eif->info)->init_got_offset;
2498 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2500 /* When warning symbols are created, they **replace** the "real"
2501 entry in the hash table, thus we never get to see the real
2502 symbol in a hash traversal. So look at it now. */
2503 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2506 /* Ignore indirect symbols. These are added by the versioning code. */
2507 if (h->root.type == bfd_link_hash_indirect)
2508 return TRUE;
2510 /* Fix the symbol flags. */
2511 if (! _bfd_elf_fix_symbol_flags (h, eif))
2512 return FALSE;
2514 /* If this symbol does not require a PLT entry, and it is not
2515 defined by a dynamic object, or is not referenced by a regular
2516 object, ignore it. We do have to handle a weak defined symbol,
2517 even if no regular object refers to it, if we decided to add it
2518 to the dynamic symbol table. FIXME: Do we normally need to worry
2519 about symbols which are defined by one dynamic object and
2520 referenced by another one? */
2521 if (!h->needs_plt
2522 && (h->def_regular
2523 || !h->def_dynamic
2524 || (!h->ref_regular
2525 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2527 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2528 return TRUE;
2531 /* If we've already adjusted this symbol, don't do it again. This
2532 can happen via a recursive call. */
2533 if (h->dynamic_adjusted)
2534 return TRUE;
2536 /* Don't look at this symbol again. Note that we must set this
2537 after checking the above conditions, because we may look at a
2538 symbol once, decide not to do anything, and then get called
2539 recursively later after REF_REGULAR is set below. */
2540 h->dynamic_adjusted = 1;
2542 /* If this is a weak definition, and we know a real definition, and
2543 the real symbol is not itself defined by a regular object file,
2544 then get a good value for the real definition. We handle the
2545 real symbol first, for the convenience of the backend routine.
2547 Note that there is a confusing case here. If the real definition
2548 is defined by a regular object file, we don't get the real symbol
2549 from the dynamic object, but we do get the weak symbol. If the
2550 processor backend uses a COPY reloc, then if some routine in the
2551 dynamic object changes the real symbol, we will not see that
2552 change in the corresponding weak symbol. This is the way other
2553 ELF linkers work as well, and seems to be a result of the shared
2554 library model.
2556 I will clarify this issue. Most SVR4 shared libraries define the
2557 variable _timezone and define timezone as a weak synonym. The
2558 tzset call changes _timezone. If you write
2559 extern int timezone;
2560 int _timezone = 5;
2561 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2562 you might expect that, since timezone is a synonym for _timezone,
2563 the same number will print both times. However, if the processor
2564 backend uses a COPY reloc, then actually timezone will be copied
2565 into your process image, and, since you define _timezone
2566 yourself, _timezone will not. Thus timezone and _timezone will
2567 wind up at different memory locations. The tzset call will set
2568 _timezone, leaving timezone unchanged. */
2570 if (h->u.weakdef != NULL)
2572 /* If we get to this point, we know there is an implicit
2573 reference by a regular object file via the weak symbol H.
2574 FIXME: Is this really true? What if the traversal finds
2575 H->U.WEAKDEF before it finds H? */
2576 h->u.weakdef->ref_regular = 1;
2578 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2579 return FALSE;
2582 /* If a symbol has no type and no size and does not require a PLT
2583 entry, then we are probably about to do the wrong thing here: we
2584 are probably going to create a COPY reloc for an empty object.
2585 This case can arise when a shared object is built with assembly
2586 code, and the assembly code fails to set the symbol type. */
2587 if (h->size == 0
2588 && h->type == STT_NOTYPE
2589 && !h->needs_plt)
2590 (*_bfd_error_handler)
2591 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2592 h->root.root.string);
2594 dynobj = elf_hash_table (eif->info)->dynobj;
2595 bed = get_elf_backend_data (dynobj);
2596 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2598 eif->failed = TRUE;
2599 return FALSE;
2602 return TRUE;
2605 /* Adjust all external symbols pointing into SEC_MERGE sections
2606 to reflect the object merging within the sections. */
2608 bfd_boolean
2609 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2611 asection *sec;
2613 if (h->root.type == bfd_link_hash_warning)
2614 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2616 if ((h->root.type == bfd_link_hash_defined
2617 || h->root.type == bfd_link_hash_defweak)
2618 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2619 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2621 bfd *output_bfd = data;
2623 h->root.u.def.value =
2624 _bfd_merged_section_offset (output_bfd,
2625 &h->root.u.def.section,
2626 elf_section_data (sec)->sec_info,
2627 h->root.u.def.value);
2630 return TRUE;
2633 /* Returns false if the symbol referred to by H should be considered
2634 to resolve local to the current module, and true if it should be
2635 considered to bind dynamically. */
2637 bfd_boolean
2638 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2639 struct bfd_link_info *info,
2640 bfd_boolean ignore_protected)
2642 bfd_boolean binding_stays_local_p;
2644 if (h == NULL)
2645 return FALSE;
2647 while (h->root.type == bfd_link_hash_indirect
2648 || h->root.type == bfd_link_hash_warning)
2649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2651 /* If it was forced local, then clearly it's not dynamic. */
2652 if (h->dynindx == -1)
2653 return FALSE;
2654 if (h->forced_local)
2655 return FALSE;
2657 /* Identify the cases where name binding rules say that a
2658 visible symbol resolves locally. */
2659 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2661 switch (ELF_ST_VISIBILITY (h->other))
2663 case STV_INTERNAL:
2664 case STV_HIDDEN:
2665 return FALSE;
2667 case STV_PROTECTED:
2668 /* Proper resolution for function pointer equality may require
2669 that these symbols perhaps be resolved dynamically, even though
2670 we should be resolving them to the current module. */
2671 if (!ignore_protected || h->type != STT_FUNC)
2672 binding_stays_local_p = TRUE;
2673 break;
2675 default:
2676 break;
2679 /* If it isn't defined locally, then clearly it's dynamic. */
2680 if (!h->def_regular)
2681 return TRUE;
2683 /* Otherwise, the symbol is dynamic if binding rules don't tell
2684 us that it remains local. */
2685 return !binding_stays_local_p;
2688 /* Return true if the symbol referred to by H should be considered
2689 to resolve local to the current module, and false otherwise. Differs
2690 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2691 undefined symbols and weak symbols. */
2693 bfd_boolean
2694 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2695 struct bfd_link_info *info,
2696 bfd_boolean local_protected)
2698 /* If it's a local sym, of course we resolve locally. */
2699 if (h == NULL)
2700 return TRUE;
2702 /* Common symbols that become definitions don't get the DEF_REGULAR
2703 flag set, so test it first, and don't bail out. */
2704 if (ELF_COMMON_DEF_P (h))
2705 /* Do nothing. */;
2706 /* If we don't have a definition in a regular file, then we can't
2707 resolve locally. The sym is either undefined or dynamic. */
2708 else if (!h->def_regular)
2709 return FALSE;
2711 /* Forced local symbols resolve locally. */
2712 if (h->forced_local)
2713 return TRUE;
2715 /* As do non-dynamic symbols. */
2716 if (h->dynindx == -1)
2717 return TRUE;
2719 /* At this point, we know the symbol is defined and dynamic. In an
2720 executable it must resolve locally, likewise when building symbolic
2721 shared libraries. */
2722 if (info->executable || SYMBOLIC_BIND (info, h))
2723 return TRUE;
2725 /* Now deal with defined dynamic symbols in shared libraries. Ones
2726 with default visibility might not resolve locally. */
2727 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2728 return FALSE;
2730 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2731 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2732 return TRUE;
2734 /* STV_PROTECTED non-function symbols are local. */
2735 if (h->type != STT_FUNC)
2736 return TRUE;
2738 /* Function pointer equality tests may require that STV_PROTECTED
2739 symbols be treated as dynamic symbols, even when we know that the
2740 dynamic linker will resolve them locally. */
2741 return local_protected;
2744 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2745 aligned. Returns the first TLS output section. */
2747 struct bfd_section *
2748 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2750 struct bfd_section *sec, *tls;
2751 unsigned int align = 0;
2753 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2754 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2755 break;
2756 tls = sec;
2758 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2759 if (sec->alignment_power > align)
2760 align = sec->alignment_power;
2762 elf_hash_table (info)->tls_sec = tls;
2764 /* Ensure the alignment of the first section is the largest alignment,
2765 so that the tls segment starts aligned. */
2766 if (tls != NULL)
2767 tls->alignment_power = align;
2769 return tls;
2772 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2773 static bfd_boolean
2774 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2775 Elf_Internal_Sym *sym)
2777 const struct elf_backend_data *bed;
2779 /* Local symbols do not count, but target specific ones might. */
2780 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2781 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2782 return FALSE;
2784 /* Function symbols do not count. */
2785 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2786 return FALSE;
2788 /* If the section is undefined, then so is the symbol. */
2789 if (sym->st_shndx == SHN_UNDEF)
2790 return FALSE;
2792 /* If the symbol is defined in the common section, then
2793 it is a common definition and so does not count. */
2794 bed = get_elf_backend_data (abfd);
2795 if (bed->common_definition (sym))
2796 return FALSE;
2798 /* If the symbol is in a target specific section then we
2799 must rely upon the backend to tell us what it is. */
2800 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2801 /* FIXME - this function is not coded yet:
2803 return _bfd_is_global_symbol_definition (abfd, sym);
2805 Instead for now assume that the definition is not global,
2806 Even if this is wrong, at least the linker will behave
2807 in the same way that it used to do. */
2808 return FALSE;
2810 return TRUE;
2813 /* Search the symbol table of the archive element of the archive ABFD
2814 whose archive map contains a mention of SYMDEF, and determine if
2815 the symbol is defined in this element. */
2816 static bfd_boolean
2817 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2819 Elf_Internal_Shdr * hdr;
2820 bfd_size_type symcount;
2821 bfd_size_type extsymcount;
2822 bfd_size_type extsymoff;
2823 Elf_Internal_Sym *isymbuf;
2824 Elf_Internal_Sym *isym;
2825 Elf_Internal_Sym *isymend;
2826 bfd_boolean result;
2828 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2829 if (abfd == NULL)
2830 return FALSE;
2832 if (! bfd_check_format (abfd, bfd_object))
2833 return FALSE;
2835 /* If we have already included the element containing this symbol in the
2836 link then we do not need to include it again. Just claim that any symbol
2837 it contains is not a definition, so that our caller will not decide to
2838 (re)include this element. */
2839 if (abfd->archive_pass)
2840 return FALSE;
2842 /* Select the appropriate symbol table. */
2843 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2844 hdr = &elf_tdata (abfd)->symtab_hdr;
2845 else
2846 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2848 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2850 /* The sh_info field of the symtab header tells us where the
2851 external symbols start. We don't care about the local symbols. */
2852 if (elf_bad_symtab (abfd))
2854 extsymcount = symcount;
2855 extsymoff = 0;
2857 else
2859 extsymcount = symcount - hdr->sh_info;
2860 extsymoff = hdr->sh_info;
2863 if (extsymcount == 0)
2864 return FALSE;
2866 /* Read in the symbol table. */
2867 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2868 NULL, NULL, NULL);
2869 if (isymbuf == NULL)
2870 return FALSE;
2872 /* Scan the symbol table looking for SYMDEF. */
2873 result = FALSE;
2874 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2876 const char *name;
2878 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2879 isym->st_name);
2880 if (name == NULL)
2881 break;
2883 if (strcmp (name, symdef->name) == 0)
2885 result = is_global_data_symbol_definition (abfd, isym);
2886 break;
2890 free (isymbuf);
2892 return result;
2895 /* Add an entry to the .dynamic table. */
2897 bfd_boolean
2898 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2899 bfd_vma tag,
2900 bfd_vma val)
2902 struct elf_link_hash_table *hash_table;
2903 const struct elf_backend_data *bed;
2904 asection *s;
2905 bfd_size_type newsize;
2906 bfd_byte *newcontents;
2907 Elf_Internal_Dyn dyn;
2909 hash_table = elf_hash_table (info);
2910 if (! is_elf_hash_table (hash_table))
2911 return FALSE;
2913 bed = get_elf_backend_data (hash_table->dynobj);
2914 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2915 BFD_ASSERT (s != NULL);
2917 newsize = s->size + bed->s->sizeof_dyn;
2918 newcontents = bfd_realloc (s->contents, newsize);
2919 if (newcontents == NULL)
2920 return FALSE;
2922 dyn.d_tag = tag;
2923 dyn.d_un.d_val = val;
2924 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2926 s->size = newsize;
2927 s->contents = newcontents;
2929 return TRUE;
2932 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2933 otherwise just check whether one already exists. Returns -1 on error,
2934 1 if a DT_NEEDED tag already exists, and 0 on success. */
2936 static int
2937 elf_add_dt_needed_tag (bfd *abfd,
2938 struct bfd_link_info *info,
2939 const char *soname,
2940 bfd_boolean do_it)
2942 struct elf_link_hash_table *hash_table;
2943 bfd_size_type oldsize;
2944 bfd_size_type strindex;
2946 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2947 return -1;
2949 hash_table = elf_hash_table (info);
2950 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2951 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2952 if (strindex == (bfd_size_type) -1)
2953 return -1;
2955 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2957 asection *sdyn;
2958 const struct elf_backend_data *bed;
2959 bfd_byte *extdyn;
2961 bed = get_elf_backend_data (hash_table->dynobj);
2962 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2963 if (sdyn != NULL)
2964 for (extdyn = sdyn->contents;
2965 extdyn < sdyn->contents + sdyn->size;
2966 extdyn += bed->s->sizeof_dyn)
2968 Elf_Internal_Dyn dyn;
2970 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2971 if (dyn.d_tag == DT_NEEDED
2972 && dyn.d_un.d_val == strindex)
2974 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2975 return 1;
2980 if (do_it)
2982 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2983 return -1;
2985 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2986 return -1;
2988 else
2989 /* We were just checking for existence of the tag. */
2990 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2992 return 0;
2995 /* Sort symbol by value and section. */
2996 static int
2997 elf_sort_symbol (const void *arg1, const void *arg2)
2999 const struct elf_link_hash_entry *h1;
3000 const struct elf_link_hash_entry *h2;
3001 bfd_signed_vma vdiff;
3003 h1 = *(const struct elf_link_hash_entry **) arg1;
3004 h2 = *(const struct elf_link_hash_entry **) arg2;
3005 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3006 if (vdiff != 0)
3007 return vdiff > 0 ? 1 : -1;
3008 else
3010 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3011 if (sdiff != 0)
3012 return sdiff > 0 ? 1 : -1;
3014 return 0;
3017 /* This function is used to adjust offsets into .dynstr for
3018 dynamic symbols. This is called via elf_link_hash_traverse. */
3020 static bfd_boolean
3021 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3023 struct elf_strtab_hash *dynstr = data;
3025 if (h->root.type == bfd_link_hash_warning)
3026 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3028 if (h->dynindx != -1)
3029 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3030 return TRUE;
3033 /* Assign string offsets in .dynstr, update all structures referencing
3034 them. */
3036 static bfd_boolean
3037 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3039 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3040 struct elf_link_local_dynamic_entry *entry;
3041 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3042 bfd *dynobj = hash_table->dynobj;
3043 asection *sdyn;
3044 bfd_size_type size;
3045 const struct elf_backend_data *bed;
3046 bfd_byte *extdyn;
3048 _bfd_elf_strtab_finalize (dynstr);
3049 size = _bfd_elf_strtab_size (dynstr);
3051 bed = get_elf_backend_data (dynobj);
3052 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3053 BFD_ASSERT (sdyn != NULL);
3055 /* Update all .dynamic entries referencing .dynstr strings. */
3056 for (extdyn = sdyn->contents;
3057 extdyn < sdyn->contents + sdyn->size;
3058 extdyn += bed->s->sizeof_dyn)
3060 Elf_Internal_Dyn dyn;
3062 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3063 switch (dyn.d_tag)
3065 case DT_STRSZ:
3066 dyn.d_un.d_val = size;
3067 break;
3068 case DT_NEEDED:
3069 case DT_SONAME:
3070 case DT_RPATH:
3071 case DT_RUNPATH:
3072 case DT_FILTER:
3073 case DT_AUXILIARY:
3074 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3075 break;
3076 default:
3077 continue;
3079 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3082 /* Now update local dynamic symbols. */
3083 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3084 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3085 entry->isym.st_name);
3087 /* And the rest of dynamic symbols. */
3088 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3090 /* Adjust version definitions. */
3091 if (elf_tdata (output_bfd)->cverdefs)
3093 asection *s;
3094 bfd_byte *p;
3095 bfd_size_type i;
3096 Elf_Internal_Verdef def;
3097 Elf_Internal_Verdaux defaux;
3099 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3100 p = s->contents;
3103 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3104 &def);
3105 p += sizeof (Elf_External_Verdef);
3106 if (def.vd_aux != sizeof (Elf_External_Verdef))
3107 continue;
3108 for (i = 0; i < def.vd_cnt; ++i)
3110 _bfd_elf_swap_verdaux_in (output_bfd,
3111 (Elf_External_Verdaux *) p, &defaux);
3112 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3113 defaux.vda_name);
3114 _bfd_elf_swap_verdaux_out (output_bfd,
3115 &defaux, (Elf_External_Verdaux *) p);
3116 p += sizeof (Elf_External_Verdaux);
3119 while (def.vd_next);
3122 /* Adjust version references. */
3123 if (elf_tdata (output_bfd)->verref)
3125 asection *s;
3126 bfd_byte *p;
3127 bfd_size_type i;
3128 Elf_Internal_Verneed need;
3129 Elf_Internal_Vernaux needaux;
3131 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3132 p = s->contents;
3135 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3136 &need);
3137 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3138 _bfd_elf_swap_verneed_out (output_bfd, &need,
3139 (Elf_External_Verneed *) p);
3140 p += sizeof (Elf_External_Verneed);
3141 for (i = 0; i < need.vn_cnt; ++i)
3143 _bfd_elf_swap_vernaux_in (output_bfd,
3144 (Elf_External_Vernaux *) p, &needaux);
3145 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3146 needaux.vna_name);
3147 _bfd_elf_swap_vernaux_out (output_bfd,
3148 &needaux,
3149 (Elf_External_Vernaux *) p);
3150 p += sizeof (Elf_External_Vernaux);
3153 while (need.vn_next);
3156 return TRUE;
3159 /* Add symbols from an ELF object file to the linker hash table. */
3161 static bfd_boolean
3162 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3164 Elf_Internal_Shdr *hdr;
3165 bfd_size_type symcount;
3166 bfd_size_type extsymcount;
3167 bfd_size_type extsymoff;
3168 struct elf_link_hash_entry **sym_hash;
3169 bfd_boolean dynamic;
3170 Elf_External_Versym *extversym = NULL;
3171 Elf_External_Versym *ever;
3172 struct elf_link_hash_entry *weaks;
3173 struct elf_link_hash_entry **nondeflt_vers = NULL;
3174 bfd_size_type nondeflt_vers_cnt = 0;
3175 Elf_Internal_Sym *isymbuf = NULL;
3176 Elf_Internal_Sym *isym;
3177 Elf_Internal_Sym *isymend;
3178 const struct elf_backend_data *bed;
3179 bfd_boolean add_needed;
3180 struct elf_link_hash_table *htab;
3181 bfd_size_type amt;
3182 void *alloc_mark = NULL;
3183 struct bfd_hash_entry **old_table = NULL;
3184 unsigned int old_size = 0;
3185 unsigned int old_count = 0;
3186 void *old_tab = NULL;
3187 void *old_hash;
3188 void *old_ent;
3189 struct bfd_link_hash_entry *old_undefs = NULL;
3190 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3191 long old_dynsymcount = 0;
3192 size_t tabsize = 0;
3193 size_t hashsize = 0;
3195 htab = elf_hash_table (info);
3196 bed = get_elf_backend_data (abfd);
3198 if ((abfd->flags & DYNAMIC) == 0)
3199 dynamic = FALSE;
3200 else
3202 dynamic = TRUE;
3204 /* You can't use -r against a dynamic object. Also, there's no
3205 hope of using a dynamic object which does not exactly match
3206 the format of the output file. */
3207 if (info->relocatable
3208 || !is_elf_hash_table (htab)
3209 || htab->root.creator != abfd->xvec)
3211 if (info->relocatable)
3212 bfd_set_error (bfd_error_invalid_operation);
3213 else
3214 bfd_set_error (bfd_error_wrong_format);
3215 goto error_return;
3219 /* As a GNU extension, any input sections which are named
3220 .gnu.warning.SYMBOL are treated as warning symbols for the given
3221 symbol. This differs from .gnu.warning sections, which generate
3222 warnings when they are included in an output file. */
3223 if (info->executable)
3225 asection *s;
3227 for (s = abfd->sections; s != NULL; s = s->next)
3229 const char *name;
3231 name = bfd_get_section_name (abfd, s);
3232 if (CONST_STRNEQ (name, ".gnu.warning."))
3234 char *msg;
3235 bfd_size_type sz;
3237 name += sizeof ".gnu.warning." - 1;
3239 /* If this is a shared object, then look up the symbol
3240 in the hash table. If it is there, and it is already
3241 been defined, then we will not be using the entry
3242 from this shared object, so we don't need to warn.
3243 FIXME: If we see the definition in a regular object
3244 later on, we will warn, but we shouldn't. The only
3245 fix is to keep track of what warnings we are supposed
3246 to emit, and then handle them all at the end of the
3247 link. */
3248 if (dynamic)
3250 struct elf_link_hash_entry *h;
3252 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3254 /* FIXME: What about bfd_link_hash_common? */
3255 if (h != NULL
3256 && (h->root.type == bfd_link_hash_defined
3257 || h->root.type == bfd_link_hash_defweak))
3259 /* We don't want to issue this warning. Clobber
3260 the section size so that the warning does not
3261 get copied into the output file. */
3262 s->size = 0;
3263 continue;
3267 sz = s->size;
3268 msg = bfd_alloc (abfd, sz + 1);
3269 if (msg == NULL)
3270 goto error_return;
3272 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3273 goto error_return;
3275 msg[sz] = '\0';
3277 if (! (_bfd_generic_link_add_one_symbol
3278 (info, abfd, name, BSF_WARNING, s, 0, msg,
3279 FALSE, bed->collect, NULL)))
3280 goto error_return;
3282 if (! info->relocatable)
3284 /* Clobber the section size so that the warning does
3285 not get copied into the output file. */
3286 s->size = 0;
3288 /* Also set SEC_EXCLUDE, so that symbols defined in
3289 the warning section don't get copied to the output. */
3290 s->flags |= SEC_EXCLUDE;
3296 add_needed = TRUE;
3297 if (! dynamic)
3299 /* If we are creating a shared library, create all the dynamic
3300 sections immediately. We need to attach them to something,
3301 so we attach them to this BFD, provided it is the right
3302 format. FIXME: If there are no input BFD's of the same
3303 format as the output, we can't make a shared library. */
3304 if (info->shared
3305 && is_elf_hash_table (htab)
3306 && htab->root.creator == abfd->xvec
3307 && !htab->dynamic_sections_created)
3309 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3310 goto error_return;
3313 else if (!is_elf_hash_table (htab))
3314 goto error_return;
3315 else
3317 asection *s;
3318 const char *soname = NULL;
3319 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3320 int ret;
3322 /* ld --just-symbols and dynamic objects don't mix very well.
3323 ld shouldn't allow it. */
3324 if ((s = abfd->sections) != NULL
3325 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3326 abort ();
3328 /* If this dynamic lib was specified on the command line with
3329 --as-needed in effect, then we don't want to add a DT_NEEDED
3330 tag unless the lib is actually used. Similary for libs brought
3331 in by another lib's DT_NEEDED. When --no-add-needed is used
3332 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3333 any dynamic library in DT_NEEDED tags in the dynamic lib at
3334 all. */
3335 add_needed = (elf_dyn_lib_class (abfd)
3336 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3337 | DYN_NO_NEEDED)) == 0;
3339 s = bfd_get_section_by_name (abfd, ".dynamic");
3340 if (s != NULL)
3342 bfd_byte *dynbuf;
3343 bfd_byte *extdyn;
3344 int elfsec;
3345 unsigned long shlink;
3347 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3348 goto error_free_dyn;
3350 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3351 if (elfsec == -1)
3352 goto error_free_dyn;
3353 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3355 for (extdyn = dynbuf;
3356 extdyn < dynbuf + s->size;
3357 extdyn += bed->s->sizeof_dyn)
3359 Elf_Internal_Dyn dyn;
3361 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3362 if (dyn.d_tag == DT_SONAME)
3364 unsigned int tagv = dyn.d_un.d_val;
3365 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3366 if (soname == NULL)
3367 goto error_free_dyn;
3369 if (dyn.d_tag == DT_NEEDED)
3371 struct bfd_link_needed_list *n, **pn;
3372 char *fnm, *anm;
3373 unsigned int tagv = dyn.d_un.d_val;
3375 amt = sizeof (struct bfd_link_needed_list);
3376 n = bfd_alloc (abfd, amt);
3377 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3378 if (n == NULL || fnm == NULL)
3379 goto error_free_dyn;
3380 amt = strlen (fnm) + 1;
3381 anm = bfd_alloc (abfd, amt);
3382 if (anm == NULL)
3383 goto error_free_dyn;
3384 memcpy (anm, fnm, amt);
3385 n->name = anm;
3386 n->by = abfd;
3387 n->next = NULL;
3388 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3390 *pn = n;
3392 if (dyn.d_tag == DT_RUNPATH)
3394 struct bfd_link_needed_list *n, **pn;
3395 char *fnm, *anm;
3396 unsigned int tagv = dyn.d_un.d_val;
3398 amt = sizeof (struct bfd_link_needed_list);
3399 n = bfd_alloc (abfd, amt);
3400 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3401 if (n == NULL || fnm == NULL)
3402 goto error_free_dyn;
3403 amt = strlen (fnm) + 1;
3404 anm = bfd_alloc (abfd, amt);
3405 if (anm == NULL)
3406 goto error_free_dyn;
3407 memcpy (anm, fnm, amt);
3408 n->name = anm;
3409 n->by = abfd;
3410 n->next = NULL;
3411 for (pn = & runpath;
3412 *pn != NULL;
3413 pn = &(*pn)->next)
3415 *pn = n;
3417 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3418 if (!runpath && dyn.d_tag == DT_RPATH)
3420 struct bfd_link_needed_list *n, **pn;
3421 char *fnm, *anm;
3422 unsigned int tagv = dyn.d_un.d_val;
3424 amt = sizeof (struct bfd_link_needed_list);
3425 n = bfd_alloc (abfd, amt);
3426 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3427 if (n == NULL || fnm == NULL)
3428 goto error_free_dyn;
3429 amt = strlen (fnm) + 1;
3430 anm = bfd_alloc (abfd, amt);
3431 if (anm == NULL)
3433 error_free_dyn:
3434 free (dynbuf);
3435 goto error_return;
3437 memcpy (anm, fnm, amt);
3438 n->name = anm;
3439 n->by = abfd;
3440 n->next = NULL;
3441 for (pn = & rpath;
3442 *pn != NULL;
3443 pn = &(*pn)->next)
3445 *pn = n;
3449 free (dynbuf);
3452 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3453 frees all more recently bfd_alloc'd blocks as well. */
3454 if (runpath)
3455 rpath = runpath;
3457 if (rpath)
3459 struct bfd_link_needed_list **pn;
3460 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3462 *pn = rpath;
3465 /* We do not want to include any of the sections in a dynamic
3466 object in the output file. We hack by simply clobbering the
3467 list of sections in the BFD. This could be handled more
3468 cleanly by, say, a new section flag; the existing
3469 SEC_NEVER_LOAD flag is not the one we want, because that one
3470 still implies that the section takes up space in the output
3471 file. */
3472 bfd_section_list_clear (abfd);
3474 /* Find the name to use in a DT_NEEDED entry that refers to this
3475 object. If the object has a DT_SONAME entry, we use it.
3476 Otherwise, if the generic linker stuck something in
3477 elf_dt_name, we use that. Otherwise, we just use the file
3478 name. */
3479 if (soname == NULL || *soname == '\0')
3481 soname = elf_dt_name (abfd);
3482 if (soname == NULL || *soname == '\0')
3483 soname = bfd_get_filename (abfd);
3486 /* Save the SONAME because sometimes the linker emulation code
3487 will need to know it. */
3488 elf_dt_name (abfd) = soname;
3490 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3491 if (ret < 0)
3492 goto error_return;
3494 /* If we have already included this dynamic object in the
3495 link, just ignore it. There is no reason to include a
3496 particular dynamic object more than once. */
3497 if (ret > 0)
3498 return TRUE;
3501 /* If this is a dynamic object, we always link against the .dynsym
3502 symbol table, not the .symtab symbol table. The dynamic linker
3503 will only see the .dynsym symbol table, so there is no reason to
3504 look at .symtab for a dynamic object. */
3506 if (! dynamic || elf_dynsymtab (abfd) == 0)
3507 hdr = &elf_tdata (abfd)->symtab_hdr;
3508 else
3509 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3511 symcount = hdr->sh_size / bed->s->sizeof_sym;
3513 /* The sh_info field of the symtab header tells us where the
3514 external symbols start. We don't care about the local symbols at
3515 this point. */
3516 if (elf_bad_symtab (abfd))
3518 extsymcount = symcount;
3519 extsymoff = 0;
3521 else
3523 extsymcount = symcount - hdr->sh_info;
3524 extsymoff = hdr->sh_info;
3527 sym_hash = NULL;
3528 if (extsymcount != 0)
3530 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3531 NULL, NULL, NULL);
3532 if (isymbuf == NULL)
3533 goto error_return;
3535 /* We store a pointer to the hash table entry for each external
3536 symbol. */
3537 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3538 sym_hash = bfd_alloc (abfd, amt);
3539 if (sym_hash == NULL)
3540 goto error_free_sym;
3541 elf_sym_hashes (abfd) = sym_hash;
3544 if (dynamic)
3546 /* Read in any version definitions. */
3547 if (!_bfd_elf_slurp_version_tables (abfd,
3548 info->default_imported_symver))
3549 goto error_free_sym;
3551 /* Read in the symbol versions, but don't bother to convert them
3552 to internal format. */
3553 if (elf_dynversym (abfd) != 0)
3555 Elf_Internal_Shdr *versymhdr;
3557 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3558 extversym = bfd_malloc (versymhdr->sh_size);
3559 if (extversym == NULL)
3560 goto error_free_sym;
3561 amt = versymhdr->sh_size;
3562 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3563 || bfd_bread (extversym, amt, abfd) != amt)
3564 goto error_free_vers;
3568 /* If we are loading an as-needed shared lib, save the symbol table
3569 state before we start adding symbols. If the lib turns out
3570 to be unneeded, restore the state. */
3571 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3573 unsigned int i;
3574 size_t entsize;
3576 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3578 struct bfd_hash_entry *p;
3579 struct elf_link_hash_entry *h;
3581 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3583 h = (struct elf_link_hash_entry *) p;
3584 entsize += htab->root.table.entsize;
3585 if (h->root.type == bfd_link_hash_warning)
3586 entsize += htab->root.table.entsize;
3590 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3591 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3592 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3593 if (old_tab == NULL)
3594 goto error_free_vers;
3596 /* Remember the current objalloc pointer, so that all mem for
3597 symbols added can later be reclaimed. */
3598 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3599 if (alloc_mark == NULL)
3600 goto error_free_vers;
3602 /* Make a special call to the linker "notice" function to
3603 tell it that we are about to handle an as-needed lib. */
3604 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3605 notice_as_needed))
3606 return FALSE;
3609 /* Clone the symbol table and sym hashes. Remember some
3610 pointers into the symbol table, and dynamic symbol count. */
3611 old_hash = (char *) old_tab + tabsize;
3612 old_ent = (char *) old_hash + hashsize;
3613 memcpy (old_tab, htab->root.table.table, tabsize);
3614 memcpy (old_hash, sym_hash, hashsize);
3615 old_undefs = htab->root.undefs;
3616 old_undefs_tail = htab->root.undefs_tail;
3617 old_table = htab->root.table.table;
3618 old_size = htab->root.table.size;
3619 old_count = htab->root.table.count;
3620 old_dynsymcount = htab->dynsymcount;
3622 for (i = 0; i < htab->root.table.size; i++)
3624 struct bfd_hash_entry *p;
3625 struct elf_link_hash_entry *h;
3627 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3629 memcpy (old_ent, p, htab->root.table.entsize);
3630 old_ent = (char *) old_ent + htab->root.table.entsize;
3631 h = (struct elf_link_hash_entry *) p;
3632 if (h->root.type == bfd_link_hash_warning)
3634 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3635 old_ent = (char *) old_ent + htab->root.table.entsize;
3641 weaks = NULL;
3642 ever = extversym != NULL ? extversym + extsymoff : NULL;
3643 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3644 isym < isymend;
3645 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3647 int bind;
3648 bfd_vma value;
3649 asection *sec, *new_sec;
3650 flagword flags;
3651 const char *name;
3652 struct elf_link_hash_entry *h;
3653 bfd_boolean definition;
3654 bfd_boolean size_change_ok;
3655 bfd_boolean type_change_ok;
3656 bfd_boolean new_weakdef;
3657 bfd_boolean override;
3658 bfd_boolean common;
3659 unsigned int old_alignment;
3660 bfd *old_bfd;
3662 override = FALSE;
3664 flags = BSF_NO_FLAGS;
3665 sec = NULL;
3666 value = isym->st_value;
3667 *sym_hash = NULL;
3668 common = bed->common_definition (isym);
3670 bind = ELF_ST_BIND (isym->st_info);
3671 if (bind == STB_LOCAL)
3673 /* This should be impossible, since ELF requires that all
3674 global symbols follow all local symbols, and that sh_info
3675 point to the first global symbol. Unfortunately, Irix 5
3676 screws this up. */
3677 continue;
3679 else if (bind == STB_GLOBAL)
3681 if (isym->st_shndx != SHN_UNDEF && !common)
3682 flags = BSF_GLOBAL;
3684 else if (bind == STB_WEAK)
3685 flags = BSF_WEAK;
3686 else
3688 /* Leave it up to the processor backend. */
3691 if (isym->st_shndx == SHN_UNDEF)
3692 sec = bfd_und_section_ptr;
3693 else if (isym->st_shndx < SHN_LORESERVE
3694 || isym->st_shndx > SHN_HIRESERVE)
3696 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3697 if (sec == NULL)
3698 sec = bfd_abs_section_ptr;
3699 else if (sec->kept_section)
3701 /* Symbols from discarded section are undefined. We keep
3702 its visibility. */
3703 sec = bfd_und_section_ptr;
3704 isym->st_shndx = SHN_UNDEF;
3706 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3707 value -= sec->vma;
3709 else if (isym->st_shndx == SHN_ABS)
3710 sec = bfd_abs_section_ptr;
3711 else if (isym->st_shndx == SHN_COMMON)
3713 sec = bfd_com_section_ptr;
3714 /* What ELF calls the size we call the value. What ELF
3715 calls the value we call the alignment. */
3716 value = isym->st_size;
3718 else
3720 /* Leave it up to the processor backend. */
3723 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3724 isym->st_name);
3725 if (name == NULL)
3726 goto error_free_vers;
3728 if (isym->st_shndx == SHN_COMMON
3729 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3730 && !info->relocatable)
3732 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3734 if (tcomm == NULL)
3736 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3737 (SEC_ALLOC
3738 | SEC_IS_COMMON
3739 | SEC_LINKER_CREATED
3740 | SEC_THREAD_LOCAL));
3741 if (tcomm == NULL)
3742 goto error_free_vers;
3744 sec = tcomm;
3746 else if (bed->elf_add_symbol_hook)
3748 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3749 &sec, &value))
3750 goto error_free_vers;
3752 /* The hook function sets the name to NULL if this symbol
3753 should be skipped for some reason. */
3754 if (name == NULL)
3755 continue;
3758 /* Sanity check that all possibilities were handled. */
3759 if (sec == NULL)
3761 bfd_set_error (bfd_error_bad_value);
3762 goto error_free_vers;
3765 if (bfd_is_und_section (sec)
3766 || bfd_is_com_section (sec))
3767 definition = FALSE;
3768 else
3769 definition = TRUE;
3771 size_change_ok = FALSE;
3772 type_change_ok = bed->type_change_ok;
3773 old_alignment = 0;
3774 old_bfd = NULL;
3775 new_sec = sec;
3777 if (is_elf_hash_table (htab))
3779 Elf_Internal_Versym iver;
3780 unsigned int vernum = 0;
3781 bfd_boolean skip;
3783 if (ever == NULL)
3785 if (info->default_imported_symver)
3786 /* Use the default symbol version created earlier. */
3787 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3788 else
3789 iver.vs_vers = 0;
3791 else
3792 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3794 vernum = iver.vs_vers & VERSYM_VERSION;
3796 /* If this is a hidden symbol, or if it is not version
3797 1, we append the version name to the symbol name.
3798 However, we do not modify a non-hidden absolute symbol
3799 if it is not a function, because it might be the version
3800 symbol itself. FIXME: What if it isn't? */
3801 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3802 || (vernum > 1 && (! bfd_is_abs_section (sec)
3803 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3805 const char *verstr;
3806 size_t namelen, verlen, newlen;
3807 char *newname, *p;
3809 if (isym->st_shndx != SHN_UNDEF)
3811 if (vernum > elf_tdata (abfd)->cverdefs)
3812 verstr = NULL;
3813 else if (vernum > 1)
3814 verstr =
3815 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3816 else
3817 verstr = "";
3819 if (verstr == NULL)
3821 (*_bfd_error_handler)
3822 (_("%B: %s: invalid version %u (max %d)"),
3823 abfd, name, vernum,
3824 elf_tdata (abfd)->cverdefs);
3825 bfd_set_error (bfd_error_bad_value);
3826 goto error_free_vers;
3829 else
3831 /* We cannot simply test for the number of
3832 entries in the VERNEED section since the
3833 numbers for the needed versions do not start
3834 at 0. */
3835 Elf_Internal_Verneed *t;
3837 verstr = NULL;
3838 for (t = elf_tdata (abfd)->verref;
3839 t != NULL;
3840 t = t->vn_nextref)
3842 Elf_Internal_Vernaux *a;
3844 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3846 if (a->vna_other == vernum)
3848 verstr = a->vna_nodename;
3849 break;
3852 if (a != NULL)
3853 break;
3855 if (verstr == NULL)
3857 (*_bfd_error_handler)
3858 (_("%B: %s: invalid needed version %d"),
3859 abfd, name, vernum);
3860 bfd_set_error (bfd_error_bad_value);
3861 goto error_free_vers;
3865 namelen = strlen (name);
3866 verlen = strlen (verstr);
3867 newlen = namelen + verlen + 2;
3868 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3869 && isym->st_shndx != SHN_UNDEF)
3870 ++newlen;
3872 newname = bfd_hash_allocate (&htab->root.table, newlen);
3873 if (newname == NULL)
3874 goto error_free_vers;
3875 memcpy (newname, name, namelen);
3876 p = newname + namelen;
3877 *p++ = ELF_VER_CHR;
3878 /* If this is a defined non-hidden version symbol,
3879 we add another @ to the name. This indicates the
3880 default version of the symbol. */
3881 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3882 && isym->st_shndx != SHN_UNDEF)
3883 *p++ = ELF_VER_CHR;
3884 memcpy (p, verstr, verlen + 1);
3886 name = newname;
3889 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3890 &value, &old_alignment,
3891 sym_hash, &skip, &override,
3892 &type_change_ok, &size_change_ok))
3893 goto error_free_vers;
3895 if (skip)
3896 continue;
3898 if (override)
3899 definition = FALSE;
3901 h = *sym_hash;
3902 while (h->root.type == bfd_link_hash_indirect
3903 || h->root.type == bfd_link_hash_warning)
3904 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3906 /* Remember the old alignment if this is a common symbol, so
3907 that we don't reduce the alignment later on. We can't
3908 check later, because _bfd_generic_link_add_one_symbol
3909 will set a default for the alignment which we want to
3910 override. We also remember the old bfd where the existing
3911 definition comes from. */
3912 switch (h->root.type)
3914 default:
3915 break;
3917 case bfd_link_hash_defined:
3918 case bfd_link_hash_defweak:
3919 old_bfd = h->root.u.def.section->owner;
3920 break;
3922 case bfd_link_hash_common:
3923 old_bfd = h->root.u.c.p->section->owner;
3924 old_alignment = h->root.u.c.p->alignment_power;
3925 break;
3928 if (elf_tdata (abfd)->verdef != NULL
3929 && ! override
3930 && vernum > 1
3931 && definition)
3932 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3935 if (! (_bfd_generic_link_add_one_symbol
3936 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3937 (struct bfd_link_hash_entry **) sym_hash)))
3938 goto error_free_vers;
3940 h = *sym_hash;
3941 while (h->root.type == bfd_link_hash_indirect
3942 || h->root.type == bfd_link_hash_warning)
3943 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3944 *sym_hash = h;
3946 new_weakdef = FALSE;
3947 if (dynamic
3948 && definition
3949 && (flags & BSF_WEAK) != 0
3950 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3951 && is_elf_hash_table (htab)
3952 && h->u.weakdef == NULL)
3954 /* Keep a list of all weak defined non function symbols from
3955 a dynamic object, using the weakdef field. Later in this
3956 function we will set the weakdef field to the correct
3957 value. We only put non-function symbols from dynamic
3958 objects on this list, because that happens to be the only
3959 time we need to know the normal symbol corresponding to a
3960 weak symbol, and the information is time consuming to
3961 figure out. If the weakdef field is not already NULL,
3962 then this symbol was already defined by some previous
3963 dynamic object, and we will be using that previous
3964 definition anyhow. */
3966 h->u.weakdef = weaks;
3967 weaks = h;
3968 new_weakdef = TRUE;
3971 /* Set the alignment of a common symbol. */
3972 if ((common || bfd_is_com_section (sec))
3973 && h->root.type == bfd_link_hash_common)
3975 unsigned int align;
3977 if (common)
3978 align = bfd_log2 (isym->st_value);
3979 else
3981 /* The new symbol is a common symbol in a shared object.
3982 We need to get the alignment from the section. */
3983 align = new_sec->alignment_power;
3985 if (align > old_alignment
3986 /* Permit an alignment power of zero if an alignment of one
3987 is specified and no other alignments have been specified. */
3988 || (isym->st_value == 1 && old_alignment == 0))
3989 h->root.u.c.p->alignment_power = align;
3990 else
3991 h->root.u.c.p->alignment_power = old_alignment;
3994 if (is_elf_hash_table (htab))
3996 bfd_boolean dynsym;
3998 /* Check the alignment when a common symbol is involved. This
3999 can change when a common symbol is overridden by a normal
4000 definition or a common symbol is ignored due to the old
4001 normal definition. We need to make sure the maximum
4002 alignment is maintained. */
4003 if ((old_alignment || common)
4004 && h->root.type != bfd_link_hash_common)
4006 unsigned int common_align;
4007 unsigned int normal_align;
4008 unsigned int symbol_align;
4009 bfd *normal_bfd;
4010 bfd *common_bfd;
4012 symbol_align = ffs (h->root.u.def.value) - 1;
4013 if (h->root.u.def.section->owner != NULL
4014 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4016 normal_align = h->root.u.def.section->alignment_power;
4017 if (normal_align > symbol_align)
4018 normal_align = symbol_align;
4020 else
4021 normal_align = symbol_align;
4023 if (old_alignment)
4025 common_align = old_alignment;
4026 common_bfd = old_bfd;
4027 normal_bfd = abfd;
4029 else
4031 common_align = bfd_log2 (isym->st_value);
4032 common_bfd = abfd;
4033 normal_bfd = old_bfd;
4036 if (normal_align < common_align)
4038 /* PR binutils/2735 */
4039 if (normal_bfd == NULL)
4040 (*_bfd_error_handler)
4041 (_("Warning: alignment %u of common symbol `%s' in %B"
4042 " is greater than the alignment (%u) of its section %A"),
4043 common_bfd, h->root.u.def.section,
4044 1 << common_align, name, 1 << normal_align);
4045 else
4046 (*_bfd_error_handler)
4047 (_("Warning: alignment %u of symbol `%s' in %B"
4048 " is smaller than %u in %B"),
4049 normal_bfd, common_bfd,
4050 1 << normal_align, name, 1 << common_align);
4054 /* Remember the symbol size if it isn't undefined. */
4055 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4056 && (definition || h->size == 0))
4058 if (h->size != 0
4059 && h->size != isym->st_size
4060 && ! size_change_ok)
4061 (*_bfd_error_handler)
4062 (_("Warning: size of symbol `%s' changed"
4063 " from %lu in %B to %lu in %B"),
4064 old_bfd, abfd,
4065 name, (unsigned long) h->size,
4066 (unsigned long) isym->st_size);
4068 h->size = isym->st_size;
4071 /* If this is a common symbol, then we always want H->SIZE
4072 to be the size of the common symbol. The code just above
4073 won't fix the size if a common symbol becomes larger. We
4074 don't warn about a size change here, because that is
4075 covered by --warn-common. */
4076 if (h->root.type == bfd_link_hash_common)
4077 h->size = h->root.u.c.size;
4079 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4080 && (definition || h->type == STT_NOTYPE))
4082 if (h->type != STT_NOTYPE
4083 && h->type != ELF_ST_TYPE (isym->st_info)
4084 && ! type_change_ok)
4085 (*_bfd_error_handler)
4086 (_("Warning: type of symbol `%s' changed"
4087 " from %d to %d in %B"),
4088 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4090 h->type = ELF_ST_TYPE (isym->st_info);
4093 /* If st_other has a processor-specific meaning, specific
4094 code might be needed here. We never merge the visibility
4095 attribute with the one from a dynamic object. */
4096 if (bed->elf_backend_merge_symbol_attribute)
4097 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4098 dynamic);
4100 /* If this symbol has default visibility and the user has requested
4101 we not re-export it, then mark it as hidden. */
4102 if (definition && !dynamic
4103 && (abfd->no_export
4104 || (abfd->my_archive && abfd->my_archive->no_export))
4105 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4106 isym->st_other = (STV_HIDDEN
4107 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4109 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4111 unsigned char hvis, symvis, other, nvis;
4113 /* Only merge the visibility. Leave the remainder of the
4114 st_other field to elf_backend_merge_symbol_attribute. */
4115 other = h->other & ~ELF_ST_VISIBILITY (-1);
4117 /* Combine visibilities, using the most constraining one. */
4118 hvis = ELF_ST_VISIBILITY (h->other);
4119 symvis = ELF_ST_VISIBILITY (isym->st_other);
4120 if (! hvis)
4121 nvis = symvis;
4122 else if (! symvis)
4123 nvis = hvis;
4124 else
4125 nvis = hvis < symvis ? hvis : symvis;
4127 h->other = other | nvis;
4130 /* Set a flag in the hash table entry indicating the type of
4131 reference or definition we just found. Keep a count of
4132 the number of dynamic symbols we find. A dynamic symbol
4133 is one which is referenced or defined by both a regular
4134 object and a shared object. */
4135 dynsym = FALSE;
4136 if (! dynamic)
4138 if (! definition)
4140 h->ref_regular = 1;
4141 if (bind != STB_WEAK)
4142 h->ref_regular_nonweak = 1;
4144 else
4145 h->def_regular = 1;
4146 if (! info->executable
4147 || h->def_dynamic
4148 || h->ref_dynamic)
4149 dynsym = TRUE;
4151 else
4153 if (! definition)
4154 h->ref_dynamic = 1;
4155 else
4156 h->def_dynamic = 1;
4157 if (h->def_regular
4158 || h->ref_regular
4159 || (h->u.weakdef != NULL
4160 && ! new_weakdef
4161 && h->u.weakdef->dynindx != -1))
4162 dynsym = TRUE;
4165 if (definition && (sec->flags & SEC_DEBUGGING))
4167 /* We don't want to make debug symbol dynamic. */
4168 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4169 dynsym = FALSE;
4172 /* Check to see if we need to add an indirect symbol for
4173 the default name. */
4174 if (definition || h->root.type == bfd_link_hash_common)
4175 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4176 &sec, &value, &dynsym,
4177 override))
4178 goto error_free_vers;
4180 if (definition && !dynamic)
4182 char *p = strchr (name, ELF_VER_CHR);
4183 if (p != NULL && p[1] != ELF_VER_CHR)
4185 /* Queue non-default versions so that .symver x, x@FOO
4186 aliases can be checked. */
4187 if (!nondeflt_vers)
4189 amt = ((isymend - isym + 1)
4190 * sizeof (struct elf_link_hash_entry *));
4191 nondeflt_vers = bfd_malloc (amt);
4193 nondeflt_vers[nondeflt_vers_cnt++] = h;
4197 if (dynsym && h->dynindx == -1)
4199 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4200 goto error_free_vers;
4201 if (h->u.weakdef != NULL
4202 && ! new_weakdef
4203 && h->u.weakdef->dynindx == -1)
4205 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4206 goto error_free_vers;
4209 else if (dynsym && h->dynindx != -1)
4210 /* If the symbol already has a dynamic index, but
4211 visibility says it should not be visible, turn it into
4212 a local symbol. */
4213 switch (ELF_ST_VISIBILITY (h->other))
4215 case STV_INTERNAL:
4216 case STV_HIDDEN:
4217 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4218 dynsym = FALSE;
4219 break;
4222 if (!add_needed
4223 && definition
4224 && dynsym
4225 && h->ref_regular)
4227 int ret;
4228 const char *soname = elf_dt_name (abfd);
4230 /* A symbol from a library loaded via DT_NEEDED of some
4231 other library is referenced by a regular object.
4232 Add a DT_NEEDED entry for it. Issue an error if
4233 --no-add-needed is used. */
4234 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4236 (*_bfd_error_handler)
4237 (_("%s: invalid DSO for symbol `%s' definition"),
4238 abfd, name);
4239 bfd_set_error (bfd_error_bad_value);
4240 goto error_free_vers;
4243 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4245 add_needed = TRUE;
4246 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4247 if (ret < 0)
4248 goto error_free_vers;
4250 BFD_ASSERT (ret == 0);
4255 if (extversym != NULL)
4257 free (extversym);
4258 extversym = NULL;
4261 if (isymbuf != NULL)
4263 free (isymbuf);
4264 isymbuf = NULL;
4267 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4269 unsigned int i;
4271 /* Restore the symbol table. */
4272 if (bed->as_needed_cleanup)
4273 (*bed->as_needed_cleanup) (abfd, info);
4274 old_hash = (char *) old_tab + tabsize;
4275 old_ent = (char *) old_hash + hashsize;
4276 sym_hash = elf_sym_hashes (abfd);
4277 htab->root.table.table = old_table;
4278 htab->root.table.size = old_size;
4279 htab->root.table.count = old_count;
4280 memcpy (htab->root.table.table, old_tab, tabsize);
4281 memcpy (sym_hash, old_hash, hashsize);
4282 htab->root.undefs = old_undefs;
4283 htab->root.undefs_tail = old_undefs_tail;
4284 for (i = 0; i < htab->root.table.size; i++)
4286 struct bfd_hash_entry *p;
4287 struct elf_link_hash_entry *h;
4289 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4291 h = (struct elf_link_hash_entry *) p;
4292 if (h->root.type == bfd_link_hash_warning)
4293 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4294 if (h->dynindx >= old_dynsymcount)
4295 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4297 memcpy (p, old_ent, htab->root.table.entsize);
4298 old_ent = (char *) old_ent + htab->root.table.entsize;
4299 h = (struct elf_link_hash_entry *) p;
4300 if (h->root.type == bfd_link_hash_warning)
4302 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4303 old_ent = (char *) old_ent + htab->root.table.entsize;
4308 /* Make a special call to the linker "notice" function to
4309 tell it that symbols added for crefs may need to be removed. */
4310 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4311 notice_not_needed))
4312 return FALSE;
4314 free (old_tab);
4315 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4316 alloc_mark);
4317 if (nondeflt_vers != NULL)
4318 free (nondeflt_vers);
4319 return TRUE;
4322 if (old_tab != NULL)
4324 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4325 notice_needed))
4326 return FALSE;
4327 free (old_tab);
4328 old_tab = NULL;
4331 /* Now that all the symbols from this input file are created, handle
4332 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4333 if (nondeflt_vers != NULL)
4335 bfd_size_type cnt, symidx;
4337 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4339 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4340 char *shortname, *p;
4342 p = strchr (h->root.root.string, ELF_VER_CHR);
4343 if (p == NULL
4344 || (h->root.type != bfd_link_hash_defined
4345 && h->root.type != bfd_link_hash_defweak))
4346 continue;
4348 amt = p - h->root.root.string;
4349 shortname = bfd_malloc (amt + 1);
4350 memcpy (shortname, h->root.root.string, amt);
4351 shortname[amt] = '\0';
4353 hi = (struct elf_link_hash_entry *)
4354 bfd_link_hash_lookup (&htab->root, shortname,
4355 FALSE, FALSE, FALSE);
4356 if (hi != NULL
4357 && hi->root.type == h->root.type
4358 && hi->root.u.def.value == h->root.u.def.value
4359 && hi->root.u.def.section == h->root.u.def.section)
4361 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4362 hi->root.type = bfd_link_hash_indirect;
4363 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4364 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4365 sym_hash = elf_sym_hashes (abfd);
4366 if (sym_hash)
4367 for (symidx = 0; symidx < extsymcount; ++symidx)
4368 if (sym_hash[symidx] == hi)
4370 sym_hash[symidx] = h;
4371 break;
4374 free (shortname);
4376 free (nondeflt_vers);
4377 nondeflt_vers = NULL;
4380 /* Now set the weakdefs field correctly for all the weak defined
4381 symbols we found. The only way to do this is to search all the
4382 symbols. Since we only need the information for non functions in
4383 dynamic objects, that's the only time we actually put anything on
4384 the list WEAKS. We need this information so that if a regular
4385 object refers to a symbol defined weakly in a dynamic object, the
4386 real symbol in the dynamic object is also put in the dynamic
4387 symbols; we also must arrange for both symbols to point to the
4388 same memory location. We could handle the general case of symbol
4389 aliasing, but a general symbol alias can only be generated in
4390 assembler code, handling it correctly would be very time
4391 consuming, and other ELF linkers don't handle general aliasing
4392 either. */
4393 if (weaks != NULL)
4395 struct elf_link_hash_entry **hpp;
4396 struct elf_link_hash_entry **hppend;
4397 struct elf_link_hash_entry **sorted_sym_hash;
4398 struct elf_link_hash_entry *h;
4399 size_t sym_count;
4401 /* Since we have to search the whole symbol list for each weak
4402 defined symbol, search time for N weak defined symbols will be
4403 O(N^2). Binary search will cut it down to O(NlogN). */
4404 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4405 sorted_sym_hash = bfd_malloc (amt);
4406 if (sorted_sym_hash == NULL)
4407 goto error_return;
4408 sym_hash = sorted_sym_hash;
4409 hpp = elf_sym_hashes (abfd);
4410 hppend = hpp + extsymcount;
4411 sym_count = 0;
4412 for (; hpp < hppend; hpp++)
4414 h = *hpp;
4415 if (h != NULL
4416 && h->root.type == bfd_link_hash_defined
4417 && h->type != STT_FUNC)
4419 *sym_hash = h;
4420 sym_hash++;
4421 sym_count++;
4425 qsort (sorted_sym_hash, sym_count,
4426 sizeof (struct elf_link_hash_entry *),
4427 elf_sort_symbol);
4429 while (weaks != NULL)
4431 struct elf_link_hash_entry *hlook;
4432 asection *slook;
4433 bfd_vma vlook;
4434 long ilook;
4435 size_t i, j, idx;
4437 hlook = weaks;
4438 weaks = hlook->u.weakdef;
4439 hlook->u.weakdef = NULL;
4441 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4442 || hlook->root.type == bfd_link_hash_defweak
4443 || hlook->root.type == bfd_link_hash_common
4444 || hlook->root.type == bfd_link_hash_indirect);
4445 slook = hlook->root.u.def.section;
4446 vlook = hlook->root.u.def.value;
4448 ilook = -1;
4449 i = 0;
4450 j = sym_count;
4451 while (i < j)
4453 bfd_signed_vma vdiff;
4454 idx = (i + j) / 2;
4455 h = sorted_sym_hash [idx];
4456 vdiff = vlook - h->root.u.def.value;
4457 if (vdiff < 0)
4458 j = idx;
4459 else if (vdiff > 0)
4460 i = idx + 1;
4461 else
4463 long sdiff = slook->id - h->root.u.def.section->id;
4464 if (sdiff < 0)
4465 j = idx;
4466 else if (sdiff > 0)
4467 i = idx + 1;
4468 else
4470 ilook = idx;
4471 break;
4476 /* We didn't find a value/section match. */
4477 if (ilook == -1)
4478 continue;
4480 for (i = ilook; i < sym_count; i++)
4482 h = sorted_sym_hash [i];
4484 /* Stop if value or section doesn't match. */
4485 if (h->root.u.def.value != vlook
4486 || h->root.u.def.section != slook)
4487 break;
4488 else if (h != hlook)
4490 hlook->u.weakdef = h;
4492 /* If the weak definition is in the list of dynamic
4493 symbols, make sure the real definition is put
4494 there as well. */
4495 if (hlook->dynindx != -1 && h->dynindx == -1)
4497 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4498 goto error_return;
4501 /* If the real definition is in the list of dynamic
4502 symbols, make sure the weak definition is put
4503 there as well. If we don't do this, then the
4504 dynamic loader might not merge the entries for the
4505 real definition and the weak definition. */
4506 if (h->dynindx != -1 && hlook->dynindx == -1)
4508 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4509 goto error_return;
4511 break;
4516 free (sorted_sym_hash);
4519 if (bed->check_directives)
4520 (*bed->check_directives) (abfd, info);
4522 /* If this object is the same format as the output object, and it is
4523 not a shared library, then let the backend look through the
4524 relocs.
4526 This is required to build global offset table entries and to
4527 arrange for dynamic relocs. It is not required for the
4528 particular common case of linking non PIC code, even when linking
4529 against shared libraries, but unfortunately there is no way of
4530 knowing whether an object file has been compiled PIC or not.
4531 Looking through the relocs is not particularly time consuming.
4532 The problem is that we must either (1) keep the relocs in memory,
4533 which causes the linker to require additional runtime memory or
4534 (2) read the relocs twice from the input file, which wastes time.
4535 This would be a good case for using mmap.
4537 I have no idea how to handle linking PIC code into a file of a
4538 different format. It probably can't be done. */
4539 if (! dynamic
4540 && is_elf_hash_table (htab)
4541 && htab->root.creator == abfd->xvec
4542 && bed->check_relocs != NULL)
4544 asection *o;
4546 for (o = abfd->sections; o != NULL; o = o->next)
4548 Elf_Internal_Rela *internal_relocs;
4549 bfd_boolean ok;
4551 if ((o->flags & SEC_RELOC) == 0
4552 || o->reloc_count == 0
4553 || ((info->strip == strip_all || info->strip == strip_debugger)
4554 && (o->flags & SEC_DEBUGGING) != 0)
4555 || bfd_is_abs_section (o->output_section))
4556 continue;
4558 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4559 info->keep_memory);
4560 if (internal_relocs == NULL)
4561 goto error_return;
4563 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4565 if (elf_section_data (o)->relocs != internal_relocs)
4566 free (internal_relocs);
4568 if (! ok)
4569 goto error_return;
4573 /* If this is a non-traditional link, try to optimize the handling
4574 of the .stab/.stabstr sections. */
4575 if (! dynamic
4576 && ! info->traditional_format
4577 && is_elf_hash_table (htab)
4578 && (info->strip != strip_all && info->strip != strip_debugger))
4580 asection *stabstr;
4582 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4583 if (stabstr != NULL)
4585 bfd_size_type string_offset = 0;
4586 asection *stab;
4588 for (stab = abfd->sections; stab; stab = stab->next)
4589 if (CONST_STRNEQ (stab->name, ".stab")
4590 && (!stab->name[5] ||
4591 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4592 && (stab->flags & SEC_MERGE) == 0
4593 && !bfd_is_abs_section (stab->output_section))
4595 struct bfd_elf_section_data *secdata;
4597 secdata = elf_section_data (stab);
4598 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4599 stabstr, &secdata->sec_info,
4600 &string_offset))
4601 goto error_return;
4602 if (secdata->sec_info)
4603 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4608 if (is_elf_hash_table (htab) && add_needed)
4610 /* Add this bfd to the loaded list. */
4611 struct elf_link_loaded_list *n;
4613 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4614 if (n == NULL)
4615 goto error_return;
4616 n->abfd = abfd;
4617 n->next = htab->loaded;
4618 htab->loaded = n;
4621 return TRUE;
4623 error_free_vers:
4624 if (old_tab != NULL)
4625 free (old_tab);
4626 if (nondeflt_vers != NULL)
4627 free (nondeflt_vers);
4628 if (extversym != NULL)
4629 free (extversym);
4630 error_free_sym:
4631 if (isymbuf != NULL)
4632 free (isymbuf);
4633 error_return:
4634 return FALSE;
4637 /* Return the linker hash table entry of a symbol that might be
4638 satisfied by an archive symbol. Return -1 on error. */
4640 struct elf_link_hash_entry *
4641 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4642 struct bfd_link_info *info,
4643 const char *name)
4645 struct elf_link_hash_entry *h;
4646 char *p, *copy;
4647 size_t len, first;
4649 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4650 if (h != NULL)
4651 return h;
4653 /* If this is a default version (the name contains @@), look up the
4654 symbol again with only one `@' as well as without the version.
4655 The effect is that references to the symbol with and without the
4656 version will be matched by the default symbol in the archive. */
4658 p = strchr (name, ELF_VER_CHR);
4659 if (p == NULL || p[1] != ELF_VER_CHR)
4660 return h;
4662 /* First check with only one `@'. */
4663 len = strlen (name);
4664 copy = bfd_alloc (abfd, len);
4665 if (copy == NULL)
4666 return (struct elf_link_hash_entry *) 0 - 1;
4668 first = p - name + 1;
4669 memcpy (copy, name, first);
4670 memcpy (copy + first, name + first + 1, len - first);
4672 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4673 if (h == NULL)
4675 /* We also need to check references to the symbol without the
4676 version. */
4677 copy[first - 1] = '\0';
4678 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4679 FALSE, FALSE, FALSE);
4682 bfd_release (abfd, copy);
4683 return h;
4686 /* Add symbols from an ELF archive file to the linker hash table. We
4687 don't use _bfd_generic_link_add_archive_symbols because of a
4688 problem which arises on UnixWare. The UnixWare libc.so is an
4689 archive which includes an entry libc.so.1 which defines a bunch of
4690 symbols. The libc.so archive also includes a number of other
4691 object files, which also define symbols, some of which are the same
4692 as those defined in libc.so.1. Correct linking requires that we
4693 consider each object file in turn, and include it if it defines any
4694 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4695 this; it looks through the list of undefined symbols, and includes
4696 any object file which defines them. When this algorithm is used on
4697 UnixWare, it winds up pulling in libc.so.1 early and defining a
4698 bunch of symbols. This means that some of the other objects in the
4699 archive are not included in the link, which is incorrect since they
4700 precede libc.so.1 in the archive.
4702 Fortunately, ELF archive handling is simpler than that done by
4703 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4704 oddities. In ELF, if we find a symbol in the archive map, and the
4705 symbol is currently undefined, we know that we must pull in that
4706 object file.
4708 Unfortunately, we do have to make multiple passes over the symbol
4709 table until nothing further is resolved. */
4711 static bfd_boolean
4712 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4714 symindex c;
4715 bfd_boolean *defined = NULL;
4716 bfd_boolean *included = NULL;
4717 carsym *symdefs;
4718 bfd_boolean loop;
4719 bfd_size_type amt;
4720 const struct elf_backend_data *bed;
4721 struct elf_link_hash_entry * (*archive_symbol_lookup)
4722 (bfd *, struct bfd_link_info *, const char *);
4724 if (! bfd_has_map (abfd))
4726 /* An empty archive is a special case. */
4727 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4728 return TRUE;
4729 bfd_set_error (bfd_error_no_armap);
4730 return FALSE;
4733 /* Keep track of all symbols we know to be already defined, and all
4734 files we know to be already included. This is to speed up the
4735 second and subsequent passes. */
4736 c = bfd_ardata (abfd)->symdef_count;
4737 if (c == 0)
4738 return TRUE;
4739 amt = c;
4740 amt *= sizeof (bfd_boolean);
4741 defined = bfd_zmalloc (amt);
4742 included = bfd_zmalloc (amt);
4743 if (defined == NULL || included == NULL)
4744 goto error_return;
4746 symdefs = bfd_ardata (abfd)->symdefs;
4747 bed = get_elf_backend_data (abfd);
4748 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4752 file_ptr last;
4753 symindex i;
4754 carsym *symdef;
4755 carsym *symdefend;
4757 loop = FALSE;
4758 last = -1;
4760 symdef = symdefs;
4761 symdefend = symdef + c;
4762 for (i = 0; symdef < symdefend; symdef++, i++)
4764 struct elf_link_hash_entry *h;
4765 bfd *element;
4766 struct bfd_link_hash_entry *undefs_tail;
4767 symindex mark;
4769 if (defined[i] || included[i])
4770 continue;
4771 if (symdef->file_offset == last)
4773 included[i] = TRUE;
4774 continue;
4777 h = archive_symbol_lookup (abfd, info, symdef->name);
4778 if (h == (struct elf_link_hash_entry *) 0 - 1)
4779 goto error_return;
4781 if (h == NULL)
4782 continue;
4784 if (h->root.type == bfd_link_hash_common)
4786 /* We currently have a common symbol. The archive map contains
4787 a reference to this symbol, so we may want to include it. We
4788 only want to include it however, if this archive element
4789 contains a definition of the symbol, not just another common
4790 declaration of it.
4792 Unfortunately some archivers (including GNU ar) will put
4793 declarations of common symbols into their archive maps, as
4794 well as real definitions, so we cannot just go by the archive
4795 map alone. Instead we must read in the element's symbol
4796 table and check that to see what kind of symbol definition
4797 this is. */
4798 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4799 continue;
4801 else if (h->root.type != bfd_link_hash_undefined)
4803 if (h->root.type != bfd_link_hash_undefweak)
4804 defined[i] = TRUE;
4805 continue;
4808 /* We need to include this archive member. */
4809 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4810 if (element == NULL)
4811 goto error_return;
4813 if (! bfd_check_format (element, bfd_object))
4814 goto error_return;
4816 /* Doublecheck that we have not included this object
4817 already--it should be impossible, but there may be
4818 something wrong with the archive. */
4819 if (element->archive_pass != 0)
4821 bfd_set_error (bfd_error_bad_value);
4822 goto error_return;
4824 element->archive_pass = 1;
4826 undefs_tail = info->hash->undefs_tail;
4828 if (! (*info->callbacks->add_archive_element) (info, element,
4829 symdef->name))
4830 goto error_return;
4831 if (! bfd_link_add_symbols (element, info))
4832 goto error_return;
4834 /* If there are any new undefined symbols, we need to make
4835 another pass through the archive in order to see whether
4836 they can be defined. FIXME: This isn't perfect, because
4837 common symbols wind up on undefs_tail and because an
4838 undefined symbol which is defined later on in this pass
4839 does not require another pass. This isn't a bug, but it
4840 does make the code less efficient than it could be. */
4841 if (undefs_tail != info->hash->undefs_tail)
4842 loop = TRUE;
4844 /* Look backward to mark all symbols from this object file
4845 which we have already seen in this pass. */
4846 mark = i;
4849 included[mark] = TRUE;
4850 if (mark == 0)
4851 break;
4852 --mark;
4854 while (symdefs[mark].file_offset == symdef->file_offset);
4856 /* We mark subsequent symbols from this object file as we go
4857 on through the loop. */
4858 last = symdef->file_offset;
4861 while (loop);
4863 free (defined);
4864 free (included);
4866 return TRUE;
4868 error_return:
4869 if (defined != NULL)
4870 free (defined);
4871 if (included != NULL)
4872 free (included);
4873 return FALSE;
4876 /* Given an ELF BFD, add symbols to the global hash table as
4877 appropriate. */
4879 bfd_boolean
4880 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4882 switch (bfd_get_format (abfd))
4884 case bfd_object:
4885 return elf_link_add_object_symbols (abfd, info);
4886 case bfd_archive:
4887 return elf_link_add_archive_symbols (abfd, info);
4888 default:
4889 bfd_set_error (bfd_error_wrong_format);
4890 return FALSE;
4894 /* This function will be called though elf_link_hash_traverse to store
4895 all hash value of the exported symbols in an array. */
4897 static bfd_boolean
4898 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4900 unsigned long **valuep = data;
4901 const char *name;
4902 char *p;
4903 unsigned long ha;
4904 char *alc = NULL;
4906 if (h->root.type == bfd_link_hash_warning)
4907 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4909 /* Ignore indirect symbols. These are added by the versioning code. */
4910 if (h->dynindx == -1)
4911 return TRUE;
4913 name = h->root.root.string;
4914 p = strchr (name, ELF_VER_CHR);
4915 if (p != NULL)
4917 alc = bfd_malloc (p - name + 1);
4918 memcpy (alc, name, p - name);
4919 alc[p - name] = '\0';
4920 name = alc;
4923 /* Compute the hash value. */
4924 ha = bfd_elf_hash (name);
4926 /* Store the found hash value in the array given as the argument. */
4927 *(*valuep)++ = ha;
4929 /* And store it in the struct so that we can put it in the hash table
4930 later. */
4931 h->u.elf_hash_value = ha;
4933 if (alc != NULL)
4934 free (alc);
4936 return TRUE;
4939 struct collect_gnu_hash_codes
4941 bfd *output_bfd;
4942 const struct elf_backend_data *bed;
4943 unsigned long int nsyms;
4944 unsigned long int maskbits;
4945 unsigned long int *hashcodes;
4946 unsigned long int *hashval;
4947 unsigned long int *indx;
4948 unsigned long int *counts;
4949 bfd_vma *bitmask;
4950 bfd_byte *contents;
4951 long int min_dynindx;
4952 unsigned long int bucketcount;
4953 unsigned long int symindx;
4954 long int local_indx;
4955 long int shift1, shift2;
4956 unsigned long int mask;
4959 /* This function will be called though elf_link_hash_traverse to store
4960 all hash value of the exported symbols in an array. */
4962 static bfd_boolean
4963 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
4965 struct collect_gnu_hash_codes *s = data;
4966 const char *name;
4967 char *p;
4968 unsigned long ha;
4969 char *alc = NULL;
4971 if (h->root.type == bfd_link_hash_warning)
4972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4974 /* Ignore indirect symbols. These are added by the versioning code. */
4975 if (h->dynindx == -1)
4976 return TRUE;
4978 /* Ignore also local symbols and undefined symbols. */
4979 if (! (*s->bed->elf_hash_symbol) (h))
4980 return TRUE;
4982 name = h->root.root.string;
4983 p = strchr (name, ELF_VER_CHR);
4984 if (p != NULL)
4986 alc = bfd_malloc (p - name + 1);
4987 memcpy (alc, name, p - name);
4988 alc[p - name] = '\0';
4989 name = alc;
4992 /* Compute the hash value. */
4993 ha = bfd_elf_gnu_hash (name);
4995 /* Store the found hash value in the array for compute_bucket_count,
4996 and also for .dynsym reordering purposes. */
4997 s->hashcodes[s->nsyms] = ha;
4998 s->hashval[h->dynindx] = ha;
4999 ++s->nsyms;
5000 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5001 s->min_dynindx = h->dynindx;
5003 if (alc != NULL)
5004 free (alc);
5006 return TRUE;
5009 /* This function will be called though elf_link_hash_traverse to do
5010 final dynaminc symbol renumbering. */
5012 static bfd_boolean
5013 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5015 struct collect_gnu_hash_codes *s = data;
5016 unsigned long int bucket;
5017 unsigned long int val;
5019 if (h->root.type == bfd_link_hash_warning)
5020 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5022 /* Ignore indirect symbols. */
5023 if (h->dynindx == -1)
5024 return TRUE;
5026 /* Ignore also local symbols and undefined symbols. */
5027 if (! (*s->bed->elf_hash_symbol) (h))
5029 if (h->dynindx >= s->min_dynindx)
5030 h->dynindx = s->local_indx++;
5031 return TRUE;
5034 bucket = s->hashval[h->dynindx] % s->bucketcount;
5035 val = (s->hashval[h->dynindx] >> s->shift1)
5036 & ((s->maskbits >> s->shift1) - 1);
5037 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5038 s->bitmask[val]
5039 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5040 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5041 if (s->counts[bucket] == 1)
5042 /* Last element terminates the chain. */
5043 val |= 1;
5044 bfd_put_32 (s->output_bfd, val,
5045 s->contents + (s->indx[bucket] - s->symindx) * 4);
5046 --s->counts[bucket];
5047 h->dynindx = s->indx[bucket]++;
5048 return TRUE;
5051 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5053 bfd_boolean
5054 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5056 return !(h->forced_local
5057 || h->root.type == bfd_link_hash_undefined
5058 || h->root.type == bfd_link_hash_undefweak
5059 || ((h->root.type == bfd_link_hash_defined
5060 || h->root.type == bfd_link_hash_defweak)
5061 && h->root.u.def.section->output_section == NULL));
5064 /* Array used to determine the number of hash table buckets to use
5065 based on the number of symbols there are. If there are fewer than
5066 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5067 fewer than 37 we use 17 buckets, and so forth. We never use more
5068 than 32771 buckets. */
5070 static const size_t elf_buckets[] =
5072 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5073 16411, 32771, 0
5076 /* Compute bucket count for hashing table. We do not use a static set
5077 of possible tables sizes anymore. Instead we determine for all
5078 possible reasonable sizes of the table the outcome (i.e., the
5079 number of collisions etc) and choose the best solution. The
5080 weighting functions are not too simple to allow the table to grow
5081 without bounds. Instead one of the weighting factors is the size.
5082 Therefore the result is always a good payoff between few collisions
5083 (= short chain lengths) and table size. */
5084 static size_t
5085 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5086 unsigned long int nsyms, int gnu_hash)
5088 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5089 size_t best_size = 0;
5090 unsigned long int i;
5091 bfd_size_type amt;
5093 /* We have a problem here. The following code to optimize the table
5094 size requires an integer type with more the 32 bits. If
5095 BFD_HOST_U_64_BIT is set we know about such a type. */
5096 #ifdef BFD_HOST_U_64_BIT
5097 if (info->optimize)
5099 size_t minsize;
5100 size_t maxsize;
5101 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5102 bfd *dynobj = elf_hash_table (info)->dynobj;
5103 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5104 unsigned long int *counts;
5106 /* Possible optimization parameters: if we have NSYMS symbols we say
5107 that the hashing table must at least have NSYMS/4 and at most
5108 2*NSYMS buckets. */
5109 minsize = nsyms / 4;
5110 if (minsize == 0)
5111 minsize = 1;
5112 best_size = maxsize = nsyms * 2;
5113 if (gnu_hash)
5115 if (minsize < 2)
5116 minsize = 2;
5117 if ((best_size & 31) == 0)
5118 ++best_size;
5121 /* Create array where we count the collisions in. We must use bfd_malloc
5122 since the size could be large. */
5123 amt = maxsize;
5124 amt *= sizeof (unsigned long int);
5125 counts = bfd_malloc (amt);
5126 if (counts == NULL)
5127 return 0;
5129 /* Compute the "optimal" size for the hash table. The criteria is a
5130 minimal chain length. The minor criteria is (of course) the size
5131 of the table. */
5132 for (i = minsize; i < maxsize; ++i)
5134 /* Walk through the array of hashcodes and count the collisions. */
5135 BFD_HOST_U_64_BIT max;
5136 unsigned long int j;
5137 unsigned long int fact;
5139 if (gnu_hash && (i & 31) == 0)
5140 continue;
5142 memset (counts, '\0', i * sizeof (unsigned long int));
5144 /* Determine how often each hash bucket is used. */
5145 for (j = 0; j < nsyms; ++j)
5146 ++counts[hashcodes[j] % i];
5148 /* For the weight function we need some information about the
5149 pagesize on the target. This is information need not be 100%
5150 accurate. Since this information is not available (so far) we
5151 define it here to a reasonable default value. If it is crucial
5152 to have a better value some day simply define this value. */
5153 # ifndef BFD_TARGET_PAGESIZE
5154 # define BFD_TARGET_PAGESIZE (4096)
5155 # endif
5157 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5158 and the chains. */
5159 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5161 # if 1
5162 /* Variant 1: optimize for short chains. We add the squares
5163 of all the chain lengths (which favors many small chain
5164 over a few long chains). */
5165 for (j = 0; j < i; ++j)
5166 max += counts[j] * counts[j];
5168 /* This adds penalties for the overall size of the table. */
5169 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5170 max *= fact * fact;
5171 # else
5172 /* Variant 2: Optimize a lot more for small table. Here we
5173 also add squares of the size but we also add penalties for
5174 empty slots (the +1 term). */
5175 for (j = 0; j < i; ++j)
5176 max += (1 + counts[j]) * (1 + counts[j]);
5178 /* The overall size of the table is considered, but not as
5179 strong as in variant 1, where it is squared. */
5180 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5181 max *= fact;
5182 # endif
5184 /* Compare with current best results. */
5185 if (max < best_chlen)
5187 best_chlen = max;
5188 best_size = i;
5192 free (counts);
5194 else
5195 #endif /* defined (BFD_HOST_U_64_BIT) */
5197 /* This is the fallback solution if no 64bit type is available or if we
5198 are not supposed to spend much time on optimizations. We select the
5199 bucket count using a fixed set of numbers. */
5200 for (i = 0; elf_buckets[i] != 0; i++)
5202 best_size = elf_buckets[i];
5203 if (nsyms < elf_buckets[i + 1])
5204 break;
5206 if (gnu_hash && best_size < 2)
5207 best_size = 2;
5210 return best_size;
5213 /* Set up the sizes and contents of the ELF dynamic sections. This is
5214 called by the ELF linker emulation before_allocation routine. We
5215 must set the sizes of the sections before the linker sets the
5216 addresses of the various sections. */
5218 bfd_boolean
5219 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5220 const char *soname,
5221 const char *rpath,
5222 const char *filter_shlib,
5223 const char * const *auxiliary_filters,
5224 struct bfd_link_info *info,
5225 asection **sinterpptr,
5226 struct bfd_elf_version_tree *verdefs)
5228 bfd_size_type soname_indx;
5229 bfd *dynobj;
5230 const struct elf_backend_data *bed;
5231 struct elf_assign_sym_version_info asvinfo;
5233 *sinterpptr = NULL;
5235 soname_indx = (bfd_size_type) -1;
5237 if (!is_elf_hash_table (info->hash))
5238 return TRUE;
5240 bed = get_elf_backend_data (output_bfd);
5241 elf_tdata (output_bfd)->relro = info->relro;
5242 if (info->execstack)
5243 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5244 else if (info->noexecstack)
5245 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5246 else
5248 bfd *inputobj;
5249 asection *notesec = NULL;
5250 int exec = 0;
5252 for (inputobj = info->input_bfds;
5253 inputobj;
5254 inputobj = inputobj->link_next)
5256 asection *s;
5258 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5259 continue;
5260 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5261 if (s)
5263 if (s->flags & SEC_CODE)
5264 exec = PF_X;
5265 notesec = s;
5267 else if (bed->default_execstack)
5268 exec = PF_X;
5270 if (notesec)
5272 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5273 if (exec && info->relocatable
5274 && notesec->output_section != bfd_abs_section_ptr)
5275 notesec->output_section->flags |= SEC_CODE;
5279 /* Any syms created from now on start with -1 in
5280 got.refcount/offset and plt.refcount/offset. */
5281 elf_hash_table (info)->init_got_refcount
5282 = elf_hash_table (info)->init_got_offset;
5283 elf_hash_table (info)->init_plt_refcount
5284 = elf_hash_table (info)->init_plt_offset;
5286 /* The backend may have to create some sections regardless of whether
5287 we're dynamic or not. */
5288 if (bed->elf_backend_always_size_sections
5289 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5290 return FALSE;
5292 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5293 return FALSE;
5295 dynobj = elf_hash_table (info)->dynobj;
5297 /* If there were no dynamic objects in the link, there is nothing to
5298 do here. */
5299 if (dynobj == NULL)
5300 return TRUE;
5302 if (elf_hash_table (info)->dynamic_sections_created)
5304 struct elf_info_failed eif;
5305 struct elf_link_hash_entry *h;
5306 asection *dynstr;
5307 struct bfd_elf_version_tree *t;
5308 struct bfd_elf_version_expr *d;
5309 asection *s;
5310 bfd_boolean all_defined;
5312 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5313 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5315 if (soname != NULL)
5317 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5318 soname, TRUE);
5319 if (soname_indx == (bfd_size_type) -1
5320 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5321 return FALSE;
5324 if (info->symbolic)
5326 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5327 return FALSE;
5328 info->flags |= DF_SYMBOLIC;
5331 if (rpath != NULL)
5333 bfd_size_type indx;
5335 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5336 TRUE);
5337 if (indx == (bfd_size_type) -1
5338 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5339 return FALSE;
5341 if (info->new_dtags)
5343 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5344 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5345 return FALSE;
5349 if (filter_shlib != NULL)
5351 bfd_size_type indx;
5353 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5354 filter_shlib, TRUE);
5355 if (indx == (bfd_size_type) -1
5356 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5357 return FALSE;
5360 if (auxiliary_filters != NULL)
5362 const char * const *p;
5364 for (p = auxiliary_filters; *p != NULL; p++)
5366 bfd_size_type indx;
5368 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5369 *p, TRUE);
5370 if (indx == (bfd_size_type) -1
5371 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5372 return FALSE;
5376 eif.info = info;
5377 eif.verdefs = verdefs;
5378 eif.failed = FALSE;
5380 /* If we are supposed to export all symbols into the dynamic symbol
5381 table (this is not the normal case), then do so. */
5382 if (info->export_dynamic
5383 || (info->executable && info->dynamic))
5385 elf_link_hash_traverse (elf_hash_table (info),
5386 _bfd_elf_export_symbol,
5387 &eif);
5388 if (eif.failed)
5389 return FALSE;
5392 /* Make all global versions with definition. */
5393 for (t = verdefs; t != NULL; t = t->next)
5394 for (d = t->globals.list; d != NULL; d = d->next)
5395 if (!d->symver && d->symbol)
5397 const char *verstr, *name;
5398 size_t namelen, verlen, newlen;
5399 char *newname, *p;
5400 struct elf_link_hash_entry *newh;
5402 name = d->symbol;
5403 namelen = strlen (name);
5404 verstr = t->name;
5405 verlen = strlen (verstr);
5406 newlen = namelen + verlen + 3;
5408 newname = bfd_malloc (newlen);
5409 if (newname == NULL)
5410 return FALSE;
5411 memcpy (newname, name, namelen);
5413 /* Check the hidden versioned definition. */
5414 p = newname + namelen;
5415 *p++ = ELF_VER_CHR;
5416 memcpy (p, verstr, verlen + 1);
5417 newh = elf_link_hash_lookup (elf_hash_table (info),
5418 newname, FALSE, FALSE,
5419 FALSE);
5420 if (newh == NULL
5421 || (newh->root.type != bfd_link_hash_defined
5422 && newh->root.type != bfd_link_hash_defweak))
5424 /* Check the default versioned definition. */
5425 *p++ = ELF_VER_CHR;
5426 memcpy (p, verstr, verlen + 1);
5427 newh = elf_link_hash_lookup (elf_hash_table (info),
5428 newname, FALSE, FALSE,
5429 FALSE);
5431 free (newname);
5433 /* Mark this version if there is a definition and it is
5434 not defined in a shared object. */
5435 if (newh != NULL
5436 && !newh->def_dynamic
5437 && (newh->root.type == bfd_link_hash_defined
5438 || newh->root.type == bfd_link_hash_defweak))
5439 d->symver = 1;
5442 /* Attach all the symbols to their version information. */
5443 asvinfo.output_bfd = output_bfd;
5444 asvinfo.info = info;
5445 asvinfo.verdefs = verdefs;
5446 asvinfo.failed = FALSE;
5448 elf_link_hash_traverse (elf_hash_table (info),
5449 _bfd_elf_link_assign_sym_version,
5450 &asvinfo);
5451 if (asvinfo.failed)
5452 return FALSE;
5454 if (!info->allow_undefined_version)
5456 /* Check if all global versions have a definition. */
5457 all_defined = TRUE;
5458 for (t = verdefs; t != NULL; t = t->next)
5459 for (d = t->globals.list; d != NULL; d = d->next)
5460 if (!d->symver && !d->script)
5462 (*_bfd_error_handler)
5463 (_("%s: undefined version: %s"),
5464 d->pattern, t->name);
5465 all_defined = FALSE;
5468 if (!all_defined)
5470 bfd_set_error (bfd_error_bad_value);
5471 return FALSE;
5475 /* Find all symbols which were defined in a dynamic object and make
5476 the backend pick a reasonable value for them. */
5477 elf_link_hash_traverse (elf_hash_table (info),
5478 _bfd_elf_adjust_dynamic_symbol,
5479 &eif);
5480 if (eif.failed)
5481 return FALSE;
5483 /* Add some entries to the .dynamic section. We fill in some of the
5484 values later, in bfd_elf_final_link, but we must add the entries
5485 now so that we know the final size of the .dynamic section. */
5487 /* If there are initialization and/or finalization functions to
5488 call then add the corresponding DT_INIT/DT_FINI entries. */
5489 h = (info->init_function
5490 ? elf_link_hash_lookup (elf_hash_table (info),
5491 info->init_function, FALSE,
5492 FALSE, FALSE)
5493 : NULL);
5494 if (h != NULL
5495 && (h->ref_regular
5496 || h->def_regular))
5498 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5499 return FALSE;
5501 h = (info->fini_function
5502 ? elf_link_hash_lookup (elf_hash_table (info),
5503 info->fini_function, FALSE,
5504 FALSE, FALSE)
5505 : NULL);
5506 if (h != NULL
5507 && (h->ref_regular
5508 || h->def_regular))
5510 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5511 return FALSE;
5514 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5515 if (s != NULL && s->linker_has_input)
5517 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5518 if (! info->executable)
5520 bfd *sub;
5521 asection *o;
5523 for (sub = info->input_bfds; sub != NULL;
5524 sub = sub->link_next)
5525 for (o = sub->sections; o != NULL; o = o->next)
5526 if (elf_section_data (o)->this_hdr.sh_type
5527 == SHT_PREINIT_ARRAY)
5529 (*_bfd_error_handler)
5530 (_("%B: .preinit_array section is not allowed in DSO"),
5531 sub);
5532 break;
5535 bfd_set_error (bfd_error_nonrepresentable_section);
5536 return FALSE;
5539 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5540 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5541 return FALSE;
5543 s = bfd_get_section_by_name (output_bfd, ".init_array");
5544 if (s != NULL && s->linker_has_input)
5546 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5547 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5548 return FALSE;
5550 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5551 if (s != NULL && s->linker_has_input)
5553 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5554 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5555 return FALSE;
5558 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5559 /* If .dynstr is excluded from the link, we don't want any of
5560 these tags. Strictly, we should be checking each section
5561 individually; This quick check covers for the case where
5562 someone does a /DISCARD/ : { *(*) }. */
5563 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5565 bfd_size_type strsize;
5567 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5568 if ((info->emit_hash
5569 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5570 || (info->emit_gnu_hash
5571 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5572 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5573 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5574 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5575 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5576 bed->s->sizeof_sym))
5577 return FALSE;
5581 /* The backend must work out the sizes of all the other dynamic
5582 sections. */
5583 if (bed->elf_backend_size_dynamic_sections
5584 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5585 return FALSE;
5587 if (elf_hash_table (info)->dynamic_sections_created)
5589 unsigned long section_sym_count;
5590 asection *s;
5592 /* Set up the version definition section. */
5593 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5594 BFD_ASSERT (s != NULL);
5596 /* We may have created additional version definitions if we are
5597 just linking a regular application. */
5598 verdefs = asvinfo.verdefs;
5600 /* Skip anonymous version tag. */
5601 if (verdefs != NULL && verdefs->vernum == 0)
5602 verdefs = verdefs->next;
5604 if (verdefs == NULL && !info->create_default_symver)
5605 s->flags |= SEC_EXCLUDE;
5606 else
5608 unsigned int cdefs;
5609 bfd_size_type size;
5610 struct bfd_elf_version_tree *t;
5611 bfd_byte *p;
5612 Elf_Internal_Verdef def;
5613 Elf_Internal_Verdaux defaux;
5614 struct bfd_link_hash_entry *bh;
5615 struct elf_link_hash_entry *h;
5616 const char *name;
5618 cdefs = 0;
5619 size = 0;
5621 /* Make space for the base version. */
5622 size += sizeof (Elf_External_Verdef);
5623 size += sizeof (Elf_External_Verdaux);
5624 ++cdefs;
5626 /* Make space for the default version. */
5627 if (info->create_default_symver)
5629 size += sizeof (Elf_External_Verdef);
5630 ++cdefs;
5633 for (t = verdefs; t != NULL; t = t->next)
5635 struct bfd_elf_version_deps *n;
5637 size += sizeof (Elf_External_Verdef);
5638 size += sizeof (Elf_External_Verdaux);
5639 ++cdefs;
5641 for (n = t->deps; n != NULL; n = n->next)
5642 size += sizeof (Elf_External_Verdaux);
5645 s->size = size;
5646 s->contents = bfd_alloc (output_bfd, s->size);
5647 if (s->contents == NULL && s->size != 0)
5648 return FALSE;
5650 /* Fill in the version definition section. */
5652 p = s->contents;
5654 def.vd_version = VER_DEF_CURRENT;
5655 def.vd_flags = VER_FLG_BASE;
5656 def.vd_ndx = 1;
5657 def.vd_cnt = 1;
5658 if (info->create_default_symver)
5660 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5661 def.vd_next = sizeof (Elf_External_Verdef);
5663 else
5665 def.vd_aux = sizeof (Elf_External_Verdef);
5666 def.vd_next = (sizeof (Elf_External_Verdef)
5667 + sizeof (Elf_External_Verdaux));
5670 if (soname_indx != (bfd_size_type) -1)
5672 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5673 soname_indx);
5674 def.vd_hash = bfd_elf_hash (soname);
5675 defaux.vda_name = soname_indx;
5676 name = soname;
5678 else
5680 bfd_size_type indx;
5682 name = lbasename (output_bfd->filename);
5683 def.vd_hash = bfd_elf_hash (name);
5684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5685 name, FALSE);
5686 if (indx == (bfd_size_type) -1)
5687 return FALSE;
5688 defaux.vda_name = indx;
5690 defaux.vda_next = 0;
5692 _bfd_elf_swap_verdef_out (output_bfd, &def,
5693 (Elf_External_Verdef *) p);
5694 p += sizeof (Elf_External_Verdef);
5695 if (info->create_default_symver)
5697 /* Add a symbol representing this version. */
5698 bh = NULL;
5699 if (! (_bfd_generic_link_add_one_symbol
5700 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5701 0, NULL, FALSE,
5702 get_elf_backend_data (dynobj)->collect, &bh)))
5703 return FALSE;
5704 h = (struct elf_link_hash_entry *) bh;
5705 h->non_elf = 0;
5706 h->def_regular = 1;
5707 h->type = STT_OBJECT;
5708 h->verinfo.vertree = NULL;
5710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5711 return FALSE;
5713 /* Create a duplicate of the base version with the same
5714 aux block, but different flags. */
5715 def.vd_flags = 0;
5716 def.vd_ndx = 2;
5717 def.vd_aux = sizeof (Elf_External_Verdef);
5718 if (verdefs)
5719 def.vd_next = (sizeof (Elf_External_Verdef)
5720 + sizeof (Elf_External_Verdaux));
5721 else
5722 def.vd_next = 0;
5723 _bfd_elf_swap_verdef_out (output_bfd, &def,
5724 (Elf_External_Verdef *) p);
5725 p += sizeof (Elf_External_Verdef);
5727 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5728 (Elf_External_Verdaux *) p);
5729 p += sizeof (Elf_External_Verdaux);
5731 for (t = verdefs; t != NULL; t = t->next)
5733 unsigned int cdeps;
5734 struct bfd_elf_version_deps *n;
5736 cdeps = 0;
5737 for (n = t->deps; n != NULL; n = n->next)
5738 ++cdeps;
5740 /* Add a symbol representing this version. */
5741 bh = NULL;
5742 if (! (_bfd_generic_link_add_one_symbol
5743 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5744 0, NULL, FALSE,
5745 get_elf_backend_data (dynobj)->collect, &bh)))
5746 return FALSE;
5747 h = (struct elf_link_hash_entry *) bh;
5748 h->non_elf = 0;
5749 h->def_regular = 1;
5750 h->type = STT_OBJECT;
5751 h->verinfo.vertree = t;
5753 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5754 return FALSE;
5756 def.vd_version = VER_DEF_CURRENT;
5757 def.vd_flags = 0;
5758 if (t->globals.list == NULL
5759 && t->locals.list == NULL
5760 && ! t->used)
5761 def.vd_flags |= VER_FLG_WEAK;
5762 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5763 def.vd_cnt = cdeps + 1;
5764 def.vd_hash = bfd_elf_hash (t->name);
5765 def.vd_aux = sizeof (Elf_External_Verdef);
5766 def.vd_next = 0;
5767 if (t->next != NULL)
5768 def.vd_next = (sizeof (Elf_External_Verdef)
5769 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5771 _bfd_elf_swap_verdef_out (output_bfd, &def,
5772 (Elf_External_Verdef *) p);
5773 p += sizeof (Elf_External_Verdef);
5775 defaux.vda_name = h->dynstr_index;
5776 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5777 h->dynstr_index);
5778 defaux.vda_next = 0;
5779 if (t->deps != NULL)
5780 defaux.vda_next = sizeof (Elf_External_Verdaux);
5781 t->name_indx = defaux.vda_name;
5783 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5784 (Elf_External_Verdaux *) p);
5785 p += sizeof (Elf_External_Verdaux);
5787 for (n = t->deps; n != NULL; n = n->next)
5789 if (n->version_needed == NULL)
5791 /* This can happen if there was an error in the
5792 version script. */
5793 defaux.vda_name = 0;
5795 else
5797 defaux.vda_name = n->version_needed->name_indx;
5798 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5799 defaux.vda_name);
5801 if (n->next == NULL)
5802 defaux.vda_next = 0;
5803 else
5804 defaux.vda_next = sizeof (Elf_External_Verdaux);
5806 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5807 (Elf_External_Verdaux *) p);
5808 p += sizeof (Elf_External_Verdaux);
5812 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5813 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5814 return FALSE;
5816 elf_tdata (output_bfd)->cverdefs = cdefs;
5819 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5821 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5822 return FALSE;
5824 else if (info->flags & DF_BIND_NOW)
5826 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5827 return FALSE;
5830 if (info->flags_1)
5832 if (info->executable)
5833 info->flags_1 &= ~ (DF_1_INITFIRST
5834 | DF_1_NODELETE
5835 | DF_1_NOOPEN);
5836 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5837 return FALSE;
5840 /* Work out the size of the version reference section. */
5842 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5843 BFD_ASSERT (s != NULL);
5845 struct elf_find_verdep_info sinfo;
5847 sinfo.output_bfd = output_bfd;
5848 sinfo.info = info;
5849 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5850 if (sinfo.vers == 0)
5851 sinfo.vers = 1;
5852 sinfo.failed = FALSE;
5854 elf_link_hash_traverse (elf_hash_table (info),
5855 _bfd_elf_link_find_version_dependencies,
5856 &sinfo);
5858 if (elf_tdata (output_bfd)->verref == NULL)
5859 s->flags |= SEC_EXCLUDE;
5860 else
5862 Elf_Internal_Verneed *t;
5863 unsigned int size;
5864 unsigned int crefs;
5865 bfd_byte *p;
5867 /* Build the version definition section. */
5868 size = 0;
5869 crefs = 0;
5870 for (t = elf_tdata (output_bfd)->verref;
5871 t != NULL;
5872 t = t->vn_nextref)
5874 Elf_Internal_Vernaux *a;
5876 size += sizeof (Elf_External_Verneed);
5877 ++crefs;
5878 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5879 size += sizeof (Elf_External_Vernaux);
5882 s->size = size;
5883 s->contents = bfd_alloc (output_bfd, s->size);
5884 if (s->contents == NULL)
5885 return FALSE;
5887 p = s->contents;
5888 for (t = elf_tdata (output_bfd)->verref;
5889 t != NULL;
5890 t = t->vn_nextref)
5892 unsigned int caux;
5893 Elf_Internal_Vernaux *a;
5894 bfd_size_type indx;
5896 caux = 0;
5897 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5898 ++caux;
5900 t->vn_version = VER_NEED_CURRENT;
5901 t->vn_cnt = caux;
5902 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5903 elf_dt_name (t->vn_bfd) != NULL
5904 ? elf_dt_name (t->vn_bfd)
5905 : lbasename (t->vn_bfd->filename),
5906 FALSE);
5907 if (indx == (bfd_size_type) -1)
5908 return FALSE;
5909 t->vn_file = indx;
5910 t->vn_aux = sizeof (Elf_External_Verneed);
5911 if (t->vn_nextref == NULL)
5912 t->vn_next = 0;
5913 else
5914 t->vn_next = (sizeof (Elf_External_Verneed)
5915 + caux * sizeof (Elf_External_Vernaux));
5917 _bfd_elf_swap_verneed_out (output_bfd, t,
5918 (Elf_External_Verneed *) p);
5919 p += sizeof (Elf_External_Verneed);
5921 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5923 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5924 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5925 a->vna_nodename, FALSE);
5926 if (indx == (bfd_size_type) -1)
5927 return FALSE;
5928 a->vna_name = indx;
5929 if (a->vna_nextptr == NULL)
5930 a->vna_next = 0;
5931 else
5932 a->vna_next = sizeof (Elf_External_Vernaux);
5934 _bfd_elf_swap_vernaux_out (output_bfd, a,
5935 (Elf_External_Vernaux *) p);
5936 p += sizeof (Elf_External_Vernaux);
5940 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5941 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5942 return FALSE;
5944 elf_tdata (output_bfd)->cverrefs = crefs;
5948 if ((elf_tdata (output_bfd)->cverrefs == 0
5949 && elf_tdata (output_bfd)->cverdefs == 0)
5950 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5951 &section_sym_count) == 0)
5953 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5954 s->flags |= SEC_EXCLUDE;
5957 return TRUE;
5960 /* Find the first non-excluded output section. We'll use its
5961 section symbol for some emitted relocs. */
5962 void
5963 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
5965 asection *s;
5967 for (s = output_bfd->sections; s != NULL; s = s->next)
5968 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
5969 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5971 elf_hash_table (info)->text_index_section = s;
5972 break;
5976 /* Find two non-excluded output sections, one for code, one for data.
5977 We'll use their section symbols for some emitted relocs. */
5978 void
5979 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
5981 asection *s;
5983 for (s = output_bfd->sections; s != NULL; s = s->next)
5984 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
5985 == (SEC_ALLOC | SEC_READONLY))
5986 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5988 elf_hash_table (info)->text_index_section = s;
5989 break;
5992 for (s = output_bfd->sections; s != NULL; s = s->next)
5993 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
5994 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5996 elf_hash_table (info)->data_index_section = s;
5997 break;
6000 if (elf_hash_table (info)->text_index_section == NULL)
6001 elf_hash_table (info)->text_index_section
6002 = elf_hash_table (info)->data_index_section;
6005 bfd_boolean
6006 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6008 const struct elf_backend_data *bed;
6010 if (!is_elf_hash_table (info->hash))
6011 return TRUE;
6013 bed = get_elf_backend_data (output_bfd);
6014 (*bed->elf_backend_init_index_section) (output_bfd, info);
6016 if (elf_hash_table (info)->dynamic_sections_created)
6018 bfd *dynobj;
6019 asection *s;
6020 bfd_size_type dynsymcount;
6021 unsigned long section_sym_count;
6022 unsigned int dtagcount;
6024 dynobj = elf_hash_table (info)->dynobj;
6026 /* Assign dynsym indicies. In a shared library we generate a
6027 section symbol for each output section, which come first.
6028 Next come all of the back-end allocated local dynamic syms,
6029 followed by the rest of the global symbols. */
6031 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6032 &section_sym_count);
6034 /* Work out the size of the symbol version section. */
6035 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6036 BFD_ASSERT (s != NULL);
6037 if (dynsymcount != 0
6038 && (s->flags & SEC_EXCLUDE) == 0)
6040 s->size = dynsymcount * sizeof (Elf_External_Versym);
6041 s->contents = bfd_zalloc (output_bfd, s->size);
6042 if (s->contents == NULL)
6043 return FALSE;
6045 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6046 return FALSE;
6049 /* Set the size of the .dynsym and .hash sections. We counted
6050 the number of dynamic symbols in elf_link_add_object_symbols.
6051 We will build the contents of .dynsym and .hash when we build
6052 the final symbol table, because until then we do not know the
6053 correct value to give the symbols. We built the .dynstr
6054 section as we went along in elf_link_add_object_symbols. */
6055 s = bfd_get_section_by_name (dynobj, ".dynsym");
6056 BFD_ASSERT (s != NULL);
6057 s->size = dynsymcount * bed->s->sizeof_sym;
6059 if (dynsymcount != 0)
6061 s->contents = bfd_alloc (output_bfd, s->size);
6062 if (s->contents == NULL)
6063 return FALSE;
6065 /* The first entry in .dynsym is a dummy symbol.
6066 Clear all the section syms, in case we don't output them all. */
6067 ++section_sym_count;
6068 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6071 elf_hash_table (info)->bucketcount = 0;
6073 /* Compute the size of the hashing table. As a side effect this
6074 computes the hash values for all the names we export. */
6075 if (info->emit_hash)
6077 unsigned long int *hashcodes;
6078 unsigned long int *hashcodesp;
6079 bfd_size_type amt;
6080 unsigned long int nsyms;
6081 size_t bucketcount;
6082 size_t hash_entry_size;
6084 /* Compute the hash values for all exported symbols. At the same
6085 time store the values in an array so that we could use them for
6086 optimizations. */
6087 amt = dynsymcount * sizeof (unsigned long int);
6088 hashcodes = bfd_malloc (amt);
6089 if (hashcodes == NULL)
6090 return FALSE;
6091 hashcodesp = hashcodes;
6093 /* Put all hash values in HASHCODES. */
6094 elf_link_hash_traverse (elf_hash_table (info),
6095 elf_collect_hash_codes, &hashcodesp);
6097 nsyms = hashcodesp - hashcodes;
6098 bucketcount
6099 = compute_bucket_count (info, hashcodes, nsyms, 0);
6100 free (hashcodes);
6102 if (bucketcount == 0)
6103 return FALSE;
6105 elf_hash_table (info)->bucketcount = bucketcount;
6107 s = bfd_get_section_by_name (dynobj, ".hash");
6108 BFD_ASSERT (s != NULL);
6109 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6110 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6111 s->contents = bfd_zalloc (output_bfd, s->size);
6112 if (s->contents == NULL)
6113 return FALSE;
6115 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6116 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6117 s->contents + hash_entry_size);
6120 if (info->emit_gnu_hash)
6122 size_t i, cnt;
6123 unsigned char *contents;
6124 struct collect_gnu_hash_codes cinfo;
6125 bfd_size_type amt;
6126 size_t bucketcount;
6128 memset (&cinfo, 0, sizeof (cinfo));
6130 /* Compute the hash values for all exported symbols. At the same
6131 time store the values in an array so that we could use them for
6132 optimizations. */
6133 amt = dynsymcount * 2 * sizeof (unsigned long int);
6134 cinfo.hashcodes = bfd_malloc (amt);
6135 if (cinfo.hashcodes == NULL)
6136 return FALSE;
6138 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6139 cinfo.min_dynindx = -1;
6140 cinfo.output_bfd = output_bfd;
6141 cinfo.bed = bed;
6143 /* Put all hash values in HASHCODES. */
6144 elf_link_hash_traverse (elf_hash_table (info),
6145 elf_collect_gnu_hash_codes, &cinfo);
6147 bucketcount
6148 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6150 if (bucketcount == 0)
6152 free (cinfo.hashcodes);
6153 return FALSE;
6156 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6157 BFD_ASSERT (s != NULL);
6159 if (cinfo.nsyms == 0)
6161 /* Empty .gnu.hash section is special. */
6162 BFD_ASSERT (cinfo.min_dynindx == -1);
6163 free (cinfo.hashcodes);
6164 s->size = 5 * 4 + bed->s->arch_size / 8;
6165 contents = bfd_zalloc (output_bfd, s->size);
6166 if (contents == NULL)
6167 return FALSE;
6168 s->contents = contents;
6169 /* 1 empty bucket. */
6170 bfd_put_32 (output_bfd, 1, contents);
6171 /* SYMIDX above the special symbol 0. */
6172 bfd_put_32 (output_bfd, 1, contents + 4);
6173 /* Just one word for bitmask. */
6174 bfd_put_32 (output_bfd, 1, contents + 8);
6175 /* Only hash fn bloom filter. */
6176 bfd_put_32 (output_bfd, 0, contents + 12);
6177 /* No hashes are valid - empty bitmask. */
6178 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6179 /* No hashes in the only bucket. */
6180 bfd_put_32 (output_bfd, 0,
6181 contents + 16 + bed->s->arch_size / 8);
6183 else
6185 unsigned long int maskwords, maskbitslog2;
6186 BFD_ASSERT (cinfo.min_dynindx != -1);
6188 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6189 if (maskbitslog2 < 3)
6190 maskbitslog2 = 5;
6191 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6192 maskbitslog2 = maskbitslog2 + 3;
6193 else
6194 maskbitslog2 = maskbitslog2 + 2;
6195 if (bed->s->arch_size == 64)
6197 if (maskbitslog2 == 5)
6198 maskbitslog2 = 6;
6199 cinfo.shift1 = 6;
6201 else
6202 cinfo.shift1 = 5;
6203 cinfo.mask = (1 << cinfo.shift1) - 1;
6204 cinfo.shift2 = maskbitslog2;
6205 cinfo.maskbits = 1 << maskbitslog2;
6206 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6207 amt = bucketcount * sizeof (unsigned long int) * 2;
6208 amt += maskwords * sizeof (bfd_vma);
6209 cinfo.bitmask = bfd_malloc (amt);
6210 if (cinfo.bitmask == NULL)
6212 free (cinfo.hashcodes);
6213 return FALSE;
6216 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6217 cinfo.indx = cinfo.counts + bucketcount;
6218 cinfo.symindx = dynsymcount - cinfo.nsyms;
6219 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6221 /* Determine how often each hash bucket is used. */
6222 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6223 for (i = 0; i < cinfo.nsyms; ++i)
6224 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6226 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6227 if (cinfo.counts[i] != 0)
6229 cinfo.indx[i] = cnt;
6230 cnt += cinfo.counts[i];
6232 BFD_ASSERT (cnt == dynsymcount);
6233 cinfo.bucketcount = bucketcount;
6234 cinfo.local_indx = cinfo.min_dynindx;
6236 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6237 s->size += cinfo.maskbits / 8;
6238 contents = bfd_zalloc (output_bfd, s->size);
6239 if (contents == NULL)
6241 free (cinfo.bitmask);
6242 free (cinfo.hashcodes);
6243 return FALSE;
6246 s->contents = contents;
6247 bfd_put_32 (output_bfd, bucketcount, contents);
6248 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6249 bfd_put_32 (output_bfd, maskwords, contents + 8);
6250 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6251 contents += 16 + cinfo.maskbits / 8;
6253 for (i = 0; i < bucketcount; ++i)
6255 if (cinfo.counts[i] == 0)
6256 bfd_put_32 (output_bfd, 0, contents);
6257 else
6258 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6259 contents += 4;
6262 cinfo.contents = contents;
6264 /* Renumber dynamic symbols, populate .gnu.hash section. */
6265 elf_link_hash_traverse (elf_hash_table (info),
6266 elf_renumber_gnu_hash_syms, &cinfo);
6268 contents = s->contents + 16;
6269 for (i = 0; i < maskwords; ++i)
6271 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6272 contents);
6273 contents += bed->s->arch_size / 8;
6276 free (cinfo.bitmask);
6277 free (cinfo.hashcodes);
6281 s = bfd_get_section_by_name (dynobj, ".dynstr");
6282 BFD_ASSERT (s != NULL);
6284 elf_finalize_dynstr (output_bfd, info);
6286 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6288 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6289 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6290 return FALSE;
6293 return TRUE;
6296 /* Final phase of ELF linker. */
6298 /* A structure we use to avoid passing large numbers of arguments. */
6300 struct elf_final_link_info
6302 /* General link information. */
6303 struct bfd_link_info *info;
6304 /* Output BFD. */
6305 bfd *output_bfd;
6306 /* Symbol string table. */
6307 struct bfd_strtab_hash *symstrtab;
6308 /* .dynsym section. */
6309 asection *dynsym_sec;
6310 /* .hash section. */
6311 asection *hash_sec;
6312 /* symbol version section (.gnu.version). */
6313 asection *symver_sec;
6314 /* Buffer large enough to hold contents of any section. */
6315 bfd_byte *contents;
6316 /* Buffer large enough to hold external relocs of any section. */
6317 void *external_relocs;
6318 /* Buffer large enough to hold internal relocs of any section. */
6319 Elf_Internal_Rela *internal_relocs;
6320 /* Buffer large enough to hold external local symbols of any input
6321 BFD. */
6322 bfd_byte *external_syms;
6323 /* And a buffer for symbol section indices. */
6324 Elf_External_Sym_Shndx *locsym_shndx;
6325 /* Buffer large enough to hold internal local symbols of any input
6326 BFD. */
6327 Elf_Internal_Sym *internal_syms;
6328 /* Array large enough to hold a symbol index for each local symbol
6329 of any input BFD. */
6330 long *indices;
6331 /* Array large enough to hold a section pointer for each local
6332 symbol of any input BFD. */
6333 asection **sections;
6334 /* Buffer to hold swapped out symbols. */
6335 bfd_byte *symbuf;
6336 /* And one for symbol section indices. */
6337 Elf_External_Sym_Shndx *symshndxbuf;
6338 /* Number of swapped out symbols in buffer. */
6339 size_t symbuf_count;
6340 /* Number of symbols which fit in symbuf. */
6341 size_t symbuf_size;
6342 /* And same for symshndxbuf. */
6343 size_t shndxbuf_size;
6346 /* This struct is used to pass information to elf_link_output_extsym. */
6348 struct elf_outext_info
6350 bfd_boolean failed;
6351 bfd_boolean localsyms;
6352 struct elf_final_link_info *finfo;
6356 /* Support for evaluating a complex relocation.
6358 Complex relocations are generalized, self-describing relocations. The
6359 implementation of them consists of two parts: complex symbols, and the
6360 relocations themselves.
6362 The relocations are use a reserved elf-wide relocation type code (R_RELC
6363 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6364 information (start bit, end bit, word width, etc) into the addend. This
6365 information is extracted from CGEN-generated operand tables within gas.
6367 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6368 internal) representing prefix-notation expressions, including but not
6369 limited to those sorts of expressions normally encoded as addends in the
6370 addend field. The symbol mangling format is:
6372 <node> := <literal>
6373 | <unary-operator> ':' <node>
6374 | <binary-operator> ':' <node> ':' <node>
6377 <literal> := 's' <digits=N> ':' <N character symbol name>
6378 | 'S' <digits=N> ':' <N character section name>
6379 | '#' <hexdigits>
6382 <binary-operator> := as in C
6383 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6385 static void
6386 set_symbol_value (bfd * bfd_with_globals,
6387 struct elf_final_link_info * finfo,
6388 int symidx,
6389 bfd_vma val)
6391 bfd_boolean is_local;
6392 Elf_Internal_Sym * sym;
6393 struct elf_link_hash_entry ** sym_hashes;
6394 struct elf_link_hash_entry * h;
6396 sym_hashes = elf_sym_hashes (bfd_with_globals);
6397 sym = finfo->internal_syms + symidx;
6398 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
6400 if (is_local)
6402 /* It is a local symbol: move it to the
6403 "absolute" section and give it a value. */
6404 sym->st_shndx = SHN_ABS;
6405 sym->st_value = val;
6407 else
6409 /* It is a global symbol: set its link type
6410 to "defined" and give it a value. */
6411 h = sym_hashes [symidx];
6412 while (h->root.type == bfd_link_hash_indirect
6413 || h->root.type == bfd_link_hash_warning)
6414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6415 h->root.type = bfd_link_hash_defined;
6416 h->root.u.def.value = val;
6417 h->root.u.def.section = bfd_abs_section_ptr;
6421 static bfd_boolean
6422 resolve_symbol (const char * name,
6423 bfd * input_bfd,
6424 struct elf_final_link_info * finfo,
6425 bfd_vma * result,
6426 size_t locsymcount)
6428 Elf_Internal_Sym * sym;
6429 struct bfd_link_hash_entry * global_entry;
6430 const char * candidate = NULL;
6431 Elf_Internal_Shdr * symtab_hdr;
6432 asection * sec = NULL;
6433 size_t i;
6435 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6437 for (i = 0; i < locsymcount; ++ i)
6439 sym = finfo->internal_syms + i;
6440 sec = finfo->sections [i];
6442 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6443 continue;
6445 candidate = bfd_elf_string_from_elf_section (input_bfd,
6446 symtab_hdr->sh_link,
6447 sym->st_name);
6448 #ifdef DEBUG
6449 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6450 name, candidate, (unsigned int)sym->st_value);
6451 #endif
6452 if (candidate && strcmp (candidate, name) == 0)
6454 * result = sym->st_value;
6456 if (sym->st_shndx > SHN_UNDEF &&
6457 sym->st_shndx < SHN_LORESERVE)
6459 #ifdef DEBUG
6460 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6461 sec->output_section->name,
6462 (unsigned int)sec->output_section->vma,
6463 (unsigned int)sec->output_offset);
6464 #endif
6465 * result += sec->output_offset + sec->output_section->vma;
6467 #ifdef DEBUG
6468 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
6469 #endif
6470 return TRUE;
6474 /* Hmm, haven't found it yet. perhaps it is a global. */
6475 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
6476 if (!global_entry)
6477 return FALSE;
6479 if (global_entry->type == bfd_link_hash_defined
6480 || global_entry->type == bfd_link_hash_defweak)
6482 * result = global_entry->u.def.value
6483 + global_entry->u.def.section->output_section->vma
6484 + global_entry->u.def.section->output_offset;
6485 #ifdef DEBUG
6486 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6487 global_entry->root.string, (unsigned int)*result);
6488 #endif
6489 return TRUE;
6492 if (global_entry->type == bfd_link_hash_common)
6494 *result = global_entry->u.def.value +
6495 bfd_com_section_ptr->output_section->vma +
6496 bfd_com_section_ptr->output_offset;
6497 #ifdef DEBUG
6498 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6499 global_entry->root.string, (unsigned int)*result);
6500 #endif
6501 return TRUE;
6504 return FALSE;
6507 static bfd_boolean
6508 resolve_section (const char * name,
6509 asection * sections,
6510 bfd_vma * result)
6512 asection * curr;
6513 unsigned int len;
6515 for (curr = sections; curr; curr = curr->next)
6516 if (strcmp (curr->name, name) == 0)
6518 *result = curr->vma;
6519 return TRUE;
6522 /* Hmm. still haven't found it. try pseudo-section names. */
6523 for (curr = sections; curr; curr = curr->next)
6525 len = strlen (curr->name);
6526 if (len > strlen (name))
6527 continue;
6529 if (strncmp (curr->name, name, len) == 0)
6531 if (strncmp (".end", name + len, 4) == 0)
6533 *result = curr->vma + curr->size;
6534 return TRUE;
6537 /* Insert more pseudo-section names here, if you like. */
6541 return FALSE;
6544 static void
6545 undefined_reference (const char * reftype,
6546 const char * name)
6548 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
6551 static bfd_boolean
6552 eval_symbol (bfd_vma * result,
6553 char * sym,
6554 char ** advanced,
6555 bfd * input_bfd,
6556 struct elf_final_link_info * finfo,
6557 bfd_vma addr,
6558 bfd_vma section_offset,
6559 size_t locsymcount,
6560 int signed_p)
6562 int len;
6563 int symlen;
6564 bfd_vma a;
6565 bfd_vma b;
6566 const int bufsz = 4096;
6567 char symbuf [bufsz];
6568 const char * symend;
6569 bfd_boolean symbol_is_section = FALSE;
6571 len = strlen (sym);
6572 symend = sym + len;
6574 if (len < 1 || len > bufsz)
6576 bfd_set_error (bfd_error_invalid_operation);
6577 return FALSE;
6580 switch (* sym)
6582 case '.':
6583 * result = addr + section_offset;
6584 * advanced = sym + 1;
6585 return TRUE;
6587 case '#':
6588 ++ sym;
6589 * result = strtoul (sym, advanced, 16);
6590 return TRUE;
6592 case 'S':
6593 symbol_is_section = TRUE;
6594 case 's':
6595 ++ sym;
6596 symlen = strtol (sym, &sym, 10);
6597 ++ sym; /* Skip the trailing ':'. */
6599 if ((symend < sym) || ((symlen + 1) > bufsz))
6601 bfd_set_error (bfd_error_invalid_operation);
6602 return FALSE;
6605 memcpy (symbuf, sym, symlen);
6606 symbuf [symlen] = '\0';
6607 * advanced = sym + symlen;
6609 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6610 the symbol as a section, or vice-versa. so we're pretty liberal in our
6611 interpretation here; section means "try section first", not "must be a
6612 section", and likewise with symbol. */
6614 if (symbol_is_section)
6616 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
6617 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
6619 undefined_reference ("section", symbuf);
6620 return FALSE;
6623 else
6625 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
6626 && (resolve_section (symbuf, finfo->output_bfd->sections,
6627 result) != TRUE))
6629 undefined_reference ("symbol", symbuf);
6630 return FALSE;
6634 return TRUE;
6636 /* All that remains are operators. */
6638 #define UNARY_OP(op) \
6639 if (strncmp (sym, #op, strlen (#op)) == 0) \
6641 sym += strlen (#op); \
6642 if (* sym == ':') \
6643 ++ sym; \
6644 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6645 section_offset, locsymcount, signed_p) \
6646 != TRUE) \
6647 return FALSE; \
6648 if (signed_p) \
6649 * result = op ((signed)a); \
6650 else \
6651 * result = op a; \
6652 * advanced = sym; \
6653 return TRUE; \
6656 #define BINARY_OP(op) \
6657 if (strncmp (sym, #op, strlen (#op)) == 0) \
6659 sym += strlen (#op); \
6660 if (* sym == ':') \
6661 ++ sym; \
6662 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6663 section_offset, locsymcount, signed_p) \
6664 != TRUE) \
6665 return FALSE; \
6666 ++ sym; \
6667 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6668 section_offset, locsymcount, signed_p) \
6669 != TRUE) \
6670 return FALSE; \
6671 if (signed_p) \
6672 * result = ((signed) a) op ((signed) b); \
6673 else \
6674 * result = a op b; \
6675 * advanced = sym; \
6676 return TRUE; \
6679 default:
6680 UNARY_OP (0-);
6681 BINARY_OP (<<);
6682 BINARY_OP (>>);
6683 BINARY_OP (==);
6684 BINARY_OP (!=);
6685 BINARY_OP (<=);
6686 BINARY_OP (>=);
6687 BINARY_OP (&&);
6688 BINARY_OP (||);
6689 UNARY_OP (~);
6690 UNARY_OP (!);
6691 BINARY_OP (*);
6692 BINARY_OP (/);
6693 BINARY_OP (%);
6694 BINARY_OP (^);
6695 BINARY_OP (|);
6696 BINARY_OP (&);
6697 BINARY_OP (+);
6698 BINARY_OP (-);
6699 BINARY_OP (<);
6700 BINARY_OP (>);
6701 #undef UNARY_OP
6702 #undef BINARY_OP
6703 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
6704 bfd_set_error (bfd_error_invalid_operation);
6705 return FALSE;
6709 /* Entry point to evaluator, called from elf_link_input_bfd. */
6711 static bfd_boolean
6712 evaluate_complex_relocation_symbols (bfd * input_bfd,
6713 struct elf_final_link_info * finfo,
6714 size_t locsymcount)
6716 const struct elf_backend_data * bed;
6717 Elf_Internal_Shdr * symtab_hdr;
6718 struct elf_link_hash_entry ** sym_hashes;
6719 asection * reloc_sec;
6720 bfd_boolean result = TRUE;
6722 /* For each section, we're going to check and see if it has any
6723 complex relocations, and we're going to evaluate any of them
6724 we can. */
6726 if (finfo->info->relocatable)
6727 return TRUE;
6729 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6730 sym_hashes = elf_sym_hashes (input_bfd);
6731 bed = get_elf_backend_data (input_bfd);
6733 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
6735 Elf_Internal_Rela * internal_relocs;
6736 unsigned long i;
6738 /* This section was omitted from the link. */
6739 if (! reloc_sec->linker_mark)
6740 continue;
6742 /* Only process sections containing relocs. */
6743 if ((reloc_sec->flags & SEC_RELOC) == 0)
6744 continue;
6746 if (reloc_sec->reloc_count == 0)
6747 continue;
6749 /* Read in the relocs for this section. */
6750 internal_relocs
6751 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
6752 (Elf_Internal_Rela *) NULL,
6753 FALSE);
6754 if (internal_relocs == NULL)
6755 continue;
6757 for (i = reloc_sec->reloc_count; i--;)
6759 Elf_Internal_Rela * rel;
6760 char * sym_name;
6761 unsigned long index;
6762 Elf_Internal_Sym * sym;
6763 bfd_vma result;
6764 bfd_vma section_offset;
6765 bfd_vma addr;
6766 int signed_p = 0;
6768 rel = internal_relocs + i;
6769 section_offset = reloc_sec->output_section->vma
6770 + reloc_sec->output_offset;
6771 addr = rel->r_offset;
6773 index = ELF32_R_SYM (rel->r_info);
6774 if (bed->s->arch_size == 64)
6775 index >>= 24;
6777 if (index == STN_UNDEF)
6778 continue;
6780 if (index < locsymcount)
6782 /* The symbol is local. */
6783 sym = finfo->internal_syms + index;
6785 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6786 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
6787 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
6788 continue;
6790 sym_name = bfd_elf_string_from_elf_section
6791 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6793 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
6795 else
6797 /* The symbol is global. */
6798 struct elf_link_hash_entry * h;
6800 if (elf_bad_symtab (input_bfd))
6801 continue;
6803 h = sym_hashes [index - locsymcount];
6804 while ( h->root.type == bfd_link_hash_indirect
6805 || h->root.type == bfd_link_hash_warning)
6806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6808 if (h->type != STT_RELC && h->type != STT_SRELC)
6809 continue;
6811 signed_p = (h->type == STT_SRELC);
6812 sym_name = (char *) h->root.root.string;
6814 #ifdef DEBUG
6815 printf ("Encountered a complex symbol!");
6816 printf (" (input_bfd %s, section %s, reloc %ld\n",
6817 input_bfd->filename, reloc_sec->name, i);
6818 printf (" symbol: idx %8.8lx, name %s\n",
6819 index, sym_name);
6820 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6821 rel->r_info, addr);
6822 printf (" Evaluating '%s' ...\n ", sym_name);
6823 #endif
6824 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
6825 finfo, addr, section_offset, locsymcount,
6826 signed_p))
6827 /* Symbol evaluated OK. Update to absolute value. */
6828 set_symbol_value (input_bfd, finfo, index, result);
6830 else
6831 result = FALSE;
6834 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
6835 free (internal_relocs);
6838 /* If nothing went wrong, then we adjusted
6839 everything we wanted to adjust. */
6840 return result;
6843 static void
6844 put_value (bfd_vma size,
6845 unsigned long chunksz,
6846 bfd * input_bfd,
6847 bfd_vma x,
6848 bfd_byte * location)
6850 location += (size - chunksz);
6852 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
6854 switch (chunksz)
6856 default:
6857 case 0:
6858 abort ();
6859 case 1:
6860 bfd_put_8 (input_bfd, x, location);
6861 break;
6862 case 2:
6863 bfd_put_16 (input_bfd, x, location);
6864 break;
6865 case 4:
6866 bfd_put_32 (input_bfd, x, location);
6867 break;
6868 case 8:
6869 #ifdef BFD64
6870 bfd_put_64 (input_bfd, x, location);
6871 #else
6872 abort ();
6873 #endif
6874 break;
6879 static bfd_vma
6880 get_value (bfd_vma size,
6881 unsigned long chunksz,
6882 bfd * input_bfd,
6883 bfd_byte * location)
6885 bfd_vma x = 0;
6887 for (; size; size -= chunksz, location += chunksz)
6889 switch (chunksz)
6891 default:
6892 case 0:
6893 abort ();
6894 case 1:
6895 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
6896 break;
6897 case 2:
6898 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
6899 break;
6900 case 4:
6901 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
6902 break;
6903 case 8:
6904 #ifdef BFD64
6905 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
6906 #else
6907 abort ();
6908 #endif
6909 break;
6912 return x;
6915 static void
6916 decode_complex_addend
6917 (unsigned long * start, /* in bits */
6918 unsigned long * oplen, /* in bits */
6919 unsigned long * len, /* in bits */
6920 unsigned long * wordsz, /* in bytes */
6921 unsigned long * chunksz, /* in bytes */
6922 unsigned long * lsb0_p,
6923 unsigned long * signed_p,
6924 unsigned long * trunc_p,
6925 unsigned long encoded)
6927 * start = encoded & 0x3F;
6928 * len = (encoded >> 6) & 0x3F;
6929 * oplen = (encoded >> 12) & 0x3F;
6930 * wordsz = (encoded >> 18) & 0xF;
6931 * chunksz = (encoded >> 22) & 0xF;
6932 * lsb0_p = (encoded >> 27) & 1;
6933 * signed_p = (encoded >> 28) & 1;
6934 * trunc_p = (encoded >> 29) & 1;
6937 void
6938 bfd_elf_perform_complex_relocation
6939 (bfd * output_bfd ATTRIBUTE_UNUSED,
6940 struct bfd_link_info * info,
6941 bfd * input_bfd,
6942 asection * input_section,
6943 bfd_byte * contents,
6944 Elf_Internal_Rela * rel,
6945 Elf_Internal_Sym * local_syms,
6946 asection ** local_sections)
6948 const struct elf_backend_data * bed;
6949 Elf_Internal_Shdr * symtab_hdr;
6950 asection * sec;
6951 bfd_vma relocation = 0, shift, x;
6952 unsigned long r_symndx;
6953 bfd_vma mask;
6954 unsigned long start, oplen, len, wordsz,
6955 chunksz, lsb0_p, signed_p, trunc_p;
6957 /* Perform this reloc, since it is complex.
6958 (this is not to say that it necessarily refers to a complex
6959 symbol; merely that it is a self-describing CGEN based reloc.
6960 i.e. the addend has the complete reloc information (bit start, end,
6961 word size, etc) encoded within it.). */
6962 r_symndx = ELF32_R_SYM (rel->r_info);
6963 bed = get_elf_backend_data (input_bfd);
6964 if (bed->s->arch_size == 64)
6965 r_symndx >>= 24;
6967 #ifdef DEBUG
6968 printf ("Performing complex relocation %ld...\n", r_symndx);
6969 #endif
6971 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6972 if (r_symndx < symtab_hdr->sh_info)
6974 /* The symbol is local. */
6975 Elf_Internal_Sym * sym;
6977 sym = local_syms + r_symndx;
6978 sec = local_sections [r_symndx];
6979 relocation = sym->st_value;
6980 if (sym->st_shndx > SHN_UNDEF &&
6981 sym->st_shndx < SHN_LORESERVE)
6982 relocation += (sec->output_offset +
6983 sec->output_section->vma);
6985 else
6987 /* The symbol is global. */
6988 struct elf_link_hash_entry **sym_hashes;
6989 struct elf_link_hash_entry * h;
6991 sym_hashes = elf_sym_hashes (input_bfd);
6992 h = sym_hashes [r_symndx];
6994 while (h->root.type == bfd_link_hash_indirect
6995 || h->root.type == bfd_link_hash_warning)
6996 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6998 if (h->root.type == bfd_link_hash_defined
6999 || h->root.type == bfd_link_hash_defweak)
7001 sec = h->root.u.def.section;
7002 relocation = h->root.u.def.value;
7004 if (! bfd_is_abs_section (sec))
7005 relocation += (sec->output_section->vma
7006 + sec->output_offset);
7008 if (h->root.type == bfd_link_hash_undefined
7009 && !((*info->callbacks->undefined_symbol)
7010 (info, h->root.root.string, input_bfd,
7011 input_section, rel->r_offset,
7012 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7013 || ELF_ST_VISIBILITY (h->other))))
7014 return;
7017 decode_complex_addend (& start, & oplen, & len, & wordsz,
7018 & chunksz, & lsb0_p, & signed_p,
7019 & trunc_p, rel->r_addend);
7021 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7023 if (lsb0_p)
7024 shift = (start + 1) - len;
7025 else
7026 shift = (8 * wordsz) - (start + len);
7028 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7030 #ifdef DEBUG
7031 printf ("Doing complex reloc: "
7032 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7033 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7034 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7035 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7036 oplen, x, mask, relocation);
7037 #endif
7039 if (! trunc_p)
7041 /* Now do an overflow check. */
7042 if (bfd_check_overflow ((signed_p ?
7043 complain_overflow_signed :
7044 complain_overflow_unsigned),
7045 len, 0, (8 * wordsz),
7046 relocation) == bfd_reloc_overflow)
7047 (*_bfd_error_handler)
7048 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7049 "within 0x%lx",
7050 input_bfd->filename, input_section->name, rel->r_offset,
7051 relocation, (signed_p ? "(signed) " : ""), mask);
7054 /* Do the deed. */
7055 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7057 #ifdef DEBUG
7058 printf (" relocation: %8.8lx\n"
7059 " shifted mask: %8.8lx\n"
7060 " shifted/masked reloc: %8.8lx\n"
7061 " result: %8.8lx\n",
7062 relocation, (mask << shift),
7063 ((relocation & mask) << shift), x);
7064 #endif
7065 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7068 /* When performing a relocatable link, the input relocations are
7069 preserved. But, if they reference global symbols, the indices
7070 referenced must be updated. Update all the relocations in
7071 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7073 static void
7074 elf_link_adjust_relocs (bfd *abfd,
7075 Elf_Internal_Shdr *rel_hdr,
7076 unsigned int count,
7077 struct elf_link_hash_entry **rel_hash)
7079 unsigned int i;
7080 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7081 bfd_byte *erela;
7082 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7083 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7084 bfd_vma r_type_mask;
7085 int r_sym_shift;
7087 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7089 swap_in = bed->s->swap_reloc_in;
7090 swap_out = bed->s->swap_reloc_out;
7092 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7094 swap_in = bed->s->swap_reloca_in;
7095 swap_out = bed->s->swap_reloca_out;
7097 else
7098 abort ();
7100 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7101 abort ();
7103 if (bed->s->arch_size == 32)
7105 r_type_mask = 0xff;
7106 r_sym_shift = 8;
7108 else
7110 r_type_mask = 0xffffffff;
7111 r_sym_shift = 32;
7114 erela = rel_hdr->contents;
7115 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7117 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7118 unsigned int j;
7120 if (*rel_hash == NULL)
7121 continue;
7123 BFD_ASSERT ((*rel_hash)->indx >= 0);
7125 (*swap_in) (abfd, erela, irela);
7126 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7127 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7128 | (irela[j].r_info & r_type_mask));
7129 (*swap_out) (abfd, irela, erela);
7133 struct elf_link_sort_rela
7135 union {
7136 bfd_vma offset;
7137 bfd_vma sym_mask;
7138 } u;
7139 enum elf_reloc_type_class type;
7140 /* We use this as an array of size int_rels_per_ext_rel. */
7141 Elf_Internal_Rela rela[1];
7144 static int
7145 elf_link_sort_cmp1 (const void *A, const void *B)
7147 const struct elf_link_sort_rela *a = A;
7148 const struct elf_link_sort_rela *b = B;
7149 int relativea, relativeb;
7151 relativea = a->type == reloc_class_relative;
7152 relativeb = b->type == reloc_class_relative;
7154 if (relativea < relativeb)
7155 return 1;
7156 if (relativea > relativeb)
7157 return -1;
7158 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7159 return -1;
7160 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7161 return 1;
7162 if (a->rela->r_offset < b->rela->r_offset)
7163 return -1;
7164 if (a->rela->r_offset > b->rela->r_offset)
7165 return 1;
7166 return 0;
7169 static int
7170 elf_link_sort_cmp2 (const void *A, const void *B)
7172 const struct elf_link_sort_rela *a = A;
7173 const struct elf_link_sort_rela *b = B;
7174 int copya, copyb;
7176 if (a->u.offset < b->u.offset)
7177 return -1;
7178 if (a->u.offset > b->u.offset)
7179 return 1;
7180 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7181 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7182 if (copya < copyb)
7183 return -1;
7184 if (copya > copyb)
7185 return 1;
7186 if (a->rela->r_offset < b->rela->r_offset)
7187 return -1;
7188 if (a->rela->r_offset > b->rela->r_offset)
7189 return 1;
7190 return 0;
7193 static size_t
7194 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7196 asection *reldyn;
7197 bfd_size_type count, size;
7198 size_t i, ret, sort_elt, ext_size;
7199 bfd_byte *sort, *s_non_relative, *p;
7200 struct elf_link_sort_rela *sq;
7201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7202 int i2e = bed->s->int_rels_per_ext_rel;
7203 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7204 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7205 struct bfd_link_order *lo;
7206 bfd_vma r_sym_mask;
7208 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7209 if (reldyn == NULL || reldyn->size == 0)
7211 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7212 if (reldyn == NULL || reldyn->size == 0)
7213 return 0;
7214 ext_size = bed->s->sizeof_rel;
7215 swap_in = bed->s->swap_reloc_in;
7216 swap_out = bed->s->swap_reloc_out;
7218 else
7220 ext_size = bed->s->sizeof_rela;
7221 swap_in = bed->s->swap_reloca_in;
7222 swap_out = bed->s->swap_reloca_out;
7224 count = reldyn->size / ext_size;
7226 size = 0;
7227 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7228 if (lo->type == bfd_indirect_link_order)
7230 asection *o = lo->u.indirect.section;
7231 size += o->size;
7234 if (size != reldyn->size)
7235 return 0;
7237 sort_elt = (sizeof (struct elf_link_sort_rela)
7238 + (i2e - 1) * sizeof (Elf_Internal_Rela));
7239 sort = bfd_zmalloc (sort_elt * count);
7240 if (sort == NULL)
7242 (*info->callbacks->warning)
7243 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
7244 return 0;
7247 if (bed->s->arch_size == 32)
7248 r_sym_mask = ~(bfd_vma) 0xff;
7249 else
7250 r_sym_mask = ~(bfd_vma) 0xffffffff;
7252 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7253 if (lo->type == bfd_indirect_link_order)
7255 bfd_byte *erel, *erelend;
7256 asection *o = lo->u.indirect.section;
7258 if (o->contents == NULL && o->size != 0)
7260 /* This is a reloc section that is being handled as a normal
7261 section. See bfd_section_from_shdr. We can't combine
7262 relocs in this case. */
7263 free (sort);
7264 return 0;
7266 erel = o->contents;
7267 erelend = o->contents + o->size;
7268 p = sort + o->output_offset / ext_size * sort_elt;
7269 while (erel < erelend)
7271 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7272 (*swap_in) (abfd, erel, s->rela);
7273 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
7274 s->u.sym_mask = r_sym_mask;
7275 p += sort_elt;
7276 erel += ext_size;
7280 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
7282 for (i = 0, p = sort; i < count; i++, p += sort_elt)
7284 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7285 if (s->type != reloc_class_relative)
7286 break;
7288 ret = i;
7289 s_non_relative = p;
7291 sq = (struct elf_link_sort_rela *) s_non_relative;
7292 for (; i < count; i++, p += sort_elt)
7294 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
7295 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
7296 sq = sp;
7297 sp->u.offset = sq->rela->r_offset;
7300 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
7302 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7303 if (lo->type == bfd_indirect_link_order)
7305 bfd_byte *erel, *erelend;
7306 asection *o = lo->u.indirect.section;
7308 erel = o->contents;
7309 erelend = o->contents + o->size;
7310 p = sort + o->output_offset / ext_size * sort_elt;
7311 while (erel < erelend)
7313 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7314 (*swap_out) (abfd, s->rela, erel);
7315 p += sort_elt;
7316 erel += ext_size;
7320 free (sort);
7321 *psec = reldyn;
7322 return ret;
7325 /* Flush the output symbols to the file. */
7327 static bfd_boolean
7328 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
7329 const struct elf_backend_data *bed)
7331 if (finfo->symbuf_count > 0)
7333 Elf_Internal_Shdr *hdr;
7334 file_ptr pos;
7335 bfd_size_type amt;
7337 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
7338 pos = hdr->sh_offset + hdr->sh_size;
7339 amt = finfo->symbuf_count * bed->s->sizeof_sym;
7340 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
7341 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
7342 return FALSE;
7344 hdr->sh_size += amt;
7345 finfo->symbuf_count = 0;
7348 return TRUE;
7351 /* Add a symbol to the output symbol table. */
7353 static bfd_boolean
7354 elf_link_output_sym (struct elf_final_link_info *finfo,
7355 const char *name,
7356 Elf_Internal_Sym *elfsym,
7357 asection *input_sec,
7358 struct elf_link_hash_entry *h)
7360 bfd_byte *dest;
7361 Elf_External_Sym_Shndx *destshndx;
7362 bfd_boolean (*output_symbol_hook)
7363 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
7364 struct elf_link_hash_entry *);
7365 const struct elf_backend_data *bed;
7367 bed = get_elf_backend_data (finfo->output_bfd);
7368 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
7369 if (output_symbol_hook != NULL)
7371 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
7372 return FALSE;
7375 if (name == NULL || *name == '\0')
7376 elfsym->st_name = 0;
7377 else if (input_sec->flags & SEC_EXCLUDE)
7378 elfsym->st_name = 0;
7379 else
7381 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
7382 name, TRUE, FALSE);
7383 if (elfsym->st_name == (unsigned long) -1)
7384 return FALSE;
7387 if (finfo->symbuf_count >= finfo->symbuf_size)
7389 if (! elf_link_flush_output_syms (finfo, bed))
7390 return FALSE;
7393 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
7394 destshndx = finfo->symshndxbuf;
7395 if (destshndx != NULL)
7397 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
7399 bfd_size_type amt;
7401 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
7402 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
7403 if (destshndx == NULL)
7404 return FALSE;
7405 memset ((char *) destshndx + amt, 0, amt);
7406 finfo->shndxbuf_size *= 2;
7408 destshndx += bfd_get_symcount (finfo->output_bfd);
7411 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
7412 finfo->symbuf_count += 1;
7413 bfd_get_symcount (finfo->output_bfd) += 1;
7415 return TRUE;
7418 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7420 static bfd_boolean
7421 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
7423 if (sym->st_shndx > SHN_HIRESERVE)
7425 /* The gABI doesn't support dynamic symbols in output sections
7426 beyond 64k. */
7427 (*_bfd_error_handler)
7428 (_("%B: Too many sections: %d (>= %d)"),
7429 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
7430 bfd_set_error (bfd_error_nonrepresentable_section);
7431 return FALSE;
7433 return TRUE;
7436 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7437 allowing an unsatisfied unversioned symbol in the DSO to match a
7438 versioned symbol that would normally require an explicit version.
7439 We also handle the case that a DSO references a hidden symbol
7440 which may be satisfied by a versioned symbol in another DSO. */
7442 static bfd_boolean
7443 elf_link_check_versioned_symbol (struct bfd_link_info *info,
7444 const struct elf_backend_data *bed,
7445 struct elf_link_hash_entry *h)
7447 bfd *abfd;
7448 struct elf_link_loaded_list *loaded;
7450 if (!is_elf_hash_table (info->hash))
7451 return FALSE;
7453 switch (h->root.type)
7455 default:
7456 abfd = NULL;
7457 break;
7459 case bfd_link_hash_undefined:
7460 case bfd_link_hash_undefweak:
7461 abfd = h->root.u.undef.abfd;
7462 if ((abfd->flags & DYNAMIC) == 0
7463 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
7464 return FALSE;
7465 break;
7467 case bfd_link_hash_defined:
7468 case bfd_link_hash_defweak:
7469 abfd = h->root.u.def.section->owner;
7470 break;
7472 case bfd_link_hash_common:
7473 abfd = h->root.u.c.p->section->owner;
7474 break;
7476 BFD_ASSERT (abfd != NULL);
7478 for (loaded = elf_hash_table (info)->loaded;
7479 loaded != NULL;
7480 loaded = loaded->next)
7482 bfd *input;
7483 Elf_Internal_Shdr *hdr;
7484 bfd_size_type symcount;
7485 bfd_size_type extsymcount;
7486 bfd_size_type extsymoff;
7487 Elf_Internal_Shdr *versymhdr;
7488 Elf_Internal_Sym *isym;
7489 Elf_Internal_Sym *isymend;
7490 Elf_Internal_Sym *isymbuf;
7491 Elf_External_Versym *ever;
7492 Elf_External_Versym *extversym;
7494 input = loaded->abfd;
7496 /* We check each DSO for a possible hidden versioned definition. */
7497 if (input == abfd
7498 || (input->flags & DYNAMIC) == 0
7499 || elf_dynversym (input) == 0)
7500 continue;
7502 hdr = &elf_tdata (input)->dynsymtab_hdr;
7504 symcount = hdr->sh_size / bed->s->sizeof_sym;
7505 if (elf_bad_symtab (input))
7507 extsymcount = symcount;
7508 extsymoff = 0;
7510 else
7512 extsymcount = symcount - hdr->sh_info;
7513 extsymoff = hdr->sh_info;
7516 if (extsymcount == 0)
7517 continue;
7519 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
7520 NULL, NULL, NULL);
7521 if (isymbuf == NULL)
7522 return FALSE;
7524 /* Read in any version definitions. */
7525 versymhdr = &elf_tdata (input)->dynversym_hdr;
7526 extversym = bfd_malloc (versymhdr->sh_size);
7527 if (extversym == NULL)
7528 goto error_ret;
7530 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
7531 || (bfd_bread (extversym, versymhdr->sh_size, input)
7532 != versymhdr->sh_size))
7534 free (extversym);
7535 error_ret:
7536 free (isymbuf);
7537 return FALSE;
7540 ever = extversym + extsymoff;
7541 isymend = isymbuf + extsymcount;
7542 for (isym = isymbuf; isym < isymend; isym++, ever++)
7544 const char *name;
7545 Elf_Internal_Versym iver;
7546 unsigned short version_index;
7548 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
7549 || isym->st_shndx == SHN_UNDEF)
7550 continue;
7552 name = bfd_elf_string_from_elf_section (input,
7553 hdr->sh_link,
7554 isym->st_name);
7555 if (strcmp (name, h->root.root.string) != 0)
7556 continue;
7558 _bfd_elf_swap_versym_in (input, ever, &iver);
7560 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
7562 /* If we have a non-hidden versioned sym, then it should
7563 have provided a definition for the undefined sym. */
7564 abort ();
7567 version_index = iver.vs_vers & VERSYM_VERSION;
7568 if (version_index == 1 || version_index == 2)
7570 /* This is the base or first version. We can use it. */
7571 free (extversym);
7572 free (isymbuf);
7573 return TRUE;
7577 free (extversym);
7578 free (isymbuf);
7581 return FALSE;
7584 /* Add an external symbol to the symbol table. This is called from
7585 the hash table traversal routine. When generating a shared object,
7586 we go through the symbol table twice. The first time we output
7587 anything that might have been forced to local scope in a version
7588 script. The second time we output the symbols that are still
7589 global symbols. */
7591 static bfd_boolean
7592 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
7594 struct elf_outext_info *eoinfo = data;
7595 struct elf_final_link_info *finfo = eoinfo->finfo;
7596 bfd_boolean strip;
7597 Elf_Internal_Sym sym;
7598 asection *input_sec;
7599 const struct elf_backend_data *bed;
7601 if (h->root.type == bfd_link_hash_warning)
7603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7604 if (h->root.type == bfd_link_hash_new)
7605 return TRUE;
7608 /* Decide whether to output this symbol in this pass. */
7609 if (eoinfo->localsyms)
7611 if (!h->forced_local)
7612 return TRUE;
7614 else
7616 if (h->forced_local)
7617 return TRUE;
7620 bed = get_elf_backend_data (finfo->output_bfd);
7622 if (h->root.type == bfd_link_hash_undefined)
7624 /* If we have an undefined symbol reference here then it must have
7625 come from a shared library that is being linked in. (Undefined
7626 references in regular files have already been handled). */
7627 bfd_boolean ignore_undef = FALSE;
7629 /* Some symbols may be special in that the fact that they're
7630 undefined can be safely ignored - let backend determine that. */
7631 if (bed->elf_backend_ignore_undef_symbol)
7632 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
7634 /* If we are reporting errors for this situation then do so now. */
7635 if (ignore_undef == FALSE
7636 && h->ref_dynamic
7637 && ! h->ref_regular
7638 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
7639 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
7641 if (! (finfo->info->callbacks->undefined_symbol
7642 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
7643 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
7645 eoinfo->failed = TRUE;
7646 return FALSE;
7651 /* We should also warn if a forced local symbol is referenced from
7652 shared libraries. */
7653 if (! finfo->info->relocatable
7654 && (! finfo->info->shared)
7655 && h->forced_local
7656 && h->ref_dynamic
7657 && !h->dynamic_def
7658 && !h->dynamic_weak
7659 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
7661 (*_bfd_error_handler)
7662 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7663 finfo->output_bfd,
7664 h->root.u.def.section == bfd_abs_section_ptr
7665 ? finfo->output_bfd : h->root.u.def.section->owner,
7666 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
7667 ? "internal"
7668 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7669 ? "hidden" : "local",
7670 h->root.root.string);
7671 eoinfo->failed = TRUE;
7672 return FALSE;
7675 /* We don't want to output symbols that have never been mentioned by
7676 a regular file, or that we have been told to strip. However, if
7677 h->indx is set to -2, the symbol is used by a reloc and we must
7678 output it. */
7679 if (h->indx == -2)
7680 strip = FALSE;
7681 else if ((h->def_dynamic
7682 || h->ref_dynamic
7683 || h->root.type == bfd_link_hash_new)
7684 && !h->def_regular
7685 && !h->ref_regular)
7686 strip = TRUE;
7687 else if (finfo->info->strip == strip_all)
7688 strip = TRUE;
7689 else if (finfo->info->strip == strip_some
7690 && bfd_hash_lookup (finfo->info->keep_hash,
7691 h->root.root.string, FALSE, FALSE) == NULL)
7692 strip = TRUE;
7693 else if (finfo->info->strip_discarded
7694 && (h->root.type == bfd_link_hash_defined
7695 || h->root.type == bfd_link_hash_defweak)
7696 && elf_discarded_section (h->root.u.def.section))
7697 strip = TRUE;
7698 else
7699 strip = FALSE;
7701 /* If we're stripping it, and it's not a dynamic symbol, there's
7702 nothing else to do unless it is a forced local symbol. */
7703 if (strip
7704 && h->dynindx == -1
7705 && !h->forced_local)
7706 return TRUE;
7708 sym.st_value = 0;
7709 sym.st_size = h->size;
7710 sym.st_other = h->other;
7711 if (h->forced_local)
7712 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
7713 else if (h->root.type == bfd_link_hash_undefweak
7714 || h->root.type == bfd_link_hash_defweak)
7715 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
7716 else
7717 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
7719 switch (h->root.type)
7721 default:
7722 case bfd_link_hash_new:
7723 case bfd_link_hash_warning:
7724 abort ();
7725 return FALSE;
7727 case bfd_link_hash_undefined:
7728 case bfd_link_hash_undefweak:
7729 input_sec = bfd_und_section_ptr;
7730 sym.st_shndx = SHN_UNDEF;
7731 break;
7733 case bfd_link_hash_defined:
7734 case bfd_link_hash_defweak:
7736 input_sec = h->root.u.def.section;
7737 if (input_sec->output_section != NULL)
7739 sym.st_shndx =
7740 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
7741 input_sec->output_section);
7742 if (sym.st_shndx == SHN_BAD)
7744 (*_bfd_error_handler)
7745 (_("%B: could not find output section %A for input section %A"),
7746 finfo->output_bfd, input_sec->output_section, input_sec);
7747 eoinfo->failed = TRUE;
7748 return FALSE;
7751 /* ELF symbols in relocatable files are section relative,
7752 but in nonrelocatable files they are virtual
7753 addresses. */
7754 sym.st_value = h->root.u.def.value + input_sec->output_offset;
7755 if (! finfo->info->relocatable)
7757 sym.st_value += input_sec->output_section->vma;
7758 if (h->type == STT_TLS)
7760 /* STT_TLS symbols are relative to PT_TLS segment
7761 base. */
7762 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7763 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7767 else
7769 BFD_ASSERT (input_sec->owner == NULL
7770 || (input_sec->owner->flags & DYNAMIC) != 0);
7771 sym.st_shndx = SHN_UNDEF;
7772 input_sec = bfd_und_section_ptr;
7775 break;
7777 case bfd_link_hash_common:
7778 input_sec = h->root.u.c.p->section;
7779 sym.st_shndx = bed->common_section_index (input_sec);
7780 sym.st_value = 1 << h->root.u.c.p->alignment_power;
7781 break;
7783 case bfd_link_hash_indirect:
7784 /* These symbols are created by symbol versioning. They point
7785 to the decorated version of the name. For example, if the
7786 symbol foo@@GNU_1.2 is the default, which should be used when
7787 foo is used with no version, then we add an indirect symbol
7788 foo which points to foo@@GNU_1.2. We ignore these symbols,
7789 since the indirected symbol is already in the hash table. */
7790 return TRUE;
7793 /* Give the processor backend a chance to tweak the symbol value,
7794 and also to finish up anything that needs to be done for this
7795 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7796 forced local syms when non-shared is due to a historical quirk. */
7797 if ((h->dynindx != -1
7798 || h->forced_local)
7799 && ((finfo->info->shared
7800 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7801 || h->root.type != bfd_link_hash_undefweak))
7802 || !h->forced_local)
7803 && elf_hash_table (finfo->info)->dynamic_sections_created)
7805 if (! ((*bed->elf_backend_finish_dynamic_symbol)
7806 (finfo->output_bfd, finfo->info, h, &sym)))
7808 eoinfo->failed = TRUE;
7809 return FALSE;
7813 /* If we are marking the symbol as undefined, and there are no
7814 non-weak references to this symbol from a regular object, then
7815 mark the symbol as weak undefined; if there are non-weak
7816 references, mark the symbol as strong. We can't do this earlier,
7817 because it might not be marked as undefined until the
7818 finish_dynamic_symbol routine gets through with it. */
7819 if (sym.st_shndx == SHN_UNDEF
7820 && h->ref_regular
7821 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
7822 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
7824 int bindtype;
7826 if (h->ref_regular_nonweak)
7827 bindtype = STB_GLOBAL;
7828 else
7829 bindtype = STB_WEAK;
7830 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
7833 /* If a non-weak symbol with non-default visibility is not defined
7834 locally, it is a fatal error. */
7835 if (! finfo->info->relocatable
7836 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
7837 && ELF_ST_BIND (sym.st_info) != STB_WEAK
7838 && h->root.type == bfd_link_hash_undefined
7839 && !h->def_regular)
7841 (*_bfd_error_handler)
7842 (_("%B: %s symbol `%s' isn't defined"),
7843 finfo->output_bfd,
7844 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
7845 ? "protected"
7846 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
7847 ? "internal" : "hidden",
7848 h->root.root.string);
7849 eoinfo->failed = TRUE;
7850 return FALSE;
7853 /* If this symbol should be put in the .dynsym section, then put it
7854 there now. We already know the symbol index. We also fill in
7855 the entry in the .hash section. */
7856 if (h->dynindx != -1
7857 && elf_hash_table (finfo->info)->dynamic_sections_created)
7859 bfd_byte *esym;
7861 sym.st_name = h->dynstr_index;
7862 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
7863 if (! check_dynsym (finfo->output_bfd, &sym))
7865 eoinfo->failed = TRUE;
7866 return FALSE;
7868 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
7870 if (finfo->hash_sec != NULL)
7872 size_t hash_entry_size;
7873 bfd_byte *bucketpos;
7874 bfd_vma chain;
7875 size_t bucketcount;
7876 size_t bucket;
7878 bucketcount = elf_hash_table (finfo->info)->bucketcount;
7879 bucket = h->u.elf_hash_value % bucketcount;
7881 hash_entry_size
7882 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
7883 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
7884 + (bucket + 2) * hash_entry_size);
7885 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
7886 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
7887 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
7888 ((bfd_byte *) finfo->hash_sec->contents
7889 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
7892 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
7894 Elf_Internal_Versym iversym;
7895 Elf_External_Versym *eversym;
7897 if (!h->def_regular)
7899 if (h->verinfo.verdef == NULL)
7900 iversym.vs_vers = 0;
7901 else
7902 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
7904 else
7906 if (h->verinfo.vertree == NULL)
7907 iversym.vs_vers = 1;
7908 else
7909 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
7910 if (finfo->info->create_default_symver)
7911 iversym.vs_vers++;
7914 if (h->hidden)
7915 iversym.vs_vers |= VERSYM_HIDDEN;
7917 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
7918 eversym += h->dynindx;
7919 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
7923 /* If we're stripping it, then it was just a dynamic symbol, and
7924 there's nothing else to do. */
7925 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
7926 return TRUE;
7928 h->indx = bfd_get_symcount (finfo->output_bfd);
7930 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
7932 eoinfo->failed = TRUE;
7933 return FALSE;
7936 return TRUE;
7939 /* Return TRUE if special handling is done for relocs in SEC against
7940 symbols defined in discarded sections. */
7942 static bfd_boolean
7943 elf_section_ignore_discarded_relocs (asection *sec)
7945 const struct elf_backend_data *bed;
7947 switch (sec->sec_info_type)
7949 case ELF_INFO_TYPE_STABS:
7950 case ELF_INFO_TYPE_EH_FRAME:
7951 return TRUE;
7952 default:
7953 break;
7956 bed = get_elf_backend_data (sec->owner);
7957 if (bed->elf_backend_ignore_discarded_relocs != NULL
7958 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
7959 return TRUE;
7961 return FALSE;
7964 /* Return a mask saying how ld should treat relocations in SEC against
7965 symbols defined in discarded sections. If this function returns
7966 COMPLAIN set, ld will issue a warning message. If this function
7967 returns PRETEND set, and the discarded section was link-once and the
7968 same size as the kept link-once section, ld will pretend that the
7969 symbol was actually defined in the kept section. Otherwise ld will
7970 zero the reloc (at least that is the intent, but some cooperation by
7971 the target dependent code is needed, particularly for REL targets). */
7973 unsigned int
7974 _bfd_elf_default_action_discarded (asection *sec)
7976 if (sec->flags & SEC_DEBUGGING)
7977 return PRETEND;
7979 if (strcmp (".eh_frame", sec->name) == 0)
7980 return 0;
7982 if (strcmp (".gcc_except_table", sec->name) == 0)
7983 return 0;
7985 return COMPLAIN | PRETEND;
7988 /* Find a match between a section and a member of a section group. */
7990 static asection *
7991 match_group_member (asection *sec, asection *group,
7992 struct bfd_link_info *info)
7994 asection *first = elf_next_in_group (group);
7995 asection *s = first;
7997 while (s != NULL)
7999 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8000 return s;
8002 s = elf_next_in_group (s);
8003 if (s == first)
8004 break;
8007 return NULL;
8010 /* Check if the kept section of a discarded section SEC can be used
8011 to replace it. Return the replacement if it is OK. Otherwise return
8012 NULL. */
8014 asection *
8015 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8017 asection *kept;
8019 kept = sec->kept_section;
8020 if (kept != NULL)
8022 if ((kept->flags & SEC_GROUP) != 0)
8023 kept = match_group_member (sec, kept, info);
8024 if (kept != NULL && sec->size != kept->size)
8025 kept = NULL;
8026 sec->kept_section = kept;
8028 return kept;
8031 /* Link an input file into the linker output file. This function
8032 handles all the sections and relocations of the input file at once.
8033 This is so that we only have to read the local symbols once, and
8034 don't have to keep them in memory. */
8036 static bfd_boolean
8037 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8039 bfd_boolean (*relocate_section)
8040 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8041 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8042 bfd *output_bfd;
8043 Elf_Internal_Shdr *symtab_hdr;
8044 size_t locsymcount;
8045 size_t extsymoff;
8046 Elf_Internal_Sym *isymbuf;
8047 Elf_Internal_Sym *isym;
8048 Elf_Internal_Sym *isymend;
8049 long *pindex;
8050 asection **ppsection;
8051 asection *o;
8052 const struct elf_backend_data *bed;
8053 bfd_boolean emit_relocs;
8054 struct elf_link_hash_entry **sym_hashes;
8056 output_bfd = finfo->output_bfd;
8057 bed = get_elf_backend_data (output_bfd);
8058 relocate_section = bed->elf_backend_relocate_section;
8060 /* If this is a dynamic object, we don't want to do anything here:
8061 we don't want the local symbols, and we don't want the section
8062 contents. */
8063 if ((input_bfd->flags & DYNAMIC) != 0)
8064 return TRUE;
8066 emit_relocs = (finfo->info->relocatable
8067 || finfo->info->emitrelocations);
8069 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8070 if (elf_bad_symtab (input_bfd))
8072 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8073 extsymoff = 0;
8075 else
8077 locsymcount = symtab_hdr->sh_info;
8078 extsymoff = symtab_hdr->sh_info;
8081 /* Read the local symbols. */
8082 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8083 if (isymbuf == NULL && locsymcount != 0)
8085 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8086 finfo->internal_syms,
8087 finfo->external_syms,
8088 finfo->locsym_shndx);
8089 if (isymbuf == NULL)
8090 return FALSE;
8092 /* evaluate_complex_relocation_symbols looks for symbols in
8093 finfo->internal_syms. */
8094 else if (isymbuf != NULL && locsymcount != 0)
8096 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8097 finfo->internal_syms,
8098 finfo->external_syms,
8099 finfo->locsym_shndx);
8102 /* Find local symbol sections and adjust values of symbols in
8103 SEC_MERGE sections. Write out those local symbols we know are
8104 going into the output file. */
8105 isymend = isymbuf + locsymcount;
8106 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8107 isym < isymend;
8108 isym++, pindex++, ppsection++)
8110 asection *isec;
8111 const char *name;
8112 Elf_Internal_Sym osym;
8114 *pindex = -1;
8116 if (elf_bad_symtab (input_bfd))
8118 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8120 *ppsection = NULL;
8121 continue;
8125 if (isym->st_shndx == SHN_UNDEF)
8126 isec = bfd_und_section_ptr;
8127 else if (isym->st_shndx < SHN_LORESERVE
8128 || isym->st_shndx > SHN_HIRESERVE)
8130 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8131 if (isec
8132 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8133 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8134 isym->st_value =
8135 _bfd_merged_section_offset (output_bfd, &isec,
8136 elf_section_data (isec)->sec_info,
8137 isym->st_value);
8139 else if (isym->st_shndx == SHN_ABS)
8140 isec = bfd_abs_section_ptr;
8141 else if (isym->st_shndx == SHN_COMMON)
8142 isec = bfd_com_section_ptr;
8143 else
8145 /* Don't attempt to output symbols with st_shnx in the
8146 reserved range other than SHN_ABS and SHN_COMMON. */
8147 *ppsection = NULL;
8148 continue;
8151 *ppsection = isec;
8153 /* Don't output the first, undefined, symbol. */
8154 if (ppsection == finfo->sections)
8155 continue;
8157 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8159 /* We never output section symbols. Instead, we use the
8160 section symbol of the corresponding section in the output
8161 file. */
8162 continue;
8165 /* If we are stripping all symbols, we don't want to output this
8166 one. */
8167 if (finfo->info->strip == strip_all)
8168 continue;
8170 /* If we are discarding all local symbols, we don't want to
8171 output this one. If we are generating a relocatable output
8172 file, then some of the local symbols may be required by
8173 relocs; we output them below as we discover that they are
8174 needed. */
8175 if (finfo->info->discard == discard_all)
8176 continue;
8178 /* If this symbol is defined in a section which we are
8179 discarding, we don't need to keep it. */
8180 if (isym->st_shndx != SHN_UNDEF
8181 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8182 && (isec == NULL
8183 || bfd_section_removed_from_list (output_bfd,
8184 isec->output_section)))
8185 continue;
8187 /* Get the name of the symbol. */
8188 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8189 isym->st_name);
8190 if (name == NULL)
8191 return FALSE;
8193 /* See if we are discarding symbols with this name. */
8194 if ((finfo->info->strip == strip_some
8195 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8196 == NULL))
8197 || (((finfo->info->discard == discard_sec_merge
8198 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8199 || finfo->info->discard == discard_l)
8200 && bfd_is_local_label_name (input_bfd, name)))
8201 continue;
8203 /* If we get here, we are going to output this symbol. */
8205 osym = *isym;
8207 /* Adjust the section index for the output file. */
8208 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8209 isec->output_section);
8210 if (osym.st_shndx == SHN_BAD)
8211 return FALSE;
8213 *pindex = bfd_get_symcount (output_bfd);
8215 /* ELF symbols in relocatable files are section relative, but
8216 in executable files they are virtual addresses. Note that
8217 this code assumes that all ELF sections have an associated
8218 BFD section with a reasonable value for output_offset; below
8219 we assume that they also have a reasonable value for
8220 output_section. Any special sections must be set up to meet
8221 these requirements. */
8222 osym.st_value += isec->output_offset;
8223 if (! finfo->info->relocatable)
8225 osym.st_value += isec->output_section->vma;
8226 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
8228 /* STT_TLS symbols are relative to PT_TLS segment base. */
8229 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8230 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8234 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
8235 return FALSE;
8238 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
8239 return FALSE;
8241 /* Relocate the contents of each section. */
8242 sym_hashes = elf_sym_hashes (input_bfd);
8243 for (o = input_bfd->sections; o != NULL; o = o->next)
8245 bfd_byte *contents;
8247 if (! o->linker_mark)
8249 /* This section was omitted from the link. */
8250 continue;
8253 if ((o->flags & SEC_HAS_CONTENTS) == 0
8254 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
8255 continue;
8257 if ((o->flags & SEC_LINKER_CREATED) != 0)
8259 /* Section was created by _bfd_elf_link_create_dynamic_sections
8260 or somesuch. */
8261 continue;
8264 /* Get the contents of the section. They have been cached by a
8265 relaxation routine. Note that o is a section in an input
8266 file, so the contents field will not have been set by any of
8267 the routines which work on output files. */
8268 if (elf_section_data (o)->this_hdr.contents != NULL)
8269 contents = elf_section_data (o)->this_hdr.contents;
8270 else
8272 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
8274 contents = finfo->contents;
8275 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
8276 return FALSE;
8279 if ((o->flags & SEC_RELOC) != 0)
8281 Elf_Internal_Rela *internal_relocs;
8282 bfd_vma r_type_mask;
8283 int r_sym_shift;
8285 /* Get the swapped relocs. */
8286 internal_relocs
8287 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
8288 finfo->internal_relocs, FALSE);
8289 if (internal_relocs == NULL
8290 && o->reloc_count > 0)
8291 return FALSE;
8293 if (bed->s->arch_size == 32)
8295 r_type_mask = 0xff;
8296 r_sym_shift = 8;
8298 else
8300 r_type_mask = 0xffffffff;
8301 r_sym_shift = 32;
8304 /* Run through the relocs looking for any against symbols
8305 from discarded sections and section symbols from
8306 removed link-once sections. Complain about relocs
8307 against discarded sections. Zero relocs against removed
8308 link-once sections. */
8309 if (!elf_section_ignore_discarded_relocs (o))
8311 Elf_Internal_Rela *rel, *relend;
8312 unsigned int action = (*bed->action_discarded) (o);
8314 rel = internal_relocs;
8315 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
8316 for ( ; rel < relend; rel++)
8318 unsigned long r_symndx = rel->r_info >> r_sym_shift;
8319 asection **ps, *sec;
8320 struct elf_link_hash_entry *h = NULL;
8321 const char *sym_name;
8323 if (r_symndx == STN_UNDEF)
8324 continue;
8326 if (r_symndx >= locsymcount
8327 || (elf_bad_symtab (input_bfd)
8328 && finfo->sections[r_symndx] == NULL))
8330 h = sym_hashes[r_symndx - extsymoff];
8332 /* Badly formatted input files can contain relocs that
8333 reference non-existant symbols. Check here so that
8334 we do not seg fault. */
8335 if (h == NULL)
8337 char buffer [32];
8339 sprintf_vma (buffer, rel->r_info);
8340 (*_bfd_error_handler)
8341 (_("error: %B contains a reloc (0x%s) for section %A "
8342 "that references a non-existent global symbol"),
8343 input_bfd, o, buffer);
8344 bfd_set_error (bfd_error_bad_value);
8345 return FALSE;
8348 while (h->root.type == bfd_link_hash_indirect
8349 || h->root.type == bfd_link_hash_warning)
8350 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8352 if (h->root.type != bfd_link_hash_defined
8353 && h->root.type != bfd_link_hash_defweak)
8354 continue;
8356 ps = &h->root.u.def.section;
8357 sym_name = h->root.root.string;
8359 else
8361 Elf_Internal_Sym *sym = isymbuf + r_symndx;
8362 ps = &finfo->sections[r_symndx];
8363 sym_name = bfd_elf_sym_name (input_bfd,
8364 symtab_hdr,
8365 sym, *ps);
8368 /* Complain if the definition comes from a
8369 discarded section. */
8370 if ((sec = *ps) != NULL && elf_discarded_section (sec))
8372 BFD_ASSERT (r_symndx != 0);
8373 if (action & COMPLAIN)
8374 (*finfo->info->callbacks->einfo)
8375 (_("%X`%s' referenced in section `%A' of %B: "
8376 "defined in discarded section `%A' of %B\n"),
8377 sym_name, o, input_bfd, sec, sec->owner);
8379 /* Try to do the best we can to support buggy old
8380 versions of gcc. Pretend that the symbol is
8381 really defined in the kept linkonce section.
8382 FIXME: This is quite broken. Modifying the
8383 symbol here means we will be changing all later
8384 uses of the symbol, not just in this section. */
8385 if (action & PRETEND)
8387 asection *kept;
8389 kept = _bfd_elf_check_kept_section (sec,
8390 finfo->info);
8391 if (kept != NULL)
8393 *ps = kept;
8394 continue;
8401 /* Relocate the section by invoking a back end routine.
8403 The back end routine is responsible for adjusting the
8404 section contents as necessary, and (if using Rela relocs
8405 and generating a relocatable output file) adjusting the
8406 reloc addend as necessary.
8408 The back end routine does not have to worry about setting
8409 the reloc address or the reloc symbol index.
8411 The back end routine is given a pointer to the swapped in
8412 internal symbols, and can access the hash table entries
8413 for the external symbols via elf_sym_hashes (input_bfd).
8415 When generating relocatable output, the back end routine
8416 must handle STB_LOCAL/STT_SECTION symbols specially. The
8417 output symbol is going to be a section symbol
8418 corresponding to the output section, which will require
8419 the addend to be adjusted. */
8421 if (! (*relocate_section) (output_bfd, finfo->info,
8422 input_bfd, o, contents,
8423 internal_relocs,
8424 isymbuf,
8425 finfo->sections))
8426 return FALSE;
8428 if (emit_relocs)
8430 Elf_Internal_Rela *irela;
8431 Elf_Internal_Rela *irelaend;
8432 bfd_vma last_offset;
8433 struct elf_link_hash_entry **rel_hash;
8434 struct elf_link_hash_entry **rel_hash_list;
8435 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
8436 unsigned int next_erel;
8437 bfd_boolean rela_normal;
8439 input_rel_hdr = &elf_section_data (o)->rel_hdr;
8440 rela_normal = (bed->rela_normal
8441 && (input_rel_hdr->sh_entsize
8442 == bed->s->sizeof_rela));
8444 /* Adjust the reloc addresses and symbol indices. */
8446 irela = internal_relocs;
8447 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
8448 rel_hash = (elf_section_data (o->output_section)->rel_hashes
8449 + elf_section_data (o->output_section)->rel_count
8450 + elf_section_data (o->output_section)->rel_count2);
8451 rel_hash_list = rel_hash;
8452 last_offset = o->output_offset;
8453 if (!finfo->info->relocatable)
8454 last_offset += o->output_section->vma;
8455 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
8457 unsigned long r_symndx;
8458 asection *sec;
8459 Elf_Internal_Sym sym;
8461 if (next_erel == bed->s->int_rels_per_ext_rel)
8463 rel_hash++;
8464 next_erel = 0;
8467 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8468 finfo->info, o,
8469 irela->r_offset);
8470 if (irela->r_offset >= (bfd_vma) -2)
8472 /* This is a reloc for a deleted entry or somesuch.
8473 Turn it into an R_*_NONE reloc, at the same
8474 offset as the last reloc. elf_eh_frame.c and
8475 bfd_elf_discard_info rely on reloc offsets
8476 being ordered. */
8477 irela->r_offset = last_offset;
8478 irela->r_info = 0;
8479 irela->r_addend = 0;
8480 continue;
8483 irela->r_offset += o->output_offset;
8485 /* Relocs in an executable have to be virtual addresses. */
8486 if (!finfo->info->relocatable)
8487 irela->r_offset += o->output_section->vma;
8489 last_offset = irela->r_offset;
8491 r_symndx = irela->r_info >> r_sym_shift;
8492 if (r_symndx == STN_UNDEF)
8493 continue;
8495 if (r_symndx >= locsymcount
8496 || (elf_bad_symtab (input_bfd)
8497 && finfo->sections[r_symndx] == NULL))
8499 struct elf_link_hash_entry *rh;
8500 unsigned long indx;
8502 /* This is a reloc against a global symbol. We
8503 have not yet output all the local symbols, so
8504 we do not know the symbol index of any global
8505 symbol. We set the rel_hash entry for this
8506 reloc to point to the global hash table entry
8507 for this symbol. The symbol index is then
8508 set at the end of bfd_elf_final_link. */
8509 indx = r_symndx - extsymoff;
8510 rh = elf_sym_hashes (input_bfd)[indx];
8511 while (rh->root.type == bfd_link_hash_indirect
8512 || rh->root.type == bfd_link_hash_warning)
8513 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
8515 /* Setting the index to -2 tells
8516 elf_link_output_extsym that this symbol is
8517 used by a reloc. */
8518 BFD_ASSERT (rh->indx < 0);
8519 rh->indx = -2;
8521 *rel_hash = rh;
8523 continue;
8526 /* This is a reloc against a local symbol. */
8528 *rel_hash = NULL;
8529 sym = isymbuf[r_symndx];
8530 sec = finfo->sections[r_symndx];
8531 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
8533 /* I suppose the backend ought to fill in the
8534 section of any STT_SECTION symbol against a
8535 processor specific section. */
8536 r_symndx = 0;
8537 if (bfd_is_abs_section (sec))
8539 else if (sec == NULL || sec->owner == NULL)
8541 bfd_set_error (bfd_error_bad_value);
8542 return FALSE;
8544 else
8546 asection *osec = sec->output_section;
8548 /* If we have discarded a section, the output
8549 section will be the absolute section. In
8550 case of discarded SEC_MERGE sections, use
8551 the kept section. relocate_section should
8552 have already handled discarded linkonce
8553 sections. */
8554 if (bfd_is_abs_section (osec)
8555 && sec->kept_section != NULL
8556 && sec->kept_section->output_section != NULL)
8558 osec = sec->kept_section->output_section;
8559 irela->r_addend -= osec->vma;
8562 if (!bfd_is_abs_section (osec))
8564 r_symndx = osec->target_index;
8565 if (r_symndx == 0)
8567 struct elf_link_hash_table *htab;
8568 asection *oi;
8570 htab = elf_hash_table (finfo->info);
8571 oi = htab->text_index_section;
8572 if ((osec->flags & SEC_READONLY) == 0
8573 && htab->data_index_section != NULL)
8574 oi = htab->data_index_section;
8576 if (oi != NULL)
8578 irela->r_addend += osec->vma - oi->vma;
8579 r_symndx = oi->target_index;
8583 BFD_ASSERT (r_symndx != 0);
8587 /* Adjust the addend according to where the
8588 section winds up in the output section. */
8589 if (rela_normal)
8590 irela->r_addend += sec->output_offset;
8592 else
8594 if (finfo->indices[r_symndx] == -1)
8596 unsigned long shlink;
8597 const char *name;
8598 asection *osec;
8600 if (finfo->info->strip == strip_all)
8602 /* You can't do ld -r -s. */
8603 bfd_set_error (bfd_error_invalid_operation);
8604 return FALSE;
8607 /* This symbol was skipped earlier, but
8608 since it is needed by a reloc, we
8609 must output it now. */
8610 shlink = symtab_hdr->sh_link;
8611 name = (bfd_elf_string_from_elf_section
8612 (input_bfd, shlink, sym.st_name));
8613 if (name == NULL)
8614 return FALSE;
8616 osec = sec->output_section;
8617 sym.st_shndx =
8618 _bfd_elf_section_from_bfd_section (output_bfd,
8619 osec);
8620 if (sym.st_shndx == SHN_BAD)
8621 return FALSE;
8623 sym.st_value += sec->output_offset;
8624 if (! finfo->info->relocatable)
8626 sym.st_value += osec->vma;
8627 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
8629 /* STT_TLS symbols are relative to PT_TLS
8630 segment base. */
8631 BFD_ASSERT (elf_hash_table (finfo->info)
8632 ->tls_sec != NULL);
8633 sym.st_value -= (elf_hash_table (finfo->info)
8634 ->tls_sec->vma);
8638 finfo->indices[r_symndx]
8639 = bfd_get_symcount (output_bfd);
8641 if (! elf_link_output_sym (finfo, name, &sym, sec,
8642 NULL))
8643 return FALSE;
8646 r_symndx = finfo->indices[r_symndx];
8649 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
8650 | (irela->r_info & r_type_mask));
8653 /* Swap out the relocs. */
8654 if (input_rel_hdr->sh_size != 0
8655 && !bed->elf_backend_emit_relocs (output_bfd, o,
8656 input_rel_hdr,
8657 internal_relocs,
8658 rel_hash_list))
8659 return FALSE;
8661 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
8662 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
8664 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
8665 * bed->s->int_rels_per_ext_rel);
8666 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
8667 if (!bed->elf_backend_emit_relocs (output_bfd, o,
8668 input_rel_hdr2,
8669 internal_relocs,
8670 rel_hash_list))
8671 return FALSE;
8676 /* Write out the modified section contents. */
8677 if (bed->elf_backend_write_section
8678 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
8679 contents))
8681 /* Section written out. */
8683 else switch (o->sec_info_type)
8685 case ELF_INFO_TYPE_STABS:
8686 if (! (_bfd_write_section_stabs
8687 (output_bfd,
8688 &elf_hash_table (finfo->info)->stab_info,
8689 o, &elf_section_data (o)->sec_info, contents)))
8690 return FALSE;
8691 break;
8692 case ELF_INFO_TYPE_MERGE:
8693 if (! _bfd_write_merged_section (output_bfd, o,
8694 elf_section_data (o)->sec_info))
8695 return FALSE;
8696 break;
8697 case ELF_INFO_TYPE_EH_FRAME:
8699 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
8700 o, contents))
8701 return FALSE;
8703 break;
8704 default:
8706 if (! (o->flags & SEC_EXCLUDE)
8707 && ! bfd_set_section_contents (output_bfd, o->output_section,
8708 contents,
8709 (file_ptr) o->output_offset,
8710 o->size))
8711 return FALSE;
8713 break;
8717 return TRUE;
8720 /* Generate a reloc when linking an ELF file. This is a reloc
8721 requested by the linker, and does not come from any input file. This
8722 is used to build constructor and destructor tables when linking
8723 with -Ur. */
8725 static bfd_boolean
8726 elf_reloc_link_order (bfd *output_bfd,
8727 struct bfd_link_info *info,
8728 asection *output_section,
8729 struct bfd_link_order *link_order)
8731 reloc_howto_type *howto;
8732 long indx;
8733 bfd_vma offset;
8734 bfd_vma addend;
8735 struct elf_link_hash_entry **rel_hash_ptr;
8736 Elf_Internal_Shdr *rel_hdr;
8737 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
8738 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
8739 bfd_byte *erel;
8740 unsigned int i;
8742 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
8743 if (howto == NULL)
8745 bfd_set_error (bfd_error_bad_value);
8746 return FALSE;
8749 addend = link_order->u.reloc.p->addend;
8751 /* Figure out the symbol index. */
8752 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
8753 + elf_section_data (output_section)->rel_count
8754 + elf_section_data (output_section)->rel_count2);
8755 if (link_order->type == bfd_section_reloc_link_order)
8757 indx = link_order->u.reloc.p->u.section->target_index;
8758 BFD_ASSERT (indx != 0);
8759 *rel_hash_ptr = NULL;
8761 else
8763 struct elf_link_hash_entry *h;
8765 /* Treat a reloc against a defined symbol as though it were
8766 actually against the section. */
8767 h = ((struct elf_link_hash_entry *)
8768 bfd_wrapped_link_hash_lookup (output_bfd, info,
8769 link_order->u.reloc.p->u.name,
8770 FALSE, FALSE, TRUE));
8771 if (h != NULL
8772 && (h->root.type == bfd_link_hash_defined
8773 || h->root.type == bfd_link_hash_defweak))
8775 asection *section;
8777 section = h->root.u.def.section;
8778 indx = section->output_section->target_index;
8779 *rel_hash_ptr = NULL;
8780 /* It seems that we ought to add the symbol value to the
8781 addend here, but in practice it has already been added
8782 because it was passed to constructor_callback. */
8783 addend += section->output_section->vma + section->output_offset;
8785 else if (h != NULL)
8787 /* Setting the index to -2 tells elf_link_output_extsym that
8788 this symbol is used by a reloc. */
8789 h->indx = -2;
8790 *rel_hash_ptr = h;
8791 indx = 0;
8793 else
8795 if (! ((*info->callbacks->unattached_reloc)
8796 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
8797 return FALSE;
8798 indx = 0;
8802 /* If this is an inplace reloc, we must write the addend into the
8803 object file. */
8804 if (howto->partial_inplace && addend != 0)
8806 bfd_size_type size;
8807 bfd_reloc_status_type rstat;
8808 bfd_byte *buf;
8809 bfd_boolean ok;
8810 const char *sym_name;
8812 size = bfd_get_reloc_size (howto);
8813 buf = bfd_zmalloc (size);
8814 if (buf == NULL)
8815 return FALSE;
8816 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
8817 switch (rstat)
8819 case bfd_reloc_ok:
8820 break;
8822 default:
8823 case bfd_reloc_outofrange:
8824 abort ();
8826 case bfd_reloc_overflow:
8827 if (link_order->type == bfd_section_reloc_link_order)
8828 sym_name = bfd_section_name (output_bfd,
8829 link_order->u.reloc.p->u.section);
8830 else
8831 sym_name = link_order->u.reloc.p->u.name;
8832 if (! ((*info->callbacks->reloc_overflow)
8833 (info, NULL, sym_name, howto->name, addend, NULL,
8834 NULL, (bfd_vma) 0)))
8836 free (buf);
8837 return FALSE;
8839 break;
8841 ok = bfd_set_section_contents (output_bfd, output_section, buf,
8842 link_order->offset, size);
8843 free (buf);
8844 if (! ok)
8845 return FALSE;
8848 /* The address of a reloc is relative to the section in a
8849 relocatable file, and is a virtual address in an executable
8850 file. */
8851 offset = link_order->offset;
8852 if (! info->relocatable)
8853 offset += output_section->vma;
8855 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
8857 irel[i].r_offset = offset;
8858 irel[i].r_info = 0;
8859 irel[i].r_addend = 0;
8861 if (bed->s->arch_size == 32)
8862 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
8863 else
8864 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
8866 rel_hdr = &elf_section_data (output_section)->rel_hdr;
8867 erel = rel_hdr->contents;
8868 if (rel_hdr->sh_type == SHT_REL)
8870 erel += (elf_section_data (output_section)->rel_count
8871 * bed->s->sizeof_rel);
8872 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
8874 else
8876 irel[0].r_addend = addend;
8877 erel += (elf_section_data (output_section)->rel_count
8878 * bed->s->sizeof_rela);
8879 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
8882 ++elf_section_data (output_section)->rel_count;
8884 return TRUE;
8888 /* Get the output vma of the section pointed to by the sh_link field. */
8890 static bfd_vma
8891 elf_get_linked_section_vma (struct bfd_link_order *p)
8893 Elf_Internal_Shdr **elf_shdrp;
8894 asection *s;
8895 int elfsec;
8897 s = p->u.indirect.section;
8898 elf_shdrp = elf_elfsections (s->owner);
8899 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
8900 elfsec = elf_shdrp[elfsec]->sh_link;
8901 /* PR 290:
8902 The Intel C compiler generates SHT_IA_64_UNWIND with
8903 SHF_LINK_ORDER. But it doesn't set the sh_link or
8904 sh_info fields. Hence we could get the situation
8905 where elfsec is 0. */
8906 if (elfsec == 0)
8908 const struct elf_backend_data *bed
8909 = get_elf_backend_data (s->owner);
8910 if (bed->link_order_error_handler)
8911 bed->link_order_error_handler
8912 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
8913 return 0;
8915 else
8917 s = elf_shdrp[elfsec]->bfd_section;
8918 return s->output_section->vma + s->output_offset;
8923 /* Compare two sections based on the locations of the sections they are
8924 linked to. Used by elf_fixup_link_order. */
8926 static int
8927 compare_link_order (const void * a, const void * b)
8929 bfd_vma apos;
8930 bfd_vma bpos;
8932 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
8933 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
8934 if (apos < bpos)
8935 return -1;
8936 return apos > bpos;
8940 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8941 order as their linked sections. Returns false if this could not be done
8942 because an output section includes both ordered and unordered
8943 sections. Ideally we'd do this in the linker proper. */
8945 static bfd_boolean
8946 elf_fixup_link_order (bfd *abfd, asection *o)
8948 int seen_linkorder;
8949 int seen_other;
8950 int n;
8951 struct bfd_link_order *p;
8952 bfd *sub;
8953 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8954 unsigned elfsec;
8955 struct bfd_link_order **sections;
8956 asection *s, *other_sec, *linkorder_sec;
8957 bfd_vma offset;
8959 other_sec = NULL;
8960 linkorder_sec = NULL;
8961 seen_other = 0;
8962 seen_linkorder = 0;
8963 for (p = o->map_head.link_order; p != NULL; p = p->next)
8965 if (p->type == bfd_indirect_link_order)
8967 s = p->u.indirect.section;
8968 sub = s->owner;
8969 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
8970 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
8971 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
8972 && elfsec < elf_numsections (sub)
8973 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
8975 seen_linkorder++;
8976 linkorder_sec = s;
8978 else
8980 seen_other++;
8981 other_sec = s;
8984 else
8985 seen_other++;
8987 if (seen_other && seen_linkorder)
8989 if (other_sec && linkorder_sec)
8990 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8991 o, linkorder_sec,
8992 linkorder_sec->owner, other_sec,
8993 other_sec->owner);
8994 else
8995 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
8997 bfd_set_error (bfd_error_bad_value);
8998 return FALSE;
9002 if (!seen_linkorder)
9003 return TRUE;
9005 sections = (struct bfd_link_order **)
9006 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
9007 seen_linkorder = 0;
9009 for (p = o->map_head.link_order; p != NULL; p = p->next)
9011 sections[seen_linkorder++] = p;
9013 /* Sort the input sections in the order of their linked section. */
9014 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9015 compare_link_order);
9017 /* Change the offsets of the sections. */
9018 offset = 0;
9019 for (n = 0; n < seen_linkorder; n++)
9021 s = sections[n]->u.indirect.section;
9022 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
9023 s->output_offset = offset;
9024 sections[n]->offset = offset;
9025 offset += sections[n]->size;
9028 return TRUE;
9032 /* Do the final step of an ELF link. */
9034 bfd_boolean
9035 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9037 bfd_boolean dynamic;
9038 bfd_boolean emit_relocs;
9039 bfd *dynobj;
9040 struct elf_final_link_info finfo;
9041 register asection *o;
9042 register struct bfd_link_order *p;
9043 register bfd *sub;
9044 bfd_size_type max_contents_size;
9045 bfd_size_type max_external_reloc_size;
9046 bfd_size_type max_internal_reloc_count;
9047 bfd_size_type max_sym_count;
9048 bfd_size_type max_sym_shndx_count;
9049 file_ptr off;
9050 Elf_Internal_Sym elfsym;
9051 unsigned int i;
9052 Elf_Internal_Shdr *symtab_hdr;
9053 Elf_Internal_Shdr *symtab_shndx_hdr;
9054 Elf_Internal_Shdr *symstrtab_hdr;
9055 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9056 struct elf_outext_info eoinfo;
9057 bfd_boolean merged;
9058 size_t relativecount = 0;
9059 asection *reldyn = 0;
9060 bfd_size_type amt;
9062 if (! is_elf_hash_table (info->hash))
9063 return FALSE;
9065 if (info->shared)
9066 abfd->flags |= DYNAMIC;
9068 dynamic = elf_hash_table (info)->dynamic_sections_created;
9069 dynobj = elf_hash_table (info)->dynobj;
9071 emit_relocs = (info->relocatable
9072 || info->emitrelocations);
9074 finfo.info = info;
9075 finfo.output_bfd = abfd;
9076 finfo.symstrtab = _bfd_elf_stringtab_init ();
9077 if (finfo.symstrtab == NULL)
9078 return FALSE;
9080 if (! dynamic)
9082 finfo.dynsym_sec = NULL;
9083 finfo.hash_sec = NULL;
9084 finfo.symver_sec = NULL;
9086 else
9088 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9089 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9090 BFD_ASSERT (finfo.dynsym_sec != NULL);
9091 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9092 /* Note that it is OK if symver_sec is NULL. */
9095 finfo.contents = NULL;
9096 finfo.external_relocs = NULL;
9097 finfo.internal_relocs = NULL;
9098 finfo.external_syms = NULL;
9099 finfo.locsym_shndx = NULL;
9100 finfo.internal_syms = NULL;
9101 finfo.indices = NULL;
9102 finfo.sections = NULL;
9103 finfo.symbuf = NULL;
9104 finfo.symshndxbuf = NULL;
9105 finfo.symbuf_count = 0;
9106 finfo.shndxbuf_size = 0;
9108 /* Count up the number of relocations we will output for each output
9109 section, so that we know the sizes of the reloc sections. We
9110 also figure out some maximum sizes. */
9111 max_contents_size = 0;
9112 max_external_reloc_size = 0;
9113 max_internal_reloc_count = 0;
9114 max_sym_count = 0;
9115 max_sym_shndx_count = 0;
9116 merged = FALSE;
9117 for (o = abfd->sections; o != NULL; o = o->next)
9119 struct bfd_elf_section_data *esdo = elf_section_data (o);
9120 o->reloc_count = 0;
9122 for (p = o->map_head.link_order; p != NULL; p = p->next)
9124 unsigned int reloc_count = 0;
9125 struct bfd_elf_section_data *esdi = NULL;
9126 unsigned int *rel_count1;
9128 if (p->type == bfd_section_reloc_link_order
9129 || p->type == bfd_symbol_reloc_link_order)
9130 reloc_count = 1;
9131 else if (p->type == bfd_indirect_link_order)
9133 asection *sec;
9135 sec = p->u.indirect.section;
9136 esdi = elf_section_data (sec);
9138 /* Mark all sections which are to be included in the
9139 link. This will normally be every section. We need
9140 to do this so that we can identify any sections which
9141 the linker has decided to not include. */
9142 sec->linker_mark = TRUE;
9144 if (sec->flags & SEC_MERGE)
9145 merged = TRUE;
9147 if (info->relocatable || info->emitrelocations)
9148 reloc_count = sec->reloc_count;
9149 else if (bed->elf_backend_count_relocs)
9151 Elf_Internal_Rela * relocs;
9153 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
9154 info->keep_memory);
9156 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9158 if (elf_section_data (o)->relocs != relocs)
9159 free (relocs);
9162 if (sec->rawsize > max_contents_size)
9163 max_contents_size = sec->rawsize;
9164 if (sec->size > max_contents_size)
9165 max_contents_size = sec->size;
9167 /* We are interested in just local symbols, not all
9168 symbols. */
9169 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
9170 && (sec->owner->flags & DYNAMIC) == 0)
9172 size_t sym_count;
9174 if (elf_bad_symtab (sec->owner))
9175 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
9176 / bed->s->sizeof_sym);
9177 else
9178 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
9180 if (sym_count > max_sym_count)
9181 max_sym_count = sym_count;
9183 if (sym_count > max_sym_shndx_count
9184 && elf_symtab_shndx (sec->owner) != 0)
9185 max_sym_shndx_count = sym_count;
9187 if ((sec->flags & SEC_RELOC) != 0)
9189 size_t ext_size;
9191 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
9192 if (ext_size > max_external_reloc_size)
9193 max_external_reloc_size = ext_size;
9194 if (sec->reloc_count > max_internal_reloc_count)
9195 max_internal_reloc_count = sec->reloc_count;
9200 if (reloc_count == 0)
9201 continue;
9203 o->reloc_count += reloc_count;
9205 /* MIPS may have a mix of REL and RELA relocs on sections.
9206 To support this curious ABI we keep reloc counts in
9207 elf_section_data too. We must be careful to add the
9208 relocations from the input section to the right output
9209 count. FIXME: Get rid of one count. We have
9210 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9211 rel_count1 = &esdo->rel_count;
9212 if (esdi != NULL)
9214 bfd_boolean same_size;
9215 bfd_size_type entsize1;
9217 entsize1 = esdi->rel_hdr.sh_entsize;
9218 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
9219 || entsize1 == bed->s->sizeof_rela);
9220 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
9222 if (!same_size)
9223 rel_count1 = &esdo->rel_count2;
9225 if (esdi->rel_hdr2 != NULL)
9227 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
9228 unsigned int alt_count;
9229 unsigned int *rel_count2;
9231 BFD_ASSERT (entsize2 != entsize1
9232 && (entsize2 == bed->s->sizeof_rel
9233 || entsize2 == bed->s->sizeof_rela));
9235 rel_count2 = &esdo->rel_count2;
9236 if (!same_size)
9237 rel_count2 = &esdo->rel_count;
9239 /* The following is probably too simplistic if the
9240 backend counts output relocs unusually. */
9241 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
9242 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9243 *rel_count2 += alt_count;
9244 reloc_count -= alt_count;
9247 *rel_count1 += reloc_count;
9250 if (o->reloc_count > 0)
9251 o->flags |= SEC_RELOC;
9252 else
9254 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9255 set it (this is probably a bug) and if it is set
9256 assign_section_numbers will create a reloc section. */
9257 o->flags &=~ SEC_RELOC;
9260 /* If the SEC_ALLOC flag is not set, force the section VMA to
9261 zero. This is done in elf_fake_sections as well, but forcing
9262 the VMA to 0 here will ensure that relocs against these
9263 sections are handled correctly. */
9264 if ((o->flags & SEC_ALLOC) == 0
9265 && ! o->user_set_vma)
9266 o->vma = 0;
9269 if (! info->relocatable && merged)
9270 elf_link_hash_traverse (elf_hash_table (info),
9271 _bfd_elf_link_sec_merge_syms, abfd);
9273 /* Figure out the file positions for everything but the symbol table
9274 and the relocs. We set symcount to force assign_section_numbers
9275 to create a symbol table. */
9276 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
9277 BFD_ASSERT (! abfd->output_has_begun);
9278 if (! _bfd_elf_compute_section_file_positions (abfd, info))
9279 goto error_return;
9281 /* Set sizes, and assign file positions for reloc sections. */
9282 for (o = abfd->sections; o != NULL; o = o->next)
9284 if ((o->flags & SEC_RELOC) != 0)
9286 if (!(_bfd_elf_link_size_reloc_section
9287 (abfd, &elf_section_data (o)->rel_hdr, o)))
9288 goto error_return;
9290 if (elf_section_data (o)->rel_hdr2
9291 && !(_bfd_elf_link_size_reloc_section
9292 (abfd, elf_section_data (o)->rel_hdr2, o)))
9293 goto error_return;
9296 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9297 to count upwards while actually outputting the relocations. */
9298 elf_section_data (o)->rel_count = 0;
9299 elf_section_data (o)->rel_count2 = 0;
9302 _bfd_elf_assign_file_positions_for_relocs (abfd);
9304 /* We have now assigned file positions for all the sections except
9305 .symtab and .strtab. We start the .symtab section at the current
9306 file position, and write directly to it. We build the .strtab
9307 section in memory. */
9308 bfd_get_symcount (abfd) = 0;
9309 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9310 /* sh_name is set in prep_headers. */
9311 symtab_hdr->sh_type = SHT_SYMTAB;
9312 /* sh_flags, sh_addr and sh_size all start off zero. */
9313 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
9314 /* sh_link is set in assign_section_numbers. */
9315 /* sh_info is set below. */
9316 /* sh_offset is set just below. */
9317 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
9319 off = elf_tdata (abfd)->next_file_pos;
9320 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
9322 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9323 incorrect. We do not yet know the size of the .symtab section.
9324 We correct next_file_pos below, after we do know the size. */
9326 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9327 continuously seeking to the right position in the file. */
9328 if (! info->keep_memory || max_sym_count < 20)
9329 finfo.symbuf_size = 20;
9330 else
9331 finfo.symbuf_size = max_sym_count;
9332 amt = finfo.symbuf_size;
9333 amt *= bed->s->sizeof_sym;
9334 finfo.symbuf = bfd_malloc (amt);
9335 if (finfo.symbuf == NULL)
9336 goto error_return;
9337 if (elf_numsections (abfd) > SHN_LORESERVE)
9339 /* Wild guess at number of output symbols. realloc'd as needed. */
9340 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
9341 finfo.shndxbuf_size = amt;
9342 amt *= sizeof (Elf_External_Sym_Shndx);
9343 finfo.symshndxbuf = bfd_zmalloc (amt);
9344 if (finfo.symshndxbuf == NULL)
9345 goto error_return;
9348 /* Start writing out the symbol table. The first symbol is always a
9349 dummy symbol. */
9350 if (info->strip != strip_all
9351 || emit_relocs)
9353 elfsym.st_value = 0;
9354 elfsym.st_size = 0;
9355 elfsym.st_info = 0;
9356 elfsym.st_other = 0;
9357 elfsym.st_shndx = SHN_UNDEF;
9358 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
9359 NULL))
9360 goto error_return;
9363 /* Output a symbol for each section. We output these even if we are
9364 discarding local symbols, since they are used for relocs. These
9365 symbols have no names. We store the index of each one in the
9366 index field of the section, so that we can find it again when
9367 outputting relocs. */
9368 if (info->strip != strip_all
9369 || emit_relocs)
9371 elfsym.st_size = 0;
9372 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9373 elfsym.st_other = 0;
9374 elfsym.st_value = 0;
9375 for (i = 1; i < elf_numsections (abfd); i++)
9377 o = bfd_section_from_elf_index (abfd, i);
9378 if (o != NULL)
9380 o->target_index = bfd_get_symcount (abfd);
9381 elfsym.st_shndx = i;
9382 if (!info->relocatable)
9383 elfsym.st_value = o->vma;
9384 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
9385 goto error_return;
9387 if (i == SHN_LORESERVE - 1)
9388 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
9392 /* Allocate some memory to hold information read in from the input
9393 files. */
9394 if (max_contents_size != 0)
9396 finfo.contents = bfd_malloc (max_contents_size);
9397 if (finfo.contents == NULL)
9398 goto error_return;
9401 if (max_external_reloc_size != 0)
9403 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9404 if (finfo.external_relocs == NULL)
9405 goto error_return;
9408 if (max_internal_reloc_count != 0)
9410 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
9411 amt *= sizeof (Elf_Internal_Rela);
9412 finfo.internal_relocs = bfd_malloc (amt);
9413 if (finfo.internal_relocs == NULL)
9414 goto error_return;
9417 if (max_sym_count != 0)
9419 amt = max_sym_count * bed->s->sizeof_sym;
9420 finfo.external_syms = bfd_malloc (amt);
9421 if (finfo.external_syms == NULL)
9422 goto error_return;
9424 amt = max_sym_count * sizeof (Elf_Internal_Sym);
9425 finfo.internal_syms = bfd_malloc (amt);
9426 if (finfo.internal_syms == NULL)
9427 goto error_return;
9429 amt = max_sym_count * sizeof (long);
9430 finfo.indices = bfd_malloc (amt);
9431 if (finfo.indices == NULL)
9432 goto error_return;
9434 amt = max_sym_count * sizeof (asection *);
9435 finfo.sections = bfd_malloc (amt);
9436 if (finfo.sections == NULL)
9437 goto error_return;
9440 if (max_sym_shndx_count != 0)
9442 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
9443 finfo.locsym_shndx = bfd_malloc (amt);
9444 if (finfo.locsym_shndx == NULL)
9445 goto error_return;
9448 if (elf_hash_table (info)->tls_sec)
9450 bfd_vma base, end = 0;
9451 asection *sec;
9453 for (sec = elf_hash_table (info)->tls_sec;
9454 sec && (sec->flags & SEC_THREAD_LOCAL);
9455 sec = sec->next)
9457 bfd_size_type size = sec->size;
9459 if (size == 0
9460 && (sec->flags & SEC_HAS_CONTENTS) == 0)
9462 struct bfd_link_order *o = sec->map_tail.link_order;
9463 if (o != NULL)
9464 size = o->offset + o->size;
9466 end = sec->vma + size;
9468 base = elf_hash_table (info)->tls_sec->vma;
9469 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
9470 elf_hash_table (info)->tls_size = end - base;
9473 /* Reorder SHF_LINK_ORDER sections. */
9474 for (o = abfd->sections; o != NULL; o = o->next)
9476 if (!elf_fixup_link_order (abfd, o))
9477 return FALSE;
9480 /* Since ELF permits relocations to be against local symbols, we
9481 must have the local symbols available when we do the relocations.
9482 Since we would rather only read the local symbols once, and we
9483 would rather not keep them in memory, we handle all the
9484 relocations for a single input file at the same time.
9486 Unfortunately, there is no way to know the total number of local
9487 symbols until we have seen all of them, and the local symbol
9488 indices precede the global symbol indices. This means that when
9489 we are generating relocatable output, and we see a reloc against
9490 a global symbol, we can not know the symbol index until we have
9491 finished examining all the local symbols to see which ones we are
9492 going to output. To deal with this, we keep the relocations in
9493 memory, and don't output them until the end of the link. This is
9494 an unfortunate waste of memory, but I don't see a good way around
9495 it. Fortunately, it only happens when performing a relocatable
9496 link, which is not the common case. FIXME: If keep_memory is set
9497 we could write the relocs out and then read them again; I don't
9498 know how bad the memory loss will be. */
9500 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9501 sub->output_has_begun = FALSE;
9502 for (o = abfd->sections; o != NULL; o = o->next)
9504 for (p = o->map_head.link_order; p != NULL; p = p->next)
9506 if (p->type == bfd_indirect_link_order
9507 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
9508 == bfd_target_elf_flavour)
9509 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
9511 if (! sub->output_has_begun)
9513 if (! elf_link_input_bfd (&finfo, sub))
9514 goto error_return;
9515 sub->output_has_begun = TRUE;
9518 else if (p->type == bfd_section_reloc_link_order
9519 || p->type == bfd_symbol_reloc_link_order)
9521 if (! elf_reloc_link_order (abfd, info, o, p))
9522 goto error_return;
9524 else
9526 if (! _bfd_default_link_order (abfd, info, o, p))
9527 goto error_return;
9532 /* Free symbol buffer if needed. */
9533 if (!info->reduce_memory_overheads)
9535 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9536 if (elf_tdata (sub)->symbuf)
9538 free (elf_tdata (sub)->symbuf);
9539 elf_tdata (sub)->symbuf = NULL;
9543 /* Output any global symbols that got converted to local in a
9544 version script or due to symbol visibility. We do this in a
9545 separate step since ELF requires all local symbols to appear
9546 prior to any global symbols. FIXME: We should only do this if
9547 some global symbols were, in fact, converted to become local.
9548 FIXME: Will this work correctly with the Irix 5 linker? */
9549 eoinfo.failed = FALSE;
9550 eoinfo.finfo = &finfo;
9551 eoinfo.localsyms = TRUE;
9552 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9553 &eoinfo);
9554 if (eoinfo.failed)
9555 return FALSE;
9557 /* If backend needs to output some local symbols not present in the hash
9558 table, do it now. */
9559 if (bed->elf_backend_output_arch_local_syms)
9561 typedef bfd_boolean (*out_sym_func)
9562 (void *, const char *, Elf_Internal_Sym *, asection *,
9563 struct elf_link_hash_entry *);
9565 if (! ((*bed->elf_backend_output_arch_local_syms)
9566 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9567 return FALSE;
9570 /* That wrote out all the local symbols. Finish up the symbol table
9571 with the global symbols. Even if we want to strip everything we
9572 can, we still need to deal with those global symbols that got
9573 converted to local in a version script. */
9575 /* The sh_info field records the index of the first non local symbol. */
9576 symtab_hdr->sh_info = bfd_get_symcount (abfd);
9578 if (dynamic
9579 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
9581 Elf_Internal_Sym sym;
9582 bfd_byte *dynsym = finfo.dynsym_sec->contents;
9583 long last_local = 0;
9585 /* Write out the section symbols for the output sections. */
9586 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
9588 asection *s;
9590 sym.st_size = 0;
9591 sym.st_name = 0;
9592 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9593 sym.st_other = 0;
9595 for (s = abfd->sections; s != NULL; s = s->next)
9597 int indx;
9598 bfd_byte *dest;
9599 long dynindx;
9601 dynindx = elf_section_data (s)->dynindx;
9602 if (dynindx <= 0)
9603 continue;
9604 indx = elf_section_data (s)->this_idx;
9605 BFD_ASSERT (indx > 0);
9606 sym.st_shndx = indx;
9607 if (! check_dynsym (abfd, &sym))
9608 return FALSE;
9609 sym.st_value = s->vma;
9610 dest = dynsym + dynindx * bed->s->sizeof_sym;
9611 if (last_local < dynindx)
9612 last_local = dynindx;
9613 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9617 /* Write out the local dynsyms. */
9618 if (elf_hash_table (info)->dynlocal)
9620 struct elf_link_local_dynamic_entry *e;
9621 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
9623 asection *s;
9624 bfd_byte *dest;
9626 sym.st_size = e->isym.st_size;
9627 sym.st_other = e->isym.st_other;
9629 /* Copy the internal symbol as is.
9630 Note that we saved a word of storage and overwrote
9631 the original st_name with the dynstr_index. */
9632 sym = e->isym;
9634 if (e->isym.st_shndx != SHN_UNDEF
9635 && (e->isym.st_shndx < SHN_LORESERVE
9636 || e->isym.st_shndx > SHN_HIRESERVE))
9638 s = bfd_section_from_elf_index (e->input_bfd,
9639 e->isym.st_shndx);
9641 sym.st_shndx =
9642 elf_section_data (s->output_section)->this_idx;
9643 if (! check_dynsym (abfd, &sym))
9644 return FALSE;
9645 sym.st_value = (s->output_section->vma
9646 + s->output_offset
9647 + e->isym.st_value);
9650 if (last_local < e->dynindx)
9651 last_local = e->dynindx;
9653 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
9654 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9658 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
9659 last_local + 1;
9662 /* We get the global symbols from the hash table. */
9663 eoinfo.failed = FALSE;
9664 eoinfo.localsyms = FALSE;
9665 eoinfo.finfo = &finfo;
9666 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9667 &eoinfo);
9668 if (eoinfo.failed)
9669 return FALSE;
9671 /* If backend needs to output some symbols not present in the hash
9672 table, do it now. */
9673 if (bed->elf_backend_output_arch_syms)
9675 typedef bfd_boolean (*out_sym_func)
9676 (void *, const char *, Elf_Internal_Sym *, asection *,
9677 struct elf_link_hash_entry *);
9679 if (! ((*bed->elf_backend_output_arch_syms)
9680 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9681 return FALSE;
9684 /* Flush all symbols to the file. */
9685 if (! elf_link_flush_output_syms (&finfo, bed))
9686 return FALSE;
9688 /* Now we know the size of the symtab section. */
9689 off += symtab_hdr->sh_size;
9691 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
9692 if (symtab_shndx_hdr->sh_name != 0)
9694 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
9695 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
9696 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
9697 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
9698 symtab_shndx_hdr->sh_size = amt;
9700 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
9701 off, TRUE);
9703 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
9704 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
9705 return FALSE;
9709 /* Finish up and write out the symbol string table (.strtab)
9710 section. */
9711 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
9712 /* sh_name was set in prep_headers. */
9713 symstrtab_hdr->sh_type = SHT_STRTAB;
9714 symstrtab_hdr->sh_flags = 0;
9715 symstrtab_hdr->sh_addr = 0;
9716 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
9717 symstrtab_hdr->sh_entsize = 0;
9718 symstrtab_hdr->sh_link = 0;
9719 symstrtab_hdr->sh_info = 0;
9720 /* sh_offset is set just below. */
9721 symstrtab_hdr->sh_addralign = 1;
9723 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
9724 elf_tdata (abfd)->next_file_pos = off;
9726 if (bfd_get_symcount (abfd) > 0)
9728 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
9729 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
9730 return FALSE;
9733 /* Adjust the relocs to have the correct symbol indices. */
9734 for (o = abfd->sections; o != NULL; o = o->next)
9736 if ((o->flags & SEC_RELOC) == 0)
9737 continue;
9739 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
9740 elf_section_data (o)->rel_count,
9741 elf_section_data (o)->rel_hashes);
9742 if (elf_section_data (o)->rel_hdr2 != NULL)
9743 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
9744 elf_section_data (o)->rel_count2,
9745 (elf_section_data (o)->rel_hashes
9746 + elf_section_data (o)->rel_count));
9748 /* Set the reloc_count field to 0 to prevent write_relocs from
9749 trying to swap the relocs out itself. */
9750 o->reloc_count = 0;
9753 if (dynamic && info->combreloc && dynobj != NULL)
9754 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
9756 /* If we are linking against a dynamic object, or generating a
9757 shared library, finish up the dynamic linking information. */
9758 if (dynamic)
9760 bfd_byte *dyncon, *dynconend;
9762 /* Fix up .dynamic entries. */
9763 o = bfd_get_section_by_name (dynobj, ".dynamic");
9764 BFD_ASSERT (o != NULL);
9766 dyncon = o->contents;
9767 dynconend = o->contents + o->size;
9768 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9770 Elf_Internal_Dyn dyn;
9771 const char *name;
9772 unsigned int type;
9774 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9776 switch (dyn.d_tag)
9778 default:
9779 continue;
9780 case DT_NULL:
9781 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
9783 switch (elf_section_data (reldyn)->this_hdr.sh_type)
9785 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
9786 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
9787 default: continue;
9789 dyn.d_un.d_val = relativecount;
9790 relativecount = 0;
9791 break;
9793 continue;
9795 case DT_INIT:
9796 name = info->init_function;
9797 goto get_sym;
9798 case DT_FINI:
9799 name = info->fini_function;
9800 get_sym:
9802 struct elf_link_hash_entry *h;
9804 h = elf_link_hash_lookup (elf_hash_table (info), name,
9805 FALSE, FALSE, TRUE);
9806 if (h != NULL
9807 && (h->root.type == bfd_link_hash_defined
9808 || h->root.type == bfd_link_hash_defweak))
9810 dyn.d_un.d_val = h->root.u.def.value;
9811 o = h->root.u.def.section;
9812 if (o->output_section != NULL)
9813 dyn.d_un.d_val += (o->output_section->vma
9814 + o->output_offset);
9815 else
9817 /* The symbol is imported from another shared
9818 library and does not apply to this one. */
9819 dyn.d_un.d_val = 0;
9821 break;
9824 continue;
9826 case DT_PREINIT_ARRAYSZ:
9827 name = ".preinit_array";
9828 goto get_size;
9829 case DT_INIT_ARRAYSZ:
9830 name = ".init_array";
9831 goto get_size;
9832 case DT_FINI_ARRAYSZ:
9833 name = ".fini_array";
9834 get_size:
9835 o = bfd_get_section_by_name (abfd, name);
9836 if (o == NULL)
9838 (*_bfd_error_handler)
9839 (_("%B: could not find output section %s"), abfd, name);
9840 goto error_return;
9842 if (o->size == 0)
9843 (*_bfd_error_handler)
9844 (_("warning: %s section has zero size"), name);
9845 dyn.d_un.d_val = o->size;
9846 break;
9848 case DT_PREINIT_ARRAY:
9849 name = ".preinit_array";
9850 goto get_vma;
9851 case DT_INIT_ARRAY:
9852 name = ".init_array";
9853 goto get_vma;
9854 case DT_FINI_ARRAY:
9855 name = ".fini_array";
9856 goto get_vma;
9858 case DT_HASH:
9859 name = ".hash";
9860 goto get_vma;
9861 case DT_GNU_HASH:
9862 name = ".gnu.hash";
9863 goto get_vma;
9864 case DT_STRTAB:
9865 name = ".dynstr";
9866 goto get_vma;
9867 case DT_SYMTAB:
9868 name = ".dynsym";
9869 goto get_vma;
9870 case DT_VERDEF:
9871 name = ".gnu.version_d";
9872 goto get_vma;
9873 case DT_VERNEED:
9874 name = ".gnu.version_r";
9875 goto get_vma;
9876 case DT_VERSYM:
9877 name = ".gnu.version";
9878 get_vma:
9879 o = bfd_get_section_by_name (abfd, name);
9880 if (o == NULL)
9882 (*_bfd_error_handler)
9883 (_("%B: could not find output section %s"), abfd, name);
9884 goto error_return;
9886 dyn.d_un.d_ptr = o->vma;
9887 break;
9889 case DT_REL:
9890 case DT_RELA:
9891 case DT_RELSZ:
9892 case DT_RELASZ:
9893 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
9894 type = SHT_REL;
9895 else
9896 type = SHT_RELA;
9897 dyn.d_un.d_val = 0;
9898 for (i = 1; i < elf_numsections (abfd); i++)
9900 Elf_Internal_Shdr *hdr;
9902 hdr = elf_elfsections (abfd)[i];
9903 if (hdr->sh_type == type
9904 && (hdr->sh_flags & SHF_ALLOC) != 0)
9906 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
9907 dyn.d_un.d_val += hdr->sh_size;
9908 else
9910 if (dyn.d_un.d_val == 0
9911 || hdr->sh_addr < dyn.d_un.d_val)
9912 dyn.d_un.d_val = hdr->sh_addr;
9916 break;
9918 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
9922 /* If we have created any dynamic sections, then output them. */
9923 if (dynobj != NULL)
9925 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
9926 goto error_return;
9928 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9929 if (info->warn_shared_textrel && info->shared)
9931 bfd_byte *dyncon, *dynconend;
9933 /* Fix up .dynamic entries. */
9934 o = bfd_get_section_by_name (dynobj, ".dynamic");
9935 BFD_ASSERT (o != NULL);
9937 dyncon = o->contents;
9938 dynconend = o->contents + o->size;
9939 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9941 Elf_Internal_Dyn dyn;
9943 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9945 if (dyn.d_tag == DT_TEXTREL)
9947 _bfd_error_handler
9948 (_("warning: creating a DT_TEXTREL in a shared object."));
9949 break;
9954 for (o = dynobj->sections; o != NULL; o = o->next)
9956 if ((o->flags & SEC_HAS_CONTENTS) == 0
9957 || o->size == 0
9958 || o->output_section == bfd_abs_section_ptr)
9959 continue;
9960 if ((o->flags & SEC_LINKER_CREATED) == 0)
9962 /* At this point, we are only interested in sections
9963 created by _bfd_elf_link_create_dynamic_sections. */
9964 continue;
9966 if (elf_hash_table (info)->stab_info.stabstr == o)
9967 continue;
9968 if (elf_hash_table (info)->eh_info.hdr_sec == o)
9969 continue;
9970 if ((elf_section_data (o->output_section)->this_hdr.sh_type
9971 != SHT_STRTAB)
9972 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
9974 if (! bfd_set_section_contents (abfd, o->output_section,
9975 o->contents,
9976 (file_ptr) o->output_offset,
9977 o->size))
9978 goto error_return;
9980 else
9982 /* The contents of the .dynstr section are actually in a
9983 stringtab. */
9984 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
9985 if (bfd_seek (abfd, off, SEEK_SET) != 0
9986 || ! _bfd_elf_strtab_emit (abfd,
9987 elf_hash_table (info)->dynstr))
9988 goto error_return;
9993 if (info->relocatable)
9995 bfd_boolean failed = FALSE;
9997 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
9998 if (failed)
9999 goto error_return;
10002 /* If we have optimized stabs strings, output them. */
10003 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10005 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10006 goto error_return;
10009 if (info->eh_frame_hdr)
10011 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10012 goto error_return;
10015 if (finfo.symstrtab != NULL)
10016 _bfd_stringtab_free (finfo.symstrtab);
10017 if (finfo.contents != NULL)
10018 free (finfo.contents);
10019 if (finfo.external_relocs != NULL)
10020 free (finfo.external_relocs);
10021 if (finfo.internal_relocs != NULL)
10022 free (finfo.internal_relocs);
10023 if (finfo.external_syms != NULL)
10024 free (finfo.external_syms);
10025 if (finfo.locsym_shndx != NULL)
10026 free (finfo.locsym_shndx);
10027 if (finfo.internal_syms != NULL)
10028 free (finfo.internal_syms);
10029 if (finfo.indices != NULL)
10030 free (finfo.indices);
10031 if (finfo.sections != NULL)
10032 free (finfo.sections);
10033 if (finfo.symbuf != NULL)
10034 free (finfo.symbuf);
10035 if (finfo.symshndxbuf != NULL)
10036 free (finfo.symshndxbuf);
10037 for (o = abfd->sections; o != NULL; o = o->next)
10039 if ((o->flags & SEC_RELOC) != 0
10040 && elf_section_data (o)->rel_hashes != NULL)
10041 free (elf_section_data (o)->rel_hashes);
10044 elf_tdata (abfd)->linker = TRUE;
10046 return TRUE;
10048 error_return:
10049 if (finfo.symstrtab != NULL)
10050 _bfd_stringtab_free (finfo.symstrtab);
10051 if (finfo.contents != NULL)
10052 free (finfo.contents);
10053 if (finfo.external_relocs != NULL)
10054 free (finfo.external_relocs);
10055 if (finfo.internal_relocs != NULL)
10056 free (finfo.internal_relocs);
10057 if (finfo.external_syms != NULL)
10058 free (finfo.external_syms);
10059 if (finfo.locsym_shndx != NULL)
10060 free (finfo.locsym_shndx);
10061 if (finfo.internal_syms != NULL)
10062 free (finfo.internal_syms);
10063 if (finfo.indices != NULL)
10064 free (finfo.indices);
10065 if (finfo.sections != NULL)
10066 free (finfo.sections);
10067 if (finfo.symbuf != NULL)
10068 free (finfo.symbuf);
10069 if (finfo.symshndxbuf != NULL)
10070 free (finfo.symshndxbuf);
10071 for (o = abfd->sections; o != NULL; o = o->next)
10073 if ((o->flags & SEC_RELOC) != 0
10074 && elf_section_data (o)->rel_hashes != NULL)
10075 free (elf_section_data (o)->rel_hashes);
10078 return FALSE;
10081 /* Garbage collect unused sections. */
10083 /* Default gc_mark_hook. */
10085 asection *
10086 _bfd_elf_gc_mark_hook (asection *sec,
10087 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10088 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
10089 struct elf_link_hash_entry *h,
10090 Elf_Internal_Sym *sym)
10092 if (h != NULL)
10094 switch (h->root.type)
10096 case bfd_link_hash_defined:
10097 case bfd_link_hash_defweak:
10098 return h->root.u.def.section;
10100 case bfd_link_hash_common:
10101 return h->root.u.c.p->section;
10103 default:
10104 break;
10107 else
10108 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
10110 return NULL;
10113 /* The mark phase of garbage collection. For a given section, mark
10114 it and any sections in this section's group, and all the sections
10115 which define symbols to which it refers. */
10117 bfd_boolean
10118 _bfd_elf_gc_mark (struct bfd_link_info *info,
10119 asection *sec,
10120 elf_gc_mark_hook_fn gc_mark_hook)
10122 bfd_boolean ret;
10123 bfd_boolean is_eh;
10124 asection *group_sec;
10126 sec->gc_mark = 1;
10128 /* Mark all the sections in the group. */
10129 group_sec = elf_section_data (sec)->next_in_group;
10130 if (group_sec && !group_sec->gc_mark)
10131 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
10132 return FALSE;
10134 /* Look through the section relocs. */
10135 ret = TRUE;
10136 is_eh = strcmp (sec->name, ".eh_frame") == 0;
10137 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
10139 Elf_Internal_Rela *relstart, *rel, *relend;
10140 Elf_Internal_Shdr *symtab_hdr;
10141 struct elf_link_hash_entry **sym_hashes;
10142 size_t nlocsyms;
10143 size_t extsymoff;
10144 bfd *input_bfd = sec->owner;
10145 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
10146 Elf_Internal_Sym *isym = NULL;
10147 int r_sym_shift;
10149 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10150 sym_hashes = elf_sym_hashes (input_bfd);
10152 /* Read the local symbols. */
10153 if (elf_bad_symtab (input_bfd))
10155 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
10156 extsymoff = 0;
10158 else
10159 extsymoff = nlocsyms = symtab_hdr->sh_info;
10161 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
10162 if (isym == NULL && nlocsyms != 0)
10164 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
10165 NULL, NULL, NULL);
10166 if (isym == NULL)
10167 return FALSE;
10170 /* Read the relocations. */
10171 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
10172 info->keep_memory);
10173 if (relstart == NULL)
10175 ret = FALSE;
10176 goto out1;
10178 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10180 if (bed->s->arch_size == 32)
10181 r_sym_shift = 8;
10182 else
10183 r_sym_shift = 32;
10185 for (rel = relstart; rel < relend; rel++)
10187 unsigned long r_symndx;
10188 asection *rsec;
10189 struct elf_link_hash_entry *h;
10191 r_symndx = rel->r_info >> r_sym_shift;
10192 if (r_symndx == 0)
10193 continue;
10195 if (r_symndx >= nlocsyms
10196 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
10198 h = sym_hashes[r_symndx - extsymoff];
10199 while (h->root.type == bfd_link_hash_indirect
10200 || h->root.type == bfd_link_hash_warning)
10201 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10202 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
10204 else
10206 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
10209 if (rsec && !rsec->gc_mark)
10211 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
10212 rsec->gc_mark = 1;
10213 else if (is_eh)
10214 rsec->gc_mark_from_eh = 1;
10215 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
10217 ret = FALSE;
10218 goto out2;
10223 out2:
10224 if (elf_section_data (sec)->relocs != relstart)
10225 free (relstart);
10226 out1:
10227 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
10229 if (! info->keep_memory)
10230 free (isym);
10231 else
10232 symtab_hdr->contents = (unsigned char *) isym;
10236 return ret;
10239 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10241 struct elf_gc_sweep_symbol_info
10243 struct bfd_link_info *info;
10244 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
10245 bfd_boolean);
10248 static bfd_boolean
10249 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
10251 if (h->root.type == bfd_link_hash_warning)
10252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10254 if ((h->root.type == bfd_link_hash_defined
10255 || h->root.type == bfd_link_hash_defweak)
10256 && !h->root.u.def.section->gc_mark
10257 && !(h->root.u.def.section->owner->flags & DYNAMIC))
10259 struct elf_gc_sweep_symbol_info *inf = data;
10260 (*inf->hide_symbol) (inf->info, h, TRUE);
10263 return TRUE;
10266 /* The sweep phase of garbage collection. Remove all garbage sections. */
10268 typedef bfd_boolean (*gc_sweep_hook_fn)
10269 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
10271 static bfd_boolean
10272 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
10274 bfd *sub;
10275 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10276 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
10277 unsigned long section_sym_count;
10278 struct elf_gc_sweep_symbol_info sweep_info;
10280 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10282 asection *o;
10284 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10285 continue;
10287 for (o = sub->sections; o != NULL; o = o->next)
10289 /* Keep debug and special sections. */
10290 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
10291 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
10292 o->gc_mark = 1;
10294 if (o->gc_mark)
10295 continue;
10297 /* Skip sweeping sections already excluded. */
10298 if (o->flags & SEC_EXCLUDE)
10299 continue;
10301 /* Since this is early in the link process, it is simple
10302 to remove a section from the output. */
10303 o->flags |= SEC_EXCLUDE;
10305 if (info->print_gc_sections == TRUE)
10306 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
10308 /* But we also have to update some of the relocation
10309 info we collected before. */
10310 if (gc_sweep_hook
10311 && (o->flags & SEC_RELOC) != 0
10312 && o->reloc_count > 0
10313 && !bfd_is_abs_section (o->output_section))
10315 Elf_Internal_Rela *internal_relocs;
10316 bfd_boolean r;
10318 internal_relocs
10319 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
10320 info->keep_memory);
10321 if (internal_relocs == NULL)
10322 return FALSE;
10324 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
10326 if (elf_section_data (o)->relocs != internal_relocs)
10327 free (internal_relocs);
10329 if (!r)
10330 return FALSE;
10335 /* Remove the symbols that were in the swept sections from the dynamic
10336 symbol table. GCFIXME: Anyone know how to get them out of the
10337 static symbol table as well? */
10338 sweep_info.info = info;
10339 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
10340 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
10341 &sweep_info);
10343 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
10344 return TRUE;
10347 /* Propagate collected vtable information. This is called through
10348 elf_link_hash_traverse. */
10350 static bfd_boolean
10351 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
10353 if (h->root.type == bfd_link_hash_warning)
10354 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10356 /* Those that are not vtables. */
10357 if (h->vtable == NULL || h->vtable->parent == NULL)
10358 return TRUE;
10360 /* Those vtables that do not have parents, we cannot merge. */
10361 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
10362 return TRUE;
10364 /* If we've already been done, exit. */
10365 if (h->vtable->used && h->vtable->used[-1])
10366 return TRUE;
10368 /* Make sure the parent's table is up to date. */
10369 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
10371 if (h->vtable->used == NULL)
10373 /* None of this table's entries were referenced. Re-use the
10374 parent's table. */
10375 h->vtable->used = h->vtable->parent->vtable->used;
10376 h->vtable->size = h->vtable->parent->vtable->size;
10378 else
10380 size_t n;
10381 bfd_boolean *cu, *pu;
10383 /* Or the parent's entries into ours. */
10384 cu = h->vtable->used;
10385 cu[-1] = TRUE;
10386 pu = h->vtable->parent->vtable->used;
10387 if (pu != NULL)
10389 const struct elf_backend_data *bed;
10390 unsigned int log_file_align;
10392 bed = get_elf_backend_data (h->root.u.def.section->owner);
10393 log_file_align = bed->s->log_file_align;
10394 n = h->vtable->parent->vtable->size >> log_file_align;
10395 while (n--)
10397 if (*pu)
10398 *cu = TRUE;
10399 pu++;
10400 cu++;
10405 return TRUE;
10408 static bfd_boolean
10409 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
10411 asection *sec;
10412 bfd_vma hstart, hend;
10413 Elf_Internal_Rela *relstart, *relend, *rel;
10414 const struct elf_backend_data *bed;
10415 unsigned int log_file_align;
10417 if (h->root.type == bfd_link_hash_warning)
10418 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10420 /* Take care of both those symbols that do not describe vtables as
10421 well as those that are not loaded. */
10422 if (h->vtable == NULL || h->vtable->parent == NULL)
10423 return TRUE;
10425 BFD_ASSERT (h->root.type == bfd_link_hash_defined
10426 || h->root.type == bfd_link_hash_defweak);
10428 sec = h->root.u.def.section;
10429 hstart = h->root.u.def.value;
10430 hend = hstart + h->size;
10432 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
10433 if (!relstart)
10434 return *(bfd_boolean *) okp = FALSE;
10435 bed = get_elf_backend_data (sec->owner);
10436 log_file_align = bed->s->log_file_align;
10438 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10440 for (rel = relstart; rel < relend; ++rel)
10441 if (rel->r_offset >= hstart && rel->r_offset < hend)
10443 /* If the entry is in use, do nothing. */
10444 if (h->vtable->used
10445 && (rel->r_offset - hstart) < h->vtable->size)
10447 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
10448 if (h->vtable->used[entry])
10449 continue;
10451 /* Otherwise, kill it. */
10452 rel->r_offset = rel->r_info = rel->r_addend = 0;
10455 return TRUE;
10458 /* Mark sections containing dynamically referenced symbols. When
10459 building shared libraries, we must assume that any visible symbol is
10460 referenced. */
10462 bfd_boolean
10463 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
10465 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10467 if (h->root.type == bfd_link_hash_warning)
10468 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10470 if ((h->root.type == bfd_link_hash_defined
10471 || h->root.type == bfd_link_hash_defweak)
10472 && (h->ref_dynamic
10473 || (!info->executable
10474 && h->def_regular
10475 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
10476 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
10477 h->root.u.def.section->flags |= SEC_KEEP;
10479 return TRUE;
10482 /* Do mark and sweep of unused sections. */
10484 bfd_boolean
10485 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
10487 bfd_boolean ok = TRUE;
10488 bfd *sub;
10489 elf_gc_mark_hook_fn gc_mark_hook;
10490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10492 if (!bed->can_gc_sections
10493 || info->relocatable
10494 || info->emitrelocations
10495 || !is_elf_hash_table (info->hash))
10497 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
10498 return TRUE;
10501 /* Apply transitive closure to the vtable entry usage info. */
10502 elf_link_hash_traverse (elf_hash_table (info),
10503 elf_gc_propagate_vtable_entries_used,
10504 &ok);
10505 if (!ok)
10506 return FALSE;
10508 /* Kill the vtable relocations that were not used. */
10509 elf_link_hash_traverse (elf_hash_table (info),
10510 elf_gc_smash_unused_vtentry_relocs,
10511 &ok);
10512 if (!ok)
10513 return FALSE;
10515 /* Mark dynamically referenced symbols. */
10516 if (elf_hash_table (info)->dynamic_sections_created)
10517 elf_link_hash_traverse (elf_hash_table (info),
10518 bed->gc_mark_dynamic_ref,
10519 info);
10521 /* Grovel through relocs to find out who stays ... */
10522 gc_mark_hook = bed->gc_mark_hook;
10523 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10525 asection *o;
10527 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10528 continue;
10530 for (o = sub->sections; o != NULL; o = o->next)
10531 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
10532 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10533 return FALSE;
10536 /* Allow the backend to mark additional target specific sections. */
10537 if (bed->gc_mark_extra_sections)
10538 bed->gc_mark_extra_sections(info, gc_mark_hook);
10540 /* ... again for sections marked from eh_frame. */
10541 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10543 asection *o;
10545 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10546 continue;
10548 /* Keep .gcc_except_table.* if the associated .text.* (or the
10549 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10550 marked. This isn't very nice, but the proper solution,
10551 splitting .eh_frame up and using comdat doesn't pan out
10552 easily due to needing special relocs to handle the
10553 difference of two symbols in separate sections.
10554 Don't keep code sections referenced by .eh_frame. */
10555 #define TEXT_PREFIX ".text."
10556 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10557 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10558 for (o = sub->sections; o != NULL; o = o->next)
10559 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
10561 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
10563 char *fn_name;
10564 const char *sec_name;
10565 asection *fn_text;
10566 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
10568 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
10569 sec_name = o->name + o_name_prefix_len;
10570 fn_name_prefix_len = strlen (TEXT_PREFIX);
10571 tmp = strlen (TEXT_PREFIX2);
10572 if (tmp > fn_name_prefix_len)
10573 fn_name_prefix_len = tmp;
10574 fn_name
10575 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
10576 if (fn_name == NULL)
10577 return FALSE;
10579 /* Try the first prefix. */
10580 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
10581 fn_text = bfd_get_section_by_name (sub, fn_name);
10583 /* Try the second prefix. */
10584 if (fn_text == NULL)
10586 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
10587 fn_text = bfd_get_section_by_name (sub, fn_name);
10590 free (fn_name);
10591 if (fn_text == NULL || !fn_text->gc_mark)
10592 continue;
10595 /* If not using specially named exception table section,
10596 then keep whatever we are using. */
10597 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10598 return FALSE;
10602 /* ... and mark SEC_EXCLUDE for those that go. */
10603 return elf_gc_sweep (abfd, info);
10606 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10608 bfd_boolean
10609 bfd_elf_gc_record_vtinherit (bfd *abfd,
10610 asection *sec,
10611 struct elf_link_hash_entry *h,
10612 bfd_vma offset)
10614 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
10615 struct elf_link_hash_entry **search, *child;
10616 bfd_size_type extsymcount;
10617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10619 /* The sh_info field of the symtab header tells us where the
10620 external symbols start. We don't care about the local symbols at
10621 this point. */
10622 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
10623 if (!elf_bad_symtab (abfd))
10624 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
10626 sym_hashes = elf_sym_hashes (abfd);
10627 sym_hashes_end = sym_hashes + extsymcount;
10629 /* Hunt down the child symbol, which is in this section at the same
10630 offset as the relocation. */
10631 for (search = sym_hashes; search != sym_hashes_end; ++search)
10633 if ((child = *search) != NULL
10634 && (child->root.type == bfd_link_hash_defined
10635 || child->root.type == bfd_link_hash_defweak)
10636 && child->root.u.def.section == sec
10637 && child->root.u.def.value == offset)
10638 goto win;
10641 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
10642 abfd, sec, (unsigned long) offset);
10643 bfd_set_error (bfd_error_invalid_operation);
10644 return FALSE;
10646 win:
10647 if (!child->vtable)
10649 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
10650 if (!child->vtable)
10651 return FALSE;
10653 if (!h)
10655 /* This *should* only be the absolute section. It could potentially
10656 be that someone has defined a non-global vtable though, which
10657 would be bad. It isn't worth paging in the local symbols to be
10658 sure though; that case should simply be handled by the assembler. */
10660 child->vtable->parent = (struct elf_link_hash_entry *) -1;
10662 else
10663 child->vtable->parent = h;
10665 return TRUE;
10668 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10670 bfd_boolean
10671 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
10672 asection *sec ATTRIBUTE_UNUSED,
10673 struct elf_link_hash_entry *h,
10674 bfd_vma addend)
10676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10677 unsigned int log_file_align = bed->s->log_file_align;
10679 if (!h->vtable)
10681 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
10682 if (!h->vtable)
10683 return FALSE;
10686 if (addend >= h->vtable->size)
10688 size_t size, bytes, file_align;
10689 bfd_boolean *ptr = h->vtable->used;
10691 /* While the symbol is undefined, we have to be prepared to handle
10692 a zero size. */
10693 file_align = 1 << log_file_align;
10694 if (h->root.type == bfd_link_hash_undefined)
10695 size = addend + file_align;
10696 else
10698 size = h->size;
10699 if (addend >= size)
10701 /* Oops! We've got a reference past the defined end of
10702 the table. This is probably a bug -- shall we warn? */
10703 size = addend + file_align;
10706 size = (size + file_align - 1) & -file_align;
10708 /* Allocate one extra entry for use as a "done" flag for the
10709 consolidation pass. */
10710 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
10712 if (ptr)
10714 ptr = bfd_realloc (ptr - 1, bytes);
10716 if (ptr != NULL)
10718 size_t oldbytes;
10720 oldbytes = (((h->vtable->size >> log_file_align) + 1)
10721 * sizeof (bfd_boolean));
10722 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
10725 else
10726 ptr = bfd_zmalloc (bytes);
10728 if (ptr == NULL)
10729 return FALSE;
10731 /* And arrange for that done flag to be at index -1. */
10732 h->vtable->used = ptr + 1;
10733 h->vtable->size = size;
10736 h->vtable->used[addend >> log_file_align] = TRUE;
10738 return TRUE;
10741 struct alloc_got_off_arg {
10742 bfd_vma gotoff;
10743 unsigned int got_elt_size;
10746 /* We need a special top-level link routine to convert got reference counts
10747 to real got offsets. */
10749 static bfd_boolean
10750 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
10752 struct alloc_got_off_arg *gofarg = arg;
10754 if (h->root.type == bfd_link_hash_warning)
10755 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10757 if (h->got.refcount > 0)
10759 h->got.offset = gofarg->gotoff;
10760 gofarg->gotoff += gofarg->got_elt_size;
10762 else
10763 h->got.offset = (bfd_vma) -1;
10765 return TRUE;
10768 /* And an accompanying bit to work out final got entry offsets once
10769 we're done. Should be called from final_link. */
10771 bfd_boolean
10772 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
10773 struct bfd_link_info *info)
10775 bfd *i;
10776 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10777 bfd_vma gotoff;
10778 unsigned int got_elt_size = bed->s->arch_size / 8;
10779 struct alloc_got_off_arg gofarg;
10781 if (! is_elf_hash_table (info->hash))
10782 return FALSE;
10784 /* The GOT offset is relative to the .got section, but the GOT header is
10785 put into the .got.plt section, if the backend uses it. */
10786 if (bed->want_got_plt)
10787 gotoff = 0;
10788 else
10789 gotoff = bed->got_header_size;
10791 /* Do the local .got entries first. */
10792 for (i = info->input_bfds; i; i = i->link_next)
10794 bfd_signed_vma *local_got;
10795 bfd_size_type j, locsymcount;
10796 Elf_Internal_Shdr *symtab_hdr;
10798 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
10799 continue;
10801 local_got = elf_local_got_refcounts (i);
10802 if (!local_got)
10803 continue;
10805 symtab_hdr = &elf_tdata (i)->symtab_hdr;
10806 if (elf_bad_symtab (i))
10807 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10808 else
10809 locsymcount = symtab_hdr->sh_info;
10811 for (j = 0; j < locsymcount; ++j)
10813 if (local_got[j] > 0)
10815 local_got[j] = gotoff;
10816 gotoff += got_elt_size;
10818 else
10819 local_got[j] = (bfd_vma) -1;
10823 /* Then the global .got entries. .plt refcounts are handled by
10824 adjust_dynamic_symbol */
10825 gofarg.gotoff = gotoff;
10826 gofarg.got_elt_size = got_elt_size;
10827 elf_link_hash_traverse (elf_hash_table (info),
10828 elf_gc_allocate_got_offsets,
10829 &gofarg);
10830 return TRUE;
10833 /* Many folk need no more in the way of final link than this, once
10834 got entry reference counting is enabled. */
10836 bfd_boolean
10837 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
10839 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
10840 return FALSE;
10842 /* Invoke the regular ELF backend linker to do all the work. */
10843 return bfd_elf_final_link (abfd, info);
10846 bfd_boolean
10847 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
10849 struct elf_reloc_cookie *rcookie = cookie;
10851 if (rcookie->bad_symtab)
10852 rcookie->rel = rcookie->rels;
10854 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
10856 unsigned long r_symndx;
10858 if (! rcookie->bad_symtab)
10859 if (rcookie->rel->r_offset > offset)
10860 return FALSE;
10861 if (rcookie->rel->r_offset != offset)
10862 continue;
10864 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
10865 if (r_symndx == SHN_UNDEF)
10866 return TRUE;
10868 if (r_symndx >= rcookie->locsymcount
10869 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
10871 struct elf_link_hash_entry *h;
10873 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
10875 while (h->root.type == bfd_link_hash_indirect
10876 || h->root.type == bfd_link_hash_warning)
10877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10879 if ((h->root.type == bfd_link_hash_defined
10880 || h->root.type == bfd_link_hash_defweak)
10881 && elf_discarded_section (h->root.u.def.section))
10882 return TRUE;
10883 else
10884 return FALSE;
10886 else
10888 /* It's not a relocation against a global symbol,
10889 but it could be a relocation against a local
10890 symbol for a discarded section. */
10891 asection *isec;
10892 Elf_Internal_Sym *isym;
10894 /* Need to: get the symbol; get the section. */
10895 isym = &rcookie->locsyms[r_symndx];
10896 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
10898 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
10899 if (isec != NULL && elf_discarded_section (isec))
10900 return TRUE;
10903 return FALSE;
10905 return FALSE;
10908 /* Discard unneeded references to discarded sections.
10909 Returns TRUE if any section's size was changed. */
10910 /* This function assumes that the relocations are in sorted order,
10911 which is true for all known assemblers. */
10913 bfd_boolean
10914 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
10916 struct elf_reloc_cookie cookie;
10917 asection *stab, *eh;
10918 Elf_Internal_Shdr *symtab_hdr;
10919 const struct elf_backend_data *bed;
10920 bfd *abfd;
10921 unsigned int count;
10922 bfd_boolean ret = FALSE;
10924 if (info->traditional_format
10925 || !is_elf_hash_table (info->hash))
10926 return FALSE;
10928 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
10930 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
10931 continue;
10933 bed = get_elf_backend_data (abfd);
10935 if ((abfd->flags & DYNAMIC) != 0)
10936 continue;
10938 eh = NULL;
10939 if (!info->relocatable)
10941 eh = bfd_get_section_by_name (abfd, ".eh_frame");
10942 if (eh != NULL
10943 && (eh->size == 0
10944 || bfd_is_abs_section (eh->output_section)))
10945 eh = NULL;
10948 stab = bfd_get_section_by_name (abfd, ".stab");
10949 if (stab != NULL
10950 && (stab->size == 0
10951 || bfd_is_abs_section (stab->output_section)
10952 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
10953 stab = NULL;
10955 if (stab == NULL
10956 && eh == NULL
10957 && bed->elf_backend_discard_info == NULL)
10958 continue;
10960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10961 cookie.abfd = abfd;
10962 cookie.sym_hashes = elf_sym_hashes (abfd);
10963 cookie.bad_symtab = elf_bad_symtab (abfd);
10964 if (cookie.bad_symtab)
10966 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10967 cookie.extsymoff = 0;
10969 else
10971 cookie.locsymcount = symtab_hdr->sh_info;
10972 cookie.extsymoff = symtab_hdr->sh_info;
10975 if (bed->s->arch_size == 32)
10976 cookie.r_sym_shift = 8;
10977 else
10978 cookie.r_sym_shift = 32;
10980 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10981 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
10983 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
10984 cookie.locsymcount, 0,
10985 NULL, NULL, NULL);
10986 if (cookie.locsyms == NULL)
10987 return FALSE;
10990 if (stab != NULL)
10992 cookie.rels = NULL;
10993 count = stab->reloc_count;
10994 if (count != 0)
10995 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
10996 info->keep_memory);
10997 if (cookie.rels != NULL)
10999 cookie.rel = cookie.rels;
11000 cookie.relend = cookie.rels;
11001 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11002 if (_bfd_discard_section_stabs (abfd, stab,
11003 elf_section_data (stab)->sec_info,
11004 bfd_elf_reloc_symbol_deleted_p,
11005 &cookie))
11006 ret = TRUE;
11007 if (elf_section_data (stab)->relocs != cookie.rels)
11008 free (cookie.rels);
11012 if (eh != NULL)
11014 cookie.rels = NULL;
11015 count = eh->reloc_count;
11016 if (count != 0)
11017 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
11018 info->keep_memory);
11019 cookie.rel = cookie.rels;
11020 cookie.relend = cookie.rels;
11021 if (cookie.rels != NULL)
11022 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11024 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11025 bfd_elf_reloc_symbol_deleted_p,
11026 &cookie))
11027 ret = TRUE;
11029 if (cookie.rels != NULL
11030 && elf_section_data (eh)->relocs != cookie.rels)
11031 free (cookie.rels);
11034 if (bed->elf_backend_discard_info != NULL
11035 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11036 ret = TRUE;
11038 if (cookie.locsyms != NULL
11039 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
11041 if (! info->keep_memory)
11042 free (cookie.locsyms);
11043 else
11044 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
11048 if (info->eh_frame_hdr
11049 && !info->relocatable
11050 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11051 ret = TRUE;
11053 return ret;
11056 void
11057 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11058 struct bfd_link_info *info)
11060 flagword flags;
11061 const char *name, *p;
11062 struct bfd_section_already_linked *l;
11063 struct bfd_section_already_linked_hash_entry *already_linked_list;
11065 if (sec->output_section == bfd_abs_section_ptr)
11066 return;
11068 flags = sec->flags;
11070 /* Return if it isn't a linkonce section. A comdat group section
11071 also has SEC_LINK_ONCE set. */
11072 if ((flags & SEC_LINK_ONCE) == 0)
11073 return;
11075 /* Don't put group member sections on our list of already linked
11076 sections. They are handled as a group via their group section. */
11077 if (elf_sec_group (sec) != NULL)
11078 return;
11080 /* FIXME: When doing a relocatable link, we may have trouble
11081 copying relocations in other sections that refer to local symbols
11082 in the section being discarded. Those relocations will have to
11083 be converted somehow; as of this writing I'm not sure that any of
11084 the backends handle that correctly.
11086 It is tempting to instead not discard link once sections when
11087 doing a relocatable link (technically, they should be discarded
11088 whenever we are building constructors). However, that fails,
11089 because the linker winds up combining all the link once sections
11090 into a single large link once section, which defeats the purpose
11091 of having link once sections in the first place.
11093 Also, not merging link once sections in a relocatable link
11094 causes trouble for MIPS ELF, which relies on link once semantics
11095 to handle the .reginfo section correctly. */
11097 name = bfd_get_section_name (abfd, sec);
11099 if (CONST_STRNEQ (name, ".gnu.linkonce.")
11100 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
11101 p++;
11102 else
11103 p = name;
11105 already_linked_list = bfd_section_already_linked_table_lookup (p);
11107 for (l = already_linked_list->entry; l != NULL; l = l->next)
11109 /* We may have 2 different types of sections on the list: group
11110 sections and linkonce sections. Match like sections. */
11111 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
11112 && strcmp (name, l->sec->name) == 0
11113 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
11115 /* The section has already been linked. See if we should
11116 issue a warning. */
11117 switch (flags & SEC_LINK_DUPLICATES)
11119 default:
11120 abort ();
11122 case SEC_LINK_DUPLICATES_DISCARD:
11123 break;
11125 case SEC_LINK_DUPLICATES_ONE_ONLY:
11126 (*_bfd_error_handler)
11127 (_("%B: ignoring duplicate section `%A'"),
11128 abfd, sec);
11129 break;
11131 case SEC_LINK_DUPLICATES_SAME_SIZE:
11132 if (sec->size != l->sec->size)
11133 (*_bfd_error_handler)
11134 (_("%B: duplicate section `%A' has different size"),
11135 abfd, sec);
11136 break;
11138 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
11139 if (sec->size != l->sec->size)
11140 (*_bfd_error_handler)
11141 (_("%B: duplicate section `%A' has different size"),
11142 abfd, sec);
11143 else if (sec->size != 0)
11145 bfd_byte *sec_contents, *l_sec_contents;
11147 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
11148 (*_bfd_error_handler)
11149 (_("%B: warning: could not read contents of section `%A'"),
11150 abfd, sec);
11151 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
11152 &l_sec_contents))
11153 (*_bfd_error_handler)
11154 (_("%B: warning: could not read contents of section `%A'"),
11155 l->sec->owner, l->sec);
11156 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
11157 (*_bfd_error_handler)
11158 (_("%B: warning: duplicate section `%A' has different contents"),
11159 abfd, sec);
11161 if (sec_contents)
11162 free (sec_contents);
11163 if (l_sec_contents)
11164 free (l_sec_contents);
11166 break;
11169 /* Set the output_section field so that lang_add_section
11170 does not create a lang_input_section structure for this
11171 section. Since there might be a symbol in the section
11172 being discarded, we must retain a pointer to the section
11173 which we are really going to use. */
11174 sec->output_section = bfd_abs_section_ptr;
11175 sec->kept_section = l->sec;
11177 if (flags & SEC_GROUP)
11179 asection *first = elf_next_in_group (sec);
11180 asection *s = first;
11182 while (s != NULL)
11184 s->output_section = bfd_abs_section_ptr;
11185 /* Record which group discards it. */
11186 s->kept_section = l->sec;
11187 s = elf_next_in_group (s);
11188 /* These lists are circular. */
11189 if (s == first)
11190 break;
11194 return;
11198 /* A single member comdat group section may be discarded by a
11199 linkonce section and vice versa. */
11201 if ((flags & SEC_GROUP) != 0)
11203 asection *first = elf_next_in_group (sec);
11205 if (first != NULL && elf_next_in_group (first) == first)
11206 /* Check this single member group against linkonce sections. */
11207 for (l = already_linked_list->entry; l != NULL; l = l->next)
11208 if ((l->sec->flags & SEC_GROUP) == 0
11209 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
11210 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
11212 first->output_section = bfd_abs_section_ptr;
11213 first->kept_section = l->sec;
11214 sec->output_section = bfd_abs_section_ptr;
11215 break;
11218 else
11219 /* Check this linkonce section against single member groups. */
11220 for (l = already_linked_list->entry; l != NULL; l = l->next)
11221 if (l->sec->flags & SEC_GROUP)
11223 asection *first = elf_next_in_group (l->sec);
11225 if (first != NULL
11226 && elf_next_in_group (first) == first
11227 && bfd_elf_match_symbols_in_sections (first, sec, info))
11229 sec->output_section = bfd_abs_section_ptr;
11230 sec->kept_section = first;
11231 break;
11235 /* This is the first section with this name. Record it. */
11236 bfd_section_already_linked_table_insert (already_linked_list, sec);
11239 bfd_boolean
11240 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
11242 return sym->st_shndx == SHN_COMMON;
11245 unsigned int
11246 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
11248 return SHN_COMMON;
11251 asection *
11252 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
11254 return bfd_com_section_ptr;