Rotate gdb/ChangeLog
[binutils-gdb.git] / bfd / elflink.c
blob1dfed2fdd1508bcf33cb82c7e25ed7f289f292bd
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.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"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #ifndef CHAR_BIT
39 #define CHAR_BIT 8
40 #endif
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
45 struct elf_info_failed
47 struct bfd_link_info *info;
48 bfd_boolean failed;
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
54 struct elf_find_verdep_info
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
67 asection *
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
75 struct elf_link_hash_entry *h;
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
86 return h->root.u.def.section;
87 else
88 return NULL;
90 else
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
105 return NULL;
108 /* Define a symbol in a dynamic linkage section. */
110 struct elf_link_hash_entry *
111 _bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
128 bh = &h->root;
130 else
131 bh = NULL;
133 bed = get_elf_backend_data (abfd);
134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
135 sec, 0, NULL, FALSE, bed->collect,
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
139 BFD_ASSERT (h != NULL);
140 h->def_regular = 1;
141 h->non_elf = 0;
142 h->root.linker_def = 1;
143 h->type = STT_OBJECT;
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
148 return h;
151 bfd_boolean
152 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
154 flagword flags;
155 asection *s;
156 struct elf_link_hash_entry *h;
157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
158 struct elf_link_hash_table *htab = elf_hash_table (info);
160 /* This function may be called more than once. */
161 if (htab->sgot != NULL)
162 return TRUE;
164 flags = bed->dynamic_sec_flags;
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
171 if (s == NULL
172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
173 return FALSE;
174 htab->srelgot = s;
176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
177 if (s == NULL
178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
179 return FALSE;
180 htab->sgot = s;
182 if (bed->want_got_plt)
184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
185 if (s == NULL
186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
187 return FALSE;
188 htab->sgotplt = s;
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
194 if (bed->want_got_sym)
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info)->hgot = h;
203 if (h == NULL)
204 return FALSE;
207 return TRUE;
210 /* Create a strtab to hold the dynamic symbol names. */
211 static bfd_boolean
212 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
214 struct elf_link_hash_table *hash_table;
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
225 bfd *ibfd;
226 asection *s;
227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
228 if ((ibfd->flags
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
235 abfd = ibfd;
236 break;
239 hash_table->dynobj = abfd;
242 if (hash_table->dynstr == NULL)
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
248 return TRUE;
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
258 bfd_boolean
259 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
261 flagword flags;
262 asection *s;
263 const struct elf_backend_data *bed;
264 struct elf_link_hash_entry *h;
266 if (! is_elf_hash_table (info->hash))
267 return FALSE;
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
275 abfd = elf_hash_table (info)->dynobj;
276 bed = get_elf_backend_data (abfd);
278 flags = bed->dynamic_sec_flags;
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info) && !info->nointerp)
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
296 return FALSE;
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || !bfd_set_section_alignment (s, 1))
302 return FALSE;
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 return FALSE;
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
312 if (s == NULL
313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
314 return FALSE;
315 elf_hash_table (info)->dynsym = s;
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
319 if (s == NULL)
320 return FALSE;
322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
323 if (s == NULL
324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
325 return FALSE;
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
336 return FALSE;
338 if (info->emit_hash)
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
342 if (s == NULL
343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
352 if (s == NULL
353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
369 return FALSE;
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
373 return TRUE;
376 /* Create dynamic sections when linking against a dynamic object. */
378 bfd_boolean
379 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
381 flagword flags, pltflags;
382 struct elf_link_hash_entry *h;
383 asection *s;
384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
385 struct elf_link_hash_table *htab = elf_hash_table (info);
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags = bed->dynamic_sec_flags;
391 pltflags = flags;
392 if (bed->plt_not_loaded)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
403 if (s == NULL
404 || !bfd_set_section_alignment (s, bed->plt_alignment))
405 return FALSE;
406 htab->splt = s;
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
410 if (bed->want_plt_sym)
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
423 if (s == NULL
424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
425 return FALSE;
426 htab->srelplt = s;
428 if (! _bfd_elf_create_got_section (abfd, info))
429 return FALSE;
431 if (bed->want_dynbss)
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
440 SEC_ALLOC | SEC_LINKER_CREATED);
441 if (s == NULL)
442 return FALSE;
443 htab->sdynbss = s;
445 if (bed->want_dynrelro)
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 flags);
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
468 if (bfd_link_executable (info))
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
474 if (s == NULL
475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
476 return FALSE;
477 htab->srelbss = s;
479 if (bed->want_dynrelro)
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
487 return FALSE;
488 htab->sreldynrelro = s;
493 return TRUE;
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
504 bfd_boolean
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
508 if (h->dynindx == -1)
510 struct elf_strtab_hash *dynstr;
511 char *p;
512 const char *name;
513 size_t indx;
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
531 case STV_INTERNAL:
532 case STV_HIDDEN:
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
536 h->forced_local = 1;
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
541 default:
542 break;
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
551 /* Create a strtab to hold the dynamic symbol names. */
552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
553 if (dynstr == NULL)
554 return FALSE;
557 /* We don't put any version information in the dynamic string
558 table. */
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
571 if (p != NULL)
572 *p = ELF_VER_CHR;
574 if (indx == (size_t) -1)
575 return FALSE;
576 h->dynstr_index = indx;
579 return TRUE;
582 /* Mark a symbol dynamic. */
584 static void
585 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
591 /* It may be called more than once on the same H. */
592 if(h->dynamic || bfd_link_relocatable (info))
593 return;
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
597 || h->type == STT_COMMON
598 || (sym != NULL
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
601 || (d != NULL
602 && h->non_elf
603 && (*d->match) (&d->head, NULL, h->root.root.string)))
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
612 /* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
615 bfd_boolean
616 bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
618 const char *name,
619 bfd_boolean provide,
620 bfd_boolean hidden)
622 struct elf_link_hash_entry *h, *hv;
623 struct elf_link_hash_table *htab;
624 const struct elf_backend_data *bed;
626 if (!is_elf_hash_table (info->hash))
627 return TRUE;
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
631 if (h == NULL)
632 return provide;
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
637 if (h->versioned == unknown)
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
658 switch (h->root.type)
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
669 h->root.type = bfd_link_hash_new;
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
672 break;
673 case bfd_link_hash_new:
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
677 the versioned symbol point to this one. */
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
690 default:
691 BFD_FAIL ();
692 return FALSE;
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
700 && h->def_dynamic
701 && !h->def_regular)
702 h->root.type = bfd_link_hash_undefined;
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
709 h->verinfo.verdef = NULL;
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
714 h->def_regular = 1;
716 if (hidden)
718 bed = get_elf_backend_data (output_bfd);
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
726 if (!bfd_link_relocatable (info)
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
732 if ((h->def_dynamic
733 || h->ref_dynamic
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
736 && !h->forced_local
737 && h->dynindx == -1)
739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
740 return FALSE;
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
745 if (h->is_weakalias)
747 struct elf_link_hash_entry *def = weakdef (h);
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
751 return FALSE;
755 return TRUE;
758 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
763 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
767 size_t amt;
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
771 size_t dynstr_index;
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
776 if (! is_elf_hash_table (info->hash))
777 return 0;
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
784 amt = sizeof (*entry);
785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
786 if (entry == NULL)
787 return 0;
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
791 1, input_indx, &entry->isym, esym, &eshndx))
793 bfd_release (input_bfd, entry);
794 return 0;
797 if (entry->isym.st_shndx != SHN_UNDEF
798 && entry->isym.st_shndx < SHN_LORESERVE)
800 asection *s;
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
826 if (dynstr_index == (size_t) -1)
827 return 0;
828 entry->isym.st_name = dynstr_index;
830 eht = elf_hash_table (info);
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
842 /* The dynindx will be set at the end of size_dynamic_sections. */
844 return 1;
847 /* Return the dynindex of a local dynamic symbol. */
849 long
850 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
854 struct elf_link_local_dynamic_entry *e;
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
862 /* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
866 static bfd_boolean
867 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
870 size_t *count = (size_t *) data;
872 if (h->forced_local)
873 return TRUE;
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
878 return TRUE;
882 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
885 static bfd_boolean
886 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
889 size_t *count = (size_t *) data;
891 if (!h->forced_local)
892 return TRUE;
894 if (h->dynindx != -1)
895 h->dynindx = ++(*count);
897 return TRUE;
900 /* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902 bfd_boolean
903 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
907 struct elf_link_hash_table *htab;
908 asection *ip;
910 switch (elf_section_data (p)->this_hdr.sh_type)
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
917 htab = elf_hash_table (info);
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
921 return (htab->dynobj != NULL
922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
923 && ip->output_section == p);
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
932 bfd_boolean
933 _bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
938 return TRUE;
941 /* Assign dynsym indices. In a shared library we generate a section
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
950 static unsigned long
951 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
955 unsigned long dynsymcount = 0;
956 bfd_boolean do_sec = section_sym_count != NULL;
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
964 if ((p->flags & SEC_EXCLUDE) == 0
965 && (p->flags & SEC_ALLOC) != 0
966 && elf_hash_table (info)->dynamic_relocs
967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
973 else if (do_sec)
974 elf_section_data (p)->dynindx = 0;
976 if (do_sec)
977 *section_sym_count = dynsymcount;
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
983 if (elf_hash_table (info)->dynlocal)
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
1005 /* Merge st_other field. */
1007 static void
1008 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1009 unsigned int st_other, asection *sec,
1010 bfd_boolean definition, bfd_boolean dynamic)
1012 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1014 /* If st_other has a processor-specific meaning, specific
1015 code might be needed here. */
1016 if (bed->elf_backend_merge_symbol_attribute)
1017 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1018 dynamic);
1020 if (!dynamic)
1022 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1023 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis - 1 < hvis - 1)
1028 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1030 else if (definition
1031 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1032 && (sec->flags & SEC_READONLY) == 0)
1033 h->protected_def = 1;
1036 /* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
1049 static bfd_boolean
1050 _bfd_elf_merge_symbol (bfd *abfd,
1051 struct bfd_link_info *info,
1052 const char *name,
1053 Elf_Internal_Sym *sym,
1054 asection **psec,
1055 bfd_vma *pvalue,
1056 struct elf_link_hash_entry **sym_hash,
1057 bfd **poldbfd,
1058 bfd_boolean *pold_weak,
1059 unsigned int *pold_alignment,
1060 bfd_boolean *skip,
1061 bfd **override,
1062 bfd_boolean *type_change_ok,
1063 bfd_boolean *size_change_ok,
1064 bfd_boolean *matched)
1066 asection *sec, *oldsec;
1067 struct elf_link_hash_entry *h;
1068 struct elf_link_hash_entry *hi;
1069 struct elf_link_hash_entry *flip;
1070 int bind;
1071 bfd *oldbfd;
1072 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1073 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1074 const struct elf_backend_data *bed;
1075 char *new_version;
1076 bfd_boolean default_sym = *matched;
1078 *skip = FALSE;
1079 *override = NULL;
1081 sec = *psec;
1082 bind = ELF_ST_BIND (sym->st_info);
1084 if (! bfd_is_und_section (sec))
1085 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1086 else
1087 h = ((struct elf_link_hash_entry *)
1088 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1089 if (h == NULL)
1090 return FALSE;
1091 *sym_hash = h;
1093 bed = get_elf_backend_data (abfd);
1095 /* NEW_VERSION is the symbol version of the new symbol. */
1096 if (h->versioned != unversioned)
1098 /* Symbol version is unknown or versioned. */
1099 new_version = strrchr (name, ELF_VER_CHR);
1100 if (new_version)
1102 if (h->versioned == unknown)
1104 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1105 h->versioned = versioned_hidden;
1106 else
1107 h->versioned = versioned;
1109 new_version += 1;
1110 if (new_version[0] == '\0')
1111 new_version = NULL;
1113 else
1114 h->versioned = unversioned;
1116 else
1117 new_version = NULL;
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1121 hi = h;
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1126 if (!*matched)
1128 if (hi == h || h->root.type == bfd_link_hash_new)
1129 *matched = TRUE;
1130 else
1132 /* OLD_HIDDEN is true if the existing symbol is only visible
1133 to the symbol with the same symbol version. NEW_HIDDEN is
1134 true if the new symbol is only visible to the symbol with
1135 the same symbol version. */
1136 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1137 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1138 if (!old_hidden && !new_hidden)
1139 /* The new symbol matches the existing symbol if both
1140 aren't hidden. */
1141 *matched = TRUE;
1142 else
1144 /* OLD_VERSION is the symbol version of the existing
1145 symbol. */
1146 char *old_version;
1148 if (h->versioned >= versioned)
1149 old_version = strrchr (h->root.root.string,
1150 ELF_VER_CHR) + 1;
1151 else
1152 old_version = NULL;
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched = (old_version == new_version
1157 || (old_version != NULL
1158 && new_version != NULL
1159 && strcmp (old_version, new_version) == 0));
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1165 existing symbol. */
1167 oldbfd = NULL;
1168 oldsec = NULL;
1169 switch (h->root.type)
1171 default:
1172 break;
1174 case bfd_link_hash_undefined:
1175 case bfd_link_hash_undefweak:
1176 oldbfd = h->root.u.undef.abfd;
1177 break;
1179 case bfd_link_hash_defined:
1180 case bfd_link_hash_defweak:
1181 oldbfd = h->root.u.def.section->owner;
1182 oldsec = h->root.u.def.section;
1183 break;
1185 case bfd_link_hash_common:
1186 oldbfd = h->root.u.c.p->section->owner;
1187 oldsec = h->root.u.c.p->section;
1188 if (pold_alignment)
1189 *pold_alignment = h->root.u.c.p->alignment_power;
1190 break;
1192 if (poldbfd && *poldbfd == NULL)
1193 *poldbfd = oldbfd;
1195 /* Differentiate strong and weak symbols. */
1196 newweak = bind == STB_WEAK;
1197 oldweak = (h->root.type == bfd_link_hash_defweak
1198 || h->root.type == bfd_link_hash_undefweak);
1199 if (pold_weak)
1200 *pold_weak = oldweak;
1202 /* We have to check it for every instance since the first few may be
1203 references and not all compilers emit symbol type for undefined
1204 symbols. */
1205 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1210 newdyn = (abfd->flags & DYNAMIC) != 0;
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1219 if (newdyn)
1221 if (bfd_is_und_section (sec))
1223 if (bind != STB_WEAK)
1225 h->ref_dynamic_nonweak = 1;
1226 hi->ref_dynamic_nonweak = 1;
1229 else
1231 /* Update the existing symbol only if they match. */
1232 if (*matched)
1233 h->dynamic_def = 1;
1234 hi->dynamic_def = 1;
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1242 if (h->root.type == bfd_link_hash_new)
1244 h->non_elf = 0;
1245 return TRUE;
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1254 if (abfd == oldbfd
1255 && (newweak || oldweak)
1256 && ((abfd->flags & DYNAMIC) == 0
1257 || !h->def_regular))
1258 return TRUE;
1260 olddyn = FALSE;
1261 if (oldbfd != NULL)
1262 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1263 else if (oldsec != NULL)
1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1266 indices used by MIPS ELF. */
1267 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1272 if (oldbfd != NULL
1273 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1274 && newdyn != olddyn)
1276 h->root.non_ir_ref_dynamic = TRUE;
1277 hi->root.non_ir_ref_dynamic = TRUE;
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1283 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1285 olddef = (h->root.type != bfd_link_hash_undefined
1286 && h->root.type != bfd_link_hash_undefweak
1287 && h->root.type != bfd_link_hash_common);
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1292 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1293 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1295 oldfunc = (h->type != STT_NOTYPE
1296 && bed->is_function_type (h->type));
1298 if (!(newfunc && oldfunc)
1299 && ELF_ST_TYPE (sym->st_info) != h->type
1300 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1301 && h->type != STT_NOTYPE
1302 && (newdef || bfd_is_com_section (sec))
1303 && (olddef || h->root.type == bfd_link_hash_common))
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1310 if (newdyn
1311 && !olddyn)
1313 *skip = TRUE;
1314 return TRUE;
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1319 dynamic state. */
1320 if (hi != h
1321 && !newdyn
1322 && olddyn)
1324 h = hi;
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 h->def_dynamic = 0;
1329 h->dynamic_def = 0;
1330 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1332 h->root.type = bfd_link_hash_undefined;
1333 h->root.u.undef.abfd = abfd;
1335 else
1337 h->root.type = bfd_link_hash_new;
1338 h->root.u.undef.abfd = NULL;
1340 return TRUE;
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1347 if (oldbfd != NULL
1348 && (oldbfd->flags & BFD_PLUGIN) == 0
1349 && (abfd->flags & BFD_PLUGIN) == 0
1350 && ELF_ST_TYPE (sym->st_info) != h->type
1351 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1353 bfd *ntbfd, *tbfd;
1354 bfd_boolean ntdef, tdef;
1355 asection *ntsec, *tsec;
1357 if (h->type == STT_TLS)
1359 ntbfd = abfd;
1360 ntsec = sec;
1361 ntdef = newdef;
1362 tbfd = oldbfd;
1363 tsec = oldsec;
1364 tdef = olddef;
1366 else
1368 ntbfd = oldbfd;
1369 ntsec = oldsec;
1370 ntdef = olddef;
1371 tbfd = abfd;
1372 tsec = sec;
1373 tdef = newdef;
1376 if (tdef && ntdef)
1377 _bfd_error_handler
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
1381 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1382 else if (!tdef && !ntdef)
1383 _bfd_error_handler
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
1387 h->root.root.string, tbfd, ntbfd);
1388 else if (tdef)
1389 _bfd_error_handler
1390 /* xgettext:c-format */
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
1393 h->root.root.string, tbfd, tsec, ntbfd);
1394 else
1395 _bfd_error_handler
1396 /* xgettext:c-format */
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h->root.root.string, tbfd, ntbfd, ntsec);
1401 bfd_set_error (bfd_error_bad_value);
1402 return FALSE;
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1407 if (newdyn
1408 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1409 && !bfd_is_und_section (sec))
1411 *skip = TRUE;
1412 /* Make sure this symbol is dynamic. */
1413 h->ref_dynamic = 1;
1414 hi->ref_dynamic = 1;
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1420 return bfd_elf_link_record_dynamic_symbol (info, h);
1421 else
1422 return TRUE;
1424 else if (!newdyn
1425 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1426 && h->def_dynamic)
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
1431 if (hi->root.type == bfd_link_hash_indirect)
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1437 if (h->ref_regular)
1439 hi->root.type = h->root.type;
1440 h->root.type = bfd_link_hash_indirect;
1441 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1443 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1444 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 h->forced_local = 0;
1450 h->ref_dynamic = 0;
1452 else
1453 h->ref_dynamic = 1;
1455 h->def_dynamic = 0;
1456 /* FIXME: Should we check type and size for protected symbol? */
1457 h->size = 0;
1458 h->type = 0;
1460 h = hi;
1462 else
1463 h = hi;
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1475 h->root.type = bfd_link_hash_undefined;
1476 h->root.u.undef.abfd = abfd;
1478 else
1480 h->root.type = bfd_link_hash_new;
1481 h->root.u.undef.abfd = NULL;
1484 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1489 h->forced_local = 0;
1490 h->ref_dynamic = 0;
1492 else
1493 h->ref_dynamic = 1;
1494 h->def_dynamic = 0;
1495 /* FIXME: Should we check type and size for protected symbol? */
1496 h->size = 0;
1497 h->type = 0;
1498 return TRUE;
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1517 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1518 newweak = FALSE;
1519 if (olddef && newdyn)
1520 oldweak = FALSE;
1522 /* Allow changes between different types of function symbol. */
1523 if (newfunc && oldfunc)
1524 *type_change_ok = TRUE;
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
1530 if (oldweak
1531 || newweak
1532 || (newdef
1533 && h->root.type == bfd_link_hash_undefined))
1534 *type_change_ok = TRUE;
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1539 if (*type_change_ok
1540 || h->root.type == bfd_link_hash_undefined)
1541 *size_change_ok = TRUE;
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1554 libraries.
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1564 harmless. */
1566 if (newdyn
1567 && newdef
1568 && !newweak
1569 && (sec->flags & SEC_ALLOC) != 0
1570 && (sec->flags & SEC_LOAD) == 0
1571 && sym->st_size > 0
1572 && !newfunc)
1573 newdyncommon = TRUE;
1574 else
1575 newdyncommon = FALSE;
1577 if (olddyn
1578 && olddef
1579 && h->root.type == bfd_link_hash_defined
1580 && h->def_dynamic
1581 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1582 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1583 && h->size > 0
1584 && !oldfunc)
1585 olddyncommon = TRUE;
1586 else
1587 olddyncommon = FALSE;
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
1591 if (bed->merge_symbol != NULL)
1593 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1594 return FALSE;
1595 sec = *psec;
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
1600 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1601 && !default_sym && h->def_regular
1602 && !(oldbfd != NULL
1603 && (oldbfd->flags & BFD_PLUGIN) != 0
1604 && (abfd->flags & BFD_PLUGIN) == 0))
1606 /* Handle a multiple definition. */
1607 (*info->callbacks->multiple_definition) (info, &h->root,
1608 abfd, sec, *pvalue);
1609 *skip = TRUE;
1610 return TRUE;
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1615 two. */
1617 if (olddyncommon
1618 && newdyncommon
1619 && sym->st_size != h->size)
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1627 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1628 bfd_link_hash_common, sym->st_size);
1629 if (sym->st_size > h->size)
1630 h->size = sym->st_size;
1632 *size_change_ok = TRUE;
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
1647 object to override a weak symbol in a shared object. */
1649 if (newdyn
1650 && newdef
1651 && (olddef
1652 || (h->root.type == bfd_link_hash_common
1653 && (newweak || newfunc))))
1655 *override = abfd;
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
1659 *psec = sec = bfd_und_section_ptr;
1660 *size_change_ok = TRUE;
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
1668 if (h->root.type == bfd_link_hash_common)
1669 *type_change_ok = TRUE;
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1676 right thing. */
1678 if (newdyncommon
1679 && h->root.type == bfd_link_hash_common)
1681 *override = oldbfd;
1682 newdef = FALSE;
1683 newdyncommon = FALSE;
1684 *pvalue = sym->st_size;
1685 *psec = sec = bed->common_section (oldsec);
1686 *size_change_ok = TRUE;
1689 /* Skip weak definitions of symbols that are already defined. */
1690 if (newdef && olddef && newweak)
1692 /* Don't skip new non-IR weak syms. */
1693 if (!(oldbfd != NULL
1694 && (oldbfd->flags & BFD_PLUGIN) != 0
1695 && (abfd->flags & BFD_PLUGIN) == 0))
1697 newdef = FALSE;
1698 *skip = TRUE;
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1703 local symbol. */
1704 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1705 if (h->dynindx != -1)
1706 switch (ELF_ST_VISIBILITY (h->other))
1708 case STV_INTERNAL:
1709 case STV_HIDDEN:
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1711 break;
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
1723 symbol is a function or is weak. */
1725 flip = NULL;
1726 if (!newdyn
1727 && (newdef
1728 || (bfd_is_com_section (sec)
1729 && (oldweak || oldfunc)))
1730 && olddyn
1731 && olddef
1732 && h->def_dynamic)
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1736 new definition. */
1738 h->root.type = bfd_link_hash_undefined;
1739 h->root.u.undef.abfd = h->root.u.def.section->owner;
1740 *size_change_ok = TRUE;
1742 olddef = FALSE;
1743 olddyncommon = FALSE;
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1748 if (bfd_is_com_section (sec))
1750 if (oldfunc)
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1754 function. */
1755 h->def_dynamic = 0;
1756 h->type = STT_NOTYPE;
1758 *type_change_ok = TRUE;
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
1763 else
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h->verinfo.vertree = NULL;
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1776 if (! newdyn
1777 && bfd_is_com_section (sec)
1778 && olddyncommon)
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1783 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1784 bfd_link_hash_common, sym->st_size);
1786 /* If the presumed common symbol in the dynamic object is
1787 larger, pretend that the new symbol has its size. */
1789 if (h->size > *pvalue)
1790 *pvalue = h->size;
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment);
1795 *pold_alignment = h->root.u.def.section->alignment_power;
1797 olddef = FALSE;
1798 olddyncommon = FALSE;
1800 h->root.type = bfd_link_hash_undefined;
1801 h->root.u.undef.abfd = h->root.u.def.section->owner;
1803 *size_change_ok = TRUE;
1804 *type_change_ok = TRUE;
1806 if (hi->root.type == bfd_link_hash_indirect)
1807 flip = hi;
1808 else
1809 h->verinfo.vertree = NULL;
1812 if (flip != NULL)
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
1817 flip->root.type = h->root.type;
1818 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1819 h->root.type = bfd_link_hash_indirect;
1820 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1821 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1822 if (h->def_dynamic)
1824 h->def_dynamic = 0;
1825 flip->ref_dynamic = 1;
1829 return TRUE;
1832 /* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1835 set DYNSYM if the new indirect symbol is dynamic. */
1837 static bfd_boolean
1838 _bfd_elf_add_default_symbol (bfd *abfd,
1839 struct bfd_link_info *info,
1840 struct elf_link_hash_entry *h,
1841 const char *name,
1842 Elf_Internal_Sym *sym,
1843 asection *sec,
1844 bfd_vma value,
1845 bfd **poldbfd,
1846 bfd_boolean *dynsym)
1848 bfd_boolean type_change_ok;
1849 bfd_boolean size_change_ok;
1850 bfd_boolean skip;
1851 char *shortname;
1852 struct elf_link_hash_entry *hi;
1853 struct bfd_link_hash_entry *bh;
1854 const struct elf_backend_data *bed;
1855 bfd_boolean collect;
1856 bfd_boolean dynamic;
1857 bfd *override;
1858 char *p;
1859 size_t len, shortlen;
1860 asection *tmp_sec;
1861 bfd_boolean matched;
1863 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1864 return TRUE;
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p = strchr (name, ELF_VER_CHR);
1871 if (h->versioned == unknown)
1873 if (p == NULL)
1875 h->versioned = unversioned;
1876 return TRUE;
1878 else
1880 if (p[1] != ELF_VER_CHR)
1882 h->versioned = versioned_hidden;
1883 return TRUE;
1885 else
1886 h->versioned = versioned;
1889 else
1891 /* PR ld/19073: We may see an unversioned definition after the
1892 default version. */
1893 if (p == NULL)
1894 return TRUE;
1897 bed = get_elf_backend_data (abfd);
1898 collect = bed->collect;
1899 dynamic = (abfd->flags & DYNAMIC) != 0;
1901 shortlen = p - name;
1902 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1903 if (shortname == NULL)
1904 return FALSE;
1905 memcpy (shortname, name, shortlen);
1906 shortname[shortlen] = '\0';
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok = FALSE;
1913 size_change_ok = FALSE;
1914 matched = TRUE;
1915 tmp_sec = sec;
1916 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1917 &hi, poldbfd, NULL, NULL, &skip, &override,
1918 &type_change_ok, &size_change_ok, &matched))
1919 return FALSE;
1921 if (skip)
1922 goto nondefault;
1924 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1933 bfd_boolean hide;
1935 hi->verinfo.vertree
1936 = bfd_find_version_for_sym (info->version_info,
1937 hi->root.root.string, &hide);
1938 if (hi->verinfo.vertree != NULL && hide)
1940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1941 goto nondefault;
1944 if (hi->verinfo.vertree != NULL
1945 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1946 goto nondefault;
1949 if (! override)
1951 /* Add the default symbol if not performing a relocatable link. */
1952 if (! bfd_link_relocatable (info))
1954 bh = &hi->root;
1955 if (bh->type == bfd_link_hash_defined
1956 && bh->u.def.section->owner != NULL
1957 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1961 it. */
1962 bh->type = bfd_link_hash_undefined;
1963 bh->u.undef.abfd = bh->u.def.section->owner;
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info, abfd, shortname, BSF_INDIRECT,
1967 bfd_ind_section_ptr,
1968 0, name, FALSE, collect, &bh)))
1969 return FALSE;
1970 hi = (struct elf_link_hash_entry *) bh;
1973 else
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1993 while (hi->root.type == bfd_link_hash_indirect
1994 || hi->root.type == bfd_link_hash_warning)
1995 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1997 h->root.type = bfd_link_hash_indirect;
1998 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1999 if (h->def_dynamic)
2001 h->def_dynamic = 0;
2002 hi->ref_dynamic = 1;
2003 if (hi->ref_regular
2004 || hi->def_regular)
2006 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2007 return FALSE;
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2013 hi = h;
2016 /* Check if HI is a warning symbol. */
2017 if (hi->root.type == bfd_link_hash_warning)
2018 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2024 if (hi->root.type == bfd_link_hash_indirect)
2026 struct elf_link_hash_entry *ht;
2028 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2029 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2031 /* If we first saw a reference to SHORTNAME with non-default
2032 visibility, merge that visibility to the @@VER symbol. */
2033 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2035 /* A reference to the SHORTNAME symbol from a dynamic library
2036 will be satisfied by the versioned symbol at runtime. In
2037 effect, we have a reference to the versioned symbol. */
2038 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2039 hi->dynamic_def |= ht->dynamic_def;
2041 /* See if the new flags lead us to realize that the symbol must
2042 be dynamic. */
2043 if (! *dynsym)
2045 if (! dynamic)
2047 if (! bfd_link_executable (info)
2048 || hi->def_dynamic
2049 || hi->ref_dynamic)
2050 *dynsym = TRUE;
2052 else
2054 if (hi->ref_regular)
2055 *dynsym = TRUE;
2060 /* We also need to define an indirection from the nondefault version
2061 of the symbol. */
2063 nondefault:
2064 len = strlen (name);
2065 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2066 if (shortname == NULL)
2067 return FALSE;
2068 memcpy (shortname, name, shortlen);
2069 memcpy (shortname + shortlen, p + 1, len - shortlen);
2071 /* Once again, merge with any existing symbol. */
2072 type_change_ok = FALSE;
2073 size_change_ok = FALSE;
2074 tmp_sec = sec;
2075 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2076 &hi, poldbfd, NULL, NULL, &skip, &override,
2077 &type_change_ok, &size_change_ok, &matched))
2078 return FALSE;
2080 if (skip)
2082 if (!dynamic
2083 && h->root.type == bfd_link_hash_defweak
2084 && hi->root.type == bfd_link_hash_defined)
2086 /* We are handling a weak sym@@ver and attempting to define
2087 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2088 new weak sym@ver because there is already a strong sym@ver.
2089 However, sym@ver and sym@@ver are really the same symbol.
2090 The existing strong sym@ver ought to override sym@@ver. */
2091 h->root.type = bfd_link_hash_defined;
2092 h->root.u.def.section = hi->root.u.def.section;
2093 h->root.u.def.value = hi->root.u.def.value;
2094 hi->root.type = bfd_link_hash_indirect;
2095 hi->root.u.i.link = &h->root;
2097 else
2098 return TRUE;
2100 else if (override)
2102 /* Here SHORTNAME is a versioned name, so we don't expect to see
2103 the type of override we do in the case above unless it is
2104 overridden by a versioned definition. */
2105 if (hi->root.type != bfd_link_hash_defined
2106 && hi->root.type != bfd_link_hash_defweak)
2107 _bfd_error_handler
2108 /* xgettext:c-format */
2109 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2110 abfd, shortname);
2111 return TRUE;
2113 else
2115 bh = &hi->root;
2116 if (! (_bfd_generic_link_add_one_symbol
2117 (info, abfd, shortname, BSF_INDIRECT,
2118 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2119 return FALSE;
2120 hi = (struct elf_link_hash_entry *) bh;
2123 /* If there is a duplicate definition somewhere, then HI may not
2124 point to an indirect symbol. We will have reported an error
2125 to the user in that case. */
2126 if (hi->root.type == bfd_link_hash_indirect)
2128 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2129 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2130 hi->dynamic_def |= h->dynamic_def;
2132 /* If we first saw a reference to @VER symbol with
2133 non-default visibility, merge that visibility to the
2134 @@VER symbol. */
2135 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2137 /* See if the new flags lead us to realize that the symbol
2138 must be dynamic. */
2139 if (! *dynsym)
2141 if (! dynamic)
2143 if (! bfd_link_executable (info)
2144 || hi->ref_dynamic)
2145 *dynsym = TRUE;
2147 else
2149 if (hi->ref_regular)
2150 *dynsym = TRUE;
2155 return TRUE;
2158 /* This routine is used to export all defined symbols into the dynamic
2159 symbol table. It is called via elf_link_hash_traverse. */
2161 static bfd_boolean
2162 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2164 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2166 /* Ignore indirect symbols. These are added by the versioning code. */
2167 if (h->root.type == bfd_link_hash_indirect)
2168 return TRUE;
2170 /* Ignore this if we won't export it. */
2171 if (!eif->info->export_dynamic && !h->dynamic)
2172 return TRUE;
2174 if (h->dynindx == -1
2175 && (h->def_regular || h->ref_regular)
2176 && ! bfd_hide_sym_by_version (eif->info->version_info,
2177 h->root.root.string))
2179 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2181 eif->failed = TRUE;
2182 return FALSE;
2186 return TRUE;
2189 /* Look through the symbols which are defined in other shared
2190 libraries and referenced here. Update the list of version
2191 dependencies. This will be put into the .gnu.version_r section.
2192 This function is called via elf_link_hash_traverse. */
2194 static bfd_boolean
2195 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2196 void *data)
2198 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2199 Elf_Internal_Verneed *t;
2200 Elf_Internal_Vernaux *a;
2201 size_t amt;
2203 /* We only care about symbols defined in shared objects with version
2204 information. */
2205 if (!h->def_dynamic
2206 || h->def_regular
2207 || h->dynindx == -1
2208 || h->verinfo.verdef == NULL
2209 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2210 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2211 return TRUE;
2213 /* See if we already know about this version. */
2214 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2215 t != NULL;
2216 t = t->vn_nextref)
2218 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2219 continue;
2221 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2222 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2223 return TRUE;
2225 break;
2228 /* This is a new version. Add it to tree we are building. */
2230 if (t == NULL)
2232 amt = sizeof *t;
2233 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2234 if (t == NULL)
2236 rinfo->failed = TRUE;
2237 return FALSE;
2240 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2241 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2242 elf_tdata (rinfo->info->output_bfd)->verref = t;
2245 amt = sizeof *a;
2246 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2247 if (a == NULL)
2249 rinfo->failed = TRUE;
2250 return FALSE;
2253 /* Note that we are copying a string pointer here, and testing it
2254 above. If bfd_elf_string_from_elf_section is ever changed to
2255 discard the string data when low in memory, this will have to be
2256 fixed. */
2257 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2259 a->vna_flags = h->verinfo.verdef->vd_flags;
2260 a->vna_nextptr = t->vn_auxptr;
2262 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2263 ++rinfo->vers;
2265 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2267 t->vn_auxptr = a;
2269 return TRUE;
2272 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2273 hidden. Set *T_P to NULL if there is no match. */
2275 static bfd_boolean
2276 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2277 struct elf_link_hash_entry *h,
2278 const char *version_p,
2279 struct bfd_elf_version_tree **t_p,
2280 bfd_boolean *hide)
2282 struct bfd_elf_version_tree *t;
2284 /* Look for the version. If we find it, it is no longer weak. */
2285 for (t = info->version_info; t != NULL; t = t->next)
2287 if (strcmp (t->name, version_p) == 0)
2289 size_t len;
2290 char *alc;
2291 struct bfd_elf_version_expr *d;
2293 len = version_p - h->root.root.string;
2294 alc = (char *) bfd_malloc (len);
2295 if (alc == NULL)
2296 return FALSE;
2297 memcpy (alc, h->root.root.string, len - 1);
2298 alc[len - 1] = '\0';
2299 if (alc[len - 2] == ELF_VER_CHR)
2300 alc[len - 2] = '\0';
2302 h->verinfo.vertree = t;
2303 t->used = TRUE;
2304 d = NULL;
2306 if (t->globals.list != NULL)
2307 d = (*t->match) (&t->globals, NULL, alc);
2309 /* See if there is anything to force this symbol to
2310 local scope. */
2311 if (d == NULL && t->locals.list != NULL)
2313 d = (*t->match) (&t->locals, NULL, alc);
2314 if (d != NULL
2315 && h->dynindx != -1
2316 && ! info->export_dynamic)
2317 *hide = TRUE;
2320 free (alc);
2321 break;
2325 *t_p = t;
2327 return TRUE;
2330 /* Return TRUE if the symbol H is hidden by version script. */
2332 bfd_boolean
2333 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2334 struct elf_link_hash_entry *h)
2336 const char *p;
2337 bfd_boolean hide = FALSE;
2338 const struct elf_backend_data *bed
2339 = get_elf_backend_data (info->output_bfd);
2341 /* Version script only hides symbols defined in regular objects. */
2342 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2343 return TRUE;
2345 p = strchr (h->root.root.string, ELF_VER_CHR);
2346 if (p != NULL && h->verinfo.vertree == NULL)
2348 struct bfd_elf_version_tree *t;
2350 ++p;
2351 if (*p == ELF_VER_CHR)
2352 ++p;
2354 if (*p != '\0'
2355 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2356 && hide)
2358 if (hide)
2359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2360 return TRUE;
2364 /* If we don't have a version for this symbol, see if we can find
2365 something. */
2366 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2374 return TRUE;
2378 return FALSE;
2381 /* Figure out appropriate versions for all the symbols. We may not
2382 have the version number script until we have read all of the input
2383 files, so until that point we don't know which symbols should be
2384 local. This function is called via elf_link_hash_traverse. */
2386 static bfd_boolean
2387 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2389 struct elf_info_failed *sinfo;
2390 struct bfd_link_info *info;
2391 const struct elf_backend_data *bed;
2392 struct elf_info_failed eif;
2393 char *p;
2394 bfd_boolean hide;
2396 sinfo = (struct elf_info_failed *) data;
2397 info = sinfo->info;
2399 /* Fix the symbol flags. */
2400 eif.failed = FALSE;
2401 eif.info = info;
2402 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2404 if (eif.failed)
2405 sinfo->failed = TRUE;
2406 return FALSE;
2409 bed = get_elf_backend_data (info->output_bfd);
2411 /* We only need version numbers for symbols defined in regular
2412 objects. */
2413 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2415 /* Hide symbols defined in discarded input sections. */
2416 if ((h->root.type == bfd_link_hash_defined
2417 || h->root.type == bfd_link_hash_defweak)
2418 && discarded_section (h->root.u.def.section))
2419 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2420 return TRUE;
2423 hide = FALSE;
2424 p = strchr (h->root.root.string, ELF_VER_CHR);
2425 if (p != NULL && h->verinfo.vertree == NULL)
2427 struct bfd_elf_version_tree *t;
2429 ++p;
2430 if (*p == ELF_VER_CHR)
2431 ++p;
2433 /* If there is no version string, we can just return out. */
2434 if (*p == '\0')
2435 return TRUE;
2437 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2439 sinfo->failed = TRUE;
2440 return FALSE;
2443 if (hide)
2444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2446 /* If we are building an application, we need to create a
2447 version node for this version. */
2448 if (t == NULL && bfd_link_executable (info))
2450 struct bfd_elf_version_tree **pp;
2451 int version_index;
2453 /* If we aren't going to export this symbol, we don't need
2454 to worry about it. */
2455 if (h->dynindx == -1)
2456 return TRUE;
2458 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2459 sizeof *t);
2460 if (t == NULL)
2462 sinfo->failed = TRUE;
2463 return FALSE;
2466 t->name = p;
2467 t->name_indx = (unsigned int) -1;
2468 t->used = TRUE;
2470 version_index = 1;
2471 /* Don't count anonymous version tag. */
2472 if (sinfo->info->version_info != NULL
2473 && sinfo->info->version_info->vernum == 0)
2474 version_index = 0;
2475 for (pp = &sinfo->info->version_info;
2476 *pp != NULL;
2477 pp = &(*pp)->next)
2478 ++version_index;
2479 t->vernum = version_index;
2481 *pp = t;
2483 h->verinfo.vertree = t;
2485 else if (t == NULL)
2487 /* We could not find the version for a symbol when
2488 generating a shared archive. Return an error. */
2489 _bfd_error_handler
2490 /* xgettext:c-format */
2491 (_("%pB: version node not found for symbol %s"),
2492 info->output_bfd, h->root.root.string);
2493 bfd_set_error (bfd_error_bad_value);
2494 sinfo->failed = TRUE;
2495 return FALSE;
2499 /* If we don't have a version for this symbol, see if we can find
2500 something. */
2501 if (!hide
2502 && h->verinfo.vertree == NULL
2503 && sinfo->info->version_info != NULL)
2505 h->verinfo.vertree
2506 = bfd_find_version_for_sym (sinfo->info->version_info,
2507 h->root.root.string, &hide);
2508 if (h->verinfo.vertree != NULL && hide)
2509 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2512 return TRUE;
2515 /* Read and swap the relocs from the section indicated by SHDR. This
2516 may be either a REL or a RELA section. The relocations are
2517 translated into RELA relocations and stored in INTERNAL_RELOCS,
2518 which should have already been allocated to contain enough space.
2519 The EXTERNAL_RELOCS are a buffer where the external form of the
2520 relocations should be stored.
2522 Returns FALSE if something goes wrong. */
2524 static bfd_boolean
2525 elf_link_read_relocs_from_section (bfd *abfd,
2526 asection *sec,
2527 Elf_Internal_Shdr *shdr,
2528 void *external_relocs,
2529 Elf_Internal_Rela *internal_relocs)
2531 const struct elf_backend_data *bed;
2532 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2533 const bfd_byte *erela;
2534 const bfd_byte *erelaend;
2535 Elf_Internal_Rela *irela;
2536 Elf_Internal_Shdr *symtab_hdr;
2537 size_t nsyms;
2539 /* Position ourselves at the start of the section. */
2540 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2541 return FALSE;
2543 /* Read the relocations. */
2544 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2545 return FALSE;
2547 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2548 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2550 bed = get_elf_backend_data (abfd);
2552 /* Convert the external relocations to the internal format. */
2553 if (shdr->sh_entsize == bed->s->sizeof_rel)
2554 swap_in = bed->s->swap_reloc_in;
2555 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2556 swap_in = bed->s->swap_reloca_in;
2557 else
2559 bfd_set_error (bfd_error_wrong_format);
2560 return FALSE;
2563 erela = (const bfd_byte *) external_relocs;
2564 /* Setting erelaend like this and comparing with <= handles case of
2565 a fuzzed object with sh_size not a multiple of sh_entsize. */
2566 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2567 irela = internal_relocs;
2568 while (erela <= erelaend)
2570 bfd_vma r_symndx;
2572 (*swap_in) (abfd, erela, irela);
2573 r_symndx = ELF32_R_SYM (irela->r_info);
2574 if (bed->s->arch_size == 64)
2575 r_symndx >>= 24;
2576 if (nsyms > 0)
2578 if ((size_t) r_symndx >= nsyms)
2580 _bfd_error_handler
2581 /* xgettext:c-format */
2582 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2583 " for offset %#" PRIx64 " in section `%pA'"),
2584 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2585 (uint64_t) irela->r_offset, sec);
2586 bfd_set_error (bfd_error_bad_value);
2587 return FALSE;
2590 else if (r_symndx != STN_UNDEF)
2592 _bfd_error_handler
2593 /* xgettext:c-format */
2594 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2595 " for offset %#" PRIx64 " in section `%pA'"
2596 " when the object file has no symbol table"),
2597 abfd, (uint64_t) r_symndx,
2598 (uint64_t) irela->r_offset, sec);
2599 bfd_set_error (bfd_error_bad_value);
2600 return FALSE;
2602 irela += bed->s->int_rels_per_ext_rel;
2603 erela += shdr->sh_entsize;
2606 return TRUE;
2609 /* Read and swap the relocs for a section O. They may have been
2610 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2611 not NULL, they are used as buffers to read into. They are known to
2612 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2613 the return value is allocated using either malloc or bfd_alloc,
2614 according to the KEEP_MEMORY argument. If O has two relocation
2615 sections (both REL and RELA relocations), then the REL_HDR
2616 relocations will appear first in INTERNAL_RELOCS, followed by the
2617 RELA_HDR relocations. */
2619 Elf_Internal_Rela *
2620 _bfd_elf_link_read_relocs (bfd *abfd,
2621 asection *o,
2622 void *external_relocs,
2623 Elf_Internal_Rela *internal_relocs,
2624 bfd_boolean keep_memory)
2626 void *alloc1 = NULL;
2627 Elf_Internal_Rela *alloc2 = NULL;
2628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2629 struct bfd_elf_section_data *esdo = elf_section_data (o);
2630 Elf_Internal_Rela *internal_rela_relocs;
2632 if (esdo->relocs != NULL)
2633 return esdo->relocs;
2635 if (o->reloc_count == 0)
2636 return NULL;
2638 if (internal_relocs == NULL)
2640 bfd_size_type size;
2642 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2643 if (keep_memory)
2644 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2645 else
2646 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2647 if (internal_relocs == NULL)
2648 goto error_return;
2651 if (external_relocs == NULL)
2653 bfd_size_type size = 0;
2655 if (esdo->rel.hdr)
2656 size += esdo->rel.hdr->sh_size;
2657 if (esdo->rela.hdr)
2658 size += esdo->rela.hdr->sh_size;
2660 alloc1 = bfd_malloc (size);
2661 if (alloc1 == NULL)
2662 goto error_return;
2663 external_relocs = alloc1;
2666 internal_rela_relocs = internal_relocs;
2667 if (esdo->rel.hdr)
2669 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2670 external_relocs,
2671 internal_relocs))
2672 goto error_return;
2673 external_relocs = (((bfd_byte *) external_relocs)
2674 + esdo->rel.hdr->sh_size);
2675 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2676 * bed->s->int_rels_per_ext_rel);
2679 if (esdo->rela.hdr
2680 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2681 external_relocs,
2682 internal_rela_relocs)))
2683 goto error_return;
2685 /* Cache the results for next time, if we can. */
2686 if (keep_memory)
2687 esdo->relocs = internal_relocs;
2689 free (alloc1);
2691 /* Don't free alloc2, since if it was allocated we are passing it
2692 back (under the name of internal_relocs). */
2694 return internal_relocs;
2696 error_return:
2697 free (alloc1);
2698 if (alloc2 != NULL)
2700 if (keep_memory)
2701 bfd_release (abfd, alloc2);
2702 else
2703 free (alloc2);
2705 return NULL;
2708 /* Compute the size of, and allocate space for, REL_HDR which is the
2709 section header for a section containing relocations for O. */
2711 static bfd_boolean
2712 _bfd_elf_link_size_reloc_section (bfd *abfd,
2713 struct bfd_elf_section_reloc_data *reldata)
2715 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2717 /* That allows us to calculate the size of the section. */
2718 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2720 /* The contents field must last into write_object_contents, so we
2721 allocate it with bfd_alloc rather than malloc. Also since we
2722 cannot be sure that the contents will actually be filled in,
2723 we zero the allocated space. */
2724 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2725 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2726 return FALSE;
2728 if (reldata->hashes == NULL && reldata->count)
2730 struct elf_link_hash_entry **p;
2732 p = ((struct elf_link_hash_entry **)
2733 bfd_zmalloc (reldata->count * sizeof (*p)));
2734 if (p == NULL)
2735 return FALSE;
2737 reldata->hashes = p;
2740 return TRUE;
2743 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2744 originated from the section given by INPUT_REL_HDR) to the
2745 OUTPUT_BFD. */
2747 bfd_boolean
2748 _bfd_elf_link_output_relocs (bfd *output_bfd,
2749 asection *input_section,
2750 Elf_Internal_Shdr *input_rel_hdr,
2751 Elf_Internal_Rela *internal_relocs,
2752 struct elf_link_hash_entry **rel_hash
2753 ATTRIBUTE_UNUSED)
2755 Elf_Internal_Rela *irela;
2756 Elf_Internal_Rela *irelaend;
2757 bfd_byte *erel;
2758 struct bfd_elf_section_reloc_data *output_reldata;
2759 asection *output_section;
2760 const struct elf_backend_data *bed;
2761 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2762 struct bfd_elf_section_data *esdo;
2764 output_section = input_section->output_section;
2766 bed = get_elf_backend_data (output_bfd);
2767 esdo = elf_section_data (output_section);
2768 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2770 output_reldata = &esdo->rel;
2771 swap_out = bed->s->swap_reloc_out;
2773 else if (esdo->rela.hdr
2774 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2776 output_reldata = &esdo->rela;
2777 swap_out = bed->s->swap_reloca_out;
2779 else
2781 _bfd_error_handler
2782 /* xgettext:c-format */
2783 (_("%pB: relocation size mismatch in %pB section %pA"),
2784 output_bfd, input_section->owner, input_section);
2785 bfd_set_error (bfd_error_wrong_format);
2786 return FALSE;
2789 erel = output_reldata->hdr->contents;
2790 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2791 irela = internal_relocs;
2792 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2793 * bed->s->int_rels_per_ext_rel);
2794 while (irela < irelaend)
2796 (*swap_out) (output_bfd, irela, erel);
2797 irela += bed->s->int_rels_per_ext_rel;
2798 erel += input_rel_hdr->sh_entsize;
2801 /* Bump the counter, so that we know where to add the next set of
2802 relocations. */
2803 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2805 return TRUE;
2808 /* Make weak undefined symbols in PIE dynamic. */
2810 bfd_boolean
2811 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2812 struct elf_link_hash_entry *h)
2814 if (bfd_link_pie (info)
2815 && h->dynindx == -1
2816 && h->root.type == bfd_link_hash_undefweak)
2817 return bfd_elf_link_record_dynamic_symbol (info, h);
2819 return TRUE;
2822 /* Fix up the flags for a symbol. This handles various cases which
2823 can only be fixed after all the input files are seen. This is
2824 currently called by both adjust_dynamic_symbol and
2825 assign_sym_version, which is unnecessary but perhaps more robust in
2826 the face of future changes. */
2828 static bfd_boolean
2829 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2830 struct elf_info_failed *eif)
2832 const struct elf_backend_data *bed;
2834 /* If this symbol was mentioned in a non-ELF file, try to set
2835 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2836 permit a non-ELF file to correctly refer to a symbol defined in
2837 an ELF dynamic object. */
2838 if (h->non_elf)
2840 while (h->root.type == bfd_link_hash_indirect)
2841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2843 if (h->root.type != bfd_link_hash_defined
2844 && h->root.type != bfd_link_hash_defweak)
2846 h->ref_regular = 1;
2847 h->ref_regular_nonweak = 1;
2849 else
2851 if (h->root.u.def.section->owner != NULL
2852 && (bfd_get_flavour (h->root.u.def.section->owner)
2853 == bfd_target_elf_flavour))
2855 h->ref_regular = 1;
2856 h->ref_regular_nonweak = 1;
2858 else
2859 h->def_regular = 1;
2862 if (h->dynindx == -1
2863 && (h->def_dynamic
2864 || h->ref_dynamic))
2866 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2868 eif->failed = TRUE;
2869 return FALSE;
2873 else
2875 /* Unfortunately, NON_ELF is only correct if the symbol
2876 was first seen in a non-ELF file. Fortunately, if the symbol
2877 was first seen in an ELF file, we're probably OK unless the
2878 symbol was defined in a non-ELF file. Catch that case here.
2879 FIXME: We're still in trouble if the symbol was first seen in
2880 a dynamic object, and then later in a non-ELF regular object. */
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
2883 && !h->def_regular
2884 && (h->root.u.def.section->owner != NULL
2885 ? (bfd_get_flavour (h->root.u.def.section->owner)
2886 != bfd_target_elf_flavour)
2887 : (bfd_is_abs_section (h->root.u.def.section)
2888 && !h->def_dynamic)))
2889 h->def_regular = 1;
2892 /* Backend specific symbol fixup. */
2893 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2894 if (bed->elf_backend_fixup_symbol
2895 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2896 return FALSE;
2898 /* If this is a final link, and the symbol was defined as a common
2899 symbol in a regular object file, and there was no definition in
2900 any dynamic object, then the linker will have allocated space for
2901 the symbol in a common section but the DEF_REGULAR
2902 flag will not have been set. */
2903 if (h->root.type == bfd_link_hash_defined
2904 && !h->def_regular
2905 && h->ref_regular
2906 && !h->def_dynamic
2907 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2908 h->def_regular = 1;
2910 /* Symbols defined in discarded sections shouldn't be dynamic. */
2911 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2914 /* If a weak undefined symbol has non-default visibility, we also
2915 hide it from the dynamic linker. */
2916 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2917 && h->root.type == bfd_link_hash_undefweak)
2918 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2920 /* A hidden versioned symbol in executable should be forced local if
2921 it is is locally defined, not referenced by shared library and not
2922 exported. */
2923 else if (bfd_link_executable (eif->info)
2924 && h->versioned == versioned_hidden
2925 && !eif->info->export_dynamic
2926 && !h->dynamic
2927 && !h->ref_dynamic
2928 && h->def_regular)
2929 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2931 /* If -Bsymbolic was used (which means to bind references to global
2932 symbols to the definition within the shared object), and this
2933 symbol was defined in a regular object, then it actually doesn't
2934 need a PLT entry. Likewise, if the symbol has non-default
2935 visibility. If the symbol has hidden or internal visibility, we
2936 will force it local. */
2937 else if (h->needs_plt
2938 && bfd_link_pic (eif->info)
2939 && is_elf_hash_table (eif->info->hash)
2940 && (SYMBOLIC_BIND (eif->info, h)
2941 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2942 && h->def_regular)
2944 bfd_boolean force_local;
2946 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2947 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2948 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2951 /* If this is a weak defined symbol in a dynamic object, and we know
2952 the real definition in the dynamic object, copy interesting flags
2953 over to the real definition. */
2954 if (h->is_weakalias)
2956 struct elf_link_hash_entry *def = weakdef (h);
2958 /* If the real definition is defined by a regular object file,
2959 don't do anything special. See the longer description in
2960 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2961 bfd_link_hash_defined as it was when put on the alias list
2962 then it must have originally been a versioned symbol (for
2963 which a non-versioned indirect symbol is created) and later
2964 a definition for the non-versioned symbol is found. In that
2965 case the indirection is flipped with the versioned symbol
2966 becoming an indirect pointing at the non-versioned symbol.
2967 Thus, not an alias any more. */
2968 if (def->def_regular
2969 || def->root.type != bfd_link_hash_defined)
2971 h = def;
2972 while ((h = h->u.alias) != def)
2973 h->is_weakalias = 0;
2975 else
2977 while (h->root.type == bfd_link_hash_indirect)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2979 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2980 || h->root.type == bfd_link_hash_defweak);
2981 BFD_ASSERT (def->def_dynamic);
2982 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2986 return TRUE;
2989 /* Make the backend pick a good value for a dynamic symbol. This is
2990 called via elf_link_hash_traverse, and also calls itself
2991 recursively. */
2993 static bfd_boolean
2994 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2996 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2997 struct elf_link_hash_table *htab;
2998 const struct elf_backend_data *bed;
3000 if (! is_elf_hash_table (eif->info->hash))
3001 return FALSE;
3003 /* Ignore indirect symbols. These are added by the versioning code. */
3004 if (h->root.type == bfd_link_hash_indirect)
3005 return TRUE;
3007 /* Fix the symbol flags. */
3008 if (! _bfd_elf_fix_symbol_flags (h, eif))
3009 return FALSE;
3011 htab = elf_hash_table (eif->info);
3012 bed = get_elf_backend_data (htab->dynobj);
3014 if (h->root.type == bfd_link_hash_undefweak)
3016 if (eif->info->dynamic_undefined_weak == 0)
3017 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
3018 else if (eif->info->dynamic_undefined_weak > 0
3019 && h->ref_regular
3020 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3021 && !bfd_hide_sym_by_version (eif->info->version_info,
3022 h->root.root.string))
3024 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3026 eif->failed = TRUE;
3027 return FALSE;
3032 /* If this symbol does not require a PLT entry, and it is not
3033 defined by a dynamic object, or is not referenced by a regular
3034 object, ignore it. We do have to handle a weak defined symbol,
3035 even if no regular object refers to it, if we decided to add it
3036 to the dynamic symbol table. FIXME: Do we normally need to worry
3037 about symbols which are defined by one dynamic object and
3038 referenced by another one? */
3039 if (!h->needs_plt
3040 && h->type != STT_GNU_IFUNC
3041 && (h->def_regular
3042 || !h->def_dynamic
3043 || (!h->ref_regular
3044 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3046 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3047 return TRUE;
3050 /* If we've already adjusted this symbol, don't do it again. This
3051 can happen via a recursive call. */
3052 if (h->dynamic_adjusted)
3053 return TRUE;
3055 /* Don't look at this symbol again. Note that we must set this
3056 after checking the above conditions, because we may look at a
3057 symbol once, decide not to do anything, and then get called
3058 recursively later after REF_REGULAR is set below. */
3059 h->dynamic_adjusted = 1;
3061 /* If this is a weak definition, and we know a real definition, and
3062 the real symbol is not itself defined by a regular object file,
3063 then get a good value for the real definition. We handle the
3064 real symbol first, for the convenience of the backend routine.
3066 Note that there is a confusing case here. If the real definition
3067 is defined by a regular object file, we don't get the real symbol
3068 from the dynamic object, but we do get the weak symbol. If the
3069 processor backend uses a COPY reloc, then if some routine in the
3070 dynamic object changes the real symbol, we will not see that
3071 change in the corresponding weak symbol. This is the way other
3072 ELF linkers work as well, and seems to be a result of the shared
3073 library model.
3075 I will clarify this issue. Most SVR4 shared libraries define the
3076 variable _timezone and define timezone as a weak synonym. The
3077 tzset call changes _timezone. If you write
3078 extern int timezone;
3079 int _timezone = 5;
3080 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3081 you might expect that, since timezone is a synonym for _timezone,
3082 the same number will print both times. However, if the processor
3083 backend uses a COPY reloc, then actually timezone will be copied
3084 into your process image, and, since you define _timezone
3085 yourself, _timezone will not. Thus timezone and _timezone will
3086 wind up at different memory locations. The tzset call will set
3087 _timezone, leaving timezone unchanged. */
3089 if (h->is_weakalias)
3091 struct elf_link_hash_entry *def = weakdef (h);
3093 /* If we get to this point, there is an implicit reference to
3094 the alias by a regular object file via the weak symbol H. */
3095 def->ref_regular = 1;
3097 /* Ensure that the backend adjust_dynamic_symbol function sees
3098 the strong alias before H by recursively calling ourselves. */
3099 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3100 return FALSE;
3103 /* If a symbol has no type and no size and does not require a PLT
3104 entry, then we are probably about to do the wrong thing here: we
3105 are probably going to create a COPY reloc for an empty object.
3106 This case can arise when a shared object is built with assembly
3107 code, and the assembly code fails to set the symbol type. */
3108 if (h->size == 0
3109 && h->type == STT_NOTYPE
3110 && !h->needs_plt)
3111 _bfd_error_handler
3112 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3113 h->root.root.string);
3115 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3117 eif->failed = TRUE;
3118 return FALSE;
3121 return TRUE;
3124 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3125 DYNBSS. */
3127 bfd_boolean
3128 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3129 struct elf_link_hash_entry *h,
3130 asection *dynbss)
3132 unsigned int power_of_two;
3133 bfd_vma mask;
3134 asection *sec = h->root.u.def.section;
3136 /* The section alignment of the definition is the maximum alignment
3137 requirement of symbols defined in the section. Since we don't
3138 know the symbol alignment requirement, we start with the
3139 maximum alignment and check low bits of the symbol address
3140 for the minimum alignment. */
3141 power_of_two = bfd_section_alignment (sec);
3142 mask = ((bfd_vma) 1 << power_of_two) - 1;
3143 while ((h->root.u.def.value & mask) != 0)
3145 mask >>= 1;
3146 --power_of_two;
3149 if (power_of_two > bfd_section_alignment (dynbss))
3151 /* Adjust the section alignment if needed. */
3152 if (!bfd_set_section_alignment (dynbss, power_of_two))
3153 return FALSE;
3156 /* We make sure that the symbol will be aligned properly. */
3157 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3159 /* Define the symbol as being at this point in DYNBSS. */
3160 h->root.u.def.section = dynbss;
3161 h->root.u.def.value = dynbss->size;
3163 /* Increment the size of DYNBSS to make room for the symbol. */
3164 dynbss->size += h->size;
3166 /* No error if extern_protected_data is true. */
3167 if (h->protected_def
3168 && (!info->extern_protected_data
3169 || (info->extern_protected_data < 0
3170 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3171 info->callbacks->einfo
3172 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3173 h->root.root.string);
3175 return TRUE;
3178 /* Adjust all external symbols pointing into SEC_MERGE sections
3179 to reflect the object merging within the sections. */
3181 static bfd_boolean
3182 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3184 asection *sec;
3186 if ((h->root.type == bfd_link_hash_defined
3187 || h->root.type == bfd_link_hash_defweak)
3188 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3189 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3191 bfd *output_bfd = (bfd *) data;
3193 h->root.u.def.value =
3194 _bfd_merged_section_offset (output_bfd,
3195 &h->root.u.def.section,
3196 elf_section_data (sec)->sec_info,
3197 h->root.u.def.value);
3200 return TRUE;
3203 /* Returns false if the symbol referred to by H should be considered
3204 to resolve local to the current module, and true if it should be
3205 considered to bind dynamically. */
3207 bfd_boolean
3208 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3209 struct bfd_link_info *info,
3210 bfd_boolean not_local_protected)
3212 bfd_boolean binding_stays_local_p;
3213 const struct elf_backend_data *bed;
3214 struct elf_link_hash_table *hash_table;
3216 if (h == NULL)
3217 return FALSE;
3219 while (h->root.type == bfd_link_hash_indirect
3220 || h->root.type == bfd_link_hash_warning)
3221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3223 /* If it was forced local, then clearly it's not dynamic. */
3224 if (h->dynindx == -1)
3225 return FALSE;
3226 if (h->forced_local)
3227 return FALSE;
3229 /* Identify the cases where name binding rules say that a
3230 visible symbol resolves locally. */
3231 binding_stays_local_p = (bfd_link_executable (info)
3232 || SYMBOLIC_BIND (info, h));
3234 switch (ELF_ST_VISIBILITY (h->other))
3236 case STV_INTERNAL:
3237 case STV_HIDDEN:
3238 return FALSE;
3240 case STV_PROTECTED:
3241 hash_table = elf_hash_table (info);
3242 if (!is_elf_hash_table (hash_table))
3243 return FALSE;
3245 bed = get_elf_backend_data (hash_table->dynobj);
3247 /* Proper resolution for function pointer equality may require
3248 that these symbols perhaps be resolved dynamically, even though
3249 we should be resolving them to the current module. */
3250 if (!not_local_protected || !bed->is_function_type (h->type))
3251 binding_stays_local_p = TRUE;
3252 break;
3254 default:
3255 break;
3258 /* If it isn't defined locally, then clearly it's dynamic. */
3259 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3260 return TRUE;
3262 /* Otherwise, the symbol is dynamic if binding rules don't tell
3263 us that it remains local. */
3264 return !binding_stays_local_p;
3267 /* Return true if the symbol referred to by H should be considered
3268 to resolve local to the current module, and false otherwise. Differs
3269 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3270 undefined symbols. The two functions are virtually identical except
3271 for the place where dynindx == -1 is tested. If that test is true,
3272 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3273 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3274 defined symbols.
3275 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3276 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3277 treatment of undefined weak symbols. For those that do not make
3278 undefined weak symbols dynamic, both functions may return false. */
3280 bfd_boolean
3281 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3282 struct bfd_link_info *info,
3283 bfd_boolean local_protected)
3285 const struct elf_backend_data *bed;
3286 struct elf_link_hash_table *hash_table;
3288 /* If it's a local sym, of course we resolve locally. */
3289 if (h == NULL)
3290 return TRUE;
3292 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3293 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3294 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3295 return TRUE;
3297 /* Forced local symbols resolve locally. */
3298 if (h->forced_local)
3299 return TRUE;
3301 /* Common symbols that become definitions don't get the DEF_REGULAR
3302 flag set, so test it first, and don't bail out. */
3303 if (ELF_COMMON_DEF_P (h))
3304 /* Do nothing. */;
3305 /* If we don't have a definition in a regular file, then we can't
3306 resolve locally. The sym is either undefined or dynamic. */
3307 else if (!h->def_regular)
3308 return FALSE;
3310 /* Non-dynamic symbols resolve locally. */
3311 if (h->dynindx == -1)
3312 return TRUE;
3314 /* At this point, we know the symbol is defined and dynamic. In an
3315 executable it must resolve locally, likewise when building symbolic
3316 shared libraries. */
3317 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3318 return TRUE;
3320 /* Now deal with defined dynamic symbols in shared libraries. Ones
3321 with default visibility might not resolve locally. */
3322 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3323 return FALSE;
3325 hash_table = elf_hash_table (info);
3326 if (!is_elf_hash_table (hash_table))
3327 return TRUE;
3329 bed = get_elf_backend_data (hash_table->dynobj);
3331 /* If extern_protected_data is false, STV_PROTECTED non-function
3332 symbols are local. */
3333 if ((!info->extern_protected_data
3334 || (info->extern_protected_data < 0
3335 && !bed->extern_protected_data))
3336 && !bed->is_function_type (h->type))
3337 return TRUE;
3339 /* Function pointer equality tests may require that STV_PROTECTED
3340 symbols be treated as dynamic symbols. If the address of a
3341 function not defined in an executable is set to that function's
3342 plt entry in the executable, then the address of the function in
3343 a shared library must also be the plt entry in the executable. */
3344 return local_protected;
3347 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3348 aligned. Returns the first TLS output section. */
3350 struct bfd_section *
3351 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3353 struct bfd_section *sec, *tls;
3354 unsigned int align = 0;
3356 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3357 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3358 break;
3359 tls = sec;
3361 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3362 if (sec->alignment_power > align)
3363 align = sec->alignment_power;
3365 elf_hash_table (info)->tls_sec = tls;
3367 /* Ensure the alignment of the first section (usually .tdata) is the largest
3368 alignment, so that the tls segment starts aligned. */
3369 if (tls != NULL)
3370 tls->alignment_power = align;
3372 return tls;
3375 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3376 static bfd_boolean
3377 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3378 Elf_Internal_Sym *sym)
3380 const struct elf_backend_data *bed;
3382 /* Local symbols do not count, but target specific ones might. */
3383 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3384 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3385 return FALSE;
3387 bed = get_elf_backend_data (abfd);
3388 /* Function symbols do not count. */
3389 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3390 return FALSE;
3392 /* If the section is undefined, then so is the symbol. */
3393 if (sym->st_shndx == SHN_UNDEF)
3394 return FALSE;
3396 /* If the symbol is defined in the common section, then
3397 it is a common definition and so does not count. */
3398 if (bed->common_definition (sym))
3399 return FALSE;
3401 /* If the symbol is in a target specific section then we
3402 must rely upon the backend to tell us what it is. */
3403 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3404 /* FIXME - this function is not coded yet:
3406 return _bfd_is_global_symbol_definition (abfd, sym);
3408 Instead for now assume that the definition is not global,
3409 Even if this is wrong, at least the linker will behave
3410 in the same way that it used to do. */
3411 return FALSE;
3413 return TRUE;
3416 /* Search the symbol table of the archive element of the archive ABFD
3417 whose archive map contains a mention of SYMDEF, and determine if
3418 the symbol is defined in this element. */
3419 static bfd_boolean
3420 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3422 Elf_Internal_Shdr * hdr;
3423 size_t symcount;
3424 size_t extsymcount;
3425 size_t extsymoff;
3426 Elf_Internal_Sym *isymbuf;
3427 Elf_Internal_Sym *isym;
3428 Elf_Internal_Sym *isymend;
3429 bfd_boolean result;
3431 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3432 if (abfd == NULL)
3433 return FALSE;
3435 if (! bfd_check_format (abfd, bfd_object))
3436 return FALSE;
3438 /* Select the appropriate symbol table. If we don't know if the
3439 object file is an IR object, give linker LTO plugin a chance to
3440 get the correct symbol table. */
3441 if (abfd->plugin_format == bfd_plugin_yes
3442 #if BFD_SUPPORTS_PLUGINS
3443 || (abfd->plugin_format == bfd_plugin_unknown
3444 && bfd_link_plugin_object_p (abfd))
3445 #endif
3448 /* Use the IR symbol table if the object has been claimed by
3449 plugin. */
3450 abfd = abfd->plugin_dummy_bfd;
3451 hdr = &elf_tdata (abfd)->symtab_hdr;
3453 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3454 hdr = &elf_tdata (abfd)->symtab_hdr;
3455 else
3456 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3458 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3460 /* The sh_info field of the symtab header tells us where the
3461 external symbols start. We don't care about the local symbols. */
3462 if (elf_bad_symtab (abfd))
3464 extsymcount = symcount;
3465 extsymoff = 0;
3467 else
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3473 if (extsymcount == 0)
3474 return FALSE;
3476 /* Read in the symbol table. */
3477 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3478 NULL, NULL, NULL);
3479 if (isymbuf == NULL)
3480 return FALSE;
3482 /* Scan the symbol table looking for SYMDEF. */
3483 result = FALSE;
3484 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3486 const char *name;
3488 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3489 isym->st_name);
3490 if (name == NULL)
3491 break;
3493 if (strcmp (name, symdef->name) == 0)
3495 result = is_global_data_symbol_definition (abfd, isym);
3496 break;
3500 free (isymbuf);
3502 return result;
3505 /* Add an entry to the .dynamic table. */
3507 bfd_boolean
3508 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3509 bfd_vma tag,
3510 bfd_vma val)
3512 struct elf_link_hash_table *hash_table;
3513 const struct elf_backend_data *bed;
3514 asection *s;
3515 bfd_size_type newsize;
3516 bfd_byte *newcontents;
3517 Elf_Internal_Dyn dyn;
3519 hash_table = elf_hash_table (info);
3520 if (! is_elf_hash_table (hash_table))
3521 return FALSE;
3523 if (tag == DT_RELA || tag == DT_REL)
3524 hash_table->dynamic_relocs = TRUE;
3526 bed = get_elf_backend_data (hash_table->dynobj);
3527 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3528 BFD_ASSERT (s != NULL);
3530 newsize = s->size + bed->s->sizeof_dyn;
3531 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3532 if (newcontents == NULL)
3533 return FALSE;
3535 dyn.d_tag = tag;
3536 dyn.d_un.d_val = val;
3537 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3539 s->size = newsize;
3540 s->contents = newcontents;
3542 return TRUE;
3545 /* Strip zero-sized dynamic sections. */
3547 bfd_boolean
3548 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3550 struct elf_link_hash_table *hash_table;
3551 const struct elf_backend_data *bed;
3552 asection *s, *sdynamic, **pp;
3553 asection *rela_dyn, *rel_dyn;
3554 Elf_Internal_Dyn dyn;
3555 bfd_byte *extdyn, *next;
3556 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3557 bfd_boolean strip_zero_sized;
3558 bfd_boolean strip_zero_sized_plt;
3560 if (bfd_link_relocatable (info))
3561 return TRUE;
3563 hash_table = elf_hash_table (info);
3564 if (!is_elf_hash_table (hash_table))
3565 return FALSE;
3567 if (!hash_table->dynobj)
3568 return TRUE;
3570 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3571 if (!sdynamic)
3572 return TRUE;
3574 bed = get_elf_backend_data (hash_table->dynobj);
3575 swap_dyn_in = bed->s->swap_dyn_in;
3577 strip_zero_sized = FALSE;
3578 strip_zero_sized_plt = FALSE;
3580 /* Strip zero-sized dynamic sections. */
3581 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3582 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3583 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3584 if (s->size == 0
3585 && (s == rela_dyn
3586 || s == rel_dyn
3587 || s == hash_table->srelplt->output_section
3588 || s == hash_table->splt->output_section))
3590 *pp = s->next;
3591 info->output_bfd->section_count--;
3592 strip_zero_sized = TRUE;
3593 if (s == rela_dyn)
3594 s = rela_dyn;
3595 if (s == rel_dyn)
3596 s = rel_dyn;
3597 else if (s == hash_table->splt->output_section)
3599 s = hash_table->splt;
3600 strip_zero_sized_plt = TRUE;
3602 else
3603 s = hash_table->srelplt;
3604 s->flags |= SEC_EXCLUDE;
3605 s->output_section = bfd_abs_section_ptr;
3607 else
3608 pp = &s->next;
3610 if (strip_zero_sized_plt)
3611 for (extdyn = sdynamic->contents;
3612 extdyn < sdynamic->contents + sdynamic->size;
3613 extdyn = next)
3615 next = extdyn + bed->s->sizeof_dyn;
3616 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3617 switch (dyn.d_tag)
3619 default:
3620 break;
3621 case DT_JMPREL:
3622 case DT_PLTRELSZ:
3623 case DT_PLTREL:
3624 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3625 the procedure linkage table (the .plt section) has been
3626 removed. */
3627 memmove (extdyn, next,
3628 sdynamic->size - (next - sdynamic->contents));
3629 next = extdyn;
3633 if (strip_zero_sized)
3635 /* Regenerate program headers. */
3636 elf_seg_map (info->output_bfd) = NULL;
3637 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3640 return TRUE;
3643 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3644 1 if a DT_NEEDED tag already exists, and 0 on success. */
3647 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3649 struct elf_link_hash_table *hash_table;
3650 size_t strindex;
3651 const char *soname;
3653 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3654 return -1;
3656 hash_table = elf_hash_table (info);
3657 soname = elf_dt_name (abfd);
3658 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3659 if (strindex == (size_t) -1)
3660 return -1;
3662 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3664 asection *sdyn;
3665 const struct elf_backend_data *bed;
3666 bfd_byte *extdyn;
3668 bed = get_elf_backend_data (hash_table->dynobj);
3669 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3670 if (sdyn != NULL)
3671 for (extdyn = sdyn->contents;
3672 extdyn < sdyn->contents + sdyn->size;
3673 extdyn += bed->s->sizeof_dyn)
3675 Elf_Internal_Dyn dyn;
3677 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3678 if (dyn.d_tag == DT_NEEDED
3679 && dyn.d_un.d_val == strindex)
3681 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3682 return 1;
3687 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3688 return -1;
3690 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3691 return -1;
3693 return 0;
3696 /* Return true if SONAME is on the needed list between NEEDED and STOP
3697 (or the end of list if STOP is NULL), and needed by a library that
3698 will be loaded. */
3700 static bfd_boolean
3701 on_needed_list (const char *soname,
3702 struct bfd_link_needed_list *needed,
3703 struct bfd_link_needed_list *stop)
3705 struct bfd_link_needed_list *look;
3706 for (look = needed; look != stop; look = look->next)
3707 if (strcmp (soname, look->name) == 0
3708 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3709 /* If needed by a library that itself is not directly
3710 needed, recursively check whether that library is
3711 indirectly needed. Since we add DT_NEEDED entries to
3712 the end of the list, library dependencies appear after
3713 the library. Therefore search prior to the current
3714 LOOK, preventing possible infinite recursion. */
3715 || on_needed_list (elf_dt_name (look->by), needed, look)))
3716 return TRUE;
3718 return FALSE;
3721 /* Sort symbol by value, section, size, and type. */
3722 static int
3723 elf_sort_symbol (const void *arg1, const void *arg2)
3725 const struct elf_link_hash_entry *h1;
3726 const struct elf_link_hash_entry *h2;
3727 bfd_signed_vma vdiff;
3728 int sdiff;
3729 const char *n1;
3730 const char *n2;
3732 h1 = *(const struct elf_link_hash_entry **) arg1;
3733 h2 = *(const struct elf_link_hash_entry **) arg2;
3734 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3735 if (vdiff != 0)
3736 return vdiff > 0 ? 1 : -1;
3738 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3739 if (sdiff != 0)
3740 return sdiff;
3742 /* Sort so that sized symbols are selected over zero size symbols. */
3743 vdiff = h1->size - h2->size;
3744 if (vdiff != 0)
3745 return vdiff > 0 ? 1 : -1;
3747 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3748 if (h1->type != h2->type)
3749 return h1->type - h2->type;
3751 /* If symbols are properly sized and typed, and multiple strong
3752 aliases are not defined in a shared library by the user we
3753 shouldn't get here. Unfortunately linker script symbols like
3754 __bss_start sometimes match a user symbol defined at the start of
3755 .bss without proper size and type. We'd like to preference the
3756 user symbol over reserved system symbols. Sort on leading
3757 underscores. */
3758 n1 = h1->root.root.string;
3759 n2 = h2->root.root.string;
3760 while (*n1 == *n2)
3762 if (*n1 == 0)
3763 break;
3764 ++n1;
3765 ++n2;
3767 if (*n1 == '_')
3768 return -1;
3769 if (*n2 == '_')
3770 return 1;
3772 /* Final sort on name selects user symbols like '_u' over reserved
3773 system symbols like '_Z' and also will avoid qsort instability. */
3774 return *n1 - *n2;
3777 /* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3780 static bfd_boolean
3781 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3783 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3785 if (h->dynindx != -1)
3786 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3787 return TRUE;
3790 /* Assign string offsets in .dynstr, update all structures referencing
3791 them. */
3793 static bfd_boolean
3794 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3796 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3797 struct elf_link_local_dynamic_entry *entry;
3798 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3799 bfd *dynobj = hash_table->dynobj;
3800 asection *sdyn;
3801 bfd_size_type size;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3805 _bfd_elf_strtab_finalize (dynstr);
3806 size = _bfd_elf_strtab_size (dynstr);
3808 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3810 if (info->callbacks->examine_strtab)
3811 info->callbacks->examine_strtab (dynstr);
3813 bed = get_elf_backend_data (dynobj);
3814 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3815 BFD_ASSERT (sdyn != NULL);
3817 /* Update all .dynamic entries referencing .dynstr strings. */
3818 for (extdyn = sdyn->contents;
3819 extdyn < sdyn->contents + sdyn->size;
3820 extdyn += bed->s->sizeof_dyn)
3822 Elf_Internal_Dyn dyn;
3824 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3825 switch (dyn.d_tag)
3827 case DT_STRSZ:
3828 dyn.d_un.d_val = size;
3829 break;
3830 case DT_NEEDED:
3831 case DT_SONAME:
3832 case DT_RPATH:
3833 case DT_RUNPATH:
3834 case DT_FILTER:
3835 case DT_AUXILIARY:
3836 case DT_AUDIT:
3837 case DT_DEPAUDIT:
3838 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3839 break;
3840 default:
3841 continue;
3843 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3846 /* Now update local dynamic symbols. */
3847 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3848 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3849 entry->isym.st_name);
3851 /* And the rest of dynamic symbols. */
3852 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3854 /* Adjust version definitions. */
3855 if (elf_tdata (output_bfd)->cverdefs)
3857 asection *s;
3858 bfd_byte *p;
3859 size_t i;
3860 Elf_Internal_Verdef def;
3861 Elf_Internal_Verdaux defaux;
3863 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3864 p = s->contents;
3867 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3868 &def);
3869 p += sizeof (Elf_External_Verdef);
3870 if (def.vd_aux != sizeof (Elf_External_Verdef))
3871 continue;
3872 for (i = 0; i < def.vd_cnt; ++i)
3874 _bfd_elf_swap_verdaux_in (output_bfd,
3875 (Elf_External_Verdaux *) p, &defaux);
3876 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3877 defaux.vda_name);
3878 _bfd_elf_swap_verdaux_out (output_bfd,
3879 &defaux, (Elf_External_Verdaux *) p);
3880 p += sizeof (Elf_External_Verdaux);
3883 while (def.vd_next);
3886 /* Adjust version references. */
3887 if (elf_tdata (output_bfd)->verref)
3889 asection *s;
3890 bfd_byte *p;
3891 size_t i;
3892 Elf_Internal_Verneed need;
3893 Elf_Internal_Vernaux needaux;
3895 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3896 p = s->contents;
3899 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3900 &need);
3901 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3902 _bfd_elf_swap_verneed_out (output_bfd, &need,
3903 (Elf_External_Verneed *) p);
3904 p += sizeof (Elf_External_Verneed);
3905 for (i = 0; i < need.vn_cnt; ++i)
3907 _bfd_elf_swap_vernaux_in (output_bfd,
3908 (Elf_External_Vernaux *) p, &needaux);
3909 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3910 needaux.vna_name);
3911 _bfd_elf_swap_vernaux_out (output_bfd,
3912 &needaux,
3913 (Elf_External_Vernaux *) p);
3914 p += sizeof (Elf_External_Vernaux);
3917 while (need.vn_next);
3920 return TRUE;
3923 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3924 The default is to only match when the INPUT and OUTPUT are exactly
3925 the same target. */
3927 bfd_boolean
3928 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3929 const bfd_target *output)
3931 return input == output;
3934 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3935 This version is used when different targets for the same architecture
3936 are virtually identical. */
3938 bfd_boolean
3939 _bfd_elf_relocs_compatible (const bfd_target *input,
3940 const bfd_target *output)
3942 const struct elf_backend_data *obed, *ibed;
3944 if (input == output)
3945 return TRUE;
3947 ibed = xvec_get_elf_backend_data (input);
3948 obed = xvec_get_elf_backend_data (output);
3950 if (ibed->arch != obed->arch)
3951 return FALSE;
3953 /* If both backends are using this function, deem them compatible. */
3954 return ibed->relocs_compatible == obed->relocs_compatible;
3957 /* Make a special call to the linker "notice" function to tell it that
3958 we are about to handle an as-needed lib, or have finished
3959 processing the lib. */
3961 bfd_boolean
3962 _bfd_elf_notice_as_needed (bfd *ibfd,
3963 struct bfd_link_info *info,
3964 enum notice_asneeded_action act)
3966 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3969 /* Check relocations an ELF object file. */
3971 bfd_boolean
3972 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3974 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3975 struct elf_link_hash_table *htab = elf_hash_table (info);
3977 /* If this object is the same format as the output object, and it is
3978 not a shared library, then let the backend look through the
3979 relocs.
3981 This is required to build global offset table entries and to
3982 arrange for dynamic relocs. It is not required for the
3983 particular common case of linking non PIC code, even when linking
3984 against shared libraries, but unfortunately there is no way of
3985 knowing whether an object file has been compiled PIC or not.
3986 Looking through the relocs is not particularly time consuming.
3987 The problem is that we must either (1) keep the relocs in memory,
3988 which causes the linker to require additional runtime memory or
3989 (2) read the relocs twice from the input file, which wastes time.
3990 This would be a good case for using mmap.
3992 I have no idea how to handle linking PIC code into a file of a
3993 different format. It probably can't be done. */
3994 if ((abfd->flags & DYNAMIC) == 0
3995 && is_elf_hash_table (htab)
3996 && bed->check_relocs != NULL
3997 && elf_object_id (abfd) == elf_hash_table_id (htab)
3998 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4000 asection *o;
4002 for (o = abfd->sections; o != NULL; o = o->next)
4004 Elf_Internal_Rela *internal_relocs;
4005 bfd_boolean ok;
4007 /* Don't check relocations in excluded sections. Don't do
4008 anything special with non-loaded, non-alloced sections.
4009 In particular, any relocs in such sections should not
4010 affect GOT and PLT reference counting (ie. we don't
4011 allow them to create GOT or PLT entries), there's no
4012 possibility or desire to optimize TLS relocs, and
4013 there's not much point in propagating relocs to shared
4014 libs that the dynamic linker won't relocate. */
4015 if ((o->flags & SEC_ALLOC) == 0
4016 || (o->flags & SEC_RELOC) == 0
4017 || (o->flags & SEC_EXCLUDE) != 0
4018 || o->reloc_count == 0
4019 || ((info->strip == strip_all || info->strip == strip_debugger)
4020 && (o->flags & SEC_DEBUGGING) != 0)
4021 || bfd_is_abs_section (o->output_section))
4022 continue;
4024 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4025 info->keep_memory);
4026 if (internal_relocs == NULL)
4027 return FALSE;
4029 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4031 if (elf_section_data (o)->relocs != internal_relocs)
4032 free (internal_relocs);
4034 if (! ok)
4035 return FALSE;
4039 return TRUE;
4042 /* Add symbols from an ELF object file to the linker hash table. */
4044 static bfd_boolean
4045 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4047 Elf_Internal_Ehdr *ehdr;
4048 Elf_Internal_Shdr *hdr;
4049 size_t symcount;
4050 size_t extsymcount;
4051 size_t extsymoff;
4052 struct elf_link_hash_entry **sym_hash;
4053 bfd_boolean dynamic;
4054 Elf_External_Versym *extversym = NULL;
4055 Elf_External_Versym *extversym_end = NULL;
4056 Elf_External_Versym *ever;
4057 struct elf_link_hash_entry *weaks;
4058 struct elf_link_hash_entry **nondeflt_vers = NULL;
4059 size_t nondeflt_vers_cnt = 0;
4060 Elf_Internal_Sym *isymbuf = NULL;
4061 Elf_Internal_Sym *isym;
4062 Elf_Internal_Sym *isymend;
4063 const struct elf_backend_data *bed;
4064 bfd_boolean add_needed;
4065 struct elf_link_hash_table *htab;
4066 void *alloc_mark = NULL;
4067 struct bfd_hash_entry **old_table = NULL;
4068 unsigned int old_size = 0;
4069 unsigned int old_count = 0;
4070 void *old_tab = NULL;
4071 void *old_ent;
4072 struct bfd_link_hash_entry *old_undefs = NULL;
4073 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4074 void *old_strtab = NULL;
4075 size_t tabsize = 0;
4076 asection *s;
4077 bfd_boolean just_syms;
4079 htab = elf_hash_table (info);
4080 bed = get_elf_backend_data (abfd);
4082 if ((abfd->flags & DYNAMIC) == 0)
4083 dynamic = FALSE;
4084 else
4086 dynamic = TRUE;
4088 /* You can't use -r against a dynamic object. Also, there's no
4089 hope of using a dynamic object which does not exactly match
4090 the format of the output file. */
4091 if (bfd_link_relocatable (info)
4092 || !is_elf_hash_table (htab)
4093 || info->output_bfd->xvec != abfd->xvec)
4095 if (bfd_link_relocatable (info))
4096 bfd_set_error (bfd_error_invalid_operation);
4097 else
4098 bfd_set_error (bfd_error_wrong_format);
4099 goto error_return;
4103 ehdr = elf_elfheader (abfd);
4104 if (info->warn_alternate_em
4105 && bed->elf_machine_code != ehdr->e_machine
4106 && ((bed->elf_machine_alt1 != 0
4107 && ehdr->e_machine == bed->elf_machine_alt1)
4108 || (bed->elf_machine_alt2 != 0
4109 && ehdr->e_machine == bed->elf_machine_alt2)))
4110 _bfd_error_handler
4111 /* xgettext:c-format */
4112 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4113 ehdr->e_machine, abfd, bed->elf_machine_code);
4115 /* As a GNU extension, any input sections which are named
4116 .gnu.warning.SYMBOL are treated as warning symbols for the given
4117 symbol. This differs from .gnu.warning sections, which generate
4118 warnings when they are included in an output file. */
4119 /* PR 12761: Also generate this warning when building shared libraries. */
4120 for (s = abfd->sections; s != NULL; s = s->next)
4122 const char *name;
4124 name = bfd_section_name (s);
4125 if (CONST_STRNEQ (name, ".gnu.warning."))
4127 char *msg;
4128 bfd_size_type sz;
4130 name += sizeof ".gnu.warning." - 1;
4132 /* If this is a shared object, then look up the symbol
4133 in the hash table. If it is there, and it is already
4134 been defined, then we will not be using the entry
4135 from this shared object, so we don't need to warn.
4136 FIXME: If we see the definition in a regular object
4137 later on, we will warn, but we shouldn't. The only
4138 fix is to keep track of what warnings we are supposed
4139 to emit, and then handle them all at the end of the
4140 link. */
4141 if (dynamic)
4143 struct elf_link_hash_entry *h;
4145 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4147 /* FIXME: What about bfd_link_hash_common? */
4148 if (h != NULL
4149 && (h->root.type == bfd_link_hash_defined
4150 || h->root.type == bfd_link_hash_defweak))
4151 continue;
4154 sz = s->size;
4155 msg = (char *) bfd_alloc (abfd, sz + 1);
4156 if (msg == NULL)
4157 goto error_return;
4159 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4160 goto error_return;
4162 msg[sz] = '\0';
4164 if (! (_bfd_generic_link_add_one_symbol
4165 (info, abfd, name, BSF_WARNING, s, 0, msg,
4166 FALSE, bed->collect, NULL)))
4167 goto error_return;
4169 if (bfd_link_executable (info))
4171 /* Clobber the section size so that the warning does
4172 not get copied into the output file. */
4173 s->size = 0;
4175 /* Also set SEC_EXCLUDE, so that symbols defined in
4176 the warning section don't get copied to the output. */
4177 s->flags |= SEC_EXCLUDE;
4182 just_syms = ((s = abfd->sections) != NULL
4183 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4185 add_needed = TRUE;
4186 if (! dynamic)
4188 /* If we are creating a shared library, create all the dynamic
4189 sections immediately. We need to attach them to something,
4190 so we attach them to this BFD, provided it is the right
4191 format and is not from ld --just-symbols. Always create the
4192 dynamic sections for -E/--dynamic-list. FIXME: If there
4193 are no input BFD's of the same format as the output, we can't
4194 make a shared library. */
4195 if (!just_syms
4196 && (bfd_link_pic (info)
4197 || (!bfd_link_relocatable (info)
4198 && info->nointerp
4199 && (info->export_dynamic || info->dynamic)))
4200 && is_elf_hash_table (htab)
4201 && info->output_bfd->xvec == abfd->xvec
4202 && !htab->dynamic_sections_created)
4204 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4205 goto error_return;
4208 else if (!is_elf_hash_table (htab))
4209 goto error_return;
4210 else
4212 const char *soname = NULL;
4213 char *audit = NULL;
4214 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4215 const Elf_Internal_Phdr *phdr;
4216 struct elf_link_loaded_list *loaded_lib;
4218 /* ld --just-symbols and dynamic objects don't mix very well.
4219 ld shouldn't allow it. */
4220 if (just_syms)
4221 abort ();
4223 /* If this dynamic lib was specified on the command line with
4224 --as-needed in effect, then we don't want to add a DT_NEEDED
4225 tag unless the lib is actually used. Similary for libs brought
4226 in by another lib's DT_NEEDED. When --no-add-needed is used
4227 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4228 any dynamic library in DT_NEEDED tags in the dynamic lib at
4229 all. */
4230 add_needed = (elf_dyn_lib_class (abfd)
4231 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4232 | DYN_NO_NEEDED)) == 0;
4234 s = bfd_get_section_by_name (abfd, ".dynamic");
4235 if (s != NULL)
4237 bfd_byte *dynbuf;
4238 bfd_byte *extdyn;
4239 unsigned int elfsec;
4240 unsigned long shlink;
4242 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4244 error_free_dyn:
4245 free (dynbuf);
4246 goto error_return;
4249 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4250 if (elfsec == SHN_BAD)
4251 goto error_free_dyn;
4252 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4254 for (extdyn = dynbuf;
4255 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4256 extdyn += bed->s->sizeof_dyn)
4258 Elf_Internal_Dyn dyn;
4260 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4261 if (dyn.d_tag == DT_SONAME)
4263 unsigned int tagv = dyn.d_un.d_val;
4264 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4265 if (soname == NULL)
4266 goto error_free_dyn;
4268 if (dyn.d_tag == DT_NEEDED)
4270 struct bfd_link_needed_list *n, **pn;
4271 char *fnm, *anm;
4272 unsigned int tagv = dyn.d_un.d_val;
4273 size_t amt = sizeof (struct bfd_link_needed_list);
4275 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4276 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4277 if (n == NULL || fnm == NULL)
4278 goto error_free_dyn;
4279 amt = strlen (fnm) + 1;
4280 anm = (char *) bfd_alloc (abfd, amt);
4281 if (anm == NULL)
4282 goto error_free_dyn;
4283 memcpy (anm, fnm, amt);
4284 n->name = anm;
4285 n->by = abfd;
4286 n->next = NULL;
4287 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4289 *pn = n;
4291 if (dyn.d_tag == DT_RUNPATH)
4293 struct bfd_link_needed_list *n, **pn;
4294 char *fnm, *anm;
4295 unsigned int tagv = dyn.d_un.d_val;
4296 size_t amt = sizeof (struct bfd_link_needed_list);
4298 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4299 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4300 if (n == NULL || fnm == NULL)
4301 goto error_free_dyn;
4302 amt = strlen (fnm) + 1;
4303 anm = (char *) bfd_alloc (abfd, amt);
4304 if (anm == NULL)
4305 goto error_free_dyn;
4306 memcpy (anm, fnm, amt);
4307 n->name = anm;
4308 n->by = abfd;
4309 n->next = NULL;
4310 for (pn = & runpath;
4311 *pn != NULL;
4312 pn = &(*pn)->next)
4314 *pn = n;
4316 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4317 if (!runpath && dyn.d_tag == DT_RPATH)
4319 struct bfd_link_needed_list *n, **pn;
4320 char *fnm, *anm;
4321 unsigned int tagv = dyn.d_un.d_val;
4322 size_t amt = sizeof (struct bfd_link_needed_list);
4324 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4325 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4326 if (n == NULL || fnm == NULL)
4327 goto error_free_dyn;
4328 amt = strlen (fnm) + 1;
4329 anm = (char *) bfd_alloc (abfd, amt);
4330 if (anm == NULL)
4331 goto error_free_dyn;
4332 memcpy (anm, fnm, amt);
4333 n->name = anm;
4334 n->by = abfd;
4335 n->next = NULL;
4336 for (pn = & rpath;
4337 *pn != NULL;
4338 pn = &(*pn)->next)
4340 *pn = n;
4342 if (dyn.d_tag == DT_AUDIT)
4344 unsigned int tagv = dyn.d_un.d_val;
4345 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4349 free (dynbuf);
4352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4353 frees all more recently bfd_alloc'd blocks as well. */
4354 if (runpath)
4355 rpath = runpath;
4357 if (rpath)
4359 struct bfd_link_needed_list **pn;
4360 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4362 *pn = rpath;
4365 /* If we have a PT_GNU_RELRO program header, mark as read-only
4366 all sections contained fully therein. This makes relro
4367 shared library sections appear as they will at run-time. */
4368 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4369 while (phdr-- > elf_tdata (abfd)->phdr)
4370 if (phdr->p_type == PT_GNU_RELRO)
4372 for (s = abfd->sections; s != NULL; s = s->next)
4374 unsigned int opb = bfd_octets_per_byte (abfd, s);
4376 if ((s->flags & SEC_ALLOC) != 0
4377 && s->vma * opb >= phdr->p_vaddr
4378 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4379 s->flags |= SEC_READONLY;
4381 break;
4384 /* We do not want to include any of the sections in a dynamic
4385 object in the output file. We hack by simply clobbering the
4386 list of sections in the BFD. This could be handled more
4387 cleanly by, say, a new section flag; the existing
4388 SEC_NEVER_LOAD flag is not the one we want, because that one
4389 still implies that the section takes up space in the output
4390 file. */
4391 bfd_section_list_clear (abfd);
4393 /* Find the name to use in a DT_NEEDED entry that refers to this
4394 object. If the object has a DT_SONAME entry, we use it.
4395 Otherwise, if the generic linker stuck something in
4396 elf_dt_name, we use that. Otherwise, we just use the file
4397 name. */
4398 if (soname == NULL || *soname == '\0')
4400 soname = elf_dt_name (abfd);
4401 if (soname == NULL || *soname == '\0')
4402 soname = bfd_get_filename (abfd);
4405 /* Save the SONAME because sometimes the linker emulation code
4406 will need to know it. */
4407 elf_dt_name (abfd) = soname;
4409 /* If we have already included this dynamic object in the
4410 link, just ignore it. There is no reason to include a
4411 particular dynamic object more than once. */
4412 for (loaded_lib = htab->dyn_loaded;
4413 loaded_lib != NULL;
4414 loaded_lib = loaded_lib->next)
4416 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4417 return TRUE;
4420 /* Create dynamic sections for backends that require that be done
4421 before setup_gnu_properties. */
4422 if (add_needed
4423 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4424 return FALSE;
4426 /* Save the DT_AUDIT entry for the linker emulation code. */
4427 elf_dt_audit (abfd) = audit;
4430 /* If this is a dynamic object, we always link against the .dynsym
4431 symbol table, not the .symtab symbol table. The dynamic linker
4432 will only see the .dynsym symbol table, so there is no reason to
4433 look at .symtab for a dynamic object. */
4435 if (! dynamic || elf_dynsymtab (abfd) == 0)
4436 hdr = &elf_tdata (abfd)->symtab_hdr;
4437 else
4438 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4440 symcount = hdr->sh_size / bed->s->sizeof_sym;
4442 /* The sh_info field of the symtab header tells us where the
4443 external symbols start. We don't care about the local symbols at
4444 this point. */
4445 if (elf_bad_symtab (abfd))
4447 extsymcount = symcount;
4448 extsymoff = 0;
4450 else
4452 extsymcount = symcount - hdr->sh_info;
4453 extsymoff = hdr->sh_info;
4456 sym_hash = elf_sym_hashes (abfd);
4457 if (extsymcount != 0)
4459 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4460 NULL, NULL, NULL);
4461 if (isymbuf == NULL)
4462 goto error_return;
4464 if (sym_hash == NULL)
4466 /* We store a pointer to the hash table entry for each
4467 external symbol. */
4468 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4469 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4470 if (sym_hash == NULL)
4471 goto error_free_sym;
4472 elf_sym_hashes (abfd) = sym_hash;
4476 if (dynamic)
4478 /* Read in any version definitions. */
4479 if (!_bfd_elf_slurp_version_tables (abfd,
4480 info->default_imported_symver))
4481 goto error_free_sym;
4483 /* Read in the symbol versions, but don't bother to convert them
4484 to internal format. */
4485 if (elf_dynversym (abfd) != 0)
4487 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4488 bfd_size_type amt = versymhdr->sh_size;
4490 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4491 goto error_free_sym;
4492 extversym = (Elf_External_Versym *)
4493 _bfd_malloc_and_read (abfd, amt, amt);
4494 if (extversym == NULL)
4495 goto error_free_sym;
4496 extversym_end = extversym + amt / sizeof (*extversym);
4500 /* If we are loading an as-needed shared lib, save the symbol table
4501 state before we start adding symbols. If the lib turns out
4502 to be unneeded, restore the state. */
4503 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4505 unsigned int i;
4506 size_t entsize;
4508 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4510 struct bfd_hash_entry *p;
4511 struct elf_link_hash_entry *h;
4513 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4515 h = (struct elf_link_hash_entry *) p;
4516 entsize += htab->root.table.entsize;
4517 if (h->root.type == bfd_link_hash_warning)
4519 entsize += htab->root.table.entsize;
4520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4522 if (h->root.type == bfd_link_hash_common)
4523 entsize += sizeof (*h->root.u.c.p);
4527 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4528 old_tab = bfd_malloc (tabsize + entsize);
4529 if (old_tab == NULL)
4530 goto error_free_vers;
4532 /* Remember the current objalloc pointer, so that all mem for
4533 symbols added can later be reclaimed. */
4534 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4535 if (alloc_mark == NULL)
4536 goto error_free_vers;
4538 /* Make a special call to the linker "notice" function to
4539 tell it that we are about to handle an as-needed lib. */
4540 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4541 goto error_free_vers;
4543 /* Clone the symbol table. Remember some pointers into the
4544 symbol table, and dynamic symbol count. */
4545 old_ent = (char *) old_tab + tabsize;
4546 memcpy (old_tab, htab->root.table.table, tabsize);
4547 old_undefs = htab->root.undefs;
4548 old_undefs_tail = htab->root.undefs_tail;
4549 old_table = htab->root.table.table;
4550 old_size = htab->root.table.size;
4551 old_count = htab->root.table.count;
4552 old_strtab = NULL;
4553 if (htab->dynstr != NULL)
4555 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4556 if (old_strtab == NULL)
4557 goto error_free_vers;
4560 for (i = 0; i < htab->root.table.size; i++)
4562 struct bfd_hash_entry *p;
4563 struct elf_link_hash_entry *h;
4565 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4567 h = (struct elf_link_hash_entry *) p;
4568 memcpy (old_ent, h, htab->root.table.entsize);
4569 old_ent = (char *) old_ent + htab->root.table.entsize;
4570 if (h->root.type == bfd_link_hash_warning)
4572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4573 memcpy (old_ent, h, htab->root.table.entsize);
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
4576 if (h->root.type == bfd_link_hash_common)
4578 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4579 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4585 weaks = NULL;
4586 if (extversym == NULL)
4587 ever = NULL;
4588 else if (extversym + extsymoff < extversym_end)
4589 ever = extversym + extsymoff;
4590 else
4592 /* xgettext:c-format */
4593 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4594 abfd, (long) extsymoff,
4595 (long) (extversym_end - extversym) / sizeof (* extversym));
4596 bfd_set_error (bfd_error_bad_value);
4597 goto error_free_vers;
4600 if (!bfd_link_relocatable (info)
4601 && abfd->lto_slim_object)
4603 _bfd_error_handler
4604 (_("%pB: plugin needed to handle lto object"), abfd);
4607 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4608 isym < isymend;
4609 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4611 int bind;
4612 bfd_vma value;
4613 asection *sec, *new_sec;
4614 flagword flags;
4615 const char *name;
4616 struct elf_link_hash_entry *h;
4617 struct elf_link_hash_entry *hi;
4618 bfd_boolean definition;
4619 bfd_boolean size_change_ok;
4620 bfd_boolean type_change_ok;
4621 bfd_boolean new_weak;
4622 bfd_boolean old_weak;
4623 bfd *override;
4624 bfd_boolean common;
4625 bfd_boolean discarded;
4626 unsigned int old_alignment;
4627 unsigned int shindex;
4628 bfd *old_bfd;
4629 bfd_boolean matched;
4631 override = NULL;
4633 flags = BSF_NO_FLAGS;
4634 sec = NULL;
4635 value = isym->st_value;
4636 common = bed->common_definition (isym);
4637 if (common && info->inhibit_common_definition)
4639 /* Treat common symbol as undefined for --no-define-common. */
4640 isym->st_shndx = SHN_UNDEF;
4641 common = FALSE;
4643 discarded = FALSE;
4645 bind = ELF_ST_BIND (isym->st_info);
4646 switch (bind)
4648 case STB_LOCAL:
4649 /* This should be impossible, since ELF requires that all
4650 global symbols follow all local symbols, and that sh_info
4651 point to the first global symbol. Unfortunately, Irix 5
4652 screws this up. */
4653 if (elf_bad_symtab (abfd))
4654 continue;
4656 /* If we aren't prepared to handle locals within the globals
4657 then we'll likely segfault on a NULL symbol hash if the
4658 symbol is ever referenced in relocations. */
4659 shindex = elf_elfheader (abfd)->e_shstrndx;
4660 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4661 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4662 " (>= sh_info of %lu)"),
4663 abfd, name, (long) (isym - isymbuf + extsymoff),
4664 (long) extsymoff);
4666 /* Dynamic object relocations are not processed by ld, so
4667 ld won't run into the problem mentioned above. */
4668 if (dynamic)
4669 continue;
4670 bfd_set_error (bfd_error_bad_value);
4671 goto error_free_vers;
4673 case STB_GLOBAL:
4674 if (isym->st_shndx != SHN_UNDEF && !common)
4675 flags = BSF_GLOBAL;
4676 break;
4678 case STB_WEAK:
4679 flags = BSF_WEAK;
4680 break;
4682 case STB_GNU_UNIQUE:
4683 flags = BSF_GNU_UNIQUE;
4684 break;
4686 default:
4687 /* Leave it up to the processor backend. */
4688 break;
4691 if (isym->st_shndx == SHN_UNDEF)
4692 sec = bfd_und_section_ptr;
4693 else if (isym->st_shndx == SHN_ABS)
4694 sec = bfd_abs_section_ptr;
4695 else if (isym->st_shndx == SHN_COMMON)
4697 sec = bfd_com_section_ptr;
4698 /* What ELF calls the size we call the value. What ELF
4699 calls the value we call the alignment. */
4700 value = isym->st_size;
4702 else
4704 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4705 if (sec == NULL)
4706 sec = bfd_abs_section_ptr;
4707 else if (discarded_section (sec))
4709 /* Symbols from discarded section are undefined. We keep
4710 its visibility. */
4711 sec = bfd_und_section_ptr;
4712 discarded = TRUE;
4713 isym->st_shndx = SHN_UNDEF;
4715 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4716 value -= sec->vma;
4719 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4720 isym->st_name);
4721 if (name == NULL)
4722 goto error_free_vers;
4724 if (isym->st_shndx == SHN_COMMON
4725 && (abfd->flags & BFD_PLUGIN) != 0)
4727 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4729 if (xc == NULL)
4731 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4732 | SEC_EXCLUDE);
4733 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4734 if (xc == NULL)
4735 goto error_free_vers;
4737 sec = xc;
4739 else if (isym->st_shndx == SHN_COMMON
4740 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4741 && !bfd_link_relocatable (info))
4743 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4745 if (tcomm == NULL)
4747 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4748 | SEC_LINKER_CREATED);
4749 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4750 if (tcomm == NULL)
4751 goto error_free_vers;
4753 sec = tcomm;
4755 else if (bed->elf_add_symbol_hook)
4757 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4758 &sec, &value))
4759 goto error_free_vers;
4761 /* The hook function sets the name to NULL if this symbol
4762 should be skipped for some reason. */
4763 if (name == NULL)
4764 continue;
4767 /* Sanity check that all possibilities were handled. */
4768 if (sec == NULL)
4769 abort ();
4771 /* Silently discard TLS symbols from --just-syms. There's
4772 no way to combine a static TLS block with a new TLS block
4773 for this executable. */
4774 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4775 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4776 continue;
4778 if (bfd_is_und_section (sec)
4779 || bfd_is_com_section (sec))
4780 definition = FALSE;
4781 else
4782 definition = TRUE;
4784 size_change_ok = FALSE;
4785 type_change_ok = bed->type_change_ok;
4786 old_weak = FALSE;
4787 matched = FALSE;
4788 old_alignment = 0;
4789 old_bfd = NULL;
4790 new_sec = sec;
4792 if (is_elf_hash_table (htab))
4794 Elf_Internal_Versym iver;
4795 unsigned int vernum = 0;
4796 bfd_boolean skip;
4798 if (ever == NULL)
4800 if (info->default_imported_symver)
4801 /* Use the default symbol version created earlier. */
4802 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4803 else
4804 iver.vs_vers = 0;
4806 else if (ever >= extversym_end)
4808 /* xgettext:c-format */
4809 _bfd_error_handler (_("%pB: not enough version information"),
4810 abfd);
4811 bfd_set_error (bfd_error_bad_value);
4812 goto error_free_vers;
4814 else
4815 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4817 vernum = iver.vs_vers & VERSYM_VERSION;
4819 /* If this is a hidden symbol, or if it is not version
4820 1, we append the version name to the symbol name.
4821 However, we do not modify a non-hidden absolute symbol
4822 if it is not a function, because it might be the version
4823 symbol itself. FIXME: What if it isn't? */
4824 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4825 || (vernum > 1
4826 && (!bfd_is_abs_section (sec)
4827 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4829 const char *verstr;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4833 if (isym->st_shndx != SHN_UNDEF)
4835 if (vernum > elf_tdata (abfd)->cverdefs)
4836 verstr = NULL;
4837 else if (vernum > 1)
4838 verstr =
4839 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4840 else
4841 verstr = "";
4843 if (verstr == NULL)
4845 _bfd_error_handler
4846 /* xgettext:c-format */
4847 (_("%pB: %s: invalid version %u (max %d)"),
4848 abfd, name, vernum,
4849 elf_tdata (abfd)->cverdefs);
4850 bfd_set_error (bfd_error_bad_value);
4851 goto error_free_vers;
4854 else
4856 /* We cannot simply test for the number of
4857 entries in the VERNEED section since the
4858 numbers for the needed versions do not start
4859 at 0. */
4860 Elf_Internal_Verneed *t;
4862 verstr = NULL;
4863 for (t = elf_tdata (abfd)->verref;
4864 t != NULL;
4865 t = t->vn_nextref)
4867 Elf_Internal_Vernaux *a;
4869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4871 if (a->vna_other == vernum)
4873 verstr = a->vna_nodename;
4874 break;
4877 if (a != NULL)
4878 break;
4880 if (verstr == NULL)
4882 _bfd_error_handler
4883 /* xgettext:c-format */
4884 (_("%pB: %s: invalid needed version %d"),
4885 abfd, name, vernum);
4886 bfd_set_error (bfd_error_bad_value);
4887 goto error_free_vers;
4891 namelen = strlen (name);
4892 verlen = strlen (verstr);
4893 newlen = namelen + verlen + 2;
4894 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4895 && isym->st_shndx != SHN_UNDEF)
4896 ++newlen;
4898 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4899 if (newname == NULL)
4900 goto error_free_vers;
4901 memcpy (newname, name, namelen);
4902 p = newname + namelen;
4903 *p++ = ELF_VER_CHR;
4904 /* If this is a defined non-hidden version symbol,
4905 we add another @ to the name. This indicates the
4906 default version of the symbol. */
4907 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4908 && isym->st_shndx != SHN_UNDEF)
4909 *p++ = ELF_VER_CHR;
4910 memcpy (p, verstr, verlen + 1);
4912 name = newname;
4915 /* If this symbol has default visibility and the user has
4916 requested we not re-export it, then mark it as hidden. */
4917 if (!bfd_is_und_section (sec)
4918 && !dynamic
4919 && abfd->no_export
4920 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4921 isym->st_other = (STV_HIDDEN
4922 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4924 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4925 sym_hash, &old_bfd, &old_weak,
4926 &old_alignment, &skip, &override,
4927 &type_change_ok, &size_change_ok,
4928 &matched))
4929 goto error_free_vers;
4931 if (skip)
4932 continue;
4934 /* Override a definition only if the new symbol matches the
4935 existing one. */
4936 if (override && matched)
4937 definition = FALSE;
4939 h = *sym_hash;
4940 while (h->root.type == bfd_link_hash_indirect
4941 || h->root.type == bfd_link_hash_warning)
4942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4944 if (elf_tdata (abfd)->verdef != NULL
4945 && vernum > 1
4946 && definition)
4947 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4950 if (! (_bfd_generic_link_add_one_symbol
4951 (info, override ? override : abfd, name, flags, sec, value,
4952 NULL, FALSE, bed->collect,
4953 (struct bfd_link_hash_entry **) sym_hash)))
4954 goto error_free_vers;
4956 h = *sym_hash;
4957 /* We need to make sure that indirect symbol dynamic flags are
4958 updated. */
4959 hi = h;
4960 while (h->root.type == bfd_link_hash_indirect
4961 || h->root.type == bfd_link_hash_warning)
4962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4964 /* Setting the index to -3 tells elf_link_output_extsym that
4965 this symbol is defined in a discarded section. */
4966 if (discarded)
4967 h->indx = -3;
4969 *sym_hash = h;
4971 new_weak = (flags & BSF_WEAK) != 0;
4972 if (dynamic
4973 && definition
4974 && new_weak
4975 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4976 && is_elf_hash_table (htab)
4977 && h->u.alias == NULL)
4979 /* Keep a list of all weak defined non function symbols from
4980 a dynamic object, using the alias field. Later in this
4981 function we will set the alias field to the correct
4982 value. We only put non-function symbols from dynamic
4983 objects on this list, because that happens to be the only
4984 time we need to know the normal symbol corresponding to a
4985 weak symbol, and the information is time consuming to
4986 figure out. If the alias field is not already NULL,
4987 then this symbol was already defined by some previous
4988 dynamic object, and we will be using that previous
4989 definition anyhow. */
4991 h->u.alias = weaks;
4992 weaks = h;
4995 /* Set the alignment of a common symbol. */
4996 if ((common || bfd_is_com_section (sec))
4997 && h->root.type == bfd_link_hash_common)
4999 unsigned int align;
5001 if (common)
5002 align = bfd_log2 (isym->st_value);
5003 else
5005 /* The new symbol is a common symbol in a shared object.
5006 We need to get the alignment from the section. */
5007 align = new_sec->alignment_power;
5009 if (align > old_alignment)
5010 h->root.u.c.p->alignment_power = align;
5011 else
5012 h->root.u.c.p->alignment_power = old_alignment;
5015 if (is_elf_hash_table (htab))
5017 /* Set a flag in the hash table entry indicating the type of
5018 reference or definition we just found. A dynamic symbol
5019 is one which is referenced or defined by both a regular
5020 object and a shared object. */
5021 bfd_boolean dynsym = FALSE;
5023 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5024 if ((abfd->flags & BFD_PLUGIN) != 0)
5026 else if (!dynamic)
5028 if (! definition)
5030 h->ref_regular = 1;
5031 if (bind != STB_WEAK)
5032 h->ref_regular_nonweak = 1;
5034 else
5036 h->def_regular = 1;
5037 if (h->def_dynamic)
5039 h->def_dynamic = 0;
5040 h->ref_dynamic = 1;
5044 else
5046 if (! definition)
5048 h->ref_dynamic = 1;
5049 hi->ref_dynamic = 1;
5051 else
5053 h->def_dynamic = 1;
5054 hi->def_dynamic = 1;
5058 /* If an indirect symbol has been forced local, don't
5059 make the real symbol dynamic. */
5060 if (h != hi && hi->forced_local)
5062 else if (!dynamic)
5064 if (bfd_link_dll (info)
5065 || h->def_dynamic
5066 || h->ref_dynamic)
5067 dynsym = TRUE;
5069 else
5071 if (h->def_regular
5072 || h->ref_regular
5073 || (h->is_weakalias
5074 && weakdef (h)->dynindx != -1))
5075 dynsym = TRUE;
5078 /* Check to see if we need to add an indirect symbol for
5079 the default name. */
5080 if ((definition
5081 || (!override && h->root.type == bfd_link_hash_common))
5082 && !(hi != h
5083 && hi->versioned == versioned_hidden))
5084 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5085 sec, value, &old_bfd, &dynsym))
5086 goto error_free_vers;
5088 /* Check the alignment when a common symbol is involved. This
5089 can change when a common symbol is overridden by a normal
5090 definition or a common symbol is ignored due to the old
5091 normal definition. We need to make sure the maximum
5092 alignment is maintained. */
5093 if ((old_alignment || common)
5094 && h->root.type != bfd_link_hash_common)
5096 unsigned int common_align;
5097 unsigned int normal_align;
5098 unsigned int symbol_align;
5099 bfd *normal_bfd;
5100 bfd *common_bfd;
5102 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5103 || h->root.type == bfd_link_hash_defweak);
5105 symbol_align = ffs (h->root.u.def.value) - 1;
5106 if (h->root.u.def.section->owner != NULL
5107 && (h->root.u.def.section->owner->flags
5108 & (DYNAMIC | BFD_PLUGIN)) == 0)
5110 normal_align = h->root.u.def.section->alignment_power;
5111 if (normal_align > symbol_align)
5112 normal_align = symbol_align;
5114 else
5115 normal_align = symbol_align;
5117 if (old_alignment)
5119 common_align = old_alignment;
5120 common_bfd = old_bfd;
5121 normal_bfd = abfd;
5123 else
5125 common_align = bfd_log2 (isym->st_value);
5126 common_bfd = abfd;
5127 normal_bfd = old_bfd;
5130 if (normal_align < common_align)
5132 /* PR binutils/2735 */
5133 if (normal_bfd == NULL)
5134 _bfd_error_handler
5135 /* xgettext:c-format */
5136 (_("warning: alignment %u of common symbol `%s' in %pB is"
5137 " greater than the alignment (%u) of its section %pA"),
5138 1 << common_align, name, common_bfd,
5139 1 << normal_align, h->root.u.def.section);
5140 else
5141 _bfd_error_handler
5142 /* xgettext:c-format */
5143 (_("warning: alignment %u of symbol `%s' in %pB"
5144 " is smaller than %u in %pB"),
5145 1 << normal_align, name, normal_bfd,
5146 1 << common_align, common_bfd);
5150 /* Remember the symbol size if it isn't undefined. */
5151 if (isym->st_size != 0
5152 && isym->st_shndx != SHN_UNDEF
5153 && (definition || h->size == 0))
5155 if (h->size != 0
5156 && h->size != isym->st_size
5157 && ! size_change_ok)
5158 _bfd_error_handler
5159 /* xgettext:c-format */
5160 (_("warning: size of symbol `%s' changed"
5161 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5162 name, (uint64_t) h->size, old_bfd,
5163 (uint64_t) isym->st_size, abfd);
5165 h->size = isym->st_size;
5168 /* If this is a common symbol, then we always want H->SIZE
5169 to be the size of the common symbol. The code just above
5170 won't fix the size if a common symbol becomes larger. We
5171 don't warn about a size change here, because that is
5172 covered by --warn-common. Allow changes between different
5173 function types. */
5174 if (h->root.type == bfd_link_hash_common)
5175 h->size = h->root.u.c.size;
5177 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5178 && ((definition && !new_weak)
5179 || (old_weak && h->root.type == bfd_link_hash_common)
5180 || h->type == STT_NOTYPE))
5182 unsigned int type = ELF_ST_TYPE (isym->st_info);
5184 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5185 symbol. */
5186 if (type == STT_GNU_IFUNC
5187 && (abfd->flags & DYNAMIC) != 0)
5188 type = STT_FUNC;
5190 if (h->type != type)
5192 if (h->type != STT_NOTYPE && ! type_change_ok)
5193 /* xgettext:c-format */
5194 _bfd_error_handler
5195 (_("warning: type of symbol `%s' changed"
5196 " from %d to %d in %pB"),
5197 name, h->type, type, abfd);
5199 h->type = type;
5203 /* Merge st_other field. */
5204 elf_merge_st_other (abfd, h, isym->st_other, sec,
5205 definition, dynamic);
5207 /* We don't want to make debug symbol dynamic. */
5208 if (definition
5209 && (sec->flags & SEC_DEBUGGING)
5210 && !bfd_link_relocatable (info))
5211 dynsym = FALSE;
5213 /* Nor should we make plugin symbols dynamic. */
5214 if ((abfd->flags & BFD_PLUGIN) != 0)
5215 dynsym = FALSE;
5217 if (definition)
5219 h->target_internal = isym->st_target_internal;
5220 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5223 if (definition && !dynamic)
5225 char *p = strchr (name, ELF_VER_CHR);
5226 if (p != NULL && p[1] != ELF_VER_CHR)
5228 /* Queue non-default versions so that .symver x, x@FOO
5229 aliases can be checked. */
5230 if (!nondeflt_vers)
5232 size_t amt = ((isymend - isym + 1)
5233 * sizeof (struct elf_link_hash_entry *));
5234 nondeflt_vers
5235 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5236 if (!nondeflt_vers)
5237 goto error_free_vers;
5239 nondeflt_vers[nondeflt_vers_cnt++] = h;
5243 if (dynsym && h->dynindx == -1)
5245 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5246 goto error_free_vers;
5247 if (h->is_weakalias
5248 && weakdef (h)->dynindx == -1)
5250 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5251 goto error_free_vers;
5254 else if (h->dynindx != -1)
5255 /* If the symbol already has a dynamic index, but
5256 visibility says it should not be visible, turn it into
5257 a local symbol. */
5258 switch (ELF_ST_VISIBILITY (h->other))
5260 case STV_INTERNAL:
5261 case STV_HIDDEN:
5262 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5263 dynsym = FALSE;
5264 break;
5267 if (!add_needed
5268 && matched
5269 && definition
5270 && ((dynsym
5271 && h->ref_regular_nonweak)
5272 || (old_bfd != NULL
5273 && (old_bfd->flags & BFD_PLUGIN) != 0
5274 && bind != STB_WEAK)
5275 || (h->ref_dynamic_nonweak
5276 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5277 && !on_needed_list (elf_dt_name (abfd),
5278 htab->needed, NULL))))
5280 const char *soname = elf_dt_name (abfd);
5282 info->callbacks->minfo ("%!", soname, old_bfd,
5283 h->root.root.string);
5285 /* A symbol from a library loaded via DT_NEEDED of some
5286 other library is referenced by a regular object.
5287 Add a DT_NEEDED entry for it. Issue an error if
5288 --no-add-needed is used and the reference was not
5289 a weak one. */
5290 if (old_bfd != NULL
5291 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5293 _bfd_error_handler
5294 /* xgettext:c-format */
5295 (_("%pB: undefined reference to symbol '%s'"),
5296 old_bfd, name);
5297 bfd_set_error (bfd_error_missing_dso);
5298 goto error_free_vers;
5301 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5302 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5304 /* Create dynamic sections for backends that require
5305 that be done before setup_gnu_properties. */
5306 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5307 return FALSE;
5308 add_needed = TRUE;
5313 if (info->lto_plugin_active
5314 && !bfd_link_relocatable (info)
5315 && (abfd->flags & BFD_PLUGIN) == 0
5316 && !just_syms
5317 && extsymcount)
5319 int r_sym_shift;
5321 if (bed->s->arch_size == 32)
5322 r_sym_shift = 8;
5323 else
5324 r_sym_shift = 32;
5326 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5327 referenced in regular objects so that linker plugin will get
5328 the correct symbol resolution. */
5330 sym_hash = elf_sym_hashes (abfd);
5331 for (s = abfd->sections; s != NULL; s = s->next)
5333 Elf_Internal_Rela *internal_relocs;
5334 Elf_Internal_Rela *rel, *relend;
5336 /* Don't check relocations in excluded sections. */
5337 if ((s->flags & SEC_RELOC) == 0
5338 || s->reloc_count == 0
5339 || (s->flags & SEC_EXCLUDE) != 0
5340 || ((info->strip == strip_all
5341 || info->strip == strip_debugger)
5342 && (s->flags & SEC_DEBUGGING) != 0))
5343 continue;
5345 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5346 NULL,
5347 info->keep_memory);
5348 if (internal_relocs == NULL)
5349 goto error_free_vers;
5351 rel = internal_relocs;
5352 relend = rel + s->reloc_count;
5353 for ( ; rel < relend; rel++)
5355 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5356 struct elf_link_hash_entry *h;
5358 /* Skip local symbols. */
5359 if (r_symndx < extsymoff)
5360 continue;
5362 h = sym_hash[r_symndx - extsymoff];
5363 if (h != NULL)
5364 h->root.non_ir_ref_regular = 1;
5367 if (elf_section_data (s)->relocs != internal_relocs)
5368 free (internal_relocs);
5372 free (extversym);
5373 extversym = NULL;
5374 free (isymbuf);
5375 isymbuf = NULL;
5377 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5379 unsigned int i;
5381 /* Restore the symbol table. */
5382 old_ent = (char *) old_tab + tabsize;
5383 memset (elf_sym_hashes (abfd), 0,
5384 extsymcount * sizeof (struct elf_link_hash_entry *));
5385 htab->root.table.table = old_table;
5386 htab->root.table.size = old_size;
5387 htab->root.table.count = old_count;
5388 memcpy (htab->root.table.table, old_tab, tabsize);
5389 htab->root.undefs = old_undefs;
5390 htab->root.undefs_tail = old_undefs_tail;
5391 if (htab->dynstr != NULL)
5392 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5393 free (old_strtab);
5394 old_strtab = NULL;
5395 for (i = 0; i < htab->root.table.size; i++)
5397 struct bfd_hash_entry *p;
5398 struct elf_link_hash_entry *h;
5399 unsigned int non_ir_ref_dynamic;
5401 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5403 /* Preserve non_ir_ref_dynamic so that this symbol
5404 will be exported when the dynamic lib becomes needed
5405 in the second pass. */
5406 h = (struct elf_link_hash_entry *) p;
5407 if (h->root.type == bfd_link_hash_warning)
5408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5409 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5411 h = (struct elf_link_hash_entry *) p;
5412 memcpy (h, old_ent, htab->root.table.entsize);
5413 old_ent = (char *) old_ent + htab->root.table.entsize;
5414 if (h->root.type == bfd_link_hash_warning)
5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
5420 if (h->root.type == bfd_link_hash_common)
5422 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5423 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5425 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5429 /* Make a special call to the linker "notice" function to
5430 tell it that symbols added for crefs may need to be removed. */
5431 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5432 goto error_free_vers;
5434 free (old_tab);
5435 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5436 alloc_mark);
5437 free (nondeflt_vers);
5438 return TRUE;
5441 if (old_tab != NULL)
5443 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5444 goto error_free_vers;
5445 free (old_tab);
5446 old_tab = NULL;
5449 /* Now that all the symbols from this input file are created, if
5450 not performing a relocatable link, handle .symver foo, foo@BAR
5451 such that any relocs against foo become foo@BAR. */
5452 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5454 size_t cnt, symidx;
5456 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5458 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5459 char *shortname, *p;
5460 size_t amt;
5462 p = strchr (h->root.root.string, ELF_VER_CHR);
5463 if (p == NULL
5464 || (h->root.type != bfd_link_hash_defined
5465 && h->root.type != bfd_link_hash_defweak))
5466 continue;
5468 amt = p - h->root.root.string;
5469 shortname = (char *) bfd_malloc (amt + 1);
5470 if (!shortname)
5471 goto error_free_vers;
5472 memcpy (shortname, h->root.root.string, amt);
5473 shortname[amt] = '\0';
5475 hi = (struct elf_link_hash_entry *)
5476 bfd_link_hash_lookup (&htab->root, shortname,
5477 FALSE, FALSE, FALSE);
5478 if (hi != NULL
5479 && hi->root.type == h->root.type
5480 && hi->root.u.def.value == h->root.u.def.value
5481 && hi->root.u.def.section == h->root.u.def.section)
5483 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5484 hi->root.type = bfd_link_hash_indirect;
5485 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5486 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5487 sym_hash = elf_sym_hashes (abfd);
5488 if (sym_hash)
5489 for (symidx = 0; symidx < extsymcount; ++symidx)
5490 if (sym_hash[symidx] == hi)
5492 sym_hash[symidx] = h;
5493 break;
5496 free (shortname);
5498 free (nondeflt_vers);
5499 nondeflt_vers = NULL;
5502 /* Now set the alias field correctly for all the weak defined
5503 symbols we found. The only way to do this is to search all the
5504 symbols. Since we only need the information for non functions in
5505 dynamic objects, that's the only time we actually put anything on
5506 the list WEAKS. We need this information so that if a regular
5507 object refers to a symbol defined weakly in a dynamic object, the
5508 real symbol in the dynamic object is also put in the dynamic
5509 symbols; we also must arrange for both symbols to point to the
5510 same memory location. We could handle the general case of symbol
5511 aliasing, but a general symbol alias can only be generated in
5512 assembler code, handling it correctly would be very time
5513 consuming, and other ELF linkers don't handle general aliasing
5514 either. */
5515 if (weaks != NULL)
5517 struct elf_link_hash_entry **hpp;
5518 struct elf_link_hash_entry **hppend;
5519 struct elf_link_hash_entry **sorted_sym_hash;
5520 struct elf_link_hash_entry *h;
5521 size_t sym_count, amt;
5523 /* Since we have to search the whole symbol list for each weak
5524 defined symbol, search time for N weak defined symbols will be
5525 O(N^2). Binary search will cut it down to O(NlogN). */
5526 amt = extsymcount * sizeof (*sorted_sym_hash);
5527 sorted_sym_hash = bfd_malloc (amt);
5528 if (sorted_sym_hash == NULL)
5529 goto error_return;
5530 sym_hash = sorted_sym_hash;
5531 hpp = elf_sym_hashes (abfd);
5532 hppend = hpp + extsymcount;
5533 sym_count = 0;
5534 for (; hpp < hppend; hpp++)
5536 h = *hpp;
5537 if (h != NULL
5538 && h->root.type == bfd_link_hash_defined
5539 && !bed->is_function_type (h->type))
5541 *sym_hash = h;
5542 sym_hash++;
5543 sym_count++;
5547 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5548 elf_sort_symbol);
5550 while (weaks != NULL)
5552 struct elf_link_hash_entry *hlook;
5553 asection *slook;
5554 bfd_vma vlook;
5555 size_t i, j, idx = 0;
5557 hlook = weaks;
5558 weaks = hlook->u.alias;
5559 hlook->u.alias = NULL;
5561 if (hlook->root.type != bfd_link_hash_defined
5562 && hlook->root.type != bfd_link_hash_defweak)
5563 continue;
5565 slook = hlook->root.u.def.section;
5566 vlook = hlook->root.u.def.value;
5568 i = 0;
5569 j = sym_count;
5570 while (i != j)
5572 bfd_signed_vma vdiff;
5573 idx = (i + j) / 2;
5574 h = sorted_sym_hash[idx];
5575 vdiff = vlook - h->root.u.def.value;
5576 if (vdiff < 0)
5577 j = idx;
5578 else if (vdiff > 0)
5579 i = idx + 1;
5580 else
5582 int sdiff = slook->id - h->root.u.def.section->id;
5583 if (sdiff < 0)
5584 j = idx;
5585 else if (sdiff > 0)
5586 i = idx + 1;
5587 else
5588 break;
5592 /* We didn't find a value/section match. */
5593 if (i == j)
5594 continue;
5596 /* With multiple aliases, or when the weak symbol is already
5597 strongly defined, we have multiple matching symbols and
5598 the binary search above may land on any of them. Step
5599 one past the matching symbol(s). */
5600 while (++idx != j)
5602 h = sorted_sym_hash[idx];
5603 if (h->root.u.def.section != slook
5604 || h->root.u.def.value != vlook)
5605 break;
5608 /* Now look back over the aliases. Since we sorted by size
5609 as well as value and section, we'll choose the one with
5610 the largest size. */
5611 while (idx-- != i)
5613 h = sorted_sym_hash[idx];
5615 /* Stop if value or section doesn't match. */
5616 if (h->root.u.def.section != slook
5617 || h->root.u.def.value != vlook)
5618 break;
5619 else if (h != hlook)
5621 struct elf_link_hash_entry *t;
5623 hlook->u.alias = h;
5624 hlook->is_weakalias = 1;
5625 t = h;
5626 if (t->u.alias != NULL)
5627 while (t->u.alias != h)
5628 t = t->u.alias;
5629 t->u.alias = hlook;
5631 /* If the weak definition is in the list of dynamic
5632 symbols, make sure the real definition is put
5633 there as well. */
5634 if (hlook->dynindx != -1 && h->dynindx == -1)
5636 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5638 err_free_sym_hash:
5639 free (sorted_sym_hash);
5640 goto error_return;
5644 /* If the real definition is in the list of dynamic
5645 symbols, make sure the weak definition is put
5646 there as well. If we don't do this, then the
5647 dynamic loader might not merge the entries for the
5648 real definition and the weak definition. */
5649 if (h->dynindx != -1 && hlook->dynindx == -1)
5651 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5652 goto err_free_sym_hash;
5654 break;
5659 free (sorted_sym_hash);
5662 if (bed->check_directives
5663 && !(*bed->check_directives) (abfd, info))
5664 return FALSE;
5666 /* If this is a non-traditional link, try to optimize the handling
5667 of the .stab/.stabstr sections. */
5668 if (! dynamic
5669 && ! info->traditional_format
5670 && is_elf_hash_table (htab)
5671 && (info->strip != strip_all && info->strip != strip_debugger))
5673 asection *stabstr;
5675 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5676 if (stabstr != NULL)
5678 bfd_size_type string_offset = 0;
5679 asection *stab;
5681 for (stab = abfd->sections; stab; stab = stab->next)
5682 if (CONST_STRNEQ (stab->name, ".stab")
5683 && (!stab->name[5] ||
5684 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5685 && (stab->flags & SEC_MERGE) == 0
5686 && !bfd_is_abs_section (stab->output_section))
5688 struct bfd_elf_section_data *secdata;
5690 secdata = elf_section_data (stab);
5691 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5692 stabstr, &secdata->sec_info,
5693 &string_offset))
5694 goto error_return;
5695 if (secdata->sec_info)
5696 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5701 if (dynamic && add_needed)
5703 /* Add this bfd to the loaded list. */
5704 struct elf_link_loaded_list *n;
5706 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5707 if (n == NULL)
5708 goto error_return;
5709 n->abfd = abfd;
5710 n->next = htab->dyn_loaded;
5711 htab->dyn_loaded = n;
5713 if (dynamic && !add_needed
5714 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5715 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5717 return TRUE;
5719 error_free_vers:
5720 free (old_tab);
5721 free (old_strtab);
5722 free (nondeflt_vers);
5723 free (extversym);
5724 error_free_sym:
5725 free (isymbuf);
5726 error_return:
5727 return FALSE;
5730 /* Return the linker hash table entry of a symbol that might be
5731 satisfied by an archive symbol. Return -1 on error. */
5733 struct elf_link_hash_entry *
5734 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5735 struct bfd_link_info *info,
5736 const char *name)
5738 struct elf_link_hash_entry *h;
5739 char *p, *copy;
5740 size_t len, first;
5742 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5743 if (h != NULL)
5744 return h;
5746 /* If this is a default version (the name contains @@), look up the
5747 symbol again with only one `@' as well as without the version.
5748 The effect is that references to the symbol with and without the
5749 version will be matched by the default symbol in the archive. */
5751 p = strchr (name, ELF_VER_CHR);
5752 if (p == NULL || p[1] != ELF_VER_CHR)
5753 return h;
5755 /* First check with only one `@'. */
5756 len = strlen (name);
5757 copy = (char *) bfd_alloc (abfd, len);
5758 if (copy == NULL)
5759 return (struct elf_link_hash_entry *) -1;
5761 first = p - name + 1;
5762 memcpy (copy, name, first);
5763 memcpy (copy + first, name + first + 1, len - first);
5765 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5766 if (h == NULL)
5768 /* We also need to check references to the symbol without the
5769 version. */
5770 copy[first - 1] = '\0';
5771 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5772 FALSE, FALSE, TRUE);
5775 bfd_release (abfd, copy);
5776 return h;
5779 /* Add symbols from an ELF archive file to the linker hash table. We
5780 don't use _bfd_generic_link_add_archive_symbols because we need to
5781 handle versioned symbols.
5783 Fortunately, ELF archive handling is simpler than that done by
5784 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5785 oddities. In ELF, if we find a symbol in the archive map, and the
5786 symbol is currently undefined, we know that we must pull in that
5787 object file.
5789 Unfortunately, we do have to make multiple passes over the symbol
5790 table until nothing further is resolved. */
5792 static bfd_boolean
5793 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5795 symindex c;
5796 unsigned char *included = NULL;
5797 carsym *symdefs;
5798 bfd_boolean loop;
5799 size_t amt;
5800 const struct elf_backend_data *bed;
5801 struct elf_link_hash_entry * (*archive_symbol_lookup)
5802 (bfd *, struct bfd_link_info *, const char *);
5804 if (! bfd_has_map (abfd))
5806 /* An empty archive is a special case. */
5807 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5808 return TRUE;
5809 bfd_set_error (bfd_error_no_armap);
5810 return FALSE;
5813 /* Keep track of all symbols we know to be already defined, and all
5814 files we know to be already included. This is to speed up the
5815 second and subsequent passes. */
5816 c = bfd_ardata (abfd)->symdef_count;
5817 if (c == 0)
5818 return TRUE;
5819 amt = c * sizeof (*included);
5820 included = (unsigned char *) bfd_zmalloc (amt);
5821 if (included == NULL)
5822 return FALSE;
5824 symdefs = bfd_ardata (abfd)->symdefs;
5825 bed = get_elf_backend_data (abfd);
5826 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5830 file_ptr last;
5831 symindex i;
5832 carsym *symdef;
5833 carsym *symdefend;
5835 loop = FALSE;
5836 last = -1;
5838 symdef = symdefs;
5839 symdefend = symdef + c;
5840 for (i = 0; symdef < symdefend; symdef++, i++)
5842 struct elf_link_hash_entry *h;
5843 bfd *element;
5844 struct bfd_link_hash_entry *undefs_tail;
5845 symindex mark;
5847 if (included[i])
5848 continue;
5849 if (symdef->file_offset == last)
5851 included[i] = TRUE;
5852 continue;
5855 h = archive_symbol_lookup (abfd, info, symdef->name);
5856 if (h == (struct elf_link_hash_entry *) -1)
5857 goto error_return;
5859 if (h == NULL)
5860 continue;
5862 if (h->root.type == bfd_link_hash_undefined)
5864 /* If the archive element has already been loaded then one
5865 of the symbols defined by that element might have been
5866 made undefined due to being in a discarded section. */
5867 if (h->indx == -3)
5868 continue;
5870 else if (h->root.type == bfd_link_hash_common)
5872 /* We currently have a common symbol. The archive map contains
5873 a reference to this symbol, so we may want to include it. We
5874 only want to include it however, if this archive element
5875 contains a definition of the symbol, not just another common
5876 declaration of it.
5878 Unfortunately some archivers (including GNU ar) will put
5879 declarations of common symbols into their archive maps, as
5880 well as real definitions, so we cannot just go by the archive
5881 map alone. Instead we must read in the element's symbol
5882 table and check that to see what kind of symbol definition
5883 this is. */
5884 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5885 continue;
5887 else
5889 if (h->root.type != bfd_link_hash_undefweak)
5890 /* Symbol must be defined. Don't check it again. */
5891 included[i] = TRUE;
5892 continue;
5895 /* We need to include this archive member. */
5896 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5897 if (element == NULL)
5898 goto error_return;
5900 if (! bfd_check_format (element, bfd_object))
5901 goto error_return;
5903 undefs_tail = info->hash->undefs_tail;
5905 if (!(*info->callbacks
5906 ->add_archive_element) (info, element, symdef->name, &element))
5907 continue;
5908 if (!bfd_link_add_symbols (element, info))
5909 goto error_return;
5911 /* If there are any new undefined symbols, we need to make
5912 another pass through the archive in order to see whether
5913 they can be defined. FIXME: This isn't perfect, because
5914 common symbols wind up on undefs_tail and because an
5915 undefined symbol which is defined later on in this pass
5916 does not require another pass. This isn't a bug, but it
5917 does make the code less efficient than it could be. */
5918 if (undefs_tail != info->hash->undefs_tail)
5919 loop = TRUE;
5921 /* Look backward to mark all symbols from this object file
5922 which we have already seen in this pass. */
5923 mark = i;
5926 included[mark] = TRUE;
5927 if (mark == 0)
5928 break;
5929 --mark;
5931 while (symdefs[mark].file_offset == symdef->file_offset);
5933 /* We mark subsequent symbols from this object file as we go
5934 on through the loop. */
5935 last = symdef->file_offset;
5938 while (loop);
5940 free (included);
5941 return TRUE;
5943 error_return:
5944 free (included);
5945 return FALSE;
5948 /* Given an ELF BFD, add symbols to the global hash table as
5949 appropriate. */
5951 bfd_boolean
5952 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5954 switch (bfd_get_format (abfd))
5956 case bfd_object:
5957 return elf_link_add_object_symbols (abfd, info);
5958 case bfd_archive:
5959 return elf_link_add_archive_symbols (abfd, info);
5960 default:
5961 bfd_set_error (bfd_error_wrong_format);
5962 return FALSE;
5966 struct hash_codes_info
5968 unsigned long *hashcodes;
5969 bfd_boolean error;
5972 /* This function will be called though elf_link_hash_traverse to store
5973 all hash value of the exported symbols in an array. */
5975 static bfd_boolean
5976 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5978 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5979 const char *name;
5980 unsigned long ha;
5981 char *alc = NULL;
5983 /* Ignore indirect symbols. These are added by the versioning code. */
5984 if (h->dynindx == -1)
5985 return TRUE;
5987 name = h->root.root.string;
5988 if (h->versioned >= versioned)
5990 char *p = strchr (name, ELF_VER_CHR);
5991 if (p != NULL)
5993 alc = (char *) bfd_malloc (p - name + 1);
5994 if (alc == NULL)
5996 inf->error = TRUE;
5997 return FALSE;
5999 memcpy (alc, name, p - name);
6000 alc[p - name] = '\0';
6001 name = alc;
6005 /* Compute the hash value. */
6006 ha = bfd_elf_hash (name);
6008 /* Store the found hash value in the array given as the argument. */
6009 *(inf->hashcodes)++ = ha;
6011 /* And store it in the struct so that we can put it in the hash table
6012 later. */
6013 h->u.elf_hash_value = ha;
6015 free (alc);
6016 return TRUE;
6019 struct collect_gnu_hash_codes
6021 bfd *output_bfd;
6022 const struct elf_backend_data *bed;
6023 unsigned long int nsyms;
6024 unsigned long int maskbits;
6025 unsigned long int *hashcodes;
6026 unsigned long int *hashval;
6027 unsigned long int *indx;
6028 unsigned long int *counts;
6029 bfd_vma *bitmask;
6030 bfd_byte *contents;
6031 bfd_size_type xlat;
6032 long int min_dynindx;
6033 unsigned long int bucketcount;
6034 unsigned long int symindx;
6035 long int local_indx;
6036 long int shift1, shift2;
6037 unsigned long int mask;
6038 bfd_boolean error;
6041 /* This function will be called though elf_link_hash_traverse to store
6042 all hash value of the exported symbols in an array. */
6044 static bfd_boolean
6045 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6048 const char *name;
6049 unsigned long ha;
6050 char *alc = NULL;
6052 /* Ignore indirect symbols. These are added by the versioning code. */
6053 if (h->dynindx == -1)
6054 return TRUE;
6056 /* Ignore also local symbols and undefined symbols. */
6057 if (! (*s->bed->elf_hash_symbol) (h))
6058 return TRUE;
6060 name = h->root.root.string;
6061 if (h->versioned >= versioned)
6063 char *p = strchr (name, ELF_VER_CHR);
6064 if (p != NULL)
6066 alc = (char *) bfd_malloc (p - name + 1);
6067 if (alc == NULL)
6069 s->error = TRUE;
6070 return FALSE;
6072 memcpy (alc, name, p - name);
6073 alc[p - name] = '\0';
6074 name = alc;
6078 /* Compute the hash value. */
6079 ha = bfd_elf_gnu_hash (name);
6081 /* Store the found hash value in the array for compute_bucket_count,
6082 and also for .dynsym reordering purposes. */
6083 s->hashcodes[s->nsyms] = ha;
6084 s->hashval[h->dynindx] = ha;
6085 ++s->nsyms;
6086 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6087 s->min_dynindx = h->dynindx;
6089 free (alc);
6090 return TRUE;
6093 /* This function will be called though elf_link_hash_traverse to do
6094 final dynamic symbol renumbering in case of .gnu.hash.
6095 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6096 to the translation table. */
6098 static bfd_boolean
6099 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6101 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6102 unsigned long int bucket;
6103 unsigned long int val;
6105 /* Ignore indirect symbols. */
6106 if (h->dynindx == -1)
6107 return TRUE;
6109 /* Ignore also local symbols and undefined symbols. */
6110 if (! (*s->bed->elf_hash_symbol) (h))
6112 if (h->dynindx >= s->min_dynindx)
6114 if (s->bed->record_xhash_symbol != NULL)
6116 (*s->bed->record_xhash_symbol) (h, 0);
6117 s->local_indx++;
6119 else
6120 h->dynindx = s->local_indx++;
6122 return TRUE;
6125 bucket = s->hashval[h->dynindx] % s->bucketcount;
6126 val = (s->hashval[h->dynindx] >> s->shift1)
6127 & ((s->maskbits >> s->shift1) - 1);
6128 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6129 s->bitmask[val]
6130 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6131 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6132 if (s->counts[bucket] == 1)
6133 /* Last element terminates the chain. */
6134 val |= 1;
6135 bfd_put_32 (s->output_bfd, val,
6136 s->contents + (s->indx[bucket] - s->symindx) * 4);
6137 --s->counts[bucket];
6138 if (s->bed->record_xhash_symbol != NULL)
6140 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6142 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6144 else
6145 h->dynindx = s->indx[bucket]++;
6146 return TRUE;
6149 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6151 bfd_boolean
6152 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6154 return !(h->forced_local
6155 || h->root.type == bfd_link_hash_undefined
6156 || h->root.type == bfd_link_hash_undefweak
6157 || ((h->root.type == bfd_link_hash_defined
6158 || h->root.type == bfd_link_hash_defweak)
6159 && h->root.u.def.section->output_section == NULL));
6162 /* Array used to determine the number of hash table buckets to use
6163 based on the number of symbols there are. If there are fewer than
6164 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6165 fewer than 37 we use 17 buckets, and so forth. We never use more
6166 than 32771 buckets. */
6168 static const size_t elf_buckets[] =
6170 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6171 16411, 32771, 0
6174 /* Compute bucket count for hashing table. We do not use a static set
6175 of possible tables sizes anymore. Instead we determine for all
6176 possible reasonable sizes of the table the outcome (i.e., the
6177 number of collisions etc) and choose the best solution. The
6178 weighting functions are not too simple to allow the table to grow
6179 without bounds. Instead one of the weighting factors is the size.
6180 Therefore the result is always a good payoff between few collisions
6181 (= short chain lengths) and table size. */
6182 static size_t
6183 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6184 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6185 unsigned long int nsyms,
6186 int gnu_hash)
6188 size_t best_size = 0;
6189 unsigned long int i;
6191 /* We have a problem here. The following code to optimize the table
6192 size requires an integer type with more the 32 bits. If
6193 BFD_HOST_U_64_BIT is set we know about such a type. */
6194 #ifdef BFD_HOST_U_64_BIT
6195 if (info->optimize)
6197 size_t minsize;
6198 size_t maxsize;
6199 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6200 bfd *dynobj = elf_hash_table (info)->dynobj;
6201 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6202 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6203 unsigned long int *counts;
6204 bfd_size_type amt;
6205 unsigned int no_improvement_count = 0;
6207 /* Possible optimization parameters: if we have NSYMS symbols we say
6208 that the hashing table must at least have NSYMS/4 and at most
6209 2*NSYMS buckets. */
6210 minsize = nsyms / 4;
6211 if (minsize == 0)
6212 minsize = 1;
6213 best_size = maxsize = nsyms * 2;
6214 if (gnu_hash)
6216 if (minsize < 2)
6217 minsize = 2;
6218 if ((best_size & 31) == 0)
6219 ++best_size;
6222 /* Create array where we count the collisions in. We must use bfd_malloc
6223 since the size could be large. */
6224 amt = maxsize;
6225 amt *= sizeof (unsigned long int);
6226 counts = (unsigned long int *) bfd_malloc (amt);
6227 if (counts == NULL)
6228 return 0;
6230 /* Compute the "optimal" size for the hash table. The criteria is a
6231 minimal chain length. The minor criteria is (of course) the size
6232 of the table. */
6233 for (i = minsize; i < maxsize; ++i)
6235 /* Walk through the array of hashcodes and count the collisions. */
6236 BFD_HOST_U_64_BIT max;
6237 unsigned long int j;
6238 unsigned long int fact;
6240 if (gnu_hash && (i & 31) == 0)
6241 continue;
6243 memset (counts, '\0', i * sizeof (unsigned long int));
6245 /* Determine how often each hash bucket is used. */
6246 for (j = 0; j < nsyms; ++j)
6247 ++counts[hashcodes[j] % i];
6249 /* For the weight function we need some information about the
6250 pagesize on the target. This is information need not be 100%
6251 accurate. Since this information is not available (so far) we
6252 define it here to a reasonable default value. If it is crucial
6253 to have a better value some day simply define this value. */
6254 # ifndef BFD_TARGET_PAGESIZE
6255 # define BFD_TARGET_PAGESIZE (4096)
6256 # endif
6258 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6259 and the chains. */
6260 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6262 # if 1
6263 /* Variant 1: optimize for short chains. We add the squares
6264 of all the chain lengths (which favors many small chain
6265 over a few long chains). */
6266 for (j = 0; j < i; ++j)
6267 max += counts[j] * counts[j];
6269 /* This adds penalties for the overall size of the table. */
6270 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6271 max *= fact * fact;
6272 # else
6273 /* Variant 2: Optimize a lot more for small table. Here we
6274 also add squares of the size but we also add penalties for
6275 empty slots (the +1 term). */
6276 for (j = 0; j < i; ++j)
6277 max += (1 + counts[j]) * (1 + counts[j]);
6279 /* The overall size of the table is considered, but not as
6280 strong as in variant 1, where it is squared. */
6281 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6282 max *= fact;
6283 # endif
6285 /* Compare with current best results. */
6286 if (max < best_chlen)
6288 best_chlen = max;
6289 best_size = i;
6290 no_improvement_count = 0;
6292 /* PR 11843: Avoid futile long searches for the best bucket size
6293 when there are a large number of symbols. */
6294 else if (++no_improvement_count == 100)
6295 break;
6298 free (counts);
6300 else
6301 #endif /* defined (BFD_HOST_U_64_BIT) */
6303 /* This is the fallback solution if no 64bit type is available or if we
6304 are not supposed to spend much time on optimizations. We select the
6305 bucket count using a fixed set of numbers. */
6306 for (i = 0; elf_buckets[i] != 0; i++)
6308 best_size = elf_buckets[i];
6309 if (nsyms < elf_buckets[i + 1])
6310 break;
6312 if (gnu_hash && best_size < 2)
6313 best_size = 2;
6316 return best_size;
6319 /* Size any SHT_GROUP section for ld -r. */
6321 bfd_boolean
6322 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6324 bfd *ibfd;
6325 asection *s;
6327 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6328 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6329 && (s = ibfd->sections) != NULL
6330 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6331 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6332 return FALSE;
6333 return TRUE;
6336 /* Set a default stack segment size. The value in INFO wins. If it
6337 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6338 undefined it is initialized. */
6340 bfd_boolean
6341 bfd_elf_stack_segment_size (bfd *output_bfd,
6342 struct bfd_link_info *info,
6343 const char *legacy_symbol,
6344 bfd_vma default_size)
6346 struct elf_link_hash_entry *h = NULL;
6348 /* Look for legacy symbol. */
6349 if (legacy_symbol)
6350 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6351 FALSE, FALSE, FALSE);
6352 if (h && (h->root.type == bfd_link_hash_defined
6353 || h->root.type == bfd_link_hash_defweak)
6354 && h->def_regular
6355 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6357 /* The symbol has no type if specified on the command line. */
6358 h->type = STT_OBJECT;
6359 if (info->stacksize)
6360 /* xgettext:c-format */
6361 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6362 output_bfd, legacy_symbol);
6363 else if (h->root.u.def.section != bfd_abs_section_ptr)
6364 /* xgettext:c-format */
6365 _bfd_error_handler (_("%pB: %s not absolute"),
6366 output_bfd, legacy_symbol);
6367 else
6368 info->stacksize = h->root.u.def.value;
6371 if (!info->stacksize)
6372 /* If the user didn't set a size, or explicitly inhibit the
6373 size, set it now. */
6374 info->stacksize = default_size;
6376 /* Provide the legacy symbol, if it is referenced. */
6377 if (h && (h->root.type == bfd_link_hash_undefined
6378 || h->root.type == bfd_link_hash_undefweak))
6380 struct bfd_link_hash_entry *bh = NULL;
6382 if (!(_bfd_generic_link_add_one_symbol
6383 (info, output_bfd, legacy_symbol,
6384 BSF_GLOBAL, bfd_abs_section_ptr,
6385 info->stacksize >= 0 ? info->stacksize : 0,
6386 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6387 return FALSE;
6389 h = (struct elf_link_hash_entry *) bh;
6390 h->def_regular = 1;
6391 h->type = STT_OBJECT;
6394 return TRUE;
6397 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6399 struct elf_gc_sweep_symbol_info
6401 struct bfd_link_info *info;
6402 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6403 bfd_boolean);
6406 static bfd_boolean
6407 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6409 if (!h->mark
6410 && (((h->root.type == bfd_link_hash_defined
6411 || h->root.type == bfd_link_hash_defweak)
6412 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6413 && h->root.u.def.section->gc_mark))
6414 || h->root.type == bfd_link_hash_undefined
6415 || h->root.type == bfd_link_hash_undefweak))
6417 struct elf_gc_sweep_symbol_info *inf;
6419 inf = (struct elf_gc_sweep_symbol_info *) data;
6420 (*inf->hide_symbol) (inf->info, h, TRUE);
6421 h->def_regular = 0;
6422 h->ref_regular = 0;
6423 h->ref_regular_nonweak = 0;
6426 return TRUE;
6429 /* Set up the sizes and contents of the ELF dynamic sections. This is
6430 called by the ELF linker emulation before_allocation routine. We
6431 must set the sizes of the sections before the linker sets the
6432 addresses of the various sections. */
6434 bfd_boolean
6435 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6436 const char *soname,
6437 const char *rpath,
6438 const char *filter_shlib,
6439 const char *audit,
6440 const char *depaudit,
6441 const char * const *auxiliary_filters,
6442 struct bfd_link_info *info,
6443 asection **sinterpptr)
6445 bfd *dynobj;
6446 const struct elf_backend_data *bed;
6448 *sinterpptr = NULL;
6450 if (!is_elf_hash_table (info->hash))
6451 return TRUE;
6453 dynobj = elf_hash_table (info)->dynobj;
6455 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6457 struct bfd_elf_version_tree *verdefs;
6458 struct elf_info_failed asvinfo;
6459 struct bfd_elf_version_tree *t;
6460 struct bfd_elf_version_expr *d;
6461 asection *s;
6462 size_t soname_indx;
6464 /* If we are supposed to export all symbols into the dynamic symbol
6465 table (this is not the normal case), then do so. */
6466 if (info->export_dynamic
6467 || (bfd_link_executable (info) && info->dynamic))
6469 struct elf_info_failed eif;
6471 eif.info = info;
6472 eif.failed = FALSE;
6473 elf_link_hash_traverse (elf_hash_table (info),
6474 _bfd_elf_export_symbol,
6475 &eif);
6476 if (eif.failed)
6477 return FALSE;
6480 if (soname != NULL)
6482 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6483 soname, TRUE);
6484 if (soname_indx == (size_t) -1
6485 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6486 return FALSE;
6488 else
6489 soname_indx = (size_t) -1;
6491 /* Make all global versions with definition. */
6492 for (t = info->version_info; t != NULL; t = t->next)
6493 for (d = t->globals.list; d != NULL; d = d->next)
6494 if (!d->symver && d->literal)
6496 const char *verstr, *name;
6497 size_t namelen, verlen, newlen;
6498 char *newname, *p, leading_char;
6499 struct elf_link_hash_entry *newh;
6501 leading_char = bfd_get_symbol_leading_char (output_bfd);
6502 name = d->pattern;
6503 namelen = strlen (name) + (leading_char != '\0');
6504 verstr = t->name;
6505 verlen = strlen (verstr);
6506 newlen = namelen + verlen + 3;
6508 newname = (char *) bfd_malloc (newlen);
6509 if (newname == NULL)
6510 return FALSE;
6511 newname[0] = leading_char;
6512 memcpy (newname + (leading_char != '\0'), name, namelen);
6514 /* Check the hidden versioned definition. */
6515 p = newname + namelen;
6516 *p++ = ELF_VER_CHR;
6517 memcpy (p, verstr, verlen + 1);
6518 newh = elf_link_hash_lookup (elf_hash_table (info),
6519 newname, FALSE, FALSE,
6520 FALSE);
6521 if (newh == NULL
6522 || (newh->root.type != bfd_link_hash_defined
6523 && newh->root.type != bfd_link_hash_defweak))
6525 /* Check the default versioned definition. */
6526 *p++ = ELF_VER_CHR;
6527 memcpy (p, verstr, verlen + 1);
6528 newh = elf_link_hash_lookup (elf_hash_table (info),
6529 newname, FALSE, FALSE,
6530 FALSE);
6532 free (newname);
6534 /* Mark this version if there is a definition and it is
6535 not defined in a shared object. */
6536 if (newh != NULL
6537 && !newh->def_dynamic
6538 && (newh->root.type == bfd_link_hash_defined
6539 || newh->root.type == bfd_link_hash_defweak))
6540 d->symver = 1;
6543 /* Attach all the symbols to their version information. */
6544 asvinfo.info = info;
6545 asvinfo.failed = FALSE;
6547 elf_link_hash_traverse (elf_hash_table (info),
6548 _bfd_elf_link_assign_sym_version,
6549 &asvinfo);
6550 if (asvinfo.failed)
6551 return FALSE;
6553 if (!info->allow_undefined_version)
6555 /* Check if all global versions have a definition. */
6556 bfd_boolean all_defined = TRUE;
6557 for (t = info->version_info; t != NULL; t = t->next)
6558 for (d = t->globals.list; d != NULL; d = d->next)
6559 if (d->literal && !d->symver && !d->script)
6561 _bfd_error_handler
6562 (_("%s: undefined version: %s"),
6563 d->pattern, t->name);
6564 all_defined = FALSE;
6567 if (!all_defined)
6569 bfd_set_error (bfd_error_bad_value);
6570 return FALSE;
6574 /* Set up the version definition section. */
6575 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6576 BFD_ASSERT (s != NULL);
6578 /* We may have created additional version definitions if we are
6579 just linking a regular application. */
6580 verdefs = info->version_info;
6582 /* Skip anonymous version tag. */
6583 if (verdefs != NULL && verdefs->vernum == 0)
6584 verdefs = verdefs->next;
6586 if (verdefs == NULL && !info->create_default_symver)
6587 s->flags |= SEC_EXCLUDE;
6588 else
6590 unsigned int cdefs;
6591 bfd_size_type size;
6592 bfd_byte *p;
6593 Elf_Internal_Verdef def;
6594 Elf_Internal_Verdaux defaux;
6595 struct bfd_link_hash_entry *bh;
6596 struct elf_link_hash_entry *h;
6597 const char *name;
6599 cdefs = 0;
6600 size = 0;
6602 /* Make space for the base version. */
6603 size += sizeof (Elf_External_Verdef);
6604 size += sizeof (Elf_External_Verdaux);
6605 ++cdefs;
6607 /* Make space for the default version. */
6608 if (info->create_default_symver)
6610 size += sizeof (Elf_External_Verdef);
6611 ++cdefs;
6614 for (t = verdefs; t != NULL; t = t->next)
6616 struct bfd_elf_version_deps *n;
6618 /* Don't emit base version twice. */
6619 if (t->vernum == 0)
6620 continue;
6622 size += sizeof (Elf_External_Verdef);
6623 size += sizeof (Elf_External_Verdaux);
6624 ++cdefs;
6626 for (n = t->deps; n != NULL; n = n->next)
6627 size += sizeof (Elf_External_Verdaux);
6630 s->size = size;
6631 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6632 if (s->contents == NULL && s->size != 0)
6633 return FALSE;
6635 /* Fill in the version definition section. */
6637 p = s->contents;
6639 def.vd_version = VER_DEF_CURRENT;
6640 def.vd_flags = VER_FLG_BASE;
6641 def.vd_ndx = 1;
6642 def.vd_cnt = 1;
6643 if (info->create_default_symver)
6645 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6646 def.vd_next = sizeof (Elf_External_Verdef);
6648 else
6650 def.vd_aux = sizeof (Elf_External_Verdef);
6651 def.vd_next = (sizeof (Elf_External_Verdef)
6652 + sizeof (Elf_External_Verdaux));
6655 if (soname_indx != (size_t) -1)
6657 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6658 soname_indx);
6659 def.vd_hash = bfd_elf_hash (soname);
6660 defaux.vda_name = soname_indx;
6661 name = soname;
6663 else
6665 size_t indx;
6667 name = lbasename (bfd_get_filename (output_bfd));
6668 def.vd_hash = bfd_elf_hash (name);
6669 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6670 name, FALSE);
6671 if (indx == (size_t) -1)
6672 return FALSE;
6673 defaux.vda_name = indx;
6675 defaux.vda_next = 0;
6677 _bfd_elf_swap_verdef_out (output_bfd, &def,
6678 (Elf_External_Verdef *) p);
6679 p += sizeof (Elf_External_Verdef);
6680 if (info->create_default_symver)
6682 /* Add a symbol representing this version. */
6683 bh = NULL;
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6686 0, NULL, FALSE,
6687 get_elf_backend_data (dynobj)->collect, &bh)))
6688 return FALSE;
6689 h = (struct elf_link_hash_entry *) bh;
6690 h->non_elf = 0;
6691 h->def_regular = 1;
6692 h->type = STT_OBJECT;
6693 h->verinfo.vertree = NULL;
6695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6696 return FALSE;
6698 /* Create a duplicate of the base version with the same
6699 aux block, but different flags. */
6700 def.vd_flags = 0;
6701 def.vd_ndx = 2;
6702 def.vd_aux = sizeof (Elf_External_Verdef);
6703 if (verdefs)
6704 def.vd_next = (sizeof (Elf_External_Verdef)
6705 + sizeof (Elf_External_Verdaux));
6706 else
6707 def.vd_next = 0;
6708 _bfd_elf_swap_verdef_out (output_bfd, &def,
6709 (Elf_External_Verdef *) p);
6710 p += sizeof (Elf_External_Verdef);
6712 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6713 (Elf_External_Verdaux *) p);
6714 p += sizeof (Elf_External_Verdaux);
6716 for (t = verdefs; t != NULL; t = t->next)
6718 unsigned int cdeps;
6719 struct bfd_elf_version_deps *n;
6721 /* Don't emit the base version twice. */
6722 if (t->vernum == 0)
6723 continue;
6725 cdeps = 0;
6726 for (n = t->deps; n != NULL; n = n->next)
6727 ++cdeps;
6729 /* Add a symbol representing this version. */
6730 bh = NULL;
6731 if (! (_bfd_generic_link_add_one_symbol
6732 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6733 0, NULL, FALSE,
6734 get_elf_backend_data (dynobj)->collect, &bh)))
6735 return FALSE;
6736 h = (struct elf_link_hash_entry *) bh;
6737 h->non_elf = 0;
6738 h->def_regular = 1;
6739 h->type = STT_OBJECT;
6740 h->verinfo.vertree = t;
6742 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6743 return FALSE;
6745 def.vd_version = VER_DEF_CURRENT;
6746 def.vd_flags = 0;
6747 if (t->globals.list == NULL
6748 && t->locals.list == NULL
6749 && ! t->used)
6750 def.vd_flags |= VER_FLG_WEAK;
6751 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6752 def.vd_cnt = cdeps + 1;
6753 def.vd_hash = bfd_elf_hash (t->name);
6754 def.vd_aux = sizeof (Elf_External_Verdef);
6755 def.vd_next = 0;
6757 /* If a basever node is next, it *must* be the last node in
6758 the chain, otherwise Verdef construction breaks. */
6759 if (t->next != NULL && t->next->vernum == 0)
6760 BFD_ASSERT (t->next->next == NULL);
6762 if (t->next != NULL && t->next->vernum != 0)
6763 def.vd_next = (sizeof (Elf_External_Verdef)
6764 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6766 _bfd_elf_swap_verdef_out (output_bfd, &def,
6767 (Elf_External_Verdef *) p);
6768 p += sizeof (Elf_External_Verdef);
6770 defaux.vda_name = h->dynstr_index;
6771 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6772 h->dynstr_index);
6773 defaux.vda_next = 0;
6774 if (t->deps != NULL)
6775 defaux.vda_next = sizeof (Elf_External_Verdaux);
6776 t->name_indx = defaux.vda_name;
6778 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6779 (Elf_External_Verdaux *) p);
6780 p += sizeof (Elf_External_Verdaux);
6782 for (n = t->deps; n != NULL; n = n->next)
6784 if (n->version_needed == NULL)
6786 /* This can happen if there was an error in the
6787 version script. */
6788 defaux.vda_name = 0;
6790 else
6792 defaux.vda_name = n->version_needed->name_indx;
6793 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6794 defaux.vda_name);
6796 if (n->next == NULL)
6797 defaux.vda_next = 0;
6798 else
6799 defaux.vda_next = sizeof (Elf_External_Verdaux);
6801 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6802 (Elf_External_Verdaux *) p);
6803 p += sizeof (Elf_External_Verdaux);
6807 elf_tdata (output_bfd)->cverdefs = cdefs;
6811 bed = get_elf_backend_data (output_bfd);
6813 if (info->gc_sections && bed->can_gc_sections)
6815 struct elf_gc_sweep_symbol_info sweep_info;
6817 /* Remove the symbols that were in the swept sections from the
6818 dynamic symbol table. */
6819 sweep_info.info = info;
6820 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6821 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6822 &sweep_info);
6825 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6827 asection *s;
6828 struct elf_find_verdep_info sinfo;
6830 /* Work out the size of the version reference section. */
6832 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6833 BFD_ASSERT (s != NULL);
6835 sinfo.info = info;
6836 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6837 if (sinfo.vers == 0)
6838 sinfo.vers = 1;
6839 sinfo.failed = FALSE;
6841 elf_link_hash_traverse (elf_hash_table (info),
6842 _bfd_elf_link_find_version_dependencies,
6843 &sinfo);
6844 if (sinfo.failed)
6845 return FALSE;
6847 if (elf_tdata (output_bfd)->verref == NULL)
6848 s->flags |= SEC_EXCLUDE;
6849 else
6851 Elf_Internal_Verneed *vn;
6852 unsigned int size;
6853 unsigned int crefs;
6854 bfd_byte *p;
6856 /* Build the version dependency section. */
6857 size = 0;
6858 crefs = 0;
6859 for (vn = elf_tdata (output_bfd)->verref;
6860 vn != NULL;
6861 vn = vn->vn_nextref)
6863 Elf_Internal_Vernaux *a;
6865 size += sizeof (Elf_External_Verneed);
6866 ++crefs;
6867 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6868 size += sizeof (Elf_External_Vernaux);
6871 s->size = size;
6872 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6873 if (s->contents == NULL)
6874 return FALSE;
6876 p = s->contents;
6877 for (vn = elf_tdata (output_bfd)->verref;
6878 vn != NULL;
6879 vn = vn->vn_nextref)
6881 unsigned int caux;
6882 Elf_Internal_Vernaux *a;
6883 size_t indx;
6885 caux = 0;
6886 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6887 ++caux;
6889 vn->vn_version = VER_NEED_CURRENT;
6890 vn->vn_cnt = caux;
6891 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6892 elf_dt_name (vn->vn_bfd) != NULL
6893 ? elf_dt_name (vn->vn_bfd)
6894 : lbasename (bfd_get_filename
6895 (vn->vn_bfd)),
6896 FALSE);
6897 if (indx == (size_t) -1)
6898 return FALSE;
6899 vn->vn_file = indx;
6900 vn->vn_aux = sizeof (Elf_External_Verneed);
6901 if (vn->vn_nextref == NULL)
6902 vn->vn_next = 0;
6903 else
6904 vn->vn_next = (sizeof (Elf_External_Verneed)
6905 + caux * sizeof (Elf_External_Vernaux));
6907 _bfd_elf_swap_verneed_out (output_bfd, vn,
6908 (Elf_External_Verneed *) p);
6909 p += sizeof (Elf_External_Verneed);
6911 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6913 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6914 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6915 a->vna_nodename, FALSE);
6916 if (indx == (size_t) -1)
6917 return FALSE;
6918 a->vna_name = indx;
6919 if (a->vna_nextptr == NULL)
6920 a->vna_next = 0;
6921 else
6922 a->vna_next = sizeof (Elf_External_Vernaux);
6924 _bfd_elf_swap_vernaux_out (output_bfd, a,
6925 (Elf_External_Vernaux *) p);
6926 p += sizeof (Elf_External_Vernaux);
6930 elf_tdata (output_bfd)->cverrefs = crefs;
6934 /* Any syms created from now on start with -1 in
6935 got.refcount/offset and plt.refcount/offset. */
6936 elf_hash_table (info)->init_got_refcount
6937 = elf_hash_table (info)->init_got_offset;
6938 elf_hash_table (info)->init_plt_refcount
6939 = elf_hash_table (info)->init_plt_offset;
6941 if (bfd_link_relocatable (info)
6942 && !_bfd_elf_size_group_sections (info))
6943 return FALSE;
6945 /* The backend may have to create some sections regardless of whether
6946 we're dynamic or not. */
6947 if (bed->elf_backend_always_size_sections
6948 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6949 return FALSE;
6951 /* Determine any GNU_STACK segment requirements, after the backend
6952 has had a chance to set a default segment size. */
6953 if (info->execstack)
6954 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6955 else if (info->noexecstack)
6956 elf_stack_flags (output_bfd) = PF_R | PF_W;
6957 else
6959 bfd *inputobj;
6960 asection *notesec = NULL;
6961 int exec = 0;
6963 for (inputobj = info->input_bfds;
6964 inputobj;
6965 inputobj = inputobj->link.next)
6967 asection *s;
6969 if (inputobj->flags
6970 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6971 continue;
6972 s = inputobj->sections;
6973 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6974 continue;
6976 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6977 if (s)
6979 if (s->flags & SEC_CODE)
6980 exec = PF_X;
6981 notesec = s;
6983 else if (bed->default_execstack)
6984 exec = PF_X;
6986 if (notesec || info->stacksize > 0)
6987 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6988 if (notesec && exec && bfd_link_relocatable (info)
6989 && notesec->output_section != bfd_abs_section_ptr)
6990 notesec->output_section->flags |= SEC_CODE;
6993 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6995 struct elf_info_failed eif;
6996 struct elf_link_hash_entry *h;
6997 asection *dynstr;
6998 asection *s;
7000 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7001 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7003 if (info->symbolic)
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7006 return FALSE;
7007 info->flags |= DF_SYMBOLIC;
7010 if (rpath != NULL)
7012 size_t indx;
7013 bfd_vma tag;
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7016 TRUE);
7017 if (indx == (size_t) -1)
7018 return FALSE;
7020 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7021 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7022 return FALSE;
7025 if (filter_shlib != NULL)
7027 size_t indx;
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7030 filter_shlib, TRUE);
7031 if (indx == (size_t) -1
7032 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7033 return FALSE;
7036 if (auxiliary_filters != NULL)
7038 const char * const *p;
7040 for (p = auxiliary_filters; *p != NULL; p++)
7042 size_t indx;
7044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7045 *p, TRUE);
7046 if (indx == (size_t) -1
7047 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7048 return FALSE;
7052 if (audit != NULL)
7054 size_t indx;
7056 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7057 TRUE);
7058 if (indx == (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7060 return FALSE;
7063 if (depaudit != NULL)
7065 size_t indx;
7067 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7068 TRUE);
7069 if (indx == (size_t) -1
7070 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7071 return FALSE;
7074 eif.info = info;
7075 eif.failed = FALSE;
7077 /* Find all symbols which were defined in a dynamic object and make
7078 the backend pick a reasonable value for them. */
7079 elf_link_hash_traverse (elf_hash_table (info),
7080 _bfd_elf_adjust_dynamic_symbol,
7081 &eif);
7082 if (eif.failed)
7083 return FALSE;
7085 /* Add some entries to the .dynamic section. We fill in some of the
7086 values later, in bfd_elf_final_link, but we must add the entries
7087 now so that we know the final size of the .dynamic section. */
7089 /* If there are initialization and/or finalization functions to
7090 call then add the corresponding DT_INIT/DT_FINI entries. */
7091 h = (info->init_function
7092 ? elf_link_hash_lookup (elf_hash_table (info),
7093 info->init_function, FALSE,
7094 FALSE, FALSE)
7095 : NULL);
7096 if (h != NULL
7097 && (h->ref_regular
7098 || h->def_regular))
7100 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7101 return FALSE;
7103 h = (info->fini_function
7104 ? elf_link_hash_lookup (elf_hash_table (info),
7105 info->fini_function, FALSE,
7106 FALSE, FALSE)
7107 : NULL);
7108 if (h != NULL
7109 && (h->ref_regular
7110 || h->def_regular))
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7113 return FALSE;
7116 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7117 if (s != NULL && s->linker_has_input)
7119 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7120 if (! bfd_link_executable (info))
7122 bfd *sub;
7123 asection *o;
7125 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7126 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7127 && (o = sub->sections) != NULL
7128 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7129 for (o = sub->sections; o != NULL; o = o->next)
7130 if (elf_section_data (o)->this_hdr.sh_type
7131 == SHT_PREINIT_ARRAY)
7133 _bfd_error_handler
7134 (_("%pB: .preinit_array section is not allowed in DSO"),
7135 sub);
7136 break;
7139 bfd_set_error (bfd_error_nonrepresentable_section);
7140 return FALSE;
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7144 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7145 return FALSE;
7147 s = bfd_get_section_by_name (output_bfd, ".init_array");
7148 if (s != NULL && s->linker_has_input)
7150 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7151 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7152 return FALSE;
7154 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7155 if (s != NULL && s->linker_has_input)
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7159 return FALSE;
7162 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7163 /* If .dynstr is excluded from the link, we don't want any of
7164 these tags. Strictly, we should be checking each section
7165 individually; This quick check covers for the case where
7166 someone does a /DISCARD/ : { *(*) }. */
7167 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7169 bfd_size_type strsize;
7171 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7172 if ((info->emit_hash
7173 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7174 || (info->emit_gnu_hash
7175 && (bed->record_xhash_symbol == NULL
7176 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7177 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7178 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7180 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7181 bed->s->sizeof_sym)
7182 || (info->gnu_flags_1
7183 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7184 info->gnu_flags_1)))
7185 return FALSE;
7189 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7190 return FALSE;
7192 /* The backend must work out the sizes of all the other dynamic
7193 sections. */
7194 if (dynobj != NULL
7195 && bed->elf_backend_size_dynamic_sections != NULL
7196 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7197 return FALSE;
7199 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7201 if (elf_tdata (output_bfd)->cverdefs)
7203 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7205 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7206 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7207 return FALSE;
7210 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7212 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7213 return FALSE;
7215 else if (info->flags & DF_BIND_NOW)
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7218 return FALSE;
7221 if (info->flags_1)
7223 if (bfd_link_executable (info))
7224 info->flags_1 &= ~ (DF_1_INITFIRST
7225 | DF_1_NODELETE
7226 | DF_1_NOOPEN);
7227 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7228 return FALSE;
7231 if (elf_tdata (output_bfd)->cverrefs)
7233 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7235 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7236 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7237 return FALSE;
7240 if ((elf_tdata (output_bfd)->cverrefs == 0
7241 && elf_tdata (output_bfd)->cverdefs == 0)
7242 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7244 asection *s;
7246 s = bfd_get_linker_section (dynobj, ".gnu.version");
7247 s->flags |= SEC_EXCLUDE;
7250 return TRUE;
7253 /* Find the first non-excluded output section. We'll use its
7254 section symbol for some emitted relocs. */
7255 void
7256 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7258 asection *s;
7259 asection *found = NULL;
7261 for (s = output_bfd->sections; s != NULL; s = s->next)
7262 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7263 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7265 found = s;
7266 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7267 break;
7269 elf_hash_table (info)->text_index_section = found;
7272 /* Find two non-excluded output sections, one for code, one for data.
7273 We'll use their section symbols for some emitted relocs. */
7274 void
7275 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7277 asection *s;
7278 asection *found = NULL;
7280 /* Data first, since setting text_index_section changes
7281 _bfd_elf_omit_section_dynsym_default. */
7282 for (s = output_bfd->sections; s != NULL; s = s->next)
7283 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7284 && !(s->flags & SEC_READONLY)
7285 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7287 found = s;
7288 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7289 break;
7291 elf_hash_table (info)->data_index_section = found;
7293 for (s = output_bfd->sections; s != NULL; s = s->next)
7294 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7295 && (s->flags & SEC_READONLY)
7296 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7298 found = s;
7299 break;
7301 elf_hash_table (info)->text_index_section = found;
7304 #define GNU_HASH_SECTION_NAME(bed) \
7305 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7307 bfd_boolean
7308 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7310 const struct elf_backend_data *bed;
7311 unsigned long section_sym_count;
7312 bfd_size_type dynsymcount = 0;
7314 if (!is_elf_hash_table (info->hash))
7315 return TRUE;
7317 bed = get_elf_backend_data (output_bfd);
7318 (*bed->elf_backend_init_index_section) (output_bfd, info);
7320 /* Assign dynsym indices. In a shared library we generate a section
7321 symbol for each output section, which come first. Next come all
7322 of the back-end allocated local dynamic syms, followed by the rest
7323 of the global symbols.
7325 This is usually not needed for static binaries, however backends
7326 can request to always do it, e.g. the MIPS backend uses dynamic
7327 symbol counts to lay out GOT, which will be produced in the
7328 presence of GOT relocations even in static binaries (holding fixed
7329 data in that case, to satisfy those relocations). */
7331 if (elf_hash_table (info)->dynamic_sections_created
7332 || bed->always_renumber_dynsyms)
7333 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7334 &section_sym_count);
7336 if (elf_hash_table (info)->dynamic_sections_created)
7338 bfd *dynobj;
7339 asection *s;
7340 unsigned int dtagcount;
7342 dynobj = elf_hash_table (info)->dynobj;
7344 /* Work out the size of the symbol version section. */
7345 s = bfd_get_linker_section (dynobj, ".gnu.version");
7346 BFD_ASSERT (s != NULL);
7347 if ((s->flags & SEC_EXCLUDE) == 0)
7349 s->size = dynsymcount * sizeof (Elf_External_Versym);
7350 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7351 if (s->contents == NULL)
7352 return FALSE;
7354 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7355 return FALSE;
7358 /* Set the size of the .dynsym and .hash sections. We counted
7359 the number of dynamic symbols in elf_link_add_object_symbols.
7360 We will build the contents of .dynsym and .hash when we build
7361 the final symbol table, because until then we do not know the
7362 correct value to give the symbols. We built the .dynstr
7363 section as we went along in elf_link_add_object_symbols. */
7364 s = elf_hash_table (info)->dynsym;
7365 BFD_ASSERT (s != NULL);
7366 s->size = dynsymcount * bed->s->sizeof_sym;
7368 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7369 if (s->contents == NULL)
7370 return FALSE;
7372 /* The first entry in .dynsym is a dummy symbol. Clear all the
7373 section syms, in case we don't output them all. */
7374 ++section_sym_count;
7375 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7377 elf_hash_table (info)->bucketcount = 0;
7379 /* Compute the size of the hashing table. As a side effect this
7380 computes the hash values for all the names we export. */
7381 if (info->emit_hash)
7383 unsigned long int *hashcodes;
7384 struct hash_codes_info hashinf;
7385 bfd_size_type amt;
7386 unsigned long int nsyms;
7387 size_t bucketcount;
7388 size_t hash_entry_size;
7390 /* Compute the hash values for all exported symbols. At the same
7391 time store the values in an array so that we could use them for
7392 optimizations. */
7393 amt = dynsymcount * sizeof (unsigned long int);
7394 hashcodes = (unsigned long int *) bfd_malloc (amt);
7395 if (hashcodes == NULL)
7396 return FALSE;
7397 hashinf.hashcodes = hashcodes;
7398 hashinf.error = FALSE;
7400 /* Put all hash values in HASHCODES. */
7401 elf_link_hash_traverse (elf_hash_table (info),
7402 elf_collect_hash_codes, &hashinf);
7403 if (hashinf.error)
7405 free (hashcodes);
7406 return FALSE;
7409 nsyms = hashinf.hashcodes - hashcodes;
7410 bucketcount
7411 = compute_bucket_count (info, hashcodes, nsyms, 0);
7412 free (hashcodes);
7414 if (bucketcount == 0 && nsyms > 0)
7415 return FALSE;
7417 elf_hash_table (info)->bucketcount = bucketcount;
7419 s = bfd_get_linker_section (dynobj, ".hash");
7420 BFD_ASSERT (s != NULL);
7421 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7422 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7423 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7424 if (s->contents == NULL)
7425 return FALSE;
7427 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7428 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7429 s->contents + hash_entry_size);
7432 if (info->emit_gnu_hash)
7434 size_t i, cnt;
7435 unsigned char *contents;
7436 struct collect_gnu_hash_codes cinfo;
7437 bfd_size_type amt;
7438 size_t bucketcount;
7440 memset (&cinfo, 0, sizeof (cinfo));
7442 /* Compute the hash values for all exported symbols. At the same
7443 time store the values in an array so that we could use them for
7444 optimizations. */
7445 amt = dynsymcount * 2 * sizeof (unsigned long int);
7446 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7447 if (cinfo.hashcodes == NULL)
7448 return FALSE;
7450 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7451 cinfo.min_dynindx = -1;
7452 cinfo.output_bfd = output_bfd;
7453 cinfo.bed = bed;
7455 /* Put all hash values in HASHCODES. */
7456 elf_link_hash_traverse (elf_hash_table (info),
7457 elf_collect_gnu_hash_codes, &cinfo);
7458 if (cinfo.error)
7460 free (cinfo.hashcodes);
7461 return FALSE;
7464 bucketcount
7465 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7467 if (bucketcount == 0)
7469 free (cinfo.hashcodes);
7470 return FALSE;
7473 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7474 BFD_ASSERT (s != NULL);
7476 if (cinfo.nsyms == 0)
7478 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7479 BFD_ASSERT (cinfo.min_dynindx == -1);
7480 free (cinfo.hashcodes);
7481 s->size = 5 * 4 + bed->s->arch_size / 8;
7482 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7483 if (contents == NULL)
7484 return FALSE;
7485 s->contents = contents;
7486 /* 1 empty bucket. */
7487 bfd_put_32 (output_bfd, 1, contents);
7488 /* SYMIDX above the special symbol 0. */
7489 bfd_put_32 (output_bfd, 1, contents + 4);
7490 /* Just one word for bitmask. */
7491 bfd_put_32 (output_bfd, 1, contents + 8);
7492 /* Only hash fn bloom filter. */
7493 bfd_put_32 (output_bfd, 0, contents + 12);
7494 /* No hashes are valid - empty bitmask. */
7495 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7496 /* No hashes in the only bucket. */
7497 bfd_put_32 (output_bfd, 0,
7498 contents + 16 + bed->s->arch_size / 8);
7500 else
7502 unsigned long int maskwords, maskbitslog2, x;
7503 BFD_ASSERT (cinfo.min_dynindx != -1);
7505 x = cinfo.nsyms;
7506 maskbitslog2 = 1;
7507 while ((x >>= 1) != 0)
7508 ++maskbitslog2;
7509 if (maskbitslog2 < 3)
7510 maskbitslog2 = 5;
7511 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7512 maskbitslog2 = maskbitslog2 + 3;
7513 else
7514 maskbitslog2 = maskbitslog2 + 2;
7515 if (bed->s->arch_size == 64)
7517 if (maskbitslog2 == 5)
7518 maskbitslog2 = 6;
7519 cinfo.shift1 = 6;
7521 else
7522 cinfo.shift1 = 5;
7523 cinfo.mask = (1 << cinfo.shift1) - 1;
7524 cinfo.shift2 = maskbitslog2;
7525 cinfo.maskbits = 1 << maskbitslog2;
7526 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7527 amt = bucketcount * sizeof (unsigned long int) * 2;
7528 amt += maskwords * sizeof (bfd_vma);
7529 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7530 if (cinfo.bitmask == NULL)
7532 free (cinfo.hashcodes);
7533 return FALSE;
7536 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7537 cinfo.indx = cinfo.counts + bucketcount;
7538 cinfo.symindx = dynsymcount - cinfo.nsyms;
7539 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7541 /* Determine how often each hash bucket is used. */
7542 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7543 for (i = 0; i < cinfo.nsyms; ++i)
7544 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7546 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7547 if (cinfo.counts[i] != 0)
7549 cinfo.indx[i] = cnt;
7550 cnt += cinfo.counts[i];
7552 BFD_ASSERT (cnt == dynsymcount);
7553 cinfo.bucketcount = bucketcount;
7554 cinfo.local_indx = cinfo.min_dynindx;
7556 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7557 s->size += cinfo.maskbits / 8;
7558 if (bed->record_xhash_symbol != NULL)
7559 s->size += cinfo.nsyms * 4;
7560 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7561 if (contents == NULL)
7563 free (cinfo.bitmask);
7564 free (cinfo.hashcodes);
7565 return FALSE;
7568 s->contents = contents;
7569 bfd_put_32 (output_bfd, bucketcount, contents);
7570 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7571 bfd_put_32 (output_bfd, maskwords, contents + 8);
7572 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7573 contents += 16 + cinfo.maskbits / 8;
7575 for (i = 0; i < bucketcount; ++i)
7577 if (cinfo.counts[i] == 0)
7578 bfd_put_32 (output_bfd, 0, contents);
7579 else
7580 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7581 contents += 4;
7584 cinfo.contents = contents;
7586 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7587 /* Renumber dynamic symbols, if populating .gnu.hash section.
7588 If using .MIPS.xhash, populate the translation table. */
7589 elf_link_hash_traverse (elf_hash_table (info),
7590 elf_gnu_hash_process_symidx, &cinfo);
7592 contents = s->contents + 16;
7593 for (i = 0; i < maskwords; ++i)
7595 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7596 contents);
7597 contents += bed->s->arch_size / 8;
7600 free (cinfo.bitmask);
7601 free (cinfo.hashcodes);
7605 s = bfd_get_linker_section (dynobj, ".dynstr");
7606 BFD_ASSERT (s != NULL);
7608 elf_finalize_dynstr (output_bfd, info);
7610 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7612 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7613 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7614 return FALSE;
7617 return TRUE;
7620 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7622 static void
7623 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7624 asection *sec)
7626 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7627 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7630 /* Finish SHF_MERGE section merging. */
7632 bfd_boolean
7633 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7635 bfd *ibfd;
7636 asection *sec;
7638 if (!is_elf_hash_table (info->hash))
7639 return FALSE;
7641 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7642 if ((ibfd->flags & DYNAMIC) == 0
7643 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7644 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7645 == get_elf_backend_data (obfd)->s->elfclass))
7646 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7647 if ((sec->flags & SEC_MERGE) != 0
7648 && !bfd_is_abs_section (sec->output_section))
7650 struct bfd_elf_section_data *secdata;
7652 secdata = elf_section_data (sec);
7653 if (! _bfd_add_merge_section (obfd,
7654 &elf_hash_table (info)->merge_info,
7655 sec, &secdata->sec_info))
7656 return FALSE;
7657 else if (secdata->sec_info)
7658 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7661 if (elf_hash_table (info)->merge_info != NULL)
7662 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7663 merge_sections_remove_hook);
7664 return TRUE;
7667 /* Create an entry in an ELF linker hash table. */
7669 struct bfd_hash_entry *
7670 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7671 struct bfd_hash_table *table,
7672 const char *string)
7674 /* Allocate the structure if it has not already been allocated by a
7675 subclass. */
7676 if (entry == NULL)
7678 entry = (struct bfd_hash_entry *)
7679 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7680 if (entry == NULL)
7681 return entry;
7684 /* Call the allocation method of the superclass. */
7685 entry = _bfd_link_hash_newfunc (entry, table, string);
7686 if (entry != NULL)
7688 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7689 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7691 /* Set local fields. */
7692 ret->indx = -1;
7693 ret->dynindx = -1;
7694 ret->got = htab->init_got_refcount;
7695 ret->plt = htab->init_plt_refcount;
7696 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7697 - offsetof (struct elf_link_hash_entry, size)));
7698 /* Assume that we have been called by a non-ELF symbol reader.
7699 This flag is then reset by the code which reads an ELF input
7700 file. This ensures that a symbol created by a non-ELF symbol
7701 reader will have the flag set correctly. */
7702 ret->non_elf = 1;
7705 return entry;
7708 /* Copy data from an indirect symbol to its direct symbol, hiding the
7709 old indirect symbol. Also used for copying flags to a weakdef. */
7711 void
7712 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7713 struct elf_link_hash_entry *dir,
7714 struct elf_link_hash_entry *ind)
7716 struct elf_link_hash_table *htab;
7718 if (ind->dyn_relocs != NULL)
7720 if (dir->dyn_relocs != NULL)
7722 struct elf_dyn_relocs **pp;
7723 struct elf_dyn_relocs *p;
7725 /* Add reloc counts against the indirect sym to the direct sym
7726 list. Merge any entries against the same section. */
7727 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7729 struct elf_dyn_relocs *q;
7731 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7732 if (q->sec == p->sec)
7734 q->pc_count += p->pc_count;
7735 q->count += p->count;
7736 *pp = p->next;
7737 break;
7739 if (q == NULL)
7740 pp = &p->next;
7742 *pp = dir->dyn_relocs;
7745 dir->dyn_relocs = ind->dyn_relocs;
7746 ind->dyn_relocs = NULL;
7749 /* Copy down any references that we may have already seen to the
7750 symbol which just became indirect. */
7752 if (dir->versioned != versioned_hidden)
7753 dir->ref_dynamic |= ind->ref_dynamic;
7754 dir->ref_regular |= ind->ref_regular;
7755 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7756 dir->non_got_ref |= ind->non_got_ref;
7757 dir->needs_plt |= ind->needs_plt;
7758 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7760 if (ind->root.type != bfd_link_hash_indirect)
7761 return;
7763 /* Copy over the global and procedure linkage table refcount entries.
7764 These may have been already set up by a check_relocs routine. */
7765 htab = elf_hash_table (info);
7766 if (ind->got.refcount > htab->init_got_refcount.refcount)
7768 if (dir->got.refcount < 0)
7769 dir->got.refcount = 0;
7770 dir->got.refcount += ind->got.refcount;
7771 ind->got.refcount = htab->init_got_refcount.refcount;
7774 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7776 if (dir->plt.refcount < 0)
7777 dir->plt.refcount = 0;
7778 dir->plt.refcount += ind->plt.refcount;
7779 ind->plt.refcount = htab->init_plt_refcount.refcount;
7782 if (ind->dynindx != -1)
7784 if (dir->dynindx != -1)
7785 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7786 dir->dynindx = ind->dynindx;
7787 dir->dynstr_index = ind->dynstr_index;
7788 ind->dynindx = -1;
7789 ind->dynstr_index = 0;
7793 void
7794 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7795 struct elf_link_hash_entry *h,
7796 bfd_boolean force_local)
7798 /* STT_GNU_IFUNC symbol must go through PLT. */
7799 if (h->type != STT_GNU_IFUNC)
7801 h->plt = elf_hash_table (info)->init_plt_offset;
7802 h->needs_plt = 0;
7804 if (force_local)
7806 h->forced_local = 1;
7807 if (h->dynindx != -1)
7809 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7810 h->dynstr_index);
7811 h->dynindx = -1;
7812 h->dynstr_index = 0;
7817 /* Hide a symbol. */
7819 void
7820 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7821 struct bfd_link_info *info,
7822 struct bfd_link_hash_entry *h)
7824 if (is_elf_hash_table (info->hash))
7826 const struct elf_backend_data *bed
7827 = get_elf_backend_data (output_bfd);
7828 struct elf_link_hash_entry *eh
7829 = (struct elf_link_hash_entry *) h;
7830 bed->elf_backend_hide_symbol (info, eh, TRUE);
7831 eh->def_dynamic = 0;
7832 eh->ref_dynamic = 0;
7833 eh->dynamic_def = 0;
7837 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7838 caller. */
7840 bfd_boolean
7841 _bfd_elf_link_hash_table_init
7842 (struct elf_link_hash_table *table,
7843 bfd *abfd,
7844 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7845 struct bfd_hash_table *,
7846 const char *),
7847 unsigned int entsize,
7848 enum elf_target_id target_id)
7850 bfd_boolean ret;
7851 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7853 table->init_got_refcount.refcount = can_refcount - 1;
7854 table->init_plt_refcount.refcount = can_refcount - 1;
7855 table->init_got_offset.offset = -(bfd_vma) 1;
7856 table->init_plt_offset.offset = -(bfd_vma) 1;
7857 /* The first dynamic symbol is a dummy. */
7858 table->dynsymcount = 1;
7860 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7862 table->root.type = bfd_link_elf_hash_table;
7863 table->hash_table_id = target_id;
7864 table->target_os = get_elf_backend_data (abfd)->target_os;
7866 return ret;
7869 /* Create an ELF linker hash table. */
7871 struct bfd_link_hash_table *
7872 _bfd_elf_link_hash_table_create (bfd *abfd)
7874 struct elf_link_hash_table *ret;
7875 size_t amt = sizeof (struct elf_link_hash_table);
7877 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7878 if (ret == NULL)
7879 return NULL;
7881 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7882 sizeof (struct elf_link_hash_entry),
7883 GENERIC_ELF_DATA))
7885 free (ret);
7886 return NULL;
7888 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7890 return &ret->root;
7893 /* Destroy an ELF linker hash table. */
7895 void
7896 _bfd_elf_link_hash_table_free (bfd *obfd)
7898 struct elf_link_hash_table *htab;
7900 htab = (struct elf_link_hash_table *) obfd->link.hash;
7901 if (htab->dynstr != NULL)
7902 _bfd_elf_strtab_free (htab->dynstr);
7903 _bfd_merge_sections_free (htab->merge_info);
7904 _bfd_generic_link_hash_table_free (obfd);
7907 /* This is a hook for the ELF emulation code in the generic linker to
7908 tell the backend linker what file name to use for the DT_NEEDED
7909 entry for a dynamic object. */
7911 void
7912 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7914 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7915 && bfd_get_format (abfd) == bfd_object)
7916 elf_dt_name (abfd) = name;
7920 bfd_elf_get_dyn_lib_class (bfd *abfd)
7922 int lib_class;
7923 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7924 && bfd_get_format (abfd) == bfd_object)
7925 lib_class = elf_dyn_lib_class (abfd);
7926 else
7927 lib_class = 0;
7928 return lib_class;
7931 void
7932 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7934 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7935 && bfd_get_format (abfd) == bfd_object)
7936 elf_dyn_lib_class (abfd) = lib_class;
7939 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7940 the linker ELF emulation code. */
7942 struct bfd_link_needed_list *
7943 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7944 struct bfd_link_info *info)
7946 if (! is_elf_hash_table (info->hash))
7947 return NULL;
7948 return elf_hash_table (info)->needed;
7951 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7952 hook for the linker ELF emulation code. */
7954 struct bfd_link_needed_list *
7955 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7956 struct bfd_link_info *info)
7958 if (! is_elf_hash_table (info->hash))
7959 return NULL;
7960 return elf_hash_table (info)->runpath;
7963 /* Get the name actually used for a dynamic object for a link. This
7964 is the SONAME entry if there is one. Otherwise, it is the string
7965 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7967 const char *
7968 bfd_elf_get_dt_soname (bfd *abfd)
7970 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7971 && bfd_get_format (abfd) == bfd_object)
7972 return elf_dt_name (abfd);
7973 return NULL;
7976 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7977 the ELF linker emulation code. */
7979 bfd_boolean
7980 bfd_elf_get_bfd_needed_list (bfd *abfd,
7981 struct bfd_link_needed_list **pneeded)
7983 asection *s;
7984 bfd_byte *dynbuf = NULL;
7985 unsigned int elfsec;
7986 unsigned long shlink;
7987 bfd_byte *extdyn, *extdynend;
7988 size_t extdynsize;
7989 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7991 *pneeded = NULL;
7993 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7994 || bfd_get_format (abfd) != bfd_object)
7995 return TRUE;
7997 s = bfd_get_section_by_name (abfd, ".dynamic");
7998 if (s == NULL || s->size == 0)
7999 return TRUE;
8001 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8002 goto error_return;
8004 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8005 if (elfsec == SHN_BAD)
8006 goto error_return;
8008 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8010 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8011 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8013 extdyn = dynbuf;
8014 extdynend = extdyn + s->size;
8015 for (; extdyn < extdynend; extdyn += extdynsize)
8017 Elf_Internal_Dyn dyn;
8019 (*swap_dyn_in) (abfd, extdyn, &dyn);
8021 if (dyn.d_tag == DT_NULL)
8022 break;
8024 if (dyn.d_tag == DT_NEEDED)
8026 const char *string;
8027 struct bfd_link_needed_list *l;
8028 unsigned int tagv = dyn.d_un.d_val;
8029 size_t amt;
8031 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8032 if (string == NULL)
8033 goto error_return;
8035 amt = sizeof *l;
8036 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8037 if (l == NULL)
8038 goto error_return;
8040 l->by = abfd;
8041 l->name = string;
8042 l->next = *pneeded;
8043 *pneeded = l;
8047 free (dynbuf);
8049 return TRUE;
8051 error_return:
8052 free (dynbuf);
8053 return FALSE;
8056 struct elf_symbuf_symbol
8058 unsigned long st_name; /* Symbol name, index in string tbl */
8059 unsigned char st_info; /* Type and binding attributes */
8060 unsigned char st_other; /* Visibilty, and target specific */
8063 struct elf_symbuf_head
8065 struct elf_symbuf_symbol *ssym;
8066 size_t count;
8067 unsigned int st_shndx;
8070 struct elf_symbol
8072 union
8074 Elf_Internal_Sym *isym;
8075 struct elf_symbuf_symbol *ssym;
8076 void *p;
8077 } u;
8078 const char *name;
8081 /* Sort references to symbols by ascending section number. */
8083 static int
8084 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8086 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8087 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8089 if (s1->st_shndx != s2->st_shndx)
8090 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8091 /* Final sort by the address of the sym in the symbuf ensures
8092 a stable sort. */
8093 if (s1 != s2)
8094 return s1 > s2 ? 1 : -1;
8095 return 0;
8098 static int
8099 elf_sym_name_compare (const void *arg1, const void *arg2)
8101 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8102 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8103 int ret = strcmp (s1->name, s2->name);
8104 if (ret != 0)
8105 return ret;
8106 if (s1->u.p != s2->u.p)
8107 return s1->u.p > s2->u.p ? 1 : -1;
8108 return 0;
8111 static struct elf_symbuf_head *
8112 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8114 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8115 struct elf_symbuf_symbol *ssym;
8116 struct elf_symbuf_head *ssymbuf, *ssymhead;
8117 size_t i, shndx_count, total_size, amt;
8119 amt = symcount * sizeof (*indbuf);
8120 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8121 if (indbuf == NULL)
8122 return NULL;
8124 for (ind = indbuf, i = 0; i < symcount; i++)
8125 if (isymbuf[i].st_shndx != SHN_UNDEF)
8126 *ind++ = &isymbuf[i];
8127 indbufend = ind;
8129 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8130 elf_sort_elf_symbol);
8132 shndx_count = 0;
8133 if (indbufend > indbuf)
8134 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8135 if (ind[0]->st_shndx != ind[1]->st_shndx)
8136 shndx_count++;
8138 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8139 + (indbufend - indbuf) * sizeof (*ssym));
8140 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8141 if (ssymbuf == NULL)
8143 free (indbuf);
8144 return NULL;
8147 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8148 ssymbuf->ssym = NULL;
8149 ssymbuf->count = shndx_count;
8150 ssymbuf->st_shndx = 0;
8151 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8153 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8155 ssymhead++;
8156 ssymhead->ssym = ssym;
8157 ssymhead->count = 0;
8158 ssymhead->st_shndx = (*ind)->st_shndx;
8160 ssym->st_name = (*ind)->st_name;
8161 ssym->st_info = (*ind)->st_info;
8162 ssym->st_other = (*ind)->st_other;
8163 ssymhead->count++;
8165 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8166 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8167 == total_size));
8169 free (indbuf);
8170 return ssymbuf;
8173 /* Check if 2 sections define the same set of local and global
8174 symbols. */
8176 static bfd_boolean
8177 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8178 struct bfd_link_info *info)
8180 bfd *bfd1, *bfd2;
8181 const struct elf_backend_data *bed1, *bed2;
8182 Elf_Internal_Shdr *hdr1, *hdr2;
8183 size_t symcount1, symcount2;
8184 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8185 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8186 Elf_Internal_Sym *isym, *isymend;
8187 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8188 size_t count1, count2, i;
8189 unsigned int shndx1, shndx2;
8190 bfd_boolean result;
8192 bfd1 = sec1->owner;
8193 bfd2 = sec2->owner;
8195 /* Both sections have to be in ELF. */
8196 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8197 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8198 return FALSE;
8200 if (elf_section_type (sec1) != elf_section_type (sec2))
8201 return FALSE;
8203 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8204 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8205 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8206 return FALSE;
8208 bed1 = get_elf_backend_data (bfd1);
8209 bed2 = get_elf_backend_data (bfd2);
8210 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8211 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8212 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8213 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8215 if (symcount1 == 0 || symcount2 == 0)
8216 return FALSE;
8218 result = FALSE;
8219 isymbuf1 = NULL;
8220 isymbuf2 = NULL;
8221 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8222 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8224 if (ssymbuf1 == NULL)
8226 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8227 NULL, NULL, NULL);
8228 if (isymbuf1 == NULL)
8229 goto done;
8231 if (info != NULL && !info->reduce_memory_overheads)
8233 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8234 elf_tdata (bfd1)->symbuf = ssymbuf1;
8238 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8240 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8241 NULL, NULL, NULL);
8242 if (isymbuf2 == NULL)
8243 goto done;
8245 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8247 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8248 elf_tdata (bfd2)->symbuf = ssymbuf2;
8252 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8254 /* Optimized faster version. */
8255 size_t lo, hi, mid;
8256 struct elf_symbol *symp;
8257 struct elf_symbuf_symbol *ssym, *ssymend;
8259 lo = 0;
8260 hi = ssymbuf1->count;
8261 ssymbuf1++;
8262 count1 = 0;
8263 while (lo < hi)
8265 mid = (lo + hi) / 2;
8266 if (shndx1 < ssymbuf1[mid].st_shndx)
8267 hi = mid;
8268 else if (shndx1 > ssymbuf1[mid].st_shndx)
8269 lo = mid + 1;
8270 else
8272 count1 = ssymbuf1[mid].count;
8273 ssymbuf1 += mid;
8274 break;
8278 lo = 0;
8279 hi = ssymbuf2->count;
8280 ssymbuf2++;
8281 count2 = 0;
8282 while (lo < hi)
8284 mid = (lo + hi) / 2;
8285 if (shndx2 < ssymbuf2[mid].st_shndx)
8286 hi = mid;
8287 else if (shndx2 > ssymbuf2[mid].st_shndx)
8288 lo = mid + 1;
8289 else
8291 count2 = ssymbuf2[mid].count;
8292 ssymbuf2 += mid;
8293 break;
8297 if (count1 == 0 || count2 == 0 || count1 != count2)
8298 goto done;
8300 symtable1
8301 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8302 symtable2
8303 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8304 if (symtable1 == NULL || symtable2 == NULL)
8305 goto done;
8307 symp = symtable1;
8308 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8309 ssym < ssymend; ssym++, symp++)
8311 symp->u.ssym = ssym;
8312 symp->name = bfd_elf_string_from_elf_section (bfd1,
8313 hdr1->sh_link,
8314 ssym->st_name);
8317 symp = symtable2;
8318 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8319 ssym < ssymend; ssym++, symp++)
8321 symp->u.ssym = ssym;
8322 symp->name = bfd_elf_string_from_elf_section (bfd2,
8323 hdr2->sh_link,
8324 ssym->st_name);
8327 /* Sort symbol by name. */
8328 qsort (symtable1, count1, sizeof (struct elf_symbol),
8329 elf_sym_name_compare);
8330 qsort (symtable2, count1, sizeof (struct elf_symbol),
8331 elf_sym_name_compare);
8333 for (i = 0; i < count1; i++)
8334 /* Two symbols must have the same binding, type and name. */
8335 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8336 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8337 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8338 goto done;
8340 result = TRUE;
8341 goto done;
8344 symtable1 = (struct elf_symbol *)
8345 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8346 symtable2 = (struct elf_symbol *)
8347 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8348 if (symtable1 == NULL || symtable2 == NULL)
8349 goto done;
8351 /* Count definitions in the section. */
8352 count1 = 0;
8353 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8354 if (isym->st_shndx == shndx1)
8355 symtable1[count1++].u.isym = isym;
8357 count2 = 0;
8358 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8359 if (isym->st_shndx == shndx2)
8360 symtable2[count2++].u.isym = isym;
8362 if (count1 == 0 || count2 == 0 || count1 != count2)
8363 goto done;
8365 for (i = 0; i < count1; i++)
8366 symtable1[i].name
8367 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8368 symtable1[i].u.isym->st_name);
8370 for (i = 0; i < count2; i++)
8371 symtable2[i].name
8372 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8373 symtable2[i].u.isym->st_name);
8375 /* Sort symbol by name. */
8376 qsort (symtable1, count1, sizeof (struct elf_symbol),
8377 elf_sym_name_compare);
8378 qsort (symtable2, count1, sizeof (struct elf_symbol),
8379 elf_sym_name_compare);
8381 for (i = 0; i < count1; i++)
8382 /* Two symbols must have the same binding, type and name. */
8383 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8384 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8385 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8386 goto done;
8388 result = TRUE;
8390 done:
8391 free (symtable1);
8392 free (symtable2);
8393 free (isymbuf1);
8394 free (isymbuf2);
8396 return result;
8399 /* Return TRUE if 2 section types are compatible. */
8401 bfd_boolean
8402 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8403 bfd *bbfd, const asection *bsec)
8405 if (asec == NULL
8406 || bsec == NULL
8407 || abfd->xvec->flavour != bfd_target_elf_flavour
8408 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8409 return TRUE;
8411 return elf_section_type (asec) == elf_section_type (bsec);
8414 /* Final phase of ELF linker. */
8416 /* A structure we use to avoid passing large numbers of arguments. */
8418 struct elf_final_link_info
8420 /* General link information. */
8421 struct bfd_link_info *info;
8422 /* Output BFD. */
8423 bfd *output_bfd;
8424 /* Symbol string table. */
8425 struct elf_strtab_hash *symstrtab;
8426 /* .hash section. */
8427 asection *hash_sec;
8428 /* symbol version section (.gnu.version). */
8429 asection *symver_sec;
8430 /* Buffer large enough to hold contents of any section. */
8431 bfd_byte *contents;
8432 /* Buffer large enough to hold external relocs of any section. */
8433 void *external_relocs;
8434 /* Buffer large enough to hold internal relocs of any section. */
8435 Elf_Internal_Rela *internal_relocs;
8436 /* Buffer large enough to hold external local symbols of any input
8437 BFD. */
8438 bfd_byte *external_syms;
8439 /* And a buffer for symbol section indices. */
8440 Elf_External_Sym_Shndx *locsym_shndx;
8441 /* Buffer large enough to hold internal local symbols of any input
8442 BFD. */
8443 Elf_Internal_Sym *internal_syms;
8444 /* Array large enough to hold a symbol index for each local symbol
8445 of any input BFD. */
8446 long *indices;
8447 /* Array large enough to hold a section pointer for each local
8448 symbol of any input BFD. */
8449 asection **sections;
8450 /* Buffer for SHT_SYMTAB_SHNDX section. */
8451 Elf_External_Sym_Shndx *symshndxbuf;
8452 /* Number of STT_FILE syms seen. */
8453 size_t filesym_count;
8454 /* Local symbol hash table. */
8455 struct bfd_hash_table local_hash_table;
8458 struct local_hash_entry
8460 /* Base hash table entry structure. */
8461 struct bfd_hash_entry root;
8462 /* Size of the local symbol name. */
8463 size_t size;
8464 /* Number of the duplicated local symbol names. */
8465 long count;
8468 /* Create an entry in the local symbol hash table. */
8470 static struct bfd_hash_entry *
8471 local_hash_newfunc (struct bfd_hash_entry *entry,
8472 struct bfd_hash_table *table,
8473 const char *string)
8476 /* Allocate the structure if it has not already been allocated by a
8477 subclass. */
8478 if (entry == NULL)
8480 entry = bfd_hash_allocate (table,
8481 sizeof (struct local_hash_entry));
8482 if (entry == NULL)
8483 return entry;
8486 /* Call the allocation method of the superclass. */
8487 entry = bfd_hash_newfunc (entry, table, string);
8488 if (entry != NULL)
8490 ((struct local_hash_entry *) entry)->count = 0;
8491 ((struct local_hash_entry *) entry)->size = 0;
8494 return entry;
8497 /* This struct is used to pass information to elf_link_output_extsym. */
8499 struct elf_outext_info
8501 bfd_boolean failed;
8502 bfd_boolean localsyms;
8503 bfd_boolean file_sym_done;
8504 struct elf_final_link_info *flinfo;
8508 /* Support for evaluating a complex relocation.
8510 Complex relocations are generalized, self-describing relocations. The
8511 implementation of them consists of two parts: complex symbols, and the
8512 relocations themselves.
8514 The relocations use a reserved elf-wide relocation type code (R_RELC
8515 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8516 information (start bit, end bit, word width, etc) into the addend. This
8517 information is extracted from CGEN-generated operand tables within gas.
8519 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8520 internal) representing prefix-notation expressions, including but not
8521 limited to those sorts of expressions normally encoded as addends in the
8522 addend field. The symbol mangling format is:
8524 <node> := <literal>
8525 | <unary-operator> ':' <node>
8526 | <binary-operator> ':' <node> ':' <node>
8529 <literal> := 's' <digits=N> ':' <N character symbol name>
8530 | 'S' <digits=N> ':' <N character section name>
8531 | '#' <hexdigits>
8534 <binary-operator> := as in C
8535 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8537 static void
8538 set_symbol_value (bfd *bfd_with_globals,
8539 Elf_Internal_Sym *isymbuf,
8540 size_t locsymcount,
8541 size_t symidx,
8542 bfd_vma val)
8544 struct elf_link_hash_entry **sym_hashes;
8545 struct elf_link_hash_entry *h;
8546 size_t extsymoff = locsymcount;
8548 if (symidx < locsymcount)
8550 Elf_Internal_Sym *sym;
8552 sym = isymbuf + symidx;
8553 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8555 /* It is a local symbol: move it to the
8556 "absolute" section and give it a value. */
8557 sym->st_shndx = SHN_ABS;
8558 sym->st_value = val;
8559 return;
8561 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8562 extsymoff = 0;
8565 /* It is a global symbol: set its link type
8566 to "defined" and give it a value. */
8568 sym_hashes = elf_sym_hashes (bfd_with_globals);
8569 h = sym_hashes [symidx - extsymoff];
8570 while (h->root.type == bfd_link_hash_indirect
8571 || h->root.type == bfd_link_hash_warning)
8572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8573 h->root.type = bfd_link_hash_defined;
8574 h->root.u.def.value = val;
8575 h->root.u.def.section = bfd_abs_section_ptr;
8578 static bfd_boolean
8579 resolve_symbol (const char *name,
8580 bfd *input_bfd,
8581 struct elf_final_link_info *flinfo,
8582 bfd_vma *result,
8583 Elf_Internal_Sym *isymbuf,
8584 size_t locsymcount)
8586 Elf_Internal_Sym *sym;
8587 struct bfd_link_hash_entry *global_entry;
8588 const char *candidate = NULL;
8589 Elf_Internal_Shdr *symtab_hdr;
8590 size_t i;
8592 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8594 for (i = 0; i < locsymcount; ++ i)
8596 sym = isymbuf + i;
8598 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8599 continue;
8601 candidate = bfd_elf_string_from_elf_section (input_bfd,
8602 symtab_hdr->sh_link,
8603 sym->st_name);
8604 #ifdef DEBUG
8605 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8606 name, candidate, (unsigned long) sym->st_value);
8607 #endif
8608 if (candidate && strcmp (candidate, name) == 0)
8610 asection *sec = flinfo->sections [i];
8612 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8613 *result += sec->output_offset + sec->output_section->vma;
8614 #ifdef DEBUG
8615 printf ("Found symbol with value %8.8lx\n",
8616 (unsigned long) *result);
8617 #endif
8618 return TRUE;
8622 /* Hmm, haven't found it yet. perhaps it is a global. */
8623 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8624 FALSE, FALSE, TRUE);
8625 if (!global_entry)
8626 return FALSE;
8628 if (global_entry->type == bfd_link_hash_defined
8629 || global_entry->type == bfd_link_hash_defweak)
8631 *result = (global_entry->u.def.value
8632 + global_entry->u.def.section->output_section->vma
8633 + global_entry->u.def.section->output_offset);
8634 #ifdef DEBUG
8635 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8636 global_entry->root.string, (unsigned long) *result);
8637 #endif
8638 return TRUE;
8641 return FALSE;
8644 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8645 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8646 names like "foo.end" which is the end address of section "foo". */
8648 static bfd_boolean
8649 resolve_section (const char *name,
8650 asection *sections,
8651 bfd_vma *result,
8652 bfd * abfd)
8654 asection *curr;
8655 unsigned int len;
8657 for (curr = sections; curr; curr = curr->next)
8658 if (strcmp (curr->name, name) == 0)
8660 *result = curr->vma;
8661 return TRUE;
8664 /* Hmm. still haven't found it. try pseudo-section names. */
8665 /* FIXME: This could be coded more efficiently... */
8666 for (curr = sections; curr; curr = curr->next)
8668 len = strlen (curr->name);
8669 if (len > strlen (name))
8670 continue;
8672 if (strncmp (curr->name, name, len) == 0)
8674 if (strncmp (".end", name + len, 4) == 0)
8676 *result = (curr->vma
8677 + curr->size / bfd_octets_per_byte (abfd, curr));
8678 return TRUE;
8681 /* Insert more pseudo-section names here, if you like. */
8685 return FALSE;
8688 static void
8689 undefined_reference (const char *reftype, const char *name)
8691 /* xgettext:c-format */
8692 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8693 reftype, name);
8694 bfd_set_error (bfd_error_bad_value);
8697 static bfd_boolean
8698 eval_symbol (bfd_vma *result,
8699 const char **symp,
8700 bfd *input_bfd,
8701 struct elf_final_link_info *flinfo,
8702 bfd_vma dot,
8703 Elf_Internal_Sym *isymbuf,
8704 size_t locsymcount,
8705 int signed_p)
8707 size_t len;
8708 size_t symlen;
8709 bfd_vma a;
8710 bfd_vma b;
8711 char symbuf[4096];
8712 const char *sym = *symp;
8713 const char *symend;
8714 bfd_boolean symbol_is_section = FALSE;
8716 len = strlen (sym);
8717 symend = sym + len;
8719 if (len < 1 || len > sizeof (symbuf))
8721 bfd_set_error (bfd_error_invalid_operation);
8722 return FALSE;
8725 switch (* sym)
8727 case '.':
8728 *result = dot;
8729 *symp = sym + 1;
8730 return TRUE;
8732 case '#':
8733 ++sym;
8734 *result = strtoul (sym, (char **) symp, 16);
8735 return TRUE;
8737 case 'S':
8738 symbol_is_section = TRUE;
8739 /* Fall through. */
8740 case 's':
8741 ++sym;
8742 symlen = strtol (sym, (char **) symp, 10);
8743 sym = *symp + 1; /* Skip the trailing ':'. */
8745 if (symend < sym || symlen + 1 > sizeof (symbuf))
8747 bfd_set_error (bfd_error_invalid_operation);
8748 return FALSE;
8751 memcpy (symbuf, sym, symlen);
8752 symbuf[symlen] = '\0';
8753 *symp = sym + symlen;
8755 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8756 the symbol as a section, or vice-versa. so we're pretty liberal in our
8757 interpretation here; section means "try section first", not "must be a
8758 section", and likewise with symbol. */
8760 if (symbol_is_section)
8762 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8763 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8764 isymbuf, locsymcount))
8766 undefined_reference ("section", symbuf);
8767 return FALSE;
8770 else
8772 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8773 isymbuf, locsymcount)
8774 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8775 result, input_bfd))
8777 undefined_reference ("symbol", symbuf);
8778 return FALSE;
8782 return TRUE;
8784 /* All that remains are operators. */
8786 #define UNARY_OP(op) \
8787 if (strncmp (sym, #op, strlen (#op)) == 0) \
8789 sym += strlen (#op); \
8790 if (*sym == ':') \
8791 ++sym; \
8792 *symp = sym; \
8793 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8794 isymbuf, locsymcount, signed_p)) \
8795 return FALSE; \
8796 if (signed_p) \
8797 *result = op ((bfd_signed_vma) a); \
8798 else \
8799 *result = op a; \
8800 return TRUE; \
8803 #define BINARY_OP_HEAD(op) \
8804 if (strncmp (sym, #op, strlen (#op)) == 0) \
8806 sym += strlen (#op); \
8807 if (*sym == ':') \
8808 ++sym; \
8809 *symp = sym; \
8810 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8811 isymbuf, locsymcount, signed_p)) \
8812 return FALSE; \
8813 ++*symp; \
8814 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8815 isymbuf, locsymcount, signed_p)) \
8816 return FALSE;
8817 #define BINARY_OP_TAIL(op) \
8818 if (signed_p) \
8819 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8820 else \
8821 *result = a op b; \
8822 return TRUE; \
8824 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8826 default:
8827 UNARY_OP (0-);
8828 BINARY_OP_HEAD (<<);
8829 if (b >= sizeof (a) * CHAR_BIT)
8831 *result = 0;
8832 return TRUE;
8834 signed_p = 0;
8835 BINARY_OP_TAIL (<<);
8836 BINARY_OP_HEAD (>>);
8837 if (b >= sizeof (a) * CHAR_BIT)
8839 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8840 return TRUE;
8842 BINARY_OP_TAIL (>>);
8843 BINARY_OP (==);
8844 BINARY_OP (!=);
8845 BINARY_OP (<=);
8846 BINARY_OP (>=);
8847 BINARY_OP (&&);
8848 BINARY_OP (||);
8849 UNARY_OP (~);
8850 UNARY_OP (!);
8851 BINARY_OP (*);
8852 BINARY_OP_HEAD (/);
8853 if (b == 0)
8855 _bfd_error_handler (_("division by zero"));
8856 bfd_set_error (bfd_error_bad_value);
8857 return FALSE;
8859 BINARY_OP_TAIL (/);
8860 BINARY_OP_HEAD (%);
8861 if (b == 0)
8863 _bfd_error_handler (_("division by zero"));
8864 bfd_set_error (bfd_error_bad_value);
8865 return FALSE;
8867 BINARY_OP_TAIL (%);
8868 BINARY_OP (^);
8869 BINARY_OP (|);
8870 BINARY_OP (&);
8871 BINARY_OP (+);
8872 BINARY_OP (-);
8873 BINARY_OP (<);
8874 BINARY_OP (>);
8875 #undef UNARY_OP
8876 #undef BINARY_OP
8877 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8878 bfd_set_error (bfd_error_invalid_operation);
8879 return FALSE;
8883 static void
8884 put_value (bfd_vma size,
8885 unsigned long chunksz,
8886 bfd *input_bfd,
8887 bfd_vma x,
8888 bfd_byte *location)
8890 location += (size - chunksz);
8892 for (; size; size -= chunksz, location -= chunksz)
8894 switch (chunksz)
8896 case 1:
8897 bfd_put_8 (input_bfd, x, location);
8898 x >>= 8;
8899 break;
8900 case 2:
8901 bfd_put_16 (input_bfd, x, location);
8902 x >>= 16;
8903 break;
8904 case 4:
8905 bfd_put_32 (input_bfd, x, location);
8906 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8907 x >>= 16;
8908 x >>= 16;
8909 break;
8910 #ifdef BFD64
8911 case 8:
8912 bfd_put_64 (input_bfd, x, location);
8913 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8914 x >>= 32;
8915 x >>= 32;
8916 break;
8917 #endif
8918 default:
8919 abort ();
8920 break;
8925 static bfd_vma
8926 get_value (bfd_vma size,
8927 unsigned long chunksz,
8928 bfd *input_bfd,
8929 bfd_byte *location)
8931 int shift;
8932 bfd_vma x = 0;
8934 /* Sanity checks. */
8935 BFD_ASSERT (chunksz <= sizeof (x)
8936 && size >= chunksz
8937 && chunksz != 0
8938 && (size % chunksz) == 0
8939 && input_bfd != NULL
8940 && location != NULL);
8942 if (chunksz == sizeof (x))
8944 BFD_ASSERT (size == chunksz);
8946 /* Make sure that we do not perform an undefined shift operation.
8947 We know that size == chunksz so there will only be one iteration
8948 of the loop below. */
8949 shift = 0;
8951 else
8952 shift = 8 * chunksz;
8954 for (; size; size -= chunksz, location += chunksz)
8956 switch (chunksz)
8958 case 1:
8959 x = (x << shift) | bfd_get_8 (input_bfd, location);
8960 break;
8961 case 2:
8962 x = (x << shift) | bfd_get_16 (input_bfd, location);
8963 break;
8964 case 4:
8965 x = (x << shift) | bfd_get_32 (input_bfd, location);
8966 break;
8967 #ifdef BFD64
8968 case 8:
8969 x = (x << shift) | bfd_get_64 (input_bfd, location);
8970 break;
8971 #endif
8972 default:
8973 abort ();
8976 return x;
8979 static void
8980 decode_complex_addend (unsigned long *start, /* in bits */
8981 unsigned long *oplen, /* in bits */
8982 unsigned long *len, /* in bits */
8983 unsigned long *wordsz, /* in bytes */
8984 unsigned long *chunksz, /* in bytes */
8985 unsigned long *lsb0_p,
8986 unsigned long *signed_p,
8987 unsigned long *trunc_p,
8988 unsigned long encoded)
8990 * start = encoded & 0x3F;
8991 * len = (encoded >> 6) & 0x3F;
8992 * oplen = (encoded >> 12) & 0x3F;
8993 * wordsz = (encoded >> 18) & 0xF;
8994 * chunksz = (encoded >> 22) & 0xF;
8995 * lsb0_p = (encoded >> 27) & 1;
8996 * signed_p = (encoded >> 28) & 1;
8997 * trunc_p = (encoded >> 29) & 1;
9000 bfd_reloc_status_type
9001 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9002 asection *input_section,
9003 bfd_byte *contents,
9004 Elf_Internal_Rela *rel,
9005 bfd_vma relocation)
9007 bfd_vma shift, x, mask;
9008 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9009 bfd_reloc_status_type r;
9010 bfd_size_type octets;
9012 /* Perform this reloc, since it is complex.
9013 (this is not to say that it necessarily refers to a complex
9014 symbol; merely that it is a self-describing CGEN based reloc.
9015 i.e. the addend has the complete reloc information (bit start, end,
9016 word size, etc) encoded within it.). */
9018 decode_complex_addend (&start, &oplen, &len, &wordsz,
9019 &chunksz, &lsb0_p, &signed_p,
9020 &trunc_p, rel->r_addend);
9022 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9024 if (lsb0_p)
9025 shift = (start + 1) - len;
9026 else
9027 shift = (8 * wordsz) - (start + len);
9029 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9030 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9032 #ifdef DEBUG
9033 printf ("Doing complex reloc: "
9034 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9035 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9036 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9037 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9038 oplen, (unsigned long) x, (unsigned long) mask,
9039 (unsigned long) relocation);
9040 #endif
9042 r = bfd_reloc_ok;
9043 if (! trunc_p)
9044 /* Now do an overflow check. */
9045 r = bfd_check_overflow ((signed_p
9046 ? complain_overflow_signed
9047 : complain_overflow_unsigned),
9048 len, 0, (8 * wordsz),
9049 relocation);
9051 /* Do the deed. */
9052 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9054 #ifdef DEBUG
9055 printf (" relocation: %8.8lx\n"
9056 " shifted mask: %8.8lx\n"
9057 " shifted/masked reloc: %8.8lx\n"
9058 " result: %8.8lx\n",
9059 (unsigned long) relocation, (unsigned long) (mask << shift),
9060 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9061 #endif
9062 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9063 return r;
9066 /* Functions to read r_offset from external (target order) reloc
9067 entry. Faster than bfd_getl32 et al, because we let the compiler
9068 know the value is aligned. */
9070 static bfd_vma
9071 ext32l_r_offset (const void *p)
9073 union aligned32
9075 uint32_t v;
9076 unsigned char c[4];
9078 const union aligned32 *a
9079 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9081 uint32_t aval = ( (uint32_t) a->c[0]
9082 | (uint32_t) a->c[1] << 8
9083 | (uint32_t) a->c[2] << 16
9084 | (uint32_t) a->c[3] << 24);
9085 return aval;
9088 static bfd_vma
9089 ext32b_r_offset (const void *p)
9091 union aligned32
9093 uint32_t v;
9094 unsigned char c[4];
9096 const union aligned32 *a
9097 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9099 uint32_t aval = ( (uint32_t) a->c[0] << 24
9100 | (uint32_t) a->c[1] << 16
9101 | (uint32_t) a->c[2] << 8
9102 | (uint32_t) a->c[3]);
9103 return aval;
9106 #ifdef BFD_HOST_64_BIT
9107 static bfd_vma
9108 ext64l_r_offset (const void *p)
9110 union aligned64
9112 uint64_t v;
9113 unsigned char c[8];
9115 const union aligned64 *a
9116 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9118 uint64_t aval = ( (uint64_t) a->c[0]
9119 | (uint64_t) a->c[1] << 8
9120 | (uint64_t) a->c[2] << 16
9121 | (uint64_t) a->c[3] << 24
9122 | (uint64_t) a->c[4] << 32
9123 | (uint64_t) a->c[5] << 40
9124 | (uint64_t) a->c[6] << 48
9125 | (uint64_t) a->c[7] << 56);
9126 return aval;
9129 static bfd_vma
9130 ext64b_r_offset (const void *p)
9132 union aligned64
9134 uint64_t v;
9135 unsigned char c[8];
9137 const union aligned64 *a
9138 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9140 uint64_t aval = ( (uint64_t) a->c[0] << 56
9141 | (uint64_t) a->c[1] << 48
9142 | (uint64_t) a->c[2] << 40
9143 | (uint64_t) a->c[3] << 32
9144 | (uint64_t) a->c[4] << 24
9145 | (uint64_t) a->c[5] << 16
9146 | (uint64_t) a->c[6] << 8
9147 | (uint64_t) a->c[7]);
9148 return aval;
9150 #endif
9152 /* When performing a relocatable link, the input relocations are
9153 preserved. But, if they reference global symbols, the indices
9154 referenced must be updated. Update all the relocations found in
9155 RELDATA. */
9157 static bfd_boolean
9158 elf_link_adjust_relocs (bfd *abfd,
9159 asection *sec,
9160 struct bfd_elf_section_reloc_data *reldata,
9161 bfd_boolean sort,
9162 struct bfd_link_info *info)
9164 unsigned int i;
9165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9166 bfd_byte *erela;
9167 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9168 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9169 bfd_vma r_type_mask;
9170 int r_sym_shift;
9171 unsigned int count = reldata->count;
9172 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9174 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9176 swap_in = bed->s->swap_reloc_in;
9177 swap_out = bed->s->swap_reloc_out;
9179 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9181 swap_in = bed->s->swap_reloca_in;
9182 swap_out = bed->s->swap_reloca_out;
9184 else
9185 abort ();
9187 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9188 abort ();
9190 if (bed->s->arch_size == 32)
9192 r_type_mask = 0xff;
9193 r_sym_shift = 8;
9195 else
9197 r_type_mask = 0xffffffff;
9198 r_sym_shift = 32;
9201 erela = reldata->hdr->contents;
9202 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9204 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9205 unsigned int j;
9207 if (*rel_hash == NULL)
9208 continue;
9210 if ((*rel_hash)->indx == -2
9211 && info->gc_sections
9212 && ! info->gc_keep_exported)
9214 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9215 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9216 abfd, sec,
9217 (*rel_hash)->root.root.string);
9218 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9219 abfd, sec);
9220 bfd_set_error (bfd_error_invalid_operation);
9221 return FALSE;
9223 BFD_ASSERT ((*rel_hash)->indx >= 0);
9225 (*swap_in) (abfd, erela, irela);
9226 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9227 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9228 | (irela[j].r_info & r_type_mask));
9229 (*swap_out) (abfd, irela, erela);
9232 if (bed->elf_backend_update_relocs)
9233 (*bed->elf_backend_update_relocs) (sec, reldata);
9235 if (sort && count != 0)
9237 bfd_vma (*ext_r_off) (const void *);
9238 bfd_vma r_off;
9239 size_t elt_size;
9240 bfd_byte *base, *end, *p, *loc;
9241 bfd_byte *buf = NULL;
9243 if (bed->s->arch_size == 32)
9245 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9246 ext_r_off = ext32l_r_offset;
9247 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9248 ext_r_off = ext32b_r_offset;
9249 else
9250 abort ();
9252 else
9254 #ifdef BFD_HOST_64_BIT
9255 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9256 ext_r_off = ext64l_r_offset;
9257 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9258 ext_r_off = ext64b_r_offset;
9259 else
9260 #endif
9261 abort ();
9264 /* Must use a stable sort here. A modified insertion sort,
9265 since the relocs are mostly sorted already. */
9266 elt_size = reldata->hdr->sh_entsize;
9267 base = reldata->hdr->contents;
9268 end = base + count * elt_size;
9269 if (elt_size > sizeof (Elf64_External_Rela))
9270 abort ();
9272 /* Ensure the first element is lowest. This acts as a sentinel,
9273 speeding the main loop below. */
9274 r_off = (*ext_r_off) (base);
9275 for (p = loc = base; (p += elt_size) < end; )
9277 bfd_vma r_off2 = (*ext_r_off) (p);
9278 if (r_off > r_off2)
9280 r_off = r_off2;
9281 loc = p;
9284 if (loc != base)
9286 /* Don't just swap *base and *loc as that changes the order
9287 of the original base[0] and base[1] if they happen to
9288 have the same r_offset. */
9289 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9290 memcpy (onebuf, loc, elt_size);
9291 memmove (base + elt_size, base, loc - base);
9292 memcpy (base, onebuf, elt_size);
9295 for (p = base + elt_size; (p += elt_size) < end; )
9297 /* base to p is sorted, *p is next to insert. */
9298 r_off = (*ext_r_off) (p);
9299 /* Search the sorted region for location to insert. */
9300 loc = p - elt_size;
9301 while (r_off < (*ext_r_off) (loc))
9302 loc -= elt_size;
9303 loc += elt_size;
9304 if (loc != p)
9306 /* Chances are there is a run of relocs to insert here,
9307 from one of more input files. Files are not always
9308 linked in order due to the way elf_link_input_bfd is
9309 called. See pr17666. */
9310 size_t sortlen = p - loc;
9311 bfd_vma r_off2 = (*ext_r_off) (loc);
9312 size_t runlen = elt_size;
9313 size_t buf_size = 96 * 1024;
9314 while (p + runlen < end
9315 && (sortlen <= buf_size
9316 || runlen + elt_size <= buf_size)
9317 && r_off2 > (*ext_r_off) (p + runlen))
9318 runlen += elt_size;
9319 if (buf == NULL)
9321 buf = bfd_malloc (buf_size);
9322 if (buf == NULL)
9323 return FALSE;
9325 if (runlen < sortlen)
9327 memcpy (buf, p, runlen);
9328 memmove (loc + runlen, loc, sortlen);
9329 memcpy (loc, buf, runlen);
9331 else
9333 memcpy (buf, loc, sortlen);
9334 memmove (loc, p, runlen);
9335 memcpy (loc + runlen, buf, sortlen);
9337 p += runlen - elt_size;
9340 /* Hashes are no longer valid. */
9341 free (reldata->hashes);
9342 reldata->hashes = NULL;
9343 free (buf);
9345 return TRUE;
9348 struct elf_link_sort_rela
9350 union {
9351 bfd_vma offset;
9352 bfd_vma sym_mask;
9353 } u;
9354 enum elf_reloc_type_class type;
9355 /* We use this as an array of size int_rels_per_ext_rel. */
9356 Elf_Internal_Rela rela[1];
9359 /* qsort stability here and for cmp2 is only an issue if multiple
9360 dynamic relocations are emitted at the same address. But targets
9361 that apply a series of dynamic relocations each operating on the
9362 result of the prior relocation can't use -z combreloc as
9363 implemented anyway. Such schemes tend to be broken by sorting on
9364 symbol index. That leaves dynamic NONE relocs as the only other
9365 case where ld might emit multiple relocs at the same address, and
9366 those are only emitted due to target bugs. */
9368 static int
9369 elf_link_sort_cmp1 (const void *A, const void *B)
9371 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9372 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9373 int relativea, relativeb;
9375 relativea = a->type == reloc_class_relative;
9376 relativeb = b->type == reloc_class_relative;
9378 if (relativea < relativeb)
9379 return 1;
9380 if (relativea > relativeb)
9381 return -1;
9382 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9383 return -1;
9384 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9385 return 1;
9386 if (a->rela->r_offset < b->rela->r_offset)
9387 return -1;
9388 if (a->rela->r_offset > b->rela->r_offset)
9389 return 1;
9390 return 0;
9393 static int
9394 elf_link_sort_cmp2 (const void *A, const void *B)
9396 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9397 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9399 if (a->type < b->type)
9400 return -1;
9401 if (a->type > b->type)
9402 return 1;
9403 if (a->u.offset < b->u.offset)
9404 return -1;
9405 if (a->u.offset > b->u.offset)
9406 return 1;
9407 if (a->rela->r_offset < b->rela->r_offset)
9408 return -1;
9409 if (a->rela->r_offset > b->rela->r_offset)
9410 return 1;
9411 return 0;
9414 static size_t
9415 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9417 asection *dynamic_relocs;
9418 asection *rela_dyn;
9419 asection *rel_dyn;
9420 bfd_size_type count, size;
9421 size_t i, ret, sort_elt, ext_size;
9422 bfd_byte *sort, *s_non_relative, *p;
9423 struct elf_link_sort_rela *sq;
9424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9425 int i2e = bed->s->int_rels_per_ext_rel;
9426 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9427 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9428 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9429 struct bfd_link_order *lo;
9430 bfd_vma r_sym_mask;
9431 bfd_boolean use_rela;
9433 /* Find a dynamic reloc section. */
9434 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9435 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9436 if (rela_dyn != NULL && rela_dyn->size > 0
9437 && rel_dyn != NULL && rel_dyn->size > 0)
9439 bfd_boolean use_rela_initialised = FALSE;
9441 /* This is just here to stop gcc from complaining.
9442 Its initialization checking code is not perfect. */
9443 use_rela = TRUE;
9445 /* Both sections are present. Examine the sizes
9446 of the indirect sections to help us choose. */
9447 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9448 if (lo->type == bfd_indirect_link_order)
9450 asection *o = lo->u.indirect.section;
9452 if ((o->size % bed->s->sizeof_rela) == 0)
9454 if ((o->size % bed->s->sizeof_rel) == 0)
9455 /* Section size is divisible by both rel and rela sizes.
9456 It is of no help to us. */
9458 else
9460 /* Section size is only divisible by rela. */
9461 if (use_rela_initialised && !use_rela)
9463 _bfd_error_handler (_("%pB: unable to sort relocs - "
9464 "they are in more than one size"),
9465 abfd);
9466 bfd_set_error (bfd_error_invalid_operation);
9467 return 0;
9469 else
9471 use_rela = TRUE;
9472 use_rela_initialised = TRUE;
9476 else if ((o->size % bed->s->sizeof_rel) == 0)
9478 /* Section size is only divisible by rel. */
9479 if (use_rela_initialised && use_rela)
9481 _bfd_error_handler (_("%pB: unable to sort relocs - "
9482 "they are in more than one size"),
9483 abfd);
9484 bfd_set_error (bfd_error_invalid_operation);
9485 return 0;
9487 else
9489 use_rela = FALSE;
9490 use_rela_initialised = TRUE;
9493 else
9495 /* The section size is not divisible by either -
9496 something is wrong. */
9497 _bfd_error_handler (_("%pB: unable to sort relocs - "
9498 "they are of an unknown size"), abfd);
9499 bfd_set_error (bfd_error_invalid_operation);
9500 return 0;
9504 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9505 if (lo->type == bfd_indirect_link_order)
9507 asection *o = lo->u.indirect.section;
9509 if ((o->size % bed->s->sizeof_rela) == 0)
9511 if ((o->size % bed->s->sizeof_rel) == 0)
9512 /* Section size is divisible by both rel and rela sizes.
9513 It is of no help to us. */
9515 else
9517 /* Section size is only divisible by rela. */
9518 if (use_rela_initialised && !use_rela)
9520 _bfd_error_handler (_("%pB: unable to sort relocs - "
9521 "they are in more than one size"),
9522 abfd);
9523 bfd_set_error (bfd_error_invalid_operation);
9524 return 0;
9526 else
9528 use_rela = TRUE;
9529 use_rela_initialised = TRUE;
9533 else if ((o->size % bed->s->sizeof_rel) == 0)
9535 /* Section size is only divisible by rel. */
9536 if (use_rela_initialised && use_rela)
9538 _bfd_error_handler (_("%pB: unable to sort relocs - "
9539 "they are in more than one size"),
9540 abfd);
9541 bfd_set_error (bfd_error_invalid_operation);
9542 return 0;
9544 else
9546 use_rela = FALSE;
9547 use_rela_initialised = TRUE;
9550 else
9552 /* The section size is not divisible by either -
9553 something is wrong. */
9554 _bfd_error_handler (_("%pB: unable to sort relocs - "
9555 "they are of an unknown size"), abfd);
9556 bfd_set_error (bfd_error_invalid_operation);
9557 return 0;
9561 if (! use_rela_initialised)
9562 /* Make a guess. */
9563 use_rela = TRUE;
9565 else if (rela_dyn != NULL && rela_dyn->size > 0)
9566 use_rela = TRUE;
9567 else if (rel_dyn != NULL && rel_dyn->size > 0)
9568 use_rela = FALSE;
9569 else
9570 return 0;
9572 if (use_rela)
9574 dynamic_relocs = rela_dyn;
9575 ext_size = bed->s->sizeof_rela;
9576 swap_in = bed->s->swap_reloca_in;
9577 swap_out = bed->s->swap_reloca_out;
9579 else
9581 dynamic_relocs = rel_dyn;
9582 ext_size = bed->s->sizeof_rel;
9583 swap_in = bed->s->swap_reloc_in;
9584 swap_out = bed->s->swap_reloc_out;
9587 size = 0;
9588 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9589 if (lo->type == bfd_indirect_link_order)
9590 size += lo->u.indirect.section->size;
9592 if (size != dynamic_relocs->size)
9593 return 0;
9595 sort_elt = (sizeof (struct elf_link_sort_rela)
9596 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9598 count = dynamic_relocs->size / ext_size;
9599 if (count == 0)
9600 return 0;
9601 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9603 if (sort == NULL)
9605 (*info->callbacks->warning)
9606 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9607 return 0;
9610 if (bed->s->arch_size == 32)
9611 r_sym_mask = ~(bfd_vma) 0xff;
9612 else
9613 r_sym_mask = ~(bfd_vma) 0xffffffff;
9615 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9616 if (lo->type == bfd_indirect_link_order)
9618 bfd_byte *erel, *erelend;
9619 asection *o = lo->u.indirect.section;
9621 if (o->contents == NULL && o->size != 0)
9623 /* This is a reloc section that is being handled as a normal
9624 section. See bfd_section_from_shdr. We can't combine
9625 relocs in this case. */
9626 free (sort);
9627 return 0;
9629 erel = o->contents;
9630 erelend = o->contents + o->size;
9631 p = sort + o->output_offset * opb / ext_size * sort_elt;
9633 while (erel < erelend)
9635 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9637 (*swap_in) (abfd, erel, s->rela);
9638 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9639 s->u.sym_mask = r_sym_mask;
9640 p += sort_elt;
9641 erel += ext_size;
9645 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9647 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9649 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9650 if (s->type != reloc_class_relative)
9651 break;
9653 ret = i;
9654 s_non_relative = p;
9656 sq = (struct elf_link_sort_rela *) s_non_relative;
9657 for (; i < count; i++, p += sort_elt)
9659 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9660 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9661 sq = sp;
9662 sp->u.offset = sq->rela->r_offset;
9665 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9667 struct elf_link_hash_table *htab = elf_hash_table (info);
9668 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9670 /* We have plt relocs in .rela.dyn. */
9671 sq = (struct elf_link_sort_rela *) sort;
9672 for (i = 0; i < count; i++)
9673 if (sq[count - i - 1].type != reloc_class_plt)
9674 break;
9675 if (i != 0 && htab->srelplt->size == i * ext_size)
9677 struct bfd_link_order **plo;
9678 /* Put srelplt link_order last. This is so the output_offset
9679 set in the next loop is correct for DT_JMPREL. */
9680 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9681 if ((*plo)->type == bfd_indirect_link_order
9682 && (*plo)->u.indirect.section == htab->srelplt)
9684 lo = *plo;
9685 *plo = lo->next;
9687 else
9688 plo = &(*plo)->next;
9689 *plo = lo;
9690 lo->next = NULL;
9691 dynamic_relocs->map_tail.link_order = lo;
9695 p = sort;
9696 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9697 if (lo->type == bfd_indirect_link_order)
9699 bfd_byte *erel, *erelend;
9700 asection *o = lo->u.indirect.section;
9702 erel = o->contents;
9703 erelend = o->contents + o->size;
9704 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9705 while (erel < erelend)
9707 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9708 (*swap_out) (abfd, s->rela, erel);
9709 p += sort_elt;
9710 erel += ext_size;
9714 free (sort);
9715 *psec = dynamic_relocs;
9716 return ret;
9719 /* Add a symbol to the output symbol string table. */
9721 static int
9722 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9723 const char *name,
9724 Elf_Internal_Sym *elfsym,
9725 asection *input_sec,
9726 struct elf_link_hash_entry *h)
9728 int (*output_symbol_hook)
9729 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9730 struct elf_link_hash_entry *);
9731 struct elf_link_hash_table *hash_table;
9732 const struct elf_backend_data *bed;
9733 bfd_size_type strtabsize;
9735 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9737 bed = get_elf_backend_data (flinfo->output_bfd);
9738 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9739 if (output_symbol_hook != NULL)
9741 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9742 if (ret != 1)
9743 return ret;
9746 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9747 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9748 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9749 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9751 if (name == NULL
9752 || *name == '\0'
9753 || (input_sec->flags & SEC_EXCLUDE))
9754 elfsym->st_name = (unsigned long) -1;
9755 else
9757 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9758 to get the final offset for st_name. */
9759 char *versioned_name = (char *) name;
9760 if (h != NULL)
9762 if (h->versioned == versioned && h->def_dynamic)
9764 /* Keep only one '@' for versioned symbols defined in
9765 shared objects. */
9766 char *version = strrchr (name, ELF_VER_CHR);
9767 char *base_end = strchr (name, ELF_VER_CHR);
9768 if (version != base_end)
9770 size_t base_len;
9771 size_t len = strlen (name);
9772 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9773 if (versioned_name == NULL)
9774 return 0;
9775 base_len = base_end - name;
9776 memcpy (versioned_name, name, base_len);
9777 memcpy (versioned_name + base_len, version,
9778 len - base_len);
9782 else if (flinfo->info->unique_symbol
9783 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
9785 struct local_hash_entry *lh;
9786 switch (ELF_ST_TYPE (elfsym->st_info))
9788 case STT_FILE:
9789 case STT_SECTION:
9790 break;
9791 default:
9792 lh = (struct local_hash_entry *) bfd_hash_lookup
9793 (&flinfo->local_hash_table, name, TRUE, FALSE);
9794 if (lh == NULL)
9795 return 0;
9796 if (lh->count)
9798 /* Append ".COUNT" to duplicated local symbols. */
9799 size_t count_len;
9800 size_t base_len = lh->size;
9801 char buf[30];
9802 sprintf (buf, "%lx", lh->count);
9803 if (!base_len)
9805 base_len = strlen (name);
9806 lh->size = base_len;
9808 count_len = strlen (buf);
9809 versioned_name = bfd_alloc (flinfo->output_bfd,
9810 base_len + count_len + 2);
9811 if (versioned_name == NULL)
9812 return 0;
9813 memcpy (versioned_name, name, base_len);
9814 versioned_name[base_len] = '.';
9815 memcpy (versioned_name + base_len + 1, buf,
9816 count_len + 1);
9818 lh->count++;
9819 break;
9822 elfsym->st_name
9823 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9824 versioned_name, FALSE);
9825 if (elfsym->st_name == (unsigned long) -1)
9826 return 0;
9829 hash_table = elf_hash_table (flinfo->info);
9830 strtabsize = hash_table->strtabsize;
9831 if (strtabsize <= hash_table->strtabcount)
9833 strtabsize += strtabsize;
9834 hash_table->strtabsize = strtabsize;
9835 strtabsize *= sizeof (*hash_table->strtab);
9836 hash_table->strtab
9837 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9838 strtabsize);
9839 if (hash_table->strtab == NULL)
9840 return 0;
9842 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9843 hash_table->strtab[hash_table->strtabcount].dest_index
9844 = hash_table->strtabcount;
9845 hash_table->strtab[hash_table->strtabcount].destshndx_index
9846 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9848 flinfo->output_bfd->symcount += 1;
9849 hash_table->strtabcount += 1;
9851 return 1;
9854 /* Swap symbols out to the symbol table and flush the output symbols to
9855 the file. */
9857 static bfd_boolean
9858 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9860 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9861 size_t amt;
9862 size_t i;
9863 const struct elf_backend_data *bed;
9864 bfd_byte *symbuf;
9865 Elf_Internal_Shdr *hdr;
9866 file_ptr pos;
9867 bfd_boolean ret;
9869 if (!hash_table->strtabcount)
9870 return TRUE;
9872 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9874 bed = get_elf_backend_data (flinfo->output_bfd);
9876 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9877 symbuf = (bfd_byte *) bfd_malloc (amt);
9878 if (symbuf == NULL)
9879 return FALSE;
9881 if (flinfo->symshndxbuf)
9883 amt = sizeof (Elf_External_Sym_Shndx);
9884 amt *= bfd_get_symcount (flinfo->output_bfd);
9885 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9886 if (flinfo->symshndxbuf == NULL)
9888 free (symbuf);
9889 return FALSE;
9893 /* Now swap out the symbols. */
9894 for (i = 0; i < hash_table->strtabcount; i++)
9896 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9897 if (elfsym->sym.st_name == (unsigned long) -1)
9898 elfsym->sym.st_name = 0;
9899 else
9900 elfsym->sym.st_name
9901 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9902 elfsym->sym.st_name);
9904 /* Inform the linker of the addition of this symbol. */
9906 if (flinfo->info->callbacks->ctf_new_symbol)
9907 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9908 &elfsym->sym);
9910 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9911 ((bfd_byte *) symbuf
9912 + (elfsym->dest_index
9913 * bed->s->sizeof_sym)),
9914 (flinfo->symshndxbuf
9915 + elfsym->destshndx_index));
9918 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9919 pos = hdr->sh_offset + hdr->sh_size;
9920 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9921 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9922 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9924 hdr->sh_size += amt;
9925 ret = TRUE;
9927 else
9928 ret = FALSE;
9930 free (symbuf);
9932 free (hash_table->strtab);
9933 hash_table->strtab = NULL;
9935 return ret;
9938 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9940 static bfd_boolean
9941 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9943 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9944 && sym->st_shndx < SHN_LORESERVE)
9946 /* The gABI doesn't support dynamic symbols in output sections
9947 beyond 64k. */
9948 _bfd_error_handler
9949 /* xgettext:c-format */
9950 (_("%pB: too many sections: %d (>= %d)"),
9951 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9952 bfd_set_error (bfd_error_nonrepresentable_section);
9953 return FALSE;
9955 return TRUE;
9958 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9959 allowing an unsatisfied unversioned symbol in the DSO to match a
9960 versioned symbol that would normally require an explicit version.
9961 We also handle the case that a DSO references a hidden symbol
9962 which may be satisfied by a versioned symbol in another DSO. */
9964 static bfd_boolean
9965 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9966 const struct elf_backend_data *bed,
9967 struct elf_link_hash_entry *h)
9969 bfd *abfd;
9970 struct elf_link_loaded_list *loaded;
9972 if (!is_elf_hash_table (info->hash))
9973 return FALSE;
9975 /* Check indirect symbol. */
9976 while (h->root.type == bfd_link_hash_indirect)
9977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9979 switch (h->root.type)
9981 default:
9982 abfd = NULL;
9983 break;
9985 case bfd_link_hash_undefined:
9986 case bfd_link_hash_undefweak:
9987 abfd = h->root.u.undef.abfd;
9988 if (abfd == NULL
9989 || (abfd->flags & DYNAMIC) == 0
9990 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9991 return FALSE;
9992 break;
9994 case bfd_link_hash_defined:
9995 case bfd_link_hash_defweak:
9996 abfd = h->root.u.def.section->owner;
9997 break;
9999 case bfd_link_hash_common:
10000 abfd = h->root.u.c.p->section->owner;
10001 break;
10003 BFD_ASSERT (abfd != NULL);
10005 for (loaded = elf_hash_table (info)->dyn_loaded;
10006 loaded != NULL;
10007 loaded = loaded->next)
10009 bfd *input;
10010 Elf_Internal_Shdr *hdr;
10011 size_t symcount;
10012 size_t extsymcount;
10013 size_t extsymoff;
10014 Elf_Internal_Shdr *versymhdr;
10015 Elf_Internal_Sym *isym;
10016 Elf_Internal_Sym *isymend;
10017 Elf_Internal_Sym *isymbuf;
10018 Elf_External_Versym *ever;
10019 Elf_External_Versym *extversym;
10021 input = loaded->abfd;
10023 /* We check each DSO for a possible hidden versioned definition. */
10024 if (input == abfd
10025 || elf_dynversym (input) == 0)
10026 continue;
10028 hdr = &elf_tdata (input)->dynsymtab_hdr;
10030 symcount = hdr->sh_size / bed->s->sizeof_sym;
10031 if (elf_bad_symtab (input))
10033 extsymcount = symcount;
10034 extsymoff = 0;
10036 else
10038 extsymcount = symcount - hdr->sh_info;
10039 extsymoff = hdr->sh_info;
10042 if (extsymcount == 0)
10043 continue;
10045 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10046 NULL, NULL, NULL);
10047 if (isymbuf == NULL)
10048 return FALSE;
10050 /* Read in any version definitions. */
10051 versymhdr = &elf_tdata (input)->dynversym_hdr;
10052 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10053 || (extversym = (Elf_External_Versym *)
10054 _bfd_malloc_and_read (input, versymhdr->sh_size,
10055 versymhdr->sh_size)) == NULL)
10057 free (isymbuf);
10058 return FALSE;
10061 ever = extversym + extsymoff;
10062 isymend = isymbuf + extsymcount;
10063 for (isym = isymbuf; isym < isymend; isym++, ever++)
10065 const char *name;
10066 Elf_Internal_Versym iver;
10067 unsigned short version_index;
10069 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10070 || isym->st_shndx == SHN_UNDEF)
10071 continue;
10073 name = bfd_elf_string_from_elf_section (input,
10074 hdr->sh_link,
10075 isym->st_name);
10076 if (strcmp (name, h->root.root.string) != 0)
10077 continue;
10079 _bfd_elf_swap_versym_in (input, ever, &iver);
10081 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10082 && !(h->def_regular
10083 && h->forced_local))
10085 /* If we have a non-hidden versioned sym, then it should
10086 have provided a definition for the undefined sym unless
10087 it is defined in a non-shared object and forced local.
10089 abort ();
10092 version_index = iver.vs_vers & VERSYM_VERSION;
10093 if (version_index == 1 || version_index == 2)
10095 /* This is the base or first version. We can use it. */
10096 free (extversym);
10097 free (isymbuf);
10098 return TRUE;
10102 free (extversym);
10103 free (isymbuf);
10106 return FALSE;
10109 /* Convert ELF common symbol TYPE. */
10111 static int
10112 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10114 /* Commom symbol can only appear in relocatable link. */
10115 if (!bfd_link_relocatable (info))
10116 abort ();
10117 switch (info->elf_stt_common)
10119 case unchanged:
10120 break;
10121 case elf_stt_common:
10122 type = STT_COMMON;
10123 break;
10124 case no_elf_stt_common:
10125 type = STT_OBJECT;
10126 break;
10128 return type;
10131 /* Add an external symbol to the symbol table. This is called from
10132 the hash table traversal routine. When generating a shared object,
10133 we go through the symbol table twice. The first time we output
10134 anything that might have been forced to local scope in a version
10135 script. The second time we output the symbols that are still
10136 global symbols. */
10138 static bfd_boolean
10139 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10141 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10142 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10143 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10144 bfd_boolean strip;
10145 Elf_Internal_Sym sym;
10146 asection *input_sec;
10147 const struct elf_backend_data *bed;
10148 long indx;
10149 int ret;
10150 unsigned int type;
10152 if (h->root.type == bfd_link_hash_warning)
10154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10155 if (h->root.type == bfd_link_hash_new)
10156 return TRUE;
10159 /* Decide whether to output this symbol in this pass. */
10160 if (eoinfo->localsyms)
10162 if (!h->forced_local)
10163 return TRUE;
10165 else
10167 if (h->forced_local)
10168 return TRUE;
10171 bed = get_elf_backend_data (flinfo->output_bfd);
10173 if (h->root.type == bfd_link_hash_undefined)
10175 /* If we have an undefined symbol reference here then it must have
10176 come from a shared library that is being linked in. (Undefined
10177 references in regular files have already been handled unless
10178 they are in unreferenced sections which are removed by garbage
10179 collection). */
10180 bfd_boolean ignore_undef = FALSE;
10182 /* Some symbols may be special in that the fact that they're
10183 undefined can be safely ignored - let backend determine that. */
10184 if (bed->elf_backend_ignore_undef_symbol)
10185 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10187 /* If we are reporting errors for this situation then do so now. */
10188 if (!ignore_undef
10189 && h->ref_dynamic_nonweak
10190 && (!h->ref_regular || flinfo->info->gc_sections)
10191 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10192 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10194 flinfo->info->callbacks->undefined_symbol
10195 (flinfo->info, h->root.root.string,
10196 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10197 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10198 && !flinfo->info->warn_unresolved_syms);
10201 /* Strip a global symbol defined in a discarded section. */
10202 if (h->indx == -3)
10203 return TRUE;
10206 /* We should also warn if a forced local symbol is referenced from
10207 shared libraries. */
10208 if (bfd_link_executable (flinfo->info)
10209 && h->forced_local
10210 && h->ref_dynamic
10211 && h->def_regular
10212 && !h->dynamic_def
10213 && h->ref_dynamic_nonweak
10214 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10216 bfd *def_bfd;
10217 const char *msg;
10218 struct elf_link_hash_entry *hi = h;
10220 /* Check indirect symbol. */
10221 while (hi->root.type == bfd_link_hash_indirect)
10222 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10224 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10225 /* xgettext:c-format */
10226 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10227 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10228 /* xgettext:c-format */
10229 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10230 else
10231 /* xgettext:c-format */
10232 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10233 def_bfd = flinfo->output_bfd;
10234 if (hi->root.u.def.section != bfd_abs_section_ptr)
10235 def_bfd = hi->root.u.def.section->owner;
10236 _bfd_error_handler (msg, flinfo->output_bfd,
10237 h->root.root.string, def_bfd);
10238 bfd_set_error (bfd_error_bad_value);
10239 eoinfo->failed = TRUE;
10240 return FALSE;
10243 /* We don't want to output symbols that have never been mentioned by
10244 a regular file, or that we have been told to strip. However, if
10245 h->indx is set to -2, the symbol is used by a reloc and we must
10246 output it. */
10247 strip = FALSE;
10248 if (h->indx == -2)
10250 else if ((h->def_dynamic
10251 || h->ref_dynamic
10252 || h->root.type == bfd_link_hash_new)
10253 && !h->def_regular
10254 && !h->ref_regular)
10255 strip = TRUE;
10256 else if (flinfo->info->strip == strip_all)
10257 strip = TRUE;
10258 else if (flinfo->info->strip == strip_some
10259 && bfd_hash_lookup (flinfo->info->keep_hash,
10260 h->root.root.string, FALSE, FALSE) == NULL)
10261 strip = TRUE;
10262 else if ((h->root.type == bfd_link_hash_defined
10263 || h->root.type == bfd_link_hash_defweak)
10264 && ((flinfo->info->strip_discarded
10265 && discarded_section (h->root.u.def.section))
10266 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10267 && h->root.u.def.section->owner != NULL
10268 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10269 strip = TRUE;
10270 else if ((h->root.type == bfd_link_hash_undefined
10271 || h->root.type == bfd_link_hash_undefweak)
10272 && h->root.u.undef.abfd != NULL
10273 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10274 strip = TRUE;
10276 type = h->type;
10278 /* If we're stripping it, and it's not a dynamic symbol, there's
10279 nothing else to do. However, if it is a forced local symbol or
10280 an ifunc symbol we need to give the backend finish_dynamic_symbol
10281 function a chance to make it dynamic. */
10282 if (strip
10283 && h->dynindx == -1
10284 && type != STT_GNU_IFUNC
10285 && !h->forced_local)
10286 return TRUE;
10288 sym.st_value = 0;
10289 sym.st_size = h->size;
10290 sym.st_other = h->other;
10291 switch (h->root.type)
10293 default:
10294 case bfd_link_hash_new:
10295 case bfd_link_hash_warning:
10296 abort ();
10297 return FALSE;
10299 case bfd_link_hash_undefined:
10300 case bfd_link_hash_undefweak:
10301 input_sec = bfd_und_section_ptr;
10302 sym.st_shndx = SHN_UNDEF;
10303 break;
10305 case bfd_link_hash_defined:
10306 case bfd_link_hash_defweak:
10308 input_sec = h->root.u.def.section;
10309 if (input_sec->output_section != NULL)
10311 sym.st_shndx =
10312 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10313 input_sec->output_section);
10314 if (sym.st_shndx == SHN_BAD)
10316 _bfd_error_handler
10317 /* xgettext:c-format */
10318 (_("%pB: could not find output section %pA for input section %pA"),
10319 flinfo->output_bfd, input_sec->output_section, input_sec);
10320 bfd_set_error (bfd_error_nonrepresentable_section);
10321 eoinfo->failed = TRUE;
10322 return FALSE;
10325 /* ELF symbols in relocatable files are section relative,
10326 but in nonrelocatable files they are virtual
10327 addresses. */
10328 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10329 if (!bfd_link_relocatable (flinfo->info))
10331 sym.st_value += input_sec->output_section->vma;
10332 if (h->type == STT_TLS)
10334 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10335 if (tls_sec != NULL)
10336 sym.st_value -= tls_sec->vma;
10340 else
10342 BFD_ASSERT (input_sec->owner == NULL
10343 || (input_sec->owner->flags & DYNAMIC) != 0);
10344 sym.st_shndx = SHN_UNDEF;
10345 input_sec = bfd_und_section_ptr;
10348 break;
10350 case bfd_link_hash_common:
10351 input_sec = h->root.u.c.p->section;
10352 sym.st_shndx = bed->common_section_index (input_sec);
10353 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10354 break;
10356 case bfd_link_hash_indirect:
10357 /* These symbols are created by symbol versioning. They point
10358 to the decorated version of the name. For example, if the
10359 symbol foo@@GNU_1.2 is the default, which should be used when
10360 foo is used with no version, then we add an indirect symbol
10361 foo which points to foo@@GNU_1.2. We ignore these symbols,
10362 since the indirected symbol is already in the hash table. */
10363 return TRUE;
10366 if (type == STT_COMMON || type == STT_OBJECT)
10367 switch (h->root.type)
10369 case bfd_link_hash_common:
10370 type = elf_link_convert_common_type (flinfo->info, type);
10371 break;
10372 case bfd_link_hash_defined:
10373 case bfd_link_hash_defweak:
10374 if (bed->common_definition (&sym))
10375 type = elf_link_convert_common_type (flinfo->info, type);
10376 else
10377 type = STT_OBJECT;
10378 break;
10379 case bfd_link_hash_undefined:
10380 case bfd_link_hash_undefweak:
10381 break;
10382 default:
10383 abort ();
10386 if (h->forced_local)
10388 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10389 /* Turn off visibility on local symbol. */
10390 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10392 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10393 else if (h->unique_global && h->def_regular)
10394 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10395 else if (h->root.type == bfd_link_hash_undefweak
10396 || h->root.type == bfd_link_hash_defweak)
10397 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10398 else
10399 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10400 sym.st_target_internal = h->target_internal;
10402 /* Give the processor backend a chance to tweak the symbol value,
10403 and also to finish up anything that needs to be done for this
10404 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10405 forced local syms when non-shared is due to a historical quirk.
10406 STT_GNU_IFUNC symbol must go through PLT. */
10407 if ((h->type == STT_GNU_IFUNC
10408 && h->def_regular
10409 && !bfd_link_relocatable (flinfo->info))
10410 || ((h->dynindx != -1
10411 || h->forced_local)
10412 && ((bfd_link_pic (flinfo->info)
10413 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10414 || h->root.type != bfd_link_hash_undefweak))
10415 || !h->forced_local)
10416 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10418 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10419 (flinfo->output_bfd, flinfo->info, h, &sym)))
10421 eoinfo->failed = TRUE;
10422 return FALSE;
10426 /* If we are marking the symbol as undefined, and there are no
10427 non-weak references to this symbol from a regular object, then
10428 mark the symbol as weak undefined; if there are non-weak
10429 references, mark the symbol as strong. We can't do this earlier,
10430 because it might not be marked as undefined until the
10431 finish_dynamic_symbol routine gets through with it. */
10432 if (sym.st_shndx == SHN_UNDEF
10433 && h->ref_regular
10434 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10435 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10437 int bindtype;
10438 type = ELF_ST_TYPE (sym.st_info);
10440 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10441 if (type == STT_GNU_IFUNC)
10442 type = STT_FUNC;
10444 if (h->ref_regular_nonweak)
10445 bindtype = STB_GLOBAL;
10446 else
10447 bindtype = STB_WEAK;
10448 sym.st_info = ELF_ST_INFO (bindtype, type);
10451 /* If this is a symbol defined in a dynamic library, don't use the
10452 symbol size from the dynamic library. Relinking an executable
10453 against a new library may introduce gratuitous changes in the
10454 executable's symbols if we keep the size. */
10455 if (sym.st_shndx == SHN_UNDEF
10456 && !h->def_regular
10457 && h->def_dynamic)
10458 sym.st_size = 0;
10460 /* If a non-weak symbol with non-default visibility is not defined
10461 locally, it is a fatal error. */
10462 if (!bfd_link_relocatable (flinfo->info)
10463 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10464 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10465 && h->root.type == bfd_link_hash_undefined
10466 && !h->def_regular)
10468 const char *msg;
10470 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10471 /* xgettext:c-format */
10472 msg = _("%pB: protected symbol `%s' isn't defined");
10473 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10474 /* xgettext:c-format */
10475 msg = _("%pB: internal symbol `%s' isn't defined");
10476 else
10477 /* xgettext:c-format */
10478 msg = _("%pB: hidden symbol `%s' isn't defined");
10479 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10480 bfd_set_error (bfd_error_bad_value);
10481 eoinfo->failed = TRUE;
10482 return FALSE;
10485 /* If this symbol should be put in the .dynsym section, then put it
10486 there now. We already know the symbol index. We also fill in
10487 the entry in the .hash section. */
10488 if (h->dynindx != -1
10489 && elf_hash_table (flinfo->info)->dynamic_sections_created
10490 && elf_hash_table (flinfo->info)->dynsym != NULL
10491 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10493 bfd_byte *esym;
10495 /* Since there is no version information in the dynamic string,
10496 if there is no version info in symbol version section, we will
10497 have a run-time problem if not linking executable, referenced
10498 by shared library, or not bound locally. */
10499 if (h->verinfo.verdef == NULL
10500 && (!bfd_link_executable (flinfo->info)
10501 || h->ref_dynamic
10502 || !h->def_regular))
10504 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10506 if (p && p [1] != '\0')
10508 _bfd_error_handler
10509 /* xgettext:c-format */
10510 (_("%pB: no symbol version section for versioned symbol `%s'"),
10511 flinfo->output_bfd, h->root.root.string);
10512 eoinfo->failed = TRUE;
10513 return FALSE;
10517 sym.st_name = h->dynstr_index;
10518 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10519 + h->dynindx * bed->s->sizeof_sym);
10520 if (!check_dynsym (flinfo->output_bfd, &sym))
10522 eoinfo->failed = TRUE;
10523 return FALSE;
10526 /* Inform the linker of the addition of this symbol. */
10528 if (flinfo->info->callbacks->ctf_new_dynsym)
10529 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10531 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10533 if (flinfo->hash_sec != NULL)
10535 size_t hash_entry_size;
10536 bfd_byte *bucketpos;
10537 bfd_vma chain;
10538 size_t bucketcount;
10539 size_t bucket;
10541 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10542 bucket = h->u.elf_hash_value % bucketcount;
10544 hash_entry_size
10545 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10546 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10547 + (bucket + 2) * hash_entry_size);
10548 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10549 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10550 bucketpos);
10551 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10552 ((bfd_byte *) flinfo->hash_sec->contents
10553 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10556 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10558 Elf_Internal_Versym iversym;
10559 Elf_External_Versym *eversym;
10561 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10563 if (h->verinfo.verdef == NULL
10564 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10565 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10566 iversym.vs_vers = 0;
10567 else
10568 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10570 else
10572 if (h->verinfo.vertree == NULL)
10573 iversym.vs_vers = 1;
10574 else
10575 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10576 if (flinfo->info->create_default_symver)
10577 iversym.vs_vers++;
10580 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10581 defined locally. */
10582 if (h->versioned == versioned_hidden && h->def_regular)
10583 iversym.vs_vers |= VERSYM_HIDDEN;
10585 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10586 eversym += h->dynindx;
10587 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10591 /* If the symbol is undefined, and we didn't output it to .dynsym,
10592 strip it from .symtab too. Obviously we can't do this for
10593 relocatable output or when needed for --emit-relocs. */
10594 else if (input_sec == bfd_und_section_ptr
10595 && h->indx != -2
10596 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10597 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10598 && !bfd_link_relocatable (flinfo->info))
10599 return TRUE;
10601 /* Also strip others that we couldn't earlier due to dynamic symbol
10602 processing. */
10603 if (strip)
10604 return TRUE;
10605 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10606 return TRUE;
10608 /* Output a FILE symbol so that following locals are not associated
10609 with the wrong input file. We need one for forced local symbols
10610 if we've seen more than one FILE symbol or when we have exactly
10611 one FILE symbol but global symbols are present in a file other
10612 than the one with the FILE symbol. We also need one if linker
10613 defined symbols are present. In practice these conditions are
10614 always met, so just emit the FILE symbol unconditionally. */
10615 if (eoinfo->localsyms
10616 && !eoinfo->file_sym_done
10617 && eoinfo->flinfo->filesym_count != 0)
10619 Elf_Internal_Sym fsym;
10621 memset (&fsym, 0, sizeof (fsym));
10622 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10623 fsym.st_shndx = SHN_ABS;
10624 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10625 bfd_und_section_ptr, NULL))
10626 return FALSE;
10628 eoinfo->file_sym_done = TRUE;
10631 indx = bfd_get_symcount (flinfo->output_bfd);
10632 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10633 input_sec, h);
10634 if (ret == 0)
10636 eoinfo->failed = TRUE;
10637 return FALSE;
10639 else if (ret == 1)
10640 h->indx = indx;
10641 else if (h->indx == -2)
10642 abort();
10644 return TRUE;
10647 /* Return TRUE if special handling is done for relocs in SEC against
10648 symbols defined in discarded sections. */
10650 static bfd_boolean
10651 elf_section_ignore_discarded_relocs (asection *sec)
10653 const struct elf_backend_data *bed;
10655 switch (sec->sec_info_type)
10657 case SEC_INFO_TYPE_STABS:
10658 case SEC_INFO_TYPE_EH_FRAME:
10659 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10660 return TRUE;
10661 default:
10662 break;
10665 bed = get_elf_backend_data (sec->owner);
10666 if (bed->elf_backend_ignore_discarded_relocs != NULL
10667 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10668 return TRUE;
10670 return FALSE;
10673 /* Return a mask saying how ld should treat relocations in SEC against
10674 symbols defined in discarded sections. If this function returns
10675 COMPLAIN set, ld will issue a warning message. If this function
10676 returns PRETEND set, and the discarded section was link-once and the
10677 same size as the kept link-once section, ld will pretend that the
10678 symbol was actually defined in the kept section. Otherwise ld will
10679 zero the reloc (at least that is the intent, but some cooperation by
10680 the target dependent code is needed, particularly for REL targets). */
10682 unsigned int
10683 _bfd_elf_default_action_discarded (asection *sec)
10685 if (sec->flags & SEC_DEBUGGING)
10686 return PRETEND;
10688 if (strcmp (".eh_frame", sec->name) == 0)
10689 return 0;
10691 if (strcmp (".gcc_except_table", sec->name) == 0)
10692 return 0;
10694 return COMPLAIN | PRETEND;
10697 /* Find a match between a section and a member of a section group. */
10699 static asection *
10700 match_group_member (asection *sec, asection *group,
10701 struct bfd_link_info *info)
10703 asection *first = elf_next_in_group (group);
10704 asection *s = first;
10706 while (s != NULL)
10708 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10709 return s;
10711 s = elf_next_in_group (s);
10712 if (s == first)
10713 break;
10716 return NULL;
10719 /* Check if the kept section of a discarded section SEC can be used
10720 to replace it. Return the replacement if it is OK. Otherwise return
10721 NULL. */
10723 asection *
10724 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10726 asection *kept;
10728 kept = sec->kept_section;
10729 if (kept != NULL)
10731 if ((kept->flags & SEC_GROUP) != 0)
10732 kept = match_group_member (sec, kept, info);
10733 if (kept != NULL)
10735 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10736 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10737 kept = NULL;
10738 else
10740 /* Get the real kept section. */
10741 asection *next;
10742 for (next = kept->kept_section;
10743 next != NULL;
10744 next = next->kept_section)
10745 kept = next;
10748 sec->kept_section = kept;
10750 return kept;
10753 /* Link an input file into the linker output file. This function
10754 handles all the sections and relocations of the input file at once.
10755 This is so that we only have to read the local symbols once, and
10756 don't have to keep them in memory. */
10758 static bfd_boolean
10759 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10761 int (*relocate_section)
10762 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10763 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10764 bfd *output_bfd;
10765 Elf_Internal_Shdr *symtab_hdr;
10766 size_t locsymcount;
10767 size_t extsymoff;
10768 Elf_Internal_Sym *isymbuf;
10769 Elf_Internal_Sym *isym;
10770 Elf_Internal_Sym *isymend;
10771 long *pindex;
10772 asection **ppsection;
10773 asection *o;
10774 const struct elf_backend_data *bed;
10775 struct elf_link_hash_entry **sym_hashes;
10776 bfd_size_type address_size;
10777 bfd_vma r_type_mask;
10778 int r_sym_shift;
10779 bfd_boolean have_file_sym = FALSE;
10781 output_bfd = flinfo->output_bfd;
10782 bed = get_elf_backend_data (output_bfd);
10783 relocate_section = bed->elf_backend_relocate_section;
10785 /* If this is a dynamic object, we don't want to do anything here:
10786 we don't want the local symbols, and we don't want the section
10787 contents. */
10788 if ((input_bfd->flags & DYNAMIC) != 0)
10789 return TRUE;
10791 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10792 if (elf_bad_symtab (input_bfd))
10794 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10795 extsymoff = 0;
10797 else
10799 locsymcount = symtab_hdr->sh_info;
10800 extsymoff = symtab_hdr->sh_info;
10803 /* Enable GNU OSABI features in the output BFD that are used in the input
10804 BFD. */
10805 if (bed->elf_osabi == ELFOSABI_NONE
10806 || bed->elf_osabi == ELFOSABI_GNU
10807 || bed->elf_osabi == ELFOSABI_FREEBSD)
10808 elf_tdata (output_bfd)->has_gnu_osabi
10809 |= (elf_tdata (input_bfd)->has_gnu_osabi
10810 & (bfd_link_relocatable (flinfo->info)
10811 ? -1 : ~elf_gnu_osabi_retain));
10813 /* Read the local symbols. */
10814 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10815 if (isymbuf == NULL && locsymcount != 0)
10817 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10818 flinfo->internal_syms,
10819 flinfo->external_syms,
10820 flinfo->locsym_shndx);
10821 if (isymbuf == NULL)
10822 return FALSE;
10825 /* Find local symbol sections and adjust values of symbols in
10826 SEC_MERGE sections. Write out those local symbols we know are
10827 going into the output file. */
10828 isymend = isymbuf + locsymcount;
10829 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10830 isym < isymend;
10831 isym++, pindex++, ppsection++)
10833 asection *isec;
10834 const char *name;
10835 Elf_Internal_Sym osym;
10836 long indx;
10837 int ret;
10839 *pindex = -1;
10841 if (elf_bad_symtab (input_bfd))
10843 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10845 *ppsection = NULL;
10846 continue;
10850 if (isym->st_shndx == SHN_UNDEF)
10851 isec = bfd_und_section_ptr;
10852 else if (isym->st_shndx == SHN_ABS)
10853 isec = bfd_abs_section_ptr;
10854 else if (isym->st_shndx == SHN_COMMON)
10855 isec = bfd_com_section_ptr;
10856 else
10858 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10859 if (isec == NULL)
10861 /* Don't attempt to output symbols with st_shnx in the
10862 reserved range other than SHN_ABS and SHN_COMMON. */
10863 isec = bfd_und_section_ptr;
10865 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10866 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10867 isym->st_value =
10868 _bfd_merged_section_offset (output_bfd, &isec,
10869 elf_section_data (isec)->sec_info,
10870 isym->st_value);
10873 *ppsection = isec;
10875 /* Don't output the first, undefined, symbol. In fact, don't
10876 output any undefined local symbol. */
10877 if (isec == bfd_und_section_ptr)
10878 continue;
10880 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10882 /* We never output section symbols. Instead, we use the
10883 section symbol of the corresponding section in the output
10884 file. */
10885 continue;
10888 /* If we are stripping all symbols, we don't want to output this
10889 one. */
10890 if (flinfo->info->strip == strip_all)
10891 continue;
10893 /* If we are discarding all local symbols, we don't want to
10894 output this one. If we are generating a relocatable output
10895 file, then some of the local symbols may be required by
10896 relocs; we output them below as we discover that they are
10897 needed. */
10898 if (flinfo->info->discard == discard_all)
10899 continue;
10901 /* If this symbol is defined in a section which we are
10902 discarding, we don't need to keep it. */
10903 if (isym->st_shndx != SHN_UNDEF
10904 && isym->st_shndx < SHN_LORESERVE
10905 && isec->output_section == NULL
10906 && flinfo->info->non_contiguous_regions
10907 && flinfo->info->non_contiguous_regions_warnings)
10909 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10910 "discards section `%s' from '%s'\n"),
10911 isec->name, bfd_get_filename (isec->owner));
10912 continue;
10915 if (isym->st_shndx != SHN_UNDEF
10916 && isym->st_shndx < SHN_LORESERVE
10917 && bfd_section_removed_from_list (output_bfd,
10918 isec->output_section))
10919 continue;
10921 /* Get the name of the symbol. */
10922 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10923 isym->st_name);
10924 if (name == NULL)
10925 return FALSE;
10927 /* See if we are discarding symbols with this name. */
10928 if ((flinfo->info->strip == strip_some
10929 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10930 == NULL))
10931 || (((flinfo->info->discard == discard_sec_merge
10932 && (isec->flags & SEC_MERGE)
10933 && !bfd_link_relocatable (flinfo->info))
10934 || flinfo->info->discard == discard_l)
10935 && bfd_is_local_label_name (input_bfd, name)))
10936 continue;
10938 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10940 if (input_bfd->lto_output)
10941 /* -flto puts a temp file name here. This means builds
10942 are not reproducible. Discard the symbol. */
10943 continue;
10944 have_file_sym = TRUE;
10945 flinfo->filesym_count += 1;
10947 if (!have_file_sym)
10949 /* In the absence of debug info, bfd_find_nearest_line uses
10950 FILE symbols to determine the source file for local
10951 function symbols. Provide a FILE symbol here if input
10952 files lack such, so that their symbols won't be
10953 associated with a previous input file. It's not the
10954 source file, but the best we can do. */
10955 have_file_sym = TRUE;
10956 flinfo->filesym_count += 1;
10957 memset (&osym, 0, sizeof (osym));
10958 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10959 osym.st_shndx = SHN_ABS;
10960 if (!elf_link_output_symstrtab (flinfo,
10961 (input_bfd->lto_output ? NULL
10962 : bfd_get_filename (input_bfd)),
10963 &osym, bfd_abs_section_ptr,
10964 NULL))
10965 return FALSE;
10968 osym = *isym;
10970 /* Adjust the section index for the output file. */
10971 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10972 isec->output_section);
10973 if (osym.st_shndx == SHN_BAD)
10974 return FALSE;
10976 /* ELF symbols in relocatable files are section relative, but
10977 in executable files they are virtual addresses. Note that
10978 this code assumes that all ELF sections have an associated
10979 BFD section with a reasonable value for output_offset; below
10980 we assume that they also have a reasonable value for
10981 output_section. Any special sections must be set up to meet
10982 these requirements. */
10983 osym.st_value += isec->output_offset;
10984 if (!bfd_link_relocatable (flinfo->info))
10986 osym.st_value += isec->output_section->vma;
10987 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10989 /* STT_TLS symbols are relative to PT_TLS segment base. */
10990 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10991 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10992 else
10993 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10994 STT_NOTYPE);
10998 indx = bfd_get_symcount (output_bfd);
10999 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11000 if (ret == 0)
11001 return FALSE;
11002 else if (ret == 1)
11003 *pindex = indx;
11006 if (bed->s->arch_size == 32)
11008 r_type_mask = 0xff;
11009 r_sym_shift = 8;
11010 address_size = 4;
11012 else
11014 r_type_mask = 0xffffffff;
11015 r_sym_shift = 32;
11016 address_size = 8;
11019 /* Relocate the contents of each section. */
11020 sym_hashes = elf_sym_hashes (input_bfd);
11021 for (o = input_bfd->sections; o != NULL; o = o->next)
11023 bfd_byte *contents;
11025 if (! o->linker_mark)
11027 /* This section was omitted from the link. */
11028 continue;
11031 if (!flinfo->info->resolve_section_groups
11032 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11034 /* Deal with the group signature symbol. */
11035 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11036 unsigned long symndx = sec_data->this_hdr.sh_info;
11037 asection *osec = o->output_section;
11039 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11040 if (symndx >= locsymcount
11041 || (elf_bad_symtab (input_bfd)
11042 && flinfo->sections[symndx] == NULL))
11044 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11045 while (h->root.type == bfd_link_hash_indirect
11046 || h->root.type == bfd_link_hash_warning)
11047 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11048 /* Arrange for symbol to be output. */
11049 h->indx = -2;
11050 elf_section_data (osec)->this_hdr.sh_info = -2;
11052 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11054 /* We'll use the output section target_index. */
11055 asection *sec = flinfo->sections[symndx]->output_section;
11056 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11058 else
11060 if (flinfo->indices[symndx] == -1)
11062 /* Otherwise output the local symbol now. */
11063 Elf_Internal_Sym sym = isymbuf[symndx];
11064 asection *sec = flinfo->sections[symndx]->output_section;
11065 const char *name;
11066 long indx;
11067 int ret;
11069 name = bfd_elf_string_from_elf_section (input_bfd,
11070 symtab_hdr->sh_link,
11071 sym.st_name);
11072 if (name == NULL)
11073 return FALSE;
11075 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11076 sec);
11077 if (sym.st_shndx == SHN_BAD)
11078 return FALSE;
11080 sym.st_value += o->output_offset;
11082 indx = bfd_get_symcount (output_bfd);
11083 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11084 NULL);
11085 if (ret == 0)
11086 return FALSE;
11087 else if (ret == 1)
11088 flinfo->indices[symndx] = indx;
11089 else
11090 abort ();
11092 elf_section_data (osec)->this_hdr.sh_info
11093 = flinfo->indices[symndx];
11097 if ((o->flags & SEC_HAS_CONTENTS) == 0
11098 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11099 continue;
11101 if ((o->flags & SEC_LINKER_CREATED) != 0)
11103 /* Section was created by _bfd_elf_link_create_dynamic_sections
11104 or somesuch. */
11105 continue;
11108 /* Get the contents of the section. They have been cached by a
11109 relaxation routine. Note that o is a section in an input
11110 file, so the contents field will not have been set by any of
11111 the routines which work on output files. */
11112 if (elf_section_data (o)->this_hdr.contents != NULL)
11114 contents = elf_section_data (o)->this_hdr.contents;
11115 if (bed->caches_rawsize
11116 && o->rawsize != 0
11117 && o->rawsize < o->size)
11119 memcpy (flinfo->contents, contents, o->rawsize);
11120 contents = flinfo->contents;
11123 else
11125 contents = flinfo->contents;
11126 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11127 return FALSE;
11130 if ((o->flags & SEC_RELOC) != 0)
11132 Elf_Internal_Rela *internal_relocs;
11133 Elf_Internal_Rela *rel, *relend;
11134 int action_discarded;
11135 int ret;
11137 /* Get the swapped relocs. */
11138 internal_relocs
11139 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11140 flinfo->internal_relocs, FALSE);
11141 if (internal_relocs == NULL
11142 && o->reloc_count > 0)
11143 return FALSE;
11145 /* We need to reverse-copy input .ctors/.dtors sections if
11146 they are placed in .init_array/.finit_array for output. */
11147 if (o->size > address_size
11148 && ((strncmp (o->name, ".ctors", 6) == 0
11149 && strcmp (o->output_section->name,
11150 ".init_array") == 0)
11151 || (strncmp (o->name, ".dtors", 6) == 0
11152 && strcmp (o->output_section->name,
11153 ".fini_array") == 0))
11154 && (o->name[6] == 0 || o->name[6] == '.'))
11156 if (o->size * bed->s->int_rels_per_ext_rel
11157 != o->reloc_count * address_size)
11159 _bfd_error_handler
11160 /* xgettext:c-format */
11161 (_("error: %pB: size of section %pA is not "
11162 "multiple of address size"),
11163 input_bfd, o);
11164 bfd_set_error (bfd_error_bad_value);
11165 return FALSE;
11167 o->flags |= SEC_ELF_REVERSE_COPY;
11170 action_discarded = -1;
11171 if (!elf_section_ignore_discarded_relocs (o))
11172 action_discarded = (*bed->action_discarded) (o);
11174 /* Run through the relocs evaluating complex reloc symbols and
11175 looking for relocs against symbols from discarded sections
11176 or section symbols from removed link-once sections.
11177 Complain about relocs against discarded sections. Zero
11178 relocs against removed link-once sections. */
11180 rel = internal_relocs;
11181 relend = rel + o->reloc_count;
11182 for ( ; rel < relend; rel++)
11184 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11185 unsigned int s_type;
11186 asection **ps, *sec;
11187 struct elf_link_hash_entry *h = NULL;
11188 const char *sym_name;
11190 if (r_symndx == STN_UNDEF)
11191 continue;
11193 if (r_symndx >= locsymcount
11194 || (elf_bad_symtab (input_bfd)
11195 && flinfo->sections[r_symndx] == NULL))
11197 h = sym_hashes[r_symndx - extsymoff];
11199 /* Badly formatted input files can contain relocs that
11200 reference non-existant symbols. Check here so that
11201 we do not seg fault. */
11202 if (h == NULL)
11204 _bfd_error_handler
11205 /* xgettext:c-format */
11206 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11207 "that references a non-existent global symbol"),
11208 input_bfd, (uint64_t) rel->r_info, o);
11209 bfd_set_error (bfd_error_bad_value);
11210 return FALSE;
11213 while (h->root.type == bfd_link_hash_indirect
11214 || h->root.type == bfd_link_hash_warning)
11215 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11217 s_type = h->type;
11219 /* If a plugin symbol is referenced from a non-IR file,
11220 mark the symbol as undefined. Note that the
11221 linker may attach linker created dynamic sections
11222 to the plugin bfd. Symbols defined in linker
11223 created sections are not plugin symbols. */
11224 if ((h->root.non_ir_ref_regular
11225 || h->root.non_ir_ref_dynamic)
11226 && (h->root.type == bfd_link_hash_defined
11227 || h->root.type == bfd_link_hash_defweak)
11228 && (h->root.u.def.section->flags
11229 & SEC_LINKER_CREATED) == 0
11230 && h->root.u.def.section->owner != NULL
11231 && (h->root.u.def.section->owner->flags
11232 & BFD_PLUGIN) != 0)
11234 h->root.type = bfd_link_hash_undefined;
11235 h->root.u.undef.abfd = h->root.u.def.section->owner;
11238 ps = NULL;
11239 if (h->root.type == bfd_link_hash_defined
11240 || h->root.type == bfd_link_hash_defweak)
11241 ps = &h->root.u.def.section;
11243 sym_name = h->root.root.string;
11245 else
11247 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11249 s_type = ELF_ST_TYPE (sym->st_info);
11250 ps = &flinfo->sections[r_symndx];
11251 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11252 sym, *ps);
11255 if ((s_type == STT_RELC || s_type == STT_SRELC)
11256 && !bfd_link_relocatable (flinfo->info))
11258 bfd_vma val;
11259 bfd_vma dot = (rel->r_offset
11260 + o->output_offset + o->output_section->vma);
11261 #ifdef DEBUG
11262 printf ("Encountered a complex symbol!");
11263 printf (" (input_bfd %s, section %s, reloc %ld\n",
11264 bfd_get_filename (input_bfd), o->name,
11265 (long) (rel - internal_relocs));
11266 printf (" symbol: idx %8.8lx, name %s\n",
11267 r_symndx, sym_name);
11268 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11269 (unsigned long) rel->r_info,
11270 (unsigned long) rel->r_offset);
11271 #endif
11272 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11273 isymbuf, locsymcount, s_type == STT_SRELC))
11274 return FALSE;
11276 /* Symbol evaluated OK. Update to absolute value. */
11277 set_symbol_value (input_bfd, isymbuf, locsymcount,
11278 r_symndx, val);
11279 continue;
11282 if (action_discarded != -1 && ps != NULL)
11284 /* Complain if the definition comes from a
11285 discarded section. */
11286 if ((sec = *ps) != NULL && discarded_section (sec))
11288 BFD_ASSERT (r_symndx != STN_UNDEF);
11289 if (action_discarded & COMPLAIN)
11290 (*flinfo->info->callbacks->einfo)
11291 /* xgettext:c-format */
11292 (_("%X`%s' referenced in section `%pA' of %pB: "
11293 "defined in discarded section `%pA' of %pB\n"),
11294 sym_name, o, input_bfd, sec, sec->owner);
11296 /* Try to do the best we can to support buggy old
11297 versions of gcc. Pretend that the symbol is
11298 really defined in the kept linkonce section.
11299 FIXME: This is quite broken. Modifying the
11300 symbol here means we will be changing all later
11301 uses of the symbol, not just in this section. */
11302 if (action_discarded & PRETEND)
11304 asection *kept;
11306 kept = _bfd_elf_check_kept_section (sec,
11307 flinfo->info);
11308 if (kept != NULL)
11310 *ps = kept;
11311 continue;
11318 /* Relocate the section by invoking a back end routine.
11320 The back end routine is responsible for adjusting the
11321 section contents as necessary, and (if using Rela relocs
11322 and generating a relocatable output file) adjusting the
11323 reloc addend as necessary.
11325 The back end routine does not have to worry about setting
11326 the reloc address or the reloc symbol index.
11328 The back end routine is given a pointer to the swapped in
11329 internal symbols, and can access the hash table entries
11330 for the external symbols via elf_sym_hashes (input_bfd).
11332 When generating relocatable output, the back end routine
11333 must handle STB_LOCAL/STT_SECTION symbols specially. The
11334 output symbol is going to be a section symbol
11335 corresponding to the output section, which will require
11336 the addend to be adjusted. */
11338 ret = (*relocate_section) (output_bfd, flinfo->info,
11339 input_bfd, o, contents,
11340 internal_relocs,
11341 isymbuf,
11342 flinfo->sections);
11343 if (!ret)
11344 return FALSE;
11346 if (ret == 2
11347 || bfd_link_relocatable (flinfo->info)
11348 || flinfo->info->emitrelocations)
11350 Elf_Internal_Rela *irela;
11351 Elf_Internal_Rela *irelaend, *irelamid;
11352 bfd_vma last_offset;
11353 struct elf_link_hash_entry **rel_hash;
11354 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11355 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11356 unsigned int next_erel;
11357 bfd_boolean rela_normal;
11358 struct bfd_elf_section_data *esdi, *esdo;
11360 esdi = elf_section_data (o);
11361 esdo = elf_section_data (o->output_section);
11362 rela_normal = FALSE;
11364 /* Adjust the reloc addresses and symbol indices. */
11366 irela = internal_relocs;
11367 irelaend = irela + o->reloc_count;
11368 rel_hash = esdo->rel.hashes + esdo->rel.count;
11369 /* We start processing the REL relocs, if any. When we reach
11370 IRELAMID in the loop, we switch to the RELA relocs. */
11371 irelamid = irela;
11372 if (esdi->rel.hdr != NULL)
11373 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11374 * bed->s->int_rels_per_ext_rel);
11375 rel_hash_list = rel_hash;
11376 rela_hash_list = NULL;
11377 last_offset = o->output_offset;
11378 if (!bfd_link_relocatable (flinfo->info))
11379 last_offset += o->output_section->vma;
11380 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11382 unsigned long r_symndx;
11383 asection *sec;
11384 Elf_Internal_Sym sym;
11386 if (next_erel == bed->s->int_rels_per_ext_rel)
11388 rel_hash++;
11389 next_erel = 0;
11392 if (irela == irelamid)
11394 rel_hash = esdo->rela.hashes + esdo->rela.count;
11395 rela_hash_list = rel_hash;
11396 rela_normal = bed->rela_normal;
11399 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11400 flinfo->info, o,
11401 irela->r_offset);
11402 if (irela->r_offset >= (bfd_vma) -2)
11404 /* This is a reloc for a deleted entry or somesuch.
11405 Turn it into an R_*_NONE reloc, at the same
11406 offset as the last reloc. elf_eh_frame.c and
11407 bfd_elf_discard_info rely on reloc offsets
11408 being ordered. */
11409 irela->r_offset = last_offset;
11410 irela->r_info = 0;
11411 irela->r_addend = 0;
11412 continue;
11415 irela->r_offset += o->output_offset;
11417 /* Relocs in an executable have to be virtual addresses. */
11418 if (!bfd_link_relocatable (flinfo->info))
11419 irela->r_offset += o->output_section->vma;
11421 last_offset = irela->r_offset;
11423 r_symndx = irela->r_info >> r_sym_shift;
11424 if (r_symndx == STN_UNDEF)
11425 continue;
11427 if (r_symndx >= locsymcount
11428 || (elf_bad_symtab (input_bfd)
11429 && flinfo->sections[r_symndx] == NULL))
11431 struct elf_link_hash_entry *rh;
11432 unsigned long indx;
11434 /* This is a reloc against a global symbol. We
11435 have not yet output all the local symbols, so
11436 we do not know the symbol index of any global
11437 symbol. We set the rel_hash entry for this
11438 reloc to point to the global hash table entry
11439 for this symbol. The symbol index is then
11440 set at the end of bfd_elf_final_link. */
11441 indx = r_symndx - extsymoff;
11442 rh = elf_sym_hashes (input_bfd)[indx];
11443 while (rh->root.type == bfd_link_hash_indirect
11444 || rh->root.type == bfd_link_hash_warning)
11445 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11447 /* Setting the index to -2 tells
11448 elf_link_output_extsym that this symbol is
11449 used by a reloc. */
11450 BFD_ASSERT (rh->indx < 0);
11451 rh->indx = -2;
11452 *rel_hash = rh;
11454 continue;
11457 /* This is a reloc against a local symbol. */
11459 *rel_hash = NULL;
11460 sym = isymbuf[r_symndx];
11461 sec = flinfo->sections[r_symndx];
11462 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11464 /* I suppose the backend ought to fill in the
11465 section of any STT_SECTION symbol against a
11466 processor specific section. */
11467 r_symndx = STN_UNDEF;
11468 if (bfd_is_abs_section (sec))
11470 else if (sec == NULL || sec->owner == NULL)
11472 bfd_set_error (bfd_error_bad_value);
11473 return FALSE;
11475 else
11477 asection *osec = sec->output_section;
11479 /* If we have discarded a section, the output
11480 section will be the absolute section. In
11481 case of discarded SEC_MERGE sections, use
11482 the kept section. relocate_section should
11483 have already handled discarded linkonce
11484 sections. */
11485 if (bfd_is_abs_section (osec)
11486 && sec->kept_section != NULL
11487 && sec->kept_section->output_section != NULL)
11489 osec = sec->kept_section->output_section;
11490 irela->r_addend -= osec->vma;
11493 if (!bfd_is_abs_section (osec))
11495 r_symndx = osec->target_index;
11496 if (r_symndx == STN_UNDEF)
11498 irela->r_addend += osec->vma;
11499 osec = _bfd_nearby_section (output_bfd, osec,
11500 osec->vma);
11501 irela->r_addend -= osec->vma;
11502 r_symndx = osec->target_index;
11507 /* Adjust the addend according to where the
11508 section winds up in the output section. */
11509 if (rela_normal)
11510 irela->r_addend += sec->output_offset;
11512 else
11514 if (flinfo->indices[r_symndx] == -1)
11516 unsigned long shlink;
11517 const char *name;
11518 asection *osec;
11519 long indx;
11521 if (flinfo->info->strip == strip_all)
11523 /* You can't do ld -r -s. */
11524 bfd_set_error (bfd_error_invalid_operation);
11525 return FALSE;
11528 /* This symbol was skipped earlier, but
11529 since it is needed by a reloc, we
11530 must output it now. */
11531 shlink = symtab_hdr->sh_link;
11532 name = (bfd_elf_string_from_elf_section
11533 (input_bfd, shlink, sym.st_name));
11534 if (name == NULL)
11535 return FALSE;
11537 osec = sec->output_section;
11538 sym.st_shndx =
11539 _bfd_elf_section_from_bfd_section (output_bfd,
11540 osec);
11541 if (sym.st_shndx == SHN_BAD)
11542 return FALSE;
11544 sym.st_value += sec->output_offset;
11545 if (!bfd_link_relocatable (flinfo->info))
11547 sym.st_value += osec->vma;
11548 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11550 struct elf_link_hash_table *htab
11551 = elf_hash_table (flinfo->info);
11553 /* STT_TLS symbols are relative to PT_TLS
11554 segment base. */
11555 if (htab->tls_sec != NULL)
11556 sym.st_value -= htab->tls_sec->vma;
11557 else
11558 sym.st_info
11559 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11560 STT_NOTYPE);
11564 indx = bfd_get_symcount (output_bfd);
11565 ret = elf_link_output_symstrtab (flinfo, name,
11566 &sym, sec,
11567 NULL);
11568 if (ret == 0)
11569 return FALSE;
11570 else if (ret == 1)
11571 flinfo->indices[r_symndx] = indx;
11572 else
11573 abort ();
11576 r_symndx = flinfo->indices[r_symndx];
11579 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11580 | (irela->r_info & r_type_mask));
11583 /* Swap out the relocs. */
11584 input_rel_hdr = esdi->rel.hdr;
11585 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11587 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11588 input_rel_hdr,
11589 internal_relocs,
11590 rel_hash_list))
11591 return FALSE;
11592 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11593 * bed->s->int_rels_per_ext_rel);
11594 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11597 input_rela_hdr = esdi->rela.hdr;
11598 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11600 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11601 input_rela_hdr,
11602 internal_relocs,
11603 rela_hash_list))
11604 return FALSE;
11609 /* Write out the modified section contents. */
11610 if (bed->elf_backend_write_section
11611 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11612 contents))
11614 /* Section written out. */
11616 else switch (o->sec_info_type)
11618 case SEC_INFO_TYPE_STABS:
11619 if (! (_bfd_write_section_stabs
11620 (output_bfd,
11621 &elf_hash_table (flinfo->info)->stab_info,
11622 o, &elf_section_data (o)->sec_info, contents)))
11623 return FALSE;
11624 break;
11625 case SEC_INFO_TYPE_MERGE:
11626 if (! _bfd_write_merged_section (output_bfd, o,
11627 elf_section_data (o)->sec_info))
11628 return FALSE;
11629 break;
11630 case SEC_INFO_TYPE_EH_FRAME:
11632 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11633 o, contents))
11634 return FALSE;
11636 break;
11637 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11639 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11640 flinfo->info,
11641 o, contents))
11642 return FALSE;
11644 break;
11645 default:
11647 if (! (o->flags & SEC_EXCLUDE))
11649 file_ptr offset = (file_ptr) o->output_offset;
11650 bfd_size_type todo = o->size;
11652 offset *= bfd_octets_per_byte (output_bfd, o);
11654 if ((o->flags & SEC_ELF_REVERSE_COPY))
11656 /* Reverse-copy input section to output. */
11659 todo -= address_size;
11660 if (! bfd_set_section_contents (output_bfd,
11661 o->output_section,
11662 contents + todo,
11663 offset,
11664 address_size))
11665 return FALSE;
11666 if (todo == 0)
11667 break;
11668 offset += address_size;
11670 while (1);
11672 else if (! bfd_set_section_contents (output_bfd,
11673 o->output_section,
11674 contents,
11675 offset, todo))
11676 return FALSE;
11679 break;
11683 return TRUE;
11686 /* Generate a reloc when linking an ELF file. This is a reloc
11687 requested by the linker, and does not come from any input file. This
11688 is used to build constructor and destructor tables when linking
11689 with -Ur. */
11691 static bfd_boolean
11692 elf_reloc_link_order (bfd *output_bfd,
11693 struct bfd_link_info *info,
11694 asection *output_section,
11695 struct bfd_link_order *link_order)
11697 reloc_howto_type *howto;
11698 long indx;
11699 bfd_vma offset;
11700 bfd_vma addend;
11701 struct bfd_elf_section_reloc_data *reldata;
11702 struct elf_link_hash_entry **rel_hash_ptr;
11703 Elf_Internal_Shdr *rel_hdr;
11704 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11705 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11706 bfd_byte *erel;
11707 unsigned int i;
11708 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11710 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11711 if (howto == NULL)
11713 bfd_set_error (bfd_error_bad_value);
11714 return FALSE;
11717 addend = link_order->u.reloc.p->addend;
11719 if (esdo->rel.hdr)
11720 reldata = &esdo->rel;
11721 else if (esdo->rela.hdr)
11722 reldata = &esdo->rela;
11723 else
11725 reldata = NULL;
11726 BFD_ASSERT (0);
11729 /* Figure out the symbol index. */
11730 rel_hash_ptr = reldata->hashes + reldata->count;
11731 if (link_order->type == bfd_section_reloc_link_order)
11733 indx = link_order->u.reloc.p->u.section->target_index;
11734 BFD_ASSERT (indx != 0);
11735 *rel_hash_ptr = NULL;
11737 else
11739 struct elf_link_hash_entry *h;
11741 /* Treat a reloc against a defined symbol as though it were
11742 actually against the section. */
11743 h = ((struct elf_link_hash_entry *)
11744 bfd_wrapped_link_hash_lookup (output_bfd, info,
11745 link_order->u.reloc.p->u.name,
11746 FALSE, FALSE, TRUE));
11747 if (h != NULL
11748 && (h->root.type == bfd_link_hash_defined
11749 || h->root.type == bfd_link_hash_defweak))
11751 asection *section;
11753 section = h->root.u.def.section;
11754 indx = section->output_section->target_index;
11755 *rel_hash_ptr = NULL;
11756 /* It seems that we ought to add the symbol value to the
11757 addend here, but in practice it has already been added
11758 because it was passed to constructor_callback. */
11759 addend += section->output_section->vma + section->output_offset;
11761 else if (h != NULL)
11763 /* Setting the index to -2 tells elf_link_output_extsym that
11764 this symbol is used by a reloc. */
11765 h->indx = -2;
11766 *rel_hash_ptr = h;
11767 indx = 0;
11769 else
11771 (*info->callbacks->unattached_reloc)
11772 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11773 indx = 0;
11777 /* If this is an inplace reloc, we must write the addend into the
11778 object file. */
11779 if (howto->partial_inplace && addend != 0)
11781 bfd_size_type size;
11782 bfd_reloc_status_type rstat;
11783 bfd_byte *buf;
11784 bfd_boolean ok;
11785 const char *sym_name;
11786 bfd_size_type octets;
11788 size = (bfd_size_type) bfd_get_reloc_size (howto);
11789 buf = (bfd_byte *) bfd_zmalloc (size);
11790 if (buf == NULL && size != 0)
11791 return FALSE;
11792 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11793 switch (rstat)
11795 case bfd_reloc_ok:
11796 break;
11798 default:
11799 case bfd_reloc_outofrange:
11800 abort ();
11802 case bfd_reloc_overflow:
11803 if (link_order->type == bfd_section_reloc_link_order)
11804 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11805 else
11806 sym_name = link_order->u.reloc.p->u.name;
11807 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11808 howto->name, addend, NULL, NULL,
11809 (bfd_vma) 0);
11810 break;
11813 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11814 output_section);
11815 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11816 octets, size);
11817 free (buf);
11818 if (! ok)
11819 return FALSE;
11822 /* The address of a reloc is relative to the section in a
11823 relocatable file, and is a virtual address in an executable
11824 file. */
11825 offset = link_order->offset;
11826 if (! bfd_link_relocatable (info))
11827 offset += output_section->vma;
11829 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11831 irel[i].r_offset = offset;
11832 irel[i].r_info = 0;
11833 irel[i].r_addend = 0;
11835 if (bed->s->arch_size == 32)
11836 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11837 else
11838 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11840 rel_hdr = reldata->hdr;
11841 erel = rel_hdr->contents;
11842 if (rel_hdr->sh_type == SHT_REL)
11844 erel += reldata->count * bed->s->sizeof_rel;
11845 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11847 else
11849 irel[0].r_addend = addend;
11850 erel += reldata->count * bed->s->sizeof_rela;
11851 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11854 ++reldata->count;
11856 return TRUE;
11860 /* Compare two sections based on the locations of the sections they are
11861 linked to. Used by elf_fixup_link_order. */
11863 static int
11864 compare_link_order (const void *a, const void *b)
11866 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11867 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11868 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11869 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11870 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11871 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11873 if (apos < bpos)
11874 return -1;
11875 if (apos > bpos)
11876 return 1;
11878 /* The only way we should get matching LMAs is when the first of two
11879 sections has zero size. */
11880 if (asec->size < bsec->size)
11881 return -1;
11882 if (asec->size > bsec->size)
11883 return 1;
11885 /* If they are both zero size then they almost certainly have the same
11886 VMA and thus are not ordered with respect to each other. Test VMA
11887 anyway, and fall back to id to make the result reproducible across
11888 qsort implementations. */
11889 apos = asec->output_section->vma + asec->output_offset;
11890 bpos = bsec->output_section->vma + bsec->output_offset;
11891 if (apos < bpos)
11892 return -1;
11893 if (apos > bpos)
11894 return 1;
11896 return asec->id - bsec->id;
11900 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11901 order as their linked sections. Returns false if this could not be done
11902 because an output section includes both ordered and unordered
11903 sections. Ideally we'd do this in the linker proper. */
11905 static bfd_boolean
11906 elf_fixup_link_order (bfd *abfd, asection *o)
11908 size_t seen_linkorder;
11909 size_t seen_other;
11910 size_t n;
11911 struct bfd_link_order *p;
11912 bfd *sub;
11913 struct bfd_link_order **sections;
11914 asection *other_sec, *linkorder_sec;
11915 bfd_vma offset; /* Octets. */
11917 other_sec = NULL;
11918 linkorder_sec = NULL;
11919 seen_other = 0;
11920 seen_linkorder = 0;
11921 for (p = o->map_head.link_order; p != NULL; p = p->next)
11923 if (p->type == bfd_indirect_link_order)
11925 asection *s = p->u.indirect.section;
11926 sub = s->owner;
11927 if ((s->flags & SEC_LINKER_CREATED) == 0
11928 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11929 && elf_section_data (s) != NULL
11930 && elf_linked_to_section (s) != NULL)
11932 seen_linkorder++;
11933 linkorder_sec = s;
11935 else
11937 seen_other++;
11938 other_sec = s;
11941 else
11942 seen_other++;
11944 if (seen_other && seen_linkorder)
11946 if (other_sec && linkorder_sec)
11947 _bfd_error_handler
11948 /* xgettext:c-format */
11949 (_("%pA has both ordered [`%pA' in %pB] "
11950 "and unordered [`%pA' in %pB] sections"),
11951 o, linkorder_sec, linkorder_sec->owner,
11952 other_sec, other_sec->owner);
11953 else
11954 _bfd_error_handler
11955 (_("%pA has both ordered and unordered sections"), o);
11956 bfd_set_error (bfd_error_bad_value);
11957 return FALSE;
11961 if (!seen_linkorder)
11962 return TRUE;
11964 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11965 if (sections == NULL)
11966 return FALSE;
11968 seen_linkorder = 0;
11969 for (p = o->map_head.link_order; p != NULL; p = p->next)
11970 sections[seen_linkorder++] = p;
11972 /* Sort the input sections in the order of their linked section. */
11973 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11975 /* Change the offsets of the sections. */
11976 offset = 0;
11977 for (n = 0; n < seen_linkorder; n++)
11979 bfd_vma mask;
11980 asection *s = sections[n]->u.indirect.section;
11981 unsigned int opb = bfd_octets_per_byte (abfd, s);
11983 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
11984 offset = (offset + ~mask) & mask;
11985 sections[n]->offset = s->output_offset = offset / opb;
11986 offset += sections[n]->size;
11989 free (sections);
11990 return TRUE;
11993 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11994 Returns TRUE upon success, FALSE otherwise. */
11996 static bfd_boolean
11997 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11999 bfd_boolean ret = FALSE;
12000 bfd *implib_bfd;
12001 const struct elf_backend_data *bed;
12002 flagword flags;
12003 enum bfd_architecture arch;
12004 unsigned int mach;
12005 asymbol **sympp = NULL;
12006 long symsize;
12007 long symcount;
12008 long src_count;
12009 elf_symbol_type *osymbuf;
12010 size_t amt;
12012 implib_bfd = info->out_implib_bfd;
12013 bed = get_elf_backend_data (abfd);
12015 if (!bfd_set_format (implib_bfd, bfd_object))
12016 return FALSE;
12018 /* Use flag from executable but make it a relocatable object. */
12019 flags = bfd_get_file_flags (abfd);
12020 flags &= ~HAS_RELOC;
12021 if (!bfd_set_start_address (implib_bfd, 0)
12022 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12023 return FALSE;
12025 /* Copy architecture of output file to import library file. */
12026 arch = bfd_get_arch (abfd);
12027 mach = bfd_get_mach (abfd);
12028 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12029 && (abfd->target_defaulted
12030 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12031 return FALSE;
12033 /* Get symbol table size. */
12034 symsize = bfd_get_symtab_upper_bound (abfd);
12035 if (symsize < 0)
12036 return FALSE;
12038 /* Read in the symbol table. */
12039 sympp = (asymbol **) bfd_malloc (symsize);
12040 if (sympp == NULL)
12041 return FALSE;
12043 symcount = bfd_canonicalize_symtab (abfd, sympp);
12044 if (symcount < 0)
12045 goto free_sym_buf;
12047 /* Allow the BFD backend to copy any private header data it
12048 understands from the output BFD to the import library BFD. */
12049 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12050 goto free_sym_buf;
12052 /* Filter symbols to appear in the import library. */
12053 if (bed->elf_backend_filter_implib_symbols)
12054 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12055 symcount);
12056 else
12057 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12058 if (symcount == 0)
12060 bfd_set_error (bfd_error_no_symbols);
12061 _bfd_error_handler (_("%pB: no symbol found for import library"),
12062 implib_bfd);
12063 goto free_sym_buf;
12067 /* Make symbols absolute. */
12068 amt = symcount * sizeof (*osymbuf);
12069 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12070 if (osymbuf == NULL)
12071 goto free_sym_buf;
12073 for (src_count = 0; src_count < symcount; src_count++)
12075 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12076 sizeof (*osymbuf));
12077 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12078 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12079 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12080 osymbuf[src_count].internal_elf_sym.st_value =
12081 osymbuf[src_count].symbol.value;
12082 sympp[src_count] = &osymbuf[src_count].symbol;
12085 bfd_set_symtab (implib_bfd, sympp, symcount);
12087 /* Allow the BFD backend to copy any private data it understands
12088 from the output BFD to the import library BFD. This is done last
12089 to permit the routine to look at the filtered symbol table. */
12090 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12091 goto free_sym_buf;
12093 if (!bfd_close (implib_bfd))
12094 goto free_sym_buf;
12096 ret = TRUE;
12098 free_sym_buf:
12099 free (sympp);
12100 return ret;
12103 static void
12104 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12106 asection *o;
12108 if (flinfo->symstrtab != NULL)
12109 _bfd_elf_strtab_free (flinfo->symstrtab);
12110 free (flinfo->contents);
12111 free (flinfo->external_relocs);
12112 free (flinfo->internal_relocs);
12113 free (flinfo->external_syms);
12114 free (flinfo->locsym_shndx);
12115 free (flinfo->internal_syms);
12116 free (flinfo->indices);
12117 free (flinfo->sections);
12118 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12119 free (flinfo->symshndxbuf);
12120 for (o = obfd->sections; o != NULL; o = o->next)
12122 struct bfd_elf_section_data *esdo = elf_section_data (o);
12123 free (esdo->rel.hashes);
12124 free (esdo->rela.hashes);
12128 /* Do the final step of an ELF link. */
12130 bfd_boolean
12131 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12133 bfd_boolean dynamic;
12134 bfd_boolean emit_relocs;
12135 bfd *dynobj;
12136 struct elf_final_link_info flinfo;
12137 asection *o;
12138 struct bfd_link_order *p;
12139 bfd *sub;
12140 bfd_size_type max_contents_size;
12141 bfd_size_type max_external_reloc_size;
12142 bfd_size_type max_internal_reloc_count;
12143 bfd_size_type max_sym_count;
12144 bfd_size_type max_sym_shndx_count;
12145 Elf_Internal_Sym elfsym;
12146 unsigned int i;
12147 Elf_Internal_Shdr *symtab_hdr;
12148 Elf_Internal_Shdr *symtab_shndx_hdr;
12149 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12150 struct elf_outext_info eoinfo;
12151 bfd_boolean merged;
12152 size_t relativecount = 0;
12153 asection *reldyn = 0;
12154 bfd_size_type amt;
12155 asection *attr_section = NULL;
12156 bfd_vma attr_size = 0;
12157 const char *std_attrs_section;
12158 struct elf_link_hash_table *htab = elf_hash_table (info);
12159 bfd_boolean sections_removed;
12160 bfd_boolean ret;
12162 if (!is_elf_hash_table (htab))
12163 return FALSE;
12165 if (bfd_link_pic (info))
12166 abfd->flags |= DYNAMIC;
12168 dynamic = htab->dynamic_sections_created;
12169 dynobj = htab->dynobj;
12171 emit_relocs = (bfd_link_relocatable (info)
12172 || info->emitrelocations);
12174 memset (&flinfo, 0, sizeof (flinfo));
12175 flinfo.info = info;
12176 flinfo.output_bfd = abfd;
12177 flinfo.symstrtab = _bfd_elf_strtab_init ();
12178 if (flinfo.symstrtab == NULL)
12179 return FALSE;
12181 if (! dynamic)
12183 flinfo.hash_sec = NULL;
12184 flinfo.symver_sec = NULL;
12186 else
12188 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12189 /* Note that dynsym_sec can be NULL (on VMS). */
12190 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12191 /* Note that it is OK if symver_sec is NULL. */
12194 if (info->unique_symbol
12195 && !bfd_hash_table_init (&flinfo.local_hash_table,
12196 local_hash_newfunc,
12197 sizeof (struct local_hash_entry)))
12198 return FALSE;
12200 /* The object attributes have been merged. Remove the input
12201 sections from the link, and set the contents of the output
12202 section. */
12203 sections_removed = FALSE;
12204 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12205 for (o = abfd->sections; o != NULL; o = o->next)
12207 bfd_boolean remove_section = FALSE;
12209 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12210 || strcmp (o->name, ".gnu.attributes") == 0)
12212 for (p = o->map_head.link_order; p != NULL; p = p->next)
12214 asection *input_section;
12216 if (p->type != bfd_indirect_link_order)
12217 continue;
12218 input_section = p->u.indirect.section;
12219 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12220 elf_link_input_bfd ignores this section. */
12221 input_section->flags &= ~SEC_HAS_CONTENTS;
12224 attr_size = bfd_elf_obj_attr_size (abfd);
12225 bfd_set_section_size (o, attr_size);
12226 /* Skip this section later on. */
12227 o->map_head.link_order = NULL;
12228 if (attr_size)
12229 attr_section = o;
12230 else
12231 remove_section = TRUE;
12233 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12235 /* Remove empty group section from linker output. */
12236 remove_section = TRUE;
12238 if (remove_section)
12240 o->flags |= SEC_EXCLUDE;
12241 bfd_section_list_remove (abfd, o);
12242 abfd->section_count--;
12243 sections_removed = TRUE;
12246 if (sections_removed)
12247 _bfd_fix_excluded_sec_syms (abfd, info);
12249 /* Count up the number of relocations we will output for each output
12250 section, so that we know the sizes of the reloc sections. We
12251 also figure out some maximum sizes. */
12252 max_contents_size = 0;
12253 max_external_reloc_size = 0;
12254 max_internal_reloc_count = 0;
12255 max_sym_count = 0;
12256 max_sym_shndx_count = 0;
12257 merged = FALSE;
12258 for (o = abfd->sections; o != NULL; o = o->next)
12260 struct bfd_elf_section_data *esdo = elf_section_data (o);
12261 o->reloc_count = 0;
12263 for (p = o->map_head.link_order; p != NULL; p = p->next)
12265 unsigned int reloc_count = 0;
12266 unsigned int additional_reloc_count = 0;
12267 struct bfd_elf_section_data *esdi = NULL;
12269 if (p->type == bfd_section_reloc_link_order
12270 || p->type == bfd_symbol_reloc_link_order)
12271 reloc_count = 1;
12272 else if (p->type == bfd_indirect_link_order)
12274 asection *sec;
12276 sec = p->u.indirect.section;
12278 /* Mark all sections which are to be included in the
12279 link. This will normally be every section. We need
12280 to do this so that we can identify any sections which
12281 the linker has decided to not include. */
12282 sec->linker_mark = TRUE;
12284 if (sec->flags & SEC_MERGE)
12285 merged = TRUE;
12287 if (sec->rawsize > max_contents_size)
12288 max_contents_size = sec->rawsize;
12289 if (sec->size > max_contents_size)
12290 max_contents_size = sec->size;
12292 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12293 && (sec->owner->flags & DYNAMIC) == 0)
12295 size_t sym_count;
12297 /* We are interested in just local symbols, not all
12298 symbols. */
12299 if (elf_bad_symtab (sec->owner))
12300 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12301 / bed->s->sizeof_sym);
12302 else
12303 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12305 if (sym_count > max_sym_count)
12306 max_sym_count = sym_count;
12308 if (sym_count > max_sym_shndx_count
12309 && elf_symtab_shndx_list (sec->owner) != NULL)
12310 max_sym_shndx_count = sym_count;
12312 if (esdo->this_hdr.sh_type == SHT_REL
12313 || esdo->this_hdr.sh_type == SHT_RELA)
12314 /* Some backends use reloc_count in relocation sections
12315 to count particular types of relocs. Of course,
12316 reloc sections themselves can't have relocations. */
12318 else if (emit_relocs)
12320 reloc_count = sec->reloc_count;
12321 if (bed->elf_backend_count_additional_relocs)
12323 int c;
12324 c = (*bed->elf_backend_count_additional_relocs) (sec);
12325 additional_reloc_count += c;
12328 else if (bed->elf_backend_count_relocs)
12329 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12331 esdi = elf_section_data (sec);
12333 if ((sec->flags & SEC_RELOC) != 0)
12335 size_t ext_size = 0;
12337 if (esdi->rel.hdr != NULL)
12338 ext_size = esdi->rel.hdr->sh_size;
12339 if (esdi->rela.hdr != NULL)
12340 ext_size += esdi->rela.hdr->sh_size;
12342 if (ext_size > max_external_reloc_size)
12343 max_external_reloc_size = ext_size;
12344 if (sec->reloc_count > max_internal_reloc_count)
12345 max_internal_reloc_count = sec->reloc_count;
12350 if (reloc_count == 0)
12351 continue;
12353 reloc_count += additional_reloc_count;
12354 o->reloc_count += reloc_count;
12356 if (p->type == bfd_indirect_link_order && emit_relocs)
12358 if (esdi->rel.hdr)
12360 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12361 esdo->rel.count += additional_reloc_count;
12363 if (esdi->rela.hdr)
12365 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12366 esdo->rela.count += additional_reloc_count;
12369 else
12371 if (o->use_rela_p)
12372 esdo->rela.count += reloc_count;
12373 else
12374 esdo->rel.count += reloc_count;
12378 if (o->reloc_count > 0)
12379 o->flags |= SEC_RELOC;
12380 else
12382 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12383 set it (this is probably a bug) and if it is set
12384 assign_section_numbers will create a reloc section. */
12385 o->flags &=~ SEC_RELOC;
12388 /* If the SEC_ALLOC flag is not set, force the section VMA to
12389 zero. This is done in elf_fake_sections as well, but forcing
12390 the VMA to 0 here will ensure that relocs against these
12391 sections are handled correctly. */
12392 if ((o->flags & SEC_ALLOC) == 0
12393 && ! o->user_set_vma)
12394 o->vma = 0;
12397 if (! bfd_link_relocatable (info) && merged)
12398 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12400 /* Figure out the file positions for everything but the symbol table
12401 and the relocs. We set symcount to force assign_section_numbers
12402 to create a symbol table. */
12403 abfd->symcount = info->strip != strip_all || emit_relocs;
12404 BFD_ASSERT (! abfd->output_has_begun);
12405 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12406 goto error_return;
12408 /* Set sizes, and assign file positions for reloc sections. */
12409 for (o = abfd->sections; o != NULL; o = o->next)
12411 struct bfd_elf_section_data *esdo = elf_section_data (o);
12412 if ((o->flags & SEC_RELOC) != 0)
12414 if (esdo->rel.hdr
12415 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12416 goto error_return;
12418 if (esdo->rela.hdr
12419 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12420 goto error_return;
12423 /* _bfd_elf_compute_section_file_positions makes temporary use
12424 of target_index. Reset it. */
12425 o->target_index = 0;
12427 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12428 to count upwards while actually outputting the relocations. */
12429 esdo->rel.count = 0;
12430 esdo->rela.count = 0;
12432 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12433 && !bfd_section_is_ctf (o))
12435 /* Cache the section contents so that they can be compressed
12436 later. Use bfd_malloc since it will be freed by
12437 bfd_compress_section_contents. */
12438 unsigned char *contents = esdo->this_hdr.contents;
12439 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12440 abort ();
12441 contents
12442 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12443 if (contents == NULL)
12444 goto error_return;
12445 esdo->this_hdr.contents = contents;
12449 /* We have now assigned file positions for all the sections except .symtab,
12450 .strtab, and non-loaded reloc and compressed debugging sections. We start
12451 the .symtab section at the current file position, and write directly to it.
12452 We build the .strtab section in memory. */
12453 abfd->symcount = 0;
12454 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12455 /* sh_name is set in prep_headers. */
12456 symtab_hdr->sh_type = SHT_SYMTAB;
12457 /* sh_flags, sh_addr and sh_size all start off zero. */
12458 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12459 /* sh_link is set in assign_section_numbers. */
12460 /* sh_info is set below. */
12461 /* sh_offset is set just below. */
12462 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12464 if (max_sym_count < 20)
12465 max_sym_count = 20;
12466 htab->strtabsize = max_sym_count;
12467 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12468 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12469 if (htab->strtab == NULL)
12470 goto error_return;
12471 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12472 flinfo.symshndxbuf
12473 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12474 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12476 if (info->strip != strip_all || emit_relocs)
12478 bfd_boolean name_local_sections;
12479 const char *name;
12481 file_ptr off = elf_next_file_pos (abfd);
12483 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12485 /* Note that at this point elf_next_file_pos (abfd) is
12486 incorrect. We do not yet know the size of the .symtab section.
12487 We correct next_file_pos below, after we do know the size. */
12489 /* Start writing out the symbol table. The first symbol is always a
12490 dummy symbol. */
12491 elfsym.st_value = 0;
12492 elfsym.st_size = 0;
12493 elfsym.st_info = 0;
12494 elfsym.st_other = 0;
12495 elfsym.st_shndx = SHN_UNDEF;
12496 elfsym.st_target_internal = 0;
12497 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12498 bfd_und_section_ptr, NULL) != 1)
12499 goto error_return;
12501 /* Output a symbol for each section. We output these even if we are
12502 discarding local symbols, since they are used for relocs. These
12503 symbols usually have no names. We store the index of each one in
12504 the index field of the section, so that we can find it again when
12505 outputting relocs. */
12507 name_local_sections
12508 = (bed->elf_backend_name_local_section_symbols
12509 && bed->elf_backend_name_local_section_symbols (abfd));
12511 name = NULL;
12512 elfsym.st_size = 0;
12513 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12514 elfsym.st_other = 0;
12515 elfsym.st_value = 0;
12516 elfsym.st_target_internal = 0;
12517 for (i = 1; i < elf_numsections (abfd); i++)
12519 o = bfd_section_from_elf_index (abfd, i);
12520 if (o != NULL)
12522 o->target_index = bfd_get_symcount (abfd);
12523 elfsym.st_shndx = i;
12524 if (!bfd_link_relocatable (info))
12525 elfsym.st_value = o->vma;
12526 if (name_local_sections)
12527 name = o->name;
12528 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12529 NULL) != 1)
12530 goto error_return;
12535 /* On some targets like Irix 5 the symbol split between local and global
12536 ones recorded in the sh_info field needs to be done between section
12537 and all other symbols. */
12538 if (bed->elf_backend_elfsym_local_is_section
12539 && bed->elf_backend_elfsym_local_is_section (abfd))
12540 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12542 /* Allocate some memory to hold information read in from the input
12543 files. */
12544 if (max_contents_size != 0)
12546 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12547 if (flinfo.contents == NULL)
12548 goto error_return;
12551 if (max_external_reloc_size != 0)
12553 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12554 if (flinfo.external_relocs == NULL)
12555 goto error_return;
12558 if (max_internal_reloc_count != 0)
12560 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12561 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12562 if (flinfo.internal_relocs == NULL)
12563 goto error_return;
12566 if (max_sym_count != 0)
12568 amt = max_sym_count * bed->s->sizeof_sym;
12569 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12570 if (flinfo.external_syms == NULL)
12571 goto error_return;
12573 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12574 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12575 if (flinfo.internal_syms == NULL)
12576 goto error_return;
12578 amt = max_sym_count * sizeof (long);
12579 flinfo.indices = (long int *) bfd_malloc (amt);
12580 if (flinfo.indices == NULL)
12581 goto error_return;
12583 amt = max_sym_count * sizeof (asection *);
12584 flinfo.sections = (asection **) bfd_malloc (amt);
12585 if (flinfo.sections == NULL)
12586 goto error_return;
12589 if (max_sym_shndx_count != 0)
12591 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12592 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12593 if (flinfo.locsym_shndx == NULL)
12594 goto error_return;
12597 if (htab->tls_sec)
12599 bfd_vma base, end = 0; /* Both bytes. */
12600 asection *sec;
12602 for (sec = htab->tls_sec;
12603 sec && (sec->flags & SEC_THREAD_LOCAL);
12604 sec = sec->next)
12606 bfd_size_type size = sec->size;
12607 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12609 if (size == 0
12610 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12612 struct bfd_link_order *ord = sec->map_tail.link_order;
12614 if (ord != NULL)
12615 size = ord->offset * opb + ord->size;
12617 end = sec->vma + size / opb;
12619 base = htab->tls_sec->vma;
12620 /* Only align end of TLS section if static TLS doesn't have special
12621 alignment requirements. */
12622 if (bed->static_tls_alignment == 1)
12623 end = align_power (end, htab->tls_sec->alignment_power);
12624 htab->tls_size = end - base;
12627 /* Reorder SHF_LINK_ORDER sections. */
12628 for (o = abfd->sections; o != NULL; o = o->next)
12630 if (!elf_fixup_link_order (abfd, o))
12631 return FALSE;
12634 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12635 return FALSE;
12637 /* Since ELF permits relocations to be against local symbols, we
12638 must have the local symbols available when we do the relocations.
12639 Since we would rather only read the local symbols once, and we
12640 would rather not keep them in memory, we handle all the
12641 relocations for a single input file at the same time.
12643 Unfortunately, there is no way to know the total number of local
12644 symbols until we have seen all of them, and the local symbol
12645 indices precede the global symbol indices. This means that when
12646 we are generating relocatable output, and we see a reloc against
12647 a global symbol, we can not know the symbol index until we have
12648 finished examining all the local symbols to see which ones we are
12649 going to output. To deal with this, we keep the relocations in
12650 memory, and don't output them until the end of the link. This is
12651 an unfortunate waste of memory, but I don't see a good way around
12652 it. Fortunately, it only happens when performing a relocatable
12653 link, which is not the common case. FIXME: If keep_memory is set
12654 we could write the relocs out and then read them again; I don't
12655 know how bad the memory loss will be. */
12657 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12658 sub->output_has_begun = FALSE;
12659 for (o = abfd->sections; o != NULL; o = o->next)
12661 for (p = o->map_head.link_order; p != NULL; p = p->next)
12663 if (p->type == bfd_indirect_link_order
12664 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12665 == bfd_target_elf_flavour)
12666 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12668 if (! sub->output_has_begun)
12670 if (! elf_link_input_bfd (&flinfo, sub))
12671 goto error_return;
12672 sub->output_has_begun = TRUE;
12675 else if (p->type == bfd_section_reloc_link_order
12676 || p->type == bfd_symbol_reloc_link_order)
12678 if (! elf_reloc_link_order (abfd, info, o, p))
12679 goto error_return;
12681 else
12683 if (! _bfd_default_link_order (abfd, info, o, p))
12685 if (p->type == bfd_indirect_link_order
12686 && (bfd_get_flavour (sub)
12687 == bfd_target_elf_flavour)
12688 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12689 != bed->s->elfclass))
12691 const char *iclass, *oclass;
12693 switch (bed->s->elfclass)
12695 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12696 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12697 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12698 default: abort ();
12701 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12703 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12704 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12705 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12706 default: abort ();
12709 bfd_set_error (bfd_error_wrong_format);
12710 _bfd_error_handler
12711 /* xgettext:c-format */
12712 (_("%pB: file class %s incompatible with %s"),
12713 sub, iclass, oclass);
12716 goto error_return;
12722 /* Free symbol buffer if needed. */
12723 if (!info->reduce_memory_overheads)
12725 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12726 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12728 free (elf_tdata (sub)->symbuf);
12729 elf_tdata (sub)->symbuf = NULL;
12733 ret = TRUE;
12735 /* Output any global symbols that got converted to local in a
12736 version script or due to symbol visibility. We do this in a
12737 separate step since ELF requires all local symbols to appear
12738 prior to any global symbols. FIXME: We should only do this if
12739 some global symbols were, in fact, converted to become local.
12740 FIXME: Will this work correctly with the Irix 5 linker? */
12741 eoinfo.failed = FALSE;
12742 eoinfo.flinfo = &flinfo;
12743 eoinfo.localsyms = TRUE;
12744 eoinfo.file_sym_done = FALSE;
12745 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12746 if (eoinfo.failed)
12748 ret = FALSE;
12749 goto return_local_hash_table;
12752 /* If backend needs to output some local symbols not present in the hash
12753 table, do it now. */
12754 if (bed->elf_backend_output_arch_local_syms
12755 && (info->strip != strip_all || emit_relocs))
12757 typedef int (*out_sym_func)
12758 (void *, const char *, Elf_Internal_Sym *, asection *,
12759 struct elf_link_hash_entry *);
12761 if (! ((*bed->elf_backend_output_arch_local_syms)
12762 (abfd, info, &flinfo,
12763 (out_sym_func) elf_link_output_symstrtab)))
12765 ret = FALSE;
12766 goto return_local_hash_table;
12770 /* That wrote out all the local symbols. Finish up the symbol table
12771 with the global symbols. Even if we want to strip everything we
12772 can, we still need to deal with those global symbols that got
12773 converted to local in a version script. */
12775 /* The sh_info field records the index of the first non local symbol. */
12776 if (!symtab_hdr->sh_info)
12777 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12779 if (dynamic
12780 && htab->dynsym != NULL
12781 && htab->dynsym->output_section != bfd_abs_section_ptr)
12783 Elf_Internal_Sym sym;
12784 bfd_byte *dynsym = htab->dynsym->contents;
12786 o = htab->dynsym->output_section;
12787 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12789 /* Write out the section symbols for the output sections. */
12790 if (bfd_link_pic (info)
12791 || htab->is_relocatable_executable)
12793 asection *s;
12795 sym.st_size = 0;
12796 sym.st_name = 0;
12797 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12798 sym.st_other = 0;
12799 sym.st_target_internal = 0;
12801 for (s = abfd->sections; s != NULL; s = s->next)
12803 int indx;
12804 bfd_byte *dest;
12805 long dynindx;
12807 dynindx = elf_section_data (s)->dynindx;
12808 if (dynindx <= 0)
12809 continue;
12810 indx = elf_section_data (s)->this_idx;
12811 BFD_ASSERT (indx > 0);
12812 sym.st_shndx = indx;
12813 if (! check_dynsym (abfd, &sym))
12815 ret = FALSE;
12816 goto return_local_hash_table;
12818 sym.st_value = s->vma;
12819 dest = dynsym + dynindx * bed->s->sizeof_sym;
12821 /* Inform the linker of the addition of this symbol. */
12823 if (info->callbacks->ctf_new_dynsym)
12824 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12826 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12830 /* Write out the local dynsyms. */
12831 if (htab->dynlocal)
12833 struct elf_link_local_dynamic_entry *e;
12834 for (e = htab->dynlocal; e ; e = e->next)
12836 asection *s;
12837 bfd_byte *dest;
12839 /* Copy the internal symbol and turn off visibility.
12840 Note that we saved a word of storage and overwrote
12841 the original st_name with the dynstr_index. */
12842 sym = e->isym;
12843 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12844 sym.st_shndx = SHN_UNDEF;
12846 s = bfd_section_from_elf_index (e->input_bfd,
12847 e->isym.st_shndx);
12848 if (s != NULL
12849 && s->output_section != NULL
12850 && elf_section_data (s->output_section) != NULL)
12852 sym.st_shndx =
12853 elf_section_data (s->output_section)->this_idx;
12854 if (! check_dynsym (abfd, &sym))
12856 ret = FALSE;
12857 goto return_local_hash_table;
12859 sym.st_value = (s->output_section->vma
12860 + s->output_offset
12861 + e->isym.st_value);
12864 /* Inform the linker of the addition of this symbol. */
12866 if (info->callbacks->ctf_new_dynsym)
12867 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12869 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12870 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12875 /* We get the global symbols from the hash table. */
12876 eoinfo.failed = FALSE;
12877 eoinfo.localsyms = FALSE;
12878 eoinfo.flinfo = &flinfo;
12879 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12880 if (eoinfo.failed)
12882 ret = FALSE;
12883 goto return_local_hash_table;
12886 /* If backend needs to output some symbols not present in the hash
12887 table, do it now. */
12888 if (bed->elf_backend_output_arch_syms
12889 && (info->strip != strip_all || emit_relocs))
12891 typedef int (*out_sym_func)
12892 (void *, const char *, Elf_Internal_Sym *, asection *,
12893 struct elf_link_hash_entry *);
12895 if (! ((*bed->elf_backend_output_arch_syms)
12896 (abfd, info, &flinfo,
12897 (out_sym_func) elf_link_output_symstrtab)))
12899 ret = FALSE;
12900 goto return_local_hash_table;
12904 /* Finalize the .strtab section. */
12905 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12907 /* Swap out the .strtab section. */
12908 if (!elf_link_swap_symbols_out (&flinfo))
12910 ret = FALSE;
12911 goto return_local_hash_table;
12914 /* Now we know the size of the symtab section. */
12915 if (bfd_get_symcount (abfd) > 0)
12917 /* Finish up and write out the symbol string table (.strtab)
12918 section. */
12919 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12920 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12922 if (elf_symtab_shndx_list (abfd))
12924 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12926 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12928 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12929 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12930 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12931 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12932 symtab_shndx_hdr->sh_size = amt;
12934 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12935 off, TRUE);
12937 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12938 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12940 ret = FALSE;
12941 goto return_local_hash_table;
12946 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12947 /* sh_name was set in prep_headers. */
12948 symstrtab_hdr->sh_type = SHT_STRTAB;
12949 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12950 symstrtab_hdr->sh_addr = 0;
12951 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12952 symstrtab_hdr->sh_entsize = 0;
12953 symstrtab_hdr->sh_link = 0;
12954 symstrtab_hdr->sh_info = 0;
12955 /* sh_offset is set just below. */
12956 symstrtab_hdr->sh_addralign = 1;
12958 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12959 off, TRUE);
12960 elf_next_file_pos (abfd) = off;
12962 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12963 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12965 ret = FALSE;
12966 goto return_local_hash_table;
12970 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12972 _bfd_error_handler (_("%pB: failed to generate import library"),
12973 info->out_implib_bfd);
12974 ret = FALSE;
12975 goto return_local_hash_table;
12978 /* Adjust the relocs to have the correct symbol indices. */
12979 for (o = abfd->sections; o != NULL; o = o->next)
12981 struct bfd_elf_section_data *esdo = elf_section_data (o);
12982 bfd_boolean sort;
12984 if ((o->flags & SEC_RELOC) == 0)
12985 continue;
12987 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12988 if (esdo->rel.hdr != NULL
12989 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12991 ret = FALSE;
12992 goto return_local_hash_table;
12994 if (esdo->rela.hdr != NULL
12995 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12997 ret = FALSE;
12998 goto return_local_hash_table;
13001 /* Set the reloc_count field to 0 to prevent write_relocs from
13002 trying to swap the relocs out itself. */
13003 o->reloc_count = 0;
13006 if (dynamic && info->combreloc && dynobj != NULL)
13007 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13009 /* If we are linking against a dynamic object, or generating a
13010 shared library, finish up the dynamic linking information. */
13011 if (dynamic)
13013 bfd_byte *dyncon, *dynconend;
13015 /* Fix up .dynamic entries. */
13016 o = bfd_get_linker_section (dynobj, ".dynamic");
13017 BFD_ASSERT (o != NULL);
13019 dyncon = o->contents;
13020 dynconend = o->contents + o->size;
13021 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13023 Elf_Internal_Dyn dyn;
13024 const char *name;
13025 unsigned int type;
13026 bfd_size_type sh_size;
13027 bfd_vma sh_addr;
13029 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13031 switch (dyn.d_tag)
13033 default:
13034 continue;
13035 case DT_NULL:
13036 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13038 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13040 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13041 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13042 default: continue;
13044 dyn.d_un.d_val = relativecount;
13045 relativecount = 0;
13046 break;
13048 continue;
13050 case DT_INIT:
13051 name = info->init_function;
13052 goto get_sym;
13053 case DT_FINI:
13054 name = info->fini_function;
13055 get_sym:
13057 struct elf_link_hash_entry *h;
13059 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
13060 if (h != NULL
13061 && (h->root.type == bfd_link_hash_defined
13062 || h->root.type == bfd_link_hash_defweak))
13064 dyn.d_un.d_ptr = h->root.u.def.value;
13065 o = h->root.u.def.section;
13066 if (o->output_section != NULL)
13067 dyn.d_un.d_ptr += (o->output_section->vma
13068 + o->output_offset);
13069 else
13071 /* The symbol is imported from another shared
13072 library and does not apply to this one. */
13073 dyn.d_un.d_ptr = 0;
13075 break;
13078 continue;
13080 case DT_PREINIT_ARRAYSZ:
13081 name = ".preinit_array";
13082 goto get_out_size;
13083 case DT_INIT_ARRAYSZ:
13084 name = ".init_array";
13085 goto get_out_size;
13086 case DT_FINI_ARRAYSZ:
13087 name = ".fini_array";
13088 get_out_size:
13089 o = bfd_get_section_by_name (abfd, name);
13090 if (o == NULL)
13092 _bfd_error_handler
13093 (_("could not find section %s"), name);
13094 goto error_return;
13096 if (o->size == 0)
13097 _bfd_error_handler
13098 (_("warning: %s section has zero size"), name);
13099 dyn.d_un.d_val = o->size;
13100 break;
13102 case DT_PREINIT_ARRAY:
13103 name = ".preinit_array";
13104 goto get_out_vma;
13105 case DT_INIT_ARRAY:
13106 name = ".init_array";
13107 goto get_out_vma;
13108 case DT_FINI_ARRAY:
13109 name = ".fini_array";
13110 get_out_vma:
13111 o = bfd_get_section_by_name (abfd, name);
13112 goto do_vma;
13114 case DT_HASH:
13115 name = ".hash";
13116 goto get_vma;
13117 case DT_GNU_HASH:
13118 name = ".gnu.hash";
13119 goto get_vma;
13120 case DT_STRTAB:
13121 name = ".dynstr";
13122 goto get_vma;
13123 case DT_SYMTAB:
13124 name = ".dynsym";
13125 goto get_vma;
13126 case DT_VERDEF:
13127 name = ".gnu.version_d";
13128 goto get_vma;
13129 case DT_VERNEED:
13130 name = ".gnu.version_r";
13131 goto get_vma;
13132 case DT_VERSYM:
13133 name = ".gnu.version";
13134 get_vma:
13135 o = bfd_get_linker_section (dynobj, name);
13136 do_vma:
13137 if (o == NULL || bfd_is_abs_section (o->output_section))
13139 _bfd_error_handler
13140 (_("could not find section %s"), name);
13141 goto error_return;
13143 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13145 _bfd_error_handler
13146 (_("warning: section '%s' is being made into a note"), name);
13147 bfd_set_error (bfd_error_nonrepresentable_section);
13148 goto error_return;
13150 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13151 break;
13153 case DT_REL:
13154 case DT_RELA:
13155 case DT_RELSZ:
13156 case DT_RELASZ:
13157 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13158 type = SHT_REL;
13159 else
13160 type = SHT_RELA;
13161 sh_size = 0;
13162 sh_addr = 0;
13163 for (i = 1; i < elf_numsections (abfd); i++)
13165 Elf_Internal_Shdr *hdr;
13167 hdr = elf_elfsections (abfd)[i];
13168 if (hdr->sh_type == type
13169 && (hdr->sh_flags & SHF_ALLOC) != 0)
13171 sh_size += hdr->sh_size;
13172 if (sh_addr == 0
13173 || sh_addr > hdr->sh_addr)
13174 sh_addr = hdr->sh_addr;
13178 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13180 unsigned int opb = bfd_octets_per_byte (abfd, o);
13182 /* Don't count procedure linkage table relocs in the
13183 overall reloc count. */
13184 sh_size -= htab->srelplt->size;
13185 if (sh_size == 0)
13186 /* If the size is zero, make the address zero too.
13187 This is to avoid a glibc bug. If the backend
13188 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13189 zero, then we'll put DT_RELA at the end of
13190 DT_JMPREL. glibc will interpret the end of
13191 DT_RELA matching the end of DT_JMPREL as the
13192 case where DT_RELA includes DT_JMPREL, and for
13193 LD_BIND_NOW will decide that processing DT_RELA
13194 will process the PLT relocs too. Net result:
13195 No PLT relocs applied. */
13196 sh_addr = 0;
13198 /* If .rela.plt is the first .rela section, exclude
13199 it from DT_RELA. */
13200 else if (sh_addr == (htab->srelplt->output_section->vma
13201 + htab->srelplt->output_offset) * opb)
13202 sh_addr += htab->srelplt->size;
13205 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13206 dyn.d_un.d_val = sh_size;
13207 else
13208 dyn.d_un.d_ptr = sh_addr;
13209 break;
13211 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13215 /* If we have created any dynamic sections, then output them. */
13216 if (dynobj != NULL)
13218 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13219 goto error_return;
13221 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13222 if (bfd_link_textrel_check (info)
13223 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
13225 bfd_byte *dyncon, *dynconend;
13227 dyncon = o->contents;
13228 dynconend = o->contents + o->size;
13229 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13231 Elf_Internal_Dyn dyn;
13233 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13235 if (dyn.d_tag == DT_TEXTREL)
13237 if (info->textrel_check == textrel_check_error)
13238 info->callbacks->einfo
13239 (_("%P%X: read-only segment has dynamic relocations\n"));
13240 else if (bfd_link_dll (info))
13241 info->callbacks->einfo
13242 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13243 else
13244 info->callbacks->einfo
13245 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13246 break;
13251 for (o = dynobj->sections; o != NULL; o = o->next)
13253 if ((o->flags & SEC_HAS_CONTENTS) == 0
13254 || o->size == 0
13255 || o->output_section == bfd_abs_section_ptr)
13256 continue;
13257 if ((o->flags & SEC_LINKER_CREATED) == 0)
13259 /* At this point, we are only interested in sections
13260 created by _bfd_elf_link_create_dynamic_sections. */
13261 continue;
13263 if (htab->stab_info.stabstr == o)
13264 continue;
13265 if (htab->eh_info.hdr_sec == o)
13266 continue;
13267 if (strcmp (o->name, ".dynstr") != 0)
13269 bfd_size_type octets = ((file_ptr) o->output_offset
13270 * bfd_octets_per_byte (abfd, o));
13271 if (!bfd_set_section_contents (abfd, o->output_section,
13272 o->contents, octets, o->size))
13273 goto error_return;
13275 else
13277 /* The contents of the .dynstr section are actually in a
13278 stringtab. */
13279 file_ptr off;
13281 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13282 if (bfd_seek (abfd, off, SEEK_SET) != 0
13283 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13284 goto error_return;
13289 if (!info->resolve_section_groups)
13291 bfd_boolean failed = FALSE;
13293 BFD_ASSERT (bfd_link_relocatable (info));
13294 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13295 if (failed)
13296 goto error_return;
13299 /* If we have optimized stabs strings, output them. */
13300 if (htab->stab_info.stabstr != NULL)
13302 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13303 goto error_return;
13306 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13307 goto error_return;
13309 if (info->callbacks->emit_ctf)
13310 info->callbacks->emit_ctf ();
13312 elf_final_link_free (abfd, &flinfo);
13314 if (attr_section)
13316 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13317 if (contents == NULL)
13319 /* Bail out and fail. */
13320 ret = FALSE;
13321 goto return_local_hash_table;
13323 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13324 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13325 free (contents);
13328 return_local_hash_table:
13329 if (info->unique_symbol)
13330 bfd_hash_table_free (&flinfo.local_hash_table);
13331 return ret;
13333 error_return:
13334 elf_final_link_free (abfd, &flinfo);
13335 ret = FALSE;
13336 goto return_local_hash_table;
13339 /* Initialize COOKIE for input bfd ABFD. */
13341 static bfd_boolean
13342 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13343 struct bfd_link_info *info, bfd *abfd)
13345 Elf_Internal_Shdr *symtab_hdr;
13346 const struct elf_backend_data *bed;
13348 bed = get_elf_backend_data (abfd);
13349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13351 cookie->abfd = abfd;
13352 cookie->sym_hashes = elf_sym_hashes (abfd);
13353 cookie->bad_symtab = elf_bad_symtab (abfd);
13354 if (cookie->bad_symtab)
13356 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13357 cookie->extsymoff = 0;
13359 else
13361 cookie->locsymcount = symtab_hdr->sh_info;
13362 cookie->extsymoff = symtab_hdr->sh_info;
13365 if (bed->s->arch_size == 32)
13366 cookie->r_sym_shift = 8;
13367 else
13368 cookie->r_sym_shift = 32;
13370 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13371 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13373 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13374 cookie->locsymcount, 0,
13375 NULL, NULL, NULL);
13376 if (cookie->locsyms == NULL)
13378 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13379 return FALSE;
13381 if (info->keep_memory)
13382 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13384 return TRUE;
13387 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13389 static void
13390 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13392 Elf_Internal_Shdr *symtab_hdr;
13394 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13395 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13396 free (cookie->locsyms);
13399 /* Initialize the relocation information in COOKIE for input section SEC
13400 of input bfd ABFD. */
13402 static bfd_boolean
13403 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13404 struct bfd_link_info *info, bfd *abfd,
13405 asection *sec)
13407 if (sec->reloc_count == 0)
13409 cookie->rels = NULL;
13410 cookie->relend = NULL;
13412 else
13414 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13415 info->keep_memory);
13416 if (cookie->rels == NULL)
13417 return FALSE;
13418 cookie->rel = cookie->rels;
13419 cookie->relend = cookie->rels + sec->reloc_count;
13421 cookie->rel = cookie->rels;
13422 return TRUE;
13425 /* Free the memory allocated by init_reloc_cookie_rels,
13426 if appropriate. */
13428 static void
13429 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13430 asection *sec)
13432 if (elf_section_data (sec)->relocs != cookie->rels)
13433 free (cookie->rels);
13436 /* Initialize the whole of COOKIE for input section SEC. */
13438 static bfd_boolean
13439 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13440 struct bfd_link_info *info,
13441 asection *sec)
13443 if (!init_reloc_cookie (cookie, info, sec->owner))
13444 goto error1;
13445 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13446 goto error2;
13447 return TRUE;
13449 error2:
13450 fini_reloc_cookie (cookie, sec->owner);
13451 error1:
13452 return FALSE;
13455 /* Free the memory allocated by init_reloc_cookie_for_section,
13456 if appropriate. */
13458 static void
13459 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13460 asection *sec)
13462 fini_reloc_cookie_rels (cookie, sec);
13463 fini_reloc_cookie (cookie, sec->owner);
13466 /* Garbage collect unused sections. */
13468 /* Default gc_mark_hook. */
13470 asection *
13471 _bfd_elf_gc_mark_hook (asection *sec,
13472 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13473 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13474 struct elf_link_hash_entry *h,
13475 Elf_Internal_Sym *sym)
13477 if (h != NULL)
13479 switch (h->root.type)
13481 case bfd_link_hash_defined:
13482 case bfd_link_hash_defweak:
13483 return h->root.u.def.section;
13485 case bfd_link_hash_common:
13486 return h->root.u.c.p->section;
13488 default:
13489 break;
13492 else
13493 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13495 return NULL;
13498 /* Return the debug definition section. */
13500 static asection *
13501 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13502 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13503 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13504 struct elf_link_hash_entry *h,
13505 Elf_Internal_Sym *sym)
13507 if (h != NULL)
13509 /* Return the global debug definition section. */
13510 if ((h->root.type == bfd_link_hash_defined
13511 || h->root.type == bfd_link_hash_defweak)
13512 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13513 return h->root.u.def.section;
13515 else
13517 /* Return the local debug definition section. */
13518 asection *isec = bfd_section_from_elf_index (sec->owner,
13519 sym->st_shndx);
13520 if ((isec->flags & SEC_DEBUGGING) != 0)
13521 return isec;
13524 return NULL;
13527 /* COOKIE->rel describes a relocation against section SEC, which is
13528 a section we've decided to keep. Return the section that contains
13529 the relocation symbol, or NULL if no section contains it. */
13531 asection *
13532 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13533 elf_gc_mark_hook_fn gc_mark_hook,
13534 struct elf_reloc_cookie *cookie,
13535 bfd_boolean *start_stop)
13537 unsigned long r_symndx;
13538 struct elf_link_hash_entry *h, *hw;
13540 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13541 if (r_symndx == STN_UNDEF)
13542 return NULL;
13544 if (r_symndx >= cookie->locsymcount
13545 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13547 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13548 if (h == NULL)
13550 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13551 sec->owner);
13552 return NULL;
13554 while (h->root.type == bfd_link_hash_indirect
13555 || h->root.type == bfd_link_hash_warning)
13556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13557 h->mark = 1;
13558 /* Keep all aliases of the symbol too. If an object symbol
13559 needs to be copied into .dynbss then all of its aliases
13560 should be present as dynamic symbols, not just the one used
13561 on the copy relocation. */
13562 hw = h;
13563 while (hw->is_weakalias)
13565 hw = hw->u.alias;
13566 hw->mark = 1;
13569 if (start_stop != NULL)
13571 /* To work around a glibc bug, mark XXX input sections
13572 when there is a reference to __start_XXX or __stop_XXX
13573 symbols. */
13574 if (h->start_stop)
13576 asection *s = h->u2.start_stop_section;
13577 *start_stop = !s->gc_mark;
13578 return s;
13582 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13585 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13586 &cookie->locsyms[r_symndx]);
13589 /* COOKIE->rel describes a relocation against section SEC, which is
13590 a section we've decided to keep. Mark the section that contains
13591 the relocation symbol. */
13593 bfd_boolean
13594 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13595 asection *sec,
13596 elf_gc_mark_hook_fn gc_mark_hook,
13597 struct elf_reloc_cookie *cookie)
13599 asection *rsec;
13600 bfd_boolean start_stop = FALSE;
13602 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13603 while (rsec != NULL)
13605 if (!rsec->gc_mark)
13607 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13608 || (rsec->owner->flags & DYNAMIC) != 0)
13609 rsec->gc_mark = 1;
13610 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13611 return FALSE;
13613 if (!start_stop)
13614 break;
13615 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13617 return TRUE;
13620 /* The mark phase of garbage collection. For a given section, mark
13621 it and any sections in this section's group, and all the sections
13622 which define symbols to which it refers. */
13624 bfd_boolean
13625 _bfd_elf_gc_mark (struct bfd_link_info *info,
13626 asection *sec,
13627 elf_gc_mark_hook_fn gc_mark_hook)
13629 bfd_boolean ret;
13630 asection *group_sec, *eh_frame;
13632 sec->gc_mark = 1;
13634 /* Mark all the sections in the group. */
13635 group_sec = elf_section_data (sec)->next_in_group;
13636 if (group_sec && !group_sec->gc_mark)
13637 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13638 return FALSE;
13640 /* Look through the section relocs. */
13641 ret = TRUE;
13642 eh_frame = elf_eh_frame_section (sec->owner);
13643 if ((sec->flags & SEC_RELOC) != 0
13644 && sec->reloc_count > 0
13645 && sec != eh_frame)
13647 struct elf_reloc_cookie cookie;
13649 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13650 ret = FALSE;
13651 else
13653 for (; cookie.rel < cookie.relend; cookie.rel++)
13654 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13656 ret = FALSE;
13657 break;
13659 fini_reloc_cookie_for_section (&cookie, sec);
13663 if (ret && eh_frame && elf_fde_list (sec))
13665 struct elf_reloc_cookie cookie;
13667 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13668 ret = FALSE;
13669 else
13671 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13672 gc_mark_hook, &cookie))
13673 ret = FALSE;
13674 fini_reloc_cookie_for_section (&cookie, eh_frame);
13678 eh_frame = elf_section_eh_frame_entry (sec);
13679 if (ret && eh_frame && !eh_frame->gc_mark)
13680 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13681 ret = FALSE;
13683 return ret;
13686 /* Scan and mark sections in a special or debug section group. */
13688 static void
13689 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13691 /* Point to first section of section group. */
13692 asection *ssec;
13693 /* Used to iterate the section group. */
13694 asection *msec;
13696 bfd_boolean is_special_grp = TRUE;
13697 bfd_boolean is_debug_grp = TRUE;
13699 /* First scan to see if group contains any section other than debug
13700 and special section. */
13701 ssec = msec = elf_next_in_group (grp);
13704 if ((msec->flags & SEC_DEBUGGING) == 0)
13705 is_debug_grp = FALSE;
13707 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13708 is_special_grp = FALSE;
13710 msec = elf_next_in_group (msec);
13712 while (msec != ssec);
13714 /* If this is a pure debug section group or pure special section group,
13715 keep all sections in this group. */
13716 if (is_debug_grp || is_special_grp)
13720 msec->gc_mark = 1;
13721 msec = elf_next_in_group (msec);
13723 while (msec != ssec);
13727 /* Keep debug and special sections. */
13729 bfd_boolean
13730 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13731 elf_gc_mark_hook_fn mark_hook)
13733 bfd *ibfd;
13735 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13737 asection *isec;
13738 bfd_boolean some_kept;
13739 bfd_boolean debug_frag_seen;
13740 bfd_boolean has_kept_debug_info;
13742 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13743 continue;
13744 isec = ibfd->sections;
13745 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13746 continue;
13748 /* Ensure all linker created sections are kept,
13749 see if any other section is already marked,
13750 and note if we have any fragmented debug sections. */
13751 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13752 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13754 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13755 isec->gc_mark = 1;
13756 else if (isec->gc_mark
13757 && (isec->flags & SEC_ALLOC) != 0
13758 && elf_section_type (isec) != SHT_NOTE)
13759 some_kept = TRUE;
13760 else
13762 /* Since all sections, except for backend specific ones,
13763 have been garbage collected, call mark_hook on this
13764 section if any of its linked-to sections is marked. */
13765 asection *linked_to_sec = elf_linked_to_section (isec);
13766 for (; linked_to_sec != NULL;
13767 linked_to_sec = elf_linked_to_section (linked_to_sec))
13768 if (linked_to_sec->gc_mark)
13770 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13771 return FALSE;
13772 break;
13776 if (!debug_frag_seen
13777 && (isec->flags & SEC_DEBUGGING)
13778 && CONST_STRNEQ (isec->name, ".debug_line."))
13779 debug_frag_seen = TRUE;
13780 else if (strcmp (bfd_section_name (isec),
13781 "__patchable_function_entries") == 0
13782 && elf_linked_to_section (isec) == NULL)
13783 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13784 "need linked-to section "
13785 "for --gc-sections\n"),
13786 isec->owner, isec);
13789 /* If no non-note alloc section in this file will be kept, then
13790 we can toss out the debug and special sections. */
13791 if (!some_kept)
13792 continue;
13794 /* Keep debug and special sections like .comment when they are
13795 not part of a group. Also keep section groups that contain
13796 just debug sections or special sections. NB: Sections with
13797 linked-to section has been handled above. */
13798 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13800 if ((isec->flags & SEC_GROUP) != 0)
13801 _bfd_elf_gc_mark_debug_special_section_group (isec);
13802 else if (((isec->flags & SEC_DEBUGGING) != 0
13803 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13804 && elf_next_in_group (isec) == NULL
13805 && elf_linked_to_section (isec) == NULL)
13806 isec->gc_mark = 1;
13807 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13808 has_kept_debug_info = TRUE;
13811 /* Look for CODE sections which are going to be discarded,
13812 and find and discard any fragmented debug sections which
13813 are associated with that code section. */
13814 if (debug_frag_seen)
13815 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13816 if ((isec->flags & SEC_CODE) != 0
13817 && isec->gc_mark == 0)
13819 unsigned int ilen;
13820 asection *dsec;
13822 ilen = strlen (isec->name);
13824 /* Association is determined by the name of the debug
13825 section containing the name of the code section as
13826 a suffix. For example .debug_line.text.foo is a
13827 debug section associated with .text.foo. */
13828 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13830 unsigned int dlen;
13832 if (dsec->gc_mark == 0
13833 || (dsec->flags & SEC_DEBUGGING) == 0)
13834 continue;
13836 dlen = strlen (dsec->name);
13838 if (dlen > ilen
13839 && strncmp (dsec->name + (dlen - ilen),
13840 isec->name, ilen) == 0)
13841 dsec->gc_mark = 0;
13845 /* Mark debug sections referenced by kept debug sections. */
13846 if (has_kept_debug_info)
13847 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13848 if (isec->gc_mark
13849 && (isec->flags & SEC_DEBUGGING) != 0)
13850 if (!_bfd_elf_gc_mark (info, isec,
13851 elf_gc_mark_debug_section))
13852 return FALSE;
13854 return TRUE;
13857 static bfd_boolean
13858 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13860 bfd *sub;
13861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13863 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13865 asection *o;
13867 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13868 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13869 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13870 continue;
13871 o = sub->sections;
13872 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13873 continue;
13875 for (o = sub->sections; o != NULL; o = o->next)
13877 /* When any section in a section group is kept, we keep all
13878 sections in the section group. If the first member of
13879 the section group is excluded, we will also exclude the
13880 group section. */
13881 if (o->flags & SEC_GROUP)
13883 asection *first = elf_next_in_group (o);
13884 o->gc_mark = first->gc_mark;
13887 if (o->gc_mark)
13888 continue;
13890 /* Skip sweeping sections already excluded. */
13891 if (o->flags & SEC_EXCLUDE)
13892 continue;
13894 /* Since this is early in the link process, it is simple
13895 to remove a section from the output. */
13896 o->flags |= SEC_EXCLUDE;
13898 if (info->print_gc_sections && o->size != 0)
13899 /* xgettext:c-format */
13900 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13901 o, sub);
13905 return TRUE;
13908 /* Propagate collected vtable information. This is called through
13909 elf_link_hash_traverse. */
13911 static bfd_boolean
13912 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13914 /* Those that are not vtables. */
13915 if (h->start_stop
13916 || h->u2.vtable == NULL
13917 || h->u2.vtable->parent == NULL)
13918 return TRUE;
13920 /* Those vtables that do not have parents, we cannot merge. */
13921 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13922 return TRUE;
13924 /* If we've already been done, exit. */
13925 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13926 return TRUE;
13928 /* Make sure the parent's table is up to date. */
13929 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13931 if (h->u2.vtable->used == NULL)
13933 /* None of this table's entries were referenced. Re-use the
13934 parent's table. */
13935 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13936 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13938 else
13940 size_t n;
13941 bfd_boolean *cu, *pu;
13943 /* Or the parent's entries into ours. */
13944 cu = h->u2.vtable->used;
13945 cu[-1] = TRUE;
13946 pu = h->u2.vtable->parent->u2.vtable->used;
13947 if (pu != NULL)
13949 const struct elf_backend_data *bed;
13950 unsigned int log_file_align;
13952 bed = get_elf_backend_data (h->root.u.def.section->owner);
13953 log_file_align = bed->s->log_file_align;
13954 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13955 while (n--)
13957 if (*pu)
13958 *cu = TRUE;
13959 pu++;
13960 cu++;
13965 return TRUE;
13968 static bfd_boolean
13969 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13971 asection *sec;
13972 bfd_vma hstart, hend;
13973 Elf_Internal_Rela *relstart, *relend, *rel;
13974 const struct elf_backend_data *bed;
13975 unsigned int log_file_align;
13977 /* Take care of both those symbols that do not describe vtables as
13978 well as those that are not loaded. */
13979 if (h->start_stop
13980 || h->u2.vtable == NULL
13981 || h->u2.vtable->parent == NULL)
13982 return TRUE;
13984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13985 || h->root.type == bfd_link_hash_defweak);
13987 sec = h->root.u.def.section;
13988 hstart = h->root.u.def.value;
13989 hend = hstart + h->size;
13991 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13992 if (!relstart)
13993 return *(bfd_boolean *) okp = FALSE;
13994 bed = get_elf_backend_data (sec->owner);
13995 log_file_align = bed->s->log_file_align;
13997 relend = relstart + sec->reloc_count;
13999 for (rel = relstart; rel < relend; ++rel)
14000 if (rel->r_offset >= hstart && rel->r_offset < hend)
14002 /* If the entry is in use, do nothing. */
14003 if (h->u2.vtable->used
14004 && (rel->r_offset - hstart) < h->u2.vtable->size)
14006 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14007 if (h->u2.vtable->used[entry])
14008 continue;
14010 /* Otherwise, kill it. */
14011 rel->r_offset = rel->r_info = rel->r_addend = 0;
14014 return TRUE;
14017 /* Mark sections containing dynamically referenced symbols. When
14018 building shared libraries, we must assume that any visible symbol is
14019 referenced. */
14021 bfd_boolean
14022 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14024 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14025 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14027 if ((h->root.type == bfd_link_hash_defined
14028 || h->root.type == bfd_link_hash_defweak)
14029 && ((h->ref_dynamic && !h->forced_local)
14030 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14031 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14032 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14033 && (!bfd_link_executable (info)
14034 || info->gc_keep_exported
14035 || info->export_dynamic
14036 || (h->dynamic
14037 && d != NULL
14038 && (*d->match) (&d->head, NULL, h->root.root.string)))
14039 && (h->versioned >= versioned
14040 || !bfd_hide_sym_by_version (info->version_info,
14041 h->root.root.string)))))
14042 h->root.u.def.section->flags |= SEC_KEEP;
14044 return TRUE;
14047 /* Keep all sections containing symbols undefined on the command-line,
14048 and the section containing the entry symbol. */
14050 void
14051 _bfd_elf_gc_keep (struct bfd_link_info *info)
14053 struct bfd_sym_chain *sym;
14055 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14057 struct elf_link_hash_entry *h;
14059 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14060 FALSE, FALSE, FALSE);
14062 if (h != NULL
14063 && (h->root.type == bfd_link_hash_defined
14064 || h->root.type == bfd_link_hash_defweak)
14065 && !bfd_is_const_section (h->root.u.def.section))
14066 h->root.u.def.section->flags |= SEC_KEEP;
14070 bfd_boolean
14071 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14072 struct bfd_link_info *info)
14074 bfd *ibfd = info->input_bfds;
14076 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14078 asection *sec;
14079 struct elf_reloc_cookie cookie;
14081 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14082 continue;
14083 sec = ibfd->sections;
14084 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14085 continue;
14087 if (!init_reloc_cookie (&cookie, info, ibfd))
14088 return FALSE;
14090 for (sec = ibfd->sections; sec; sec = sec->next)
14092 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
14093 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14095 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14096 fini_reloc_cookie_rels (&cookie, sec);
14100 return TRUE;
14103 /* Do mark and sweep of unused sections. */
14105 bfd_boolean
14106 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14108 bfd_boolean ok = TRUE;
14109 bfd *sub;
14110 elf_gc_mark_hook_fn gc_mark_hook;
14111 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14112 struct elf_link_hash_table *htab;
14114 if (!bed->can_gc_sections
14115 || !is_elf_hash_table (info->hash))
14117 _bfd_error_handler(_("warning: gc-sections option ignored"));
14118 return TRUE;
14121 bed->gc_keep (info);
14122 htab = elf_hash_table (info);
14124 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14125 at the .eh_frame section if we can mark the FDEs individually. */
14126 for (sub = info->input_bfds;
14127 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14128 sub = sub->link.next)
14130 asection *sec;
14131 struct elf_reloc_cookie cookie;
14133 sec = sub->sections;
14134 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14135 continue;
14136 sec = bfd_get_section_by_name (sub, ".eh_frame");
14137 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14139 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14140 if (elf_section_data (sec)->sec_info
14141 && (sec->flags & SEC_LINKER_CREATED) == 0)
14142 elf_eh_frame_section (sub) = sec;
14143 fini_reloc_cookie_for_section (&cookie, sec);
14144 sec = bfd_get_next_section_by_name (NULL, sec);
14148 /* Apply transitive closure to the vtable entry usage info. */
14149 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14150 if (!ok)
14151 return FALSE;
14153 /* Kill the vtable relocations that were not used. */
14154 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
14155 if (!ok)
14156 return FALSE;
14158 /* Mark dynamically referenced symbols. */
14159 if (htab->dynamic_sections_created || info->gc_keep_exported)
14160 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14162 /* Grovel through relocs to find out who stays ... */
14163 gc_mark_hook = bed->gc_mark_hook;
14164 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14166 asection *o;
14168 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14169 || elf_object_id (sub) != elf_hash_table_id (htab)
14170 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14171 continue;
14173 o = sub->sections;
14174 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14175 continue;
14177 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14178 Also treat note sections as a root, if the section is not part
14179 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14180 well as FINI_ARRAY sections for ld -r. */
14181 for (o = sub->sections; o != NULL; o = o->next)
14182 if (!o->gc_mark
14183 && (o->flags & SEC_EXCLUDE) == 0
14184 && ((o->flags & SEC_KEEP) != 0
14185 || (bfd_link_relocatable (info)
14186 && ((elf_section_data (o)->this_hdr.sh_type
14187 == SHT_PREINIT_ARRAY)
14188 || (elf_section_data (o)->this_hdr.sh_type
14189 == SHT_INIT_ARRAY)
14190 || (elf_section_data (o)->this_hdr.sh_type
14191 == SHT_FINI_ARRAY)))
14192 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14193 && elf_next_in_group (o) == NULL
14194 && elf_linked_to_section (o) == NULL)
14195 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14196 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14198 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14199 return FALSE;
14203 /* Allow the backend to mark additional target specific sections. */
14204 bed->gc_mark_extra_sections (info, gc_mark_hook);
14206 /* ... and mark SEC_EXCLUDE for those that go. */
14207 return elf_gc_sweep (abfd, info);
14210 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14212 bfd_boolean
14213 bfd_elf_gc_record_vtinherit (bfd *abfd,
14214 asection *sec,
14215 struct elf_link_hash_entry *h,
14216 bfd_vma offset)
14218 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14219 struct elf_link_hash_entry **search, *child;
14220 size_t extsymcount;
14221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14223 /* The sh_info field of the symtab header tells us where the
14224 external symbols start. We don't care about the local symbols at
14225 this point. */
14226 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14227 if (!elf_bad_symtab (abfd))
14228 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14230 sym_hashes = elf_sym_hashes (abfd);
14231 sym_hashes_end = sym_hashes + extsymcount;
14233 /* Hunt down the child symbol, which is in this section at the same
14234 offset as the relocation. */
14235 for (search = sym_hashes; search != sym_hashes_end; ++search)
14237 if ((child = *search) != NULL
14238 && (child->root.type == bfd_link_hash_defined
14239 || child->root.type == bfd_link_hash_defweak)
14240 && child->root.u.def.section == sec
14241 && child->root.u.def.value == offset)
14242 goto win;
14245 /* xgettext:c-format */
14246 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14247 abfd, sec, (uint64_t) offset);
14248 bfd_set_error (bfd_error_invalid_operation);
14249 return FALSE;
14251 win:
14252 if (!child->u2.vtable)
14254 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14255 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14256 if (!child->u2.vtable)
14257 return FALSE;
14259 if (!h)
14261 /* This *should* only be the absolute section. It could potentially
14262 be that someone has defined a non-global vtable though, which
14263 would be bad. It isn't worth paging in the local symbols to be
14264 sure though; that case should simply be handled by the assembler. */
14266 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14268 else
14269 child->u2.vtable->parent = h;
14271 return TRUE;
14274 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14276 bfd_boolean
14277 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14278 struct elf_link_hash_entry *h,
14279 bfd_vma addend)
14281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14282 unsigned int log_file_align = bed->s->log_file_align;
14284 if (!h)
14286 /* xgettext:c-format */
14287 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14288 abfd, sec);
14289 bfd_set_error (bfd_error_bad_value);
14290 return FALSE;
14293 if (!h->u2.vtable)
14295 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14296 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14297 if (!h->u2.vtable)
14298 return FALSE;
14301 if (addend >= h->u2.vtable->size)
14303 size_t size, bytes, file_align;
14304 bfd_boolean *ptr = h->u2.vtable->used;
14306 /* While the symbol is undefined, we have to be prepared to handle
14307 a zero size. */
14308 file_align = 1 << log_file_align;
14309 if (h->root.type == bfd_link_hash_undefined)
14310 size = addend + file_align;
14311 else
14313 size = h->size;
14314 if (addend >= size)
14316 /* Oops! We've got a reference past the defined end of
14317 the table. This is probably a bug -- shall we warn? */
14318 size = addend + file_align;
14321 size = (size + file_align - 1) & -file_align;
14323 /* Allocate one extra entry for use as a "done" flag for the
14324 consolidation pass. */
14325 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14327 if (ptr)
14329 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
14331 if (ptr != NULL)
14333 size_t oldbytes;
14335 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14336 * sizeof (bfd_boolean));
14337 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14340 else
14341 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
14343 if (ptr == NULL)
14344 return FALSE;
14346 /* And arrange for that done flag to be at index -1. */
14347 h->u2.vtable->used = ptr + 1;
14348 h->u2.vtable->size = size;
14351 h->u2.vtable->used[addend >> log_file_align] = TRUE;
14353 return TRUE;
14356 /* Map an ELF section header flag to its corresponding string. */
14357 typedef struct
14359 char *flag_name;
14360 flagword flag_value;
14361 } elf_flags_to_name_table;
14363 static const elf_flags_to_name_table elf_flags_to_names [] =
14365 { "SHF_WRITE", SHF_WRITE },
14366 { "SHF_ALLOC", SHF_ALLOC },
14367 { "SHF_EXECINSTR", SHF_EXECINSTR },
14368 { "SHF_MERGE", SHF_MERGE },
14369 { "SHF_STRINGS", SHF_STRINGS },
14370 { "SHF_INFO_LINK", SHF_INFO_LINK},
14371 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14372 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14373 { "SHF_GROUP", SHF_GROUP },
14374 { "SHF_TLS", SHF_TLS },
14375 { "SHF_MASKOS", SHF_MASKOS },
14376 { "SHF_EXCLUDE", SHF_EXCLUDE },
14379 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14380 bfd_boolean
14381 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14382 struct flag_info *flaginfo,
14383 asection *section)
14385 const bfd_vma sh_flags = elf_section_flags (section);
14387 if (!flaginfo->flags_initialized)
14389 bfd *obfd = info->output_bfd;
14390 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14391 struct flag_info_list *tf = flaginfo->flag_list;
14392 int with_hex = 0;
14393 int without_hex = 0;
14395 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14397 unsigned i;
14398 flagword (*lookup) (char *);
14400 lookup = bed->elf_backend_lookup_section_flags_hook;
14401 if (lookup != NULL)
14403 flagword hexval = (*lookup) ((char *) tf->name);
14405 if (hexval != 0)
14407 if (tf->with == with_flags)
14408 with_hex |= hexval;
14409 else if (tf->with == without_flags)
14410 without_hex |= hexval;
14411 tf->valid = TRUE;
14412 continue;
14415 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14417 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14419 if (tf->with == with_flags)
14420 with_hex |= elf_flags_to_names[i].flag_value;
14421 else if (tf->with == without_flags)
14422 without_hex |= elf_flags_to_names[i].flag_value;
14423 tf->valid = TRUE;
14424 break;
14427 if (!tf->valid)
14429 info->callbacks->einfo
14430 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14431 return FALSE;
14434 flaginfo->flags_initialized = TRUE;
14435 flaginfo->only_with_flags |= with_hex;
14436 flaginfo->not_with_flags |= without_hex;
14439 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14440 return FALSE;
14442 if ((flaginfo->not_with_flags & sh_flags) != 0)
14443 return FALSE;
14445 return TRUE;
14448 struct alloc_got_off_arg {
14449 bfd_vma gotoff;
14450 struct bfd_link_info *info;
14453 /* We need a special top-level link routine to convert got reference counts
14454 to real got offsets. */
14456 static bfd_boolean
14457 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14459 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14460 bfd *obfd = gofarg->info->output_bfd;
14461 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14463 if (h->got.refcount > 0)
14465 h->got.offset = gofarg->gotoff;
14466 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14468 else
14469 h->got.offset = (bfd_vma) -1;
14471 return TRUE;
14474 /* And an accompanying bit to work out final got entry offsets once
14475 we're done. Should be called from final_link. */
14477 bfd_boolean
14478 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14479 struct bfd_link_info *info)
14481 bfd *i;
14482 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14483 bfd_vma gotoff;
14484 struct alloc_got_off_arg gofarg;
14486 BFD_ASSERT (abfd == info->output_bfd);
14488 if (! is_elf_hash_table (info->hash))
14489 return FALSE;
14491 /* The GOT offset is relative to the .got section, but the GOT header is
14492 put into the .got.plt section, if the backend uses it. */
14493 if (bed->want_got_plt)
14494 gotoff = 0;
14495 else
14496 gotoff = bed->got_header_size;
14498 /* Do the local .got entries first. */
14499 for (i = info->input_bfds; i; i = i->link.next)
14501 bfd_signed_vma *local_got;
14502 size_t j, locsymcount;
14503 Elf_Internal_Shdr *symtab_hdr;
14505 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14506 continue;
14508 local_got = elf_local_got_refcounts (i);
14509 if (!local_got)
14510 continue;
14512 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14513 if (elf_bad_symtab (i))
14514 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14515 else
14516 locsymcount = symtab_hdr->sh_info;
14518 for (j = 0; j < locsymcount; ++j)
14520 if (local_got[j] > 0)
14522 local_got[j] = gotoff;
14523 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14525 else
14526 local_got[j] = (bfd_vma) -1;
14530 /* Then the global .got entries. .plt refcounts are handled by
14531 adjust_dynamic_symbol */
14532 gofarg.gotoff = gotoff;
14533 gofarg.info = info;
14534 elf_link_hash_traverse (elf_hash_table (info),
14535 elf_gc_allocate_got_offsets,
14536 &gofarg);
14537 return TRUE;
14540 /* Many folk need no more in the way of final link than this, once
14541 got entry reference counting is enabled. */
14543 bfd_boolean
14544 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14546 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14547 return FALSE;
14549 /* Invoke the regular ELF backend linker to do all the work. */
14550 return bfd_elf_final_link (abfd, info);
14553 bfd_boolean
14554 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14556 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14558 if (rcookie->bad_symtab)
14559 rcookie->rel = rcookie->rels;
14561 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14563 unsigned long r_symndx;
14565 if (! rcookie->bad_symtab)
14566 if (rcookie->rel->r_offset > offset)
14567 return FALSE;
14568 if (rcookie->rel->r_offset != offset)
14569 continue;
14571 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14572 if (r_symndx == STN_UNDEF)
14573 return TRUE;
14575 if (r_symndx >= rcookie->locsymcount
14576 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14578 struct elf_link_hash_entry *h;
14580 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14582 while (h->root.type == bfd_link_hash_indirect
14583 || h->root.type == bfd_link_hash_warning)
14584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14586 if ((h->root.type == bfd_link_hash_defined
14587 || h->root.type == bfd_link_hash_defweak)
14588 && (h->root.u.def.section->owner != rcookie->abfd
14589 || h->root.u.def.section->kept_section != NULL
14590 || discarded_section (h->root.u.def.section)))
14591 return TRUE;
14593 else
14595 /* It's not a relocation against a global symbol,
14596 but it could be a relocation against a local
14597 symbol for a discarded section. */
14598 asection *isec;
14599 Elf_Internal_Sym *isym;
14601 /* Need to: get the symbol; get the section. */
14602 isym = &rcookie->locsyms[r_symndx];
14603 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14604 if (isec != NULL
14605 && (isec->kept_section != NULL
14606 || discarded_section (isec)))
14607 return TRUE;
14609 return FALSE;
14611 return FALSE;
14614 /* Discard unneeded references to discarded sections.
14615 Returns -1 on error, 1 if any section's size was changed, 0 if
14616 nothing changed. This function assumes that the relocations are in
14617 sorted order, which is true for all known assemblers. */
14620 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14622 struct elf_reloc_cookie cookie;
14623 asection *o;
14624 bfd *abfd;
14625 int changed = 0;
14627 if (info->traditional_format
14628 || !is_elf_hash_table (info->hash))
14629 return 0;
14631 o = bfd_get_section_by_name (output_bfd, ".stab");
14632 if (o != NULL)
14634 asection *i;
14636 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14638 if (i->size == 0
14639 || i->reloc_count == 0
14640 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14641 continue;
14643 abfd = i->owner;
14644 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14645 continue;
14647 if (!init_reloc_cookie_for_section (&cookie, info, i))
14648 return -1;
14650 if (_bfd_discard_section_stabs (abfd, i,
14651 elf_section_data (i)->sec_info,
14652 bfd_elf_reloc_symbol_deleted_p,
14653 &cookie))
14654 changed = 1;
14656 fini_reloc_cookie_for_section (&cookie, i);
14660 o = NULL;
14661 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14662 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14663 if (o != NULL)
14665 asection *i;
14666 int eh_changed = 0;
14667 unsigned int eh_alignment; /* Octets. */
14669 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14671 if (i->size == 0)
14672 continue;
14674 abfd = i->owner;
14675 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14676 continue;
14678 if (!init_reloc_cookie_for_section (&cookie, info, i))
14679 return -1;
14681 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14682 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14683 bfd_elf_reloc_symbol_deleted_p,
14684 &cookie))
14686 eh_changed = 1;
14687 if (i->size != i->rawsize)
14688 changed = 1;
14691 fini_reloc_cookie_for_section (&cookie, i);
14694 eh_alignment = ((1 << o->alignment_power)
14695 * bfd_octets_per_byte (output_bfd, o));
14696 /* Skip over zero terminator, and prevent empty sections from
14697 adding alignment padding at the end. */
14698 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14699 if (i->size == 0)
14700 i->flags |= SEC_EXCLUDE;
14701 else if (i->size > 4)
14702 break;
14703 /* The last non-empty eh_frame section doesn't need padding. */
14704 if (i != NULL)
14705 i = i->map_tail.s;
14706 /* Any prior sections must pad the last FDE out to the output
14707 section alignment. Otherwise we might have zero padding
14708 between sections, which would be seen as a terminator. */
14709 for (; i != NULL; i = i->map_tail.s)
14710 if (i->size == 4)
14711 /* All but the last zero terminator should have been removed. */
14712 BFD_FAIL ();
14713 else
14715 bfd_size_type size
14716 = (i->size + eh_alignment - 1) & -eh_alignment;
14717 if (i->size != size)
14719 i->size = size;
14720 changed = 1;
14721 eh_changed = 1;
14724 if (eh_changed)
14725 elf_link_hash_traverse (elf_hash_table (info),
14726 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14729 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14731 const struct elf_backend_data *bed;
14732 asection *s;
14734 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14735 continue;
14736 s = abfd->sections;
14737 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14738 continue;
14740 bed = get_elf_backend_data (abfd);
14742 if (bed->elf_backend_discard_info != NULL)
14744 if (!init_reloc_cookie (&cookie, info, abfd))
14745 return -1;
14747 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14748 changed = 1;
14750 fini_reloc_cookie (&cookie, abfd);
14754 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14755 _bfd_elf_end_eh_frame_parsing (info);
14757 if (info->eh_frame_hdr_type
14758 && !bfd_link_relocatable (info)
14759 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14760 changed = 1;
14762 return changed;
14765 bfd_boolean
14766 _bfd_elf_section_already_linked (bfd *abfd,
14767 asection *sec,
14768 struct bfd_link_info *info)
14770 flagword flags;
14771 const char *name, *key;
14772 struct bfd_section_already_linked *l;
14773 struct bfd_section_already_linked_hash_entry *already_linked_list;
14775 if (sec->output_section == bfd_abs_section_ptr)
14776 return FALSE;
14778 flags = sec->flags;
14780 /* Return if it isn't a linkonce section. A comdat group section
14781 also has SEC_LINK_ONCE set. */
14782 if ((flags & SEC_LINK_ONCE) == 0)
14783 return FALSE;
14785 /* Don't put group member sections on our list of already linked
14786 sections. They are handled as a group via their group section. */
14787 if (elf_sec_group (sec) != NULL)
14788 return FALSE;
14790 /* For a SHT_GROUP section, use the group signature as the key. */
14791 name = sec->name;
14792 if ((flags & SEC_GROUP) != 0
14793 && elf_next_in_group (sec) != NULL
14794 && elf_group_name (elf_next_in_group (sec)) != NULL)
14795 key = elf_group_name (elf_next_in_group (sec));
14796 else
14798 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14799 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14800 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14801 key++;
14802 else
14803 /* Must be a user linkonce section that doesn't follow gcc's
14804 naming convention. In this case we won't be matching
14805 single member groups. */
14806 key = name;
14809 already_linked_list = bfd_section_already_linked_table_lookup (key);
14811 for (l = already_linked_list->entry; l != NULL; l = l->next)
14813 /* We may have 2 different types of sections on the list: group
14814 sections with a signature of <key> (<key> is some string),
14815 and linkonce sections named .gnu.linkonce.<type>.<key>.
14816 Match like sections. LTO plugin sections are an exception.
14817 They are always named .gnu.linkonce.t.<key> and match either
14818 type of section. */
14819 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14820 && ((flags & SEC_GROUP) != 0
14821 || strcmp (name, l->sec->name) == 0))
14822 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14823 || (sec->owner->flags & BFD_PLUGIN) != 0)
14825 /* The section has already been linked. See if we should
14826 issue a warning. */
14827 if (!_bfd_handle_already_linked (sec, l, info))
14828 return FALSE;
14830 if (flags & SEC_GROUP)
14832 asection *first = elf_next_in_group (sec);
14833 asection *s = first;
14835 while (s != NULL)
14837 s->output_section = bfd_abs_section_ptr;
14838 /* Record which group discards it. */
14839 s->kept_section = l->sec;
14840 s = elf_next_in_group (s);
14841 /* These lists are circular. */
14842 if (s == first)
14843 break;
14847 return TRUE;
14851 /* A single member comdat group section may be discarded by a
14852 linkonce section and vice versa. */
14853 if ((flags & SEC_GROUP) != 0)
14855 asection *first = elf_next_in_group (sec);
14857 if (first != NULL && elf_next_in_group (first) == first)
14858 /* Check this single member group against linkonce sections. */
14859 for (l = already_linked_list->entry; l != NULL; l = l->next)
14860 if ((l->sec->flags & SEC_GROUP) == 0
14861 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14863 first->output_section = bfd_abs_section_ptr;
14864 first->kept_section = l->sec;
14865 sec->output_section = bfd_abs_section_ptr;
14866 break;
14869 else
14870 /* Check this linkonce section against single member groups. */
14871 for (l = already_linked_list->entry; l != NULL; l = l->next)
14872 if (l->sec->flags & SEC_GROUP)
14874 asection *first = elf_next_in_group (l->sec);
14876 if (first != NULL
14877 && elf_next_in_group (first) == first
14878 && bfd_elf_match_symbols_in_sections (first, sec, info))
14880 sec->output_section = bfd_abs_section_ptr;
14881 sec->kept_section = first;
14882 break;
14886 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14887 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14888 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14889 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14890 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14891 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14892 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14893 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14894 The reverse order cannot happen as there is never a bfd with only the
14895 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14896 matter as here were are looking only for cross-bfd sections. */
14898 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14899 for (l = already_linked_list->entry; l != NULL; l = l->next)
14900 if ((l->sec->flags & SEC_GROUP) == 0
14901 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14903 if (abfd != l->sec->owner)
14904 sec->output_section = bfd_abs_section_ptr;
14905 break;
14908 /* This is the first section with this name. Record it. */
14909 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14910 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14911 return sec->output_section == bfd_abs_section_ptr;
14914 bfd_boolean
14915 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14917 return sym->st_shndx == SHN_COMMON;
14920 unsigned int
14921 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14923 return SHN_COMMON;
14926 asection *
14927 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14929 return bfd_com_section_ptr;
14932 bfd_vma
14933 _bfd_elf_default_got_elt_size (bfd *abfd,
14934 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14935 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14936 bfd *ibfd ATTRIBUTE_UNUSED,
14937 unsigned long symndx ATTRIBUTE_UNUSED)
14939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14940 return bed->s->arch_size / 8;
14943 /* Routines to support the creation of dynamic relocs. */
14945 /* Returns the name of the dynamic reloc section associated with SEC. */
14947 static const char *
14948 get_dynamic_reloc_section_name (bfd * abfd,
14949 asection * sec,
14950 bfd_boolean is_rela)
14952 char *name;
14953 const char *old_name = bfd_section_name (sec);
14954 const char *prefix = is_rela ? ".rela" : ".rel";
14956 if (old_name == NULL)
14957 return NULL;
14959 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14960 sprintf (name, "%s%s", prefix, old_name);
14962 return name;
14965 /* Returns the dynamic reloc section associated with SEC.
14966 If necessary compute the name of the dynamic reloc section based
14967 on SEC's name (looked up in ABFD's string table) and the setting
14968 of IS_RELA. */
14970 asection *
14971 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14972 asection * sec,
14973 bfd_boolean is_rela)
14975 asection * reloc_sec = elf_section_data (sec)->sreloc;
14977 if (reloc_sec == NULL)
14979 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14981 if (name != NULL)
14983 reloc_sec = bfd_get_linker_section (abfd, name);
14985 if (reloc_sec != NULL)
14986 elf_section_data (sec)->sreloc = reloc_sec;
14990 return reloc_sec;
14993 /* Returns the dynamic reloc section associated with SEC. If the
14994 section does not exist it is created and attached to the DYNOBJ
14995 bfd and stored in the SRELOC field of SEC's elf_section_data
14996 structure.
14998 ALIGNMENT is the alignment for the newly created section and
14999 IS_RELA defines whether the name should be .rela.<SEC's name>
15000 or .rel.<SEC's name>. The section name is looked up in the
15001 string table associated with ABFD. */
15003 asection *
15004 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15005 bfd *dynobj,
15006 unsigned int alignment,
15007 bfd *abfd,
15008 bfd_boolean is_rela)
15010 asection * reloc_sec = elf_section_data (sec)->sreloc;
15012 if (reloc_sec == NULL)
15014 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15016 if (name == NULL)
15017 return NULL;
15019 reloc_sec = bfd_get_linker_section (dynobj, name);
15021 if (reloc_sec == NULL)
15023 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15024 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15025 if ((sec->flags & SEC_ALLOC) != 0)
15026 flags |= SEC_ALLOC | SEC_LOAD;
15028 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15029 if (reloc_sec != NULL)
15031 /* _bfd_elf_get_sec_type_attr chooses a section type by
15032 name. Override as it may be wrong, eg. for a user
15033 section named "auto" we'll get ".relauto" which is
15034 seen to be a .rela section. */
15035 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15036 if (!bfd_set_section_alignment (reloc_sec, alignment))
15037 reloc_sec = NULL;
15041 elf_section_data (sec)->sreloc = reloc_sec;
15044 return reloc_sec;
15047 /* Copy the ELF symbol type and other attributes for a linker script
15048 assignment from HSRC to HDEST. Generally this should be treated as
15049 if we found a strong non-dynamic definition for HDEST (except that
15050 ld ignores multiple definition errors). */
15051 void
15052 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15053 struct bfd_link_hash_entry *hdest,
15054 struct bfd_link_hash_entry *hsrc)
15056 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15057 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15058 Elf_Internal_Sym isym;
15060 ehdest->type = ehsrc->type;
15061 ehdest->target_internal = ehsrc->target_internal;
15063 isym.st_other = ehsrc->other;
15064 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
15067 /* Append a RELA relocation REL to section S in BFD. */
15069 void
15070 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15073 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15074 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15075 bed->s->swap_reloca_out (abfd, rel, loc);
15078 /* Append a REL relocation REL to section S in BFD. */
15080 void
15081 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15084 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15085 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15086 bed->s->swap_reloc_out (abfd, rel, loc);
15089 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15091 struct bfd_link_hash_entry *
15092 bfd_elf_define_start_stop (struct bfd_link_info *info,
15093 const char *symbol, asection *sec)
15095 struct elf_link_hash_entry *h;
15097 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15098 FALSE, FALSE, TRUE);
15099 /* NB: Common symbols will be turned into definition later. */
15100 if (h != NULL
15101 && (h->root.type == bfd_link_hash_undefined
15102 || h->root.type == bfd_link_hash_undefweak
15103 || ((h->ref_regular || h->def_dynamic)
15104 && !h->def_regular
15105 && h->root.type != bfd_link_hash_common)))
15107 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
15108 h->verinfo.verdef = NULL;
15109 h->root.type = bfd_link_hash_defined;
15110 h->root.u.def.section = sec;
15111 h->root.u.def.value = 0;
15112 h->def_regular = 1;
15113 h->def_dynamic = 0;
15114 h->start_stop = 1;
15115 h->u2.start_stop_section = sec;
15116 if (symbol[0] == '.')
15118 /* .startof. and .sizeof. symbols are local. */
15119 const struct elf_backend_data *bed;
15120 bed = get_elf_backend_data (info->output_bfd);
15121 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
15123 else
15125 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15126 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15127 | info->start_stop_visibility);
15128 if (was_dynamic)
15129 bfd_elf_link_record_dynamic_symbol (info, h);
15131 return &h->root;
15133 return NULL;
15136 /* Find dynamic relocs for H that apply to read-only sections. */
15138 asection *
15139 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15141 struct elf_dyn_relocs *p;
15143 for (p = h->dyn_relocs; p != NULL; p = p->next)
15145 asection *s = p->sec->output_section;
15147 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15148 return p->sec;
15150 return NULL;
15153 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15154 read-only sections. */
15156 bfd_boolean
15157 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15159 asection *sec;
15161 if (h->root.type == bfd_link_hash_indirect)
15162 return TRUE;
15164 sec = _bfd_elf_readonly_dynrelocs (h);
15165 if (sec != NULL)
15167 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15169 info->flags |= DF_TEXTREL;
15170 /* xgettext:c-format */
15171 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15172 "in read-only section `%pA'\n"),
15173 sec->owner, h->root.root.string, sec);
15175 if (bfd_link_textrel_check (info))
15176 /* xgettext:c-format */
15177 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15178 "in read-only section `%pA'\n"),
15179 sec->owner, h->root.root.string, sec);
15181 /* Not an error, just cut short the traversal. */
15182 return FALSE;
15184 return TRUE;
15187 /* Add dynamic tags. */
15189 bfd_boolean
15190 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15191 bfd_boolean need_dynamic_reloc)
15193 struct elf_link_hash_table *htab = elf_hash_table (info);
15195 if (htab->dynamic_sections_created)
15197 /* Add some entries to the .dynamic section. We fill in the
15198 values later, in finish_dynamic_sections, but we must add
15199 the entries now so that we get the correct size for the
15200 .dynamic section. The DT_DEBUG entry is filled in by the
15201 dynamic linker and used by the debugger. */
15202 #define add_dynamic_entry(TAG, VAL) \
15203 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15205 const struct elf_backend_data *bed
15206 = get_elf_backend_data (output_bfd);
15208 if (bfd_link_executable (info))
15210 if (!add_dynamic_entry (DT_DEBUG, 0))
15211 return FALSE;
15214 if (htab->dt_pltgot_required || htab->splt->size != 0)
15216 /* DT_PLTGOT is used by prelink even if there is no PLT
15217 relocation. */
15218 if (!add_dynamic_entry (DT_PLTGOT, 0))
15219 return FALSE;
15222 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15224 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15225 || !add_dynamic_entry (DT_PLTREL,
15226 (bed->rela_plts_and_copies_p
15227 ? DT_RELA : DT_REL))
15228 || !add_dynamic_entry (DT_JMPREL, 0))
15229 return FALSE;
15232 if (htab->tlsdesc_plt
15233 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15234 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15235 return FALSE;
15237 if (need_dynamic_reloc)
15239 if (bed->rela_plts_and_copies_p)
15241 if (!add_dynamic_entry (DT_RELA, 0)
15242 || !add_dynamic_entry (DT_RELASZ, 0)
15243 || !add_dynamic_entry (DT_RELAENT,
15244 bed->s->sizeof_rela))
15245 return FALSE;
15247 else
15249 if (!add_dynamic_entry (DT_REL, 0)
15250 || !add_dynamic_entry (DT_RELSZ, 0)
15251 || !add_dynamic_entry (DT_RELENT,
15252 bed->s->sizeof_rel))
15253 return FALSE;
15256 /* If any dynamic relocs apply to a read-only section,
15257 then we need a DT_TEXTREL entry. */
15258 if ((info->flags & DF_TEXTREL) == 0)
15259 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15260 info);
15262 if ((info->flags & DF_TEXTREL) != 0)
15264 if (htab->ifunc_resolvers)
15265 info->callbacks->einfo
15266 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15267 "may result in a segfault at runtime; recompile with %s\n"),
15268 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15270 if (!add_dynamic_entry (DT_TEXTREL, 0))
15271 return FALSE;
15275 #undef add_dynamic_entry
15277 return TRUE;