[Aarch64] Add Binutils support for MEC
[binutils-gdb.git] / bfd / elflink.c
blobbab1a36598ea8f4281e7dcf1637825099ade362e
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2023 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 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info *info;
46 bool failed;
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
52 struct elf_find_verdep_info
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
65 asection *
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73 struct elf_link_hash_entry *h;
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
88 else
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
103 return NULL;
106 /* Define a symbol in a dynamic linkage section. */
108 struct elf_link_hash_entry *
109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
128 else
129 bh = NULL;
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
149 bool
150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
162 flags = bed->dynamic_sec_flags;
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
180 if (bed->want_got_plt)
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
192 if (bed->want_got_sym)
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
205 return true;
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
212 struct elf_link_hash_table *hash_table;
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 abfd = ibfd;
234 break;
237 hash_table->dynobj = abfd;
240 if (hash_table->dynstr == NULL)
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
246 return true;
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
256 bool
257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
264 if (! is_elf_hash_table (info->hash))
265 return false;
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
276 flags = bed->dynamic_sec_flags;
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
336 if (info->emit_hash)
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 if (info->enable_dt_relr)
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
380 elf_hash_table (info)->dynamic_sections_created = true;
382 return true;
385 /* Create dynamic sections when linking against a dynamic object. */
387 bool
388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
440 if (bed->want_dynbss)
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
454 if (bed->want_dynrelro)
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
488 if (bed->want_dynrelro)
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
502 return true;
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
513 bool
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
517 if (h->dynindx == -1)
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
557 default:
558 break;
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
587 if (p != NULL)
588 *p = ELF_VER_CHR;
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
595 return true;
598 /* Mark a symbol dynamic. */
600 static void
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
631 bool
632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
642 if (!is_elf_hash_table (info->hash))
643 return true;
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
653 if (h->versioned == unknown)
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
674 switch (h->root.type)
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
730 h->def_regular = 1;
732 if (hidden)
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
763 struct elf_link_hash_entry *def = weakdef (h);
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
771 return true;
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
792 if (! is_elf_hash_table (info->hash))
793 return 0;
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
809 bfd_release (input_bfd, entry);
810 return 0;
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
816 asection *s;
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
846 eht = elf_hash_table (info);
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
858 /* The dynindx will be set at the end of size_dynamic_sections. */
860 return 1;
863 /* Return the dynindex of a local dynamic symbol. */
865 long
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
870 struct elf_link_local_dynamic_entry *e;
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
882 static bool
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
886 size_t *count = (size_t *) data;
888 if (h->forced_local)
889 return true;
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
894 return true;
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
901 static bool
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
905 size_t *count = (size_t *) data;
907 if (!h->forced_local)
908 return true;
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
913 return true;
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
923 struct elf_link_hash_table *htab;
924 asection *ip;
926 switch (elf_section_data (p)->this_hdr.sh_type)
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
948 bool
949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
954 return true;
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
966 static unsigned long
967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
992 if (do_sec)
993 *section_sym_count = dynsymcount;
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
999 if (elf_hash_table (info)->dynlocal)
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1021 /* Merge st_other field. */
1023 static void
1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1036 if (!dynamic)
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1065 static bool
1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093 struct elf_link_hash_table *htab;
1095 *skip = false;
1096 *override = NULL;
1098 sec = *psec;
1099 bind = ELF_ST_BIND (sym->st_info);
1101 if (! bfd_is_und_section (sec))
1102 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1103 else
1104 h = ((struct elf_link_hash_entry *)
1105 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1106 if (h == NULL)
1107 return false;
1108 *sym_hash = h;
1110 bed = get_elf_backend_data (abfd);
1112 /* NEW_VERSION is the symbol version of the new symbol. */
1113 if (h->versioned != unversioned)
1115 /* Symbol version is unknown or versioned. */
1116 new_version = strrchr (name, ELF_VER_CHR);
1117 if (new_version)
1119 if (h->versioned == unknown)
1121 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1122 h->versioned = versioned_hidden;
1123 else
1124 h->versioned = versioned;
1126 new_version += 1;
1127 if (new_version[0] == '\0')
1128 new_version = NULL;
1130 else
1131 h->versioned = unversioned;
1133 else
1134 new_version = NULL;
1136 /* For merging, we only care about real symbols. But we need to make
1137 sure that indirect symbol dynamic flags are updated. */
1138 hi = h;
1139 while (h->root.type == bfd_link_hash_indirect
1140 || h->root.type == bfd_link_hash_warning)
1141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1143 if (!*matched)
1145 if (hi == h || h->root.type == bfd_link_hash_new)
1146 *matched = true;
1147 else
1149 /* OLD_HIDDEN is true if the existing symbol is only visible
1150 to the symbol with the same symbol version. NEW_HIDDEN is
1151 true if the new symbol is only visible to the symbol with
1152 the same symbol version. */
1153 bool old_hidden = h->versioned == versioned_hidden;
1154 bool new_hidden = hi->versioned == versioned_hidden;
1155 if (!old_hidden && !new_hidden)
1156 /* The new symbol matches the existing symbol if both
1157 aren't hidden. */
1158 *matched = true;
1159 else
1161 /* OLD_VERSION is the symbol version of the existing
1162 symbol. */
1163 char *old_version;
1165 if (h->versioned >= versioned)
1166 old_version = strrchr (h->root.root.string,
1167 ELF_VER_CHR) + 1;
1168 else
1169 old_version = NULL;
1171 /* The new symbol matches the existing symbol if they
1172 have the same symbol version. */
1173 *matched = (old_version == new_version
1174 || (old_version != NULL
1175 && new_version != NULL
1176 && strcmp (old_version, new_version) == 0));
1181 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1182 existing symbol. */
1184 oldbfd = NULL;
1185 oldsec = NULL;
1186 switch (h->root.type)
1188 default:
1189 break;
1191 case bfd_link_hash_undefined:
1192 case bfd_link_hash_undefweak:
1193 oldbfd = h->root.u.undef.abfd;
1194 break;
1196 case bfd_link_hash_defined:
1197 case bfd_link_hash_defweak:
1198 oldbfd = h->root.u.def.section->owner;
1199 oldsec = h->root.u.def.section;
1200 break;
1202 case bfd_link_hash_common:
1203 oldbfd = h->root.u.c.p->section->owner;
1204 oldsec = h->root.u.c.p->section;
1205 if (pold_alignment)
1206 *pold_alignment = h->root.u.c.p->alignment_power;
1207 break;
1209 if (poldbfd && *poldbfd == NULL)
1210 *poldbfd = oldbfd;
1212 /* Differentiate strong and weak symbols. */
1213 newweak = bind == STB_WEAK;
1214 oldweak = (h->root.type == bfd_link_hash_defweak
1215 || h->root.type == bfd_link_hash_undefweak);
1216 if (pold_weak)
1217 *pold_weak = oldweak;
1219 /* We have to check it for every instance since the first few may be
1220 references and not all compilers emit symbol type for undefined
1221 symbols. */
1222 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1224 htab = elf_hash_table (info);
1226 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227 respectively, is from a dynamic object. */
1229 newdyn = (abfd->flags & DYNAMIC) != 0;
1231 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232 syms and defined syms in dynamic libraries respectively.
1233 ref_dynamic on the other hand can be set for a symbol defined in
1234 a dynamic library, and def_dynamic may not be set; When the
1235 definition in a dynamic lib is overridden by a definition in the
1236 executable use of the symbol in the dynamic lib becomes a
1237 reference to the executable symbol. */
1238 if (newdyn)
1240 if (bfd_is_und_section (sec))
1242 if (bind != STB_WEAK)
1244 h->ref_dynamic_nonweak = 1;
1245 hi->ref_dynamic_nonweak = 1;
1248 else
1250 /* Update the existing symbol only if they match. */
1251 if (*matched)
1252 h->dynamic_def = 1;
1253 hi->dynamic_def = 1;
1257 /* If we just created the symbol, mark it as being an ELF symbol.
1258 Other than that, there is nothing to do--there is no merge issue
1259 with a newly defined symbol--so we just return. */
1261 if (h->root.type == bfd_link_hash_new)
1263 h->non_elf = 0;
1264 return true;
1267 /* In cases involving weak versioned symbols, we may wind up trying
1268 to merge a symbol with itself. Catch that here, to avoid the
1269 confusion that results if we try to override a symbol with
1270 itself. The additional tests catch cases like
1271 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272 dynamic object, which we do want to handle here. */
1273 if (abfd == oldbfd
1274 && (newweak || oldweak)
1275 && ((abfd->flags & DYNAMIC) == 0
1276 || !h->def_regular))
1277 return true;
1279 olddyn = false;
1280 if (oldbfd != NULL)
1281 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1282 else if (oldsec != NULL)
1284 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285 indices used by MIPS ELF. */
1286 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1289 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1290 if (!htab->handling_dt_needed
1291 && oldbfd != NULL
1292 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1294 if (newdyn != olddyn)
1296 /* Handle a case where plugin_notice won't be called and thus
1297 won't set the non_ir_ref flags on the first pass over
1298 symbols. */
1299 h->root.non_ir_ref_dynamic = true;
1300 hi->root.non_ir_ref_dynamic = true;
1302 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1303 && hi->root.type == bfd_link_hash_indirect)
1305 /* Change indirect symbol from IR to undefined. */
1306 hi->root.type = bfd_link_hash_undefined;
1307 hi->root.u.undef.abfd = oldbfd;
1311 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312 respectively, appear to be a definition rather than reference. */
1314 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1316 olddef = (h->root.type != bfd_link_hash_undefined
1317 && h->root.type != bfd_link_hash_undefweak
1318 && h->root.type != bfd_link_hash_common);
1320 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321 respectively, appear to be a function. */
1323 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1324 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1326 oldfunc = (h->type != STT_NOTYPE
1327 && bed->is_function_type (h->type));
1329 if (!(newfunc && oldfunc)
1330 && ELF_ST_TYPE (sym->st_info) != h->type
1331 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1332 && h->type != STT_NOTYPE
1333 && (newdef || bfd_is_com_section (sec))
1334 && (olddef || h->root.type == bfd_link_hash_common))
1336 /* If creating a default indirect symbol ("foo" or "foo@") from
1337 a dynamic versioned definition ("foo@@") skip doing so if
1338 there is an existing regular definition with a different
1339 type. We don't want, for example, a "time" variable in the
1340 executable overriding a "time" function in a shared library. */
1341 if (newdyn
1342 && !olddyn)
1344 *skip = true;
1345 return true;
1348 /* When adding a symbol from a regular object file after we have
1349 created indirect symbols, undo the indirection and any
1350 dynamic state. */
1351 if (hi != h
1352 && !newdyn
1353 && olddyn)
1355 h = hi;
1356 (*bed->elf_backend_hide_symbol) (info, h, true);
1357 h->forced_local = 0;
1358 h->ref_dynamic = 0;
1359 h->def_dynamic = 0;
1360 h->dynamic_def = 0;
1361 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1363 h->root.type = bfd_link_hash_undefined;
1364 h->root.u.undef.abfd = abfd;
1366 else
1368 h->root.type = bfd_link_hash_new;
1369 h->root.u.undef.abfd = NULL;
1371 return true;
1375 /* Check TLS symbols. We don't check undefined symbols introduced
1376 by "ld -u" which have no type (and oldbfd NULL), and we don't
1377 check symbols from plugins because they also have no type. */
1378 if (oldbfd != NULL
1379 && (oldbfd->flags & BFD_PLUGIN) == 0
1380 && (abfd->flags & BFD_PLUGIN) == 0
1381 && ELF_ST_TYPE (sym->st_info) != h->type
1382 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1384 bfd *ntbfd, *tbfd;
1385 bool ntdef, tdef;
1386 asection *ntsec, *tsec;
1388 if (h->type == STT_TLS)
1390 ntbfd = abfd;
1391 ntsec = sec;
1392 ntdef = newdef;
1393 tbfd = oldbfd;
1394 tsec = oldsec;
1395 tdef = olddef;
1397 else
1399 ntbfd = oldbfd;
1400 ntsec = oldsec;
1401 ntdef = olddef;
1402 tbfd = abfd;
1403 tsec = sec;
1404 tdef = newdef;
1407 if (tdef && ntdef)
1408 _bfd_error_handler
1409 /* xgettext:c-format */
1410 (_("%s: TLS definition in %pB section %pA "
1411 "mismatches non-TLS definition in %pB section %pA"),
1412 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1413 else if (!tdef && !ntdef)
1414 _bfd_error_handler
1415 /* xgettext:c-format */
1416 (_("%s: TLS reference in %pB "
1417 "mismatches non-TLS reference in %pB"),
1418 h->root.root.string, tbfd, ntbfd);
1419 else if (tdef)
1420 _bfd_error_handler
1421 /* xgettext:c-format */
1422 (_("%s: TLS definition in %pB section %pA "
1423 "mismatches non-TLS reference in %pB"),
1424 h->root.root.string, tbfd, tsec, ntbfd);
1425 else
1426 _bfd_error_handler
1427 /* xgettext:c-format */
1428 (_("%s: TLS reference in %pB "
1429 "mismatches non-TLS definition in %pB section %pA"),
1430 h->root.root.string, tbfd, ntbfd, ntsec);
1432 bfd_set_error (bfd_error_bad_value);
1433 return false;
1436 /* If the old symbol has non-default visibility, we ignore the new
1437 definition from a dynamic object. */
1438 if (newdyn
1439 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440 && !bfd_is_und_section (sec))
1442 *skip = true;
1443 /* Make sure this symbol is dynamic. */
1444 h->ref_dynamic = 1;
1445 hi->ref_dynamic = 1;
1446 /* A protected symbol has external availability. Make sure it is
1447 recorded as dynamic.
1449 FIXME: Should we check type and size for protected symbol? */
1450 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1451 return bfd_elf_link_record_dynamic_symbol (info, h);
1452 else
1453 return true;
1455 else if (!newdyn
1456 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1457 && h->def_dynamic)
1459 /* If the new symbol with non-default visibility comes from a
1460 relocatable file and the old definition comes from a dynamic
1461 object, we remove the old definition. */
1462 if (hi->root.type == bfd_link_hash_indirect)
1464 /* Handle the case where the old dynamic definition is
1465 default versioned. We need to copy the symbol info from
1466 the symbol with default version to the normal one if it
1467 was referenced before. */
1468 if (h->ref_regular)
1470 hi->root.type = h->root.type;
1471 h->root.type = bfd_link_hash_indirect;
1472 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1474 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1475 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1477 /* If the new symbol is hidden or internal, completely undo
1478 any dynamic link state. */
1479 (*bed->elf_backend_hide_symbol) (info, h, true);
1480 h->forced_local = 0;
1481 h->ref_dynamic = 0;
1483 else
1484 h->ref_dynamic = 1;
1486 h->def_dynamic = 0;
1487 /* FIXME: Should we check type and size for protected symbol? */
1488 h->size = 0;
1489 h->type = 0;
1491 h = hi;
1493 else
1494 h = hi;
1497 /* If the old symbol was undefined before, then it will still be
1498 on the undefs list. If the new symbol is undefined or
1499 common, we can't make it bfd_link_hash_new here, because new
1500 undefined or common symbols will be added to the undefs list
1501 by _bfd_generic_link_add_one_symbol. Symbols may not be
1502 added twice to the undefs list. Also, if the new symbol is
1503 undefweak then we don't want to lose the strong undef. */
1504 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1506 h->root.type = bfd_link_hash_undefined;
1507 h->root.u.undef.abfd = abfd;
1509 else
1511 h->root.type = bfd_link_hash_new;
1512 h->root.u.undef.abfd = NULL;
1515 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1517 /* If the new symbol is hidden or internal, completely undo
1518 any dynamic link state. */
1519 (*bed->elf_backend_hide_symbol) (info, h, true);
1520 h->forced_local = 0;
1521 h->ref_dynamic = 0;
1523 else
1524 h->ref_dynamic = 1;
1525 h->def_dynamic = 0;
1526 /* FIXME: Should we check type and size for protected symbol? */
1527 h->size = 0;
1528 h->type = 0;
1529 return true;
1532 /* If a new weak symbol definition comes from a regular file and the
1533 old symbol comes from a dynamic library, we treat the new one as
1534 strong. Similarly, an old weak symbol definition from a regular
1535 file is treated as strong when the new symbol comes from a dynamic
1536 library. Further, an old weak symbol from a dynamic library is
1537 treated as strong if the new symbol is from a dynamic library.
1538 This reflects the way glibc's ld.so works.
1540 Also allow a weak symbol to override a linker script symbol
1541 defined by an early pass over the script. This is done so the
1542 linker knows the symbol is defined in an object file, for the
1543 DEFINED script function.
1545 Do this before setting *type_change_ok or *size_change_ok so that
1546 we warn properly when dynamic library symbols are overridden. */
1548 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1549 newweak = false;
1550 if (olddef && newdyn)
1551 oldweak = false;
1553 /* Allow changes between different types of function symbol. */
1554 if (newfunc && oldfunc)
1555 *type_change_ok = true;
1557 /* It's OK to change the type if either the existing symbol or the
1558 new symbol is weak. A type change is also OK if the old symbol
1559 is undefined and the new symbol is defined. */
1561 if (oldweak
1562 || newweak
1563 || (newdef
1564 && h->root.type == bfd_link_hash_undefined))
1565 *type_change_ok = true;
1567 /* It's OK to change the size if either the existing symbol or the
1568 new symbol is weak, or if the old symbol is undefined. */
1570 if (*type_change_ok
1571 || h->root.type == bfd_link_hash_undefined)
1572 *size_change_ok = true;
1574 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575 symbol, respectively, appears to be a common symbol in a dynamic
1576 object. If a symbol appears in an uninitialized section, and is
1577 not weak, and is not a function, then it may be a common symbol
1578 which was resolved when the dynamic object was created. We want
1579 to treat such symbols specially, because they raise special
1580 considerations when setting the symbol size: if the symbol
1581 appears as a common symbol in a regular object, and the size in
1582 the regular object is larger, we must make sure that we use the
1583 larger size. This problematic case can always be avoided in C,
1584 but it must be handled correctly when using Fortran shared
1585 libraries.
1587 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588 likewise for OLDDYNCOMMON and OLDDEF.
1590 Note that this test is just a heuristic, and that it is quite
1591 possible to have an uninitialized symbol in a shared object which
1592 is really a definition, rather than a common symbol. This could
1593 lead to some minor confusion when the symbol really is a common
1594 symbol in some regular object. However, I think it will be
1595 harmless. */
1597 if (newdyn
1598 && newdef
1599 && !newweak
1600 && (sec->flags & SEC_ALLOC) != 0
1601 && (sec->flags & SEC_LOAD) == 0
1602 && sym->st_size > 0
1603 && !newfunc)
1604 newdyncommon = true;
1605 else
1606 newdyncommon = false;
1608 if (olddyn
1609 && olddef
1610 && h->root.type == bfd_link_hash_defined
1611 && h->def_dynamic
1612 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1613 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1614 && h->size > 0
1615 && !oldfunc)
1616 olddyncommon = true;
1617 else
1618 olddyncommon = false;
1620 /* We now know everything about the old and new symbols. We ask the
1621 backend to check if we can merge them. */
1622 if (bed->merge_symbol != NULL)
1624 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1625 return false;
1626 sec = *psec;
1629 /* There are multiple definitions of a normal symbol. Skip the
1630 default symbol as well as definition from an IR object. */
1631 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1632 && !default_sym && h->def_regular
1633 && !(oldbfd != NULL
1634 && (oldbfd->flags & BFD_PLUGIN) != 0
1635 && (abfd->flags & BFD_PLUGIN) == 0))
1637 /* Handle a multiple definition. */
1638 (*info->callbacks->multiple_definition) (info, &h->root,
1639 abfd, sec, *pvalue);
1640 *skip = true;
1641 return true;
1644 /* If both the old and the new symbols look like common symbols in a
1645 dynamic object, set the size of the symbol to the larger of the
1646 two. */
1648 if (olddyncommon
1649 && newdyncommon
1650 && sym->st_size != h->size)
1652 /* Since we think we have two common symbols, issue a multiple
1653 common warning if desired. Note that we only warn if the
1654 size is different. If the size is the same, we simply let
1655 the old symbol override the new one as normally happens with
1656 symbols defined in dynamic objects. */
1658 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1659 bfd_link_hash_common, sym->st_size);
1660 if (sym->st_size > h->size)
1661 h->size = sym->st_size;
1663 *size_change_ok = true;
1666 /* If we are looking at a dynamic object, and we have found a
1667 definition, we need to see if the symbol was already defined by
1668 some other object. If so, we want to use the existing
1669 definition, and we do not want to report a multiple symbol
1670 definition error; we do this by clobbering *PSEC to be
1671 bfd_und_section_ptr.
1673 We treat a common symbol as a definition if the symbol in the
1674 shared library is a function, since common symbols always
1675 represent variables; this can cause confusion in principle, but
1676 any such confusion would seem to indicate an erroneous program or
1677 shared library. We also permit a common symbol in a regular
1678 object to override a weak symbol in a shared object. */
1680 if (newdyn
1681 && newdef
1682 && (olddef
1683 || (h->root.type == bfd_link_hash_common
1684 && (newweak || newfunc))))
1686 *override = abfd;
1687 newdef = false;
1688 newdyncommon = false;
1690 *psec = sec = bfd_und_section_ptr;
1691 *size_change_ok = true;
1693 /* If we get here when the old symbol is a common symbol, then
1694 we are explicitly letting it override a weak symbol or
1695 function in a dynamic object, and we don't want to warn about
1696 a type change. If the old symbol is a defined symbol, a type
1697 change warning may still be appropriate. */
1699 if (h->root.type == bfd_link_hash_common)
1700 *type_change_ok = true;
1703 /* Handle the special case of an old common symbol merging with a
1704 new symbol which looks like a common symbol in a shared object.
1705 We change *PSEC and *PVALUE to make the new symbol look like a
1706 common symbol, and let _bfd_generic_link_add_one_symbol do the
1707 right thing. */
1709 if (newdyncommon
1710 && h->root.type == bfd_link_hash_common)
1712 *override = oldbfd;
1713 newdef = false;
1714 newdyncommon = false;
1715 *pvalue = sym->st_size;
1716 *psec = sec = bed->common_section (oldsec);
1717 *size_change_ok = true;
1720 /* Skip weak definitions of symbols that are already defined. */
1721 if (newdef && olddef && newweak)
1723 /* Don't skip new non-IR weak syms. */
1724 if (!(oldbfd != NULL
1725 && (oldbfd->flags & BFD_PLUGIN) != 0
1726 && (abfd->flags & BFD_PLUGIN) == 0))
1728 newdef = false;
1729 *skip = true;
1732 /* Merge st_other. If the symbol already has a dynamic index,
1733 but visibility says it should not be visible, turn it into a
1734 local symbol. */
1735 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1736 if (h->dynindx != -1)
1737 switch (ELF_ST_VISIBILITY (h->other))
1739 case STV_INTERNAL:
1740 case STV_HIDDEN:
1741 (*bed->elf_backend_hide_symbol) (info, h, true);
1742 break;
1746 /* If the old symbol is from a dynamic object, and the new symbol is
1747 a definition which is not from a dynamic object, then the new
1748 symbol overrides the old symbol. Symbols from regular files
1749 always take precedence over symbols from dynamic objects, even if
1750 they are defined after the dynamic object in the link.
1752 As above, we again permit a common symbol in a regular object to
1753 override a definition in a shared object if the shared object
1754 symbol is a function or is weak. */
1756 flip = NULL;
1757 if (!newdyn
1758 && (newdef
1759 || (bfd_is_com_section (sec)
1760 && (oldweak || oldfunc)))
1761 && olddyn
1762 && olddef
1763 && h->def_dynamic)
1765 /* Change the hash table entry to undefined, and let
1766 _bfd_generic_link_add_one_symbol do the right thing with the
1767 new definition. */
1769 h->root.type = bfd_link_hash_undefined;
1770 h->root.u.undef.abfd = h->root.u.def.section->owner;
1771 *size_change_ok = true;
1773 olddef = false;
1774 olddyncommon = false;
1776 /* We again permit a type change when a common symbol may be
1777 overriding a function. */
1779 if (bfd_is_com_section (sec))
1781 if (oldfunc)
1783 /* If a common symbol overrides a function, make sure
1784 that it isn't defined dynamically nor has type
1785 function. */
1786 h->def_dynamic = 0;
1787 h->type = STT_NOTYPE;
1789 *type_change_ok = true;
1792 if (hi->root.type == bfd_link_hash_indirect)
1793 flip = hi;
1794 else
1795 /* This union may have been set to be non-NULL when this symbol
1796 was seen in a dynamic object. We must force the union to be
1797 NULL, so that it is correct for a regular symbol. */
1798 h->verinfo.vertree = NULL;
1801 /* Handle the special case of a new common symbol merging with an
1802 old symbol that looks like it might be a common symbol defined in
1803 a shared object. Note that we have already handled the case in
1804 which a new common symbol should simply override the definition
1805 in the shared library. */
1807 if (! newdyn
1808 && bfd_is_com_section (sec)
1809 && olddyncommon)
1811 /* It would be best if we could set the hash table entry to a
1812 common symbol, but we don't know what to use for the section
1813 or the alignment. */
1814 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1815 bfd_link_hash_common, sym->st_size);
1817 /* If the presumed common symbol in the dynamic object is
1818 larger, pretend that the new symbol has its size. */
1820 if (h->size > *pvalue)
1821 *pvalue = h->size;
1823 /* We need to remember the alignment required by the symbol
1824 in the dynamic object. */
1825 BFD_ASSERT (pold_alignment);
1826 *pold_alignment = h->root.u.def.section->alignment_power;
1828 olddef = false;
1829 olddyncommon = false;
1831 h->root.type = bfd_link_hash_undefined;
1832 h->root.u.undef.abfd = h->root.u.def.section->owner;
1834 *size_change_ok = true;
1835 *type_change_ok = true;
1837 if (hi->root.type == bfd_link_hash_indirect)
1838 flip = hi;
1839 else
1840 h->verinfo.vertree = NULL;
1843 if (flip != NULL)
1845 /* Handle the case where we had a versioned symbol in a dynamic
1846 library and now find a definition in a normal object. In this
1847 case, we make the versioned symbol point to the normal one. */
1848 flip->root.type = h->root.type;
1849 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1850 h->root.type = bfd_link_hash_indirect;
1851 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1852 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1853 if (h->def_dynamic)
1855 h->def_dynamic = 0;
1856 flip->ref_dynamic = 1;
1860 return true;
1863 /* This function is called to create an indirect symbol from the
1864 default for the symbol with the default version if needed. The
1865 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1866 set DYNSYM if the new indirect symbol is dynamic. */
1868 static bool
1869 _bfd_elf_add_default_symbol (bfd *abfd,
1870 struct bfd_link_info *info,
1871 struct elf_link_hash_entry *h,
1872 const char *name,
1873 Elf_Internal_Sym *sym,
1874 asection *sec,
1875 bfd_vma value,
1876 bfd **poldbfd,
1877 bool *dynsym)
1879 bool type_change_ok;
1880 bool size_change_ok;
1881 bool skip;
1882 char *shortname;
1883 struct elf_link_hash_entry *hi;
1884 struct bfd_link_hash_entry *bh;
1885 const struct elf_backend_data *bed;
1886 bool collect;
1887 bool dynamic;
1888 bfd *override;
1889 char *p;
1890 size_t len, shortlen;
1891 asection *tmp_sec;
1892 bool matched;
1894 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1895 return true;
1897 /* If this symbol has a version, and it is the default version, we
1898 create an indirect symbol from the default name to the fully
1899 decorated name. This will cause external references which do not
1900 specify a version to be bound to this version of the symbol. */
1901 p = strchr (name, ELF_VER_CHR);
1902 if (h->versioned == unknown)
1904 if (p == NULL)
1906 h->versioned = unversioned;
1907 return true;
1909 else
1911 if (p[1] != ELF_VER_CHR)
1913 h->versioned = versioned_hidden;
1914 return true;
1916 else
1917 h->versioned = versioned;
1920 else
1922 /* PR ld/19073: We may see an unversioned definition after the
1923 default version. */
1924 if (p == NULL)
1925 return true;
1928 bed = get_elf_backend_data (abfd);
1929 collect = bed->collect;
1930 dynamic = (abfd->flags & DYNAMIC) != 0;
1932 shortlen = p - name;
1933 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1934 if (shortname == NULL)
1935 return false;
1936 memcpy (shortname, name, shortlen);
1937 shortname[shortlen] = '\0';
1939 /* We are going to create a new symbol. Merge it with any existing
1940 symbol with this name. For the purposes of the merge, act as
1941 though we were defining the symbol we just defined, although we
1942 actually going to define an indirect symbol. */
1943 type_change_ok = false;
1944 size_change_ok = false;
1945 matched = true;
1946 tmp_sec = sec;
1947 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1948 &hi, poldbfd, NULL, NULL, &skip, &override,
1949 &type_change_ok, &size_change_ok, &matched))
1950 return false;
1952 if (skip)
1953 goto nondefault;
1955 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1957 /* If the undecorated symbol will have a version added by a
1958 script different to H, then don't indirect to/from the
1959 undecorated symbol. This isn't ideal because we may not yet
1960 have seen symbol versions, if given by a script on the
1961 command line rather than via --version-script. */
1962 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1964 bool hide;
1966 hi->verinfo.vertree
1967 = bfd_find_version_for_sym (info->version_info,
1968 hi->root.root.string, &hide);
1969 if (hi->verinfo.vertree != NULL && hide)
1971 (*bed->elf_backend_hide_symbol) (info, hi, true);
1972 goto nondefault;
1975 if (hi->verinfo.vertree != NULL
1976 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1977 goto nondefault;
1980 if (! override)
1982 /* Add the default symbol if not performing a relocatable link. */
1983 if (! bfd_link_relocatable (info))
1985 bh = &hi->root;
1986 if (bh->type == bfd_link_hash_defined
1987 && bh->u.def.section->owner != NULL
1988 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1990 /* Mark the previous definition from IR object as
1991 undefined so that the generic linker will override
1992 it. */
1993 bh->type = bfd_link_hash_undefined;
1994 bh->u.undef.abfd = bh->u.def.section->owner;
1996 if (! (_bfd_generic_link_add_one_symbol
1997 (info, abfd, shortname, BSF_INDIRECT,
1998 bfd_ind_section_ptr,
1999 0, name, false, collect, &bh)))
2000 return false;
2001 hi = (struct elf_link_hash_entry *) bh;
2004 else
2006 /* In this case the symbol named SHORTNAME is overriding the
2007 indirect symbol we want to add. We were planning on making
2008 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2009 is the name without a version. NAME is the fully versioned
2010 name, and it is the default version.
2012 Overriding means that we already saw a definition for the
2013 symbol SHORTNAME in a regular object, and it is overriding
2014 the symbol defined in the dynamic object.
2016 When this happens, we actually want to change NAME, the
2017 symbol we just added, to refer to SHORTNAME. This will cause
2018 references to NAME in the shared object to become references
2019 to SHORTNAME in the regular object. This is what we expect
2020 when we override a function in a shared object: that the
2021 references in the shared object will be mapped to the
2022 definition in the regular object. */
2024 while (hi->root.type == bfd_link_hash_indirect
2025 || hi->root.type == bfd_link_hash_warning)
2026 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2028 h->root.type = bfd_link_hash_indirect;
2029 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2030 if (h->def_dynamic)
2032 h->def_dynamic = 0;
2033 hi->ref_dynamic = 1;
2034 if (hi->ref_regular
2035 || hi->def_regular)
2037 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2038 return false;
2042 /* Now set HI to H, so that the following code will set the
2043 other fields correctly. */
2044 hi = h;
2047 /* Check if HI is a warning symbol. */
2048 if (hi->root.type == bfd_link_hash_warning)
2049 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2051 /* If there is a duplicate definition somewhere, then HI may not
2052 point to an indirect symbol. We will have reported an error to
2053 the user in that case. */
2055 if (hi->root.type == bfd_link_hash_indirect)
2057 struct elf_link_hash_entry *ht;
2059 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2060 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2062 /* If we first saw a reference to SHORTNAME with non-default
2063 visibility, merge that visibility to the @@VER symbol. */
2064 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2066 /* A reference to the SHORTNAME symbol from a dynamic library
2067 will be satisfied by the versioned symbol at runtime. In
2068 effect, we have a reference to the versioned symbol. */
2069 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2070 hi->dynamic_def |= ht->dynamic_def;
2072 /* See if the new flags lead us to realize that the symbol must
2073 be dynamic. */
2074 if (! *dynsym)
2076 if (! dynamic)
2078 if (! bfd_link_executable (info)
2079 || hi->def_dynamic
2080 || hi->ref_dynamic)
2081 *dynsym = true;
2083 else
2085 if (hi->ref_regular)
2086 *dynsym = true;
2091 /* We also need to define an indirection from the nondefault version
2092 of the symbol. */
2094 nondefault:
2095 len = strlen (name);
2096 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2097 if (shortname == NULL)
2098 return false;
2099 memcpy (shortname, name, shortlen);
2100 memcpy (shortname + shortlen, p + 1, len - shortlen);
2102 /* Once again, merge with any existing symbol. */
2103 type_change_ok = false;
2104 size_change_ok = false;
2105 tmp_sec = sec;
2106 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2107 &hi, poldbfd, NULL, NULL, &skip, &override,
2108 &type_change_ok, &size_change_ok, &matched))
2109 return false;
2111 if (skip)
2113 if (!dynamic
2114 && h->root.type == bfd_link_hash_defweak
2115 && hi->root.type == bfd_link_hash_defined)
2117 /* We are handling a weak sym@@ver and attempting to define
2118 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119 new weak sym@ver because there is already a strong sym@ver.
2120 However, sym@ver and sym@@ver are really the same symbol.
2121 The existing strong sym@ver ought to override sym@@ver. */
2122 h->root.type = bfd_link_hash_defined;
2123 h->root.u.def.section = hi->root.u.def.section;
2124 h->root.u.def.value = hi->root.u.def.value;
2125 hi->root.type = bfd_link_hash_indirect;
2126 hi->root.u.i.link = &h->root;
2128 else
2129 return true;
2131 else if (override)
2133 /* Here SHORTNAME is a versioned name, so we don't expect to see
2134 the type of override we do in the case above unless it is
2135 overridden by a versioned definition. */
2136 if (hi->root.type != bfd_link_hash_defined
2137 && hi->root.type != bfd_link_hash_defweak)
2138 _bfd_error_handler
2139 /* xgettext:c-format */
2140 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2141 abfd, shortname);
2142 return true;
2144 else
2146 bh = &hi->root;
2147 if (! (_bfd_generic_link_add_one_symbol
2148 (info, abfd, shortname, BSF_INDIRECT,
2149 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2150 return false;
2151 hi = (struct elf_link_hash_entry *) bh;
2154 /* If there is a duplicate definition somewhere, then HI may not
2155 point to an indirect symbol. We will have reported an error
2156 to the user in that case. */
2157 if (hi->root.type == bfd_link_hash_indirect)
2159 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2160 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2161 hi->dynamic_def |= h->dynamic_def;
2163 /* If we first saw a reference to @VER symbol with
2164 non-default visibility, merge that visibility to the
2165 @@VER symbol. */
2166 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2168 /* See if the new flags lead us to realize that the symbol
2169 must be dynamic. */
2170 if (! *dynsym)
2172 if (! dynamic)
2174 if (! bfd_link_executable (info)
2175 || hi->ref_dynamic)
2176 *dynsym = true;
2178 else
2180 if (hi->ref_regular)
2181 *dynsym = true;
2186 return true;
2189 /* This routine is used to export all defined symbols into the dynamic
2190 symbol table. It is called via elf_link_hash_traverse. */
2192 static bool
2193 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2195 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2197 /* Ignore indirect symbols. These are added by the versioning code. */
2198 if (h->root.type == bfd_link_hash_indirect)
2199 return true;
2201 /* Ignore this if we won't export it. */
2202 if (!eif->info->export_dynamic && !h->dynamic)
2203 return true;
2205 if (h->dynindx == -1
2206 && (h->def_regular || h->ref_regular)
2207 && ! bfd_hide_sym_by_version (eif->info->version_info,
2208 h->root.root.string))
2210 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2212 eif->failed = true;
2213 return false;
2217 return true;
2220 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2222 .gnu.version_r section. */
2224 static bool
2225 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2227 bfd *glibc_bfd = NULL;
2228 Elf_Internal_Verneed *t;
2229 Elf_Internal_Vernaux *a;
2230 size_t amt;
2231 const char *relr = "GLIBC_ABI_DT_RELR";
2233 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2234 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2235 t != NULL;
2236 t = t->vn_nextref)
2238 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2239 /* Skip the shared library if it isn't libc.so. */
2240 if (!soname || !startswith (soname, "libc.so."))
2241 continue;
2243 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2245 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2246 added. */
2247 if (a->vna_nodename == relr
2248 || strcmp (a->vna_nodename, relr) == 0)
2249 return true;
2251 /* Check if libc.so provides GLIBC_2.XX version. */
2252 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2253 glibc_bfd = t->vn_bfd;
2256 break;
2259 /* Skip if it isn't linked against glibc. */
2260 if (glibc_bfd == NULL)
2261 return true;
2263 /* This is a new version. Add it to tree we are building. */
2264 if (t == NULL)
2266 amt = sizeof *t;
2267 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2268 amt);
2269 if (t == NULL)
2271 rinfo->failed = true;
2272 return false;
2275 t->vn_bfd = glibc_bfd;
2276 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2277 elf_tdata (rinfo->info->output_bfd)->verref = t;
2280 amt = sizeof *a;
2281 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2282 if (a == NULL)
2284 rinfo->failed = true;
2285 return false;
2288 a->vna_nodename = relr;
2289 a->vna_flags = 0;
2290 a->vna_nextptr = t->vn_auxptr;
2291 a->vna_other = rinfo->vers + 1;
2292 ++rinfo->vers;
2294 t->vn_auxptr = a;
2296 return true;
2299 /* Look through the symbols which are defined in other shared
2300 libraries and referenced here. Update the list of version
2301 dependencies. This will be put into the .gnu.version_r section.
2302 This function is called via elf_link_hash_traverse. */
2304 static bool
2305 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2306 void *data)
2308 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2309 Elf_Internal_Verneed *t;
2310 Elf_Internal_Vernaux *a;
2311 size_t amt;
2313 /* We only care about symbols defined in shared objects with version
2314 information. */
2315 if (!h->def_dynamic
2316 || h->def_regular
2317 || h->dynindx == -1
2318 || h->verinfo.verdef == NULL
2319 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2320 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2321 return true;
2323 /* See if we already know about this version. */
2324 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2325 t != NULL;
2326 t = t->vn_nextref)
2328 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2329 continue;
2331 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2332 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2333 return true;
2335 break;
2338 /* This is a new version. Add it to tree we are building. */
2340 if (t == NULL)
2342 amt = sizeof *t;
2343 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2344 if (t == NULL)
2346 rinfo->failed = true;
2347 return false;
2350 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2351 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2352 elf_tdata (rinfo->info->output_bfd)->verref = t;
2355 amt = sizeof *a;
2356 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2357 if (a == NULL)
2359 rinfo->failed = true;
2360 return false;
2363 /* Note that we are copying a string pointer here, and testing it
2364 above. If bfd_elf_string_from_elf_section is ever changed to
2365 discard the string data when low in memory, this will have to be
2366 fixed. */
2367 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2369 a->vna_flags = h->verinfo.verdef->vd_flags;
2370 a->vna_nextptr = t->vn_auxptr;
2372 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2373 ++rinfo->vers;
2375 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2377 t->vn_auxptr = a;
2379 return true;
2382 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383 hidden. Set *T_P to NULL if there is no match. */
2385 static bool
2386 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2387 struct elf_link_hash_entry *h,
2388 const char *version_p,
2389 struct bfd_elf_version_tree **t_p,
2390 bool *hide)
2392 struct bfd_elf_version_tree *t;
2394 /* Look for the version. If we find it, it is no longer weak. */
2395 for (t = info->version_info; t != NULL; t = t->next)
2397 if (strcmp (t->name, version_p) == 0)
2399 size_t len;
2400 char *alc;
2401 struct bfd_elf_version_expr *d;
2403 len = version_p - h->root.root.string;
2404 alc = (char *) bfd_malloc (len);
2405 if (alc == NULL)
2406 return false;
2407 memcpy (alc, h->root.root.string, len - 1);
2408 alc[len - 1] = '\0';
2409 if (alc[len - 2] == ELF_VER_CHR)
2410 alc[len - 2] = '\0';
2412 h->verinfo.vertree = t;
2413 t->used = true;
2414 d = NULL;
2416 if (t->globals.list != NULL)
2417 d = (*t->match) (&t->globals, NULL, alc);
2419 /* See if there is anything to force this symbol to
2420 local scope. */
2421 if (d == NULL && t->locals.list != NULL)
2423 d = (*t->match) (&t->locals, NULL, alc);
2424 if (d != NULL
2425 && h->dynindx != -1
2426 && ! info->export_dynamic)
2427 *hide = true;
2430 free (alc);
2431 break;
2435 *t_p = t;
2437 return true;
2440 /* Return TRUE if the symbol H is hidden by version script. */
2442 bool
2443 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2444 struct elf_link_hash_entry *h)
2446 const char *p;
2447 bool hide = false;
2448 const struct elf_backend_data *bed
2449 = get_elf_backend_data (info->output_bfd);
2451 /* Version script only hides symbols defined in regular objects. */
2452 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2453 return true;
2455 p = strchr (h->root.root.string, ELF_VER_CHR);
2456 if (p != NULL && h->verinfo.vertree == NULL)
2458 struct bfd_elf_version_tree *t;
2460 ++p;
2461 if (*p == ELF_VER_CHR)
2462 ++p;
2464 if (*p != '\0'
2465 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2466 && hide)
2468 if (hide)
2469 (*bed->elf_backend_hide_symbol) (info, h, true);
2470 return true;
2474 /* If we don't have a version for this symbol, see if we can find
2475 something. */
2476 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2478 h->verinfo.vertree
2479 = bfd_find_version_for_sym (info->version_info,
2480 h->root.root.string, &hide);
2481 if (h->verinfo.vertree != NULL && hide)
2483 (*bed->elf_backend_hide_symbol) (info, h, true);
2484 return true;
2488 return false;
2491 /* Figure out appropriate versions for all the symbols. We may not
2492 have the version number script until we have read all of the input
2493 files, so until that point we don't know which symbols should be
2494 local. This function is called via elf_link_hash_traverse. */
2496 static bool
2497 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2499 struct elf_info_failed *sinfo;
2500 struct bfd_link_info *info;
2501 const struct elf_backend_data *bed;
2502 struct elf_info_failed eif;
2503 char *p;
2504 bool hide;
2506 sinfo = (struct elf_info_failed *) data;
2507 info = sinfo->info;
2509 /* Fix the symbol flags. */
2510 eif.failed = false;
2511 eif.info = info;
2512 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2514 if (eif.failed)
2515 sinfo->failed = true;
2516 return false;
2519 bed = get_elf_backend_data (info->output_bfd);
2521 /* We only need version numbers for symbols defined in regular
2522 objects. */
2523 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2525 /* Hide symbols defined in discarded input sections. */
2526 if ((h->root.type == bfd_link_hash_defined
2527 || h->root.type == bfd_link_hash_defweak)
2528 && discarded_section (h->root.u.def.section))
2529 (*bed->elf_backend_hide_symbol) (info, h, true);
2530 return true;
2533 hide = false;
2534 p = strchr (h->root.root.string, ELF_VER_CHR);
2535 if (p != NULL && h->verinfo.vertree == NULL)
2537 struct bfd_elf_version_tree *t;
2539 ++p;
2540 if (*p == ELF_VER_CHR)
2541 ++p;
2543 /* If there is no version string, we can just return out. */
2544 if (*p == '\0')
2545 return true;
2547 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2549 sinfo->failed = true;
2550 return false;
2553 if (hide)
2554 (*bed->elf_backend_hide_symbol) (info, h, true);
2556 /* If we are building an application, we need to create a
2557 version node for this version. */
2558 if (t == NULL && bfd_link_executable (info))
2560 struct bfd_elf_version_tree **pp;
2561 int version_index;
2563 /* If we aren't going to export this symbol, we don't need
2564 to worry about it. */
2565 if (h->dynindx == -1)
2566 return true;
2568 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2569 sizeof *t);
2570 if (t == NULL)
2572 sinfo->failed = true;
2573 return false;
2576 t->name = p;
2577 t->name_indx = (unsigned int) -1;
2578 t->used = true;
2580 version_index = 1;
2581 /* Don't count anonymous version tag. */
2582 if (sinfo->info->version_info != NULL
2583 && sinfo->info->version_info->vernum == 0)
2584 version_index = 0;
2585 for (pp = &sinfo->info->version_info;
2586 *pp != NULL;
2587 pp = &(*pp)->next)
2588 ++version_index;
2589 t->vernum = version_index;
2591 *pp = t;
2593 h->verinfo.vertree = t;
2595 else if (t == NULL)
2597 /* We could not find the version for a symbol when
2598 generating a shared archive. Return an error. */
2599 _bfd_error_handler
2600 /* xgettext:c-format */
2601 (_("%pB: version node not found for symbol %s"),
2602 info->output_bfd, h->root.root.string);
2603 bfd_set_error (bfd_error_bad_value);
2604 sinfo->failed = true;
2605 return false;
2609 /* If we don't have a version for this symbol, see if we can find
2610 something. */
2611 if (!hide
2612 && h->verinfo.vertree == NULL
2613 && sinfo->info->version_info != NULL)
2615 h->verinfo.vertree
2616 = bfd_find_version_for_sym (sinfo->info->version_info,
2617 h->root.root.string, &hide);
2618 if (h->verinfo.vertree != NULL && hide)
2619 (*bed->elf_backend_hide_symbol) (info, h, true);
2622 return true;
2625 /* Read and swap the relocs from the section indicated by SHDR. This
2626 may be either a REL or a RELA section. The relocations are
2627 translated into RELA relocations and stored in INTERNAL_RELOCS,
2628 which should have already been allocated to contain enough space.
2629 The EXTERNAL_RELOCS are a buffer where the external form of the
2630 relocations should be stored.
2632 Returns FALSE if something goes wrong. */
2634 static bool
2635 elf_link_read_relocs_from_section (bfd *abfd,
2636 asection *sec,
2637 Elf_Internal_Shdr *shdr,
2638 void *external_relocs,
2639 Elf_Internal_Rela *internal_relocs)
2641 const struct elf_backend_data *bed;
2642 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2643 const bfd_byte *erela;
2644 const bfd_byte *erelaend;
2645 Elf_Internal_Rela *irela;
2646 Elf_Internal_Shdr *symtab_hdr;
2647 size_t nsyms;
2649 /* Position ourselves at the start of the section. */
2650 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2651 return false;
2653 /* Read the relocations. */
2654 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2655 return false;
2657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2658 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2660 bed = get_elf_backend_data (abfd);
2662 /* Convert the external relocations to the internal format. */
2663 if (shdr->sh_entsize == bed->s->sizeof_rel)
2664 swap_in = bed->s->swap_reloc_in;
2665 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2666 swap_in = bed->s->swap_reloca_in;
2667 else
2669 bfd_set_error (bfd_error_wrong_format);
2670 return false;
2673 erela = (const bfd_byte *) external_relocs;
2674 /* Setting erelaend like this and comparing with <= handles case of
2675 a fuzzed object with sh_size not a multiple of sh_entsize. */
2676 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2677 irela = internal_relocs;
2678 while (erela <= erelaend)
2680 bfd_vma r_symndx;
2682 (*swap_in) (abfd, erela, irela);
2683 r_symndx = ELF32_R_SYM (irela->r_info);
2684 if (bed->s->arch_size == 64)
2685 r_symndx >>= 24;
2686 if (nsyms > 0)
2688 if ((size_t) r_symndx >= nsyms)
2690 _bfd_error_handler
2691 /* xgettext:c-format */
2692 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2693 " for offset %#" PRIx64 " in section `%pA'"),
2694 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2695 (uint64_t) irela->r_offset, sec);
2696 bfd_set_error (bfd_error_bad_value);
2697 return false;
2700 else if (r_symndx != STN_UNDEF)
2702 _bfd_error_handler
2703 /* xgettext:c-format */
2704 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2705 " for offset %#" PRIx64 " in section `%pA'"
2706 " when the object file has no symbol table"),
2707 abfd, (uint64_t) r_symndx,
2708 (uint64_t) irela->r_offset, sec);
2709 bfd_set_error (bfd_error_bad_value);
2710 return false;
2712 irela += bed->s->int_rels_per_ext_rel;
2713 erela += shdr->sh_entsize;
2716 return true;
2719 /* Read and swap the relocs for a section O. They may have been
2720 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721 not NULL, they are used as buffers to read into. They are known to
2722 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2723 the return value is allocated using either malloc or bfd_alloc,
2724 according to the KEEP_MEMORY argument. If O has two relocation
2725 sections (both REL and RELA relocations), then the REL_HDR
2726 relocations will appear first in INTERNAL_RELOCS, followed by the
2727 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2728 update cache_size. */
2730 Elf_Internal_Rela *
2731 _bfd_elf_link_info_read_relocs (bfd *abfd,
2732 struct bfd_link_info *info,
2733 asection *o,
2734 void *external_relocs,
2735 Elf_Internal_Rela *internal_relocs,
2736 bool keep_memory)
2738 void *alloc1 = NULL;
2739 Elf_Internal_Rela *alloc2 = NULL;
2740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2741 struct bfd_elf_section_data *esdo = elf_section_data (o);
2742 Elf_Internal_Rela *internal_rela_relocs;
2744 if (esdo->relocs != NULL)
2745 return esdo->relocs;
2747 if (o->reloc_count == 0)
2748 return NULL;
2750 if (internal_relocs == NULL)
2752 bfd_size_type size;
2754 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2755 if (keep_memory)
2757 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2758 if (info)
2759 info->cache_size += size;
2761 else
2762 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2763 if (internal_relocs == NULL)
2764 goto error_return;
2767 if (external_relocs == NULL)
2769 bfd_size_type size = 0;
2771 if (esdo->rel.hdr)
2772 size += esdo->rel.hdr->sh_size;
2773 if (esdo->rela.hdr)
2774 size += esdo->rela.hdr->sh_size;
2776 alloc1 = bfd_malloc (size);
2777 if (alloc1 == NULL)
2778 goto error_return;
2779 external_relocs = alloc1;
2782 internal_rela_relocs = internal_relocs;
2783 if (esdo->rel.hdr)
2785 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2786 external_relocs,
2787 internal_relocs))
2788 goto error_return;
2789 external_relocs = (((bfd_byte *) external_relocs)
2790 + esdo->rel.hdr->sh_size);
2791 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2792 * bed->s->int_rels_per_ext_rel);
2795 if (esdo->rela.hdr
2796 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2797 external_relocs,
2798 internal_rela_relocs)))
2799 goto error_return;
2801 /* Cache the results for next time, if we can. */
2802 if (keep_memory)
2803 esdo->relocs = internal_relocs;
2805 free (alloc1);
2807 /* Don't free alloc2, since if it was allocated we are passing it
2808 back (under the name of internal_relocs). */
2810 return internal_relocs;
2812 error_return:
2813 free (alloc1);
2814 if (alloc2 != NULL)
2816 if (keep_memory)
2817 bfd_release (abfd, alloc2);
2818 else
2819 free (alloc2);
2821 return NULL;
2824 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826 struct bfd_link_info. */
2828 Elf_Internal_Rela *
2829 _bfd_elf_link_read_relocs (bfd *abfd,
2830 asection *o,
2831 void *external_relocs,
2832 Elf_Internal_Rela *internal_relocs,
2833 bool keep_memory)
2835 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2836 internal_relocs, keep_memory);
2840 /* Compute the size of, and allocate space for, REL_HDR which is the
2841 section header for a section containing relocations for O. */
2843 static bool
2844 _bfd_elf_link_size_reloc_section (bfd *abfd,
2845 struct bfd_elf_section_reloc_data *reldata)
2847 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2849 /* That allows us to calculate the size of the section. */
2850 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2852 /* The contents field must last into write_object_contents, so we
2853 allocate it with bfd_alloc rather than malloc. Also since we
2854 cannot be sure that the contents will actually be filled in,
2855 we zero the allocated space. */
2856 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2857 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2858 return false;
2860 if (reldata->hashes == NULL && reldata->count)
2862 struct elf_link_hash_entry **p;
2864 p = ((struct elf_link_hash_entry **)
2865 bfd_zmalloc (reldata->count * sizeof (*p)));
2866 if (p == NULL)
2867 return false;
2869 reldata->hashes = p;
2872 return true;
2875 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876 originated from the section given by INPUT_REL_HDR) to the
2877 OUTPUT_BFD. */
2879 bool
2880 _bfd_elf_link_output_relocs (bfd *output_bfd,
2881 asection *input_section,
2882 Elf_Internal_Shdr *input_rel_hdr,
2883 Elf_Internal_Rela *internal_relocs,
2884 struct elf_link_hash_entry **rel_hash
2885 ATTRIBUTE_UNUSED)
2887 Elf_Internal_Rela *irela;
2888 Elf_Internal_Rela *irelaend;
2889 bfd_byte *erel;
2890 struct bfd_elf_section_reloc_data *output_reldata;
2891 asection *output_section;
2892 const struct elf_backend_data *bed;
2893 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2894 struct bfd_elf_section_data *esdo;
2896 output_section = input_section->output_section;
2898 bed = get_elf_backend_data (output_bfd);
2899 esdo = elf_section_data (output_section);
2900 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2902 output_reldata = &esdo->rel;
2903 swap_out = bed->s->swap_reloc_out;
2905 else if (esdo->rela.hdr
2906 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2908 output_reldata = &esdo->rela;
2909 swap_out = bed->s->swap_reloca_out;
2911 else
2913 _bfd_error_handler
2914 /* xgettext:c-format */
2915 (_("%pB: relocation size mismatch in %pB section %pA"),
2916 output_bfd, input_section->owner, input_section);
2917 bfd_set_error (bfd_error_wrong_format);
2918 return false;
2921 erel = output_reldata->hdr->contents;
2922 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2923 irela = internal_relocs;
2924 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2925 * bed->s->int_rels_per_ext_rel);
2926 while (irela < irelaend)
2928 (*swap_out) (output_bfd, irela, erel);
2929 irela += bed->s->int_rels_per_ext_rel;
2930 erel += input_rel_hdr->sh_entsize;
2933 /* Bump the counter, so that we know where to add the next set of
2934 relocations. */
2935 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2937 return true;
2940 /* Make weak undefined symbols in PIE dynamic. */
2942 bool
2943 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2944 struct elf_link_hash_entry *h)
2946 if (bfd_link_pie (info)
2947 && h->dynindx == -1
2948 && h->root.type == bfd_link_hash_undefweak)
2949 return bfd_elf_link_record_dynamic_symbol (info, h);
2951 return true;
2954 /* Fix up the flags for a symbol. This handles various cases which
2955 can only be fixed after all the input files are seen. This is
2956 currently called by both adjust_dynamic_symbol and
2957 assign_sym_version, which is unnecessary but perhaps more robust in
2958 the face of future changes. */
2960 static bool
2961 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2962 struct elf_info_failed *eif)
2964 const struct elf_backend_data *bed;
2966 /* If this symbol was mentioned in a non-ELF file, try to set
2967 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2968 permit a non-ELF file to correctly refer to a symbol defined in
2969 an ELF dynamic object. */
2970 if (h->non_elf)
2972 while (h->root.type == bfd_link_hash_indirect)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2975 if (h->root.type != bfd_link_hash_defined
2976 && h->root.type != bfd_link_hash_defweak)
2978 h->ref_regular = 1;
2979 h->ref_regular_nonweak = 1;
2981 else
2983 if (h->root.u.def.section->owner != NULL
2984 && (bfd_get_flavour (h->root.u.def.section->owner)
2985 == bfd_target_elf_flavour))
2987 h->ref_regular = 1;
2988 h->ref_regular_nonweak = 1;
2990 else
2991 h->def_regular = 1;
2994 if (h->dynindx == -1
2995 && (h->def_dynamic
2996 || h->ref_dynamic))
2998 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3000 eif->failed = true;
3001 return false;
3005 else
3007 /* Unfortunately, NON_ELF is only correct if the symbol
3008 was first seen in a non-ELF file. Fortunately, if the symbol
3009 was first seen in an ELF file, we're probably OK unless the
3010 symbol was defined in a non-ELF file. Catch that case here.
3011 FIXME: We're still in trouble if the symbol was first seen in
3012 a dynamic object, and then later in a non-ELF regular object. */
3013 if ((h->root.type == bfd_link_hash_defined
3014 || h->root.type == bfd_link_hash_defweak)
3015 && !h->def_regular
3016 && (h->root.u.def.section->owner != NULL
3017 ? (bfd_get_flavour (h->root.u.def.section->owner)
3018 != bfd_target_elf_flavour)
3019 : (bfd_is_abs_section (h->root.u.def.section)
3020 && !h->def_dynamic)))
3021 h->def_regular = 1;
3024 /* Backend specific symbol fixup. */
3025 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3026 if (bed->elf_backend_fixup_symbol
3027 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3028 return false;
3030 /* If this is a final link, and the symbol was defined as a common
3031 symbol in a regular object file, and there was no definition in
3032 any dynamic object, then the linker will have allocated space for
3033 the symbol in a common section but the DEF_REGULAR
3034 flag will not have been set. */
3035 if (h->root.type == bfd_link_hash_defined
3036 && !h->def_regular
3037 && h->ref_regular
3038 && !h->def_dynamic
3039 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3040 h->def_regular = 1;
3042 /* Symbols defined in discarded sections shouldn't be dynamic. */
3043 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3046 /* If a weak undefined symbol has non-default visibility, we also
3047 hide it from the dynamic linker. */
3048 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3049 && h->root.type == bfd_link_hash_undefweak)
3050 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3052 /* A hidden versioned symbol in executable should be forced local if
3053 it is is locally defined, not referenced by shared library and not
3054 exported. */
3055 else if (bfd_link_executable (eif->info)
3056 && h->versioned == versioned_hidden
3057 && !eif->info->export_dynamic
3058 && !h->dynamic
3059 && !h->ref_dynamic
3060 && h->def_regular)
3061 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3063 /* If -Bsymbolic was used (which means to bind references to global
3064 symbols to the definition within the shared object), and this
3065 symbol was defined in a regular object, then it actually doesn't
3066 need a PLT entry. Likewise, if the symbol has non-default
3067 visibility. If the symbol has hidden or internal visibility, we
3068 will force it local. */
3069 else if (h->needs_plt
3070 && bfd_link_pic (eif->info)
3071 && is_elf_hash_table (eif->info->hash)
3072 && (SYMBOLIC_BIND (eif->info, h)
3073 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3074 && h->def_regular)
3076 bool force_local;
3078 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3079 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3080 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3083 /* If this is a weak defined symbol in a dynamic object, and we know
3084 the real definition in the dynamic object, copy interesting flags
3085 over to the real definition. */
3086 if (h->is_weakalias)
3088 struct elf_link_hash_entry *def = weakdef (h);
3090 /* If the real definition is defined by a regular object file,
3091 don't do anything special. See the longer description in
3092 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3093 bfd_link_hash_defined as it was when put on the alias list
3094 then it must have originally been a versioned symbol (for
3095 which a non-versioned indirect symbol is created) and later
3096 a definition for the non-versioned symbol is found. In that
3097 case the indirection is flipped with the versioned symbol
3098 becoming an indirect pointing at the non-versioned symbol.
3099 Thus, not an alias any more. */
3100 if (def->def_regular
3101 || def->root.type != bfd_link_hash_defined)
3103 h = def;
3104 while ((h = h->u.alias) != def)
3105 h->is_weakalias = 0;
3107 else
3109 while (h->root.type == bfd_link_hash_indirect)
3110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3111 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3112 || h->root.type == bfd_link_hash_defweak);
3113 BFD_ASSERT (def->def_dynamic);
3114 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3118 return true;
3121 /* Make the backend pick a good value for a dynamic symbol. This is
3122 called via elf_link_hash_traverse, and also calls itself
3123 recursively. */
3125 static bool
3126 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3128 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3129 struct elf_link_hash_table *htab;
3130 const struct elf_backend_data *bed;
3132 if (! is_elf_hash_table (eif->info->hash))
3133 return false;
3135 /* Ignore indirect symbols. These are added by the versioning code. */
3136 if (h->root.type == bfd_link_hash_indirect)
3137 return true;
3139 /* Fix the symbol flags. */
3140 if (! _bfd_elf_fix_symbol_flags (h, eif))
3141 return false;
3143 htab = elf_hash_table (eif->info);
3144 bed = get_elf_backend_data (htab->dynobj);
3146 if (h->root.type == bfd_link_hash_undefweak)
3148 if (eif->info->dynamic_undefined_weak == 0)
3149 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3150 else if (eif->info->dynamic_undefined_weak > 0
3151 && h->ref_regular
3152 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3153 && !bfd_hide_sym_by_version (eif->info->version_info,
3154 h->root.root.string))
3156 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3158 eif->failed = true;
3159 return false;
3164 /* If this symbol does not require a PLT entry, and it is not
3165 defined by a dynamic object, or is not referenced by a regular
3166 object, ignore it. We do have to handle a weak defined symbol,
3167 even if no regular object refers to it, if we decided to add it
3168 to the dynamic symbol table. FIXME: Do we normally need to worry
3169 about symbols which are defined by one dynamic object and
3170 referenced by another one? */
3171 if (!h->needs_plt
3172 && h->type != STT_GNU_IFUNC
3173 && (h->def_regular
3174 || !h->def_dynamic
3175 || (!h->ref_regular
3176 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3178 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3179 return true;
3182 /* If we've already adjusted this symbol, don't do it again. This
3183 can happen via a recursive call. */
3184 if (h->dynamic_adjusted)
3185 return true;
3187 /* Don't look at this symbol again. Note that we must set this
3188 after checking the above conditions, because we may look at a
3189 symbol once, decide not to do anything, and then get called
3190 recursively later after REF_REGULAR is set below. */
3191 h->dynamic_adjusted = 1;
3193 /* If this is a weak definition, and we know a real definition, and
3194 the real symbol is not itself defined by a regular object file,
3195 then get a good value for the real definition. We handle the
3196 real symbol first, for the convenience of the backend routine.
3198 Note that there is a confusing case here. If the real definition
3199 is defined by a regular object file, we don't get the real symbol
3200 from the dynamic object, but we do get the weak symbol. If the
3201 processor backend uses a COPY reloc, then if some routine in the
3202 dynamic object changes the real symbol, we will not see that
3203 change in the corresponding weak symbol. This is the way other
3204 ELF linkers work as well, and seems to be a result of the shared
3205 library model.
3207 I will clarify this issue. Most SVR4 shared libraries define the
3208 variable _timezone and define timezone as a weak synonym. The
3209 tzset call changes _timezone. If you write
3210 extern int timezone;
3211 int _timezone = 5;
3212 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3213 you might expect that, since timezone is a synonym for _timezone,
3214 the same number will print both times. However, if the processor
3215 backend uses a COPY reloc, then actually timezone will be copied
3216 into your process image, and, since you define _timezone
3217 yourself, _timezone will not. Thus timezone and _timezone will
3218 wind up at different memory locations. The tzset call will set
3219 _timezone, leaving timezone unchanged. */
3221 if (h->is_weakalias)
3223 struct elf_link_hash_entry *def = weakdef (h);
3225 /* If we get to this point, there is an implicit reference to
3226 the alias by a regular object file via the weak symbol H. */
3227 def->ref_regular = 1;
3229 /* Ensure that the backend adjust_dynamic_symbol function sees
3230 the strong alias before H by recursively calling ourselves. */
3231 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3232 return false;
3235 /* If a symbol has no type and no size and does not require a PLT
3236 entry, then we are probably about to do the wrong thing here: we
3237 are probably going to create a COPY reloc for an empty object.
3238 This case can arise when a shared object is built with assembly
3239 code, and the assembly code fails to set the symbol type. */
3240 if (h->size == 0
3241 && h->type == STT_NOTYPE
3242 && !h->needs_plt)
3243 _bfd_error_handler
3244 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3245 h->root.root.string);
3247 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3249 eif->failed = true;
3250 return false;
3253 return true;
3256 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3257 DYNBSS. */
3259 bool
3260 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3261 struct elf_link_hash_entry *h,
3262 asection *dynbss)
3264 unsigned int power_of_two;
3265 bfd_vma mask;
3266 asection *sec = h->root.u.def.section;
3268 /* The section alignment of the definition is the maximum alignment
3269 requirement of symbols defined in the section. Since we don't
3270 know the symbol alignment requirement, we start with the
3271 maximum alignment and check low bits of the symbol address
3272 for the minimum alignment. */
3273 power_of_two = bfd_section_alignment (sec);
3274 mask = ((bfd_vma) 1 << power_of_two) - 1;
3275 while ((h->root.u.def.value & mask) != 0)
3277 mask >>= 1;
3278 --power_of_two;
3281 if (power_of_two > bfd_section_alignment (dynbss))
3283 /* Adjust the section alignment if needed. */
3284 if (!bfd_set_section_alignment (dynbss, power_of_two))
3285 return false;
3288 /* We make sure that the symbol will be aligned properly. */
3289 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3291 /* Define the symbol as being at this point in DYNBSS. */
3292 h->root.u.def.section = dynbss;
3293 h->root.u.def.value = dynbss->size;
3295 /* Increment the size of DYNBSS to make room for the symbol. */
3296 dynbss->size += h->size;
3298 /* No error if extern_protected_data is true. */
3299 if (h->protected_def
3300 && (!info->extern_protected_data
3301 || (info->extern_protected_data < 0
3302 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3303 info->callbacks->einfo
3304 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3305 h->root.root.string);
3307 return true;
3310 /* Adjust all external symbols pointing into SEC_MERGE sections
3311 to reflect the object merging within the sections. */
3313 static bool
3314 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3316 asection *sec;
3318 if ((h->root.type == bfd_link_hash_defined
3319 || h->root.type == bfd_link_hash_defweak)
3320 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3321 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3323 bfd *output_bfd = (bfd *) data;
3325 h->root.u.def.value =
3326 _bfd_merged_section_offset (output_bfd,
3327 &h->root.u.def.section,
3328 elf_section_data (sec)->sec_info,
3329 h->root.u.def.value);
3332 return true;
3335 /* Returns false if the symbol referred to by H should be considered
3336 to resolve local to the current module, and true if it should be
3337 considered to bind dynamically. */
3339 bool
3340 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3341 struct bfd_link_info *info,
3342 bool not_local_protected)
3344 bool binding_stays_local_p;
3345 const struct elf_backend_data *bed;
3346 struct elf_link_hash_table *hash_table;
3348 if (h == NULL)
3349 return false;
3351 while (h->root.type == bfd_link_hash_indirect
3352 || h->root.type == bfd_link_hash_warning)
3353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3355 /* If it was forced local, then clearly it's not dynamic. */
3356 if (h->dynindx == -1)
3357 return false;
3358 if (h->forced_local)
3359 return false;
3361 /* Identify the cases where name binding rules say that a
3362 visible symbol resolves locally. */
3363 binding_stays_local_p = (bfd_link_executable (info)
3364 || SYMBOLIC_BIND (info, h));
3366 switch (ELF_ST_VISIBILITY (h->other))
3368 case STV_INTERNAL:
3369 case STV_HIDDEN:
3370 return false;
3372 case STV_PROTECTED:
3373 hash_table = elf_hash_table (info);
3374 if (!is_elf_hash_table (&hash_table->root))
3375 return false;
3377 bed = get_elf_backend_data (hash_table->dynobj);
3379 /* Proper resolution for function pointer equality may require
3380 that these symbols perhaps be resolved dynamically, even though
3381 we should be resolving them to the current module. */
3382 if (!not_local_protected || !bed->is_function_type (h->type))
3383 binding_stays_local_p = true;
3384 break;
3386 default:
3387 break;
3390 /* If it isn't defined locally, then clearly it's dynamic. */
3391 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3392 return true;
3394 /* Otherwise, the symbol is dynamic if binding rules don't tell
3395 us that it remains local. */
3396 return !binding_stays_local_p;
3399 /* Return true if the symbol referred to by H should be considered
3400 to resolve local to the current module, and false otherwise. Differs
3401 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3402 undefined symbols. The two functions are virtually identical except
3403 for the place where dynindx == -1 is tested. If that test is true,
3404 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3405 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3406 defined symbols.
3407 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3408 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3409 treatment of undefined weak symbols. For those that do not make
3410 undefined weak symbols dynamic, both functions may return false. */
3412 bool
3413 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3414 struct bfd_link_info *info,
3415 bool local_protected)
3417 const struct elf_backend_data *bed;
3418 struct elf_link_hash_table *hash_table;
3420 /* If it's a local sym, of course we resolve locally. */
3421 if (h == NULL)
3422 return true;
3424 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3425 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3426 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3427 return true;
3429 /* Forced local symbols resolve locally. */
3430 if (h->forced_local)
3431 return true;
3433 /* Common symbols that become definitions don't get the DEF_REGULAR
3434 flag set, so test it first, and don't bail out. */
3435 if (ELF_COMMON_DEF_P (h))
3436 /* Do nothing. */;
3437 /* If we don't have a definition in a regular file, then we can't
3438 resolve locally. The sym is either undefined or dynamic. */
3439 else if (!h->def_regular)
3440 return false;
3442 /* Non-dynamic symbols resolve locally. */
3443 if (h->dynindx == -1)
3444 return true;
3446 /* At this point, we know the symbol is defined and dynamic. In an
3447 executable it must resolve locally, likewise when building symbolic
3448 shared libraries. */
3449 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3450 return true;
3452 /* Now deal with defined dynamic symbols in shared libraries. Ones
3453 with default visibility might not resolve locally. */
3454 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3455 return false;
3457 hash_table = elf_hash_table (info);
3458 if (!is_elf_hash_table (&hash_table->root))
3459 return true;
3461 /* STV_PROTECTED symbols with indirect external access are local. */
3462 if (info->indirect_extern_access > 0)
3463 return true;
3465 bed = get_elf_backend_data (hash_table->dynobj);
3467 /* If extern_protected_data is false, STV_PROTECTED non-function
3468 symbols are local. */
3469 if ((!info->extern_protected_data
3470 || (info->extern_protected_data < 0
3471 && !bed->extern_protected_data))
3472 && !bed->is_function_type (h->type))
3473 return true;
3475 /* Function pointer equality tests may require that STV_PROTECTED
3476 symbols be treated as dynamic symbols. If the address of a
3477 function not defined in an executable is set to that function's
3478 plt entry in the executable, then the address of the function in
3479 a shared library must also be the plt entry in the executable. */
3480 return local_protected;
3483 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3484 aligned. Returns the first TLS output section. */
3486 struct bfd_section *
3487 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3489 struct bfd_section *sec, *tls;
3490 unsigned int align = 0;
3492 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3493 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3494 break;
3495 tls = sec;
3497 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3498 if (sec->alignment_power > align)
3499 align = sec->alignment_power;
3501 elf_hash_table (info)->tls_sec = tls;
3503 /* Ensure the alignment of the first section (usually .tdata) is the largest
3504 alignment, so that the tls segment starts aligned. */
3505 if (tls != NULL)
3506 tls->alignment_power = align;
3508 return tls;
3511 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3512 static bool
3513 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3514 Elf_Internal_Sym *sym)
3516 const struct elf_backend_data *bed;
3518 /* Local symbols do not count, but target specific ones might. */
3519 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3520 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3521 return false;
3523 bed = get_elf_backend_data (abfd);
3524 /* Function symbols do not count. */
3525 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3526 return false;
3528 /* If the section is undefined, then so is the symbol. */
3529 if (sym->st_shndx == SHN_UNDEF)
3530 return false;
3532 /* If the symbol is defined in the common section, then
3533 it is a common definition and so does not count. */
3534 if (bed->common_definition (sym))
3535 return false;
3537 /* If the symbol is in a target specific section then we
3538 must rely upon the backend to tell us what it is. */
3539 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3540 /* FIXME - this function is not coded yet:
3542 return _bfd_is_global_symbol_definition (abfd, sym);
3544 Instead for now assume that the definition is not global,
3545 Even if this is wrong, at least the linker will behave
3546 in the same way that it used to do. */
3547 return false;
3549 return true;
3552 /* Search the symbol table of the archive element of the archive ABFD
3553 whose archive map contains a mention of SYMDEF, and determine if
3554 the symbol is defined in this element. */
3555 static bool
3556 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3558 Elf_Internal_Shdr * hdr;
3559 size_t symcount;
3560 size_t extsymcount;
3561 size_t extsymoff;
3562 Elf_Internal_Sym *isymbuf;
3563 Elf_Internal_Sym *isym;
3564 Elf_Internal_Sym *isymend;
3565 bool result;
3567 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3568 if (abfd == NULL)
3569 return false;
3571 if (! bfd_check_format (abfd, bfd_object))
3572 return false;
3574 /* Select the appropriate symbol table. If we don't know if the
3575 object file is an IR object, give linker LTO plugin a chance to
3576 get the correct symbol table. */
3577 if (abfd->plugin_format == bfd_plugin_yes
3578 #if BFD_SUPPORTS_PLUGINS
3579 || (abfd->plugin_format == bfd_plugin_unknown
3580 && bfd_link_plugin_object_p (abfd))
3581 #endif
3584 /* Use the IR symbol table if the object has been claimed by
3585 plugin. */
3586 abfd = abfd->plugin_dummy_bfd;
3587 hdr = &elf_tdata (abfd)->symtab_hdr;
3589 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3590 hdr = &elf_tdata (abfd)->symtab_hdr;
3591 else
3592 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3594 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3596 /* The sh_info field of the symtab header tells us where the
3597 external symbols start. We don't care about the local symbols. */
3598 if (elf_bad_symtab (abfd))
3600 extsymcount = symcount;
3601 extsymoff = 0;
3603 else
3605 extsymcount = symcount - hdr->sh_info;
3606 extsymoff = hdr->sh_info;
3609 if (extsymcount == 0)
3610 return false;
3612 /* Read in the symbol table. */
3613 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3614 NULL, NULL, NULL);
3615 if (isymbuf == NULL)
3616 return false;
3618 /* Scan the symbol table looking for SYMDEF. */
3619 result = false;
3620 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3622 const char *name;
3624 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3625 isym->st_name);
3626 if (name == NULL)
3627 break;
3629 if (strcmp (name, symdef->name) == 0)
3631 result = is_global_data_symbol_definition (abfd, isym);
3632 break;
3636 free (isymbuf);
3638 return result;
3641 /* Add an entry to the .dynamic table. */
3643 bool
3644 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3645 bfd_vma tag,
3646 bfd_vma val)
3648 struct elf_link_hash_table *hash_table;
3649 const struct elf_backend_data *bed;
3650 asection *s;
3651 bfd_size_type newsize;
3652 bfd_byte *newcontents;
3653 Elf_Internal_Dyn dyn;
3655 hash_table = elf_hash_table (info);
3656 if (! is_elf_hash_table (&hash_table->root))
3657 return false;
3659 if (tag == DT_RELA || tag == DT_REL)
3660 hash_table->dynamic_relocs = true;
3662 bed = get_elf_backend_data (hash_table->dynobj);
3663 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3664 BFD_ASSERT (s != NULL);
3666 newsize = s->size + bed->s->sizeof_dyn;
3667 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3668 if (newcontents == NULL)
3669 return false;
3671 dyn.d_tag = tag;
3672 dyn.d_un.d_val = val;
3673 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3675 s->size = newsize;
3676 s->contents = newcontents;
3678 return true;
3681 /* Strip zero-sized dynamic sections. */
3683 bool
3684 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3686 struct elf_link_hash_table *hash_table;
3687 const struct elf_backend_data *bed;
3688 asection *s, *sdynamic, **pp;
3689 asection *rela_dyn, *rel_dyn;
3690 Elf_Internal_Dyn dyn;
3691 bfd_byte *extdyn, *next;
3692 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3693 bool strip_zero_sized;
3694 bool strip_zero_sized_plt;
3696 if (bfd_link_relocatable (info))
3697 return true;
3699 hash_table = elf_hash_table (info);
3700 if (!is_elf_hash_table (&hash_table->root))
3701 return false;
3703 if (!hash_table->dynobj)
3704 return true;
3706 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3707 if (!sdynamic)
3708 return true;
3710 bed = get_elf_backend_data (hash_table->dynobj);
3711 swap_dyn_in = bed->s->swap_dyn_in;
3713 strip_zero_sized = false;
3714 strip_zero_sized_plt = false;
3716 /* Strip zero-sized dynamic sections. */
3717 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3718 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3719 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3720 if (s->size == 0
3721 && (s == rela_dyn
3722 || s == rel_dyn
3723 || s == hash_table->srelplt->output_section
3724 || s == hash_table->splt->output_section))
3726 *pp = s->next;
3727 info->output_bfd->section_count--;
3728 strip_zero_sized = true;
3729 if (s == rela_dyn)
3730 s = rela_dyn;
3731 if (s == rel_dyn)
3732 s = rel_dyn;
3733 else if (s == hash_table->splt->output_section)
3735 s = hash_table->splt;
3736 strip_zero_sized_plt = true;
3738 else
3739 s = hash_table->srelplt;
3740 s->flags |= SEC_EXCLUDE;
3741 s->output_section = bfd_abs_section_ptr;
3743 else
3744 pp = &s->next;
3746 if (strip_zero_sized_plt && sdynamic->size != 0)
3747 for (extdyn = sdynamic->contents;
3748 extdyn < sdynamic->contents + sdynamic->size;
3749 extdyn = next)
3751 next = extdyn + bed->s->sizeof_dyn;
3752 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3753 switch (dyn.d_tag)
3755 default:
3756 break;
3757 case DT_JMPREL:
3758 case DT_PLTRELSZ:
3759 case DT_PLTREL:
3760 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3761 the procedure linkage table (the .plt section) has been
3762 removed. */
3763 memmove (extdyn, next,
3764 sdynamic->size - (next - sdynamic->contents));
3765 next = extdyn;
3769 if (strip_zero_sized)
3771 /* Regenerate program headers. */
3772 elf_seg_map (info->output_bfd) = NULL;
3773 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3774 NULL);
3777 return true;
3780 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3781 1 if a DT_NEEDED tag already exists, and 0 on success. */
3784 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3786 struct elf_link_hash_table *hash_table;
3787 size_t strindex;
3788 const char *soname;
3790 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3791 return -1;
3793 hash_table = elf_hash_table (info);
3794 soname = elf_dt_name (abfd);
3795 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3796 if (strindex == (size_t) -1)
3797 return -1;
3799 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3801 asection *sdyn;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3805 bed = get_elf_backend_data (hash_table->dynobj);
3806 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3807 if (sdyn != NULL && sdyn->size != 0)
3808 for (extdyn = sdyn->contents;
3809 extdyn < sdyn->contents + sdyn->size;
3810 extdyn += bed->s->sizeof_dyn)
3812 Elf_Internal_Dyn dyn;
3814 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3815 if (dyn.d_tag == DT_NEEDED
3816 && dyn.d_un.d_val == strindex)
3818 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3819 return 1;
3824 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3825 return -1;
3827 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3828 return -1;
3830 return 0;
3833 /* Return true if SONAME is on the needed list between NEEDED and STOP
3834 (or the end of list if STOP is NULL), and needed by a library that
3835 will be loaded. */
3837 static bool
3838 on_needed_list (const char *soname,
3839 struct bfd_link_needed_list *needed,
3840 struct bfd_link_needed_list *stop)
3842 struct bfd_link_needed_list *look;
3843 for (look = needed; look != stop; look = look->next)
3844 if (strcmp (soname, look->name) == 0
3845 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3846 /* If needed by a library that itself is not directly
3847 needed, recursively check whether that library is
3848 indirectly needed. Since we add DT_NEEDED entries to
3849 the end of the list, library dependencies appear after
3850 the library. Therefore search prior to the current
3851 LOOK, preventing possible infinite recursion. */
3852 || on_needed_list (elf_dt_name (look->by), needed, look)))
3853 return true;
3855 return false;
3858 /* Sort symbol by value, section, size, and type. */
3859 static int
3860 elf_sort_symbol (const void *arg1, const void *arg2)
3862 const struct elf_link_hash_entry *h1;
3863 const struct elf_link_hash_entry *h2;
3864 bfd_signed_vma vdiff;
3865 int sdiff;
3866 const char *n1;
3867 const char *n2;
3869 h1 = *(const struct elf_link_hash_entry **) arg1;
3870 h2 = *(const struct elf_link_hash_entry **) arg2;
3871 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3872 if (vdiff != 0)
3873 return vdiff > 0 ? 1 : -1;
3875 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3876 if (sdiff != 0)
3877 return sdiff;
3879 /* Sort so that sized symbols are selected over zero size symbols. */
3880 vdiff = h1->size - h2->size;
3881 if (vdiff != 0)
3882 return vdiff > 0 ? 1 : -1;
3884 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3885 if (h1->type != h2->type)
3886 return h1->type - h2->type;
3888 /* If symbols are properly sized and typed, and multiple strong
3889 aliases are not defined in a shared library by the user we
3890 shouldn't get here. Unfortunately linker script symbols like
3891 __bss_start sometimes match a user symbol defined at the start of
3892 .bss without proper size and type. We'd like to preference the
3893 user symbol over reserved system symbols. Sort on leading
3894 underscores. */
3895 n1 = h1->root.root.string;
3896 n2 = h2->root.root.string;
3897 while (*n1 == *n2)
3899 if (*n1 == 0)
3900 break;
3901 ++n1;
3902 ++n2;
3904 if (*n1 == '_')
3905 return -1;
3906 if (*n2 == '_')
3907 return 1;
3909 /* Final sort on name selects user symbols like '_u' over reserved
3910 system symbols like '_Z' and also will avoid qsort instability. */
3911 return *n1 - *n2;
3914 /* This function is used to adjust offsets into .dynstr for
3915 dynamic symbols. This is called via elf_link_hash_traverse. */
3917 static bool
3918 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3920 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3922 if (h->dynindx != -1)
3923 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3924 return true;
3927 /* Assign string offsets in .dynstr, update all structures referencing
3928 them. */
3930 static bool
3931 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3933 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3934 struct elf_link_local_dynamic_entry *entry;
3935 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3936 bfd *dynobj = hash_table->dynobj;
3937 asection *sdyn;
3938 bfd_size_type size;
3939 const struct elf_backend_data *bed;
3940 bfd_byte *extdyn;
3942 _bfd_elf_strtab_finalize (dynstr);
3943 size = _bfd_elf_strtab_size (dynstr);
3945 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3947 if (info->callbacks->examine_strtab)
3948 info->callbacks->examine_strtab (dynstr);
3950 bed = get_elf_backend_data (dynobj);
3951 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3952 BFD_ASSERT (sdyn != NULL);
3954 /* Update all .dynamic entries referencing .dynstr strings. */
3955 for (extdyn = sdyn->contents;
3956 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3957 extdyn += bed->s->sizeof_dyn)
3959 Elf_Internal_Dyn dyn;
3961 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3962 switch (dyn.d_tag)
3964 case DT_STRSZ:
3965 dyn.d_un.d_val = size;
3966 break;
3967 case DT_NEEDED:
3968 case DT_SONAME:
3969 case DT_RPATH:
3970 case DT_RUNPATH:
3971 case DT_FILTER:
3972 case DT_AUXILIARY:
3973 case DT_AUDIT:
3974 case DT_DEPAUDIT:
3975 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3976 break;
3977 default:
3978 continue;
3980 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3983 /* Now update local dynamic symbols. */
3984 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3985 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3986 entry->isym.st_name);
3988 /* And the rest of dynamic symbols. */
3989 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3991 /* Adjust version definitions. */
3992 if (elf_tdata (output_bfd)->cverdefs)
3994 asection *s;
3995 bfd_byte *p;
3996 size_t i;
3997 Elf_Internal_Verdef def;
3998 Elf_Internal_Verdaux defaux;
4000 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4001 p = s->contents;
4004 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4005 &def);
4006 p += sizeof (Elf_External_Verdef);
4007 if (def.vd_aux != sizeof (Elf_External_Verdef))
4008 continue;
4009 for (i = 0; i < def.vd_cnt; ++i)
4011 _bfd_elf_swap_verdaux_in (output_bfd,
4012 (Elf_External_Verdaux *) p, &defaux);
4013 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4014 defaux.vda_name);
4015 _bfd_elf_swap_verdaux_out (output_bfd,
4016 &defaux, (Elf_External_Verdaux *) p);
4017 p += sizeof (Elf_External_Verdaux);
4020 while (def.vd_next);
4023 /* Adjust version references. */
4024 if (elf_tdata (output_bfd)->verref)
4026 asection *s;
4027 bfd_byte *p;
4028 size_t i;
4029 Elf_Internal_Verneed need;
4030 Elf_Internal_Vernaux needaux;
4032 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4033 p = s->contents;
4036 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4037 &need);
4038 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4039 _bfd_elf_swap_verneed_out (output_bfd, &need,
4040 (Elf_External_Verneed *) p);
4041 p += sizeof (Elf_External_Verneed);
4042 for (i = 0; i < need.vn_cnt; ++i)
4044 _bfd_elf_swap_vernaux_in (output_bfd,
4045 (Elf_External_Vernaux *) p, &needaux);
4046 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4047 needaux.vna_name);
4048 _bfd_elf_swap_vernaux_out (output_bfd,
4049 &needaux,
4050 (Elf_External_Vernaux *) p);
4051 p += sizeof (Elf_External_Vernaux);
4054 while (need.vn_next);
4057 return true;
4060 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4061 The default is to only match when the INPUT and OUTPUT are exactly
4062 the same target. */
4064 bool
4065 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4066 const bfd_target *output)
4068 return input == output;
4071 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4072 This version is used when different targets for the same architecture
4073 are virtually identical. */
4075 bool
4076 _bfd_elf_relocs_compatible (const bfd_target *input,
4077 const bfd_target *output)
4079 const struct elf_backend_data *obed, *ibed;
4081 if (input == output)
4082 return true;
4084 ibed = xvec_get_elf_backend_data (input);
4085 obed = xvec_get_elf_backend_data (output);
4087 if (ibed->arch != obed->arch)
4088 return false;
4090 /* If both backends are using this function, deem them compatible. */
4091 return ibed->relocs_compatible == obed->relocs_compatible;
4094 /* Make a special call to the linker "notice" function to tell it that
4095 we are about to handle an as-needed lib, or have finished
4096 processing the lib. */
4098 bool
4099 _bfd_elf_notice_as_needed (bfd *ibfd,
4100 struct bfd_link_info *info,
4101 enum notice_asneeded_action act)
4103 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4106 /* Call ACTION on each relocation in an ELF object file. */
4108 bool
4109 _bfd_elf_link_iterate_on_relocs
4110 (bfd *abfd, struct bfd_link_info *info,
4111 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4112 const Elf_Internal_Rela *))
4114 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4115 struct elf_link_hash_table *htab = elf_hash_table (info);
4117 /* If this object is the same format as the output object, and it is
4118 not a shared library, then let the backend look through the
4119 relocs.
4121 This is required to build global offset table entries and to
4122 arrange for dynamic relocs. It is not required for the
4123 particular common case of linking non PIC code, even when linking
4124 against shared libraries, but unfortunately there is no way of
4125 knowing whether an object file has been compiled PIC or not.
4126 Looking through the relocs is not particularly time consuming.
4127 The problem is that we must either (1) keep the relocs in memory,
4128 which causes the linker to require additional runtime memory or
4129 (2) read the relocs twice from the input file, which wastes time.
4130 This would be a good case for using mmap.
4132 I have no idea how to handle linking PIC code into a file of a
4133 different format. It probably can't be done. */
4134 if ((abfd->flags & DYNAMIC) == 0
4135 && is_elf_hash_table (&htab->root)
4136 && elf_object_id (abfd) == elf_hash_table_id (htab)
4137 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4139 asection *o;
4141 for (o = abfd->sections; o != NULL; o = o->next)
4143 Elf_Internal_Rela *internal_relocs;
4144 bool ok;
4146 /* Don't check relocations in excluded sections. Don't do
4147 anything special with non-loaded, non-alloced sections.
4148 In particular, any relocs in such sections should not
4149 affect GOT and PLT reference counting (ie. we don't
4150 allow them to create GOT or PLT entries), there's no
4151 possibility or desire to optimize TLS relocs, and
4152 there's not much point in propagating relocs to shared
4153 libs that the dynamic linker won't relocate. */
4154 if ((o->flags & SEC_ALLOC) == 0
4155 || (o->flags & SEC_RELOC) == 0
4156 || (o->flags & SEC_EXCLUDE) != 0
4157 || o->reloc_count == 0
4158 || ((info->strip == strip_all || info->strip == strip_debugger)
4159 && (o->flags & SEC_DEBUGGING) != 0)
4160 || bfd_is_abs_section (o->output_section))
4161 continue;
4163 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4164 o, NULL,
4165 NULL,
4166 _bfd_link_keep_memory (info));
4167 if (internal_relocs == NULL)
4168 return false;
4170 ok = action (abfd, info, o, internal_relocs);
4172 if (elf_section_data (o)->relocs != internal_relocs)
4173 free (internal_relocs);
4175 if (! ok)
4176 return false;
4180 return true;
4183 /* Check relocations in an ELF object file. This is called after
4184 all input files have been opened. */
4186 bool
4187 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4190 if (bed->check_relocs != NULL)
4191 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4192 bed->check_relocs);
4193 return true;
4196 /* Add symbols from an ELF object file to the linker hash table. */
4198 static bool
4199 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4201 Elf_Internal_Ehdr *ehdr;
4202 Elf_Internal_Shdr *hdr;
4203 size_t symcount;
4204 size_t extsymcount;
4205 size_t extsymoff;
4206 struct elf_link_hash_entry **sym_hash;
4207 bool dynamic;
4208 Elf_External_Versym *extversym = NULL;
4209 Elf_External_Versym *extversym_end = NULL;
4210 Elf_External_Versym *ever;
4211 struct elf_link_hash_entry *weaks;
4212 struct elf_link_hash_entry **nondeflt_vers = NULL;
4213 size_t nondeflt_vers_cnt = 0;
4214 Elf_Internal_Sym *isymbuf = NULL;
4215 Elf_Internal_Sym *isym;
4216 Elf_Internal_Sym *isymend;
4217 const struct elf_backend_data *bed;
4218 bool add_needed;
4219 struct elf_link_hash_table *htab;
4220 void *alloc_mark = NULL;
4221 struct bfd_hash_entry **old_table = NULL;
4222 unsigned int old_size = 0;
4223 unsigned int old_count = 0;
4224 void *old_tab = NULL;
4225 void *old_ent;
4226 struct bfd_link_hash_entry *old_undefs = NULL;
4227 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4228 void *old_strtab = NULL;
4229 size_t tabsize = 0;
4230 asection *s;
4231 bool just_syms;
4233 htab = elf_hash_table (info);
4234 bed = get_elf_backend_data (abfd);
4236 if ((abfd->flags & DYNAMIC) == 0)
4237 dynamic = false;
4238 else
4240 dynamic = true;
4242 /* You can't use -r against a dynamic object. Also, there's no
4243 hope of using a dynamic object which does not exactly match
4244 the format of the output file. */
4245 if (bfd_link_relocatable (info)
4246 || !is_elf_hash_table (&htab->root)
4247 || info->output_bfd->xvec != abfd->xvec)
4249 if (bfd_link_relocatable (info))
4250 bfd_set_error (bfd_error_invalid_operation);
4251 else
4252 bfd_set_error (bfd_error_wrong_format);
4253 goto error_return;
4257 ehdr = elf_elfheader (abfd);
4258 if (info->warn_alternate_em
4259 && bed->elf_machine_code != ehdr->e_machine
4260 && ((bed->elf_machine_alt1 != 0
4261 && ehdr->e_machine == bed->elf_machine_alt1)
4262 || (bed->elf_machine_alt2 != 0
4263 && ehdr->e_machine == bed->elf_machine_alt2)))
4264 _bfd_error_handler
4265 /* xgettext:c-format */
4266 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4267 ehdr->e_machine, abfd, bed->elf_machine_code);
4269 /* As a GNU extension, any input sections which are named
4270 .gnu.warning.SYMBOL are treated as warning symbols for the given
4271 symbol. This differs from .gnu.warning sections, which generate
4272 warnings when they are included in an output file. */
4273 /* PR 12761: Also generate this warning when building shared libraries. */
4274 for (s = abfd->sections; s != NULL; s = s->next)
4276 const char *name;
4278 name = bfd_section_name (s);
4279 if (startswith (name, ".gnu.warning."))
4281 char *msg;
4282 bfd_size_type sz;
4284 name += sizeof ".gnu.warning." - 1;
4286 /* If this is a shared object, then look up the symbol
4287 in the hash table. If it is there, and it is already
4288 been defined, then we will not be using the entry
4289 from this shared object, so we don't need to warn.
4290 FIXME: If we see the definition in a regular object
4291 later on, we will warn, but we shouldn't. The only
4292 fix is to keep track of what warnings we are supposed
4293 to emit, and then handle them all at the end of the
4294 link. */
4295 if (dynamic)
4297 struct elf_link_hash_entry *h;
4299 h = elf_link_hash_lookup (htab, name, false, false, true);
4301 /* FIXME: What about bfd_link_hash_common? */
4302 if (h != NULL
4303 && (h->root.type == bfd_link_hash_defined
4304 || h->root.type == bfd_link_hash_defweak))
4305 continue;
4308 sz = s->size;
4309 msg = (char *) bfd_alloc (abfd, sz + 1);
4310 if (msg == NULL)
4311 goto error_return;
4313 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4314 goto error_return;
4316 msg[sz] = '\0';
4318 if (! (_bfd_generic_link_add_one_symbol
4319 (info, abfd, name, BSF_WARNING, s, 0, msg,
4320 false, bed->collect, NULL)))
4321 goto error_return;
4323 if (bfd_link_executable (info))
4325 /* Clobber the section size so that the warning does
4326 not get copied into the output file. */
4327 s->size = 0;
4329 /* Also set SEC_EXCLUDE, so that symbols defined in
4330 the warning section don't get copied to the output. */
4331 s->flags |= SEC_EXCLUDE;
4336 just_syms = ((s = abfd->sections) != NULL
4337 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4339 add_needed = true;
4340 if (! dynamic)
4342 /* If we are creating a shared library, create all the dynamic
4343 sections immediately. We need to attach them to something,
4344 so we attach them to this BFD, provided it is the right
4345 format and is not from ld --just-symbols. Always create the
4346 dynamic sections for -E/--dynamic-list. FIXME: If there
4347 are no input BFD's of the same format as the output, we can't
4348 make a shared library. */
4349 if (!just_syms
4350 && (bfd_link_pic (info)
4351 || (!bfd_link_relocatable (info)
4352 && info->nointerp
4353 && (info->export_dynamic || info->dynamic)))
4354 && is_elf_hash_table (&htab->root)
4355 && info->output_bfd->xvec == abfd->xvec
4356 && !htab->dynamic_sections_created)
4358 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4359 goto error_return;
4362 else if (!is_elf_hash_table (&htab->root))
4363 goto error_return;
4364 else
4366 const char *soname = NULL;
4367 char *audit = NULL;
4368 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4369 const Elf_Internal_Phdr *phdr;
4370 struct elf_link_loaded_list *loaded_lib;
4372 /* ld --just-symbols and dynamic objects don't mix very well.
4373 ld shouldn't allow it. */
4374 if (just_syms)
4375 abort ();
4377 /* If this dynamic lib was specified on the command line with
4378 --as-needed in effect, then we don't want to add a DT_NEEDED
4379 tag unless the lib is actually used. Similary for libs brought
4380 in by another lib's DT_NEEDED. When --no-add-needed is used
4381 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4382 any dynamic library in DT_NEEDED tags in the dynamic lib at
4383 all. */
4384 add_needed = (elf_dyn_lib_class (abfd)
4385 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4386 | DYN_NO_NEEDED)) == 0;
4388 s = bfd_get_section_by_name (abfd, ".dynamic");
4389 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4391 bfd_byte *dynbuf;
4392 bfd_byte *extdyn;
4393 unsigned int elfsec;
4394 unsigned long shlink;
4396 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4398 error_free_dyn:
4399 free (dynbuf);
4400 goto error_return;
4403 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4404 if (elfsec == SHN_BAD)
4405 goto error_free_dyn;
4406 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4408 for (extdyn = dynbuf;
4409 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4410 extdyn += bed->s->sizeof_dyn)
4412 Elf_Internal_Dyn dyn;
4414 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4415 if (dyn.d_tag == DT_SONAME)
4417 unsigned int tagv = dyn.d_un.d_val;
4418 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4419 if (soname == NULL)
4420 goto error_free_dyn;
4422 if (dyn.d_tag == DT_NEEDED)
4424 struct bfd_link_needed_list *n, **pn;
4425 char *fnm, *anm;
4426 unsigned int tagv = dyn.d_un.d_val;
4427 size_t amt = sizeof (struct bfd_link_needed_list);
4429 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4430 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4431 if (n == NULL || fnm == NULL)
4432 goto error_free_dyn;
4433 amt = strlen (fnm) + 1;
4434 anm = (char *) bfd_alloc (abfd, amt);
4435 if (anm == NULL)
4436 goto error_free_dyn;
4437 memcpy (anm, fnm, amt);
4438 n->name = anm;
4439 n->by = abfd;
4440 n->next = NULL;
4441 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4443 *pn = n;
4445 if (dyn.d_tag == DT_RUNPATH)
4447 struct bfd_link_needed_list *n, **pn;
4448 char *fnm, *anm;
4449 unsigned int tagv = dyn.d_un.d_val;
4450 size_t amt = sizeof (struct bfd_link_needed_list);
4452 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4453 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4454 if (n == NULL || fnm == NULL)
4455 goto error_free_dyn;
4456 amt = strlen (fnm) + 1;
4457 anm = (char *) bfd_alloc (abfd, amt);
4458 if (anm == NULL)
4459 goto error_free_dyn;
4460 memcpy (anm, fnm, amt);
4461 n->name = anm;
4462 n->by = abfd;
4463 n->next = NULL;
4464 for (pn = & runpath;
4465 *pn != NULL;
4466 pn = &(*pn)->next)
4468 *pn = n;
4470 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4471 if (!runpath && dyn.d_tag == DT_RPATH)
4473 struct bfd_link_needed_list *n, **pn;
4474 char *fnm, *anm;
4475 unsigned int tagv = dyn.d_un.d_val;
4476 size_t amt = sizeof (struct bfd_link_needed_list);
4478 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4479 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4480 if (n == NULL || fnm == NULL)
4481 goto error_free_dyn;
4482 amt = strlen (fnm) + 1;
4483 anm = (char *) bfd_alloc (abfd, amt);
4484 if (anm == NULL)
4485 goto error_free_dyn;
4486 memcpy (anm, fnm, amt);
4487 n->name = anm;
4488 n->by = abfd;
4489 n->next = NULL;
4490 for (pn = & rpath;
4491 *pn != NULL;
4492 pn = &(*pn)->next)
4494 *pn = n;
4496 if (dyn.d_tag == DT_AUDIT)
4498 unsigned int tagv = dyn.d_un.d_val;
4499 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4501 if (dyn.d_tag == DT_FLAGS_1)
4502 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4505 free (dynbuf);
4508 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4509 frees all more recently bfd_alloc'd blocks as well. */
4510 if (runpath)
4511 rpath = runpath;
4513 if (rpath)
4515 struct bfd_link_needed_list **pn;
4516 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4518 *pn = rpath;
4521 /* If we have a PT_GNU_RELRO program header, mark as read-only
4522 all sections contained fully therein. This makes relro
4523 shared library sections appear as they will at run-time. */
4524 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4525 while (phdr-- > elf_tdata (abfd)->phdr)
4526 if (phdr->p_type == PT_GNU_RELRO)
4528 for (s = abfd->sections; s != NULL; s = s->next)
4530 unsigned int opb = bfd_octets_per_byte (abfd, s);
4532 if ((s->flags & SEC_ALLOC) != 0
4533 && s->vma * opb >= phdr->p_vaddr
4534 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4535 s->flags |= SEC_READONLY;
4537 break;
4540 /* We do not want to include any of the sections in a dynamic
4541 object in the output file. We hack by simply clobbering the
4542 list of sections in the BFD. This could be handled more
4543 cleanly by, say, a new section flag; the existing
4544 SEC_NEVER_LOAD flag is not the one we want, because that one
4545 still implies that the section takes up space in the output
4546 file. */
4547 bfd_section_list_clear (abfd);
4549 /* Find the name to use in a DT_NEEDED entry that refers to this
4550 object. If the object has a DT_SONAME entry, we use it.
4551 Otherwise, if the generic linker stuck something in
4552 elf_dt_name, we use that. Otherwise, we just use the file
4553 name. */
4554 if (soname == NULL || *soname == '\0')
4556 soname = elf_dt_name (abfd);
4557 if (soname == NULL || *soname == '\0')
4558 soname = bfd_get_filename (abfd);
4561 /* Save the SONAME because sometimes the linker emulation code
4562 will need to know it. */
4563 elf_dt_name (abfd) = soname;
4565 /* If we have already included this dynamic object in the
4566 link, just ignore it. There is no reason to include a
4567 particular dynamic object more than once. */
4568 for (loaded_lib = htab->dyn_loaded;
4569 loaded_lib != NULL;
4570 loaded_lib = loaded_lib->next)
4572 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4573 return true;
4576 /* Create dynamic sections for backends that require that be done
4577 before setup_gnu_properties. */
4578 if (add_needed
4579 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4580 return false;
4582 /* Save the DT_AUDIT entry for the linker emulation code. */
4583 elf_dt_audit (abfd) = audit;
4586 /* If this is a dynamic object, we always link against the .dynsym
4587 symbol table, not the .symtab symbol table. The dynamic linker
4588 will only see the .dynsym symbol table, so there is no reason to
4589 look at .symtab for a dynamic object. */
4591 if (! dynamic || elf_dynsymtab (abfd) == 0)
4592 hdr = &elf_tdata (abfd)->symtab_hdr;
4593 else
4594 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4596 symcount = hdr->sh_size / bed->s->sizeof_sym;
4598 /* The sh_info field of the symtab header tells us where the
4599 external symbols start. We don't care about the local symbols at
4600 this point. */
4601 if (elf_bad_symtab (abfd))
4603 extsymcount = symcount;
4604 extsymoff = 0;
4606 else
4608 extsymcount = symcount - hdr->sh_info;
4609 extsymoff = hdr->sh_info;
4612 sym_hash = elf_sym_hashes (abfd);
4613 if (extsymcount != 0)
4615 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4616 NULL, NULL, NULL);
4617 if (isymbuf == NULL)
4618 goto error_return;
4620 if (sym_hash == NULL)
4622 /* We store a pointer to the hash table entry for each
4623 external symbol. */
4624 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4625 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4626 if (sym_hash == NULL)
4627 goto error_free_sym;
4628 elf_sym_hashes (abfd) = sym_hash;
4632 if (dynamic)
4634 /* Read in any version definitions. */
4635 if (!_bfd_elf_slurp_version_tables (abfd,
4636 info->default_imported_symver))
4637 goto error_free_sym;
4639 /* Read in the symbol versions, but don't bother to convert them
4640 to internal format. */
4641 if (elf_dynversym (abfd) != 0)
4643 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4644 bfd_size_type amt = versymhdr->sh_size;
4646 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4647 goto error_free_sym;
4648 extversym = (Elf_External_Versym *)
4649 _bfd_malloc_and_read (abfd, amt, amt);
4650 if (extversym == NULL)
4651 goto error_free_sym;
4652 extversym_end = extversym + amt / sizeof (*extversym);
4656 /* If we are loading an as-needed shared lib, save the symbol table
4657 state before we start adding symbols. If the lib turns out
4658 to be unneeded, restore the state. */
4659 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4661 unsigned int i;
4662 size_t entsize;
4664 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4666 struct bfd_hash_entry *p;
4667 struct elf_link_hash_entry *h;
4669 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4671 h = (struct elf_link_hash_entry *) p;
4672 entsize += htab->root.table.entsize;
4673 if (h->root.type == bfd_link_hash_warning)
4675 entsize += htab->root.table.entsize;
4676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4678 if (h->root.type == bfd_link_hash_common)
4679 entsize += sizeof (*h->root.u.c.p);
4683 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4684 old_tab = bfd_malloc (tabsize + entsize);
4685 if (old_tab == NULL)
4686 goto error_free_vers;
4688 /* Remember the current objalloc pointer, so that all mem for
4689 symbols added can later be reclaimed. */
4690 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4691 if (alloc_mark == NULL)
4692 goto error_free_vers;
4694 /* Make a special call to the linker "notice" function to
4695 tell it that we are about to handle an as-needed lib. */
4696 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4697 goto error_free_vers;
4699 /* Clone the symbol table. Remember some pointers into the
4700 symbol table, and dynamic symbol count. */
4701 old_ent = (char *) old_tab + tabsize;
4702 memcpy (old_tab, htab->root.table.table, tabsize);
4703 old_undefs = htab->root.undefs;
4704 old_undefs_tail = htab->root.undefs_tail;
4705 old_table = htab->root.table.table;
4706 old_size = htab->root.table.size;
4707 old_count = htab->root.table.count;
4708 old_strtab = NULL;
4709 if (htab->dynstr != NULL)
4711 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4712 if (old_strtab == NULL)
4713 goto error_free_vers;
4716 for (i = 0; i < htab->root.table.size; i++)
4718 struct bfd_hash_entry *p;
4719 struct elf_link_hash_entry *h;
4721 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4723 h = (struct elf_link_hash_entry *) p;
4724 memcpy (old_ent, h, htab->root.table.entsize);
4725 old_ent = (char *) old_ent + htab->root.table.entsize;
4726 if (h->root.type == bfd_link_hash_warning)
4728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4729 memcpy (old_ent, h, htab->root.table.entsize);
4730 old_ent = (char *) old_ent + htab->root.table.entsize;
4732 if (h->root.type == bfd_link_hash_common)
4734 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4735 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4741 weaks = NULL;
4742 if (extversym == NULL)
4743 ever = NULL;
4744 else if (extversym + extsymoff < extversym_end)
4745 ever = extversym + extsymoff;
4746 else
4748 /* xgettext:c-format */
4749 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4750 abfd, (long) extsymoff,
4751 (long) (extversym_end - extversym) / sizeof (* extversym));
4752 bfd_set_error (bfd_error_bad_value);
4753 goto error_free_vers;
4756 if (!bfd_link_relocatable (info)
4757 && abfd->lto_slim_object)
4759 _bfd_error_handler
4760 (_("%pB: plugin needed to handle lto object"), abfd);
4763 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4764 isym < isymend;
4765 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4767 int bind;
4768 bfd_vma value;
4769 asection *sec, *new_sec;
4770 flagword flags;
4771 const char *name;
4772 struct elf_link_hash_entry *h;
4773 struct elf_link_hash_entry *hi;
4774 bool definition;
4775 bool size_change_ok;
4776 bool type_change_ok;
4777 bool new_weak;
4778 bool old_weak;
4779 bfd *override;
4780 bool common;
4781 bool discarded;
4782 unsigned int old_alignment;
4783 unsigned int shindex;
4784 bfd *old_bfd;
4785 bool matched;
4787 override = NULL;
4789 flags = BSF_NO_FLAGS;
4790 sec = NULL;
4791 value = isym->st_value;
4792 common = bed->common_definition (isym);
4793 if (common && info->inhibit_common_definition)
4795 /* Treat common symbol as undefined for --no-define-common. */
4796 isym->st_shndx = SHN_UNDEF;
4797 common = false;
4799 discarded = false;
4801 bind = ELF_ST_BIND (isym->st_info);
4802 switch (bind)
4804 case STB_LOCAL:
4805 /* This should be impossible, since ELF requires that all
4806 global symbols follow all local symbols, and that sh_info
4807 point to the first global symbol. Unfortunately, Irix 5
4808 screws this up. */
4809 if (elf_bad_symtab (abfd))
4810 continue;
4812 /* If we aren't prepared to handle locals within the globals
4813 then we'll likely segfault on a NULL symbol hash if the
4814 symbol is ever referenced in relocations. */
4815 shindex = elf_elfheader (abfd)->e_shstrndx;
4816 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4817 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4818 " (>= sh_info of %lu)"),
4819 abfd, name, (long) (isym - isymbuf + extsymoff),
4820 (long) extsymoff);
4822 /* Dynamic object relocations are not processed by ld, so
4823 ld won't run into the problem mentioned above. */
4824 if (dynamic)
4825 continue;
4826 bfd_set_error (bfd_error_bad_value);
4827 goto error_free_vers;
4829 case STB_GLOBAL:
4830 if (isym->st_shndx != SHN_UNDEF && !common)
4831 flags = BSF_GLOBAL;
4832 break;
4834 case STB_WEAK:
4835 flags = BSF_WEAK;
4836 break;
4838 case STB_GNU_UNIQUE:
4839 flags = BSF_GNU_UNIQUE;
4840 break;
4842 default:
4843 /* Leave it up to the processor backend. */
4844 break;
4847 if (isym->st_shndx == SHN_UNDEF)
4848 sec = bfd_und_section_ptr;
4849 else if (isym->st_shndx == SHN_ABS)
4850 sec = bfd_abs_section_ptr;
4851 else if (isym->st_shndx == SHN_COMMON)
4853 sec = bfd_com_section_ptr;
4854 /* What ELF calls the size we call the value. What ELF
4855 calls the value we call the alignment. */
4856 value = isym->st_size;
4858 else
4860 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4861 if (sec == NULL)
4862 sec = bfd_abs_section_ptr;
4863 else if (discarded_section (sec))
4865 /* Symbols from discarded section are undefined. We keep
4866 its visibility. */
4867 sec = bfd_und_section_ptr;
4868 discarded = true;
4869 isym->st_shndx = SHN_UNDEF;
4871 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4872 value -= sec->vma;
4875 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4876 isym->st_name);
4877 if (name == NULL)
4878 goto error_free_vers;
4880 if (isym->st_shndx == SHN_COMMON
4881 && (abfd->flags & BFD_PLUGIN) != 0)
4883 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4885 if (xc == NULL)
4887 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4888 | SEC_EXCLUDE);
4889 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4890 if (xc == NULL)
4891 goto error_free_vers;
4893 sec = xc;
4895 else if (isym->st_shndx == SHN_COMMON
4896 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4897 && !bfd_link_relocatable (info))
4899 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4901 if (tcomm == NULL)
4903 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4904 | SEC_LINKER_CREATED);
4905 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4906 if (tcomm == NULL)
4907 goto error_free_vers;
4909 sec = tcomm;
4911 else if (bed->elf_add_symbol_hook)
4913 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4914 &sec, &value))
4915 goto error_free_vers;
4917 /* The hook function sets the name to NULL if this symbol
4918 should be skipped for some reason. */
4919 if (name == NULL)
4920 continue;
4923 /* Sanity check that all possibilities were handled. */
4924 if (sec == NULL)
4925 abort ();
4927 /* Silently discard TLS symbols from --just-syms. There's
4928 no way to combine a static TLS block with a new TLS block
4929 for this executable. */
4930 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4931 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4932 continue;
4934 if (bfd_is_und_section (sec)
4935 || bfd_is_com_section (sec))
4936 definition = false;
4937 else
4938 definition = true;
4940 size_change_ok = false;
4941 type_change_ok = bed->type_change_ok;
4942 old_weak = false;
4943 matched = false;
4944 old_alignment = 0;
4945 old_bfd = NULL;
4946 new_sec = sec;
4948 if (is_elf_hash_table (&htab->root))
4950 Elf_Internal_Versym iver;
4951 unsigned int vernum = 0;
4952 bool skip;
4954 if (ever == NULL)
4956 if (info->default_imported_symver)
4957 /* Use the default symbol version created earlier. */
4958 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4959 else
4960 iver.vs_vers = 0;
4962 else if (ever >= extversym_end)
4964 /* xgettext:c-format */
4965 _bfd_error_handler (_("%pB: not enough version information"),
4966 abfd);
4967 bfd_set_error (bfd_error_bad_value);
4968 goto error_free_vers;
4970 else
4971 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4973 vernum = iver.vs_vers & VERSYM_VERSION;
4975 /* If this is a hidden symbol, or if it is not version
4976 1, we append the version name to the symbol name.
4977 However, we do not modify a non-hidden absolute symbol
4978 if it is not a function, because it might be the version
4979 symbol itself. FIXME: What if it isn't? */
4980 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4981 || (vernum > 1
4982 && (!bfd_is_abs_section (sec)
4983 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4985 const char *verstr;
4986 size_t namelen, verlen, newlen;
4987 char *newname, *p;
4989 if (isym->st_shndx != SHN_UNDEF)
4991 if (vernum > elf_tdata (abfd)->cverdefs)
4992 verstr = NULL;
4993 else if (vernum > 1)
4994 verstr =
4995 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4996 else
4997 verstr = "";
4999 if (verstr == NULL)
5001 _bfd_error_handler
5002 /* xgettext:c-format */
5003 (_("%pB: %s: invalid version %u (max %d)"),
5004 abfd, name, vernum,
5005 elf_tdata (abfd)->cverdefs);
5006 bfd_set_error (bfd_error_bad_value);
5007 goto error_free_vers;
5010 else
5012 /* We cannot simply test for the number of
5013 entries in the VERNEED section since the
5014 numbers for the needed versions do not start
5015 at 0. */
5016 Elf_Internal_Verneed *t;
5018 verstr = NULL;
5019 for (t = elf_tdata (abfd)->verref;
5020 t != NULL;
5021 t = t->vn_nextref)
5023 Elf_Internal_Vernaux *a;
5025 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5027 if (a->vna_other == vernum)
5029 verstr = a->vna_nodename;
5030 break;
5033 if (a != NULL)
5034 break;
5036 if (verstr == NULL)
5038 _bfd_error_handler
5039 /* xgettext:c-format */
5040 (_("%pB: %s: invalid needed version %d"),
5041 abfd, name, vernum);
5042 bfd_set_error (bfd_error_bad_value);
5043 goto error_free_vers;
5047 namelen = strlen (name);
5048 verlen = strlen (verstr);
5049 newlen = namelen + verlen + 2;
5050 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5051 && isym->st_shndx != SHN_UNDEF)
5052 ++newlen;
5054 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5055 if (newname == NULL)
5056 goto error_free_vers;
5057 memcpy (newname, name, namelen);
5058 p = newname + namelen;
5059 *p++ = ELF_VER_CHR;
5060 /* If this is a defined non-hidden version symbol,
5061 we add another @ to the name. This indicates the
5062 default version of the symbol. */
5063 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5064 && isym->st_shndx != SHN_UNDEF)
5065 *p++ = ELF_VER_CHR;
5066 memcpy (p, verstr, verlen + 1);
5068 name = newname;
5071 /* If this symbol has default visibility and the user has
5072 requested we not re-export it, then mark it as hidden. */
5073 if (!bfd_is_und_section (sec)
5074 && !dynamic
5075 && abfd->no_export
5076 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5077 isym->st_other = (STV_HIDDEN
5078 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5080 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5081 sym_hash, &old_bfd, &old_weak,
5082 &old_alignment, &skip, &override,
5083 &type_change_ok, &size_change_ok,
5084 &matched))
5085 goto error_free_vers;
5087 if (skip)
5088 continue;
5090 /* Override a definition only if the new symbol matches the
5091 existing one. */
5092 if (override && matched)
5093 definition = false;
5095 h = *sym_hash;
5096 while (h->root.type == bfd_link_hash_indirect
5097 || h->root.type == bfd_link_hash_warning)
5098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5100 if (h->versioned != unversioned
5101 && elf_tdata (abfd)->verdef != NULL
5102 && vernum > 1
5103 && definition)
5104 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5107 if (! (_bfd_generic_link_add_one_symbol
5108 (info, override ? override : abfd, name, flags, sec, value,
5109 NULL, false, bed->collect,
5110 (struct bfd_link_hash_entry **) sym_hash)))
5111 goto error_free_vers;
5113 h = *sym_hash;
5114 /* We need to make sure that indirect symbol dynamic flags are
5115 updated. */
5116 hi = h;
5117 while (h->root.type == bfd_link_hash_indirect
5118 || h->root.type == bfd_link_hash_warning)
5119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5121 *sym_hash = h;
5123 /* Setting the index to -3 tells elf_link_output_extsym that
5124 this symbol is defined in a discarded section. */
5125 if (discarded && is_elf_hash_table (&htab->root))
5126 h->indx = -3;
5128 new_weak = (flags & BSF_WEAK) != 0;
5129 if (dynamic
5130 && definition
5131 && new_weak
5132 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5133 && is_elf_hash_table (&htab->root)
5134 && h->u.alias == NULL)
5136 /* Keep a list of all weak defined non function symbols from
5137 a dynamic object, using the alias field. Later in this
5138 function we will set the alias field to the correct
5139 value. We only put non-function symbols from dynamic
5140 objects on this list, because that happens to be the only
5141 time we need to know the normal symbol corresponding to a
5142 weak symbol, and the information is time consuming to
5143 figure out. If the alias field is not already NULL,
5144 then this symbol was already defined by some previous
5145 dynamic object, and we will be using that previous
5146 definition anyhow. */
5148 h->u.alias = weaks;
5149 weaks = h;
5152 /* Set the alignment of a common symbol. */
5153 if ((common || bfd_is_com_section (sec))
5154 && h->root.type == bfd_link_hash_common)
5156 unsigned int align;
5158 if (common)
5159 align = bfd_log2 (isym->st_value);
5160 else
5162 /* The new symbol is a common symbol in a shared object.
5163 We need to get the alignment from the section. */
5164 align = new_sec->alignment_power;
5166 if (align > old_alignment)
5167 h->root.u.c.p->alignment_power = align;
5168 else
5169 h->root.u.c.p->alignment_power = old_alignment;
5172 if (is_elf_hash_table (&htab->root))
5174 /* Set a flag in the hash table entry indicating the type of
5175 reference or definition we just found. A dynamic symbol
5176 is one which is referenced or defined by both a regular
5177 object and a shared object. */
5178 bool dynsym = false;
5180 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5181 if ((abfd->flags & BFD_PLUGIN) != 0)
5183 /* Except for this flag to track nonweak references. */
5184 if (!definition
5185 && bind != STB_WEAK)
5186 h->ref_ir_nonweak = 1;
5188 else if (!dynamic)
5190 if (! definition)
5192 h->ref_regular = 1;
5193 if (bind != STB_WEAK)
5194 h->ref_regular_nonweak = 1;
5196 else
5198 h->def_regular = 1;
5199 if (h->def_dynamic)
5201 h->def_dynamic = 0;
5202 h->ref_dynamic = 1;
5206 else
5208 if (! definition)
5210 h->ref_dynamic = 1;
5211 hi->ref_dynamic = 1;
5213 else
5215 h->def_dynamic = 1;
5216 hi->def_dynamic = 1;
5220 /* If an indirect symbol has been forced local, don't
5221 make the real symbol dynamic. */
5222 if (h != hi && hi->forced_local)
5224 else if (!dynamic)
5226 if (bfd_link_dll (info)
5227 || h->def_dynamic
5228 || h->ref_dynamic)
5229 dynsym = true;
5231 else
5233 if (h->def_regular
5234 || h->ref_regular
5235 || (h->is_weakalias
5236 && weakdef (h)->dynindx != -1))
5237 dynsym = true;
5240 /* Check to see if we need to add an indirect symbol for
5241 the default name. */
5242 if ((definition
5243 || (!override && h->root.type == bfd_link_hash_common))
5244 && !(hi != h
5245 && hi->versioned == versioned_hidden))
5246 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5247 sec, value, &old_bfd, &dynsym))
5248 goto error_free_vers;
5250 /* Check the alignment when a common symbol is involved. This
5251 can change when a common symbol is overridden by a normal
5252 definition or a common symbol is ignored due to the old
5253 normal definition. We need to make sure the maximum
5254 alignment is maintained. */
5255 if ((old_alignment || common)
5256 && h->root.type != bfd_link_hash_common)
5258 unsigned int common_align;
5259 unsigned int normal_align;
5260 unsigned int symbol_align;
5261 bfd *normal_bfd;
5262 bfd *common_bfd;
5264 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5265 || h->root.type == bfd_link_hash_defweak);
5267 symbol_align = ffs (h->root.u.def.value) - 1;
5268 if (h->root.u.def.section->owner != NULL
5269 && (h->root.u.def.section->owner->flags
5270 & (DYNAMIC | BFD_PLUGIN)) == 0)
5272 normal_align = h->root.u.def.section->alignment_power;
5273 if (normal_align > symbol_align)
5274 normal_align = symbol_align;
5276 else
5277 normal_align = symbol_align;
5279 if (old_alignment)
5281 common_align = old_alignment;
5282 common_bfd = old_bfd;
5283 normal_bfd = abfd;
5285 else
5287 common_align = bfd_log2 (isym->st_value);
5288 common_bfd = abfd;
5289 normal_bfd = old_bfd;
5292 if (normal_align < common_align)
5294 /* PR binutils/2735 */
5295 if (normal_bfd == NULL)
5296 _bfd_error_handler
5297 /* xgettext:c-format */
5298 (_("warning: alignment %u of common symbol `%s' in %pB is"
5299 " greater than the alignment (%u) of its section %pA"),
5300 1 << common_align, name, common_bfd,
5301 1 << normal_align, h->root.u.def.section);
5302 else
5303 _bfd_error_handler
5304 /* xgettext:c-format */
5305 (_("warning: alignment %u of symbol `%s' in %pB"
5306 " is smaller than %u in %pB"),
5307 1 << normal_align, name, normal_bfd,
5308 1 << common_align, common_bfd);
5312 /* Remember the symbol size if it isn't undefined. */
5313 if (isym->st_size != 0
5314 && isym->st_shndx != SHN_UNDEF
5315 && (definition || h->size == 0))
5317 if (h->size != 0
5318 && h->size != isym->st_size
5319 && ! size_change_ok)
5320 _bfd_error_handler
5321 /* xgettext:c-format */
5322 (_("warning: size of symbol `%s' changed"
5323 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5324 name, (uint64_t) h->size, old_bfd,
5325 (uint64_t) isym->st_size, abfd);
5327 h->size = isym->st_size;
5330 /* If this is a common symbol, then we always want H->SIZE
5331 to be the size of the common symbol. The code just above
5332 won't fix the size if a common symbol becomes larger. We
5333 don't warn about a size change here, because that is
5334 covered by --warn-common. Allow changes between different
5335 function types. */
5336 if (h->root.type == bfd_link_hash_common)
5337 h->size = h->root.u.c.size;
5339 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5340 && ((definition && !new_weak)
5341 || (old_weak && h->root.type == bfd_link_hash_common)
5342 || h->type == STT_NOTYPE))
5344 unsigned int type = ELF_ST_TYPE (isym->st_info);
5346 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5347 symbol. */
5348 if (type == STT_GNU_IFUNC
5349 && (abfd->flags & DYNAMIC) != 0)
5350 type = STT_FUNC;
5352 if (h->type != type)
5354 if (h->type != STT_NOTYPE && ! type_change_ok)
5355 /* xgettext:c-format */
5356 _bfd_error_handler
5357 (_("warning: type of symbol `%s' changed"
5358 " from %d to %d in %pB"),
5359 name, h->type, type, abfd);
5361 h->type = type;
5365 /* Merge st_other field. */
5366 elf_merge_st_other (abfd, h, isym->st_other, sec,
5367 definition, dynamic);
5369 /* We don't want to make debug symbol dynamic. */
5370 if (definition
5371 && (sec->flags & SEC_DEBUGGING)
5372 && !bfd_link_relocatable (info))
5373 dynsym = false;
5375 /* Nor should we make plugin symbols dynamic. */
5376 if ((abfd->flags & BFD_PLUGIN) != 0)
5377 dynsym = false;
5379 if (definition)
5381 h->target_internal = isym->st_target_internal;
5382 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5385 if (definition && !dynamic)
5387 char *p = strchr (name, ELF_VER_CHR);
5388 if (p != NULL && p[1] != ELF_VER_CHR)
5390 /* Queue non-default versions so that .symver x, x@FOO
5391 aliases can be checked. */
5392 if (!nondeflt_vers)
5394 size_t amt = ((isymend - isym + 1)
5395 * sizeof (struct elf_link_hash_entry *));
5396 nondeflt_vers
5397 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5398 if (!nondeflt_vers)
5399 goto error_free_vers;
5401 nondeflt_vers[nondeflt_vers_cnt++] = h;
5405 if (dynsym && h->dynindx == -1)
5407 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5408 goto error_free_vers;
5409 if (h->is_weakalias
5410 && weakdef (h)->dynindx == -1)
5412 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5413 goto error_free_vers;
5416 else if (h->dynindx != -1)
5417 /* If the symbol already has a dynamic index, but
5418 visibility says it should not be visible, turn it into
5419 a local symbol. */
5420 switch (ELF_ST_VISIBILITY (h->other))
5422 case STV_INTERNAL:
5423 case STV_HIDDEN:
5424 (*bed->elf_backend_hide_symbol) (info, h, true);
5425 dynsym = false;
5426 break;
5429 if (!add_needed
5430 && matched
5431 && definition
5432 && h->root.type != bfd_link_hash_indirect
5433 && ((dynsym
5434 && h->ref_regular_nonweak)
5435 || (old_bfd != NULL
5436 && (old_bfd->flags & BFD_PLUGIN) != 0
5437 && h->ref_ir_nonweak
5438 && !info->lto_all_symbols_read)
5439 || (h->ref_dynamic_nonweak
5440 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5441 && !on_needed_list (elf_dt_name (abfd),
5442 htab->needed, NULL))))
5444 const char *soname = elf_dt_name (abfd);
5446 info->callbacks->minfo ("%!", soname, old_bfd,
5447 h->root.root.string);
5449 /* A symbol from a library loaded via DT_NEEDED of some
5450 other library is referenced by a regular object.
5451 Add a DT_NEEDED entry for it. Issue an error if
5452 --no-add-needed is used and the reference was not
5453 a weak one. */
5454 if (old_bfd != NULL
5455 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5457 _bfd_error_handler
5458 /* xgettext:c-format */
5459 (_("%pB: undefined reference to symbol '%s'"),
5460 old_bfd, name);
5461 bfd_set_error (bfd_error_missing_dso);
5462 goto error_free_vers;
5465 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5466 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5468 /* Create dynamic sections for backends that require
5469 that be done before setup_gnu_properties. */
5470 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5471 return false;
5472 add_needed = true;
5477 if (info->lto_plugin_active
5478 && !bfd_link_relocatable (info)
5479 && (abfd->flags & BFD_PLUGIN) == 0
5480 && !just_syms
5481 && extsymcount)
5483 int r_sym_shift;
5485 if (bed->s->arch_size == 32)
5486 r_sym_shift = 8;
5487 else
5488 r_sym_shift = 32;
5490 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5491 referenced in regular objects so that linker plugin will get
5492 the correct symbol resolution. */
5494 sym_hash = elf_sym_hashes (abfd);
5495 for (s = abfd->sections; s != NULL; s = s->next)
5497 Elf_Internal_Rela *internal_relocs;
5498 Elf_Internal_Rela *rel, *relend;
5500 /* Don't check relocations in excluded sections. */
5501 if ((s->flags & SEC_RELOC) == 0
5502 || s->reloc_count == 0
5503 || (s->flags & SEC_EXCLUDE) != 0
5504 || ((info->strip == strip_all
5505 || info->strip == strip_debugger)
5506 && (s->flags & SEC_DEBUGGING) != 0))
5507 continue;
5509 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5510 s, NULL,
5511 NULL,
5512 _bfd_link_keep_memory (info));
5513 if (internal_relocs == NULL)
5514 goto error_free_vers;
5516 rel = internal_relocs;
5517 relend = rel + s->reloc_count;
5518 for ( ; rel < relend; rel++)
5520 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5521 struct elf_link_hash_entry *h;
5523 /* Skip local symbols. */
5524 if (r_symndx < extsymoff)
5525 continue;
5527 h = sym_hash[r_symndx - extsymoff];
5528 if (h != NULL)
5529 h->root.non_ir_ref_regular = 1;
5532 if (elf_section_data (s)->relocs != internal_relocs)
5533 free (internal_relocs);
5537 free (extversym);
5538 extversym = NULL;
5539 free (isymbuf);
5540 isymbuf = NULL;
5542 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5544 unsigned int i;
5546 /* Restore the symbol table. */
5547 old_ent = (char *) old_tab + tabsize;
5548 memset (elf_sym_hashes (abfd), 0,
5549 extsymcount * sizeof (struct elf_link_hash_entry *));
5550 htab->root.table.table = old_table;
5551 htab->root.table.size = old_size;
5552 htab->root.table.count = old_count;
5553 memcpy (htab->root.table.table, old_tab, tabsize);
5554 htab->root.undefs = old_undefs;
5555 htab->root.undefs_tail = old_undefs_tail;
5556 if (htab->dynstr != NULL)
5557 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5558 free (old_strtab);
5559 old_strtab = NULL;
5560 for (i = 0; i < htab->root.table.size; i++)
5562 struct bfd_hash_entry *p;
5563 struct elf_link_hash_entry *h;
5564 unsigned int non_ir_ref_dynamic;
5566 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5568 /* Preserve non_ir_ref_dynamic so that this symbol
5569 will be exported when the dynamic lib becomes needed
5570 in the second pass. */
5571 h = (struct elf_link_hash_entry *) p;
5572 if (h->root.type == bfd_link_hash_warning)
5573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5574 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5576 h = (struct elf_link_hash_entry *) p;
5577 memcpy (h, old_ent, htab->root.table.entsize);
5578 old_ent = (char *) old_ent + htab->root.table.entsize;
5579 if (h->root.type == bfd_link_hash_warning)
5581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5582 memcpy (h, old_ent, htab->root.table.entsize);
5583 old_ent = (char *) old_ent + htab->root.table.entsize;
5585 if (h->root.type == bfd_link_hash_common)
5587 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5588 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5590 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5594 /* Make a special call to the linker "notice" function to
5595 tell it that symbols added for crefs may need to be removed. */
5596 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5597 goto error_free_vers;
5599 free (old_tab);
5600 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5601 alloc_mark);
5602 free (nondeflt_vers);
5603 return true;
5606 if (old_tab != NULL)
5608 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5609 goto error_free_vers;
5610 free (old_tab);
5611 old_tab = NULL;
5614 /* Now that all the symbols from this input file are created, if
5615 not performing a relocatable link, handle .symver foo, foo@BAR
5616 such that any relocs against foo become foo@BAR. */
5617 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5619 size_t cnt, symidx;
5621 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5623 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5624 char *shortname, *p;
5625 size_t amt;
5627 p = strchr (h->root.root.string, ELF_VER_CHR);
5628 if (p == NULL
5629 || (h->root.type != bfd_link_hash_defined
5630 && h->root.type != bfd_link_hash_defweak))
5631 continue;
5633 amt = p - h->root.root.string;
5634 shortname = (char *) bfd_malloc (amt + 1);
5635 if (!shortname)
5636 goto error_free_vers;
5637 memcpy (shortname, h->root.root.string, amt);
5638 shortname[amt] = '\0';
5640 hi = (struct elf_link_hash_entry *)
5641 bfd_link_hash_lookup (&htab->root, shortname,
5642 false, false, false);
5643 if (hi != NULL
5644 && hi->root.type == h->root.type
5645 && hi->root.u.def.value == h->root.u.def.value
5646 && hi->root.u.def.section == h->root.u.def.section)
5648 (*bed->elf_backend_hide_symbol) (info, hi, true);
5649 hi->root.type = bfd_link_hash_indirect;
5650 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5651 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5652 sym_hash = elf_sym_hashes (abfd);
5653 if (sym_hash)
5654 for (symidx = 0; symidx < extsymcount; ++symidx)
5655 if (sym_hash[symidx] == hi)
5657 sym_hash[symidx] = h;
5658 break;
5661 free (shortname);
5663 free (nondeflt_vers);
5664 nondeflt_vers = NULL;
5667 /* Now set the alias field correctly for all the weak defined
5668 symbols we found. The only way to do this is to search all the
5669 symbols. Since we only need the information for non functions in
5670 dynamic objects, that's the only time we actually put anything on
5671 the list WEAKS. We need this information so that if a regular
5672 object refers to a symbol defined weakly in a dynamic object, the
5673 real symbol in the dynamic object is also put in the dynamic
5674 symbols; we also must arrange for both symbols to point to the
5675 same memory location. We could handle the general case of symbol
5676 aliasing, but a general symbol alias can only be generated in
5677 assembler code, handling it correctly would be very time
5678 consuming, and other ELF linkers don't handle general aliasing
5679 either. */
5680 if (weaks != NULL)
5682 struct elf_link_hash_entry **hpp;
5683 struct elf_link_hash_entry **hppend;
5684 struct elf_link_hash_entry **sorted_sym_hash;
5685 struct elf_link_hash_entry *h;
5686 size_t sym_count, amt;
5688 /* Since we have to search the whole symbol list for each weak
5689 defined symbol, search time for N weak defined symbols will be
5690 O(N^2). Binary search will cut it down to O(NlogN). */
5691 amt = extsymcount * sizeof (*sorted_sym_hash);
5692 sorted_sym_hash = bfd_malloc (amt);
5693 if (sorted_sym_hash == NULL)
5694 goto error_return;
5695 sym_hash = sorted_sym_hash;
5696 hpp = elf_sym_hashes (abfd);
5697 hppend = hpp + extsymcount;
5698 sym_count = 0;
5699 for (; hpp < hppend; hpp++)
5701 h = *hpp;
5702 if (h != NULL
5703 && h->root.type == bfd_link_hash_defined
5704 && !bed->is_function_type (h->type))
5706 *sym_hash = h;
5707 sym_hash++;
5708 sym_count++;
5712 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5713 elf_sort_symbol);
5715 while (weaks != NULL)
5717 struct elf_link_hash_entry *hlook;
5718 asection *slook;
5719 bfd_vma vlook;
5720 size_t i, j, idx = 0;
5722 hlook = weaks;
5723 weaks = hlook->u.alias;
5724 hlook->u.alias = NULL;
5726 if (hlook->root.type != bfd_link_hash_defined
5727 && hlook->root.type != bfd_link_hash_defweak)
5728 continue;
5730 slook = hlook->root.u.def.section;
5731 vlook = hlook->root.u.def.value;
5733 i = 0;
5734 j = sym_count;
5735 while (i != j)
5737 bfd_signed_vma vdiff;
5738 idx = (i + j) / 2;
5739 h = sorted_sym_hash[idx];
5740 vdiff = vlook - h->root.u.def.value;
5741 if (vdiff < 0)
5742 j = idx;
5743 else if (vdiff > 0)
5744 i = idx + 1;
5745 else
5747 int sdiff = slook->id - h->root.u.def.section->id;
5748 if (sdiff < 0)
5749 j = idx;
5750 else if (sdiff > 0)
5751 i = idx + 1;
5752 else
5753 break;
5757 /* We didn't find a value/section match. */
5758 if (i == j)
5759 continue;
5761 /* With multiple aliases, or when the weak symbol is already
5762 strongly defined, we have multiple matching symbols and
5763 the binary search above may land on any of them. Step
5764 one past the matching symbol(s). */
5765 while (++idx != j)
5767 h = sorted_sym_hash[idx];
5768 if (h->root.u.def.section != slook
5769 || h->root.u.def.value != vlook)
5770 break;
5773 /* Now look back over the aliases. Since we sorted by size
5774 as well as value and section, we'll choose the one with
5775 the largest size. */
5776 while (idx-- != i)
5778 h = sorted_sym_hash[idx];
5780 /* Stop if value or section doesn't match. */
5781 if (h->root.u.def.section != slook
5782 || h->root.u.def.value != vlook)
5783 break;
5784 else if (h != hlook)
5786 struct elf_link_hash_entry *t;
5788 hlook->u.alias = h;
5789 hlook->is_weakalias = 1;
5790 t = h;
5791 if (t->u.alias != NULL)
5792 while (t->u.alias != h)
5793 t = t->u.alias;
5794 t->u.alias = hlook;
5796 /* If the weak definition is in the list of dynamic
5797 symbols, make sure the real definition is put
5798 there as well. */
5799 if (hlook->dynindx != -1 && h->dynindx == -1)
5801 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5803 err_free_sym_hash:
5804 free (sorted_sym_hash);
5805 goto error_return;
5809 /* If the real definition is in the list of dynamic
5810 symbols, make sure the weak definition is put
5811 there as well. If we don't do this, then the
5812 dynamic loader might not merge the entries for the
5813 real definition and the weak definition. */
5814 if (h->dynindx != -1 && hlook->dynindx == -1)
5816 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5817 goto err_free_sym_hash;
5819 break;
5824 free (sorted_sym_hash);
5827 if (bed->check_directives
5828 && !(*bed->check_directives) (abfd, info))
5829 return false;
5831 /* If this is a non-traditional link, try to optimize the handling
5832 of the .stab/.stabstr sections. */
5833 if (! dynamic
5834 && ! info->traditional_format
5835 && is_elf_hash_table (&htab->root)
5836 && (info->strip != strip_all && info->strip != strip_debugger))
5838 asection *stabstr;
5840 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5841 if (stabstr != NULL)
5843 bfd_size_type string_offset = 0;
5844 asection *stab;
5846 for (stab = abfd->sections; stab; stab = stab->next)
5847 if (startswith (stab->name, ".stab")
5848 && (!stab->name[5] ||
5849 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5850 && (stab->flags & SEC_MERGE) == 0
5851 && !bfd_is_abs_section (stab->output_section))
5853 struct bfd_elf_section_data *secdata;
5855 secdata = elf_section_data (stab);
5856 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5857 stabstr, &secdata->sec_info,
5858 &string_offset))
5859 goto error_return;
5860 if (secdata->sec_info)
5861 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5866 if (dynamic && add_needed)
5868 /* Add this bfd to the loaded list. */
5869 struct elf_link_loaded_list *n;
5871 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5872 if (n == NULL)
5873 goto error_return;
5874 n->abfd = abfd;
5875 n->next = htab->dyn_loaded;
5876 htab->dyn_loaded = n;
5878 if (dynamic && !add_needed
5879 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5880 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5882 return true;
5884 error_free_vers:
5885 free (old_tab);
5886 free (old_strtab);
5887 free (nondeflt_vers);
5888 free (extversym);
5889 error_free_sym:
5890 free (isymbuf);
5891 error_return:
5892 return false;
5895 /* Return the linker hash table entry of a symbol that might be
5896 satisfied by an archive symbol. Return -1 on error. */
5898 struct bfd_link_hash_entry *
5899 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5900 struct bfd_link_info *info,
5901 const char *name)
5903 struct bfd_link_hash_entry *h;
5904 char *p, *copy;
5905 size_t len, first;
5907 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5908 if (h != NULL)
5909 return h;
5911 /* If this is a default version (the name contains @@), look up the
5912 symbol again with only one `@' as well as without the version.
5913 The effect is that references to the symbol with and without the
5914 version will be matched by the default symbol in the archive. */
5916 p = strchr (name, ELF_VER_CHR);
5917 if (p == NULL || p[1] != ELF_VER_CHR)
5918 return h;
5920 /* First check with only one `@'. */
5921 len = strlen (name);
5922 copy = (char *) bfd_alloc (abfd, len);
5923 if (copy == NULL)
5924 return (struct bfd_link_hash_entry *) -1;
5926 first = p - name + 1;
5927 memcpy (copy, name, first);
5928 memcpy (copy + first, name + first + 1, len - first);
5930 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5931 if (h == NULL)
5933 /* We also need to check references to the symbol without the
5934 version. */
5935 copy[first - 1] = '\0';
5936 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5939 bfd_release (abfd, copy);
5940 return h;
5943 /* Add symbols from an ELF archive file to the linker hash table. We
5944 don't use _bfd_generic_link_add_archive_symbols because we need to
5945 handle versioned symbols.
5947 Fortunately, ELF archive handling is simpler than that done by
5948 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5949 oddities. In ELF, if we find a symbol in the archive map, and the
5950 symbol is currently undefined, we know that we must pull in that
5951 object file.
5953 Unfortunately, we do have to make multiple passes over the symbol
5954 table until nothing further is resolved. */
5956 static bool
5957 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5959 symindex c;
5960 unsigned char *included = NULL;
5961 carsym *symdefs;
5962 bool loop;
5963 size_t amt;
5964 const struct elf_backend_data *bed;
5965 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5966 (bfd *, struct bfd_link_info *, const char *);
5968 if (! bfd_has_map (abfd))
5970 /* An empty archive is a special case. */
5971 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5972 return true;
5973 bfd_set_error (bfd_error_no_armap);
5974 return false;
5977 /* Keep track of all symbols we know to be already defined, and all
5978 files we know to be already included. This is to speed up the
5979 second and subsequent passes. */
5980 c = bfd_ardata (abfd)->symdef_count;
5981 if (c == 0)
5982 return true;
5983 amt = c * sizeof (*included);
5984 included = (unsigned char *) bfd_zmalloc (amt);
5985 if (included == NULL)
5986 return false;
5988 symdefs = bfd_ardata (abfd)->symdefs;
5989 bed = get_elf_backend_data (abfd);
5990 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5994 file_ptr last;
5995 symindex i;
5996 carsym *symdef;
5997 carsym *symdefend;
5999 loop = false;
6000 last = -1;
6002 symdef = symdefs;
6003 symdefend = symdef + c;
6004 for (i = 0; symdef < symdefend; symdef++, i++)
6006 struct bfd_link_hash_entry *h;
6007 bfd *element;
6008 struct bfd_link_hash_entry *undefs_tail;
6009 symindex mark;
6011 if (included[i])
6012 continue;
6013 if (symdef->file_offset == last)
6015 included[i] = true;
6016 continue;
6019 h = archive_symbol_lookup (abfd, info, symdef->name);
6020 if (h == (struct bfd_link_hash_entry *) -1)
6021 goto error_return;
6023 if (h == NULL)
6024 continue;
6026 if (h->type == bfd_link_hash_undefined)
6028 /* If the archive element has already been loaded then one
6029 of the symbols defined by that element might have been
6030 made undefined due to being in a discarded section. */
6031 if (is_elf_hash_table (info->hash)
6032 && ((struct elf_link_hash_entry *) h)->indx == -3)
6033 continue;
6035 else if (h->type == bfd_link_hash_common)
6037 /* We currently have a common symbol. The archive map contains
6038 a reference to this symbol, so we may want to include it. We
6039 only want to include it however, if this archive element
6040 contains a definition of the symbol, not just another common
6041 declaration of it.
6043 Unfortunately some archivers (including GNU ar) will put
6044 declarations of common symbols into their archive maps, as
6045 well as real definitions, so we cannot just go by the archive
6046 map alone. Instead we must read in the element's symbol
6047 table and check that to see what kind of symbol definition
6048 this is. */
6049 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6050 continue;
6052 else
6054 if (h->type != bfd_link_hash_undefweak)
6055 /* Symbol must be defined. Don't check it again. */
6056 included[i] = true;
6057 continue;
6060 /* We need to include this archive member. */
6061 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6062 info);
6063 if (element == NULL)
6064 goto error_return;
6066 if (! bfd_check_format (element, bfd_object))
6067 goto error_return;
6069 undefs_tail = info->hash->undefs_tail;
6071 if (!(*info->callbacks
6072 ->add_archive_element) (info, element, symdef->name, &element))
6073 continue;
6074 if (!bfd_link_add_symbols (element, info))
6075 goto error_return;
6077 /* If there are any new undefined symbols, we need to make
6078 another pass through the archive in order to see whether
6079 they can be defined. FIXME: This isn't perfect, because
6080 common symbols wind up on undefs_tail and because an
6081 undefined symbol which is defined later on in this pass
6082 does not require another pass. This isn't a bug, but it
6083 does make the code less efficient than it could be. */
6084 if (undefs_tail != info->hash->undefs_tail)
6085 loop = true;
6087 /* Look backward to mark all symbols from this object file
6088 which we have already seen in this pass. */
6089 mark = i;
6092 included[mark] = true;
6093 if (mark == 0)
6094 break;
6095 --mark;
6097 while (symdefs[mark].file_offset == symdef->file_offset);
6099 /* We mark subsequent symbols from this object file as we go
6100 on through the loop. */
6101 last = symdef->file_offset;
6104 while (loop);
6106 free (included);
6107 return true;
6109 error_return:
6110 free (included);
6111 return false;
6114 /* Given an ELF BFD, add symbols to the global hash table as
6115 appropriate. */
6117 bool
6118 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6120 switch (bfd_get_format (abfd))
6122 case bfd_object:
6123 return elf_link_add_object_symbols (abfd, info);
6124 case bfd_archive:
6125 return elf_link_add_archive_symbols (abfd, info);
6126 default:
6127 bfd_set_error (bfd_error_wrong_format);
6128 return false;
6132 struct hash_codes_info
6134 unsigned long *hashcodes;
6135 bool error;
6138 /* This function will be called though elf_link_hash_traverse to store
6139 all hash value of the exported symbols in an array. */
6141 static bool
6142 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6144 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6145 const char *name;
6146 unsigned long ha;
6147 char *alc = NULL;
6149 /* Ignore indirect symbols. These are added by the versioning code. */
6150 if (h->dynindx == -1)
6151 return true;
6153 name = h->root.root.string;
6154 if (h->versioned >= versioned)
6156 char *p = strchr (name, ELF_VER_CHR);
6157 if (p != NULL)
6159 alc = (char *) bfd_malloc (p - name + 1);
6160 if (alc == NULL)
6162 inf->error = true;
6163 return false;
6165 memcpy (alc, name, p - name);
6166 alc[p - name] = '\0';
6167 name = alc;
6171 /* Compute the hash value. */
6172 ha = bfd_elf_hash (name);
6174 /* Store the found hash value in the array given as the argument. */
6175 *(inf->hashcodes)++ = ha;
6177 /* And store it in the struct so that we can put it in the hash table
6178 later. */
6179 h->u.elf_hash_value = ha;
6181 free (alc);
6182 return true;
6185 struct collect_gnu_hash_codes
6187 bfd *output_bfd;
6188 const struct elf_backend_data *bed;
6189 unsigned long int nsyms;
6190 unsigned long int maskbits;
6191 unsigned long int *hashcodes;
6192 unsigned long int *hashval;
6193 unsigned long int *indx;
6194 unsigned long int *counts;
6195 bfd_vma *bitmask;
6196 bfd_byte *contents;
6197 bfd_size_type xlat;
6198 long int min_dynindx;
6199 unsigned long int bucketcount;
6200 unsigned long int symindx;
6201 long int local_indx;
6202 long int shift1, shift2;
6203 unsigned long int mask;
6204 bool error;
6207 /* This function will be called though elf_link_hash_traverse to store
6208 all hash value of the exported symbols in an array. */
6210 static bool
6211 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6213 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6214 const char *name;
6215 unsigned long ha;
6216 char *alc = NULL;
6218 /* Ignore indirect symbols. These are added by the versioning code. */
6219 if (h->dynindx == -1)
6220 return true;
6222 /* Ignore also local symbols and undefined symbols. */
6223 if (! (*s->bed->elf_hash_symbol) (h))
6224 return true;
6226 name = h->root.root.string;
6227 if (h->versioned >= versioned)
6229 char *p = strchr (name, ELF_VER_CHR);
6230 if (p != NULL)
6232 alc = (char *) bfd_malloc (p - name + 1);
6233 if (alc == NULL)
6235 s->error = true;
6236 return false;
6238 memcpy (alc, name, p - name);
6239 alc[p - name] = '\0';
6240 name = alc;
6244 /* Compute the hash value. */
6245 ha = bfd_elf_gnu_hash (name);
6247 /* Store the found hash value in the array for compute_bucket_count,
6248 and also for .dynsym reordering purposes. */
6249 s->hashcodes[s->nsyms] = ha;
6250 s->hashval[h->dynindx] = ha;
6251 ++s->nsyms;
6252 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6253 s->min_dynindx = h->dynindx;
6255 free (alc);
6256 return true;
6259 /* This function will be called though elf_link_hash_traverse to do
6260 final dynamic symbol renumbering in case of .gnu.hash.
6261 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6262 to the translation table. */
6264 static bool
6265 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6267 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6268 unsigned long int bucket;
6269 unsigned long int val;
6271 /* Ignore indirect symbols. */
6272 if (h->dynindx == -1)
6273 return true;
6275 /* Ignore also local symbols and undefined symbols. */
6276 if (! (*s->bed->elf_hash_symbol) (h))
6278 if (h->dynindx >= s->min_dynindx)
6280 if (s->bed->record_xhash_symbol != NULL)
6282 (*s->bed->record_xhash_symbol) (h, 0);
6283 s->local_indx++;
6285 else
6286 h->dynindx = s->local_indx++;
6288 return true;
6291 bucket = s->hashval[h->dynindx] % s->bucketcount;
6292 val = (s->hashval[h->dynindx] >> s->shift1)
6293 & ((s->maskbits >> s->shift1) - 1);
6294 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6295 s->bitmask[val]
6296 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6297 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6298 if (s->counts[bucket] == 1)
6299 /* Last element terminates the chain. */
6300 val |= 1;
6301 bfd_put_32 (s->output_bfd, val,
6302 s->contents + (s->indx[bucket] - s->symindx) * 4);
6303 --s->counts[bucket];
6304 if (s->bed->record_xhash_symbol != NULL)
6306 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6308 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6310 else
6311 h->dynindx = s->indx[bucket]++;
6312 return true;
6315 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6317 bool
6318 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6320 return !(h->forced_local
6321 || h->root.type == bfd_link_hash_undefined
6322 || h->root.type == bfd_link_hash_undefweak
6323 || ((h->root.type == bfd_link_hash_defined
6324 || h->root.type == bfd_link_hash_defweak)
6325 && h->root.u.def.section->output_section == NULL));
6328 /* Array used to determine the number of hash table buckets to use
6329 based on the number of symbols there are. If there are fewer than
6330 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6331 fewer than 37 we use 17 buckets, and so forth. We never use more
6332 than 32771 buckets. */
6334 static const size_t elf_buckets[] =
6336 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6337 16411, 32771, 0
6340 /* Compute bucket count for hashing table. We do not use a static set
6341 of possible tables sizes anymore. Instead we determine for all
6342 possible reasonable sizes of the table the outcome (i.e., the
6343 number of collisions etc) and choose the best solution. The
6344 weighting functions are not too simple to allow the table to grow
6345 without bounds. Instead one of the weighting factors is the size.
6346 Therefore the result is always a good payoff between few collisions
6347 (= short chain lengths) and table size. */
6348 static size_t
6349 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6350 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6351 unsigned long int nsyms,
6352 int gnu_hash)
6354 size_t best_size = 0;
6355 unsigned long int i;
6357 if (info->optimize)
6359 size_t minsize;
6360 size_t maxsize;
6361 uint64_t best_chlen = ~((uint64_t) 0);
6362 bfd *dynobj = elf_hash_table (info)->dynobj;
6363 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6364 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6365 unsigned long int *counts;
6366 bfd_size_type amt;
6367 unsigned int no_improvement_count = 0;
6369 /* Possible optimization parameters: if we have NSYMS symbols we say
6370 that the hashing table must at least have NSYMS/4 and at most
6371 2*NSYMS buckets. */
6372 minsize = nsyms / 4;
6373 if (minsize == 0)
6374 minsize = 1;
6375 best_size = maxsize = nsyms * 2;
6376 if (gnu_hash)
6378 if (minsize < 2)
6379 minsize = 2;
6380 if ((best_size & 31) == 0)
6381 ++best_size;
6384 /* Create array where we count the collisions in. We must use bfd_malloc
6385 since the size could be large. */
6386 amt = maxsize;
6387 amt *= sizeof (unsigned long int);
6388 counts = (unsigned long int *) bfd_malloc (amt);
6389 if (counts == NULL)
6390 return 0;
6392 /* Compute the "optimal" size for the hash table. The criteria is a
6393 minimal chain length. The minor criteria is (of course) the size
6394 of the table. */
6395 for (i = minsize; i < maxsize; ++i)
6397 /* Walk through the array of hashcodes and count the collisions. */
6398 uint64_t max;
6399 unsigned long int j;
6400 unsigned long int fact;
6402 if (gnu_hash && (i & 31) == 0)
6403 continue;
6405 memset (counts, '\0', i * sizeof (unsigned long int));
6407 /* Determine how often each hash bucket is used. */
6408 for (j = 0; j < nsyms; ++j)
6409 ++counts[hashcodes[j] % i];
6411 /* For the weight function we need some information about the
6412 pagesize on the target. This is information need not be 100%
6413 accurate. Since this information is not available (so far) we
6414 define it here to a reasonable default value. If it is crucial
6415 to have a better value some day simply define this value. */
6416 # ifndef BFD_TARGET_PAGESIZE
6417 # define BFD_TARGET_PAGESIZE (4096)
6418 # endif
6420 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6421 and the chains. */
6422 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6424 # if 1
6425 /* Variant 1: optimize for short chains. We add the squares
6426 of all the chain lengths (which favors many small chain
6427 over a few long chains). */
6428 for (j = 0; j < i; ++j)
6429 max += counts[j] * counts[j];
6431 /* This adds penalties for the overall size of the table. */
6432 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6433 max *= fact * fact;
6434 # else
6435 /* Variant 2: Optimize a lot more for small table. Here we
6436 also add squares of the size but we also add penalties for
6437 empty slots (the +1 term). */
6438 for (j = 0; j < i; ++j)
6439 max += (1 + counts[j]) * (1 + counts[j]);
6441 /* The overall size of the table is considered, but not as
6442 strong as in variant 1, where it is squared. */
6443 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6444 max *= fact;
6445 # endif
6447 /* Compare with current best results. */
6448 if (max < best_chlen)
6450 best_chlen = max;
6451 best_size = i;
6452 no_improvement_count = 0;
6454 /* PR 11843: Avoid futile long searches for the best bucket size
6455 when there are a large number of symbols. */
6456 else if (++no_improvement_count == 100)
6457 break;
6460 free (counts);
6462 else
6464 for (i = 0; elf_buckets[i] != 0; i++)
6466 best_size = elf_buckets[i];
6467 if (nsyms < elf_buckets[i + 1])
6468 break;
6470 if (gnu_hash && best_size < 2)
6471 best_size = 2;
6474 return best_size;
6477 /* Size any SHT_GROUP section for ld -r. */
6479 bool
6480 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6482 bfd *ibfd;
6483 asection *s;
6485 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6486 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6487 && (s = ibfd->sections) != NULL
6488 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6489 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6490 return false;
6491 return true;
6494 /* Set a default stack segment size. The value in INFO wins. If it
6495 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6496 undefined it is initialized. */
6498 bool
6499 bfd_elf_stack_segment_size (bfd *output_bfd,
6500 struct bfd_link_info *info,
6501 const char *legacy_symbol,
6502 bfd_vma default_size)
6504 struct elf_link_hash_entry *h = NULL;
6506 /* Look for legacy symbol. */
6507 if (legacy_symbol)
6508 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6509 false, false, false);
6510 if (h && (h->root.type == bfd_link_hash_defined
6511 || h->root.type == bfd_link_hash_defweak)
6512 && h->def_regular
6513 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6515 /* The symbol has no type if specified on the command line. */
6516 h->type = STT_OBJECT;
6517 if (info->stacksize)
6518 /* xgettext:c-format */
6519 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6520 output_bfd, legacy_symbol);
6521 else if (h->root.u.def.section != bfd_abs_section_ptr)
6522 /* xgettext:c-format */
6523 _bfd_error_handler (_("%pB: %s not absolute"),
6524 output_bfd, legacy_symbol);
6525 else
6526 info->stacksize = h->root.u.def.value;
6529 if (!info->stacksize)
6530 /* If the user didn't set a size, or explicitly inhibit the
6531 size, set it now. */
6532 info->stacksize = default_size;
6534 /* Provide the legacy symbol, if it is referenced. */
6535 if (h && (h->root.type == bfd_link_hash_undefined
6536 || h->root.type == bfd_link_hash_undefweak))
6538 struct bfd_link_hash_entry *bh = NULL;
6540 if (!(_bfd_generic_link_add_one_symbol
6541 (info, output_bfd, legacy_symbol,
6542 BSF_GLOBAL, bfd_abs_section_ptr,
6543 info->stacksize >= 0 ? info->stacksize : 0,
6544 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6545 return false;
6547 h = (struct elf_link_hash_entry *) bh;
6548 h->def_regular = 1;
6549 h->type = STT_OBJECT;
6552 return true;
6555 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6557 struct elf_gc_sweep_symbol_info
6559 struct bfd_link_info *info;
6560 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6561 bool);
6564 static bool
6565 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6567 if (!h->mark
6568 && (((h->root.type == bfd_link_hash_defined
6569 || h->root.type == bfd_link_hash_defweak)
6570 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6571 && h->root.u.def.section->gc_mark))
6572 || h->root.type == bfd_link_hash_undefined
6573 || h->root.type == bfd_link_hash_undefweak))
6575 struct elf_gc_sweep_symbol_info *inf;
6577 inf = (struct elf_gc_sweep_symbol_info *) data;
6578 (*inf->hide_symbol) (inf->info, h, true);
6579 h->def_regular = 0;
6580 h->ref_regular = 0;
6581 h->ref_regular_nonweak = 0;
6584 return true;
6587 /* Set up the sizes and contents of the ELF dynamic sections. This is
6588 called by the ELF linker emulation before_allocation routine. We
6589 must set the sizes of the sections before the linker sets the
6590 addresses of the various sections. */
6592 bool
6593 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6594 const char *soname,
6595 const char *rpath,
6596 const char *filter_shlib,
6597 const char *audit,
6598 const char *depaudit,
6599 const char * const *auxiliary_filters,
6600 struct bfd_link_info *info,
6601 asection **sinterpptr)
6603 bfd *dynobj;
6604 const struct elf_backend_data *bed;
6606 *sinterpptr = NULL;
6608 if (!is_elf_hash_table (info->hash))
6609 return true;
6611 /* Any syms created from now on start with -1 in
6612 got.refcount/offset and plt.refcount/offset. */
6613 elf_hash_table (info)->init_got_refcount
6614 = elf_hash_table (info)->init_got_offset;
6615 elf_hash_table (info)->init_plt_refcount
6616 = elf_hash_table (info)->init_plt_offset;
6618 bed = get_elf_backend_data (output_bfd);
6620 /* The backend may have to create some sections regardless of whether
6621 we're dynamic or not. */
6622 if (bed->elf_backend_always_size_sections
6623 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6624 return false;
6626 dynobj = elf_hash_table (info)->dynobj;
6628 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6630 struct bfd_elf_version_tree *verdefs;
6631 struct elf_info_failed asvinfo;
6632 struct bfd_elf_version_tree *t;
6633 struct bfd_elf_version_expr *d;
6634 asection *s;
6635 size_t soname_indx;
6637 /* If we are supposed to export all symbols into the dynamic symbol
6638 table (this is not the normal case), then do so. */
6639 if (info->export_dynamic
6640 || (bfd_link_executable (info) && info->dynamic))
6642 struct elf_info_failed eif;
6644 eif.info = info;
6645 eif.failed = false;
6646 elf_link_hash_traverse (elf_hash_table (info),
6647 _bfd_elf_export_symbol,
6648 &eif);
6649 if (eif.failed)
6650 return false;
6653 if (soname != NULL)
6655 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6656 soname, true);
6657 if (soname_indx == (size_t) -1
6658 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6659 return false;
6661 else
6662 soname_indx = (size_t) -1;
6664 /* Make all global versions with definition. */
6665 for (t = info->version_info; t != NULL; t = t->next)
6666 for (d = t->globals.list; d != NULL; d = d->next)
6667 if (!d->symver && d->literal)
6669 const char *verstr, *name;
6670 size_t namelen, verlen, newlen;
6671 char *newname, *p, leading_char;
6672 struct elf_link_hash_entry *newh;
6674 leading_char = bfd_get_symbol_leading_char (output_bfd);
6675 name = d->pattern;
6676 namelen = strlen (name) + (leading_char != '\0');
6677 verstr = t->name;
6678 verlen = strlen (verstr);
6679 newlen = namelen + verlen + 3;
6681 newname = (char *) bfd_malloc (newlen);
6682 if (newname == NULL)
6683 return false;
6684 newname[0] = leading_char;
6685 memcpy (newname + (leading_char != '\0'), name, namelen);
6687 /* Check the hidden versioned definition. */
6688 p = newname + namelen;
6689 *p++ = ELF_VER_CHR;
6690 memcpy (p, verstr, verlen + 1);
6691 newh = elf_link_hash_lookup (elf_hash_table (info),
6692 newname, false, false,
6693 false);
6694 if (newh == NULL
6695 || (newh->root.type != bfd_link_hash_defined
6696 && newh->root.type != bfd_link_hash_defweak))
6698 /* Check the default versioned definition. */
6699 *p++ = ELF_VER_CHR;
6700 memcpy (p, verstr, verlen + 1);
6701 newh = elf_link_hash_lookup (elf_hash_table (info),
6702 newname, false, false,
6703 false);
6705 free (newname);
6707 /* Mark this version if there is a definition and it is
6708 not defined in a shared object. */
6709 if (newh != NULL
6710 && !newh->def_dynamic
6711 && (newh->root.type == bfd_link_hash_defined
6712 || newh->root.type == bfd_link_hash_defweak))
6713 d->symver = 1;
6716 /* Attach all the symbols to their version information. */
6717 asvinfo.info = info;
6718 asvinfo.failed = false;
6720 elf_link_hash_traverse (elf_hash_table (info),
6721 _bfd_elf_link_assign_sym_version,
6722 &asvinfo);
6723 if (asvinfo.failed)
6724 return false;
6726 if (!info->allow_undefined_version)
6728 /* Check if all global versions have a definition. */
6729 bool all_defined = true;
6730 for (t = info->version_info; t != NULL; t = t->next)
6731 for (d = t->globals.list; d != NULL; d = d->next)
6732 if (d->literal && !d->symver && !d->script)
6734 _bfd_error_handler
6735 (_("%s: undefined version: %s"),
6736 d->pattern, t->name);
6737 all_defined = false;
6740 if (!all_defined)
6742 bfd_set_error (bfd_error_bad_value);
6743 return false;
6747 /* Set up the version definition section. */
6748 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6749 BFD_ASSERT (s != NULL);
6751 /* We may have created additional version definitions if we are
6752 just linking a regular application. */
6753 verdefs = info->version_info;
6755 /* Skip anonymous version tag. */
6756 if (verdefs != NULL && verdefs->vernum == 0)
6757 verdefs = verdefs->next;
6759 if (verdefs == NULL && !info->create_default_symver)
6760 s->flags |= SEC_EXCLUDE;
6761 else
6763 unsigned int cdefs;
6764 bfd_size_type size;
6765 bfd_byte *p;
6766 Elf_Internal_Verdef def;
6767 Elf_Internal_Verdaux defaux;
6768 struct bfd_link_hash_entry *bh;
6769 struct elf_link_hash_entry *h;
6770 const char *name;
6772 cdefs = 0;
6773 size = 0;
6775 /* Make space for the base version. */
6776 size += sizeof (Elf_External_Verdef);
6777 size += sizeof (Elf_External_Verdaux);
6778 ++cdefs;
6780 /* Make space for the default version. */
6781 if (info->create_default_symver)
6783 size += sizeof (Elf_External_Verdef);
6784 ++cdefs;
6787 for (t = verdefs; t != NULL; t = t->next)
6789 struct bfd_elf_version_deps *n;
6791 /* Don't emit base version twice. */
6792 if (t->vernum == 0)
6793 continue;
6795 size += sizeof (Elf_External_Verdef);
6796 size += sizeof (Elf_External_Verdaux);
6797 ++cdefs;
6799 for (n = t->deps; n != NULL; n = n->next)
6800 size += sizeof (Elf_External_Verdaux);
6803 s->size = size;
6804 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6805 if (s->contents == NULL && s->size != 0)
6806 return false;
6808 /* Fill in the version definition section. */
6810 p = s->contents;
6812 def.vd_version = VER_DEF_CURRENT;
6813 def.vd_flags = VER_FLG_BASE;
6814 def.vd_ndx = 1;
6815 def.vd_cnt = 1;
6816 if (info->create_default_symver)
6818 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6819 def.vd_next = sizeof (Elf_External_Verdef);
6821 else
6823 def.vd_aux = sizeof (Elf_External_Verdef);
6824 def.vd_next = (sizeof (Elf_External_Verdef)
6825 + sizeof (Elf_External_Verdaux));
6828 if (soname_indx != (size_t) -1)
6830 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6831 soname_indx);
6832 def.vd_hash = bfd_elf_hash (soname);
6833 defaux.vda_name = soname_indx;
6834 name = soname;
6836 else
6838 size_t indx;
6840 name = lbasename (bfd_get_filename (output_bfd));
6841 def.vd_hash = bfd_elf_hash (name);
6842 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6843 name, false);
6844 if (indx == (size_t) -1)
6845 return false;
6846 defaux.vda_name = indx;
6848 defaux.vda_next = 0;
6850 _bfd_elf_swap_verdef_out (output_bfd, &def,
6851 (Elf_External_Verdef *) p);
6852 p += sizeof (Elf_External_Verdef);
6853 if (info->create_default_symver)
6855 /* Add a symbol representing this version. */
6856 bh = NULL;
6857 if (! (_bfd_generic_link_add_one_symbol
6858 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6859 0, NULL, false,
6860 get_elf_backend_data (dynobj)->collect, &bh)))
6861 return false;
6862 h = (struct elf_link_hash_entry *) bh;
6863 h->non_elf = 0;
6864 h->def_regular = 1;
6865 h->type = STT_OBJECT;
6866 h->verinfo.vertree = NULL;
6868 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6869 return false;
6871 /* Create a duplicate of the base version with the same
6872 aux block, but different flags. */
6873 def.vd_flags = 0;
6874 def.vd_ndx = 2;
6875 def.vd_aux = sizeof (Elf_External_Verdef);
6876 if (verdefs)
6877 def.vd_next = (sizeof (Elf_External_Verdef)
6878 + sizeof (Elf_External_Verdaux));
6879 else
6880 def.vd_next = 0;
6881 _bfd_elf_swap_verdef_out (output_bfd, &def,
6882 (Elf_External_Verdef *) p);
6883 p += sizeof (Elf_External_Verdef);
6885 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6886 (Elf_External_Verdaux *) p);
6887 p += sizeof (Elf_External_Verdaux);
6889 for (t = verdefs; t != NULL; t = t->next)
6891 unsigned int cdeps;
6892 struct bfd_elf_version_deps *n;
6894 /* Don't emit the base version twice. */
6895 if (t->vernum == 0)
6896 continue;
6898 cdeps = 0;
6899 for (n = t->deps; n != NULL; n = n->next)
6900 ++cdeps;
6902 /* Add a symbol representing this version. */
6903 bh = NULL;
6904 if (! (_bfd_generic_link_add_one_symbol
6905 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6906 0, NULL, false,
6907 get_elf_backend_data (dynobj)->collect, &bh)))
6908 return false;
6909 h = (struct elf_link_hash_entry *) bh;
6910 h->non_elf = 0;
6911 h->def_regular = 1;
6912 h->type = STT_OBJECT;
6913 h->verinfo.vertree = t;
6915 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6916 return false;
6918 def.vd_version = VER_DEF_CURRENT;
6919 def.vd_flags = 0;
6920 if (t->globals.list == NULL
6921 && t->locals.list == NULL
6922 && ! t->used)
6923 def.vd_flags |= VER_FLG_WEAK;
6924 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6925 def.vd_cnt = cdeps + 1;
6926 def.vd_hash = bfd_elf_hash (t->name);
6927 def.vd_aux = sizeof (Elf_External_Verdef);
6928 def.vd_next = 0;
6930 /* If a basever node is next, it *must* be the last node in
6931 the chain, otherwise Verdef construction breaks. */
6932 if (t->next != NULL && t->next->vernum == 0)
6933 BFD_ASSERT (t->next->next == NULL);
6935 if (t->next != NULL && t->next->vernum != 0)
6936 def.vd_next = (sizeof (Elf_External_Verdef)
6937 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6939 _bfd_elf_swap_verdef_out (output_bfd, &def,
6940 (Elf_External_Verdef *) p);
6941 p += sizeof (Elf_External_Verdef);
6943 defaux.vda_name = h->dynstr_index;
6944 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6945 h->dynstr_index);
6946 defaux.vda_next = 0;
6947 if (t->deps != NULL)
6948 defaux.vda_next = sizeof (Elf_External_Verdaux);
6949 t->name_indx = defaux.vda_name;
6951 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6952 (Elf_External_Verdaux *) p);
6953 p += sizeof (Elf_External_Verdaux);
6955 for (n = t->deps; n != NULL; n = n->next)
6957 if (n->version_needed == NULL)
6959 /* This can happen if there was an error in the
6960 version script. */
6961 defaux.vda_name = 0;
6963 else
6965 defaux.vda_name = n->version_needed->name_indx;
6966 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6967 defaux.vda_name);
6969 if (n->next == NULL)
6970 defaux.vda_next = 0;
6971 else
6972 defaux.vda_next = sizeof (Elf_External_Verdaux);
6974 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6975 (Elf_External_Verdaux *) p);
6976 p += sizeof (Elf_External_Verdaux);
6980 elf_tdata (output_bfd)->cverdefs = cdefs;
6984 if (info->gc_sections && bed->can_gc_sections)
6986 struct elf_gc_sweep_symbol_info sweep_info;
6988 /* Remove the symbols that were in the swept sections from the
6989 dynamic symbol table. */
6990 sweep_info.info = info;
6991 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6992 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6993 &sweep_info);
6996 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6998 asection *s;
6999 struct elf_find_verdep_info sinfo;
7001 /* Work out the size of the version reference section. */
7003 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7004 BFD_ASSERT (s != NULL);
7006 sinfo.info = info;
7007 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7008 if (sinfo.vers == 0)
7009 sinfo.vers = 1;
7010 sinfo.failed = false;
7012 elf_link_hash_traverse (elf_hash_table (info),
7013 _bfd_elf_link_find_version_dependencies,
7014 &sinfo);
7015 if (sinfo.failed)
7016 return false;
7018 if (info->enable_dt_relr)
7020 elf_link_add_dt_relr_dependency (&sinfo);
7021 if (sinfo.failed)
7022 return false;
7025 if (elf_tdata (output_bfd)->verref == NULL)
7026 s->flags |= SEC_EXCLUDE;
7027 else
7029 Elf_Internal_Verneed *vn;
7030 unsigned int size;
7031 unsigned int crefs;
7032 bfd_byte *p;
7034 /* Build the version dependency section. */
7035 size = 0;
7036 crefs = 0;
7037 for (vn = elf_tdata (output_bfd)->verref;
7038 vn != NULL;
7039 vn = vn->vn_nextref)
7041 Elf_Internal_Vernaux *a;
7043 size += sizeof (Elf_External_Verneed);
7044 ++crefs;
7045 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7046 size += sizeof (Elf_External_Vernaux);
7049 s->size = size;
7050 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7051 if (s->contents == NULL)
7052 return false;
7054 p = s->contents;
7055 for (vn = elf_tdata (output_bfd)->verref;
7056 vn != NULL;
7057 vn = vn->vn_nextref)
7059 unsigned int caux;
7060 Elf_Internal_Vernaux *a;
7061 size_t indx;
7063 caux = 0;
7064 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7065 ++caux;
7067 vn->vn_version = VER_NEED_CURRENT;
7068 vn->vn_cnt = caux;
7069 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7070 elf_dt_name (vn->vn_bfd) != NULL
7071 ? elf_dt_name (vn->vn_bfd)
7072 : lbasename (bfd_get_filename
7073 (vn->vn_bfd)),
7074 false);
7075 if (indx == (size_t) -1)
7076 return false;
7077 vn->vn_file = indx;
7078 vn->vn_aux = sizeof (Elf_External_Verneed);
7079 if (vn->vn_nextref == NULL)
7080 vn->vn_next = 0;
7081 else
7082 vn->vn_next = (sizeof (Elf_External_Verneed)
7083 + caux * sizeof (Elf_External_Vernaux));
7085 _bfd_elf_swap_verneed_out (output_bfd, vn,
7086 (Elf_External_Verneed *) p);
7087 p += sizeof (Elf_External_Verneed);
7089 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7091 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7092 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7093 a->vna_nodename, false);
7094 if (indx == (size_t) -1)
7095 return false;
7096 a->vna_name = indx;
7097 if (a->vna_nextptr == NULL)
7098 a->vna_next = 0;
7099 else
7100 a->vna_next = sizeof (Elf_External_Vernaux);
7102 _bfd_elf_swap_vernaux_out (output_bfd, a,
7103 (Elf_External_Vernaux *) p);
7104 p += sizeof (Elf_External_Vernaux);
7108 elf_tdata (output_bfd)->cverrefs = crefs;
7112 if (bfd_link_relocatable (info)
7113 && !_bfd_elf_size_group_sections (info))
7114 return false;
7116 /* Determine any GNU_STACK segment requirements, after the backend
7117 has had a chance to set a default segment size. */
7118 if (info->execstack)
7120 /* If the user has explicitly requested warnings, then generate one even
7121 though the choice is the result of another command line option. */
7122 if (info->warn_execstack == 1)
7123 _bfd_error_handler
7124 (_("\
7125 warning: enabling an executable stack because of -z execstack command line option"));
7126 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7128 else if (info->noexecstack)
7129 elf_stack_flags (output_bfd) = PF_R | PF_W;
7130 else
7132 bfd *inputobj;
7133 asection *notesec = NULL;
7134 bfd *noteobj = NULL;
7135 bfd *emptyobj = NULL;
7136 int exec = 0;
7138 for (inputobj = info->input_bfds;
7139 inputobj;
7140 inputobj = inputobj->link.next)
7142 asection *s;
7144 if (inputobj->flags
7145 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7146 continue;
7147 s = inputobj->sections;
7148 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7149 continue;
7151 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7152 if (s)
7154 notesec = s;
7155 if (s->flags & SEC_CODE)
7157 noteobj = inputobj;
7158 exec = PF_X;
7159 /* There is no point in scanning the remaining bfds. */
7160 break;
7163 else if (bed->default_execstack && info->default_execstack)
7165 exec = PF_X;
7166 emptyobj = inputobj;
7170 if (notesec || info->stacksize > 0)
7172 if (exec)
7174 if (info->warn_execstack != 0)
7176 /* PR 29072: Because an executable stack is a serious
7177 security risk, make sure that the user knows that it is
7178 being enabled despite the fact that it was not requested
7179 on the command line. */
7180 if (noteobj)
7181 _bfd_error_handler (_("\
7182 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7183 bfd_get_filename (noteobj));
7184 else if (emptyobj)
7186 _bfd_error_handler (_("\
7187 warning: %s: missing .note.GNU-stack section implies executable stack"),
7188 bfd_get_filename (emptyobj));
7189 _bfd_error_handler (_("\
7190 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7194 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7197 if (notesec && exec && bfd_link_relocatable (info)
7198 && notesec->output_section != bfd_abs_section_ptr)
7199 notesec->output_section->flags |= SEC_CODE;
7202 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7204 struct elf_info_failed eif;
7205 struct elf_link_hash_entry *h;
7206 asection *dynstr;
7207 asection *s;
7209 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7210 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7212 if (info->symbolic)
7214 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7215 return false;
7216 info->flags |= DF_SYMBOLIC;
7219 if (rpath != NULL)
7221 size_t indx;
7222 bfd_vma tag;
7224 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7225 true);
7226 if (indx == (size_t) -1)
7227 return false;
7229 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7230 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7231 return false;
7234 if (filter_shlib != NULL)
7236 size_t indx;
7238 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7239 filter_shlib, true);
7240 if (indx == (size_t) -1
7241 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7242 return false;
7245 if (auxiliary_filters != NULL)
7247 const char * const *p;
7249 for (p = auxiliary_filters; *p != NULL; p++)
7251 size_t indx;
7253 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7254 *p, true);
7255 if (indx == (size_t) -1
7256 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7257 return false;
7261 if (audit != NULL)
7263 size_t indx;
7265 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7266 true);
7267 if (indx == (size_t) -1
7268 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7269 return false;
7272 if (depaudit != NULL)
7274 size_t indx;
7276 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7277 true);
7278 if (indx == (size_t) -1
7279 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7280 return false;
7283 eif.info = info;
7284 eif.failed = false;
7286 /* Find all symbols which were defined in a dynamic object and make
7287 the backend pick a reasonable value for them. */
7288 elf_link_hash_traverse (elf_hash_table (info),
7289 _bfd_elf_adjust_dynamic_symbol,
7290 &eif);
7291 if (eif.failed)
7292 return false;
7294 /* Add some entries to the .dynamic section. We fill in some of the
7295 values later, in bfd_elf_final_link, but we must add the entries
7296 now so that we know the final size of the .dynamic section. */
7298 /* If there are initialization and/or finalization functions to
7299 call then add the corresponding DT_INIT/DT_FINI entries. */
7300 h = (info->init_function
7301 ? elf_link_hash_lookup (elf_hash_table (info),
7302 info->init_function, false,
7303 false, false)
7304 : NULL);
7305 if (h != NULL
7306 && (h->ref_regular
7307 || h->def_regular))
7309 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7310 return false;
7312 h = (info->fini_function
7313 ? elf_link_hash_lookup (elf_hash_table (info),
7314 info->fini_function, false,
7315 false, false)
7316 : NULL);
7317 if (h != NULL
7318 && (h->ref_regular
7319 || h->def_regular))
7321 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7322 return false;
7325 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7326 if (s != NULL && s->linker_has_input)
7328 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7329 if (! bfd_link_executable (info))
7331 bfd *sub;
7332 asection *o;
7334 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7335 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7336 && (o = sub->sections) != NULL
7337 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7338 for (o = sub->sections; o != NULL; o = o->next)
7339 if (elf_section_data (o)->this_hdr.sh_type
7340 == SHT_PREINIT_ARRAY)
7342 _bfd_error_handler
7343 (_("%pB: .preinit_array section is not allowed in DSO"),
7344 sub);
7345 break;
7348 bfd_set_error (bfd_error_nonrepresentable_section);
7349 return false;
7352 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7353 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7354 return false;
7356 s = bfd_get_section_by_name (output_bfd, ".init_array");
7357 if (s != NULL && s->linker_has_input)
7359 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7360 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7361 return false;
7363 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7364 if (s != NULL && s->linker_has_input)
7366 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7367 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7368 return false;
7371 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7372 /* If .dynstr is excluded from the link, we don't want any of
7373 these tags. Strictly, we should be checking each section
7374 individually; This quick check covers for the case where
7375 someone does a /DISCARD/ : { *(*) }. */
7376 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7378 bfd_size_type strsize;
7380 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7381 if ((info->emit_hash
7382 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7383 || (info->emit_gnu_hash
7384 && (bed->record_xhash_symbol == NULL
7385 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7386 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7387 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7388 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7389 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7390 bed->s->sizeof_sym)
7391 || (info->gnu_flags_1
7392 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7393 info->gnu_flags_1)))
7394 return false;
7398 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7399 return false;
7401 /* The backend must work out the sizes of all the other dynamic
7402 sections. */
7403 if (dynobj != NULL
7404 && bed->elf_backend_size_dynamic_sections != NULL
7405 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7406 return false;
7408 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7410 if (elf_tdata (output_bfd)->cverdefs)
7412 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7414 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7415 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7416 return false;
7419 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7421 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7422 return false;
7424 else if (info->flags & DF_BIND_NOW)
7426 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7427 return false;
7430 if (info->flags_1)
7432 if (bfd_link_executable (info))
7433 info->flags_1 &= ~ (DF_1_INITFIRST
7434 | DF_1_NODELETE
7435 | DF_1_NOOPEN);
7436 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7437 return false;
7440 if (elf_tdata (output_bfd)->cverrefs)
7442 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7444 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7445 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7446 return false;
7449 if ((elf_tdata (output_bfd)->cverrefs == 0
7450 && elf_tdata (output_bfd)->cverdefs == 0)
7451 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7453 asection *s;
7455 s = bfd_get_linker_section (dynobj, ".gnu.version");
7456 s->flags |= SEC_EXCLUDE;
7459 return true;
7462 /* Find the first non-excluded output section. We'll use its
7463 section symbol for some emitted relocs. */
7464 void
7465 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7467 asection *s;
7468 asection *found = NULL;
7470 for (s = output_bfd->sections; s != NULL; s = s->next)
7471 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7472 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7474 found = s;
7475 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7476 break;
7478 elf_hash_table (info)->text_index_section = found;
7481 /* Find two non-excluded output sections, one for code, one for data.
7482 We'll use their section symbols for some emitted relocs. */
7483 void
7484 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7486 asection *s;
7487 asection *found = NULL;
7489 /* Data first, since setting text_index_section changes
7490 _bfd_elf_omit_section_dynsym_default. */
7491 for (s = output_bfd->sections; s != NULL; s = s->next)
7492 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7493 && !(s->flags & SEC_READONLY)
7494 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7496 found = s;
7497 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7498 break;
7500 elf_hash_table (info)->data_index_section = found;
7502 for (s = output_bfd->sections; s != NULL; s = s->next)
7503 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7504 && (s->flags & SEC_READONLY)
7505 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7507 found = s;
7508 break;
7510 elf_hash_table (info)->text_index_section = found;
7513 #define GNU_HASH_SECTION_NAME(bed) \
7514 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7516 bool
7517 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7519 const struct elf_backend_data *bed;
7520 unsigned long section_sym_count;
7521 bfd_size_type dynsymcount = 0;
7523 if (!is_elf_hash_table (info->hash))
7524 return true;
7526 bed = get_elf_backend_data (output_bfd);
7527 (*bed->elf_backend_init_index_section) (output_bfd, info);
7529 /* Assign dynsym indices. In a shared library we generate a section
7530 symbol for each output section, which come first. Next come all
7531 of the back-end allocated local dynamic syms, followed by the rest
7532 of the global symbols.
7534 This is usually not needed for static binaries, however backends
7535 can request to always do it, e.g. the MIPS backend uses dynamic
7536 symbol counts to lay out GOT, which will be produced in the
7537 presence of GOT relocations even in static binaries (holding fixed
7538 data in that case, to satisfy those relocations). */
7540 if (elf_hash_table (info)->dynamic_sections_created
7541 || bed->always_renumber_dynsyms)
7542 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7543 &section_sym_count);
7545 if (elf_hash_table (info)->dynamic_sections_created)
7547 bfd *dynobj;
7548 asection *s;
7549 unsigned int dtagcount;
7551 dynobj = elf_hash_table (info)->dynobj;
7553 /* Work out the size of the symbol version section. */
7554 s = bfd_get_linker_section (dynobj, ".gnu.version");
7555 BFD_ASSERT (s != NULL);
7556 if ((s->flags & SEC_EXCLUDE) == 0)
7558 s->size = dynsymcount * sizeof (Elf_External_Versym);
7559 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7560 if (s->contents == NULL)
7561 return false;
7563 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7564 return false;
7567 /* Set the size of the .dynsym and .hash sections. We counted
7568 the number of dynamic symbols in elf_link_add_object_symbols.
7569 We will build the contents of .dynsym and .hash when we build
7570 the final symbol table, because until then we do not know the
7571 correct value to give the symbols. We built the .dynstr
7572 section as we went along in elf_link_add_object_symbols. */
7573 s = elf_hash_table (info)->dynsym;
7574 BFD_ASSERT (s != NULL);
7575 s->size = dynsymcount * bed->s->sizeof_sym;
7577 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7578 if (s->contents == NULL)
7579 return false;
7581 /* The first entry in .dynsym is a dummy symbol. Clear all the
7582 section syms, in case we don't output them all. */
7583 ++section_sym_count;
7584 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7586 elf_hash_table (info)->bucketcount = 0;
7588 /* Compute the size of the hashing table. As a side effect this
7589 computes the hash values for all the names we export. */
7590 if (info->emit_hash)
7592 unsigned long int *hashcodes;
7593 struct hash_codes_info hashinf;
7594 bfd_size_type amt;
7595 unsigned long int nsyms;
7596 size_t bucketcount;
7597 size_t hash_entry_size;
7599 /* Compute the hash values for all exported symbols. At the same
7600 time store the values in an array so that we could use them for
7601 optimizations. */
7602 amt = dynsymcount * sizeof (unsigned long int);
7603 hashcodes = (unsigned long int *) bfd_malloc (amt);
7604 if (hashcodes == NULL)
7605 return false;
7606 hashinf.hashcodes = hashcodes;
7607 hashinf.error = false;
7609 /* Put all hash values in HASHCODES. */
7610 elf_link_hash_traverse (elf_hash_table (info),
7611 elf_collect_hash_codes, &hashinf);
7612 if (hashinf.error)
7614 free (hashcodes);
7615 return false;
7618 nsyms = hashinf.hashcodes - hashcodes;
7619 bucketcount
7620 = compute_bucket_count (info, hashcodes, nsyms, 0);
7621 free (hashcodes);
7623 if (bucketcount == 0 && nsyms > 0)
7624 return false;
7626 elf_hash_table (info)->bucketcount = bucketcount;
7628 s = bfd_get_linker_section (dynobj, ".hash");
7629 BFD_ASSERT (s != NULL);
7630 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7631 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7632 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7633 if (s->contents == NULL)
7634 return false;
7636 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7637 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7638 s->contents + hash_entry_size);
7641 if (info->emit_gnu_hash)
7643 size_t i, cnt;
7644 unsigned char *contents;
7645 struct collect_gnu_hash_codes cinfo;
7646 bfd_size_type amt;
7647 size_t bucketcount;
7649 memset (&cinfo, 0, sizeof (cinfo));
7651 /* Compute the hash values for all exported symbols. At the same
7652 time store the values in an array so that we could use them for
7653 optimizations. */
7654 amt = dynsymcount * 2 * sizeof (unsigned long int);
7655 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7656 if (cinfo.hashcodes == NULL)
7657 return false;
7659 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7660 cinfo.min_dynindx = -1;
7661 cinfo.output_bfd = output_bfd;
7662 cinfo.bed = bed;
7664 /* Put all hash values in HASHCODES. */
7665 elf_link_hash_traverse (elf_hash_table (info),
7666 elf_collect_gnu_hash_codes, &cinfo);
7667 if (cinfo.error)
7669 free (cinfo.hashcodes);
7670 return false;
7673 bucketcount
7674 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7676 if (bucketcount == 0)
7678 free (cinfo.hashcodes);
7679 return false;
7682 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7683 BFD_ASSERT (s != NULL);
7685 if (cinfo.nsyms == 0)
7687 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7688 BFD_ASSERT (cinfo.min_dynindx == -1);
7689 free (cinfo.hashcodes);
7690 s->size = 5 * 4 + bed->s->arch_size / 8;
7691 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7692 if (contents == NULL)
7693 return false;
7694 s->contents = contents;
7695 /* 1 empty bucket. */
7696 bfd_put_32 (output_bfd, 1, contents);
7697 /* SYMIDX above the special symbol 0. */
7698 bfd_put_32 (output_bfd, 1, contents + 4);
7699 /* Just one word for bitmask. */
7700 bfd_put_32 (output_bfd, 1, contents + 8);
7701 /* Only hash fn bloom filter. */
7702 bfd_put_32 (output_bfd, 0, contents + 12);
7703 /* No hashes are valid - empty bitmask. */
7704 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7705 /* No hashes in the only bucket. */
7706 bfd_put_32 (output_bfd, 0,
7707 contents + 16 + bed->s->arch_size / 8);
7709 else
7711 unsigned long int maskwords, maskbitslog2, x;
7712 BFD_ASSERT (cinfo.min_dynindx != -1);
7714 x = cinfo.nsyms;
7715 maskbitslog2 = 1;
7716 while ((x >>= 1) != 0)
7717 ++maskbitslog2;
7718 if (maskbitslog2 < 3)
7719 maskbitslog2 = 5;
7720 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7721 maskbitslog2 = maskbitslog2 + 3;
7722 else
7723 maskbitslog2 = maskbitslog2 + 2;
7724 if (bed->s->arch_size == 64)
7726 if (maskbitslog2 == 5)
7727 maskbitslog2 = 6;
7728 cinfo.shift1 = 6;
7730 else
7731 cinfo.shift1 = 5;
7732 cinfo.mask = (1 << cinfo.shift1) - 1;
7733 cinfo.shift2 = maskbitslog2;
7734 cinfo.maskbits = 1 << maskbitslog2;
7735 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7736 amt = bucketcount * sizeof (unsigned long int) * 2;
7737 amt += maskwords * sizeof (bfd_vma);
7738 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7739 if (cinfo.bitmask == NULL)
7741 free (cinfo.hashcodes);
7742 return false;
7745 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7746 cinfo.indx = cinfo.counts + bucketcount;
7747 cinfo.symindx = dynsymcount - cinfo.nsyms;
7748 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7750 /* Determine how often each hash bucket is used. */
7751 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7752 for (i = 0; i < cinfo.nsyms; ++i)
7753 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7755 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7756 if (cinfo.counts[i] != 0)
7758 cinfo.indx[i] = cnt;
7759 cnt += cinfo.counts[i];
7761 BFD_ASSERT (cnt == dynsymcount);
7762 cinfo.bucketcount = bucketcount;
7763 cinfo.local_indx = cinfo.min_dynindx;
7765 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7766 s->size += cinfo.maskbits / 8;
7767 if (bed->record_xhash_symbol != NULL)
7768 s->size += cinfo.nsyms * 4;
7769 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7770 if (contents == NULL)
7772 free (cinfo.bitmask);
7773 free (cinfo.hashcodes);
7774 return false;
7777 s->contents = contents;
7778 bfd_put_32 (output_bfd, bucketcount, contents);
7779 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7780 bfd_put_32 (output_bfd, maskwords, contents + 8);
7781 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7782 contents += 16 + cinfo.maskbits / 8;
7784 for (i = 0; i < bucketcount; ++i)
7786 if (cinfo.counts[i] == 0)
7787 bfd_put_32 (output_bfd, 0, contents);
7788 else
7789 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7790 contents += 4;
7793 cinfo.contents = contents;
7795 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7796 /* Renumber dynamic symbols, if populating .gnu.hash section.
7797 If using .MIPS.xhash, populate the translation table. */
7798 elf_link_hash_traverse (elf_hash_table (info),
7799 elf_gnu_hash_process_symidx, &cinfo);
7801 contents = s->contents + 16;
7802 for (i = 0; i < maskwords; ++i)
7804 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7805 contents);
7806 contents += bed->s->arch_size / 8;
7809 free (cinfo.bitmask);
7810 free (cinfo.hashcodes);
7814 s = bfd_get_linker_section (dynobj, ".dynstr");
7815 BFD_ASSERT (s != NULL);
7817 elf_finalize_dynstr (output_bfd, info);
7819 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7821 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7822 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7823 return false;
7826 return true;
7829 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7831 static void
7832 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7833 asection *sec)
7835 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7836 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7839 /* Finish SHF_MERGE section merging. */
7841 bool
7842 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7844 bfd *ibfd;
7845 asection *sec;
7847 if (!is_elf_hash_table (info->hash))
7848 return false;
7850 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7851 if ((ibfd->flags & DYNAMIC) == 0
7852 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7853 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7854 == get_elf_backend_data (obfd)->s->elfclass))
7855 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7856 if ((sec->flags & SEC_MERGE) != 0
7857 && !bfd_is_abs_section (sec->output_section))
7859 struct bfd_elf_section_data *secdata;
7861 secdata = elf_section_data (sec);
7862 if (! _bfd_add_merge_section (obfd,
7863 &elf_hash_table (info)->merge_info,
7864 sec, &secdata->sec_info))
7865 return false;
7866 else if (secdata->sec_info)
7867 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7870 if (elf_hash_table (info)->merge_info != NULL)
7871 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7872 merge_sections_remove_hook);
7873 return true;
7876 /* Create an entry in an ELF linker hash table. */
7878 struct bfd_hash_entry *
7879 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7880 struct bfd_hash_table *table,
7881 const char *string)
7883 /* Allocate the structure if it has not already been allocated by a
7884 subclass. */
7885 if (entry == NULL)
7887 entry = (struct bfd_hash_entry *)
7888 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7889 if (entry == NULL)
7890 return entry;
7893 /* Call the allocation method of the superclass. */
7894 entry = _bfd_link_hash_newfunc (entry, table, string);
7895 if (entry != NULL)
7897 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7898 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7900 /* Set local fields. */
7901 ret->indx = -1;
7902 ret->dynindx = -1;
7903 ret->got = htab->init_got_refcount;
7904 ret->plt = htab->init_plt_refcount;
7905 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7906 - offsetof (struct elf_link_hash_entry, size)));
7907 /* Assume that we have been called by a non-ELF symbol reader.
7908 This flag is then reset by the code which reads an ELF input
7909 file. This ensures that a symbol created by a non-ELF symbol
7910 reader will have the flag set correctly. */
7911 ret->non_elf = 1;
7914 return entry;
7917 /* Copy data from an indirect symbol to its direct symbol, hiding the
7918 old indirect symbol. Also used for copying flags to a weakdef. */
7920 void
7921 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7922 struct elf_link_hash_entry *dir,
7923 struct elf_link_hash_entry *ind)
7925 struct elf_link_hash_table *htab;
7927 if (ind->dyn_relocs != NULL)
7929 if (dir->dyn_relocs != NULL)
7931 struct elf_dyn_relocs **pp;
7932 struct elf_dyn_relocs *p;
7934 /* Add reloc counts against the indirect sym to the direct sym
7935 list. Merge any entries against the same section. */
7936 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7938 struct elf_dyn_relocs *q;
7940 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7941 if (q->sec == p->sec)
7943 q->pc_count += p->pc_count;
7944 q->count += p->count;
7945 *pp = p->next;
7946 break;
7948 if (q == NULL)
7949 pp = &p->next;
7951 *pp = dir->dyn_relocs;
7954 dir->dyn_relocs = ind->dyn_relocs;
7955 ind->dyn_relocs = NULL;
7958 /* Copy down any references that we may have already seen to the
7959 symbol which just became indirect. */
7961 if (dir->versioned != versioned_hidden)
7962 dir->ref_dynamic |= ind->ref_dynamic;
7963 dir->ref_regular |= ind->ref_regular;
7964 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7965 dir->non_got_ref |= ind->non_got_ref;
7966 dir->needs_plt |= ind->needs_plt;
7967 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7969 if (ind->root.type != bfd_link_hash_indirect)
7970 return;
7972 /* Copy over the global and procedure linkage table refcount entries.
7973 These may have been already set up by a check_relocs routine. */
7974 htab = elf_hash_table (info);
7975 if (ind->got.refcount > htab->init_got_refcount.refcount)
7977 if (dir->got.refcount < 0)
7978 dir->got.refcount = 0;
7979 dir->got.refcount += ind->got.refcount;
7980 ind->got.refcount = htab->init_got_refcount.refcount;
7983 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7985 if (dir->plt.refcount < 0)
7986 dir->plt.refcount = 0;
7987 dir->plt.refcount += ind->plt.refcount;
7988 ind->plt.refcount = htab->init_plt_refcount.refcount;
7991 if (ind->dynindx != -1)
7993 if (dir->dynindx != -1)
7994 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7995 dir->dynindx = ind->dynindx;
7996 dir->dynstr_index = ind->dynstr_index;
7997 ind->dynindx = -1;
7998 ind->dynstr_index = 0;
8002 void
8003 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8004 struct elf_link_hash_entry *h,
8005 bool force_local)
8007 /* STT_GNU_IFUNC symbol must go through PLT. */
8008 if (h->type != STT_GNU_IFUNC)
8010 h->plt = elf_hash_table (info)->init_plt_offset;
8011 h->needs_plt = 0;
8013 if (force_local)
8015 h->forced_local = 1;
8016 if (h->dynindx != -1)
8018 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8019 h->dynstr_index);
8020 h->dynindx = -1;
8021 h->dynstr_index = 0;
8026 /* Hide a symbol. */
8028 void
8029 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8030 struct bfd_link_info *info,
8031 struct bfd_link_hash_entry *h)
8033 if (is_elf_hash_table (info->hash))
8035 const struct elf_backend_data *bed
8036 = get_elf_backend_data (output_bfd);
8037 struct elf_link_hash_entry *eh
8038 = (struct elf_link_hash_entry *) h;
8039 bed->elf_backend_hide_symbol (info, eh, true);
8040 eh->def_dynamic = 0;
8041 eh->ref_dynamic = 0;
8042 eh->dynamic_def = 0;
8046 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8047 caller. */
8049 bool
8050 _bfd_elf_link_hash_table_init
8051 (struct elf_link_hash_table *table,
8052 bfd *abfd,
8053 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8054 struct bfd_hash_table *,
8055 const char *),
8056 unsigned int entsize,
8057 enum elf_target_id target_id)
8059 bool ret;
8060 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8062 table->init_got_refcount.refcount = can_refcount - 1;
8063 table->init_plt_refcount.refcount = can_refcount - 1;
8064 table->init_got_offset.offset = -(bfd_vma) 1;
8065 table->init_plt_offset.offset = -(bfd_vma) 1;
8066 /* The first dynamic symbol is a dummy. */
8067 table->dynsymcount = 1;
8069 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8071 table->root.type = bfd_link_elf_hash_table;
8072 table->hash_table_id = target_id;
8073 table->target_os = get_elf_backend_data (abfd)->target_os;
8075 return ret;
8078 /* Create an ELF linker hash table. */
8080 struct bfd_link_hash_table *
8081 _bfd_elf_link_hash_table_create (bfd *abfd)
8083 struct elf_link_hash_table *ret;
8084 size_t amt = sizeof (struct elf_link_hash_table);
8086 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8087 if (ret == NULL)
8088 return NULL;
8090 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8091 sizeof (struct elf_link_hash_entry),
8092 GENERIC_ELF_DATA))
8094 free (ret);
8095 return NULL;
8097 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8099 return &ret->root;
8102 /* Destroy an ELF linker hash table. */
8104 void
8105 _bfd_elf_link_hash_table_free (bfd *obfd)
8107 struct elf_link_hash_table *htab;
8109 htab = (struct elf_link_hash_table *) obfd->link.hash;
8110 if (htab->dynstr != NULL)
8111 _bfd_elf_strtab_free (htab->dynstr);
8112 _bfd_merge_sections_free (htab->merge_info);
8113 _bfd_generic_link_hash_table_free (obfd);
8116 /* This is a hook for the ELF emulation code in the generic linker to
8117 tell the backend linker what file name to use for the DT_NEEDED
8118 entry for a dynamic object. */
8120 void
8121 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8123 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8124 && bfd_get_format (abfd) == bfd_object)
8125 elf_dt_name (abfd) = name;
8129 bfd_elf_get_dyn_lib_class (bfd *abfd)
8131 int lib_class;
8132 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8133 && bfd_get_format (abfd) == bfd_object)
8134 lib_class = elf_dyn_lib_class (abfd);
8135 else
8136 lib_class = 0;
8137 return lib_class;
8140 void
8141 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8143 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8144 && bfd_get_format (abfd) == bfd_object)
8145 elf_dyn_lib_class (abfd) = lib_class;
8148 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8149 the linker ELF emulation code. */
8151 struct bfd_link_needed_list *
8152 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8153 struct bfd_link_info *info)
8155 if (! is_elf_hash_table (info->hash))
8156 return NULL;
8157 return elf_hash_table (info)->needed;
8160 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8161 hook for the linker ELF emulation code. */
8163 struct bfd_link_needed_list *
8164 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8165 struct bfd_link_info *info)
8167 if (! is_elf_hash_table (info->hash))
8168 return NULL;
8169 return elf_hash_table (info)->runpath;
8172 /* Get the name actually used for a dynamic object for a link. This
8173 is the SONAME entry if there is one. Otherwise, it is the string
8174 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8176 const char *
8177 bfd_elf_get_dt_soname (bfd *abfd)
8179 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8180 && bfd_get_format (abfd) == bfd_object)
8181 return elf_dt_name (abfd);
8182 return NULL;
8185 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8186 the ELF linker emulation code. */
8188 bool
8189 bfd_elf_get_bfd_needed_list (bfd *abfd,
8190 struct bfd_link_needed_list **pneeded)
8192 asection *s;
8193 bfd_byte *dynbuf = NULL;
8194 unsigned int elfsec;
8195 unsigned long shlink;
8196 bfd_byte *extdyn, *extdynend;
8197 size_t extdynsize;
8198 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8200 *pneeded = NULL;
8202 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8203 || bfd_get_format (abfd) != bfd_object)
8204 return true;
8206 s = bfd_get_section_by_name (abfd, ".dynamic");
8207 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8208 return true;
8210 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8211 goto error_return;
8213 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8214 if (elfsec == SHN_BAD)
8215 goto error_return;
8217 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8219 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8220 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8222 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8223 (size_t) (extdynend - extdyn) >= extdynsize;
8224 extdyn += extdynsize)
8226 Elf_Internal_Dyn dyn;
8228 (*swap_dyn_in) (abfd, extdyn, &dyn);
8230 if (dyn.d_tag == DT_NULL)
8231 break;
8233 if (dyn.d_tag == DT_NEEDED)
8235 const char *string;
8236 struct bfd_link_needed_list *l;
8237 unsigned int tagv = dyn.d_un.d_val;
8238 size_t amt;
8240 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8241 if (string == NULL)
8242 goto error_return;
8244 amt = sizeof *l;
8245 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8246 if (l == NULL)
8247 goto error_return;
8249 l->by = abfd;
8250 l->name = string;
8251 l->next = *pneeded;
8252 *pneeded = l;
8256 free (dynbuf);
8258 return true;
8260 error_return:
8261 free (dynbuf);
8262 return false;
8265 struct elf_symbuf_symbol
8267 unsigned long st_name; /* Symbol name, index in string tbl */
8268 unsigned char st_info; /* Type and binding attributes */
8269 unsigned char st_other; /* Visibilty, and target specific */
8272 struct elf_symbuf_head
8274 struct elf_symbuf_symbol *ssym;
8275 size_t count;
8276 unsigned int st_shndx;
8279 struct elf_symbol
8281 union
8283 Elf_Internal_Sym *isym;
8284 struct elf_symbuf_symbol *ssym;
8285 void *p;
8286 } u;
8287 const char *name;
8290 /* Sort references to symbols by ascending section number. */
8292 static int
8293 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8295 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8296 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8298 if (s1->st_shndx != s2->st_shndx)
8299 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8300 /* Final sort by the address of the sym in the symbuf ensures
8301 a stable sort. */
8302 if (s1 != s2)
8303 return s1 > s2 ? 1 : -1;
8304 return 0;
8307 static int
8308 elf_sym_name_compare (const void *arg1, const void *arg2)
8310 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8311 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8312 int ret = strcmp (s1->name, s2->name);
8313 if (ret != 0)
8314 return ret;
8315 if (s1->u.p != s2->u.p)
8316 return s1->u.p > s2->u.p ? 1 : -1;
8317 return 0;
8320 static struct elf_symbuf_head *
8321 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8323 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8324 struct elf_symbuf_symbol *ssym;
8325 struct elf_symbuf_head *ssymbuf, *ssymhead;
8326 size_t i, shndx_count, total_size, amt;
8328 amt = symcount * sizeof (*indbuf);
8329 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8330 if (indbuf == NULL)
8331 return NULL;
8333 for (ind = indbuf, i = 0; i < symcount; i++)
8334 if (isymbuf[i].st_shndx != SHN_UNDEF)
8335 *ind++ = &isymbuf[i];
8336 indbufend = ind;
8338 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8339 elf_sort_elf_symbol);
8341 shndx_count = 0;
8342 if (indbufend > indbuf)
8343 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8344 if (ind[0]->st_shndx != ind[1]->st_shndx)
8345 shndx_count++;
8347 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8348 + (indbufend - indbuf) * sizeof (*ssym));
8349 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8350 if (ssymbuf == NULL)
8352 free (indbuf);
8353 return NULL;
8356 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8357 ssymbuf->ssym = NULL;
8358 ssymbuf->count = shndx_count;
8359 ssymbuf->st_shndx = 0;
8360 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8362 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8364 ssymhead++;
8365 ssymhead->ssym = ssym;
8366 ssymhead->count = 0;
8367 ssymhead->st_shndx = (*ind)->st_shndx;
8369 ssym->st_name = (*ind)->st_name;
8370 ssym->st_info = (*ind)->st_info;
8371 ssym->st_other = (*ind)->st_other;
8372 ssymhead->count++;
8374 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8375 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8377 free (indbuf);
8378 return ssymbuf;
8381 /* Check if 2 sections define the same set of local and global
8382 symbols. */
8384 static bool
8385 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8386 struct bfd_link_info *info)
8388 bfd *bfd1, *bfd2;
8389 const struct elf_backend_data *bed1, *bed2;
8390 Elf_Internal_Shdr *hdr1, *hdr2;
8391 size_t symcount1, symcount2;
8392 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8393 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8394 Elf_Internal_Sym *isym, *isymend;
8395 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8396 size_t count1, count2, sec_count1, sec_count2, i;
8397 unsigned int shndx1, shndx2;
8398 bool result;
8399 bool ignore_section_symbol_p;
8401 bfd1 = sec1->owner;
8402 bfd2 = sec2->owner;
8404 /* Both sections have to be in ELF. */
8405 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8406 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8407 return false;
8409 if (elf_section_type (sec1) != elf_section_type (sec2))
8410 return false;
8412 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8413 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8414 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8415 return false;
8417 bed1 = get_elf_backend_data (bfd1);
8418 bed2 = get_elf_backend_data (bfd2);
8419 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8420 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8421 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8422 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8424 if (symcount1 == 0 || symcount2 == 0)
8425 return false;
8427 result = false;
8428 isymbuf1 = NULL;
8429 isymbuf2 = NULL;
8430 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8431 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8433 /* Ignore section symbols only when matching non-debugging sections
8434 or linkonce section with comdat section. */
8435 ignore_section_symbol_p
8436 = ((sec1->flags & SEC_DEBUGGING) == 0
8437 || ((elf_section_flags (sec1) & SHF_GROUP)
8438 != (elf_section_flags (sec2) & SHF_GROUP)));
8440 if (ssymbuf1 == NULL)
8442 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8443 NULL, NULL, NULL);
8444 if (isymbuf1 == NULL)
8445 goto done;
8447 if (info != NULL && !info->reduce_memory_overheads)
8449 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8450 elf_tdata (bfd1)->symbuf = ssymbuf1;
8454 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8456 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8457 NULL, NULL, NULL);
8458 if (isymbuf2 == NULL)
8459 goto done;
8461 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8463 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8464 elf_tdata (bfd2)->symbuf = ssymbuf2;
8468 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8470 /* Optimized faster version. */
8471 size_t lo, hi, mid;
8472 struct elf_symbol *symp;
8473 struct elf_symbuf_symbol *ssym, *ssymend;
8475 lo = 0;
8476 hi = ssymbuf1->count;
8477 ssymbuf1++;
8478 count1 = 0;
8479 sec_count1 = 0;
8480 while (lo < hi)
8482 mid = (lo + hi) / 2;
8483 if (shndx1 < ssymbuf1[mid].st_shndx)
8484 hi = mid;
8485 else if (shndx1 > ssymbuf1[mid].st_shndx)
8486 lo = mid + 1;
8487 else
8489 count1 = ssymbuf1[mid].count;
8490 ssymbuf1 += mid;
8491 break;
8494 if (ignore_section_symbol_p)
8496 for (i = 0; i < count1; i++)
8497 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8498 sec_count1++;
8499 count1 -= sec_count1;
8502 lo = 0;
8503 hi = ssymbuf2->count;
8504 ssymbuf2++;
8505 count2 = 0;
8506 sec_count2 = 0;
8507 while (lo < hi)
8509 mid = (lo + hi) / 2;
8510 if (shndx2 < ssymbuf2[mid].st_shndx)
8511 hi = mid;
8512 else if (shndx2 > ssymbuf2[mid].st_shndx)
8513 lo = mid + 1;
8514 else
8516 count2 = ssymbuf2[mid].count;
8517 ssymbuf2 += mid;
8518 break;
8521 if (ignore_section_symbol_p)
8523 for (i = 0; i < count2; i++)
8524 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8525 sec_count2++;
8526 count2 -= sec_count2;
8529 if (count1 == 0 || count2 == 0 || count1 != count2)
8530 goto done;
8532 symtable1
8533 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8534 symtable2
8535 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8536 if (symtable1 == NULL || symtable2 == NULL)
8537 goto done;
8539 symp = symtable1;
8540 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8541 ssym < ssymend; ssym++)
8542 if (sec_count1 == 0
8543 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8545 symp->u.ssym = ssym;
8546 symp->name = bfd_elf_string_from_elf_section (bfd1,
8547 hdr1->sh_link,
8548 ssym->st_name);
8549 symp++;
8552 symp = symtable2;
8553 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8554 ssym < ssymend; ssym++)
8555 if (sec_count2 == 0
8556 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8558 symp->u.ssym = ssym;
8559 symp->name = bfd_elf_string_from_elf_section (bfd2,
8560 hdr2->sh_link,
8561 ssym->st_name);
8562 symp++;
8565 /* Sort symbol by name. */
8566 qsort (symtable1, count1, sizeof (struct elf_symbol),
8567 elf_sym_name_compare);
8568 qsort (symtable2, count1, sizeof (struct elf_symbol),
8569 elf_sym_name_compare);
8571 for (i = 0; i < count1; i++)
8572 /* Two symbols must have the same binding, type and name. */
8573 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8574 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8575 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8576 goto done;
8578 result = true;
8579 goto done;
8582 symtable1 = (struct elf_symbol *)
8583 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8584 symtable2 = (struct elf_symbol *)
8585 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8586 if (symtable1 == NULL || symtable2 == NULL)
8587 goto done;
8589 /* Count definitions in the section. */
8590 count1 = 0;
8591 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8592 if (isym->st_shndx == shndx1
8593 && (!ignore_section_symbol_p
8594 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8595 symtable1[count1++].u.isym = isym;
8597 count2 = 0;
8598 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8599 if (isym->st_shndx == shndx2
8600 && (!ignore_section_symbol_p
8601 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8602 symtable2[count2++].u.isym = isym;
8604 if (count1 == 0 || count2 == 0 || count1 != count2)
8605 goto done;
8607 for (i = 0; i < count1; i++)
8608 symtable1[i].name
8609 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8610 symtable1[i].u.isym->st_name);
8612 for (i = 0; i < count2; i++)
8613 symtable2[i].name
8614 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8615 symtable2[i].u.isym->st_name);
8617 /* Sort symbol by name. */
8618 qsort (symtable1, count1, sizeof (struct elf_symbol),
8619 elf_sym_name_compare);
8620 qsort (symtable2, count1, sizeof (struct elf_symbol),
8621 elf_sym_name_compare);
8623 for (i = 0; i < count1; i++)
8624 /* Two symbols must have the same binding, type and name. */
8625 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8626 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8627 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8628 goto done;
8630 result = true;
8632 done:
8633 free (symtable1);
8634 free (symtable2);
8635 free (isymbuf1);
8636 free (isymbuf2);
8638 return result;
8641 /* Return TRUE if 2 section types are compatible. */
8643 bool
8644 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8645 bfd *bbfd, const asection *bsec)
8647 if (asec == NULL
8648 || bsec == NULL
8649 || abfd->xvec->flavour != bfd_target_elf_flavour
8650 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8651 return true;
8653 return elf_section_type (asec) == elf_section_type (bsec);
8656 /* Final phase of ELF linker. */
8658 /* A structure we use to avoid passing large numbers of arguments. */
8660 struct elf_final_link_info
8662 /* General link information. */
8663 struct bfd_link_info *info;
8664 /* Output BFD. */
8665 bfd *output_bfd;
8666 /* Symbol string table. */
8667 struct elf_strtab_hash *symstrtab;
8668 /* .hash section. */
8669 asection *hash_sec;
8670 /* symbol version section (.gnu.version). */
8671 asection *symver_sec;
8672 /* Buffer large enough to hold contents of any section. */
8673 bfd_byte *contents;
8674 /* Buffer large enough to hold external relocs of any section. */
8675 void *external_relocs;
8676 /* Buffer large enough to hold internal relocs of any section. */
8677 Elf_Internal_Rela *internal_relocs;
8678 /* Buffer large enough to hold external local symbols of any input
8679 BFD. */
8680 bfd_byte *external_syms;
8681 /* And a buffer for symbol section indices. */
8682 Elf_External_Sym_Shndx *locsym_shndx;
8683 /* Buffer large enough to hold internal local symbols of any input
8684 BFD. */
8685 Elf_Internal_Sym *internal_syms;
8686 /* Array large enough to hold a symbol index for each local symbol
8687 of any input BFD. */
8688 long *indices;
8689 /* Array large enough to hold a section pointer for each local
8690 symbol of any input BFD. */
8691 asection **sections;
8692 /* Buffer for SHT_SYMTAB_SHNDX section. */
8693 Elf_External_Sym_Shndx *symshndxbuf;
8694 /* Number of STT_FILE syms seen. */
8695 size_t filesym_count;
8696 /* Local symbol hash table. */
8697 struct bfd_hash_table local_hash_table;
8700 struct local_hash_entry
8702 /* Base hash table entry structure. */
8703 struct bfd_hash_entry root;
8704 /* Size of the local symbol name. */
8705 size_t size;
8706 /* Number of the duplicated local symbol names. */
8707 long count;
8710 /* Create an entry in the local symbol hash table. */
8712 static struct bfd_hash_entry *
8713 local_hash_newfunc (struct bfd_hash_entry *entry,
8714 struct bfd_hash_table *table,
8715 const char *string)
8718 /* Allocate the structure if it has not already been allocated by a
8719 subclass. */
8720 if (entry == NULL)
8722 entry = bfd_hash_allocate (table,
8723 sizeof (struct local_hash_entry));
8724 if (entry == NULL)
8725 return entry;
8728 /* Call the allocation method of the superclass. */
8729 entry = bfd_hash_newfunc (entry, table, string);
8730 if (entry != NULL)
8732 ((struct local_hash_entry *) entry)->count = 0;
8733 ((struct local_hash_entry *) entry)->size = 0;
8736 return entry;
8739 /* This struct is used to pass information to elf_link_output_extsym. */
8741 struct elf_outext_info
8743 bool failed;
8744 bool localsyms;
8745 bool file_sym_done;
8746 struct elf_final_link_info *flinfo;
8750 /* Support for evaluating a complex relocation.
8752 Complex relocations are generalized, self-describing relocations. The
8753 implementation of them consists of two parts: complex symbols, and the
8754 relocations themselves.
8756 The relocations use a reserved elf-wide relocation type code (R_RELC
8757 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8758 information (start bit, end bit, word width, etc) into the addend. This
8759 information is extracted from CGEN-generated operand tables within gas.
8761 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8762 internal) representing prefix-notation expressions, including but not
8763 limited to those sorts of expressions normally encoded as addends in the
8764 addend field. The symbol mangling format is:
8766 <node> := <literal>
8767 | <unary-operator> ':' <node>
8768 | <binary-operator> ':' <node> ':' <node>
8771 <literal> := 's' <digits=N> ':' <N character symbol name>
8772 | 'S' <digits=N> ':' <N character section name>
8773 | '#' <hexdigits>
8776 <binary-operator> := as in C
8777 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8779 static void
8780 set_symbol_value (bfd *bfd_with_globals,
8781 Elf_Internal_Sym *isymbuf,
8782 size_t locsymcount,
8783 size_t symidx,
8784 bfd_vma val)
8786 struct elf_link_hash_entry **sym_hashes;
8787 struct elf_link_hash_entry *h;
8788 size_t extsymoff = locsymcount;
8790 if (symidx < locsymcount)
8792 Elf_Internal_Sym *sym;
8794 sym = isymbuf + symidx;
8795 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8797 /* It is a local symbol: move it to the
8798 "absolute" section and give it a value. */
8799 sym->st_shndx = SHN_ABS;
8800 sym->st_value = val;
8801 return;
8803 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8804 extsymoff = 0;
8807 /* It is a global symbol: set its link type
8808 to "defined" and give it a value. */
8810 sym_hashes = elf_sym_hashes (bfd_with_globals);
8811 h = sym_hashes [symidx - extsymoff];
8812 while (h->root.type == bfd_link_hash_indirect
8813 || h->root.type == bfd_link_hash_warning)
8814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8815 h->root.type = bfd_link_hash_defined;
8816 h->root.u.def.value = val;
8817 h->root.u.def.section = bfd_abs_section_ptr;
8820 static bool
8821 resolve_symbol (const char *name,
8822 bfd *input_bfd,
8823 struct elf_final_link_info *flinfo,
8824 bfd_vma *result,
8825 Elf_Internal_Sym *isymbuf,
8826 size_t locsymcount)
8828 Elf_Internal_Sym *sym;
8829 struct bfd_link_hash_entry *global_entry;
8830 const char *candidate = NULL;
8831 Elf_Internal_Shdr *symtab_hdr;
8832 size_t i;
8834 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8836 for (i = 0; i < locsymcount; ++ i)
8838 sym = isymbuf + i;
8840 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8841 continue;
8843 candidate = bfd_elf_string_from_elf_section (input_bfd,
8844 symtab_hdr->sh_link,
8845 sym->st_name);
8846 #ifdef DEBUG
8847 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8848 name, candidate, (unsigned long) sym->st_value);
8849 #endif
8850 if (candidate && strcmp (candidate, name) == 0)
8852 asection *sec = flinfo->sections [i];
8854 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8855 *result += sec->output_offset + sec->output_section->vma;
8856 #ifdef DEBUG
8857 printf ("Found symbol with value %8.8lx\n",
8858 (unsigned long) *result);
8859 #endif
8860 return true;
8864 /* Hmm, haven't found it yet. perhaps it is a global. */
8865 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8866 false, false, true);
8867 if (!global_entry)
8868 return false;
8870 if (global_entry->type == bfd_link_hash_defined
8871 || global_entry->type == bfd_link_hash_defweak)
8873 *result = (global_entry->u.def.value
8874 + global_entry->u.def.section->output_section->vma
8875 + global_entry->u.def.section->output_offset);
8876 #ifdef DEBUG
8877 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8878 global_entry->root.string, (unsigned long) *result);
8879 #endif
8880 return true;
8883 return false;
8886 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8887 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8888 names like "foo.end" which is the end address of section "foo". */
8890 static bool
8891 resolve_section (const char *name,
8892 asection *sections,
8893 bfd_vma *result,
8894 bfd * abfd)
8896 asection *curr;
8897 unsigned int len;
8899 for (curr = sections; curr; curr = curr->next)
8900 if (strcmp (curr->name, name) == 0)
8902 *result = curr->vma;
8903 return true;
8906 /* Hmm. still haven't found it. try pseudo-section names. */
8907 /* FIXME: This could be coded more efficiently... */
8908 for (curr = sections; curr; curr = curr->next)
8910 len = strlen (curr->name);
8911 if (len > strlen (name))
8912 continue;
8914 if (strncmp (curr->name, name, len) == 0)
8916 if (startswith (name + len, ".end"))
8918 *result = (curr->vma
8919 + curr->size / bfd_octets_per_byte (abfd, curr));
8920 return true;
8923 /* Insert more pseudo-section names here, if you like. */
8927 return false;
8930 static void
8931 undefined_reference (const char *reftype, const char *name)
8933 /* xgettext:c-format */
8934 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8935 reftype, name);
8936 bfd_set_error (bfd_error_bad_value);
8939 static bool
8940 eval_symbol (bfd_vma *result,
8941 const char **symp,
8942 bfd *input_bfd,
8943 struct elf_final_link_info *flinfo,
8944 bfd_vma dot,
8945 Elf_Internal_Sym *isymbuf,
8946 size_t locsymcount,
8947 int signed_p)
8949 size_t len;
8950 size_t symlen;
8951 bfd_vma a;
8952 bfd_vma b;
8953 char symbuf[4096];
8954 const char *sym = *symp;
8955 const char *symend;
8956 bool symbol_is_section = false;
8958 len = strlen (sym);
8959 symend = sym + len;
8961 if (len < 1 || len > sizeof (symbuf))
8963 bfd_set_error (bfd_error_invalid_operation);
8964 return false;
8967 switch (* sym)
8969 case '.':
8970 *result = dot;
8971 *symp = sym + 1;
8972 return true;
8974 case '#':
8975 ++sym;
8976 *result = strtoul (sym, (char **) symp, 16);
8977 return true;
8979 case 'S':
8980 symbol_is_section = true;
8981 /* Fall through. */
8982 case 's':
8983 ++sym;
8984 symlen = strtol (sym, (char **) symp, 10);
8985 sym = *symp + 1; /* Skip the trailing ':'. */
8987 if (symend < sym || symlen + 1 > sizeof (symbuf))
8989 bfd_set_error (bfd_error_invalid_operation);
8990 return false;
8993 memcpy (symbuf, sym, symlen);
8994 symbuf[symlen] = '\0';
8995 *symp = sym + symlen;
8997 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8998 the symbol as a section, or vice-versa. so we're pretty liberal in our
8999 interpretation here; section means "try section first", not "must be a
9000 section", and likewise with symbol. */
9002 if (symbol_is_section)
9004 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9005 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9006 isymbuf, locsymcount))
9008 undefined_reference ("section", symbuf);
9009 return false;
9012 else
9014 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9015 isymbuf, locsymcount)
9016 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9017 result, input_bfd))
9019 undefined_reference ("symbol", symbuf);
9020 return false;
9024 return true;
9026 /* All that remains are operators. */
9028 #define UNARY_OP(op) \
9029 if (startswith (sym, #op)) \
9031 sym += strlen (#op); \
9032 if (*sym == ':') \
9033 ++sym; \
9034 *symp = sym; \
9035 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9036 isymbuf, locsymcount, signed_p)) \
9037 return false; \
9038 if (signed_p) \
9039 *result = op ((bfd_signed_vma) a); \
9040 else \
9041 *result = op a; \
9042 return true; \
9045 #define BINARY_OP_HEAD(op) \
9046 if (startswith (sym, #op)) \
9048 sym += strlen (#op); \
9049 if (*sym == ':') \
9050 ++sym; \
9051 *symp = sym; \
9052 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9053 isymbuf, locsymcount, signed_p)) \
9054 return false; \
9055 ++*symp; \
9056 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9057 isymbuf, locsymcount, signed_p)) \
9058 return false;
9059 #define BINARY_OP_TAIL(op) \
9060 if (signed_p) \
9061 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9062 else \
9063 *result = a op b; \
9064 return true; \
9066 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9068 default:
9069 UNARY_OP (0-);
9070 BINARY_OP_HEAD (<<);
9071 if (b >= sizeof (a) * CHAR_BIT)
9073 *result = 0;
9074 return true;
9076 signed_p = 0;
9077 BINARY_OP_TAIL (<<);
9078 BINARY_OP_HEAD (>>);
9079 if (b >= sizeof (a) * CHAR_BIT)
9081 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9082 return true;
9084 BINARY_OP_TAIL (>>);
9085 BINARY_OP (==);
9086 BINARY_OP (!=);
9087 BINARY_OP (<=);
9088 BINARY_OP (>=);
9089 BINARY_OP (&&);
9090 BINARY_OP (||);
9091 UNARY_OP (~);
9092 UNARY_OP (!);
9093 BINARY_OP (*);
9094 BINARY_OP_HEAD (/);
9095 if (b == 0)
9097 _bfd_error_handler (_("division by zero"));
9098 bfd_set_error (bfd_error_bad_value);
9099 return false;
9101 BINARY_OP_TAIL (/);
9102 BINARY_OP_HEAD (%);
9103 if (b == 0)
9105 _bfd_error_handler (_("division by zero"));
9106 bfd_set_error (bfd_error_bad_value);
9107 return false;
9109 BINARY_OP_TAIL (%);
9110 BINARY_OP (^);
9111 BINARY_OP (|);
9112 BINARY_OP (&);
9113 BINARY_OP (+);
9114 BINARY_OP (-);
9115 BINARY_OP (<);
9116 BINARY_OP (>);
9117 #undef UNARY_OP
9118 #undef BINARY_OP
9119 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9120 bfd_set_error (bfd_error_invalid_operation);
9121 return false;
9125 static void
9126 put_value (bfd_vma size,
9127 unsigned long chunksz,
9128 bfd *input_bfd,
9129 bfd_vma x,
9130 bfd_byte *location)
9132 location += (size - chunksz);
9134 for (; size; size -= chunksz, location -= chunksz)
9136 switch (chunksz)
9138 case 1:
9139 bfd_put_8 (input_bfd, x, location);
9140 x >>= 8;
9141 break;
9142 case 2:
9143 bfd_put_16 (input_bfd, x, location);
9144 x >>= 16;
9145 break;
9146 case 4:
9147 bfd_put_32 (input_bfd, x, location);
9148 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9149 x >>= 16;
9150 x >>= 16;
9151 break;
9152 #ifdef BFD64
9153 case 8:
9154 bfd_put_64 (input_bfd, x, location);
9155 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9156 x >>= 32;
9157 x >>= 32;
9158 break;
9159 #endif
9160 default:
9161 abort ();
9162 break;
9167 static bfd_vma
9168 get_value (bfd_vma size,
9169 unsigned long chunksz,
9170 bfd *input_bfd,
9171 bfd_byte *location)
9173 int shift;
9174 bfd_vma x = 0;
9176 /* Sanity checks. */
9177 BFD_ASSERT (chunksz <= sizeof (x)
9178 && size >= chunksz
9179 && chunksz != 0
9180 && (size % chunksz) == 0
9181 && input_bfd != NULL
9182 && location != NULL);
9184 if (chunksz == sizeof (x))
9186 BFD_ASSERT (size == chunksz);
9188 /* Make sure that we do not perform an undefined shift operation.
9189 We know that size == chunksz so there will only be one iteration
9190 of the loop below. */
9191 shift = 0;
9193 else
9194 shift = 8 * chunksz;
9196 for (; size; size -= chunksz, location += chunksz)
9198 switch (chunksz)
9200 case 1:
9201 x = (x << shift) | bfd_get_8 (input_bfd, location);
9202 break;
9203 case 2:
9204 x = (x << shift) | bfd_get_16 (input_bfd, location);
9205 break;
9206 case 4:
9207 x = (x << shift) | bfd_get_32 (input_bfd, location);
9208 break;
9209 #ifdef BFD64
9210 case 8:
9211 x = (x << shift) | bfd_get_64 (input_bfd, location);
9212 break;
9213 #endif
9214 default:
9215 abort ();
9218 return x;
9221 static void
9222 decode_complex_addend (unsigned long *start, /* in bits */
9223 unsigned long *oplen, /* in bits */
9224 unsigned long *len, /* in bits */
9225 unsigned long *wordsz, /* in bytes */
9226 unsigned long *chunksz, /* in bytes */
9227 unsigned long *lsb0_p,
9228 unsigned long *signed_p,
9229 unsigned long *trunc_p,
9230 unsigned long encoded)
9232 * start = encoded & 0x3F;
9233 * len = (encoded >> 6) & 0x3F;
9234 * oplen = (encoded >> 12) & 0x3F;
9235 * wordsz = (encoded >> 18) & 0xF;
9236 * chunksz = (encoded >> 22) & 0xF;
9237 * lsb0_p = (encoded >> 27) & 1;
9238 * signed_p = (encoded >> 28) & 1;
9239 * trunc_p = (encoded >> 29) & 1;
9242 bfd_reloc_status_type
9243 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9244 asection *input_section,
9245 bfd_byte *contents,
9246 Elf_Internal_Rela *rel,
9247 bfd_vma relocation)
9249 bfd_vma shift, x, mask;
9250 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9251 bfd_reloc_status_type r;
9252 bfd_size_type octets;
9254 /* Perform this reloc, since it is complex.
9255 (this is not to say that it necessarily refers to a complex
9256 symbol; merely that it is a self-describing CGEN based reloc.
9257 i.e. the addend has the complete reloc information (bit start, end,
9258 word size, etc) encoded within it.). */
9260 decode_complex_addend (&start, &oplen, &len, &wordsz,
9261 &chunksz, &lsb0_p, &signed_p,
9262 &trunc_p, rel->r_addend);
9264 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9266 if (lsb0_p)
9267 shift = (start + 1) - len;
9268 else
9269 shift = (8 * wordsz) - (start + len);
9271 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9272 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9274 #ifdef DEBUG
9275 printf ("Doing complex reloc: "
9276 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9277 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9278 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9279 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9280 oplen, (unsigned long) x, (unsigned long) mask,
9281 (unsigned long) relocation);
9282 #endif
9284 r = bfd_reloc_ok;
9285 if (! trunc_p)
9286 /* Now do an overflow check. */
9287 r = bfd_check_overflow ((signed_p
9288 ? complain_overflow_signed
9289 : complain_overflow_unsigned),
9290 len, 0, (8 * wordsz),
9291 relocation);
9293 /* Do the deed. */
9294 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9296 #ifdef DEBUG
9297 printf (" relocation: %8.8lx\n"
9298 " shifted mask: %8.8lx\n"
9299 " shifted/masked reloc: %8.8lx\n"
9300 " result: %8.8lx\n",
9301 (unsigned long) relocation, (unsigned long) (mask << shift),
9302 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9303 #endif
9304 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9305 return r;
9308 /* Functions to read r_offset from external (target order) reloc
9309 entry. Faster than bfd_getl32 et al, because we let the compiler
9310 know the value is aligned. */
9312 static bfd_vma
9313 ext32l_r_offset (const void *p)
9315 union aligned32
9317 uint32_t v;
9318 unsigned char c[4];
9320 const union aligned32 *a
9321 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9323 uint32_t aval = ( (uint32_t) a->c[0]
9324 | (uint32_t) a->c[1] << 8
9325 | (uint32_t) a->c[2] << 16
9326 | (uint32_t) a->c[3] << 24);
9327 return aval;
9330 static bfd_vma
9331 ext32b_r_offset (const void *p)
9333 union aligned32
9335 uint32_t v;
9336 unsigned char c[4];
9338 const union aligned32 *a
9339 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9341 uint32_t aval = ( (uint32_t) a->c[0] << 24
9342 | (uint32_t) a->c[1] << 16
9343 | (uint32_t) a->c[2] << 8
9344 | (uint32_t) a->c[3]);
9345 return aval;
9348 static bfd_vma
9349 ext64l_r_offset (const void *p)
9351 union aligned64
9353 uint64_t v;
9354 unsigned char c[8];
9356 const union aligned64 *a
9357 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9359 uint64_t aval = ( (uint64_t) a->c[0]
9360 | (uint64_t) a->c[1] << 8
9361 | (uint64_t) a->c[2] << 16
9362 | (uint64_t) a->c[3] << 24
9363 | (uint64_t) a->c[4] << 32
9364 | (uint64_t) a->c[5] << 40
9365 | (uint64_t) a->c[6] << 48
9366 | (uint64_t) a->c[7] << 56);
9367 return aval;
9370 static bfd_vma
9371 ext64b_r_offset (const void *p)
9373 union aligned64
9375 uint64_t v;
9376 unsigned char c[8];
9378 const union aligned64 *a
9379 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9381 uint64_t aval = ( (uint64_t) a->c[0] << 56
9382 | (uint64_t) a->c[1] << 48
9383 | (uint64_t) a->c[2] << 40
9384 | (uint64_t) a->c[3] << 32
9385 | (uint64_t) a->c[4] << 24
9386 | (uint64_t) a->c[5] << 16
9387 | (uint64_t) a->c[6] << 8
9388 | (uint64_t) a->c[7]);
9389 return aval;
9392 /* When performing a relocatable link, the input relocations are
9393 preserved. But, if they reference global symbols, the indices
9394 referenced must be updated. Update all the relocations found in
9395 RELDATA. */
9397 static bool
9398 elf_link_adjust_relocs (bfd *abfd,
9399 asection *sec,
9400 struct bfd_elf_section_reloc_data *reldata,
9401 bool sort,
9402 struct bfd_link_info *info)
9404 unsigned int i;
9405 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9406 bfd_byte *erela;
9407 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9408 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9409 bfd_vma r_type_mask;
9410 int r_sym_shift;
9411 unsigned int count = reldata->count;
9412 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9414 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9416 swap_in = bed->s->swap_reloc_in;
9417 swap_out = bed->s->swap_reloc_out;
9419 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9421 swap_in = bed->s->swap_reloca_in;
9422 swap_out = bed->s->swap_reloca_out;
9424 else
9425 abort ();
9427 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9428 abort ();
9430 if (bed->s->arch_size == 32)
9432 r_type_mask = 0xff;
9433 r_sym_shift = 8;
9435 else
9437 r_type_mask = 0xffffffff;
9438 r_sym_shift = 32;
9441 erela = reldata->hdr->contents;
9442 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9444 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9445 unsigned int j;
9447 if (*rel_hash == NULL)
9448 continue;
9450 if ((*rel_hash)->indx == -2
9451 && info->gc_sections
9452 && ! info->gc_keep_exported)
9454 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9455 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9456 abfd, sec,
9457 (*rel_hash)->root.root.string);
9458 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9459 abfd, sec);
9460 bfd_set_error (bfd_error_invalid_operation);
9461 return false;
9463 BFD_ASSERT ((*rel_hash)->indx >= 0);
9465 (*swap_in) (abfd, erela, irela);
9466 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9467 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9468 | (irela[j].r_info & r_type_mask));
9469 (*swap_out) (abfd, irela, erela);
9472 if (bed->elf_backend_update_relocs)
9473 (*bed->elf_backend_update_relocs) (sec, reldata);
9475 if (sort && count != 0)
9477 bfd_vma (*ext_r_off) (const void *);
9478 bfd_vma r_off;
9479 size_t elt_size;
9480 bfd_byte *base, *end, *p, *loc;
9481 bfd_byte *buf = NULL;
9483 if (bed->s->arch_size == 32)
9485 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9486 ext_r_off = ext32l_r_offset;
9487 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9488 ext_r_off = ext32b_r_offset;
9489 else
9490 abort ();
9492 else
9494 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9495 ext_r_off = ext64l_r_offset;
9496 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9497 ext_r_off = ext64b_r_offset;
9498 else
9499 abort ();
9502 /* Must use a stable sort here. A modified insertion sort,
9503 since the relocs are mostly sorted already. */
9504 elt_size = reldata->hdr->sh_entsize;
9505 base = reldata->hdr->contents;
9506 end = base + count * elt_size;
9507 if (elt_size > sizeof (Elf64_External_Rela))
9508 abort ();
9510 /* Ensure the first element is lowest. This acts as a sentinel,
9511 speeding the main loop below. */
9512 r_off = (*ext_r_off) (base);
9513 for (p = loc = base; (p += elt_size) < end; )
9515 bfd_vma r_off2 = (*ext_r_off) (p);
9516 if (r_off > r_off2)
9518 r_off = r_off2;
9519 loc = p;
9522 if (loc != base)
9524 /* Don't just swap *base and *loc as that changes the order
9525 of the original base[0] and base[1] if they happen to
9526 have the same r_offset. */
9527 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9528 memcpy (onebuf, loc, elt_size);
9529 memmove (base + elt_size, base, loc - base);
9530 memcpy (base, onebuf, elt_size);
9533 for (p = base + elt_size; (p += elt_size) < end; )
9535 /* base to p is sorted, *p is next to insert. */
9536 r_off = (*ext_r_off) (p);
9537 /* Search the sorted region for location to insert. */
9538 loc = p - elt_size;
9539 while (r_off < (*ext_r_off) (loc))
9540 loc -= elt_size;
9541 loc += elt_size;
9542 if (loc != p)
9544 /* Chances are there is a run of relocs to insert here,
9545 from one of more input files. Files are not always
9546 linked in order due to the way elf_link_input_bfd is
9547 called. See pr17666. */
9548 size_t sortlen = p - loc;
9549 bfd_vma r_off2 = (*ext_r_off) (loc);
9550 size_t runlen = elt_size;
9551 bfd_vma r_off_runend = r_off;
9552 bfd_vma r_off_runend_next;
9553 size_t buf_size = 96 * 1024;
9554 while (p + runlen < end
9555 && (sortlen <= buf_size
9556 || runlen + elt_size <= buf_size)
9557 /* run must not break the ordering of base..loc+1 */
9558 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9559 /* run must be already sorted */
9560 && r_off_runend_next >= r_off_runend)
9562 runlen += elt_size;
9563 r_off_runend = r_off_runend_next;
9565 if (buf == NULL)
9567 buf = bfd_malloc (buf_size);
9568 if (buf == NULL)
9569 return false;
9571 if (runlen < sortlen)
9573 memcpy (buf, p, runlen);
9574 memmove (loc + runlen, loc, sortlen);
9575 memcpy (loc, buf, runlen);
9577 else
9579 memcpy (buf, loc, sortlen);
9580 memmove (loc, p, runlen);
9581 memcpy (loc + runlen, buf, sortlen);
9583 p += runlen - elt_size;
9586 /* Hashes are no longer valid. */
9587 free (reldata->hashes);
9588 reldata->hashes = NULL;
9589 free (buf);
9591 return true;
9594 struct elf_link_sort_rela
9596 union {
9597 bfd_vma offset;
9598 bfd_vma sym_mask;
9599 } u;
9600 enum elf_reloc_type_class type;
9601 /* We use this as an array of size int_rels_per_ext_rel. */
9602 Elf_Internal_Rela rela[1];
9605 /* qsort stability here and for cmp2 is only an issue if multiple
9606 dynamic relocations are emitted at the same address. But targets
9607 that apply a series of dynamic relocations each operating on the
9608 result of the prior relocation can't use -z combreloc as
9609 implemented anyway. Such schemes tend to be broken by sorting on
9610 symbol index. That leaves dynamic NONE relocs as the only other
9611 case where ld might emit multiple relocs at the same address, and
9612 those are only emitted due to target bugs. */
9614 static int
9615 elf_link_sort_cmp1 (const void *A, const void *B)
9617 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9618 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9619 int relativea, relativeb;
9621 relativea = a->type == reloc_class_relative;
9622 relativeb = b->type == reloc_class_relative;
9624 if (relativea < relativeb)
9625 return 1;
9626 if (relativea > relativeb)
9627 return -1;
9628 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9629 return -1;
9630 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9631 return 1;
9632 if (a->rela->r_offset < b->rela->r_offset)
9633 return -1;
9634 if (a->rela->r_offset > b->rela->r_offset)
9635 return 1;
9636 return 0;
9639 static int
9640 elf_link_sort_cmp2 (const void *A, const void *B)
9642 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9643 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9645 if (a->type < b->type)
9646 return -1;
9647 if (a->type > b->type)
9648 return 1;
9649 if (a->u.offset < b->u.offset)
9650 return -1;
9651 if (a->u.offset > b->u.offset)
9652 return 1;
9653 if (a->rela->r_offset < b->rela->r_offset)
9654 return -1;
9655 if (a->rela->r_offset > b->rela->r_offset)
9656 return 1;
9657 return 0;
9660 static size_t
9661 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9663 asection *dynamic_relocs;
9664 asection *rela_dyn;
9665 asection *rel_dyn;
9666 bfd_size_type count, size;
9667 size_t i, ret, sort_elt, ext_size;
9668 bfd_byte *sort, *s_non_relative, *p;
9669 struct elf_link_sort_rela *sq;
9670 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9671 int i2e = bed->s->int_rels_per_ext_rel;
9672 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9673 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9674 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9675 struct bfd_link_order *lo;
9676 bfd_vma r_sym_mask;
9677 bool use_rela;
9679 /* Find a dynamic reloc section. */
9680 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9681 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9682 if (rela_dyn != NULL && rela_dyn->size > 0
9683 && rel_dyn != NULL && rel_dyn->size > 0)
9685 bool use_rela_initialised = false;
9687 /* This is just here to stop gcc from complaining.
9688 Its initialization checking code is not perfect. */
9689 use_rela = true;
9691 /* Both sections are present. Examine the sizes
9692 of the indirect sections to help us choose. */
9693 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9694 if (lo->type == bfd_indirect_link_order)
9696 asection *o = lo->u.indirect.section;
9698 if ((o->size % bed->s->sizeof_rela) == 0)
9700 if ((o->size % bed->s->sizeof_rel) == 0)
9701 /* Section size is divisible by both rel and rela sizes.
9702 It is of no help to us. */
9704 else
9706 /* Section size is only divisible by rela. */
9707 if (use_rela_initialised && !use_rela)
9709 _bfd_error_handler (_("%pB: unable to sort relocs - "
9710 "they are in more than one size"),
9711 abfd);
9712 bfd_set_error (bfd_error_invalid_operation);
9713 return 0;
9715 else
9717 use_rela = true;
9718 use_rela_initialised = true;
9722 else if ((o->size % bed->s->sizeof_rel) == 0)
9724 /* Section size is only divisible by rel. */
9725 if (use_rela_initialised && use_rela)
9727 _bfd_error_handler (_("%pB: unable to sort relocs - "
9728 "they are in more than one size"),
9729 abfd);
9730 bfd_set_error (bfd_error_invalid_operation);
9731 return 0;
9733 else
9735 use_rela = false;
9736 use_rela_initialised = true;
9739 else
9741 /* The section size is not divisible by either -
9742 something is wrong. */
9743 _bfd_error_handler (_("%pB: unable to sort relocs - "
9744 "they are of an unknown size"), abfd);
9745 bfd_set_error (bfd_error_invalid_operation);
9746 return 0;
9750 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9751 if (lo->type == bfd_indirect_link_order)
9753 asection *o = lo->u.indirect.section;
9755 if ((o->size % bed->s->sizeof_rela) == 0)
9757 if ((o->size % bed->s->sizeof_rel) == 0)
9758 /* Section size is divisible by both rel and rela sizes.
9759 It is of no help to us. */
9761 else
9763 /* Section size is only divisible by rela. */
9764 if (use_rela_initialised && !use_rela)
9766 _bfd_error_handler (_("%pB: unable to sort relocs - "
9767 "they are in more than one size"),
9768 abfd);
9769 bfd_set_error (bfd_error_invalid_operation);
9770 return 0;
9772 else
9774 use_rela = true;
9775 use_rela_initialised = true;
9779 else if ((o->size % bed->s->sizeof_rel) == 0)
9781 /* Section size is only divisible by rel. */
9782 if (use_rela_initialised && use_rela)
9784 _bfd_error_handler (_("%pB: unable to sort relocs - "
9785 "they are in more than one size"),
9786 abfd);
9787 bfd_set_error (bfd_error_invalid_operation);
9788 return 0;
9790 else
9792 use_rela = false;
9793 use_rela_initialised = true;
9796 else
9798 /* The section size is not divisible by either -
9799 something is wrong. */
9800 _bfd_error_handler (_("%pB: unable to sort relocs - "
9801 "they are of an unknown size"), abfd);
9802 bfd_set_error (bfd_error_invalid_operation);
9803 return 0;
9807 if (! use_rela_initialised)
9808 /* Make a guess. */
9809 use_rela = true;
9811 else if (rela_dyn != NULL && rela_dyn->size > 0)
9812 use_rela = true;
9813 else if (rel_dyn != NULL && rel_dyn->size > 0)
9814 use_rela = false;
9815 else
9816 return 0;
9818 if (use_rela)
9820 dynamic_relocs = rela_dyn;
9821 ext_size = bed->s->sizeof_rela;
9822 swap_in = bed->s->swap_reloca_in;
9823 swap_out = bed->s->swap_reloca_out;
9825 else
9827 dynamic_relocs = rel_dyn;
9828 ext_size = bed->s->sizeof_rel;
9829 swap_in = bed->s->swap_reloc_in;
9830 swap_out = bed->s->swap_reloc_out;
9833 size = 0;
9834 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9835 if (lo->type == bfd_indirect_link_order)
9836 size += lo->u.indirect.section->size;
9838 if (size != dynamic_relocs->size)
9839 return 0;
9841 sort_elt = (sizeof (struct elf_link_sort_rela)
9842 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9844 count = dynamic_relocs->size / ext_size;
9845 if (count == 0)
9846 return 0;
9847 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9849 if (sort == NULL)
9851 (*info->callbacks->warning)
9852 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9853 return 0;
9856 if (bed->s->arch_size == 32)
9857 r_sym_mask = ~(bfd_vma) 0xff;
9858 else
9859 r_sym_mask = ~(bfd_vma) 0xffffffff;
9861 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9862 if (lo->type == bfd_indirect_link_order)
9864 bfd_byte *erel, *erelend;
9865 asection *o = lo->u.indirect.section;
9867 if (o->contents == NULL && o->size != 0)
9869 /* This is a reloc section that is being handled as a normal
9870 section. See bfd_section_from_shdr. We can't combine
9871 relocs in this case. */
9872 free (sort);
9873 return 0;
9875 erel = o->contents;
9876 erelend = o->contents + o->size;
9877 p = sort + o->output_offset * opb / ext_size * sort_elt;
9879 while (erel < erelend)
9881 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9883 (*swap_in) (abfd, erel, s->rela);
9884 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9885 s->u.sym_mask = r_sym_mask;
9886 p += sort_elt;
9887 erel += ext_size;
9891 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9893 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9895 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9896 if (s->type != reloc_class_relative)
9897 break;
9899 ret = i;
9900 s_non_relative = p;
9902 sq = (struct elf_link_sort_rela *) s_non_relative;
9903 for (; i < count; i++, p += sort_elt)
9905 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9906 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9907 sq = sp;
9908 sp->u.offset = sq->rela->r_offset;
9911 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9913 struct elf_link_hash_table *htab = elf_hash_table (info);
9914 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9916 /* We have plt relocs in .rela.dyn. */
9917 sq = (struct elf_link_sort_rela *) sort;
9918 for (i = 0; i < count; i++)
9919 if (sq[count - i - 1].type != reloc_class_plt)
9920 break;
9921 if (i != 0 && htab->srelplt->size == i * ext_size)
9923 struct bfd_link_order **plo;
9924 /* Put srelplt link_order last. This is so the output_offset
9925 set in the next loop is correct for DT_JMPREL. */
9926 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9927 if ((*plo)->type == bfd_indirect_link_order
9928 && (*plo)->u.indirect.section == htab->srelplt)
9930 lo = *plo;
9931 *plo = lo->next;
9933 else
9934 plo = &(*plo)->next;
9935 *plo = lo;
9936 lo->next = NULL;
9937 dynamic_relocs->map_tail.link_order = lo;
9941 p = sort;
9942 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9943 if (lo->type == bfd_indirect_link_order)
9945 bfd_byte *erel, *erelend;
9946 asection *o = lo->u.indirect.section;
9948 erel = o->contents;
9949 erelend = o->contents + o->size;
9950 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9951 while (erel < erelend)
9953 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9954 (*swap_out) (abfd, s->rela, erel);
9955 p += sort_elt;
9956 erel += ext_size;
9960 free (sort);
9961 *psec = dynamic_relocs;
9962 return ret;
9965 /* Add a symbol to the output symbol string table. */
9967 static int
9968 elf_link_output_symstrtab (void *finf,
9969 const char *name,
9970 Elf_Internal_Sym *elfsym,
9971 asection *input_sec,
9972 struct elf_link_hash_entry *h)
9974 struct elf_final_link_info *flinfo = finf;
9975 int (*output_symbol_hook)
9976 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9977 struct elf_link_hash_entry *);
9978 struct elf_link_hash_table *hash_table;
9979 const struct elf_backend_data *bed;
9980 bfd_size_type strtabsize;
9982 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9984 bed = get_elf_backend_data (flinfo->output_bfd);
9985 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9986 if (output_symbol_hook != NULL)
9988 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9989 if (ret != 1)
9990 return ret;
9993 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9994 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9995 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9996 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9998 if (name == NULL || *name == '\0')
9999 elfsym->st_name = (unsigned long) -1;
10000 else
10002 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10003 to get the final offset for st_name. */
10004 char *versioned_name = (char *) name;
10005 if (h != NULL)
10007 if (h->versioned == versioned && h->def_dynamic)
10009 /* Keep only one '@' for versioned symbols defined in
10010 shared objects. */
10011 char *version = strrchr (name, ELF_VER_CHR);
10012 char *base_end = strchr (name, ELF_VER_CHR);
10013 if (version != base_end)
10015 size_t base_len;
10016 size_t len = strlen (name);
10017 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10018 if (versioned_name == NULL)
10019 return 0;
10020 base_len = base_end - name;
10021 memcpy (versioned_name, name, base_len);
10022 memcpy (versioned_name + base_len, version,
10023 len - base_len);
10027 else if (flinfo->info->unique_symbol
10028 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10030 struct local_hash_entry *lh;
10031 size_t count_len;
10032 size_t base_len;
10033 char buf[30];
10034 switch (ELF_ST_TYPE (elfsym->st_info))
10036 case STT_FILE:
10037 case STT_SECTION:
10038 break;
10039 default:
10040 lh = (struct local_hash_entry *) bfd_hash_lookup
10041 (&flinfo->local_hash_table, name, true, false);
10042 if (lh == NULL)
10043 return 0;
10044 /* Always append ".COUNT" to local symbols to avoid
10045 potential conflicts with local symbol "XXX.COUNT". */
10046 sprintf (buf, "%lx", lh->count);
10047 base_len = lh->size;
10048 if (!base_len)
10050 base_len = strlen (name);
10051 lh->size = base_len;
10053 count_len = strlen (buf);
10054 versioned_name = bfd_alloc (flinfo->output_bfd,
10055 base_len + count_len + 2);
10056 if (versioned_name == NULL)
10057 return 0;
10058 memcpy (versioned_name, name, base_len);
10059 versioned_name[base_len] = '.';
10060 memcpy (versioned_name + base_len + 1, buf,
10061 count_len + 1);
10062 lh->count++;
10063 break;
10066 elfsym->st_name
10067 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10068 versioned_name, false);
10069 if (elfsym->st_name == (unsigned long) -1)
10070 return 0;
10073 hash_table = elf_hash_table (flinfo->info);
10074 strtabsize = hash_table->strtabsize;
10075 if (strtabsize <= flinfo->output_bfd->symcount)
10077 strtabsize += strtabsize;
10078 hash_table->strtabsize = strtabsize;
10079 strtabsize *= sizeof (*hash_table->strtab);
10080 hash_table->strtab
10081 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10082 strtabsize);
10083 if (hash_table->strtab == NULL)
10084 return 0;
10086 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10087 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10088 = flinfo->output_bfd->symcount;
10089 flinfo->output_bfd->symcount += 1;
10091 return 1;
10094 /* Swap symbols out to the symbol table and flush the output symbols to
10095 the file. */
10097 static bool
10098 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10100 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10101 size_t amt;
10102 size_t i;
10103 const struct elf_backend_data *bed;
10104 bfd_byte *symbuf;
10105 Elf_Internal_Shdr *hdr;
10106 file_ptr pos;
10107 bool ret;
10109 if (flinfo->output_bfd->symcount == 0)
10110 return true;
10112 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10114 bed = get_elf_backend_data (flinfo->output_bfd);
10116 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10117 symbuf = (bfd_byte *) bfd_malloc (amt);
10118 if (symbuf == NULL)
10119 return false;
10121 if (flinfo->symshndxbuf)
10123 amt = sizeof (Elf_External_Sym_Shndx);
10124 amt *= bfd_get_symcount (flinfo->output_bfd);
10125 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10126 if (flinfo->symshndxbuf == NULL)
10128 free (symbuf);
10129 return false;
10133 /* Now swap out the symbols. */
10134 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10136 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10137 if (elfsym->sym.st_name == (unsigned long) -1)
10138 elfsym->sym.st_name = 0;
10139 else
10140 elfsym->sym.st_name
10141 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10142 elfsym->sym.st_name);
10144 /* Inform the linker of the addition of this symbol. */
10146 if (flinfo->info->callbacks->ctf_new_symbol)
10147 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10148 &elfsym->sym);
10150 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10151 ((bfd_byte *) symbuf
10152 + (elfsym->dest_index
10153 * bed->s->sizeof_sym)),
10154 NPTR_ADD (flinfo->symshndxbuf,
10155 elfsym->dest_index));
10158 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10159 pos = hdr->sh_offset + hdr->sh_size;
10160 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10161 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10162 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10164 hdr->sh_size += amt;
10165 ret = true;
10167 else
10168 ret = false;
10170 free (symbuf);
10172 free (hash_table->strtab);
10173 hash_table->strtab = NULL;
10175 return ret;
10178 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10180 static bool
10181 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10183 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10184 && sym->st_shndx < SHN_LORESERVE)
10186 /* The gABI doesn't support dynamic symbols in output sections
10187 beyond 64k. */
10188 _bfd_error_handler
10189 /* xgettext:c-format */
10190 (_("%pB: too many sections: %d (>= %d)"),
10191 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10192 bfd_set_error (bfd_error_nonrepresentable_section);
10193 return false;
10195 return true;
10198 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10199 allowing an unsatisfied unversioned symbol in the DSO to match a
10200 versioned symbol that would normally require an explicit version.
10201 We also handle the case that a DSO references a hidden symbol
10202 which may be satisfied by a versioned symbol in another DSO. */
10204 static bool
10205 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10206 const struct elf_backend_data *bed,
10207 struct elf_link_hash_entry *h)
10209 bfd *abfd;
10210 struct elf_link_loaded_list *loaded;
10212 if (!is_elf_hash_table (info->hash))
10213 return false;
10215 /* Check indirect symbol. */
10216 while (h->root.type == bfd_link_hash_indirect)
10217 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10219 switch (h->root.type)
10221 default:
10222 abfd = NULL;
10223 break;
10225 case bfd_link_hash_undefined:
10226 case bfd_link_hash_undefweak:
10227 abfd = h->root.u.undef.abfd;
10228 if (abfd == NULL
10229 || (abfd->flags & DYNAMIC) == 0
10230 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10231 return false;
10232 break;
10234 case bfd_link_hash_defined:
10235 case bfd_link_hash_defweak:
10236 abfd = h->root.u.def.section->owner;
10237 break;
10239 case bfd_link_hash_common:
10240 abfd = h->root.u.c.p->section->owner;
10241 break;
10243 BFD_ASSERT (abfd != NULL);
10245 for (loaded = elf_hash_table (info)->dyn_loaded;
10246 loaded != NULL;
10247 loaded = loaded->next)
10249 bfd *input;
10250 Elf_Internal_Shdr *hdr;
10251 size_t symcount;
10252 size_t extsymcount;
10253 size_t extsymoff;
10254 Elf_Internal_Shdr *versymhdr;
10255 Elf_Internal_Sym *isym;
10256 Elf_Internal_Sym *isymend;
10257 Elf_Internal_Sym *isymbuf;
10258 Elf_External_Versym *ever;
10259 Elf_External_Versym *extversym;
10261 input = loaded->abfd;
10263 /* We check each DSO for a possible hidden versioned definition. */
10264 if (input == abfd
10265 || elf_dynversym (input) == 0)
10266 continue;
10268 hdr = &elf_tdata (input)->dynsymtab_hdr;
10270 symcount = hdr->sh_size / bed->s->sizeof_sym;
10271 if (elf_bad_symtab (input))
10273 extsymcount = symcount;
10274 extsymoff = 0;
10276 else
10278 extsymcount = symcount - hdr->sh_info;
10279 extsymoff = hdr->sh_info;
10282 if (extsymcount == 0)
10283 continue;
10285 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10286 NULL, NULL, NULL);
10287 if (isymbuf == NULL)
10288 return false;
10290 /* Read in any version definitions. */
10291 versymhdr = &elf_tdata (input)->dynversym_hdr;
10292 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10293 || (extversym = (Elf_External_Versym *)
10294 _bfd_malloc_and_read (input, versymhdr->sh_size,
10295 versymhdr->sh_size)) == NULL)
10297 free (isymbuf);
10298 return false;
10301 ever = extversym + extsymoff;
10302 isymend = isymbuf + extsymcount;
10303 for (isym = isymbuf; isym < isymend; isym++, ever++)
10305 const char *name;
10306 Elf_Internal_Versym iver;
10307 unsigned short version_index;
10309 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10310 || isym->st_shndx == SHN_UNDEF)
10311 continue;
10313 name = bfd_elf_string_from_elf_section (input,
10314 hdr->sh_link,
10315 isym->st_name);
10316 if (strcmp (name, h->root.root.string) != 0)
10317 continue;
10319 _bfd_elf_swap_versym_in (input, ever, &iver);
10321 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10322 && !(h->def_regular
10323 && h->forced_local))
10325 /* If we have a non-hidden versioned sym, then it should
10326 have provided a definition for the undefined sym unless
10327 it is defined in a non-shared object and forced local.
10329 abort ();
10332 version_index = iver.vs_vers & VERSYM_VERSION;
10333 if (version_index == 1 || version_index == 2)
10335 /* This is the base or first version. We can use it. */
10336 free (extversym);
10337 free (isymbuf);
10338 return true;
10342 free (extversym);
10343 free (isymbuf);
10346 return false;
10349 /* Convert ELF common symbol TYPE. */
10351 static int
10352 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10354 /* Commom symbol can only appear in relocatable link. */
10355 if (!bfd_link_relocatable (info))
10356 abort ();
10357 switch (info->elf_stt_common)
10359 case unchanged:
10360 break;
10361 case elf_stt_common:
10362 type = STT_COMMON;
10363 break;
10364 case no_elf_stt_common:
10365 type = STT_OBJECT;
10366 break;
10368 return type;
10371 /* Add an external symbol to the symbol table. This is called from
10372 the hash table traversal routine. When generating a shared object,
10373 we go through the symbol table twice. The first time we output
10374 anything that might have been forced to local scope in a version
10375 script. The second time we output the symbols that are still
10376 global symbols. */
10378 static bool
10379 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10381 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10382 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10383 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10384 bool strip;
10385 Elf_Internal_Sym sym;
10386 asection *input_sec;
10387 const struct elf_backend_data *bed;
10388 long indx;
10389 int ret;
10390 unsigned int type;
10392 if (h->root.type == bfd_link_hash_warning)
10394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10395 if (h->root.type == bfd_link_hash_new)
10396 return true;
10399 /* Decide whether to output this symbol in this pass. */
10400 if (eoinfo->localsyms)
10402 if (!h->forced_local)
10403 return true;
10405 else
10407 if (h->forced_local)
10408 return true;
10411 bed = get_elf_backend_data (flinfo->output_bfd);
10413 if (h->root.type == bfd_link_hash_undefined)
10415 /* If we have an undefined symbol reference here then it must have
10416 come from a shared library that is being linked in. (Undefined
10417 references in regular files have already been handled unless
10418 they are in unreferenced sections which are removed by garbage
10419 collection). */
10420 bool ignore_undef = false;
10422 /* Some symbols may be special in that the fact that they're
10423 undefined can be safely ignored - let backend determine that. */
10424 if (bed->elf_backend_ignore_undef_symbol)
10425 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10427 /* If we are reporting errors for this situation then do so now. */
10428 if (!ignore_undef
10429 && h->ref_dynamic_nonweak
10430 && (!h->ref_regular || flinfo->info->gc_sections)
10431 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10432 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10434 flinfo->info->callbacks->undefined_symbol
10435 (flinfo->info, h->root.root.string,
10436 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10437 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10438 && !flinfo->info->warn_unresolved_syms);
10441 /* Strip a global symbol defined in a discarded section. */
10442 if (h->indx == -3)
10443 return true;
10446 /* We should also warn if a forced local symbol is referenced from
10447 shared libraries. */
10448 if (bfd_link_executable (flinfo->info)
10449 && h->forced_local
10450 && h->ref_dynamic
10451 && h->def_regular
10452 && !h->dynamic_def
10453 && h->ref_dynamic_nonweak
10454 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10456 bfd *def_bfd;
10457 const char *msg;
10458 struct elf_link_hash_entry *hi = h;
10460 /* Check indirect symbol. */
10461 while (hi->root.type == bfd_link_hash_indirect)
10462 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10464 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10465 /* xgettext:c-format */
10466 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10467 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10468 /* xgettext:c-format */
10469 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10470 else
10471 /* xgettext:c-format */
10472 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10473 def_bfd = flinfo->output_bfd;
10474 if (hi->root.u.def.section != bfd_abs_section_ptr)
10475 def_bfd = hi->root.u.def.section->owner;
10476 _bfd_error_handler (msg, flinfo->output_bfd,
10477 h->root.root.string, def_bfd);
10478 bfd_set_error (bfd_error_bad_value);
10479 eoinfo->failed = true;
10480 return false;
10483 /* We don't want to output symbols that have never been mentioned by
10484 a regular file, or that we have been told to strip. However, if
10485 h->indx is set to -2, the symbol is used by a reloc and we must
10486 output it. */
10487 strip = false;
10488 if (h->indx == -2)
10490 else if ((h->def_dynamic
10491 || h->ref_dynamic
10492 || h->root.type == bfd_link_hash_new)
10493 && !h->def_regular
10494 && !h->ref_regular)
10495 strip = true;
10496 else if (flinfo->info->strip == strip_all)
10497 strip = true;
10498 else if (flinfo->info->strip == strip_some
10499 && bfd_hash_lookup (flinfo->info->keep_hash,
10500 h->root.root.string, false, false) == NULL)
10501 strip = true;
10502 else if ((h->root.type == bfd_link_hash_defined
10503 || h->root.type == bfd_link_hash_defweak)
10504 && ((flinfo->info->strip_discarded
10505 && discarded_section (h->root.u.def.section))
10506 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10507 && h->root.u.def.section->owner != NULL
10508 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10509 strip = true;
10510 else if ((h->root.type == bfd_link_hash_undefined
10511 || h->root.type == bfd_link_hash_undefweak)
10512 && h->root.u.undef.abfd != NULL
10513 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10514 strip = true;
10516 type = h->type;
10518 /* If we're stripping it, and it's not a dynamic symbol, there's
10519 nothing else to do. However, if it is a forced local symbol or
10520 an ifunc symbol we need to give the backend finish_dynamic_symbol
10521 function a chance to make it dynamic. */
10522 if (strip
10523 && h->dynindx == -1
10524 && type != STT_GNU_IFUNC
10525 && !h->forced_local)
10526 return true;
10528 sym.st_value = 0;
10529 sym.st_size = h->size;
10530 sym.st_other = h->other;
10531 switch (h->root.type)
10533 default:
10534 case bfd_link_hash_new:
10535 case bfd_link_hash_warning:
10536 abort ();
10537 return false;
10539 case bfd_link_hash_undefined:
10540 case bfd_link_hash_undefweak:
10541 input_sec = bfd_und_section_ptr;
10542 sym.st_shndx = SHN_UNDEF;
10543 break;
10545 case bfd_link_hash_defined:
10546 case bfd_link_hash_defweak:
10548 input_sec = h->root.u.def.section;
10549 if (input_sec->output_section != NULL)
10551 sym.st_shndx =
10552 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10553 input_sec->output_section);
10554 if (sym.st_shndx == SHN_BAD)
10556 _bfd_error_handler
10557 /* xgettext:c-format */
10558 (_("%pB: could not find output section %pA for input section %pA"),
10559 flinfo->output_bfd, input_sec->output_section, input_sec);
10560 bfd_set_error (bfd_error_nonrepresentable_section);
10561 eoinfo->failed = true;
10562 return false;
10565 /* ELF symbols in relocatable files are section relative,
10566 but in nonrelocatable files they are virtual
10567 addresses. */
10568 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10569 if (!bfd_link_relocatable (flinfo->info))
10571 sym.st_value += input_sec->output_section->vma;
10572 if (h->type == STT_TLS)
10574 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10575 if (tls_sec != NULL)
10576 sym.st_value -= tls_sec->vma;
10580 else
10582 BFD_ASSERT (input_sec->owner == NULL
10583 || (input_sec->owner->flags & DYNAMIC) != 0);
10584 sym.st_shndx = SHN_UNDEF;
10585 input_sec = bfd_und_section_ptr;
10588 break;
10590 case bfd_link_hash_common:
10591 input_sec = h->root.u.c.p->section;
10592 sym.st_shndx = bed->common_section_index (input_sec);
10593 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10594 break;
10596 case bfd_link_hash_indirect:
10597 /* These symbols are created by symbol versioning. They point
10598 to the decorated version of the name. For example, if the
10599 symbol foo@@GNU_1.2 is the default, which should be used when
10600 foo is used with no version, then we add an indirect symbol
10601 foo which points to foo@@GNU_1.2. We ignore these symbols,
10602 since the indirected symbol is already in the hash table. */
10603 return true;
10606 if (type == STT_COMMON || type == STT_OBJECT)
10607 switch (h->root.type)
10609 case bfd_link_hash_common:
10610 type = elf_link_convert_common_type (flinfo->info, type);
10611 break;
10612 case bfd_link_hash_defined:
10613 case bfd_link_hash_defweak:
10614 if (bed->common_definition (&sym))
10615 type = elf_link_convert_common_type (flinfo->info, type);
10616 else
10617 type = STT_OBJECT;
10618 break;
10619 case bfd_link_hash_undefined:
10620 case bfd_link_hash_undefweak:
10621 break;
10622 default:
10623 abort ();
10626 if (h->forced_local)
10628 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10629 /* Turn off visibility on local symbol. */
10630 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10632 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10633 else if (h->unique_global && h->def_regular)
10634 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10635 else if (h->root.type == bfd_link_hash_undefweak
10636 || h->root.type == bfd_link_hash_defweak)
10637 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10638 else
10639 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10640 sym.st_target_internal = h->target_internal;
10642 /* Give the processor backend a chance to tweak the symbol value,
10643 and also to finish up anything that needs to be done for this
10644 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10645 forced local syms when non-shared is due to a historical quirk.
10646 STT_GNU_IFUNC symbol must go through PLT. */
10647 if ((h->type == STT_GNU_IFUNC
10648 && h->def_regular
10649 && !bfd_link_relocatable (flinfo->info))
10650 || ((h->dynindx != -1
10651 || h->forced_local)
10652 && ((bfd_link_pic (flinfo->info)
10653 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10654 || h->root.type != bfd_link_hash_undefweak))
10655 || !h->forced_local)
10656 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10658 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10659 (flinfo->output_bfd, flinfo->info, h, &sym)))
10661 eoinfo->failed = true;
10662 return false;
10666 /* If we are marking the symbol as undefined, and there are no
10667 non-weak references to this symbol from a regular object, then
10668 mark the symbol as weak undefined; if there are non-weak
10669 references, mark the symbol as strong. We can't do this earlier,
10670 because it might not be marked as undefined until the
10671 finish_dynamic_symbol routine gets through with it. */
10672 if (sym.st_shndx == SHN_UNDEF
10673 && h->ref_regular
10674 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10675 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10677 int bindtype;
10678 type = ELF_ST_TYPE (sym.st_info);
10680 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10681 if (type == STT_GNU_IFUNC)
10682 type = STT_FUNC;
10684 if (h->ref_regular_nonweak)
10685 bindtype = STB_GLOBAL;
10686 else
10687 bindtype = STB_WEAK;
10688 sym.st_info = ELF_ST_INFO (bindtype, type);
10691 /* If this is a symbol defined in a dynamic library, don't use the
10692 symbol size from the dynamic library. Relinking an executable
10693 against a new library may introduce gratuitous changes in the
10694 executable's symbols if we keep the size. */
10695 if (sym.st_shndx == SHN_UNDEF
10696 && !h->def_regular
10697 && h->def_dynamic)
10698 sym.st_size = 0;
10700 /* If a non-weak symbol with non-default visibility is not defined
10701 locally, it is a fatal error. */
10702 if (!bfd_link_relocatable (flinfo->info)
10703 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10704 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10705 && h->root.type == bfd_link_hash_undefined
10706 && !h->def_regular)
10708 const char *msg;
10710 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10711 /* xgettext:c-format */
10712 msg = _("%pB: protected symbol `%s' isn't defined");
10713 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10714 /* xgettext:c-format */
10715 msg = _("%pB: internal symbol `%s' isn't defined");
10716 else
10717 /* xgettext:c-format */
10718 msg = _("%pB: hidden symbol `%s' isn't defined");
10719 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10720 bfd_set_error (bfd_error_bad_value);
10721 eoinfo->failed = true;
10722 return false;
10725 /* If this symbol should be put in the .dynsym section, then put it
10726 there now. We already know the symbol index. We also fill in
10727 the entry in the .hash section. */
10728 if (h->dynindx != -1
10729 && elf_hash_table (flinfo->info)->dynamic_sections_created
10730 && elf_hash_table (flinfo->info)->dynsym != NULL
10731 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10733 bfd_byte *esym;
10735 /* Since there is no version information in the dynamic string,
10736 if there is no version info in symbol version section, we will
10737 have a run-time problem if not linking executable, referenced
10738 by shared library, or not bound locally. */
10739 if (h->verinfo.verdef == NULL
10740 && (!bfd_link_executable (flinfo->info)
10741 || h->ref_dynamic
10742 || !h->def_regular))
10744 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10746 if (p && p [1] != '\0')
10748 _bfd_error_handler
10749 /* xgettext:c-format */
10750 (_("%pB: no symbol version section for versioned symbol `%s'"),
10751 flinfo->output_bfd, h->root.root.string);
10752 eoinfo->failed = true;
10753 return false;
10757 sym.st_name = h->dynstr_index;
10758 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10759 + h->dynindx * bed->s->sizeof_sym);
10760 if (!check_dynsym (flinfo->output_bfd, &sym))
10762 eoinfo->failed = true;
10763 return false;
10766 /* Inform the linker of the addition of this symbol. */
10768 if (flinfo->info->callbacks->ctf_new_dynsym)
10769 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10771 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10773 if (flinfo->hash_sec != NULL)
10775 size_t hash_entry_size;
10776 bfd_byte *bucketpos;
10777 bfd_vma chain;
10778 size_t bucketcount;
10779 size_t bucket;
10781 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10782 bucket = h->u.elf_hash_value % bucketcount;
10784 hash_entry_size
10785 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10786 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10787 + (bucket + 2) * hash_entry_size);
10788 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10789 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10790 bucketpos);
10791 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10792 ((bfd_byte *) flinfo->hash_sec->contents
10793 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10796 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10798 Elf_Internal_Versym iversym;
10799 Elf_External_Versym *eversym;
10801 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10803 if (h->verinfo.verdef == NULL
10804 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10805 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10806 iversym.vs_vers = 1;
10807 else
10808 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10810 else
10812 if (h->verinfo.vertree == NULL)
10813 iversym.vs_vers = 1;
10814 else
10815 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10816 if (flinfo->info->create_default_symver)
10817 iversym.vs_vers++;
10820 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10821 defined locally. */
10822 if (h->versioned == versioned_hidden && h->def_regular)
10823 iversym.vs_vers |= VERSYM_HIDDEN;
10825 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10826 eversym += h->dynindx;
10827 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10831 /* If the symbol is undefined, and we didn't output it to .dynsym,
10832 strip it from .symtab too. Obviously we can't do this for
10833 relocatable output or when needed for --emit-relocs. */
10834 else if (input_sec == bfd_und_section_ptr
10835 && h->indx != -2
10836 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10837 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10838 && !bfd_link_relocatable (flinfo->info))
10839 return true;
10841 /* Also strip others that we couldn't earlier due to dynamic symbol
10842 processing. */
10843 if (strip)
10844 return true;
10845 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10846 return true;
10848 /* Output a FILE symbol so that following locals are not associated
10849 with the wrong input file. We need one for forced local symbols
10850 if we've seen more than one FILE symbol or when we have exactly
10851 one FILE symbol but global symbols are present in a file other
10852 than the one with the FILE symbol. We also need one if linker
10853 defined symbols are present. In practice these conditions are
10854 always met, so just emit the FILE symbol unconditionally. */
10855 if (eoinfo->localsyms
10856 && !eoinfo->file_sym_done
10857 && eoinfo->flinfo->filesym_count != 0)
10859 Elf_Internal_Sym fsym;
10861 memset (&fsym, 0, sizeof (fsym));
10862 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10863 fsym.st_shndx = SHN_ABS;
10864 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10865 bfd_und_section_ptr, NULL))
10866 return false;
10868 eoinfo->file_sym_done = true;
10871 indx = bfd_get_symcount (flinfo->output_bfd);
10872 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10873 input_sec, h);
10874 if (ret == 0)
10876 eoinfo->failed = true;
10877 return false;
10879 else if (ret == 1)
10880 h->indx = indx;
10881 else if (h->indx == -2)
10882 abort();
10884 return true;
10887 /* Return TRUE if special handling is done for relocs in SEC against
10888 symbols defined in discarded sections. */
10890 static bool
10891 elf_section_ignore_discarded_relocs (asection *sec)
10893 const struct elf_backend_data *bed;
10895 switch (sec->sec_info_type)
10897 case SEC_INFO_TYPE_STABS:
10898 case SEC_INFO_TYPE_EH_FRAME:
10899 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10900 case SEC_INFO_TYPE_SFRAME:
10901 return true;
10902 default:
10903 break;
10906 bed = get_elf_backend_data (sec->owner);
10907 if (bed->elf_backend_ignore_discarded_relocs != NULL
10908 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10909 return true;
10911 return false;
10914 /* Return a mask saying how ld should treat relocations in SEC against
10915 symbols defined in discarded sections. If this function returns
10916 COMPLAIN set, ld will issue a warning message. If this function
10917 returns PRETEND set, and the discarded section was link-once and the
10918 same size as the kept link-once section, ld will pretend that the
10919 symbol was actually defined in the kept section. Otherwise ld will
10920 zero the reloc (at least that is the intent, but some cooperation by
10921 the target dependent code is needed, particularly for REL targets). */
10923 unsigned int
10924 _bfd_elf_default_action_discarded (asection *sec)
10926 const struct elf_backend_data *bed;
10927 bed = get_elf_backend_data (sec->owner);
10929 if (sec->flags & SEC_DEBUGGING)
10930 return PRETEND;
10932 if (strcmp (".eh_frame", sec->name) == 0)
10933 return 0;
10935 if (bed->elf_backend_can_make_multiple_eh_frame
10936 && strncmp (sec->name, ".eh_frame.", 10) == 0)
10937 return 0;
10939 if (strcmp (".sframe", sec->name) == 0)
10940 return 0;
10942 if (strcmp (".gcc_except_table", sec->name) == 0)
10943 return 0;
10945 return COMPLAIN | PRETEND;
10948 /* Find a match between a section and a member of a section group. */
10950 static asection *
10951 match_group_member (asection *sec, asection *group,
10952 struct bfd_link_info *info)
10954 asection *first = elf_next_in_group (group);
10955 asection *s = first;
10957 while (s != NULL)
10959 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10960 return s;
10962 s = elf_next_in_group (s);
10963 if (s == first)
10964 break;
10967 return NULL;
10970 /* Check if the kept section of a discarded section SEC can be used
10971 to replace it. Return the replacement if it is OK. Otherwise return
10972 NULL. */
10974 asection *
10975 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10977 asection *kept;
10979 kept = sec->kept_section;
10980 if (kept != NULL)
10982 if ((kept->flags & SEC_GROUP) != 0)
10983 kept = match_group_member (sec, kept, info);
10984 if (kept != NULL)
10986 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10987 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10988 kept = NULL;
10989 else
10991 /* Get the real kept section. */
10992 asection *next;
10993 for (next = kept->kept_section;
10994 next != NULL;
10995 next = next->kept_section)
10996 kept = next;
10999 sec->kept_section = kept;
11001 return kept;
11004 /* Link an input file into the linker output file. This function
11005 handles all the sections and relocations of the input file at once.
11006 This is so that we only have to read the local symbols once, and
11007 don't have to keep them in memory. */
11009 static bool
11010 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11012 int (*relocate_section)
11013 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11014 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11015 bfd *output_bfd;
11016 Elf_Internal_Shdr *symtab_hdr;
11017 size_t locsymcount;
11018 size_t extsymoff;
11019 Elf_Internal_Sym *isymbuf;
11020 Elf_Internal_Sym *isym;
11021 Elf_Internal_Sym *isymend;
11022 long *pindex;
11023 asection **ppsection;
11024 asection *o;
11025 const struct elf_backend_data *bed;
11026 struct elf_link_hash_entry **sym_hashes;
11027 bfd_size_type address_size;
11028 bfd_vma r_type_mask;
11029 int r_sym_shift;
11030 bool have_file_sym = false;
11032 output_bfd = flinfo->output_bfd;
11033 bed = get_elf_backend_data (output_bfd);
11034 relocate_section = bed->elf_backend_relocate_section;
11036 /* If this is a dynamic object, we don't want to do anything here:
11037 we don't want the local symbols, and we don't want the section
11038 contents. */
11039 if ((input_bfd->flags & DYNAMIC) != 0)
11040 return true;
11042 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11043 if (elf_bad_symtab (input_bfd))
11045 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11046 extsymoff = 0;
11048 else
11050 locsymcount = symtab_hdr->sh_info;
11051 extsymoff = symtab_hdr->sh_info;
11054 /* Enable GNU OSABI features in the output BFD that are used in the input
11055 BFD. */
11056 if (bed->elf_osabi == ELFOSABI_NONE
11057 || bed->elf_osabi == ELFOSABI_GNU
11058 || bed->elf_osabi == ELFOSABI_FREEBSD)
11059 elf_tdata (output_bfd)->has_gnu_osabi
11060 |= (elf_tdata (input_bfd)->has_gnu_osabi
11061 & (bfd_link_relocatable (flinfo->info)
11062 ? -1 : ~elf_gnu_osabi_retain));
11064 /* Read the local symbols. */
11065 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11066 if (isymbuf == NULL && locsymcount != 0)
11068 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11069 flinfo->internal_syms,
11070 flinfo->external_syms,
11071 flinfo->locsym_shndx);
11072 if (isymbuf == NULL)
11073 return false;
11076 /* Find local symbol sections and adjust values of symbols in
11077 SEC_MERGE sections. Write out those local symbols we know are
11078 going into the output file. */
11079 isymend = PTR_ADD (isymbuf, locsymcount);
11080 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11081 isym < isymend;
11082 isym++, pindex++, ppsection++)
11084 asection *isec;
11085 const char *name;
11086 Elf_Internal_Sym osym;
11087 long indx;
11088 int ret;
11090 *pindex = -1;
11092 if (elf_bad_symtab (input_bfd))
11094 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11096 *ppsection = NULL;
11097 continue;
11101 if (isym->st_shndx == SHN_UNDEF)
11102 isec = bfd_und_section_ptr;
11103 else if (isym->st_shndx == SHN_ABS)
11104 isec = bfd_abs_section_ptr;
11105 else if (isym->st_shndx == SHN_COMMON)
11106 isec = bfd_com_section_ptr;
11107 else
11109 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11110 if (isec == NULL)
11112 /* Don't attempt to output symbols with st_shnx in the
11113 reserved range other than SHN_ABS and SHN_COMMON. */
11114 isec = bfd_und_section_ptr;
11116 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11117 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11118 isym->st_value =
11119 _bfd_merged_section_offset (output_bfd, &isec,
11120 elf_section_data (isec)->sec_info,
11121 isym->st_value);
11124 *ppsection = isec;
11126 /* Don't output the first, undefined, symbol. In fact, don't
11127 output any undefined local symbol. */
11128 if (isec == bfd_und_section_ptr)
11129 continue;
11131 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11133 /* We never output section symbols. Instead, we use the
11134 section symbol of the corresponding section in the output
11135 file. */
11136 continue;
11139 /* If we are stripping all symbols, we don't want to output this
11140 one. */
11141 if (flinfo->info->strip == strip_all)
11142 continue;
11144 /* If we are discarding all local symbols, we don't want to
11145 output this one. If we are generating a relocatable output
11146 file, then some of the local symbols may be required by
11147 relocs; we output them below as we discover that they are
11148 needed. */
11149 if (flinfo->info->discard == discard_all)
11150 continue;
11152 /* If this symbol is defined in a section which we are
11153 discarding, we don't need to keep it. */
11154 if (isym->st_shndx < SHN_LORESERVE
11155 && (isec->output_section == NULL
11156 || bfd_section_removed_from_list (output_bfd,
11157 isec->output_section)))
11158 continue;
11160 /* Get the name of the symbol. */
11161 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11162 isym->st_name);
11163 if (name == NULL)
11164 return false;
11166 /* See if we are discarding symbols with this name. */
11167 if ((flinfo->info->strip == strip_some
11168 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11169 == NULL))
11170 || (((flinfo->info->discard == discard_sec_merge
11171 && (isec->flags & SEC_MERGE)
11172 && !bfd_link_relocatable (flinfo->info))
11173 || flinfo->info->discard == discard_l)
11174 && bfd_is_local_label_name (input_bfd, name)))
11175 continue;
11177 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11179 if (input_bfd->lto_output)
11180 /* -flto puts a temp file name here. This means builds
11181 are not reproducible. Discard the symbol. */
11182 continue;
11183 have_file_sym = true;
11184 flinfo->filesym_count += 1;
11186 if (!have_file_sym)
11188 /* In the absence of debug info, bfd_find_nearest_line uses
11189 FILE symbols to determine the source file for local
11190 function symbols. Provide a FILE symbol here if input
11191 files lack such, so that their symbols won't be
11192 associated with a previous input file. It's not the
11193 source file, but the best we can do. */
11194 const char *filename;
11195 have_file_sym = true;
11196 flinfo->filesym_count += 1;
11197 memset (&osym, 0, sizeof (osym));
11198 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11199 osym.st_shndx = SHN_ABS;
11200 if (input_bfd->lto_output)
11201 filename = NULL;
11202 else
11203 filename = lbasename (bfd_get_filename (input_bfd));
11204 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11205 bfd_abs_section_ptr, NULL))
11206 return false;
11209 osym = *isym;
11211 /* Adjust the section index for the output file. */
11212 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11213 isec->output_section);
11214 if (osym.st_shndx == SHN_BAD)
11215 return false;
11217 /* ELF symbols in relocatable files are section relative, but
11218 in executable files they are virtual addresses. Note that
11219 this code assumes that all ELF sections have an associated
11220 BFD section with a reasonable value for output_offset; below
11221 we assume that they also have a reasonable value for
11222 output_section. Any special sections must be set up to meet
11223 these requirements. */
11224 osym.st_value += isec->output_offset;
11225 if (!bfd_link_relocatable (flinfo->info))
11227 osym.st_value += isec->output_section->vma;
11228 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11230 /* STT_TLS symbols are relative to PT_TLS segment base. */
11231 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11232 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11233 else
11234 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11235 STT_NOTYPE);
11239 indx = bfd_get_symcount (output_bfd);
11240 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11241 if (ret == 0)
11242 return false;
11243 else if (ret == 1)
11244 *pindex = indx;
11247 if (bed->s->arch_size == 32)
11249 r_type_mask = 0xff;
11250 r_sym_shift = 8;
11251 address_size = 4;
11253 else
11255 r_type_mask = 0xffffffff;
11256 r_sym_shift = 32;
11257 address_size = 8;
11260 /* Relocate the contents of each section. */
11261 sym_hashes = elf_sym_hashes (input_bfd);
11262 for (o = input_bfd->sections; o != NULL; o = o->next)
11264 bfd_byte *contents;
11266 if (! o->linker_mark)
11268 /* This section was omitted from the link. */
11269 continue;
11272 if (!flinfo->info->resolve_section_groups
11273 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11275 /* Deal with the group signature symbol. */
11276 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11277 unsigned long symndx = sec_data->this_hdr.sh_info;
11278 asection *osec = o->output_section;
11280 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11281 if (symndx >= locsymcount
11282 || (elf_bad_symtab (input_bfd)
11283 && flinfo->sections[symndx] == NULL))
11285 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11286 while (h->root.type == bfd_link_hash_indirect
11287 || h->root.type == bfd_link_hash_warning)
11288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11289 /* Arrange for symbol to be output. */
11290 h->indx = -2;
11291 elf_section_data (osec)->this_hdr.sh_info = -2;
11293 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11295 /* We'll use the output section target_index. */
11296 asection *sec = flinfo->sections[symndx]->output_section;
11297 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11299 else
11301 if (flinfo->indices[symndx] == -1)
11303 /* Otherwise output the local symbol now. */
11304 Elf_Internal_Sym sym = isymbuf[symndx];
11305 asection *sec = flinfo->sections[symndx]->output_section;
11306 const char *name;
11307 long indx;
11308 int ret;
11310 name = bfd_elf_string_from_elf_section (input_bfd,
11311 symtab_hdr->sh_link,
11312 sym.st_name);
11313 if (name == NULL)
11314 return false;
11316 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11317 sec);
11318 if (sym.st_shndx == SHN_BAD)
11319 return false;
11321 sym.st_value += o->output_offset;
11323 indx = bfd_get_symcount (output_bfd);
11324 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11325 NULL);
11326 if (ret == 0)
11327 return false;
11328 else if (ret == 1)
11329 flinfo->indices[symndx] = indx;
11330 else
11331 abort ();
11333 elf_section_data (osec)->this_hdr.sh_info
11334 = flinfo->indices[symndx];
11338 if ((o->flags & SEC_HAS_CONTENTS) == 0
11339 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11340 continue;
11342 if ((o->flags & SEC_LINKER_CREATED) != 0)
11344 /* Section was created by _bfd_elf_link_create_dynamic_sections
11345 or somesuch. */
11346 continue;
11349 /* Get the contents of the section. They have been cached by a
11350 relaxation routine. Note that o is a section in an input
11351 file, so the contents field will not have been set by any of
11352 the routines which work on output files. */
11353 if (elf_section_data (o)->this_hdr.contents != NULL)
11355 contents = elf_section_data (o)->this_hdr.contents;
11356 if (bed->caches_rawsize
11357 && o->rawsize != 0
11358 && o->rawsize < o->size)
11360 memcpy (flinfo->contents, contents, o->rawsize);
11361 contents = flinfo->contents;
11364 else if (!(o->flags & SEC_RELOC)
11365 && !bed->elf_backend_write_section
11366 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11367 /* A MERGE section that has no relocations doesn't need the
11368 contents anymore, they have been recorded earlier. Except
11369 if the backend has special provisions for writing sections. */
11370 contents = NULL;
11371 else
11373 contents = flinfo->contents;
11374 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11375 return false;
11378 if ((o->flags & SEC_RELOC) != 0)
11380 Elf_Internal_Rela *internal_relocs;
11381 Elf_Internal_Rela *rel, *relend;
11382 int action_discarded;
11383 int ret;
11385 /* Get the swapped relocs. */
11386 internal_relocs
11387 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11388 flinfo->external_relocs,
11389 flinfo->internal_relocs,
11390 false);
11391 if (internal_relocs == NULL
11392 && o->reloc_count > 0)
11393 return false;
11395 action_discarded = -1;
11396 if (!elf_section_ignore_discarded_relocs (o))
11397 action_discarded = (*bed->action_discarded) (o);
11399 /* Run through the relocs evaluating complex reloc symbols and
11400 looking for relocs against symbols from discarded sections
11401 or section symbols from removed link-once sections.
11402 Complain about relocs against discarded sections. Zero
11403 relocs against removed link-once sections. */
11405 rel = internal_relocs;
11406 relend = rel + o->reloc_count;
11407 for ( ; rel < relend; rel++)
11409 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11410 unsigned int s_type;
11411 asection **ps, *sec;
11412 struct elf_link_hash_entry *h = NULL;
11413 const char *sym_name;
11415 if (r_symndx == STN_UNDEF)
11416 continue;
11418 if (r_symndx >= locsymcount
11419 || (elf_bad_symtab (input_bfd)
11420 && flinfo->sections[r_symndx] == NULL))
11422 h = sym_hashes[r_symndx - extsymoff];
11424 /* Badly formatted input files can contain relocs that
11425 reference non-existant symbols. Check here so that
11426 we do not seg fault. */
11427 if (h == NULL)
11429 _bfd_error_handler
11430 /* xgettext:c-format */
11431 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11432 "that references a non-existent global symbol"),
11433 input_bfd, (uint64_t) rel->r_info, o);
11434 bfd_set_error (bfd_error_bad_value);
11435 return false;
11438 while (h->root.type == bfd_link_hash_indirect
11439 || h->root.type == bfd_link_hash_warning)
11440 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11442 s_type = h->type;
11444 /* If a plugin symbol is referenced from a non-IR file,
11445 mark the symbol as undefined. Note that the
11446 linker may attach linker created dynamic sections
11447 to the plugin bfd. Symbols defined in linker
11448 created sections are not plugin symbols. */
11449 if ((h->root.non_ir_ref_regular
11450 || h->root.non_ir_ref_dynamic)
11451 && (h->root.type == bfd_link_hash_defined
11452 || h->root.type == bfd_link_hash_defweak)
11453 && (h->root.u.def.section->flags
11454 & SEC_LINKER_CREATED) == 0
11455 && h->root.u.def.section->owner != NULL
11456 && (h->root.u.def.section->owner->flags
11457 & BFD_PLUGIN) != 0)
11459 h->root.type = bfd_link_hash_undefined;
11460 h->root.u.undef.abfd = h->root.u.def.section->owner;
11463 ps = NULL;
11464 if (h->root.type == bfd_link_hash_defined
11465 || h->root.type == bfd_link_hash_defweak)
11466 ps = &h->root.u.def.section;
11468 sym_name = h->root.root.string;
11470 else
11472 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11474 s_type = ELF_ST_TYPE (sym->st_info);
11475 ps = &flinfo->sections[r_symndx];
11476 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11477 sym, *ps);
11480 if ((s_type == STT_RELC || s_type == STT_SRELC)
11481 && !bfd_link_relocatable (flinfo->info))
11483 bfd_vma val;
11484 bfd_vma dot = (rel->r_offset
11485 + o->output_offset + o->output_section->vma);
11486 #ifdef DEBUG
11487 printf ("Encountered a complex symbol!");
11488 printf (" (input_bfd %s, section %s, reloc %ld\n",
11489 bfd_get_filename (input_bfd), o->name,
11490 (long) (rel - internal_relocs));
11491 printf (" symbol: idx %8.8lx, name %s\n",
11492 r_symndx, sym_name);
11493 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11494 (unsigned long) rel->r_info,
11495 (unsigned long) rel->r_offset);
11496 #endif
11497 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11498 isymbuf, locsymcount, s_type == STT_SRELC))
11499 return false;
11501 /* Symbol evaluated OK. Update to absolute value. */
11502 set_symbol_value (input_bfd, isymbuf, locsymcount,
11503 r_symndx, val);
11504 continue;
11507 if (action_discarded != -1 && ps != NULL)
11509 /* Complain if the definition comes from a
11510 discarded section. */
11511 if ((sec = *ps) != NULL && discarded_section (sec))
11513 BFD_ASSERT (r_symndx != STN_UNDEF);
11514 if (action_discarded & COMPLAIN)
11515 (*flinfo->info->callbacks->einfo)
11516 /* xgettext:c-format */
11517 (_("%X`%s' referenced in section `%pA' of %pB: "
11518 "defined in discarded section `%pA' of %pB\n"),
11519 sym_name, o, input_bfd, sec, sec->owner);
11521 /* Try to do the best we can to support buggy old
11522 versions of gcc. Pretend that the symbol is
11523 really defined in the kept linkonce section.
11524 FIXME: This is quite broken. Modifying the
11525 symbol here means we will be changing all later
11526 uses of the symbol, not just in this section. */
11527 if (action_discarded & PRETEND)
11529 asection *kept;
11531 kept = _bfd_elf_check_kept_section (sec,
11532 flinfo->info);
11533 if (kept != NULL)
11535 *ps = kept;
11536 continue;
11543 /* Relocate the section by invoking a back end routine.
11545 The back end routine is responsible for adjusting the
11546 section contents as necessary, and (if using Rela relocs
11547 and generating a relocatable output file) adjusting the
11548 reloc addend as necessary.
11550 The back end routine does not have to worry about setting
11551 the reloc address or the reloc symbol index.
11553 The back end routine is given a pointer to the swapped in
11554 internal symbols, and can access the hash table entries
11555 for the external symbols via elf_sym_hashes (input_bfd).
11557 When generating relocatable output, the back end routine
11558 must handle STB_LOCAL/STT_SECTION symbols specially. The
11559 output symbol is going to be a section symbol
11560 corresponding to the output section, which will require
11561 the addend to be adjusted. */
11563 ret = (*relocate_section) (output_bfd, flinfo->info,
11564 input_bfd, o, contents,
11565 internal_relocs,
11566 isymbuf,
11567 flinfo->sections);
11568 if (!ret)
11569 return false;
11571 if (ret == 2
11572 || bfd_link_relocatable (flinfo->info)
11573 || flinfo->info->emitrelocations)
11575 Elf_Internal_Rela *irela;
11576 Elf_Internal_Rela *irelaend, *irelamid;
11577 bfd_vma last_offset;
11578 struct elf_link_hash_entry **rel_hash;
11579 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11580 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11581 unsigned int next_erel;
11582 bool rela_normal;
11583 struct bfd_elf_section_data *esdi, *esdo;
11585 esdi = elf_section_data (o);
11586 esdo = elf_section_data (o->output_section);
11587 rela_normal = false;
11589 /* Adjust the reloc addresses and symbol indices. */
11591 irela = internal_relocs;
11592 irelaend = irela + o->reloc_count;
11593 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11594 /* We start processing the REL relocs, if any. When we reach
11595 IRELAMID in the loop, we switch to the RELA relocs. */
11596 irelamid = irela;
11597 if (esdi->rel.hdr != NULL)
11598 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11599 * bed->s->int_rels_per_ext_rel);
11600 rel_hash_list = rel_hash;
11601 rela_hash_list = NULL;
11602 last_offset = o->output_offset;
11603 if (!bfd_link_relocatable (flinfo->info))
11604 last_offset += o->output_section->vma;
11605 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11607 unsigned long r_symndx;
11608 asection *sec;
11609 Elf_Internal_Sym sym;
11611 if (next_erel == bed->s->int_rels_per_ext_rel)
11613 rel_hash++;
11614 next_erel = 0;
11617 if (irela == irelamid)
11619 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11620 rela_hash_list = rel_hash;
11621 rela_normal = bed->rela_normal;
11624 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11625 flinfo->info, o,
11626 irela->r_offset);
11627 if (irela->r_offset >= (bfd_vma) -2)
11629 /* This is a reloc for a deleted entry or somesuch.
11630 Turn it into an R_*_NONE reloc, at the same
11631 offset as the last reloc. elf_eh_frame.c and
11632 bfd_elf_discard_info rely on reloc offsets
11633 being ordered. */
11634 irela->r_offset = last_offset;
11635 irela->r_info = 0;
11636 irela->r_addend = 0;
11637 continue;
11640 irela->r_offset += o->output_offset;
11642 /* Relocs in an executable have to be virtual addresses. */
11643 if (!bfd_link_relocatable (flinfo->info))
11644 irela->r_offset += o->output_section->vma;
11646 last_offset = irela->r_offset;
11648 r_symndx = irela->r_info >> r_sym_shift;
11649 if (r_symndx == STN_UNDEF)
11650 continue;
11652 if (r_symndx >= locsymcount
11653 || (elf_bad_symtab (input_bfd)
11654 && flinfo->sections[r_symndx] == NULL))
11656 struct elf_link_hash_entry *rh;
11657 unsigned long indx;
11659 /* This is a reloc against a global symbol. We
11660 have not yet output all the local symbols, so
11661 we do not know the symbol index of any global
11662 symbol. We set the rel_hash entry for this
11663 reloc to point to the global hash table entry
11664 for this symbol. The symbol index is then
11665 set at the end of bfd_elf_final_link. */
11666 indx = r_symndx - extsymoff;
11667 rh = elf_sym_hashes (input_bfd)[indx];
11668 while (rh->root.type == bfd_link_hash_indirect
11669 || rh->root.type == bfd_link_hash_warning)
11670 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11672 /* Setting the index to -2 tells
11673 elf_link_output_extsym that this symbol is
11674 used by a reloc. */
11675 BFD_ASSERT (rh->indx < 0);
11676 rh->indx = -2;
11677 *rel_hash = rh;
11679 continue;
11682 /* This is a reloc against a local symbol. */
11684 *rel_hash = NULL;
11685 sym = isymbuf[r_symndx];
11686 sec = flinfo->sections[r_symndx];
11687 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11689 /* I suppose the backend ought to fill in the
11690 section of any STT_SECTION symbol against a
11691 processor specific section. */
11692 r_symndx = STN_UNDEF;
11693 if (bfd_is_abs_section (sec))
11695 else if (sec == NULL || sec->owner == NULL)
11697 bfd_set_error (bfd_error_bad_value);
11698 return false;
11700 else
11702 asection *osec = sec->output_section;
11704 /* If we have discarded a section, the output
11705 section will be the absolute section. In
11706 case of discarded SEC_MERGE sections, use
11707 the kept section. relocate_section should
11708 have already handled discarded linkonce
11709 sections. */
11710 if (bfd_is_abs_section (osec)
11711 && sec->kept_section != NULL
11712 && sec->kept_section->output_section != NULL)
11714 osec = sec->kept_section->output_section;
11715 irela->r_addend -= osec->vma;
11718 if (!bfd_is_abs_section (osec))
11720 r_symndx = osec->target_index;
11721 if (r_symndx == STN_UNDEF)
11723 irela->r_addend += osec->vma;
11724 osec = _bfd_nearby_section (output_bfd, osec,
11725 osec->vma);
11726 irela->r_addend -= osec->vma;
11727 r_symndx = osec->target_index;
11732 /* Adjust the addend according to where the
11733 section winds up in the output section. */
11734 if (rela_normal)
11735 irela->r_addend += sec->output_offset;
11737 else
11739 if (flinfo->indices[r_symndx] == -1)
11741 unsigned long shlink;
11742 const char *name;
11743 asection *osec;
11744 long indx;
11746 if (flinfo->info->strip == strip_all)
11748 /* You can't do ld -r -s. */
11749 bfd_set_error (bfd_error_invalid_operation);
11750 return false;
11753 /* This symbol was skipped earlier, but
11754 since it is needed by a reloc, we
11755 must output it now. */
11756 shlink = symtab_hdr->sh_link;
11757 name = (bfd_elf_string_from_elf_section
11758 (input_bfd, shlink, sym.st_name));
11759 if (name == NULL)
11760 return false;
11762 osec = sec->output_section;
11763 sym.st_shndx =
11764 _bfd_elf_section_from_bfd_section (output_bfd,
11765 osec);
11766 if (sym.st_shndx == SHN_BAD)
11767 return false;
11769 sym.st_value += sec->output_offset;
11770 if (!bfd_link_relocatable (flinfo->info))
11772 sym.st_value += osec->vma;
11773 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11775 struct elf_link_hash_table *htab
11776 = elf_hash_table (flinfo->info);
11778 /* STT_TLS symbols are relative to PT_TLS
11779 segment base. */
11780 if (htab->tls_sec != NULL)
11781 sym.st_value -= htab->tls_sec->vma;
11782 else
11783 sym.st_info
11784 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11785 STT_NOTYPE);
11789 indx = bfd_get_symcount (output_bfd);
11790 ret = elf_link_output_symstrtab (flinfo, name,
11791 &sym, sec,
11792 NULL);
11793 if (ret == 0)
11794 return false;
11795 else if (ret == 1)
11796 flinfo->indices[r_symndx] = indx;
11797 else
11798 abort ();
11801 r_symndx = flinfo->indices[r_symndx];
11804 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11805 | (irela->r_info & r_type_mask));
11808 /* Swap out the relocs. */
11809 input_rel_hdr = esdi->rel.hdr;
11810 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11812 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11813 input_rel_hdr,
11814 internal_relocs,
11815 rel_hash_list))
11816 return false;
11817 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11818 * bed->s->int_rels_per_ext_rel);
11819 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11822 input_rela_hdr = esdi->rela.hdr;
11823 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11825 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11826 input_rela_hdr,
11827 internal_relocs,
11828 rela_hash_list))
11829 return false;
11834 /* Write out the modified section contents. */
11835 if (bed->elf_backend_write_section
11836 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11837 contents))
11839 /* Section written out. */
11841 else switch (o->sec_info_type)
11843 case SEC_INFO_TYPE_STABS:
11844 if (! (_bfd_write_section_stabs
11845 (output_bfd,
11846 &elf_hash_table (flinfo->info)->stab_info,
11847 o, &elf_section_data (o)->sec_info, contents)))
11848 return false;
11849 break;
11850 case SEC_INFO_TYPE_MERGE:
11851 if (! _bfd_write_merged_section (output_bfd, o,
11852 elf_section_data (o)->sec_info))
11853 return false;
11854 break;
11855 case SEC_INFO_TYPE_EH_FRAME:
11857 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11858 o, contents))
11859 return false;
11861 break;
11862 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11864 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11865 flinfo->info,
11866 o, contents))
11867 return false;
11869 break;
11870 case SEC_INFO_TYPE_SFRAME:
11872 /* Merge .sframe sections into the ctf frame encoder
11873 context of the output_bfd's section. The final .sframe
11874 output section will be written out later. */
11875 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11876 o, contents))
11877 return false;
11879 break;
11880 default:
11882 if (! (o->flags & SEC_EXCLUDE))
11884 file_ptr offset = (file_ptr) o->output_offset;
11885 bfd_size_type todo = o->size;
11887 offset *= bfd_octets_per_byte (output_bfd, o);
11889 if ((o->flags & SEC_ELF_REVERSE_COPY)
11890 && o->size > address_size)
11892 /* Reverse-copy input section to output. */
11894 if ((o->size & (address_size - 1)) != 0
11895 || (o->reloc_count != 0
11896 && (o->size * bed->s->int_rels_per_ext_rel
11897 != o->reloc_count * address_size)))
11899 _bfd_error_handler
11900 /* xgettext:c-format */
11901 (_("error: %pB: size of section %pA is not "
11902 "multiple of address size"),
11903 input_bfd, o);
11904 bfd_set_error (bfd_error_bad_value);
11905 return false;
11910 todo -= address_size;
11911 if (! bfd_set_section_contents (output_bfd,
11912 o->output_section,
11913 contents + todo,
11914 offset,
11915 address_size))
11916 return false;
11917 if (todo == 0)
11918 break;
11919 offset += address_size;
11921 while (1);
11923 else if (! bfd_set_section_contents (output_bfd,
11924 o->output_section,
11925 contents,
11926 offset, todo))
11927 return false;
11930 break;
11934 return true;
11937 /* Generate a reloc when linking an ELF file. This is a reloc
11938 requested by the linker, and does not come from any input file. This
11939 is used to build constructor and destructor tables when linking
11940 with -Ur. */
11942 static bool
11943 elf_reloc_link_order (bfd *output_bfd,
11944 struct bfd_link_info *info,
11945 asection *output_section,
11946 struct bfd_link_order *link_order)
11948 reloc_howto_type *howto;
11949 long indx;
11950 bfd_vma offset;
11951 bfd_vma addend;
11952 struct bfd_elf_section_reloc_data *reldata;
11953 struct elf_link_hash_entry **rel_hash_ptr;
11954 Elf_Internal_Shdr *rel_hdr;
11955 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11956 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11957 bfd_byte *erel;
11958 unsigned int i;
11959 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11961 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11962 if (howto == NULL)
11964 bfd_set_error (bfd_error_bad_value);
11965 return false;
11968 addend = link_order->u.reloc.p->addend;
11970 if (esdo->rel.hdr)
11971 reldata = &esdo->rel;
11972 else if (esdo->rela.hdr)
11973 reldata = &esdo->rela;
11974 else
11976 reldata = NULL;
11977 BFD_ASSERT (0);
11980 /* Figure out the symbol index. */
11981 rel_hash_ptr = reldata->hashes + reldata->count;
11982 if (link_order->type == bfd_section_reloc_link_order)
11984 indx = link_order->u.reloc.p->u.section->target_index;
11985 BFD_ASSERT (indx != 0);
11986 *rel_hash_ptr = NULL;
11988 else
11990 struct elf_link_hash_entry *h;
11992 /* Treat a reloc against a defined symbol as though it were
11993 actually against the section. */
11994 h = ((struct elf_link_hash_entry *)
11995 bfd_wrapped_link_hash_lookup (output_bfd, info,
11996 link_order->u.reloc.p->u.name,
11997 false, false, true));
11998 if (h != NULL
11999 && (h->root.type == bfd_link_hash_defined
12000 || h->root.type == bfd_link_hash_defweak))
12002 asection *section;
12004 section = h->root.u.def.section;
12005 indx = section->output_section->target_index;
12006 *rel_hash_ptr = NULL;
12007 /* It seems that we ought to add the symbol value to the
12008 addend here, but in practice it has already been added
12009 because it was passed to constructor_callback. */
12010 addend += section->output_section->vma + section->output_offset;
12012 else if (h != NULL)
12014 /* Setting the index to -2 tells elf_link_output_extsym that
12015 this symbol is used by a reloc. */
12016 h->indx = -2;
12017 *rel_hash_ptr = h;
12018 indx = 0;
12020 else
12022 (*info->callbacks->unattached_reloc)
12023 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12024 indx = 0;
12028 /* If this is an inplace reloc, we must write the addend into the
12029 object file. */
12030 if (howto->partial_inplace && addend != 0)
12032 bfd_size_type size;
12033 bfd_reloc_status_type rstat;
12034 bfd_byte *buf;
12035 bool ok;
12036 const char *sym_name;
12037 bfd_size_type octets;
12039 size = (bfd_size_type) bfd_get_reloc_size (howto);
12040 buf = (bfd_byte *) bfd_zmalloc (size);
12041 if (buf == NULL && size != 0)
12042 return false;
12043 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12044 switch (rstat)
12046 case bfd_reloc_ok:
12047 break;
12049 default:
12050 case bfd_reloc_outofrange:
12051 abort ();
12053 case bfd_reloc_overflow:
12054 if (link_order->type == bfd_section_reloc_link_order)
12055 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12056 else
12057 sym_name = link_order->u.reloc.p->u.name;
12058 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12059 howto->name, addend, NULL, NULL,
12060 (bfd_vma) 0);
12061 break;
12064 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12065 output_section);
12066 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12067 octets, size);
12068 free (buf);
12069 if (! ok)
12070 return false;
12073 /* The address of a reloc is relative to the section in a
12074 relocatable file, and is a virtual address in an executable
12075 file. */
12076 offset = link_order->offset;
12077 if (! bfd_link_relocatable (info))
12078 offset += output_section->vma;
12080 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12082 irel[i].r_offset = offset;
12083 irel[i].r_info = 0;
12084 irel[i].r_addend = 0;
12086 if (bed->s->arch_size == 32)
12087 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12088 else
12089 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12091 rel_hdr = reldata->hdr;
12092 erel = rel_hdr->contents;
12093 if (rel_hdr->sh_type == SHT_REL)
12095 erel += reldata->count * bed->s->sizeof_rel;
12096 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12098 else
12100 irel[0].r_addend = addend;
12101 erel += reldata->count * bed->s->sizeof_rela;
12102 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12105 ++reldata->count;
12107 return true;
12110 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12111 Returns TRUE upon success, FALSE otherwise. */
12113 static bool
12114 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12116 bool ret = false;
12117 bfd *implib_bfd;
12118 const struct elf_backend_data *bed;
12119 flagword flags;
12120 enum bfd_architecture arch;
12121 unsigned int mach;
12122 asymbol **sympp = NULL;
12123 long symsize;
12124 long symcount;
12125 long src_count;
12126 elf_symbol_type *osymbuf;
12127 size_t amt;
12129 implib_bfd = info->out_implib_bfd;
12130 bed = get_elf_backend_data (abfd);
12132 if (!bfd_set_format (implib_bfd, bfd_object))
12133 return false;
12135 /* Use flag from executable but make it a relocatable object. */
12136 flags = bfd_get_file_flags (abfd);
12137 flags &= ~HAS_RELOC;
12138 if (!bfd_set_start_address (implib_bfd, 0)
12139 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12140 return false;
12142 /* Copy architecture of output file to import library file. */
12143 arch = bfd_get_arch (abfd);
12144 mach = bfd_get_mach (abfd);
12145 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12146 && (abfd->target_defaulted
12147 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12148 return false;
12150 /* Get symbol table size. */
12151 symsize = bfd_get_symtab_upper_bound (abfd);
12152 if (symsize < 0)
12153 return false;
12155 /* Read in the symbol table. */
12156 sympp = (asymbol **) bfd_malloc (symsize);
12157 if (sympp == NULL)
12158 return false;
12160 symcount = bfd_canonicalize_symtab (abfd, sympp);
12161 if (symcount < 0)
12162 goto free_sym_buf;
12164 /* Allow the BFD backend to copy any private header data it
12165 understands from the output BFD to the import library BFD. */
12166 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12167 goto free_sym_buf;
12169 /* Filter symbols to appear in the import library. */
12170 if (bed->elf_backend_filter_implib_symbols)
12171 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12172 symcount);
12173 else
12174 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12175 if (symcount == 0)
12177 bfd_set_error (bfd_error_no_symbols);
12178 _bfd_error_handler (_("%pB: no symbol found for import library"),
12179 implib_bfd);
12180 goto free_sym_buf;
12184 /* Make symbols absolute. */
12185 amt = symcount * sizeof (*osymbuf);
12186 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12187 if (osymbuf == NULL)
12188 goto free_sym_buf;
12190 for (src_count = 0; src_count < symcount; src_count++)
12192 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12193 sizeof (*osymbuf));
12194 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12195 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12196 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12197 osymbuf[src_count].internal_elf_sym.st_value =
12198 osymbuf[src_count].symbol.value;
12199 sympp[src_count] = &osymbuf[src_count].symbol;
12202 bfd_set_symtab (implib_bfd, sympp, symcount);
12204 /* Allow the BFD backend to copy any private data it understands
12205 from the output BFD to the import library BFD. This is done last
12206 to permit the routine to look at the filtered symbol table. */
12207 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12208 goto free_sym_buf;
12210 if (!bfd_close (implib_bfd))
12211 goto free_sym_buf;
12213 ret = true;
12215 free_sym_buf:
12216 free (sympp);
12217 return ret;
12220 static void
12221 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12223 asection *o;
12225 if (flinfo->symstrtab != NULL)
12226 _bfd_elf_strtab_free (flinfo->symstrtab);
12227 free (flinfo->contents);
12228 free (flinfo->external_relocs);
12229 free (flinfo->internal_relocs);
12230 free (flinfo->external_syms);
12231 free (flinfo->locsym_shndx);
12232 free (flinfo->internal_syms);
12233 free (flinfo->indices);
12234 free (flinfo->sections);
12235 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12236 free (flinfo->symshndxbuf);
12237 for (o = obfd->sections; o != NULL; o = o->next)
12239 struct bfd_elf_section_data *esdo = elf_section_data (o);
12240 free (esdo->rel.hashes);
12241 free (esdo->rela.hashes);
12245 /* Do the final step of an ELF link. */
12247 bool
12248 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12250 bool dynamic;
12251 bool emit_relocs;
12252 bfd *dynobj;
12253 struct elf_final_link_info flinfo;
12254 asection *o;
12255 struct bfd_link_order *p;
12256 bfd *sub;
12257 bfd_size_type max_contents_size;
12258 bfd_size_type max_external_reloc_size;
12259 bfd_size_type max_internal_reloc_count;
12260 bfd_size_type max_sym_count;
12261 bfd_size_type max_sym_shndx_count;
12262 Elf_Internal_Sym elfsym;
12263 unsigned int i;
12264 Elf_Internal_Shdr *symtab_hdr;
12265 Elf_Internal_Shdr *symtab_shndx_hdr;
12266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12267 struct elf_outext_info eoinfo;
12268 bool merged;
12269 size_t relativecount;
12270 size_t relr_entsize;
12271 asection *reldyn = 0;
12272 bfd_size_type amt;
12273 asection *attr_section = NULL;
12274 bfd_vma attr_size = 0;
12275 const char *std_attrs_section;
12276 struct elf_link_hash_table *htab = elf_hash_table (info);
12277 bool sections_removed;
12278 bool ret;
12280 if (!is_elf_hash_table (&htab->root))
12281 return false;
12283 if (bfd_link_pic (info))
12284 abfd->flags |= DYNAMIC;
12286 dynamic = htab->dynamic_sections_created;
12287 dynobj = htab->dynobj;
12289 emit_relocs = (bfd_link_relocatable (info)
12290 || info->emitrelocations);
12292 memset (&flinfo, 0, sizeof (flinfo));
12293 flinfo.info = info;
12294 flinfo.output_bfd = abfd;
12295 flinfo.symstrtab = _bfd_elf_strtab_init ();
12296 if (flinfo.symstrtab == NULL)
12297 return false;
12299 if (! dynamic)
12301 flinfo.hash_sec = NULL;
12302 flinfo.symver_sec = NULL;
12304 else
12306 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12307 /* Note that dynsym_sec can be NULL (on VMS). */
12308 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12309 /* Note that it is OK if symver_sec is NULL. */
12312 if (info->unique_symbol
12313 && !bfd_hash_table_init (&flinfo.local_hash_table,
12314 local_hash_newfunc,
12315 sizeof (struct local_hash_entry)))
12316 return false;
12318 /* The object attributes have been merged. Remove the input
12319 sections from the link, and set the contents of the output
12320 section. */
12321 sections_removed = false;
12322 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12323 for (o = abfd->sections; o != NULL; o = o->next)
12325 bool remove_section = false;
12327 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12328 || strcmp (o->name, ".gnu.attributes") == 0)
12330 for (p = o->map_head.link_order; p != NULL; p = p->next)
12332 asection *input_section;
12334 if (p->type != bfd_indirect_link_order)
12335 continue;
12336 input_section = p->u.indirect.section;
12337 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12338 elf_link_input_bfd ignores this section. */
12339 input_section->flags &= ~SEC_HAS_CONTENTS;
12342 attr_size = bfd_elf_obj_attr_size (abfd);
12343 bfd_set_section_size (o, attr_size);
12344 /* Skip this section later on. */
12345 o->map_head.link_order = NULL;
12346 if (attr_size)
12347 attr_section = o;
12348 else
12349 remove_section = true;
12351 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12353 /* Remove empty group section from linker output. */
12354 remove_section = true;
12356 if (remove_section)
12358 o->flags |= SEC_EXCLUDE;
12359 bfd_section_list_remove (abfd, o);
12360 abfd->section_count--;
12361 sections_removed = true;
12364 if (sections_removed)
12365 _bfd_fix_excluded_sec_syms (abfd, info);
12367 /* Count up the number of relocations we will output for each output
12368 section, so that we know the sizes of the reloc sections. We
12369 also figure out some maximum sizes. */
12370 max_contents_size = 0;
12371 max_external_reloc_size = 0;
12372 max_internal_reloc_count = 0;
12373 max_sym_count = 0;
12374 max_sym_shndx_count = 0;
12375 merged = false;
12376 for (o = abfd->sections; o != NULL; o = o->next)
12378 struct bfd_elf_section_data *esdo = elf_section_data (o);
12379 o->reloc_count = 0;
12381 for (p = o->map_head.link_order; p != NULL; p = p->next)
12383 unsigned int reloc_count = 0;
12384 unsigned int additional_reloc_count = 0;
12385 struct bfd_elf_section_data *esdi = NULL;
12387 if (p->type == bfd_section_reloc_link_order
12388 || p->type == bfd_symbol_reloc_link_order)
12389 reloc_count = 1;
12390 else if (p->type == bfd_indirect_link_order)
12392 asection *sec;
12394 sec = p->u.indirect.section;
12396 /* Mark all sections which are to be included in the
12397 link. This will normally be every section. We need
12398 to do this so that we can identify any sections which
12399 the linker has decided to not include. */
12400 sec->linker_mark = true;
12402 if (sec->flags & SEC_MERGE)
12403 merged = true;
12405 if (sec->rawsize > max_contents_size)
12406 max_contents_size = sec->rawsize;
12407 if (sec->size > max_contents_size)
12408 max_contents_size = sec->size;
12410 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12411 && (sec->owner->flags & DYNAMIC) == 0)
12413 size_t sym_count;
12415 /* We are interested in just local symbols, not all
12416 symbols. */
12417 if (elf_bad_symtab (sec->owner))
12418 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12419 / bed->s->sizeof_sym);
12420 else
12421 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12423 if (sym_count > max_sym_count)
12424 max_sym_count = sym_count;
12426 if (sym_count > max_sym_shndx_count
12427 && elf_symtab_shndx_list (sec->owner) != NULL)
12428 max_sym_shndx_count = sym_count;
12430 esdi = elf_section_data (sec);
12432 if (esdi->this_hdr.sh_type == SHT_REL
12433 || esdi->this_hdr.sh_type == SHT_RELA)
12434 /* Some backends use reloc_count in relocation sections
12435 to count particular types of relocs. Of course,
12436 reloc sections themselves can't have relocations. */
12438 else if (emit_relocs)
12440 reloc_count = sec->reloc_count;
12441 if (bed->elf_backend_count_additional_relocs)
12443 int c;
12444 c = (*bed->elf_backend_count_additional_relocs) (sec);
12445 additional_reloc_count += c;
12448 else if (bed->elf_backend_count_relocs)
12449 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12451 if ((sec->flags & SEC_RELOC) != 0)
12453 size_t ext_size = 0;
12455 if (esdi->rel.hdr != NULL)
12456 ext_size = esdi->rel.hdr->sh_size;
12457 if (esdi->rela.hdr != NULL)
12458 ext_size += esdi->rela.hdr->sh_size;
12460 if (ext_size > max_external_reloc_size)
12461 max_external_reloc_size = ext_size;
12462 if (sec->reloc_count > max_internal_reloc_count)
12463 max_internal_reloc_count = sec->reloc_count;
12468 if (reloc_count == 0)
12469 continue;
12471 reloc_count += additional_reloc_count;
12472 o->reloc_count += reloc_count;
12474 if (p->type == bfd_indirect_link_order && emit_relocs)
12476 if (esdi->rel.hdr)
12478 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12479 esdo->rel.count += additional_reloc_count;
12481 if (esdi->rela.hdr)
12483 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12484 esdo->rela.count += additional_reloc_count;
12487 else
12489 if (o->use_rela_p)
12490 esdo->rela.count += reloc_count;
12491 else
12492 esdo->rel.count += reloc_count;
12496 if (o->reloc_count > 0)
12497 o->flags |= SEC_RELOC;
12498 else
12500 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12501 set it (this is probably a bug) and if it is set
12502 assign_section_numbers will create a reloc section. */
12503 o->flags &=~ SEC_RELOC;
12506 /* If the SEC_ALLOC flag is not set, force the section VMA to
12507 zero. This is done in elf_fake_sections as well, but forcing
12508 the VMA to 0 here will ensure that relocs against these
12509 sections are handled correctly. */
12510 if ((o->flags & SEC_ALLOC) == 0
12511 && ! o->user_set_vma)
12512 o->vma = 0;
12515 if (! bfd_link_relocatable (info) && merged)
12516 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12518 /* Figure out the file positions for everything but the symbol table
12519 and the relocs. We set symcount to force assign_section_numbers
12520 to create a symbol table. */
12521 abfd->symcount = info->strip != strip_all || emit_relocs;
12522 BFD_ASSERT (! abfd->output_has_begun);
12523 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12524 goto error_return;
12526 /* Set sizes, and assign file positions for reloc sections. */
12527 for (o = abfd->sections; o != NULL; o = o->next)
12529 struct bfd_elf_section_data *esdo = elf_section_data (o);
12530 if ((o->flags & SEC_RELOC) != 0)
12532 if (esdo->rel.hdr
12533 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12534 goto error_return;
12536 if (esdo->rela.hdr
12537 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12538 goto error_return;
12541 /* _bfd_elf_compute_section_file_positions makes temporary use
12542 of target_index. Reset it. */
12543 o->target_index = 0;
12545 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12546 to count upwards while actually outputting the relocations. */
12547 esdo->rel.count = 0;
12548 esdo->rela.count = 0;
12550 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12551 && !bfd_section_is_ctf (o))
12553 /* Cache the section contents so that they can be compressed
12554 later. Use bfd_malloc since it will be freed by
12555 bfd_compress_section_contents. */
12556 unsigned char *contents = esdo->this_hdr.contents;
12557 if (contents != NULL)
12558 abort ();
12559 contents
12560 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12561 if (contents == NULL)
12562 goto error_return;
12563 esdo->this_hdr.contents = contents;
12567 /* We have now assigned file positions for all the sections except .symtab,
12568 .strtab, and non-loaded reloc and compressed debugging sections. We start
12569 the .symtab section at the current file position, and write directly to it.
12570 We build the .strtab section in memory. */
12571 abfd->symcount = 0;
12572 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12573 /* sh_name is set in prep_headers. */
12574 symtab_hdr->sh_type = SHT_SYMTAB;
12575 /* sh_flags, sh_addr and sh_size all start off zero. */
12576 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12577 /* sh_link is set in assign_section_numbers. */
12578 /* sh_info is set below. */
12579 /* sh_offset is set just below. */
12580 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12582 if (max_sym_count < 20)
12583 max_sym_count = 20;
12584 htab->strtabsize = max_sym_count;
12585 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12586 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12587 if (htab->strtab == NULL)
12588 goto error_return;
12589 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12590 flinfo.symshndxbuf
12591 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12592 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12594 if (info->strip != strip_all || emit_relocs)
12596 file_ptr off = elf_next_file_pos (abfd);
12598 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12600 /* Note that at this point elf_next_file_pos (abfd) is
12601 incorrect. We do not yet know the size of the .symtab section.
12602 We correct next_file_pos below, after we do know the size. */
12604 /* Start writing out the symbol table. The first symbol is always a
12605 dummy symbol. */
12606 elfsym.st_value = 0;
12607 elfsym.st_size = 0;
12608 elfsym.st_info = 0;
12609 elfsym.st_other = 0;
12610 elfsym.st_shndx = SHN_UNDEF;
12611 elfsym.st_target_internal = 0;
12612 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12613 bfd_und_section_ptr, NULL) != 1)
12614 goto error_return;
12616 /* Output a symbol for each section if asked or they are used for
12617 relocs. These symbols usually have no names. We store the
12618 index of each one in the index field of the section, so that
12619 we can find it again when outputting relocs. */
12621 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12623 bool name_local_sections
12624 = (bed->elf_backend_name_local_section_symbols
12625 && bed->elf_backend_name_local_section_symbols (abfd));
12626 const char *name = NULL;
12628 elfsym.st_size = 0;
12629 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12630 elfsym.st_other = 0;
12631 elfsym.st_value = 0;
12632 elfsym.st_target_internal = 0;
12633 for (i = 1; i < elf_numsections (abfd); i++)
12635 o = bfd_section_from_elf_index (abfd, i);
12636 if (o != NULL)
12638 o->target_index = bfd_get_symcount (abfd);
12639 elfsym.st_shndx = i;
12640 if (!bfd_link_relocatable (info))
12641 elfsym.st_value = o->vma;
12642 if (name_local_sections)
12643 name = o->name;
12644 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12645 NULL) != 1)
12646 goto error_return;
12652 /* On some targets like Irix 5 the symbol split between local and global
12653 ones recorded in the sh_info field needs to be done between section
12654 and all other symbols. */
12655 if (bed->elf_backend_elfsym_local_is_section
12656 && bed->elf_backend_elfsym_local_is_section (abfd))
12657 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12659 /* Allocate some memory to hold information read in from the input
12660 files. */
12661 if (max_contents_size != 0)
12663 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12664 if (flinfo.contents == NULL)
12665 goto error_return;
12668 if (max_external_reloc_size != 0)
12670 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12671 if (flinfo.external_relocs == NULL)
12672 goto error_return;
12675 if (max_internal_reloc_count != 0)
12677 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12678 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12679 if (flinfo.internal_relocs == NULL)
12680 goto error_return;
12683 if (max_sym_count != 0)
12685 amt = max_sym_count * bed->s->sizeof_sym;
12686 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12687 if (flinfo.external_syms == NULL)
12688 goto error_return;
12690 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12691 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12692 if (flinfo.internal_syms == NULL)
12693 goto error_return;
12695 amt = max_sym_count * sizeof (long);
12696 flinfo.indices = (long int *) bfd_malloc (amt);
12697 if (flinfo.indices == NULL)
12698 goto error_return;
12700 amt = max_sym_count * sizeof (asection *);
12701 flinfo.sections = (asection **) bfd_malloc (amt);
12702 if (flinfo.sections == NULL)
12703 goto error_return;
12706 if (max_sym_shndx_count != 0)
12708 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12709 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12710 if (flinfo.locsym_shndx == NULL)
12711 goto error_return;
12714 if (htab->tls_sec)
12716 bfd_vma base, end = 0; /* Both bytes. */
12717 asection *sec;
12719 for (sec = htab->tls_sec;
12720 sec && (sec->flags & SEC_THREAD_LOCAL);
12721 sec = sec->next)
12723 bfd_size_type size = sec->size;
12724 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12726 if (size == 0
12727 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12729 struct bfd_link_order *ord = sec->map_tail.link_order;
12731 if (ord != NULL)
12732 size = ord->offset * opb + ord->size;
12734 end = sec->vma + size / opb;
12736 base = htab->tls_sec->vma;
12737 /* Only align end of TLS section if static TLS doesn't have special
12738 alignment requirements. */
12739 if (bed->static_tls_alignment == 1)
12740 end = align_power (end, htab->tls_sec->alignment_power);
12741 htab->tls_size = end - base;
12744 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12745 return false;
12747 /* Finish relative relocations here after regular symbol processing
12748 is finished if DT_RELR is enabled. */
12749 if (info->enable_dt_relr
12750 && bed->finish_relative_relocs
12751 && !bed->finish_relative_relocs (info))
12752 info->callbacks->einfo
12753 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12755 /* Since ELF permits relocations to be against local symbols, we
12756 must have the local symbols available when we do the relocations.
12757 Since we would rather only read the local symbols once, and we
12758 would rather not keep them in memory, we handle all the
12759 relocations for a single input file at the same time.
12761 Unfortunately, there is no way to know the total number of local
12762 symbols until we have seen all of them, and the local symbol
12763 indices precede the global symbol indices. This means that when
12764 we are generating relocatable output, and we see a reloc against
12765 a global symbol, we can not know the symbol index until we have
12766 finished examining all the local symbols to see which ones we are
12767 going to output. To deal with this, we keep the relocations in
12768 memory, and don't output them until the end of the link. This is
12769 an unfortunate waste of memory, but I don't see a good way around
12770 it. Fortunately, it only happens when performing a relocatable
12771 link, which is not the common case. FIXME: If keep_memory is set
12772 we could write the relocs out and then read them again; I don't
12773 know how bad the memory loss will be. */
12775 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12776 sub->output_has_begun = false;
12777 for (o = abfd->sections; o != NULL; o = o->next)
12779 for (p = o->map_head.link_order; p != NULL; p = p->next)
12781 if (p->type == bfd_indirect_link_order
12782 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12783 == bfd_target_elf_flavour)
12784 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12786 if (! sub->output_has_begun)
12788 if (! elf_link_input_bfd (&flinfo, sub))
12789 goto error_return;
12790 sub->output_has_begun = true;
12793 else if (p->type == bfd_section_reloc_link_order
12794 || p->type == bfd_symbol_reloc_link_order)
12796 if (! elf_reloc_link_order (abfd, info, o, p))
12797 goto error_return;
12799 else
12801 if (! _bfd_default_link_order (abfd, info, o, p))
12803 if (p->type == bfd_indirect_link_order
12804 && (bfd_get_flavour (sub)
12805 == bfd_target_elf_flavour)
12806 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12807 != bed->s->elfclass))
12809 const char *iclass, *oclass;
12811 switch (bed->s->elfclass)
12813 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12814 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12815 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12816 default: abort ();
12819 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12821 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12822 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12823 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12824 default: abort ();
12827 bfd_set_error (bfd_error_wrong_format);
12828 _bfd_error_handler
12829 /* xgettext:c-format */
12830 (_("%pB: file class %s incompatible with %s"),
12831 sub, iclass, oclass);
12834 goto error_return;
12840 /* Free symbol buffer if needed. */
12841 if (!info->reduce_memory_overheads)
12843 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12844 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12846 free (elf_tdata (sub)->symbuf);
12847 elf_tdata (sub)->symbuf = NULL;
12851 ret = true;
12853 /* Output any global symbols that got converted to local in a
12854 version script or due to symbol visibility. We do this in a
12855 separate step since ELF requires all local symbols to appear
12856 prior to any global symbols. FIXME: We should only do this if
12857 some global symbols were, in fact, converted to become local.
12858 FIXME: Will this work correctly with the Irix 5 linker? */
12859 eoinfo.failed = false;
12860 eoinfo.flinfo = &flinfo;
12861 eoinfo.localsyms = true;
12862 eoinfo.file_sym_done = false;
12863 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12864 if (eoinfo.failed)
12866 ret = false;
12867 goto return_local_hash_table;
12870 /* If backend needs to output some local symbols not present in the hash
12871 table, do it now. */
12872 if (bed->elf_backend_output_arch_local_syms)
12874 if (! ((*bed->elf_backend_output_arch_local_syms)
12875 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12877 ret = false;
12878 goto return_local_hash_table;
12882 /* That wrote out all the local symbols. Finish up the symbol table
12883 with the global symbols. Even if we want to strip everything we
12884 can, we still need to deal with those global symbols that got
12885 converted to local in a version script. */
12887 /* The sh_info field records the index of the first non local symbol. */
12888 if (!symtab_hdr->sh_info)
12889 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12891 if (dynamic
12892 && htab->dynsym != NULL
12893 && htab->dynsym->output_section != bfd_abs_section_ptr)
12895 Elf_Internal_Sym sym;
12896 bfd_byte *dynsym = htab->dynsym->contents;
12898 o = htab->dynsym->output_section;
12899 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12901 /* Write out the section symbols for the output sections. */
12902 if (bfd_link_pic (info)
12903 || htab->is_relocatable_executable)
12905 asection *s;
12907 sym.st_size = 0;
12908 sym.st_name = 0;
12909 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12910 sym.st_other = 0;
12911 sym.st_target_internal = 0;
12913 for (s = abfd->sections; s != NULL; s = s->next)
12915 int indx;
12916 bfd_byte *dest;
12917 long dynindx;
12919 dynindx = elf_section_data (s)->dynindx;
12920 if (dynindx <= 0)
12921 continue;
12922 indx = elf_section_data (s)->this_idx;
12923 BFD_ASSERT (indx > 0);
12924 sym.st_shndx = indx;
12925 if (! check_dynsym (abfd, &sym))
12927 ret = false;
12928 goto return_local_hash_table;
12930 sym.st_value = s->vma;
12931 dest = dynsym + dynindx * bed->s->sizeof_sym;
12933 /* Inform the linker of the addition of this symbol. */
12935 if (info->callbacks->ctf_new_dynsym)
12936 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12938 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12942 /* Write out the local dynsyms. */
12943 if (htab->dynlocal)
12945 struct elf_link_local_dynamic_entry *e;
12946 for (e = htab->dynlocal; e ; e = e->next)
12948 asection *s;
12949 bfd_byte *dest;
12951 /* Copy the internal symbol and turn off visibility.
12952 Note that we saved a word of storage and overwrote
12953 the original st_name with the dynstr_index. */
12954 sym = e->isym;
12955 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12956 sym.st_shndx = SHN_UNDEF;
12958 s = bfd_section_from_elf_index (e->input_bfd,
12959 e->isym.st_shndx);
12960 if (s != NULL
12961 && s->output_section != NULL
12962 && elf_section_data (s->output_section) != NULL)
12964 sym.st_shndx =
12965 elf_section_data (s->output_section)->this_idx;
12966 if (! check_dynsym (abfd, &sym))
12968 ret = false;
12969 goto return_local_hash_table;
12971 sym.st_value = (s->output_section->vma
12972 + s->output_offset
12973 + e->isym.st_value);
12976 /* Inform the linker of the addition of this symbol. */
12978 if (info->callbacks->ctf_new_dynsym)
12979 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12981 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12982 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12987 /* We get the global symbols from the hash table. */
12988 eoinfo.failed = false;
12989 eoinfo.localsyms = false;
12990 eoinfo.flinfo = &flinfo;
12991 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12992 if (eoinfo.failed)
12994 ret = false;
12995 goto return_local_hash_table;
12998 /* If backend needs to output some symbols not present in the hash
12999 table, do it now. */
13000 if (bed->elf_backend_output_arch_syms
13001 && (info->strip != strip_all || emit_relocs))
13003 if (! ((*bed->elf_backend_output_arch_syms)
13004 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13006 ret = false;
13007 goto return_local_hash_table;
13011 /* Finalize the .strtab section. */
13012 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13014 /* Swap out the .strtab section. */
13015 if (!elf_link_swap_symbols_out (&flinfo))
13017 ret = false;
13018 goto return_local_hash_table;
13021 /* Now we know the size of the symtab section. */
13022 if (bfd_get_symcount (abfd) > 0)
13024 /* Finish up and write out the symbol string table (.strtab)
13025 section. */
13026 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13027 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13029 if (elf_symtab_shndx_list (abfd))
13031 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13033 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13035 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13036 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13037 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13038 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13039 symtab_shndx_hdr->sh_size = amt;
13041 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13042 off, true);
13044 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13045 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
13047 ret = false;
13048 goto return_local_hash_table;
13053 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13054 /* sh_name was set in prep_headers. */
13055 symstrtab_hdr->sh_type = SHT_STRTAB;
13056 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13057 symstrtab_hdr->sh_addr = 0;
13058 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13059 symstrtab_hdr->sh_entsize = 0;
13060 symstrtab_hdr->sh_link = 0;
13061 symstrtab_hdr->sh_info = 0;
13062 /* sh_offset is set just below. */
13063 symstrtab_hdr->sh_addralign = 1;
13065 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13066 off, true);
13067 elf_next_file_pos (abfd) = off;
13069 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13070 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13072 ret = false;
13073 goto return_local_hash_table;
13077 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13079 _bfd_error_handler (_("%pB: failed to generate import library"),
13080 info->out_implib_bfd);
13081 ret = false;
13082 goto return_local_hash_table;
13085 /* Adjust the relocs to have the correct symbol indices. */
13086 for (o = abfd->sections; o != NULL; o = o->next)
13088 struct bfd_elf_section_data *esdo = elf_section_data (o);
13089 bool sort;
13091 if ((o->flags & SEC_RELOC) == 0)
13092 continue;
13094 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13095 if (esdo->rel.hdr != NULL
13096 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13098 ret = false;
13099 goto return_local_hash_table;
13101 if (esdo->rela.hdr != NULL
13102 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13104 ret = false;
13105 goto return_local_hash_table;
13108 /* Set the reloc_count field to 0 to prevent write_relocs from
13109 trying to swap the relocs out itself. */
13110 o->reloc_count = 0;
13113 relativecount = 0;
13114 if (dynamic && info->combreloc && dynobj != NULL)
13115 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13117 relr_entsize = 0;
13118 if (htab->srelrdyn != NULL
13119 && htab->srelrdyn->output_section != NULL
13120 && htab->srelrdyn->size != 0)
13122 asection *s = htab->srelrdyn->output_section;
13123 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13124 if (relr_entsize == 0)
13126 relr_entsize = bed->s->arch_size / 8;
13127 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13131 /* If we are linking against a dynamic object, or generating a
13132 shared library, finish up the dynamic linking information. */
13133 if (dynamic)
13135 bfd_byte *dyncon, *dynconend;
13137 /* Fix up .dynamic entries. */
13138 o = bfd_get_linker_section (dynobj, ".dynamic");
13139 BFD_ASSERT (o != NULL);
13141 dyncon = o->contents;
13142 dynconend = PTR_ADD (o->contents, o->size);
13143 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13145 Elf_Internal_Dyn dyn;
13146 const char *name;
13147 unsigned int type;
13148 bfd_size_type sh_size;
13149 bfd_vma sh_addr;
13151 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13153 switch (dyn.d_tag)
13155 default:
13156 continue;
13157 case DT_NULL:
13158 if (relativecount != 0)
13160 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13162 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13163 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13165 if (dyn.d_tag != DT_NULL
13166 && dynconend - dyncon >= bed->s->sizeof_dyn)
13168 dyn.d_un.d_val = relativecount;
13169 relativecount = 0;
13170 break;
13172 relativecount = 0;
13174 if (relr_entsize != 0)
13176 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13178 asection *s = htab->srelrdyn;
13179 dyn.d_tag = DT_RELR;
13180 dyn.d_un.d_ptr
13181 = s->output_section->vma + s->output_offset;
13182 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13183 dyncon += bed->s->sizeof_dyn;
13185 dyn.d_tag = DT_RELRSZ;
13186 dyn.d_un.d_val = s->size;
13187 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13188 dyncon += bed->s->sizeof_dyn;
13190 dyn.d_tag = DT_RELRENT;
13191 dyn.d_un.d_val = relr_entsize;
13192 relr_entsize = 0;
13193 break;
13195 relr_entsize = 0;
13197 continue;
13199 case DT_INIT:
13200 name = info->init_function;
13201 goto get_sym;
13202 case DT_FINI:
13203 name = info->fini_function;
13204 get_sym:
13206 struct elf_link_hash_entry *h;
13208 h = elf_link_hash_lookup (htab, name, false, false, true);
13209 if (h != NULL
13210 && (h->root.type == bfd_link_hash_defined
13211 || h->root.type == bfd_link_hash_defweak))
13213 dyn.d_un.d_ptr = h->root.u.def.value;
13214 o = h->root.u.def.section;
13215 if (o->output_section != NULL)
13216 dyn.d_un.d_ptr += (o->output_section->vma
13217 + o->output_offset);
13218 else
13220 /* The symbol is imported from another shared
13221 library and does not apply to this one. */
13222 dyn.d_un.d_ptr = 0;
13224 break;
13227 continue;
13229 case DT_PREINIT_ARRAYSZ:
13230 name = ".preinit_array";
13231 goto get_out_size;
13232 case DT_INIT_ARRAYSZ:
13233 name = ".init_array";
13234 goto get_out_size;
13235 case DT_FINI_ARRAYSZ:
13236 name = ".fini_array";
13237 get_out_size:
13238 o = bfd_get_section_by_name (abfd, name);
13239 if (o == NULL)
13241 _bfd_error_handler
13242 (_("could not find section %s"), name);
13243 goto error_return;
13245 if (o->size == 0)
13246 _bfd_error_handler
13247 (_("warning: %s section has zero size"), name);
13248 dyn.d_un.d_val = o->size;
13249 break;
13251 case DT_PREINIT_ARRAY:
13252 name = ".preinit_array";
13253 goto get_out_vma;
13254 case DT_INIT_ARRAY:
13255 name = ".init_array";
13256 goto get_out_vma;
13257 case DT_FINI_ARRAY:
13258 name = ".fini_array";
13259 get_out_vma:
13260 o = bfd_get_section_by_name (abfd, name);
13261 goto do_vma;
13263 case DT_HASH:
13264 name = ".hash";
13265 goto get_vma;
13266 case DT_GNU_HASH:
13267 name = ".gnu.hash";
13268 goto get_vma;
13269 case DT_STRTAB:
13270 name = ".dynstr";
13271 goto get_vma;
13272 case DT_SYMTAB:
13273 name = ".dynsym";
13274 goto get_vma;
13275 case DT_VERDEF:
13276 name = ".gnu.version_d";
13277 goto get_vma;
13278 case DT_VERNEED:
13279 name = ".gnu.version_r";
13280 goto get_vma;
13281 case DT_VERSYM:
13282 name = ".gnu.version";
13283 get_vma:
13284 o = bfd_get_linker_section (dynobj, name);
13285 do_vma:
13286 if (o == NULL || bfd_is_abs_section (o->output_section))
13288 _bfd_error_handler
13289 (_("could not find section %s"), name);
13290 goto error_return;
13292 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13294 _bfd_error_handler
13295 (_("warning: section '%s' is being made into a note"), name);
13296 bfd_set_error (bfd_error_nonrepresentable_section);
13297 goto error_return;
13299 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13300 break;
13302 case DT_REL:
13303 case DT_RELA:
13304 case DT_RELSZ:
13305 case DT_RELASZ:
13306 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13307 type = SHT_REL;
13308 else
13309 type = SHT_RELA;
13310 sh_size = 0;
13311 sh_addr = 0;
13312 for (i = 1; i < elf_numsections (abfd); i++)
13314 Elf_Internal_Shdr *hdr;
13316 hdr = elf_elfsections (abfd)[i];
13317 if (hdr->sh_type == type
13318 && (hdr->sh_flags & SHF_ALLOC) != 0)
13320 sh_size += hdr->sh_size;
13321 if (sh_addr == 0
13322 || sh_addr > hdr->sh_addr)
13323 sh_addr = hdr->sh_addr;
13327 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13329 unsigned int opb = bfd_octets_per_byte (abfd, o);
13331 /* Don't count procedure linkage table relocs in the
13332 overall reloc count. */
13333 sh_size -= htab->srelplt->size;
13334 if (sh_size == 0)
13335 /* If the size is zero, make the address zero too.
13336 This is to avoid a glibc bug. If the backend
13337 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13338 zero, then we'll put DT_RELA at the end of
13339 DT_JMPREL. glibc will interpret the end of
13340 DT_RELA matching the end of DT_JMPREL as the
13341 case where DT_RELA includes DT_JMPREL, and for
13342 LD_BIND_NOW will decide that processing DT_RELA
13343 will process the PLT relocs too. Net result:
13344 No PLT relocs applied. */
13345 sh_addr = 0;
13347 /* If .rela.plt is the first .rela section, exclude
13348 it from DT_RELA. */
13349 else if (sh_addr == (htab->srelplt->output_section->vma
13350 + htab->srelplt->output_offset) * opb)
13351 sh_addr += htab->srelplt->size;
13354 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13355 dyn.d_un.d_val = sh_size;
13356 else
13357 dyn.d_un.d_ptr = sh_addr;
13358 break;
13360 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13364 /* If we have created any dynamic sections, then output them. */
13365 if (dynobj != NULL)
13367 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13368 goto error_return;
13370 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13371 if (bfd_link_textrel_check (info)
13372 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13373 && o->size != 0)
13375 bfd_byte *dyncon, *dynconend;
13377 dyncon = o->contents;
13378 dynconend = o->contents + o->size;
13379 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13381 Elf_Internal_Dyn dyn;
13383 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13385 if (dyn.d_tag == DT_TEXTREL)
13387 if (info->textrel_check == textrel_check_error)
13388 info->callbacks->einfo
13389 (_("%P%X: read-only segment has dynamic relocations\n"));
13390 else if (bfd_link_dll (info))
13391 info->callbacks->einfo
13392 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13393 else if (bfd_link_pde (info))
13394 info->callbacks->einfo
13395 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13396 else
13397 info->callbacks->einfo
13398 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13399 break;
13404 for (o = dynobj->sections; o != NULL; o = o->next)
13406 if ((o->flags & SEC_HAS_CONTENTS) == 0
13407 || o->size == 0
13408 || o->output_section == bfd_abs_section_ptr)
13409 continue;
13410 if ((o->flags & SEC_LINKER_CREATED) == 0)
13412 /* At this point, we are only interested in sections
13413 created by _bfd_elf_link_create_dynamic_sections. */
13414 continue;
13416 if (htab->stab_info.stabstr == o)
13417 continue;
13418 if (htab->eh_info.hdr_sec == o)
13419 continue;
13420 if (strcmp (o->name, ".dynstr") != 0)
13422 bfd_size_type octets = ((file_ptr) o->output_offset
13423 * bfd_octets_per_byte (abfd, o));
13424 if (!bfd_set_section_contents (abfd, o->output_section,
13425 o->contents, octets, o->size))
13426 goto error_return;
13428 else
13430 /* The contents of the .dynstr section are actually in a
13431 stringtab. */
13432 file_ptr off;
13434 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13435 if (bfd_seek (abfd, off, SEEK_SET) != 0
13436 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13437 goto error_return;
13442 if (!info->resolve_section_groups)
13444 bool failed = false;
13446 BFD_ASSERT (bfd_link_relocatable (info));
13447 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13448 if (failed)
13449 goto error_return;
13452 /* If we have optimized stabs strings, output them. */
13453 if (htab->stab_info.stabstr != NULL)
13455 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13456 goto error_return;
13459 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13460 goto error_return;
13462 if (! _bfd_elf_write_section_sframe (abfd, info))
13463 goto error_return;
13465 if (info->callbacks->emit_ctf)
13466 info->callbacks->emit_ctf ();
13468 elf_final_link_free (abfd, &flinfo);
13470 if (attr_section)
13472 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13473 if (contents == NULL)
13475 /* Bail out and fail. */
13476 ret = false;
13477 goto return_local_hash_table;
13479 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13480 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13481 free (contents);
13484 return_local_hash_table:
13485 if (info->unique_symbol)
13486 bfd_hash_table_free (&flinfo.local_hash_table);
13487 return ret;
13489 error_return:
13490 elf_final_link_free (abfd, &flinfo);
13491 ret = false;
13492 goto return_local_hash_table;
13495 /* Initialize COOKIE for input bfd ABFD. */
13497 static bool
13498 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13499 struct bfd_link_info *info, bfd *abfd)
13501 Elf_Internal_Shdr *symtab_hdr;
13502 const struct elf_backend_data *bed;
13504 bed = get_elf_backend_data (abfd);
13505 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13507 cookie->abfd = abfd;
13508 cookie->sym_hashes = elf_sym_hashes (abfd);
13509 cookie->bad_symtab = elf_bad_symtab (abfd);
13510 if (cookie->bad_symtab)
13512 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13513 cookie->extsymoff = 0;
13515 else
13517 cookie->locsymcount = symtab_hdr->sh_info;
13518 cookie->extsymoff = symtab_hdr->sh_info;
13521 if (bed->s->arch_size == 32)
13522 cookie->r_sym_shift = 8;
13523 else
13524 cookie->r_sym_shift = 32;
13526 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13527 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13529 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13530 cookie->locsymcount, 0,
13531 NULL, NULL, NULL);
13532 if (cookie->locsyms == NULL)
13534 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13535 return false;
13537 if (_bfd_link_keep_memory (info) )
13539 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13540 info->cache_size += (cookie->locsymcount
13541 * sizeof (Elf_External_Sym_Shndx));
13544 return true;
13547 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13549 static void
13550 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13552 Elf_Internal_Shdr *symtab_hdr;
13554 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13555 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13556 free (cookie->locsyms);
13559 /* Initialize the relocation information in COOKIE for input section SEC
13560 of input bfd ABFD. */
13562 static bool
13563 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13564 struct bfd_link_info *info, bfd *abfd,
13565 asection *sec)
13567 if (sec->reloc_count == 0)
13569 cookie->rels = NULL;
13570 cookie->relend = NULL;
13572 else
13574 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13575 NULL, NULL,
13576 _bfd_link_keep_memory (info));
13577 if (cookie->rels == NULL)
13578 return false;
13579 cookie->rel = cookie->rels;
13580 cookie->relend = cookie->rels + sec->reloc_count;
13582 cookie->rel = cookie->rels;
13583 return true;
13586 /* Free the memory allocated by init_reloc_cookie_rels,
13587 if appropriate. */
13589 static void
13590 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13591 asection *sec)
13593 if (elf_section_data (sec)->relocs != cookie->rels)
13594 free (cookie->rels);
13597 /* Initialize the whole of COOKIE for input section SEC. */
13599 static bool
13600 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13601 struct bfd_link_info *info,
13602 asection *sec)
13604 if (!init_reloc_cookie (cookie, info, sec->owner))
13605 goto error1;
13606 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13607 goto error2;
13608 return true;
13610 error2:
13611 fini_reloc_cookie (cookie, sec->owner);
13612 error1:
13613 return false;
13616 /* Free the memory allocated by init_reloc_cookie_for_section,
13617 if appropriate. */
13619 static void
13620 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13621 asection *sec)
13623 fini_reloc_cookie_rels (cookie, sec);
13624 fini_reloc_cookie (cookie, sec->owner);
13627 /* Garbage collect unused sections. */
13629 /* Default gc_mark_hook. */
13631 asection *
13632 _bfd_elf_gc_mark_hook (asection *sec,
13633 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13634 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13635 struct elf_link_hash_entry *h,
13636 Elf_Internal_Sym *sym)
13638 if (h != NULL)
13640 switch (h->root.type)
13642 case bfd_link_hash_defined:
13643 case bfd_link_hash_defweak:
13644 return h->root.u.def.section;
13646 case bfd_link_hash_common:
13647 return h->root.u.c.p->section;
13649 default:
13650 break;
13653 else
13654 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13656 return NULL;
13659 /* Return the debug definition section. */
13661 static asection *
13662 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13663 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13664 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13665 struct elf_link_hash_entry *h,
13666 Elf_Internal_Sym *sym)
13668 if (h != NULL)
13670 /* Return the global debug definition section. */
13671 if ((h->root.type == bfd_link_hash_defined
13672 || h->root.type == bfd_link_hash_defweak)
13673 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13674 return h->root.u.def.section;
13676 else
13678 /* Return the local debug definition section. */
13679 asection *isec = bfd_section_from_elf_index (sec->owner,
13680 sym->st_shndx);
13681 if ((isec->flags & SEC_DEBUGGING) != 0)
13682 return isec;
13685 return NULL;
13688 /* COOKIE->rel describes a relocation against section SEC, which is
13689 a section we've decided to keep. Return the section that contains
13690 the relocation symbol, or NULL if no section contains it. */
13692 asection *
13693 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13694 elf_gc_mark_hook_fn gc_mark_hook,
13695 struct elf_reloc_cookie *cookie,
13696 bool *start_stop)
13698 unsigned long r_symndx;
13699 struct elf_link_hash_entry *h, *hw;
13701 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13702 if (r_symndx == STN_UNDEF)
13703 return NULL;
13705 if (r_symndx >= cookie->locsymcount
13706 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13708 bool was_marked;
13710 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13711 if (h == NULL)
13713 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13714 sec->owner);
13715 return NULL;
13717 while (h->root.type == bfd_link_hash_indirect
13718 || h->root.type == bfd_link_hash_warning)
13719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13721 was_marked = h->mark;
13722 h->mark = 1;
13723 /* Keep all aliases of the symbol too. If an object symbol
13724 needs to be copied into .dynbss then all of its aliases
13725 should be present as dynamic symbols, not just the one used
13726 on the copy relocation. */
13727 hw = h;
13728 while (hw->is_weakalias)
13730 hw = hw->u.alias;
13731 hw->mark = 1;
13734 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13736 if (info->start_stop_gc)
13737 return NULL;
13739 /* To work around a glibc bug, mark XXX input sections
13740 when there is a reference to __start_XXX or __stop_XXX
13741 symbols. */
13742 else if (start_stop != NULL)
13744 asection *s = h->u2.start_stop_section;
13745 *start_stop = true;
13746 return s;
13750 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13753 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13754 &cookie->locsyms[r_symndx]);
13757 /* COOKIE->rel describes a relocation against section SEC, which is
13758 a section we've decided to keep. Mark the section that contains
13759 the relocation symbol. */
13761 bool
13762 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13763 asection *sec,
13764 elf_gc_mark_hook_fn gc_mark_hook,
13765 struct elf_reloc_cookie *cookie)
13767 asection *rsec;
13768 bool start_stop = false;
13770 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13771 while (rsec != NULL)
13773 if (!rsec->gc_mark)
13775 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13776 || (rsec->owner->flags & DYNAMIC) != 0)
13777 rsec->gc_mark = 1;
13778 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13779 return false;
13781 if (!start_stop)
13782 break;
13783 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13785 return true;
13788 /* The mark phase of garbage collection. For a given section, mark
13789 it and any sections in this section's group, and all the sections
13790 which define symbols to which it refers. */
13792 bool
13793 _bfd_elf_gc_mark (struct bfd_link_info *info,
13794 asection *sec,
13795 elf_gc_mark_hook_fn gc_mark_hook)
13797 bool ret;
13798 asection *group_sec, *eh_frame;
13800 sec->gc_mark = 1;
13802 /* Mark all the sections in the group. */
13803 group_sec = elf_section_data (sec)->next_in_group;
13804 if (group_sec && !group_sec->gc_mark)
13805 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13806 return false;
13808 /* Look through the section relocs. */
13809 ret = true;
13810 eh_frame = elf_eh_frame_section (sec->owner);
13811 if ((sec->flags & SEC_RELOC) != 0
13812 && sec->reloc_count > 0
13813 && sec != eh_frame)
13815 struct elf_reloc_cookie cookie;
13817 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13818 ret = false;
13819 else
13821 for (; cookie.rel < cookie.relend; cookie.rel++)
13822 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13824 ret = false;
13825 break;
13827 fini_reloc_cookie_for_section (&cookie, sec);
13831 if (ret && eh_frame && elf_fde_list (sec))
13833 struct elf_reloc_cookie cookie;
13835 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13836 ret = false;
13837 else
13839 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13840 gc_mark_hook, &cookie))
13841 ret = false;
13842 fini_reloc_cookie_for_section (&cookie, eh_frame);
13846 eh_frame = elf_section_eh_frame_entry (sec);
13847 if (ret && eh_frame && !eh_frame->gc_mark)
13848 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13849 ret = false;
13851 return ret;
13854 /* Scan and mark sections in a special or debug section group. */
13856 static void
13857 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13859 /* Point to first section of section group. */
13860 asection *ssec;
13861 /* Used to iterate the section group. */
13862 asection *msec;
13864 bool is_special_grp = true;
13865 bool is_debug_grp = true;
13867 /* First scan to see if group contains any section other than debug
13868 and special section. */
13869 ssec = msec = elf_next_in_group (grp);
13872 if ((msec->flags & SEC_DEBUGGING) == 0)
13873 is_debug_grp = false;
13875 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13876 is_special_grp = false;
13878 msec = elf_next_in_group (msec);
13880 while (msec != ssec);
13882 /* If this is a pure debug section group or pure special section group,
13883 keep all sections in this group. */
13884 if (is_debug_grp || is_special_grp)
13888 msec->gc_mark = 1;
13889 msec = elf_next_in_group (msec);
13891 while (msec != ssec);
13895 /* Keep debug and special sections. */
13897 bool
13898 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13899 elf_gc_mark_hook_fn mark_hook)
13901 bfd *ibfd;
13903 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13905 asection *isec;
13906 bool some_kept;
13907 bool debug_frag_seen;
13908 bool has_kept_debug_info;
13910 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13911 continue;
13912 isec = ibfd->sections;
13913 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13914 continue;
13916 /* Ensure all linker created sections are kept,
13917 see if any other section is already marked,
13918 and note if we have any fragmented debug sections. */
13919 debug_frag_seen = some_kept = has_kept_debug_info = false;
13920 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13922 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13923 isec->gc_mark = 1;
13924 else if (isec->gc_mark
13925 && (isec->flags & SEC_ALLOC) != 0
13926 && elf_section_type (isec) != SHT_NOTE)
13927 some_kept = true;
13928 else
13930 /* Since all sections, except for backend specific ones,
13931 have been garbage collected, call mark_hook on this
13932 section if any of its linked-to sections is marked. */
13933 asection *linked_to_sec;
13934 for (linked_to_sec = elf_linked_to_section (isec);
13935 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13936 linked_to_sec = elf_linked_to_section (linked_to_sec))
13938 if (linked_to_sec->gc_mark)
13940 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13941 return false;
13942 break;
13944 linked_to_sec->linker_mark = 1;
13946 for (linked_to_sec = elf_linked_to_section (isec);
13947 linked_to_sec != NULL && linked_to_sec->linker_mark;
13948 linked_to_sec = elf_linked_to_section (linked_to_sec))
13949 linked_to_sec->linker_mark = 0;
13952 if (!debug_frag_seen
13953 && (isec->flags & SEC_DEBUGGING)
13954 && startswith (isec->name, ".debug_line."))
13955 debug_frag_seen = true;
13956 else if (strcmp (bfd_section_name (isec),
13957 "__patchable_function_entries") == 0
13958 && elf_linked_to_section (isec) == NULL)
13959 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13960 "need linked-to section "
13961 "for --gc-sections\n"),
13962 isec->owner, isec);
13965 /* If no non-note alloc section in this file will be kept, then
13966 we can toss out the debug and special sections. */
13967 if (!some_kept)
13968 continue;
13970 /* Keep debug and special sections like .comment when they are
13971 not part of a group. Also keep section groups that contain
13972 just debug sections or special sections. NB: Sections with
13973 linked-to section has been handled above. */
13974 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13976 if ((isec->flags & SEC_GROUP) != 0)
13977 _bfd_elf_gc_mark_debug_special_section_group (isec);
13978 else if (((isec->flags & SEC_DEBUGGING) != 0
13979 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13980 && elf_next_in_group (isec) == NULL
13981 && elf_linked_to_section (isec) == NULL)
13982 isec->gc_mark = 1;
13983 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13984 has_kept_debug_info = true;
13987 /* Look for CODE sections which are going to be discarded,
13988 and find and discard any fragmented debug sections which
13989 are associated with that code section. */
13990 if (debug_frag_seen)
13991 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13992 if ((isec->flags & SEC_CODE) != 0
13993 && isec->gc_mark == 0)
13995 unsigned int ilen;
13996 asection *dsec;
13998 ilen = strlen (isec->name);
14000 /* Association is determined by the name of the debug
14001 section containing the name of the code section as
14002 a suffix. For example .debug_line.text.foo is a
14003 debug section associated with .text.foo. */
14004 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14006 unsigned int dlen;
14008 if (dsec->gc_mark == 0
14009 || (dsec->flags & SEC_DEBUGGING) == 0)
14010 continue;
14012 dlen = strlen (dsec->name);
14014 if (dlen > ilen
14015 && strncmp (dsec->name + (dlen - ilen),
14016 isec->name, ilen) == 0)
14017 dsec->gc_mark = 0;
14021 /* Mark debug sections referenced by kept debug sections. */
14022 if (has_kept_debug_info)
14023 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14024 if (isec->gc_mark
14025 && (isec->flags & SEC_DEBUGGING) != 0)
14026 if (!_bfd_elf_gc_mark (info, isec,
14027 elf_gc_mark_debug_section))
14028 return false;
14030 return true;
14033 static bool
14034 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14036 bfd *sub;
14037 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14039 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14041 asection *o;
14043 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14044 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14045 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14046 continue;
14047 o = sub->sections;
14048 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14049 continue;
14051 for (o = sub->sections; o != NULL; o = o->next)
14053 /* When any section in a section group is kept, we keep all
14054 sections in the section group. If the first member of
14055 the section group is excluded, we will also exclude the
14056 group section. */
14057 if (o->flags & SEC_GROUP)
14059 asection *first = elf_next_in_group (o);
14060 o->gc_mark = first->gc_mark;
14063 if (o->gc_mark)
14064 continue;
14066 /* Skip sweeping sections already excluded. */
14067 if (o->flags & SEC_EXCLUDE)
14068 continue;
14070 /* Since this is early in the link process, it is simple
14071 to remove a section from the output. */
14072 o->flags |= SEC_EXCLUDE;
14074 if (info->print_gc_sections && o->size != 0)
14075 /* xgettext:c-format */
14076 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14077 o, sub);
14081 return true;
14084 /* Propagate collected vtable information. This is called through
14085 elf_link_hash_traverse. */
14087 static bool
14088 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14090 /* Those that are not vtables. */
14091 if (h->start_stop
14092 || h->u2.vtable == NULL
14093 || h->u2.vtable->parent == NULL)
14094 return true;
14096 /* Those vtables that do not have parents, we cannot merge. */
14097 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14098 return true;
14100 /* If we've already been done, exit. */
14101 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14102 return true;
14104 /* Make sure the parent's table is up to date. */
14105 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14107 if (h->u2.vtable->used == NULL)
14109 /* None of this table's entries were referenced. Re-use the
14110 parent's table. */
14111 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14112 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14114 else
14116 size_t n;
14117 bool *cu, *pu;
14119 /* Or the parent's entries into ours. */
14120 cu = h->u2.vtable->used;
14121 cu[-1] = true;
14122 pu = h->u2.vtable->parent->u2.vtable->used;
14123 if (pu != NULL)
14125 const struct elf_backend_data *bed;
14126 unsigned int log_file_align;
14128 bed = get_elf_backend_data (h->root.u.def.section->owner);
14129 log_file_align = bed->s->log_file_align;
14130 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14131 while (n--)
14133 if (*pu)
14134 *cu = true;
14135 pu++;
14136 cu++;
14141 return true;
14144 struct link_info_ok
14146 struct bfd_link_info *info;
14147 bool ok;
14150 static bool
14151 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14152 void *ptr)
14154 asection *sec;
14155 bfd_vma hstart, hend;
14156 Elf_Internal_Rela *relstart, *relend, *rel;
14157 const struct elf_backend_data *bed;
14158 unsigned int log_file_align;
14159 struct link_info_ok *info = (struct link_info_ok *) ptr;
14161 /* Take care of both those symbols that do not describe vtables as
14162 well as those that are not loaded. */
14163 if (h->start_stop
14164 || h->u2.vtable == NULL
14165 || h->u2.vtable->parent == NULL)
14166 return true;
14168 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14169 || h->root.type == bfd_link_hash_defweak);
14171 sec = h->root.u.def.section;
14172 hstart = h->root.u.def.value;
14173 hend = hstart + h->size;
14175 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14176 sec, NULL, NULL, true);
14177 if (!relstart)
14178 return info->ok = false;
14179 bed = get_elf_backend_data (sec->owner);
14180 log_file_align = bed->s->log_file_align;
14182 relend = relstart + sec->reloc_count;
14184 for (rel = relstart; rel < relend; ++rel)
14185 if (rel->r_offset >= hstart && rel->r_offset < hend)
14187 /* If the entry is in use, do nothing. */
14188 if (h->u2.vtable->used
14189 && (rel->r_offset - hstart) < h->u2.vtable->size)
14191 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14192 if (h->u2.vtable->used[entry])
14193 continue;
14195 /* Otherwise, kill it. */
14196 rel->r_offset = rel->r_info = rel->r_addend = 0;
14199 return true;
14202 /* Mark sections containing dynamically referenced symbols. When
14203 building shared libraries, we must assume that any visible symbol is
14204 referenced. */
14206 bool
14207 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14209 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14210 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14212 if ((h->root.type == bfd_link_hash_defined
14213 || h->root.type == bfd_link_hash_defweak)
14214 && (!h->start_stop
14215 || h->root.ldscript_def
14216 || !info->start_stop_gc)
14217 && ((h->ref_dynamic && !h->forced_local)
14218 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14219 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14220 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14221 && (!bfd_link_executable (info)
14222 || info->gc_keep_exported
14223 || info->export_dynamic
14224 || (h->dynamic
14225 && d != NULL
14226 && (*d->match) (&d->head, NULL, h->root.root.string)))
14227 && (h->versioned >= versioned
14228 || !bfd_hide_sym_by_version (info->version_info,
14229 h->root.root.string)))))
14230 h->root.u.def.section->flags |= SEC_KEEP;
14232 return true;
14235 /* Keep all sections containing symbols undefined on the command-line,
14236 and the section containing the entry symbol. */
14238 void
14239 _bfd_elf_gc_keep (struct bfd_link_info *info)
14241 struct bfd_sym_chain *sym;
14243 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14245 struct elf_link_hash_entry *h;
14247 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14248 false, false, false);
14250 if (h != NULL
14251 && (h->root.type == bfd_link_hash_defined
14252 || h->root.type == bfd_link_hash_defweak)
14253 && !bfd_is_const_section (h->root.u.def.section))
14254 h->root.u.def.section->flags |= SEC_KEEP;
14258 bool
14259 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14260 struct bfd_link_info *info)
14262 bfd *ibfd = info->input_bfds;
14264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14266 asection *sec;
14267 struct elf_reloc_cookie cookie;
14269 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14270 continue;
14271 sec = ibfd->sections;
14272 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14273 continue;
14275 if (!init_reloc_cookie (&cookie, info, ibfd))
14276 return false;
14278 for (sec = ibfd->sections; sec; sec = sec->next)
14280 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14281 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14283 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14284 fini_reloc_cookie_rels (&cookie, sec);
14288 return true;
14291 /* Do mark and sweep of unused sections. */
14293 bool
14294 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14296 bool ok = true;
14297 bfd *sub;
14298 elf_gc_mark_hook_fn gc_mark_hook;
14299 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14300 struct elf_link_hash_table *htab;
14301 struct link_info_ok info_ok;
14303 if (!bed->can_gc_sections
14304 || !is_elf_hash_table (info->hash))
14306 _bfd_error_handler(_("warning: gc-sections option ignored"));
14307 return true;
14310 bed->gc_keep (info);
14311 htab = elf_hash_table (info);
14313 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14314 at the .eh_frame section if we can mark the FDEs individually. */
14315 for (sub = info->input_bfds;
14316 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14317 sub = sub->link.next)
14319 asection *sec;
14320 struct elf_reloc_cookie cookie;
14322 sec = sub->sections;
14323 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14324 continue;
14325 sec = bfd_get_section_by_name (sub, ".eh_frame");
14326 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14328 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14329 if (elf_section_data (sec)->sec_info
14330 && (sec->flags & SEC_LINKER_CREATED) == 0)
14331 elf_eh_frame_section (sub) = sec;
14332 fini_reloc_cookie_for_section (&cookie, sec);
14333 sec = bfd_get_next_section_by_name (NULL, sec);
14337 /* Apply transitive closure to the vtable entry usage info. */
14338 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14339 if (!ok)
14340 return false;
14342 /* Kill the vtable relocations that were not used. */
14343 info_ok.info = info;
14344 info_ok.ok = true;
14345 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14346 if (!info_ok.ok)
14347 return false;
14349 /* Mark dynamically referenced symbols. */
14350 if (htab->dynamic_sections_created || info->gc_keep_exported)
14351 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14353 /* Grovel through relocs to find out who stays ... */
14354 gc_mark_hook = bed->gc_mark_hook;
14355 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14357 asection *o;
14359 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14360 || elf_object_id (sub) != elf_hash_table_id (htab)
14361 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14362 continue;
14364 o = sub->sections;
14365 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14366 continue;
14368 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14369 Also treat note sections as a root, if the section is not part
14370 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14371 well as FINI_ARRAY sections for ld -r. */
14372 for (o = sub->sections; o != NULL; o = o->next)
14373 if (!o->gc_mark
14374 && (o->flags & SEC_EXCLUDE) == 0
14375 && ((o->flags & SEC_KEEP) != 0
14376 || (bfd_link_relocatable (info)
14377 && ((elf_section_data (o)->this_hdr.sh_type
14378 == SHT_PREINIT_ARRAY)
14379 || (elf_section_data (o)->this_hdr.sh_type
14380 == SHT_INIT_ARRAY)
14381 || (elf_section_data (o)->this_hdr.sh_type
14382 == SHT_FINI_ARRAY)))
14383 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14384 && elf_next_in_group (o) == NULL
14385 && elf_linked_to_section (o) == NULL)
14386 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14387 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14389 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14390 return false;
14394 /* Allow the backend to mark additional target specific sections. */
14395 bed->gc_mark_extra_sections (info, gc_mark_hook);
14397 /* ... and mark SEC_EXCLUDE for those that go. */
14398 return elf_gc_sweep (abfd, info);
14401 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14403 bool
14404 bfd_elf_gc_record_vtinherit (bfd *abfd,
14405 asection *sec,
14406 struct elf_link_hash_entry *h,
14407 bfd_vma offset)
14409 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14410 struct elf_link_hash_entry **search, *child;
14411 size_t extsymcount;
14412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14414 /* The sh_info field of the symtab header tells us where the
14415 external symbols start. We don't care about the local symbols at
14416 this point. */
14417 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14418 if (!elf_bad_symtab (abfd))
14419 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14421 sym_hashes = elf_sym_hashes (abfd);
14422 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14424 /* Hunt down the child symbol, which is in this section at the same
14425 offset as the relocation. */
14426 for (search = sym_hashes; search != sym_hashes_end; ++search)
14428 if ((child = *search) != NULL
14429 && (child->root.type == bfd_link_hash_defined
14430 || child->root.type == bfd_link_hash_defweak)
14431 && child->root.u.def.section == sec
14432 && child->root.u.def.value == offset)
14433 goto win;
14436 /* xgettext:c-format */
14437 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14438 abfd, sec, (uint64_t) offset);
14439 bfd_set_error (bfd_error_invalid_operation);
14440 return false;
14442 win:
14443 if (!child->u2.vtable)
14445 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14446 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14447 if (!child->u2.vtable)
14448 return false;
14450 if (!h)
14452 /* This *should* only be the absolute section. It could potentially
14453 be that someone has defined a non-global vtable though, which
14454 would be bad. It isn't worth paging in the local symbols to be
14455 sure though; that case should simply be handled by the assembler. */
14457 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14459 else
14460 child->u2.vtable->parent = h;
14462 return true;
14465 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14467 bool
14468 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14469 struct elf_link_hash_entry *h,
14470 bfd_vma addend)
14472 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14473 unsigned int log_file_align = bed->s->log_file_align;
14475 if (!h)
14477 /* xgettext:c-format */
14478 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14479 abfd, sec);
14480 bfd_set_error (bfd_error_bad_value);
14481 return false;
14484 if (!h->u2.vtable)
14486 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14487 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14488 if (!h->u2.vtable)
14489 return false;
14492 if (addend >= h->u2.vtable->size)
14494 size_t size, bytes, file_align;
14495 bool *ptr = h->u2.vtable->used;
14497 /* While the symbol is undefined, we have to be prepared to handle
14498 a zero size. */
14499 file_align = 1 << log_file_align;
14500 if (h->root.type == bfd_link_hash_undefined)
14501 size = addend + file_align;
14502 else
14504 size = h->size;
14505 if (addend >= size)
14507 /* Oops! We've got a reference past the defined end of
14508 the table. This is probably a bug -- shall we warn? */
14509 size = addend + file_align;
14512 size = (size + file_align - 1) & -file_align;
14514 /* Allocate one extra entry for use as a "done" flag for the
14515 consolidation pass. */
14516 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14518 if (ptr)
14520 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14522 if (ptr != NULL)
14524 size_t oldbytes;
14526 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14527 * sizeof (bool));
14528 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14531 else
14532 ptr = (bool *) bfd_zmalloc (bytes);
14534 if (ptr == NULL)
14535 return false;
14537 /* And arrange for that done flag to be at index -1. */
14538 h->u2.vtable->used = ptr + 1;
14539 h->u2.vtable->size = size;
14542 h->u2.vtable->used[addend >> log_file_align] = true;
14544 return true;
14547 /* Map an ELF section header flag to its corresponding string. */
14548 typedef struct
14550 char *flag_name;
14551 flagword flag_value;
14552 } elf_flags_to_name_table;
14554 static const elf_flags_to_name_table elf_flags_to_names [] =
14556 { "SHF_WRITE", SHF_WRITE },
14557 { "SHF_ALLOC", SHF_ALLOC },
14558 { "SHF_EXECINSTR", SHF_EXECINSTR },
14559 { "SHF_MERGE", SHF_MERGE },
14560 { "SHF_STRINGS", SHF_STRINGS },
14561 { "SHF_INFO_LINK", SHF_INFO_LINK},
14562 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14563 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14564 { "SHF_GROUP", SHF_GROUP },
14565 { "SHF_TLS", SHF_TLS },
14566 { "SHF_MASKOS", SHF_MASKOS },
14567 { "SHF_EXCLUDE", SHF_EXCLUDE },
14570 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14571 bool
14572 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14573 struct flag_info *flaginfo,
14574 asection *section)
14576 const bfd_vma sh_flags = elf_section_flags (section);
14578 if (!flaginfo->flags_initialized)
14580 bfd *obfd = info->output_bfd;
14581 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14582 struct flag_info_list *tf = flaginfo->flag_list;
14583 int with_hex = 0;
14584 int without_hex = 0;
14586 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14588 unsigned i;
14589 flagword (*lookup) (char *);
14591 lookup = bed->elf_backend_lookup_section_flags_hook;
14592 if (lookup != NULL)
14594 flagword hexval = (*lookup) ((char *) tf->name);
14596 if (hexval != 0)
14598 if (tf->with == with_flags)
14599 with_hex |= hexval;
14600 else if (tf->with == without_flags)
14601 without_hex |= hexval;
14602 tf->valid = true;
14603 continue;
14606 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14608 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14610 if (tf->with == with_flags)
14611 with_hex |= elf_flags_to_names[i].flag_value;
14612 else if (tf->with == without_flags)
14613 without_hex |= elf_flags_to_names[i].flag_value;
14614 tf->valid = true;
14615 break;
14618 if (!tf->valid)
14620 info->callbacks->einfo
14621 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14622 return false;
14625 flaginfo->flags_initialized = true;
14626 flaginfo->only_with_flags |= with_hex;
14627 flaginfo->not_with_flags |= without_hex;
14630 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14631 return false;
14633 if ((flaginfo->not_with_flags & sh_flags) != 0)
14634 return false;
14636 return true;
14639 struct alloc_got_off_arg {
14640 bfd_vma gotoff;
14641 struct bfd_link_info *info;
14644 /* We need a special top-level link routine to convert got reference counts
14645 to real got offsets. */
14647 static bool
14648 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14650 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14651 bfd *obfd = gofarg->info->output_bfd;
14652 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14654 if (h->got.refcount > 0)
14656 h->got.offset = gofarg->gotoff;
14657 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14659 else
14660 h->got.offset = (bfd_vma) -1;
14662 return true;
14665 /* And an accompanying bit to work out final got entry offsets once
14666 we're done. Should be called from final_link. */
14668 bool
14669 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14670 struct bfd_link_info *info)
14672 bfd *i;
14673 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14674 bfd_vma gotoff;
14675 struct alloc_got_off_arg gofarg;
14677 BFD_ASSERT (abfd == info->output_bfd);
14679 if (! is_elf_hash_table (info->hash))
14680 return false;
14682 /* The GOT offset is relative to the .got section, but the GOT header is
14683 put into the .got.plt section, if the backend uses it. */
14684 if (bed->want_got_plt)
14685 gotoff = 0;
14686 else
14687 gotoff = bed->got_header_size;
14689 /* Do the local .got entries first. */
14690 for (i = info->input_bfds; i; i = i->link.next)
14692 bfd_signed_vma *local_got;
14693 size_t j, locsymcount;
14694 Elf_Internal_Shdr *symtab_hdr;
14696 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14697 continue;
14699 local_got = elf_local_got_refcounts (i);
14700 if (!local_got)
14701 continue;
14703 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14704 if (elf_bad_symtab (i))
14705 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14706 else
14707 locsymcount = symtab_hdr->sh_info;
14709 for (j = 0; j < locsymcount; ++j)
14711 if (local_got[j] > 0)
14713 local_got[j] = gotoff;
14714 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14716 else
14717 local_got[j] = (bfd_vma) -1;
14721 /* Then the global .got entries. .plt refcounts are handled by
14722 adjust_dynamic_symbol */
14723 gofarg.gotoff = gotoff;
14724 gofarg.info = info;
14725 elf_link_hash_traverse (elf_hash_table (info),
14726 elf_gc_allocate_got_offsets,
14727 &gofarg);
14728 return true;
14731 /* Many folk need no more in the way of final link than this, once
14732 got entry reference counting is enabled. */
14734 bool
14735 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14737 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14738 return false;
14740 /* Invoke the regular ELF backend linker to do all the work. */
14741 return bfd_elf_final_link (abfd, info);
14744 bool
14745 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14747 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14749 if (rcookie->bad_symtab)
14750 rcookie->rel = rcookie->rels;
14752 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14754 unsigned long r_symndx;
14756 if (! rcookie->bad_symtab)
14757 if (rcookie->rel->r_offset > offset)
14758 return false;
14759 if (rcookie->rel->r_offset != offset)
14760 continue;
14762 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14763 if (r_symndx == STN_UNDEF)
14764 return true;
14766 if (r_symndx >= rcookie->locsymcount
14767 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14769 struct elf_link_hash_entry *h;
14771 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14773 while (h->root.type == bfd_link_hash_indirect
14774 || h->root.type == bfd_link_hash_warning)
14775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14777 if ((h->root.type == bfd_link_hash_defined
14778 || h->root.type == bfd_link_hash_defweak)
14779 && (h->root.u.def.section->owner != rcookie->abfd
14780 || h->root.u.def.section->kept_section != NULL
14781 || discarded_section (h->root.u.def.section)))
14782 return true;
14784 else
14786 /* It's not a relocation against a global symbol,
14787 but it could be a relocation against a local
14788 symbol for a discarded section. */
14789 asection *isec;
14790 Elf_Internal_Sym *isym;
14792 /* Need to: get the symbol; get the section. */
14793 isym = &rcookie->locsyms[r_symndx];
14794 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14795 if (isec != NULL
14796 && (isec->kept_section != NULL
14797 || discarded_section (isec)))
14798 return true;
14800 return false;
14802 return false;
14805 /* Discard unneeded references to discarded sections.
14806 Returns -1 on error, 1 if any section's size was changed, 0 if
14807 nothing changed. This function assumes that the relocations are in
14808 sorted order, which is true for all known assemblers. */
14811 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14813 struct elf_reloc_cookie cookie;
14814 asection *o;
14815 bfd *abfd;
14816 int changed = 0;
14818 if (info->traditional_format
14819 || !is_elf_hash_table (info->hash))
14820 return 0;
14822 o = bfd_get_section_by_name (output_bfd, ".stab");
14823 if (o != NULL)
14825 asection *i;
14827 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14829 if (i->size == 0
14830 || i->reloc_count == 0
14831 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14832 continue;
14834 abfd = i->owner;
14835 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14836 continue;
14838 if (!init_reloc_cookie_for_section (&cookie, info, i))
14839 return -1;
14841 if (_bfd_discard_section_stabs (abfd, i,
14842 elf_section_data (i)->sec_info,
14843 bfd_elf_reloc_symbol_deleted_p,
14844 &cookie))
14845 changed = 1;
14847 fini_reloc_cookie_for_section (&cookie, i);
14851 o = NULL;
14852 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14853 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14854 if (o != NULL)
14856 asection *i;
14857 int eh_changed = 0;
14858 unsigned int eh_alignment; /* Octets. */
14860 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14862 if (i->size == 0)
14863 continue;
14865 abfd = i->owner;
14866 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14867 continue;
14869 if (!init_reloc_cookie_for_section (&cookie, info, i))
14870 return -1;
14872 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14873 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14874 bfd_elf_reloc_symbol_deleted_p,
14875 &cookie))
14877 eh_changed = 1;
14878 if (i->size != i->rawsize)
14879 changed = 1;
14882 fini_reloc_cookie_for_section (&cookie, i);
14885 eh_alignment = ((1 << o->alignment_power)
14886 * bfd_octets_per_byte (output_bfd, o));
14887 /* Skip over zero terminator, and prevent empty sections from
14888 adding alignment padding at the end. */
14889 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14890 if (i->size == 0)
14891 i->flags |= SEC_EXCLUDE;
14892 else if (i->size > 4)
14893 break;
14894 /* The last non-empty eh_frame section doesn't need padding. */
14895 if (i != NULL)
14896 i = i->map_tail.s;
14897 /* Any prior sections must pad the last FDE out to the output
14898 section alignment. Otherwise we might have zero padding
14899 between sections, which would be seen as a terminator. */
14900 for (; i != NULL; i = i->map_tail.s)
14901 if (i->size == 4)
14902 /* All but the last zero terminator should have been removed. */
14903 BFD_FAIL ();
14904 else
14906 bfd_size_type size
14907 = (i->size + eh_alignment - 1) & -eh_alignment;
14908 if (i->size != size)
14910 i->size = size;
14911 changed = 1;
14912 eh_changed = 1;
14915 if (eh_changed)
14916 elf_link_hash_traverse (elf_hash_table (info),
14917 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14920 o = bfd_get_section_by_name (output_bfd, ".sframe");
14921 if (o != NULL)
14923 asection *i;
14925 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14927 if (i->size == 0)
14928 continue;
14930 abfd = i->owner;
14931 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14932 continue;
14934 if (!init_reloc_cookie_for_section (&cookie, info, i))
14935 return -1;
14937 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
14939 if (_bfd_elf_discard_section_sframe (i,
14940 bfd_elf_reloc_symbol_deleted_p,
14941 &cookie))
14943 if (i->size != i->rawsize)
14944 changed = 1;
14947 fini_reloc_cookie_for_section (&cookie, i);
14949 /* Update the reference to the output .sframe section. Used to
14950 determine later if PT_GNU_SFRAME segment is to be generated. */
14951 if (!_bfd_elf_set_section_sframe (output_bfd, info))
14952 return -1;
14955 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14957 const struct elf_backend_data *bed;
14958 asection *s;
14960 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14961 continue;
14962 s = abfd->sections;
14963 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14964 continue;
14966 bed = get_elf_backend_data (abfd);
14968 if (bed->elf_backend_discard_info != NULL)
14970 if (!init_reloc_cookie (&cookie, info, abfd))
14971 return -1;
14973 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14974 changed = 1;
14976 fini_reloc_cookie (&cookie, abfd);
14980 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14981 _bfd_elf_end_eh_frame_parsing (info);
14983 if (info->eh_frame_hdr_type
14984 && !bfd_link_relocatable (info)
14985 && _bfd_elf_discard_section_eh_frame_hdr (info))
14986 changed = 1;
14988 return changed;
14991 bool
14992 _bfd_elf_section_already_linked (bfd *abfd,
14993 asection *sec,
14994 struct bfd_link_info *info)
14996 flagword flags;
14997 const char *name, *key;
14998 struct bfd_section_already_linked *l;
14999 struct bfd_section_already_linked_hash_entry *already_linked_list;
15001 if (sec->output_section == bfd_abs_section_ptr)
15002 return false;
15004 flags = sec->flags;
15006 /* Return if it isn't a linkonce section. A comdat group section
15007 also has SEC_LINK_ONCE set. */
15008 if ((flags & SEC_LINK_ONCE) == 0)
15009 return false;
15011 /* Don't put group member sections on our list of already linked
15012 sections. They are handled as a group via their group section. */
15013 if (elf_sec_group (sec) != NULL)
15014 return false;
15016 /* For a SHT_GROUP section, use the group signature as the key. */
15017 name = sec->name;
15018 if ((flags & SEC_GROUP) != 0
15019 && elf_next_in_group (sec) != NULL
15020 && elf_group_name (elf_next_in_group (sec)) != NULL)
15021 key = elf_group_name (elf_next_in_group (sec));
15022 else
15024 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15025 if (startswith (name, ".gnu.linkonce.")
15026 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15027 key++;
15028 else
15029 /* Must be a user linkonce section that doesn't follow gcc's
15030 naming convention. In this case we won't be matching
15031 single member groups. */
15032 key = name;
15035 already_linked_list = bfd_section_already_linked_table_lookup (key);
15037 for (l = already_linked_list->entry; l != NULL; l = l->next)
15039 /* We may have 2 different types of sections on the list: group
15040 sections with a signature of <key> (<key> is some string),
15041 and linkonce sections named .gnu.linkonce.<type>.<key>.
15042 Match like sections. LTO plugin sections are an exception.
15043 They are always named .gnu.linkonce.t.<key> and match either
15044 type of section. */
15045 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15046 && ((flags & SEC_GROUP) != 0
15047 || strcmp (name, l->sec->name) == 0))
15048 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15049 || (sec->owner->flags & BFD_PLUGIN) != 0)
15051 /* The section has already been linked. See if we should
15052 issue a warning. */
15053 if (!_bfd_handle_already_linked (sec, l, info))
15054 return false;
15056 if (flags & SEC_GROUP)
15058 asection *first = elf_next_in_group (sec);
15059 asection *s = first;
15061 while (s != NULL)
15063 s->output_section = bfd_abs_section_ptr;
15064 /* Record which group discards it. */
15065 s->kept_section = l->sec;
15066 s = elf_next_in_group (s);
15067 /* These lists are circular. */
15068 if (s == first)
15069 break;
15073 return true;
15077 /* A single member comdat group section may be discarded by a
15078 linkonce section and vice versa. */
15079 if ((flags & SEC_GROUP) != 0)
15081 asection *first = elf_next_in_group (sec);
15083 if (first != NULL && elf_next_in_group (first) == first)
15084 /* Check this single member group against linkonce sections. */
15085 for (l = already_linked_list->entry; l != NULL; l = l->next)
15086 if ((l->sec->flags & SEC_GROUP) == 0
15087 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15089 first->output_section = bfd_abs_section_ptr;
15090 first->kept_section = l->sec;
15091 sec->output_section = bfd_abs_section_ptr;
15092 break;
15095 else
15096 /* Check this linkonce section against single member groups. */
15097 for (l = already_linked_list->entry; l != NULL; l = l->next)
15098 if (l->sec->flags & SEC_GROUP)
15100 asection *first = elf_next_in_group (l->sec);
15102 if (first != NULL
15103 && elf_next_in_group (first) == first
15104 && bfd_elf_match_symbols_in_sections (first, sec, info))
15106 sec->output_section = bfd_abs_section_ptr;
15107 sec->kept_section = first;
15108 break;
15112 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15113 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15114 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15115 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15116 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15117 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15118 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15119 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15120 The reverse order cannot happen as there is never a bfd with only the
15121 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15122 matter as here were are looking only for cross-bfd sections. */
15124 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15125 for (l = already_linked_list->entry; l != NULL; l = l->next)
15126 if ((l->sec->flags & SEC_GROUP) == 0
15127 && startswith (l->sec->name, ".gnu.linkonce.t."))
15129 if (abfd != l->sec->owner)
15130 sec->output_section = bfd_abs_section_ptr;
15131 break;
15134 /* This is the first section with this name. Record it. */
15135 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15136 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15137 return sec->output_section == bfd_abs_section_ptr;
15140 bool
15141 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15143 return sym->st_shndx == SHN_COMMON;
15146 unsigned int
15147 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15149 return SHN_COMMON;
15152 asection *
15153 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15155 return bfd_com_section_ptr;
15158 bfd_vma
15159 _bfd_elf_default_got_elt_size (bfd *abfd,
15160 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15161 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15162 bfd *ibfd ATTRIBUTE_UNUSED,
15163 unsigned long symndx ATTRIBUTE_UNUSED)
15165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15166 return bed->s->arch_size / 8;
15169 /* Routines to support the creation of dynamic relocs. */
15171 /* Returns the name of the dynamic reloc section associated with SEC. */
15173 static const char *
15174 get_dynamic_reloc_section_name (bfd * abfd,
15175 asection * sec,
15176 bool is_rela)
15178 char *name;
15179 const char *old_name = bfd_section_name (sec);
15180 const char *prefix = is_rela ? ".rela" : ".rel";
15182 if (old_name == NULL)
15183 return NULL;
15185 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15186 sprintf (name, "%s%s", prefix, old_name);
15188 return name;
15191 /* Returns the dynamic reloc section associated with SEC.
15192 If necessary compute the name of the dynamic reloc section based
15193 on SEC's name (looked up in ABFD's string table) and the setting
15194 of IS_RELA. */
15196 asection *
15197 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15198 asection *sec,
15199 bool is_rela)
15201 asection *reloc_sec = elf_section_data (sec)->sreloc;
15203 if (reloc_sec == NULL)
15205 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15207 if (name != NULL)
15209 reloc_sec = bfd_get_linker_section (abfd, name);
15211 if (reloc_sec != NULL)
15212 elf_section_data (sec)->sreloc = reloc_sec;
15216 return reloc_sec;
15219 /* Returns the dynamic reloc section associated with SEC. If the
15220 section does not exist it is created and attached to the DYNOBJ
15221 bfd and stored in the SRELOC field of SEC's elf_section_data
15222 structure.
15224 ALIGNMENT is the alignment for the newly created section and
15225 IS_RELA defines whether the name should be .rela.<SEC's name>
15226 or .rel.<SEC's name>. The section name is looked up in the
15227 string table associated with ABFD. */
15229 asection *
15230 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15231 bfd *dynobj,
15232 unsigned int alignment,
15233 bfd *abfd,
15234 bool is_rela)
15236 asection * reloc_sec = elf_section_data (sec)->sreloc;
15238 if (reloc_sec == NULL)
15240 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15242 if (name == NULL)
15243 return NULL;
15245 reloc_sec = bfd_get_linker_section (dynobj, name);
15247 if (reloc_sec == NULL)
15249 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15250 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15251 if ((sec->flags & SEC_ALLOC) != 0)
15252 flags |= SEC_ALLOC | SEC_LOAD;
15254 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15255 if (reloc_sec != NULL)
15257 /* _bfd_elf_get_sec_type_attr chooses a section type by
15258 name. Override as it may be wrong, eg. for a user
15259 section named "auto" we'll get ".relauto" which is
15260 seen to be a .rela section. */
15261 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15262 if (!bfd_set_section_alignment (reloc_sec, alignment))
15263 reloc_sec = NULL;
15267 elf_section_data (sec)->sreloc = reloc_sec;
15270 return reloc_sec;
15273 /* Copy the ELF symbol type and other attributes for a linker script
15274 assignment from HSRC to HDEST. Generally this should be treated as
15275 if we found a strong non-dynamic definition for HDEST (except that
15276 ld ignores multiple definition errors). */
15277 void
15278 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15279 struct bfd_link_hash_entry *hdest,
15280 struct bfd_link_hash_entry *hsrc)
15282 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15283 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15284 Elf_Internal_Sym isym;
15286 ehdest->type = ehsrc->type;
15287 ehdest->target_internal = ehsrc->target_internal;
15289 isym.st_other = ehsrc->other;
15290 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15293 /* Append a RELA relocation REL to section S in BFD. */
15295 void
15296 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15299 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15300 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15301 bed->s->swap_reloca_out (abfd, rel, loc);
15304 /* Append a REL relocation REL to section S in BFD. */
15306 void
15307 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15309 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15310 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15311 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15312 bed->s->swap_reloc_out (abfd, rel, loc);
15315 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15317 struct bfd_link_hash_entry *
15318 bfd_elf_define_start_stop (struct bfd_link_info *info,
15319 const char *symbol, asection *sec)
15321 struct elf_link_hash_entry *h;
15323 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15324 false, false, true);
15325 /* NB: Common symbols will be turned into definition later. */
15326 if (h != NULL
15327 && !h->root.ldscript_def
15328 && (h->root.type == bfd_link_hash_undefined
15329 || h->root.type == bfd_link_hash_undefweak
15330 || ((h->ref_regular || h->def_dynamic)
15331 && !h->def_regular
15332 && h->root.type != bfd_link_hash_common)))
15334 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15335 h->verinfo.verdef = NULL;
15336 h->root.type = bfd_link_hash_defined;
15337 h->root.u.def.section = sec;
15338 h->root.u.def.value = 0;
15339 h->def_regular = 1;
15340 h->def_dynamic = 0;
15341 h->start_stop = 1;
15342 h->u2.start_stop_section = sec;
15343 if (symbol[0] == '.')
15345 /* .startof. and .sizeof. symbols are local. */
15346 const struct elf_backend_data *bed;
15347 bed = get_elf_backend_data (info->output_bfd);
15348 (*bed->elf_backend_hide_symbol) (info, h, true);
15350 else
15352 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15353 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15354 | info->start_stop_visibility);
15355 if (was_dynamic)
15356 bfd_elf_link_record_dynamic_symbol (info, h);
15358 return &h->root;
15360 return NULL;
15363 /* Find dynamic relocs for H that apply to read-only sections. */
15365 asection *
15366 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15368 struct elf_dyn_relocs *p;
15370 for (p = h->dyn_relocs; p != NULL; p = p->next)
15372 asection *s = p->sec->output_section;
15374 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15375 return p->sec;
15377 return NULL;
15380 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15381 read-only sections. */
15383 bool
15384 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15386 asection *sec;
15388 if (h->root.type == bfd_link_hash_indirect)
15389 return true;
15391 sec = _bfd_elf_readonly_dynrelocs (h);
15392 if (sec != NULL)
15394 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15396 info->flags |= DF_TEXTREL;
15397 /* xgettext:c-format */
15398 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15399 "in read-only section `%pA'\n"),
15400 sec->owner, h->root.root.string, sec);
15402 if (bfd_link_textrel_check (info))
15403 /* xgettext:c-format */
15404 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15405 "in read-only section `%pA'\n"),
15406 sec->owner, h->root.root.string, sec);
15408 /* Not an error, just cut short the traversal. */
15409 return false;
15411 return true;
15414 /* Add dynamic tags. */
15416 bool
15417 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15418 bool need_dynamic_reloc)
15420 struct elf_link_hash_table *htab = elf_hash_table (info);
15422 if (htab->dynamic_sections_created)
15424 /* Add some entries to the .dynamic section. We fill in the
15425 values later, in finish_dynamic_sections, but we must add
15426 the entries now so that we get the correct size for the
15427 .dynamic section. The DT_DEBUG entry is filled in by the
15428 dynamic linker and used by the debugger. */
15429 #define add_dynamic_entry(TAG, VAL) \
15430 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15432 const struct elf_backend_data *bed
15433 = get_elf_backend_data (output_bfd);
15435 if (bfd_link_executable (info))
15437 if (!add_dynamic_entry (DT_DEBUG, 0))
15438 return false;
15441 if (htab->dt_pltgot_required || htab->splt->size != 0)
15443 /* DT_PLTGOT is used by prelink even if there is no PLT
15444 relocation. */
15445 if (!add_dynamic_entry (DT_PLTGOT, 0))
15446 return false;
15449 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15451 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15452 || !add_dynamic_entry (DT_PLTREL,
15453 (bed->rela_plts_and_copies_p
15454 ? DT_RELA : DT_REL))
15455 || !add_dynamic_entry (DT_JMPREL, 0))
15456 return false;
15459 if (htab->tlsdesc_plt
15460 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15461 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15462 return false;
15464 if (need_dynamic_reloc)
15466 if (bed->rela_plts_and_copies_p)
15468 if (!add_dynamic_entry (DT_RELA, 0)
15469 || !add_dynamic_entry (DT_RELASZ, 0)
15470 || !add_dynamic_entry (DT_RELAENT,
15471 bed->s->sizeof_rela))
15472 return false;
15474 else
15476 if (!add_dynamic_entry (DT_REL, 0)
15477 || !add_dynamic_entry (DT_RELSZ, 0)
15478 || !add_dynamic_entry (DT_RELENT,
15479 bed->s->sizeof_rel))
15480 return false;
15483 /* If any dynamic relocs apply to a read-only section,
15484 then we need a DT_TEXTREL entry. */
15485 if ((info->flags & DF_TEXTREL) == 0)
15486 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15487 info);
15489 if ((info->flags & DF_TEXTREL) != 0)
15491 if (htab->ifunc_resolvers)
15492 info->callbacks->einfo
15493 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15494 "may result in a segfault at runtime; recompile with %s\n"),
15495 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15497 if (!add_dynamic_entry (DT_TEXTREL, 0))
15498 return false;
15502 #undef add_dynamic_entry
15504 return true;