Update my e-mail address.
[binutils-gdb.git] / bfd / elflink.c
blob122549c594b1034553383200abe225f1146cf95d
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 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 "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
42 bfd_boolean failed;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
84 else
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
99 return NULL;
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 bh = &h->root;
124 else
125 bh = NULL;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
134 h->def_regular = 1;
135 h->non_elf = 0;
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 return h;
145 bfd_boolean
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
148 flagword flags;
149 asection *s;
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
156 return TRUE;
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 if (s == NULL
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
182 return FALSE;
183 htab->sgotplt = s;
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
198 if (h == NULL)
199 return FALSE;
202 return TRUE;
205 /* Create a strtab to hold the dynamic symbol names. */
206 static bfd_boolean
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
220 bfd *ibfd;
221 asection *s;
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 if ((ibfd->flags
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
229 abfd = ibfd;
230 break;
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
242 return TRUE;
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252 bfd_boolean
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
332 if (info->emit_hash)
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
367 return TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
372 bfd_boolean
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
488 return TRUE;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
499 bfd_boolean
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
526 default:
527 break;
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
556 if (p != NULL)
557 *p = ELF_VER_CHR;
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
564 return TRUE;
567 /* Mark a symbol dynamic. */
569 static void
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 h->dynamic = 1;
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
595 bfd_boolean
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
598 const char *name,
599 bfd_boolean provide,
600 bfd_boolean hidden)
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
606 if (!is_elf_hash_table (info->hash))
607 return TRUE;
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
611 if (h == NULL)
612 return provide;
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
617 if (h->versioned == unknown)
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
652 break;
653 case bfd_link_hash_new:
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
670 default:
671 BFD_FAIL ();
672 return FALSE;
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
680 && h->def_dynamic
681 && !h->def_regular)
682 h->root.type = bfd_link_hash_undefined;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
688 if (!provide
689 && h->def_dynamic
690 && !h->def_regular)
691 h->verinfo.verdef = NULL;
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
696 h->def_regular = 1;
698 if (hidden)
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
708 if (!bfd_link_relocatable (info)
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
714 if ((h->def_dynamic
715 || h->ref_dynamic
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
718 && h->dynindx == -1)
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
721 return FALSE;
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
726 if (h->u.weakdef != NULL
727 && h->u.weakdef->dynindx == -1)
729 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
730 return FALSE;
734 return TRUE;
737 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
738 success, and 2 on a failure caused by attempting to record a symbol
739 in a discarded section, eg. a discarded link-once section symbol. */
742 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
743 bfd *input_bfd,
744 long input_indx)
746 bfd_size_type amt;
747 struct elf_link_local_dynamic_entry *entry;
748 struct elf_link_hash_table *eht;
749 struct elf_strtab_hash *dynstr;
750 size_t dynstr_index;
751 char *name;
752 Elf_External_Sym_Shndx eshndx;
753 char esym[sizeof (Elf64_External_Sym)];
755 if (! is_elf_hash_table (info->hash))
756 return 0;
758 /* See if the entry exists already. */
759 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
760 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
761 return 1;
763 amt = sizeof (*entry);
764 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
765 if (entry == NULL)
766 return 0;
768 /* Go find the symbol, so that we can find it's name. */
769 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
770 1, input_indx, &entry->isym, esym, &eshndx))
772 bfd_release (input_bfd, entry);
773 return 0;
776 if (entry->isym.st_shndx != SHN_UNDEF
777 && entry->isym.st_shndx < SHN_LORESERVE)
779 asection *s;
781 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
782 if (s == NULL || bfd_is_abs_section (s->output_section))
784 /* We can still bfd_release here as nothing has done another
785 bfd_alloc. We can't do this later in this function. */
786 bfd_release (input_bfd, entry);
787 return 2;
791 name = (bfd_elf_string_from_elf_section
792 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
793 entry->isym.st_name));
795 dynstr = elf_hash_table (info)->dynstr;
796 if (dynstr == NULL)
798 /* Create a strtab to hold the dynamic symbol names. */
799 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
800 if (dynstr == NULL)
801 return 0;
804 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
805 if (dynstr_index == (size_t) -1)
806 return 0;
807 entry->isym.st_name = dynstr_index;
809 eht = elf_hash_table (info);
811 entry->next = eht->dynlocal;
812 eht->dynlocal = entry;
813 entry->input_bfd = input_bfd;
814 entry->input_indx = input_indx;
815 eht->dynsymcount++;
817 /* Whatever binding the symbol had before, it's now local. */
818 entry->isym.st_info
819 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
821 /* The dynindx will be set at the end of size_dynamic_sections. */
823 return 1;
826 /* Return the dynindex of a local dynamic symbol. */
828 long
829 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
830 bfd *input_bfd,
831 long input_indx)
833 struct elf_link_local_dynamic_entry *e;
835 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
836 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
837 return e->dynindx;
838 return -1;
841 /* This function is used to renumber the dynamic symbols, if some of
842 them are removed because they are marked as local. This is called
843 via elf_link_hash_traverse. */
845 static bfd_boolean
846 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
847 void *data)
849 size_t *count = (size_t *) data;
851 if (h->forced_local)
852 return TRUE;
854 if (h->dynindx != -1)
855 h->dynindx = ++(*count);
857 return TRUE;
861 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
862 STB_LOCAL binding. */
864 static bfd_boolean
865 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
866 void *data)
868 size_t *count = (size_t *) data;
870 if (!h->forced_local)
871 return TRUE;
873 if (h->dynindx != -1)
874 h->dynindx = ++(*count);
876 return TRUE;
879 /* Return true if the dynamic symbol for a given section should be
880 omitted when creating a shared library. */
881 bfd_boolean
882 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
883 struct bfd_link_info *info,
884 asection *p)
886 struct elf_link_hash_table *htab;
887 asection *ip;
889 switch (elf_section_data (p)->this_hdr.sh_type)
891 case SHT_PROGBITS:
892 case SHT_NOBITS:
893 /* If sh_type is yet undecided, assume it could be
894 SHT_PROGBITS/SHT_NOBITS. */
895 case SHT_NULL:
896 htab = elf_hash_table (info);
897 if (p == htab->tls_sec)
898 return FALSE;
900 if (htab->text_index_section != NULL)
901 return p != htab->text_index_section && p != htab->data_index_section;
903 return (htab->dynobj != NULL
904 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
905 && ip->output_section == p);
907 /* There shouldn't be section relative relocations
908 against any other section. */
909 default:
910 return TRUE;
914 /* Assign dynsym indices. In a shared library we generate a section
915 symbol for each output section, which come first. Next come symbols
916 which have been forced to local binding. Then all of the back-end
917 allocated local dynamic syms, followed by the rest of the global
918 symbols. */
920 static unsigned long
921 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
922 struct bfd_link_info *info,
923 unsigned long *section_sym_count)
925 unsigned long dynsymcount = 0;
927 if (bfd_link_pic (info)
928 || elf_hash_table (info)->is_relocatable_executable)
930 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
931 asection *p;
932 for (p = output_bfd->sections; p ; p = p->next)
933 if ((p->flags & SEC_EXCLUDE) == 0
934 && (p->flags & SEC_ALLOC) != 0
935 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
936 elf_section_data (p)->dynindx = ++dynsymcount;
937 else
938 elf_section_data (p)->dynindx = 0;
940 *section_sym_count = dynsymcount;
942 elf_link_hash_traverse (elf_hash_table (info),
943 elf_link_renumber_local_hash_table_dynsyms,
944 &dynsymcount);
946 if (elf_hash_table (info)->dynlocal)
948 struct elf_link_local_dynamic_entry *p;
949 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
950 p->dynindx = ++dynsymcount;
952 elf_hash_table (info)->local_dynsymcount = dynsymcount;
954 elf_link_hash_traverse (elf_hash_table (info),
955 elf_link_renumber_hash_table_dynsyms,
956 &dynsymcount);
958 /* There is an unused NULL entry at the head of the table which we
959 must account for in our count even if the table is empty since it
960 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
961 .dynamic section. */
962 dynsymcount++;
964 elf_hash_table (info)->dynsymcount = dynsymcount;
965 return dynsymcount;
968 /* Merge st_other field. */
970 static void
971 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
972 const Elf_Internal_Sym *isym, asection *sec,
973 bfd_boolean definition, bfd_boolean dynamic)
975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
977 /* If st_other has a processor-specific meaning, specific
978 code might be needed here. */
979 if (bed->elf_backend_merge_symbol_attribute)
980 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
981 dynamic);
983 if (!dynamic)
985 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
986 unsigned hvis = ELF_ST_VISIBILITY (h->other);
988 /* Keep the most constraining visibility. Leave the remainder
989 of the st_other field to elf_backend_merge_symbol_attribute. */
990 if (symvis - 1 < hvis - 1)
991 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
993 else if (definition
994 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
995 && (sec->flags & SEC_READONLY) == 0)
996 h->protected_def = 1;
999 /* This function is called when we want to merge a new symbol with an
1000 existing symbol. It handles the various cases which arise when we
1001 find a definition in a dynamic object, or when there is already a
1002 definition in a dynamic object. The new symbol is described by
1003 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1004 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1005 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1006 of an old common symbol. We set OVERRIDE if the old symbol is
1007 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1008 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1009 to change. By OK to change, we mean that we shouldn't warn if the
1010 type or size does change. */
1012 static bfd_boolean
1013 _bfd_elf_merge_symbol (bfd *abfd,
1014 struct bfd_link_info *info,
1015 const char *name,
1016 Elf_Internal_Sym *sym,
1017 asection **psec,
1018 bfd_vma *pvalue,
1019 struct elf_link_hash_entry **sym_hash,
1020 bfd **poldbfd,
1021 bfd_boolean *pold_weak,
1022 unsigned int *pold_alignment,
1023 bfd_boolean *skip,
1024 bfd_boolean *override,
1025 bfd_boolean *type_change_ok,
1026 bfd_boolean *size_change_ok,
1027 bfd_boolean *matched)
1029 asection *sec, *oldsec;
1030 struct elf_link_hash_entry *h;
1031 struct elf_link_hash_entry *hi;
1032 struct elf_link_hash_entry *flip;
1033 int bind;
1034 bfd *oldbfd;
1035 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1036 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1037 const struct elf_backend_data *bed;
1038 char *new_version;
1039 bfd_boolean default_sym = *matched;
1041 *skip = FALSE;
1042 *override = FALSE;
1044 sec = *psec;
1045 bind = ELF_ST_BIND (sym->st_info);
1047 if (! bfd_is_und_section (sec))
1048 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1049 else
1050 h = ((struct elf_link_hash_entry *)
1051 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1052 if (h == NULL)
1053 return FALSE;
1054 *sym_hash = h;
1056 bed = get_elf_backend_data (abfd);
1058 /* NEW_VERSION is the symbol version of the new symbol. */
1059 if (h->versioned != unversioned)
1061 /* Symbol version is unknown or versioned. */
1062 new_version = strrchr (name, ELF_VER_CHR);
1063 if (new_version)
1065 if (h->versioned == unknown)
1067 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1068 h->versioned = versioned_hidden;
1069 else
1070 h->versioned = versioned;
1072 new_version += 1;
1073 if (new_version[0] == '\0')
1074 new_version = NULL;
1076 else
1077 h->versioned = unversioned;
1079 else
1080 new_version = NULL;
1082 /* For merging, we only care about real symbols. But we need to make
1083 sure that indirect symbol dynamic flags are updated. */
1084 hi = h;
1085 while (h->root.type == bfd_link_hash_indirect
1086 || h->root.type == bfd_link_hash_warning)
1087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1089 if (!*matched)
1091 if (hi == h || h->root.type == bfd_link_hash_new)
1092 *matched = TRUE;
1093 else
1095 /* OLD_HIDDEN is true if the existing symbol is only visible
1096 to the symbol with the same symbol version. NEW_HIDDEN is
1097 true if the new symbol is only visible to the symbol with
1098 the same symbol version. */
1099 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1100 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1101 if (!old_hidden && !new_hidden)
1102 /* The new symbol matches the existing symbol if both
1103 aren't hidden. */
1104 *matched = TRUE;
1105 else
1107 /* OLD_VERSION is the symbol version of the existing
1108 symbol. */
1109 char *old_version;
1111 if (h->versioned >= versioned)
1112 old_version = strrchr (h->root.root.string,
1113 ELF_VER_CHR) + 1;
1114 else
1115 old_version = NULL;
1117 /* The new symbol matches the existing symbol if they
1118 have the same symbol version. */
1119 *matched = (old_version == new_version
1120 || (old_version != NULL
1121 && new_version != NULL
1122 && strcmp (old_version, new_version) == 0));
1127 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1128 existing symbol. */
1130 oldbfd = NULL;
1131 oldsec = NULL;
1132 switch (h->root.type)
1134 default:
1135 break;
1137 case bfd_link_hash_undefined:
1138 case bfd_link_hash_undefweak:
1139 oldbfd = h->root.u.undef.abfd;
1140 break;
1142 case bfd_link_hash_defined:
1143 case bfd_link_hash_defweak:
1144 oldbfd = h->root.u.def.section->owner;
1145 oldsec = h->root.u.def.section;
1146 break;
1148 case bfd_link_hash_common:
1149 oldbfd = h->root.u.c.p->section->owner;
1150 oldsec = h->root.u.c.p->section;
1151 if (pold_alignment)
1152 *pold_alignment = h->root.u.c.p->alignment_power;
1153 break;
1155 if (poldbfd && *poldbfd == NULL)
1156 *poldbfd = oldbfd;
1158 /* Differentiate strong and weak symbols. */
1159 newweak = bind == STB_WEAK;
1160 oldweak = (h->root.type == bfd_link_hash_defweak
1161 || h->root.type == bfd_link_hash_undefweak);
1162 if (pold_weak)
1163 *pold_weak = oldweak;
1165 /* We have to check it for every instance since the first few may be
1166 references and not all compilers emit symbol type for undefined
1167 symbols. */
1168 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1170 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1171 respectively, is from a dynamic object. */
1173 newdyn = (abfd->flags & DYNAMIC) != 0;
1175 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1176 syms and defined syms in dynamic libraries respectively.
1177 ref_dynamic on the other hand can be set for a symbol defined in
1178 a dynamic library, and def_dynamic may not be set; When the
1179 definition in a dynamic lib is overridden by a definition in the
1180 executable use of the symbol in the dynamic lib becomes a
1181 reference to the executable symbol. */
1182 if (newdyn)
1184 if (bfd_is_und_section (sec))
1186 if (bind != STB_WEAK)
1188 h->ref_dynamic_nonweak = 1;
1189 hi->ref_dynamic_nonweak = 1;
1192 else
1194 /* Update the existing symbol only if they match. */
1195 if (*matched)
1196 h->dynamic_def = 1;
1197 hi->dynamic_def = 1;
1201 /* If we just created the symbol, mark it as being an ELF symbol.
1202 Other than that, there is nothing to do--there is no merge issue
1203 with a newly defined symbol--so we just return. */
1205 if (h->root.type == bfd_link_hash_new)
1207 h->non_elf = 0;
1208 return TRUE;
1211 /* In cases involving weak versioned symbols, we may wind up trying
1212 to merge a symbol with itself. Catch that here, to avoid the
1213 confusion that results if we try to override a symbol with
1214 itself. The additional tests catch cases like
1215 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1216 dynamic object, which we do want to handle here. */
1217 if (abfd == oldbfd
1218 && (newweak || oldweak)
1219 && ((abfd->flags & DYNAMIC) == 0
1220 || !h->def_regular))
1221 return TRUE;
1223 olddyn = FALSE;
1224 if (oldbfd != NULL)
1225 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1226 else if (oldsec != NULL)
1228 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1229 indices used by MIPS ELF. */
1230 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1233 /* Handle a case where plugin_notice won't be called and thus won't
1234 set the non_ir_ref flags on the first pass over symbols. */
1235 if (oldbfd != NULL
1236 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1237 && newdyn != olddyn)
1239 h->root.non_ir_ref_dynamic = TRUE;
1240 hi->root.non_ir_ref_dynamic = TRUE;
1243 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1244 respectively, appear to be a definition rather than reference. */
1246 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1248 olddef = (h->root.type != bfd_link_hash_undefined
1249 && h->root.type != bfd_link_hash_undefweak
1250 && h->root.type != bfd_link_hash_common);
1252 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1253 respectively, appear to be a function. */
1255 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1256 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1258 oldfunc = (h->type != STT_NOTYPE
1259 && bed->is_function_type (h->type));
1261 if (!(newfunc && oldfunc)
1262 && ELF_ST_TYPE (sym->st_info) != h->type
1263 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1264 && h->type != STT_NOTYPE
1265 && (newdef || bfd_is_com_section (sec))
1266 && (olddef || h->root.type == bfd_link_hash_common))
1268 /* If creating a default indirect symbol ("foo" or "foo@") from
1269 a dynamic versioned definition ("foo@@") skip doing so if
1270 there is an existing regular definition with a different
1271 type. We don't want, for example, a "time" variable in the
1272 executable overriding a "time" function in a shared library. */
1273 if (newdyn
1274 && !olddyn)
1276 *skip = TRUE;
1277 return TRUE;
1280 /* When adding a symbol from a regular object file after we have
1281 created indirect symbols, undo the indirection and any
1282 dynamic state. */
1283 if (hi != h
1284 && !newdyn
1285 && olddyn)
1287 h = hi;
1288 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1289 h->forced_local = 0;
1290 h->ref_dynamic = 0;
1291 h->def_dynamic = 0;
1292 h->dynamic_def = 0;
1293 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1295 h->root.type = bfd_link_hash_undefined;
1296 h->root.u.undef.abfd = abfd;
1298 else
1300 h->root.type = bfd_link_hash_new;
1301 h->root.u.undef.abfd = NULL;
1303 return TRUE;
1307 /* Check TLS symbols. We don't check undefined symbols introduced
1308 by "ld -u" which have no type (and oldbfd NULL), and we don't
1309 check symbols from plugins because they also have no type. */
1310 if (oldbfd != NULL
1311 && (oldbfd->flags & BFD_PLUGIN) == 0
1312 && (abfd->flags & BFD_PLUGIN) == 0
1313 && ELF_ST_TYPE (sym->st_info) != h->type
1314 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1316 bfd *ntbfd, *tbfd;
1317 bfd_boolean ntdef, tdef;
1318 asection *ntsec, *tsec;
1320 if (h->type == STT_TLS)
1322 ntbfd = abfd;
1323 ntsec = sec;
1324 ntdef = newdef;
1325 tbfd = oldbfd;
1326 tsec = oldsec;
1327 tdef = olddef;
1329 else
1331 ntbfd = oldbfd;
1332 ntsec = oldsec;
1333 ntdef = olddef;
1334 tbfd = abfd;
1335 tsec = sec;
1336 tdef = newdef;
1339 if (tdef && ntdef)
1340 _bfd_error_handler
1341 /* xgettext:c-format */
1342 (_("%s: TLS definition in %B section %A "
1343 "mismatches non-TLS definition in %B section %A"),
1344 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1345 else if (!tdef && !ntdef)
1346 _bfd_error_handler
1347 /* xgettext:c-format */
1348 (_("%s: TLS reference in %B "
1349 "mismatches non-TLS reference in %B"),
1350 h->root.root.string, tbfd, ntbfd);
1351 else if (tdef)
1352 _bfd_error_handler
1353 /* xgettext:c-format */
1354 (_("%s: TLS definition in %B section %A "
1355 "mismatches non-TLS reference in %B"),
1356 h->root.root.string, tbfd, tsec, ntbfd);
1357 else
1358 _bfd_error_handler
1359 /* xgettext:c-format */
1360 (_("%s: TLS reference in %B "
1361 "mismatches non-TLS definition in %B section %A"),
1362 h->root.root.string, tbfd, ntbfd, ntsec);
1364 bfd_set_error (bfd_error_bad_value);
1365 return FALSE;
1368 /* If the old symbol has non-default visibility, we ignore the new
1369 definition from a dynamic object. */
1370 if (newdyn
1371 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1372 && !bfd_is_und_section (sec))
1374 *skip = TRUE;
1375 /* Make sure this symbol is dynamic. */
1376 h->ref_dynamic = 1;
1377 hi->ref_dynamic = 1;
1378 /* A protected symbol has external availability. Make sure it is
1379 recorded as dynamic.
1381 FIXME: Should we check type and size for protected symbol? */
1382 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1383 return bfd_elf_link_record_dynamic_symbol (info, h);
1384 else
1385 return TRUE;
1387 else if (!newdyn
1388 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1389 && h->def_dynamic)
1391 /* If the new symbol with non-default visibility comes from a
1392 relocatable file and the old definition comes from a dynamic
1393 object, we remove the old definition. */
1394 if (hi->root.type == bfd_link_hash_indirect)
1396 /* Handle the case where the old dynamic definition is
1397 default versioned. We need to copy the symbol info from
1398 the symbol with default version to the normal one if it
1399 was referenced before. */
1400 if (h->ref_regular)
1402 hi->root.type = h->root.type;
1403 h->root.type = bfd_link_hash_indirect;
1404 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1406 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1407 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1409 /* If the new symbol is hidden or internal, completely undo
1410 any dynamic link state. */
1411 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1412 h->forced_local = 0;
1413 h->ref_dynamic = 0;
1415 else
1416 h->ref_dynamic = 1;
1418 h->def_dynamic = 0;
1419 /* FIXME: Should we check type and size for protected symbol? */
1420 h->size = 0;
1421 h->type = 0;
1423 h = hi;
1425 else
1426 h = hi;
1429 /* If the old symbol was undefined before, then it will still be
1430 on the undefs list. If the new symbol is undefined or
1431 common, we can't make it bfd_link_hash_new here, because new
1432 undefined or common symbols will be added to the undefs list
1433 by _bfd_generic_link_add_one_symbol. Symbols may not be
1434 added twice to the undefs list. Also, if the new symbol is
1435 undefweak then we don't want to lose the strong undef. */
1436 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1438 h->root.type = bfd_link_hash_undefined;
1439 h->root.u.undef.abfd = abfd;
1441 else
1443 h->root.type = bfd_link_hash_new;
1444 h->root.u.undef.abfd = NULL;
1447 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1449 /* If the new symbol is hidden or internal, completely undo
1450 any dynamic link state. */
1451 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1452 h->forced_local = 0;
1453 h->ref_dynamic = 0;
1455 else
1456 h->ref_dynamic = 1;
1457 h->def_dynamic = 0;
1458 /* FIXME: Should we check type and size for protected symbol? */
1459 h->size = 0;
1460 h->type = 0;
1461 return TRUE;
1464 /* If a new weak symbol definition comes from a regular file and the
1465 old symbol comes from a dynamic library, we treat the new one as
1466 strong. Similarly, an old weak symbol definition from a regular
1467 file is treated as strong when the new symbol comes from a dynamic
1468 library. Further, an old weak symbol from a dynamic library is
1469 treated as strong if the new symbol is from a dynamic library.
1470 This reflects the way glibc's ld.so works.
1472 Do this before setting *type_change_ok or *size_change_ok so that
1473 we warn properly when dynamic library symbols are overridden. */
1475 if (newdef && !newdyn && olddyn)
1476 newweak = FALSE;
1477 if (olddef && newdyn)
1478 oldweak = FALSE;
1480 /* Allow changes between different types of function symbol. */
1481 if (newfunc && oldfunc)
1482 *type_change_ok = TRUE;
1484 /* It's OK to change the type if either the existing symbol or the
1485 new symbol is weak. A type change is also OK if the old symbol
1486 is undefined and the new symbol is defined. */
1488 if (oldweak
1489 || newweak
1490 || (newdef
1491 && h->root.type == bfd_link_hash_undefined))
1492 *type_change_ok = TRUE;
1494 /* It's OK to change the size if either the existing symbol or the
1495 new symbol is weak, or if the old symbol is undefined. */
1497 if (*type_change_ok
1498 || h->root.type == bfd_link_hash_undefined)
1499 *size_change_ok = TRUE;
1501 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1502 symbol, respectively, appears to be a common symbol in a dynamic
1503 object. If a symbol appears in an uninitialized section, and is
1504 not weak, and is not a function, then it may be a common symbol
1505 which was resolved when the dynamic object was created. We want
1506 to treat such symbols specially, because they raise special
1507 considerations when setting the symbol size: if the symbol
1508 appears as a common symbol in a regular object, and the size in
1509 the regular object is larger, we must make sure that we use the
1510 larger size. This problematic case can always be avoided in C,
1511 but it must be handled correctly when using Fortran shared
1512 libraries.
1514 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1515 likewise for OLDDYNCOMMON and OLDDEF.
1517 Note that this test is just a heuristic, and that it is quite
1518 possible to have an uninitialized symbol in a shared object which
1519 is really a definition, rather than a common symbol. This could
1520 lead to some minor confusion when the symbol really is a common
1521 symbol in some regular object. However, I think it will be
1522 harmless. */
1524 if (newdyn
1525 && newdef
1526 && !newweak
1527 && (sec->flags & SEC_ALLOC) != 0
1528 && (sec->flags & SEC_LOAD) == 0
1529 && sym->st_size > 0
1530 && !newfunc)
1531 newdyncommon = TRUE;
1532 else
1533 newdyncommon = FALSE;
1535 if (olddyn
1536 && olddef
1537 && h->root.type == bfd_link_hash_defined
1538 && h->def_dynamic
1539 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1540 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1541 && h->size > 0
1542 && !oldfunc)
1543 olddyncommon = TRUE;
1544 else
1545 olddyncommon = FALSE;
1547 /* We now know everything about the old and new symbols. We ask the
1548 backend to check if we can merge them. */
1549 if (bed->merge_symbol != NULL)
1551 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1552 return FALSE;
1553 sec = *psec;
1556 /* There are multiple definitions of a normal symbol.
1557 Skip the default symbol as well. */
1558 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1559 && !default_sym && h->def_regular)
1561 /* Handle a multiple definition. */
1562 (*info->callbacks->multiple_definition) (info, &h->root,
1563 abfd, sec, *pvalue);
1564 *skip = TRUE;
1565 return TRUE;
1568 /* If both the old and the new symbols look like common symbols in a
1569 dynamic object, set the size of the symbol to the larger of the
1570 two. */
1572 if (olddyncommon
1573 && newdyncommon
1574 && sym->st_size != h->size)
1576 /* Since we think we have two common symbols, issue a multiple
1577 common warning if desired. Note that we only warn if the
1578 size is different. If the size is the same, we simply let
1579 the old symbol override the new one as normally happens with
1580 symbols defined in dynamic objects. */
1582 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1583 bfd_link_hash_common, sym->st_size);
1584 if (sym->st_size > h->size)
1585 h->size = sym->st_size;
1587 *size_change_ok = TRUE;
1590 /* If we are looking at a dynamic object, and we have found a
1591 definition, we need to see if the symbol was already defined by
1592 some other object. If so, we want to use the existing
1593 definition, and we do not want to report a multiple symbol
1594 definition error; we do this by clobbering *PSEC to be
1595 bfd_und_section_ptr.
1597 We treat a common symbol as a definition if the symbol in the
1598 shared library is a function, since common symbols always
1599 represent variables; this can cause confusion in principle, but
1600 any such confusion would seem to indicate an erroneous program or
1601 shared library. We also permit a common symbol in a regular
1602 object to override a weak symbol in a shared object. */
1604 if (newdyn
1605 && newdef
1606 && (olddef
1607 || (h->root.type == bfd_link_hash_common
1608 && (newweak || newfunc))))
1610 *override = TRUE;
1611 newdef = FALSE;
1612 newdyncommon = FALSE;
1614 *psec = sec = bfd_und_section_ptr;
1615 *size_change_ok = TRUE;
1617 /* If we get here when the old symbol is a common symbol, then
1618 we are explicitly letting it override a weak symbol or
1619 function in a dynamic object, and we don't want to warn about
1620 a type change. If the old symbol is a defined symbol, a type
1621 change warning may still be appropriate. */
1623 if (h->root.type == bfd_link_hash_common)
1624 *type_change_ok = TRUE;
1627 /* Handle the special case of an old common symbol merging with a
1628 new symbol which looks like a common symbol in a shared object.
1629 We change *PSEC and *PVALUE to make the new symbol look like a
1630 common symbol, and let _bfd_generic_link_add_one_symbol do the
1631 right thing. */
1633 if (newdyncommon
1634 && h->root.type == bfd_link_hash_common)
1636 *override = TRUE;
1637 newdef = FALSE;
1638 newdyncommon = FALSE;
1639 *pvalue = sym->st_size;
1640 *psec = sec = bed->common_section (oldsec);
1641 *size_change_ok = TRUE;
1644 /* Skip weak definitions of symbols that are already defined. */
1645 if (newdef && olddef && newweak)
1647 /* Don't skip new non-IR weak syms. */
1648 if (!(oldbfd != NULL
1649 && (oldbfd->flags & BFD_PLUGIN) != 0
1650 && (abfd->flags & BFD_PLUGIN) == 0))
1652 newdef = FALSE;
1653 *skip = TRUE;
1656 /* Merge st_other. If the symbol already has a dynamic index,
1657 but visibility says it should not be visible, turn it into a
1658 local symbol. */
1659 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1660 if (h->dynindx != -1)
1661 switch (ELF_ST_VISIBILITY (h->other))
1663 case STV_INTERNAL:
1664 case STV_HIDDEN:
1665 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1666 break;
1670 /* If the old symbol is from a dynamic object, and the new symbol is
1671 a definition which is not from a dynamic object, then the new
1672 symbol overrides the old symbol. Symbols from regular files
1673 always take precedence over symbols from dynamic objects, even if
1674 they are defined after the dynamic object in the link.
1676 As above, we again permit a common symbol in a regular object to
1677 override a definition in a shared object if the shared object
1678 symbol is a function or is weak. */
1680 flip = NULL;
1681 if (!newdyn
1682 && (newdef
1683 || (bfd_is_com_section (sec)
1684 && (oldweak || oldfunc)))
1685 && olddyn
1686 && olddef
1687 && h->def_dynamic)
1689 /* Change the hash table entry to undefined, and let
1690 _bfd_generic_link_add_one_symbol do the right thing with the
1691 new definition. */
1693 h->root.type = bfd_link_hash_undefined;
1694 h->root.u.undef.abfd = h->root.u.def.section->owner;
1695 *size_change_ok = TRUE;
1697 olddef = FALSE;
1698 olddyncommon = FALSE;
1700 /* We again permit a type change when a common symbol may be
1701 overriding a function. */
1703 if (bfd_is_com_section (sec))
1705 if (oldfunc)
1707 /* If a common symbol overrides a function, make sure
1708 that it isn't defined dynamically nor has type
1709 function. */
1710 h->def_dynamic = 0;
1711 h->type = STT_NOTYPE;
1713 *type_change_ok = TRUE;
1716 if (hi->root.type == bfd_link_hash_indirect)
1717 flip = hi;
1718 else
1719 /* This union may have been set to be non-NULL when this symbol
1720 was seen in a dynamic object. We must force the union to be
1721 NULL, so that it is correct for a regular symbol. */
1722 h->verinfo.vertree = NULL;
1725 /* Handle the special case of a new common symbol merging with an
1726 old symbol that looks like it might be a common symbol defined in
1727 a shared object. Note that we have already handled the case in
1728 which a new common symbol should simply override the definition
1729 in the shared library. */
1731 if (! newdyn
1732 && bfd_is_com_section (sec)
1733 && olddyncommon)
1735 /* It would be best if we could set the hash table entry to a
1736 common symbol, but we don't know what to use for the section
1737 or the alignment. */
1738 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1739 bfd_link_hash_common, sym->st_size);
1741 /* If the presumed common symbol in the dynamic object is
1742 larger, pretend that the new symbol has its size. */
1744 if (h->size > *pvalue)
1745 *pvalue = h->size;
1747 /* We need to remember the alignment required by the symbol
1748 in the dynamic object. */
1749 BFD_ASSERT (pold_alignment);
1750 *pold_alignment = h->root.u.def.section->alignment_power;
1752 olddef = FALSE;
1753 olddyncommon = FALSE;
1755 h->root.type = bfd_link_hash_undefined;
1756 h->root.u.undef.abfd = h->root.u.def.section->owner;
1758 *size_change_ok = TRUE;
1759 *type_change_ok = TRUE;
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
1763 else
1764 h->verinfo.vertree = NULL;
1767 if (flip != NULL)
1769 /* Handle the case where we had a versioned symbol in a dynamic
1770 library and now find a definition in a normal object. In this
1771 case, we make the versioned symbol point to the normal one. */
1772 flip->root.type = h->root.type;
1773 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1774 h->root.type = bfd_link_hash_indirect;
1775 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1776 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1777 if (h->def_dynamic)
1779 h->def_dynamic = 0;
1780 flip->ref_dynamic = 1;
1784 return TRUE;
1787 /* This function is called to create an indirect symbol from the
1788 default for the symbol with the default version if needed. The
1789 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1790 set DYNSYM if the new indirect symbol is dynamic. */
1792 static bfd_boolean
1793 _bfd_elf_add_default_symbol (bfd *abfd,
1794 struct bfd_link_info *info,
1795 struct elf_link_hash_entry *h,
1796 const char *name,
1797 Elf_Internal_Sym *sym,
1798 asection *sec,
1799 bfd_vma value,
1800 bfd **poldbfd,
1801 bfd_boolean *dynsym)
1803 bfd_boolean type_change_ok;
1804 bfd_boolean size_change_ok;
1805 bfd_boolean skip;
1806 char *shortname;
1807 struct elf_link_hash_entry *hi;
1808 struct bfd_link_hash_entry *bh;
1809 const struct elf_backend_data *bed;
1810 bfd_boolean collect;
1811 bfd_boolean dynamic;
1812 bfd_boolean override;
1813 char *p;
1814 size_t len, shortlen;
1815 asection *tmp_sec;
1816 bfd_boolean matched;
1818 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1819 return TRUE;
1821 /* If this symbol has a version, and it is the default version, we
1822 create an indirect symbol from the default name to the fully
1823 decorated name. This will cause external references which do not
1824 specify a version to be bound to this version of the symbol. */
1825 p = strchr (name, ELF_VER_CHR);
1826 if (h->versioned == unknown)
1828 if (p == NULL)
1830 h->versioned = unversioned;
1831 return TRUE;
1833 else
1835 if (p[1] != ELF_VER_CHR)
1837 h->versioned = versioned_hidden;
1838 return TRUE;
1840 else
1841 h->versioned = versioned;
1844 else
1846 /* PR ld/19073: We may see an unversioned definition after the
1847 default version. */
1848 if (p == NULL)
1849 return TRUE;
1852 bed = get_elf_backend_data (abfd);
1853 collect = bed->collect;
1854 dynamic = (abfd->flags & DYNAMIC) != 0;
1856 shortlen = p - name;
1857 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1858 if (shortname == NULL)
1859 return FALSE;
1860 memcpy (shortname, name, shortlen);
1861 shortname[shortlen] = '\0';
1863 /* We are going to create a new symbol. Merge it with any existing
1864 symbol with this name. For the purposes of the merge, act as
1865 though we were defining the symbol we just defined, although we
1866 actually going to define an indirect symbol. */
1867 type_change_ok = FALSE;
1868 size_change_ok = FALSE;
1869 matched = TRUE;
1870 tmp_sec = sec;
1871 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1872 &hi, poldbfd, NULL, NULL, &skip, &override,
1873 &type_change_ok, &size_change_ok, &matched))
1874 return FALSE;
1876 if (skip)
1877 goto nondefault;
1879 if (hi->def_regular)
1881 /* If the undecorated symbol will have a version added by a
1882 script different to H, then don't indirect to/from the
1883 undecorated symbol. This isn't ideal because we may not yet
1884 have seen symbol versions, if given by a script on the
1885 command line rather than via --version-script. */
1886 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1888 bfd_boolean hide;
1890 hi->verinfo.vertree
1891 = bfd_find_version_for_sym (info->version_info,
1892 hi->root.root.string, &hide);
1893 if (hi->verinfo.vertree != NULL && hide)
1895 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1896 goto nondefault;
1899 if (hi->verinfo.vertree != NULL
1900 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1901 goto nondefault;
1904 if (! override)
1906 /* Add the default symbol if not performing a relocatable link. */
1907 if (! bfd_link_relocatable (info))
1909 bh = &hi->root;
1910 if (! (_bfd_generic_link_add_one_symbol
1911 (info, abfd, shortname, BSF_INDIRECT,
1912 bfd_ind_section_ptr,
1913 0, name, FALSE, collect, &bh)))
1914 return FALSE;
1915 hi = (struct elf_link_hash_entry *) bh;
1918 else
1920 /* In this case the symbol named SHORTNAME is overriding the
1921 indirect symbol we want to add. We were planning on making
1922 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1923 is the name without a version. NAME is the fully versioned
1924 name, and it is the default version.
1926 Overriding means that we already saw a definition for the
1927 symbol SHORTNAME in a regular object, and it is overriding
1928 the symbol defined in the dynamic object.
1930 When this happens, we actually want to change NAME, the
1931 symbol we just added, to refer to SHORTNAME. This will cause
1932 references to NAME in the shared object to become references
1933 to SHORTNAME in the regular object. This is what we expect
1934 when we override a function in a shared object: that the
1935 references in the shared object will be mapped to the
1936 definition in the regular object. */
1938 while (hi->root.type == bfd_link_hash_indirect
1939 || hi->root.type == bfd_link_hash_warning)
1940 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1942 h->root.type = bfd_link_hash_indirect;
1943 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1944 if (h->def_dynamic)
1946 h->def_dynamic = 0;
1947 hi->ref_dynamic = 1;
1948 if (hi->ref_regular
1949 || hi->def_regular)
1951 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1952 return FALSE;
1956 /* Now set HI to H, so that the following code will set the
1957 other fields correctly. */
1958 hi = h;
1961 /* Check if HI is a warning symbol. */
1962 if (hi->root.type == bfd_link_hash_warning)
1963 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1965 /* If there is a duplicate definition somewhere, then HI may not
1966 point to an indirect symbol. We will have reported an error to
1967 the user in that case. */
1969 if (hi->root.type == bfd_link_hash_indirect)
1971 struct elf_link_hash_entry *ht;
1973 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1974 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1976 /* A reference to the SHORTNAME symbol from a dynamic library
1977 will be satisfied by the versioned symbol at runtime. In
1978 effect, we have a reference to the versioned symbol. */
1979 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1980 hi->dynamic_def |= ht->dynamic_def;
1982 /* See if the new flags lead us to realize that the symbol must
1983 be dynamic. */
1984 if (! *dynsym)
1986 if (! dynamic)
1988 if (! bfd_link_executable (info)
1989 || hi->def_dynamic
1990 || hi->ref_dynamic)
1991 *dynsym = TRUE;
1993 else
1995 if (hi->ref_regular)
1996 *dynsym = TRUE;
2001 /* We also need to define an indirection from the nondefault version
2002 of the symbol. */
2004 nondefault:
2005 len = strlen (name);
2006 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2007 if (shortname == NULL)
2008 return FALSE;
2009 memcpy (shortname, name, shortlen);
2010 memcpy (shortname + shortlen, p + 1, len - shortlen);
2012 /* Once again, merge with any existing symbol. */
2013 type_change_ok = FALSE;
2014 size_change_ok = FALSE;
2015 tmp_sec = sec;
2016 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2017 &hi, poldbfd, NULL, NULL, &skip, &override,
2018 &type_change_ok, &size_change_ok, &matched))
2019 return FALSE;
2021 if (skip)
2022 return TRUE;
2024 if (override)
2026 /* Here SHORTNAME is a versioned name, so we don't expect to see
2027 the type of override we do in the case above unless it is
2028 overridden by a versioned definition. */
2029 if (hi->root.type != bfd_link_hash_defined
2030 && hi->root.type != bfd_link_hash_defweak)
2031 _bfd_error_handler
2032 /* xgettext:c-format */
2033 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2034 abfd, shortname);
2036 else
2038 bh = &hi->root;
2039 if (! (_bfd_generic_link_add_one_symbol
2040 (info, abfd, shortname, BSF_INDIRECT,
2041 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2042 return FALSE;
2043 hi = (struct elf_link_hash_entry *) bh;
2045 /* If there is a duplicate definition somewhere, then HI may not
2046 point to an indirect symbol. We will have reported an error
2047 to the user in that case. */
2049 if (hi->root.type == bfd_link_hash_indirect)
2051 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2052 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2053 hi->dynamic_def |= h->dynamic_def;
2055 /* See if the new flags lead us to realize that the symbol
2056 must be dynamic. */
2057 if (! *dynsym)
2059 if (! dynamic)
2061 if (! bfd_link_executable (info)
2062 || hi->ref_dynamic)
2063 *dynsym = TRUE;
2065 else
2067 if (hi->ref_regular)
2068 *dynsym = TRUE;
2074 return TRUE;
2077 /* This routine is used to export all defined symbols into the dynamic
2078 symbol table. It is called via elf_link_hash_traverse. */
2080 static bfd_boolean
2081 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2083 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2085 /* Ignore indirect symbols. These are added by the versioning code. */
2086 if (h->root.type == bfd_link_hash_indirect)
2087 return TRUE;
2089 /* Ignore this if we won't export it. */
2090 if (!eif->info->export_dynamic && !h->dynamic)
2091 return TRUE;
2093 if (h->dynindx == -1
2094 && (h->def_regular || h->ref_regular)
2095 && ! bfd_hide_sym_by_version (eif->info->version_info,
2096 h->root.root.string))
2098 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2100 eif->failed = TRUE;
2101 return FALSE;
2105 return TRUE;
2108 /* Look through the symbols which are defined in other shared
2109 libraries and referenced here. Update the list of version
2110 dependencies. This will be put into the .gnu.version_r section.
2111 This function is called via elf_link_hash_traverse. */
2113 static bfd_boolean
2114 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2115 void *data)
2117 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2118 Elf_Internal_Verneed *t;
2119 Elf_Internal_Vernaux *a;
2120 bfd_size_type amt;
2122 /* We only care about symbols defined in shared objects with version
2123 information. */
2124 if (!h->def_dynamic
2125 || h->def_regular
2126 || h->dynindx == -1
2127 || h->verinfo.verdef == NULL
2128 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2129 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2130 return TRUE;
2132 /* See if we already know about this version. */
2133 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2134 t != NULL;
2135 t = t->vn_nextref)
2137 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2138 continue;
2140 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2141 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2142 return TRUE;
2144 break;
2147 /* This is a new version. Add it to tree we are building. */
2149 if (t == NULL)
2151 amt = sizeof *t;
2152 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2153 if (t == NULL)
2155 rinfo->failed = TRUE;
2156 return FALSE;
2159 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2160 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2161 elf_tdata (rinfo->info->output_bfd)->verref = t;
2164 amt = sizeof *a;
2165 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2166 if (a == NULL)
2168 rinfo->failed = TRUE;
2169 return FALSE;
2172 /* Note that we are copying a string pointer here, and testing it
2173 above. If bfd_elf_string_from_elf_section is ever changed to
2174 discard the string data when low in memory, this will have to be
2175 fixed. */
2176 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2178 a->vna_flags = h->verinfo.verdef->vd_flags;
2179 a->vna_nextptr = t->vn_auxptr;
2181 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2182 ++rinfo->vers;
2184 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2186 t->vn_auxptr = a;
2188 return TRUE;
2191 /* Figure out appropriate versions for all the symbols. We may not
2192 have the version number script until we have read all of the input
2193 files, so until that point we don't know which symbols should be
2194 local. This function is called via elf_link_hash_traverse. */
2196 static bfd_boolean
2197 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2199 struct elf_info_failed *sinfo;
2200 struct bfd_link_info *info;
2201 const struct elf_backend_data *bed;
2202 struct elf_info_failed eif;
2203 char *p;
2205 sinfo = (struct elf_info_failed *) data;
2206 info = sinfo->info;
2208 /* Fix the symbol flags. */
2209 eif.failed = FALSE;
2210 eif.info = info;
2211 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2213 if (eif.failed)
2214 sinfo->failed = TRUE;
2215 return FALSE;
2218 /* We only need version numbers for symbols defined in regular
2219 objects. */
2220 if (!h->def_regular)
2221 return TRUE;
2223 bed = get_elf_backend_data (info->output_bfd);
2224 p = strchr (h->root.root.string, ELF_VER_CHR);
2225 if (p != NULL && h->verinfo.vertree == NULL)
2227 struct bfd_elf_version_tree *t;
2229 ++p;
2230 if (*p == ELF_VER_CHR)
2231 ++p;
2233 /* If there is no version string, we can just return out. */
2234 if (*p == '\0')
2235 return TRUE;
2237 /* Look for the version. If we find it, it is no longer weak. */
2238 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2240 if (strcmp (t->name, p) == 0)
2242 size_t len;
2243 char *alc;
2244 struct bfd_elf_version_expr *d;
2246 len = p - h->root.root.string;
2247 alc = (char *) bfd_malloc (len);
2248 if (alc == NULL)
2250 sinfo->failed = TRUE;
2251 return FALSE;
2253 memcpy (alc, h->root.root.string, len - 1);
2254 alc[len - 1] = '\0';
2255 if (alc[len - 2] == ELF_VER_CHR)
2256 alc[len - 2] = '\0';
2258 h->verinfo.vertree = t;
2259 t->used = TRUE;
2260 d = NULL;
2262 if (t->globals.list != NULL)
2263 d = (*t->match) (&t->globals, NULL, alc);
2265 /* See if there is anything to force this symbol to
2266 local scope. */
2267 if (d == NULL && t->locals.list != NULL)
2269 d = (*t->match) (&t->locals, NULL, alc);
2270 if (d != NULL
2271 && h->dynindx != -1
2272 && ! info->export_dynamic)
2273 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2276 free (alc);
2277 break;
2281 /* If we are building an application, we need to create a
2282 version node for this version. */
2283 if (t == NULL && bfd_link_executable (info))
2285 struct bfd_elf_version_tree **pp;
2286 int version_index;
2288 /* If we aren't going to export this symbol, we don't need
2289 to worry about it. */
2290 if (h->dynindx == -1)
2291 return TRUE;
2293 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2294 sizeof *t);
2295 if (t == NULL)
2297 sinfo->failed = TRUE;
2298 return FALSE;
2301 t->name = p;
2302 t->name_indx = (unsigned int) -1;
2303 t->used = TRUE;
2305 version_index = 1;
2306 /* Don't count anonymous version tag. */
2307 if (sinfo->info->version_info != NULL
2308 && sinfo->info->version_info->vernum == 0)
2309 version_index = 0;
2310 for (pp = &sinfo->info->version_info;
2311 *pp != NULL;
2312 pp = &(*pp)->next)
2313 ++version_index;
2314 t->vernum = version_index;
2316 *pp = t;
2318 h->verinfo.vertree = t;
2320 else if (t == NULL)
2322 /* We could not find the version for a symbol when
2323 generating a shared archive. Return an error. */
2324 _bfd_error_handler
2325 /* xgettext:c-format */
2326 (_("%B: version node not found for symbol %s"),
2327 info->output_bfd, h->root.root.string);
2328 bfd_set_error (bfd_error_bad_value);
2329 sinfo->failed = TRUE;
2330 return FALSE;
2334 /* If we don't have a version for this symbol, see if we can find
2335 something. */
2336 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2338 bfd_boolean hide;
2340 h->verinfo.vertree
2341 = bfd_find_version_for_sym (sinfo->info->version_info,
2342 h->root.root.string, &hide);
2343 if (h->verinfo.vertree != NULL && hide)
2344 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2347 return TRUE;
2350 /* Read and swap the relocs from the section indicated by SHDR. This
2351 may be either a REL or a RELA section. The relocations are
2352 translated into RELA relocations and stored in INTERNAL_RELOCS,
2353 which should have already been allocated to contain enough space.
2354 The EXTERNAL_RELOCS are a buffer where the external form of the
2355 relocations should be stored.
2357 Returns FALSE if something goes wrong. */
2359 static bfd_boolean
2360 elf_link_read_relocs_from_section (bfd *abfd,
2361 asection *sec,
2362 Elf_Internal_Shdr *shdr,
2363 void *external_relocs,
2364 Elf_Internal_Rela *internal_relocs)
2366 const struct elf_backend_data *bed;
2367 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2368 const bfd_byte *erela;
2369 const bfd_byte *erelaend;
2370 Elf_Internal_Rela *irela;
2371 Elf_Internal_Shdr *symtab_hdr;
2372 size_t nsyms;
2374 /* Position ourselves at the start of the section. */
2375 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2376 return FALSE;
2378 /* Read the relocations. */
2379 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2380 return FALSE;
2382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2383 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2385 bed = get_elf_backend_data (abfd);
2387 /* Convert the external relocations to the internal format. */
2388 if (shdr->sh_entsize == bed->s->sizeof_rel)
2389 swap_in = bed->s->swap_reloc_in;
2390 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2391 swap_in = bed->s->swap_reloca_in;
2392 else
2394 bfd_set_error (bfd_error_wrong_format);
2395 return FALSE;
2398 erela = (const bfd_byte *) external_relocs;
2399 erelaend = erela + shdr->sh_size;
2400 irela = internal_relocs;
2401 while (erela < erelaend)
2403 bfd_vma r_symndx;
2405 (*swap_in) (abfd, erela, irela);
2406 r_symndx = ELF32_R_SYM (irela->r_info);
2407 if (bed->s->arch_size == 64)
2408 r_symndx >>= 24;
2409 if (nsyms > 0)
2411 if ((size_t) r_symndx >= nsyms)
2413 _bfd_error_handler
2414 /* xgettext:c-format */
2415 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2416 " for offset %#Lx in section `%A'"),
2417 abfd, r_symndx, (unsigned long) nsyms,
2418 irela->r_offset, sec);
2419 bfd_set_error (bfd_error_bad_value);
2420 return FALSE;
2423 else if (r_symndx != STN_UNDEF)
2425 _bfd_error_handler
2426 /* xgettext:c-format */
2427 (_("%B: non-zero symbol index (%#Lx)"
2428 " for offset %#Lx in section `%A'"
2429 " when the object file has no symbol table"),
2430 abfd, r_symndx,
2431 irela->r_offset, sec);
2432 bfd_set_error (bfd_error_bad_value);
2433 return FALSE;
2435 irela += bed->s->int_rels_per_ext_rel;
2436 erela += shdr->sh_entsize;
2439 return TRUE;
2442 /* Read and swap the relocs for a section O. They may have been
2443 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2444 not NULL, they are used as buffers to read into. They are known to
2445 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2446 the return value is allocated using either malloc or bfd_alloc,
2447 according to the KEEP_MEMORY argument. If O has two relocation
2448 sections (both REL and RELA relocations), then the REL_HDR
2449 relocations will appear first in INTERNAL_RELOCS, followed by the
2450 RELA_HDR relocations. */
2452 Elf_Internal_Rela *
2453 _bfd_elf_link_read_relocs (bfd *abfd,
2454 asection *o,
2455 void *external_relocs,
2456 Elf_Internal_Rela *internal_relocs,
2457 bfd_boolean keep_memory)
2459 void *alloc1 = NULL;
2460 Elf_Internal_Rela *alloc2 = NULL;
2461 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2462 struct bfd_elf_section_data *esdo = elf_section_data (o);
2463 Elf_Internal_Rela *internal_rela_relocs;
2465 if (esdo->relocs != NULL)
2466 return esdo->relocs;
2468 if (o->reloc_count == 0)
2469 return NULL;
2471 if (internal_relocs == NULL)
2473 bfd_size_type size;
2475 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2476 if (keep_memory)
2477 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2478 else
2479 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2480 if (internal_relocs == NULL)
2481 goto error_return;
2484 if (external_relocs == NULL)
2486 bfd_size_type size = 0;
2488 if (esdo->rel.hdr)
2489 size += esdo->rel.hdr->sh_size;
2490 if (esdo->rela.hdr)
2491 size += esdo->rela.hdr->sh_size;
2493 alloc1 = bfd_malloc (size);
2494 if (alloc1 == NULL)
2495 goto error_return;
2496 external_relocs = alloc1;
2499 internal_rela_relocs = internal_relocs;
2500 if (esdo->rel.hdr)
2502 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2503 external_relocs,
2504 internal_relocs))
2505 goto error_return;
2506 external_relocs = (((bfd_byte *) external_relocs)
2507 + esdo->rel.hdr->sh_size);
2508 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2509 * bed->s->int_rels_per_ext_rel);
2512 if (esdo->rela.hdr
2513 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2514 external_relocs,
2515 internal_rela_relocs)))
2516 goto error_return;
2518 /* Cache the results for next time, if we can. */
2519 if (keep_memory)
2520 esdo->relocs = internal_relocs;
2522 if (alloc1 != NULL)
2523 free (alloc1);
2525 /* Don't free alloc2, since if it was allocated we are passing it
2526 back (under the name of internal_relocs). */
2528 return internal_relocs;
2530 error_return:
2531 if (alloc1 != NULL)
2532 free (alloc1);
2533 if (alloc2 != NULL)
2535 if (keep_memory)
2536 bfd_release (abfd, alloc2);
2537 else
2538 free (alloc2);
2540 return NULL;
2543 /* Compute the size of, and allocate space for, REL_HDR which is the
2544 section header for a section containing relocations for O. */
2546 static bfd_boolean
2547 _bfd_elf_link_size_reloc_section (bfd *abfd,
2548 struct bfd_elf_section_reloc_data *reldata)
2550 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2552 /* That allows us to calculate the size of the section. */
2553 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2555 /* The contents field must last into write_object_contents, so we
2556 allocate it with bfd_alloc rather than malloc. Also since we
2557 cannot be sure that the contents will actually be filled in,
2558 we zero the allocated space. */
2559 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2560 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2561 return FALSE;
2563 if (reldata->hashes == NULL && reldata->count)
2565 struct elf_link_hash_entry **p;
2567 p = ((struct elf_link_hash_entry **)
2568 bfd_zmalloc (reldata->count * sizeof (*p)));
2569 if (p == NULL)
2570 return FALSE;
2572 reldata->hashes = p;
2575 return TRUE;
2578 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2579 originated from the section given by INPUT_REL_HDR) to the
2580 OUTPUT_BFD. */
2582 bfd_boolean
2583 _bfd_elf_link_output_relocs (bfd *output_bfd,
2584 asection *input_section,
2585 Elf_Internal_Shdr *input_rel_hdr,
2586 Elf_Internal_Rela *internal_relocs,
2587 struct elf_link_hash_entry **rel_hash
2588 ATTRIBUTE_UNUSED)
2590 Elf_Internal_Rela *irela;
2591 Elf_Internal_Rela *irelaend;
2592 bfd_byte *erel;
2593 struct bfd_elf_section_reloc_data *output_reldata;
2594 asection *output_section;
2595 const struct elf_backend_data *bed;
2596 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2597 struct bfd_elf_section_data *esdo;
2599 output_section = input_section->output_section;
2601 bed = get_elf_backend_data (output_bfd);
2602 esdo = elf_section_data (output_section);
2603 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2605 output_reldata = &esdo->rel;
2606 swap_out = bed->s->swap_reloc_out;
2608 else if (esdo->rela.hdr
2609 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2611 output_reldata = &esdo->rela;
2612 swap_out = bed->s->swap_reloca_out;
2614 else
2616 _bfd_error_handler
2617 /* xgettext:c-format */
2618 (_("%B: relocation size mismatch in %B section %A"),
2619 output_bfd, input_section->owner, input_section);
2620 bfd_set_error (bfd_error_wrong_format);
2621 return FALSE;
2624 erel = output_reldata->hdr->contents;
2625 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2626 irela = internal_relocs;
2627 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2628 * bed->s->int_rels_per_ext_rel);
2629 while (irela < irelaend)
2631 (*swap_out) (output_bfd, irela, erel);
2632 irela += bed->s->int_rels_per_ext_rel;
2633 erel += input_rel_hdr->sh_entsize;
2636 /* Bump the counter, so that we know where to add the next set of
2637 relocations. */
2638 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2640 return TRUE;
2643 /* Make weak undefined symbols in PIE dynamic. */
2645 bfd_boolean
2646 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2647 struct elf_link_hash_entry *h)
2649 if (bfd_link_pie (info)
2650 && h->dynindx == -1
2651 && h->root.type == bfd_link_hash_undefweak)
2652 return bfd_elf_link_record_dynamic_symbol (info, h);
2654 return TRUE;
2657 /* Fix up the flags for a symbol. This handles various cases which
2658 can only be fixed after all the input files are seen. This is
2659 currently called by both adjust_dynamic_symbol and
2660 assign_sym_version, which is unnecessary but perhaps more robust in
2661 the face of future changes. */
2663 static bfd_boolean
2664 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2665 struct elf_info_failed *eif)
2667 const struct elf_backend_data *bed;
2669 /* If this symbol was mentioned in a non-ELF file, try to set
2670 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2671 permit a non-ELF file to correctly refer to a symbol defined in
2672 an ELF dynamic object. */
2673 if (h->non_elf)
2675 while (h->root.type == bfd_link_hash_indirect)
2676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2678 if (h->root.type != bfd_link_hash_defined
2679 && h->root.type != bfd_link_hash_defweak)
2681 h->ref_regular = 1;
2682 h->ref_regular_nonweak = 1;
2684 else
2686 if (h->root.u.def.section->owner != NULL
2687 && (bfd_get_flavour (h->root.u.def.section->owner)
2688 == bfd_target_elf_flavour))
2690 h->ref_regular = 1;
2691 h->ref_regular_nonweak = 1;
2693 else
2694 h->def_regular = 1;
2697 if (h->dynindx == -1
2698 && (h->def_dynamic
2699 || h->ref_dynamic))
2701 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2703 eif->failed = TRUE;
2704 return FALSE;
2708 else
2710 /* Unfortunately, NON_ELF is only correct if the symbol
2711 was first seen in a non-ELF file. Fortunately, if the symbol
2712 was first seen in an ELF file, we're probably OK unless the
2713 symbol was defined in a non-ELF file. Catch that case here.
2714 FIXME: We're still in trouble if the symbol was first seen in
2715 a dynamic object, and then later in a non-ELF regular object. */
2716 if ((h->root.type == bfd_link_hash_defined
2717 || h->root.type == bfd_link_hash_defweak)
2718 && !h->def_regular
2719 && (h->root.u.def.section->owner != NULL
2720 ? (bfd_get_flavour (h->root.u.def.section->owner)
2721 != bfd_target_elf_flavour)
2722 : (bfd_is_abs_section (h->root.u.def.section)
2723 && !h->def_dynamic)))
2724 h->def_regular = 1;
2727 /* Backend specific symbol fixup. */
2728 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2729 if (bed->elf_backend_fixup_symbol
2730 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2731 return FALSE;
2733 /* If this is a final link, and the symbol was defined as a common
2734 symbol in a regular object file, and there was no definition in
2735 any dynamic object, then the linker will have allocated space for
2736 the symbol in a common section but the DEF_REGULAR
2737 flag will not have been set. */
2738 if (h->root.type == bfd_link_hash_defined
2739 && !h->def_regular
2740 && h->ref_regular
2741 && !h->def_dynamic
2742 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2743 h->def_regular = 1;
2745 /* If a weak undefined symbol has non-default visibility, we also
2746 hide it from the dynamic linker. */
2747 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2748 && h->root.type == bfd_link_hash_undefweak)
2749 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2751 /* A hidden versioned symbol in executable should be forced local if
2752 it is is locally defined, not referenced by shared library and not
2753 exported. */
2754 else if (bfd_link_executable (eif->info)
2755 && h->versioned == versioned_hidden
2756 && !eif->info->export_dynamic
2757 && !h->dynamic
2758 && !h->ref_dynamic
2759 && h->def_regular)
2760 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2762 /* If -Bsymbolic was used (which means to bind references to global
2763 symbols to the definition within the shared object), and this
2764 symbol was defined in a regular object, then it actually doesn't
2765 need a PLT entry. Likewise, if the symbol has non-default
2766 visibility. If the symbol has hidden or internal visibility, we
2767 will force it local. */
2768 else if (h->needs_plt
2769 && bfd_link_pic (eif->info)
2770 && is_elf_hash_table (eif->info->hash)
2771 && (SYMBOLIC_BIND (eif->info, h)
2772 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2773 && h->def_regular)
2775 bfd_boolean force_local;
2777 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2778 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2779 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2782 /* If this is a weak defined symbol in a dynamic object, and we know
2783 the real definition in the dynamic object, copy interesting flags
2784 over to the real definition. */
2785 if (h->u.weakdef != NULL)
2787 /* If the real definition is defined by a regular object file,
2788 don't do anything special. See the longer description in
2789 _bfd_elf_adjust_dynamic_symbol, below. */
2790 if (h->u.weakdef->def_regular)
2791 h->u.weakdef = NULL;
2792 else
2794 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2796 while (h->root.type == bfd_link_hash_indirect)
2797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2799 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2800 || h->root.type == bfd_link_hash_defweak);
2801 BFD_ASSERT (weakdef->def_dynamic);
2802 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2803 || weakdef->root.type == bfd_link_hash_defweak);
2804 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2808 return TRUE;
2811 /* Make the backend pick a good value for a dynamic symbol. This is
2812 called via elf_link_hash_traverse, and also calls itself
2813 recursively. */
2815 static bfd_boolean
2816 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2818 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2819 struct elf_link_hash_table *htab;
2820 const struct elf_backend_data *bed;
2822 if (! is_elf_hash_table (eif->info->hash))
2823 return FALSE;
2825 /* Ignore indirect symbols. These are added by the versioning code. */
2826 if (h->root.type == bfd_link_hash_indirect)
2827 return TRUE;
2829 /* Fix the symbol flags. */
2830 if (! _bfd_elf_fix_symbol_flags (h, eif))
2831 return FALSE;
2833 htab = elf_hash_table (eif->info);
2834 bed = get_elf_backend_data (htab->dynobj);
2836 if (h->root.type == bfd_link_hash_undefweak)
2838 if (eif->info->dynamic_undefined_weak == 0)
2839 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2840 else if (eif->info->dynamic_undefined_weak > 0
2841 && h->ref_regular
2842 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2843 && !bfd_hide_sym_by_version (eif->info->version_info,
2844 h->root.root.string))
2846 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2848 eif->failed = TRUE;
2849 return FALSE;
2854 /* If this symbol does not require a PLT entry, and it is not
2855 defined by a dynamic object, or is not referenced by a regular
2856 object, ignore it. We do have to handle a weak defined symbol,
2857 even if no regular object refers to it, if we decided to add it
2858 to the dynamic symbol table. FIXME: Do we normally need to worry
2859 about symbols which are defined by one dynamic object and
2860 referenced by another one? */
2861 if (!h->needs_plt
2862 && h->type != STT_GNU_IFUNC
2863 && (h->def_regular
2864 || !h->def_dynamic
2865 || (!h->ref_regular
2866 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2868 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2869 return TRUE;
2872 /* If we've already adjusted this symbol, don't do it again. This
2873 can happen via a recursive call. */
2874 if (h->dynamic_adjusted)
2875 return TRUE;
2877 /* Don't look at this symbol again. Note that we must set this
2878 after checking the above conditions, because we may look at a
2879 symbol once, decide not to do anything, and then get called
2880 recursively later after REF_REGULAR is set below. */
2881 h->dynamic_adjusted = 1;
2883 /* If this is a weak definition, and we know a real definition, and
2884 the real symbol is not itself defined by a regular object file,
2885 then get a good value for the real definition. We handle the
2886 real symbol first, for the convenience of the backend routine.
2888 Note that there is a confusing case here. If the real definition
2889 is defined by a regular object file, we don't get the real symbol
2890 from the dynamic object, but we do get the weak symbol. If the
2891 processor backend uses a COPY reloc, then if some routine in the
2892 dynamic object changes the real symbol, we will not see that
2893 change in the corresponding weak symbol. This is the way other
2894 ELF linkers work as well, and seems to be a result of the shared
2895 library model.
2897 I will clarify this issue. Most SVR4 shared libraries define the
2898 variable _timezone and define timezone as a weak synonym. The
2899 tzset call changes _timezone. If you write
2900 extern int timezone;
2901 int _timezone = 5;
2902 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2903 you might expect that, since timezone is a synonym for _timezone,
2904 the same number will print both times. However, if the processor
2905 backend uses a COPY reloc, then actually timezone will be copied
2906 into your process image, and, since you define _timezone
2907 yourself, _timezone will not. Thus timezone and _timezone will
2908 wind up at different memory locations. The tzset call will set
2909 _timezone, leaving timezone unchanged. */
2911 if (h->u.weakdef != NULL)
2913 /* If we get to this point, there is an implicit reference to
2914 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2915 h->u.weakdef->ref_regular = 1;
2917 /* Ensure that the backend adjust_dynamic_symbol function sees
2918 H->U.WEAKDEF before H by recursively calling ourselves. */
2919 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2920 return FALSE;
2923 /* If a symbol has no type and no size and does not require a PLT
2924 entry, then we are probably about to do the wrong thing here: we
2925 are probably going to create a COPY reloc for an empty object.
2926 This case can arise when a shared object is built with assembly
2927 code, and the assembly code fails to set the symbol type. */
2928 if (h->size == 0
2929 && h->type == STT_NOTYPE
2930 && !h->needs_plt)
2931 _bfd_error_handler
2932 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2933 h->root.root.string);
2935 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2937 eif->failed = TRUE;
2938 return FALSE;
2941 return TRUE;
2944 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2945 DYNBSS. */
2947 bfd_boolean
2948 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2949 struct elf_link_hash_entry *h,
2950 asection *dynbss)
2952 unsigned int power_of_two;
2953 bfd_vma mask;
2954 asection *sec = h->root.u.def.section;
2956 /* The section alignment of the definition is the maximum alignment
2957 requirement of symbols defined in the section. Since we don't
2958 know the symbol alignment requirement, we start with the
2959 maximum alignment and check low bits of the symbol address
2960 for the minimum alignment. */
2961 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2962 mask = ((bfd_vma) 1 << power_of_two) - 1;
2963 while ((h->root.u.def.value & mask) != 0)
2965 mask >>= 1;
2966 --power_of_two;
2969 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2970 dynbss))
2972 /* Adjust the section alignment if needed. */
2973 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2974 power_of_two))
2975 return FALSE;
2978 /* We make sure that the symbol will be aligned properly. */
2979 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2981 /* Define the symbol as being at this point in DYNBSS. */
2982 h->root.u.def.section = dynbss;
2983 h->root.u.def.value = dynbss->size;
2985 /* Increment the size of DYNBSS to make room for the symbol. */
2986 dynbss->size += h->size;
2988 /* No error if extern_protected_data is true. */
2989 if (h->protected_def
2990 && (!info->extern_protected_data
2991 || (info->extern_protected_data < 0
2992 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2993 info->callbacks->einfo
2994 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2995 h->root.root.string);
2997 return TRUE;
3000 /* Adjust all external symbols pointing into SEC_MERGE sections
3001 to reflect the object merging within the sections. */
3003 static bfd_boolean
3004 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3006 asection *sec;
3008 if ((h->root.type == bfd_link_hash_defined
3009 || h->root.type == bfd_link_hash_defweak)
3010 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3011 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3013 bfd *output_bfd = (bfd *) data;
3015 h->root.u.def.value =
3016 _bfd_merged_section_offset (output_bfd,
3017 &h->root.u.def.section,
3018 elf_section_data (sec)->sec_info,
3019 h->root.u.def.value);
3022 return TRUE;
3025 /* Returns false if the symbol referred to by H should be considered
3026 to resolve local to the current module, and true if it should be
3027 considered to bind dynamically. */
3029 bfd_boolean
3030 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3031 struct bfd_link_info *info,
3032 bfd_boolean not_local_protected)
3034 bfd_boolean binding_stays_local_p;
3035 const struct elf_backend_data *bed;
3036 struct elf_link_hash_table *hash_table;
3038 if (h == NULL)
3039 return FALSE;
3041 while (h->root.type == bfd_link_hash_indirect
3042 || h->root.type == bfd_link_hash_warning)
3043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3045 /* If it was forced local, then clearly it's not dynamic. */
3046 if (h->dynindx == -1)
3047 return FALSE;
3048 if (h->forced_local)
3049 return FALSE;
3051 /* Identify the cases where name binding rules say that a
3052 visible symbol resolves locally. */
3053 binding_stays_local_p = (bfd_link_executable (info)
3054 || SYMBOLIC_BIND (info, h));
3056 switch (ELF_ST_VISIBILITY (h->other))
3058 case STV_INTERNAL:
3059 case STV_HIDDEN:
3060 return FALSE;
3062 case STV_PROTECTED:
3063 hash_table = elf_hash_table (info);
3064 if (!is_elf_hash_table (hash_table))
3065 return FALSE;
3067 bed = get_elf_backend_data (hash_table->dynobj);
3069 /* Proper resolution for function pointer equality may require
3070 that these symbols perhaps be resolved dynamically, even though
3071 we should be resolving them to the current module. */
3072 if (!not_local_protected || !bed->is_function_type (h->type))
3073 binding_stays_local_p = TRUE;
3074 break;
3076 default:
3077 break;
3080 /* If it isn't defined locally, then clearly it's dynamic. */
3081 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3082 return TRUE;
3084 /* Otherwise, the symbol is dynamic if binding rules don't tell
3085 us that it remains local. */
3086 return !binding_stays_local_p;
3089 /* Return true if the symbol referred to by H should be considered
3090 to resolve local to the current module, and false otherwise. Differs
3091 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3092 undefined symbols. The two functions are virtually identical except
3093 for the place where dynindx == -1 is tested. If that test is true,
3094 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3095 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3096 defined symbols.
3097 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3098 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3099 treatment of undefined weak symbols. For those that do not make
3100 undefined weak symbols dynamic, both functions may return false. */
3102 bfd_boolean
3103 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3104 struct bfd_link_info *info,
3105 bfd_boolean local_protected)
3107 const struct elf_backend_data *bed;
3108 struct elf_link_hash_table *hash_table;
3110 /* If it's a local sym, of course we resolve locally. */
3111 if (h == NULL)
3112 return TRUE;
3114 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3115 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3116 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3117 return TRUE;
3119 /* Forced local symbols resolve locally. */
3120 if (h->forced_local)
3121 return TRUE;
3123 /* Common symbols that become definitions don't get the DEF_REGULAR
3124 flag set, so test it first, and don't bail out. */
3125 if (ELF_COMMON_DEF_P (h))
3126 /* Do nothing. */;
3127 /* If we don't have a definition in a regular file, then we can't
3128 resolve locally. The sym is either undefined or dynamic. */
3129 else if (!h->def_regular)
3130 return FALSE;
3132 /* Non-dynamic symbols resolve locally. */
3133 if (h->dynindx == -1)
3134 return TRUE;
3136 /* At this point, we know the symbol is defined and dynamic. In an
3137 executable it must resolve locally, likewise when building symbolic
3138 shared libraries. */
3139 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3140 return TRUE;
3142 /* Now deal with defined dynamic symbols in shared libraries. Ones
3143 with default visibility might not resolve locally. */
3144 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3145 return FALSE;
3147 hash_table = elf_hash_table (info);
3148 if (!is_elf_hash_table (hash_table))
3149 return TRUE;
3151 bed = get_elf_backend_data (hash_table->dynobj);
3153 /* If extern_protected_data is false, STV_PROTECTED non-function
3154 symbols are local. */
3155 if ((!info->extern_protected_data
3156 || (info->extern_protected_data < 0
3157 && !bed->extern_protected_data))
3158 && !bed->is_function_type (h->type))
3159 return TRUE;
3161 /* Function pointer equality tests may require that STV_PROTECTED
3162 symbols be treated as dynamic symbols. If the address of a
3163 function not defined in an executable is set to that function's
3164 plt entry in the executable, then the address of the function in
3165 a shared library must also be the plt entry in the executable. */
3166 return local_protected;
3169 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3170 aligned. Returns the first TLS output section. */
3172 struct bfd_section *
3173 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3175 struct bfd_section *sec, *tls;
3176 unsigned int align = 0;
3178 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3179 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3180 break;
3181 tls = sec;
3183 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3184 if (sec->alignment_power > align)
3185 align = sec->alignment_power;
3187 elf_hash_table (info)->tls_sec = tls;
3189 /* Ensure the alignment of the first section is the largest alignment,
3190 so that the tls segment starts aligned. */
3191 if (tls != NULL)
3192 tls->alignment_power = align;
3194 return tls;
3197 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3198 static bfd_boolean
3199 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3200 Elf_Internal_Sym *sym)
3202 const struct elf_backend_data *bed;
3204 /* Local symbols do not count, but target specific ones might. */
3205 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3206 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3207 return FALSE;
3209 bed = get_elf_backend_data (abfd);
3210 /* Function symbols do not count. */
3211 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3212 return FALSE;
3214 /* If the section is undefined, then so is the symbol. */
3215 if (sym->st_shndx == SHN_UNDEF)
3216 return FALSE;
3218 /* If the symbol is defined in the common section, then
3219 it is a common definition and so does not count. */
3220 if (bed->common_definition (sym))
3221 return FALSE;
3223 /* If the symbol is in a target specific section then we
3224 must rely upon the backend to tell us what it is. */
3225 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3226 /* FIXME - this function is not coded yet:
3228 return _bfd_is_global_symbol_definition (abfd, sym);
3230 Instead for now assume that the definition is not global,
3231 Even if this is wrong, at least the linker will behave
3232 in the same way that it used to do. */
3233 return FALSE;
3235 return TRUE;
3238 /* Search the symbol table of the archive element of the archive ABFD
3239 whose archive map contains a mention of SYMDEF, and determine if
3240 the symbol is defined in this element. */
3241 static bfd_boolean
3242 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3244 Elf_Internal_Shdr * hdr;
3245 size_t symcount;
3246 size_t extsymcount;
3247 size_t extsymoff;
3248 Elf_Internal_Sym *isymbuf;
3249 Elf_Internal_Sym *isym;
3250 Elf_Internal_Sym *isymend;
3251 bfd_boolean result;
3253 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3254 if (abfd == NULL)
3255 return FALSE;
3257 if (! bfd_check_format (abfd, bfd_object))
3258 return FALSE;
3260 /* Select the appropriate symbol table. If we don't know if the
3261 object file is an IR object, give linker LTO plugin a chance to
3262 get the correct symbol table. */
3263 if (abfd->plugin_format == bfd_plugin_yes
3264 #if BFD_SUPPORTS_PLUGINS
3265 || (abfd->plugin_format == bfd_plugin_unknown
3266 && bfd_link_plugin_object_p (abfd))
3267 #endif
3270 /* Use the IR symbol table if the object has been claimed by
3271 plugin. */
3272 abfd = abfd->plugin_dummy_bfd;
3273 hdr = &elf_tdata (abfd)->symtab_hdr;
3275 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3276 hdr = &elf_tdata (abfd)->symtab_hdr;
3277 else
3278 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3280 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3282 /* The sh_info field of the symtab header tells us where the
3283 external symbols start. We don't care about the local symbols. */
3284 if (elf_bad_symtab (abfd))
3286 extsymcount = symcount;
3287 extsymoff = 0;
3289 else
3291 extsymcount = symcount - hdr->sh_info;
3292 extsymoff = hdr->sh_info;
3295 if (extsymcount == 0)
3296 return FALSE;
3298 /* Read in the symbol table. */
3299 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3300 NULL, NULL, NULL);
3301 if (isymbuf == NULL)
3302 return FALSE;
3304 /* Scan the symbol table looking for SYMDEF. */
3305 result = FALSE;
3306 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3308 const char *name;
3310 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3311 isym->st_name);
3312 if (name == NULL)
3313 break;
3315 if (strcmp (name, symdef->name) == 0)
3317 result = is_global_data_symbol_definition (abfd, isym);
3318 break;
3322 free (isymbuf);
3324 return result;
3327 /* Add an entry to the .dynamic table. */
3329 bfd_boolean
3330 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3331 bfd_vma tag,
3332 bfd_vma val)
3334 struct elf_link_hash_table *hash_table;
3335 const struct elf_backend_data *bed;
3336 asection *s;
3337 bfd_size_type newsize;
3338 bfd_byte *newcontents;
3339 Elf_Internal_Dyn dyn;
3341 hash_table = elf_hash_table (info);
3342 if (! is_elf_hash_table (hash_table))
3343 return FALSE;
3345 bed = get_elf_backend_data (hash_table->dynobj);
3346 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3347 BFD_ASSERT (s != NULL);
3349 newsize = s->size + bed->s->sizeof_dyn;
3350 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3351 if (newcontents == NULL)
3352 return FALSE;
3354 dyn.d_tag = tag;
3355 dyn.d_un.d_val = val;
3356 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3358 s->size = newsize;
3359 s->contents = newcontents;
3361 return TRUE;
3364 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3365 otherwise just check whether one already exists. Returns -1 on error,
3366 1 if a DT_NEEDED tag already exists, and 0 on success. */
3368 static int
3369 elf_add_dt_needed_tag (bfd *abfd,
3370 struct bfd_link_info *info,
3371 const char *soname,
3372 bfd_boolean do_it)
3374 struct elf_link_hash_table *hash_table;
3375 size_t strindex;
3377 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3378 return -1;
3380 hash_table = elf_hash_table (info);
3381 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3382 if (strindex == (size_t) -1)
3383 return -1;
3385 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3387 asection *sdyn;
3388 const struct elf_backend_data *bed;
3389 bfd_byte *extdyn;
3391 bed = get_elf_backend_data (hash_table->dynobj);
3392 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3393 if (sdyn != NULL)
3394 for (extdyn = sdyn->contents;
3395 extdyn < sdyn->contents + sdyn->size;
3396 extdyn += bed->s->sizeof_dyn)
3398 Elf_Internal_Dyn dyn;
3400 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3401 if (dyn.d_tag == DT_NEEDED
3402 && dyn.d_un.d_val == strindex)
3404 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3405 return 1;
3410 if (do_it)
3412 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3413 return -1;
3415 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3416 return -1;
3418 else
3419 /* We were just checking for existence of the tag. */
3420 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3422 return 0;
3425 /* Return true if SONAME is on the needed list between NEEDED and STOP
3426 (or the end of list if STOP is NULL), and needed by a library that
3427 will be loaded. */
3429 static bfd_boolean
3430 on_needed_list (const char *soname,
3431 struct bfd_link_needed_list *needed,
3432 struct bfd_link_needed_list *stop)
3434 struct bfd_link_needed_list *look;
3435 for (look = needed; look != stop; look = look->next)
3436 if (strcmp (soname, look->name) == 0
3437 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3438 /* If needed by a library that itself is not directly
3439 needed, recursively check whether that library is
3440 indirectly needed. Since we add DT_NEEDED entries to
3441 the end of the list, library dependencies appear after
3442 the library. Therefore search prior to the current
3443 LOOK, preventing possible infinite recursion. */
3444 || on_needed_list (elf_dt_name (look->by), needed, look)))
3445 return TRUE;
3447 return FALSE;
3450 /* Sort symbol by value, section, and size. */
3451 static int
3452 elf_sort_symbol (const void *arg1, const void *arg2)
3454 const struct elf_link_hash_entry *h1;
3455 const struct elf_link_hash_entry *h2;
3456 bfd_signed_vma vdiff;
3458 h1 = *(const struct elf_link_hash_entry **) arg1;
3459 h2 = *(const struct elf_link_hash_entry **) arg2;
3460 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3461 if (vdiff != 0)
3462 return vdiff > 0 ? 1 : -1;
3463 else
3465 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3466 if (sdiff != 0)
3467 return sdiff > 0 ? 1 : -1;
3469 vdiff = h1->size - h2->size;
3470 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3473 /* This function is used to adjust offsets into .dynstr for
3474 dynamic symbols. This is called via elf_link_hash_traverse. */
3476 static bfd_boolean
3477 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3479 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3481 if (h->dynindx != -1)
3482 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3483 return TRUE;
3486 /* Assign string offsets in .dynstr, update all structures referencing
3487 them. */
3489 static bfd_boolean
3490 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3492 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3493 struct elf_link_local_dynamic_entry *entry;
3494 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3495 bfd *dynobj = hash_table->dynobj;
3496 asection *sdyn;
3497 bfd_size_type size;
3498 const struct elf_backend_data *bed;
3499 bfd_byte *extdyn;
3501 _bfd_elf_strtab_finalize (dynstr);
3502 size = _bfd_elf_strtab_size (dynstr);
3504 bed = get_elf_backend_data (dynobj);
3505 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3506 BFD_ASSERT (sdyn != NULL);
3508 /* Update all .dynamic entries referencing .dynstr strings. */
3509 for (extdyn = sdyn->contents;
3510 extdyn < sdyn->contents + sdyn->size;
3511 extdyn += bed->s->sizeof_dyn)
3513 Elf_Internal_Dyn dyn;
3515 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3516 switch (dyn.d_tag)
3518 case DT_STRSZ:
3519 dyn.d_un.d_val = size;
3520 break;
3521 case DT_NEEDED:
3522 case DT_SONAME:
3523 case DT_RPATH:
3524 case DT_RUNPATH:
3525 case DT_FILTER:
3526 case DT_AUXILIARY:
3527 case DT_AUDIT:
3528 case DT_DEPAUDIT:
3529 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3530 break;
3531 default:
3532 continue;
3534 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3537 /* Now update local dynamic symbols. */
3538 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3539 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3540 entry->isym.st_name);
3542 /* And the rest of dynamic symbols. */
3543 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3545 /* Adjust version definitions. */
3546 if (elf_tdata (output_bfd)->cverdefs)
3548 asection *s;
3549 bfd_byte *p;
3550 size_t i;
3551 Elf_Internal_Verdef def;
3552 Elf_Internal_Verdaux defaux;
3554 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3555 p = s->contents;
3558 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3559 &def);
3560 p += sizeof (Elf_External_Verdef);
3561 if (def.vd_aux != sizeof (Elf_External_Verdef))
3562 continue;
3563 for (i = 0; i < def.vd_cnt; ++i)
3565 _bfd_elf_swap_verdaux_in (output_bfd,
3566 (Elf_External_Verdaux *) p, &defaux);
3567 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3568 defaux.vda_name);
3569 _bfd_elf_swap_verdaux_out (output_bfd,
3570 &defaux, (Elf_External_Verdaux *) p);
3571 p += sizeof (Elf_External_Verdaux);
3574 while (def.vd_next);
3577 /* Adjust version references. */
3578 if (elf_tdata (output_bfd)->verref)
3580 asection *s;
3581 bfd_byte *p;
3582 size_t i;
3583 Elf_Internal_Verneed need;
3584 Elf_Internal_Vernaux needaux;
3586 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3587 p = s->contents;
3590 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3591 &need);
3592 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3593 _bfd_elf_swap_verneed_out (output_bfd, &need,
3594 (Elf_External_Verneed *) p);
3595 p += sizeof (Elf_External_Verneed);
3596 for (i = 0; i < need.vn_cnt; ++i)
3598 _bfd_elf_swap_vernaux_in (output_bfd,
3599 (Elf_External_Vernaux *) p, &needaux);
3600 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3601 needaux.vna_name);
3602 _bfd_elf_swap_vernaux_out (output_bfd,
3603 &needaux,
3604 (Elf_External_Vernaux *) p);
3605 p += sizeof (Elf_External_Vernaux);
3608 while (need.vn_next);
3611 return TRUE;
3614 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3615 The default is to only match when the INPUT and OUTPUT are exactly
3616 the same target. */
3618 bfd_boolean
3619 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3620 const bfd_target *output)
3622 return input == output;
3625 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3626 This version is used when different targets for the same architecture
3627 are virtually identical. */
3629 bfd_boolean
3630 _bfd_elf_relocs_compatible (const bfd_target *input,
3631 const bfd_target *output)
3633 const struct elf_backend_data *obed, *ibed;
3635 if (input == output)
3636 return TRUE;
3638 ibed = xvec_get_elf_backend_data (input);
3639 obed = xvec_get_elf_backend_data (output);
3641 if (ibed->arch != obed->arch)
3642 return FALSE;
3644 /* If both backends are using this function, deem them compatible. */
3645 return ibed->relocs_compatible == obed->relocs_compatible;
3648 /* Make a special call to the linker "notice" function to tell it that
3649 we are about to handle an as-needed lib, or have finished
3650 processing the lib. */
3652 bfd_boolean
3653 _bfd_elf_notice_as_needed (bfd *ibfd,
3654 struct bfd_link_info *info,
3655 enum notice_asneeded_action act)
3657 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3660 /* Check relocations an ELF object file. */
3662 bfd_boolean
3663 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3665 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3666 struct elf_link_hash_table *htab = elf_hash_table (info);
3668 /* If this object is the same format as the output object, and it is
3669 not a shared library, then let the backend look through the
3670 relocs.
3672 This is required to build global offset table entries and to
3673 arrange for dynamic relocs. It is not required for the
3674 particular common case of linking non PIC code, even when linking
3675 against shared libraries, but unfortunately there is no way of
3676 knowing whether an object file has been compiled PIC or not.
3677 Looking through the relocs is not particularly time consuming.
3678 The problem is that we must either (1) keep the relocs in memory,
3679 which causes the linker to require additional runtime memory or
3680 (2) read the relocs twice from the input file, which wastes time.
3681 This would be a good case for using mmap.
3683 I have no idea how to handle linking PIC code into a file of a
3684 different format. It probably can't be done. */
3685 if ((abfd->flags & DYNAMIC) == 0
3686 && is_elf_hash_table (htab)
3687 && bed->check_relocs != NULL
3688 && elf_object_id (abfd) == elf_hash_table_id (htab)
3689 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3691 asection *o;
3693 for (o = abfd->sections; o != NULL; o = o->next)
3695 Elf_Internal_Rela *internal_relocs;
3696 bfd_boolean ok;
3698 /* Don't check relocations in excluded sections. */
3699 if ((o->flags & SEC_RELOC) == 0
3700 || (o->flags & SEC_EXCLUDE) != 0
3701 || o->reloc_count == 0
3702 || ((info->strip == strip_all || info->strip == strip_debugger)
3703 && (o->flags & SEC_DEBUGGING) != 0)
3704 || bfd_is_abs_section (o->output_section))
3705 continue;
3707 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3708 info->keep_memory);
3709 if (internal_relocs == NULL)
3710 return FALSE;
3712 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3714 if (elf_section_data (o)->relocs != internal_relocs)
3715 free (internal_relocs);
3717 if (! ok)
3718 return FALSE;
3722 return TRUE;
3725 /* Add symbols from an ELF object file to the linker hash table. */
3727 static bfd_boolean
3728 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3730 Elf_Internal_Ehdr *ehdr;
3731 Elf_Internal_Shdr *hdr;
3732 size_t symcount;
3733 size_t extsymcount;
3734 size_t extsymoff;
3735 struct elf_link_hash_entry **sym_hash;
3736 bfd_boolean dynamic;
3737 Elf_External_Versym *extversym = NULL;
3738 Elf_External_Versym *ever;
3739 struct elf_link_hash_entry *weaks;
3740 struct elf_link_hash_entry **nondeflt_vers = NULL;
3741 size_t nondeflt_vers_cnt = 0;
3742 Elf_Internal_Sym *isymbuf = NULL;
3743 Elf_Internal_Sym *isym;
3744 Elf_Internal_Sym *isymend;
3745 const struct elf_backend_data *bed;
3746 bfd_boolean add_needed;
3747 struct elf_link_hash_table *htab;
3748 bfd_size_type amt;
3749 void *alloc_mark = NULL;
3750 struct bfd_hash_entry **old_table = NULL;
3751 unsigned int old_size = 0;
3752 unsigned int old_count = 0;
3753 void *old_tab = NULL;
3754 void *old_ent;
3755 struct bfd_link_hash_entry *old_undefs = NULL;
3756 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3757 void *old_strtab = NULL;
3758 size_t tabsize = 0;
3759 asection *s;
3760 bfd_boolean just_syms;
3762 htab = elf_hash_table (info);
3763 bed = get_elf_backend_data (abfd);
3765 if ((abfd->flags & DYNAMIC) == 0)
3766 dynamic = FALSE;
3767 else
3769 dynamic = TRUE;
3771 /* You can't use -r against a dynamic object. Also, there's no
3772 hope of using a dynamic object which does not exactly match
3773 the format of the output file. */
3774 if (bfd_link_relocatable (info)
3775 || !is_elf_hash_table (htab)
3776 || info->output_bfd->xvec != abfd->xvec)
3778 if (bfd_link_relocatable (info))
3779 bfd_set_error (bfd_error_invalid_operation);
3780 else
3781 bfd_set_error (bfd_error_wrong_format);
3782 goto error_return;
3786 ehdr = elf_elfheader (abfd);
3787 if (info->warn_alternate_em
3788 && bed->elf_machine_code != ehdr->e_machine
3789 && ((bed->elf_machine_alt1 != 0
3790 && ehdr->e_machine == bed->elf_machine_alt1)
3791 || (bed->elf_machine_alt2 != 0
3792 && ehdr->e_machine == bed->elf_machine_alt2)))
3793 info->callbacks->einfo
3794 /* xgettext:c-format */
3795 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3796 ehdr->e_machine, abfd, bed->elf_machine_code);
3798 /* As a GNU extension, any input sections which are named
3799 .gnu.warning.SYMBOL are treated as warning symbols for the given
3800 symbol. This differs from .gnu.warning sections, which generate
3801 warnings when they are included in an output file. */
3802 /* PR 12761: Also generate this warning when building shared libraries. */
3803 for (s = abfd->sections; s != NULL; s = s->next)
3805 const char *name;
3807 name = bfd_get_section_name (abfd, s);
3808 if (CONST_STRNEQ (name, ".gnu.warning."))
3810 char *msg;
3811 bfd_size_type sz;
3813 name += sizeof ".gnu.warning." - 1;
3815 /* If this is a shared object, then look up the symbol
3816 in the hash table. If it is there, and it is already
3817 been defined, then we will not be using the entry
3818 from this shared object, so we don't need to warn.
3819 FIXME: If we see the definition in a regular object
3820 later on, we will warn, but we shouldn't. The only
3821 fix is to keep track of what warnings we are supposed
3822 to emit, and then handle them all at the end of the
3823 link. */
3824 if (dynamic)
3826 struct elf_link_hash_entry *h;
3828 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3830 /* FIXME: What about bfd_link_hash_common? */
3831 if (h != NULL
3832 && (h->root.type == bfd_link_hash_defined
3833 || h->root.type == bfd_link_hash_defweak))
3834 continue;
3837 sz = s->size;
3838 msg = (char *) bfd_alloc (abfd, sz + 1);
3839 if (msg == NULL)
3840 goto error_return;
3842 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3843 goto error_return;
3845 msg[sz] = '\0';
3847 if (! (_bfd_generic_link_add_one_symbol
3848 (info, abfd, name, BSF_WARNING, s, 0, msg,
3849 FALSE, bed->collect, NULL)))
3850 goto error_return;
3852 if (bfd_link_executable (info))
3854 /* Clobber the section size so that the warning does
3855 not get copied into the output file. */
3856 s->size = 0;
3858 /* Also set SEC_EXCLUDE, so that symbols defined in
3859 the warning section don't get copied to the output. */
3860 s->flags |= SEC_EXCLUDE;
3865 just_syms = ((s = abfd->sections) != NULL
3866 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3868 add_needed = TRUE;
3869 if (! dynamic)
3871 /* If we are creating a shared library, create all the dynamic
3872 sections immediately. We need to attach them to something,
3873 so we attach them to this BFD, provided it is the right
3874 format and is not from ld --just-symbols. Always create the
3875 dynamic sections for -E/--dynamic-list. FIXME: If there
3876 are no input BFD's of the same format as the output, we can't
3877 make a shared library. */
3878 if (!just_syms
3879 && (bfd_link_pic (info)
3880 || (!bfd_link_relocatable (info)
3881 && info->nointerp
3882 && (info->export_dynamic || info->dynamic)))
3883 && is_elf_hash_table (htab)
3884 && info->output_bfd->xvec == abfd->xvec
3885 && !htab->dynamic_sections_created)
3887 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3888 goto error_return;
3891 else if (!is_elf_hash_table (htab))
3892 goto error_return;
3893 else
3895 const char *soname = NULL;
3896 char *audit = NULL;
3897 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3898 const Elf_Internal_Phdr *phdr;
3899 int ret;
3901 /* ld --just-symbols and dynamic objects don't mix very well.
3902 ld shouldn't allow it. */
3903 if (just_syms)
3904 abort ();
3906 /* If this dynamic lib was specified on the command line with
3907 --as-needed in effect, then we don't want to add a DT_NEEDED
3908 tag unless the lib is actually used. Similary for libs brought
3909 in by another lib's DT_NEEDED. When --no-add-needed is used
3910 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3911 any dynamic library in DT_NEEDED tags in the dynamic lib at
3912 all. */
3913 add_needed = (elf_dyn_lib_class (abfd)
3914 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3915 | DYN_NO_NEEDED)) == 0;
3917 s = bfd_get_section_by_name (abfd, ".dynamic");
3918 if (s != NULL)
3920 bfd_byte *dynbuf;
3921 bfd_byte *extdyn;
3922 unsigned int elfsec;
3923 unsigned long shlink;
3925 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3927 error_free_dyn:
3928 free (dynbuf);
3929 goto error_return;
3932 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3933 if (elfsec == SHN_BAD)
3934 goto error_free_dyn;
3935 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3937 for (extdyn = dynbuf;
3938 extdyn < dynbuf + s->size;
3939 extdyn += bed->s->sizeof_dyn)
3941 Elf_Internal_Dyn dyn;
3943 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3944 if (dyn.d_tag == DT_SONAME)
3946 unsigned int tagv = dyn.d_un.d_val;
3947 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3948 if (soname == NULL)
3949 goto error_free_dyn;
3951 if (dyn.d_tag == DT_NEEDED)
3953 struct bfd_link_needed_list *n, **pn;
3954 char *fnm, *anm;
3955 unsigned int tagv = dyn.d_un.d_val;
3957 amt = sizeof (struct bfd_link_needed_list);
3958 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3959 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3960 if (n == NULL || fnm == NULL)
3961 goto error_free_dyn;
3962 amt = strlen (fnm) + 1;
3963 anm = (char *) bfd_alloc (abfd, amt);
3964 if (anm == NULL)
3965 goto error_free_dyn;
3966 memcpy (anm, fnm, amt);
3967 n->name = anm;
3968 n->by = abfd;
3969 n->next = NULL;
3970 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3972 *pn = n;
3974 if (dyn.d_tag == DT_RUNPATH)
3976 struct bfd_link_needed_list *n, **pn;
3977 char *fnm, *anm;
3978 unsigned int tagv = dyn.d_un.d_val;
3980 amt = sizeof (struct bfd_link_needed_list);
3981 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3982 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3983 if (n == NULL || fnm == NULL)
3984 goto error_free_dyn;
3985 amt = strlen (fnm) + 1;
3986 anm = (char *) bfd_alloc (abfd, amt);
3987 if (anm == NULL)
3988 goto error_free_dyn;
3989 memcpy (anm, fnm, amt);
3990 n->name = anm;
3991 n->by = abfd;
3992 n->next = NULL;
3993 for (pn = & runpath;
3994 *pn != NULL;
3995 pn = &(*pn)->next)
3997 *pn = n;
3999 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4000 if (!runpath && dyn.d_tag == DT_RPATH)
4002 struct bfd_link_needed_list *n, **pn;
4003 char *fnm, *anm;
4004 unsigned int tagv = dyn.d_un.d_val;
4006 amt = sizeof (struct bfd_link_needed_list);
4007 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4008 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4009 if (n == NULL || fnm == NULL)
4010 goto error_free_dyn;
4011 amt = strlen (fnm) + 1;
4012 anm = (char *) bfd_alloc (abfd, amt);
4013 if (anm == NULL)
4014 goto error_free_dyn;
4015 memcpy (anm, fnm, amt);
4016 n->name = anm;
4017 n->by = abfd;
4018 n->next = NULL;
4019 for (pn = & rpath;
4020 *pn != NULL;
4021 pn = &(*pn)->next)
4023 *pn = n;
4025 if (dyn.d_tag == DT_AUDIT)
4027 unsigned int tagv = dyn.d_un.d_val;
4028 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4032 free (dynbuf);
4035 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4036 frees all more recently bfd_alloc'd blocks as well. */
4037 if (runpath)
4038 rpath = runpath;
4040 if (rpath)
4042 struct bfd_link_needed_list **pn;
4043 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4045 *pn = rpath;
4048 /* If we have a PT_GNU_RELRO program header, mark as read-only
4049 all sections contained fully therein. This makes relro
4050 shared library sections appear as they will at run-time. */
4051 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4052 while (--phdr >= elf_tdata (abfd)->phdr)
4053 if (phdr->p_type == PT_GNU_RELRO)
4055 for (s = abfd->sections; s != NULL; s = s->next)
4056 if ((s->flags & SEC_ALLOC) != 0
4057 && s->vma >= phdr->p_vaddr
4058 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4059 s->flags |= SEC_READONLY;
4060 break;
4063 /* We do not want to include any of the sections in a dynamic
4064 object in the output file. We hack by simply clobbering the
4065 list of sections in the BFD. This could be handled more
4066 cleanly by, say, a new section flag; the existing
4067 SEC_NEVER_LOAD flag is not the one we want, because that one
4068 still implies that the section takes up space in the output
4069 file. */
4070 bfd_section_list_clear (abfd);
4072 /* Find the name to use in a DT_NEEDED entry that refers to this
4073 object. If the object has a DT_SONAME entry, we use it.
4074 Otherwise, if the generic linker stuck something in
4075 elf_dt_name, we use that. Otherwise, we just use the file
4076 name. */
4077 if (soname == NULL || *soname == '\0')
4079 soname = elf_dt_name (abfd);
4080 if (soname == NULL || *soname == '\0')
4081 soname = bfd_get_filename (abfd);
4084 /* Save the SONAME because sometimes the linker emulation code
4085 will need to know it. */
4086 elf_dt_name (abfd) = soname;
4088 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4089 if (ret < 0)
4090 goto error_return;
4092 /* If we have already included this dynamic object in the
4093 link, just ignore it. There is no reason to include a
4094 particular dynamic object more than once. */
4095 if (ret > 0)
4096 return TRUE;
4098 /* Save the DT_AUDIT entry for the linker emulation code. */
4099 elf_dt_audit (abfd) = audit;
4102 /* If this is a dynamic object, we always link against the .dynsym
4103 symbol table, not the .symtab symbol table. The dynamic linker
4104 will only see the .dynsym symbol table, so there is no reason to
4105 look at .symtab for a dynamic object. */
4107 if (! dynamic || elf_dynsymtab (abfd) == 0)
4108 hdr = &elf_tdata (abfd)->symtab_hdr;
4109 else
4110 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4112 symcount = hdr->sh_size / bed->s->sizeof_sym;
4114 /* The sh_info field of the symtab header tells us where the
4115 external symbols start. We don't care about the local symbols at
4116 this point. */
4117 if (elf_bad_symtab (abfd))
4119 extsymcount = symcount;
4120 extsymoff = 0;
4122 else
4124 extsymcount = symcount - hdr->sh_info;
4125 extsymoff = hdr->sh_info;
4128 sym_hash = elf_sym_hashes (abfd);
4129 if (extsymcount != 0)
4131 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4132 NULL, NULL, NULL);
4133 if (isymbuf == NULL)
4134 goto error_return;
4136 if (sym_hash == NULL)
4138 /* We store a pointer to the hash table entry for each
4139 external symbol. */
4140 amt = extsymcount;
4141 amt *= sizeof (struct elf_link_hash_entry *);
4142 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4143 if (sym_hash == NULL)
4144 goto error_free_sym;
4145 elf_sym_hashes (abfd) = sym_hash;
4149 if (dynamic)
4151 /* Read in any version definitions. */
4152 if (!_bfd_elf_slurp_version_tables (abfd,
4153 info->default_imported_symver))
4154 goto error_free_sym;
4156 /* Read in the symbol versions, but don't bother to convert them
4157 to internal format. */
4158 if (elf_dynversym (abfd) != 0)
4160 Elf_Internal_Shdr *versymhdr;
4162 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4163 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4164 if (extversym == NULL)
4165 goto error_free_sym;
4166 amt = versymhdr->sh_size;
4167 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4168 || bfd_bread (extversym, amt, abfd) != amt)
4169 goto error_free_vers;
4173 /* If we are loading an as-needed shared lib, save the symbol table
4174 state before we start adding symbols. If the lib turns out
4175 to be unneeded, restore the state. */
4176 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4178 unsigned int i;
4179 size_t entsize;
4181 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4183 struct bfd_hash_entry *p;
4184 struct elf_link_hash_entry *h;
4186 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4188 h = (struct elf_link_hash_entry *) p;
4189 entsize += htab->root.table.entsize;
4190 if (h->root.type == bfd_link_hash_warning)
4191 entsize += htab->root.table.entsize;
4195 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4196 old_tab = bfd_malloc (tabsize + entsize);
4197 if (old_tab == NULL)
4198 goto error_free_vers;
4200 /* Remember the current objalloc pointer, so that all mem for
4201 symbols added can later be reclaimed. */
4202 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4203 if (alloc_mark == NULL)
4204 goto error_free_vers;
4206 /* Make a special call to the linker "notice" function to
4207 tell it that we are about to handle an as-needed lib. */
4208 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4209 goto error_free_vers;
4211 /* Clone the symbol table. Remember some pointers into the
4212 symbol table, and dynamic symbol count. */
4213 old_ent = (char *) old_tab + tabsize;
4214 memcpy (old_tab, htab->root.table.table, tabsize);
4215 old_undefs = htab->root.undefs;
4216 old_undefs_tail = htab->root.undefs_tail;
4217 old_table = htab->root.table.table;
4218 old_size = htab->root.table.size;
4219 old_count = htab->root.table.count;
4220 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4221 if (old_strtab == NULL)
4222 goto error_free_vers;
4224 for (i = 0; i < htab->root.table.size; i++)
4226 struct bfd_hash_entry *p;
4227 struct elf_link_hash_entry *h;
4229 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4231 memcpy (old_ent, p, htab->root.table.entsize);
4232 old_ent = (char *) old_ent + htab->root.table.entsize;
4233 h = (struct elf_link_hash_entry *) p;
4234 if (h->root.type == bfd_link_hash_warning)
4236 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4237 old_ent = (char *) old_ent + htab->root.table.entsize;
4243 weaks = NULL;
4244 ever = extversym != NULL ? extversym + extsymoff : NULL;
4245 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4246 isym < isymend;
4247 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4249 int bind;
4250 bfd_vma value;
4251 asection *sec, *new_sec;
4252 flagword flags;
4253 const char *name;
4254 struct elf_link_hash_entry *h;
4255 struct elf_link_hash_entry *hi;
4256 bfd_boolean definition;
4257 bfd_boolean size_change_ok;
4258 bfd_boolean type_change_ok;
4259 bfd_boolean new_weakdef;
4260 bfd_boolean new_weak;
4261 bfd_boolean old_weak;
4262 bfd_boolean override;
4263 bfd_boolean common;
4264 bfd_boolean discarded;
4265 unsigned int old_alignment;
4266 bfd *old_bfd;
4267 bfd_boolean matched;
4269 override = FALSE;
4271 flags = BSF_NO_FLAGS;
4272 sec = NULL;
4273 value = isym->st_value;
4274 common = bed->common_definition (isym);
4275 if (common && info->inhibit_common_definition)
4277 /* Treat common symbol as undefined for --no-define-common. */
4278 isym->st_shndx = SHN_UNDEF;
4279 common = FALSE;
4281 discarded = FALSE;
4283 bind = ELF_ST_BIND (isym->st_info);
4284 switch (bind)
4286 case STB_LOCAL:
4287 /* This should be impossible, since ELF requires that all
4288 global symbols follow all local symbols, and that sh_info
4289 point to the first global symbol. Unfortunately, Irix 5
4290 screws this up. */
4291 continue;
4293 case STB_GLOBAL:
4294 if (isym->st_shndx != SHN_UNDEF && !common)
4295 flags = BSF_GLOBAL;
4296 break;
4298 case STB_WEAK:
4299 flags = BSF_WEAK;
4300 break;
4302 case STB_GNU_UNIQUE:
4303 flags = BSF_GNU_UNIQUE;
4304 break;
4306 default:
4307 /* Leave it up to the processor backend. */
4308 break;
4311 if (isym->st_shndx == SHN_UNDEF)
4312 sec = bfd_und_section_ptr;
4313 else if (isym->st_shndx == SHN_ABS)
4314 sec = bfd_abs_section_ptr;
4315 else if (isym->st_shndx == SHN_COMMON)
4317 sec = bfd_com_section_ptr;
4318 /* What ELF calls the size we call the value. What ELF
4319 calls the value we call the alignment. */
4320 value = isym->st_size;
4322 else
4324 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4325 if (sec == NULL)
4326 sec = bfd_abs_section_ptr;
4327 else if (discarded_section (sec))
4329 /* Symbols from discarded section are undefined. We keep
4330 its visibility. */
4331 sec = bfd_und_section_ptr;
4332 discarded = TRUE;
4333 isym->st_shndx = SHN_UNDEF;
4335 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4336 value -= sec->vma;
4339 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4340 isym->st_name);
4341 if (name == NULL)
4342 goto error_free_vers;
4344 if (isym->st_shndx == SHN_COMMON
4345 && (abfd->flags & BFD_PLUGIN) != 0)
4347 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4349 if (xc == NULL)
4351 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4352 | SEC_EXCLUDE);
4353 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4354 if (xc == NULL)
4355 goto error_free_vers;
4357 sec = xc;
4359 else if (isym->st_shndx == SHN_COMMON
4360 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4361 && !bfd_link_relocatable (info))
4363 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4365 if (tcomm == NULL)
4367 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4368 | SEC_LINKER_CREATED);
4369 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4370 if (tcomm == NULL)
4371 goto error_free_vers;
4373 sec = tcomm;
4375 else if (bed->elf_add_symbol_hook)
4377 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4378 &sec, &value))
4379 goto error_free_vers;
4381 /* The hook function sets the name to NULL if this symbol
4382 should be skipped for some reason. */
4383 if (name == NULL)
4384 continue;
4387 /* Sanity check that all possibilities were handled. */
4388 if (sec == NULL)
4390 bfd_set_error (bfd_error_bad_value);
4391 goto error_free_vers;
4394 /* Silently discard TLS symbols from --just-syms. There's
4395 no way to combine a static TLS block with a new TLS block
4396 for this executable. */
4397 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4398 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4399 continue;
4401 if (bfd_is_und_section (sec)
4402 || bfd_is_com_section (sec))
4403 definition = FALSE;
4404 else
4405 definition = TRUE;
4407 size_change_ok = FALSE;
4408 type_change_ok = bed->type_change_ok;
4409 old_weak = FALSE;
4410 matched = FALSE;
4411 old_alignment = 0;
4412 old_bfd = NULL;
4413 new_sec = sec;
4415 if (is_elf_hash_table (htab))
4417 Elf_Internal_Versym iver;
4418 unsigned int vernum = 0;
4419 bfd_boolean skip;
4421 if (ever == NULL)
4423 if (info->default_imported_symver)
4424 /* Use the default symbol version created earlier. */
4425 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4426 else
4427 iver.vs_vers = 0;
4429 else
4430 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4432 vernum = iver.vs_vers & VERSYM_VERSION;
4434 /* If this is a hidden symbol, or if it is not version
4435 1, we append the version name to the symbol name.
4436 However, we do not modify a non-hidden absolute symbol
4437 if it is not a function, because it might be the version
4438 symbol itself. FIXME: What if it isn't? */
4439 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4440 || (vernum > 1
4441 && (!bfd_is_abs_section (sec)
4442 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4444 const char *verstr;
4445 size_t namelen, verlen, newlen;
4446 char *newname, *p;
4448 if (isym->st_shndx != SHN_UNDEF)
4450 if (vernum > elf_tdata (abfd)->cverdefs)
4451 verstr = NULL;
4452 else if (vernum > 1)
4453 verstr =
4454 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4455 else
4456 verstr = "";
4458 if (verstr == NULL)
4460 _bfd_error_handler
4461 /* xgettext:c-format */
4462 (_("%B: %s: invalid version %u (max %d)"),
4463 abfd, name, vernum,
4464 elf_tdata (abfd)->cverdefs);
4465 bfd_set_error (bfd_error_bad_value);
4466 goto error_free_vers;
4469 else
4471 /* We cannot simply test for the number of
4472 entries in the VERNEED section since the
4473 numbers for the needed versions do not start
4474 at 0. */
4475 Elf_Internal_Verneed *t;
4477 verstr = NULL;
4478 for (t = elf_tdata (abfd)->verref;
4479 t != NULL;
4480 t = t->vn_nextref)
4482 Elf_Internal_Vernaux *a;
4484 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4486 if (a->vna_other == vernum)
4488 verstr = a->vna_nodename;
4489 break;
4492 if (a != NULL)
4493 break;
4495 if (verstr == NULL)
4497 _bfd_error_handler
4498 /* xgettext:c-format */
4499 (_("%B: %s: invalid needed version %d"),
4500 abfd, name, vernum);
4501 bfd_set_error (bfd_error_bad_value);
4502 goto error_free_vers;
4506 namelen = strlen (name);
4507 verlen = strlen (verstr);
4508 newlen = namelen + verlen + 2;
4509 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4510 && isym->st_shndx != SHN_UNDEF)
4511 ++newlen;
4513 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4514 if (newname == NULL)
4515 goto error_free_vers;
4516 memcpy (newname, name, namelen);
4517 p = newname + namelen;
4518 *p++ = ELF_VER_CHR;
4519 /* If this is a defined non-hidden version symbol,
4520 we add another @ to the name. This indicates the
4521 default version of the symbol. */
4522 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4523 && isym->st_shndx != SHN_UNDEF)
4524 *p++ = ELF_VER_CHR;
4525 memcpy (p, verstr, verlen + 1);
4527 name = newname;
4530 /* If this symbol has default visibility and the user has
4531 requested we not re-export it, then mark it as hidden. */
4532 if (!bfd_is_und_section (sec)
4533 && !dynamic
4534 && abfd->no_export
4535 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4536 isym->st_other = (STV_HIDDEN
4537 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4539 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4540 sym_hash, &old_bfd, &old_weak,
4541 &old_alignment, &skip, &override,
4542 &type_change_ok, &size_change_ok,
4543 &matched))
4544 goto error_free_vers;
4546 if (skip)
4547 continue;
4549 /* Override a definition only if the new symbol matches the
4550 existing one. */
4551 if (override && matched)
4552 definition = FALSE;
4554 h = *sym_hash;
4555 while (h->root.type == bfd_link_hash_indirect
4556 || h->root.type == bfd_link_hash_warning)
4557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4559 if (elf_tdata (abfd)->verdef != NULL
4560 && vernum > 1
4561 && definition)
4562 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4565 if (! (_bfd_generic_link_add_one_symbol
4566 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4567 (struct bfd_link_hash_entry **) sym_hash)))
4568 goto error_free_vers;
4570 if ((flags & BSF_GNU_UNIQUE)
4571 && (abfd->flags & DYNAMIC) == 0
4572 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4573 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4575 h = *sym_hash;
4576 /* We need to make sure that indirect symbol dynamic flags are
4577 updated. */
4578 hi = h;
4579 while (h->root.type == bfd_link_hash_indirect
4580 || h->root.type == bfd_link_hash_warning)
4581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4583 /* Setting the index to -3 tells elf_link_output_extsym that
4584 this symbol is defined in a discarded section. */
4585 if (discarded)
4586 h->indx = -3;
4588 *sym_hash = h;
4590 new_weak = (flags & BSF_WEAK) != 0;
4591 new_weakdef = FALSE;
4592 if (dynamic
4593 && definition
4594 && new_weak
4595 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4596 && is_elf_hash_table (htab)
4597 && h->u.weakdef == NULL)
4599 /* Keep a list of all weak defined non function symbols from
4600 a dynamic object, using the weakdef field. Later in this
4601 function we will set the weakdef field to the correct
4602 value. We only put non-function symbols from dynamic
4603 objects on this list, because that happens to be the only
4604 time we need to know the normal symbol corresponding to a
4605 weak symbol, and the information is time consuming to
4606 figure out. If the weakdef field is not already NULL,
4607 then this symbol was already defined by some previous
4608 dynamic object, and we will be using that previous
4609 definition anyhow. */
4611 h->u.weakdef = weaks;
4612 weaks = h;
4613 new_weakdef = TRUE;
4616 /* Set the alignment of a common symbol. */
4617 if ((common || bfd_is_com_section (sec))
4618 && h->root.type == bfd_link_hash_common)
4620 unsigned int align;
4622 if (common)
4623 align = bfd_log2 (isym->st_value);
4624 else
4626 /* The new symbol is a common symbol in a shared object.
4627 We need to get the alignment from the section. */
4628 align = new_sec->alignment_power;
4630 if (align > old_alignment)
4631 h->root.u.c.p->alignment_power = align;
4632 else
4633 h->root.u.c.p->alignment_power = old_alignment;
4636 if (is_elf_hash_table (htab))
4638 /* Set a flag in the hash table entry indicating the type of
4639 reference or definition we just found. A dynamic symbol
4640 is one which is referenced or defined by both a regular
4641 object and a shared object. */
4642 bfd_boolean dynsym = FALSE;
4644 /* Plugin symbols aren't normal. Don't set def_regular or
4645 ref_regular for them, or make them dynamic. */
4646 if ((abfd->flags & BFD_PLUGIN) != 0)
4648 else if (! dynamic)
4650 if (! definition)
4652 h->ref_regular = 1;
4653 if (bind != STB_WEAK)
4654 h->ref_regular_nonweak = 1;
4656 else
4658 h->def_regular = 1;
4659 if (h->def_dynamic)
4661 h->def_dynamic = 0;
4662 h->ref_dynamic = 1;
4666 /* If the indirect symbol has been forced local, don't
4667 make the real symbol dynamic. */
4668 if ((h == hi || !hi->forced_local)
4669 && (bfd_link_dll (info)
4670 || h->def_dynamic
4671 || h->ref_dynamic))
4672 dynsym = TRUE;
4674 else
4676 if (! definition)
4678 h->ref_dynamic = 1;
4679 hi->ref_dynamic = 1;
4681 else
4683 h->def_dynamic = 1;
4684 hi->def_dynamic = 1;
4687 /* If the indirect symbol has been forced local, don't
4688 make the real symbol dynamic. */
4689 if ((h == hi || !hi->forced_local)
4690 && (h->def_regular
4691 || h->ref_regular
4692 || (h->u.weakdef != NULL
4693 && ! new_weakdef
4694 && h->u.weakdef->dynindx != -1)))
4695 dynsym = TRUE;
4698 /* Check to see if we need to add an indirect symbol for
4699 the default name. */
4700 if (definition
4701 || (!override && h->root.type == bfd_link_hash_common))
4702 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4703 sec, value, &old_bfd, &dynsym))
4704 goto error_free_vers;
4706 /* Check the alignment when a common symbol is involved. This
4707 can change when a common symbol is overridden by a normal
4708 definition or a common symbol is ignored due to the old
4709 normal definition. We need to make sure the maximum
4710 alignment is maintained. */
4711 if ((old_alignment || common)
4712 && h->root.type != bfd_link_hash_common)
4714 unsigned int common_align;
4715 unsigned int normal_align;
4716 unsigned int symbol_align;
4717 bfd *normal_bfd;
4718 bfd *common_bfd;
4720 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4721 || h->root.type == bfd_link_hash_defweak);
4723 symbol_align = ffs (h->root.u.def.value) - 1;
4724 if (h->root.u.def.section->owner != NULL
4725 && (h->root.u.def.section->owner->flags
4726 & (DYNAMIC | BFD_PLUGIN)) == 0)
4728 normal_align = h->root.u.def.section->alignment_power;
4729 if (normal_align > symbol_align)
4730 normal_align = symbol_align;
4732 else
4733 normal_align = symbol_align;
4735 if (old_alignment)
4737 common_align = old_alignment;
4738 common_bfd = old_bfd;
4739 normal_bfd = abfd;
4741 else
4743 common_align = bfd_log2 (isym->st_value);
4744 common_bfd = abfd;
4745 normal_bfd = old_bfd;
4748 if (normal_align < common_align)
4750 /* PR binutils/2735 */
4751 if (normal_bfd == NULL)
4752 _bfd_error_handler
4753 /* xgettext:c-format */
4754 (_("Warning: alignment %u of common symbol `%s' in %B is"
4755 " greater than the alignment (%u) of its section %A"),
4756 1 << common_align, name, common_bfd,
4757 1 << normal_align, h->root.u.def.section);
4758 else
4759 _bfd_error_handler
4760 /* xgettext:c-format */
4761 (_("Warning: alignment %u of symbol `%s' in %B"
4762 " is smaller than %u in %B"),
4763 1 << normal_align, name, normal_bfd,
4764 1 << common_align, common_bfd);
4768 /* Remember the symbol size if it isn't undefined. */
4769 if (isym->st_size != 0
4770 && isym->st_shndx != SHN_UNDEF
4771 && (definition || h->size == 0))
4773 if (h->size != 0
4774 && h->size != isym->st_size
4775 && ! size_change_ok)
4776 _bfd_error_handler
4777 /* xgettext:c-format */
4778 (_("Warning: size of symbol `%s' changed"
4779 " from %Lu in %B to %Lu in %B"),
4780 name, h->size, old_bfd, isym->st_size, abfd);
4782 h->size = isym->st_size;
4785 /* If this is a common symbol, then we always want H->SIZE
4786 to be the size of the common symbol. The code just above
4787 won't fix the size if a common symbol becomes larger. We
4788 don't warn about a size change here, because that is
4789 covered by --warn-common. Allow changes between different
4790 function types. */
4791 if (h->root.type == bfd_link_hash_common)
4792 h->size = h->root.u.c.size;
4794 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4795 && ((definition && !new_weak)
4796 || (old_weak && h->root.type == bfd_link_hash_common)
4797 || h->type == STT_NOTYPE))
4799 unsigned int type = ELF_ST_TYPE (isym->st_info);
4801 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4802 symbol. */
4803 if (type == STT_GNU_IFUNC
4804 && (abfd->flags & DYNAMIC) != 0)
4805 type = STT_FUNC;
4807 if (h->type != type)
4809 if (h->type != STT_NOTYPE && ! type_change_ok)
4810 /* xgettext:c-format */
4811 _bfd_error_handler
4812 (_("Warning: type of symbol `%s' changed"
4813 " from %d to %d in %B"),
4814 name, h->type, type, abfd);
4816 h->type = type;
4820 /* Merge st_other field. */
4821 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4823 /* We don't want to make debug symbol dynamic. */
4824 if (definition
4825 && (sec->flags & SEC_DEBUGGING)
4826 && !bfd_link_relocatable (info))
4827 dynsym = FALSE;
4829 /* Nor should we make plugin symbols dynamic. */
4830 if ((abfd->flags & BFD_PLUGIN) != 0)
4831 dynsym = FALSE;
4833 if (definition)
4835 h->target_internal = isym->st_target_internal;
4836 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4839 if (definition && !dynamic)
4841 char *p = strchr (name, ELF_VER_CHR);
4842 if (p != NULL && p[1] != ELF_VER_CHR)
4844 /* Queue non-default versions so that .symver x, x@FOO
4845 aliases can be checked. */
4846 if (!nondeflt_vers)
4848 amt = ((isymend - isym + 1)
4849 * sizeof (struct elf_link_hash_entry *));
4850 nondeflt_vers
4851 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4852 if (!nondeflt_vers)
4853 goto error_free_vers;
4855 nondeflt_vers[nondeflt_vers_cnt++] = h;
4859 if (dynsym && h->dynindx == -1)
4861 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4862 goto error_free_vers;
4863 if (h->u.weakdef != NULL
4864 && ! new_weakdef
4865 && h->u.weakdef->dynindx == -1)
4867 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4868 goto error_free_vers;
4871 else if (h->dynindx != -1)
4872 /* If the symbol already has a dynamic index, but
4873 visibility says it should not be visible, turn it into
4874 a local symbol. */
4875 switch (ELF_ST_VISIBILITY (h->other))
4877 case STV_INTERNAL:
4878 case STV_HIDDEN:
4879 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4880 dynsym = FALSE;
4881 break;
4884 /* Don't add DT_NEEDED for references from the dummy bfd nor
4885 for unmatched symbol. */
4886 if (!add_needed
4887 && matched
4888 && definition
4889 && ((dynsym
4890 && h->ref_regular_nonweak
4891 && (old_bfd == NULL
4892 || (old_bfd->flags & BFD_PLUGIN) == 0))
4893 || (h->ref_dynamic_nonweak
4894 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4895 && !on_needed_list (elf_dt_name (abfd),
4896 htab->needed, NULL))))
4898 int ret;
4899 const char *soname = elf_dt_name (abfd);
4901 info->callbacks->minfo ("%!", soname, old_bfd,
4902 h->root.root.string);
4904 /* A symbol from a library loaded via DT_NEEDED of some
4905 other library is referenced by a regular object.
4906 Add a DT_NEEDED entry for it. Issue an error if
4907 --no-add-needed is used and the reference was not
4908 a weak one. */
4909 if (old_bfd != NULL
4910 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4912 _bfd_error_handler
4913 /* xgettext:c-format */
4914 (_("%B: undefined reference to symbol '%s'"),
4915 old_bfd, name);
4916 bfd_set_error (bfd_error_missing_dso);
4917 goto error_free_vers;
4920 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4921 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4923 add_needed = TRUE;
4924 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4925 if (ret < 0)
4926 goto error_free_vers;
4928 BFD_ASSERT (ret == 0);
4933 if (extversym != NULL)
4935 free (extversym);
4936 extversym = NULL;
4939 if (isymbuf != NULL)
4941 free (isymbuf);
4942 isymbuf = NULL;
4945 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4947 unsigned int i;
4949 /* Restore the symbol table. */
4950 old_ent = (char *) old_tab + tabsize;
4951 memset (elf_sym_hashes (abfd), 0,
4952 extsymcount * sizeof (struct elf_link_hash_entry *));
4953 htab->root.table.table = old_table;
4954 htab->root.table.size = old_size;
4955 htab->root.table.count = old_count;
4956 memcpy (htab->root.table.table, old_tab, tabsize);
4957 htab->root.undefs = old_undefs;
4958 htab->root.undefs_tail = old_undefs_tail;
4959 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4960 free (old_strtab);
4961 old_strtab = NULL;
4962 for (i = 0; i < htab->root.table.size; i++)
4964 struct bfd_hash_entry *p;
4965 struct elf_link_hash_entry *h;
4966 bfd_size_type size;
4967 unsigned int alignment_power;
4968 unsigned int non_ir_ref_dynamic;
4970 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4972 h = (struct elf_link_hash_entry *) p;
4973 if (h->root.type == bfd_link_hash_warning)
4974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4976 /* Preserve the maximum alignment and size for common
4977 symbols even if this dynamic lib isn't on DT_NEEDED
4978 since it can still be loaded at run time by another
4979 dynamic lib. */
4980 if (h->root.type == bfd_link_hash_common)
4982 size = h->root.u.c.size;
4983 alignment_power = h->root.u.c.p->alignment_power;
4985 else
4987 size = 0;
4988 alignment_power = 0;
4990 /* Preserve non_ir_ref_dynamic so that this symbol
4991 will be exported when the dynamic lib becomes needed
4992 in the second pass. */
4993 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
4994 memcpy (p, old_ent, htab->root.table.entsize);
4995 old_ent = (char *) old_ent + htab->root.table.entsize;
4996 h = (struct elf_link_hash_entry *) p;
4997 if (h->root.type == bfd_link_hash_warning)
4999 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5000 old_ent = (char *) old_ent + htab->root.table.entsize;
5001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5003 if (h->root.type == bfd_link_hash_common)
5005 if (size > h->root.u.c.size)
5006 h->root.u.c.size = size;
5007 if (alignment_power > h->root.u.c.p->alignment_power)
5008 h->root.u.c.p->alignment_power = alignment_power;
5010 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5014 /* Make a special call to the linker "notice" function to
5015 tell it that symbols added for crefs may need to be removed. */
5016 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5017 goto error_free_vers;
5019 free (old_tab);
5020 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5021 alloc_mark);
5022 if (nondeflt_vers != NULL)
5023 free (nondeflt_vers);
5024 return TRUE;
5027 if (old_tab != NULL)
5029 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5030 goto error_free_vers;
5031 free (old_tab);
5032 old_tab = NULL;
5035 /* Now that all the symbols from this input file are created, if
5036 not performing a relocatable link, handle .symver foo, foo@BAR
5037 such that any relocs against foo become foo@BAR. */
5038 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5040 size_t cnt, symidx;
5042 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5044 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5045 char *shortname, *p;
5047 p = strchr (h->root.root.string, ELF_VER_CHR);
5048 if (p == NULL
5049 || (h->root.type != bfd_link_hash_defined
5050 && h->root.type != bfd_link_hash_defweak))
5051 continue;
5053 amt = p - h->root.root.string;
5054 shortname = (char *) bfd_malloc (amt + 1);
5055 if (!shortname)
5056 goto error_free_vers;
5057 memcpy (shortname, h->root.root.string, amt);
5058 shortname[amt] = '\0';
5060 hi = (struct elf_link_hash_entry *)
5061 bfd_link_hash_lookup (&htab->root, shortname,
5062 FALSE, FALSE, FALSE);
5063 if (hi != NULL
5064 && hi->root.type == h->root.type
5065 && hi->root.u.def.value == h->root.u.def.value
5066 && hi->root.u.def.section == h->root.u.def.section)
5068 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5069 hi->root.type = bfd_link_hash_indirect;
5070 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5071 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5072 sym_hash = elf_sym_hashes (abfd);
5073 if (sym_hash)
5074 for (symidx = 0; symidx < extsymcount; ++symidx)
5075 if (sym_hash[symidx] == hi)
5077 sym_hash[symidx] = h;
5078 break;
5081 free (shortname);
5083 free (nondeflt_vers);
5084 nondeflt_vers = NULL;
5087 /* Now set the weakdefs field correctly for all the weak defined
5088 symbols we found. The only way to do this is to search all the
5089 symbols. Since we only need the information for non functions in
5090 dynamic objects, that's the only time we actually put anything on
5091 the list WEAKS. We need this information so that if a regular
5092 object refers to a symbol defined weakly in a dynamic object, the
5093 real symbol in the dynamic object is also put in the dynamic
5094 symbols; we also must arrange for both symbols to point to the
5095 same memory location. We could handle the general case of symbol
5096 aliasing, but a general symbol alias can only be generated in
5097 assembler code, handling it correctly would be very time
5098 consuming, and other ELF linkers don't handle general aliasing
5099 either. */
5100 if (weaks != NULL)
5102 struct elf_link_hash_entry **hpp;
5103 struct elf_link_hash_entry **hppend;
5104 struct elf_link_hash_entry **sorted_sym_hash;
5105 struct elf_link_hash_entry *h;
5106 size_t sym_count;
5108 /* Since we have to search the whole symbol list for each weak
5109 defined symbol, search time for N weak defined symbols will be
5110 O(N^2). Binary search will cut it down to O(NlogN). */
5111 amt = extsymcount;
5112 amt *= sizeof (struct elf_link_hash_entry *);
5113 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5114 if (sorted_sym_hash == NULL)
5115 goto error_return;
5116 sym_hash = sorted_sym_hash;
5117 hpp = elf_sym_hashes (abfd);
5118 hppend = hpp + extsymcount;
5119 sym_count = 0;
5120 for (; hpp < hppend; hpp++)
5122 h = *hpp;
5123 if (h != NULL
5124 && h->root.type == bfd_link_hash_defined
5125 && !bed->is_function_type (h->type))
5127 *sym_hash = h;
5128 sym_hash++;
5129 sym_count++;
5133 qsort (sorted_sym_hash, sym_count,
5134 sizeof (struct elf_link_hash_entry *),
5135 elf_sort_symbol);
5137 while (weaks != NULL)
5139 struct elf_link_hash_entry *hlook;
5140 asection *slook;
5141 bfd_vma vlook;
5142 size_t i, j, idx = 0;
5144 hlook = weaks;
5145 weaks = hlook->u.weakdef;
5146 hlook->u.weakdef = NULL;
5148 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5149 || hlook->root.type == bfd_link_hash_defweak
5150 || hlook->root.type == bfd_link_hash_common
5151 || hlook->root.type == bfd_link_hash_indirect);
5152 slook = hlook->root.u.def.section;
5153 vlook = hlook->root.u.def.value;
5155 i = 0;
5156 j = sym_count;
5157 while (i != j)
5159 bfd_signed_vma vdiff;
5160 idx = (i + j) / 2;
5161 h = sorted_sym_hash[idx];
5162 vdiff = vlook - h->root.u.def.value;
5163 if (vdiff < 0)
5164 j = idx;
5165 else if (vdiff > 0)
5166 i = idx + 1;
5167 else
5169 int sdiff = slook->id - h->root.u.def.section->id;
5170 if (sdiff < 0)
5171 j = idx;
5172 else if (sdiff > 0)
5173 i = idx + 1;
5174 else
5175 break;
5179 /* We didn't find a value/section match. */
5180 if (i == j)
5181 continue;
5183 /* With multiple aliases, or when the weak symbol is already
5184 strongly defined, we have multiple matching symbols and
5185 the binary search above may land on any of them. Step
5186 one past the matching symbol(s). */
5187 while (++idx != j)
5189 h = sorted_sym_hash[idx];
5190 if (h->root.u.def.section != slook
5191 || h->root.u.def.value != vlook)
5192 break;
5195 /* Now look back over the aliases. Since we sorted by size
5196 as well as value and section, we'll choose the one with
5197 the largest size. */
5198 while (idx-- != i)
5200 h = sorted_sym_hash[idx];
5202 /* Stop if value or section doesn't match. */
5203 if (h->root.u.def.section != slook
5204 || h->root.u.def.value != vlook)
5205 break;
5206 else if (h != hlook)
5208 hlook->u.weakdef = h;
5210 /* If the weak definition is in the list of dynamic
5211 symbols, make sure the real definition is put
5212 there as well. */
5213 if (hlook->dynindx != -1 && h->dynindx == -1)
5215 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5217 err_free_sym_hash:
5218 free (sorted_sym_hash);
5219 goto error_return;
5223 /* If the real definition is in the list of dynamic
5224 symbols, make sure the weak definition is put
5225 there as well. If we don't do this, then the
5226 dynamic loader might not merge the entries for the
5227 real definition and the weak definition. */
5228 if (h->dynindx != -1 && hlook->dynindx == -1)
5230 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5231 goto err_free_sym_hash;
5233 break;
5238 free (sorted_sym_hash);
5241 if (bed->check_directives
5242 && !(*bed->check_directives) (abfd, info))
5243 return FALSE;
5245 /* If this is a non-traditional link, try to optimize the handling
5246 of the .stab/.stabstr sections. */
5247 if (! dynamic
5248 && ! info->traditional_format
5249 && is_elf_hash_table (htab)
5250 && (info->strip != strip_all && info->strip != strip_debugger))
5252 asection *stabstr;
5254 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5255 if (stabstr != NULL)
5257 bfd_size_type string_offset = 0;
5258 asection *stab;
5260 for (stab = abfd->sections; stab; stab = stab->next)
5261 if (CONST_STRNEQ (stab->name, ".stab")
5262 && (!stab->name[5] ||
5263 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5264 && (stab->flags & SEC_MERGE) == 0
5265 && !bfd_is_abs_section (stab->output_section))
5267 struct bfd_elf_section_data *secdata;
5269 secdata = elf_section_data (stab);
5270 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5271 stabstr, &secdata->sec_info,
5272 &string_offset))
5273 goto error_return;
5274 if (secdata->sec_info)
5275 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5280 if (is_elf_hash_table (htab) && add_needed)
5282 /* Add this bfd to the loaded list. */
5283 struct elf_link_loaded_list *n;
5285 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5286 if (n == NULL)
5287 goto error_return;
5288 n->abfd = abfd;
5289 n->next = htab->loaded;
5290 htab->loaded = n;
5293 return TRUE;
5295 error_free_vers:
5296 if (old_tab != NULL)
5297 free (old_tab);
5298 if (old_strtab != NULL)
5299 free (old_strtab);
5300 if (nondeflt_vers != NULL)
5301 free (nondeflt_vers);
5302 if (extversym != NULL)
5303 free (extversym);
5304 error_free_sym:
5305 if (isymbuf != NULL)
5306 free (isymbuf);
5307 error_return:
5308 return FALSE;
5311 /* Return the linker hash table entry of a symbol that might be
5312 satisfied by an archive symbol. Return -1 on error. */
5314 struct elf_link_hash_entry *
5315 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5316 struct bfd_link_info *info,
5317 const char *name)
5319 struct elf_link_hash_entry *h;
5320 char *p, *copy;
5321 size_t len, first;
5323 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5324 if (h != NULL)
5325 return h;
5327 /* If this is a default version (the name contains @@), look up the
5328 symbol again with only one `@' as well as without the version.
5329 The effect is that references to the symbol with and without the
5330 version will be matched by the default symbol in the archive. */
5332 p = strchr (name, ELF_VER_CHR);
5333 if (p == NULL || p[1] != ELF_VER_CHR)
5334 return h;
5336 /* First check with only one `@'. */
5337 len = strlen (name);
5338 copy = (char *) bfd_alloc (abfd, len);
5339 if (copy == NULL)
5340 return (struct elf_link_hash_entry *) 0 - 1;
5342 first = p - name + 1;
5343 memcpy (copy, name, first);
5344 memcpy (copy + first, name + first + 1, len - first);
5346 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5347 if (h == NULL)
5349 /* We also need to check references to the symbol without the
5350 version. */
5351 copy[first - 1] = '\0';
5352 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5353 FALSE, FALSE, TRUE);
5356 bfd_release (abfd, copy);
5357 return h;
5360 /* Add symbols from an ELF archive file to the linker hash table. We
5361 don't use _bfd_generic_link_add_archive_symbols because we need to
5362 handle versioned symbols.
5364 Fortunately, ELF archive handling is simpler than that done by
5365 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5366 oddities. In ELF, if we find a symbol in the archive map, and the
5367 symbol is currently undefined, we know that we must pull in that
5368 object file.
5370 Unfortunately, we do have to make multiple passes over the symbol
5371 table until nothing further is resolved. */
5373 static bfd_boolean
5374 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5376 symindex c;
5377 unsigned char *included = NULL;
5378 carsym *symdefs;
5379 bfd_boolean loop;
5380 bfd_size_type amt;
5381 const struct elf_backend_data *bed;
5382 struct elf_link_hash_entry * (*archive_symbol_lookup)
5383 (bfd *, struct bfd_link_info *, const char *);
5385 if (! bfd_has_map (abfd))
5387 /* An empty archive is a special case. */
5388 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5389 return TRUE;
5390 bfd_set_error (bfd_error_no_armap);
5391 return FALSE;
5394 /* Keep track of all symbols we know to be already defined, and all
5395 files we know to be already included. This is to speed up the
5396 second and subsequent passes. */
5397 c = bfd_ardata (abfd)->symdef_count;
5398 if (c == 0)
5399 return TRUE;
5400 amt = c;
5401 amt *= sizeof (*included);
5402 included = (unsigned char *) bfd_zmalloc (amt);
5403 if (included == NULL)
5404 return FALSE;
5406 symdefs = bfd_ardata (abfd)->symdefs;
5407 bed = get_elf_backend_data (abfd);
5408 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5412 file_ptr last;
5413 symindex i;
5414 carsym *symdef;
5415 carsym *symdefend;
5417 loop = FALSE;
5418 last = -1;
5420 symdef = symdefs;
5421 symdefend = symdef + c;
5422 for (i = 0; symdef < symdefend; symdef++, i++)
5424 struct elf_link_hash_entry *h;
5425 bfd *element;
5426 struct bfd_link_hash_entry *undefs_tail;
5427 symindex mark;
5429 if (included[i])
5430 continue;
5431 if (symdef->file_offset == last)
5433 included[i] = TRUE;
5434 continue;
5437 h = archive_symbol_lookup (abfd, info, symdef->name);
5438 if (h == (struct elf_link_hash_entry *) 0 - 1)
5439 goto error_return;
5441 if (h == NULL)
5442 continue;
5444 if (h->root.type == bfd_link_hash_common)
5446 /* We currently have a common symbol. The archive map contains
5447 a reference to this symbol, so we may want to include it. We
5448 only want to include it however, if this archive element
5449 contains a definition of the symbol, not just another common
5450 declaration of it.
5452 Unfortunately some archivers (including GNU ar) will put
5453 declarations of common symbols into their archive maps, as
5454 well as real definitions, so we cannot just go by the archive
5455 map alone. Instead we must read in the element's symbol
5456 table and check that to see what kind of symbol definition
5457 this is. */
5458 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5459 continue;
5461 else if (h->root.type != bfd_link_hash_undefined)
5463 if (h->root.type != bfd_link_hash_undefweak)
5464 /* Symbol must be defined. Don't check it again. */
5465 included[i] = TRUE;
5466 continue;
5469 /* We need to include this archive member. */
5470 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5471 if (element == NULL)
5472 goto error_return;
5474 if (! bfd_check_format (element, bfd_object))
5475 goto error_return;
5477 undefs_tail = info->hash->undefs_tail;
5479 if (!(*info->callbacks
5480 ->add_archive_element) (info, element, symdef->name, &element))
5481 continue;
5482 if (!bfd_link_add_symbols (element, info))
5483 goto error_return;
5485 /* If there are any new undefined symbols, we need to make
5486 another pass through the archive in order to see whether
5487 they can be defined. FIXME: This isn't perfect, because
5488 common symbols wind up on undefs_tail and because an
5489 undefined symbol which is defined later on in this pass
5490 does not require another pass. This isn't a bug, but it
5491 does make the code less efficient than it could be. */
5492 if (undefs_tail != info->hash->undefs_tail)
5493 loop = TRUE;
5495 /* Look backward to mark all symbols from this object file
5496 which we have already seen in this pass. */
5497 mark = i;
5500 included[mark] = TRUE;
5501 if (mark == 0)
5502 break;
5503 --mark;
5505 while (symdefs[mark].file_offset == symdef->file_offset);
5507 /* We mark subsequent symbols from this object file as we go
5508 on through the loop. */
5509 last = symdef->file_offset;
5512 while (loop);
5514 free (included);
5516 return TRUE;
5518 error_return:
5519 if (included != NULL)
5520 free (included);
5521 return FALSE;
5524 /* Given an ELF BFD, add symbols to the global hash table as
5525 appropriate. */
5527 bfd_boolean
5528 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5530 switch (bfd_get_format (abfd))
5532 case bfd_object:
5533 return elf_link_add_object_symbols (abfd, info);
5534 case bfd_archive:
5535 return elf_link_add_archive_symbols (abfd, info);
5536 default:
5537 bfd_set_error (bfd_error_wrong_format);
5538 return FALSE;
5542 struct hash_codes_info
5544 unsigned long *hashcodes;
5545 bfd_boolean error;
5548 /* This function will be called though elf_link_hash_traverse to store
5549 all hash value of the exported symbols in an array. */
5551 static bfd_boolean
5552 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5554 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5555 const char *name;
5556 unsigned long ha;
5557 char *alc = NULL;
5559 /* Ignore indirect symbols. These are added by the versioning code. */
5560 if (h->dynindx == -1)
5561 return TRUE;
5563 name = h->root.root.string;
5564 if (h->versioned >= versioned)
5566 char *p = strchr (name, ELF_VER_CHR);
5567 if (p != NULL)
5569 alc = (char *) bfd_malloc (p - name + 1);
5570 if (alc == NULL)
5572 inf->error = TRUE;
5573 return FALSE;
5575 memcpy (alc, name, p - name);
5576 alc[p - name] = '\0';
5577 name = alc;
5581 /* Compute the hash value. */
5582 ha = bfd_elf_hash (name);
5584 /* Store the found hash value in the array given as the argument. */
5585 *(inf->hashcodes)++ = ha;
5587 /* And store it in the struct so that we can put it in the hash table
5588 later. */
5589 h->u.elf_hash_value = ha;
5591 if (alc != NULL)
5592 free (alc);
5594 return TRUE;
5597 struct collect_gnu_hash_codes
5599 bfd *output_bfd;
5600 const struct elf_backend_data *bed;
5601 unsigned long int nsyms;
5602 unsigned long int maskbits;
5603 unsigned long int *hashcodes;
5604 unsigned long int *hashval;
5605 unsigned long int *indx;
5606 unsigned long int *counts;
5607 bfd_vma *bitmask;
5608 bfd_byte *contents;
5609 long int min_dynindx;
5610 unsigned long int bucketcount;
5611 unsigned long int symindx;
5612 long int local_indx;
5613 long int shift1, shift2;
5614 unsigned long int mask;
5615 bfd_boolean error;
5618 /* This function will be called though elf_link_hash_traverse to store
5619 all hash value of the exported symbols in an array. */
5621 static bfd_boolean
5622 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5624 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5625 const char *name;
5626 unsigned long ha;
5627 char *alc = NULL;
5629 /* Ignore indirect symbols. These are added by the versioning code. */
5630 if (h->dynindx == -1)
5631 return TRUE;
5633 /* Ignore also local symbols and undefined symbols. */
5634 if (! (*s->bed->elf_hash_symbol) (h))
5635 return TRUE;
5637 name = h->root.root.string;
5638 if (h->versioned >= versioned)
5640 char *p = strchr (name, ELF_VER_CHR);
5641 if (p != NULL)
5643 alc = (char *) bfd_malloc (p - name + 1);
5644 if (alc == NULL)
5646 s->error = TRUE;
5647 return FALSE;
5649 memcpy (alc, name, p - name);
5650 alc[p - name] = '\0';
5651 name = alc;
5655 /* Compute the hash value. */
5656 ha = bfd_elf_gnu_hash (name);
5658 /* Store the found hash value in the array for compute_bucket_count,
5659 and also for .dynsym reordering purposes. */
5660 s->hashcodes[s->nsyms] = ha;
5661 s->hashval[h->dynindx] = ha;
5662 ++s->nsyms;
5663 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5664 s->min_dynindx = h->dynindx;
5666 if (alc != NULL)
5667 free (alc);
5669 return TRUE;
5672 /* This function will be called though elf_link_hash_traverse to do
5673 final dynaminc symbol renumbering. */
5675 static bfd_boolean
5676 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5678 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5679 unsigned long int bucket;
5680 unsigned long int val;
5682 /* Ignore indirect symbols. */
5683 if (h->dynindx == -1)
5684 return TRUE;
5686 /* Ignore also local symbols and undefined symbols. */
5687 if (! (*s->bed->elf_hash_symbol) (h))
5689 if (h->dynindx >= s->min_dynindx)
5690 h->dynindx = s->local_indx++;
5691 return TRUE;
5694 bucket = s->hashval[h->dynindx] % s->bucketcount;
5695 val = (s->hashval[h->dynindx] >> s->shift1)
5696 & ((s->maskbits >> s->shift1) - 1);
5697 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5698 s->bitmask[val]
5699 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5700 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5701 if (s->counts[bucket] == 1)
5702 /* Last element terminates the chain. */
5703 val |= 1;
5704 bfd_put_32 (s->output_bfd, val,
5705 s->contents + (s->indx[bucket] - s->symindx) * 4);
5706 --s->counts[bucket];
5707 h->dynindx = s->indx[bucket]++;
5708 return TRUE;
5711 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5713 bfd_boolean
5714 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5716 return !(h->forced_local
5717 || h->root.type == bfd_link_hash_undefined
5718 || h->root.type == bfd_link_hash_undefweak
5719 || ((h->root.type == bfd_link_hash_defined
5720 || h->root.type == bfd_link_hash_defweak)
5721 && h->root.u.def.section->output_section == NULL));
5724 /* Array used to determine the number of hash table buckets to use
5725 based on the number of symbols there are. If there are fewer than
5726 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5727 fewer than 37 we use 17 buckets, and so forth. We never use more
5728 than 32771 buckets. */
5730 static const size_t elf_buckets[] =
5732 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5733 16411, 32771, 0
5736 /* Compute bucket count for hashing table. We do not use a static set
5737 of possible tables sizes anymore. Instead we determine for all
5738 possible reasonable sizes of the table the outcome (i.e., the
5739 number of collisions etc) and choose the best solution. The
5740 weighting functions are not too simple to allow the table to grow
5741 without bounds. Instead one of the weighting factors is the size.
5742 Therefore the result is always a good payoff between few collisions
5743 (= short chain lengths) and table size. */
5744 static size_t
5745 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5746 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5747 unsigned long int nsyms,
5748 int gnu_hash)
5750 size_t best_size = 0;
5751 unsigned long int i;
5753 /* We have a problem here. The following code to optimize the table
5754 size requires an integer type with more the 32 bits. If
5755 BFD_HOST_U_64_BIT is set we know about such a type. */
5756 #ifdef BFD_HOST_U_64_BIT
5757 if (info->optimize)
5759 size_t minsize;
5760 size_t maxsize;
5761 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5762 bfd *dynobj = elf_hash_table (info)->dynobj;
5763 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5764 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5765 unsigned long int *counts;
5766 bfd_size_type amt;
5767 unsigned int no_improvement_count = 0;
5769 /* Possible optimization parameters: if we have NSYMS symbols we say
5770 that the hashing table must at least have NSYMS/4 and at most
5771 2*NSYMS buckets. */
5772 minsize = nsyms / 4;
5773 if (minsize == 0)
5774 minsize = 1;
5775 best_size = maxsize = nsyms * 2;
5776 if (gnu_hash)
5778 if (minsize < 2)
5779 minsize = 2;
5780 if ((best_size & 31) == 0)
5781 ++best_size;
5784 /* Create array where we count the collisions in. We must use bfd_malloc
5785 since the size could be large. */
5786 amt = maxsize;
5787 amt *= sizeof (unsigned long int);
5788 counts = (unsigned long int *) bfd_malloc (amt);
5789 if (counts == NULL)
5790 return 0;
5792 /* Compute the "optimal" size for the hash table. The criteria is a
5793 minimal chain length. The minor criteria is (of course) the size
5794 of the table. */
5795 for (i = minsize; i < maxsize; ++i)
5797 /* Walk through the array of hashcodes and count the collisions. */
5798 BFD_HOST_U_64_BIT max;
5799 unsigned long int j;
5800 unsigned long int fact;
5802 if (gnu_hash && (i & 31) == 0)
5803 continue;
5805 memset (counts, '\0', i * sizeof (unsigned long int));
5807 /* Determine how often each hash bucket is used. */
5808 for (j = 0; j < nsyms; ++j)
5809 ++counts[hashcodes[j] % i];
5811 /* For the weight function we need some information about the
5812 pagesize on the target. This is information need not be 100%
5813 accurate. Since this information is not available (so far) we
5814 define it here to a reasonable default value. If it is crucial
5815 to have a better value some day simply define this value. */
5816 # ifndef BFD_TARGET_PAGESIZE
5817 # define BFD_TARGET_PAGESIZE (4096)
5818 # endif
5820 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5821 and the chains. */
5822 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5824 # if 1
5825 /* Variant 1: optimize for short chains. We add the squares
5826 of all the chain lengths (which favors many small chain
5827 over a few long chains). */
5828 for (j = 0; j < i; ++j)
5829 max += counts[j] * counts[j];
5831 /* This adds penalties for the overall size of the table. */
5832 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5833 max *= fact * fact;
5834 # else
5835 /* Variant 2: Optimize a lot more for small table. Here we
5836 also add squares of the size but we also add penalties for
5837 empty slots (the +1 term). */
5838 for (j = 0; j < i; ++j)
5839 max += (1 + counts[j]) * (1 + counts[j]);
5841 /* The overall size of the table is considered, but not as
5842 strong as in variant 1, where it is squared. */
5843 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5844 max *= fact;
5845 # endif
5847 /* Compare with current best results. */
5848 if (max < best_chlen)
5850 best_chlen = max;
5851 best_size = i;
5852 no_improvement_count = 0;
5854 /* PR 11843: Avoid futile long searches for the best bucket size
5855 when there are a large number of symbols. */
5856 else if (++no_improvement_count == 100)
5857 break;
5860 free (counts);
5862 else
5863 #endif /* defined (BFD_HOST_U_64_BIT) */
5865 /* This is the fallback solution if no 64bit type is available or if we
5866 are not supposed to spend much time on optimizations. We select the
5867 bucket count using a fixed set of numbers. */
5868 for (i = 0; elf_buckets[i] != 0; i++)
5870 best_size = elf_buckets[i];
5871 if (nsyms < elf_buckets[i + 1])
5872 break;
5874 if (gnu_hash && best_size < 2)
5875 best_size = 2;
5878 return best_size;
5881 /* Size any SHT_GROUP section for ld -r. */
5883 bfd_boolean
5884 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5886 bfd *ibfd;
5887 asection *s;
5889 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5890 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5891 && (s = ibfd->sections) != NULL
5892 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5893 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5894 return FALSE;
5895 return TRUE;
5898 /* Set a default stack segment size. The value in INFO wins. If it
5899 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5900 undefined it is initialized. */
5902 bfd_boolean
5903 bfd_elf_stack_segment_size (bfd *output_bfd,
5904 struct bfd_link_info *info,
5905 const char *legacy_symbol,
5906 bfd_vma default_size)
5908 struct elf_link_hash_entry *h = NULL;
5910 /* Look for legacy symbol. */
5911 if (legacy_symbol)
5912 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5913 FALSE, FALSE, FALSE);
5914 if (h && (h->root.type == bfd_link_hash_defined
5915 || h->root.type == bfd_link_hash_defweak)
5916 && h->def_regular
5917 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5919 /* The symbol has no type if specified on the command line. */
5920 h->type = STT_OBJECT;
5921 if (info->stacksize)
5922 /* xgettext:c-format */
5923 _bfd_error_handler (_("%B: stack size specified and %s set"),
5924 output_bfd, legacy_symbol);
5925 else if (h->root.u.def.section != bfd_abs_section_ptr)
5926 /* xgettext:c-format */
5927 _bfd_error_handler (_("%B: %s not absolute"),
5928 output_bfd, legacy_symbol);
5929 else
5930 info->stacksize = h->root.u.def.value;
5933 if (!info->stacksize)
5934 /* If the user didn't set a size, or explicitly inhibit the
5935 size, set it now. */
5936 info->stacksize = default_size;
5938 /* Provide the legacy symbol, if it is referenced. */
5939 if (h && (h->root.type == bfd_link_hash_undefined
5940 || h->root.type == bfd_link_hash_undefweak))
5942 struct bfd_link_hash_entry *bh = NULL;
5944 if (!(_bfd_generic_link_add_one_symbol
5945 (info, output_bfd, legacy_symbol,
5946 BSF_GLOBAL, bfd_abs_section_ptr,
5947 info->stacksize >= 0 ? info->stacksize : 0,
5948 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5949 return FALSE;
5951 h = (struct elf_link_hash_entry *) bh;
5952 h->def_regular = 1;
5953 h->type = STT_OBJECT;
5956 return TRUE;
5959 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5961 struct elf_gc_sweep_symbol_info
5963 struct bfd_link_info *info;
5964 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5965 bfd_boolean);
5968 static bfd_boolean
5969 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5971 if (!h->mark
5972 && (((h->root.type == bfd_link_hash_defined
5973 || h->root.type == bfd_link_hash_defweak)
5974 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5975 && h->root.u.def.section->gc_mark))
5976 || h->root.type == bfd_link_hash_undefined
5977 || h->root.type == bfd_link_hash_undefweak))
5979 struct elf_gc_sweep_symbol_info *inf;
5981 inf = (struct elf_gc_sweep_symbol_info *) data;
5982 (*inf->hide_symbol) (inf->info, h, TRUE);
5983 h->def_regular = 0;
5984 h->ref_regular = 0;
5985 h->ref_regular_nonweak = 0;
5988 return TRUE;
5991 /* Set up the sizes and contents of the ELF dynamic sections. This is
5992 called by the ELF linker emulation before_allocation routine. We
5993 must set the sizes of the sections before the linker sets the
5994 addresses of the various sections. */
5996 bfd_boolean
5997 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5998 const char *soname,
5999 const char *rpath,
6000 const char *filter_shlib,
6001 const char *audit,
6002 const char *depaudit,
6003 const char * const *auxiliary_filters,
6004 struct bfd_link_info *info,
6005 asection **sinterpptr)
6007 bfd *dynobj;
6008 const struct elf_backend_data *bed;
6010 *sinterpptr = NULL;
6012 if (!is_elf_hash_table (info->hash))
6013 return TRUE;
6015 dynobj = elf_hash_table (info)->dynobj;
6017 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6019 struct bfd_elf_version_tree *verdefs;
6020 struct elf_info_failed asvinfo;
6021 struct bfd_elf_version_tree *t;
6022 struct bfd_elf_version_expr *d;
6023 asection *s;
6024 size_t soname_indx;
6026 /* If we are supposed to export all symbols into the dynamic symbol
6027 table (this is not the normal case), then do so. */
6028 if (info->export_dynamic
6029 || (bfd_link_executable (info) && info->dynamic))
6031 struct elf_info_failed eif;
6033 eif.info = info;
6034 eif.failed = FALSE;
6035 elf_link_hash_traverse (elf_hash_table (info),
6036 _bfd_elf_export_symbol,
6037 &eif);
6038 if (eif.failed)
6039 return FALSE;
6042 if (soname != NULL)
6044 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6045 soname, TRUE);
6046 if (soname_indx == (size_t) -1
6047 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6048 return FALSE;
6050 else
6051 soname_indx = (size_t) -1;
6053 /* Make all global versions with definition. */
6054 for (t = info->version_info; t != NULL; t = t->next)
6055 for (d = t->globals.list; d != NULL; d = d->next)
6056 if (!d->symver && d->literal)
6058 const char *verstr, *name;
6059 size_t namelen, verlen, newlen;
6060 char *newname, *p, leading_char;
6061 struct elf_link_hash_entry *newh;
6063 leading_char = bfd_get_symbol_leading_char (output_bfd);
6064 name = d->pattern;
6065 namelen = strlen (name) + (leading_char != '\0');
6066 verstr = t->name;
6067 verlen = strlen (verstr);
6068 newlen = namelen + verlen + 3;
6070 newname = (char *) bfd_malloc (newlen);
6071 if (newname == NULL)
6072 return FALSE;
6073 newname[0] = leading_char;
6074 memcpy (newname + (leading_char != '\0'), name, namelen);
6076 /* Check the hidden versioned definition. */
6077 p = newname + namelen;
6078 *p++ = ELF_VER_CHR;
6079 memcpy (p, verstr, verlen + 1);
6080 newh = elf_link_hash_lookup (elf_hash_table (info),
6081 newname, FALSE, FALSE,
6082 FALSE);
6083 if (newh == NULL
6084 || (newh->root.type != bfd_link_hash_defined
6085 && newh->root.type != bfd_link_hash_defweak))
6087 /* Check the default versioned definition. */
6088 *p++ = ELF_VER_CHR;
6089 memcpy (p, verstr, verlen + 1);
6090 newh = elf_link_hash_lookup (elf_hash_table (info),
6091 newname, FALSE, FALSE,
6092 FALSE);
6094 free (newname);
6096 /* Mark this version if there is a definition and it is
6097 not defined in a shared object. */
6098 if (newh != NULL
6099 && !newh->def_dynamic
6100 && (newh->root.type == bfd_link_hash_defined
6101 || newh->root.type == bfd_link_hash_defweak))
6102 d->symver = 1;
6105 /* Attach all the symbols to their version information. */
6106 asvinfo.info = info;
6107 asvinfo.failed = FALSE;
6109 elf_link_hash_traverse (elf_hash_table (info),
6110 _bfd_elf_link_assign_sym_version,
6111 &asvinfo);
6112 if (asvinfo.failed)
6113 return FALSE;
6115 if (!info->allow_undefined_version)
6117 /* Check if all global versions have a definition. */
6118 bfd_boolean all_defined = TRUE;
6119 for (t = info->version_info; t != NULL; t = t->next)
6120 for (d = t->globals.list; d != NULL; d = d->next)
6121 if (d->literal && !d->symver && !d->script)
6123 _bfd_error_handler
6124 (_("%s: undefined version: %s"),
6125 d->pattern, t->name);
6126 all_defined = FALSE;
6129 if (!all_defined)
6131 bfd_set_error (bfd_error_bad_value);
6132 return FALSE;
6136 /* Set up the version definition section. */
6137 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6138 BFD_ASSERT (s != NULL);
6140 /* We may have created additional version definitions if we are
6141 just linking a regular application. */
6142 verdefs = info->version_info;
6144 /* Skip anonymous version tag. */
6145 if (verdefs != NULL && verdefs->vernum == 0)
6146 verdefs = verdefs->next;
6148 if (verdefs == NULL && !info->create_default_symver)
6149 s->flags |= SEC_EXCLUDE;
6150 else
6152 unsigned int cdefs;
6153 bfd_size_type size;
6154 bfd_byte *p;
6155 Elf_Internal_Verdef def;
6156 Elf_Internal_Verdaux defaux;
6157 struct bfd_link_hash_entry *bh;
6158 struct elf_link_hash_entry *h;
6159 const char *name;
6161 cdefs = 0;
6162 size = 0;
6164 /* Make space for the base version. */
6165 size += sizeof (Elf_External_Verdef);
6166 size += sizeof (Elf_External_Verdaux);
6167 ++cdefs;
6169 /* Make space for the default version. */
6170 if (info->create_default_symver)
6172 size += sizeof (Elf_External_Verdef);
6173 ++cdefs;
6176 for (t = verdefs; t != NULL; t = t->next)
6178 struct bfd_elf_version_deps *n;
6180 /* Don't emit base version twice. */
6181 if (t->vernum == 0)
6182 continue;
6184 size += sizeof (Elf_External_Verdef);
6185 size += sizeof (Elf_External_Verdaux);
6186 ++cdefs;
6188 for (n = t->deps; n != NULL; n = n->next)
6189 size += sizeof (Elf_External_Verdaux);
6192 s->size = size;
6193 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6194 if (s->contents == NULL && s->size != 0)
6195 return FALSE;
6197 /* Fill in the version definition section. */
6199 p = s->contents;
6201 def.vd_version = VER_DEF_CURRENT;
6202 def.vd_flags = VER_FLG_BASE;
6203 def.vd_ndx = 1;
6204 def.vd_cnt = 1;
6205 if (info->create_default_symver)
6207 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6208 def.vd_next = sizeof (Elf_External_Verdef);
6210 else
6212 def.vd_aux = sizeof (Elf_External_Verdef);
6213 def.vd_next = (sizeof (Elf_External_Verdef)
6214 + sizeof (Elf_External_Verdaux));
6217 if (soname_indx != (size_t) -1)
6219 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6220 soname_indx);
6221 def.vd_hash = bfd_elf_hash (soname);
6222 defaux.vda_name = soname_indx;
6223 name = soname;
6225 else
6227 size_t indx;
6229 name = lbasename (output_bfd->filename);
6230 def.vd_hash = bfd_elf_hash (name);
6231 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6232 name, FALSE);
6233 if (indx == (size_t) -1)
6234 return FALSE;
6235 defaux.vda_name = indx;
6237 defaux.vda_next = 0;
6239 _bfd_elf_swap_verdef_out (output_bfd, &def,
6240 (Elf_External_Verdef *) p);
6241 p += sizeof (Elf_External_Verdef);
6242 if (info->create_default_symver)
6244 /* Add a symbol representing this version. */
6245 bh = NULL;
6246 if (! (_bfd_generic_link_add_one_symbol
6247 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6248 0, NULL, FALSE,
6249 get_elf_backend_data (dynobj)->collect, &bh)))
6250 return FALSE;
6251 h = (struct elf_link_hash_entry *) bh;
6252 h->non_elf = 0;
6253 h->def_regular = 1;
6254 h->type = STT_OBJECT;
6255 h->verinfo.vertree = NULL;
6257 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6258 return FALSE;
6260 /* Create a duplicate of the base version with the same
6261 aux block, but different flags. */
6262 def.vd_flags = 0;
6263 def.vd_ndx = 2;
6264 def.vd_aux = sizeof (Elf_External_Verdef);
6265 if (verdefs)
6266 def.vd_next = (sizeof (Elf_External_Verdef)
6267 + sizeof (Elf_External_Verdaux));
6268 else
6269 def.vd_next = 0;
6270 _bfd_elf_swap_verdef_out (output_bfd, &def,
6271 (Elf_External_Verdef *) p);
6272 p += sizeof (Elf_External_Verdef);
6274 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6275 (Elf_External_Verdaux *) p);
6276 p += sizeof (Elf_External_Verdaux);
6278 for (t = verdefs; t != NULL; t = t->next)
6280 unsigned int cdeps;
6281 struct bfd_elf_version_deps *n;
6283 /* Don't emit the base version twice. */
6284 if (t->vernum == 0)
6285 continue;
6287 cdeps = 0;
6288 for (n = t->deps; n != NULL; n = n->next)
6289 ++cdeps;
6291 /* Add a symbol representing this version. */
6292 bh = NULL;
6293 if (! (_bfd_generic_link_add_one_symbol
6294 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6295 0, NULL, FALSE,
6296 get_elf_backend_data (dynobj)->collect, &bh)))
6297 return FALSE;
6298 h = (struct elf_link_hash_entry *) bh;
6299 h->non_elf = 0;
6300 h->def_regular = 1;
6301 h->type = STT_OBJECT;
6302 h->verinfo.vertree = t;
6304 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6305 return FALSE;
6307 def.vd_version = VER_DEF_CURRENT;
6308 def.vd_flags = 0;
6309 if (t->globals.list == NULL
6310 && t->locals.list == NULL
6311 && ! t->used)
6312 def.vd_flags |= VER_FLG_WEAK;
6313 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6314 def.vd_cnt = cdeps + 1;
6315 def.vd_hash = bfd_elf_hash (t->name);
6316 def.vd_aux = sizeof (Elf_External_Verdef);
6317 def.vd_next = 0;
6319 /* If a basever node is next, it *must* be the last node in
6320 the chain, otherwise Verdef construction breaks. */
6321 if (t->next != NULL && t->next->vernum == 0)
6322 BFD_ASSERT (t->next->next == NULL);
6324 if (t->next != NULL && t->next->vernum != 0)
6325 def.vd_next = (sizeof (Elf_External_Verdef)
6326 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6328 _bfd_elf_swap_verdef_out (output_bfd, &def,
6329 (Elf_External_Verdef *) p);
6330 p += sizeof (Elf_External_Verdef);
6332 defaux.vda_name = h->dynstr_index;
6333 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6334 h->dynstr_index);
6335 defaux.vda_next = 0;
6336 if (t->deps != NULL)
6337 defaux.vda_next = sizeof (Elf_External_Verdaux);
6338 t->name_indx = defaux.vda_name;
6340 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6341 (Elf_External_Verdaux *) p);
6342 p += sizeof (Elf_External_Verdaux);
6344 for (n = t->deps; n != NULL; n = n->next)
6346 if (n->version_needed == NULL)
6348 /* This can happen if there was an error in the
6349 version script. */
6350 defaux.vda_name = 0;
6352 else
6354 defaux.vda_name = n->version_needed->name_indx;
6355 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6356 defaux.vda_name);
6358 if (n->next == NULL)
6359 defaux.vda_next = 0;
6360 else
6361 defaux.vda_next = sizeof (Elf_External_Verdaux);
6363 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6364 (Elf_External_Verdaux *) p);
6365 p += sizeof (Elf_External_Verdaux);
6369 elf_tdata (output_bfd)->cverdefs = cdefs;
6373 bed = get_elf_backend_data (output_bfd);
6375 if (info->gc_sections && bed->can_gc_sections)
6377 struct elf_gc_sweep_symbol_info sweep_info;
6379 /* Remove the symbols that were in the swept sections from the
6380 dynamic symbol table. */
6381 sweep_info.info = info;
6382 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6383 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6384 &sweep_info);
6387 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6389 asection *s;
6390 struct elf_find_verdep_info sinfo;
6392 /* Work out the size of the version reference section. */
6394 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6395 BFD_ASSERT (s != NULL);
6397 sinfo.info = info;
6398 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6399 if (sinfo.vers == 0)
6400 sinfo.vers = 1;
6401 sinfo.failed = FALSE;
6403 elf_link_hash_traverse (elf_hash_table (info),
6404 _bfd_elf_link_find_version_dependencies,
6405 &sinfo);
6406 if (sinfo.failed)
6407 return FALSE;
6409 if (elf_tdata (output_bfd)->verref == NULL)
6410 s->flags |= SEC_EXCLUDE;
6411 else
6413 Elf_Internal_Verneed *vn;
6414 unsigned int size;
6415 unsigned int crefs;
6416 bfd_byte *p;
6418 /* Build the version dependency section. */
6419 size = 0;
6420 crefs = 0;
6421 for (vn = elf_tdata (output_bfd)->verref;
6422 vn != NULL;
6423 vn = vn->vn_nextref)
6425 Elf_Internal_Vernaux *a;
6427 size += sizeof (Elf_External_Verneed);
6428 ++crefs;
6429 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6430 size += sizeof (Elf_External_Vernaux);
6433 s->size = size;
6434 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6435 if (s->contents == NULL)
6436 return FALSE;
6438 p = s->contents;
6439 for (vn = elf_tdata (output_bfd)->verref;
6440 vn != NULL;
6441 vn = vn->vn_nextref)
6443 unsigned int caux;
6444 Elf_Internal_Vernaux *a;
6445 size_t indx;
6447 caux = 0;
6448 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6449 ++caux;
6451 vn->vn_version = VER_NEED_CURRENT;
6452 vn->vn_cnt = caux;
6453 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6454 elf_dt_name (vn->vn_bfd) != NULL
6455 ? elf_dt_name (vn->vn_bfd)
6456 : lbasename (vn->vn_bfd->filename),
6457 FALSE);
6458 if (indx == (size_t) -1)
6459 return FALSE;
6460 vn->vn_file = indx;
6461 vn->vn_aux = sizeof (Elf_External_Verneed);
6462 if (vn->vn_nextref == NULL)
6463 vn->vn_next = 0;
6464 else
6465 vn->vn_next = (sizeof (Elf_External_Verneed)
6466 + caux * sizeof (Elf_External_Vernaux));
6468 _bfd_elf_swap_verneed_out (output_bfd, vn,
6469 (Elf_External_Verneed *) p);
6470 p += sizeof (Elf_External_Verneed);
6472 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6474 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6475 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6476 a->vna_nodename, FALSE);
6477 if (indx == (size_t) -1)
6478 return FALSE;
6479 a->vna_name = indx;
6480 if (a->vna_nextptr == NULL)
6481 a->vna_next = 0;
6482 else
6483 a->vna_next = sizeof (Elf_External_Vernaux);
6485 _bfd_elf_swap_vernaux_out (output_bfd, a,
6486 (Elf_External_Vernaux *) p);
6487 p += sizeof (Elf_External_Vernaux);
6491 elf_tdata (output_bfd)->cverrefs = crefs;
6495 /* Any syms created from now on start with -1 in
6496 got.refcount/offset and plt.refcount/offset. */
6497 elf_hash_table (info)->init_got_refcount
6498 = elf_hash_table (info)->init_got_offset;
6499 elf_hash_table (info)->init_plt_refcount
6500 = elf_hash_table (info)->init_plt_offset;
6502 if (bfd_link_relocatable (info)
6503 && !_bfd_elf_size_group_sections (info))
6504 return FALSE;
6506 /* The backend may have to create some sections regardless of whether
6507 we're dynamic or not. */
6508 if (bed->elf_backend_always_size_sections
6509 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6510 return FALSE;
6512 /* Determine any GNU_STACK segment requirements, after the backend
6513 has had a chance to set a default segment size. */
6514 if (info->execstack)
6515 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6516 else if (info->noexecstack)
6517 elf_stack_flags (output_bfd) = PF_R | PF_W;
6518 else
6520 bfd *inputobj;
6521 asection *notesec = NULL;
6522 int exec = 0;
6524 for (inputobj = info->input_bfds;
6525 inputobj;
6526 inputobj = inputobj->link.next)
6528 asection *s;
6530 if (inputobj->flags
6531 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6532 continue;
6533 s = inputobj->sections;
6534 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6535 continue;
6537 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6538 if (s)
6540 if (s->flags & SEC_CODE)
6541 exec = PF_X;
6542 notesec = s;
6544 else if (bed->default_execstack)
6545 exec = PF_X;
6547 if (notesec || info->stacksize > 0)
6548 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6549 if (notesec && exec && bfd_link_relocatable (info)
6550 && notesec->output_section != bfd_abs_section_ptr)
6551 notesec->output_section->flags |= SEC_CODE;
6554 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6556 struct elf_info_failed eif;
6557 struct elf_link_hash_entry *h;
6558 asection *dynstr;
6559 asection *s;
6561 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6562 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6564 if (info->symbolic)
6566 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6567 return FALSE;
6568 info->flags |= DF_SYMBOLIC;
6571 if (rpath != NULL)
6573 size_t indx;
6574 bfd_vma tag;
6576 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6577 TRUE);
6578 if (indx == (size_t) -1)
6579 return FALSE;
6581 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6582 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6583 return FALSE;
6586 if (filter_shlib != NULL)
6588 size_t indx;
6590 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6591 filter_shlib, TRUE);
6592 if (indx == (size_t) -1
6593 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6594 return FALSE;
6597 if (auxiliary_filters != NULL)
6599 const char * const *p;
6601 for (p = auxiliary_filters; *p != NULL; p++)
6603 size_t indx;
6605 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6606 *p, TRUE);
6607 if (indx == (size_t) -1
6608 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6609 return FALSE;
6613 if (audit != NULL)
6615 size_t indx;
6617 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6618 TRUE);
6619 if (indx == (size_t) -1
6620 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6621 return FALSE;
6624 if (depaudit != NULL)
6626 size_t indx;
6628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6629 TRUE);
6630 if (indx == (size_t) -1
6631 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6632 return FALSE;
6635 eif.info = info;
6636 eif.failed = FALSE;
6638 /* Find all symbols which were defined in a dynamic object and make
6639 the backend pick a reasonable value for them. */
6640 elf_link_hash_traverse (elf_hash_table (info),
6641 _bfd_elf_adjust_dynamic_symbol,
6642 &eif);
6643 if (eif.failed)
6644 return FALSE;
6646 /* Add some entries to the .dynamic section. We fill in some of the
6647 values later, in bfd_elf_final_link, but we must add the entries
6648 now so that we know the final size of the .dynamic section. */
6650 /* If there are initialization and/or finalization functions to
6651 call then add the corresponding DT_INIT/DT_FINI entries. */
6652 h = (info->init_function
6653 ? elf_link_hash_lookup (elf_hash_table (info),
6654 info->init_function, FALSE,
6655 FALSE, FALSE)
6656 : NULL);
6657 if (h != NULL
6658 && (h->ref_regular
6659 || h->def_regular))
6661 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6662 return FALSE;
6664 h = (info->fini_function
6665 ? elf_link_hash_lookup (elf_hash_table (info),
6666 info->fini_function, FALSE,
6667 FALSE, FALSE)
6668 : NULL);
6669 if (h != NULL
6670 && (h->ref_regular
6671 || h->def_regular))
6673 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6674 return FALSE;
6677 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6678 if (s != NULL && s->linker_has_input)
6680 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6681 if (! bfd_link_executable (info))
6683 bfd *sub;
6684 asection *o;
6686 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6687 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6688 && (o = sub->sections) != NULL
6689 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6690 for (o = sub->sections; o != NULL; o = o->next)
6691 if (elf_section_data (o)->this_hdr.sh_type
6692 == SHT_PREINIT_ARRAY)
6694 _bfd_error_handler
6695 (_("%B: .preinit_array section is not allowed in DSO"),
6696 sub);
6697 break;
6700 bfd_set_error (bfd_error_nonrepresentable_section);
6701 return FALSE;
6704 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6705 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6706 return FALSE;
6708 s = bfd_get_section_by_name (output_bfd, ".init_array");
6709 if (s != NULL && s->linker_has_input)
6711 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6712 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6713 return FALSE;
6715 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6716 if (s != NULL && s->linker_has_input)
6718 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6719 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6720 return FALSE;
6723 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6724 /* If .dynstr is excluded from the link, we don't want any of
6725 these tags. Strictly, we should be checking each section
6726 individually; This quick check covers for the case where
6727 someone does a /DISCARD/ : { *(*) }. */
6728 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6730 bfd_size_type strsize;
6732 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6733 if ((info->emit_hash
6734 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6735 || (info->emit_gnu_hash
6736 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6737 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6738 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6739 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6740 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6741 bed->s->sizeof_sym))
6742 return FALSE;
6746 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6747 return FALSE;
6749 /* The backend must work out the sizes of all the other dynamic
6750 sections. */
6751 if (dynobj != NULL
6752 && bed->elf_backend_size_dynamic_sections != NULL
6753 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6754 return FALSE;
6756 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6758 unsigned long section_sym_count;
6760 if (elf_tdata (output_bfd)->cverdefs)
6762 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6764 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6765 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6766 return FALSE;
6769 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6771 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6772 return FALSE;
6774 else if (info->flags & DF_BIND_NOW)
6776 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6777 return FALSE;
6780 if (info->flags_1)
6782 if (bfd_link_executable (info))
6783 info->flags_1 &= ~ (DF_1_INITFIRST
6784 | DF_1_NODELETE
6785 | DF_1_NOOPEN);
6786 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6787 return FALSE;
6790 if (elf_tdata (output_bfd)->cverrefs)
6792 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6794 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6795 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6796 return FALSE;
6799 if ((elf_tdata (output_bfd)->cverrefs == 0
6800 && elf_tdata (output_bfd)->cverdefs == 0)
6801 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6802 &section_sym_count) <= 1)
6804 asection *s;
6806 s = bfd_get_linker_section (dynobj, ".gnu.version");
6807 s->flags |= SEC_EXCLUDE;
6810 return TRUE;
6813 /* Find the first non-excluded output section. We'll use its
6814 section symbol for some emitted relocs. */
6815 void
6816 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6818 asection *s;
6820 for (s = output_bfd->sections; s != NULL; s = s->next)
6821 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6822 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6824 elf_hash_table (info)->text_index_section = s;
6825 break;
6829 /* Find two non-excluded output sections, one for code, one for data.
6830 We'll use their section symbols for some emitted relocs. */
6831 void
6832 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6834 asection *s;
6836 /* Data first, since setting text_index_section changes
6837 _bfd_elf_link_omit_section_dynsym. */
6838 for (s = output_bfd->sections; s != NULL; s = s->next)
6839 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6840 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6842 elf_hash_table (info)->data_index_section = s;
6843 break;
6846 for (s = output_bfd->sections; s != NULL; s = s->next)
6847 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6848 == (SEC_ALLOC | SEC_READONLY))
6849 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6851 elf_hash_table (info)->text_index_section = s;
6852 break;
6855 if (elf_hash_table (info)->text_index_section == NULL)
6856 elf_hash_table (info)->text_index_section
6857 = elf_hash_table (info)->data_index_section;
6860 bfd_boolean
6861 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6863 const struct elf_backend_data *bed;
6864 unsigned long section_sym_count;
6865 bfd_size_type dynsymcount = 0;
6867 if (!is_elf_hash_table (info->hash))
6868 return TRUE;
6870 bed = get_elf_backend_data (output_bfd);
6871 (*bed->elf_backend_init_index_section) (output_bfd, info);
6873 /* Assign dynsym indices. In a shared library we generate a section
6874 symbol for each output section, which come first. Next come all
6875 of the back-end allocated local dynamic syms, followed by the rest
6876 of the global symbols.
6878 This is usually not needed for static binaries, however backends
6879 can request to always do it, e.g. the MIPS backend uses dynamic
6880 symbol counts to lay out GOT, which will be produced in the
6881 presence of GOT relocations even in static binaries (holding fixed
6882 data in that case, to satisfy those relocations). */
6884 if (elf_hash_table (info)->dynamic_sections_created
6885 || bed->always_renumber_dynsyms)
6886 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6887 &section_sym_count);
6889 if (elf_hash_table (info)->dynamic_sections_created)
6891 bfd *dynobj;
6892 asection *s;
6893 unsigned int dtagcount;
6895 dynobj = elf_hash_table (info)->dynobj;
6897 /* Work out the size of the symbol version section. */
6898 s = bfd_get_linker_section (dynobj, ".gnu.version");
6899 BFD_ASSERT (s != NULL);
6900 if ((s->flags & SEC_EXCLUDE) == 0)
6902 s->size = dynsymcount * sizeof (Elf_External_Versym);
6903 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6904 if (s->contents == NULL)
6905 return FALSE;
6907 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6908 return FALSE;
6911 /* Set the size of the .dynsym and .hash sections. We counted
6912 the number of dynamic symbols in elf_link_add_object_symbols.
6913 We will build the contents of .dynsym and .hash when we build
6914 the final symbol table, because until then we do not know the
6915 correct value to give the symbols. We built the .dynstr
6916 section as we went along in elf_link_add_object_symbols. */
6917 s = elf_hash_table (info)->dynsym;
6918 BFD_ASSERT (s != NULL);
6919 s->size = dynsymcount * bed->s->sizeof_sym;
6921 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6922 if (s->contents == NULL)
6923 return FALSE;
6925 /* The first entry in .dynsym is a dummy symbol. Clear all the
6926 section syms, in case we don't output them all. */
6927 ++section_sym_count;
6928 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6930 elf_hash_table (info)->bucketcount = 0;
6932 /* Compute the size of the hashing table. As a side effect this
6933 computes the hash values for all the names we export. */
6934 if (info->emit_hash)
6936 unsigned long int *hashcodes;
6937 struct hash_codes_info hashinf;
6938 bfd_size_type amt;
6939 unsigned long int nsyms;
6940 size_t bucketcount;
6941 size_t hash_entry_size;
6943 /* Compute the hash values for all exported symbols. At the same
6944 time store the values in an array so that we could use them for
6945 optimizations. */
6946 amt = dynsymcount * sizeof (unsigned long int);
6947 hashcodes = (unsigned long int *) bfd_malloc (amt);
6948 if (hashcodes == NULL)
6949 return FALSE;
6950 hashinf.hashcodes = hashcodes;
6951 hashinf.error = FALSE;
6953 /* Put all hash values in HASHCODES. */
6954 elf_link_hash_traverse (elf_hash_table (info),
6955 elf_collect_hash_codes, &hashinf);
6956 if (hashinf.error)
6958 free (hashcodes);
6959 return FALSE;
6962 nsyms = hashinf.hashcodes - hashcodes;
6963 bucketcount
6964 = compute_bucket_count (info, hashcodes, nsyms, 0);
6965 free (hashcodes);
6967 if (bucketcount == 0 && nsyms > 0)
6968 return FALSE;
6970 elf_hash_table (info)->bucketcount = bucketcount;
6972 s = bfd_get_linker_section (dynobj, ".hash");
6973 BFD_ASSERT (s != NULL);
6974 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6975 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6976 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6977 if (s->contents == NULL)
6978 return FALSE;
6980 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6981 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6982 s->contents + hash_entry_size);
6985 if (info->emit_gnu_hash)
6987 size_t i, cnt;
6988 unsigned char *contents;
6989 struct collect_gnu_hash_codes cinfo;
6990 bfd_size_type amt;
6991 size_t bucketcount;
6993 memset (&cinfo, 0, sizeof (cinfo));
6995 /* Compute the hash values for all exported symbols. At the same
6996 time store the values in an array so that we could use them for
6997 optimizations. */
6998 amt = dynsymcount * 2 * sizeof (unsigned long int);
6999 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7000 if (cinfo.hashcodes == NULL)
7001 return FALSE;
7003 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7004 cinfo.min_dynindx = -1;
7005 cinfo.output_bfd = output_bfd;
7006 cinfo.bed = bed;
7008 /* Put all hash values in HASHCODES. */
7009 elf_link_hash_traverse (elf_hash_table (info),
7010 elf_collect_gnu_hash_codes, &cinfo);
7011 if (cinfo.error)
7013 free (cinfo.hashcodes);
7014 return FALSE;
7017 bucketcount
7018 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7020 if (bucketcount == 0)
7022 free (cinfo.hashcodes);
7023 return FALSE;
7026 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7027 BFD_ASSERT (s != NULL);
7029 if (cinfo.nsyms == 0)
7031 /* Empty .gnu.hash section is special. */
7032 BFD_ASSERT (cinfo.min_dynindx == -1);
7033 free (cinfo.hashcodes);
7034 s->size = 5 * 4 + bed->s->arch_size / 8;
7035 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7036 if (contents == NULL)
7037 return FALSE;
7038 s->contents = contents;
7039 /* 1 empty bucket. */
7040 bfd_put_32 (output_bfd, 1, contents);
7041 /* SYMIDX above the special symbol 0. */
7042 bfd_put_32 (output_bfd, 1, contents + 4);
7043 /* Just one word for bitmask. */
7044 bfd_put_32 (output_bfd, 1, contents + 8);
7045 /* Only hash fn bloom filter. */
7046 bfd_put_32 (output_bfd, 0, contents + 12);
7047 /* No hashes are valid - empty bitmask. */
7048 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7049 /* No hashes in the only bucket. */
7050 bfd_put_32 (output_bfd, 0,
7051 contents + 16 + bed->s->arch_size / 8);
7053 else
7055 unsigned long int maskwords, maskbitslog2, x;
7056 BFD_ASSERT (cinfo.min_dynindx != -1);
7058 x = cinfo.nsyms;
7059 maskbitslog2 = 1;
7060 while ((x >>= 1) != 0)
7061 ++maskbitslog2;
7062 if (maskbitslog2 < 3)
7063 maskbitslog2 = 5;
7064 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7065 maskbitslog2 = maskbitslog2 + 3;
7066 else
7067 maskbitslog2 = maskbitslog2 + 2;
7068 if (bed->s->arch_size == 64)
7070 if (maskbitslog2 == 5)
7071 maskbitslog2 = 6;
7072 cinfo.shift1 = 6;
7074 else
7075 cinfo.shift1 = 5;
7076 cinfo.mask = (1 << cinfo.shift1) - 1;
7077 cinfo.shift2 = maskbitslog2;
7078 cinfo.maskbits = 1 << maskbitslog2;
7079 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7080 amt = bucketcount * sizeof (unsigned long int) * 2;
7081 amt += maskwords * sizeof (bfd_vma);
7082 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7083 if (cinfo.bitmask == NULL)
7085 free (cinfo.hashcodes);
7086 return FALSE;
7089 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7090 cinfo.indx = cinfo.counts + bucketcount;
7091 cinfo.symindx = dynsymcount - cinfo.nsyms;
7092 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7094 /* Determine how often each hash bucket is used. */
7095 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7096 for (i = 0; i < cinfo.nsyms; ++i)
7097 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7099 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7100 if (cinfo.counts[i] != 0)
7102 cinfo.indx[i] = cnt;
7103 cnt += cinfo.counts[i];
7105 BFD_ASSERT (cnt == dynsymcount);
7106 cinfo.bucketcount = bucketcount;
7107 cinfo.local_indx = cinfo.min_dynindx;
7109 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7110 s->size += cinfo.maskbits / 8;
7111 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7112 if (contents == NULL)
7114 free (cinfo.bitmask);
7115 free (cinfo.hashcodes);
7116 return FALSE;
7119 s->contents = contents;
7120 bfd_put_32 (output_bfd, bucketcount, contents);
7121 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7122 bfd_put_32 (output_bfd, maskwords, contents + 8);
7123 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7124 contents += 16 + cinfo.maskbits / 8;
7126 for (i = 0; i < bucketcount; ++i)
7128 if (cinfo.counts[i] == 0)
7129 bfd_put_32 (output_bfd, 0, contents);
7130 else
7131 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7132 contents += 4;
7135 cinfo.contents = contents;
7137 /* Renumber dynamic symbols, populate .gnu.hash section. */
7138 elf_link_hash_traverse (elf_hash_table (info),
7139 elf_renumber_gnu_hash_syms, &cinfo);
7141 contents = s->contents + 16;
7142 for (i = 0; i < maskwords; ++i)
7144 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7145 contents);
7146 contents += bed->s->arch_size / 8;
7149 free (cinfo.bitmask);
7150 free (cinfo.hashcodes);
7154 s = bfd_get_linker_section (dynobj, ".dynstr");
7155 BFD_ASSERT (s != NULL);
7157 elf_finalize_dynstr (output_bfd, info);
7159 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7161 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7163 return FALSE;
7166 return TRUE;
7169 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7171 static void
7172 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7173 asection *sec)
7175 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7176 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7179 /* Finish SHF_MERGE section merging. */
7181 bfd_boolean
7182 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7184 bfd *ibfd;
7185 asection *sec;
7187 if (!is_elf_hash_table (info->hash))
7188 return FALSE;
7190 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7191 if ((ibfd->flags & DYNAMIC) == 0
7192 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7193 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7194 == get_elf_backend_data (obfd)->s->elfclass))
7195 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7196 if ((sec->flags & SEC_MERGE) != 0
7197 && !bfd_is_abs_section (sec->output_section))
7199 struct bfd_elf_section_data *secdata;
7201 secdata = elf_section_data (sec);
7202 if (! _bfd_add_merge_section (obfd,
7203 &elf_hash_table (info)->merge_info,
7204 sec, &secdata->sec_info))
7205 return FALSE;
7206 else if (secdata->sec_info)
7207 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7210 if (elf_hash_table (info)->merge_info != NULL)
7211 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7212 merge_sections_remove_hook);
7213 return TRUE;
7216 /* Create an entry in an ELF linker hash table. */
7218 struct bfd_hash_entry *
7219 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7220 struct bfd_hash_table *table,
7221 const char *string)
7223 /* Allocate the structure if it has not already been allocated by a
7224 subclass. */
7225 if (entry == NULL)
7227 entry = (struct bfd_hash_entry *)
7228 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7229 if (entry == NULL)
7230 return entry;
7233 /* Call the allocation method of the superclass. */
7234 entry = _bfd_link_hash_newfunc (entry, table, string);
7235 if (entry != NULL)
7237 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7238 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7240 /* Set local fields. */
7241 ret->indx = -1;
7242 ret->dynindx = -1;
7243 ret->got = htab->init_got_refcount;
7244 ret->plt = htab->init_plt_refcount;
7245 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7246 - offsetof (struct elf_link_hash_entry, size)));
7247 /* Assume that we have been called by a non-ELF symbol reader.
7248 This flag is then reset by the code which reads an ELF input
7249 file. This ensures that a symbol created by a non-ELF symbol
7250 reader will have the flag set correctly. */
7251 ret->non_elf = 1;
7254 return entry;
7257 /* Copy data from an indirect symbol to its direct symbol, hiding the
7258 old indirect symbol. Also used for copying flags to a weakdef. */
7260 void
7261 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7262 struct elf_link_hash_entry *dir,
7263 struct elf_link_hash_entry *ind)
7265 struct elf_link_hash_table *htab;
7267 /* Copy down any references that we may have already seen to the
7268 symbol which just became indirect. */
7270 if (dir->versioned != versioned_hidden)
7271 dir->ref_dynamic |= ind->ref_dynamic;
7272 dir->ref_regular |= ind->ref_regular;
7273 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7274 dir->non_got_ref |= ind->non_got_ref;
7275 dir->needs_plt |= ind->needs_plt;
7276 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7278 if (ind->root.type != bfd_link_hash_indirect)
7279 return;
7281 /* Copy over the global and procedure linkage table refcount entries.
7282 These may have been already set up by a check_relocs routine. */
7283 htab = elf_hash_table (info);
7284 if (ind->got.refcount > htab->init_got_refcount.refcount)
7286 if (dir->got.refcount < 0)
7287 dir->got.refcount = 0;
7288 dir->got.refcount += ind->got.refcount;
7289 ind->got.refcount = htab->init_got_refcount.refcount;
7292 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7294 if (dir->plt.refcount < 0)
7295 dir->plt.refcount = 0;
7296 dir->plt.refcount += ind->plt.refcount;
7297 ind->plt.refcount = htab->init_plt_refcount.refcount;
7300 if (ind->dynindx != -1)
7302 if (dir->dynindx != -1)
7303 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7304 dir->dynindx = ind->dynindx;
7305 dir->dynstr_index = ind->dynstr_index;
7306 ind->dynindx = -1;
7307 ind->dynstr_index = 0;
7311 void
7312 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7313 struct elf_link_hash_entry *h,
7314 bfd_boolean force_local)
7316 /* STT_GNU_IFUNC symbol must go through PLT. */
7317 if (h->type != STT_GNU_IFUNC)
7319 h->plt = elf_hash_table (info)->init_plt_offset;
7320 h->needs_plt = 0;
7322 if (force_local)
7324 h->forced_local = 1;
7325 if (h->dynindx != -1)
7327 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7328 h->dynstr_index);
7329 h->dynindx = -1;
7330 h->dynstr_index = 0;
7335 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7336 caller. */
7338 bfd_boolean
7339 _bfd_elf_link_hash_table_init
7340 (struct elf_link_hash_table *table,
7341 bfd *abfd,
7342 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7343 struct bfd_hash_table *,
7344 const char *),
7345 unsigned int entsize,
7346 enum elf_target_id target_id)
7348 bfd_boolean ret;
7349 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7351 table->init_got_refcount.refcount = can_refcount - 1;
7352 table->init_plt_refcount.refcount = can_refcount - 1;
7353 table->init_got_offset.offset = -(bfd_vma) 1;
7354 table->init_plt_offset.offset = -(bfd_vma) 1;
7355 /* The first dynamic symbol is a dummy. */
7356 table->dynsymcount = 1;
7358 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7360 table->root.type = bfd_link_elf_hash_table;
7361 table->hash_table_id = target_id;
7363 return ret;
7366 /* Create an ELF linker hash table. */
7368 struct bfd_link_hash_table *
7369 _bfd_elf_link_hash_table_create (bfd *abfd)
7371 struct elf_link_hash_table *ret;
7372 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7374 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7375 if (ret == NULL)
7376 return NULL;
7378 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7379 sizeof (struct elf_link_hash_entry),
7380 GENERIC_ELF_DATA))
7382 free (ret);
7383 return NULL;
7385 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7387 return &ret->root;
7390 /* Destroy an ELF linker hash table. */
7392 void
7393 _bfd_elf_link_hash_table_free (bfd *obfd)
7395 struct elf_link_hash_table *htab;
7397 htab = (struct elf_link_hash_table *) obfd->link.hash;
7398 if (htab->dynstr != NULL)
7399 _bfd_elf_strtab_free (htab->dynstr);
7400 _bfd_merge_sections_free (htab->merge_info);
7401 _bfd_generic_link_hash_table_free (obfd);
7404 /* This is a hook for the ELF emulation code in the generic linker to
7405 tell the backend linker what file name to use for the DT_NEEDED
7406 entry for a dynamic object. */
7408 void
7409 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7411 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7412 && bfd_get_format (abfd) == bfd_object)
7413 elf_dt_name (abfd) = name;
7417 bfd_elf_get_dyn_lib_class (bfd *abfd)
7419 int lib_class;
7420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7421 && bfd_get_format (abfd) == bfd_object)
7422 lib_class = elf_dyn_lib_class (abfd);
7423 else
7424 lib_class = 0;
7425 return lib_class;
7428 void
7429 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7431 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7432 && bfd_get_format (abfd) == bfd_object)
7433 elf_dyn_lib_class (abfd) = lib_class;
7436 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7437 the linker ELF emulation code. */
7439 struct bfd_link_needed_list *
7440 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7441 struct bfd_link_info *info)
7443 if (! is_elf_hash_table (info->hash))
7444 return NULL;
7445 return elf_hash_table (info)->needed;
7448 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7449 hook for the linker ELF emulation code. */
7451 struct bfd_link_needed_list *
7452 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7453 struct bfd_link_info *info)
7455 if (! is_elf_hash_table (info->hash))
7456 return NULL;
7457 return elf_hash_table (info)->runpath;
7460 /* Get the name actually used for a dynamic object for a link. This
7461 is the SONAME entry if there is one. Otherwise, it is the string
7462 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7464 const char *
7465 bfd_elf_get_dt_soname (bfd *abfd)
7467 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7468 && bfd_get_format (abfd) == bfd_object)
7469 return elf_dt_name (abfd);
7470 return NULL;
7473 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7474 the ELF linker emulation code. */
7476 bfd_boolean
7477 bfd_elf_get_bfd_needed_list (bfd *abfd,
7478 struct bfd_link_needed_list **pneeded)
7480 asection *s;
7481 bfd_byte *dynbuf = NULL;
7482 unsigned int elfsec;
7483 unsigned long shlink;
7484 bfd_byte *extdyn, *extdynend;
7485 size_t extdynsize;
7486 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7488 *pneeded = NULL;
7490 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7491 || bfd_get_format (abfd) != bfd_object)
7492 return TRUE;
7494 s = bfd_get_section_by_name (abfd, ".dynamic");
7495 if (s == NULL || s->size == 0)
7496 return TRUE;
7498 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7499 goto error_return;
7501 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7502 if (elfsec == SHN_BAD)
7503 goto error_return;
7505 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7507 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7508 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7510 extdyn = dynbuf;
7511 extdynend = extdyn + s->size;
7512 for (; extdyn < extdynend; extdyn += extdynsize)
7514 Elf_Internal_Dyn dyn;
7516 (*swap_dyn_in) (abfd, extdyn, &dyn);
7518 if (dyn.d_tag == DT_NULL)
7519 break;
7521 if (dyn.d_tag == DT_NEEDED)
7523 const char *string;
7524 struct bfd_link_needed_list *l;
7525 unsigned int tagv = dyn.d_un.d_val;
7526 bfd_size_type amt;
7528 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7529 if (string == NULL)
7530 goto error_return;
7532 amt = sizeof *l;
7533 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7534 if (l == NULL)
7535 goto error_return;
7537 l->by = abfd;
7538 l->name = string;
7539 l->next = *pneeded;
7540 *pneeded = l;
7544 free (dynbuf);
7546 return TRUE;
7548 error_return:
7549 if (dynbuf != NULL)
7550 free (dynbuf);
7551 return FALSE;
7554 struct elf_symbuf_symbol
7556 unsigned long st_name; /* Symbol name, index in string tbl */
7557 unsigned char st_info; /* Type and binding attributes */
7558 unsigned char st_other; /* Visibilty, and target specific */
7561 struct elf_symbuf_head
7563 struct elf_symbuf_symbol *ssym;
7564 size_t count;
7565 unsigned int st_shndx;
7568 struct elf_symbol
7570 union
7572 Elf_Internal_Sym *isym;
7573 struct elf_symbuf_symbol *ssym;
7574 } u;
7575 const char *name;
7578 /* Sort references to symbols by ascending section number. */
7580 static int
7581 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7583 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7584 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7586 return s1->st_shndx - s2->st_shndx;
7589 static int
7590 elf_sym_name_compare (const void *arg1, const void *arg2)
7592 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7593 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7594 return strcmp (s1->name, s2->name);
7597 static struct elf_symbuf_head *
7598 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7600 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7601 struct elf_symbuf_symbol *ssym;
7602 struct elf_symbuf_head *ssymbuf, *ssymhead;
7603 size_t i, shndx_count, total_size;
7605 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7606 if (indbuf == NULL)
7607 return NULL;
7609 for (ind = indbuf, i = 0; i < symcount; i++)
7610 if (isymbuf[i].st_shndx != SHN_UNDEF)
7611 *ind++ = &isymbuf[i];
7612 indbufend = ind;
7614 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7615 elf_sort_elf_symbol);
7617 shndx_count = 0;
7618 if (indbufend > indbuf)
7619 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7620 if (ind[0]->st_shndx != ind[1]->st_shndx)
7621 shndx_count++;
7623 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7624 + (indbufend - indbuf) * sizeof (*ssym));
7625 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7626 if (ssymbuf == NULL)
7628 free (indbuf);
7629 return NULL;
7632 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7633 ssymbuf->ssym = NULL;
7634 ssymbuf->count = shndx_count;
7635 ssymbuf->st_shndx = 0;
7636 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7638 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7640 ssymhead++;
7641 ssymhead->ssym = ssym;
7642 ssymhead->count = 0;
7643 ssymhead->st_shndx = (*ind)->st_shndx;
7645 ssym->st_name = (*ind)->st_name;
7646 ssym->st_info = (*ind)->st_info;
7647 ssym->st_other = (*ind)->st_other;
7648 ssymhead->count++;
7650 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7651 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7652 == total_size));
7654 free (indbuf);
7655 return ssymbuf;
7658 /* Check if 2 sections define the same set of local and global
7659 symbols. */
7661 static bfd_boolean
7662 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7663 struct bfd_link_info *info)
7665 bfd *bfd1, *bfd2;
7666 const struct elf_backend_data *bed1, *bed2;
7667 Elf_Internal_Shdr *hdr1, *hdr2;
7668 size_t symcount1, symcount2;
7669 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7670 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7671 Elf_Internal_Sym *isym, *isymend;
7672 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7673 size_t count1, count2, i;
7674 unsigned int shndx1, shndx2;
7675 bfd_boolean result;
7677 bfd1 = sec1->owner;
7678 bfd2 = sec2->owner;
7680 /* Both sections have to be in ELF. */
7681 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7682 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7683 return FALSE;
7685 if (elf_section_type (sec1) != elf_section_type (sec2))
7686 return FALSE;
7688 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7689 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7690 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7691 return FALSE;
7693 bed1 = get_elf_backend_data (bfd1);
7694 bed2 = get_elf_backend_data (bfd2);
7695 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7696 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7697 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7698 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7700 if (symcount1 == 0 || symcount2 == 0)
7701 return FALSE;
7703 result = FALSE;
7704 isymbuf1 = NULL;
7705 isymbuf2 = NULL;
7706 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7707 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7709 if (ssymbuf1 == NULL)
7711 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7712 NULL, NULL, NULL);
7713 if (isymbuf1 == NULL)
7714 goto done;
7716 if (!info->reduce_memory_overheads)
7717 elf_tdata (bfd1)->symbuf = ssymbuf1
7718 = elf_create_symbuf (symcount1, isymbuf1);
7721 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7723 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7724 NULL, NULL, NULL);
7725 if (isymbuf2 == NULL)
7726 goto done;
7728 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7729 elf_tdata (bfd2)->symbuf = ssymbuf2
7730 = elf_create_symbuf (symcount2, isymbuf2);
7733 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7735 /* Optimized faster version. */
7736 size_t lo, hi, mid;
7737 struct elf_symbol *symp;
7738 struct elf_symbuf_symbol *ssym, *ssymend;
7740 lo = 0;
7741 hi = ssymbuf1->count;
7742 ssymbuf1++;
7743 count1 = 0;
7744 while (lo < hi)
7746 mid = (lo + hi) / 2;
7747 if (shndx1 < ssymbuf1[mid].st_shndx)
7748 hi = mid;
7749 else if (shndx1 > ssymbuf1[mid].st_shndx)
7750 lo = mid + 1;
7751 else
7753 count1 = ssymbuf1[mid].count;
7754 ssymbuf1 += mid;
7755 break;
7759 lo = 0;
7760 hi = ssymbuf2->count;
7761 ssymbuf2++;
7762 count2 = 0;
7763 while (lo < hi)
7765 mid = (lo + hi) / 2;
7766 if (shndx2 < ssymbuf2[mid].st_shndx)
7767 hi = mid;
7768 else if (shndx2 > ssymbuf2[mid].st_shndx)
7769 lo = mid + 1;
7770 else
7772 count2 = ssymbuf2[mid].count;
7773 ssymbuf2 += mid;
7774 break;
7778 if (count1 == 0 || count2 == 0 || count1 != count2)
7779 goto done;
7781 symtable1
7782 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7783 symtable2
7784 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7785 if (symtable1 == NULL || symtable2 == NULL)
7786 goto done;
7788 symp = symtable1;
7789 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7790 ssym < ssymend; ssym++, symp++)
7792 symp->u.ssym = ssym;
7793 symp->name = bfd_elf_string_from_elf_section (bfd1,
7794 hdr1->sh_link,
7795 ssym->st_name);
7798 symp = symtable2;
7799 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7800 ssym < ssymend; ssym++, symp++)
7802 symp->u.ssym = ssym;
7803 symp->name = bfd_elf_string_from_elf_section (bfd2,
7804 hdr2->sh_link,
7805 ssym->st_name);
7808 /* Sort symbol by name. */
7809 qsort (symtable1, count1, sizeof (struct elf_symbol),
7810 elf_sym_name_compare);
7811 qsort (symtable2, count1, sizeof (struct elf_symbol),
7812 elf_sym_name_compare);
7814 for (i = 0; i < count1; i++)
7815 /* Two symbols must have the same binding, type and name. */
7816 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7817 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7818 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7819 goto done;
7821 result = TRUE;
7822 goto done;
7825 symtable1 = (struct elf_symbol *)
7826 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7827 symtable2 = (struct elf_symbol *)
7828 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7829 if (symtable1 == NULL || symtable2 == NULL)
7830 goto done;
7832 /* Count definitions in the section. */
7833 count1 = 0;
7834 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7835 if (isym->st_shndx == shndx1)
7836 symtable1[count1++].u.isym = isym;
7838 count2 = 0;
7839 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7840 if (isym->st_shndx == shndx2)
7841 symtable2[count2++].u.isym = isym;
7843 if (count1 == 0 || count2 == 0 || count1 != count2)
7844 goto done;
7846 for (i = 0; i < count1; i++)
7847 symtable1[i].name
7848 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7849 symtable1[i].u.isym->st_name);
7851 for (i = 0; i < count2; i++)
7852 symtable2[i].name
7853 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7854 symtable2[i].u.isym->st_name);
7856 /* Sort symbol by name. */
7857 qsort (symtable1, count1, sizeof (struct elf_symbol),
7858 elf_sym_name_compare);
7859 qsort (symtable2, count1, sizeof (struct elf_symbol),
7860 elf_sym_name_compare);
7862 for (i = 0; i < count1; i++)
7863 /* Two symbols must have the same binding, type and name. */
7864 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7865 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7866 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7867 goto done;
7869 result = TRUE;
7871 done:
7872 if (symtable1)
7873 free (symtable1);
7874 if (symtable2)
7875 free (symtable2);
7876 if (isymbuf1)
7877 free (isymbuf1);
7878 if (isymbuf2)
7879 free (isymbuf2);
7881 return result;
7884 /* Return TRUE if 2 section types are compatible. */
7886 bfd_boolean
7887 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7888 bfd *bbfd, const asection *bsec)
7890 if (asec == NULL
7891 || bsec == NULL
7892 || abfd->xvec->flavour != bfd_target_elf_flavour
7893 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7894 return TRUE;
7896 return elf_section_type (asec) == elf_section_type (bsec);
7899 /* Final phase of ELF linker. */
7901 /* A structure we use to avoid passing large numbers of arguments. */
7903 struct elf_final_link_info
7905 /* General link information. */
7906 struct bfd_link_info *info;
7907 /* Output BFD. */
7908 bfd *output_bfd;
7909 /* Symbol string table. */
7910 struct elf_strtab_hash *symstrtab;
7911 /* .hash section. */
7912 asection *hash_sec;
7913 /* symbol version section (.gnu.version). */
7914 asection *symver_sec;
7915 /* Buffer large enough to hold contents of any section. */
7916 bfd_byte *contents;
7917 /* Buffer large enough to hold external relocs of any section. */
7918 void *external_relocs;
7919 /* Buffer large enough to hold internal relocs of any section. */
7920 Elf_Internal_Rela *internal_relocs;
7921 /* Buffer large enough to hold external local symbols of any input
7922 BFD. */
7923 bfd_byte *external_syms;
7924 /* And a buffer for symbol section indices. */
7925 Elf_External_Sym_Shndx *locsym_shndx;
7926 /* Buffer large enough to hold internal local symbols of any input
7927 BFD. */
7928 Elf_Internal_Sym *internal_syms;
7929 /* Array large enough to hold a symbol index for each local symbol
7930 of any input BFD. */
7931 long *indices;
7932 /* Array large enough to hold a section pointer for each local
7933 symbol of any input BFD. */
7934 asection **sections;
7935 /* Buffer for SHT_SYMTAB_SHNDX section. */
7936 Elf_External_Sym_Shndx *symshndxbuf;
7937 /* Number of STT_FILE syms seen. */
7938 size_t filesym_count;
7941 /* This struct is used to pass information to elf_link_output_extsym. */
7943 struct elf_outext_info
7945 bfd_boolean failed;
7946 bfd_boolean localsyms;
7947 bfd_boolean file_sym_done;
7948 struct elf_final_link_info *flinfo;
7952 /* Support for evaluating a complex relocation.
7954 Complex relocations are generalized, self-describing relocations. The
7955 implementation of them consists of two parts: complex symbols, and the
7956 relocations themselves.
7958 The relocations are use a reserved elf-wide relocation type code (R_RELC
7959 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7960 information (start bit, end bit, word width, etc) into the addend. This
7961 information is extracted from CGEN-generated operand tables within gas.
7963 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7964 internal) representing prefix-notation expressions, including but not
7965 limited to those sorts of expressions normally encoded as addends in the
7966 addend field. The symbol mangling format is:
7968 <node> := <literal>
7969 | <unary-operator> ':' <node>
7970 | <binary-operator> ':' <node> ':' <node>
7973 <literal> := 's' <digits=N> ':' <N character symbol name>
7974 | 'S' <digits=N> ':' <N character section name>
7975 | '#' <hexdigits>
7978 <binary-operator> := as in C
7979 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7981 static void
7982 set_symbol_value (bfd *bfd_with_globals,
7983 Elf_Internal_Sym *isymbuf,
7984 size_t locsymcount,
7985 size_t symidx,
7986 bfd_vma val)
7988 struct elf_link_hash_entry **sym_hashes;
7989 struct elf_link_hash_entry *h;
7990 size_t extsymoff = locsymcount;
7992 if (symidx < locsymcount)
7994 Elf_Internal_Sym *sym;
7996 sym = isymbuf + symidx;
7997 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7999 /* It is a local symbol: move it to the
8000 "absolute" section and give it a value. */
8001 sym->st_shndx = SHN_ABS;
8002 sym->st_value = val;
8003 return;
8005 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8006 extsymoff = 0;
8009 /* It is a global symbol: set its link type
8010 to "defined" and give it a value. */
8012 sym_hashes = elf_sym_hashes (bfd_with_globals);
8013 h = sym_hashes [symidx - extsymoff];
8014 while (h->root.type == bfd_link_hash_indirect
8015 || h->root.type == bfd_link_hash_warning)
8016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8017 h->root.type = bfd_link_hash_defined;
8018 h->root.u.def.value = val;
8019 h->root.u.def.section = bfd_abs_section_ptr;
8022 static bfd_boolean
8023 resolve_symbol (const char *name,
8024 bfd *input_bfd,
8025 struct elf_final_link_info *flinfo,
8026 bfd_vma *result,
8027 Elf_Internal_Sym *isymbuf,
8028 size_t locsymcount)
8030 Elf_Internal_Sym *sym;
8031 struct bfd_link_hash_entry *global_entry;
8032 const char *candidate = NULL;
8033 Elf_Internal_Shdr *symtab_hdr;
8034 size_t i;
8036 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8038 for (i = 0; i < locsymcount; ++ i)
8040 sym = isymbuf + i;
8042 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8043 continue;
8045 candidate = bfd_elf_string_from_elf_section (input_bfd,
8046 symtab_hdr->sh_link,
8047 sym->st_name);
8048 #ifdef DEBUG
8049 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8050 name, candidate, (unsigned long) sym->st_value);
8051 #endif
8052 if (candidate && strcmp (candidate, name) == 0)
8054 asection *sec = flinfo->sections [i];
8056 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8057 *result += sec->output_offset + sec->output_section->vma;
8058 #ifdef DEBUG
8059 printf ("Found symbol with value %8.8lx\n",
8060 (unsigned long) *result);
8061 #endif
8062 return TRUE;
8066 /* Hmm, haven't found it yet. perhaps it is a global. */
8067 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8068 FALSE, FALSE, TRUE);
8069 if (!global_entry)
8070 return FALSE;
8072 if (global_entry->type == bfd_link_hash_defined
8073 || global_entry->type == bfd_link_hash_defweak)
8075 *result = (global_entry->u.def.value
8076 + global_entry->u.def.section->output_section->vma
8077 + global_entry->u.def.section->output_offset);
8078 #ifdef DEBUG
8079 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8080 global_entry->root.string, (unsigned long) *result);
8081 #endif
8082 return TRUE;
8085 return FALSE;
8088 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8089 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8090 names like "foo.end" which is the end address of section "foo". */
8092 static bfd_boolean
8093 resolve_section (const char *name,
8094 asection *sections,
8095 bfd_vma *result,
8096 bfd * abfd)
8098 asection *curr;
8099 unsigned int len;
8101 for (curr = sections; curr; curr = curr->next)
8102 if (strcmp (curr->name, name) == 0)
8104 *result = curr->vma;
8105 return TRUE;
8108 /* Hmm. still haven't found it. try pseudo-section names. */
8109 /* FIXME: This could be coded more efficiently... */
8110 for (curr = sections; curr; curr = curr->next)
8112 len = strlen (curr->name);
8113 if (len > strlen (name))
8114 continue;
8116 if (strncmp (curr->name, name, len) == 0)
8118 if (strncmp (".end", name + len, 4) == 0)
8120 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8121 return TRUE;
8124 /* Insert more pseudo-section names here, if you like. */
8128 return FALSE;
8131 static void
8132 undefined_reference (const char *reftype, const char *name)
8134 /* xgettext:c-format */
8135 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8136 reftype, name);
8139 static bfd_boolean
8140 eval_symbol (bfd_vma *result,
8141 const char **symp,
8142 bfd *input_bfd,
8143 struct elf_final_link_info *flinfo,
8144 bfd_vma dot,
8145 Elf_Internal_Sym *isymbuf,
8146 size_t locsymcount,
8147 int signed_p)
8149 size_t len;
8150 size_t symlen;
8151 bfd_vma a;
8152 bfd_vma b;
8153 char symbuf[4096];
8154 const char *sym = *symp;
8155 const char *symend;
8156 bfd_boolean symbol_is_section = FALSE;
8158 len = strlen (sym);
8159 symend = sym + len;
8161 if (len < 1 || len > sizeof (symbuf))
8163 bfd_set_error (bfd_error_invalid_operation);
8164 return FALSE;
8167 switch (* sym)
8169 case '.':
8170 *result = dot;
8171 *symp = sym + 1;
8172 return TRUE;
8174 case '#':
8175 ++sym;
8176 *result = strtoul (sym, (char **) symp, 16);
8177 return TRUE;
8179 case 'S':
8180 symbol_is_section = TRUE;
8181 /* Fall through. */
8182 case 's':
8183 ++sym;
8184 symlen = strtol (sym, (char **) symp, 10);
8185 sym = *symp + 1; /* Skip the trailing ':'. */
8187 if (symend < sym || symlen + 1 > sizeof (symbuf))
8189 bfd_set_error (bfd_error_invalid_operation);
8190 return FALSE;
8193 memcpy (symbuf, sym, symlen);
8194 symbuf[symlen] = '\0';
8195 *symp = sym + symlen;
8197 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8198 the symbol as a section, or vice-versa. so we're pretty liberal in our
8199 interpretation here; section means "try section first", not "must be a
8200 section", and likewise with symbol. */
8202 if (symbol_is_section)
8204 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8205 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8206 isymbuf, locsymcount))
8208 undefined_reference ("section", symbuf);
8209 return FALSE;
8212 else
8214 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8215 isymbuf, locsymcount)
8216 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8217 result, input_bfd))
8219 undefined_reference ("symbol", symbuf);
8220 return FALSE;
8224 return TRUE;
8226 /* All that remains are operators. */
8228 #define UNARY_OP(op) \
8229 if (strncmp (sym, #op, strlen (#op)) == 0) \
8231 sym += strlen (#op); \
8232 if (*sym == ':') \
8233 ++sym; \
8234 *symp = sym; \
8235 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8236 isymbuf, locsymcount, signed_p)) \
8237 return FALSE; \
8238 if (signed_p) \
8239 *result = op ((bfd_signed_vma) a); \
8240 else \
8241 *result = op a; \
8242 return TRUE; \
8245 #define BINARY_OP(op) \
8246 if (strncmp (sym, #op, strlen (#op)) == 0) \
8248 sym += strlen (#op); \
8249 if (*sym == ':') \
8250 ++sym; \
8251 *symp = sym; \
8252 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8253 isymbuf, locsymcount, signed_p)) \
8254 return FALSE; \
8255 ++*symp; \
8256 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8257 isymbuf, locsymcount, signed_p)) \
8258 return FALSE; \
8259 if (signed_p) \
8260 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8261 else \
8262 *result = a op b; \
8263 return TRUE; \
8266 default:
8267 UNARY_OP (0-);
8268 BINARY_OP (<<);
8269 BINARY_OP (>>);
8270 BINARY_OP (==);
8271 BINARY_OP (!=);
8272 BINARY_OP (<=);
8273 BINARY_OP (>=);
8274 BINARY_OP (&&);
8275 BINARY_OP (||);
8276 UNARY_OP (~);
8277 UNARY_OP (!);
8278 BINARY_OP (*);
8279 BINARY_OP (/);
8280 BINARY_OP (%);
8281 BINARY_OP (^);
8282 BINARY_OP (|);
8283 BINARY_OP (&);
8284 BINARY_OP (+);
8285 BINARY_OP (-);
8286 BINARY_OP (<);
8287 BINARY_OP (>);
8288 #undef UNARY_OP
8289 #undef BINARY_OP
8290 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8291 bfd_set_error (bfd_error_invalid_operation);
8292 return FALSE;
8296 static void
8297 put_value (bfd_vma size,
8298 unsigned long chunksz,
8299 bfd *input_bfd,
8300 bfd_vma x,
8301 bfd_byte *location)
8303 location += (size - chunksz);
8305 for (; size; size -= chunksz, location -= chunksz)
8307 switch (chunksz)
8309 case 1:
8310 bfd_put_8 (input_bfd, x, location);
8311 x >>= 8;
8312 break;
8313 case 2:
8314 bfd_put_16 (input_bfd, x, location);
8315 x >>= 16;
8316 break;
8317 case 4:
8318 bfd_put_32 (input_bfd, x, location);
8319 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8320 x >>= 16;
8321 x >>= 16;
8322 break;
8323 #ifdef BFD64
8324 case 8:
8325 bfd_put_64 (input_bfd, x, location);
8326 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8327 x >>= 32;
8328 x >>= 32;
8329 break;
8330 #endif
8331 default:
8332 abort ();
8333 break;
8338 static bfd_vma
8339 get_value (bfd_vma size,
8340 unsigned long chunksz,
8341 bfd *input_bfd,
8342 bfd_byte *location)
8344 int shift;
8345 bfd_vma x = 0;
8347 /* Sanity checks. */
8348 BFD_ASSERT (chunksz <= sizeof (x)
8349 && size >= chunksz
8350 && chunksz != 0
8351 && (size % chunksz) == 0
8352 && input_bfd != NULL
8353 && location != NULL);
8355 if (chunksz == sizeof (x))
8357 BFD_ASSERT (size == chunksz);
8359 /* Make sure that we do not perform an undefined shift operation.
8360 We know that size == chunksz so there will only be one iteration
8361 of the loop below. */
8362 shift = 0;
8364 else
8365 shift = 8 * chunksz;
8367 for (; size; size -= chunksz, location += chunksz)
8369 switch (chunksz)
8371 case 1:
8372 x = (x << shift) | bfd_get_8 (input_bfd, location);
8373 break;
8374 case 2:
8375 x = (x << shift) | bfd_get_16 (input_bfd, location);
8376 break;
8377 case 4:
8378 x = (x << shift) | bfd_get_32 (input_bfd, location);
8379 break;
8380 #ifdef BFD64
8381 case 8:
8382 x = (x << shift) | bfd_get_64 (input_bfd, location);
8383 break;
8384 #endif
8385 default:
8386 abort ();
8389 return x;
8392 static void
8393 decode_complex_addend (unsigned long *start, /* in bits */
8394 unsigned long *oplen, /* in bits */
8395 unsigned long *len, /* in bits */
8396 unsigned long *wordsz, /* in bytes */
8397 unsigned long *chunksz, /* in bytes */
8398 unsigned long *lsb0_p,
8399 unsigned long *signed_p,
8400 unsigned long *trunc_p,
8401 unsigned long encoded)
8403 * start = encoded & 0x3F;
8404 * len = (encoded >> 6) & 0x3F;
8405 * oplen = (encoded >> 12) & 0x3F;
8406 * wordsz = (encoded >> 18) & 0xF;
8407 * chunksz = (encoded >> 22) & 0xF;
8408 * lsb0_p = (encoded >> 27) & 1;
8409 * signed_p = (encoded >> 28) & 1;
8410 * trunc_p = (encoded >> 29) & 1;
8413 bfd_reloc_status_type
8414 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8415 asection *input_section ATTRIBUTE_UNUSED,
8416 bfd_byte *contents,
8417 Elf_Internal_Rela *rel,
8418 bfd_vma relocation)
8420 bfd_vma shift, x, mask;
8421 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8422 bfd_reloc_status_type r;
8424 /* Perform this reloc, since it is complex.
8425 (this is not to say that it necessarily refers to a complex
8426 symbol; merely that it is a self-describing CGEN based reloc.
8427 i.e. the addend has the complete reloc information (bit start, end,
8428 word size, etc) encoded within it.). */
8430 decode_complex_addend (&start, &oplen, &len, &wordsz,
8431 &chunksz, &lsb0_p, &signed_p,
8432 &trunc_p, rel->r_addend);
8434 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8436 if (lsb0_p)
8437 shift = (start + 1) - len;
8438 else
8439 shift = (8 * wordsz) - (start + len);
8441 x = get_value (wordsz, chunksz, input_bfd,
8442 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8444 #ifdef DEBUG
8445 printf ("Doing complex reloc: "
8446 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8447 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8448 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8449 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8450 oplen, (unsigned long) x, (unsigned long) mask,
8451 (unsigned long) relocation);
8452 #endif
8454 r = bfd_reloc_ok;
8455 if (! trunc_p)
8456 /* Now do an overflow check. */
8457 r = bfd_check_overflow ((signed_p
8458 ? complain_overflow_signed
8459 : complain_overflow_unsigned),
8460 len, 0, (8 * wordsz),
8461 relocation);
8463 /* Do the deed. */
8464 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8466 #ifdef DEBUG
8467 printf (" relocation: %8.8lx\n"
8468 " shifted mask: %8.8lx\n"
8469 " shifted/masked reloc: %8.8lx\n"
8470 " result: %8.8lx\n",
8471 (unsigned long) relocation, (unsigned long) (mask << shift),
8472 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8473 #endif
8474 put_value (wordsz, chunksz, input_bfd, x,
8475 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8476 return r;
8479 /* Functions to read r_offset from external (target order) reloc
8480 entry. Faster than bfd_getl32 et al, because we let the compiler
8481 know the value is aligned. */
8483 static bfd_vma
8484 ext32l_r_offset (const void *p)
8486 union aligned32
8488 uint32_t v;
8489 unsigned char c[4];
8491 const union aligned32 *a
8492 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8494 uint32_t aval = ( (uint32_t) a->c[0]
8495 | (uint32_t) a->c[1] << 8
8496 | (uint32_t) a->c[2] << 16
8497 | (uint32_t) a->c[3] << 24);
8498 return aval;
8501 static bfd_vma
8502 ext32b_r_offset (const void *p)
8504 union aligned32
8506 uint32_t v;
8507 unsigned char c[4];
8509 const union aligned32 *a
8510 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8512 uint32_t aval = ( (uint32_t) a->c[0] << 24
8513 | (uint32_t) a->c[1] << 16
8514 | (uint32_t) a->c[2] << 8
8515 | (uint32_t) a->c[3]);
8516 return aval;
8519 #ifdef BFD_HOST_64_BIT
8520 static bfd_vma
8521 ext64l_r_offset (const void *p)
8523 union aligned64
8525 uint64_t v;
8526 unsigned char c[8];
8528 const union aligned64 *a
8529 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8531 uint64_t aval = ( (uint64_t) a->c[0]
8532 | (uint64_t) a->c[1] << 8
8533 | (uint64_t) a->c[2] << 16
8534 | (uint64_t) a->c[3] << 24
8535 | (uint64_t) a->c[4] << 32
8536 | (uint64_t) a->c[5] << 40
8537 | (uint64_t) a->c[6] << 48
8538 | (uint64_t) a->c[7] << 56);
8539 return aval;
8542 static bfd_vma
8543 ext64b_r_offset (const void *p)
8545 union aligned64
8547 uint64_t v;
8548 unsigned char c[8];
8550 const union aligned64 *a
8551 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8553 uint64_t aval = ( (uint64_t) a->c[0] << 56
8554 | (uint64_t) a->c[1] << 48
8555 | (uint64_t) a->c[2] << 40
8556 | (uint64_t) a->c[3] << 32
8557 | (uint64_t) a->c[4] << 24
8558 | (uint64_t) a->c[5] << 16
8559 | (uint64_t) a->c[6] << 8
8560 | (uint64_t) a->c[7]);
8561 return aval;
8563 #endif
8565 /* When performing a relocatable link, the input relocations are
8566 preserved. But, if they reference global symbols, the indices
8567 referenced must be updated. Update all the relocations found in
8568 RELDATA. */
8570 static bfd_boolean
8571 elf_link_adjust_relocs (bfd *abfd,
8572 asection *sec,
8573 struct bfd_elf_section_reloc_data *reldata,
8574 bfd_boolean sort,
8575 struct bfd_link_info *info)
8577 unsigned int i;
8578 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8579 bfd_byte *erela;
8580 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8581 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8582 bfd_vma r_type_mask;
8583 int r_sym_shift;
8584 unsigned int count = reldata->count;
8585 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8587 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8589 swap_in = bed->s->swap_reloc_in;
8590 swap_out = bed->s->swap_reloc_out;
8592 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8594 swap_in = bed->s->swap_reloca_in;
8595 swap_out = bed->s->swap_reloca_out;
8597 else
8598 abort ();
8600 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8601 abort ();
8603 if (bed->s->arch_size == 32)
8605 r_type_mask = 0xff;
8606 r_sym_shift = 8;
8608 else
8610 r_type_mask = 0xffffffff;
8611 r_sym_shift = 32;
8614 erela = reldata->hdr->contents;
8615 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8617 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8618 unsigned int j;
8620 if (*rel_hash == NULL)
8621 continue;
8623 if ((*rel_hash)->indx == -2
8624 && info->gc_sections
8625 && ! info->gc_keep_exported)
8627 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8628 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8629 abfd, sec,
8630 (*rel_hash)->root.root.string);
8631 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8632 abfd, sec);
8633 bfd_set_error (bfd_error_invalid_operation);
8634 return FALSE;
8636 BFD_ASSERT ((*rel_hash)->indx >= 0);
8638 (*swap_in) (abfd, erela, irela);
8639 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8640 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8641 | (irela[j].r_info & r_type_mask));
8642 (*swap_out) (abfd, irela, erela);
8645 if (bed->elf_backend_update_relocs)
8646 (*bed->elf_backend_update_relocs) (sec, reldata);
8648 if (sort && count != 0)
8650 bfd_vma (*ext_r_off) (const void *);
8651 bfd_vma r_off;
8652 size_t elt_size;
8653 bfd_byte *base, *end, *p, *loc;
8654 bfd_byte *buf = NULL;
8656 if (bed->s->arch_size == 32)
8658 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8659 ext_r_off = ext32l_r_offset;
8660 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8661 ext_r_off = ext32b_r_offset;
8662 else
8663 abort ();
8665 else
8667 #ifdef BFD_HOST_64_BIT
8668 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8669 ext_r_off = ext64l_r_offset;
8670 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8671 ext_r_off = ext64b_r_offset;
8672 else
8673 #endif
8674 abort ();
8677 /* Must use a stable sort here. A modified insertion sort,
8678 since the relocs are mostly sorted already. */
8679 elt_size = reldata->hdr->sh_entsize;
8680 base = reldata->hdr->contents;
8681 end = base + count * elt_size;
8682 if (elt_size > sizeof (Elf64_External_Rela))
8683 abort ();
8685 /* Ensure the first element is lowest. This acts as a sentinel,
8686 speeding the main loop below. */
8687 r_off = (*ext_r_off) (base);
8688 for (p = loc = base; (p += elt_size) < end; )
8690 bfd_vma r_off2 = (*ext_r_off) (p);
8691 if (r_off > r_off2)
8693 r_off = r_off2;
8694 loc = p;
8697 if (loc != base)
8699 /* Don't just swap *base and *loc as that changes the order
8700 of the original base[0] and base[1] if they happen to
8701 have the same r_offset. */
8702 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8703 memcpy (onebuf, loc, elt_size);
8704 memmove (base + elt_size, base, loc - base);
8705 memcpy (base, onebuf, elt_size);
8708 for (p = base + elt_size; (p += elt_size) < end; )
8710 /* base to p is sorted, *p is next to insert. */
8711 r_off = (*ext_r_off) (p);
8712 /* Search the sorted region for location to insert. */
8713 loc = p - elt_size;
8714 while (r_off < (*ext_r_off) (loc))
8715 loc -= elt_size;
8716 loc += elt_size;
8717 if (loc != p)
8719 /* Chances are there is a run of relocs to insert here,
8720 from one of more input files. Files are not always
8721 linked in order due to the way elf_link_input_bfd is
8722 called. See pr17666. */
8723 size_t sortlen = p - loc;
8724 bfd_vma r_off2 = (*ext_r_off) (loc);
8725 size_t runlen = elt_size;
8726 size_t buf_size = 96 * 1024;
8727 while (p + runlen < end
8728 && (sortlen <= buf_size
8729 || runlen + elt_size <= buf_size)
8730 && r_off2 > (*ext_r_off) (p + runlen))
8731 runlen += elt_size;
8732 if (buf == NULL)
8734 buf = bfd_malloc (buf_size);
8735 if (buf == NULL)
8736 return FALSE;
8738 if (runlen < sortlen)
8740 memcpy (buf, p, runlen);
8741 memmove (loc + runlen, loc, sortlen);
8742 memcpy (loc, buf, runlen);
8744 else
8746 memcpy (buf, loc, sortlen);
8747 memmove (loc, p, runlen);
8748 memcpy (loc + runlen, buf, sortlen);
8750 p += runlen - elt_size;
8753 /* Hashes are no longer valid. */
8754 free (reldata->hashes);
8755 reldata->hashes = NULL;
8756 free (buf);
8758 return TRUE;
8761 struct elf_link_sort_rela
8763 union {
8764 bfd_vma offset;
8765 bfd_vma sym_mask;
8766 } u;
8767 enum elf_reloc_type_class type;
8768 /* We use this as an array of size int_rels_per_ext_rel. */
8769 Elf_Internal_Rela rela[1];
8772 static int
8773 elf_link_sort_cmp1 (const void *A, const void *B)
8775 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8776 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8777 int relativea, relativeb;
8779 relativea = a->type == reloc_class_relative;
8780 relativeb = b->type == reloc_class_relative;
8782 if (relativea < relativeb)
8783 return 1;
8784 if (relativea > relativeb)
8785 return -1;
8786 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8787 return -1;
8788 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8789 return 1;
8790 if (a->rela->r_offset < b->rela->r_offset)
8791 return -1;
8792 if (a->rela->r_offset > b->rela->r_offset)
8793 return 1;
8794 return 0;
8797 static int
8798 elf_link_sort_cmp2 (const void *A, const void *B)
8800 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8801 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8803 if (a->type < b->type)
8804 return -1;
8805 if (a->type > b->type)
8806 return 1;
8807 if (a->u.offset < b->u.offset)
8808 return -1;
8809 if (a->u.offset > b->u.offset)
8810 return 1;
8811 if (a->rela->r_offset < b->rela->r_offset)
8812 return -1;
8813 if (a->rela->r_offset > b->rela->r_offset)
8814 return 1;
8815 return 0;
8818 static size_t
8819 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8821 asection *dynamic_relocs;
8822 asection *rela_dyn;
8823 asection *rel_dyn;
8824 bfd_size_type count, size;
8825 size_t i, ret, sort_elt, ext_size;
8826 bfd_byte *sort, *s_non_relative, *p;
8827 struct elf_link_sort_rela *sq;
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8829 int i2e = bed->s->int_rels_per_ext_rel;
8830 unsigned int opb = bfd_octets_per_byte (abfd);
8831 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8832 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8833 struct bfd_link_order *lo;
8834 bfd_vma r_sym_mask;
8835 bfd_boolean use_rela;
8837 /* Find a dynamic reloc section. */
8838 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8839 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8840 if (rela_dyn != NULL && rela_dyn->size > 0
8841 && rel_dyn != NULL && rel_dyn->size > 0)
8843 bfd_boolean use_rela_initialised = FALSE;
8845 /* This is just here to stop gcc from complaining.
8846 Its initialization checking code is not perfect. */
8847 use_rela = TRUE;
8849 /* Both sections are present. Examine the sizes
8850 of the indirect sections to help us choose. */
8851 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8852 if (lo->type == bfd_indirect_link_order)
8854 asection *o = lo->u.indirect.section;
8856 if ((o->size % bed->s->sizeof_rela) == 0)
8858 if ((o->size % bed->s->sizeof_rel) == 0)
8859 /* Section size is divisible by both rel and rela sizes.
8860 It is of no help to us. */
8862 else
8864 /* Section size is only divisible by rela. */
8865 if (use_rela_initialised && !use_rela)
8867 _bfd_error_handler (_("%B: Unable to sort relocs - "
8868 "they are in more than one size"),
8869 abfd);
8870 bfd_set_error (bfd_error_invalid_operation);
8871 return 0;
8873 else
8875 use_rela = TRUE;
8876 use_rela_initialised = TRUE;
8880 else if ((o->size % bed->s->sizeof_rel) == 0)
8882 /* Section size is only divisible by rel. */
8883 if (use_rela_initialised && use_rela)
8885 _bfd_error_handler (_("%B: Unable to sort relocs - "
8886 "they are in more than one size"),
8887 abfd);
8888 bfd_set_error (bfd_error_invalid_operation);
8889 return 0;
8891 else
8893 use_rela = FALSE;
8894 use_rela_initialised = TRUE;
8897 else
8899 /* The section size is not divisible by either -
8900 something is wrong. */
8901 _bfd_error_handler (_("%B: Unable to sort relocs - "
8902 "they are of an unknown size"), abfd);
8903 bfd_set_error (bfd_error_invalid_operation);
8904 return 0;
8908 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8909 if (lo->type == bfd_indirect_link_order)
8911 asection *o = lo->u.indirect.section;
8913 if ((o->size % bed->s->sizeof_rela) == 0)
8915 if ((o->size % bed->s->sizeof_rel) == 0)
8916 /* Section size is divisible by both rel and rela sizes.
8917 It is of no help to us. */
8919 else
8921 /* Section size is only divisible by rela. */
8922 if (use_rela_initialised && !use_rela)
8924 _bfd_error_handler (_("%B: Unable to sort relocs - "
8925 "they are in more than one size"),
8926 abfd);
8927 bfd_set_error (bfd_error_invalid_operation);
8928 return 0;
8930 else
8932 use_rela = TRUE;
8933 use_rela_initialised = TRUE;
8937 else if ((o->size % bed->s->sizeof_rel) == 0)
8939 /* Section size is only divisible by rel. */
8940 if (use_rela_initialised && use_rela)
8942 _bfd_error_handler (_("%B: Unable to sort relocs - "
8943 "they are in more than one size"),
8944 abfd);
8945 bfd_set_error (bfd_error_invalid_operation);
8946 return 0;
8948 else
8950 use_rela = FALSE;
8951 use_rela_initialised = TRUE;
8954 else
8956 /* The section size is not divisible by either -
8957 something is wrong. */
8958 _bfd_error_handler (_("%B: Unable to sort relocs - "
8959 "they are of an unknown size"), abfd);
8960 bfd_set_error (bfd_error_invalid_operation);
8961 return 0;
8965 if (! use_rela_initialised)
8966 /* Make a guess. */
8967 use_rela = TRUE;
8969 else if (rela_dyn != NULL && rela_dyn->size > 0)
8970 use_rela = TRUE;
8971 else if (rel_dyn != NULL && rel_dyn->size > 0)
8972 use_rela = FALSE;
8973 else
8974 return 0;
8976 if (use_rela)
8978 dynamic_relocs = rela_dyn;
8979 ext_size = bed->s->sizeof_rela;
8980 swap_in = bed->s->swap_reloca_in;
8981 swap_out = bed->s->swap_reloca_out;
8983 else
8985 dynamic_relocs = rel_dyn;
8986 ext_size = bed->s->sizeof_rel;
8987 swap_in = bed->s->swap_reloc_in;
8988 swap_out = bed->s->swap_reloc_out;
8991 size = 0;
8992 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8993 if (lo->type == bfd_indirect_link_order)
8994 size += lo->u.indirect.section->size;
8996 if (size != dynamic_relocs->size)
8997 return 0;
8999 sort_elt = (sizeof (struct elf_link_sort_rela)
9000 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9002 count = dynamic_relocs->size / ext_size;
9003 if (count == 0)
9004 return 0;
9005 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9007 if (sort == NULL)
9009 (*info->callbacks->warning)
9010 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9011 return 0;
9014 if (bed->s->arch_size == 32)
9015 r_sym_mask = ~(bfd_vma) 0xff;
9016 else
9017 r_sym_mask = ~(bfd_vma) 0xffffffff;
9019 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9020 if (lo->type == bfd_indirect_link_order)
9022 bfd_byte *erel, *erelend;
9023 asection *o = lo->u.indirect.section;
9025 if (o->contents == NULL && o->size != 0)
9027 /* This is a reloc section that is being handled as a normal
9028 section. See bfd_section_from_shdr. We can't combine
9029 relocs in this case. */
9030 free (sort);
9031 return 0;
9033 erel = o->contents;
9034 erelend = o->contents + o->size;
9035 p = sort + o->output_offset * opb / ext_size * sort_elt;
9037 while (erel < erelend)
9039 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9041 (*swap_in) (abfd, erel, s->rela);
9042 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9043 s->u.sym_mask = r_sym_mask;
9044 p += sort_elt;
9045 erel += ext_size;
9049 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9051 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9053 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9054 if (s->type != reloc_class_relative)
9055 break;
9057 ret = i;
9058 s_non_relative = p;
9060 sq = (struct elf_link_sort_rela *) s_non_relative;
9061 for (; i < count; i++, p += sort_elt)
9063 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9064 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9065 sq = sp;
9066 sp->u.offset = sq->rela->r_offset;
9069 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9071 struct elf_link_hash_table *htab = elf_hash_table (info);
9072 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9074 /* We have plt relocs in .rela.dyn. */
9075 sq = (struct elf_link_sort_rela *) sort;
9076 for (i = 0; i < count; i++)
9077 if (sq[count - i - 1].type != reloc_class_plt)
9078 break;
9079 if (i != 0 && htab->srelplt->size == i * ext_size)
9081 struct bfd_link_order **plo;
9082 /* Put srelplt link_order last. This is so the output_offset
9083 set in the next loop is correct for DT_JMPREL. */
9084 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9085 if ((*plo)->type == bfd_indirect_link_order
9086 && (*plo)->u.indirect.section == htab->srelplt)
9088 lo = *plo;
9089 *plo = lo->next;
9091 else
9092 plo = &(*plo)->next;
9093 *plo = lo;
9094 lo->next = NULL;
9095 dynamic_relocs->map_tail.link_order = lo;
9099 p = sort;
9100 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9101 if (lo->type == bfd_indirect_link_order)
9103 bfd_byte *erel, *erelend;
9104 asection *o = lo->u.indirect.section;
9106 erel = o->contents;
9107 erelend = o->contents + o->size;
9108 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9109 while (erel < erelend)
9111 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9112 (*swap_out) (abfd, s->rela, erel);
9113 p += sort_elt;
9114 erel += ext_size;
9118 free (sort);
9119 *psec = dynamic_relocs;
9120 return ret;
9123 /* Add a symbol to the output symbol string table. */
9125 static int
9126 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9127 const char *name,
9128 Elf_Internal_Sym *elfsym,
9129 asection *input_sec,
9130 struct elf_link_hash_entry *h)
9132 int (*output_symbol_hook)
9133 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9134 struct elf_link_hash_entry *);
9135 struct elf_link_hash_table *hash_table;
9136 const struct elf_backend_data *bed;
9137 bfd_size_type strtabsize;
9139 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9141 bed = get_elf_backend_data (flinfo->output_bfd);
9142 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9143 if (output_symbol_hook != NULL)
9145 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9146 if (ret != 1)
9147 return ret;
9150 if (name == NULL
9151 || *name == '\0'
9152 || (input_sec->flags & SEC_EXCLUDE))
9153 elfsym->st_name = (unsigned long) -1;
9154 else
9156 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9157 to get the final offset for st_name. */
9158 elfsym->st_name
9159 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9160 name, FALSE);
9161 if (elfsym->st_name == (unsigned long) -1)
9162 return 0;
9165 hash_table = elf_hash_table (flinfo->info);
9166 strtabsize = hash_table->strtabsize;
9167 if (strtabsize <= hash_table->strtabcount)
9169 strtabsize += strtabsize;
9170 hash_table->strtabsize = strtabsize;
9171 strtabsize *= sizeof (*hash_table->strtab);
9172 hash_table->strtab
9173 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9174 strtabsize);
9175 if (hash_table->strtab == NULL)
9176 return 0;
9178 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9179 hash_table->strtab[hash_table->strtabcount].dest_index
9180 = hash_table->strtabcount;
9181 hash_table->strtab[hash_table->strtabcount].destshndx_index
9182 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9184 bfd_get_symcount (flinfo->output_bfd) += 1;
9185 hash_table->strtabcount += 1;
9187 return 1;
9190 /* Swap symbols out to the symbol table and flush the output symbols to
9191 the file. */
9193 static bfd_boolean
9194 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9196 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9197 bfd_size_type amt;
9198 size_t i;
9199 const struct elf_backend_data *bed;
9200 bfd_byte *symbuf;
9201 Elf_Internal_Shdr *hdr;
9202 file_ptr pos;
9203 bfd_boolean ret;
9205 if (!hash_table->strtabcount)
9206 return TRUE;
9208 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9210 bed = get_elf_backend_data (flinfo->output_bfd);
9212 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9213 symbuf = (bfd_byte *) bfd_malloc (amt);
9214 if (symbuf == NULL)
9215 return FALSE;
9217 if (flinfo->symshndxbuf)
9219 amt = sizeof (Elf_External_Sym_Shndx);
9220 amt *= bfd_get_symcount (flinfo->output_bfd);
9221 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9222 if (flinfo->symshndxbuf == NULL)
9224 free (symbuf);
9225 return FALSE;
9229 for (i = 0; i < hash_table->strtabcount; i++)
9231 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9232 if (elfsym->sym.st_name == (unsigned long) -1)
9233 elfsym->sym.st_name = 0;
9234 else
9235 elfsym->sym.st_name
9236 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9237 elfsym->sym.st_name);
9238 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9239 ((bfd_byte *) symbuf
9240 + (elfsym->dest_index
9241 * bed->s->sizeof_sym)),
9242 (flinfo->symshndxbuf
9243 + elfsym->destshndx_index));
9246 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9247 pos = hdr->sh_offset + hdr->sh_size;
9248 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9249 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9250 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9252 hdr->sh_size += amt;
9253 ret = TRUE;
9255 else
9256 ret = FALSE;
9258 free (symbuf);
9260 free (hash_table->strtab);
9261 hash_table->strtab = NULL;
9263 return ret;
9266 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9268 static bfd_boolean
9269 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9271 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9272 && sym->st_shndx < SHN_LORESERVE)
9274 /* The gABI doesn't support dynamic symbols in output sections
9275 beyond 64k. */
9276 _bfd_error_handler
9277 /* xgettext:c-format */
9278 (_("%B: Too many sections: %d (>= %d)"),
9279 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9280 bfd_set_error (bfd_error_nonrepresentable_section);
9281 return FALSE;
9283 return TRUE;
9286 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9287 allowing an unsatisfied unversioned symbol in the DSO to match a
9288 versioned symbol that would normally require an explicit version.
9289 We also handle the case that a DSO references a hidden symbol
9290 which may be satisfied by a versioned symbol in another DSO. */
9292 static bfd_boolean
9293 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9294 const struct elf_backend_data *bed,
9295 struct elf_link_hash_entry *h)
9297 bfd *abfd;
9298 struct elf_link_loaded_list *loaded;
9300 if (!is_elf_hash_table (info->hash))
9301 return FALSE;
9303 /* Check indirect symbol. */
9304 while (h->root.type == bfd_link_hash_indirect)
9305 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9307 switch (h->root.type)
9309 default:
9310 abfd = NULL;
9311 break;
9313 case bfd_link_hash_undefined:
9314 case bfd_link_hash_undefweak:
9315 abfd = h->root.u.undef.abfd;
9316 if (abfd == NULL
9317 || (abfd->flags & DYNAMIC) == 0
9318 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9319 return FALSE;
9320 break;
9322 case bfd_link_hash_defined:
9323 case bfd_link_hash_defweak:
9324 abfd = h->root.u.def.section->owner;
9325 break;
9327 case bfd_link_hash_common:
9328 abfd = h->root.u.c.p->section->owner;
9329 break;
9331 BFD_ASSERT (abfd != NULL);
9333 for (loaded = elf_hash_table (info)->loaded;
9334 loaded != NULL;
9335 loaded = loaded->next)
9337 bfd *input;
9338 Elf_Internal_Shdr *hdr;
9339 size_t symcount;
9340 size_t extsymcount;
9341 size_t extsymoff;
9342 Elf_Internal_Shdr *versymhdr;
9343 Elf_Internal_Sym *isym;
9344 Elf_Internal_Sym *isymend;
9345 Elf_Internal_Sym *isymbuf;
9346 Elf_External_Versym *ever;
9347 Elf_External_Versym *extversym;
9349 input = loaded->abfd;
9351 /* We check each DSO for a possible hidden versioned definition. */
9352 if (input == abfd
9353 || (input->flags & DYNAMIC) == 0
9354 || elf_dynversym (input) == 0)
9355 continue;
9357 hdr = &elf_tdata (input)->dynsymtab_hdr;
9359 symcount = hdr->sh_size / bed->s->sizeof_sym;
9360 if (elf_bad_symtab (input))
9362 extsymcount = symcount;
9363 extsymoff = 0;
9365 else
9367 extsymcount = symcount - hdr->sh_info;
9368 extsymoff = hdr->sh_info;
9371 if (extsymcount == 0)
9372 continue;
9374 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9375 NULL, NULL, NULL);
9376 if (isymbuf == NULL)
9377 return FALSE;
9379 /* Read in any version definitions. */
9380 versymhdr = &elf_tdata (input)->dynversym_hdr;
9381 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9382 if (extversym == NULL)
9383 goto error_ret;
9385 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9386 || (bfd_bread (extversym, versymhdr->sh_size, input)
9387 != versymhdr->sh_size))
9389 free (extversym);
9390 error_ret:
9391 free (isymbuf);
9392 return FALSE;
9395 ever = extversym + extsymoff;
9396 isymend = isymbuf + extsymcount;
9397 for (isym = isymbuf; isym < isymend; isym++, ever++)
9399 const char *name;
9400 Elf_Internal_Versym iver;
9401 unsigned short version_index;
9403 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9404 || isym->st_shndx == SHN_UNDEF)
9405 continue;
9407 name = bfd_elf_string_from_elf_section (input,
9408 hdr->sh_link,
9409 isym->st_name);
9410 if (strcmp (name, h->root.root.string) != 0)
9411 continue;
9413 _bfd_elf_swap_versym_in (input, ever, &iver);
9415 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9416 && !(h->def_regular
9417 && h->forced_local))
9419 /* If we have a non-hidden versioned sym, then it should
9420 have provided a definition for the undefined sym unless
9421 it is defined in a non-shared object and forced local.
9423 abort ();
9426 version_index = iver.vs_vers & VERSYM_VERSION;
9427 if (version_index == 1 || version_index == 2)
9429 /* This is the base or first version. We can use it. */
9430 free (extversym);
9431 free (isymbuf);
9432 return TRUE;
9436 free (extversym);
9437 free (isymbuf);
9440 return FALSE;
9443 /* Convert ELF common symbol TYPE. */
9445 static int
9446 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9448 /* Commom symbol can only appear in relocatable link. */
9449 if (!bfd_link_relocatable (info))
9450 abort ();
9451 switch (info->elf_stt_common)
9453 case unchanged:
9454 break;
9455 case elf_stt_common:
9456 type = STT_COMMON;
9457 break;
9458 case no_elf_stt_common:
9459 type = STT_OBJECT;
9460 break;
9462 return type;
9465 /* Add an external symbol to the symbol table. This is called from
9466 the hash table traversal routine. When generating a shared object,
9467 we go through the symbol table twice. The first time we output
9468 anything that might have been forced to local scope in a version
9469 script. The second time we output the symbols that are still
9470 global symbols. */
9472 static bfd_boolean
9473 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9475 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9476 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9477 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9478 bfd_boolean strip;
9479 Elf_Internal_Sym sym;
9480 asection *input_sec;
9481 const struct elf_backend_data *bed;
9482 long indx;
9483 int ret;
9484 unsigned int type;
9486 if (h->root.type == bfd_link_hash_warning)
9488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9489 if (h->root.type == bfd_link_hash_new)
9490 return TRUE;
9493 /* Decide whether to output this symbol in this pass. */
9494 if (eoinfo->localsyms)
9496 if (!h->forced_local)
9497 return TRUE;
9499 else
9501 if (h->forced_local)
9502 return TRUE;
9505 bed = get_elf_backend_data (flinfo->output_bfd);
9507 if (h->root.type == bfd_link_hash_undefined)
9509 /* If we have an undefined symbol reference here then it must have
9510 come from a shared library that is being linked in. (Undefined
9511 references in regular files have already been handled unless
9512 they are in unreferenced sections which are removed by garbage
9513 collection). */
9514 bfd_boolean ignore_undef = FALSE;
9516 /* Some symbols may be special in that the fact that they're
9517 undefined can be safely ignored - let backend determine that. */
9518 if (bed->elf_backend_ignore_undef_symbol)
9519 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9521 /* If we are reporting errors for this situation then do so now. */
9522 if (!ignore_undef
9523 && h->ref_dynamic
9524 && (!h->ref_regular || flinfo->info->gc_sections)
9525 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9526 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9527 (*flinfo->info->callbacks->undefined_symbol)
9528 (flinfo->info, h->root.root.string,
9529 h->ref_regular ? NULL : h->root.u.undef.abfd,
9530 NULL, 0,
9531 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9533 /* Strip a global symbol defined in a discarded section. */
9534 if (h->indx == -3)
9535 return TRUE;
9538 /* We should also warn if a forced local symbol is referenced from
9539 shared libraries. */
9540 if (bfd_link_executable (flinfo->info)
9541 && h->forced_local
9542 && h->ref_dynamic
9543 && h->def_regular
9544 && !h->dynamic_def
9545 && h->ref_dynamic_nonweak
9546 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9548 bfd *def_bfd;
9549 const char *msg;
9550 struct elf_link_hash_entry *hi = h;
9552 /* Check indirect symbol. */
9553 while (hi->root.type == bfd_link_hash_indirect)
9554 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9556 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9557 /* xgettext:c-format */
9558 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9559 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9560 /* xgettext:c-format */
9561 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9562 else
9563 /* xgettext:c-format */
9564 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9565 def_bfd = flinfo->output_bfd;
9566 if (hi->root.u.def.section != bfd_abs_section_ptr)
9567 def_bfd = hi->root.u.def.section->owner;
9568 _bfd_error_handler (msg, flinfo->output_bfd,
9569 h->root.root.string, def_bfd);
9570 bfd_set_error (bfd_error_bad_value);
9571 eoinfo->failed = TRUE;
9572 return FALSE;
9575 /* We don't want to output symbols that have never been mentioned by
9576 a regular file, or that we have been told to strip. However, if
9577 h->indx is set to -2, the symbol is used by a reloc and we must
9578 output it. */
9579 strip = FALSE;
9580 if (h->indx == -2)
9582 else if ((h->def_dynamic
9583 || h->ref_dynamic
9584 || h->root.type == bfd_link_hash_new)
9585 && !h->def_regular
9586 && !h->ref_regular)
9587 strip = TRUE;
9588 else if (flinfo->info->strip == strip_all)
9589 strip = TRUE;
9590 else if (flinfo->info->strip == strip_some
9591 && bfd_hash_lookup (flinfo->info->keep_hash,
9592 h->root.root.string, FALSE, FALSE) == NULL)
9593 strip = TRUE;
9594 else if ((h->root.type == bfd_link_hash_defined
9595 || h->root.type == bfd_link_hash_defweak)
9596 && ((flinfo->info->strip_discarded
9597 && discarded_section (h->root.u.def.section))
9598 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9599 && h->root.u.def.section->owner != NULL
9600 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9601 strip = TRUE;
9602 else if ((h->root.type == bfd_link_hash_undefined
9603 || h->root.type == bfd_link_hash_undefweak)
9604 && h->root.u.undef.abfd != NULL
9605 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9606 strip = TRUE;
9608 type = h->type;
9610 /* If we're stripping it, and it's not a dynamic symbol, there's
9611 nothing else to do. However, if it is a forced local symbol or
9612 an ifunc symbol we need to give the backend finish_dynamic_symbol
9613 function a chance to make it dynamic. */
9614 if (strip
9615 && h->dynindx == -1
9616 && type != STT_GNU_IFUNC
9617 && !h->forced_local)
9618 return TRUE;
9620 sym.st_value = 0;
9621 sym.st_size = h->size;
9622 sym.st_other = h->other;
9623 switch (h->root.type)
9625 default:
9626 case bfd_link_hash_new:
9627 case bfd_link_hash_warning:
9628 abort ();
9629 return FALSE;
9631 case bfd_link_hash_undefined:
9632 case bfd_link_hash_undefweak:
9633 input_sec = bfd_und_section_ptr;
9634 sym.st_shndx = SHN_UNDEF;
9635 break;
9637 case bfd_link_hash_defined:
9638 case bfd_link_hash_defweak:
9640 input_sec = h->root.u.def.section;
9641 if (input_sec->output_section != NULL)
9643 sym.st_shndx =
9644 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9645 input_sec->output_section);
9646 if (sym.st_shndx == SHN_BAD)
9648 _bfd_error_handler
9649 /* xgettext:c-format */
9650 (_("%B: could not find output section %A for input section %A"),
9651 flinfo->output_bfd, input_sec->output_section, input_sec);
9652 bfd_set_error (bfd_error_nonrepresentable_section);
9653 eoinfo->failed = TRUE;
9654 return FALSE;
9657 /* ELF symbols in relocatable files are section relative,
9658 but in nonrelocatable files they are virtual
9659 addresses. */
9660 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9661 if (!bfd_link_relocatable (flinfo->info))
9663 sym.st_value += input_sec->output_section->vma;
9664 if (h->type == STT_TLS)
9666 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9667 if (tls_sec != NULL)
9668 sym.st_value -= tls_sec->vma;
9672 else
9674 BFD_ASSERT (input_sec->owner == NULL
9675 || (input_sec->owner->flags & DYNAMIC) != 0);
9676 sym.st_shndx = SHN_UNDEF;
9677 input_sec = bfd_und_section_ptr;
9680 break;
9682 case bfd_link_hash_common:
9683 input_sec = h->root.u.c.p->section;
9684 sym.st_shndx = bed->common_section_index (input_sec);
9685 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9686 break;
9688 case bfd_link_hash_indirect:
9689 /* These symbols are created by symbol versioning. They point
9690 to the decorated version of the name. For example, if the
9691 symbol foo@@GNU_1.2 is the default, which should be used when
9692 foo is used with no version, then we add an indirect symbol
9693 foo which points to foo@@GNU_1.2. We ignore these symbols,
9694 since the indirected symbol is already in the hash table. */
9695 return TRUE;
9698 if (type == STT_COMMON || type == STT_OBJECT)
9699 switch (h->root.type)
9701 case bfd_link_hash_common:
9702 type = elf_link_convert_common_type (flinfo->info, type);
9703 break;
9704 case bfd_link_hash_defined:
9705 case bfd_link_hash_defweak:
9706 if (bed->common_definition (&sym))
9707 type = elf_link_convert_common_type (flinfo->info, type);
9708 else
9709 type = STT_OBJECT;
9710 break;
9711 case bfd_link_hash_undefined:
9712 case bfd_link_hash_undefweak:
9713 break;
9714 default:
9715 abort ();
9718 if (h->forced_local)
9720 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9721 /* Turn off visibility on local symbol. */
9722 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9724 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9725 else if (h->unique_global && h->def_regular)
9726 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9727 else if (h->root.type == bfd_link_hash_undefweak
9728 || h->root.type == bfd_link_hash_defweak)
9729 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9730 else
9731 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9732 sym.st_target_internal = h->target_internal;
9734 /* Give the processor backend a chance to tweak the symbol value,
9735 and also to finish up anything that needs to be done for this
9736 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9737 forced local syms when non-shared is due to a historical quirk.
9738 STT_GNU_IFUNC symbol must go through PLT. */
9739 if ((h->type == STT_GNU_IFUNC
9740 && h->def_regular
9741 && !bfd_link_relocatable (flinfo->info))
9742 || ((h->dynindx != -1
9743 || h->forced_local)
9744 && ((bfd_link_pic (flinfo->info)
9745 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9746 || h->root.type != bfd_link_hash_undefweak))
9747 || !h->forced_local)
9748 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9750 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9751 (flinfo->output_bfd, flinfo->info, h, &sym)))
9753 eoinfo->failed = TRUE;
9754 return FALSE;
9758 /* If we are marking the symbol as undefined, and there are no
9759 non-weak references to this symbol from a regular object, then
9760 mark the symbol as weak undefined; if there are non-weak
9761 references, mark the symbol as strong. We can't do this earlier,
9762 because it might not be marked as undefined until the
9763 finish_dynamic_symbol routine gets through with it. */
9764 if (sym.st_shndx == SHN_UNDEF
9765 && h->ref_regular
9766 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9767 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9769 int bindtype;
9770 type = ELF_ST_TYPE (sym.st_info);
9772 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9773 if (type == STT_GNU_IFUNC)
9774 type = STT_FUNC;
9776 if (h->ref_regular_nonweak)
9777 bindtype = STB_GLOBAL;
9778 else
9779 bindtype = STB_WEAK;
9780 sym.st_info = ELF_ST_INFO (bindtype, type);
9783 /* If this is a symbol defined in a dynamic library, don't use the
9784 symbol size from the dynamic library. Relinking an executable
9785 against a new library may introduce gratuitous changes in the
9786 executable's symbols if we keep the size. */
9787 if (sym.st_shndx == SHN_UNDEF
9788 && !h->def_regular
9789 && h->def_dynamic)
9790 sym.st_size = 0;
9792 /* If a non-weak symbol with non-default visibility is not defined
9793 locally, it is a fatal error. */
9794 if (!bfd_link_relocatable (flinfo->info)
9795 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9796 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9797 && h->root.type == bfd_link_hash_undefined
9798 && !h->def_regular)
9800 const char *msg;
9802 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9803 /* xgettext:c-format */
9804 msg = _("%B: protected symbol `%s' isn't defined");
9805 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9806 /* xgettext:c-format */
9807 msg = _("%B: internal symbol `%s' isn't defined");
9808 else
9809 /* xgettext:c-format */
9810 msg = _("%B: hidden symbol `%s' isn't defined");
9811 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9812 bfd_set_error (bfd_error_bad_value);
9813 eoinfo->failed = TRUE;
9814 return FALSE;
9817 /* If this symbol should be put in the .dynsym section, then put it
9818 there now. We already know the symbol index. We also fill in
9819 the entry in the .hash section. */
9820 if (elf_hash_table (flinfo->info)->dynsym != NULL
9821 && h->dynindx != -1
9822 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9824 bfd_byte *esym;
9826 /* Since there is no version information in the dynamic string,
9827 if there is no version info in symbol version section, we will
9828 have a run-time problem if not linking executable, referenced
9829 by shared library, or not bound locally. */
9830 if (h->verinfo.verdef == NULL
9831 && (!bfd_link_executable (flinfo->info)
9832 || h->ref_dynamic
9833 || !h->def_regular))
9835 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9837 if (p && p [1] != '\0')
9839 _bfd_error_handler
9840 /* xgettext:c-format */
9841 (_("%B: No symbol version section for versioned symbol `%s'"),
9842 flinfo->output_bfd, h->root.root.string);
9843 eoinfo->failed = TRUE;
9844 return FALSE;
9848 sym.st_name = h->dynstr_index;
9849 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9850 + h->dynindx * bed->s->sizeof_sym);
9851 if (!check_dynsym (flinfo->output_bfd, &sym))
9853 eoinfo->failed = TRUE;
9854 return FALSE;
9856 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9858 if (flinfo->hash_sec != NULL)
9860 size_t hash_entry_size;
9861 bfd_byte *bucketpos;
9862 bfd_vma chain;
9863 size_t bucketcount;
9864 size_t bucket;
9866 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9867 bucket = h->u.elf_hash_value % bucketcount;
9869 hash_entry_size
9870 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9871 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9872 + (bucket + 2) * hash_entry_size);
9873 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9874 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9875 bucketpos);
9876 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9877 ((bfd_byte *) flinfo->hash_sec->contents
9878 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9881 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9883 Elf_Internal_Versym iversym;
9884 Elf_External_Versym *eversym;
9886 if (!h->def_regular)
9888 if (h->verinfo.verdef == NULL
9889 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9890 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9891 iversym.vs_vers = 0;
9892 else
9893 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9895 else
9897 if (h->verinfo.vertree == NULL)
9898 iversym.vs_vers = 1;
9899 else
9900 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9901 if (flinfo->info->create_default_symver)
9902 iversym.vs_vers++;
9905 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9906 defined locally. */
9907 if (h->versioned == versioned_hidden && h->def_regular)
9908 iversym.vs_vers |= VERSYM_HIDDEN;
9910 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9911 eversym += h->dynindx;
9912 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9916 /* If the symbol is undefined, and we didn't output it to .dynsym,
9917 strip it from .symtab too. Obviously we can't do this for
9918 relocatable output or when needed for --emit-relocs. */
9919 else if (input_sec == bfd_und_section_ptr
9920 && h->indx != -2
9921 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9922 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
9923 && !bfd_link_relocatable (flinfo->info))
9924 return TRUE;
9926 /* Also strip others that we couldn't earlier due to dynamic symbol
9927 processing. */
9928 if (strip)
9929 return TRUE;
9930 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9931 return TRUE;
9933 /* Output a FILE symbol so that following locals are not associated
9934 with the wrong input file. We need one for forced local symbols
9935 if we've seen more than one FILE symbol or when we have exactly
9936 one FILE symbol but global symbols are present in a file other
9937 than the one with the FILE symbol. We also need one if linker
9938 defined symbols are present. In practice these conditions are
9939 always met, so just emit the FILE symbol unconditionally. */
9940 if (eoinfo->localsyms
9941 && !eoinfo->file_sym_done
9942 && eoinfo->flinfo->filesym_count != 0)
9944 Elf_Internal_Sym fsym;
9946 memset (&fsym, 0, sizeof (fsym));
9947 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9948 fsym.st_shndx = SHN_ABS;
9949 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9950 bfd_und_section_ptr, NULL))
9951 return FALSE;
9953 eoinfo->file_sym_done = TRUE;
9956 indx = bfd_get_symcount (flinfo->output_bfd);
9957 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9958 input_sec, h);
9959 if (ret == 0)
9961 eoinfo->failed = TRUE;
9962 return FALSE;
9964 else if (ret == 1)
9965 h->indx = indx;
9966 else if (h->indx == -2)
9967 abort();
9969 return TRUE;
9972 /* Return TRUE if special handling is done for relocs in SEC against
9973 symbols defined in discarded sections. */
9975 static bfd_boolean
9976 elf_section_ignore_discarded_relocs (asection *sec)
9978 const struct elf_backend_data *bed;
9980 switch (sec->sec_info_type)
9982 case SEC_INFO_TYPE_STABS:
9983 case SEC_INFO_TYPE_EH_FRAME:
9984 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9985 return TRUE;
9986 default:
9987 break;
9990 bed = get_elf_backend_data (sec->owner);
9991 if (bed->elf_backend_ignore_discarded_relocs != NULL
9992 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9993 return TRUE;
9995 return FALSE;
9998 /* Return a mask saying how ld should treat relocations in SEC against
9999 symbols defined in discarded sections. If this function returns
10000 COMPLAIN set, ld will issue a warning message. If this function
10001 returns PRETEND set, and the discarded section was link-once and the
10002 same size as the kept link-once section, ld will pretend that the
10003 symbol was actually defined in the kept section. Otherwise ld will
10004 zero the reloc (at least that is the intent, but some cooperation by
10005 the target dependent code is needed, particularly for REL targets). */
10007 unsigned int
10008 _bfd_elf_default_action_discarded (asection *sec)
10010 if (sec->flags & SEC_DEBUGGING)
10011 return PRETEND;
10013 if (strcmp (".eh_frame", sec->name) == 0)
10014 return 0;
10016 if (strcmp (".gcc_except_table", sec->name) == 0)
10017 return 0;
10019 return COMPLAIN | PRETEND;
10022 /* Find a match between a section and a member of a section group. */
10024 static asection *
10025 match_group_member (asection *sec, asection *group,
10026 struct bfd_link_info *info)
10028 asection *first = elf_next_in_group (group);
10029 asection *s = first;
10031 while (s != NULL)
10033 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10034 return s;
10036 s = elf_next_in_group (s);
10037 if (s == first)
10038 break;
10041 return NULL;
10044 /* Check if the kept section of a discarded section SEC can be used
10045 to replace it. Return the replacement if it is OK. Otherwise return
10046 NULL. */
10048 asection *
10049 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10051 asection *kept;
10053 kept = sec->kept_section;
10054 if (kept != NULL)
10056 if ((kept->flags & SEC_GROUP) != 0)
10057 kept = match_group_member (sec, kept, info);
10058 if (kept != NULL
10059 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10060 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10061 kept = NULL;
10062 sec->kept_section = kept;
10064 return kept;
10067 /* Link an input file into the linker output file. This function
10068 handles all the sections and relocations of the input file at once.
10069 This is so that we only have to read the local symbols once, and
10070 don't have to keep them in memory. */
10072 static bfd_boolean
10073 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10075 int (*relocate_section)
10076 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10077 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10078 bfd *output_bfd;
10079 Elf_Internal_Shdr *symtab_hdr;
10080 size_t locsymcount;
10081 size_t extsymoff;
10082 Elf_Internal_Sym *isymbuf;
10083 Elf_Internal_Sym *isym;
10084 Elf_Internal_Sym *isymend;
10085 long *pindex;
10086 asection **ppsection;
10087 asection *o;
10088 const struct elf_backend_data *bed;
10089 struct elf_link_hash_entry **sym_hashes;
10090 bfd_size_type address_size;
10091 bfd_vma r_type_mask;
10092 int r_sym_shift;
10093 bfd_boolean have_file_sym = FALSE;
10095 output_bfd = flinfo->output_bfd;
10096 bed = get_elf_backend_data (output_bfd);
10097 relocate_section = bed->elf_backend_relocate_section;
10099 /* If this is a dynamic object, we don't want to do anything here:
10100 we don't want the local symbols, and we don't want the section
10101 contents. */
10102 if ((input_bfd->flags & DYNAMIC) != 0)
10103 return TRUE;
10105 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10106 if (elf_bad_symtab (input_bfd))
10108 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10109 extsymoff = 0;
10111 else
10113 locsymcount = symtab_hdr->sh_info;
10114 extsymoff = symtab_hdr->sh_info;
10117 /* Read the local symbols. */
10118 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10119 if (isymbuf == NULL && locsymcount != 0)
10121 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10122 flinfo->internal_syms,
10123 flinfo->external_syms,
10124 flinfo->locsym_shndx);
10125 if (isymbuf == NULL)
10126 return FALSE;
10129 /* Find local symbol sections and adjust values of symbols in
10130 SEC_MERGE sections. Write out those local symbols we know are
10131 going into the output file. */
10132 isymend = isymbuf + locsymcount;
10133 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10134 isym < isymend;
10135 isym++, pindex++, ppsection++)
10137 asection *isec;
10138 const char *name;
10139 Elf_Internal_Sym osym;
10140 long indx;
10141 int ret;
10143 *pindex = -1;
10145 if (elf_bad_symtab (input_bfd))
10147 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10149 *ppsection = NULL;
10150 continue;
10154 if (isym->st_shndx == SHN_UNDEF)
10155 isec = bfd_und_section_ptr;
10156 else if (isym->st_shndx == SHN_ABS)
10157 isec = bfd_abs_section_ptr;
10158 else if (isym->st_shndx == SHN_COMMON)
10159 isec = bfd_com_section_ptr;
10160 else
10162 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10163 if (isec == NULL)
10165 /* Don't attempt to output symbols with st_shnx in the
10166 reserved range other than SHN_ABS and SHN_COMMON. */
10167 *ppsection = NULL;
10168 continue;
10170 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10171 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10172 isym->st_value =
10173 _bfd_merged_section_offset (output_bfd, &isec,
10174 elf_section_data (isec)->sec_info,
10175 isym->st_value);
10178 *ppsection = isec;
10180 /* Don't output the first, undefined, symbol. In fact, don't
10181 output any undefined local symbol. */
10182 if (isec == bfd_und_section_ptr)
10183 continue;
10185 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10187 /* We never output section symbols. Instead, we use the
10188 section symbol of the corresponding section in the output
10189 file. */
10190 continue;
10193 /* If we are stripping all symbols, we don't want to output this
10194 one. */
10195 if (flinfo->info->strip == strip_all)
10196 continue;
10198 /* If we are discarding all local symbols, we don't want to
10199 output this one. If we are generating a relocatable output
10200 file, then some of the local symbols may be required by
10201 relocs; we output them below as we discover that they are
10202 needed. */
10203 if (flinfo->info->discard == discard_all)
10204 continue;
10206 /* If this symbol is defined in a section which we are
10207 discarding, we don't need to keep it. */
10208 if (isym->st_shndx != SHN_UNDEF
10209 && isym->st_shndx < SHN_LORESERVE
10210 && bfd_section_removed_from_list (output_bfd,
10211 isec->output_section))
10212 continue;
10214 /* Get the name of the symbol. */
10215 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10216 isym->st_name);
10217 if (name == NULL)
10218 return FALSE;
10220 /* See if we are discarding symbols with this name. */
10221 if ((flinfo->info->strip == strip_some
10222 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10223 == NULL))
10224 || (((flinfo->info->discard == discard_sec_merge
10225 && (isec->flags & SEC_MERGE)
10226 && !bfd_link_relocatable (flinfo->info))
10227 || flinfo->info->discard == discard_l)
10228 && bfd_is_local_label_name (input_bfd, name)))
10229 continue;
10231 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10233 if (input_bfd->lto_output)
10234 /* -flto puts a temp file name here. This means builds
10235 are not reproducible. Discard the symbol. */
10236 continue;
10237 have_file_sym = TRUE;
10238 flinfo->filesym_count += 1;
10240 if (!have_file_sym)
10242 /* In the absence of debug info, bfd_find_nearest_line uses
10243 FILE symbols to determine the source file for local
10244 function symbols. Provide a FILE symbol here if input
10245 files lack such, so that their symbols won't be
10246 associated with a previous input file. It's not the
10247 source file, but the best we can do. */
10248 have_file_sym = TRUE;
10249 flinfo->filesym_count += 1;
10250 memset (&osym, 0, sizeof (osym));
10251 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10252 osym.st_shndx = SHN_ABS;
10253 if (!elf_link_output_symstrtab (flinfo,
10254 (input_bfd->lto_output ? NULL
10255 : input_bfd->filename),
10256 &osym, bfd_abs_section_ptr,
10257 NULL))
10258 return FALSE;
10261 osym = *isym;
10263 /* Adjust the section index for the output file. */
10264 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10265 isec->output_section);
10266 if (osym.st_shndx == SHN_BAD)
10267 return FALSE;
10269 /* ELF symbols in relocatable files are section relative, but
10270 in executable files they are virtual addresses. Note that
10271 this code assumes that all ELF sections have an associated
10272 BFD section with a reasonable value for output_offset; below
10273 we assume that they also have a reasonable value for
10274 output_section. Any special sections must be set up to meet
10275 these requirements. */
10276 osym.st_value += isec->output_offset;
10277 if (!bfd_link_relocatable (flinfo->info))
10279 osym.st_value += isec->output_section->vma;
10280 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10282 /* STT_TLS symbols are relative to PT_TLS segment base. */
10283 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10284 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10288 indx = bfd_get_symcount (output_bfd);
10289 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10290 if (ret == 0)
10291 return FALSE;
10292 else if (ret == 1)
10293 *pindex = indx;
10296 if (bed->s->arch_size == 32)
10298 r_type_mask = 0xff;
10299 r_sym_shift = 8;
10300 address_size = 4;
10302 else
10304 r_type_mask = 0xffffffff;
10305 r_sym_shift = 32;
10306 address_size = 8;
10309 /* Relocate the contents of each section. */
10310 sym_hashes = elf_sym_hashes (input_bfd);
10311 for (o = input_bfd->sections; o != NULL; o = o->next)
10313 bfd_byte *contents;
10315 if (! o->linker_mark)
10317 /* This section was omitted from the link. */
10318 continue;
10321 if (!flinfo->info->resolve_section_groups
10322 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10324 /* Deal with the group signature symbol. */
10325 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10326 unsigned long symndx = sec_data->this_hdr.sh_info;
10327 asection *osec = o->output_section;
10329 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10330 if (symndx >= locsymcount
10331 || (elf_bad_symtab (input_bfd)
10332 && flinfo->sections[symndx] == NULL))
10334 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10335 while (h->root.type == bfd_link_hash_indirect
10336 || h->root.type == bfd_link_hash_warning)
10337 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10338 /* Arrange for symbol to be output. */
10339 h->indx = -2;
10340 elf_section_data (osec)->this_hdr.sh_info = -2;
10342 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10344 /* We'll use the output section target_index. */
10345 asection *sec = flinfo->sections[symndx]->output_section;
10346 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10348 else
10350 if (flinfo->indices[symndx] == -1)
10352 /* Otherwise output the local symbol now. */
10353 Elf_Internal_Sym sym = isymbuf[symndx];
10354 asection *sec = flinfo->sections[symndx]->output_section;
10355 const char *name;
10356 long indx;
10357 int ret;
10359 name = bfd_elf_string_from_elf_section (input_bfd,
10360 symtab_hdr->sh_link,
10361 sym.st_name);
10362 if (name == NULL)
10363 return FALSE;
10365 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10366 sec);
10367 if (sym.st_shndx == SHN_BAD)
10368 return FALSE;
10370 sym.st_value += o->output_offset;
10372 indx = bfd_get_symcount (output_bfd);
10373 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10374 NULL);
10375 if (ret == 0)
10376 return FALSE;
10377 else if (ret == 1)
10378 flinfo->indices[symndx] = indx;
10379 else
10380 abort ();
10382 elf_section_data (osec)->this_hdr.sh_info
10383 = flinfo->indices[symndx];
10387 if ((o->flags & SEC_HAS_CONTENTS) == 0
10388 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10389 continue;
10391 if ((o->flags & SEC_LINKER_CREATED) != 0)
10393 /* Section was created by _bfd_elf_link_create_dynamic_sections
10394 or somesuch. */
10395 continue;
10398 /* Get the contents of the section. They have been cached by a
10399 relaxation routine. Note that o is a section in an input
10400 file, so the contents field will not have been set by any of
10401 the routines which work on output files. */
10402 if (elf_section_data (o)->this_hdr.contents != NULL)
10404 contents = elf_section_data (o)->this_hdr.contents;
10405 if (bed->caches_rawsize
10406 && o->rawsize != 0
10407 && o->rawsize < o->size)
10409 memcpy (flinfo->contents, contents, o->rawsize);
10410 contents = flinfo->contents;
10413 else
10415 contents = flinfo->contents;
10416 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10417 return FALSE;
10420 if ((o->flags & SEC_RELOC) != 0)
10422 Elf_Internal_Rela *internal_relocs;
10423 Elf_Internal_Rela *rel, *relend;
10424 int action_discarded;
10425 int ret;
10427 /* Get the swapped relocs. */
10428 internal_relocs
10429 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10430 flinfo->internal_relocs, FALSE);
10431 if (internal_relocs == NULL
10432 && o->reloc_count > 0)
10433 return FALSE;
10435 /* We need to reverse-copy input .ctors/.dtors sections if
10436 they are placed in .init_array/.finit_array for output. */
10437 if (o->size > address_size
10438 && ((strncmp (o->name, ".ctors", 6) == 0
10439 && strcmp (o->output_section->name,
10440 ".init_array") == 0)
10441 || (strncmp (o->name, ".dtors", 6) == 0
10442 && strcmp (o->output_section->name,
10443 ".fini_array") == 0))
10444 && (o->name[6] == 0 || o->name[6] == '.'))
10446 if (o->size * bed->s->int_rels_per_ext_rel
10447 != o->reloc_count * address_size)
10449 _bfd_error_handler
10450 /* xgettext:c-format */
10451 (_("error: %B: size of section %A is not "
10452 "multiple of address size"),
10453 input_bfd, o);
10454 bfd_set_error (bfd_error_bad_value);
10455 return FALSE;
10457 o->flags |= SEC_ELF_REVERSE_COPY;
10460 action_discarded = -1;
10461 if (!elf_section_ignore_discarded_relocs (o))
10462 action_discarded = (*bed->action_discarded) (o);
10464 /* Run through the relocs evaluating complex reloc symbols and
10465 looking for relocs against symbols from discarded sections
10466 or section symbols from removed link-once sections.
10467 Complain about relocs against discarded sections. Zero
10468 relocs against removed link-once sections. */
10470 rel = internal_relocs;
10471 relend = rel + o->reloc_count;
10472 for ( ; rel < relend; rel++)
10474 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10475 unsigned int s_type;
10476 asection **ps, *sec;
10477 struct elf_link_hash_entry *h = NULL;
10478 const char *sym_name;
10480 if (r_symndx == STN_UNDEF)
10481 continue;
10483 if (r_symndx >= locsymcount
10484 || (elf_bad_symtab (input_bfd)
10485 && flinfo->sections[r_symndx] == NULL))
10487 h = sym_hashes[r_symndx - extsymoff];
10489 /* Badly formatted input files can contain relocs that
10490 reference non-existant symbols. Check here so that
10491 we do not seg fault. */
10492 if (h == NULL)
10494 _bfd_error_handler
10495 /* xgettext:c-format */
10496 (_("error: %B contains a reloc (%#Lx) for section %A "
10497 "that references a non-existent global symbol"),
10498 input_bfd, rel->r_info, o);
10499 bfd_set_error (bfd_error_bad_value);
10500 return FALSE;
10503 while (h->root.type == bfd_link_hash_indirect
10504 || h->root.type == bfd_link_hash_warning)
10505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10507 s_type = h->type;
10509 /* If a plugin symbol is referenced from a non-IR file,
10510 mark the symbol as undefined. Note that the
10511 linker may attach linker created dynamic sections
10512 to the plugin bfd. Symbols defined in linker
10513 created sections are not plugin symbols. */
10514 if ((h->root.non_ir_ref_regular
10515 || h->root.non_ir_ref_dynamic)
10516 && (h->root.type == bfd_link_hash_defined
10517 || h->root.type == bfd_link_hash_defweak)
10518 && (h->root.u.def.section->flags
10519 & SEC_LINKER_CREATED) == 0
10520 && h->root.u.def.section->owner != NULL
10521 && (h->root.u.def.section->owner->flags
10522 & BFD_PLUGIN) != 0)
10524 h->root.type = bfd_link_hash_undefined;
10525 h->root.u.undef.abfd = h->root.u.def.section->owner;
10528 ps = NULL;
10529 if (h->root.type == bfd_link_hash_defined
10530 || h->root.type == bfd_link_hash_defweak)
10531 ps = &h->root.u.def.section;
10533 sym_name = h->root.root.string;
10535 else
10537 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10539 s_type = ELF_ST_TYPE (sym->st_info);
10540 ps = &flinfo->sections[r_symndx];
10541 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10542 sym, *ps);
10545 if ((s_type == STT_RELC || s_type == STT_SRELC)
10546 && !bfd_link_relocatable (flinfo->info))
10548 bfd_vma val;
10549 bfd_vma dot = (rel->r_offset
10550 + o->output_offset + o->output_section->vma);
10551 #ifdef DEBUG
10552 printf ("Encountered a complex symbol!");
10553 printf (" (input_bfd %s, section %s, reloc %ld\n",
10554 input_bfd->filename, o->name,
10555 (long) (rel - internal_relocs));
10556 printf (" symbol: idx %8.8lx, name %s\n",
10557 r_symndx, sym_name);
10558 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10559 (unsigned long) rel->r_info,
10560 (unsigned long) rel->r_offset);
10561 #endif
10562 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10563 isymbuf, locsymcount, s_type == STT_SRELC))
10564 return FALSE;
10566 /* Symbol evaluated OK. Update to absolute value. */
10567 set_symbol_value (input_bfd, isymbuf, locsymcount,
10568 r_symndx, val);
10569 continue;
10572 if (action_discarded != -1 && ps != NULL)
10574 /* Complain if the definition comes from a
10575 discarded section. */
10576 if ((sec = *ps) != NULL && discarded_section (sec))
10578 BFD_ASSERT (r_symndx != STN_UNDEF);
10579 if (action_discarded & COMPLAIN)
10580 (*flinfo->info->callbacks->einfo)
10581 /* xgettext:c-format */
10582 (_("%X`%s' referenced in section `%A' of %B: "
10583 "defined in discarded section `%A' of %B\n"),
10584 sym_name, o, input_bfd, sec, sec->owner);
10586 /* Try to do the best we can to support buggy old
10587 versions of gcc. Pretend that the symbol is
10588 really defined in the kept linkonce section.
10589 FIXME: This is quite broken. Modifying the
10590 symbol here means we will be changing all later
10591 uses of the symbol, not just in this section. */
10592 if (action_discarded & PRETEND)
10594 asection *kept;
10596 kept = _bfd_elf_check_kept_section (sec,
10597 flinfo->info);
10598 if (kept != NULL)
10600 *ps = kept;
10601 continue;
10608 /* Relocate the section by invoking a back end routine.
10610 The back end routine is responsible for adjusting the
10611 section contents as necessary, and (if using Rela relocs
10612 and generating a relocatable output file) adjusting the
10613 reloc addend as necessary.
10615 The back end routine does not have to worry about setting
10616 the reloc address or the reloc symbol index.
10618 The back end routine is given a pointer to the swapped in
10619 internal symbols, and can access the hash table entries
10620 for the external symbols via elf_sym_hashes (input_bfd).
10622 When generating relocatable output, the back end routine
10623 must handle STB_LOCAL/STT_SECTION symbols specially. The
10624 output symbol is going to be a section symbol
10625 corresponding to the output section, which will require
10626 the addend to be adjusted. */
10628 ret = (*relocate_section) (output_bfd, flinfo->info,
10629 input_bfd, o, contents,
10630 internal_relocs,
10631 isymbuf,
10632 flinfo->sections);
10633 if (!ret)
10634 return FALSE;
10636 if (ret == 2
10637 || bfd_link_relocatable (flinfo->info)
10638 || flinfo->info->emitrelocations)
10640 Elf_Internal_Rela *irela;
10641 Elf_Internal_Rela *irelaend, *irelamid;
10642 bfd_vma last_offset;
10643 struct elf_link_hash_entry **rel_hash;
10644 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10645 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10646 unsigned int next_erel;
10647 bfd_boolean rela_normal;
10648 struct bfd_elf_section_data *esdi, *esdo;
10650 esdi = elf_section_data (o);
10651 esdo = elf_section_data (o->output_section);
10652 rela_normal = FALSE;
10654 /* Adjust the reloc addresses and symbol indices. */
10656 irela = internal_relocs;
10657 irelaend = irela + o->reloc_count;
10658 rel_hash = esdo->rel.hashes + esdo->rel.count;
10659 /* We start processing the REL relocs, if any. When we reach
10660 IRELAMID in the loop, we switch to the RELA relocs. */
10661 irelamid = irela;
10662 if (esdi->rel.hdr != NULL)
10663 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10664 * bed->s->int_rels_per_ext_rel);
10665 rel_hash_list = rel_hash;
10666 rela_hash_list = NULL;
10667 last_offset = o->output_offset;
10668 if (!bfd_link_relocatable (flinfo->info))
10669 last_offset += o->output_section->vma;
10670 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10672 unsigned long r_symndx;
10673 asection *sec;
10674 Elf_Internal_Sym sym;
10676 if (next_erel == bed->s->int_rels_per_ext_rel)
10678 rel_hash++;
10679 next_erel = 0;
10682 if (irela == irelamid)
10684 rel_hash = esdo->rela.hashes + esdo->rela.count;
10685 rela_hash_list = rel_hash;
10686 rela_normal = bed->rela_normal;
10689 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10690 flinfo->info, o,
10691 irela->r_offset);
10692 if (irela->r_offset >= (bfd_vma) -2)
10694 /* This is a reloc for a deleted entry or somesuch.
10695 Turn it into an R_*_NONE reloc, at the same
10696 offset as the last reloc. elf_eh_frame.c and
10697 bfd_elf_discard_info rely on reloc offsets
10698 being ordered. */
10699 irela->r_offset = last_offset;
10700 irela->r_info = 0;
10701 irela->r_addend = 0;
10702 continue;
10705 irela->r_offset += o->output_offset;
10707 /* Relocs in an executable have to be virtual addresses. */
10708 if (!bfd_link_relocatable (flinfo->info))
10709 irela->r_offset += o->output_section->vma;
10711 last_offset = irela->r_offset;
10713 r_symndx = irela->r_info >> r_sym_shift;
10714 if (r_symndx == STN_UNDEF)
10715 continue;
10717 if (r_symndx >= locsymcount
10718 || (elf_bad_symtab (input_bfd)
10719 && flinfo->sections[r_symndx] == NULL))
10721 struct elf_link_hash_entry *rh;
10722 unsigned long indx;
10724 /* This is a reloc against a global symbol. We
10725 have not yet output all the local symbols, so
10726 we do not know the symbol index of any global
10727 symbol. We set the rel_hash entry for this
10728 reloc to point to the global hash table entry
10729 for this symbol. The symbol index is then
10730 set at the end of bfd_elf_final_link. */
10731 indx = r_symndx - extsymoff;
10732 rh = elf_sym_hashes (input_bfd)[indx];
10733 while (rh->root.type == bfd_link_hash_indirect
10734 || rh->root.type == bfd_link_hash_warning)
10735 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10737 /* Setting the index to -2 tells
10738 elf_link_output_extsym that this symbol is
10739 used by a reloc. */
10740 BFD_ASSERT (rh->indx < 0);
10741 rh->indx = -2;
10742 *rel_hash = rh;
10744 continue;
10747 /* This is a reloc against a local symbol. */
10749 *rel_hash = NULL;
10750 sym = isymbuf[r_symndx];
10751 sec = flinfo->sections[r_symndx];
10752 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10754 /* I suppose the backend ought to fill in the
10755 section of any STT_SECTION symbol against a
10756 processor specific section. */
10757 r_symndx = STN_UNDEF;
10758 if (bfd_is_abs_section (sec))
10760 else if (sec == NULL || sec->owner == NULL)
10762 bfd_set_error (bfd_error_bad_value);
10763 return FALSE;
10765 else
10767 asection *osec = sec->output_section;
10769 /* If we have discarded a section, the output
10770 section will be the absolute section. In
10771 case of discarded SEC_MERGE sections, use
10772 the kept section. relocate_section should
10773 have already handled discarded linkonce
10774 sections. */
10775 if (bfd_is_abs_section (osec)
10776 && sec->kept_section != NULL
10777 && sec->kept_section->output_section != NULL)
10779 osec = sec->kept_section->output_section;
10780 irela->r_addend -= osec->vma;
10783 if (!bfd_is_abs_section (osec))
10785 r_symndx = osec->target_index;
10786 if (r_symndx == STN_UNDEF)
10788 irela->r_addend += osec->vma;
10789 osec = _bfd_nearby_section (output_bfd, osec,
10790 osec->vma);
10791 irela->r_addend -= osec->vma;
10792 r_symndx = osec->target_index;
10797 /* Adjust the addend according to where the
10798 section winds up in the output section. */
10799 if (rela_normal)
10800 irela->r_addend += sec->output_offset;
10802 else
10804 if (flinfo->indices[r_symndx] == -1)
10806 unsigned long shlink;
10807 const char *name;
10808 asection *osec;
10809 long indx;
10811 if (flinfo->info->strip == strip_all)
10813 /* You can't do ld -r -s. */
10814 bfd_set_error (bfd_error_invalid_operation);
10815 return FALSE;
10818 /* This symbol was skipped earlier, but
10819 since it is needed by a reloc, we
10820 must output it now. */
10821 shlink = symtab_hdr->sh_link;
10822 name = (bfd_elf_string_from_elf_section
10823 (input_bfd, shlink, sym.st_name));
10824 if (name == NULL)
10825 return FALSE;
10827 osec = sec->output_section;
10828 sym.st_shndx =
10829 _bfd_elf_section_from_bfd_section (output_bfd,
10830 osec);
10831 if (sym.st_shndx == SHN_BAD)
10832 return FALSE;
10834 sym.st_value += sec->output_offset;
10835 if (!bfd_link_relocatable (flinfo->info))
10837 sym.st_value += osec->vma;
10838 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10840 /* STT_TLS symbols are relative to PT_TLS
10841 segment base. */
10842 BFD_ASSERT (elf_hash_table (flinfo->info)
10843 ->tls_sec != NULL);
10844 sym.st_value -= (elf_hash_table (flinfo->info)
10845 ->tls_sec->vma);
10849 indx = bfd_get_symcount (output_bfd);
10850 ret = elf_link_output_symstrtab (flinfo, name,
10851 &sym, sec,
10852 NULL);
10853 if (ret == 0)
10854 return FALSE;
10855 else if (ret == 1)
10856 flinfo->indices[r_symndx] = indx;
10857 else
10858 abort ();
10861 r_symndx = flinfo->indices[r_symndx];
10864 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10865 | (irela->r_info & r_type_mask));
10868 /* Swap out the relocs. */
10869 input_rel_hdr = esdi->rel.hdr;
10870 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10872 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10873 input_rel_hdr,
10874 internal_relocs,
10875 rel_hash_list))
10876 return FALSE;
10877 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10878 * bed->s->int_rels_per_ext_rel);
10879 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10882 input_rela_hdr = esdi->rela.hdr;
10883 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10885 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10886 input_rela_hdr,
10887 internal_relocs,
10888 rela_hash_list))
10889 return FALSE;
10894 /* Write out the modified section contents. */
10895 if (bed->elf_backend_write_section
10896 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10897 contents))
10899 /* Section written out. */
10901 else switch (o->sec_info_type)
10903 case SEC_INFO_TYPE_STABS:
10904 if (! (_bfd_write_section_stabs
10905 (output_bfd,
10906 &elf_hash_table (flinfo->info)->stab_info,
10907 o, &elf_section_data (o)->sec_info, contents)))
10908 return FALSE;
10909 break;
10910 case SEC_INFO_TYPE_MERGE:
10911 if (! _bfd_write_merged_section (output_bfd, o,
10912 elf_section_data (o)->sec_info))
10913 return FALSE;
10914 break;
10915 case SEC_INFO_TYPE_EH_FRAME:
10917 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10918 o, contents))
10919 return FALSE;
10921 break;
10922 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10924 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10925 flinfo->info,
10926 o, contents))
10927 return FALSE;
10929 break;
10930 default:
10932 if (! (o->flags & SEC_EXCLUDE))
10934 file_ptr offset = (file_ptr) o->output_offset;
10935 bfd_size_type todo = o->size;
10937 offset *= bfd_octets_per_byte (output_bfd);
10939 if ((o->flags & SEC_ELF_REVERSE_COPY))
10941 /* Reverse-copy input section to output. */
10944 todo -= address_size;
10945 if (! bfd_set_section_contents (output_bfd,
10946 o->output_section,
10947 contents + todo,
10948 offset,
10949 address_size))
10950 return FALSE;
10951 if (todo == 0)
10952 break;
10953 offset += address_size;
10955 while (1);
10957 else if (! bfd_set_section_contents (output_bfd,
10958 o->output_section,
10959 contents,
10960 offset, todo))
10961 return FALSE;
10964 break;
10968 return TRUE;
10971 /* Generate a reloc when linking an ELF file. This is a reloc
10972 requested by the linker, and does not come from any input file. This
10973 is used to build constructor and destructor tables when linking
10974 with -Ur. */
10976 static bfd_boolean
10977 elf_reloc_link_order (bfd *output_bfd,
10978 struct bfd_link_info *info,
10979 asection *output_section,
10980 struct bfd_link_order *link_order)
10982 reloc_howto_type *howto;
10983 long indx;
10984 bfd_vma offset;
10985 bfd_vma addend;
10986 struct bfd_elf_section_reloc_data *reldata;
10987 struct elf_link_hash_entry **rel_hash_ptr;
10988 Elf_Internal_Shdr *rel_hdr;
10989 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10990 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10991 bfd_byte *erel;
10992 unsigned int i;
10993 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10995 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10996 if (howto == NULL)
10998 bfd_set_error (bfd_error_bad_value);
10999 return FALSE;
11002 addend = link_order->u.reloc.p->addend;
11004 if (esdo->rel.hdr)
11005 reldata = &esdo->rel;
11006 else if (esdo->rela.hdr)
11007 reldata = &esdo->rela;
11008 else
11010 reldata = NULL;
11011 BFD_ASSERT (0);
11014 /* Figure out the symbol index. */
11015 rel_hash_ptr = reldata->hashes + reldata->count;
11016 if (link_order->type == bfd_section_reloc_link_order)
11018 indx = link_order->u.reloc.p->u.section->target_index;
11019 BFD_ASSERT (indx != 0);
11020 *rel_hash_ptr = NULL;
11022 else
11024 struct elf_link_hash_entry *h;
11026 /* Treat a reloc against a defined symbol as though it were
11027 actually against the section. */
11028 h = ((struct elf_link_hash_entry *)
11029 bfd_wrapped_link_hash_lookup (output_bfd, info,
11030 link_order->u.reloc.p->u.name,
11031 FALSE, FALSE, TRUE));
11032 if (h != NULL
11033 && (h->root.type == bfd_link_hash_defined
11034 || h->root.type == bfd_link_hash_defweak))
11036 asection *section;
11038 section = h->root.u.def.section;
11039 indx = section->output_section->target_index;
11040 *rel_hash_ptr = NULL;
11041 /* It seems that we ought to add the symbol value to the
11042 addend here, but in practice it has already been added
11043 because it was passed to constructor_callback. */
11044 addend += section->output_section->vma + section->output_offset;
11046 else if (h != NULL)
11048 /* Setting the index to -2 tells elf_link_output_extsym that
11049 this symbol is used by a reloc. */
11050 h->indx = -2;
11051 *rel_hash_ptr = h;
11052 indx = 0;
11054 else
11056 (*info->callbacks->unattached_reloc)
11057 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11058 indx = 0;
11062 /* If this is an inplace reloc, we must write the addend into the
11063 object file. */
11064 if (howto->partial_inplace && addend != 0)
11066 bfd_size_type size;
11067 bfd_reloc_status_type rstat;
11068 bfd_byte *buf;
11069 bfd_boolean ok;
11070 const char *sym_name;
11072 size = (bfd_size_type) bfd_get_reloc_size (howto);
11073 buf = (bfd_byte *) bfd_zmalloc (size);
11074 if (buf == NULL && size != 0)
11075 return FALSE;
11076 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11077 switch (rstat)
11079 case bfd_reloc_ok:
11080 break;
11082 default:
11083 case bfd_reloc_outofrange:
11084 abort ();
11086 case bfd_reloc_overflow:
11087 if (link_order->type == bfd_section_reloc_link_order)
11088 sym_name = bfd_section_name (output_bfd,
11089 link_order->u.reloc.p->u.section);
11090 else
11091 sym_name = link_order->u.reloc.p->u.name;
11092 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11093 howto->name, addend, NULL, NULL,
11094 (bfd_vma) 0);
11095 break;
11098 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11099 link_order->offset
11100 * bfd_octets_per_byte (output_bfd),
11101 size);
11102 free (buf);
11103 if (! ok)
11104 return FALSE;
11107 /* The address of a reloc is relative to the section in a
11108 relocatable file, and is a virtual address in an executable
11109 file. */
11110 offset = link_order->offset;
11111 if (! bfd_link_relocatable (info))
11112 offset += output_section->vma;
11114 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11116 irel[i].r_offset = offset;
11117 irel[i].r_info = 0;
11118 irel[i].r_addend = 0;
11120 if (bed->s->arch_size == 32)
11121 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11122 else
11123 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11125 rel_hdr = reldata->hdr;
11126 erel = rel_hdr->contents;
11127 if (rel_hdr->sh_type == SHT_REL)
11129 erel += reldata->count * bed->s->sizeof_rel;
11130 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11132 else
11134 irel[0].r_addend = addend;
11135 erel += reldata->count * bed->s->sizeof_rela;
11136 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11139 ++reldata->count;
11141 return TRUE;
11145 /* Get the output vma of the section pointed to by the sh_link field. */
11147 static bfd_vma
11148 elf_get_linked_section_vma (struct bfd_link_order *p)
11150 Elf_Internal_Shdr **elf_shdrp;
11151 asection *s;
11152 int elfsec;
11154 s = p->u.indirect.section;
11155 elf_shdrp = elf_elfsections (s->owner);
11156 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11157 elfsec = elf_shdrp[elfsec]->sh_link;
11158 /* PR 290:
11159 The Intel C compiler generates SHT_IA_64_UNWIND with
11160 SHF_LINK_ORDER. But it doesn't set the sh_link or
11161 sh_info fields. Hence we could get the situation
11162 where elfsec is 0. */
11163 if (elfsec == 0)
11165 const struct elf_backend_data *bed
11166 = get_elf_backend_data (s->owner);
11167 if (bed->link_order_error_handler)
11168 bed->link_order_error_handler
11169 /* xgettext:c-format */
11170 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11171 return 0;
11173 else
11175 s = elf_shdrp[elfsec]->bfd_section;
11176 return s->output_section->vma + s->output_offset;
11181 /* Compare two sections based on the locations of the sections they are
11182 linked to. Used by elf_fixup_link_order. */
11184 static int
11185 compare_link_order (const void * a, const void * b)
11187 bfd_vma apos;
11188 bfd_vma bpos;
11190 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11191 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11192 if (apos < bpos)
11193 return -1;
11194 return apos > bpos;
11198 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11199 order as their linked sections. Returns false if this could not be done
11200 because an output section includes both ordered and unordered
11201 sections. Ideally we'd do this in the linker proper. */
11203 static bfd_boolean
11204 elf_fixup_link_order (bfd *abfd, asection *o)
11206 int seen_linkorder;
11207 int seen_other;
11208 int n;
11209 struct bfd_link_order *p;
11210 bfd *sub;
11211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11212 unsigned elfsec;
11213 struct bfd_link_order **sections;
11214 asection *s, *other_sec, *linkorder_sec;
11215 bfd_vma offset;
11217 other_sec = NULL;
11218 linkorder_sec = NULL;
11219 seen_other = 0;
11220 seen_linkorder = 0;
11221 for (p = o->map_head.link_order; p != NULL; p = p->next)
11223 if (p->type == bfd_indirect_link_order)
11225 s = p->u.indirect.section;
11226 sub = s->owner;
11227 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11228 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11229 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11230 && elfsec < elf_numsections (sub)
11231 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11232 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11234 seen_linkorder++;
11235 linkorder_sec = s;
11237 else
11239 seen_other++;
11240 other_sec = s;
11243 else
11244 seen_other++;
11246 if (seen_other && seen_linkorder)
11248 if (other_sec && linkorder_sec)
11249 _bfd_error_handler
11250 /* xgettext:c-format */
11251 (_("%A has both ordered [`%A' in %B] "
11252 "and unordered [`%A' in %B] sections"),
11253 o, linkorder_sec, linkorder_sec->owner,
11254 other_sec, other_sec->owner);
11255 else
11256 _bfd_error_handler
11257 (_("%A has both ordered and unordered sections"), o);
11258 bfd_set_error (bfd_error_bad_value);
11259 return FALSE;
11263 if (!seen_linkorder)
11264 return TRUE;
11266 sections = (struct bfd_link_order **)
11267 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11268 if (sections == NULL)
11269 return FALSE;
11270 seen_linkorder = 0;
11272 for (p = o->map_head.link_order; p != NULL; p = p->next)
11274 sections[seen_linkorder++] = p;
11276 /* Sort the input sections in the order of their linked section. */
11277 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11278 compare_link_order);
11280 /* Change the offsets of the sections. */
11281 offset = 0;
11282 for (n = 0; n < seen_linkorder; n++)
11284 s = sections[n]->u.indirect.section;
11285 offset &= ~(bfd_vma) 0 << s->alignment_power;
11286 s->output_offset = offset / bfd_octets_per_byte (abfd);
11287 sections[n]->offset = offset;
11288 offset += sections[n]->size;
11291 free (sections);
11292 return TRUE;
11295 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11296 Returns TRUE upon success, FALSE otherwise. */
11298 static bfd_boolean
11299 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11301 bfd_boolean ret = FALSE;
11302 bfd *implib_bfd;
11303 const struct elf_backend_data *bed;
11304 flagword flags;
11305 enum bfd_architecture arch;
11306 unsigned int mach;
11307 asymbol **sympp = NULL;
11308 long symsize;
11309 long symcount;
11310 long src_count;
11311 elf_symbol_type *osymbuf;
11313 implib_bfd = info->out_implib_bfd;
11314 bed = get_elf_backend_data (abfd);
11316 if (!bfd_set_format (implib_bfd, bfd_object))
11317 return FALSE;
11319 /* Use flag from executable but make it a relocatable object. */
11320 flags = bfd_get_file_flags (abfd);
11321 flags &= ~HAS_RELOC;
11322 if (!bfd_set_start_address (implib_bfd, 0)
11323 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11324 return FALSE;
11326 /* Copy architecture of output file to import library file. */
11327 arch = bfd_get_arch (abfd);
11328 mach = bfd_get_mach (abfd);
11329 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11330 && (abfd->target_defaulted
11331 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11332 return FALSE;
11334 /* Get symbol table size. */
11335 symsize = bfd_get_symtab_upper_bound (abfd);
11336 if (symsize < 0)
11337 return FALSE;
11339 /* Read in the symbol table. */
11340 sympp = (asymbol **) xmalloc (symsize);
11341 symcount = bfd_canonicalize_symtab (abfd, sympp);
11342 if (symcount < 0)
11343 goto free_sym_buf;
11345 /* Allow the BFD backend to copy any private header data it
11346 understands from the output BFD to the import library BFD. */
11347 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11348 goto free_sym_buf;
11350 /* Filter symbols to appear in the import library. */
11351 if (bed->elf_backend_filter_implib_symbols)
11352 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11353 symcount);
11354 else
11355 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11356 if (symcount == 0)
11358 bfd_set_error (bfd_error_no_symbols);
11359 _bfd_error_handler (_("%B: no symbol found for import library"),
11360 implib_bfd);
11361 goto free_sym_buf;
11365 /* Make symbols absolute. */
11366 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11367 sizeof (*osymbuf));
11368 for (src_count = 0; src_count < symcount; src_count++)
11370 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11371 sizeof (*osymbuf));
11372 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11373 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11374 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11375 osymbuf[src_count].internal_elf_sym.st_value =
11376 osymbuf[src_count].symbol.value;
11377 sympp[src_count] = &osymbuf[src_count].symbol;
11380 bfd_set_symtab (implib_bfd, sympp, symcount);
11382 /* Allow the BFD backend to copy any private data it understands
11383 from the output BFD to the import library BFD. This is done last
11384 to permit the routine to look at the filtered symbol table. */
11385 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11386 goto free_sym_buf;
11388 if (!bfd_close (implib_bfd))
11389 goto free_sym_buf;
11391 ret = TRUE;
11393 free_sym_buf:
11394 free (sympp);
11395 return ret;
11398 static void
11399 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11401 asection *o;
11403 if (flinfo->symstrtab != NULL)
11404 _bfd_elf_strtab_free (flinfo->symstrtab);
11405 if (flinfo->contents != NULL)
11406 free (flinfo->contents);
11407 if (flinfo->external_relocs != NULL)
11408 free (flinfo->external_relocs);
11409 if (flinfo->internal_relocs != NULL)
11410 free (flinfo->internal_relocs);
11411 if (flinfo->external_syms != NULL)
11412 free (flinfo->external_syms);
11413 if (flinfo->locsym_shndx != NULL)
11414 free (flinfo->locsym_shndx);
11415 if (flinfo->internal_syms != NULL)
11416 free (flinfo->internal_syms);
11417 if (flinfo->indices != NULL)
11418 free (flinfo->indices);
11419 if (flinfo->sections != NULL)
11420 free (flinfo->sections);
11421 if (flinfo->symshndxbuf != NULL)
11422 free (flinfo->symshndxbuf);
11423 for (o = obfd->sections; o != NULL; o = o->next)
11425 struct bfd_elf_section_data *esdo = elf_section_data (o);
11426 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11427 free (esdo->rel.hashes);
11428 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11429 free (esdo->rela.hashes);
11433 /* Do the final step of an ELF link. */
11435 bfd_boolean
11436 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11438 bfd_boolean dynamic;
11439 bfd_boolean emit_relocs;
11440 bfd *dynobj;
11441 struct elf_final_link_info flinfo;
11442 asection *o;
11443 struct bfd_link_order *p;
11444 bfd *sub;
11445 bfd_size_type max_contents_size;
11446 bfd_size_type max_external_reloc_size;
11447 bfd_size_type max_internal_reloc_count;
11448 bfd_size_type max_sym_count;
11449 bfd_size_type max_sym_shndx_count;
11450 Elf_Internal_Sym elfsym;
11451 unsigned int i;
11452 Elf_Internal_Shdr *symtab_hdr;
11453 Elf_Internal_Shdr *symtab_shndx_hdr;
11454 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11455 struct elf_outext_info eoinfo;
11456 bfd_boolean merged;
11457 size_t relativecount = 0;
11458 asection *reldyn = 0;
11459 bfd_size_type amt;
11460 asection *attr_section = NULL;
11461 bfd_vma attr_size = 0;
11462 const char *std_attrs_section;
11463 struct elf_link_hash_table *htab = elf_hash_table (info);
11465 if (!is_elf_hash_table (htab))
11466 return FALSE;
11468 if (bfd_link_pic (info))
11469 abfd->flags |= DYNAMIC;
11471 dynamic = htab->dynamic_sections_created;
11472 dynobj = htab->dynobj;
11474 emit_relocs = (bfd_link_relocatable (info)
11475 || info->emitrelocations);
11477 flinfo.info = info;
11478 flinfo.output_bfd = abfd;
11479 flinfo.symstrtab = _bfd_elf_strtab_init ();
11480 if (flinfo.symstrtab == NULL)
11481 return FALSE;
11483 if (! dynamic)
11485 flinfo.hash_sec = NULL;
11486 flinfo.symver_sec = NULL;
11488 else
11490 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11491 /* Note that dynsym_sec can be NULL (on VMS). */
11492 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11493 /* Note that it is OK if symver_sec is NULL. */
11496 flinfo.contents = NULL;
11497 flinfo.external_relocs = NULL;
11498 flinfo.internal_relocs = NULL;
11499 flinfo.external_syms = NULL;
11500 flinfo.locsym_shndx = NULL;
11501 flinfo.internal_syms = NULL;
11502 flinfo.indices = NULL;
11503 flinfo.sections = NULL;
11504 flinfo.symshndxbuf = NULL;
11505 flinfo.filesym_count = 0;
11507 /* The object attributes have been merged. Remove the input
11508 sections from the link, and set the contents of the output
11509 secton. */
11510 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11511 for (o = abfd->sections; o != NULL; o = o->next)
11513 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11514 || strcmp (o->name, ".gnu.attributes") == 0)
11516 for (p = o->map_head.link_order; p != NULL; p = p->next)
11518 asection *input_section;
11520 if (p->type != bfd_indirect_link_order)
11521 continue;
11522 input_section = p->u.indirect.section;
11523 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11524 elf_link_input_bfd ignores this section. */
11525 input_section->flags &= ~SEC_HAS_CONTENTS;
11528 attr_size = bfd_elf_obj_attr_size (abfd);
11529 if (attr_size)
11531 bfd_set_section_size (abfd, o, attr_size);
11532 attr_section = o;
11533 /* Skip this section later on. */
11534 o->map_head.link_order = NULL;
11536 else
11537 o->flags |= SEC_EXCLUDE;
11541 /* Count up the number of relocations we will output for each output
11542 section, so that we know the sizes of the reloc sections. We
11543 also figure out some maximum sizes. */
11544 max_contents_size = 0;
11545 max_external_reloc_size = 0;
11546 max_internal_reloc_count = 0;
11547 max_sym_count = 0;
11548 max_sym_shndx_count = 0;
11549 merged = FALSE;
11550 for (o = abfd->sections; o != NULL; o = o->next)
11552 struct bfd_elf_section_data *esdo = elf_section_data (o);
11553 o->reloc_count = 0;
11555 for (p = o->map_head.link_order; p != NULL; p = p->next)
11557 unsigned int reloc_count = 0;
11558 unsigned int additional_reloc_count = 0;
11559 struct bfd_elf_section_data *esdi = NULL;
11561 if (p->type == bfd_section_reloc_link_order
11562 || p->type == bfd_symbol_reloc_link_order)
11563 reloc_count = 1;
11564 else if (p->type == bfd_indirect_link_order)
11566 asection *sec;
11568 sec = p->u.indirect.section;
11570 /* Mark all sections which are to be included in the
11571 link. This will normally be every section. We need
11572 to do this so that we can identify any sections which
11573 the linker has decided to not include. */
11574 sec->linker_mark = TRUE;
11576 if (sec->flags & SEC_MERGE)
11577 merged = TRUE;
11579 if (sec->rawsize > max_contents_size)
11580 max_contents_size = sec->rawsize;
11581 if (sec->size > max_contents_size)
11582 max_contents_size = sec->size;
11584 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11585 && (sec->owner->flags & DYNAMIC) == 0)
11587 size_t sym_count;
11589 /* We are interested in just local symbols, not all
11590 symbols. */
11591 if (elf_bad_symtab (sec->owner))
11592 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11593 / bed->s->sizeof_sym);
11594 else
11595 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11597 if (sym_count > max_sym_count)
11598 max_sym_count = sym_count;
11600 if (sym_count > max_sym_shndx_count
11601 && elf_symtab_shndx_list (sec->owner) != NULL)
11602 max_sym_shndx_count = sym_count;
11604 if (esdo->this_hdr.sh_type == SHT_REL
11605 || esdo->this_hdr.sh_type == SHT_RELA)
11606 /* Some backends use reloc_count in relocation sections
11607 to count particular types of relocs. Of course,
11608 reloc sections themselves can't have relocations. */
11610 else if (emit_relocs)
11612 reloc_count = sec->reloc_count;
11613 if (bed->elf_backend_count_additional_relocs)
11615 int c;
11616 c = (*bed->elf_backend_count_additional_relocs) (sec);
11617 additional_reloc_count += c;
11620 else if (bed->elf_backend_count_relocs)
11621 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11623 esdi = elf_section_data (sec);
11625 if ((sec->flags & SEC_RELOC) != 0)
11627 size_t ext_size = 0;
11629 if (esdi->rel.hdr != NULL)
11630 ext_size = esdi->rel.hdr->sh_size;
11631 if (esdi->rela.hdr != NULL)
11632 ext_size += esdi->rela.hdr->sh_size;
11634 if (ext_size > max_external_reloc_size)
11635 max_external_reloc_size = ext_size;
11636 if (sec->reloc_count > max_internal_reloc_count)
11637 max_internal_reloc_count = sec->reloc_count;
11642 if (reloc_count == 0)
11643 continue;
11645 reloc_count += additional_reloc_count;
11646 o->reloc_count += reloc_count;
11648 if (p->type == bfd_indirect_link_order && emit_relocs)
11650 if (esdi->rel.hdr)
11652 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11653 esdo->rel.count += additional_reloc_count;
11655 if (esdi->rela.hdr)
11657 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11658 esdo->rela.count += additional_reloc_count;
11661 else
11663 if (o->use_rela_p)
11664 esdo->rela.count += reloc_count;
11665 else
11666 esdo->rel.count += reloc_count;
11670 if (o->reloc_count > 0)
11671 o->flags |= SEC_RELOC;
11672 else
11674 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11675 set it (this is probably a bug) and if it is set
11676 assign_section_numbers will create a reloc section. */
11677 o->flags &=~ SEC_RELOC;
11680 /* If the SEC_ALLOC flag is not set, force the section VMA to
11681 zero. This is done in elf_fake_sections as well, but forcing
11682 the VMA to 0 here will ensure that relocs against these
11683 sections are handled correctly. */
11684 if ((o->flags & SEC_ALLOC) == 0
11685 && ! o->user_set_vma)
11686 o->vma = 0;
11689 if (! bfd_link_relocatable (info) && merged)
11690 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11692 /* Figure out the file positions for everything but the symbol table
11693 and the relocs. We set symcount to force assign_section_numbers
11694 to create a symbol table. */
11695 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11696 BFD_ASSERT (! abfd->output_has_begun);
11697 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11698 goto error_return;
11700 /* Set sizes, and assign file positions for reloc sections. */
11701 for (o = abfd->sections; o != NULL; o = o->next)
11703 struct bfd_elf_section_data *esdo = elf_section_data (o);
11704 if ((o->flags & SEC_RELOC) != 0)
11706 if (esdo->rel.hdr
11707 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11708 goto error_return;
11710 if (esdo->rela.hdr
11711 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11712 goto error_return;
11715 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11716 to count upwards while actually outputting the relocations. */
11717 esdo->rel.count = 0;
11718 esdo->rela.count = 0;
11720 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11722 /* Cache the section contents so that they can be compressed
11723 later. Use bfd_malloc since it will be freed by
11724 bfd_compress_section_contents. */
11725 unsigned char *contents = esdo->this_hdr.contents;
11726 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11727 abort ();
11728 contents
11729 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11730 if (contents == NULL)
11731 goto error_return;
11732 esdo->this_hdr.contents = contents;
11736 /* We have now assigned file positions for all the sections except
11737 .symtab, .strtab, and non-loaded reloc sections. We start the
11738 .symtab section at the current file position, and write directly
11739 to it. We build the .strtab section in memory. */
11740 bfd_get_symcount (abfd) = 0;
11741 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11742 /* sh_name is set in prep_headers. */
11743 symtab_hdr->sh_type = SHT_SYMTAB;
11744 /* sh_flags, sh_addr and sh_size all start off zero. */
11745 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11746 /* sh_link is set in assign_section_numbers. */
11747 /* sh_info is set below. */
11748 /* sh_offset is set just below. */
11749 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11751 if (max_sym_count < 20)
11752 max_sym_count = 20;
11753 htab->strtabsize = max_sym_count;
11754 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11755 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11756 if (htab->strtab == NULL)
11757 goto error_return;
11758 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11759 flinfo.symshndxbuf
11760 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11761 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11763 if (info->strip != strip_all || emit_relocs)
11765 file_ptr off = elf_next_file_pos (abfd);
11767 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11769 /* Note that at this point elf_next_file_pos (abfd) is
11770 incorrect. We do not yet know the size of the .symtab section.
11771 We correct next_file_pos below, after we do know the size. */
11773 /* Start writing out the symbol table. The first symbol is always a
11774 dummy symbol. */
11775 elfsym.st_value = 0;
11776 elfsym.st_size = 0;
11777 elfsym.st_info = 0;
11778 elfsym.st_other = 0;
11779 elfsym.st_shndx = SHN_UNDEF;
11780 elfsym.st_target_internal = 0;
11781 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11782 bfd_und_section_ptr, NULL) != 1)
11783 goto error_return;
11785 /* Output a symbol for each section. We output these even if we are
11786 discarding local symbols, since they are used for relocs. These
11787 symbols have no names. We store the index of each one in the
11788 index field of the section, so that we can find it again when
11789 outputting relocs. */
11791 elfsym.st_size = 0;
11792 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11793 elfsym.st_other = 0;
11794 elfsym.st_value = 0;
11795 elfsym.st_target_internal = 0;
11796 for (i = 1; i < elf_numsections (abfd); i++)
11798 o = bfd_section_from_elf_index (abfd, i);
11799 if (o != NULL)
11801 o->target_index = bfd_get_symcount (abfd);
11802 elfsym.st_shndx = i;
11803 if (!bfd_link_relocatable (info))
11804 elfsym.st_value = o->vma;
11805 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11806 NULL) != 1)
11807 goto error_return;
11812 /* Allocate some memory to hold information read in from the input
11813 files. */
11814 if (max_contents_size != 0)
11816 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11817 if (flinfo.contents == NULL)
11818 goto error_return;
11821 if (max_external_reloc_size != 0)
11823 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11824 if (flinfo.external_relocs == NULL)
11825 goto error_return;
11828 if (max_internal_reloc_count != 0)
11830 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11831 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11832 if (flinfo.internal_relocs == NULL)
11833 goto error_return;
11836 if (max_sym_count != 0)
11838 amt = max_sym_count * bed->s->sizeof_sym;
11839 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11840 if (flinfo.external_syms == NULL)
11841 goto error_return;
11843 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11844 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11845 if (flinfo.internal_syms == NULL)
11846 goto error_return;
11848 amt = max_sym_count * sizeof (long);
11849 flinfo.indices = (long int *) bfd_malloc (amt);
11850 if (flinfo.indices == NULL)
11851 goto error_return;
11853 amt = max_sym_count * sizeof (asection *);
11854 flinfo.sections = (asection **) bfd_malloc (amt);
11855 if (flinfo.sections == NULL)
11856 goto error_return;
11859 if (max_sym_shndx_count != 0)
11861 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11862 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11863 if (flinfo.locsym_shndx == NULL)
11864 goto error_return;
11867 if (htab->tls_sec)
11869 bfd_vma base, end = 0;
11870 asection *sec;
11872 for (sec = htab->tls_sec;
11873 sec && (sec->flags & SEC_THREAD_LOCAL);
11874 sec = sec->next)
11876 bfd_size_type size = sec->size;
11878 if (size == 0
11879 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11881 struct bfd_link_order *ord = sec->map_tail.link_order;
11883 if (ord != NULL)
11884 size = ord->offset + ord->size;
11886 end = sec->vma + size;
11888 base = htab->tls_sec->vma;
11889 /* Only align end of TLS section if static TLS doesn't have special
11890 alignment requirements. */
11891 if (bed->static_tls_alignment == 1)
11892 end = align_power (end, htab->tls_sec->alignment_power);
11893 htab->tls_size = end - base;
11896 /* Reorder SHF_LINK_ORDER sections. */
11897 for (o = abfd->sections; o != NULL; o = o->next)
11899 if (!elf_fixup_link_order (abfd, o))
11900 return FALSE;
11903 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11904 return FALSE;
11906 /* Since ELF permits relocations to be against local symbols, we
11907 must have the local symbols available when we do the relocations.
11908 Since we would rather only read the local symbols once, and we
11909 would rather not keep them in memory, we handle all the
11910 relocations for a single input file at the same time.
11912 Unfortunately, there is no way to know the total number of local
11913 symbols until we have seen all of them, and the local symbol
11914 indices precede the global symbol indices. This means that when
11915 we are generating relocatable output, and we see a reloc against
11916 a global symbol, we can not know the symbol index until we have
11917 finished examining all the local symbols to see which ones we are
11918 going to output. To deal with this, we keep the relocations in
11919 memory, and don't output them until the end of the link. This is
11920 an unfortunate waste of memory, but I don't see a good way around
11921 it. Fortunately, it only happens when performing a relocatable
11922 link, which is not the common case. FIXME: If keep_memory is set
11923 we could write the relocs out and then read them again; I don't
11924 know how bad the memory loss will be. */
11926 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11927 sub->output_has_begun = FALSE;
11928 for (o = abfd->sections; o != NULL; o = o->next)
11930 for (p = o->map_head.link_order; p != NULL; p = p->next)
11932 if (p->type == bfd_indirect_link_order
11933 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11934 == bfd_target_elf_flavour)
11935 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11937 if (! sub->output_has_begun)
11939 if (! elf_link_input_bfd (&flinfo, sub))
11940 goto error_return;
11941 sub->output_has_begun = TRUE;
11944 else if (p->type == bfd_section_reloc_link_order
11945 || p->type == bfd_symbol_reloc_link_order)
11947 if (! elf_reloc_link_order (abfd, info, o, p))
11948 goto error_return;
11950 else
11952 if (! _bfd_default_link_order (abfd, info, o, p))
11954 if (p->type == bfd_indirect_link_order
11955 && (bfd_get_flavour (sub)
11956 == bfd_target_elf_flavour)
11957 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11958 != bed->s->elfclass))
11960 const char *iclass, *oclass;
11962 switch (bed->s->elfclass)
11964 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11965 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11966 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11967 default: abort ();
11970 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11972 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11973 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11974 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11975 default: abort ();
11978 bfd_set_error (bfd_error_wrong_format);
11979 _bfd_error_handler
11980 /* xgettext:c-format */
11981 (_("%B: file class %s incompatible with %s"),
11982 sub, iclass, oclass);
11985 goto error_return;
11991 /* Free symbol buffer if needed. */
11992 if (!info->reduce_memory_overheads)
11994 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11995 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11996 && elf_tdata (sub)->symbuf)
11998 free (elf_tdata (sub)->symbuf);
11999 elf_tdata (sub)->symbuf = NULL;
12003 /* Output any global symbols that got converted to local in a
12004 version script or due to symbol visibility. We do this in a
12005 separate step since ELF requires all local symbols to appear
12006 prior to any global symbols. FIXME: We should only do this if
12007 some global symbols were, in fact, converted to become local.
12008 FIXME: Will this work correctly with the Irix 5 linker? */
12009 eoinfo.failed = FALSE;
12010 eoinfo.flinfo = &flinfo;
12011 eoinfo.localsyms = TRUE;
12012 eoinfo.file_sym_done = FALSE;
12013 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12014 if (eoinfo.failed)
12015 return FALSE;
12017 /* If backend needs to output some local symbols not present in the hash
12018 table, do it now. */
12019 if (bed->elf_backend_output_arch_local_syms
12020 && (info->strip != strip_all || emit_relocs))
12022 typedef int (*out_sym_func)
12023 (void *, const char *, Elf_Internal_Sym *, asection *,
12024 struct elf_link_hash_entry *);
12026 if (! ((*bed->elf_backend_output_arch_local_syms)
12027 (abfd, info, &flinfo,
12028 (out_sym_func) elf_link_output_symstrtab)))
12029 return FALSE;
12032 /* That wrote out all the local symbols. Finish up the symbol table
12033 with the global symbols. Even if we want to strip everything we
12034 can, we still need to deal with those global symbols that got
12035 converted to local in a version script. */
12037 /* The sh_info field records the index of the first non local symbol. */
12038 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12040 if (dynamic
12041 && htab->dynsym != NULL
12042 && htab->dynsym->output_section != bfd_abs_section_ptr)
12044 Elf_Internal_Sym sym;
12045 bfd_byte *dynsym = htab->dynsym->contents;
12047 o = htab->dynsym->output_section;
12048 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12050 /* Write out the section symbols for the output sections. */
12051 if (bfd_link_pic (info)
12052 || htab->is_relocatable_executable)
12054 asection *s;
12056 sym.st_size = 0;
12057 sym.st_name = 0;
12058 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12059 sym.st_other = 0;
12060 sym.st_target_internal = 0;
12062 for (s = abfd->sections; s != NULL; s = s->next)
12064 int indx;
12065 bfd_byte *dest;
12066 long dynindx;
12068 dynindx = elf_section_data (s)->dynindx;
12069 if (dynindx <= 0)
12070 continue;
12071 indx = elf_section_data (s)->this_idx;
12072 BFD_ASSERT (indx > 0);
12073 sym.st_shndx = indx;
12074 if (! check_dynsym (abfd, &sym))
12075 return FALSE;
12076 sym.st_value = s->vma;
12077 dest = dynsym + dynindx * bed->s->sizeof_sym;
12078 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12082 /* Write out the local dynsyms. */
12083 if (htab->dynlocal)
12085 struct elf_link_local_dynamic_entry *e;
12086 for (e = htab->dynlocal; e ; e = e->next)
12088 asection *s;
12089 bfd_byte *dest;
12091 /* Copy the internal symbol and turn off visibility.
12092 Note that we saved a word of storage and overwrote
12093 the original st_name with the dynstr_index. */
12094 sym = e->isym;
12095 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12097 s = bfd_section_from_elf_index (e->input_bfd,
12098 e->isym.st_shndx);
12099 if (s != NULL)
12101 sym.st_shndx =
12102 elf_section_data (s->output_section)->this_idx;
12103 if (! check_dynsym (abfd, &sym))
12104 return FALSE;
12105 sym.st_value = (s->output_section->vma
12106 + s->output_offset
12107 + e->isym.st_value);
12110 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12111 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12116 /* We get the global symbols from the hash table. */
12117 eoinfo.failed = FALSE;
12118 eoinfo.localsyms = FALSE;
12119 eoinfo.flinfo = &flinfo;
12120 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12121 if (eoinfo.failed)
12122 return FALSE;
12124 /* If backend needs to output some symbols not present in the hash
12125 table, do it now. */
12126 if (bed->elf_backend_output_arch_syms
12127 && (info->strip != strip_all || emit_relocs))
12129 typedef int (*out_sym_func)
12130 (void *, const char *, Elf_Internal_Sym *, asection *,
12131 struct elf_link_hash_entry *);
12133 if (! ((*bed->elf_backend_output_arch_syms)
12134 (abfd, info, &flinfo,
12135 (out_sym_func) elf_link_output_symstrtab)))
12136 return FALSE;
12139 /* Finalize the .strtab section. */
12140 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12142 /* Swap out the .strtab section. */
12143 if (!elf_link_swap_symbols_out (&flinfo))
12144 return FALSE;
12146 /* Now we know the size of the symtab section. */
12147 if (bfd_get_symcount (abfd) > 0)
12149 /* Finish up and write out the symbol string table (.strtab)
12150 section. */
12151 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12152 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12154 if (elf_symtab_shndx_list (abfd))
12156 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12158 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12160 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12161 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12162 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12163 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12164 symtab_shndx_hdr->sh_size = amt;
12166 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12167 off, TRUE);
12169 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12170 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12171 return FALSE;
12175 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12176 /* sh_name was set in prep_headers. */
12177 symstrtab_hdr->sh_type = SHT_STRTAB;
12178 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12179 symstrtab_hdr->sh_addr = 0;
12180 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12181 symstrtab_hdr->sh_entsize = 0;
12182 symstrtab_hdr->sh_link = 0;
12183 symstrtab_hdr->sh_info = 0;
12184 /* sh_offset is set just below. */
12185 symstrtab_hdr->sh_addralign = 1;
12187 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12188 off, TRUE);
12189 elf_next_file_pos (abfd) = off;
12191 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12192 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12193 return FALSE;
12196 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12198 _bfd_error_handler (_("%B: failed to generate import library"),
12199 info->out_implib_bfd);
12200 return FALSE;
12203 /* Adjust the relocs to have the correct symbol indices. */
12204 for (o = abfd->sections; o != NULL; o = o->next)
12206 struct bfd_elf_section_data *esdo = elf_section_data (o);
12207 bfd_boolean sort;
12209 if ((o->flags & SEC_RELOC) == 0)
12210 continue;
12212 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12213 if (esdo->rel.hdr != NULL
12214 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12215 return FALSE;
12216 if (esdo->rela.hdr != NULL
12217 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12218 return FALSE;
12220 /* Set the reloc_count field to 0 to prevent write_relocs from
12221 trying to swap the relocs out itself. */
12222 o->reloc_count = 0;
12225 if (dynamic && info->combreloc && dynobj != NULL)
12226 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12228 /* If we are linking against a dynamic object, or generating a
12229 shared library, finish up the dynamic linking information. */
12230 if (dynamic)
12232 bfd_byte *dyncon, *dynconend;
12234 /* Fix up .dynamic entries. */
12235 o = bfd_get_linker_section (dynobj, ".dynamic");
12236 BFD_ASSERT (o != NULL);
12238 dyncon = o->contents;
12239 dynconend = o->contents + o->size;
12240 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12242 Elf_Internal_Dyn dyn;
12243 const char *name;
12244 unsigned int type;
12245 bfd_size_type sh_size;
12246 bfd_vma sh_addr;
12248 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12250 switch (dyn.d_tag)
12252 default:
12253 continue;
12254 case DT_NULL:
12255 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12257 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12259 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12260 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12261 default: continue;
12263 dyn.d_un.d_val = relativecount;
12264 relativecount = 0;
12265 break;
12267 continue;
12269 case DT_INIT:
12270 name = info->init_function;
12271 goto get_sym;
12272 case DT_FINI:
12273 name = info->fini_function;
12274 get_sym:
12276 struct elf_link_hash_entry *h;
12278 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12279 if (h != NULL
12280 && (h->root.type == bfd_link_hash_defined
12281 || h->root.type == bfd_link_hash_defweak))
12283 dyn.d_un.d_ptr = h->root.u.def.value;
12284 o = h->root.u.def.section;
12285 if (o->output_section != NULL)
12286 dyn.d_un.d_ptr += (o->output_section->vma
12287 + o->output_offset);
12288 else
12290 /* The symbol is imported from another shared
12291 library and does not apply to this one. */
12292 dyn.d_un.d_ptr = 0;
12294 break;
12297 continue;
12299 case DT_PREINIT_ARRAYSZ:
12300 name = ".preinit_array";
12301 goto get_out_size;
12302 case DT_INIT_ARRAYSZ:
12303 name = ".init_array";
12304 goto get_out_size;
12305 case DT_FINI_ARRAYSZ:
12306 name = ".fini_array";
12307 get_out_size:
12308 o = bfd_get_section_by_name (abfd, name);
12309 if (o == NULL)
12311 _bfd_error_handler
12312 (_("could not find section %s"), name);
12313 goto error_return;
12315 if (o->size == 0)
12316 _bfd_error_handler
12317 (_("warning: %s section has zero size"), name);
12318 dyn.d_un.d_val = o->size;
12319 break;
12321 case DT_PREINIT_ARRAY:
12322 name = ".preinit_array";
12323 goto get_out_vma;
12324 case DT_INIT_ARRAY:
12325 name = ".init_array";
12326 goto get_out_vma;
12327 case DT_FINI_ARRAY:
12328 name = ".fini_array";
12329 get_out_vma:
12330 o = bfd_get_section_by_name (abfd, name);
12331 goto do_vma;
12333 case DT_HASH:
12334 name = ".hash";
12335 goto get_vma;
12336 case DT_GNU_HASH:
12337 name = ".gnu.hash";
12338 goto get_vma;
12339 case DT_STRTAB:
12340 name = ".dynstr";
12341 goto get_vma;
12342 case DT_SYMTAB:
12343 name = ".dynsym";
12344 goto get_vma;
12345 case DT_VERDEF:
12346 name = ".gnu.version_d";
12347 goto get_vma;
12348 case DT_VERNEED:
12349 name = ".gnu.version_r";
12350 goto get_vma;
12351 case DT_VERSYM:
12352 name = ".gnu.version";
12353 get_vma:
12354 o = bfd_get_linker_section (dynobj, name);
12355 do_vma:
12356 if (o == NULL || bfd_is_abs_section (o->output_section))
12358 _bfd_error_handler
12359 (_("could not find section %s"), name);
12360 goto error_return;
12362 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12364 _bfd_error_handler
12365 (_("warning: section '%s' is being made into a note"), name);
12366 bfd_set_error (bfd_error_nonrepresentable_section);
12367 goto error_return;
12369 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12370 break;
12372 case DT_REL:
12373 case DT_RELA:
12374 case DT_RELSZ:
12375 case DT_RELASZ:
12376 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12377 type = SHT_REL;
12378 else
12379 type = SHT_RELA;
12380 sh_size = 0;
12381 sh_addr = 0;
12382 for (i = 1; i < elf_numsections (abfd); i++)
12384 Elf_Internal_Shdr *hdr;
12386 hdr = elf_elfsections (abfd)[i];
12387 if (hdr->sh_type == type
12388 && (hdr->sh_flags & SHF_ALLOC) != 0)
12390 sh_size += hdr->sh_size;
12391 if (sh_addr == 0
12392 || sh_addr > hdr->sh_addr)
12393 sh_addr = hdr->sh_addr;
12397 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12399 /* Don't count procedure linkage table relocs in the
12400 overall reloc count. */
12401 sh_size -= htab->srelplt->size;
12402 if (sh_size == 0)
12403 /* If the size is zero, make the address zero too.
12404 This is to avoid a glibc bug. If the backend
12405 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12406 zero, then we'll put DT_RELA at the end of
12407 DT_JMPREL. glibc will interpret the end of
12408 DT_RELA matching the end of DT_JMPREL as the
12409 case where DT_RELA includes DT_JMPREL, and for
12410 LD_BIND_NOW will decide that processing DT_RELA
12411 will process the PLT relocs too. Net result:
12412 No PLT relocs applied. */
12413 sh_addr = 0;
12415 /* If .rela.plt is the first .rela section, exclude
12416 it from DT_RELA. */
12417 else if (sh_addr == (htab->srelplt->output_section->vma
12418 + htab->srelplt->output_offset))
12419 sh_addr += htab->srelplt->size;
12422 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12423 dyn.d_un.d_val = sh_size;
12424 else
12425 dyn.d_un.d_ptr = sh_addr;
12426 break;
12428 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12432 /* If we have created any dynamic sections, then output them. */
12433 if (dynobj != NULL)
12435 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12436 goto error_return;
12438 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12439 if (((info->warn_shared_textrel && bfd_link_pic (info))
12440 || info->error_textrel)
12441 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12443 bfd_byte *dyncon, *dynconend;
12445 dyncon = o->contents;
12446 dynconend = o->contents + o->size;
12447 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12449 Elf_Internal_Dyn dyn;
12451 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12453 if (dyn.d_tag == DT_TEXTREL)
12455 if (info->error_textrel)
12456 info->callbacks->einfo
12457 (_("%P%X: read-only segment has dynamic relocations.\n"));
12458 else
12459 info->callbacks->einfo
12460 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12461 break;
12466 for (o = dynobj->sections; o != NULL; o = o->next)
12468 if ((o->flags & SEC_HAS_CONTENTS) == 0
12469 || o->size == 0
12470 || o->output_section == bfd_abs_section_ptr)
12471 continue;
12472 if ((o->flags & SEC_LINKER_CREATED) == 0)
12474 /* At this point, we are only interested in sections
12475 created by _bfd_elf_link_create_dynamic_sections. */
12476 continue;
12478 if (htab->stab_info.stabstr == o)
12479 continue;
12480 if (htab->eh_info.hdr_sec == o)
12481 continue;
12482 if (strcmp (o->name, ".dynstr") != 0)
12484 if (! bfd_set_section_contents (abfd, o->output_section,
12485 o->contents,
12486 (file_ptr) o->output_offset
12487 * bfd_octets_per_byte (abfd),
12488 o->size))
12489 goto error_return;
12491 else
12493 /* The contents of the .dynstr section are actually in a
12494 stringtab. */
12495 file_ptr off;
12497 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12498 if (bfd_seek (abfd, off, SEEK_SET) != 0
12499 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12500 goto error_return;
12505 if (!info->resolve_section_groups)
12507 bfd_boolean failed = FALSE;
12509 BFD_ASSERT (bfd_link_relocatable (info));
12510 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12511 if (failed)
12512 goto error_return;
12515 /* If we have optimized stabs strings, output them. */
12516 if (htab->stab_info.stabstr != NULL)
12518 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12519 goto error_return;
12522 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12523 goto error_return;
12525 elf_final_link_free (abfd, &flinfo);
12527 elf_linker (abfd) = TRUE;
12529 if (attr_section)
12531 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12532 if (contents == NULL)
12533 return FALSE; /* Bail out and fail. */
12534 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12535 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12536 free (contents);
12539 return TRUE;
12541 error_return:
12542 elf_final_link_free (abfd, &flinfo);
12543 return FALSE;
12546 /* Initialize COOKIE for input bfd ABFD. */
12548 static bfd_boolean
12549 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12550 struct bfd_link_info *info, bfd *abfd)
12552 Elf_Internal_Shdr *symtab_hdr;
12553 const struct elf_backend_data *bed;
12555 bed = get_elf_backend_data (abfd);
12556 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12558 cookie->abfd = abfd;
12559 cookie->sym_hashes = elf_sym_hashes (abfd);
12560 cookie->bad_symtab = elf_bad_symtab (abfd);
12561 if (cookie->bad_symtab)
12563 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12564 cookie->extsymoff = 0;
12566 else
12568 cookie->locsymcount = symtab_hdr->sh_info;
12569 cookie->extsymoff = symtab_hdr->sh_info;
12572 if (bed->s->arch_size == 32)
12573 cookie->r_sym_shift = 8;
12574 else
12575 cookie->r_sym_shift = 32;
12577 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12578 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12580 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12581 cookie->locsymcount, 0,
12582 NULL, NULL, NULL);
12583 if (cookie->locsyms == NULL)
12585 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12586 return FALSE;
12588 if (info->keep_memory)
12589 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12591 return TRUE;
12594 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12596 static void
12597 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12599 Elf_Internal_Shdr *symtab_hdr;
12601 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12602 if (cookie->locsyms != NULL
12603 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12604 free (cookie->locsyms);
12607 /* Initialize the relocation information in COOKIE for input section SEC
12608 of input bfd ABFD. */
12610 static bfd_boolean
12611 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12612 struct bfd_link_info *info, bfd *abfd,
12613 asection *sec)
12615 if (sec->reloc_count == 0)
12617 cookie->rels = NULL;
12618 cookie->relend = NULL;
12620 else
12622 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12623 info->keep_memory);
12624 if (cookie->rels == NULL)
12625 return FALSE;
12626 cookie->rel = cookie->rels;
12627 cookie->relend = cookie->rels + sec->reloc_count;
12629 cookie->rel = cookie->rels;
12630 return TRUE;
12633 /* Free the memory allocated by init_reloc_cookie_rels,
12634 if appropriate. */
12636 static void
12637 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12638 asection *sec)
12640 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12641 free (cookie->rels);
12644 /* Initialize the whole of COOKIE for input section SEC. */
12646 static bfd_boolean
12647 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12648 struct bfd_link_info *info,
12649 asection *sec)
12651 if (!init_reloc_cookie (cookie, info, sec->owner))
12652 goto error1;
12653 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12654 goto error2;
12655 return TRUE;
12657 error2:
12658 fini_reloc_cookie (cookie, sec->owner);
12659 error1:
12660 return FALSE;
12663 /* Free the memory allocated by init_reloc_cookie_for_section,
12664 if appropriate. */
12666 static void
12667 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12668 asection *sec)
12670 fini_reloc_cookie_rels (cookie, sec);
12671 fini_reloc_cookie (cookie, sec->owner);
12674 /* Garbage collect unused sections. */
12676 /* Default gc_mark_hook. */
12678 asection *
12679 _bfd_elf_gc_mark_hook (asection *sec,
12680 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12681 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12682 struct elf_link_hash_entry *h,
12683 Elf_Internal_Sym *sym)
12685 if (h != NULL)
12687 switch (h->root.type)
12689 case bfd_link_hash_defined:
12690 case bfd_link_hash_defweak:
12691 return h->root.u.def.section;
12693 case bfd_link_hash_common:
12694 return h->root.u.c.p->section;
12696 default:
12697 break;
12700 else
12701 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12703 return NULL;
12706 /* Return the global debug definition section. */
12708 static asection *
12709 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12710 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12711 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12712 struct elf_link_hash_entry *h,
12713 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12715 if (h != NULL
12716 && (h->root.type == bfd_link_hash_defined
12717 || h->root.type == bfd_link_hash_defweak)
12718 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12719 return h->root.u.def.section;
12721 return NULL;
12724 /* COOKIE->rel describes a relocation against section SEC, which is
12725 a section we've decided to keep. Return the section that contains
12726 the relocation symbol, or NULL if no section contains it. */
12728 asection *
12729 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12730 elf_gc_mark_hook_fn gc_mark_hook,
12731 struct elf_reloc_cookie *cookie,
12732 bfd_boolean *start_stop)
12734 unsigned long r_symndx;
12735 struct elf_link_hash_entry *h;
12737 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12738 if (r_symndx == STN_UNDEF)
12739 return NULL;
12741 if (r_symndx >= cookie->locsymcount
12742 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12744 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12745 if (h == NULL)
12747 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12748 sec->owner);
12749 return NULL;
12751 while (h->root.type == bfd_link_hash_indirect
12752 || h->root.type == bfd_link_hash_warning)
12753 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12754 h->mark = 1;
12755 /* If this symbol is weak and there is a non-weak definition, we
12756 keep the non-weak definition because many backends put
12757 dynamic reloc info on the non-weak definition for code
12758 handling copy relocs. */
12759 if (h->u.weakdef != NULL)
12760 h->u.weakdef->mark = 1;
12762 if (start_stop != NULL)
12764 /* To work around a glibc bug, mark XXX input sections
12765 when there is a reference to __start_XXX or __stop_XXX
12766 symbols. */
12767 if (h->start_stop)
12769 asection *s = h->u2.start_stop_section;
12770 *start_stop = !s->gc_mark;
12771 return s;
12775 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12778 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12779 &cookie->locsyms[r_symndx]);
12782 /* COOKIE->rel describes a relocation against section SEC, which is
12783 a section we've decided to keep. Mark the section that contains
12784 the relocation symbol. */
12786 bfd_boolean
12787 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12788 asection *sec,
12789 elf_gc_mark_hook_fn gc_mark_hook,
12790 struct elf_reloc_cookie *cookie)
12792 asection *rsec;
12793 bfd_boolean start_stop = FALSE;
12795 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12796 while (rsec != NULL)
12798 if (!rsec->gc_mark)
12800 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12801 || (rsec->owner->flags & DYNAMIC) != 0)
12802 rsec->gc_mark = 1;
12803 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12804 return FALSE;
12806 if (!start_stop)
12807 break;
12808 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12810 return TRUE;
12813 /* The mark phase of garbage collection. For a given section, mark
12814 it and any sections in this section's group, and all the sections
12815 which define symbols to which it refers. */
12817 bfd_boolean
12818 _bfd_elf_gc_mark (struct bfd_link_info *info,
12819 asection *sec,
12820 elf_gc_mark_hook_fn gc_mark_hook)
12822 bfd_boolean ret;
12823 asection *group_sec, *eh_frame;
12825 sec->gc_mark = 1;
12827 /* Mark all the sections in the group. */
12828 group_sec = elf_section_data (sec)->next_in_group;
12829 if (group_sec && !group_sec->gc_mark)
12830 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12831 return FALSE;
12833 /* Look through the section relocs. */
12834 ret = TRUE;
12835 eh_frame = elf_eh_frame_section (sec->owner);
12836 if ((sec->flags & SEC_RELOC) != 0
12837 && sec->reloc_count > 0
12838 && sec != eh_frame)
12840 struct elf_reloc_cookie cookie;
12842 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12843 ret = FALSE;
12844 else
12846 for (; cookie.rel < cookie.relend; cookie.rel++)
12847 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12849 ret = FALSE;
12850 break;
12852 fini_reloc_cookie_for_section (&cookie, sec);
12856 if (ret && eh_frame && elf_fde_list (sec))
12858 struct elf_reloc_cookie cookie;
12860 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12861 ret = FALSE;
12862 else
12864 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12865 gc_mark_hook, &cookie))
12866 ret = FALSE;
12867 fini_reloc_cookie_for_section (&cookie, eh_frame);
12871 eh_frame = elf_section_eh_frame_entry (sec);
12872 if (ret && eh_frame && !eh_frame->gc_mark)
12873 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12874 ret = FALSE;
12876 return ret;
12879 /* Scan and mark sections in a special or debug section group. */
12881 static void
12882 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12884 /* Point to first section of section group. */
12885 asection *ssec;
12886 /* Used to iterate the section group. */
12887 asection *msec;
12889 bfd_boolean is_special_grp = TRUE;
12890 bfd_boolean is_debug_grp = TRUE;
12892 /* First scan to see if group contains any section other than debug
12893 and special section. */
12894 ssec = msec = elf_next_in_group (grp);
12897 if ((msec->flags & SEC_DEBUGGING) == 0)
12898 is_debug_grp = FALSE;
12900 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12901 is_special_grp = FALSE;
12903 msec = elf_next_in_group (msec);
12905 while (msec != ssec);
12907 /* If this is a pure debug section group or pure special section group,
12908 keep all sections in this group. */
12909 if (is_debug_grp || is_special_grp)
12913 msec->gc_mark = 1;
12914 msec = elf_next_in_group (msec);
12916 while (msec != ssec);
12920 /* Keep debug and special sections. */
12922 bfd_boolean
12923 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12924 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12926 bfd *ibfd;
12928 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12930 asection *isec;
12931 bfd_boolean some_kept;
12932 bfd_boolean debug_frag_seen;
12933 bfd_boolean has_kept_debug_info;
12935 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12936 continue;
12937 isec = ibfd->sections;
12938 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12939 continue;
12941 /* Ensure all linker created sections are kept,
12942 see if any other section is already marked,
12943 and note if we have any fragmented debug sections. */
12944 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
12945 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12947 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12948 isec->gc_mark = 1;
12949 else if (isec->gc_mark
12950 && (isec->flags & SEC_ALLOC) != 0
12951 && elf_section_type (isec) != SHT_NOTE)
12952 some_kept = TRUE;
12954 if (!debug_frag_seen
12955 && (isec->flags & SEC_DEBUGGING)
12956 && CONST_STRNEQ (isec->name, ".debug_line."))
12957 debug_frag_seen = TRUE;
12960 /* If no non-note alloc section in this file will be kept, then
12961 we can toss out the debug and special sections. */
12962 if (!some_kept)
12963 continue;
12965 /* Keep debug and special sections like .comment when they are
12966 not part of a group. Also keep section groups that contain
12967 just debug sections or special sections. */
12968 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12970 if ((isec->flags & SEC_GROUP) != 0)
12971 _bfd_elf_gc_mark_debug_special_section_group (isec);
12972 else if (((isec->flags & SEC_DEBUGGING) != 0
12973 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12974 && elf_next_in_group (isec) == NULL)
12975 isec->gc_mark = 1;
12976 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12977 has_kept_debug_info = TRUE;
12980 /* Look for CODE sections which are going to be discarded,
12981 and find and discard any fragmented debug sections which
12982 are associated with that code section. */
12983 if (debug_frag_seen)
12984 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12985 if ((isec->flags & SEC_CODE) != 0
12986 && isec->gc_mark == 0)
12988 unsigned int ilen;
12989 asection *dsec;
12991 ilen = strlen (isec->name);
12993 /* Association is determined by the name of the debug
12994 section containing the name of the code section as
12995 a suffix. For example .debug_line.text.foo is a
12996 debug section associated with .text.foo. */
12997 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12999 unsigned int dlen;
13001 if (dsec->gc_mark == 0
13002 || (dsec->flags & SEC_DEBUGGING) == 0)
13003 continue;
13005 dlen = strlen (dsec->name);
13007 if (dlen > ilen
13008 && strncmp (dsec->name + (dlen - ilen),
13009 isec->name, ilen) == 0)
13010 dsec->gc_mark = 0;
13014 /* Mark debug sections referenced by kept debug sections. */
13015 if (has_kept_debug_info)
13016 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13017 if (isec->gc_mark
13018 && (isec->flags & SEC_DEBUGGING) != 0)
13019 if (!_bfd_elf_gc_mark (info, isec,
13020 elf_gc_mark_debug_section))
13021 return FALSE;
13023 return TRUE;
13026 static bfd_boolean
13027 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13029 bfd *sub;
13030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13032 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13034 asection *o;
13036 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13037 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13038 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13039 continue;
13040 o = sub->sections;
13041 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13042 continue;
13044 for (o = sub->sections; o != NULL; o = o->next)
13046 /* When any section in a section group is kept, we keep all
13047 sections in the section group. If the first member of
13048 the section group is excluded, we will also exclude the
13049 group section. */
13050 if (o->flags & SEC_GROUP)
13052 asection *first = elf_next_in_group (o);
13053 o->gc_mark = first->gc_mark;
13056 if (o->gc_mark)
13057 continue;
13059 /* Skip sweeping sections already excluded. */
13060 if (o->flags & SEC_EXCLUDE)
13061 continue;
13063 /* Since this is early in the link process, it is simple
13064 to remove a section from the output. */
13065 o->flags |= SEC_EXCLUDE;
13067 if (info->print_gc_sections && o->size != 0)
13068 /* xgettext:c-format */
13069 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13070 o, sub);
13074 return TRUE;
13077 /* Propagate collected vtable information. This is called through
13078 elf_link_hash_traverse. */
13080 static bfd_boolean
13081 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13083 /* Those that are not vtables. */
13084 if (h->start_stop
13085 || h->u2.vtable == NULL
13086 || h->u2.vtable->parent == NULL)
13087 return TRUE;
13089 /* Those vtables that do not have parents, we cannot merge. */
13090 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13091 return TRUE;
13093 /* If we've already been done, exit. */
13094 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13095 return TRUE;
13097 /* Make sure the parent's table is up to date. */
13098 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13100 if (h->u2.vtable->used == NULL)
13102 /* None of this table's entries were referenced. Re-use the
13103 parent's table. */
13104 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13105 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13107 else
13109 size_t n;
13110 bfd_boolean *cu, *pu;
13112 /* Or the parent's entries into ours. */
13113 cu = h->u2.vtable->used;
13114 cu[-1] = TRUE;
13115 pu = h->u2.vtable->parent->u2.vtable->used;
13116 if (pu != NULL)
13118 const struct elf_backend_data *bed;
13119 unsigned int log_file_align;
13121 bed = get_elf_backend_data (h->root.u.def.section->owner);
13122 log_file_align = bed->s->log_file_align;
13123 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13124 while (n--)
13126 if (*pu)
13127 *cu = TRUE;
13128 pu++;
13129 cu++;
13134 return TRUE;
13137 static bfd_boolean
13138 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13140 asection *sec;
13141 bfd_vma hstart, hend;
13142 Elf_Internal_Rela *relstart, *relend, *rel;
13143 const struct elf_backend_data *bed;
13144 unsigned int log_file_align;
13146 /* Take care of both those symbols that do not describe vtables as
13147 well as those that are not loaded. */
13148 if (h->start_stop
13149 || h->u2.vtable == NULL
13150 || h->u2.vtable->parent == NULL)
13151 return TRUE;
13153 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13154 || h->root.type == bfd_link_hash_defweak);
13156 sec = h->root.u.def.section;
13157 hstart = h->root.u.def.value;
13158 hend = hstart + h->size;
13160 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13161 if (!relstart)
13162 return *(bfd_boolean *) okp = FALSE;
13163 bed = get_elf_backend_data (sec->owner);
13164 log_file_align = bed->s->log_file_align;
13166 relend = relstart + sec->reloc_count;
13168 for (rel = relstart; rel < relend; ++rel)
13169 if (rel->r_offset >= hstart && rel->r_offset < hend)
13171 /* If the entry is in use, do nothing. */
13172 if (h->u2.vtable->used
13173 && (rel->r_offset - hstart) < h->u2.vtable->size)
13175 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13176 if (h->u2.vtable->used[entry])
13177 continue;
13179 /* Otherwise, kill it. */
13180 rel->r_offset = rel->r_info = rel->r_addend = 0;
13183 return TRUE;
13186 /* Mark sections containing dynamically referenced symbols. When
13187 building shared libraries, we must assume that any visible symbol is
13188 referenced. */
13190 bfd_boolean
13191 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13193 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13194 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13196 if ((h->root.type == bfd_link_hash_defined
13197 || h->root.type == bfd_link_hash_defweak)
13198 && (h->ref_dynamic
13199 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13200 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13201 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13202 && (!bfd_link_executable (info)
13203 || info->gc_keep_exported
13204 || info->export_dynamic
13205 || (h->dynamic
13206 && d != NULL
13207 && (*d->match) (&d->head, NULL, h->root.root.string)))
13208 && (h->versioned >= versioned
13209 || !bfd_hide_sym_by_version (info->version_info,
13210 h->root.root.string)))))
13211 h->root.u.def.section->flags |= SEC_KEEP;
13213 return TRUE;
13216 /* Keep all sections containing symbols undefined on the command-line,
13217 and the section containing the entry symbol. */
13219 void
13220 _bfd_elf_gc_keep (struct bfd_link_info *info)
13222 struct bfd_sym_chain *sym;
13224 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13226 struct elf_link_hash_entry *h;
13228 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13229 FALSE, FALSE, FALSE);
13231 if (h != NULL
13232 && (h->root.type == bfd_link_hash_defined
13233 || h->root.type == bfd_link_hash_defweak)
13234 && !bfd_is_abs_section (h->root.u.def.section)
13235 && !bfd_is_und_section (h->root.u.def.section))
13236 h->root.u.def.section->flags |= SEC_KEEP;
13240 bfd_boolean
13241 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13242 struct bfd_link_info *info)
13244 bfd *ibfd = info->input_bfds;
13246 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13248 asection *sec;
13249 struct elf_reloc_cookie cookie;
13251 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13252 continue;
13253 sec = ibfd->sections;
13254 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13255 continue;
13257 if (!init_reloc_cookie (&cookie, info, ibfd))
13258 return FALSE;
13260 for (sec = ibfd->sections; sec; sec = sec->next)
13262 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13263 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13265 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13266 fini_reloc_cookie_rels (&cookie, sec);
13270 return TRUE;
13273 /* Do mark and sweep of unused sections. */
13275 bfd_boolean
13276 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13278 bfd_boolean ok = TRUE;
13279 bfd *sub;
13280 elf_gc_mark_hook_fn gc_mark_hook;
13281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13282 struct elf_link_hash_table *htab;
13284 if (!bed->can_gc_sections
13285 || !is_elf_hash_table (info->hash))
13287 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13288 return TRUE;
13291 bed->gc_keep (info);
13292 htab = elf_hash_table (info);
13294 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13295 at the .eh_frame section if we can mark the FDEs individually. */
13296 for (sub = info->input_bfds;
13297 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13298 sub = sub->link.next)
13300 asection *sec;
13301 struct elf_reloc_cookie cookie;
13303 sec = sub->sections;
13304 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13305 continue;
13306 sec = bfd_get_section_by_name (sub, ".eh_frame");
13307 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13309 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13310 if (elf_section_data (sec)->sec_info
13311 && (sec->flags & SEC_LINKER_CREATED) == 0)
13312 elf_eh_frame_section (sub) = sec;
13313 fini_reloc_cookie_for_section (&cookie, sec);
13314 sec = bfd_get_next_section_by_name (NULL, sec);
13318 /* Apply transitive closure to the vtable entry usage info. */
13319 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13320 if (!ok)
13321 return FALSE;
13323 /* Kill the vtable relocations that were not used. */
13324 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13325 if (!ok)
13326 return FALSE;
13328 /* Mark dynamically referenced symbols. */
13329 if (htab->dynamic_sections_created || info->gc_keep_exported)
13330 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13332 /* Grovel through relocs to find out who stays ... */
13333 gc_mark_hook = bed->gc_mark_hook;
13334 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13336 asection *o;
13338 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13339 || elf_object_id (sub) != elf_hash_table_id (htab)
13340 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13341 continue;
13343 o = sub->sections;
13344 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13345 continue;
13347 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13348 Also treat note sections as a root, if the section is not part
13349 of a group. */
13350 for (o = sub->sections; o != NULL; o = o->next)
13351 if (!o->gc_mark
13352 && (o->flags & SEC_EXCLUDE) == 0
13353 && ((o->flags & SEC_KEEP) != 0
13354 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13355 && elf_next_in_group (o) == NULL )))
13357 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13358 return FALSE;
13362 /* Allow the backend to mark additional target specific sections. */
13363 bed->gc_mark_extra_sections (info, gc_mark_hook);
13365 /* ... and mark SEC_EXCLUDE for those that go. */
13366 return elf_gc_sweep (abfd, info);
13369 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13371 bfd_boolean
13372 bfd_elf_gc_record_vtinherit (bfd *abfd,
13373 asection *sec,
13374 struct elf_link_hash_entry *h,
13375 bfd_vma offset)
13377 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13378 struct elf_link_hash_entry **search, *child;
13379 size_t extsymcount;
13380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13382 /* The sh_info field of the symtab header tells us where the
13383 external symbols start. We don't care about the local symbols at
13384 this point. */
13385 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13386 if (!elf_bad_symtab (abfd))
13387 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13389 sym_hashes = elf_sym_hashes (abfd);
13390 sym_hashes_end = sym_hashes + extsymcount;
13392 /* Hunt down the child symbol, which is in this section at the same
13393 offset as the relocation. */
13394 for (search = sym_hashes; search != sym_hashes_end; ++search)
13396 if ((child = *search) != NULL
13397 && (child->root.type == bfd_link_hash_defined
13398 || child->root.type == bfd_link_hash_defweak)
13399 && child->root.u.def.section == sec
13400 && child->root.u.def.value == offset)
13401 goto win;
13404 /* xgettext:c-format */
13405 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13406 abfd, sec, offset);
13407 bfd_set_error (bfd_error_invalid_operation);
13408 return FALSE;
13410 win:
13411 if (!child->u2.vtable)
13413 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13414 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13415 if (!child->u2.vtable)
13416 return FALSE;
13418 if (!h)
13420 /* This *should* only be the absolute section. It could potentially
13421 be that someone has defined a non-global vtable though, which
13422 would be bad. It isn't worth paging in the local symbols to be
13423 sure though; that case should simply be handled by the assembler. */
13425 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13427 else
13428 child->u2.vtable->parent = h;
13430 return TRUE;
13433 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13435 bfd_boolean
13436 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13437 asection *sec ATTRIBUTE_UNUSED,
13438 struct elf_link_hash_entry *h,
13439 bfd_vma addend)
13441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13442 unsigned int log_file_align = bed->s->log_file_align;
13444 if (!h->u2.vtable)
13446 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13447 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13448 if (!h->u2.vtable)
13449 return FALSE;
13452 if (addend >= h->u2.vtable->size)
13454 size_t size, bytes, file_align;
13455 bfd_boolean *ptr = h->u2.vtable->used;
13457 /* While the symbol is undefined, we have to be prepared to handle
13458 a zero size. */
13459 file_align = 1 << log_file_align;
13460 if (h->root.type == bfd_link_hash_undefined)
13461 size = addend + file_align;
13462 else
13464 size = h->size;
13465 if (addend >= size)
13467 /* Oops! We've got a reference past the defined end of
13468 the table. This is probably a bug -- shall we warn? */
13469 size = addend + file_align;
13472 size = (size + file_align - 1) & -file_align;
13474 /* Allocate one extra entry for use as a "done" flag for the
13475 consolidation pass. */
13476 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13478 if (ptr)
13480 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13482 if (ptr != NULL)
13484 size_t oldbytes;
13486 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13487 * sizeof (bfd_boolean));
13488 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13491 else
13492 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13494 if (ptr == NULL)
13495 return FALSE;
13497 /* And arrange for that done flag to be at index -1. */
13498 h->u2.vtable->used = ptr + 1;
13499 h->u2.vtable->size = size;
13502 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13504 return TRUE;
13507 /* Map an ELF section header flag to its corresponding string. */
13508 typedef struct
13510 char *flag_name;
13511 flagword flag_value;
13512 } elf_flags_to_name_table;
13514 static elf_flags_to_name_table elf_flags_to_names [] =
13516 { "SHF_WRITE", SHF_WRITE },
13517 { "SHF_ALLOC", SHF_ALLOC },
13518 { "SHF_EXECINSTR", SHF_EXECINSTR },
13519 { "SHF_MERGE", SHF_MERGE },
13520 { "SHF_STRINGS", SHF_STRINGS },
13521 { "SHF_INFO_LINK", SHF_INFO_LINK},
13522 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13523 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13524 { "SHF_GROUP", SHF_GROUP },
13525 { "SHF_TLS", SHF_TLS },
13526 { "SHF_MASKOS", SHF_MASKOS },
13527 { "SHF_EXCLUDE", SHF_EXCLUDE },
13530 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13531 bfd_boolean
13532 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13533 struct flag_info *flaginfo,
13534 asection *section)
13536 const bfd_vma sh_flags = elf_section_flags (section);
13538 if (!flaginfo->flags_initialized)
13540 bfd *obfd = info->output_bfd;
13541 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13542 struct flag_info_list *tf = flaginfo->flag_list;
13543 int with_hex = 0;
13544 int without_hex = 0;
13546 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13548 unsigned i;
13549 flagword (*lookup) (char *);
13551 lookup = bed->elf_backend_lookup_section_flags_hook;
13552 if (lookup != NULL)
13554 flagword hexval = (*lookup) ((char *) tf->name);
13556 if (hexval != 0)
13558 if (tf->with == with_flags)
13559 with_hex |= hexval;
13560 else if (tf->with == without_flags)
13561 without_hex |= hexval;
13562 tf->valid = TRUE;
13563 continue;
13566 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13568 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13570 if (tf->with == with_flags)
13571 with_hex |= elf_flags_to_names[i].flag_value;
13572 else if (tf->with == without_flags)
13573 without_hex |= elf_flags_to_names[i].flag_value;
13574 tf->valid = TRUE;
13575 break;
13578 if (!tf->valid)
13580 info->callbacks->einfo
13581 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13582 return FALSE;
13585 flaginfo->flags_initialized = TRUE;
13586 flaginfo->only_with_flags |= with_hex;
13587 flaginfo->not_with_flags |= without_hex;
13590 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13591 return FALSE;
13593 if ((flaginfo->not_with_flags & sh_flags) != 0)
13594 return FALSE;
13596 return TRUE;
13599 struct alloc_got_off_arg {
13600 bfd_vma gotoff;
13601 struct bfd_link_info *info;
13604 /* We need a special top-level link routine to convert got reference counts
13605 to real got offsets. */
13607 static bfd_boolean
13608 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13610 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13611 bfd *obfd = gofarg->info->output_bfd;
13612 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13614 if (h->got.refcount > 0)
13616 h->got.offset = gofarg->gotoff;
13617 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13619 else
13620 h->got.offset = (bfd_vma) -1;
13622 return TRUE;
13625 /* And an accompanying bit to work out final got entry offsets once
13626 we're done. Should be called from final_link. */
13628 bfd_boolean
13629 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13630 struct bfd_link_info *info)
13632 bfd *i;
13633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13634 bfd_vma gotoff;
13635 struct alloc_got_off_arg gofarg;
13637 BFD_ASSERT (abfd == info->output_bfd);
13639 if (! is_elf_hash_table (info->hash))
13640 return FALSE;
13642 /* The GOT offset is relative to the .got section, but the GOT header is
13643 put into the .got.plt section, if the backend uses it. */
13644 if (bed->want_got_plt)
13645 gotoff = 0;
13646 else
13647 gotoff = bed->got_header_size;
13649 /* Do the local .got entries first. */
13650 for (i = info->input_bfds; i; i = i->link.next)
13652 bfd_signed_vma *local_got;
13653 size_t j, locsymcount;
13654 Elf_Internal_Shdr *symtab_hdr;
13656 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13657 continue;
13659 local_got = elf_local_got_refcounts (i);
13660 if (!local_got)
13661 continue;
13663 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13664 if (elf_bad_symtab (i))
13665 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13666 else
13667 locsymcount = symtab_hdr->sh_info;
13669 for (j = 0; j < locsymcount; ++j)
13671 if (local_got[j] > 0)
13673 local_got[j] = gotoff;
13674 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13676 else
13677 local_got[j] = (bfd_vma) -1;
13681 /* Then the global .got entries. .plt refcounts are handled by
13682 adjust_dynamic_symbol */
13683 gofarg.gotoff = gotoff;
13684 gofarg.info = info;
13685 elf_link_hash_traverse (elf_hash_table (info),
13686 elf_gc_allocate_got_offsets,
13687 &gofarg);
13688 return TRUE;
13691 /* Many folk need no more in the way of final link than this, once
13692 got entry reference counting is enabled. */
13694 bfd_boolean
13695 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13697 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13698 return FALSE;
13700 /* Invoke the regular ELF backend linker to do all the work. */
13701 return bfd_elf_final_link (abfd, info);
13704 bfd_boolean
13705 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13707 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13709 if (rcookie->bad_symtab)
13710 rcookie->rel = rcookie->rels;
13712 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13714 unsigned long r_symndx;
13716 if (! rcookie->bad_symtab)
13717 if (rcookie->rel->r_offset > offset)
13718 return FALSE;
13719 if (rcookie->rel->r_offset != offset)
13720 continue;
13722 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13723 if (r_symndx == STN_UNDEF)
13724 return TRUE;
13726 if (r_symndx >= rcookie->locsymcount
13727 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13729 struct elf_link_hash_entry *h;
13731 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13733 while (h->root.type == bfd_link_hash_indirect
13734 || h->root.type == bfd_link_hash_warning)
13735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13737 if ((h->root.type == bfd_link_hash_defined
13738 || h->root.type == bfd_link_hash_defweak)
13739 && (h->root.u.def.section->owner != rcookie->abfd
13740 || h->root.u.def.section->kept_section != NULL
13741 || discarded_section (h->root.u.def.section)))
13742 return TRUE;
13744 else
13746 /* It's not a relocation against a global symbol,
13747 but it could be a relocation against a local
13748 symbol for a discarded section. */
13749 asection *isec;
13750 Elf_Internal_Sym *isym;
13752 /* Need to: get the symbol; get the section. */
13753 isym = &rcookie->locsyms[r_symndx];
13754 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13755 if (isec != NULL
13756 && (isec->kept_section != NULL
13757 || discarded_section (isec)))
13758 return TRUE;
13760 return FALSE;
13762 return FALSE;
13765 /* Discard unneeded references to discarded sections.
13766 Returns -1 on error, 1 if any section's size was changed, 0 if
13767 nothing changed. This function assumes that the relocations are in
13768 sorted order, which is true for all known assemblers. */
13771 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13773 struct elf_reloc_cookie cookie;
13774 asection *o;
13775 bfd *abfd;
13776 int changed = 0;
13778 if (info->traditional_format
13779 || !is_elf_hash_table (info->hash))
13780 return 0;
13782 o = bfd_get_section_by_name (output_bfd, ".stab");
13783 if (o != NULL)
13785 asection *i;
13787 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13789 if (i->size == 0
13790 || i->reloc_count == 0
13791 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13792 continue;
13794 abfd = i->owner;
13795 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13796 continue;
13798 if (!init_reloc_cookie_for_section (&cookie, info, i))
13799 return -1;
13801 if (_bfd_discard_section_stabs (abfd, i,
13802 elf_section_data (i)->sec_info,
13803 bfd_elf_reloc_symbol_deleted_p,
13804 &cookie))
13805 changed = 1;
13807 fini_reloc_cookie_for_section (&cookie, i);
13811 o = NULL;
13812 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13813 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13814 if (o != NULL)
13816 asection *i;
13817 int eh_changed = 0;
13818 unsigned int eh_alignment;
13820 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13822 if (i->size == 0)
13823 continue;
13825 abfd = i->owner;
13826 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13827 continue;
13829 if (!init_reloc_cookie_for_section (&cookie, info, i))
13830 return -1;
13832 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13833 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13834 bfd_elf_reloc_symbol_deleted_p,
13835 &cookie))
13837 eh_changed = 1;
13838 if (i->size != i->rawsize)
13839 changed = 1;
13842 fini_reloc_cookie_for_section (&cookie, i);
13845 eh_alignment = 1 << o->alignment_power;
13846 /* Skip over zero terminator, and prevent empty sections from
13847 adding alignment padding at the end. */
13848 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13849 if (i->size == 0)
13850 i->flags |= SEC_EXCLUDE;
13851 else if (i->size > 4)
13852 break;
13853 /* The last non-empty eh_frame section doesn't need padding. */
13854 if (i != NULL)
13855 i = i->map_tail.s;
13856 /* Any prior sections must pad the last FDE out to the output
13857 section alignment. Otherwise we might have zero padding
13858 between sections, which would be seen as a terminator. */
13859 for (; i != NULL; i = i->map_tail.s)
13860 if (i->size == 4)
13861 /* All but the last zero terminator should have been removed. */
13862 BFD_FAIL ();
13863 else
13865 bfd_size_type size
13866 = (i->size + eh_alignment - 1) & -eh_alignment;
13867 if (i->size != size)
13869 i->size = size;
13870 changed = 1;
13871 eh_changed = 1;
13874 if (eh_changed)
13875 elf_link_hash_traverse (elf_hash_table (info),
13876 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13879 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13881 const struct elf_backend_data *bed;
13882 asection *s;
13884 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13885 continue;
13886 s = abfd->sections;
13887 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13888 continue;
13890 bed = get_elf_backend_data (abfd);
13892 if (bed->elf_backend_discard_info != NULL)
13894 if (!init_reloc_cookie (&cookie, info, abfd))
13895 return -1;
13897 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13898 changed = 1;
13900 fini_reloc_cookie (&cookie, abfd);
13904 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13905 _bfd_elf_end_eh_frame_parsing (info);
13907 if (info->eh_frame_hdr_type
13908 && !bfd_link_relocatable (info)
13909 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13910 changed = 1;
13912 return changed;
13915 bfd_boolean
13916 _bfd_elf_section_already_linked (bfd *abfd,
13917 asection *sec,
13918 struct bfd_link_info *info)
13920 flagword flags;
13921 const char *name, *key;
13922 struct bfd_section_already_linked *l;
13923 struct bfd_section_already_linked_hash_entry *already_linked_list;
13925 if (sec->output_section == bfd_abs_section_ptr)
13926 return FALSE;
13928 flags = sec->flags;
13930 /* Return if it isn't a linkonce section. A comdat group section
13931 also has SEC_LINK_ONCE set. */
13932 if ((flags & SEC_LINK_ONCE) == 0)
13933 return FALSE;
13935 /* Don't put group member sections on our list of already linked
13936 sections. They are handled as a group via their group section. */
13937 if (elf_sec_group (sec) != NULL)
13938 return FALSE;
13940 /* For a SHT_GROUP section, use the group signature as the key. */
13941 name = sec->name;
13942 if ((flags & SEC_GROUP) != 0
13943 && elf_next_in_group (sec) != NULL
13944 && elf_group_name (elf_next_in_group (sec)) != NULL)
13945 key = elf_group_name (elf_next_in_group (sec));
13946 else
13948 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13949 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13950 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13951 key++;
13952 else
13953 /* Must be a user linkonce section that doesn't follow gcc's
13954 naming convention. In this case we won't be matching
13955 single member groups. */
13956 key = name;
13959 already_linked_list = bfd_section_already_linked_table_lookup (key);
13961 for (l = already_linked_list->entry; l != NULL; l = l->next)
13963 /* We may have 2 different types of sections on the list: group
13964 sections with a signature of <key> (<key> is some string),
13965 and linkonce sections named .gnu.linkonce.<type>.<key>.
13966 Match like sections. LTO plugin sections are an exception.
13967 They are always named .gnu.linkonce.t.<key> and match either
13968 type of section. */
13969 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13970 && ((flags & SEC_GROUP) != 0
13971 || strcmp (name, l->sec->name) == 0))
13972 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13974 /* The section has already been linked. See if we should
13975 issue a warning. */
13976 if (!_bfd_handle_already_linked (sec, l, info))
13977 return FALSE;
13979 if (flags & SEC_GROUP)
13981 asection *first = elf_next_in_group (sec);
13982 asection *s = first;
13984 while (s != NULL)
13986 s->output_section = bfd_abs_section_ptr;
13987 /* Record which group discards it. */
13988 s->kept_section = l->sec;
13989 s = elf_next_in_group (s);
13990 /* These lists are circular. */
13991 if (s == first)
13992 break;
13996 return TRUE;
14000 /* A single member comdat group section may be discarded by a
14001 linkonce section and vice versa. */
14002 if ((flags & SEC_GROUP) != 0)
14004 asection *first = elf_next_in_group (sec);
14006 if (first != NULL && elf_next_in_group (first) == first)
14007 /* Check this single member group against linkonce sections. */
14008 for (l = already_linked_list->entry; l != NULL; l = l->next)
14009 if ((l->sec->flags & SEC_GROUP) == 0
14010 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14012 first->output_section = bfd_abs_section_ptr;
14013 first->kept_section = l->sec;
14014 sec->output_section = bfd_abs_section_ptr;
14015 break;
14018 else
14019 /* Check this linkonce section against single member groups. */
14020 for (l = already_linked_list->entry; l != NULL; l = l->next)
14021 if (l->sec->flags & SEC_GROUP)
14023 asection *first = elf_next_in_group (l->sec);
14025 if (first != NULL
14026 && elf_next_in_group (first) == first
14027 && bfd_elf_match_symbols_in_sections (first, sec, info))
14029 sec->output_section = bfd_abs_section_ptr;
14030 sec->kept_section = first;
14031 break;
14035 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14036 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14037 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14038 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14039 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14040 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14041 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14042 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14043 The reverse order cannot happen as there is never a bfd with only the
14044 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14045 matter as here were are looking only for cross-bfd sections. */
14047 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14048 for (l = already_linked_list->entry; l != NULL; l = l->next)
14049 if ((l->sec->flags & SEC_GROUP) == 0
14050 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14052 if (abfd != l->sec->owner)
14053 sec->output_section = bfd_abs_section_ptr;
14054 break;
14057 /* This is the first section with this name. Record it. */
14058 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14059 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14060 return sec->output_section == bfd_abs_section_ptr;
14063 bfd_boolean
14064 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14066 return sym->st_shndx == SHN_COMMON;
14069 unsigned int
14070 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14072 return SHN_COMMON;
14075 asection *
14076 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14078 return bfd_com_section_ptr;
14081 bfd_vma
14082 _bfd_elf_default_got_elt_size (bfd *abfd,
14083 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14084 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14085 bfd *ibfd ATTRIBUTE_UNUSED,
14086 unsigned long symndx ATTRIBUTE_UNUSED)
14088 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14089 return bed->s->arch_size / 8;
14092 /* Routines to support the creation of dynamic relocs. */
14094 /* Returns the name of the dynamic reloc section associated with SEC. */
14096 static const char *
14097 get_dynamic_reloc_section_name (bfd * abfd,
14098 asection * sec,
14099 bfd_boolean is_rela)
14101 char *name;
14102 const char *old_name = bfd_get_section_name (NULL, sec);
14103 const char *prefix = is_rela ? ".rela" : ".rel";
14105 if (old_name == NULL)
14106 return NULL;
14108 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14109 sprintf (name, "%s%s", prefix, old_name);
14111 return name;
14114 /* Returns the dynamic reloc section associated with SEC.
14115 If necessary compute the name of the dynamic reloc section based
14116 on SEC's name (looked up in ABFD's string table) and the setting
14117 of IS_RELA. */
14119 asection *
14120 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14121 asection * sec,
14122 bfd_boolean is_rela)
14124 asection * reloc_sec = elf_section_data (sec)->sreloc;
14126 if (reloc_sec == NULL)
14128 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14130 if (name != NULL)
14132 reloc_sec = bfd_get_linker_section (abfd, name);
14134 if (reloc_sec != NULL)
14135 elf_section_data (sec)->sreloc = reloc_sec;
14139 return reloc_sec;
14142 /* Returns the dynamic reloc section associated with SEC. If the
14143 section does not exist it is created and attached to the DYNOBJ
14144 bfd and stored in the SRELOC field of SEC's elf_section_data
14145 structure.
14147 ALIGNMENT is the alignment for the newly created section and
14148 IS_RELA defines whether the name should be .rela.<SEC's name>
14149 or .rel.<SEC's name>. The section name is looked up in the
14150 string table associated with ABFD. */
14152 asection *
14153 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14154 bfd *dynobj,
14155 unsigned int alignment,
14156 bfd *abfd,
14157 bfd_boolean is_rela)
14159 asection * reloc_sec = elf_section_data (sec)->sreloc;
14161 if (reloc_sec == NULL)
14163 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14165 if (name == NULL)
14166 return NULL;
14168 reloc_sec = bfd_get_linker_section (dynobj, name);
14170 if (reloc_sec == NULL)
14172 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14173 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14174 if ((sec->flags & SEC_ALLOC) != 0)
14175 flags |= SEC_ALLOC | SEC_LOAD;
14177 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14178 if (reloc_sec != NULL)
14180 /* _bfd_elf_get_sec_type_attr chooses a section type by
14181 name. Override as it may be wrong, eg. for a user
14182 section named "auto" we'll get ".relauto" which is
14183 seen to be a .rela section. */
14184 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14185 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14186 reloc_sec = NULL;
14190 elf_section_data (sec)->sreloc = reloc_sec;
14193 return reloc_sec;
14196 /* Copy the ELF symbol type and other attributes for a linker script
14197 assignment from HSRC to HDEST. Generally this should be treated as
14198 if we found a strong non-dynamic definition for HDEST (except that
14199 ld ignores multiple definition errors). */
14200 void
14201 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14202 struct bfd_link_hash_entry *hdest,
14203 struct bfd_link_hash_entry *hsrc)
14205 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14206 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14207 Elf_Internal_Sym isym;
14209 ehdest->type = ehsrc->type;
14210 ehdest->target_internal = ehsrc->target_internal;
14212 isym.st_other = ehsrc->other;
14213 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14216 /* Append a RELA relocation REL to section S in BFD. */
14218 void
14219 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14222 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14223 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14224 bed->s->swap_reloca_out (abfd, rel, loc);
14227 /* Append a REL relocation REL to section S in BFD. */
14229 void
14230 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14232 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14233 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14234 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14235 bed->s->swap_reloc_out (abfd, rel, loc);
14238 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14240 struct bfd_link_hash_entry *
14241 bfd_elf_define_start_stop (struct bfd_link_info *info,
14242 const char *symbol, asection *sec)
14244 struct elf_link_hash_entry *h;
14246 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14247 FALSE, FALSE, TRUE);
14248 if (h != NULL
14249 && (h->root.type == bfd_link_hash_undefined
14250 || h->root.type == bfd_link_hash_undefweak
14251 || (h->ref_regular && !h->def_regular)))
14253 h->root.type = bfd_link_hash_defined;
14254 h->root.u.def.section = sec;
14255 h->root.u.def.value = 0;
14256 h->def_regular = 1;
14257 h->def_dynamic = 0;
14258 h->start_stop = 1;
14259 h->u2.start_stop_section = sec;
14260 if (symbol[0] == '.')
14262 /* .startof. and .sizeof. symbols are local. */
14263 const struct elf_backend_data *bed;
14264 bed = get_elf_backend_data (info->output_bfd);
14265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14267 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14268 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14269 return &h->root;
14271 return NULL;