bfd/
[binutils.git] / bfd / elflink.h
blobadecd808cf02e2220c201d5f3200b78b0a72136f
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* ELF linker code. */
23 #include "safe-ctype.h"
25 static bfd_boolean elf_link_add_object_symbols (bfd *, struct bfd_link_info *);
26 static bfd_boolean elf_finalize_dynstr (bfd *, struct bfd_link_info *);
27 static bfd_boolean elf_collect_hash_codes (struct elf_link_hash_entry *,
28 void *);
29 static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
31 /* Given an ELF BFD, add symbols to the global hash table as
32 appropriate. */
34 bfd_boolean
35 elf_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
37 switch (bfd_get_format (abfd))
39 case bfd_object:
40 return elf_link_add_object_symbols (abfd, info);
41 case bfd_archive:
42 return _bfd_elf_link_add_archive_symbols (abfd, info);
43 default:
44 bfd_set_error (bfd_error_wrong_format);
45 return FALSE;
49 /* Sort symbol by value and section. */
50 static int
51 sort_symbol (const void *arg1, const void *arg2)
53 const struct elf_link_hash_entry *h1
54 = *(const struct elf_link_hash_entry **) arg1;
55 const struct elf_link_hash_entry *h2
56 = *(const struct elf_link_hash_entry **) arg2;
57 bfd_signed_vma vdiff = h1->root.u.def.value - h2->root.u.def.value;
59 if (vdiff)
60 return vdiff > 0 ? 1 : -1;
61 else
63 long sdiff = h1->root.u.def.section - h2->root.u.def.section;
64 if (sdiff)
65 return sdiff > 0 ? 1 : -1;
66 else
67 return 0;
71 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
72 1 if a DT_NEEDED tag already exists, and 0 on success. */
74 static int
75 add_dt_needed_tag (struct bfd_link_info *info, const char *soname,
76 bfd_boolean do_it)
78 struct elf_link_hash_table *hash_table;
79 bfd_size_type oldsize;
80 bfd_size_type strindex;
82 hash_table = elf_hash_table (info);
83 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
84 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
85 if (strindex == (bfd_size_type) -1)
86 return -1;
88 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
90 asection *sdyn;
91 Elf_External_Dyn *dyncon, *dynconend;
93 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
94 BFD_ASSERT (sdyn != NULL);
96 dyncon = (Elf_External_Dyn *) sdyn->contents;
97 dynconend = (Elf_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
98 for (; dyncon < dynconend; dyncon++)
100 Elf_Internal_Dyn dyn;
102 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
103 if (dyn.d_tag == DT_NEEDED
104 && dyn.d_un.d_val == strindex)
106 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
107 return 1;
112 if (do_it)
114 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
115 return -1;
117 else
118 /* We were just checking for existence of the tag. */
119 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
121 return 0;
124 /* Add symbols from an ELF object file to the linker hash table. */
126 static bfd_boolean
127 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
129 bfd_boolean (*add_symbol_hook)
130 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
131 const char **, flagword *, asection **, bfd_vma *);
132 bfd_boolean (*check_relocs)
133 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
134 bfd_boolean collect;
135 Elf_Internal_Shdr *hdr;
136 bfd_size_type symcount;
137 bfd_size_type extsymcount;
138 bfd_size_type extsymoff;
139 struct elf_link_hash_entry **sym_hash;
140 bfd_boolean dynamic;
141 Elf_External_Versym *extversym = NULL;
142 Elf_External_Versym *ever;
143 struct elf_link_hash_entry *weaks;
144 struct elf_link_hash_entry **nondeflt_vers = NULL;
145 bfd_size_type nondeflt_vers_cnt = 0;
146 Elf_Internal_Sym *isymbuf = NULL;
147 Elf_Internal_Sym *isym;
148 Elf_Internal_Sym *isymend;
149 const struct elf_backend_data *bed;
150 bfd_boolean add_needed;
151 struct elf_link_hash_table * hash_table;
152 bfd_size_type amt;
154 hash_table = elf_hash_table (info);
156 bed = get_elf_backend_data (abfd);
157 add_symbol_hook = bed->elf_add_symbol_hook;
158 collect = bed->collect;
160 if ((abfd->flags & DYNAMIC) == 0)
161 dynamic = FALSE;
162 else
164 dynamic = TRUE;
166 /* You can't use -r against a dynamic object. Also, there's no
167 hope of using a dynamic object which does not exactly match
168 the format of the output file. */
169 if (info->relocatable
170 || !is_elf_hash_table (hash_table)
171 || hash_table->root.creator != abfd->xvec)
173 bfd_set_error (bfd_error_invalid_operation);
174 goto error_return;
178 /* As a GNU extension, any input sections which are named
179 .gnu.warning.SYMBOL are treated as warning symbols for the given
180 symbol. This differs from .gnu.warning sections, which generate
181 warnings when they are included in an output file. */
182 if (info->executable)
184 asection *s;
186 for (s = abfd->sections; s != NULL; s = s->next)
188 const char *name;
190 name = bfd_get_section_name (abfd, s);
191 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
193 char *msg;
194 bfd_size_type sz;
195 bfd_size_type prefix_len;
196 const char * gnu_warning_prefix = _("warning: ");
198 name += sizeof ".gnu.warning." - 1;
200 /* If this is a shared object, then look up the symbol
201 in the hash table. If it is there, and it is already
202 been defined, then we will not be using the entry
203 from this shared object, so we don't need to warn.
204 FIXME: If we see the definition in a regular object
205 later on, we will warn, but we shouldn't. The only
206 fix is to keep track of what warnings we are supposed
207 to emit, and then handle them all at the end of the
208 link. */
209 if (dynamic)
211 struct elf_link_hash_entry *h;
213 h = elf_link_hash_lookup (hash_table, name,
214 FALSE, FALSE, TRUE);
216 /* FIXME: What about bfd_link_hash_common? */
217 if (h != NULL
218 && (h->root.type == bfd_link_hash_defined
219 || h->root.type == bfd_link_hash_defweak))
221 /* We don't want to issue this warning. Clobber
222 the section size so that the warning does not
223 get copied into the output file. */
224 s->_raw_size = 0;
225 continue;
229 sz = bfd_section_size (abfd, s);
230 prefix_len = strlen (gnu_warning_prefix);
231 msg = bfd_alloc (abfd, prefix_len + sz + 1);
232 if (msg == NULL)
233 goto error_return;
235 strcpy (msg, gnu_warning_prefix);
236 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
237 goto error_return;
239 msg[prefix_len + sz] = '\0';
241 if (! (_bfd_generic_link_add_one_symbol
242 (info, abfd, name, BSF_WARNING, s, 0, msg,
243 FALSE, collect, NULL)))
244 goto error_return;
246 if (! info->relocatable)
248 /* Clobber the section size so that the warning does
249 not get copied into the output file. */
250 s->_raw_size = 0;
256 add_needed = TRUE;
257 if (! dynamic)
259 /* If we are creating a shared library, create all the dynamic
260 sections immediately. We need to attach them to something,
261 so we attach them to this BFD, provided it is the right
262 format. FIXME: If there are no input BFD's of the same
263 format as the output, we can't make a shared library. */
264 if (info->shared
265 && is_elf_hash_table (hash_table)
266 && hash_table->root.creator == abfd->xvec
267 && ! hash_table->dynamic_sections_created)
269 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
270 goto error_return;
273 else if (!is_elf_hash_table (hash_table))
274 goto error_return;
275 else
277 asection *s;
278 const char *soname = NULL;
279 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
280 int ret;
282 /* ld --just-symbols and dynamic objects don't mix very well.
283 Test for --just-symbols by looking at info set up by
284 _bfd_elf_link_just_syms. */
285 if ((s = abfd->sections) != NULL
286 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
287 goto error_return;
289 /* If this dynamic lib was specified on the command line with
290 --as-needed in effect, then we don't want to add a DT_NEEDED
291 tag unless the lib is actually used. Similary for libs brought
292 in by another lib's DT_NEEDED. */
293 add_needed = elf_dyn_lib_class (abfd) == DYN_NORMAL;
295 s = bfd_get_section_by_name (abfd, ".dynamic");
296 if (s != NULL)
298 Elf_External_Dyn *dynbuf = NULL;
299 Elf_External_Dyn *extdyn;
300 Elf_External_Dyn *extdynend;
301 int elfsec;
302 unsigned long shlink;
304 dynbuf = bfd_malloc (s->_raw_size);
305 if (dynbuf == NULL)
306 goto error_return;
308 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
309 goto error_free_dyn;
311 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
312 if (elfsec == -1)
313 goto error_free_dyn;
314 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
316 extdyn = dynbuf;
317 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
318 for (; extdyn < extdynend; extdyn++)
320 Elf_Internal_Dyn dyn;
322 elf_swap_dyn_in (abfd, extdyn, &dyn);
323 if (dyn.d_tag == DT_SONAME)
325 unsigned int tagv = dyn.d_un.d_val;
326 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
327 if (soname == NULL)
328 goto error_free_dyn;
330 if (dyn.d_tag == DT_NEEDED)
332 struct bfd_link_needed_list *n, **pn;
333 char *fnm, *anm;
334 unsigned int tagv = dyn.d_un.d_val;
336 amt = sizeof (struct bfd_link_needed_list);
337 n = bfd_alloc (abfd, amt);
338 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
339 if (n == NULL || fnm == NULL)
340 goto error_free_dyn;
341 amt = strlen (fnm) + 1;
342 anm = bfd_alloc (abfd, amt);
343 if (anm == NULL)
344 goto error_free_dyn;
345 memcpy (anm, fnm, amt);
346 n->name = anm;
347 n->by = abfd;
348 n->next = NULL;
349 for (pn = & hash_table->needed;
350 *pn != NULL;
351 pn = &(*pn)->next)
353 *pn = n;
355 if (dyn.d_tag == DT_RUNPATH)
357 struct bfd_link_needed_list *n, **pn;
358 char *fnm, *anm;
359 unsigned int tagv = dyn.d_un.d_val;
361 amt = sizeof (struct bfd_link_needed_list);
362 n = bfd_alloc (abfd, amt);
363 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
364 if (n == NULL || fnm == NULL)
365 goto error_free_dyn;
366 amt = strlen (fnm) + 1;
367 anm = bfd_alloc (abfd, amt);
368 if (anm == NULL)
369 goto error_free_dyn;
370 memcpy (anm, fnm, amt);
371 n->name = anm;
372 n->by = abfd;
373 n->next = NULL;
374 for (pn = & runpath;
375 *pn != NULL;
376 pn = &(*pn)->next)
378 *pn = n;
380 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
381 if (!runpath && dyn.d_tag == DT_RPATH)
383 struct bfd_link_needed_list *n, **pn;
384 char *fnm, *anm;
385 unsigned int tagv = dyn.d_un.d_val;
387 amt = sizeof (struct bfd_link_needed_list);
388 n = bfd_alloc (abfd, amt);
389 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
390 if (n == NULL || fnm == NULL)
391 goto error_free_dyn;
392 amt = strlen (fnm) + 1;
393 anm = bfd_alloc (abfd, amt);
394 if (anm == NULL)
396 error_free_dyn:
397 free (dynbuf);
398 goto error_return;
400 memcpy (anm, fnm, amt);
401 n->name = anm;
402 n->by = abfd;
403 n->next = NULL;
404 for (pn = & rpath;
405 *pn != NULL;
406 pn = &(*pn)->next)
408 *pn = n;
412 free (dynbuf);
415 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
416 frees all more recently bfd_alloc'd blocks as well. */
417 if (runpath)
418 rpath = runpath;
420 if (rpath)
422 struct bfd_link_needed_list **pn;
423 for (pn = & hash_table->runpath;
424 *pn != NULL;
425 pn = &(*pn)->next)
427 *pn = rpath;
430 /* We do not want to include any of the sections in a dynamic
431 object in the output file. We hack by simply clobbering the
432 list of sections in the BFD. This could be handled more
433 cleanly by, say, a new section flag; the existing
434 SEC_NEVER_LOAD flag is not the one we want, because that one
435 still implies that the section takes up space in the output
436 file. */
437 bfd_section_list_clear (abfd);
439 /* If this is the first dynamic object found in the link, create
440 the special sections required for dynamic linking. */
441 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
442 goto error_return;
444 /* Find the name to use in a DT_NEEDED entry that refers to this
445 object. If the object has a DT_SONAME entry, we use it.
446 Otherwise, if the generic linker stuck something in
447 elf_dt_name, we use that. Otherwise, we just use the file
448 name. */
449 if (soname == NULL || *soname == '\0')
451 soname = elf_dt_name (abfd);
452 if (soname == NULL || *soname == '\0')
453 soname = bfd_get_filename (abfd);
456 /* Save the SONAME because sometimes the linker emulation code
457 will need to know it. */
458 elf_dt_name (abfd) = soname;
460 ret = add_dt_needed_tag (info, soname, add_needed);
461 if (ret < 0)
462 goto error_return;
464 /* If we have already included this dynamic object in the
465 link, just ignore it. There is no reason to include a
466 particular dynamic object more than once. */
467 if (ret > 0)
468 return TRUE;
471 /* If this is a dynamic object, we always link against the .dynsym
472 symbol table, not the .symtab symbol table. The dynamic linker
473 will only see the .dynsym symbol table, so there is no reason to
474 look at .symtab for a dynamic object. */
476 if (! dynamic || elf_dynsymtab (abfd) == 0)
477 hdr = &elf_tdata (abfd)->symtab_hdr;
478 else
479 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
481 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
483 /* The sh_info field of the symtab header tells us where the
484 external symbols start. We don't care about the local symbols at
485 this point. */
486 if (elf_bad_symtab (abfd))
488 extsymcount = symcount;
489 extsymoff = 0;
491 else
493 extsymcount = symcount - hdr->sh_info;
494 extsymoff = hdr->sh_info;
497 sym_hash = NULL;
498 if (extsymcount != 0)
500 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
501 NULL, NULL, NULL);
502 if (isymbuf == NULL)
503 goto error_return;
505 /* We store a pointer to the hash table entry for each external
506 symbol. */
507 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
508 sym_hash = bfd_alloc (abfd, amt);
509 if (sym_hash == NULL)
510 goto error_free_sym;
511 elf_sym_hashes (abfd) = sym_hash;
514 if (dynamic)
516 /* Read in any version definitions. */
517 if (! _bfd_elf_slurp_version_tables (abfd))
518 goto error_free_sym;
520 /* Read in the symbol versions, but don't bother to convert them
521 to internal format. */
522 if (elf_dynversym (abfd) != 0)
524 Elf_Internal_Shdr *versymhdr;
526 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
527 extversym = bfd_malloc (versymhdr->sh_size);
528 if (extversym == NULL)
529 goto error_free_sym;
530 amt = versymhdr->sh_size;
531 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
532 || bfd_bread (extversym, amt, abfd) != amt)
533 goto error_free_vers;
537 weaks = NULL;
539 ever = extversym != NULL ? extversym + extsymoff : NULL;
540 for (isym = isymbuf, isymend = isymbuf + extsymcount;
541 isym < isymend;
542 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
544 int bind;
545 bfd_vma value;
546 asection *sec;
547 flagword flags;
548 const char *name;
549 struct elf_link_hash_entry *h;
550 bfd_boolean definition;
551 bfd_boolean size_change_ok;
552 bfd_boolean type_change_ok;
553 bfd_boolean new_weakdef;
554 bfd_boolean override;
555 unsigned int old_alignment;
556 bfd *old_bfd;
558 override = FALSE;
560 flags = BSF_NO_FLAGS;
561 sec = NULL;
562 value = isym->st_value;
563 *sym_hash = NULL;
565 bind = ELF_ST_BIND (isym->st_info);
566 if (bind == STB_LOCAL)
568 /* This should be impossible, since ELF requires that all
569 global symbols follow all local symbols, and that sh_info
570 point to the first global symbol. Unfortunately, Irix 5
571 screws this up. */
572 continue;
574 else if (bind == STB_GLOBAL)
576 if (isym->st_shndx != SHN_UNDEF
577 && isym->st_shndx != SHN_COMMON)
578 flags = BSF_GLOBAL;
580 else if (bind == STB_WEAK)
581 flags = BSF_WEAK;
582 else
584 /* Leave it up to the processor backend. */
587 if (isym->st_shndx == SHN_UNDEF)
588 sec = bfd_und_section_ptr;
589 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
591 sec = section_from_elf_index (abfd, isym->st_shndx);
592 if (sec == NULL)
593 sec = bfd_abs_section_ptr;
594 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
595 value -= sec->vma;
597 else if (isym->st_shndx == SHN_ABS)
598 sec = bfd_abs_section_ptr;
599 else if (isym->st_shndx == SHN_COMMON)
601 sec = bfd_com_section_ptr;
602 /* What ELF calls the size we call the value. What ELF
603 calls the value we call the alignment. */
604 value = isym->st_size;
606 else
608 /* Leave it up to the processor backend. */
611 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
612 isym->st_name);
613 if (name == NULL)
614 goto error_free_vers;
616 if (isym->st_shndx == SHN_COMMON
617 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
619 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
621 if (tcomm == NULL)
623 tcomm = bfd_make_section (abfd, ".tcommon");
624 if (tcomm == NULL
625 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
626 | SEC_IS_COMMON
627 | SEC_LINKER_CREATED
628 | SEC_THREAD_LOCAL)))
629 goto error_free_vers;
631 sec = tcomm;
633 else if (add_symbol_hook)
635 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
636 &value))
637 goto error_free_vers;
639 /* The hook function sets the name to NULL if this symbol
640 should be skipped for some reason. */
641 if (name == NULL)
642 continue;
645 /* Sanity check that all possibilities were handled. */
646 if (sec == NULL)
648 bfd_set_error (bfd_error_bad_value);
649 goto error_free_vers;
652 if (bfd_is_und_section (sec)
653 || bfd_is_com_section (sec))
654 definition = FALSE;
655 else
656 definition = TRUE;
658 size_change_ok = FALSE;
659 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
660 old_alignment = 0;
661 old_bfd = NULL;
663 if (is_elf_hash_table (hash_table))
665 Elf_Internal_Versym iver;
666 unsigned int vernum = 0;
667 bfd_boolean skip;
669 if (ever != NULL)
671 _bfd_elf_swap_versym_in (abfd, ever, &iver);
672 vernum = iver.vs_vers & VERSYM_VERSION;
674 /* If this is a hidden symbol, or if it is not version
675 1, we append the version name to the symbol name.
676 However, we do not modify a non-hidden absolute
677 symbol, because it might be the version symbol
678 itself. FIXME: What if it isn't? */
679 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
680 || (vernum > 1 && ! bfd_is_abs_section (sec)))
682 const char *verstr;
683 size_t namelen, verlen, newlen;
684 char *newname, *p;
686 if (isym->st_shndx != SHN_UNDEF)
688 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
690 (*_bfd_error_handler)
691 (_("%s: %s: invalid version %u (max %d)"),
692 bfd_archive_filename (abfd), name, vernum,
693 elf_tdata (abfd)->dynverdef_hdr.sh_info);
694 bfd_set_error (bfd_error_bad_value);
695 goto error_free_vers;
697 else if (vernum > 1)
698 verstr =
699 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
700 else
701 verstr = "";
703 else
705 /* We cannot simply test for the number of
706 entries in the VERNEED section since the
707 numbers for the needed versions do not start
708 at 0. */
709 Elf_Internal_Verneed *t;
711 verstr = NULL;
712 for (t = elf_tdata (abfd)->verref;
713 t != NULL;
714 t = t->vn_nextref)
716 Elf_Internal_Vernaux *a;
718 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
720 if (a->vna_other == vernum)
722 verstr = a->vna_nodename;
723 break;
726 if (a != NULL)
727 break;
729 if (verstr == NULL)
731 (*_bfd_error_handler)
732 (_("%s: %s: invalid needed version %d"),
733 bfd_archive_filename (abfd), name, vernum);
734 bfd_set_error (bfd_error_bad_value);
735 goto error_free_vers;
739 namelen = strlen (name);
740 verlen = strlen (verstr);
741 newlen = namelen + verlen + 2;
742 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
743 && isym->st_shndx != SHN_UNDEF)
744 ++newlen;
746 newname = bfd_alloc (abfd, newlen);
747 if (newname == NULL)
748 goto error_free_vers;
749 memcpy (newname, name, namelen);
750 p = newname + namelen;
751 *p++ = ELF_VER_CHR;
752 /* If this is a defined non-hidden version symbol,
753 we add another @ to the name. This indicates the
754 default version of the symbol. */
755 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
756 && isym->st_shndx != SHN_UNDEF)
757 *p++ = ELF_VER_CHR;
758 memcpy (p, verstr, verlen + 1);
760 name = newname;
764 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
765 sym_hash, &skip, &override,
766 &type_change_ok, &size_change_ok))
767 goto error_free_vers;
769 if (skip)
770 continue;
772 if (override)
773 definition = FALSE;
775 h = *sym_hash;
776 while (h->root.type == bfd_link_hash_indirect
777 || h->root.type == bfd_link_hash_warning)
778 h = (struct elf_link_hash_entry *) h->root.u.i.link;
780 /* Remember the old alignment if this is a common symbol, so
781 that we don't reduce the alignment later on. We can't
782 check later, because _bfd_generic_link_add_one_symbol
783 will set a default for the alignment which we want to
784 override. We also remember the old bfd where the existing
785 definition comes from. */
786 switch (h->root.type)
788 default:
789 break;
791 case bfd_link_hash_defined:
792 case bfd_link_hash_defweak:
793 old_bfd = h->root.u.def.section->owner;
794 break;
796 case bfd_link_hash_common:
797 old_bfd = h->root.u.c.p->section->owner;
798 old_alignment = h->root.u.c.p->alignment_power;
799 break;
802 if (elf_tdata (abfd)->verdef != NULL
803 && ! override
804 && vernum > 1
805 && definition)
806 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
809 if (! (_bfd_generic_link_add_one_symbol
810 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
811 (struct bfd_link_hash_entry **) sym_hash)))
812 goto error_free_vers;
814 h = *sym_hash;
815 while (h->root.type == bfd_link_hash_indirect
816 || h->root.type == bfd_link_hash_warning)
817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
818 *sym_hash = h;
820 new_weakdef = FALSE;
821 if (dynamic
822 && definition
823 && (flags & BSF_WEAK) != 0
824 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
825 && is_elf_hash_table (hash_table)
826 && h->weakdef == NULL)
828 /* Keep a list of all weak defined non function symbols from
829 a dynamic object, using the weakdef field. Later in this
830 function we will set the weakdef field to the correct
831 value. We only put non-function symbols from dynamic
832 objects on this list, because that happens to be the only
833 time we need to know the normal symbol corresponding to a
834 weak symbol, and the information is time consuming to
835 figure out. If the weakdef field is not already NULL,
836 then this symbol was already defined by some previous
837 dynamic object, and we will be using that previous
838 definition anyhow. */
840 h->weakdef = weaks;
841 weaks = h;
842 new_weakdef = TRUE;
845 /* Set the alignment of a common symbol. */
846 if (isym->st_shndx == SHN_COMMON
847 && h->root.type == bfd_link_hash_common)
849 unsigned int align;
851 align = bfd_log2 (isym->st_value);
852 if (align > old_alignment
853 /* Permit an alignment power of zero if an alignment of one
854 is specified and no other alignments have been specified. */
855 || (isym->st_value == 1 && old_alignment == 0))
856 h->root.u.c.p->alignment_power = align;
857 else
858 h->root.u.c.p->alignment_power = old_alignment;
861 if (is_elf_hash_table (hash_table))
863 int old_flags;
864 bfd_boolean dynsym;
865 int new_flag;
867 /* Check the alignment when a common symbol is involved. This
868 can change when a common symbol is overridden by a normal
869 definition or a common symbol is ignored due to the old
870 normal definition. We need to make sure the maximum
871 alignment is maintained. */
872 if ((old_alignment || isym->st_shndx == SHN_COMMON)
873 && h->root.type != bfd_link_hash_common)
875 unsigned int common_align;
876 unsigned int normal_align;
877 unsigned int symbol_align;
878 bfd *normal_bfd;
879 bfd *common_bfd;
881 symbol_align = ffs (h->root.u.def.value) - 1;
882 if (h->root.u.def.section->owner != NULL
883 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
885 normal_align = h->root.u.def.section->alignment_power;
886 if (normal_align > symbol_align)
887 normal_align = symbol_align;
889 else
890 normal_align = symbol_align;
892 if (old_alignment)
894 common_align = old_alignment;
895 common_bfd = old_bfd;
896 normal_bfd = abfd;
898 else
900 common_align = bfd_log2 (isym->st_value);
901 common_bfd = abfd;
902 normal_bfd = old_bfd;
905 if (normal_align < common_align)
906 (*_bfd_error_handler)
907 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
908 1 << normal_align,
909 name,
910 bfd_archive_filename (normal_bfd),
911 1 << common_align,
912 bfd_archive_filename (common_bfd));
915 /* Remember the symbol size and type. */
916 if (isym->st_size != 0
917 && (definition || h->size == 0))
919 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
920 (*_bfd_error_handler)
921 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
922 name, (unsigned long) h->size,
923 bfd_archive_filename (old_bfd),
924 (unsigned long) isym->st_size,
925 bfd_archive_filename (abfd));
927 h->size = isym->st_size;
930 /* If this is a common symbol, then we always want H->SIZE
931 to be the size of the common symbol. The code just above
932 won't fix the size if a common symbol becomes larger. We
933 don't warn about a size change here, because that is
934 covered by --warn-common. */
935 if (h->root.type == bfd_link_hash_common)
936 h->size = h->root.u.c.size;
938 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
939 && (definition || h->type == STT_NOTYPE))
941 if (h->type != STT_NOTYPE
942 && h->type != ELF_ST_TYPE (isym->st_info)
943 && ! type_change_ok)
944 (*_bfd_error_handler)
945 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
946 name, h->type, ELF_ST_TYPE (isym->st_info),
947 bfd_archive_filename (abfd));
949 h->type = ELF_ST_TYPE (isym->st_info);
952 /* If st_other has a processor-specific meaning, specific
953 code might be needed here. We never merge the visibility
954 attribute with the one from a dynamic object. */
955 if (bed->elf_backend_merge_symbol_attribute)
956 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
957 dynamic);
959 if (isym->st_other != 0 && !dynamic)
961 unsigned char hvis, symvis, other, nvis;
963 /* Take the balance of OTHER from the definition. */
964 other = (definition ? isym->st_other : h->other);
965 other &= ~ ELF_ST_VISIBILITY (-1);
967 /* Combine visibilities, using the most constraining one. */
968 hvis = ELF_ST_VISIBILITY (h->other);
969 symvis = ELF_ST_VISIBILITY (isym->st_other);
970 if (! hvis)
971 nvis = symvis;
972 else if (! symvis)
973 nvis = hvis;
974 else
975 nvis = hvis < symvis ? hvis : symvis;
977 h->other = other | nvis;
980 /* Set a flag in the hash table entry indicating the type of
981 reference or definition we just found. Keep a count of
982 the number of dynamic symbols we find. A dynamic symbol
983 is one which is referenced or defined by both a regular
984 object and a shared object. */
985 old_flags = h->elf_link_hash_flags;
986 dynsym = FALSE;
987 if (! dynamic)
989 if (! definition)
991 new_flag = ELF_LINK_HASH_REF_REGULAR;
992 if (bind != STB_WEAK)
993 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
995 else
996 new_flag = ELF_LINK_HASH_DEF_REGULAR;
997 if (! info->executable
998 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
999 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1000 dynsym = TRUE;
1002 else
1004 if (! definition)
1005 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1006 else
1007 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1008 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1009 | ELF_LINK_HASH_REF_REGULAR)) != 0
1010 || (h->weakdef != NULL
1011 && ! new_weakdef
1012 && h->weakdef->dynindx != -1))
1013 dynsym = TRUE;
1016 h->elf_link_hash_flags |= new_flag;
1018 /* Check to see if we need to add an indirect symbol for
1019 the default name. */
1020 if (definition || h->root.type == bfd_link_hash_common)
1021 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1022 &sec, &value, &dynsym,
1023 override))
1024 goto error_free_vers;
1026 if (definition && !dynamic)
1028 char *p = strchr (name, ELF_VER_CHR);
1029 if (p != NULL && p[1] != ELF_VER_CHR)
1031 /* Queue non-default versions so that .symver x, x@FOO
1032 aliases can be checked. */
1033 if (! nondeflt_vers)
1035 amt = (isymend - isym + 1)
1036 * sizeof (struct elf_link_hash_entry *);
1037 nondeflt_vers = bfd_malloc (amt);
1039 nondeflt_vers [nondeflt_vers_cnt++] = h;
1043 if (dynsym && h->dynindx == -1)
1045 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1046 goto error_free_vers;
1047 if (h->weakdef != NULL
1048 && ! new_weakdef
1049 && h->weakdef->dynindx == -1)
1051 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1052 goto error_free_vers;
1055 else if (dynsym && h->dynindx != -1)
1056 /* If the symbol already has a dynamic index, but
1057 visibility says it should not be visible, turn it into
1058 a local symbol. */
1059 switch (ELF_ST_VISIBILITY (h->other))
1061 case STV_INTERNAL:
1062 case STV_HIDDEN:
1063 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1064 break;
1067 if (!add_needed && definition
1068 && (h->elf_link_hash_flags
1069 & ELF_LINK_HASH_REF_REGULAR) != 0)
1071 int ret;
1073 /* A symbol from a library loaded via DT_NEEDED of some
1074 other library is referenced by a regular object.
1075 Add a DT_NEEDED entry for it. */
1076 add_needed = TRUE;
1077 ret = add_dt_needed_tag (info, elf_dt_name (abfd), add_needed);
1078 if (ret < 0)
1079 goto error_free_vers;
1081 BFD_ASSERT (ret == 0);
1086 /* Now that all the symbols from this input file are created, handle
1087 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1088 if (nondeflt_vers != NULL)
1090 bfd_size_type cnt, symidx;
1092 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
1094 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1095 char *shortname, *p;
1097 p = strchr (h->root.root.string, ELF_VER_CHR);
1098 if (p == NULL
1099 || (h->root.type != bfd_link_hash_defined
1100 && h->root.type != bfd_link_hash_defweak))
1101 continue;
1103 amt = p - h->root.root.string;
1104 shortname = bfd_malloc (amt + 1);
1105 memcpy (shortname, h->root.root.string, amt);
1106 shortname[amt] = '\0';
1108 hi = (struct elf_link_hash_entry *)
1109 bfd_link_hash_lookup (&hash_table->root, shortname,
1110 FALSE, FALSE, FALSE);
1111 if (hi != NULL
1112 && hi->root.type == h->root.type
1113 && hi->root.u.def.value == h->root.u.def.value
1114 && hi->root.u.def.section == h->root.u.def.section)
1116 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1117 hi->root.type = bfd_link_hash_indirect;
1118 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1119 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1120 sym_hash = elf_sym_hashes (abfd);
1121 if (sym_hash)
1122 for (symidx = 0; symidx < extsymcount; ++symidx)
1123 if (sym_hash[symidx] == hi)
1125 sym_hash[symidx] = h;
1126 break;
1129 free (shortname);
1131 free (nondeflt_vers);
1132 nondeflt_vers = NULL;
1135 if (extversym != NULL)
1137 free (extversym);
1138 extversym = NULL;
1141 if (isymbuf != NULL)
1142 free (isymbuf);
1143 isymbuf = NULL;
1145 /* Now set the weakdefs field correctly for all the weak defined
1146 symbols we found. The only way to do this is to search all the
1147 symbols. Since we only need the information for non functions in
1148 dynamic objects, that's the only time we actually put anything on
1149 the list WEAKS. We need this information so that if a regular
1150 object refers to a symbol defined weakly in a dynamic object, the
1151 real symbol in the dynamic object is also put in the dynamic
1152 symbols; we also must arrange for both symbols to point to the
1153 same memory location. We could handle the general case of symbol
1154 aliasing, but a general symbol alias can only be generated in
1155 assembler code, handling it correctly would be very time
1156 consuming, and other ELF linkers don't handle general aliasing
1157 either. */
1158 if (weaks != NULL)
1160 struct elf_link_hash_entry **hpp;
1161 struct elf_link_hash_entry **hppend;
1162 struct elf_link_hash_entry **sorted_sym_hash;
1163 struct elf_link_hash_entry *h;
1164 size_t sym_count;
1166 /* Since we have to search the whole symbol list for each weak
1167 defined symbol, search time for N weak defined symbols will be
1168 O(N^2). Binary search will cut it down to O(NlogN). */
1169 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1170 sorted_sym_hash = bfd_malloc (amt);
1171 if (sorted_sym_hash == NULL)
1172 goto error_return;
1173 sym_hash = sorted_sym_hash;
1174 hpp = elf_sym_hashes (abfd);
1175 hppend = hpp + extsymcount;
1176 sym_count = 0;
1177 for (; hpp < hppend; hpp++)
1179 h = *hpp;
1180 if (h != NULL
1181 && h->root.type == bfd_link_hash_defined
1182 && h->type != STT_FUNC)
1184 *sym_hash = h;
1185 sym_hash++;
1186 sym_count++;
1190 qsort (sorted_sym_hash, sym_count,
1191 sizeof (struct elf_link_hash_entry *),
1192 sort_symbol);
1194 while (weaks != NULL)
1196 struct elf_link_hash_entry *hlook;
1197 asection *slook;
1198 bfd_vma vlook;
1199 long ilook;
1200 size_t i, j, idx;
1202 hlook = weaks;
1203 weaks = hlook->weakdef;
1204 hlook->weakdef = NULL;
1206 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1207 || hlook->root.type == bfd_link_hash_defweak
1208 || hlook->root.type == bfd_link_hash_common
1209 || hlook->root.type == bfd_link_hash_indirect);
1210 slook = hlook->root.u.def.section;
1211 vlook = hlook->root.u.def.value;
1213 ilook = -1;
1214 i = 0;
1215 j = sym_count;
1216 while (i < j)
1218 bfd_signed_vma vdiff;
1219 idx = (i + j) / 2;
1220 h = sorted_sym_hash [idx];
1221 vdiff = vlook - h->root.u.def.value;
1222 if (vdiff < 0)
1223 j = idx;
1224 else if (vdiff > 0)
1225 i = idx + 1;
1226 else
1228 long sdiff = slook - h->root.u.def.section;
1229 if (sdiff < 0)
1230 j = idx;
1231 else if (sdiff > 0)
1232 i = idx + 1;
1233 else
1235 ilook = idx;
1236 break;
1241 /* We didn't find a value/section match. */
1242 if (ilook == -1)
1243 continue;
1245 for (i = ilook; i < sym_count; i++)
1247 h = sorted_sym_hash [i];
1249 /* Stop if value or section doesn't match. */
1250 if (h->root.u.def.value != vlook
1251 || h->root.u.def.section != slook)
1252 break;
1253 else if (h != hlook)
1255 hlook->weakdef = h;
1257 /* If the weak definition is in the list of dynamic
1258 symbols, make sure the real definition is put
1259 there as well. */
1260 if (hlook->dynindx != -1 && h->dynindx == -1)
1262 if (! _bfd_elf_link_record_dynamic_symbol (info,
1264 goto error_return;
1267 /* If the real definition is in the list of dynamic
1268 symbols, make sure the weak definition is put
1269 there as well. If we don't do this, then the
1270 dynamic loader might not merge the entries for the
1271 real definition and the weak definition. */
1272 if (h->dynindx != -1 && hlook->dynindx == -1)
1274 if (! _bfd_elf_link_record_dynamic_symbol (info,
1275 hlook))
1276 goto error_return;
1278 break;
1283 free (sorted_sym_hash);
1286 /* If this object is the same format as the output object, and it is
1287 not a shared library, then let the backend look through the
1288 relocs.
1290 This is required to build global offset table entries and to
1291 arrange for dynamic relocs. It is not required for the
1292 particular common case of linking non PIC code, even when linking
1293 against shared libraries, but unfortunately there is no way of
1294 knowing whether an object file has been compiled PIC or not.
1295 Looking through the relocs is not particularly time consuming.
1296 The problem is that we must either (1) keep the relocs in memory,
1297 which causes the linker to require additional runtime memory or
1298 (2) read the relocs twice from the input file, which wastes time.
1299 This would be a good case for using mmap.
1301 I have no idea how to handle linking PIC code into a file of a
1302 different format. It probably can't be done. */
1303 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1304 if (! dynamic
1305 && is_elf_hash_table (hash_table)
1306 && hash_table->root.creator == abfd->xvec
1307 && check_relocs != NULL)
1309 asection *o;
1311 for (o = abfd->sections; o != NULL; o = o->next)
1313 Elf_Internal_Rela *internal_relocs;
1314 bfd_boolean ok;
1316 if ((o->flags & SEC_RELOC) == 0
1317 || o->reloc_count == 0
1318 || ((info->strip == strip_all || info->strip == strip_debugger)
1319 && (o->flags & SEC_DEBUGGING) != 0)
1320 || bfd_is_abs_section (o->output_section))
1321 continue;
1323 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1324 info->keep_memory);
1325 if (internal_relocs == NULL)
1326 goto error_return;
1328 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1330 if (elf_section_data (o)->relocs != internal_relocs)
1331 free (internal_relocs);
1333 if (! ok)
1334 goto error_return;
1338 /* If this is a non-traditional link, try to optimize the handling
1339 of the .stab/.stabstr sections. */
1340 if (! dynamic
1341 && ! info->traditional_format
1342 && is_elf_hash_table (hash_table)
1343 && (info->strip != strip_all && info->strip != strip_debugger))
1345 asection *stabstr;
1347 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1348 if (stabstr != NULL)
1350 bfd_size_type string_offset = 0;
1351 asection *stab;
1353 for (stab = abfd->sections; stab; stab = stab->next)
1354 if (strncmp (".stab", stab->name, 5) == 0
1355 && (!stab->name[5] ||
1356 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1357 && (stab->flags & SEC_MERGE) == 0
1358 && !bfd_is_abs_section (stab->output_section))
1360 struct bfd_elf_section_data *secdata;
1362 secdata = elf_section_data (stab);
1363 if (! _bfd_link_section_stabs (abfd,
1364 & hash_table->stab_info,
1365 stab, stabstr,
1366 &secdata->sec_info,
1367 &string_offset))
1368 goto error_return;
1369 if (secdata->sec_info)
1370 stab->sec_info_type = ELF_INFO_TYPE_STABS;
1375 if (! info->relocatable
1376 && ! dynamic
1377 && is_elf_hash_table (hash_table))
1379 asection *s;
1381 for (s = abfd->sections; s != NULL; s = s->next)
1382 if ((s->flags & SEC_MERGE) != 0
1383 && !bfd_is_abs_section (s->output_section))
1385 struct bfd_elf_section_data *secdata;
1387 secdata = elf_section_data (s);
1388 if (! _bfd_merge_section (abfd,
1389 & hash_table->merge_info,
1390 s, &secdata->sec_info))
1391 goto error_return;
1392 else if (secdata->sec_info)
1393 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1397 if (is_elf_hash_table (hash_table))
1399 /* Add this bfd to the loaded list. */
1400 struct elf_link_loaded_list *n;
1402 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
1403 if (n == NULL)
1404 goto error_return;
1405 n->abfd = abfd;
1406 n->next = hash_table->loaded;
1407 hash_table->loaded = n;
1410 return TRUE;
1412 error_free_vers:
1413 if (nondeflt_vers != NULL)
1414 free (nondeflt_vers);
1415 if (extversym != NULL)
1416 free (extversym);
1417 error_free_sym:
1418 if (isymbuf != NULL)
1419 free (isymbuf);
1420 error_return:
1421 return FALSE;
1424 /* Add an entry to the .dynamic table. */
1426 bfd_boolean
1427 elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
1429 Elf_Internal_Dyn dyn;
1430 bfd *dynobj;
1431 asection *s;
1432 bfd_size_type newsize;
1433 bfd_byte *newcontents;
1435 if (! is_elf_hash_table (info->hash))
1436 return FALSE;
1438 dynobj = elf_hash_table (info)->dynobj;
1440 s = bfd_get_section_by_name (dynobj, ".dynamic");
1441 BFD_ASSERT (s != NULL);
1443 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1444 newcontents = bfd_realloc (s->contents, newsize);
1445 if (newcontents == NULL)
1446 return FALSE;
1448 dyn.d_tag = tag;
1449 dyn.d_un.d_val = val;
1450 elf_swap_dyn_out (dynobj, &dyn,
1451 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1453 s->_raw_size = newsize;
1454 s->contents = newcontents;
1456 return TRUE;
1459 /* Array used to determine the number of hash table buckets to use
1460 based on the number of symbols there are. If there are fewer than
1461 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1462 fewer than 37 we use 17 buckets, and so forth. We never use more
1463 than 32771 buckets. */
1465 static const size_t elf_buckets[] =
1467 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1468 16411, 32771, 0
1471 /* Compute bucket count for hashing table. We do not use a static set
1472 of possible tables sizes anymore. Instead we determine for all
1473 possible reasonable sizes of the table the outcome (i.e., the
1474 number of collisions etc) and choose the best solution. The
1475 weighting functions are not too simple to allow the table to grow
1476 without bounds. Instead one of the weighting factors is the size.
1477 Therefore the result is always a good payoff between few collisions
1478 (= short chain lengths) and table size. */
1479 static size_t
1480 compute_bucket_count (struct bfd_link_info *info)
1482 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1483 size_t best_size = 0;
1484 unsigned long int *hashcodes;
1485 unsigned long int *hashcodesp;
1486 unsigned long int i;
1487 bfd_size_type amt;
1489 /* Compute the hash values for all exported symbols. At the same
1490 time store the values in an array so that we could use them for
1491 optimizations. */
1492 amt = dynsymcount;
1493 amt *= sizeof (unsigned long int);
1494 hashcodes = bfd_malloc (amt);
1495 if (hashcodes == NULL)
1496 return 0;
1497 hashcodesp = hashcodes;
1499 /* Put all hash values in HASHCODES. */
1500 elf_link_hash_traverse (elf_hash_table (info),
1501 elf_collect_hash_codes, &hashcodesp);
1503 /* We have a problem here. The following code to optimize the table
1504 size requires an integer type with more the 32 bits. If
1505 BFD_HOST_U_64_BIT is set we know about such a type. */
1506 #ifdef BFD_HOST_U_64_BIT
1507 if (info->optimize)
1509 unsigned long int nsyms = hashcodesp - hashcodes;
1510 size_t minsize;
1511 size_t maxsize;
1512 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1513 unsigned long int *counts ;
1515 /* Possible optimization parameters: if we have NSYMS symbols we say
1516 that the hashing table must at least have NSYMS/4 and at most
1517 2*NSYMS buckets. */
1518 minsize = nsyms / 4;
1519 if (minsize == 0)
1520 minsize = 1;
1521 best_size = maxsize = nsyms * 2;
1523 /* Create array where we count the collisions in. We must use bfd_malloc
1524 since the size could be large. */
1525 amt = maxsize;
1526 amt *= sizeof (unsigned long int);
1527 counts = bfd_malloc (amt);
1528 if (counts == NULL)
1530 free (hashcodes);
1531 return 0;
1534 /* Compute the "optimal" size for the hash table. The criteria is a
1535 minimal chain length. The minor criteria is (of course) the size
1536 of the table. */
1537 for (i = minsize; i < maxsize; ++i)
1539 /* Walk through the array of hashcodes and count the collisions. */
1540 BFD_HOST_U_64_BIT max;
1541 unsigned long int j;
1542 unsigned long int fact;
1544 memset (counts, '\0', i * sizeof (unsigned long int));
1546 /* Determine how often each hash bucket is used. */
1547 for (j = 0; j < nsyms; ++j)
1548 ++counts[hashcodes[j] % i];
1550 /* For the weight function we need some information about the
1551 pagesize on the target. This is information need not be 100%
1552 accurate. Since this information is not available (so far) we
1553 define it here to a reasonable default value. If it is crucial
1554 to have a better value some day simply define this value. */
1555 # ifndef BFD_TARGET_PAGESIZE
1556 # define BFD_TARGET_PAGESIZE (4096)
1557 # endif
1559 /* We in any case need 2 + NSYMS entries for the size values and
1560 the chains. */
1561 max = (2 + nsyms) * (ARCH_SIZE / 8);
1563 # if 1
1564 /* Variant 1: optimize for short chains. We add the squares
1565 of all the chain lengths (which favors many small chain
1566 over a few long chains). */
1567 for (j = 0; j < i; ++j)
1568 max += counts[j] * counts[j];
1570 /* This adds penalties for the overall size of the table. */
1571 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1572 max *= fact * fact;
1573 # else
1574 /* Variant 2: Optimize a lot more for small table. Here we
1575 also add squares of the size but we also add penalties for
1576 empty slots (the +1 term). */
1577 for (j = 0; j < i; ++j)
1578 max += (1 + counts[j]) * (1 + counts[j]);
1580 /* The overall size of the table is considered, but not as
1581 strong as in variant 1, where it is squared. */
1582 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1583 max *= fact;
1584 # endif
1586 /* Compare with current best results. */
1587 if (max < best_chlen)
1589 best_chlen = max;
1590 best_size = i;
1594 free (counts);
1596 else
1597 #endif /* defined (BFD_HOST_U_64_BIT) */
1599 /* This is the fallback solution if no 64bit type is available or if we
1600 are not supposed to spend much time on optimizations. We select the
1601 bucket count using a fixed set of numbers. */
1602 for (i = 0; elf_buckets[i] != 0; i++)
1604 best_size = elf_buckets[i];
1605 if (dynsymcount < elf_buckets[i + 1])
1606 break;
1610 /* Free the arrays we needed. */
1611 free (hashcodes);
1613 return best_size;
1616 /* Set up the sizes and contents of the ELF dynamic sections. This is
1617 called by the ELF linker emulation before_allocation routine. We
1618 must set the sizes of the sections before the linker sets the
1619 addresses of the various sections. */
1621 bfd_boolean
1622 NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1623 const char *soname,
1624 const char *rpath,
1625 const char *filter_shlib,
1626 const char * const *auxiliary_filters,
1627 struct bfd_link_info *info,
1628 asection **sinterpptr,
1629 struct bfd_elf_version_tree *verdefs)
1631 bfd_size_type soname_indx;
1632 bfd *dynobj;
1633 const struct elf_backend_data *bed;
1634 struct elf_assign_sym_version_info asvinfo;
1636 *sinterpptr = NULL;
1638 soname_indx = (bfd_size_type) -1;
1640 if (!is_elf_hash_table (info->hash))
1641 return TRUE;
1643 if (info->execstack)
1644 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1645 else if (info->noexecstack)
1646 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1647 else
1649 bfd *inputobj;
1650 asection *notesec = NULL;
1651 int exec = 0;
1653 for (inputobj = info->input_bfds;
1654 inputobj;
1655 inputobj = inputobj->link_next)
1657 asection *s;
1659 if (inputobj->flags & DYNAMIC)
1660 continue;
1661 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1662 if (s)
1664 if (s->flags & SEC_CODE)
1665 exec = PF_X;
1666 notesec = s;
1668 else
1669 exec = PF_X;
1671 if (notesec)
1673 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1674 if (exec && info->relocatable
1675 && notesec->output_section != bfd_abs_section_ptr)
1676 notesec->output_section->flags |= SEC_CODE;
1680 /* Any syms created from now on start with -1 in
1681 got.refcount/offset and plt.refcount/offset. */
1682 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
1684 /* The backend may have to create some sections regardless of whether
1685 we're dynamic or not. */
1686 bed = get_elf_backend_data (output_bfd);
1687 if (bed->elf_backend_always_size_sections
1688 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1689 return FALSE;
1691 dynobj = elf_hash_table (info)->dynobj;
1693 /* If there were no dynamic objects in the link, there is nothing to
1694 do here. */
1695 if (dynobj == NULL)
1696 return TRUE;
1698 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1699 return FALSE;
1701 if (elf_hash_table (info)->dynamic_sections_created)
1703 struct elf_info_failed eif;
1704 struct elf_link_hash_entry *h;
1705 asection *dynstr;
1706 struct bfd_elf_version_tree *t;
1707 struct bfd_elf_version_expr *d;
1708 bfd_boolean all_defined;
1710 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1711 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
1713 if (soname != NULL)
1715 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1716 soname, TRUE);
1717 if (soname_indx == (bfd_size_type) -1
1718 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
1719 return FALSE;
1722 if (info->symbolic)
1724 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1725 return FALSE;
1726 info->flags |= DF_SYMBOLIC;
1729 if (rpath != NULL)
1731 bfd_size_type indx;
1733 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
1734 TRUE);
1735 if (info->new_dtags)
1736 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
1737 if (indx == (bfd_size_type) -1
1738 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
1739 || (info->new_dtags
1740 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
1741 return FALSE;
1744 if (filter_shlib != NULL)
1746 bfd_size_type indx;
1748 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1749 filter_shlib, TRUE);
1750 if (indx == (bfd_size_type) -1
1751 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
1752 return FALSE;
1755 if (auxiliary_filters != NULL)
1757 const char * const *p;
1759 for (p = auxiliary_filters; *p != NULL; p++)
1761 bfd_size_type indx;
1763 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1764 *p, TRUE);
1765 if (indx == (bfd_size_type) -1
1766 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
1767 return FALSE;
1771 eif.info = info;
1772 eif.verdefs = verdefs;
1773 eif.failed = FALSE;
1775 /* If we are supposed to export all symbols into the dynamic symbol
1776 table (this is not the normal case), then do so. */
1777 if (info->export_dynamic)
1779 elf_link_hash_traverse (elf_hash_table (info),
1780 _bfd_elf_export_symbol,
1781 &eif);
1782 if (eif.failed)
1783 return FALSE;
1786 /* Make all global versions with definition. */
1787 for (t = verdefs; t != NULL; t = t->next)
1788 for (d = t->globals.list; d != NULL; d = d->next)
1789 if (!d->symver && d->symbol)
1791 const char *verstr, *name;
1792 size_t namelen, verlen, newlen;
1793 char *newname, *p;
1794 struct elf_link_hash_entry *newh;
1796 name = d->symbol;
1797 namelen = strlen (name);
1798 verstr = t->name;
1799 verlen = strlen (verstr);
1800 newlen = namelen + verlen + 3;
1802 newname = bfd_malloc (newlen);
1803 if (newname == NULL)
1804 return FALSE;
1805 memcpy (newname, name, namelen);
1807 /* Check the hidden versioned definition. */
1808 p = newname + namelen;
1809 *p++ = ELF_VER_CHR;
1810 memcpy (p, verstr, verlen + 1);
1811 newh = elf_link_hash_lookup (elf_hash_table (info),
1812 newname, FALSE, FALSE,
1813 FALSE);
1814 if (newh == NULL
1815 || (newh->root.type != bfd_link_hash_defined
1816 && newh->root.type != bfd_link_hash_defweak))
1818 /* Check the default versioned definition. */
1819 *p++ = ELF_VER_CHR;
1820 memcpy (p, verstr, verlen + 1);
1821 newh = elf_link_hash_lookup (elf_hash_table (info),
1822 newname, FALSE, FALSE,
1823 FALSE);
1825 free (newname);
1827 /* Mark this version if there is a definition and it is
1828 not defined in a shared object. */
1829 if (newh != NULL
1830 && ((newh->elf_link_hash_flags
1831 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
1832 && (newh->root.type == bfd_link_hash_defined
1833 || newh->root.type == bfd_link_hash_defweak))
1834 d->symver = 1;
1837 /* Attach all the symbols to their version information. */
1838 asvinfo.output_bfd = output_bfd;
1839 asvinfo.info = info;
1840 asvinfo.verdefs = verdefs;
1841 asvinfo.failed = FALSE;
1843 elf_link_hash_traverse (elf_hash_table (info),
1844 _bfd_elf_link_assign_sym_version,
1845 &asvinfo);
1846 if (asvinfo.failed)
1847 return FALSE;
1849 if (!info->allow_undefined_version)
1851 /* Check if all global versions have a definition. */
1852 all_defined = TRUE;
1853 for (t = verdefs; t != NULL; t = t->next)
1854 for (d = t->globals.list; d != NULL; d = d->next)
1855 if (!d->symver && !d->script)
1857 (*_bfd_error_handler)
1858 (_("%s: undefined version: %s"),
1859 d->pattern, t->name);
1860 all_defined = FALSE;
1863 if (!all_defined)
1865 bfd_set_error (bfd_error_bad_value);
1866 return FALSE;
1870 /* Find all symbols which were defined in a dynamic object and make
1871 the backend pick a reasonable value for them. */
1872 elf_link_hash_traverse (elf_hash_table (info),
1873 _bfd_elf_adjust_dynamic_symbol,
1874 &eif);
1875 if (eif.failed)
1876 return FALSE;
1878 /* Add some entries to the .dynamic section. We fill in some of the
1879 values later, in elf_bfd_final_link, but we must add the entries
1880 now so that we know the final size of the .dynamic section. */
1882 /* If there are initialization and/or finalization functions to
1883 call then add the corresponding DT_INIT/DT_FINI entries. */
1884 h = (info->init_function
1885 ? elf_link_hash_lookup (elf_hash_table (info),
1886 info->init_function, FALSE,
1887 FALSE, FALSE)
1888 : NULL);
1889 if (h != NULL
1890 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1891 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1893 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
1894 return FALSE;
1896 h = (info->fini_function
1897 ? elf_link_hash_lookup (elf_hash_table (info),
1898 info->fini_function, FALSE,
1899 FALSE, FALSE)
1900 : NULL);
1901 if (h != NULL
1902 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1903 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1905 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
1906 return FALSE;
1909 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
1911 /* DT_PREINIT_ARRAY is not allowed in shared library. */
1912 if (! info->executable)
1914 bfd *sub;
1915 asection *o;
1917 for (sub = info->input_bfds; sub != NULL;
1918 sub = sub->link_next)
1919 for (o = sub->sections; o != NULL; o = o->next)
1920 if (elf_section_data (o)->this_hdr.sh_type
1921 == SHT_PREINIT_ARRAY)
1923 (*_bfd_error_handler)
1924 (_("%s: .preinit_array section is not allowed in DSO"),
1925 bfd_archive_filename (sub));
1926 break;
1929 bfd_set_error (bfd_error_nonrepresentable_section);
1930 return FALSE;
1933 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
1934 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
1935 return FALSE;
1937 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
1939 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
1940 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
1941 return FALSE;
1943 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
1945 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
1946 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
1947 return FALSE;
1950 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
1951 /* If .dynstr is excluded from the link, we don't want any of
1952 these tags. Strictly, we should be checking each section
1953 individually; This quick check covers for the case where
1954 someone does a /DISCARD/ : { *(*) }. */
1955 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
1957 bfd_size_type strsize;
1959 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
1960 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1961 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1962 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1963 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1964 || ! elf_add_dynamic_entry (info, DT_SYMENT,
1965 sizeof (Elf_External_Sym)))
1966 return FALSE;
1970 /* The backend must work out the sizes of all the other dynamic
1971 sections. */
1972 if (bed->elf_backend_size_dynamic_sections
1973 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1974 return FALSE;
1976 if (elf_hash_table (info)->dynamic_sections_created)
1978 bfd_size_type dynsymcount;
1979 asection *s;
1980 size_t bucketcount = 0;
1981 size_t hash_entry_size;
1982 unsigned int dtagcount;
1984 /* Set up the version definition section. */
1985 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
1986 BFD_ASSERT (s != NULL);
1988 /* We may have created additional version definitions if we are
1989 just linking a regular application. */
1990 verdefs = asvinfo.verdefs;
1992 /* Skip anonymous version tag. */
1993 if (verdefs != NULL && verdefs->vernum == 0)
1994 verdefs = verdefs->next;
1996 if (verdefs == NULL)
1997 _bfd_strip_section_from_output (info, s);
1998 else
2000 unsigned int cdefs;
2001 bfd_size_type size;
2002 struct bfd_elf_version_tree *t;
2003 bfd_byte *p;
2004 Elf_Internal_Verdef def;
2005 Elf_Internal_Verdaux defaux;
2007 cdefs = 0;
2008 size = 0;
2010 /* Make space for the base version. */
2011 size += sizeof (Elf_External_Verdef);
2012 size += sizeof (Elf_External_Verdaux);
2013 ++cdefs;
2015 for (t = verdefs; t != NULL; t = t->next)
2017 struct bfd_elf_version_deps *n;
2019 size += sizeof (Elf_External_Verdef);
2020 size += sizeof (Elf_External_Verdaux);
2021 ++cdefs;
2023 for (n = t->deps; n != NULL; n = n->next)
2024 size += sizeof (Elf_External_Verdaux);
2027 s->_raw_size = size;
2028 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2029 if (s->contents == NULL && s->_raw_size != 0)
2030 return FALSE;
2032 /* Fill in the version definition section. */
2034 p = s->contents;
2036 def.vd_version = VER_DEF_CURRENT;
2037 def.vd_flags = VER_FLG_BASE;
2038 def.vd_ndx = 1;
2039 def.vd_cnt = 1;
2040 def.vd_aux = sizeof (Elf_External_Verdef);
2041 def.vd_next = (sizeof (Elf_External_Verdef)
2042 + sizeof (Elf_External_Verdaux));
2044 if (soname_indx != (bfd_size_type) -1)
2046 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2047 soname_indx);
2048 def.vd_hash = bfd_elf_hash (soname);
2049 defaux.vda_name = soname_indx;
2051 else
2053 const char *name;
2054 bfd_size_type indx;
2056 name = basename (output_bfd->filename);
2057 def.vd_hash = bfd_elf_hash (name);
2058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2059 name, FALSE);
2060 if (indx == (bfd_size_type) -1)
2061 return FALSE;
2062 defaux.vda_name = indx;
2064 defaux.vda_next = 0;
2066 _bfd_elf_swap_verdef_out (output_bfd, &def,
2067 (Elf_External_Verdef *) p);
2068 p += sizeof (Elf_External_Verdef);
2069 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2070 (Elf_External_Verdaux *) p);
2071 p += sizeof (Elf_External_Verdaux);
2073 for (t = verdefs; t != NULL; t = t->next)
2075 unsigned int cdeps;
2076 struct bfd_elf_version_deps *n;
2077 struct elf_link_hash_entry *h;
2078 struct bfd_link_hash_entry *bh;
2080 cdeps = 0;
2081 for (n = t->deps; n != NULL; n = n->next)
2082 ++cdeps;
2084 /* Add a symbol representing this version. */
2085 bh = NULL;
2086 if (! (_bfd_generic_link_add_one_symbol
2087 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2088 0, NULL, FALSE,
2089 get_elf_backend_data (dynobj)->collect, &bh)))
2090 return FALSE;
2091 h = (struct elf_link_hash_entry *) bh;
2092 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2093 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2094 h->type = STT_OBJECT;
2095 h->verinfo.vertree = t;
2097 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2098 return FALSE;
2100 def.vd_version = VER_DEF_CURRENT;
2101 def.vd_flags = 0;
2102 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
2103 def.vd_flags |= VER_FLG_WEAK;
2104 def.vd_ndx = t->vernum + 1;
2105 def.vd_cnt = cdeps + 1;
2106 def.vd_hash = bfd_elf_hash (t->name);
2107 def.vd_aux = sizeof (Elf_External_Verdef);
2108 if (t->next != NULL)
2109 def.vd_next = (sizeof (Elf_External_Verdef)
2110 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2111 else
2112 def.vd_next = 0;
2114 _bfd_elf_swap_verdef_out (output_bfd, &def,
2115 (Elf_External_Verdef *) p);
2116 p += sizeof (Elf_External_Verdef);
2118 defaux.vda_name = h->dynstr_index;
2119 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2120 h->dynstr_index);
2121 if (t->deps == NULL)
2122 defaux.vda_next = 0;
2123 else
2124 defaux.vda_next = sizeof (Elf_External_Verdaux);
2125 t->name_indx = defaux.vda_name;
2127 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2128 (Elf_External_Verdaux *) p);
2129 p += sizeof (Elf_External_Verdaux);
2131 for (n = t->deps; n != NULL; n = n->next)
2133 if (n->version_needed == NULL)
2135 /* This can happen if there was an error in the
2136 version script. */
2137 defaux.vda_name = 0;
2139 else
2141 defaux.vda_name = n->version_needed->name_indx;
2142 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2143 defaux.vda_name);
2145 if (n->next == NULL)
2146 defaux.vda_next = 0;
2147 else
2148 defaux.vda_next = sizeof (Elf_External_Verdaux);
2150 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2151 (Elf_External_Verdaux *) p);
2152 p += sizeof (Elf_External_Verdaux);
2156 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2157 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2158 return FALSE;
2160 elf_tdata (output_bfd)->cverdefs = cdefs;
2163 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
2165 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
2166 return FALSE;
2168 else if (info->flags & DF_BIND_NOW)
2170 if (! elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
2171 return FALSE;
2174 if (info->flags_1)
2176 if (info->executable)
2177 info->flags_1 &= ~ (DF_1_INITFIRST
2178 | DF_1_NODELETE
2179 | DF_1_NOOPEN);
2180 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
2181 return FALSE;
2184 /* Work out the size of the version reference section. */
2186 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2187 BFD_ASSERT (s != NULL);
2189 struct elf_find_verdep_info sinfo;
2191 sinfo.output_bfd = output_bfd;
2192 sinfo.info = info;
2193 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2194 if (sinfo.vers == 0)
2195 sinfo.vers = 1;
2196 sinfo.failed = FALSE;
2198 elf_link_hash_traverse (elf_hash_table (info),
2199 _bfd_elf_link_find_version_dependencies,
2200 &sinfo);
2202 if (elf_tdata (output_bfd)->verref == NULL)
2203 _bfd_strip_section_from_output (info, s);
2204 else
2206 Elf_Internal_Verneed *t;
2207 unsigned int size;
2208 unsigned int crefs;
2209 bfd_byte *p;
2211 /* Build the version definition section. */
2212 size = 0;
2213 crefs = 0;
2214 for (t = elf_tdata (output_bfd)->verref;
2215 t != NULL;
2216 t = t->vn_nextref)
2218 Elf_Internal_Vernaux *a;
2220 size += sizeof (Elf_External_Verneed);
2221 ++crefs;
2222 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2223 size += sizeof (Elf_External_Vernaux);
2226 s->_raw_size = size;
2227 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2228 if (s->contents == NULL)
2229 return FALSE;
2231 p = s->contents;
2232 for (t = elf_tdata (output_bfd)->verref;
2233 t != NULL;
2234 t = t->vn_nextref)
2236 unsigned int caux;
2237 Elf_Internal_Vernaux *a;
2238 bfd_size_type indx;
2240 caux = 0;
2241 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2242 ++caux;
2244 t->vn_version = VER_NEED_CURRENT;
2245 t->vn_cnt = caux;
2246 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2247 elf_dt_name (t->vn_bfd) != NULL
2248 ? elf_dt_name (t->vn_bfd)
2249 : basename (t->vn_bfd->filename),
2250 FALSE);
2251 if (indx == (bfd_size_type) -1)
2252 return FALSE;
2253 t->vn_file = indx;
2254 t->vn_aux = sizeof (Elf_External_Verneed);
2255 if (t->vn_nextref == NULL)
2256 t->vn_next = 0;
2257 else
2258 t->vn_next = (sizeof (Elf_External_Verneed)
2259 + caux * sizeof (Elf_External_Vernaux));
2261 _bfd_elf_swap_verneed_out (output_bfd, t,
2262 (Elf_External_Verneed *) p);
2263 p += sizeof (Elf_External_Verneed);
2265 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2267 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2268 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2269 a->vna_nodename, FALSE);
2270 if (indx == (bfd_size_type) -1)
2271 return FALSE;
2272 a->vna_name = indx;
2273 if (a->vna_nextptr == NULL)
2274 a->vna_next = 0;
2275 else
2276 a->vna_next = sizeof (Elf_External_Vernaux);
2278 _bfd_elf_swap_vernaux_out (output_bfd, a,
2279 (Elf_External_Vernaux *) p);
2280 p += sizeof (Elf_External_Vernaux);
2284 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2285 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2286 return FALSE;
2288 elf_tdata (output_bfd)->cverrefs = crefs;
2292 /* Assign dynsym indicies. In a shared library we generate a
2293 section symbol for each output section, which come first.
2294 Next come all of the back-end allocated local dynamic syms,
2295 followed by the rest of the global symbols. */
2297 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2299 /* Work out the size of the symbol version section. */
2300 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2301 BFD_ASSERT (s != NULL);
2302 if (dynsymcount == 0
2303 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2305 _bfd_strip_section_from_output (info, s);
2306 /* The DYNSYMCOUNT might have changed if we were going to
2307 output a dynamic symbol table entry for S. */
2308 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2310 else
2312 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2313 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2314 if (s->contents == NULL)
2315 return FALSE;
2317 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2318 return FALSE;
2321 /* Set the size of the .dynsym and .hash sections. We counted
2322 the number of dynamic symbols in elf_link_add_object_symbols.
2323 We will build the contents of .dynsym and .hash when we build
2324 the final symbol table, because until then we do not know the
2325 correct value to give the symbols. We built the .dynstr
2326 section as we went along in elf_link_add_object_symbols. */
2327 s = bfd_get_section_by_name (dynobj, ".dynsym");
2328 BFD_ASSERT (s != NULL);
2329 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2330 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2331 if (s->contents == NULL && s->_raw_size != 0)
2332 return FALSE;
2334 if (dynsymcount != 0)
2336 Elf_Internal_Sym isym;
2338 /* The first entry in .dynsym is a dummy symbol. */
2339 isym.st_value = 0;
2340 isym.st_size = 0;
2341 isym.st_name = 0;
2342 isym.st_info = 0;
2343 isym.st_other = 0;
2344 isym.st_shndx = 0;
2345 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
2348 /* Compute the size of the hashing table. As a side effect this
2349 computes the hash values for all the names we export. */
2350 bucketcount = compute_bucket_count (info);
2352 s = bfd_get_section_by_name (dynobj, ".hash");
2353 BFD_ASSERT (s != NULL);
2354 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2355 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
2356 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2357 if (s->contents == NULL)
2358 return FALSE;
2360 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2361 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
2362 s->contents + hash_entry_size);
2364 elf_hash_table (info)->bucketcount = bucketcount;
2366 s = bfd_get_section_by_name (dynobj, ".dynstr");
2367 BFD_ASSERT (s != NULL);
2369 elf_finalize_dynstr (output_bfd, info);
2371 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2373 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
2374 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2375 return FALSE;
2378 return TRUE;
2381 /* This function is used to adjust offsets into .dynstr for
2382 dynamic symbols. This is called via elf_link_hash_traverse. */
2384 static bfd_boolean
2385 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2387 struct elf_strtab_hash *dynstr = data;
2389 if (h->root.type == bfd_link_hash_warning)
2390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2392 if (h->dynindx != -1)
2393 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2394 return TRUE;
2397 /* Assign string offsets in .dynstr, update all structures referencing
2398 them. */
2400 static bfd_boolean
2401 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2403 struct elf_link_local_dynamic_entry *entry;
2404 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2405 bfd *dynobj = elf_hash_table (info)->dynobj;
2406 asection *sdyn;
2407 bfd_size_type size;
2408 Elf_External_Dyn *dyncon, *dynconend;
2410 _bfd_elf_strtab_finalize (dynstr);
2411 size = _bfd_elf_strtab_size (dynstr);
2413 /* Update all .dynamic entries referencing .dynstr strings. */
2414 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2415 BFD_ASSERT (sdyn != NULL);
2417 dyncon = (Elf_External_Dyn *) sdyn->contents;
2418 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2419 sdyn->_raw_size);
2420 for (; dyncon < dynconend; dyncon++)
2422 Elf_Internal_Dyn dyn;
2424 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2425 switch (dyn.d_tag)
2427 case DT_STRSZ:
2428 dyn.d_un.d_val = size;
2429 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2430 break;
2431 case DT_NEEDED:
2432 case DT_SONAME:
2433 case DT_RPATH:
2434 case DT_RUNPATH:
2435 case DT_FILTER:
2436 case DT_AUXILIARY:
2437 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2438 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2439 break;
2440 default:
2441 break;
2445 /* Now update local dynamic symbols. */
2446 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2447 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2448 entry->isym.st_name);
2450 /* And the rest of dynamic symbols. */
2451 elf_link_hash_traverse (elf_hash_table (info),
2452 elf_adjust_dynstr_offsets, dynstr);
2454 /* Adjust version definitions. */
2455 if (elf_tdata (output_bfd)->cverdefs)
2457 asection *s;
2458 bfd_byte *p;
2459 bfd_size_type i;
2460 Elf_Internal_Verdef def;
2461 Elf_Internal_Verdaux defaux;
2463 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2464 p = (bfd_byte *) s->contents;
2467 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2468 &def);
2469 p += sizeof (Elf_External_Verdef);
2470 for (i = 0; i < def.vd_cnt; ++i)
2472 _bfd_elf_swap_verdaux_in (output_bfd,
2473 (Elf_External_Verdaux *) p, &defaux);
2474 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2475 defaux.vda_name);
2476 _bfd_elf_swap_verdaux_out (output_bfd,
2477 &defaux, (Elf_External_Verdaux *) p);
2478 p += sizeof (Elf_External_Verdaux);
2481 while (def.vd_next);
2484 /* Adjust version references. */
2485 if (elf_tdata (output_bfd)->verref)
2487 asection *s;
2488 bfd_byte *p;
2489 bfd_size_type i;
2490 Elf_Internal_Verneed need;
2491 Elf_Internal_Vernaux needaux;
2493 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2494 p = (bfd_byte *) s->contents;
2497 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2498 &need);
2499 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2500 _bfd_elf_swap_verneed_out (output_bfd, &need,
2501 (Elf_External_Verneed *) p);
2502 p += sizeof (Elf_External_Verneed);
2503 for (i = 0; i < need.vn_cnt; ++i)
2505 _bfd_elf_swap_vernaux_in (output_bfd,
2506 (Elf_External_Vernaux *) p, &needaux);
2507 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2508 needaux.vna_name);
2509 _bfd_elf_swap_vernaux_out (output_bfd,
2510 &needaux,
2511 (Elf_External_Vernaux *) p);
2512 p += sizeof (Elf_External_Vernaux);
2515 while (need.vn_next);
2518 return TRUE;
2521 /* Final phase of ELF linker. */
2523 /* A structure we use to avoid passing large numbers of arguments. */
2525 struct elf_final_link_info
2527 /* General link information. */
2528 struct bfd_link_info *info;
2529 /* Output BFD. */
2530 bfd *output_bfd;
2531 /* Symbol string table. */
2532 struct bfd_strtab_hash *symstrtab;
2533 /* .dynsym section. */
2534 asection *dynsym_sec;
2535 /* .hash section. */
2536 asection *hash_sec;
2537 /* symbol version section (.gnu.version). */
2538 asection *symver_sec;
2539 /* Buffer large enough to hold contents of any section. */
2540 bfd_byte *contents;
2541 /* Buffer large enough to hold external relocs of any section. */
2542 void *external_relocs;
2543 /* Buffer large enough to hold internal relocs of any section. */
2544 Elf_Internal_Rela *internal_relocs;
2545 /* Buffer large enough to hold external local symbols of any input
2546 BFD. */
2547 Elf_External_Sym *external_syms;
2548 /* And a buffer for symbol section indices. */
2549 Elf_External_Sym_Shndx *locsym_shndx;
2550 /* Buffer large enough to hold internal local symbols of any input
2551 BFD. */
2552 Elf_Internal_Sym *internal_syms;
2553 /* Array large enough to hold a symbol index for each local symbol
2554 of any input BFD. */
2555 long *indices;
2556 /* Array large enough to hold a section pointer for each local
2557 symbol of any input BFD. */
2558 asection **sections;
2559 /* Buffer to hold swapped out symbols. */
2560 Elf_External_Sym *symbuf;
2561 /* And one for symbol section indices. */
2562 Elf_External_Sym_Shndx *symshndxbuf;
2563 /* Number of swapped out symbols in buffer. */
2564 size_t symbuf_count;
2565 /* Number of symbols which fit in symbuf. */
2566 size_t symbuf_size;
2567 /* And same for symshndxbuf. */
2568 size_t shndxbuf_size;
2571 static bfd_boolean elf_link_output_sym
2572 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2573 struct elf_link_hash_entry *);
2574 static bfd_boolean elf_link_flush_output_syms
2575 (struct elf_final_link_info *);
2576 static bfd_boolean elf_link_output_extsym
2577 (struct elf_link_hash_entry *, void *);
2578 static bfd_boolean elf_link_input_bfd
2579 (struct elf_final_link_info *, bfd *);
2580 static bfd_boolean elf_reloc_link_order
2581 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
2583 /* This struct is used to pass information to elf_link_output_extsym. */
2585 struct elf_outext_info
2587 bfd_boolean failed;
2588 bfd_boolean localsyms;
2589 struct elf_final_link_info *finfo;
2592 /* When performing a relocatable link, the input relocations are
2593 preserved. But, if they reference global symbols, the indices
2594 referenced must be updated. Update all the relocations in
2595 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2597 static void
2598 elf_link_adjust_relocs (bfd *abfd,
2599 Elf_Internal_Shdr *rel_hdr,
2600 unsigned int count,
2601 struct elf_link_hash_entry **rel_hash)
2603 unsigned int i;
2604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2605 bfd_byte *erela;
2606 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2607 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2609 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2611 swap_in = bed->s->swap_reloc_in;
2612 swap_out = bed->s->swap_reloc_out;
2614 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2616 swap_in = bed->s->swap_reloca_in;
2617 swap_out = bed->s->swap_reloca_out;
2619 else
2620 abort ();
2622 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2623 abort ();
2625 erela = rel_hdr->contents;
2626 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2628 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2629 unsigned int j;
2631 if (*rel_hash == NULL)
2632 continue;
2634 BFD_ASSERT ((*rel_hash)->indx >= 0);
2636 (*swap_in) (abfd, erela, irela);
2637 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2638 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2639 ELF_R_TYPE (irela[j].r_info));
2640 (*swap_out) (abfd, irela, erela);
2644 struct elf_link_sort_rela
2646 bfd_vma offset;
2647 enum elf_reloc_type_class type;
2648 /* We use this as an array of size int_rels_per_ext_rel. */
2649 Elf_Internal_Rela rela[1];
2652 static int
2653 elf_link_sort_cmp1 (const void *A, const void *B)
2655 const struct elf_link_sort_rela *a = A;
2656 const struct elf_link_sort_rela *b = B;
2657 int relativea, relativeb;
2659 relativea = a->type == reloc_class_relative;
2660 relativeb = b->type == reloc_class_relative;
2662 if (relativea < relativeb)
2663 return 1;
2664 if (relativea > relativeb)
2665 return -1;
2666 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2667 return -1;
2668 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2669 return 1;
2670 if (a->rela->r_offset < b->rela->r_offset)
2671 return -1;
2672 if (a->rela->r_offset > b->rela->r_offset)
2673 return 1;
2674 return 0;
2677 static int
2678 elf_link_sort_cmp2 (const void *A, const void *B)
2680 const struct elf_link_sort_rela *a = A;
2681 const struct elf_link_sort_rela *b = B;
2682 int copya, copyb;
2684 if (a->offset < b->offset)
2685 return -1;
2686 if (a->offset > b->offset)
2687 return 1;
2688 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2689 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
2690 if (copya < copyb)
2691 return -1;
2692 if (copya > copyb)
2693 return 1;
2694 if (a->rela->r_offset < b->rela->r_offset)
2695 return -1;
2696 if (a->rela->r_offset > b->rela->r_offset)
2697 return 1;
2698 return 0;
2701 static size_t
2702 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
2704 asection *reldyn;
2705 bfd_size_type count, size;
2706 size_t i, ret, sort_elt, ext_size;
2707 bfd_byte *sort, *s_non_relative, *p;
2708 struct elf_link_sort_rela *sq;
2709 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2710 int i2e = bed->s->int_rels_per_ext_rel;
2711 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2712 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2713 struct bfd_link_order *lo;
2715 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2716 if (reldyn == NULL || reldyn->_raw_size == 0)
2718 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2719 if (reldyn == NULL || reldyn->_raw_size == 0)
2720 return 0;
2721 ext_size = sizeof (Elf_External_Rel);
2722 swap_in = bed->s->swap_reloc_in;
2723 swap_out = bed->s->swap_reloc_out;
2725 else
2727 ext_size = sizeof (Elf_External_Rela);
2728 swap_in = bed->s->swap_reloca_in;
2729 swap_out = bed->s->swap_reloca_out;
2731 count = reldyn->_raw_size / ext_size;
2733 size = 0;
2734 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2735 if (lo->type == bfd_indirect_link_order)
2737 asection *o = lo->u.indirect.section;
2738 size += o->_raw_size;
2741 if (size != reldyn->_raw_size)
2742 return 0;
2744 sort_elt = (sizeof (struct elf_link_sort_rela)
2745 + (i2e - 1) * sizeof (Elf_Internal_Rela));
2746 sort = bfd_zmalloc (sort_elt * count);
2747 if (sort == NULL)
2749 (*info->callbacks->warning)
2750 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
2751 return 0;
2754 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2755 if (lo->type == bfd_indirect_link_order)
2757 bfd_byte *erel, *erelend;
2758 asection *o = lo->u.indirect.section;
2760 erel = o->contents;
2761 erelend = o->contents + o->_raw_size;
2762 p = sort + o->output_offset / ext_size * sort_elt;
2763 while (erel < erelend)
2765 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2766 (*swap_in) (abfd, erel, s->rela);
2767 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
2768 p += sort_elt;
2769 erel += ext_size;
2773 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
2775 for (i = 0, p = sort; i < count; i++, p += sort_elt)
2777 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2778 if (s->type != reloc_class_relative)
2779 break;
2781 ret = i;
2782 s_non_relative = p;
2784 sq = (struct elf_link_sort_rela *) s_non_relative;
2785 for (; i < count; i++, p += sort_elt)
2787 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
2788 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
2789 sq = sp;
2790 sp->offset = sq->rela->r_offset;
2793 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
2795 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2796 if (lo->type == bfd_indirect_link_order)
2798 bfd_byte *erel, *erelend;
2799 asection *o = lo->u.indirect.section;
2801 erel = o->contents;
2802 erelend = o->contents + o->_raw_size;
2803 p = sort + o->output_offset / ext_size * sort_elt;
2804 while (erel < erelend)
2806 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2807 (*swap_out) (abfd, s->rela, erel);
2808 p += sort_elt;
2809 erel += ext_size;
2813 free (sort);
2814 *psec = reldyn;
2815 return ret;
2818 /* Do the final step of an ELF link. */
2820 bfd_boolean
2821 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
2823 bfd_boolean dynamic;
2824 bfd_boolean emit_relocs;
2825 bfd *dynobj;
2826 struct elf_final_link_info finfo;
2827 register asection *o;
2828 register struct bfd_link_order *p;
2829 register bfd *sub;
2830 bfd_size_type max_contents_size;
2831 bfd_size_type max_external_reloc_size;
2832 bfd_size_type max_internal_reloc_count;
2833 bfd_size_type max_sym_count;
2834 bfd_size_type max_sym_shndx_count;
2835 file_ptr off;
2836 Elf_Internal_Sym elfsym;
2837 unsigned int i;
2838 Elf_Internal_Shdr *symtab_hdr;
2839 Elf_Internal_Shdr *symtab_shndx_hdr;
2840 Elf_Internal_Shdr *symstrtab_hdr;
2841 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2842 struct elf_outext_info eoinfo;
2843 bfd_boolean merged;
2844 size_t relativecount = 0;
2845 asection *reldyn = 0;
2846 bfd_size_type amt;
2848 if (! is_elf_hash_table (info->hash))
2849 return FALSE;
2851 if (info->shared)
2852 abfd->flags |= DYNAMIC;
2854 dynamic = elf_hash_table (info)->dynamic_sections_created;
2855 dynobj = elf_hash_table (info)->dynobj;
2857 emit_relocs = (info->relocatable
2858 || info->emitrelocations
2859 || bed->elf_backend_emit_relocs);
2861 finfo.info = info;
2862 finfo.output_bfd = abfd;
2863 finfo.symstrtab = elf_stringtab_init ();
2864 if (finfo.symstrtab == NULL)
2865 return FALSE;
2867 if (! dynamic)
2869 finfo.dynsym_sec = NULL;
2870 finfo.hash_sec = NULL;
2871 finfo.symver_sec = NULL;
2873 else
2875 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
2876 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
2877 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
2878 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
2879 /* Note that it is OK if symver_sec is NULL. */
2882 finfo.contents = NULL;
2883 finfo.external_relocs = NULL;
2884 finfo.internal_relocs = NULL;
2885 finfo.external_syms = NULL;
2886 finfo.locsym_shndx = NULL;
2887 finfo.internal_syms = NULL;
2888 finfo.indices = NULL;
2889 finfo.sections = NULL;
2890 finfo.symbuf = NULL;
2891 finfo.symshndxbuf = NULL;
2892 finfo.symbuf_count = 0;
2893 finfo.shndxbuf_size = 0;
2895 /* Count up the number of relocations we will output for each output
2896 section, so that we know the sizes of the reloc sections. We
2897 also figure out some maximum sizes. */
2898 max_contents_size = 0;
2899 max_external_reloc_size = 0;
2900 max_internal_reloc_count = 0;
2901 max_sym_count = 0;
2902 max_sym_shndx_count = 0;
2903 merged = FALSE;
2904 for (o = abfd->sections; o != NULL; o = o->next)
2906 struct bfd_elf_section_data *esdo = elf_section_data (o);
2907 o->reloc_count = 0;
2909 for (p = o->link_order_head; p != NULL; p = p->next)
2911 unsigned int reloc_count = 0;
2912 struct bfd_elf_section_data *esdi = NULL;
2913 unsigned int *rel_count1;
2915 if (p->type == bfd_section_reloc_link_order
2916 || p->type == bfd_symbol_reloc_link_order)
2917 reloc_count = 1;
2918 else if (p->type == bfd_indirect_link_order)
2920 asection *sec;
2922 sec = p->u.indirect.section;
2923 esdi = elf_section_data (sec);
2925 /* Mark all sections which are to be included in the
2926 link. This will normally be every section. We need
2927 to do this so that we can identify any sections which
2928 the linker has decided to not include. */
2929 sec->linker_mark = TRUE;
2931 if (sec->flags & SEC_MERGE)
2932 merged = TRUE;
2934 if (info->relocatable || info->emitrelocations)
2935 reloc_count = sec->reloc_count;
2936 else if (bed->elf_backend_count_relocs)
2938 Elf_Internal_Rela * relocs;
2940 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2941 info->keep_memory);
2943 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
2945 if (elf_section_data (o)->relocs != relocs)
2946 free (relocs);
2949 if (sec->_raw_size > max_contents_size)
2950 max_contents_size = sec->_raw_size;
2951 if (sec->_cooked_size > max_contents_size)
2952 max_contents_size = sec->_cooked_size;
2954 /* We are interested in just local symbols, not all
2955 symbols. */
2956 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
2957 && (sec->owner->flags & DYNAMIC) == 0)
2959 size_t sym_count;
2961 if (elf_bad_symtab (sec->owner))
2962 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
2963 / sizeof (Elf_External_Sym));
2964 else
2965 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
2967 if (sym_count > max_sym_count)
2968 max_sym_count = sym_count;
2970 if (sym_count > max_sym_shndx_count
2971 && elf_symtab_shndx (sec->owner) != 0)
2972 max_sym_shndx_count = sym_count;
2974 if ((sec->flags & SEC_RELOC) != 0)
2976 size_t ext_size;
2978 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
2979 if (ext_size > max_external_reloc_size)
2980 max_external_reloc_size = ext_size;
2981 if (sec->reloc_count > max_internal_reloc_count)
2982 max_internal_reloc_count = sec->reloc_count;
2987 if (reloc_count == 0)
2988 continue;
2990 o->reloc_count += reloc_count;
2992 /* MIPS may have a mix of REL and RELA relocs on sections.
2993 To support this curious ABI we keep reloc counts in
2994 elf_section_data too. We must be careful to add the
2995 relocations from the input section to the right output
2996 count. FIXME: Get rid of one count. We have
2997 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
2998 rel_count1 = &esdo->rel_count;
2999 if (esdi != NULL)
3001 bfd_boolean same_size;
3002 bfd_size_type entsize1;
3004 entsize1 = esdi->rel_hdr.sh_entsize;
3005 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3006 || entsize1 == sizeof (Elf_External_Rela));
3007 same_size = (!o->use_rela_p
3008 == (entsize1 == sizeof (Elf_External_Rel)));
3010 if (!same_size)
3011 rel_count1 = &esdo->rel_count2;
3013 if (esdi->rel_hdr2 != NULL)
3015 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3016 unsigned int alt_count;
3017 unsigned int *rel_count2;
3019 BFD_ASSERT (entsize2 != entsize1
3020 && (entsize2 == sizeof (Elf_External_Rel)
3021 || entsize2 == sizeof (Elf_External_Rela)));
3023 rel_count2 = &esdo->rel_count2;
3024 if (!same_size)
3025 rel_count2 = &esdo->rel_count;
3027 /* The following is probably too simplistic if the
3028 backend counts output relocs unusually. */
3029 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3030 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3031 *rel_count2 += alt_count;
3032 reloc_count -= alt_count;
3035 *rel_count1 += reloc_count;
3038 if (o->reloc_count > 0)
3039 o->flags |= SEC_RELOC;
3040 else
3042 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3043 set it (this is probably a bug) and if it is set
3044 assign_section_numbers will create a reloc section. */
3045 o->flags &=~ SEC_RELOC;
3048 /* If the SEC_ALLOC flag is not set, force the section VMA to
3049 zero. This is done in elf_fake_sections as well, but forcing
3050 the VMA to 0 here will ensure that relocs against these
3051 sections are handled correctly. */
3052 if ((o->flags & SEC_ALLOC) == 0
3053 && ! o->user_set_vma)
3054 o->vma = 0;
3057 if (! info->relocatable && merged)
3058 elf_link_hash_traverse (elf_hash_table (info),
3059 _bfd_elf_link_sec_merge_syms, abfd);
3061 /* Figure out the file positions for everything but the symbol table
3062 and the relocs. We set symcount to force assign_section_numbers
3063 to create a symbol table. */
3064 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3065 BFD_ASSERT (! abfd->output_has_begun);
3066 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3067 goto error_return;
3069 /* That created the reloc sections. Set their sizes, and assign
3070 them file positions, and allocate some buffers. */
3071 for (o = abfd->sections; o != NULL; o = o->next)
3073 if ((o->flags & SEC_RELOC) != 0)
3075 if (!(_bfd_elf_link_size_reloc_section
3076 (abfd, &elf_section_data (o)->rel_hdr, o)))
3077 goto error_return;
3079 if (elf_section_data (o)->rel_hdr2
3080 && !(_bfd_elf_link_size_reloc_section
3081 (abfd, elf_section_data (o)->rel_hdr2, o)))
3082 goto error_return;
3085 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3086 to count upwards while actually outputting the relocations. */
3087 elf_section_data (o)->rel_count = 0;
3088 elf_section_data (o)->rel_count2 = 0;
3091 _bfd_elf_assign_file_positions_for_relocs (abfd);
3093 /* We have now assigned file positions for all the sections except
3094 .symtab and .strtab. We start the .symtab section at the current
3095 file position, and write directly to it. We build the .strtab
3096 section in memory. */
3097 bfd_get_symcount (abfd) = 0;
3098 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3099 /* sh_name is set in prep_headers. */
3100 symtab_hdr->sh_type = SHT_SYMTAB;
3101 /* sh_flags, sh_addr and sh_size all start off zero. */
3102 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3103 /* sh_link is set in assign_section_numbers. */
3104 /* sh_info is set below. */
3105 /* sh_offset is set just below. */
3106 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3108 off = elf_tdata (abfd)->next_file_pos;
3109 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3111 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3112 incorrect. We do not yet know the size of the .symtab section.
3113 We correct next_file_pos below, after we do know the size. */
3115 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3116 continuously seeking to the right position in the file. */
3117 if (! info->keep_memory || max_sym_count < 20)
3118 finfo.symbuf_size = 20;
3119 else
3120 finfo.symbuf_size = max_sym_count;
3121 amt = finfo.symbuf_size;
3122 amt *= sizeof (Elf_External_Sym);
3123 finfo.symbuf = bfd_malloc (amt);
3124 if (finfo.symbuf == NULL)
3125 goto error_return;
3126 if (elf_numsections (abfd) > SHN_LORESERVE)
3128 /* Wild guess at number of output symbols. realloc'd as needed. */
3129 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3130 finfo.shndxbuf_size = amt;
3131 amt *= sizeof (Elf_External_Sym_Shndx);
3132 finfo.symshndxbuf = bfd_zmalloc (amt);
3133 if (finfo.symshndxbuf == NULL)
3134 goto error_return;
3137 /* Start writing out the symbol table. The first symbol is always a
3138 dummy symbol. */
3139 if (info->strip != strip_all
3140 || emit_relocs)
3142 elfsym.st_value = 0;
3143 elfsym.st_size = 0;
3144 elfsym.st_info = 0;
3145 elfsym.st_other = 0;
3146 elfsym.st_shndx = SHN_UNDEF;
3147 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3148 NULL))
3149 goto error_return;
3152 #if 0
3153 /* Some standard ELF linkers do this, but we don't because it causes
3154 bootstrap comparison failures. */
3155 /* Output a file symbol for the output file as the second symbol.
3156 We output this even if we are discarding local symbols, although
3157 I'm not sure if this is correct. */
3158 elfsym.st_value = 0;
3159 elfsym.st_size = 0;
3160 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3161 elfsym.st_other = 0;
3162 elfsym.st_shndx = SHN_ABS;
3163 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3164 &elfsym, bfd_abs_section_ptr, NULL))
3165 goto error_return;
3166 #endif
3168 /* Output a symbol for each section. We output these even if we are
3169 discarding local symbols, since they are used for relocs. These
3170 symbols have no names. We store the index of each one in the
3171 index field of the section, so that we can find it again when
3172 outputting relocs. */
3173 if (info->strip != strip_all
3174 || emit_relocs)
3176 elfsym.st_size = 0;
3177 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3178 elfsym.st_other = 0;
3179 for (i = 1; i < elf_numsections (abfd); i++)
3181 o = section_from_elf_index (abfd, i);
3182 if (o != NULL)
3183 o->target_index = bfd_get_symcount (abfd);
3184 elfsym.st_shndx = i;
3185 if (info->relocatable || o == NULL)
3186 elfsym.st_value = 0;
3187 else
3188 elfsym.st_value = o->vma;
3189 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
3190 goto error_return;
3191 if (i == SHN_LORESERVE - 1)
3192 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3196 /* Allocate some memory to hold information read in from the input
3197 files. */
3198 if (max_contents_size != 0)
3200 finfo.contents = bfd_malloc (max_contents_size);
3201 if (finfo.contents == NULL)
3202 goto error_return;
3205 if (max_external_reloc_size != 0)
3207 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
3208 if (finfo.external_relocs == NULL)
3209 goto error_return;
3212 if (max_internal_reloc_count != 0)
3214 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3215 amt *= sizeof (Elf_Internal_Rela);
3216 finfo.internal_relocs = bfd_malloc (amt);
3217 if (finfo.internal_relocs == NULL)
3218 goto error_return;
3221 if (max_sym_count != 0)
3223 amt = max_sym_count * sizeof (Elf_External_Sym);
3224 finfo.external_syms = bfd_malloc (amt);
3225 if (finfo.external_syms == NULL)
3226 goto error_return;
3228 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3229 finfo.internal_syms = bfd_malloc (amt);
3230 if (finfo.internal_syms == NULL)
3231 goto error_return;
3233 amt = max_sym_count * sizeof (long);
3234 finfo.indices = bfd_malloc (amt);
3235 if (finfo.indices == NULL)
3236 goto error_return;
3238 amt = max_sym_count * sizeof (asection *);
3239 finfo.sections = bfd_malloc (amt);
3240 if (finfo.sections == NULL)
3241 goto error_return;
3244 if (max_sym_shndx_count != 0)
3246 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3247 finfo.locsym_shndx = bfd_malloc (amt);
3248 if (finfo.locsym_shndx == NULL)
3249 goto error_return;
3252 if (elf_hash_table (info)->tls_sec)
3254 bfd_vma base, end = 0;
3255 asection *sec;
3257 for (sec = elf_hash_table (info)->tls_sec;
3258 sec && (sec->flags & SEC_THREAD_LOCAL);
3259 sec = sec->next)
3261 bfd_vma size = sec->_raw_size;
3263 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3265 struct bfd_link_order *o;
3267 for (o = sec->link_order_head; o != NULL; o = o->next)
3268 if (size < o->offset + o->size)
3269 size = o->offset + o->size;
3271 end = sec->vma + size;
3273 base = elf_hash_table (info)->tls_sec->vma;
3274 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3275 elf_hash_table (info)->tls_size = end - base;
3278 /* Since ELF permits relocations to be against local symbols, we
3279 must have the local symbols available when we do the relocations.
3280 Since we would rather only read the local symbols once, and we
3281 would rather not keep them in memory, we handle all the
3282 relocations for a single input file at the same time.
3284 Unfortunately, there is no way to know the total number of local
3285 symbols until we have seen all of them, and the local symbol
3286 indices precede the global symbol indices. This means that when
3287 we are generating relocatable output, and we see a reloc against
3288 a global symbol, we can not know the symbol index until we have
3289 finished examining all the local symbols to see which ones we are
3290 going to output. To deal with this, we keep the relocations in
3291 memory, and don't output them until the end of the link. This is
3292 an unfortunate waste of memory, but I don't see a good way around
3293 it. Fortunately, it only happens when performing a relocatable
3294 link, which is not the common case. FIXME: If keep_memory is set
3295 we could write the relocs out and then read them again; I don't
3296 know how bad the memory loss will be. */
3298 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3299 sub->output_has_begun = FALSE;
3300 for (o = abfd->sections; o != NULL; o = o->next)
3302 for (p = o->link_order_head; p != NULL; p = p->next)
3304 if (p->type == bfd_indirect_link_order
3305 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3306 == bfd_target_elf_flavour)
3307 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3309 if (! sub->output_has_begun)
3311 if (! elf_link_input_bfd (&finfo, sub))
3312 goto error_return;
3313 sub->output_has_begun = TRUE;
3316 else if (p->type == bfd_section_reloc_link_order
3317 || p->type == bfd_symbol_reloc_link_order)
3319 if (! elf_reloc_link_order (abfd, info, o, p))
3320 goto error_return;
3322 else
3324 if (! _bfd_default_link_order (abfd, info, o, p))
3325 goto error_return;
3330 /* Output any global symbols that got converted to local in a
3331 version script or due to symbol visibility. We do this in a
3332 separate step since ELF requires all local symbols to appear
3333 prior to any global symbols. FIXME: We should only do this if
3334 some global symbols were, in fact, converted to become local.
3335 FIXME: Will this work correctly with the Irix 5 linker? */
3336 eoinfo.failed = FALSE;
3337 eoinfo.finfo = &finfo;
3338 eoinfo.localsyms = TRUE;
3339 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3340 &eoinfo);
3341 if (eoinfo.failed)
3342 return FALSE;
3344 /* That wrote out all the local symbols. Finish up the symbol table
3345 with the global symbols. Even if we want to strip everything we
3346 can, we still need to deal with those global symbols that got
3347 converted to local in a version script. */
3349 /* The sh_info field records the index of the first non local symbol. */
3350 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3352 if (dynamic
3353 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3355 Elf_Internal_Sym sym;
3356 Elf_External_Sym *dynsym =
3357 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3358 long last_local = 0;
3360 /* Write out the section symbols for the output sections. */
3361 if (info->shared)
3363 asection *s;
3365 sym.st_size = 0;
3366 sym.st_name = 0;
3367 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3368 sym.st_other = 0;
3370 for (s = abfd->sections; s != NULL; s = s->next)
3372 int indx;
3373 Elf_External_Sym *dest;
3375 indx = elf_section_data (s)->this_idx;
3376 BFD_ASSERT (indx > 0);
3377 sym.st_shndx = indx;
3378 sym.st_value = s->vma;
3379 dest = dynsym + elf_section_data (s)->dynindx;
3380 elf_swap_symbol_out (abfd, &sym, dest, 0);
3383 last_local = bfd_count_sections (abfd);
3386 /* Write out the local dynsyms. */
3387 if (elf_hash_table (info)->dynlocal)
3389 struct elf_link_local_dynamic_entry *e;
3390 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3392 asection *s;
3393 Elf_External_Sym *dest;
3395 sym.st_size = e->isym.st_size;
3396 sym.st_other = e->isym.st_other;
3398 /* Copy the internal symbol as is.
3399 Note that we saved a word of storage and overwrote
3400 the original st_name with the dynstr_index. */
3401 sym = e->isym;
3403 if (e->isym.st_shndx != SHN_UNDEF
3404 && (e->isym.st_shndx < SHN_LORESERVE
3405 || e->isym.st_shndx > SHN_HIRESERVE))
3407 s = bfd_section_from_elf_index (e->input_bfd,
3408 e->isym.st_shndx);
3410 sym.st_shndx =
3411 elf_section_data (s->output_section)->this_idx;
3412 sym.st_value = (s->output_section->vma
3413 + s->output_offset
3414 + e->isym.st_value);
3417 if (last_local < e->dynindx)
3418 last_local = e->dynindx;
3420 dest = dynsym + e->dynindx;
3421 elf_swap_symbol_out (abfd, &sym, dest, 0);
3425 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3426 last_local + 1;
3429 /* We get the global symbols from the hash table. */
3430 eoinfo.failed = FALSE;
3431 eoinfo.localsyms = FALSE;
3432 eoinfo.finfo = &finfo;
3433 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3434 &eoinfo);
3435 if (eoinfo.failed)
3436 return FALSE;
3438 /* If backend needs to output some symbols not present in the hash
3439 table, do it now. */
3440 if (bed->elf_backend_output_arch_syms)
3442 typedef bfd_boolean (*out_sym_func)
3443 (void *, const char *, Elf_Internal_Sym *, asection *,
3444 struct elf_link_hash_entry *);
3446 if (! ((*bed->elf_backend_output_arch_syms)
3447 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
3448 return FALSE;
3451 /* Flush all symbols to the file. */
3452 if (! elf_link_flush_output_syms (&finfo))
3453 return FALSE;
3455 /* Now we know the size of the symtab section. */
3456 off += symtab_hdr->sh_size;
3458 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3459 if (symtab_shndx_hdr->sh_name != 0)
3461 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3462 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3463 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3464 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3465 symtab_shndx_hdr->sh_size = amt;
3467 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3468 off, TRUE);
3470 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3471 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
3472 return FALSE;
3476 /* Finish up and write out the symbol string table (.strtab)
3477 section. */
3478 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3479 /* sh_name was set in prep_headers. */
3480 symstrtab_hdr->sh_type = SHT_STRTAB;
3481 symstrtab_hdr->sh_flags = 0;
3482 symstrtab_hdr->sh_addr = 0;
3483 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3484 symstrtab_hdr->sh_entsize = 0;
3485 symstrtab_hdr->sh_link = 0;
3486 symstrtab_hdr->sh_info = 0;
3487 /* sh_offset is set just below. */
3488 symstrtab_hdr->sh_addralign = 1;
3490 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3491 elf_tdata (abfd)->next_file_pos = off;
3493 if (bfd_get_symcount (abfd) > 0)
3495 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3496 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3497 return FALSE;
3500 /* Adjust the relocs to have the correct symbol indices. */
3501 for (o = abfd->sections; o != NULL; o = o->next)
3503 if ((o->flags & SEC_RELOC) == 0)
3504 continue;
3506 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3507 elf_section_data (o)->rel_count,
3508 elf_section_data (o)->rel_hashes);
3509 if (elf_section_data (o)->rel_hdr2 != NULL)
3510 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3511 elf_section_data (o)->rel_count2,
3512 (elf_section_data (o)->rel_hashes
3513 + elf_section_data (o)->rel_count));
3515 /* Set the reloc_count field to 0 to prevent write_relocs from
3516 trying to swap the relocs out itself. */
3517 o->reloc_count = 0;
3520 if (dynamic && info->combreloc && dynobj != NULL)
3521 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3523 /* If we are linking against a dynamic object, or generating a
3524 shared library, finish up the dynamic linking information. */
3525 if (dynamic)
3527 Elf_External_Dyn *dyncon, *dynconend;
3529 /* Fix up .dynamic entries. */
3530 o = bfd_get_section_by_name (dynobj, ".dynamic");
3531 BFD_ASSERT (o != NULL);
3533 dyncon = (Elf_External_Dyn *) o->contents;
3534 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3535 for (; dyncon < dynconend; dyncon++)
3537 Elf_Internal_Dyn dyn;
3538 const char *name;
3539 unsigned int type;
3541 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3543 switch (dyn.d_tag)
3545 default:
3546 break;
3547 case DT_NULL:
3548 if (relativecount > 0 && dyncon + 1 < dynconend)
3550 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3552 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3553 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3554 default: break;
3556 if (dyn.d_tag != DT_NULL)
3558 dyn.d_un.d_val = relativecount;
3559 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3560 relativecount = 0;
3563 break;
3564 case DT_INIT:
3565 name = info->init_function;
3566 goto get_sym;
3567 case DT_FINI:
3568 name = info->fini_function;
3569 get_sym:
3571 struct elf_link_hash_entry *h;
3573 h = elf_link_hash_lookup (elf_hash_table (info), name,
3574 FALSE, FALSE, TRUE);
3575 if (h != NULL
3576 && (h->root.type == bfd_link_hash_defined
3577 || h->root.type == bfd_link_hash_defweak))
3579 dyn.d_un.d_val = h->root.u.def.value;
3580 o = h->root.u.def.section;
3581 if (o->output_section != NULL)
3582 dyn.d_un.d_val += (o->output_section->vma
3583 + o->output_offset);
3584 else
3586 /* The symbol is imported from another shared
3587 library and does not apply to this one. */
3588 dyn.d_un.d_val = 0;
3591 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3594 break;
3596 case DT_PREINIT_ARRAYSZ:
3597 name = ".preinit_array";
3598 goto get_size;
3599 case DT_INIT_ARRAYSZ:
3600 name = ".init_array";
3601 goto get_size;
3602 case DT_FINI_ARRAYSZ:
3603 name = ".fini_array";
3604 get_size:
3605 o = bfd_get_section_by_name (abfd, name);
3606 if (o == NULL)
3608 (*_bfd_error_handler)
3609 (_("%s: could not find output section %s"),
3610 bfd_get_filename (abfd), name);
3611 goto error_return;
3613 if (o->_raw_size == 0)
3614 (*_bfd_error_handler)
3615 (_("warning: %s section has zero size"), name);
3616 dyn.d_un.d_val = o->_raw_size;
3617 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3618 break;
3620 case DT_PREINIT_ARRAY:
3621 name = ".preinit_array";
3622 goto get_vma;
3623 case DT_INIT_ARRAY:
3624 name = ".init_array";
3625 goto get_vma;
3626 case DT_FINI_ARRAY:
3627 name = ".fini_array";
3628 goto get_vma;
3630 case DT_HASH:
3631 name = ".hash";
3632 goto get_vma;
3633 case DT_STRTAB:
3634 name = ".dynstr";
3635 goto get_vma;
3636 case DT_SYMTAB:
3637 name = ".dynsym";
3638 goto get_vma;
3639 case DT_VERDEF:
3640 name = ".gnu.version_d";
3641 goto get_vma;
3642 case DT_VERNEED:
3643 name = ".gnu.version_r";
3644 goto get_vma;
3645 case DT_VERSYM:
3646 name = ".gnu.version";
3647 get_vma:
3648 o = bfd_get_section_by_name (abfd, name);
3649 if (o == NULL)
3651 (*_bfd_error_handler)
3652 (_("%s: could not find output section %s"),
3653 bfd_get_filename (abfd), name);
3654 goto error_return;
3656 dyn.d_un.d_ptr = o->vma;
3657 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3658 break;
3660 case DT_REL:
3661 case DT_RELA:
3662 case DT_RELSZ:
3663 case DT_RELASZ:
3664 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3665 type = SHT_REL;
3666 else
3667 type = SHT_RELA;
3668 dyn.d_un.d_val = 0;
3669 for (i = 1; i < elf_numsections (abfd); i++)
3671 Elf_Internal_Shdr *hdr;
3673 hdr = elf_elfsections (abfd)[i];
3674 if (hdr->sh_type == type
3675 && (hdr->sh_flags & SHF_ALLOC) != 0)
3677 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3678 dyn.d_un.d_val += hdr->sh_size;
3679 else
3681 if (dyn.d_un.d_val == 0
3682 || hdr->sh_addr < dyn.d_un.d_val)
3683 dyn.d_un.d_val = hdr->sh_addr;
3687 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3688 break;
3693 /* If we have created any dynamic sections, then output them. */
3694 if (dynobj != NULL)
3696 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3697 goto error_return;
3699 for (o = dynobj->sections; o != NULL; o = o->next)
3701 if ((o->flags & SEC_HAS_CONTENTS) == 0
3702 || o->_raw_size == 0
3703 || o->output_section == bfd_abs_section_ptr)
3704 continue;
3705 if ((o->flags & SEC_LINKER_CREATED) == 0)
3707 /* At this point, we are only interested in sections
3708 created by _bfd_elf_link_create_dynamic_sections. */
3709 continue;
3711 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3712 != SHT_STRTAB)
3713 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3715 if (! bfd_set_section_contents (abfd, o->output_section,
3716 o->contents,
3717 (file_ptr) o->output_offset,
3718 o->_raw_size))
3719 goto error_return;
3721 else
3723 /* The contents of the .dynstr section are actually in a
3724 stringtab. */
3725 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3726 if (bfd_seek (abfd, off, SEEK_SET) != 0
3727 || ! _bfd_elf_strtab_emit (abfd,
3728 elf_hash_table (info)->dynstr))
3729 goto error_return;
3734 if (info->relocatable)
3736 bfd_boolean failed = FALSE;
3738 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3739 if (failed)
3740 goto error_return;
3743 /* If we have optimized stabs strings, output them. */
3744 if (elf_hash_table (info)->stab_info != NULL)
3746 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3747 goto error_return;
3750 if (info->eh_frame_hdr)
3752 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
3753 goto error_return;
3756 if (finfo.symstrtab != NULL)
3757 _bfd_stringtab_free (finfo.symstrtab);
3758 if (finfo.contents != NULL)
3759 free (finfo.contents);
3760 if (finfo.external_relocs != NULL)
3761 free (finfo.external_relocs);
3762 if (finfo.internal_relocs != NULL)
3763 free (finfo.internal_relocs);
3764 if (finfo.external_syms != NULL)
3765 free (finfo.external_syms);
3766 if (finfo.locsym_shndx != NULL)
3767 free (finfo.locsym_shndx);
3768 if (finfo.internal_syms != NULL)
3769 free (finfo.internal_syms);
3770 if (finfo.indices != NULL)
3771 free (finfo.indices);
3772 if (finfo.sections != NULL)
3773 free (finfo.sections);
3774 if (finfo.symbuf != NULL)
3775 free (finfo.symbuf);
3776 if (finfo.symshndxbuf != NULL)
3777 free (finfo.symshndxbuf);
3778 for (o = abfd->sections; o != NULL; o = o->next)
3780 if ((o->flags & SEC_RELOC) != 0
3781 && elf_section_data (o)->rel_hashes != NULL)
3782 free (elf_section_data (o)->rel_hashes);
3785 elf_tdata (abfd)->linker = TRUE;
3787 return TRUE;
3789 error_return:
3790 if (finfo.symstrtab != NULL)
3791 _bfd_stringtab_free (finfo.symstrtab);
3792 if (finfo.contents != NULL)
3793 free (finfo.contents);
3794 if (finfo.external_relocs != NULL)
3795 free (finfo.external_relocs);
3796 if (finfo.internal_relocs != NULL)
3797 free (finfo.internal_relocs);
3798 if (finfo.external_syms != NULL)
3799 free (finfo.external_syms);
3800 if (finfo.locsym_shndx != NULL)
3801 free (finfo.locsym_shndx);
3802 if (finfo.internal_syms != NULL)
3803 free (finfo.internal_syms);
3804 if (finfo.indices != NULL)
3805 free (finfo.indices);
3806 if (finfo.sections != NULL)
3807 free (finfo.sections);
3808 if (finfo.symbuf != NULL)
3809 free (finfo.symbuf);
3810 if (finfo.symshndxbuf != NULL)
3811 free (finfo.symshndxbuf);
3812 for (o = abfd->sections; o != NULL; o = o->next)
3814 if ((o->flags & SEC_RELOC) != 0
3815 && elf_section_data (o)->rel_hashes != NULL)
3816 free (elf_section_data (o)->rel_hashes);
3819 return FALSE;
3822 /* Add a symbol to the output symbol table. */
3824 static bfd_boolean
3825 elf_link_output_sym (struct elf_final_link_info *finfo,
3826 const char *name,
3827 Elf_Internal_Sym *elfsym,
3828 asection *input_sec,
3829 struct elf_link_hash_entry *h)
3831 Elf_External_Sym *dest;
3832 Elf_External_Sym_Shndx *destshndx;
3833 bfd_boolean (*output_symbol_hook)
3834 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
3835 struct elf_link_hash_entry *);
3837 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3838 elf_backend_link_output_symbol_hook;
3839 if (output_symbol_hook != NULL)
3841 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
3842 return FALSE;
3845 if (name == NULL || *name == '\0')
3846 elfsym->st_name = 0;
3847 else if (input_sec->flags & SEC_EXCLUDE)
3848 elfsym->st_name = 0;
3849 else
3851 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
3852 name, TRUE, FALSE);
3853 if (elfsym->st_name == (unsigned long) -1)
3854 return FALSE;
3857 if (finfo->symbuf_count >= finfo->symbuf_size)
3859 if (! elf_link_flush_output_syms (finfo))
3860 return FALSE;
3863 dest = finfo->symbuf + finfo->symbuf_count;
3864 destshndx = finfo->symshndxbuf;
3865 if (destshndx != NULL)
3867 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
3869 bfd_size_type amt;
3871 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
3872 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
3873 if (destshndx == NULL)
3874 return FALSE;
3875 memset ((char *) destshndx + amt, 0, amt);
3876 finfo->shndxbuf_size *= 2;
3878 destshndx += bfd_get_symcount (finfo->output_bfd);
3881 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
3882 finfo->symbuf_count += 1;
3883 bfd_get_symcount (finfo->output_bfd) += 1;
3885 return TRUE;
3888 /* Flush the output symbols to the file. */
3890 static bfd_boolean
3891 elf_link_flush_output_syms (struct elf_final_link_info *finfo)
3893 if (finfo->symbuf_count > 0)
3895 Elf_Internal_Shdr *hdr;
3896 file_ptr pos;
3897 bfd_size_type amt;
3899 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
3900 pos = hdr->sh_offset + hdr->sh_size;
3901 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
3902 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
3903 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
3904 return FALSE;
3906 hdr->sh_size += amt;
3907 finfo->symbuf_count = 0;
3910 return TRUE;
3913 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
3914 allowing an unsatisfied unversioned symbol in the DSO to match a
3915 versioned symbol that would normally require an explicit version.
3916 We also handle the case that a DSO references a hidden symbol
3917 which may be satisfied by a versioned symbol in another DSO. */
3919 static bfd_boolean
3920 elf_link_check_versioned_symbol (struct bfd_link_info *info,
3921 struct elf_link_hash_entry *h)
3923 bfd *abfd;
3924 struct elf_link_loaded_list *loaded;
3926 if (!is_elf_hash_table (info->hash))
3927 return FALSE;
3929 switch (h->root.type)
3931 default:
3932 abfd = NULL;
3933 break;
3935 case bfd_link_hash_undefined:
3936 case bfd_link_hash_undefweak:
3937 abfd = h->root.u.undef.abfd;
3938 if ((abfd->flags & DYNAMIC) == 0
3939 || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
3940 return FALSE;
3941 break;
3943 case bfd_link_hash_defined:
3944 case bfd_link_hash_defweak:
3945 abfd = h->root.u.def.section->owner;
3946 break;
3948 case bfd_link_hash_common:
3949 abfd = h->root.u.c.p->section->owner;
3950 break;
3952 BFD_ASSERT (abfd != NULL);
3954 for (loaded = elf_hash_table (info)->loaded;
3955 loaded != NULL;
3956 loaded = loaded->next)
3958 bfd *input;
3959 Elf_Internal_Shdr *hdr;
3960 bfd_size_type symcount;
3961 bfd_size_type extsymcount;
3962 bfd_size_type extsymoff;
3963 Elf_Internal_Shdr *versymhdr;
3964 Elf_Internal_Sym *isym;
3965 Elf_Internal_Sym *isymend;
3966 Elf_Internal_Sym *isymbuf;
3967 Elf_External_Versym *ever;
3968 Elf_External_Versym *extversym;
3970 input = loaded->abfd;
3972 /* We check each DSO for a possible hidden versioned definition. */
3973 if (input == abfd
3974 || (input->flags & DYNAMIC) == 0
3975 || elf_dynversym (input) == 0)
3976 continue;
3978 hdr = &elf_tdata (input)->dynsymtab_hdr;
3980 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
3981 if (elf_bad_symtab (input))
3983 extsymcount = symcount;
3984 extsymoff = 0;
3986 else
3988 extsymcount = symcount - hdr->sh_info;
3989 extsymoff = hdr->sh_info;
3992 if (extsymcount == 0)
3993 continue;
3995 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
3996 NULL, NULL, NULL);
3997 if (isymbuf == NULL)
3998 return FALSE;
4000 /* Read in any version definitions. */
4001 versymhdr = &elf_tdata (input)->dynversym_hdr;
4002 extversym = bfd_malloc (versymhdr->sh_size);
4003 if (extversym == NULL)
4004 goto error_ret;
4006 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4007 || (bfd_bread (extversym, versymhdr->sh_size, input)
4008 != versymhdr->sh_size))
4010 free (extversym);
4011 error_ret:
4012 free (isymbuf);
4013 return FALSE;
4016 ever = extversym + extsymoff;
4017 isymend = isymbuf + extsymcount;
4018 for (isym = isymbuf; isym < isymend; isym++, ever++)
4020 const char *name;
4021 Elf_Internal_Versym iver;
4022 unsigned short version_index;
4024 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4025 || isym->st_shndx == SHN_UNDEF)
4026 continue;
4028 name = bfd_elf_string_from_elf_section (input,
4029 hdr->sh_link,
4030 isym->st_name);
4031 if (strcmp (name, h->root.root.string) != 0)
4032 continue;
4034 _bfd_elf_swap_versym_in (input, ever, &iver);
4036 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4038 /* If we have a non-hidden versioned sym, then it should
4039 have provided a definition for the undefined sym. */
4040 abort ();
4043 version_index = iver.vs_vers & VERSYM_VERSION;
4044 if (version_index == 1 || version_index == 2)
4046 /* This is the base or first version. We can use it. */
4047 free (extversym);
4048 free (isymbuf);
4049 return TRUE;
4053 free (extversym);
4054 free (isymbuf);
4057 return FALSE;
4060 /* Add an external symbol to the symbol table. This is called from
4061 the hash table traversal routine. When generating a shared object,
4062 we go through the symbol table twice. The first time we output
4063 anything that might have been forced to local scope in a version
4064 script. The second time we output the symbols that are still
4065 global symbols. */
4067 static bfd_boolean
4068 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
4070 struct elf_outext_info *eoinfo = data;
4071 struct elf_final_link_info *finfo = eoinfo->finfo;
4072 bfd_boolean strip;
4073 Elf_Internal_Sym sym;
4074 asection *input_sec;
4076 if (h->root.type == bfd_link_hash_warning)
4078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4079 if (h->root.type == bfd_link_hash_new)
4080 return TRUE;
4083 /* Decide whether to output this symbol in this pass. */
4084 if (eoinfo->localsyms)
4086 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4087 return TRUE;
4089 else
4091 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4092 return TRUE;
4095 /* If we have an undefined symbol reference here then it must have
4096 come from a shared library that is being linked in. (Undefined
4097 references in regular files have already been handled). If we
4098 are reporting errors for this situation then do so now. */
4099 if (h->root.type == bfd_link_hash_undefined
4100 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4101 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4102 && ! elf_link_check_versioned_symbol (finfo->info, h)
4103 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
4105 if (! ((*finfo->info->callbacks->undefined_symbol)
4106 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4107 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
4109 eoinfo->failed = TRUE;
4110 return FALSE;
4114 /* We should also warn if a forced local symbol is referenced from
4115 shared libraries. */
4116 if (! finfo->info->relocatable
4117 && (! finfo->info->shared)
4118 && (h->elf_link_hash_flags
4119 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4120 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4121 && ! elf_link_check_versioned_symbol (finfo->info, h))
4123 (*_bfd_error_handler)
4124 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4125 bfd_get_filename (finfo->output_bfd),
4126 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4127 ? "internal"
4128 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4129 ? "hidden" : "local",
4130 h->root.root.string,
4131 bfd_archive_filename (h->root.u.def.section->owner));
4132 eoinfo->failed = TRUE;
4133 return FALSE;
4136 /* We don't want to output symbols that have never been mentioned by
4137 a regular file, or that we have been told to strip. However, if
4138 h->indx is set to -2, the symbol is used by a reloc and we must
4139 output it. */
4140 if (h->indx == -2)
4141 strip = FALSE;
4142 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4143 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4144 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4145 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4146 strip = TRUE;
4147 else if (finfo->info->strip == strip_all)
4148 strip = TRUE;
4149 else if (finfo->info->strip == strip_some
4150 && bfd_hash_lookup (finfo->info->keep_hash,
4151 h->root.root.string, FALSE, FALSE) == NULL)
4152 strip = TRUE;
4153 else if (finfo->info->strip_discarded
4154 && (h->root.type == bfd_link_hash_defined
4155 || h->root.type == bfd_link_hash_defweak)
4156 && elf_discarded_section (h->root.u.def.section))
4157 strip = TRUE;
4158 else
4159 strip = FALSE;
4161 /* If we're stripping it, and it's not a dynamic symbol, there's
4162 nothing else to do unless it is a forced local symbol. */
4163 if (strip
4164 && h->dynindx == -1
4165 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4166 return TRUE;
4168 sym.st_value = 0;
4169 sym.st_size = h->size;
4170 sym.st_other = h->other;
4171 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4172 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4173 else if (h->root.type == bfd_link_hash_undefweak
4174 || h->root.type == bfd_link_hash_defweak)
4175 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4176 else
4177 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4179 switch (h->root.type)
4181 default:
4182 case bfd_link_hash_new:
4183 case bfd_link_hash_warning:
4184 abort ();
4185 return FALSE;
4187 case bfd_link_hash_undefined:
4188 case bfd_link_hash_undefweak:
4189 input_sec = bfd_und_section_ptr;
4190 sym.st_shndx = SHN_UNDEF;
4191 break;
4193 case bfd_link_hash_defined:
4194 case bfd_link_hash_defweak:
4196 input_sec = h->root.u.def.section;
4197 if (input_sec->output_section != NULL)
4199 sym.st_shndx =
4200 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4201 input_sec->output_section);
4202 if (sym.st_shndx == SHN_BAD)
4204 (*_bfd_error_handler)
4205 (_("%s: could not find output section %s for input section %s"),
4206 bfd_get_filename (finfo->output_bfd),
4207 input_sec->output_section->name,
4208 input_sec->name);
4209 eoinfo->failed = TRUE;
4210 return FALSE;
4213 /* ELF symbols in relocatable files are section relative,
4214 but in nonrelocatable files they are virtual
4215 addresses. */
4216 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4217 if (! finfo->info->relocatable)
4219 sym.st_value += input_sec->output_section->vma;
4220 if (h->type == STT_TLS)
4222 /* STT_TLS symbols are relative to PT_TLS segment
4223 base. */
4224 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4225 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4229 else
4231 BFD_ASSERT (input_sec->owner == NULL
4232 || (input_sec->owner->flags & DYNAMIC) != 0);
4233 sym.st_shndx = SHN_UNDEF;
4234 input_sec = bfd_und_section_ptr;
4237 break;
4239 case bfd_link_hash_common:
4240 input_sec = h->root.u.c.p->section;
4241 sym.st_shndx = SHN_COMMON;
4242 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4243 break;
4245 case bfd_link_hash_indirect:
4246 /* These symbols are created by symbol versioning. They point
4247 to the decorated version of the name. For example, if the
4248 symbol foo@@GNU_1.2 is the default, which should be used when
4249 foo is used with no version, then we add an indirect symbol
4250 foo which points to foo@@GNU_1.2. We ignore these symbols,
4251 since the indirected symbol is already in the hash table. */
4252 return TRUE;
4255 /* Give the processor backend a chance to tweak the symbol value,
4256 and also to finish up anything that needs to be done for this
4257 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4258 forced local syms when non-shared is due to a historical quirk. */
4259 if ((h->dynindx != -1
4260 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4261 && ((finfo->info->shared
4262 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4263 || h->root.type != bfd_link_hash_undefweak))
4264 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4265 && elf_hash_table (finfo->info)->dynamic_sections_created)
4267 const struct elf_backend_data *bed;
4269 bed = get_elf_backend_data (finfo->output_bfd);
4270 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4271 (finfo->output_bfd, finfo->info, h, &sym)))
4273 eoinfo->failed = TRUE;
4274 return FALSE;
4278 /* If we are marking the symbol as undefined, and there are no
4279 non-weak references to this symbol from a regular object, then
4280 mark the symbol as weak undefined; if there are non-weak
4281 references, mark the symbol as strong. We can't do this earlier,
4282 because it might not be marked as undefined until the
4283 finish_dynamic_symbol routine gets through with it. */
4284 if (sym.st_shndx == SHN_UNDEF
4285 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4286 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4287 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4289 int bindtype;
4291 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4292 bindtype = STB_GLOBAL;
4293 else
4294 bindtype = STB_WEAK;
4295 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4298 /* If a non-weak symbol with non-default visibility is not defined
4299 locally, it is a fatal error. */
4300 if (! finfo->info->relocatable
4301 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4302 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4303 && h->root.type == bfd_link_hash_undefined
4304 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4306 (*_bfd_error_handler)
4307 (_("%s: %s symbol `%s' isn't defined"),
4308 bfd_get_filename (finfo->output_bfd),
4309 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4310 ? "protected"
4311 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4312 ? "internal" : "hidden",
4313 h->root.root.string);
4314 eoinfo->failed = TRUE;
4315 return FALSE;
4318 /* If this symbol should be put in the .dynsym section, then put it
4319 there now. We already know the symbol index. We also fill in
4320 the entry in the .hash section. */
4321 if (h->dynindx != -1
4322 && elf_hash_table (finfo->info)->dynamic_sections_created)
4324 size_t bucketcount;
4325 size_t bucket;
4326 size_t hash_entry_size;
4327 bfd_byte *bucketpos;
4328 bfd_vma chain;
4329 Elf_External_Sym *esym;
4331 sym.st_name = h->dynstr_index;
4332 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4333 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
4335 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4336 bucket = h->elf_hash_value % bucketcount;
4337 hash_entry_size
4338 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4339 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4340 + (bucket + 2) * hash_entry_size);
4341 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4342 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
4343 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4344 ((bfd_byte *) finfo->hash_sec->contents
4345 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4347 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4349 Elf_Internal_Versym iversym;
4350 Elf_External_Versym *eversym;
4352 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4354 if (h->verinfo.verdef == NULL)
4355 iversym.vs_vers = 0;
4356 else
4357 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4359 else
4361 if (h->verinfo.vertree == NULL)
4362 iversym.vs_vers = 1;
4363 else
4364 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4367 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4368 iversym.vs_vers |= VERSYM_HIDDEN;
4370 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4371 eversym += h->dynindx;
4372 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4376 /* If we're stripping it, then it was just a dynamic symbol, and
4377 there's nothing else to do. */
4378 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4379 return TRUE;
4381 h->indx = bfd_get_symcount (finfo->output_bfd);
4383 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
4385 eoinfo->failed = TRUE;
4386 return FALSE;
4389 return TRUE;
4392 /* Link an input file into the linker output file. This function
4393 handles all the sections and relocations of the input file at once.
4394 This is so that we only have to read the local symbols once, and
4395 don't have to keep them in memory. */
4397 static bfd_boolean
4398 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
4400 bfd_boolean (*relocate_section)
4401 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4402 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
4403 bfd *output_bfd;
4404 Elf_Internal_Shdr *symtab_hdr;
4405 size_t locsymcount;
4406 size_t extsymoff;
4407 Elf_Internal_Sym *isymbuf;
4408 Elf_Internal_Sym *isym;
4409 Elf_Internal_Sym *isymend;
4410 long *pindex;
4411 asection **ppsection;
4412 asection *o;
4413 const struct elf_backend_data *bed;
4414 bfd_boolean emit_relocs;
4415 struct elf_link_hash_entry **sym_hashes;
4417 output_bfd = finfo->output_bfd;
4418 bed = get_elf_backend_data (output_bfd);
4419 relocate_section = bed->elf_backend_relocate_section;
4421 /* If this is a dynamic object, we don't want to do anything here:
4422 we don't want the local symbols, and we don't want the section
4423 contents. */
4424 if ((input_bfd->flags & DYNAMIC) != 0)
4425 return TRUE;
4427 emit_relocs = (finfo->info->relocatable
4428 || finfo->info->emitrelocations
4429 || bed->elf_backend_emit_relocs);
4431 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4432 if (elf_bad_symtab (input_bfd))
4434 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4435 extsymoff = 0;
4437 else
4439 locsymcount = symtab_hdr->sh_info;
4440 extsymoff = symtab_hdr->sh_info;
4443 /* Read the local symbols. */
4444 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4445 if (isymbuf == NULL && locsymcount != 0)
4447 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4448 finfo->internal_syms,
4449 finfo->external_syms,
4450 finfo->locsym_shndx);
4451 if (isymbuf == NULL)
4452 return FALSE;
4455 /* Find local symbol sections and adjust values of symbols in
4456 SEC_MERGE sections. Write out those local symbols we know are
4457 going into the output file. */
4458 isymend = isymbuf + locsymcount;
4459 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4460 isym < isymend;
4461 isym++, pindex++, ppsection++)
4463 asection *isec;
4464 const char *name;
4465 Elf_Internal_Sym osym;
4467 *pindex = -1;
4469 if (elf_bad_symtab (input_bfd))
4471 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4473 *ppsection = NULL;
4474 continue;
4478 if (isym->st_shndx == SHN_UNDEF)
4479 isec = bfd_und_section_ptr;
4480 else if (isym->st_shndx < SHN_LORESERVE
4481 || isym->st_shndx > SHN_HIRESERVE)
4483 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4484 if (isec
4485 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4486 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4487 isym->st_value =
4488 _bfd_merged_section_offset (output_bfd, &isec,
4489 elf_section_data (isec)->sec_info,
4490 isym->st_value, 0);
4492 else if (isym->st_shndx == SHN_ABS)
4493 isec = bfd_abs_section_ptr;
4494 else if (isym->st_shndx == SHN_COMMON)
4495 isec = bfd_com_section_ptr;
4496 else
4498 /* Who knows? */
4499 isec = NULL;
4502 *ppsection = isec;
4504 /* Don't output the first, undefined, symbol. */
4505 if (ppsection == finfo->sections)
4506 continue;
4508 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4510 /* We never output section symbols. Instead, we use the
4511 section symbol of the corresponding section in the output
4512 file. */
4513 continue;
4516 /* If we are stripping all symbols, we don't want to output this
4517 one. */
4518 if (finfo->info->strip == strip_all)
4519 continue;
4521 /* If we are discarding all local symbols, we don't want to
4522 output this one. If we are generating a relocatable output
4523 file, then some of the local symbols may be required by
4524 relocs; we output them below as we discover that they are
4525 needed. */
4526 if (finfo->info->discard == discard_all)
4527 continue;
4529 /* If this symbol is defined in a section which we are
4530 discarding, we don't need to keep it, but note that
4531 linker_mark is only reliable for sections that have contents.
4532 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4533 as well as linker_mark. */
4534 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4535 && isec != NULL
4536 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4537 || (! finfo->info->relocatable
4538 && (isec->flags & SEC_EXCLUDE) != 0)))
4539 continue;
4541 /* Get the name of the symbol. */
4542 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4543 isym->st_name);
4544 if (name == NULL)
4545 return FALSE;
4547 /* See if we are discarding symbols with this name. */
4548 if ((finfo->info->strip == strip_some
4549 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4550 == NULL))
4551 || (((finfo->info->discard == discard_sec_merge
4552 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4553 || finfo->info->discard == discard_l)
4554 && bfd_is_local_label_name (input_bfd, name)))
4555 continue;
4557 /* If we get here, we are going to output this symbol. */
4559 osym = *isym;
4561 /* Adjust the section index for the output file. */
4562 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4563 isec->output_section);
4564 if (osym.st_shndx == SHN_BAD)
4565 return FALSE;
4567 *pindex = bfd_get_symcount (output_bfd);
4569 /* ELF symbols in relocatable files are section relative, but
4570 in executable files they are virtual addresses. Note that
4571 this code assumes that all ELF sections have an associated
4572 BFD section with a reasonable value for output_offset; below
4573 we assume that they also have a reasonable value for
4574 output_section. Any special sections must be set up to meet
4575 these requirements. */
4576 osym.st_value += isec->output_offset;
4577 if (! finfo->info->relocatable)
4579 osym.st_value += isec->output_section->vma;
4580 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4582 /* STT_TLS symbols are relative to PT_TLS segment base. */
4583 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4584 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4588 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
4589 return FALSE;
4592 /* Relocate the contents of each section. */
4593 sym_hashes = elf_sym_hashes (input_bfd);
4594 for (o = input_bfd->sections; o != NULL; o = o->next)
4596 bfd_byte *contents;
4598 if (! o->linker_mark)
4600 /* This section was omitted from the link. */
4601 continue;
4604 if ((o->flags & SEC_HAS_CONTENTS) == 0
4605 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4606 continue;
4608 if ((o->flags & SEC_LINKER_CREATED) != 0)
4610 /* Section was created by _bfd_elf_link_create_dynamic_sections
4611 or somesuch. */
4612 continue;
4615 /* Get the contents of the section. They have been cached by a
4616 relaxation routine. Note that o is a section in an input
4617 file, so the contents field will not have been set by any of
4618 the routines which work on output files. */
4619 if (elf_section_data (o)->this_hdr.contents != NULL)
4620 contents = elf_section_data (o)->this_hdr.contents;
4621 else
4623 contents = finfo->contents;
4624 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4625 o->_raw_size))
4626 return FALSE;
4629 if ((o->flags & SEC_RELOC) != 0)
4631 Elf_Internal_Rela *internal_relocs;
4633 /* Get the swapped relocs. */
4634 internal_relocs
4635 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4636 finfo->internal_relocs, FALSE);
4637 if (internal_relocs == NULL
4638 && o->reloc_count > 0)
4639 return FALSE;
4641 /* Run through the relocs looking for any against symbols
4642 from discarded sections and section symbols from
4643 removed link-once sections. Complain about relocs
4644 against discarded sections. Zero relocs against removed
4645 link-once sections. Preserve debug information as much
4646 as we can. */
4647 if (!elf_section_ignore_discarded_relocs (o))
4649 Elf_Internal_Rela *rel, *relend;
4651 rel = internal_relocs;
4652 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4653 for ( ; rel < relend; rel++)
4655 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4656 asection *sec;
4658 if (r_symndx >= locsymcount
4659 || (elf_bad_symtab (input_bfd)
4660 && finfo->sections[r_symndx] == NULL))
4662 struct elf_link_hash_entry *h;
4664 h = sym_hashes[r_symndx - extsymoff];
4665 while (h->root.type == bfd_link_hash_indirect
4666 || h->root.type == bfd_link_hash_warning)
4667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4669 /* Complain if the definition comes from a
4670 discarded section. */
4671 sec = h->root.u.def.section;
4672 if ((h->root.type == bfd_link_hash_defined
4673 || h->root.type == bfd_link_hash_defweak)
4674 && elf_discarded_section (sec))
4676 if ((o->flags & SEC_DEBUGGING) != 0)
4678 BFD_ASSERT (r_symndx != 0);
4679 /* Try to preserve debug information. */
4680 if ((o->flags & SEC_DEBUGGING) != 0
4681 && sec->kept_section != NULL
4682 && sec->_raw_size == sec->kept_section->_raw_size)
4683 h->root.u.def.section
4684 = sec->kept_section;
4685 else
4686 memset (rel, 0, sizeof (*rel));
4688 else
4689 finfo->info->callbacks->error_handler
4690 (LD_DEFINITION_IN_DISCARDED_SECTION,
4691 _("%T: discarded in section `%s' from %s\n"),
4692 h->root.root.string,
4693 h->root.root.string,
4694 h->root.u.def.section->name,
4695 bfd_archive_filename (h->root.u.def.section->owner));
4698 else
4700 sec = finfo->sections[r_symndx];
4702 if (sec != NULL && elf_discarded_section (sec))
4704 if ((o->flags & SEC_DEBUGGING) != 0
4705 || (sec->flags & SEC_LINK_ONCE) != 0)
4707 BFD_ASSERT (r_symndx != 0);
4708 /* Try to preserve debug information. */
4709 if ((o->flags & SEC_DEBUGGING) != 0
4710 && sec->kept_section != NULL
4711 && sec->_raw_size == sec->kept_section->_raw_size)
4712 finfo->sections[r_symndx]
4713 = sec->kept_section;
4714 else
4716 rel->r_info
4717 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4718 rel->r_addend = 0;
4721 else
4723 static int count;
4724 int ok;
4725 char *buf;
4727 ok = asprintf (&buf, "local symbol %d",
4728 count++);
4729 if (ok <= 0)
4730 buf = (char *) "local symbol";
4731 finfo->info->callbacks->error_handler
4732 (LD_DEFINITION_IN_DISCARDED_SECTION,
4733 _("%T: discarded in section `%s' from %s\n"),
4734 buf, buf, sec->name,
4735 bfd_archive_filename (input_bfd));
4736 if (ok != -1)
4737 free (buf);
4744 /* Relocate the section by invoking a back end routine.
4746 The back end routine is responsible for adjusting the
4747 section contents as necessary, and (if using Rela relocs
4748 and generating a relocatable output file) adjusting the
4749 reloc addend as necessary.
4751 The back end routine does not have to worry about setting
4752 the reloc address or the reloc symbol index.
4754 The back end routine is given a pointer to the swapped in
4755 internal symbols, and can access the hash table entries
4756 for the external symbols via elf_sym_hashes (input_bfd).
4758 When generating relocatable output, the back end routine
4759 must handle STB_LOCAL/STT_SECTION symbols specially. The
4760 output symbol is going to be a section symbol
4761 corresponding to the output section, which will require
4762 the addend to be adjusted. */
4764 if (! (*relocate_section) (output_bfd, finfo->info,
4765 input_bfd, o, contents,
4766 internal_relocs,
4767 isymbuf,
4768 finfo->sections))
4769 return FALSE;
4771 if (emit_relocs)
4773 Elf_Internal_Rela *irela;
4774 Elf_Internal_Rela *irelaend;
4775 bfd_vma last_offset;
4776 struct elf_link_hash_entry **rel_hash;
4777 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4778 unsigned int next_erel;
4779 bfd_boolean (*reloc_emitter)
4780 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
4781 bfd_boolean rela_normal;
4783 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4784 rela_normal = (bed->rela_normal
4785 && (input_rel_hdr->sh_entsize
4786 == sizeof (Elf_External_Rela)));
4788 /* Adjust the reloc addresses and symbol indices. */
4790 irela = internal_relocs;
4791 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
4792 rel_hash = (elf_section_data (o->output_section)->rel_hashes
4793 + elf_section_data (o->output_section)->rel_count
4794 + elf_section_data (o->output_section)->rel_count2);
4795 last_offset = o->output_offset;
4796 if (!finfo->info->relocatable)
4797 last_offset += o->output_section->vma;
4798 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
4800 unsigned long r_symndx;
4801 asection *sec;
4802 Elf_Internal_Sym sym;
4804 if (next_erel == bed->s->int_rels_per_ext_rel)
4806 rel_hash++;
4807 next_erel = 0;
4810 irela->r_offset = _bfd_elf_section_offset (output_bfd,
4811 finfo->info, o,
4812 irela->r_offset);
4813 if (irela->r_offset >= (bfd_vma) -2)
4815 /* This is a reloc for a deleted entry or somesuch.
4816 Turn it into an R_*_NONE reloc, at the same
4817 offset as the last reloc. elf_eh_frame.c and
4818 elf_bfd_discard_info rely on reloc offsets
4819 being ordered. */
4820 irela->r_offset = last_offset;
4821 irela->r_info = 0;
4822 irela->r_addend = 0;
4823 continue;
4826 irela->r_offset += o->output_offset;
4828 /* Relocs in an executable have to be virtual addresses. */
4829 if (!finfo->info->relocatable)
4830 irela->r_offset += o->output_section->vma;
4832 last_offset = irela->r_offset;
4834 r_symndx = ELF_R_SYM (irela->r_info);
4835 if (r_symndx == STN_UNDEF)
4836 continue;
4838 if (r_symndx >= locsymcount
4839 || (elf_bad_symtab (input_bfd)
4840 && finfo->sections[r_symndx] == NULL))
4842 struct elf_link_hash_entry *rh;
4843 unsigned long indx;
4845 /* This is a reloc against a global symbol. We
4846 have not yet output all the local symbols, so
4847 we do not know the symbol index of any global
4848 symbol. We set the rel_hash entry for this
4849 reloc to point to the global hash table entry
4850 for this symbol. The symbol index is then
4851 set at the end of elf_bfd_final_link. */
4852 indx = r_symndx - extsymoff;
4853 rh = elf_sym_hashes (input_bfd)[indx];
4854 while (rh->root.type == bfd_link_hash_indirect
4855 || rh->root.type == bfd_link_hash_warning)
4856 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
4858 /* Setting the index to -2 tells
4859 elf_link_output_extsym that this symbol is
4860 used by a reloc. */
4861 BFD_ASSERT (rh->indx < 0);
4862 rh->indx = -2;
4864 *rel_hash = rh;
4866 continue;
4869 /* This is a reloc against a local symbol. */
4871 *rel_hash = NULL;
4872 sym = isymbuf[r_symndx];
4873 sec = finfo->sections[r_symndx];
4874 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
4876 /* I suppose the backend ought to fill in the
4877 section of any STT_SECTION symbol against a
4878 processor specific section. If we have
4879 discarded a section, the output_section will
4880 be the absolute section. */
4881 if (bfd_is_abs_section (sec)
4882 || (sec != NULL
4883 && bfd_is_abs_section (sec->output_section)))
4884 r_symndx = 0;
4885 else if (sec == NULL || sec->owner == NULL)
4887 bfd_set_error (bfd_error_bad_value);
4888 return FALSE;
4890 else
4892 r_symndx = sec->output_section->target_index;
4893 BFD_ASSERT (r_symndx != 0);
4896 /* Adjust the addend according to where the
4897 section winds up in the output section. */
4898 if (rela_normal)
4899 irela->r_addend += sec->output_offset;
4901 else
4903 if (finfo->indices[r_symndx] == -1)
4905 unsigned long shlink;
4906 const char *name;
4907 asection *osec;
4909 if (finfo->info->strip == strip_all)
4911 /* You can't do ld -r -s. */
4912 bfd_set_error (bfd_error_invalid_operation);
4913 return FALSE;
4916 /* This symbol was skipped earlier, but
4917 since it is needed by a reloc, we
4918 must output it now. */
4919 shlink = symtab_hdr->sh_link;
4920 name = (bfd_elf_string_from_elf_section
4921 (input_bfd, shlink, sym.st_name));
4922 if (name == NULL)
4923 return FALSE;
4925 osec = sec->output_section;
4926 sym.st_shndx =
4927 _bfd_elf_section_from_bfd_section (output_bfd,
4928 osec);
4929 if (sym.st_shndx == SHN_BAD)
4930 return FALSE;
4932 sym.st_value += sec->output_offset;
4933 if (! finfo->info->relocatable)
4935 sym.st_value += osec->vma;
4936 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
4938 /* STT_TLS symbols are relative to PT_TLS
4939 segment base. */
4940 BFD_ASSERT (elf_hash_table (finfo->info)
4941 ->tls_sec != NULL);
4942 sym.st_value -= (elf_hash_table (finfo->info)
4943 ->tls_sec->vma);
4947 finfo->indices[r_symndx]
4948 = bfd_get_symcount (output_bfd);
4950 if (! elf_link_output_sym (finfo, name, &sym, sec,
4951 NULL))
4952 return FALSE;
4955 r_symndx = finfo->indices[r_symndx];
4958 irela->r_info = ELF_R_INFO (r_symndx,
4959 ELF_R_TYPE (irela->r_info));
4962 /* Swap out the relocs. */
4963 if (bed->elf_backend_emit_relocs
4964 && !(finfo->info->relocatable
4965 || finfo->info->emitrelocations))
4966 reloc_emitter = bed->elf_backend_emit_relocs;
4967 else
4968 reloc_emitter = _bfd_elf_link_output_relocs;
4970 if (input_rel_hdr->sh_size != 0
4971 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
4972 internal_relocs))
4973 return FALSE;
4975 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
4976 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
4978 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
4979 * bed->s->int_rels_per_ext_rel);
4980 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
4981 internal_relocs))
4982 return FALSE;
4987 /* Write out the modified section contents. */
4988 if (bed->elf_backend_write_section
4989 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
4991 /* Section written out. */
4993 else switch (o->sec_info_type)
4995 case ELF_INFO_TYPE_STABS:
4996 if (! (_bfd_write_section_stabs
4997 (output_bfd,
4998 &elf_hash_table (finfo->info)->stab_info,
4999 o, &elf_section_data (o)->sec_info, contents)))
5000 return FALSE;
5001 break;
5002 case ELF_INFO_TYPE_MERGE:
5003 if (! _bfd_write_merged_section (output_bfd, o,
5004 elf_section_data (o)->sec_info))
5005 return FALSE;
5006 break;
5007 case ELF_INFO_TYPE_EH_FRAME:
5009 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5010 o, contents))
5011 return FALSE;
5013 break;
5014 default:
5016 bfd_size_type sec_size;
5018 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5019 if (! (o->flags & SEC_EXCLUDE)
5020 && ! bfd_set_section_contents (output_bfd, o->output_section,
5021 contents,
5022 (file_ptr) o->output_offset,
5023 sec_size))
5024 return FALSE;
5026 break;
5030 return TRUE;
5033 /* Generate a reloc when linking an ELF file. This is a reloc
5034 requested by the linker, and does come from any input file. This
5035 is used to build constructor and destructor tables when linking
5036 with -Ur. */
5038 static bfd_boolean
5039 elf_reloc_link_order (bfd *output_bfd,
5040 struct bfd_link_info *info,
5041 asection *output_section,
5042 struct bfd_link_order *link_order)
5044 reloc_howto_type *howto;
5045 long indx;
5046 bfd_vma offset;
5047 bfd_vma addend;
5048 struct elf_link_hash_entry **rel_hash_ptr;
5049 Elf_Internal_Shdr *rel_hdr;
5050 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5051 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5052 bfd_byte *erel;
5053 unsigned int i;
5055 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5056 if (howto == NULL)
5058 bfd_set_error (bfd_error_bad_value);
5059 return FALSE;
5062 addend = link_order->u.reloc.p->addend;
5064 /* Figure out the symbol index. */
5065 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5066 + elf_section_data (output_section)->rel_count
5067 + elf_section_data (output_section)->rel_count2);
5068 if (link_order->type == bfd_section_reloc_link_order)
5070 indx = link_order->u.reloc.p->u.section->target_index;
5071 BFD_ASSERT (indx != 0);
5072 *rel_hash_ptr = NULL;
5074 else
5076 struct elf_link_hash_entry *h;
5078 /* Treat a reloc against a defined symbol as though it were
5079 actually against the section. */
5080 h = ((struct elf_link_hash_entry *)
5081 bfd_wrapped_link_hash_lookup (output_bfd, info,
5082 link_order->u.reloc.p->u.name,
5083 FALSE, FALSE, TRUE));
5084 if (h != NULL
5085 && (h->root.type == bfd_link_hash_defined
5086 || h->root.type == bfd_link_hash_defweak))
5088 asection *section;
5090 section = h->root.u.def.section;
5091 indx = section->output_section->target_index;
5092 *rel_hash_ptr = NULL;
5093 /* It seems that we ought to add the symbol value to the
5094 addend here, but in practice it has already been added
5095 because it was passed to constructor_callback. */
5096 addend += section->output_section->vma + section->output_offset;
5098 else if (h != NULL)
5100 /* Setting the index to -2 tells elf_link_output_extsym that
5101 this symbol is used by a reloc. */
5102 h->indx = -2;
5103 *rel_hash_ptr = h;
5104 indx = 0;
5106 else
5108 if (! ((*info->callbacks->unattached_reloc)
5109 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
5110 return FALSE;
5111 indx = 0;
5115 /* If this is an inplace reloc, we must write the addend into the
5116 object file. */
5117 if (howto->partial_inplace && addend != 0)
5119 bfd_size_type size;
5120 bfd_reloc_status_type rstat;
5121 bfd_byte *buf;
5122 bfd_boolean ok;
5123 const char *sym_name;
5125 size = bfd_get_reloc_size (howto);
5126 buf = bfd_zmalloc (size);
5127 if (buf == NULL)
5128 return FALSE;
5129 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5130 switch (rstat)
5132 case bfd_reloc_ok:
5133 break;
5135 default:
5136 case bfd_reloc_outofrange:
5137 abort ();
5139 case bfd_reloc_overflow:
5140 if (link_order->type == bfd_section_reloc_link_order)
5141 sym_name = bfd_section_name (output_bfd,
5142 link_order->u.reloc.p->u.section);
5143 else
5144 sym_name = link_order->u.reloc.p->u.name;
5145 if (! ((*info->callbacks->reloc_overflow)
5146 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
5148 free (buf);
5149 return FALSE;
5151 break;
5153 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5154 link_order->offset, size);
5155 free (buf);
5156 if (! ok)
5157 return FALSE;
5160 /* The address of a reloc is relative to the section in a
5161 relocatable file, and is a virtual address in an executable
5162 file. */
5163 offset = link_order->offset;
5164 if (! info->relocatable)
5165 offset += output_section->vma;
5167 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5169 irel[i].r_offset = offset;
5170 irel[i].r_info = 0;
5171 irel[i].r_addend = 0;
5173 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5175 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5176 erel = rel_hdr->contents;
5177 if (rel_hdr->sh_type == SHT_REL)
5179 erel += (elf_section_data (output_section)->rel_count
5180 * sizeof (Elf_External_Rel));
5181 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5183 else
5185 irel[0].r_addend = addend;
5186 erel += (elf_section_data (output_section)->rel_count
5187 * sizeof (Elf_External_Rela));
5188 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5191 ++elf_section_data (output_section)->rel_count;
5193 return TRUE;
5196 /* Garbage collect unused sections. */
5198 static bfd_boolean elf_gc_sweep_symbol
5199 (struct elf_link_hash_entry *, void *);
5201 static bfd_boolean elf_gc_allocate_got_offsets
5202 (struct elf_link_hash_entry *, void *);
5204 /* The mark phase of garbage collection. For a given section, mark
5205 it and any sections in this section's group, and all the sections
5206 which define symbols to which it refers. */
5208 typedef asection * (*gc_mark_hook_fn)
5209 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5210 struct elf_link_hash_entry *, Elf_Internal_Sym *);
5212 static bfd_boolean
5213 elf_gc_mark (struct bfd_link_info *info,
5214 asection *sec,
5215 gc_mark_hook_fn gc_mark_hook)
5217 bfd_boolean ret;
5218 asection *group_sec;
5220 sec->gc_mark = 1;
5222 /* Mark all the sections in the group. */
5223 group_sec = elf_section_data (sec)->next_in_group;
5224 if (group_sec && !group_sec->gc_mark)
5225 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5226 return FALSE;
5228 /* Look through the section relocs. */
5229 ret = TRUE;
5230 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5232 Elf_Internal_Rela *relstart, *rel, *relend;
5233 Elf_Internal_Shdr *symtab_hdr;
5234 struct elf_link_hash_entry **sym_hashes;
5235 size_t nlocsyms;
5236 size_t extsymoff;
5237 bfd *input_bfd = sec->owner;
5238 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5239 Elf_Internal_Sym *isym = NULL;
5241 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5242 sym_hashes = elf_sym_hashes (input_bfd);
5244 /* Read the local symbols. */
5245 if (elf_bad_symtab (input_bfd))
5247 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5248 extsymoff = 0;
5250 else
5251 extsymoff = nlocsyms = symtab_hdr->sh_info;
5253 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5254 if (isym == NULL && nlocsyms != 0)
5256 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5257 NULL, NULL, NULL);
5258 if (isym == NULL)
5259 return FALSE;
5262 /* Read the relocations. */
5263 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5264 info->keep_memory);
5265 if (relstart == NULL)
5267 ret = FALSE;
5268 goto out1;
5270 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5272 for (rel = relstart; rel < relend; rel++)
5274 unsigned long r_symndx;
5275 asection *rsec;
5276 struct elf_link_hash_entry *h;
5278 r_symndx = ELF_R_SYM (rel->r_info);
5279 if (r_symndx == 0)
5280 continue;
5282 if (r_symndx >= nlocsyms
5283 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5285 h = sym_hashes[r_symndx - extsymoff];
5286 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5288 else
5290 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5293 if (rsec && !rsec->gc_mark)
5295 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5296 rsec->gc_mark = 1;
5297 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5299 ret = FALSE;
5300 goto out2;
5305 out2:
5306 if (elf_section_data (sec)->relocs != relstart)
5307 free (relstart);
5308 out1:
5309 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5311 if (! info->keep_memory)
5312 free (isym);
5313 else
5314 symtab_hdr->contents = (unsigned char *) isym;
5318 return ret;
5321 /* The sweep phase of garbage collection. Remove all garbage sections. */
5323 typedef bfd_boolean (*gc_sweep_hook_fn)
5324 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
5326 static bfd_boolean
5327 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
5329 bfd *sub;
5331 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5333 asection *o;
5335 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5336 continue;
5338 for (o = sub->sections; o != NULL; o = o->next)
5340 /* Keep special sections. Keep .debug sections. */
5341 if ((o->flags & SEC_LINKER_CREATED)
5342 || (o->flags & SEC_DEBUGGING))
5343 o->gc_mark = 1;
5345 if (o->gc_mark)
5346 continue;
5348 /* Skip sweeping sections already excluded. */
5349 if (o->flags & SEC_EXCLUDE)
5350 continue;
5352 /* Since this is early in the link process, it is simple
5353 to remove a section from the output. */
5354 o->flags |= SEC_EXCLUDE;
5356 /* But we also have to update some of the relocation
5357 info we collected before. */
5358 if (gc_sweep_hook
5359 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5361 Elf_Internal_Rela *internal_relocs;
5362 bfd_boolean r;
5364 internal_relocs
5365 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
5366 info->keep_memory);
5367 if (internal_relocs == NULL)
5368 return FALSE;
5370 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5372 if (elf_section_data (o)->relocs != internal_relocs)
5373 free (internal_relocs);
5375 if (!r)
5376 return FALSE;
5381 /* Remove the symbols that were in the swept sections from the dynamic
5382 symbol table. GCFIXME: Anyone know how to get them out of the
5383 static symbol table as well? */
5385 int i = 0;
5387 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
5389 elf_hash_table (info)->dynsymcount = i;
5392 return TRUE;
5395 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5397 static bfd_boolean
5398 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
5400 int *idx = idxptr;
5402 if (h->root.type == bfd_link_hash_warning)
5403 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5405 if (h->dynindx != -1
5406 && ((h->root.type != bfd_link_hash_defined
5407 && h->root.type != bfd_link_hash_defweak)
5408 || h->root.u.def.section->gc_mark))
5409 h->dynindx = (*idx)++;
5411 return TRUE;
5414 /* Propagate collected vtable information. This is called through
5415 elf_link_hash_traverse. */
5417 static bfd_boolean
5418 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
5420 if (h->root.type == bfd_link_hash_warning)
5421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5423 /* Those that are not vtables. */
5424 if (h->vtable_parent == NULL)
5425 return TRUE;
5427 /* Those vtables that do not have parents, we cannot merge. */
5428 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5429 return TRUE;
5431 /* If we've already been done, exit. */
5432 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5433 return TRUE;
5435 /* Make sure the parent's table is up to date. */
5436 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5438 if (h->vtable_entries_used == NULL)
5440 /* None of this table's entries were referenced. Re-use the
5441 parent's table. */
5442 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5443 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5445 else
5447 size_t n;
5448 bfd_boolean *cu, *pu;
5450 /* Or the parent's entries into ours. */
5451 cu = h->vtable_entries_used;
5452 cu[-1] = TRUE;
5453 pu = h->vtable_parent->vtable_entries_used;
5454 if (pu != NULL)
5456 const struct elf_backend_data *bed;
5457 unsigned int log_file_align;
5459 bed = get_elf_backend_data (h->root.u.def.section->owner);
5460 log_file_align = bed->s->log_file_align;
5461 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5462 while (n--)
5464 if (*pu)
5465 *cu = TRUE;
5466 pu++;
5467 cu++;
5472 return TRUE;
5475 static bfd_boolean
5476 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
5478 asection *sec;
5479 bfd_vma hstart, hend;
5480 Elf_Internal_Rela *relstart, *relend, *rel;
5481 const struct elf_backend_data *bed;
5482 unsigned int log_file_align;
5484 if (h->root.type == bfd_link_hash_warning)
5485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5487 /* Take care of both those symbols that do not describe vtables as
5488 well as those that are not loaded. */
5489 if (h->vtable_parent == NULL)
5490 return TRUE;
5492 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5493 || h->root.type == bfd_link_hash_defweak);
5495 sec = h->root.u.def.section;
5496 hstart = h->root.u.def.value;
5497 hend = hstart + h->size;
5499 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
5500 if (!relstart)
5501 return *(bfd_boolean *) okp = FALSE;
5502 bed = get_elf_backend_data (sec->owner);
5503 log_file_align = bed->s->log_file_align;
5505 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5507 for (rel = relstart; rel < relend; ++rel)
5508 if (rel->r_offset >= hstart && rel->r_offset < hend)
5510 /* If the entry is in use, do nothing. */
5511 if (h->vtable_entries_used
5512 && (rel->r_offset - hstart) < h->vtable_entries_size)
5514 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5515 if (h->vtable_entries_used[entry])
5516 continue;
5518 /* Otherwise, kill it. */
5519 rel->r_offset = rel->r_info = rel->r_addend = 0;
5522 return TRUE;
5525 /* Do mark and sweep of unused sections. */
5527 bfd_boolean
5528 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5530 bfd_boolean ok = TRUE;
5531 bfd *sub;
5532 asection * (*gc_mark_hook)
5533 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5534 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
5536 if (!get_elf_backend_data (abfd)->can_gc_sections
5537 || info->relocatable
5538 || info->emitrelocations
5539 || !is_elf_hash_table (info->hash)
5540 || elf_hash_table (info)->dynamic_sections_created)
5542 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5543 return TRUE;
5546 /* Apply transitive closure to the vtable entry usage info. */
5547 elf_link_hash_traverse (elf_hash_table (info),
5548 elf_gc_propagate_vtable_entries_used,
5549 &ok);
5550 if (!ok)
5551 return FALSE;
5553 /* Kill the vtable relocations that were not used. */
5554 elf_link_hash_traverse (elf_hash_table (info),
5555 elf_gc_smash_unused_vtentry_relocs,
5556 &ok);
5557 if (!ok)
5558 return FALSE;
5560 /* Grovel through relocs to find out who stays ... */
5562 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5563 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5565 asection *o;
5567 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5568 continue;
5570 for (o = sub->sections; o != NULL; o = o->next)
5572 if (o->flags & SEC_KEEP)
5573 if (!elf_gc_mark (info, o, gc_mark_hook))
5574 return FALSE;
5578 /* ... and mark SEC_EXCLUDE for those that go. */
5579 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5580 return FALSE;
5582 return TRUE;
5585 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5587 bfd_boolean
5588 elf_gc_record_vtinherit (bfd *abfd,
5589 asection *sec,
5590 struct elf_link_hash_entry *h,
5591 bfd_vma offset)
5593 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5594 struct elf_link_hash_entry **search, *child;
5595 bfd_size_type extsymcount;
5597 /* The sh_info field of the symtab header tells us where the
5598 external symbols start. We don't care about the local symbols at
5599 this point. */
5600 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5601 if (!elf_bad_symtab (abfd))
5602 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5604 sym_hashes = elf_sym_hashes (abfd);
5605 sym_hashes_end = sym_hashes + extsymcount;
5607 /* Hunt down the child symbol, which is in this section at the same
5608 offset as the relocation. */
5609 for (search = sym_hashes; search != sym_hashes_end; ++search)
5611 if ((child = *search) != NULL
5612 && (child->root.type == bfd_link_hash_defined
5613 || child->root.type == bfd_link_hash_defweak)
5614 && child->root.u.def.section == sec
5615 && child->root.u.def.value == offset)
5616 goto win;
5619 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
5620 bfd_archive_filename (abfd), sec->name,
5621 (unsigned long) offset);
5622 bfd_set_error (bfd_error_invalid_operation);
5623 return FALSE;
5625 win:
5626 if (!h)
5628 /* This *should* only be the absolute section. It could potentially
5629 be that someone has defined a non-global vtable though, which
5630 would be bad. It isn't worth paging in the local symbols to be
5631 sure though; that case should simply be handled by the assembler. */
5633 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5635 else
5636 child->vtable_parent = h;
5638 return TRUE;
5641 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5643 bfd_boolean
5644 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5645 asection *sec ATTRIBUTE_UNUSED,
5646 struct elf_link_hash_entry *h,
5647 bfd_vma addend)
5649 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5650 unsigned int log_file_align = bed->s->log_file_align;
5652 if (addend >= h->vtable_entries_size)
5654 size_t size, bytes, file_align;
5655 bfd_boolean *ptr = h->vtable_entries_used;
5657 /* While the symbol is undefined, we have to be prepared to handle
5658 a zero size. */
5659 file_align = 1 << log_file_align;
5660 if (h->root.type == bfd_link_hash_undefined)
5661 size = addend + file_align;
5662 else
5664 size = h->size;
5665 if (addend >= size)
5667 /* Oops! We've got a reference past the defined end of
5668 the table. This is probably a bug -- shall we warn? */
5669 size = addend + file_align;
5672 size = (size + file_align - 1) & -file_align;
5674 /* Allocate one extra entry for use as a "done" flag for the
5675 consolidation pass. */
5676 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
5678 if (ptr)
5680 ptr = bfd_realloc (ptr - 1, bytes);
5682 if (ptr != NULL)
5684 size_t oldbytes;
5686 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
5687 * sizeof (bfd_boolean));
5688 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
5691 else
5692 ptr = bfd_zmalloc (bytes);
5694 if (ptr == NULL)
5695 return FALSE;
5697 /* And arrange for that done flag to be at index -1. */
5698 h->vtable_entries_used = ptr + 1;
5699 h->vtable_entries_size = size;
5702 h->vtable_entries_used[addend >> log_file_align] = TRUE;
5704 return TRUE;
5707 /* And an accompanying bit to work out final got entry offsets once
5708 we're done. Should be called from final_link. */
5710 bfd_boolean
5711 elf_gc_common_finalize_got_offsets (bfd *abfd,
5712 struct bfd_link_info *info)
5714 bfd *i;
5715 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5716 bfd_vma gotoff;
5718 if (! is_elf_hash_table (info->hash))
5719 return FALSE;
5721 /* The GOT offset is relative to the .got section, but the GOT header is
5722 put into the .got.plt section, if the backend uses it. */
5723 if (bed->want_got_plt)
5724 gotoff = 0;
5725 else
5726 gotoff = bed->got_header_size;
5728 /* Do the local .got entries first. */
5729 for (i = info->input_bfds; i; i = i->link_next)
5731 bfd_signed_vma *local_got;
5732 bfd_size_type j, locsymcount;
5733 Elf_Internal_Shdr *symtab_hdr;
5735 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5736 continue;
5738 local_got = elf_local_got_refcounts (i);
5739 if (!local_got)
5740 continue;
5742 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5743 if (elf_bad_symtab (i))
5744 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5745 else
5746 locsymcount = symtab_hdr->sh_info;
5748 for (j = 0; j < locsymcount; ++j)
5750 if (local_got[j] > 0)
5752 local_got[j] = gotoff;
5753 gotoff += ARCH_SIZE / 8;
5755 else
5756 local_got[j] = (bfd_vma) -1;
5760 /* Then the global .got entries. .plt refcounts are handled by
5761 adjust_dynamic_symbol */
5762 elf_link_hash_traverse (elf_hash_table (info),
5763 elf_gc_allocate_got_offsets,
5764 &gotoff);
5765 return TRUE;
5768 /* We need a special top-level link routine to convert got reference counts
5769 to real got offsets. */
5771 static bfd_boolean
5772 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
5774 bfd_vma *off = offarg;
5776 if (h->root.type == bfd_link_hash_warning)
5777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5779 if (h->got.refcount > 0)
5781 h->got.offset = off[0];
5782 off[0] += ARCH_SIZE / 8;
5784 else
5785 h->got.offset = (bfd_vma) -1;
5787 return TRUE;
5790 /* Many folk need no more in the way of final link than this, once
5791 got entry reference counting is enabled. */
5793 bfd_boolean
5794 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
5796 if (!elf_gc_common_finalize_got_offsets (abfd, info))
5797 return FALSE;
5799 /* Invoke the regular ELF backend linker to do all the work. */
5800 return elf_bfd_final_link (abfd, info);
5803 /* This function will be called though elf_link_hash_traverse to store
5804 all hash value of the exported symbols in an array. */
5806 static bfd_boolean
5807 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5809 unsigned long **valuep = data;
5810 const char *name;
5811 char *p;
5812 unsigned long ha;
5813 char *alc = NULL;
5815 if (h->root.type == bfd_link_hash_warning)
5816 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5818 /* Ignore indirect symbols. These are added by the versioning code. */
5819 if (h->dynindx == -1)
5820 return TRUE;
5822 name = h->root.root.string;
5823 p = strchr (name, ELF_VER_CHR);
5824 if (p != NULL)
5826 alc = bfd_malloc (p - name + 1);
5827 memcpy (alc, name, p - name);
5828 alc[p - name] = '\0';
5829 name = alc;
5832 /* Compute the hash value. */
5833 ha = bfd_elf_hash (name);
5835 /* Store the found hash value in the array given as the argument. */
5836 *(*valuep)++ = ha;
5838 /* And store it in the struct so that we can put it in the hash table
5839 later. */
5840 h->elf_hash_value = ha;
5842 if (alc != NULL)
5843 free (alc);
5845 return TRUE;
5848 bfd_boolean
5849 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
5851 struct elf_reloc_cookie *rcookie = cookie;
5853 if (rcookie->bad_symtab)
5854 rcookie->rel = rcookie->rels;
5856 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
5858 unsigned long r_symndx;
5860 if (! rcookie->bad_symtab)
5861 if (rcookie->rel->r_offset > offset)
5862 return FALSE;
5863 if (rcookie->rel->r_offset != offset)
5864 continue;
5866 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
5867 if (r_symndx == SHN_UNDEF)
5868 return TRUE;
5870 if (r_symndx >= rcookie->locsymcount
5871 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
5873 struct elf_link_hash_entry *h;
5875 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
5877 while (h->root.type == bfd_link_hash_indirect
5878 || h->root.type == bfd_link_hash_warning)
5879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5881 if ((h->root.type == bfd_link_hash_defined
5882 || h->root.type == bfd_link_hash_defweak)
5883 && elf_discarded_section (h->root.u.def.section))
5884 return TRUE;
5885 else
5886 return FALSE;
5888 else
5890 /* It's not a relocation against a global symbol,
5891 but it could be a relocation against a local
5892 symbol for a discarded section. */
5893 asection *isec;
5894 Elf_Internal_Sym *isym;
5896 /* Need to: get the symbol; get the section. */
5897 isym = &rcookie->locsyms[r_symndx];
5898 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
5900 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
5901 if (isec != NULL && elf_discarded_section (isec))
5902 return TRUE;
5905 return FALSE;
5907 return FALSE;
5910 /* Discard unneeded references to discarded sections.
5911 Returns TRUE if any section's size was changed. */
5912 /* This function assumes that the relocations are in sorted order,
5913 which is true for all known assemblers. */
5915 bfd_boolean
5916 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
5918 struct elf_reloc_cookie cookie;
5919 asection *stab, *eh;
5920 Elf_Internal_Shdr *symtab_hdr;
5921 const struct elf_backend_data *bed;
5922 bfd *abfd;
5923 unsigned int count;
5924 bfd_boolean ret = FALSE;
5926 if (info->traditional_format
5927 || !is_elf_hash_table (info->hash))
5928 return FALSE;
5930 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
5932 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5933 continue;
5935 bed = get_elf_backend_data (abfd);
5937 if ((abfd->flags & DYNAMIC) != 0)
5938 continue;
5940 eh = bfd_get_section_by_name (abfd, ".eh_frame");
5941 if (info->relocatable
5942 || (eh != NULL
5943 && (eh->_raw_size == 0
5944 || bfd_is_abs_section (eh->output_section))))
5945 eh = NULL;
5947 stab = bfd_get_section_by_name (abfd, ".stab");
5948 if (stab != NULL
5949 && (stab->_raw_size == 0
5950 || bfd_is_abs_section (stab->output_section)
5951 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
5952 stab = NULL;
5954 if (stab == NULL
5955 && eh == NULL
5956 && bed->elf_backend_discard_info == NULL)
5957 continue;
5959 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5960 cookie.abfd = abfd;
5961 cookie.sym_hashes = elf_sym_hashes (abfd);
5962 cookie.bad_symtab = elf_bad_symtab (abfd);
5963 if (cookie.bad_symtab)
5965 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5966 cookie.extsymoff = 0;
5968 else
5970 cookie.locsymcount = symtab_hdr->sh_info;
5971 cookie.extsymoff = symtab_hdr->sh_info;
5974 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5975 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
5977 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5978 cookie.locsymcount, 0,
5979 NULL, NULL, NULL);
5980 if (cookie.locsyms == NULL)
5981 return FALSE;
5984 if (stab != NULL)
5986 cookie.rels = NULL;
5987 count = stab->reloc_count;
5988 if (count != 0)
5989 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
5990 info->keep_memory);
5991 if (cookie.rels != NULL)
5993 cookie.rel = cookie.rels;
5994 cookie.relend = cookie.rels;
5995 cookie.relend += count * bed->s->int_rels_per_ext_rel;
5996 if (_bfd_discard_section_stabs (abfd, stab,
5997 elf_section_data (stab)->sec_info,
5998 elf_reloc_symbol_deleted_p,
5999 &cookie))
6000 ret = TRUE;
6001 if (elf_section_data (stab)->relocs != cookie.rels)
6002 free (cookie.rels);
6006 if (eh != NULL)
6008 cookie.rels = NULL;
6009 count = eh->reloc_count;
6010 if (count != 0)
6011 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6012 info->keep_memory);
6013 cookie.rel = cookie.rels;
6014 cookie.relend = cookie.rels;
6015 if (cookie.rels != NULL)
6016 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6018 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6019 elf_reloc_symbol_deleted_p,
6020 &cookie))
6021 ret = TRUE;
6023 if (cookie.rels != NULL
6024 && elf_section_data (eh)->relocs != cookie.rels)
6025 free (cookie.rels);
6028 if (bed->elf_backend_discard_info != NULL
6029 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6030 ret = TRUE;
6032 if (cookie.locsyms != NULL
6033 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6035 if (! info->keep_memory)
6036 free (cookie.locsyms);
6037 else
6038 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6042 if (info->eh_frame_hdr
6043 && !info->relocatable
6044 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6045 ret = TRUE;
6047 return ret;
6050 static bfd_boolean
6051 elf_section_ignore_discarded_relocs (asection *sec)
6053 const struct elf_backend_data *bed;
6055 switch (sec->sec_info_type)
6057 case ELF_INFO_TYPE_STABS:
6058 case ELF_INFO_TYPE_EH_FRAME:
6059 return TRUE;
6060 default:
6061 break;
6064 bed = get_elf_backend_data (sec->owner);
6065 if (bed->elf_backend_ignore_discarded_relocs != NULL
6066 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6067 return TRUE;
6069 return FALSE;