* elflink.c (_bfd_elf_merge_symbol): Rewrite weak symbol handling.
[binutils.git] / bfd / elflink.h
blobec20e75167dad55cc076f6d9e5b39ec0881de289
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 symbols from an ELF object file to the linker hash table. */
73 static bfd_boolean
74 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
76 bfd_boolean (*add_symbol_hook)
77 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
78 const char **, flagword *, asection **, bfd_vma *);
79 bfd_boolean (*check_relocs)
80 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
81 bfd_boolean collect;
82 Elf_Internal_Shdr *hdr;
83 bfd_size_type symcount;
84 bfd_size_type extsymcount;
85 bfd_size_type extsymoff;
86 struct elf_link_hash_entry **sym_hash;
87 bfd_boolean dynamic;
88 Elf_External_Versym *extversym = NULL;
89 Elf_External_Versym *ever;
90 struct elf_link_hash_entry *weaks;
91 struct elf_link_hash_entry **nondeflt_vers = NULL;
92 bfd_size_type nondeflt_vers_cnt = 0;
93 Elf_Internal_Sym *isymbuf = NULL;
94 Elf_Internal_Sym *isym;
95 Elf_Internal_Sym *isymend;
96 const struct elf_backend_data *bed;
97 bfd_boolean dt_needed;
98 bfd_boolean add_needed;
99 struct elf_link_hash_table * hash_table;
100 bfd_size_type amt;
102 hash_table = elf_hash_table (info);
104 bed = get_elf_backend_data (abfd);
105 add_symbol_hook = bed->elf_add_symbol_hook;
106 collect = bed->collect;
108 if ((abfd->flags & DYNAMIC) == 0)
109 dynamic = FALSE;
110 else
112 dynamic = TRUE;
114 /* You can't use -r against a dynamic object. Also, there's no
115 hope of using a dynamic object which does not exactly match
116 the format of the output file. */
117 if (info->relocatable
118 || !is_elf_hash_table (hash_table)
119 || hash_table->root.creator != abfd->xvec)
121 bfd_set_error (bfd_error_invalid_operation);
122 goto error_return;
126 /* As a GNU extension, any input sections which are named
127 .gnu.warning.SYMBOL are treated as warning symbols for the given
128 symbol. This differs from .gnu.warning sections, which generate
129 warnings when they are included in an output file. */
130 if (info->executable)
132 asection *s;
134 for (s = abfd->sections; s != NULL; s = s->next)
136 const char *name;
138 name = bfd_get_section_name (abfd, s);
139 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
141 char *msg;
142 bfd_size_type sz;
143 bfd_size_type prefix_len;
144 const char * gnu_warning_prefix = _("warning: ");
146 name += sizeof ".gnu.warning." - 1;
148 /* If this is a shared object, then look up the symbol
149 in the hash table. If it is there, and it is already
150 been defined, then we will not be using the entry
151 from this shared object, so we don't need to warn.
152 FIXME: If we see the definition in a regular object
153 later on, we will warn, but we shouldn't. The only
154 fix is to keep track of what warnings we are supposed
155 to emit, and then handle them all at the end of the
156 link. */
157 if (dynamic)
159 struct elf_link_hash_entry *h;
161 h = elf_link_hash_lookup (hash_table, name,
162 FALSE, FALSE, TRUE);
164 /* FIXME: What about bfd_link_hash_common? */
165 if (h != NULL
166 && (h->root.type == bfd_link_hash_defined
167 || h->root.type == bfd_link_hash_defweak))
169 /* We don't want to issue this warning. Clobber
170 the section size so that the warning does not
171 get copied into the output file. */
172 s->_raw_size = 0;
173 continue;
177 sz = bfd_section_size (abfd, s);
178 prefix_len = strlen (gnu_warning_prefix);
179 msg = bfd_alloc (abfd, prefix_len + sz + 1);
180 if (msg == NULL)
181 goto error_return;
183 strcpy (msg, gnu_warning_prefix);
184 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
185 goto error_return;
187 msg[prefix_len + sz] = '\0';
189 if (! (_bfd_generic_link_add_one_symbol
190 (info, abfd, name, BSF_WARNING, s, 0, msg,
191 FALSE, collect, NULL)))
192 goto error_return;
194 if (! info->relocatable)
196 /* Clobber the section size so that the warning does
197 not get copied into the output file. */
198 s->_raw_size = 0;
204 dt_needed = FALSE;
205 add_needed = FALSE;
206 if (! dynamic)
208 /* If we are creating a shared library, create all the dynamic
209 sections immediately. We need to attach them to something,
210 so we attach them to this BFD, provided it is the right
211 format. FIXME: If there are no input BFD's of the same
212 format as the output, we can't make a shared library. */
213 if (info->shared
214 && is_elf_hash_table (hash_table)
215 && hash_table->root.creator == abfd->xvec
216 && ! hash_table->dynamic_sections_created)
218 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
219 goto error_return;
222 else if (!is_elf_hash_table (hash_table))
223 goto error_return;
224 else
226 asection *s;
227 const char *name;
228 bfd_size_type oldsize;
229 bfd_size_type strindex;
230 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
232 /* ld --just-symbols and dynamic objects don't mix very well.
233 Test for --just-symbols by looking at info set up by
234 _bfd_elf_link_just_syms. */
235 if ((s = abfd->sections) != NULL
236 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
237 goto error_return;
239 /* Find the name to use in a DT_NEEDED entry that refers to this
240 object. If the object has a DT_SONAME entry, we use it.
241 Otherwise, if the generic linker stuck something in
242 elf_dt_name, we use that. Otherwise, we just use the file
243 name. If the generic linker put a null string into
244 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
245 there is a DT_SONAME entry. */
246 add_needed = TRUE;
247 name = bfd_get_filename (abfd);
248 if (elf_dt_name (abfd) != NULL)
250 name = elf_dt_name (abfd);
251 if (*name == '\0')
253 if (elf_dt_soname (abfd) != NULL)
254 dt_needed = TRUE;
256 add_needed = FALSE;
259 s = bfd_get_section_by_name (abfd, ".dynamic");
260 if (s != NULL)
262 Elf_External_Dyn *dynbuf = NULL;
263 Elf_External_Dyn *extdyn;
264 Elf_External_Dyn *extdynend;
265 int elfsec;
266 unsigned long shlink;
268 dynbuf = bfd_malloc (s->_raw_size);
269 if (dynbuf == NULL)
270 goto error_return;
272 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
273 goto error_free_dyn;
275 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
276 if (elfsec == -1)
277 goto error_free_dyn;
278 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
280 extdyn = dynbuf;
281 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
282 for (; extdyn < extdynend; extdyn++)
284 Elf_Internal_Dyn dyn;
286 elf_swap_dyn_in (abfd, extdyn, &dyn);
287 if (dyn.d_tag == DT_SONAME)
289 unsigned int tagv = dyn.d_un.d_val;
290 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
291 if (name == NULL)
292 goto error_free_dyn;
294 if (dyn.d_tag == DT_NEEDED)
296 struct bfd_link_needed_list *n, **pn;
297 char *fnm, *anm;
298 unsigned int tagv = dyn.d_un.d_val;
300 amt = sizeof (struct bfd_link_needed_list);
301 n = bfd_alloc (abfd, amt);
302 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
303 if (n == NULL || fnm == NULL)
304 goto error_free_dyn;
305 amt = strlen (fnm) + 1;
306 anm = bfd_alloc (abfd, amt);
307 if (anm == NULL)
308 goto error_free_dyn;
309 memcpy (anm, fnm, amt);
310 n->name = anm;
311 n->by = abfd;
312 n->next = NULL;
313 for (pn = & hash_table->needed;
314 *pn != NULL;
315 pn = &(*pn)->next)
317 *pn = n;
319 if (dyn.d_tag == DT_RUNPATH)
321 struct bfd_link_needed_list *n, **pn;
322 char *fnm, *anm;
323 unsigned int tagv = dyn.d_un.d_val;
325 amt = sizeof (struct bfd_link_needed_list);
326 n = bfd_alloc (abfd, amt);
327 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
328 if (n == NULL || fnm == NULL)
329 goto error_free_dyn;
330 amt = strlen (fnm) + 1;
331 anm = bfd_alloc (abfd, amt);
332 if (anm == NULL)
333 goto error_free_dyn;
334 memcpy (anm, fnm, amt);
335 n->name = anm;
336 n->by = abfd;
337 n->next = NULL;
338 for (pn = & runpath;
339 *pn != NULL;
340 pn = &(*pn)->next)
342 *pn = n;
344 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
345 if (!runpath && dyn.d_tag == DT_RPATH)
347 struct bfd_link_needed_list *n, **pn;
348 char *fnm, *anm;
349 unsigned int tagv = dyn.d_un.d_val;
351 amt = sizeof (struct bfd_link_needed_list);
352 n = bfd_alloc (abfd, amt);
353 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
354 if (n == NULL || fnm == NULL)
355 goto error_free_dyn;
356 amt = strlen (fnm) + 1;
357 anm = bfd_alloc (abfd, amt);
358 if (anm == NULL)
360 error_free_dyn:
361 free (dynbuf);
362 goto error_return;
364 memcpy (anm, fnm, amt);
365 n->name = anm;
366 n->by = abfd;
367 n->next = NULL;
368 for (pn = & rpath;
369 *pn != NULL;
370 pn = &(*pn)->next)
372 *pn = n;
376 free (dynbuf);
379 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
380 frees all more recently bfd_alloc'd blocks as well. */
381 if (runpath)
382 rpath = runpath;
384 if (rpath)
386 struct bfd_link_needed_list **pn;
387 for (pn = & hash_table->runpath;
388 *pn != NULL;
389 pn = &(*pn)->next)
391 *pn = rpath;
394 /* We do not want to include any of the sections in a dynamic
395 object in the output file. We hack by simply clobbering the
396 list of sections in the BFD. This could be handled more
397 cleanly by, say, a new section flag; the existing
398 SEC_NEVER_LOAD flag is not the one we want, because that one
399 still implies that the section takes up space in the output
400 file. */
401 bfd_section_list_clear (abfd);
403 /* If this is the first dynamic object found in the link, create
404 the special sections required for dynamic linking. */
405 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
406 goto error_return;
408 if (add_needed)
410 /* Add a DT_NEEDED entry for this dynamic object. */
411 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
412 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
413 if (strindex == (bfd_size_type) -1)
414 goto error_return;
416 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
418 asection *sdyn;
419 Elf_External_Dyn *dyncon, *dynconend;
421 /* The hash table size did not change, which means that
422 the dynamic object name was already entered. If we
423 have already included this dynamic object in the
424 link, just ignore it. There is no reason to include
425 a particular dynamic object more than once. */
426 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
427 BFD_ASSERT (sdyn != NULL);
429 dyncon = (Elf_External_Dyn *) sdyn->contents;
430 dynconend = (Elf_External_Dyn *) (sdyn->contents +
431 sdyn->_raw_size);
432 for (; dyncon < dynconend; dyncon++)
434 Elf_Internal_Dyn dyn;
436 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
437 if (dyn.d_tag == DT_NEEDED
438 && dyn.d_un.d_val == strindex)
440 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
441 return TRUE;
446 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
447 goto error_return;
450 /* Save the SONAME, if there is one, because sometimes the
451 linker emulation code will need to know it. */
452 if (*name == '\0')
453 name = basename (bfd_get_filename (abfd));
454 elf_dt_name (abfd) = name;
457 /* If this is a dynamic object, we always link against the .dynsym
458 symbol table, not the .symtab symbol table. The dynamic linker
459 will only see the .dynsym symbol table, so there is no reason to
460 look at .symtab for a dynamic object. */
462 if (! dynamic || elf_dynsymtab (abfd) == 0)
463 hdr = &elf_tdata (abfd)->symtab_hdr;
464 else
465 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
467 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
469 /* The sh_info field of the symtab header tells us where the
470 external symbols start. We don't care about the local symbols at
471 this point. */
472 if (elf_bad_symtab (abfd))
474 extsymcount = symcount;
475 extsymoff = 0;
477 else
479 extsymcount = symcount - hdr->sh_info;
480 extsymoff = hdr->sh_info;
483 sym_hash = NULL;
484 if (extsymcount != 0)
486 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
487 NULL, NULL, NULL);
488 if (isymbuf == NULL)
489 goto error_return;
491 /* We store a pointer to the hash table entry for each external
492 symbol. */
493 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
494 sym_hash = bfd_alloc (abfd, amt);
495 if (sym_hash == NULL)
496 goto error_free_sym;
497 elf_sym_hashes (abfd) = sym_hash;
500 if (dynamic)
502 /* Read in any version definitions. */
503 if (! _bfd_elf_slurp_version_tables (abfd))
504 goto error_free_sym;
506 /* Read in the symbol versions, but don't bother to convert them
507 to internal format. */
508 if (elf_dynversym (abfd) != 0)
510 Elf_Internal_Shdr *versymhdr;
512 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
513 extversym = bfd_malloc (versymhdr->sh_size);
514 if (extversym == NULL)
515 goto error_free_sym;
516 amt = versymhdr->sh_size;
517 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
518 || bfd_bread (extversym, amt, abfd) != amt)
519 goto error_free_vers;
523 weaks = NULL;
525 ever = extversym != NULL ? extversym + extsymoff : NULL;
526 for (isym = isymbuf, isymend = isymbuf + extsymcount;
527 isym < isymend;
528 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
530 int bind;
531 bfd_vma value;
532 asection *sec;
533 flagword flags;
534 const char *name;
535 struct elf_link_hash_entry *h;
536 bfd_boolean definition;
537 bfd_boolean size_change_ok;
538 bfd_boolean type_change_ok;
539 bfd_boolean new_weakdef;
540 bfd_boolean override;
541 unsigned int old_alignment;
542 bfd *old_bfd;
544 override = FALSE;
546 flags = BSF_NO_FLAGS;
547 sec = NULL;
548 value = isym->st_value;
549 *sym_hash = NULL;
551 bind = ELF_ST_BIND (isym->st_info);
552 if (bind == STB_LOCAL)
554 /* This should be impossible, since ELF requires that all
555 global symbols follow all local symbols, and that sh_info
556 point to the first global symbol. Unfortunately, Irix 5
557 screws this up. */
558 continue;
560 else if (bind == STB_GLOBAL)
562 if (isym->st_shndx != SHN_UNDEF
563 && isym->st_shndx != SHN_COMMON)
564 flags = BSF_GLOBAL;
566 else if (bind == STB_WEAK)
567 flags = BSF_WEAK;
568 else
570 /* Leave it up to the processor backend. */
573 if (isym->st_shndx == SHN_UNDEF)
574 sec = bfd_und_section_ptr;
575 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
577 sec = section_from_elf_index (abfd, isym->st_shndx);
578 if (sec == NULL)
579 sec = bfd_abs_section_ptr;
580 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
581 value -= sec->vma;
583 else if (isym->st_shndx == SHN_ABS)
584 sec = bfd_abs_section_ptr;
585 else if (isym->st_shndx == SHN_COMMON)
587 sec = bfd_com_section_ptr;
588 /* What ELF calls the size we call the value. What ELF
589 calls the value we call the alignment. */
590 value = isym->st_size;
592 else
594 /* Leave it up to the processor backend. */
597 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
598 isym->st_name);
599 if (name == NULL)
600 goto error_free_vers;
602 if (isym->st_shndx == SHN_COMMON
603 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
605 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
607 if (tcomm == NULL)
609 tcomm = bfd_make_section (abfd, ".tcommon");
610 if (tcomm == NULL
611 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
612 | SEC_IS_COMMON
613 | SEC_LINKER_CREATED
614 | SEC_THREAD_LOCAL)))
615 goto error_free_vers;
617 sec = tcomm;
619 else if (add_symbol_hook)
621 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
622 &value))
623 goto error_free_vers;
625 /* The hook function sets the name to NULL if this symbol
626 should be skipped for some reason. */
627 if (name == NULL)
628 continue;
631 /* Sanity check that all possibilities were handled. */
632 if (sec == NULL)
634 bfd_set_error (bfd_error_bad_value);
635 goto error_free_vers;
638 if (bfd_is_und_section (sec)
639 || bfd_is_com_section (sec))
640 definition = FALSE;
641 else
642 definition = TRUE;
644 size_change_ok = FALSE;
645 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
646 old_alignment = 0;
647 old_bfd = NULL;
649 if (is_elf_hash_table (hash_table))
651 Elf_Internal_Versym iver;
652 unsigned int vernum = 0;
653 bfd_boolean skip;
655 if (ever != NULL)
657 _bfd_elf_swap_versym_in (abfd, ever, &iver);
658 vernum = iver.vs_vers & VERSYM_VERSION;
660 /* If this is a hidden symbol, or if it is not version
661 1, we append the version name to the symbol name.
662 However, we do not modify a non-hidden absolute
663 symbol, because it might be the version symbol
664 itself. FIXME: What if it isn't? */
665 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
666 || (vernum > 1 && ! bfd_is_abs_section (sec)))
668 const char *verstr;
669 size_t namelen, verlen, newlen;
670 char *newname, *p;
672 if (isym->st_shndx != SHN_UNDEF)
674 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
676 (*_bfd_error_handler)
677 (_("%s: %s: invalid version %u (max %d)"),
678 bfd_archive_filename (abfd), name, vernum,
679 elf_tdata (abfd)->dynverdef_hdr.sh_info);
680 bfd_set_error (bfd_error_bad_value);
681 goto error_free_vers;
683 else if (vernum > 1)
684 verstr =
685 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
686 else
687 verstr = "";
689 else
691 /* We cannot simply test for the number of
692 entries in the VERNEED section since the
693 numbers for the needed versions do not start
694 at 0. */
695 Elf_Internal_Verneed *t;
697 verstr = NULL;
698 for (t = elf_tdata (abfd)->verref;
699 t != NULL;
700 t = t->vn_nextref)
702 Elf_Internal_Vernaux *a;
704 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
706 if (a->vna_other == vernum)
708 verstr = a->vna_nodename;
709 break;
712 if (a != NULL)
713 break;
715 if (verstr == NULL)
717 (*_bfd_error_handler)
718 (_("%s: %s: invalid needed version %d"),
719 bfd_archive_filename (abfd), name, vernum);
720 bfd_set_error (bfd_error_bad_value);
721 goto error_free_vers;
725 namelen = strlen (name);
726 verlen = strlen (verstr);
727 newlen = namelen + verlen + 2;
728 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
729 && isym->st_shndx != SHN_UNDEF)
730 ++newlen;
732 newname = bfd_alloc (abfd, newlen);
733 if (newname == NULL)
734 goto error_free_vers;
735 memcpy (newname, name, namelen);
736 p = newname + namelen;
737 *p++ = ELF_VER_CHR;
738 /* If this is a defined non-hidden version symbol,
739 we add another @ to the name. This indicates the
740 default version of the symbol. */
741 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
742 && isym->st_shndx != SHN_UNDEF)
743 *p++ = ELF_VER_CHR;
744 memcpy (p, verstr, verlen + 1);
746 name = newname;
750 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
751 sym_hash, &skip, &override,
752 &type_change_ok, &size_change_ok,
753 dt_needed))
754 goto error_free_vers;
756 if (skip)
757 continue;
759 if (override)
760 definition = FALSE;
762 h = *sym_hash;
763 while (h->root.type == bfd_link_hash_indirect
764 || h->root.type == bfd_link_hash_warning)
765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
767 /* Remember the old alignment if this is a common symbol, so
768 that we don't reduce the alignment later on. We can't
769 check later, because _bfd_generic_link_add_one_symbol
770 will set a default for the alignment which we want to
771 override. We also remember the old bfd where the existing
772 definition comes from. */
773 switch (h->root.type)
775 default:
776 break;
778 case bfd_link_hash_defined:
779 case bfd_link_hash_defweak:
780 old_bfd = h->root.u.def.section->owner;
781 break;
783 case bfd_link_hash_common:
784 old_bfd = h->root.u.c.p->section->owner;
785 old_alignment = h->root.u.c.p->alignment_power;
786 break;
789 if (elf_tdata (abfd)->verdef != NULL
790 && ! override
791 && vernum > 1
792 && definition)
793 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
796 if (! (_bfd_generic_link_add_one_symbol
797 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
798 (struct bfd_link_hash_entry **) sym_hash)))
799 goto error_free_vers;
801 h = *sym_hash;
802 while (h->root.type == bfd_link_hash_indirect
803 || h->root.type == bfd_link_hash_warning)
804 h = (struct elf_link_hash_entry *) h->root.u.i.link;
805 *sym_hash = h;
807 new_weakdef = FALSE;
808 if (dynamic
809 && definition
810 && (flags & BSF_WEAK) != 0
811 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
812 && is_elf_hash_table (hash_table)
813 && h->weakdef == NULL)
815 /* Keep a list of all weak defined non function symbols from
816 a dynamic object, using the weakdef field. Later in this
817 function we will set the weakdef field to the correct
818 value. We only put non-function symbols from dynamic
819 objects on this list, because that happens to be the only
820 time we need to know the normal symbol corresponding to a
821 weak symbol, and the information is time consuming to
822 figure out. If the weakdef field is not already NULL,
823 then this symbol was already defined by some previous
824 dynamic object, and we will be using that previous
825 definition anyhow. */
827 h->weakdef = weaks;
828 weaks = h;
829 new_weakdef = TRUE;
832 /* Set the alignment of a common symbol. */
833 if (isym->st_shndx == SHN_COMMON
834 && h->root.type == bfd_link_hash_common)
836 unsigned int align;
838 align = bfd_log2 (isym->st_value);
839 if (align > old_alignment
840 /* Permit an alignment power of zero if an alignment of one
841 is specified and no other alignments have been specified. */
842 || (isym->st_value == 1 && old_alignment == 0))
843 h->root.u.c.p->alignment_power = align;
844 else
845 h->root.u.c.p->alignment_power = old_alignment;
848 if (is_elf_hash_table (hash_table))
850 int old_flags;
851 bfd_boolean dynsym;
852 int new_flag;
854 /* Check the alignment when a common symbol is involved. This
855 can change when a common symbol is overridden by a normal
856 definition or a common symbol is ignored due to the old
857 normal definition. We need to make sure the maximum
858 alignment is maintained. */
859 if ((old_alignment || isym->st_shndx == SHN_COMMON)
860 && h->root.type != bfd_link_hash_common)
862 unsigned int common_align;
863 unsigned int normal_align;
864 unsigned int symbol_align;
865 bfd *normal_bfd;
866 bfd *common_bfd;
868 symbol_align = ffs (h->root.u.def.value) - 1;
869 if (h->root.u.def.section->owner != NULL
870 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
872 normal_align = h->root.u.def.section->alignment_power;
873 if (normal_align > symbol_align)
874 normal_align = symbol_align;
876 else
877 normal_align = symbol_align;
879 if (old_alignment)
881 common_align = old_alignment;
882 common_bfd = old_bfd;
883 normal_bfd = abfd;
885 else
887 common_align = bfd_log2 (isym->st_value);
888 common_bfd = abfd;
889 normal_bfd = old_bfd;
892 if (normal_align < common_align)
893 (*_bfd_error_handler)
894 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
895 1 << normal_align,
896 name,
897 bfd_archive_filename (normal_bfd),
898 1 << common_align,
899 bfd_archive_filename (common_bfd));
902 /* Remember the symbol size and type. */
903 if (isym->st_size != 0
904 && (definition || h->size == 0))
906 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
907 (*_bfd_error_handler)
908 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
909 name, (unsigned long) h->size,
910 bfd_archive_filename (old_bfd),
911 (unsigned long) isym->st_size,
912 bfd_archive_filename (abfd));
914 h->size = isym->st_size;
917 /* If this is a common symbol, then we always want H->SIZE
918 to be the size of the common symbol. The code just above
919 won't fix the size if a common symbol becomes larger. We
920 don't warn about a size change here, because that is
921 covered by --warn-common. */
922 if (h->root.type == bfd_link_hash_common)
923 h->size = h->root.u.c.size;
925 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
926 && (definition || h->type == STT_NOTYPE))
928 if (h->type != STT_NOTYPE
929 && h->type != ELF_ST_TYPE (isym->st_info)
930 && ! type_change_ok)
931 (*_bfd_error_handler)
932 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
933 name, h->type, ELF_ST_TYPE (isym->st_info),
934 bfd_archive_filename (abfd));
936 h->type = ELF_ST_TYPE (isym->st_info);
939 /* If st_other has a processor-specific meaning, specific
940 code might be needed here. We never merge the visibility
941 attribute with the one from a dynamic object. */
942 if (bed->elf_backend_merge_symbol_attribute)
943 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
944 dynamic);
946 if (isym->st_other != 0 && !dynamic)
948 unsigned char hvis, symvis, other, nvis;
950 /* Take the balance of OTHER from the definition. */
951 other = (definition ? isym->st_other : h->other);
952 other &= ~ ELF_ST_VISIBILITY (-1);
954 /* Combine visibilities, using the most constraining one. */
955 hvis = ELF_ST_VISIBILITY (h->other);
956 symvis = ELF_ST_VISIBILITY (isym->st_other);
957 if (! hvis)
958 nvis = symvis;
959 else if (! symvis)
960 nvis = hvis;
961 else
962 nvis = hvis < symvis ? hvis : symvis;
964 h->other = other | nvis;
967 /* Set a flag in the hash table entry indicating the type of
968 reference or definition we just found. Keep a count of
969 the number of dynamic symbols we find. A dynamic symbol
970 is one which is referenced or defined by both a regular
971 object and a shared object. */
972 old_flags = h->elf_link_hash_flags;
973 dynsym = FALSE;
974 if (! dynamic)
976 if (! definition)
978 new_flag = ELF_LINK_HASH_REF_REGULAR;
979 if (bind != STB_WEAK)
980 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
982 else
983 new_flag = ELF_LINK_HASH_DEF_REGULAR;
984 if (! info->executable
985 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
986 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
987 dynsym = TRUE;
989 else
991 if (! definition)
992 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
993 else
994 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
995 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
996 | ELF_LINK_HASH_REF_REGULAR)) != 0
997 || (h->weakdef != NULL
998 && ! new_weakdef
999 && h->weakdef->dynindx != -1))
1000 dynsym = TRUE;
1003 h->elf_link_hash_flags |= new_flag;
1005 /* Check to see if we need to add an indirect symbol for
1006 the default name. */
1007 if (definition || h->root.type == bfd_link_hash_common)
1008 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1009 &sec, &value, &dynsym,
1010 override, dt_needed))
1011 goto error_free_vers;
1013 if (definition && !dynamic)
1015 char *p = strchr (name, ELF_VER_CHR);
1016 if (p != NULL && p[1] != ELF_VER_CHR)
1018 /* Queue non-default versions so that .symver x, x@FOO
1019 aliases can be checked. */
1020 if (! nondeflt_vers)
1022 amt = (isymend - isym + 1)
1023 * sizeof (struct elf_link_hash_entry *);
1024 nondeflt_vers = bfd_malloc (amt);
1026 nondeflt_vers [nondeflt_vers_cnt++] = h;
1030 if (dynsym && h->dynindx == -1)
1032 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1033 goto error_free_vers;
1034 if (h->weakdef != NULL
1035 && ! new_weakdef
1036 && h->weakdef->dynindx == -1)
1038 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1039 goto error_free_vers;
1042 else if (dynsym && h->dynindx != -1)
1043 /* If the symbol already has a dynamic index, but
1044 visibility says it should not be visible, turn it into
1045 a local symbol. */
1046 switch (ELF_ST_VISIBILITY (h->other))
1048 case STV_INTERNAL:
1049 case STV_HIDDEN:
1050 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1051 break;
1054 if (dt_needed && !add_needed && definition
1055 && (h->elf_link_hash_flags
1056 & ELF_LINK_HASH_REF_REGULAR) != 0)
1058 bfd_size_type oldsize;
1059 bfd_size_type strindex;
1061 /* The symbol from a DT_NEEDED object is referenced from
1062 the regular object to create a dynamic executable. We
1063 have to make sure there is a DT_NEEDED entry for it. */
1065 add_needed = TRUE;
1066 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1067 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1068 elf_dt_soname (abfd), FALSE);
1069 if (strindex == (bfd_size_type) -1)
1070 goto error_free_vers;
1072 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1074 asection *sdyn;
1075 Elf_External_Dyn *dyncon, *dynconend;
1077 sdyn = bfd_get_section_by_name (hash_table->dynobj,
1078 ".dynamic");
1079 BFD_ASSERT (sdyn != NULL);
1081 dyncon = (Elf_External_Dyn *) sdyn->contents;
1082 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1083 sdyn->_raw_size);
1084 for (; dyncon < dynconend; dyncon++)
1086 Elf_Internal_Dyn dyn;
1088 elf_swap_dyn_in (hash_table->dynobj,
1089 dyncon, &dyn);
1090 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1091 dyn.d_un.d_val != strindex);
1095 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1096 goto error_free_vers;
1101 /* Now that all the symbols from this input file are created, handle
1102 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1103 if (nondeflt_vers != NULL)
1105 bfd_size_type cnt, symidx;
1107 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
1109 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1110 char *shortname, *p;
1112 p = strchr (h->root.root.string, ELF_VER_CHR);
1113 if (p == NULL
1114 || (h->root.type != bfd_link_hash_defined
1115 && h->root.type != bfd_link_hash_defweak))
1116 continue;
1118 amt = p - h->root.root.string;
1119 shortname = bfd_malloc (amt + 1);
1120 memcpy (shortname, h->root.root.string, amt);
1121 shortname[amt] = '\0';
1123 hi = (struct elf_link_hash_entry *)
1124 bfd_link_hash_lookup (&hash_table->root, shortname,
1125 FALSE, FALSE, FALSE);
1126 if (hi != NULL
1127 && hi->root.type == h->root.type
1128 && hi->root.u.def.value == h->root.u.def.value
1129 && hi->root.u.def.section == h->root.u.def.section)
1131 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1132 hi->root.type = bfd_link_hash_indirect;
1133 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1134 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1135 sym_hash = elf_sym_hashes (abfd);
1136 if (sym_hash)
1137 for (symidx = 0; symidx < extsymcount; ++symidx)
1138 if (sym_hash[symidx] == hi)
1140 sym_hash[symidx] = h;
1141 break;
1144 free (shortname);
1146 free (nondeflt_vers);
1147 nondeflt_vers = NULL;
1150 if (extversym != NULL)
1152 free (extversym);
1153 extversym = NULL;
1156 if (isymbuf != NULL)
1157 free (isymbuf);
1158 isymbuf = NULL;
1160 /* Now set the weakdefs field correctly for all the weak defined
1161 symbols we found. The only way to do this is to search all the
1162 symbols. Since we only need the information for non functions in
1163 dynamic objects, that's the only time we actually put anything on
1164 the list WEAKS. We need this information so that if a regular
1165 object refers to a symbol defined weakly in a dynamic object, the
1166 real symbol in the dynamic object is also put in the dynamic
1167 symbols; we also must arrange for both symbols to point to the
1168 same memory location. We could handle the general case of symbol
1169 aliasing, but a general symbol alias can only be generated in
1170 assembler code, handling it correctly would be very time
1171 consuming, and other ELF linkers don't handle general aliasing
1172 either. */
1173 if (weaks != NULL)
1175 struct elf_link_hash_entry **hpp;
1176 struct elf_link_hash_entry **hppend;
1177 struct elf_link_hash_entry **sorted_sym_hash;
1178 struct elf_link_hash_entry *h;
1179 size_t sym_count;
1181 /* Since we have to search the whole symbol list for each weak
1182 defined symbol, search time for N weak defined symbols will be
1183 O(N^2). Binary search will cut it down to O(NlogN). */
1184 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1185 sorted_sym_hash = bfd_malloc (amt);
1186 if (sorted_sym_hash == NULL)
1187 goto error_return;
1188 sym_hash = sorted_sym_hash;
1189 hpp = elf_sym_hashes (abfd);
1190 hppend = hpp + extsymcount;
1191 sym_count = 0;
1192 for (; hpp < hppend; hpp++)
1194 h = *hpp;
1195 if (h != NULL
1196 && h->root.type == bfd_link_hash_defined
1197 && h->type != STT_FUNC)
1199 *sym_hash = h;
1200 sym_hash++;
1201 sym_count++;
1205 qsort (sorted_sym_hash, sym_count,
1206 sizeof (struct elf_link_hash_entry *),
1207 sort_symbol);
1209 while (weaks != NULL)
1211 struct elf_link_hash_entry *hlook;
1212 asection *slook;
1213 bfd_vma vlook;
1214 long ilook;
1215 size_t i, j, idx;
1217 hlook = weaks;
1218 weaks = hlook->weakdef;
1219 hlook->weakdef = NULL;
1221 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1222 || hlook->root.type == bfd_link_hash_defweak
1223 || hlook->root.type == bfd_link_hash_common
1224 || hlook->root.type == bfd_link_hash_indirect);
1225 slook = hlook->root.u.def.section;
1226 vlook = hlook->root.u.def.value;
1228 ilook = -1;
1229 i = 0;
1230 j = sym_count;
1231 while (i < j)
1233 bfd_signed_vma vdiff;
1234 idx = (i + j) / 2;
1235 h = sorted_sym_hash [idx];
1236 vdiff = vlook - h->root.u.def.value;
1237 if (vdiff < 0)
1238 j = idx;
1239 else if (vdiff > 0)
1240 i = idx + 1;
1241 else
1243 long sdiff = slook - h->root.u.def.section;
1244 if (sdiff < 0)
1245 j = idx;
1246 else if (sdiff > 0)
1247 i = idx + 1;
1248 else
1250 ilook = idx;
1251 break;
1256 /* We didn't find a value/section match. */
1257 if (ilook == -1)
1258 continue;
1260 for (i = ilook; i < sym_count; i++)
1262 h = sorted_sym_hash [i];
1264 /* Stop if value or section doesn't match. */
1265 if (h->root.u.def.value != vlook
1266 || h->root.u.def.section != slook)
1267 break;
1268 else if (h != hlook)
1270 hlook->weakdef = h;
1272 /* If the weak definition is in the list of dynamic
1273 symbols, make sure the real definition is put
1274 there as well. */
1275 if (hlook->dynindx != -1 && h->dynindx == -1)
1277 if (! _bfd_elf_link_record_dynamic_symbol (info,
1279 goto error_return;
1282 /* If the real definition is in the list of dynamic
1283 symbols, make sure the weak definition is put
1284 there as well. If we don't do this, then the
1285 dynamic loader might not merge the entries for the
1286 real definition and the weak definition. */
1287 if (h->dynindx != -1 && hlook->dynindx == -1)
1289 if (! _bfd_elf_link_record_dynamic_symbol (info,
1290 hlook))
1291 goto error_return;
1293 break;
1298 free (sorted_sym_hash);
1301 /* If this object is the same format as the output object, and it is
1302 not a shared library, then let the backend look through the
1303 relocs.
1305 This is required to build global offset table entries and to
1306 arrange for dynamic relocs. It is not required for the
1307 particular common case of linking non PIC code, even when linking
1308 against shared libraries, but unfortunately there is no way of
1309 knowing whether an object file has been compiled PIC or not.
1310 Looking through the relocs is not particularly time consuming.
1311 The problem is that we must either (1) keep the relocs in memory,
1312 which causes the linker to require additional runtime memory or
1313 (2) read the relocs twice from the input file, which wastes time.
1314 This would be a good case for using mmap.
1316 I have no idea how to handle linking PIC code into a file of a
1317 different format. It probably can't be done. */
1318 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1319 if (! dynamic
1320 && is_elf_hash_table (hash_table)
1321 && hash_table->root.creator == abfd->xvec
1322 && check_relocs != NULL)
1324 asection *o;
1326 for (o = abfd->sections; o != NULL; o = o->next)
1328 Elf_Internal_Rela *internal_relocs;
1329 bfd_boolean ok;
1331 if ((o->flags & SEC_RELOC) == 0
1332 || o->reloc_count == 0
1333 || ((info->strip == strip_all || info->strip == strip_debugger)
1334 && (o->flags & SEC_DEBUGGING) != 0)
1335 || bfd_is_abs_section (o->output_section))
1336 continue;
1338 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1339 info->keep_memory);
1340 if (internal_relocs == NULL)
1341 goto error_return;
1343 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1345 if (elf_section_data (o)->relocs != internal_relocs)
1346 free (internal_relocs);
1348 if (! ok)
1349 goto error_return;
1353 /* If this is a non-traditional link, try to optimize the handling
1354 of the .stab/.stabstr sections. */
1355 if (! dynamic
1356 && ! info->traditional_format
1357 && is_elf_hash_table (hash_table)
1358 && (info->strip != strip_all && info->strip != strip_debugger))
1360 asection *stabstr;
1362 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1363 if (stabstr != NULL)
1365 bfd_size_type string_offset = 0;
1366 asection *stab;
1368 for (stab = abfd->sections; stab; stab = stab->next)
1369 if (strncmp (".stab", stab->name, 5) == 0
1370 && (!stab->name[5] ||
1371 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1372 && (stab->flags & SEC_MERGE) == 0
1373 && !bfd_is_abs_section (stab->output_section))
1375 struct bfd_elf_section_data *secdata;
1377 secdata = elf_section_data (stab);
1378 if (! _bfd_link_section_stabs (abfd,
1379 & hash_table->stab_info,
1380 stab, stabstr,
1381 &secdata->sec_info,
1382 &string_offset))
1383 goto error_return;
1384 if (secdata->sec_info)
1385 stab->sec_info_type = ELF_INFO_TYPE_STABS;
1390 if (! info->relocatable
1391 && ! dynamic
1392 && is_elf_hash_table (hash_table))
1394 asection *s;
1396 for (s = abfd->sections; s != NULL; s = s->next)
1397 if ((s->flags & SEC_MERGE) != 0
1398 && !bfd_is_abs_section (s->output_section))
1400 struct bfd_elf_section_data *secdata;
1402 secdata = elf_section_data (s);
1403 if (! _bfd_merge_section (abfd,
1404 & hash_table->merge_info,
1405 s, &secdata->sec_info))
1406 goto error_return;
1407 else if (secdata->sec_info)
1408 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1412 if (is_elf_hash_table (hash_table))
1414 /* Add this bfd to the loaded list. */
1415 struct elf_link_loaded_list *n;
1417 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
1418 if (n == NULL)
1419 goto error_return;
1420 n->abfd = abfd;
1421 n->next = hash_table->loaded;
1422 hash_table->loaded = n;
1425 return TRUE;
1427 error_free_vers:
1428 if (nondeflt_vers != NULL)
1429 free (nondeflt_vers);
1430 if (extversym != NULL)
1431 free (extversym);
1432 error_free_sym:
1433 if (isymbuf != NULL)
1434 free (isymbuf);
1435 error_return:
1436 return FALSE;
1439 /* Add an entry to the .dynamic table. */
1441 bfd_boolean
1442 elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
1444 Elf_Internal_Dyn dyn;
1445 bfd *dynobj;
1446 asection *s;
1447 bfd_size_type newsize;
1448 bfd_byte *newcontents;
1450 if (! is_elf_hash_table (info->hash))
1451 return FALSE;
1453 dynobj = elf_hash_table (info)->dynobj;
1455 s = bfd_get_section_by_name (dynobj, ".dynamic");
1456 BFD_ASSERT (s != NULL);
1458 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1459 newcontents = bfd_realloc (s->contents, newsize);
1460 if (newcontents == NULL)
1461 return FALSE;
1463 dyn.d_tag = tag;
1464 dyn.d_un.d_val = val;
1465 elf_swap_dyn_out (dynobj, &dyn,
1466 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1468 s->_raw_size = newsize;
1469 s->contents = newcontents;
1471 return TRUE;
1474 /* Array used to determine the number of hash table buckets to use
1475 based on the number of symbols there are. If there are fewer than
1476 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1477 fewer than 37 we use 17 buckets, and so forth. We never use more
1478 than 32771 buckets. */
1480 static const size_t elf_buckets[] =
1482 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1483 16411, 32771, 0
1486 /* Compute bucket count for hashing table. We do not use a static set
1487 of possible tables sizes anymore. Instead we determine for all
1488 possible reasonable sizes of the table the outcome (i.e., the
1489 number of collisions etc) and choose the best solution. The
1490 weighting functions are not too simple to allow the table to grow
1491 without bounds. Instead one of the weighting factors is the size.
1492 Therefore the result is always a good payoff between few collisions
1493 (= short chain lengths) and table size. */
1494 static size_t
1495 compute_bucket_count (struct bfd_link_info *info)
1497 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1498 size_t best_size = 0;
1499 unsigned long int *hashcodes;
1500 unsigned long int *hashcodesp;
1501 unsigned long int i;
1502 bfd_size_type amt;
1504 /* Compute the hash values for all exported symbols. At the same
1505 time store the values in an array so that we could use them for
1506 optimizations. */
1507 amt = dynsymcount;
1508 amt *= sizeof (unsigned long int);
1509 hashcodes = bfd_malloc (amt);
1510 if (hashcodes == NULL)
1511 return 0;
1512 hashcodesp = hashcodes;
1514 /* Put all hash values in HASHCODES. */
1515 elf_link_hash_traverse (elf_hash_table (info),
1516 elf_collect_hash_codes, &hashcodesp);
1518 /* We have a problem here. The following code to optimize the table
1519 size requires an integer type with more the 32 bits. If
1520 BFD_HOST_U_64_BIT is set we know about such a type. */
1521 #ifdef BFD_HOST_U_64_BIT
1522 if (info->optimize)
1524 unsigned long int nsyms = hashcodesp - hashcodes;
1525 size_t minsize;
1526 size_t maxsize;
1527 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1528 unsigned long int *counts ;
1530 /* Possible optimization parameters: if we have NSYMS symbols we say
1531 that the hashing table must at least have NSYMS/4 and at most
1532 2*NSYMS buckets. */
1533 minsize = nsyms / 4;
1534 if (minsize == 0)
1535 minsize = 1;
1536 best_size = maxsize = nsyms * 2;
1538 /* Create array where we count the collisions in. We must use bfd_malloc
1539 since the size could be large. */
1540 amt = maxsize;
1541 amt *= sizeof (unsigned long int);
1542 counts = bfd_malloc (amt);
1543 if (counts == NULL)
1545 free (hashcodes);
1546 return 0;
1549 /* Compute the "optimal" size for the hash table. The criteria is a
1550 minimal chain length. The minor criteria is (of course) the size
1551 of the table. */
1552 for (i = minsize; i < maxsize; ++i)
1554 /* Walk through the array of hashcodes and count the collisions. */
1555 BFD_HOST_U_64_BIT max;
1556 unsigned long int j;
1557 unsigned long int fact;
1559 memset (counts, '\0', i * sizeof (unsigned long int));
1561 /* Determine how often each hash bucket is used. */
1562 for (j = 0; j < nsyms; ++j)
1563 ++counts[hashcodes[j] % i];
1565 /* For the weight function we need some information about the
1566 pagesize on the target. This is information need not be 100%
1567 accurate. Since this information is not available (so far) we
1568 define it here to a reasonable default value. If it is crucial
1569 to have a better value some day simply define this value. */
1570 # ifndef BFD_TARGET_PAGESIZE
1571 # define BFD_TARGET_PAGESIZE (4096)
1572 # endif
1574 /* We in any case need 2 + NSYMS entries for the size values and
1575 the chains. */
1576 max = (2 + nsyms) * (ARCH_SIZE / 8);
1578 # if 1
1579 /* Variant 1: optimize for short chains. We add the squares
1580 of all the chain lengths (which favors many small chain
1581 over a few long chains). */
1582 for (j = 0; j < i; ++j)
1583 max += counts[j] * counts[j];
1585 /* This adds penalties for the overall size of the table. */
1586 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1587 max *= fact * fact;
1588 # else
1589 /* Variant 2: Optimize a lot more for small table. Here we
1590 also add squares of the size but we also add penalties for
1591 empty slots (the +1 term). */
1592 for (j = 0; j < i; ++j)
1593 max += (1 + counts[j]) * (1 + counts[j]);
1595 /* The overall size of the table is considered, but not as
1596 strong as in variant 1, where it is squared. */
1597 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1598 max *= fact;
1599 # endif
1601 /* Compare with current best results. */
1602 if (max < best_chlen)
1604 best_chlen = max;
1605 best_size = i;
1609 free (counts);
1611 else
1612 #endif /* defined (BFD_HOST_U_64_BIT) */
1614 /* This is the fallback solution if no 64bit type is available or if we
1615 are not supposed to spend much time on optimizations. We select the
1616 bucket count using a fixed set of numbers. */
1617 for (i = 0; elf_buckets[i] != 0; i++)
1619 best_size = elf_buckets[i];
1620 if (dynsymcount < elf_buckets[i + 1])
1621 break;
1625 /* Free the arrays we needed. */
1626 free (hashcodes);
1628 return best_size;
1631 /* Set up the sizes and contents of the ELF dynamic sections. This is
1632 called by the ELF linker emulation before_allocation routine. We
1633 must set the sizes of the sections before the linker sets the
1634 addresses of the various sections. */
1636 bfd_boolean
1637 NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1638 const char *soname,
1639 const char *rpath,
1640 const char *filter_shlib,
1641 const char * const *auxiliary_filters,
1642 struct bfd_link_info *info,
1643 asection **sinterpptr,
1644 struct bfd_elf_version_tree *verdefs)
1646 bfd_size_type soname_indx;
1647 bfd *dynobj;
1648 const struct elf_backend_data *bed;
1649 struct elf_assign_sym_version_info asvinfo;
1651 *sinterpptr = NULL;
1653 soname_indx = (bfd_size_type) -1;
1655 if (!is_elf_hash_table (info->hash))
1656 return TRUE;
1658 if (info->execstack)
1659 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1660 else if (info->noexecstack)
1661 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1662 else
1664 bfd *inputobj;
1665 asection *notesec = NULL;
1666 int exec = 0;
1668 for (inputobj = info->input_bfds;
1669 inputobj;
1670 inputobj = inputobj->link_next)
1672 asection *s;
1674 if (inputobj->flags & DYNAMIC)
1675 continue;
1676 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1677 if (s)
1679 if (s->flags & SEC_CODE)
1680 exec = PF_X;
1681 notesec = s;
1683 else
1684 exec = PF_X;
1686 if (notesec)
1688 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1689 if (exec && info->relocatable
1690 && notesec->output_section != bfd_abs_section_ptr)
1691 notesec->output_section->flags |= SEC_CODE;
1695 /* Any syms created from now on start with -1 in
1696 got.refcount/offset and plt.refcount/offset. */
1697 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
1699 /* The backend may have to create some sections regardless of whether
1700 we're dynamic or not. */
1701 bed = get_elf_backend_data (output_bfd);
1702 if (bed->elf_backend_always_size_sections
1703 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1704 return FALSE;
1706 dynobj = elf_hash_table (info)->dynobj;
1708 /* If there were no dynamic objects in the link, there is nothing to
1709 do here. */
1710 if (dynobj == NULL)
1711 return TRUE;
1713 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1714 return FALSE;
1716 if (elf_hash_table (info)->dynamic_sections_created)
1718 struct elf_info_failed eif;
1719 struct elf_link_hash_entry *h;
1720 asection *dynstr;
1721 struct bfd_elf_version_tree *t;
1722 struct bfd_elf_version_expr *d;
1723 bfd_boolean all_defined;
1725 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1726 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
1728 if (soname != NULL)
1730 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1731 soname, TRUE);
1732 if (soname_indx == (bfd_size_type) -1
1733 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
1734 return FALSE;
1737 if (info->symbolic)
1739 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1740 return FALSE;
1741 info->flags |= DF_SYMBOLIC;
1744 if (rpath != NULL)
1746 bfd_size_type indx;
1748 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
1749 TRUE);
1750 if (info->new_dtags)
1751 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
1752 if (indx == (bfd_size_type) -1
1753 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
1754 || (info->new_dtags
1755 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
1756 return FALSE;
1759 if (filter_shlib != NULL)
1761 bfd_size_type indx;
1763 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1764 filter_shlib, TRUE);
1765 if (indx == (bfd_size_type) -1
1766 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
1767 return FALSE;
1770 if (auxiliary_filters != NULL)
1772 const char * const *p;
1774 for (p = auxiliary_filters; *p != NULL; p++)
1776 bfd_size_type indx;
1778 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1779 *p, TRUE);
1780 if (indx == (bfd_size_type) -1
1781 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
1782 return FALSE;
1786 eif.info = info;
1787 eif.verdefs = verdefs;
1788 eif.failed = FALSE;
1790 /* If we are supposed to export all symbols into the dynamic symbol
1791 table (this is not the normal case), then do so. */
1792 if (info->export_dynamic)
1794 elf_link_hash_traverse (elf_hash_table (info),
1795 _bfd_elf_export_symbol,
1796 &eif);
1797 if (eif.failed)
1798 return FALSE;
1801 /* Make all global versions with definition. */
1802 for (t = verdefs; t != NULL; t = t->next)
1803 for (d = t->globals.list; d != NULL; d = d->next)
1804 if (!d->symver && d->symbol)
1806 const char *verstr, *name;
1807 size_t namelen, verlen, newlen;
1808 char *newname, *p;
1809 struct elf_link_hash_entry *newh;
1811 name = d->symbol;
1812 namelen = strlen (name);
1813 verstr = t->name;
1814 verlen = strlen (verstr);
1815 newlen = namelen + verlen + 3;
1817 newname = bfd_malloc (newlen);
1818 if (newname == NULL)
1819 return FALSE;
1820 memcpy (newname, name, namelen);
1822 /* Check the hidden versioned definition. */
1823 p = newname + namelen;
1824 *p++ = ELF_VER_CHR;
1825 memcpy (p, verstr, verlen + 1);
1826 newh = elf_link_hash_lookup (elf_hash_table (info),
1827 newname, FALSE, FALSE,
1828 FALSE);
1829 if (newh == NULL
1830 || (newh->root.type != bfd_link_hash_defined
1831 && newh->root.type != bfd_link_hash_defweak))
1833 /* Check the default versioned definition. */
1834 *p++ = ELF_VER_CHR;
1835 memcpy (p, verstr, verlen + 1);
1836 newh = elf_link_hash_lookup (elf_hash_table (info),
1837 newname, FALSE, FALSE,
1838 FALSE);
1840 free (newname);
1842 /* Mark this version if there is a definition and it is
1843 not defined in a shared object. */
1844 if (newh != NULL
1845 && ((newh->elf_link_hash_flags
1846 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
1847 && (newh->root.type == bfd_link_hash_defined
1848 || newh->root.type == bfd_link_hash_defweak))
1849 d->symver = 1;
1852 /* Attach all the symbols to their version information. */
1853 asvinfo.output_bfd = output_bfd;
1854 asvinfo.info = info;
1855 asvinfo.verdefs = verdefs;
1856 asvinfo.failed = FALSE;
1858 elf_link_hash_traverse (elf_hash_table (info),
1859 _bfd_elf_link_assign_sym_version,
1860 &asvinfo);
1861 if (asvinfo.failed)
1862 return FALSE;
1864 if (!info->allow_undefined_version)
1866 /* Check if all global versions have a definition. */
1867 all_defined = TRUE;
1868 for (t = verdefs; t != NULL; t = t->next)
1869 for (d = t->globals.list; d != NULL; d = d->next)
1870 if (!d->symver && !d->script)
1872 (*_bfd_error_handler)
1873 (_("%s: undefined version: %s"),
1874 d->pattern, t->name);
1875 all_defined = FALSE;
1878 if (!all_defined)
1880 bfd_set_error (bfd_error_bad_value);
1881 return FALSE;
1885 /* Find all symbols which were defined in a dynamic object and make
1886 the backend pick a reasonable value for them. */
1887 elf_link_hash_traverse (elf_hash_table (info),
1888 _bfd_elf_adjust_dynamic_symbol,
1889 &eif);
1890 if (eif.failed)
1891 return FALSE;
1893 /* Add some entries to the .dynamic section. We fill in some of the
1894 values later, in elf_bfd_final_link, but we must add the entries
1895 now so that we know the final size of the .dynamic section. */
1897 /* If there are initialization and/or finalization functions to
1898 call then add the corresponding DT_INIT/DT_FINI entries. */
1899 h = (info->init_function
1900 ? elf_link_hash_lookup (elf_hash_table (info),
1901 info->init_function, FALSE,
1902 FALSE, FALSE)
1903 : NULL);
1904 if (h != NULL
1905 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1906 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1908 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
1909 return FALSE;
1911 h = (info->fini_function
1912 ? elf_link_hash_lookup (elf_hash_table (info),
1913 info->fini_function, FALSE,
1914 FALSE, FALSE)
1915 : NULL);
1916 if (h != NULL
1917 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1918 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1920 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
1921 return FALSE;
1924 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
1926 /* DT_PREINIT_ARRAY is not allowed in shared library. */
1927 if (! info->executable)
1929 bfd *sub;
1930 asection *o;
1932 for (sub = info->input_bfds; sub != NULL;
1933 sub = sub->link_next)
1934 for (o = sub->sections; o != NULL; o = o->next)
1935 if (elf_section_data (o)->this_hdr.sh_type
1936 == SHT_PREINIT_ARRAY)
1938 (*_bfd_error_handler)
1939 (_("%s: .preinit_array section is not allowed in DSO"),
1940 bfd_archive_filename (sub));
1941 break;
1944 bfd_set_error (bfd_error_nonrepresentable_section);
1945 return FALSE;
1948 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
1949 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
1950 return FALSE;
1952 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
1954 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
1955 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
1956 return FALSE;
1958 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
1960 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
1961 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
1962 return FALSE;
1965 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
1966 /* If .dynstr is excluded from the link, we don't want any of
1967 these tags. Strictly, we should be checking each section
1968 individually; This quick check covers for the case where
1969 someone does a /DISCARD/ : { *(*) }. */
1970 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
1972 bfd_size_type strsize;
1974 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
1975 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1976 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1977 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1978 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1979 || ! elf_add_dynamic_entry (info, DT_SYMENT,
1980 sizeof (Elf_External_Sym)))
1981 return FALSE;
1985 /* The backend must work out the sizes of all the other dynamic
1986 sections. */
1987 if (bed->elf_backend_size_dynamic_sections
1988 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1989 return FALSE;
1991 if (elf_hash_table (info)->dynamic_sections_created)
1993 bfd_size_type dynsymcount;
1994 asection *s;
1995 size_t bucketcount = 0;
1996 size_t hash_entry_size;
1997 unsigned int dtagcount;
1999 /* Set up the version definition section. */
2000 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2001 BFD_ASSERT (s != NULL);
2003 /* We may have created additional version definitions if we are
2004 just linking a regular application. */
2005 verdefs = asvinfo.verdefs;
2007 /* Skip anonymous version tag. */
2008 if (verdefs != NULL && verdefs->vernum == 0)
2009 verdefs = verdefs->next;
2011 if (verdefs == NULL)
2012 _bfd_strip_section_from_output (info, s);
2013 else
2015 unsigned int cdefs;
2016 bfd_size_type size;
2017 struct bfd_elf_version_tree *t;
2018 bfd_byte *p;
2019 Elf_Internal_Verdef def;
2020 Elf_Internal_Verdaux defaux;
2022 cdefs = 0;
2023 size = 0;
2025 /* Make space for the base version. */
2026 size += sizeof (Elf_External_Verdef);
2027 size += sizeof (Elf_External_Verdaux);
2028 ++cdefs;
2030 for (t = verdefs; t != NULL; t = t->next)
2032 struct bfd_elf_version_deps *n;
2034 size += sizeof (Elf_External_Verdef);
2035 size += sizeof (Elf_External_Verdaux);
2036 ++cdefs;
2038 for (n = t->deps; n != NULL; n = n->next)
2039 size += sizeof (Elf_External_Verdaux);
2042 s->_raw_size = size;
2043 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2044 if (s->contents == NULL && s->_raw_size != 0)
2045 return FALSE;
2047 /* Fill in the version definition section. */
2049 p = s->contents;
2051 def.vd_version = VER_DEF_CURRENT;
2052 def.vd_flags = VER_FLG_BASE;
2053 def.vd_ndx = 1;
2054 def.vd_cnt = 1;
2055 def.vd_aux = sizeof (Elf_External_Verdef);
2056 def.vd_next = (sizeof (Elf_External_Verdef)
2057 + sizeof (Elf_External_Verdaux));
2059 if (soname_indx != (bfd_size_type) -1)
2061 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2062 soname_indx);
2063 def.vd_hash = bfd_elf_hash (soname);
2064 defaux.vda_name = soname_indx;
2066 else
2068 const char *name;
2069 bfd_size_type indx;
2071 name = basename (output_bfd->filename);
2072 def.vd_hash = bfd_elf_hash (name);
2073 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2074 name, FALSE);
2075 if (indx == (bfd_size_type) -1)
2076 return FALSE;
2077 defaux.vda_name = indx;
2079 defaux.vda_next = 0;
2081 _bfd_elf_swap_verdef_out (output_bfd, &def,
2082 (Elf_External_Verdef *) p);
2083 p += sizeof (Elf_External_Verdef);
2084 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2085 (Elf_External_Verdaux *) p);
2086 p += sizeof (Elf_External_Verdaux);
2088 for (t = verdefs; t != NULL; t = t->next)
2090 unsigned int cdeps;
2091 struct bfd_elf_version_deps *n;
2092 struct elf_link_hash_entry *h;
2093 struct bfd_link_hash_entry *bh;
2095 cdeps = 0;
2096 for (n = t->deps; n != NULL; n = n->next)
2097 ++cdeps;
2099 /* Add a symbol representing this version. */
2100 bh = NULL;
2101 if (! (_bfd_generic_link_add_one_symbol
2102 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2103 0, NULL, FALSE,
2104 get_elf_backend_data (dynobj)->collect, &bh)))
2105 return FALSE;
2106 h = (struct elf_link_hash_entry *) bh;
2107 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2108 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2109 h->type = STT_OBJECT;
2110 h->verinfo.vertree = t;
2112 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2113 return FALSE;
2115 def.vd_version = VER_DEF_CURRENT;
2116 def.vd_flags = 0;
2117 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
2118 def.vd_flags |= VER_FLG_WEAK;
2119 def.vd_ndx = t->vernum + 1;
2120 def.vd_cnt = cdeps + 1;
2121 def.vd_hash = bfd_elf_hash (t->name);
2122 def.vd_aux = sizeof (Elf_External_Verdef);
2123 if (t->next != NULL)
2124 def.vd_next = (sizeof (Elf_External_Verdef)
2125 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2126 else
2127 def.vd_next = 0;
2129 _bfd_elf_swap_verdef_out (output_bfd, &def,
2130 (Elf_External_Verdef *) p);
2131 p += sizeof (Elf_External_Verdef);
2133 defaux.vda_name = h->dynstr_index;
2134 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2135 h->dynstr_index);
2136 if (t->deps == NULL)
2137 defaux.vda_next = 0;
2138 else
2139 defaux.vda_next = sizeof (Elf_External_Verdaux);
2140 t->name_indx = defaux.vda_name;
2142 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2143 (Elf_External_Verdaux *) p);
2144 p += sizeof (Elf_External_Verdaux);
2146 for (n = t->deps; n != NULL; n = n->next)
2148 if (n->version_needed == NULL)
2150 /* This can happen if there was an error in the
2151 version script. */
2152 defaux.vda_name = 0;
2154 else
2156 defaux.vda_name = n->version_needed->name_indx;
2157 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2158 defaux.vda_name);
2160 if (n->next == NULL)
2161 defaux.vda_next = 0;
2162 else
2163 defaux.vda_next = sizeof (Elf_External_Verdaux);
2165 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2166 (Elf_External_Verdaux *) p);
2167 p += sizeof (Elf_External_Verdaux);
2171 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2172 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2173 return FALSE;
2175 elf_tdata (output_bfd)->cverdefs = cdefs;
2178 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
2180 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
2181 return FALSE;
2183 else if (info->flags & DF_BIND_NOW)
2185 if (! elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
2186 return FALSE;
2189 if (info->flags_1)
2191 if (info->executable)
2192 info->flags_1 &= ~ (DF_1_INITFIRST
2193 | DF_1_NODELETE
2194 | DF_1_NOOPEN);
2195 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
2196 return FALSE;
2199 /* Work out the size of the version reference section. */
2201 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2202 BFD_ASSERT (s != NULL);
2204 struct elf_find_verdep_info sinfo;
2206 sinfo.output_bfd = output_bfd;
2207 sinfo.info = info;
2208 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2209 if (sinfo.vers == 0)
2210 sinfo.vers = 1;
2211 sinfo.failed = FALSE;
2213 elf_link_hash_traverse (elf_hash_table (info),
2214 _bfd_elf_link_find_version_dependencies,
2215 &sinfo);
2217 if (elf_tdata (output_bfd)->verref == NULL)
2218 _bfd_strip_section_from_output (info, s);
2219 else
2221 Elf_Internal_Verneed *t;
2222 unsigned int size;
2223 unsigned int crefs;
2224 bfd_byte *p;
2226 /* Build the version definition section. */
2227 size = 0;
2228 crefs = 0;
2229 for (t = elf_tdata (output_bfd)->verref;
2230 t != NULL;
2231 t = t->vn_nextref)
2233 Elf_Internal_Vernaux *a;
2235 size += sizeof (Elf_External_Verneed);
2236 ++crefs;
2237 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2238 size += sizeof (Elf_External_Vernaux);
2241 s->_raw_size = size;
2242 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2243 if (s->contents == NULL)
2244 return FALSE;
2246 p = s->contents;
2247 for (t = elf_tdata (output_bfd)->verref;
2248 t != NULL;
2249 t = t->vn_nextref)
2251 unsigned int caux;
2252 Elf_Internal_Vernaux *a;
2253 bfd_size_type indx;
2255 caux = 0;
2256 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2257 ++caux;
2259 t->vn_version = VER_NEED_CURRENT;
2260 t->vn_cnt = caux;
2261 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2262 elf_dt_name (t->vn_bfd) != NULL
2263 ? elf_dt_name (t->vn_bfd)
2264 : basename (t->vn_bfd->filename),
2265 FALSE);
2266 if (indx == (bfd_size_type) -1)
2267 return FALSE;
2268 t->vn_file = indx;
2269 t->vn_aux = sizeof (Elf_External_Verneed);
2270 if (t->vn_nextref == NULL)
2271 t->vn_next = 0;
2272 else
2273 t->vn_next = (sizeof (Elf_External_Verneed)
2274 + caux * sizeof (Elf_External_Vernaux));
2276 _bfd_elf_swap_verneed_out (output_bfd, t,
2277 (Elf_External_Verneed *) p);
2278 p += sizeof (Elf_External_Verneed);
2280 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2282 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2283 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2284 a->vna_nodename, FALSE);
2285 if (indx == (bfd_size_type) -1)
2286 return FALSE;
2287 a->vna_name = indx;
2288 if (a->vna_nextptr == NULL)
2289 a->vna_next = 0;
2290 else
2291 a->vna_next = sizeof (Elf_External_Vernaux);
2293 _bfd_elf_swap_vernaux_out (output_bfd, a,
2294 (Elf_External_Vernaux *) p);
2295 p += sizeof (Elf_External_Vernaux);
2299 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2300 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2301 return FALSE;
2303 elf_tdata (output_bfd)->cverrefs = crefs;
2307 /* Assign dynsym indicies. In a shared library we generate a
2308 section symbol for each output section, which come first.
2309 Next come all of the back-end allocated local dynamic syms,
2310 followed by the rest of the global symbols. */
2312 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2314 /* Work out the size of the symbol version section. */
2315 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2316 BFD_ASSERT (s != NULL);
2317 if (dynsymcount == 0
2318 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2320 _bfd_strip_section_from_output (info, s);
2321 /* The DYNSYMCOUNT might have changed if we were going to
2322 output a dynamic symbol table entry for S. */
2323 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2325 else
2327 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2328 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2329 if (s->contents == NULL)
2330 return FALSE;
2332 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2333 return FALSE;
2336 /* Set the size of the .dynsym and .hash sections. We counted
2337 the number of dynamic symbols in elf_link_add_object_symbols.
2338 We will build the contents of .dynsym and .hash when we build
2339 the final symbol table, because until then we do not know the
2340 correct value to give the symbols. We built the .dynstr
2341 section as we went along in elf_link_add_object_symbols. */
2342 s = bfd_get_section_by_name (dynobj, ".dynsym");
2343 BFD_ASSERT (s != NULL);
2344 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2345 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2346 if (s->contents == NULL && s->_raw_size != 0)
2347 return FALSE;
2349 if (dynsymcount != 0)
2351 Elf_Internal_Sym isym;
2353 /* The first entry in .dynsym is a dummy symbol. */
2354 isym.st_value = 0;
2355 isym.st_size = 0;
2356 isym.st_name = 0;
2357 isym.st_info = 0;
2358 isym.st_other = 0;
2359 isym.st_shndx = 0;
2360 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
2363 /* Compute the size of the hashing table. As a side effect this
2364 computes the hash values for all the names we export. */
2365 bucketcount = compute_bucket_count (info);
2367 s = bfd_get_section_by_name (dynobj, ".hash");
2368 BFD_ASSERT (s != NULL);
2369 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2370 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
2371 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2372 if (s->contents == NULL)
2373 return FALSE;
2375 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2376 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
2377 s->contents + hash_entry_size);
2379 elf_hash_table (info)->bucketcount = bucketcount;
2381 s = bfd_get_section_by_name (dynobj, ".dynstr");
2382 BFD_ASSERT (s != NULL);
2384 elf_finalize_dynstr (output_bfd, info);
2386 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2388 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
2389 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2390 return FALSE;
2393 return TRUE;
2396 /* This function is used to adjust offsets into .dynstr for
2397 dynamic symbols. This is called via elf_link_hash_traverse. */
2399 static bfd_boolean
2400 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2402 struct elf_strtab_hash *dynstr = data;
2404 if (h->root.type == bfd_link_hash_warning)
2405 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2407 if (h->dynindx != -1)
2408 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2409 return TRUE;
2412 /* Assign string offsets in .dynstr, update all structures referencing
2413 them. */
2415 static bfd_boolean
2416 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2418 struct elf_link_local_dynamic_entry *entry;
2419 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2420 bfd *dynobj = elf_hash_table (info)->dynobj;
2421 asection *sdyn;
2422 bfd_size_type size;
2423 Elf_External_Dyn *dyncon, *dynconend;
2425 _bfd_elf_strtab_finalize (dynstr);
2426 size = _bfd_elf_strtab_size (dynstr);
2428 /* Update all .dynamic entries referencing .dynstr strings. */
2429 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2430 BFD_ASSERT (sdyn != NULL);
2432 dyncon = (Elf_External_Dyn *) sdyn->contents;
2433 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2434 sdyn->_raw_size);
2435 for (; dyncon < dynconend; dyncon++)
2437 Elf_Internal_Dyn dyn;
2439 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2440 switch (dyn.d_tag)
2442 case DT_STRSZ:
2443 dyn.d_un.d_val = size;
2444 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2445 break;
2446 case DT_NEEDED:
2447 case DT_SONAME:
2448 case DT_RPATH:
2449 case DT_RUNPATH:
2450 case DT_FILTER:
2451 case DT_AUXILIARY:
2452 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2453 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2454 break;
2455 default:
2456 break;
2460 /* Now update local dynamic symbols. */
2461 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2462 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2463 entry->isym.st_name);
2465 /* And the rest of dynamic symbols. */
2466 elf_link_hash_traverse (elf_hash_table (info),
2467 elf_adjust_dynstr_offsets, dynstr);
2469 /* Adjust version definitions. */
2470 if (elf_tdata (output_bfd)->cverdefs)
2472 asection *s;
2473 bfd_byte *p;
2474 bfd_size_type i;
2475 Elf_Internal_Verdef def;
2476 Elf_Internal_Verdaux defaux;
2478 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2479 p = (bfd_byte *) s->contents;
2482 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2483 &def);
2484 p += sizeof (Elf_External_Verdef);
2485 for (i = 0; i < def.vd_cnt; ++i)
2487 _bfd_elf_swap_verdaux_in (output_bfd,
2488 (Elf_External_Verdaux *) p, &defaux);
2489 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2490 defaux.vda_name);
2491 _bfd_elf_swap_verdaux_out (output_bfd,
2492 &defaux, (Elf_External_Verdaux *) p);
2493 p += sizeof (Elf_External_Verdaux);
2496 while (def.vd_next);
2499 /* Adjust version references. */
2500 if (elf_tdata (output_bfd)->verref)
2502 asection *s;
2503 bfd_byte *p;
2504 bfd_size_type i;
2505 Elf_Internal_Verneed need;
2506 Elf_Internal_Vernaux needaux;
2508 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2509 p = (bfd_byte *) s->contents;
2512 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2513 &need);
2514 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2515 _bfd_elf_swap_verneed_out (output_bfd, &need,
2516 (Elf_External_Verneed *) p);
2517 p += sizeof (Elf_External_Verneed);
2518 for (i = 0; i < need.vn_cnt; ++i)
2520 _bfd_elf_swap_vernaux_in (output_bfd,
2521 (Elf_External_Vernaux *) p, &needaux);
2522 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2523 needaux.vna_name);
2524 _bfd_elf_swap_vernaux_out (output_bfd,
2525 &needaux,
2526 (Elf_External_Vernaux *) p);
2527 p += sizeof (Elf_External_Vernaux);
2530 while (need.vn_next);
2533 return TRUE;
2536 /* Final phase of ELF linker. */
2538 /* A structure we use to avoid passing large numbers of arguments. */
2540 struct elf_final_link_info
2542 /* General link information. */
2543 struct bfd_link_info *info;
2544 /* Output BFD. */
2545 bfd *output_bfd;
2546 /* Symbol string table. */
2547 struct bfd_strtab_hash *symstrtab;
2548 /* .dynsym section. */
2549 asection *dynsym_sec;
2550 /* .hash section. */
2551 asection *hash_sec;
2552 /* symbol version section (.gnu.version). */
2553 asection *symver_sec;
2554 /* Buffer large enough to hold contents of any section. */
2555 bfd_byte *contents;
2556 /* Buffer large enough to hold external relocs of any section. */
2557 void *external_relocs;
2558 /* Buffer large enough to hold internal relocs of any section. */
2559 Elf_Internal_Rela *internal_relocs;
2560 /* Buffer large enough to hold external local symbols of any input
2561 BFD. */
2562 Elf_External_Sym *external_syms;
2563 /* And a buffer for symbol section indices. */
2564 Elf_External_Sym_Shndx *locsym_shndx;
2565 /* Buffer large enough to hold internal local symbols of any input
2566 BFD. */
2567 Elf_Internal_Sym *internal_syms;
2568 /* Array large enough to hold a symbol index for each local symbol
2569 of any input BFD. */
2570 long *indices;
2571 /* Array large enough to hold a section pointer for each local
2572 symbol of any input BFD. */
2573 asection **sections;
2574 /* Buffer to hold swapped out symbols. */
2575 Elf_External_Sym *symbuf;
2576 /* And one for symbol section indices. */
2577 Elf_External_Sym_Shndx *symshndxbuf;
2578 /* Number of swapped out symbols in buffer. */
2579 size_t symbuf_count;
2580 /* Number of symbols which fit in symbuf. */
2581 size_t symbuf_size;
2582 /* And same for symshndxbuf. */
2583 size_t shndxbuf_size;
2586 static bfd_boolean elf_link_output_sym
2587 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2588 struct elf_link_hash_entry *);
2589 static bfd_boolean elf_link_flush_output_syms
2590 (struct elf_final_link_info *);
2591 static bfd_boolean elf_link_output_extsym
2592 (struct elf_link_hash_entry *, void *);
2593 static bfd_boolean elf_link_input_bfd
2594 (struct elf_final_link_info *, bfd *);
2595 static bfd_boolean elf_reloc_link_order
2596 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
2598 /* This struct is used to pass information to elf_link_output_extsym. */
2600 struct elf_outext_info
2602 bfd_boolean failed;
2603 bfd_boolean localsyms;
2604 struct elf_final_link_info *finfo;
2607 /* When performing a relocatable link, the input relocations are
2608 preserved. But, if they reference global symbols, the indices
2609 referenced must be updated. Update all the relocations in
2610 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2612 static void
2613 elf_link_adjust_relocs (bfd *abfd,
2614 Elf_Internal_Shdr *rel_hdr,
2615 unsigned int count,
2616 struct elf_link_hash_entry **rel_hash)
2618 unsigned int i;
2619 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2620 bfd_byte *erela;
2621 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2622 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2624 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2626 swap_in = bed->s->swap_reloc_in;
2627 swap_out = bed->s->swap_reloc_out;
2629 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2631 swap_in = bed->s->swap_reloca_in;
2632 swap_out = bed->s->swap_reloca_out;
2634 else
2635 abort ();
2637 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2638 abort ();
2640 erela = rel_hdr->contents;
2641 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2643 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2644 unsigned int j;
2646 if (*rel_hash == NULL)
2647 continue;
2649 BFD_ASSERT ((*rel_hash)->indx >= 0);
2651 (*swap_in) (abfd, erela, irela);
2652 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2653 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2654 ELF_R_TYPE (irela[j].r_info));
2655 (*swap_out) (abfd, irela, erela);
2659 struct elf_link_sort_rela
2661 bfd_vma offset;
2662 enum elf_reloc_type_class type;
2663 /* We use this as an array of size int_rels_per_ext_rel. */
2664 Elf_Internal_Rela rela[1];
2667 static int
2668 elf_link_sort_cmp1 (const void *A, const void *B)
2670 const struct elf_link_sort_rela *a = A;
2671 const struct elf_link_sort_rela *b = B;
2672 int relativea, relativeb;
2674 relativea = a->type == reloc_class_relative;
2675 relativeb = b->type == reloc_class_relative;
2677 if (relativea < relativeb)
2678 return 1;
2679 if (relativea > relativeb)
2680 return -1;
2681 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2682 return -1;
2683 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2684 return 1;
2685 if (a->rela->r_offset < b->rela->r_offset)
2686 return -1;
2687 if (a->rela->r_offset > b->rela->r_offset)
2688 return 1;
2689 return 0;
2692 static int
2693 elf_link_sort_cmp2 (const void *A, const void *B)
2695 const struct elf_link_sort_rela *a = A;
2696 const struct elf_link_sort_rela *b = B;
2697 int copya, copyb;
2699 if (a->offset < b->offset)
2700 return -1;
2701 if (a->offset > b->offset)
2702 return 1;
2703 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2704 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
2705 if (copya < copyb)
2706 return -1;
2707 if (copya > copyb)
2708 return 1;
2709 if (a->rela->r_offset < b->rela->r_offset)
2710 return -1;
2711 if (a->rela->r_offset > b->rela->r_offset)
2712 return 1;
2713 return 0;
2716 static size_t
2717 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
2719 asection *reldyn;
2720 bfd_size_type count, size;
2721 size_t i, ret, sort_elt, ext_size;
2722 bfd_byte *sort, *s_non_relative, *p;
2723 struct elf_link_sort_rela *sq;
2724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2725 int i2e = bed->s->int_rels_per_ext_rel;
2726 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2727 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2728 struct bfd_link_order *lo;
2730 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2731 if (reldyn == NULL || reldyn->_raw_size == 0)
2733 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2734 if (reldyn == NULL || reldyn->_raw_size == 0)
2735 return 0;
2736 ext_size = sizeof (Elf_External_Rel);
2737 swap_in = bed->s->swap_reloc_in;
2738 swap_out = bed->s->swap_reloc_out;
2740 else
2742 ext_size = sizeof (Elf_External_Rela);
2743 swap_in = bed->s->swap_reloca_in;
2744 swap_out = bed->s->swap_reloca_out;
2746 count = reldyn->_raw_size / ext_size;
2748 size = 0;
2749 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2750 if (lo->type == bfd_indirect_link_order)
2752 asection *o = lo->u.indirect.section;
2753 size += o->_raw_size;
2756 if (size != reldyn->_raw_size)
2757 return 0;
2759 sort_elt = (sizeof (struct elf_link_sort_rela)
2760 + (i2e - 1) * sizeof (Elf_Internal_Rela));
2761 sort = bfd_zmalloc (sort_elt * count);
2762 if (sort == NULL)
2764 (*info->callbacks->warning)
2765 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
2766 return 0;
2769 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2770 if (lo->type == bfd_indirect_link_order)
2772 bfd_byte *erel, *erelend;
2773 asection *o = lo->u.indirect.section;
2775 erel = o->contents;
2776 erelend = o->contents + o->_raw_size;
2777 p = sort + o->output_offset / ext_size * sort_elt;
2778 while (erel < erelend)
2780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2781 (*swap_in) (abfd, erel, s->rela);
2782 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
2783 p += sort_elt;
2784 erel += ext_size;
2788 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
2790 for (i = 0, p = sort; i < count; i++, p += sort_elt)
2792 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2793 if (s->type != reloc_class_relative)
2794 break;
2796 ret = i;
2797 s_non_relative = p;
2799 sq = (struct elf_link_sort_rela *) s_non_relative;
2800 for (; i < count; i++, p += sort_elt)
2802 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
2803 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
2804 sq = sp;
2805 sp->offset = sq->rela->r_offset;
2808 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
2810 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2811 if (lo->type == bfd_indirect_link_order)
2813 bfd_byte *erel, *erelend;
2814 asection *o = lo->u.indirect.section;
2816 erel = o->contents;
2817 erelend = o->contents + o->_raw_size;
2818 p = sort + o->output_offset / ext_size * sort_elt;
2819 while (erel < erelend)
2821 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2822 (*swap_out) (abfd, s->rela, erel);
2823 p += sort_elt;
2824 erel += ext_size;
2828 free (sort);
2829 *psec = reldyn;
2830 return ret;
2833 /* Do the final step of an ELF link. */
2835 bfd_boolean
2836 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
2838 bfd_boolean dynamic;
2839 bfd_boolean emit_relocs;
2840 bfd *dynobj;
2841 struct elf_final_link_info finfo;
2842 register asection *o;
2843 register struct bfd_link_order *p;
2844 register bfd *sub;
2845 bfd_size_type max_contents_size;
2846 bfd_size_type max_external_reloc_size;
2847 bfd_size_type max_internal_reloc_count;
2848 bfd_size_type max_sym_count;
2849 bfd_size_type max_sym_shndx_count;
2850 file_ptr off;
2851 Elf_Internal_Sym elfsym;
2852 unsigned int i;
2853 Elf_Internal_Shdr *symtab_hdr;
2854 Elf_Internal_Shdr *symtab_shndx_hdr;
2855 Elf_Internal_Shdr *symstrtab_hdr;
2856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2857 struct elf_outext_info eoinfo;
2858 bfd_boolean merged;
2859 size_t relativecount = 0;
2860 asection *reldyn = 0;
2861 bfd_size_type amt;
2863 if (! is_elf_hash_table (info->hash))
2864 return FALSE;
2866 if (info->shared)
2867 abfd->flags |= DYNAMIC;
2869 dynamic = elf_hash_table (info)->dynamic_sections_created;
2870 dynobj = elf_hash_table (info)->dynobj;
2872 emit_relocs = (info->relocatable
2873 || info->emitrelocations
2874 || bed->elf_backend_emit_relocs);
2876 finfo.info = info;
2877 finfo.output_bfd = abfd;
2878 finfo.symstrtab = elf_stringtab_init ();
2879 if (finfo.symstrtab == NULL)
2880 return FALSE;
2882 if (! dynamic)
2884 finfo.dynsym_sec = NULL;
2885 finfo.hash_sec = NULL;
2886 finfo.symver_sec = NULL;
2888 else
2890 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
2891 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
2892 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
2893 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
2894 /* Note that it is OK if symver_sec is NULL. */
2897 finfo.contents = NULL;
2898 finfo.external_relocs = NULL;
2899 finfo.internal_relocs = NULL;
2900 finfo.external_syms = NULL;
2901 finfo.locsym_shndx = NULL;
2902 finfo.internal_syms = NULL;
2903 finfo.indices = NULL;
2904 finfo.sections = NULL;
2905 finfo.symbuf = NULL;
2906 finfo.symshndxbuf = NULL;
2907 finfo.symbuf_count = 0;
2908 finfo.shndxbuf_size = 0;
2910 /* Count up the number of relocations we will output for each output
2911 section, so that we know the sizes of the reloc sections. We
2912 also figure out some maximum sizes. */
2913 max_contents_size = 0;
2914 max_external_reloc_size = 0;
2915 max_internal_reloc_count = 0;
2916 max_sym_count = 0;
2917 max_sym_shndx_count = 0;
2918 merged = FALSE;
2919 for (o = abfd->sections; o != NULL; o = o->next)
2921 struct bfd_elf_section_data *esdo = elf_section_data (o);
2922 o->reloc_count = 0;
2924 for (p = o->link_order_head; p != NULL; p = p->next)
2926 unsigned int reloc_count = 0;
2927 struct bfd_elf_section_data *esdi = NULL;
2928 unsigned int *rel_count1;
2930 if (p->type == bfd_section_reloc_link_order
2931 || p->type == bfd_symbol_reloc_link_order)
2932 reloc_count = 1;
2933 else if (p->type == bfd_indirect_link_order)
2935 asection *sec;
2937 sec = p->u.indirect.section;
2938 esdi = elf_section_data (sec);
2940 /* Mark all sections which are to be included in the
2941 link. This will normally be every section. We need
2942 to do this so that we can identify any sections which
2943 the linker has decided to not include. */
2944 sec->linker_mark = TRUE;
2946 if (sec->flags & SEC_MERGE)
2947 merged = TRUE;
2949 if (info->relocatable || info->emitrelocations)
2950 reloc_count = sec->reloc_count;
2951 else if (bed->elf_backend_count_relocs)
2953 Elf_Internal_Rela * relocs;
2955 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2956 info->keep_memory);
2958 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
2960 if (elf_section_data (o)->relocs != relocs)
2961 free (relocs);
2964 if (sec->_raw_size > max_contents_size)
2965 max_contents_size = sec->_raw_size;
2966 if (sec->_cooked_size > max_contents_size)
2967 max_contents_size = sec->_cooked_size;
2969 /* We are interested in just local symbols, not all
2970 symbols. */
2971 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
2972 && (sec->owner->flags & DYNAMIC) == 0)
2974 size_t sym_count;
2976 if (elf_bad_symtab (sec->owner))
2977 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
2978 / sizeof (Elf_External_Sym));
2979 else
2980 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
2982 if (sym_count > max_sym_count)
2983 max_sym_count = sym_count;
2985 if (sym_count > max_sym_shndx_count
2986 && elf_symtab_shndx (sec->owner) != 0)
2987 max_sym_shndx_count = sym_count;
2989 if ((sec->flags & SEC_RELOC) != 0)
2991 size_t ext_size;
2993 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
2994 if (ext_size > max_external_reloc_size)
2995 max_external_reloc_size = ext_size;
2996 if (sec->reloc_count > max_internal_reloc_count)
2997 max_internal_reloc_count = sec->reloc_count;
3002 if (reloc_count == 0)
3003 continue;
3005 o->reloc_count += reloc_count;
3007 /* MIPS may have a mix of REL and RELA relocs on sections.
3008 To support this curious ABI we keep reloc counts in
3009 elf_section_data too. We must be careful to add the
3010 relocations from the input section to the right output
3011 count. FIXME: Get rid of one count. We have
3012 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3013 rel_count1 = &esdo->rel_count;
3014 if (esdi != NULL)
3016 bfd_boolean same_size;
3017 bfd_size_type entsize1;
3019 entsize1 = esdi->rel_hdr.sh_entsize;
3020 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3021 || entsize1 == sizeof (Elf_External_Rela));
3022 same_size = (!o->use_rela_p
3023 == (entsize1 == sizeof (Elf_External_Rel)));
3025 if (!same_size)
3026 rel_count1 = &esdo->rel_count2;
3028 if (esdi->rel_hdr2 != NULL)
3030 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3031 unsigned int alt_count;
3032 unsigned int *rel_count2;
3034 BFD_ASSERT (entsize2 != entsize1
3035 && (entsize2 == sizeof (Elf_External_Rel)
3036 || entsize2 == sizeof (Elf_External_Rela)));
3038 rel_count2 = &esdo->rel_count2;
3039 if (!same_size)
3040 rel_count2 = &esdo->rel_count;
3042 /* The following is probably too simplistic if the
3043 backend counts output relocs unusually. */
3044 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3045 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3046 *rel_count2 += alt_count;
3047 reloc_count -= alt_count;
3050 *rel_count1 += reloc_count;
3053 if (o->reloc_count > 0)
3054 o->flags |= SEC_RELOC;
3055 else
3057 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3058 set it (this is probably a bug) and if it is set
3059 assign_section_numbers will create a reloc section. */
3060 o->flags &=~ SEC_RELOC;
3063 /* If the SEC_ALLOC flag is not set, force the section VMA to
3064 zero. This is done in elf_fake_sections as well, but forcing
3065 the VMA to 0 here will ensure that relocs against these
3066 sections are handled correctly. */
3067 if ((o->flags & SEC_ALLOC) == 0
3068 && ! o->user_set_vma)
3069 o->vma = 0;
3072 if (! info->relocatable && merged)
3073 elf_link_hash_traverse (elf_hash_table (info),
3074 _bfd_elf_link_sec_merge_syms, abfd);
3076 /* Figure out the file positions for everything but the symbol table
3077 and the relocs. We set symcount to force assign_section_numbers
3078 to create a symbol table. */
3079 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3080 BFD_ASSERT (! abfd->output_has_begun);
3081 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3082 goto error_return;
3084 /* That created the reloc sections. Set their sizes, and assign
3085 them file positions, and allocate some buffers. */
3086 for (o = abfd->sections; o != NULL; o = o->next)
3088 if ((o->flags & SEC_RELOC) != 0)
3090 if (!(_bfd_elf_link_size_reloc_section
3091 (abfd, &elf_section_data (o)->rel_hdr, o)))
3092 goto error_return;
3094 if (elf_section_data (o)->rel_hdr2
3095 && !(_bfd_elf_link_size_reloc_section
3096 (abfd, elf_section_data (o)->rel_hdr2, o)))
3097 goto error_return;
3100 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3101 to count upwards while actually outputting the relocations. */
3102 elf_section_data (o)->rel_count = 0;
3103 elf_section_data (o)->rel_count2 = 0;
3106 _bfd_elf_assign_file_positions_for_relocs (abfd);
3108 /* We have now assigned file positions for all the sections except
3109 .symtab and .strtab. We start the .symtab section at the current
3110 file position, and write directly to it. We build the .strtab
3111 section in memory. */
3112 bfd_get_symcount (abfd) = 0;
3113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3114 /* sh_name is set in prep_headers. */
3115 symtab_hdr->sh_type = SHT_SYMTAB;
3116 /* sh_flags, sh_addr and sh_size all start off zero. */
3117 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3118 /* sh_link is set in assign_section_numbers. */
3119 /* sh_info is set below. */
3120 /* sh_offset is set just below. */
3121 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3123 off = elf_tdata (abfd)->next_file_pos;
3124 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3126 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3127 incorrect. We do not yet know the size of the .symtab section.
3128 We correct next_file_pos below, after we do know the size. */
3130 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3131 continuously seeking to the right position in the file. */
3132 if (! info->keep_memory || max_sym_count < 20)
3133 finfo.symbuf_size = 20;
3134 else
3135 finfo.symbuf_size = max_sym_count;
3136 amt = finfo.symbuf_size;
3137 amt *= sizeof (Elf_External_Sym);
3138 finfo.symbuf = bfd_malloc (amt);
3139 if (finfo.symbuf == NULL)
3140 goto error_return;
3141 if (elf_numsections (abfd) > SHN_LORESERVE)
3143 /* Wild guess at number of output symbols. realloc'd as needed. */
3144 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3145 finfo.shndxbuf_size = amt;
3146 amt *= sizeof (Elf_External_Sym_Shndx);
3147 finfo.symshndxbuf = bfd_zmalloc (amt);
3148 if (finfo.symshndxbuf == NULL)
3149 goto error_return;
3152 /* Start writing out the symbol table. The first symbol is always a
3153 dummy symbol. */
3154 if (info->strip != strip_all
3155 || emit_relocs)
3157 elfsym.st_value = 0;
3158 elfsym.st_size = 0;
3159 elfsym.st_info = 0;
3160 elfsym.st_other = 0;
3161 elfsym.st_shndx = SHN_UNDEF;
3162 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3163 NULL))
3164 goto error_return;
3167 #if 0
3168 /* Some standard ELF linkers do this, but we don't because it causes
3169 bootstrap comparison failures. */
3170 /* Output a file symbol for the output file as the second symbol.
3171 We output this even if we are discarding local symbols, although
3172 I'm not sure if this is correct. */
3173 elfsym.st_value = 0;
3174 elfsym.st_size = 0;
3175 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3176 elfsym.st_other = 0;
3177 elfsym.st_shndx = SHN_ABS;
3178 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3179 &elfsym, bfd_abs_section_ptr, NULL))
3180 goto error_return;
3181 #endif
3183 /* Output a symbol for each section. We output these even if we are
3184 discarding local symbols, since they are used for relocs. These
3185 symbols have no names. We store the index of each one in the
3186 index field of the section, so that we can find it again when
3187 outputting relocs. */
3188 if (info->strip != strip_all
3189 || emit_relocs)
3191 elfsym.st_size = 0;
3192 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3193 elfsym.st_other = 0;
3194 for (i = 1; i < elf_numsections (abfd); i++)
3196 o = section_from_elf_index (abfd, i);
3197 if (o != NULL)
3198 o->target_index = bfd_get_symcount (abfd);
3199 elfsym.st_shndx = i;
3200 if (info->relocatable || o == NULL)
3201 elfsym.st_value = 0;
3202 else
3203 elfsym.st_value = o->vma;
3204 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
3205 goto error_return;
3206 if (i == SHN_LORESERVE - 1)
3207 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3211 /* Allocate some memory to hold information read in from the input
3212 files. */
3213 if (max_contents_size != 0)
3215 finfo.contents = bfd_malloc (max_contents_size);
3216 if (finfo.contents == NULL)
3217 goto error_return;
3220 if (max_external_reloc_size != 0)
3222 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
3223 if (finfo.external_relocs == NULL)
3224 goto error_return;
3227 if (max_internal_reloc_count != 0)
3229 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3230 amt *= sizeof (Elf_Internal_Rela);
3231 finfo.internal_relocs = bfd_malloc (amt);
3232 if (finfo.internal_relocs == NULL)
3233 goto error_return;
3236 if (max_sym_count != 0)
3238 amt = max_sym_count * sizeof (Elf_External_Sym);
3239 finfo.external_syms = bfd_malloc (amt);
3240 if (finfo.external_syms == NULL)
3241 goto error_return;
3243 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3244 finfo.internal_syms = bfd_malloc (amt);
3245 if (finfo.internal_syms == NULL)
3246 goto error_return;
3248 amt = max_sym_count * sizeof (long);
3249 finfo.indices = bfd_malloc (amt);
3250 if (finfo.indices == NULL)
3251 goto error_return;
3253 amt = max_sym_count * sizeof (asection *);
3254 finfo.sections = bfd_malloc (amt);
3255 if (finfo.sections == NULL)
3256 goto error_return;
3259 if (max_sym_shndx_count != 0)
3261 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3262 finfo.locsym_shndx = bfd_malloc (amt);
3263 if (finfo.locsym_shndx == NULL)
3264 goto error_return;
3267 if (elf_hash_table (info)->tls_sec)
3269 bfd_vma base, end = 0;
3270 asection *sec;
3272 for (sec = elf_hash_table (info)->tls_sec;
3273 sec && (sec->flags & SEC_THREAD_LOCAL);
3274 sec = sec->next)
3276 bfd_vma size = sec->_raw_size;
3278 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3280 struct bfd_link_order *o;
3282 for (o = sec->link_order_head; o != NULL; o = o->next)
3283 if (size < o->offset + o->size)
3284 size = o->offset + o->size;
3286 end = sec->vma + size;
3288 base = elf_hash_table (info)->tls_sec->vma;
3289 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3290 elf_hash_table (info)->tls_size = end - base;
3293 /* Since ELF permits relocations to be against local symbols, we
3294 must have the local symbols available when we do the relocations.
3295 Since we would rather only read the local symbols once, and we
3296 would rather not keep them in memory, we handle all the
3297 relocations for a single input file at the same time.
3299 Unfortunately, there is no way to know the total number of local
3300 symbols until we have seen all of them, and the local symbol
3301 indices precede the global symbol indices. This means that when
3302 we are generating relocatable output, and we see a reloc against
3303 a global symbol, we can not know the symbol index until we have
3304 finished examining all the local symbols to see which ones we are
3305 going to output. To deal with this, we keep the relocations in
3306 memory, and don't output them until the end of the link. This is
3307 an unfortunate waste of memory, but I don't see a good way around
3308 it. Fortunately, it only happens when performing a relocatable
3309 link, which is not the common case. FIXME: If keep_memory is set
3310 we could write the relocs out and then read them again; I don't
3311 know how bad the memory loss will be. */
3313 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3314 sub->output_has_begun = FALSE;
3315 for (o = abfd->sections; o != NULL; o = o->next)
3317 for (p = o->link_order_head; p != NULL; p = p->next)
3319 if (p->type == bfd_indirect_link_order
3320 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3321 == bfd_target_elf_flavour)
3322 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3324 if (! sub->output_has_begun)
3326 if (! elf_link_input_bfd (&finfo, sub))
3327 goto error_return;
3328 sub->output_has_begun = TRUE;
3331 else if (p->type == bfd_section_reloc_link_order
3332 || p->type == bfd_symbol_reloc_link_order)
3334 if (! elf_reloc_link_order (abfd, info, o, p))
3335 goto error_return;
3337 else
3339 if (! _bfd_default_link_order (abfd, info, o, p))
3340 goto error_return;
3345 /* Output any global symbols that got converted to local in a
3346 version script or due to symbol visibility. We do this in a
3347 separate step since ELF requires all local symbols to appear
3348 prior to any global symbols. FIXME: We should only do this if
3349 some global symbols were, in fact, converted to become local.
3350 FIXME: Will this work correctly with the Irix 5 linker? */
3351 eoinfo.failed = FALSE;
3352 eoinfo.finfo = &finfo;
3353 eoinfo.localsyms = TRUE;
3354 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3355 &eoinfo);
3356 if (eoinfo.failed)
3357 return FALSE;
3359 /* That wrote out all the local symbols. Finish up the symbol table
3360 with the global symbols. Even if we want to strip everything we
3361 can, we still need to deal with those global symbols that got
3362 converted to local in a version script. */
3364 /* The sh_info field records the index of the first non local symbol. */
3365 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3367 if (dynamic
3368 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3370 Elf_Internal_Sym sym;
3371 Elf_External_Sym *dynsym =
3372 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3373 long last_local = 0;
3375 /* Write out the section symbols for the output sections. */
3376 if (info->shared)
3378 asection *s;
3380 sym.st_size = 0;
3381 sym.st_name = 0;
3382 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3383 sym.st_other = 0;
3385 for (s = abfd->sections; s != NULL; s = s->next)
3387 int indx;
3388 Elf_External_Sym *dest;
3390 indx = elf_section_data (s)->this_idx;
3391 BFD_ASSERT (indx > 0);
3392 sym.st_shndx = indx;
3393 sym.st_value = s->vma;
3394 dest = dynsym + elf_section_data (s)->dynindx;
3395 elf_swap_symbol_out (abfd, &sym, dest, 0);
3398 last_local = bfd_count_sections (abfd);
3401 /* Write out the local dynsyms. */
3402 if (elf_hash_table (info)->dynlocal)
3404 struct elf_link_local_dynamic_entry *e;
3405 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3407 asection *s;
3408 Elf_External_Sym *dest;
3410 sym.st_size = e->isym.st_size;
3411 sym.st_other = e->isym.st_other;
3413 /* Copy the internal symbol as is.
3414 Note that we saved a word of storage and overwrote
3415 the original st_name with the dynstr_index. */
3416 sym = e->isym;
3418 if (e->isym.st_shndx != SHN_UNDEF
3419 && (e->isym.st_shndx < SHN_LORESERVE
3420 || e->isym.st_shndx > SHN_HIRESERVE))
3422 s = bfd_section_from_elf_index (e->input_bfd,
3423 e->isym.st_shndx);
3425 sym.st_shndx =
3426 elf_section_data (s->output_section)->this_idx;
3427 sym.st_value = (s->output_section->vma
3428 + s->output_offset
3429 + e->isym.st_value);
3432 if (last_local < e->dynindx)
3433 last_local = e->dynindx;
3435 dest = dynsym + e->dynindx;
3436 elf_swap_symbol_out (abfd, &sym, dest, 0);
3440 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3441 last_local + 1;
3444 /* We get the global symbols from the hash table. */
3445 eoinfo.failed = FALSE;
3446 eoinfo.localsyms = FALSE;
3447 eoinfo.finfo = &finfo;
3448 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3449 &eoinfo);
3450 if (eoinfo.failed)
3451 return FALSE;
3453 /* If backend needs to output some symbols not present in the hash
3454 table, do it now. */
3455 if (bed->elf_backend_output_arch_syms)
3457 typedef bfd_boolean (*out_sym_func)
3458 (void *, const char *, Elf_Internal_Sym *, asection *,
3459 struct elf_link_hash_entry *);
3461 if (! ((*bed->elf_backend_output_arch_syms)
3462 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
3463 return FALSE;
3466 /* Flush all symbols to the file. */
3467 if (! elf_link_flush_output_syms (&finfo))
3468 return FALSE;
3470 /* Now we know the size of the symtab section. */
3471 off += symtab_hdr->sh_size;
3473 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3474 if (symtab_shndx_hdr->sh_name != 0)
3476 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3477 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3478 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3479 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3480 symtab_shndx_hdr->sh_size = amt;
3482 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3483 off, TRUE);
3485 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3486 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
3487 return FALSE;
3491 /* Finish up and write out the symbol string table (.strtab)
3492 section. */
3493 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3494 /* sh_name was set in prep_headers. */
3495 symstrtab_hdr->sh_type = SHT_STRTAB;
3496 symstrtab_hdr->sh_flags = 0;
3497 symstrtab_hdr->sh_addr = 0;
3498 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3499 symstrtab_hdr->sh_entsize = 0;
3500 symstrtab_hdr->sh_link = 0;
3501 symstrtab_hdr->sh_info = 0;
3502 /* sh_offset is set just below. */
3503 symstrtab_hdr->sh_addralign = 1;
3505 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3506 elf_tdata (abfd)->next_file_pos = off;
3508 if (bfd_get_symcount (abfd) > 0)
3510 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3511 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3512 return FALSE;
3515 /* Adjust the relocs to have the correct symbol indices. */
3516 for (o = abfd->sections; o != NULL; o = o->next)
3518 if ((o->flags & SEC_RELOC) == 0)
3519 continue;
3521 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3522 elf_section_data (o)->rel_count,
3523 elf_section_data (o)->rel_hashes);
3524 if (elf_section_data (o)->rel_hdr2 != NULL)
3525 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3526 elf_section_data (o)->rel_count2,
3527 (elf_section_data (o)->rel_hashes
3528 + elf_section_data (o)->rel_count));
3530 /* Set the reloc_count field to 0 to prevent write_relocs from
3531 trying to swap the relocs out itself. */
3532 o->reloc_count = 0;
3535 if (dynamic && info->combreloc && dynobj != NULL)
3536 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3538 /* If we are linking against a dynamic object, or generating a
3539 shared library, finish up the dynamic linking information. */
3540 if (dynamic)
3542 Elf_External_Dyn *dyncon, *dynconend;
3544 /* Fix up .dynamic entries. */
3545 o = bfd_get_section_by_name (dynobj, ".dynamic");
3546 BFD_ASSERT (o != NULL);
3548 dyncon = (Elf_External_Dyn *) o->contents;
3549 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3550 for (; dyncon < dynconend; dyncon++)
3552 Elf_Internal_Dyn dyn;
3553 const char *name;
3554 unsigned int type;
3556 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3558 switch (dyn.d_tag)
3560 default:
3561 break;
3562 case DT_NULL:
3563 if (relativecount > 0 && dyncon + 1 < dynconend)
3565 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3567 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3568 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3569 default: break;
3571 if (dyn.d_tag != DT_NULL)
3573 dyn.d_un.d_val = relativecount;
3574 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3575 relativecount = 0;
3578 break;
3579 case DT_INIT:
3580 name = info->init_function;
3581 goto get_sym;
3582 case DT_FINI:
3583 name = info->fini_function;
3584 get_sym:
3586 struct elf_link_hash_entry *h;
3588 h = elf_link_hash_lookup (elf_hash_table (info), name,
3589 FALSE, FALSE, TRUE);
3590 if (h != NULL
3591 && (h->root.type == bfd_link_hash_defined
3592 || h->root.type == bfd_link_hash_defweak))
3594 dyn.d_un.d_val = h->root.u.def.value;
3595 o = h->root.u.def.section;
3596 if (o->output_section != NULL)
3597 dyn.d_un.d_val += (o->output_section->vma
3598 + o->output_offset);
3599 else
3601 /* The symbol is imported from another shared
3602 library and does not apply to this one. */
3603 dyn.d_un.d_val = 0;
3606 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3609 break;
3611 case DT_PREINIT_ARRAYSZ:
3612 name = ".preinit_array";
3613 goto get_size;
3614 case DT_INIT_ARRAYSZ:
3615 name = ".init_array";
3616 goto get_size;
3617 case DT_FINI_ARRAYSZ:
3618 name = ".fini_array";
3619 get_size:
3620 o = bfd_get_section_by_name (abfd, name);
3621 if (o == NULL)
3623 (*_bfd_error_handler)
3624 (_("%s: could not find output section %s"),
3625 bfd_get_filename (abfd), name);
3626 goto error_return;
3628 if (o->_raw_size == 0)
3629 (*_bfd_error_handler)
3630 (_("warning: %s section has zero size"), name);
3631 dyn.d_un.d_val = o->_raw_size;
3632 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3633 break;
3635 case DT_PREINIT_ARRAY:
3636 name = ".preinit_array";
3637 goto get_vma;
3638 case DT_INIT_ARRAY:
3639 name = ".init_array";
3640 goto get_vma;
3641 case DT_FINI_ARRAY:
3642 name = ".fini_array";
3643 goto get_vma;
3645 case DT_HASH:
3646 name = ".hash";
3647 goto get_vma;
3648 case DT_STRTAB:
3649 name = ".dynstr";
3650 goto get_vma;
3651 case DT_SYMTAB:
3652 name = ".dynsym";
3653 goto get_vma;
3654 case DT_VERDEF:
3655 name = ".gnu.version_d";
3656 goto get_vma;
3657 case DT_VERNEED:
3658 name = ".gnu.version_r";
3659 goto get_vma;
3660 case DT_VERSYM:
3661 name = ".gnu.version";
3662 get_vma:
3663 o = bfd_get_section_by_name (abfd, name);
3664 if (o == NULL)
3666 (*_bfd_error_handler)
3667 (_("%s: could not find output section %s"),
3668 bfd_get_filename (abfd), name);
3669 goto error_return;
3671 dyn.d_un.d_ptr = o->vma;
3672 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3673 break;
3675 case DT_REL:
3676 case DT_RELA:
3677 case DT_RELSZ:
3678 case DT_RELASZ:
3679 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3680 type = SHT_REL;
3681 else
3682 type = SHT_RELA;
3683 dyn.d_un.d_val = 0;
3684 for (i = 1; i < elf_numsections (abfd); i++)
3686 Elf_Internal_Shdr *hdr;
3688 hdr = elf_elfsections (abfd)[i];
3689 if (hdr->sh_type == type
3690 && (hdr->sh_flags & SHF_ALLOC) != 0)
3692 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3693 dyn.d_un.d_val += hdr->sh_size;
3694 else
3696 if (dyn.d_un.d_val == 0
3697 || hdr->sh_addr < dyn.d_un.d_val)
3698 dyn.d_un.d_val = hdr->sh_addr;
3702 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3703 break;
3708 /* If we have created any dynamic sections, then output them. */
3709 if (dynobj != NULL)
3711 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3712 goto error_return;
3714 for (o = dynobj->sections; o != NULL; o = o->next)
3716 if ((o->flags & SEC_HAS_CONTENTS) == 0
3717 || o->_raw_size == 0
3718 || o->output_section == bfd_abs_section_ptr)
3719 continue;
3720 if ((o->flags & SEC_LINKER_CREATED) == 0)
3722 /* At this point, we are only interested in sections
3723 created by _bfd_elf_link_create_dynamic_sections. */
3724 continue;
3726 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3727 != SHT_STRTAB)
3728 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3730 if (! bfd_set_section_contents (abfd, o->output_section,
3731 o->contents,
3732 (file_ptr) o->output_offset,
3733 o->_raw_size))
3734 goto error_return;
3736 else
3738 /* The contents of the .dynstr section are actually in a
3739 stringtab. */
3740 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3741 if (bfd_seek (abfd, off, SEEK_SET) != 0
3742 || ! _bfd_elf_strtab_emit (abfd,
3743 elf_hash_table (info)->dynstr))
3744 goto error_return;
3749 if (info->relocatable)
3751 bfd_boolean failed = FALSE;
3753 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3754 if (failed)
3755 goto error_return;
3758 /* If we have optimized stabs strings, output them. */
3759 if (elf_hash_table (info)->stab_info != NULL)
3761 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3762 goto error_return;
3765 if (info->eh_frame_hdr)
3767 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
3768 goto error_return;
3771 if (finfo.symstrtab != NULL)
3772 _bfd_stringtab_free (finfo.symstrtab);
3773 if (finfo.contents != NULL)
3774 free (finfo.contents);
3775 if (finfo.external_relocs != NULL)
3776 free (finfo.external_relocs);
3777 if (finfo.internal_relocs != NULL)
3778 free (finfo.internal_relocs);
3779 if (finfo.external_syms != NULL)
3780 free (finfo.external_syms);
3781 if (finfo.locsym_shndx != NULL)
3782 free (finfo.locsym_shndx);
3783 if (finfo.internal_syms != NULL)
3784 free (finfo.internal_syms);
3785 if (finfo.indices != NULL)
3786 free (finfo.indices);
3787 if (finfo.sections != NULL)
3788 free (finfo.sections);
3789 if (finfo.symbuf != NULL)
3790 free (finfo.symbuf);
3791 if (finfo.symshndxbuf != NULL)
3792 free (finfo.symshndxbuf);
3793 for (o = abfd->sections; o != NULL; o = o->next)
3795 if ((o->flags & SEC_RELOC) != 0
3796 && elf_section_data (o)->rel_hashes != NULL)
3797 free (elf_section_data (o)->rel_hashes);
3800 elf_tdata (abfd)->linker = TRUE;
3802 return TRUE;
3804 error_return:
3805 if (finfo.symstrtab != NULL)
3806 _bfd_stringtab_free (finfo.symstrtab);
3807 if (finfo.contents != NULL)
3808 free (finfo.contents);
3809 if (finfo.external_relocs != NULL)
3810 free (finfo.external_relocs);
3811 if (finfo.internal_relocs != NULL)
3812 free (finfo.internal_relocs);
3813 if (finfo.external_syms != NULL)
3814 free (finfo.external_syms);
3815 if (finfo.locsym_shndx != NULL)
3816 free (finfo.locsym_shndx);
3817 if (finfo.internal_syms != NULL)
3818 free (finfo.internal_syms);
3819 if (finfo.indices != NULL)
3820 free (finfo.indices);
3821 if (finfo.sections != NULL)
3822 free (finfo.sections);
3823 if (finfo.symbuf != NULL)
3824 free (finfo.symbuf);
3825 if (finfo.symshndxbuf != NULL)
3826 free (finfo.symshndxbuf);
3827 for (o = abfd->sections; o != NULL; o = o->next)
3829 if ((o->flags & SEC_RELOC) != 0
3830 && elf_section_data (o)->rel_hashes != NULL)
3831 free (elf_section_data (o)->rel_hashes);
3834 return FALSE;
3837 /* Add a symbol to the output symbol table. */
3839 static bfd_boolean
3840 elf_link_output_sym (struct elf_final_link_info *finfo,
3841 const char *name,
3842 Elf_Internal_Sym *elfsym,
3843 asection *input_sec,
3844 struct elf_link_hash_entry *h)
3846 Elf_External_Sym *dest;
3847 Elf_External_Sym_Shndx *destshndx;
3848 bfd_boolean (*output_symbol_hook)
3849 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
3850 struct elf_link_hash_entry *);
3852 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3853 elf_backend_link_output_symbol_hook;
3854 if (output_symbol_hook != NULL)
3856 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
3857 return FALSE;
3860 if (name == NULL || *name == '\0')
3861 elfsym->st_name = 0;
3862 else if (input_sec->flags & SEC_EXCLUDE)
3863 elfsym->st_name = 0;
3864 else
3866 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
3867 name, TRUE, FALSE);
3868 if (elfsym->st_name == (unsigned long) -1)
3869 return FALSE;
3872 if (finfo->symbuf_count >= finfo->symbuf_size)
3874 if (! elf_link_flush_output_syms (finfo))
3875 return FALSE;
3878 dest = finfo->symbuf + finfo->symbuf_count;
3879 destshndx = finfo->symshndxbuf;
3880 if (destshndx != NULL)
3882 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
3884 bfd_size_type amt;
3886 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
3887 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
3888 if (destshndx == NULL)
3889 return FALSE;
3890 memset ((char *) destshndx + amt, 0, amt);
3891 finfo->shndxbuf_size *= 2;
3893 destshndx += bfd_get_symcount (finfo->output_bfd);
3896 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
3897 finfo->symbuf_count += 1;
3898 bfd_get_symcount (finfo->output_bfd) += 1;
3900 return TRUE;
3903 /* Flush the output symbols to the file. */
3905 static bfd_boolean
3906 elf_link_flush_output_syms (struct elf_final_link_info *finfo)
3908 if (finfo->symbuf_count > 0)
3910 Elf_Internal_Shdr *hdr;
3911 file_ptr pos;
3912 bfd_size_type amt;
3914 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
3915 pos = hdr->sh_offset + hdr->sh_size;
3916 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
3917 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
3918 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
3919 return FALSE;
3921 hdr->sh_size += amt;
3922 finfo->symbuf_count = 0;
3925 return TRUE;
3928 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
3929 allowing an unsatisfied unversioned symbol in the DSO to match a
3930 versioned symbol that would normally require an explicit version.
3931 We also handle the case that a DSO references a hidden symbol
3932 which may be satisfied by a versioned symbol in another DSO. */
3934 static bfd_boolean
3935 elf_link_check_versioned_symbol (struct bfd_link_info *info,
3936 struct elf_link_hash_entry *h)
3938 bfd *abfd;
3939 struct elf_link_loaded_list *loaded;
3941 if (!is_elf_hash_table (info->hash))
3942 return FALSE;
3944 switch (h->root.type)
3946 default:
3947 abfd = NULL;
3948 break;
3950 case bfd_link_hash_undefined:
3951 case bfd_link_hash_undefweak:
3952 abfd = h->root.u.undef.abfd;
3953 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
3954 return FALSE;
3955 break;
3957 case bfd_link_hash_defined:
3958 case bfd_link_hash_defweak:
3959 abfd = h->root.u.def.section->owner;
3960 break;
3962 case bfd_link_hash_common:
3963 abfd = h->root.u.c.p->section->owner;
3964 break;
3966 BFD_ASSERT (abfd != NULL);
3968 for (loaded = elf_hash_table (info)->loaded;
3969 loaded != NULL;
3970 loaded = loaded->next)
3972 bfd *input;
3973 Elf_Internal_Shdr *hdr;
3974 bfd_size_type symcount;
3975 bfd_size_type extsymcount;
3976 bfd_size_type extsymoff;
3977 Elf_Internal_Shdr *versymhdr;
3978 Elf_Internal_Sym *isym;
3979 Elf_Internal_Sym *isymend;
3980 Elf_Internal_Sym *isymbuf;
3981 Elf_External_Versym *ever;
3982 Elf_External_Versym *extversym;
3984 input = loaded->abfd;
3986 /* We check each DSO for a possible hidden versioned definition. */
3987 if (input == abfd
3988 || (input->flags & DYNAMIC) == 0
3989 || elf_dynversym (input) == 0)
3990 continue;
3992 hdr = &elf_tdata (input)->dynsymtab_hdr;
3994 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
3995 if (elf_bad_symtab (input))
3997 extsymcount = symcount;
3998 extsymoff = 0;
4000 else
4002 extsymcount = symcount - hdr->sh_info;
4003 extsymoff = hdr->sh_info;
4006 if (extsymcount == 0)
4007 continue;
4009 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4010 NULL, NULL, NULL);
4011 if (isymbuf == NULL)
4012 return FALSE;
4014 /* Read in any version definitions. */
4015 versymhdr = &elf_tdata (input)->dynversym_hdr;
4016 extversym = bfd_malloc (versymhdr->sh_size);
4017 if (extversym == NULL)
4018 goto error_ret;
4020 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4021 || (bfd_bread (extversym, versymhdr->sh_size, input)
4022 != versymhdr->sh_size))
4024 free (extversym);
4025 error_ret:
4026 free (isymbuf);
4027 return FALSE;
4030 ever = extversym + extsymoff;
4031 isymend = isymbuf + extsymcount;
4032 for (isym = isymbuf; isym < isymend; isym++, ever++)
4034 const char *name;
4035 Elf_Internal_Versym iver;
4036 unsigned short version_index;
4038 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4039 || isym->st_shndx == SHN_UNDEF)
4040 continue;
4042 name = bfd_elf_string_from_elf_section (input,
4043 hdr->sh_link,
4044 isym->st_name);
4045 if (strcmp (name, h->root.root.string) != 0)
4046 continue;
4048 _bfd_elf_swap_versym_in (input, ever, &iver);
4050 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4052 /* If we have a non-hidden versioned sym, then it should
4053 have provided a definition for the undefined sym. */
4054 abort ();
4057 version_index = iver.vs_vers & VERSYM_VERSION;
4058 if (version_index == 1 || version_index == 2)
4060 /* This is the base or first version. We can use it. */
4061 free (extversym);
4062 free (isymbuf);
4063 return TRUE;
4067 free (extversym);
4068 free (isymbuf);
4071 return FALSE;
4074 /* Add an external symbol to the symbol table. This is called from
4075 the hash table traversal routine. When generating a shared object,
4076 we go through the symbol table twice. The first time we output
4077 anything that might have been forced to local scope in a version
4078 script. The second time we output the symbols that are still
4079 global symbols. */
4081 static bfd_boolean
4082 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
4084 struct elf_outext_info *eoinfo = data;
4085 struct elf_final_link_info *finfo = eoinfo->finfo;
4086 bfd_boolean strip;
4087 Elf_Internal_Sym sym;
4088 asection *input_sec;
4090 if (h->root.type == bfd_link_hash_warning)
4092 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4093 if (h->root.type == bfd_link_hash_new)
4094 return TRUE;
4097 /* Decide whether to output this symbol in this pass. */
4098 if (eoinfo->localsyms)
4100 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4101 return TRUE;
4103 else
4105 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4106 return TRUE;
4109 /* If we have an undefined symbol reference here then it must have
4110 come from a shared library that is being linked in. (Undefined
4111 references in regular files have already been handled). If we
4112 are reporting errors for this situation then do so now. */
4113 if (h->root.type == bfd_link_hash_undefined
4114 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4115 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4116 && ! elf_link_check_versioned_symbol (finfo->info, h)
4117 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
4119 if (! ((*finfo->info->callbacks->undefined_symbol)
4120 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4121 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
4123 eoinfo->failed = TRUE;
4124 return FALSE;
4128 /* We should also warn if a forced local symbol is referenced from
4129 shared libraries. */
4130 if (! finfo->info->relocatable
4131 && (! finfo->info->shared)
4132 && (h->elf_link_hash_flags
4133 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4134 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4135 && ! elf_link_check_versioned_symbol (finfo->info, h))
4137 (*_bfd_error_handler)
4138 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4139 bfd_get_filename (finfo->output_bfd),
4140 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4141 ? "internal"
4142 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4143 ? "hidden" : "local",
4144 h->root.root.string,
4145 bfd_archive_filename (h->root.u.def.section->owner));
4146 eoinfo->failed = TRUE;
4147 return FALSE;
4150 /* We don't want to output symbols that have never been mentioned by
4151 a regular file, or that we have been told to strip. However, if
4152 h->indx is set to -2, the symbol is used by a reloc and we must
4153 output it. */
4154 if (h->indx == -2)
4155 strip = FALSE;
4156 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4157 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4158 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4159 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4160 strip = TRUE;
4161 else if (finfo->info->strip == strip_all)
4162 strip = TRUE;
4163 else if (finfo->info->strip == strip_some
4164 && bfd_hash_lookup (finfo->info->keep_hash,
4165 h->root.root.string, FALSE, FALSE) == NULL)
4166 strip = TRUE;
4167 else if (finfo->info->strip_discarded
4168 && (h->root.type == bfd_link_hash_defined
4169 || h->root.type == bfd_link_hash_defweak)
4170 && elf_discarded_section (h->root.u.def.section))
4171 strip = TRUE;
4172 else
4173 strip = FALSE;
4175 /* If we're stripping it, and it's not a dynamic symbol, there's
4176 nothing else to do unless it is a forced local symbol. */
4177 if (strip
4178 && h->dynindx == -1
4179 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4180 return TRUE;
4182 sym.st_value = 0;
4183 sym.st_size = h->size;
4184 sym.st_other = h->other;
4185 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4186 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4187 else if (h->root.type == bfd_link_hash_undefweak
4188 || h->root.type == bfd_link_hash_defweak)
4189 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4190 else
4191 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4193 switch (h->root.type)
4195 default:
4196 case bfd_link_hash_new:
4197 case bfd_link_hash_warning:
4198 abort ();
4199 return FALSE;
4201 case bfd_link_hash_undefined:
4202 case bfd_link_hash_undefweak:
4203 input_sec = bfd_und_section_ptr;
4204 sym.st_shndx = SHN_UNDEF;
4205 break;
4207 case bfd_link_hash_defined:
4208 case bfd_link_hash_defweak:
4210 input_sec = h->root.u.def.section;
4211 if (input_sec->output_section != NULL)
4213 sym.st_shndx =
4214 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4215 input_sec->output_section);
4216 if (sym.st_shndx == SHN_BAD)
4218 (*_bfd_error_handler)
4219 (_("%s: could not find output section %s for input section %s"),
4220 bfd_get_filename (finfo->output_bfd),
4221 input_sec->output_section->name,
4222 input_sec->name);
4223 eoinfo->failed = TRUE;
4224 return FALSE;
4227 /* ELF symbols in relocatable files are section relative,
4228 but in nonrelocatable files they are virtual
4229 addresses. */
4230 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4231 if (! finfo->info->relocatable)
4233 sym.st_value += input_sec->output_section->vma;
4234 if (h->type == STT_TLS)
4236 /* STT_TLS symbols are relative to PT_TLS segment
4237 base. */
4238 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4239 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4243 else
4245 BFD_ASSERT (input_sec->owner == NULL
4246 || (input_sec->owner->flags & DYNAMIC) != 0);
4247 sym.st_shndx = SHN_UNDEF;
4248 input_sec = bfd_und_section_ptr;
4251 break;
4253 case bfd_link_hash_common:
4254 input_sec = h->root.u.c.p->section;
4255 sym.st_shndx = SHN_COMMON;
4256 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4257 break;
4259 case bfd_link_hash_indirect:
4260 /* These symbols are created by symbol versioning. They point
4261 to the decorated version of the name. For example, if the
4262 symbol foo@@GNU_1.2 is the default, which should be used when
4263 foo is used with no version, then we add an indirect symbol
4264 foo which points to foo@@GNU_1.2. We ignore these symbols,
4265 since the indirected symbol is already in the hash table. */
4266 return TRUE;
4269 /* Give the processor backend a chance to tweak the symbol value,
4270 and also to finish up anything that needs to be done for this
4271 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4272 forced local syms when non-shared is due to a historical quirk. */
4273 if ((h->dynindx != -1
4274 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4275 && ((finfo->info->shared
4276 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4277 || h->root.type != bfd_link_hash_undefweak))
4278 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4279 && elf_hash_table (finfo->info)->dynamic_sections_created)
4281 const struct elf_backend_data *bed;
4283 bed = get_elf_backend_data (finfo->output_bfd);
4284 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4285 (finfo->output_bfd, finfo->info, h, &sym)))
4287 eoinfo->failed = TRUE;
4288 return FALSE;
4292 /* If we are marking the symbol as undefined, and there are no
4293 non-weak references to this symbol from a regular object, then
4294 mark the symbol as weak undefined; if there are non-weak
4295 references, mark the symbol as strong. We can't do this earlier,
4296 because it might not be marked as undefined until the
4297 finish_dynamic_symbol routine gets through with it. */
4298 if (sym.st_shndx == SHN_UNDEF
4299 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4300 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4301 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4303 int bindtype;
4305 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4306 bindtype = STB_GLOBAL;
4307 else
4308 bindtype = STB_WEAK;
4309 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4312 /* If a non-weak symbol with non-default visibility is not defined
4313 locally, it is a fatal error. */
4314 if (! finfo->info->relocatable
4315 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4316 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4317 && h->root.type == bfd_link_hash_undefined
4318 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4320 (*_bfd_error_handler)
4321 (_("%s: %s symbol `%s' isn't defined"),
4322 bfd_get_filename (finfo->output_bfd),
4323 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4324 ? "protected"
4325 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4326 ? "internal" : "hidden",
4327 h->root.root.string);
4328 eoinfo->failed = TRUE;
4329 return FALSE;
4332 /* If this symbol should be put in the .dynsym section, then put it
4333 there now. We already know the symbol index. We also fill in
4334 the entry in the .hash section. */
4335 if (h->dynindx != -1
4336 && elf_hash_table (finfo->info)->dynamic_sections_created)
4338 size_t bucketcount;
4339 size_t bucket;
4340 size_t hash_entry_size;
4341 bfd_byte *bucketpos;
4342 bfd_vma chain;
4343 Elf_External_Sym *esym;
4345 sym.st_name = h->dynstr_index;
4346 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4347 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
4349 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4350 bucket = h->elf_hash_value % bucketcount;
4351 hash_entry_size
4352 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4353 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4354 + (bucket + 2) * hash_entry_size);
4355 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4356 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
4357 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4358 ((bfd_byte *) finfo->hash_sec->contents
4359 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4361 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4363 Elf_Internal_Versym iversym;
4364 Elf_External_Versym *eversym;
4366 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4368 if (h->verinfo.verdef == NULL)
4369 iversym.vs_vers = 0;
4370 else
4371 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4373 else
4375 if (h->verinfo.vertree == NULL)
4376 iversym.vs_vers = 1;
4377 else
4378 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4381 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4382 iversym.vs_vers |= VERSYM_HIDDEN;
4384 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4385 eversym += h->dynindx;
4386 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4390 /* If we're stripping it, then it was just a dynamic symbol, and
4391 there's nothing else to do. */
4392 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4393 return TRUE;
4395 h->indx = bfd_get_symcount (finfo->output_bfd);
4397 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
4399 eoinfo->failed = TRUE;
4400 return FALSE;
4403 return TRUE;
4406 /* Link an input file into the linker output file. This function
4407 handles all the sections and relocations of the input file at once.
4408 This is so that we only have to read the local symbols once, and
4409 don't have to keep them in memory. */
4411 static bfd_boolean
4412 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
4414 bfd_boolean (*relocate_section)
4415 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4416 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
4417 bfd *output_bfd;
4418 Elf_Internal_Shdr *symtab_hdr;
4419 size_t locsymcount;
4420 size_t extsymoff;
4421 Elf_Internal_Sym *isymbuf;
4422 Elf_Internal_Sym *isym;
4423 Elf_Internal_Sym *isymend;
4424 long *pindex;
4425 asection **ppsection;
4426 asection *o;
4427 const struct elf_backend_data *bed;
4428 bfd_boolean emit_relocs;
4429 struct elf_link_hash_entry **sym_hashes;
4431 output_bfd = finfo->output_bfd;
4432 bed = get_elf_backend_data (output_bfd);
4433 relocate_section = bed->elf_backend_relocate_section;
4435 /* If this is a dynamic object, we don't want to do anything here:
4436 we don't want the local symbols, and we don't want the section
4437 contents. */
4438 if ((input_bfd->flags & DYNAMIC) != 0)
4439 return TRUE;
4441 emit_relocs = (finfo->info->relocatable
4442 || finfo->info->emitrelocations
4443 || bed->elf_backend_emit_relocs);
4445 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4446 if (elf_bad_symtab (input_bfd))
4448 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4449 extsymoff = 0;
4451 else
4453 locsymcount = symtab_hdr->sh_info;
4454 extsymoff = symtab_hdr->sh_info;
4457 /* Read the local symbols. */
4458 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4459 if (isymbuf == NULL && locsymcount != 0)
4461 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4462 finfo->internal_syms,
4463 finfo->external_syms,
4464 finfo->locsym_shndx);
4465 if (isymbuf == NULL)
4466 return FALSE;
4469 /* Find local symbol sections and adjust values of symbols in
4470 SEC_MERGE sections. Write out those local symbols we know are
4471 going into the output file. */
4472 isymend = isymbuf + locsymcount;
4473 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4474 isym < isymend;
4475 isym++, pindex++, ppsection++)
4477 asection *isec;
4478 const char *name;
4479 Elf_Internal_Sym osym;
4481 *pindex = -1;
4483 if (elf_bad_symtab (input_bfd))
4485 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4487 *ppsection = NULL;
4488 continue;
4492 if (isym->st_shndx == SHN_UNDEF)
4493 isec = bfd_und_section_ptr;
4494 else if (isym->st_shndx < SHN_LORESERVE
4495 || isym->st_shndx > SHN_HIRESERVE)
4497 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4498 if (isec
4499 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4500 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4501 isym->st_value =
4502 _bfd_merged_section_offset (output_bfd, &isec,
4503 elf_section_data (isec)->sec_info,
4504 isym->st_value, 0);
4506 else if (isym->st_shndx == SHN_ABS)
4507 isec = bfd_abs_section_ptr;
4508 else if (isym->st_shndx == SHN_COMMON)
4509 isec = bfd_com_section_ptr;
4510 else
4512 /* Who knows? */
4513 isec = NULL;
4516 *ppsection = isec;
4518 /* Don't output the first, undefined, symbol. */
4519 if (ppsection == finfo->sections)
4520 continue;
4522 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4524 /* We never output section symbols. Instead, we use the
4525 section symbol of the corresponding section in the output
4526 file. */
4527 continue;
4530 /* If we are stripping all symbols, we don't want to output this
4531 one. */
4532 if (finfo->info->strip == strip_all)
4533 continue;
4535 /* If we are discarding all local symbols, we don't want to
4536 output this one. If we are generating a relocatable output
4537 file, then some of the local symbols may be required by
4538 relocs; we output them below as we discover that they are
4539 needed. */
4540 if (finfo->info->discard == discard_all)
4541 continue;
4543 /* If this symbol is defined in a section which we are
4544 discarding, we don't need to keep it, but note that
4545 linker_mark is only reliable for sections that have contents.
4546 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4547 as well as linker_mark. */
4548 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4549 && isec != NULL
4550 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4551 || (! finfo->info->relocatable
4552 && (isec->flags & SEC_EXCLUDE) != 0)))
4553 continue;
4555 /* Get the name of the symbol. */
4556 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4557 isym->st_name);
4558 if (name == NULL)
4559 return FALSE;
4561 /* See if we are discarding symbols with this name. */
4562 if ((finfo->info->strip == strip_some
4563 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4564 == NULL))
4565 || (((finfo->info->discard == discard_sec_merge
4566 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4567 || finfo->info->discard == discard_l)
4568 && bfd_is_local_label_name (input_bfd, name)))
4569 continue;
4571 /* If we get here, we are going to output this symbol. */
4573 osym = *isym;
4575 /* Adjust the section index for the output file. */
4576 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4577 isec->output_section);
4578 if (osym.st_shndx == SHN_BAD)
4579 return FALSE;
4581 *pindex = bfd_get_symcount (output_bfd);
4583 /* ELF symbols in relocatable files are section relative, but
4584 in executable files they are virtual addresses. Note that
4585 this code assumes that all ELF sections have an associated
4586 BFD section with a reasonable value for output_offset; below
4587 we assume that they also have a reasonable value for
4588 output_section. Any special sections must be set up to meet
4589 these requirements. */
4590 osym.st_value += isec->output_offset;
4591 if (! finfo->info->relocatable)
4593 osym.st_value += isec->output_section->vma;
4594 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4596 /* STT_TLS symbols are relative to PT_TLS segment base. */
4597 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4598 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4602 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
4603 return FALSE;
4606 /* Relocate the contents of each section. */
4607 sym_hashes = elf_sym_hashes (input_bfd);
4608 for (o = input_bfd->sections; o != NULL; o = o->next)
4610 bfd_byte *contents;
4612 if (! o->linker_mark)
4614 /* This section was omitted from the link. */
4615 continue;
4618 if ((o->flags & SEC_HAS_CONTENTS) == 0
4619 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4620 continue;
4622 if ((o->flags & SEC_LINKER_CREATED) != 0)
4624 /* Section was created by _bfd_elf_link_create_dynamic_sections
4625 or somesuch. */
4626 continue;
4629 /* Get the contents of the section. They have been cached by a
4630 relaxation routine. Note that o is a section in an input
4631 file, so the contents field will not have been set by any of
4632 the routines which work on output files. */
4633 if (elf_section_data (o)->this_hdr.contents != NULL)
4634 contents = elf_section_data (o)->this_hdr.contents;
4635 else
4637 contents = finfo->contents;
4638 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4639 o->_raw_size))
4640 return FALSE;
4643 if ((o->flags & SEC_RELOC) != 0)
4645 Elf_Internal_Rela *internal_relocs;
4647 /* Get the swapped relocs. */
4648 internal_relocs
4649 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4650 finfo->internal_relocs, FALSE);
4651 if (internal_relocs == NULL
4652 && o->reloc_count > 0)
4653 return FALSE;
4655 /* Run through the relocs looking for any against symbols
4656 from discarded sections and section symbols from
4657 removed link-once sections. Complain about relocs
4658 against discarded sections. Zero relocs against removed
4659 link-once sections. Preserve debug information as much
4660 as we can. */
4661 if (!elf_section_ignore_discarded_relocs (o))
4663 Elf_Internal_Rela *rel, *relend;
4665 rel = internal_relocs;
4666 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4667 for ( ; rel < relend; rel++)
4669 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4670 asection *sec;
4672 if (r_symndx >= locsymcount
4673 || (elf_bad_symtab (input_bfd)
4674 && finfo->sections[r_symndx] == NULL))
4676 struct elf_link_hash_entry *h;
4678 h = sym_hashes[r_symndx - extsymoff];
4679 while (h->root.type == bfd_link_hash_indirect
4680 || h->root.type == bfd_link_hash_warning)
4681 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4683 /* Complain if the definition comes from a
4684 discarded section. */
4685 sec = h->root.u.def.section;
4686 if ((h->root.type == bfd_link_hash_defined
4687 || h->root.type == bfd_link_hash_defweak)
4688 && elf_discarded_section (sec))
4690 if ((o->flags & SEC_DEBUGGING) != 0)
4692 BFD_ASSERT (r_symndx != 0);
4693 /* Try to preserve debug information. */
4694 if ((o->flags & SEC_DEBUGGING) != 0
4695 && sec->kept_section != NULL
4696 && sec->_raw_size == sec->kept_section->_raw_size)
4697 h->root.u.def.section
4698 = sec->kept_section;
4699 else
4700 memset (rel, 0, sizeof (*rel));
4702 else
4703 finfo->info->callbacks->error_handler
4704 (LD_DEFINITION_IN_DISCARDED_SECTION,
4705 _("%T: discarded in section `%s' from %s\n"),
4706 h->root.root.string,
4707 h->root.root.string,
4708 h->root.u.def.section->name,
4709 bfd_archive_filename (h->root.u.def.section->owner));
4712 else
4714 sec = finfo->sections[r_symndx];
4716 if (sec != NULL && elf_discarded_section (sec))
4718 if ((o->flags & SEC_DEBUGGING) != 0
4719 || (sec->flags & SEC_LINK_ONCE) != 0)
4721 BFD_ASSERT (r_symndx != 0);
4722 /* Try to preserve debug information. */
4723 if ((o->flags & SEC_DEBUGGING) != 0
4724 && sec->kept_section != NULL
4725 && sec->_raw_size == sec->kept_section->_raw_size)
4726 finfo->sections[r_symndx]
4727 = sec->kept_section;
4728 else
4730 rel->r_info
4731 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4732 rel->r_addend = 0;
4735 else
4737 static int count;
4738 int ok;
4739 char *buf;
4741 ok = asprintf (&buf, "local symbol %d",
4742 count++);
4743 if (ok <= 0)
4744 buf = (char *) "local symbol";
4745 finfo->info->callbacks->error_handler
4746 (LD_DEFINITION_IN_DISCARDED_SECTION,
4747 _("%T: discarded in section `%s' from %s\n"),
4748 buf, buf, sec->name,
4749 bfd_archive_filename (input_bfd));
4750 if (ok != -1)
4751 free (buf);
4758 /* Relocate the section by invoking a back end routine.
4760 The back end routine is responsible for adjusting the
4761 section contents as necessary, and (if using Rela relocs
4762 and generating a relocatable output file) adjusting the
4763 reloc addend as necessary.
4765 The back end routine does not have to worry about setting
4766 the reloc address or the reloc symbol index.
4768 The back end routine is given a pointer to the swapped in
4769 internal symbols, and can access the hash table entries
4770 for the external symbols via elf_sym_hashes (input_bfd).
4772 When generating relocatable output, the back end routine
4773 must handle STB_LOCAL/STT_SECTION symbols specially. The
4774 output symbol is going to be a section symbol
4775 corresponding to the output section, which will require
4776 the addend to be adjusted. */
4778 if (! (*relocate_section) (output_bfd, finfo->info,
4779 input_bfd, o, contents,
4780 internal_relocs,
4781 isymbuf,
4782 finfo->sections))
4783 return FALSE;
4785 if (emit_relocs)
4787 Elf_Internal_Rela *irela;
4788 Elf_Internal_Rela *irelaend;
4789 bfd_vma last_offset;
4790 struct elf_link_hash_entry **rel_hash;
4791 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4792 unsigned int next_erel;
4793 bfd_boolean (*reloc_emitter)
4794 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
4795 bfd_boolean rela_normal;
4797 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4798 rela_normal = (bed->rela_normal
4799 && (input_rel_hdr->sh_entsize
4800 == sizeof (Elf_External_Rela)));
4802 /* Adjust the reloc addresses and symbol indices. */
4804 irela = internal_relocs;
4805 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
4806 rel_hash = (elf_section_data (o->output_section)->rel_hashes
4807 + elf_section_data (o->output_section)->rel_count
4808 + elf_section_data (o->output_section)->rel_count2);
4809 last_offset = o->output_offset;
4810 if (!finfo->info->relocatable)
4811 last_offset += o->output_section->vma;
4812 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
4814 unsigned long r_symndx;
4815 asection *sec;
4816 Elf_Internal_Sym sym;
4818 if (next_erel == bed->s->int_rels_per_ext_rel)
4820 rel_hash++;
4821 next_erel = 0;
4824 irela->r_offset = _bfd_elf_section_offset (output_bfd,
4825 finfo->info, o,
4826 irela->r_offset);
4827 if (irela->r_offset >= (bfd_vma) -2)
4829 /* This is a reloc for a deleted entry or somesuch.
4830 Turn it into an R_*_NONE reloc, at the same
4831 offset as the last reloc. elf_eh_frame.c and
4832 elf_bfd_discard_info rely on reloc offsets
4833 being ordered. */
4834 irela->r_offset = last_offset;
4835 irela->r_info = 0;
4836 irela->r_addend = 0;
4837 continue;
4840 irela->r_offset += o->output_offset;
4842 /* Relocs in an executable have to be virtual addresses. */
4843 if (!finfo->info->relocatable)
4844 irela->r_offset += o->output_section->vma;
4846 last_offset = irela->r_offset;
4848 r_symndx = ELF_R_SYM (irela->r_info);
4849 if (r_symndx == STN_UNDEF)
4850 continue;
4852 if (r_symndx >= locsymcount
4853 || (elf_bad_symtab (input_bfd)
4854 && finfo->sections[r_symndx] == NULL))
4856 struct elf_link_hash_entry *rh;
4857 unsigned long indx;
4859 /* This is a reloc against a global symbol. We
4860 have not yet output all the local symbols, so
4861 we do not know the symbol index of any global
4862 symbol. We set the rel_hash entry for this
4863 reloc to point to the global hash table entry
4864 for this symbol. The symbol index is then
4865 set at the end of elf_bfd_final_link. */
4866 indx = r_symndx - extsymoff;
4867 rh = elf_sym_hashes (input_bfd)[indx];
4868 while (rh->root.type == bfd_link_hash_indirect
4869 || rh->root.type == bfd_link_hash_warning)
4870 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
4872 /* Setting the index to -2 tells
4873 elf_link_output_extsym that this symbol is
4874 used by a reloc. */
4875 BFD_ASSERT (rh->indx < 0);
4876 rh->indx = -2;
4878 *rel_hash = rh;
4880 continue;
4883 /* This is a reloc against a local symbol. */
4885 *rel_hash = NULL;
4886 sym = isymbuf[r_symndx];
4887 sec = finfo->sections[r_symndx];
4888 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
4890 /* I suppose the backend ought to fill in the
4891 section of any STT_SECTION symbol against a
4892 processor specific section. If we have
4893 discarded a section, the output_section will
4894 be the absolute section. */
4895 if (bfd_is_abs_section (sec)
4896 || (sec != NULL
4897 && bfd_is_abs_section (sec->output_section)))
4898 r_symndx = 0;
4899 else if (sec == NULL || sec->owner == NULL)
4901 bfd_set_error (bfd_error_bad_value);
4902 return FALSE;
4904 else
4906 r_symndx = sec->output_section->target_index;
4907 BFD_ASSERT (r_symndx != 0);
4910 /* Adjust the addend according to where the
4911 section winds up in the output section. */
4912 if (rela_normal)
4913 irela->r_addend += sec->output_offset;
4915 else
4917 if (finfo->indices[r_symndx] == -1)
4919 unsigned long shlink;
4920 const char *name;
4921 asection *osec;
4923 if (finfo->info->strip == strip_all)
4925 /* You can't do ld -r -s. */
4926 bfd_set_error (bfd_error_invalid_operation);
4927 return FALSE;
4930 /* This symbol was skipped earlier, but
4931 since it is needed by a reloc, we
4932 must output it now. */
4933 shlink = symtab_hdr->sh_link;
4934 name = (bfd_elf_string_from_elf_section
4935 (input_bfd, shlink, sym.st_name));
4936 if (name == NULL)
4937 return FALSE;
4939 osec = sec->output_section;
4940 sym.st_shndx =
4941 _bfd_elf_section_from_bfd_section (output_bfd,
4942 osec);
4943 if (sym.st_shndx == SHN_BAD)
4944 return FALSE;
4946 sym.st_value += sec->output_offset;
4947 if (! finfo->info->relocatable)
4949 sym.st_value += osec->vma;
4950 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
4952 /* STT_TLS symbols are relative to PT_TLS
4953 segment base. */
4954 BFD_ASSERT (elf_hash_table (finfo->info)
4955 ->tls_sec != NULL);
4956 sym.st_value -= (elf_hash_table (finfo->info)
4957 ->tls_sec->vma);
4961 finfo->indices[r_symndx]
4962 = bfd_get_symcount (output_bfd);
4964 if (! elf_link_output_sym (finfo, name, &sym, sec,
4965 NULL))
4966 return FALSE;
4969 r_symndx = finfo->indices[r_symndx];
4972 irela->r_info = ELF_R_INFO (r_symndx,
4973 ELF_R_TYPE (irela->r_info));
4976 /* Swap out the relocs. */
4977 if (bed->elf_backend_emit_relocs
4978 && !(finfo->info->relocatable
4979 || finfo->info->emitrelocations))
4980 reloc_emitter = bed->elf_backend_emit_relocs;
4981 else
4982 reloc_emitter = _bfd_elf_link_output_relocs;
4984 if (input_rel_hdr->sh_size != 0
4985 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
4986 internal_relocs))
4987 return FALSE;
4989 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
4990 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
4992 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
4993 * bed->s->int_rels_per_ext_rel);
4994 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
4995 internal_relocs))
4996 return FALSE;
5001 /* Write out the modified section contents. */
5002 if (bed->elf_backend_write_section
5003 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
5005 /* Section written out. */
5007 else switch (o->sec_info_type)
5009 case ELF_INFO_TYPE_STABS:
5010 if (! (_bfd_write_section_stabs
5011 (output_bfd,
5012 &elf_hash_table (finfo->info)->stab_info,
5013 o, &elf_section_data (o)->sec_info, contents)))
5014 return FALSE;
5015 break;
5016 case ELF_INFO_TYPE_MERGE:
5017 if (! _bfd_write_merged_section (output_bfd, o,
5018 elf_section_data (o)->sec_info))
5019 return FALSE;
5020 break;
5021 case ELF_INFO_TYPE_EH_FRAME:
5023 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5024 o, contents))
5025 return FALSE;
5027 break;
5028 default:
5030 bfd_size_type sec_size;
5032 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5033 if (! (o->flags & SEC_EXCLUDE)
5034 && ! bfd_set_section_contents (output_bfd, o->output_section,
5035 contents,
5036 (file_ptr) o->output_offset,
5037 sec_size))
5038 return FALSE;
5040 break;
5044 return TRUE;
5047 /* Generate a reloc when linking an ELF file. This is a reloc
5048 requested by the linker, and does come from any input file. This
5049 is used to build constructor and destructor tables when linking
5050 with -Ur. */
5052 static bfd_boolean
5053 elf_reloc_link_order (bfd *output_bfd,
5054 struct bfd_link_info *info,
5055 asection *output_section,
5056 struct bfd_link_order *link_order)
5058 reloc_howto_type *howto;
5059 long indx;
5060 bfd_vma offset;
5061 bfd_vma addend;
5062 struct elf_link_hash_entry **rel_hash_ptr;
5063 Elf_Internal_Shdr *rel_hdr;
5064 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5065 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5066 bfd_byte *erel;
5067 unsigned int i;
5069 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5070 if (howto == NULL)
5072 bfd_set_error (bfd_error_bad_value);
5073 return FALSE;
5076 addend = link_order->u.reloc.p->addend;
5078 /* Figure out the symbol index. */
5079 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5080 + elf_section_data (output_section)->rel_count
5081 + elf_section_data (output_section)->rel_count2);
5082 if (link_order->type == bfd_section_reloc_link_order)
5084 indx = link_order->u.reloc.p->u.section->target_index;
5085 BFD_ASSERT (indx != 0);
5086 *rel_hash_ptr = NULL;
5088 else
5090 struct elf_link_hash_entry *h;
5092 /* Treat a reloc against a defined symbol as though it were
5093 actually against the section. */
5094 h = ((struct elf_link_hash_entry *)
5095 bfd_wrapped_link_hash_lookup (output_bfd, info,
5096 link_order->u.reloc.p->u.name,
5097 FALSE, FALSE, TRUE));
5098 if (h != NULL
5099 && (h->root.type == bfd_link_hash_defined
5100 || h->root.type == bfd_link_hash_defweak))
5102 asection *section;
5104 section = h->root.u.def.section;
5105 indx = section->output_section->target_index;
5106 *rel_hash_ptr = NULL;
5107 /* It seems that we ought to add the symbol value to the
5108 addend here, but in practice it has already been added
5109 because it was passed to constructor_callback. */
5110 addend += section->output_section->vma + section->output_offset;
5112 else if (h != NULL)
5114 /* Setting the index to -2 tells elf_link_output_extsym that
5115 this symbol is used by a reloc. */
5116 h->indx = -2;
5117 *rel_hash_ptr = h;
5118 indx = 0;
5120 else
5122 if (! ((*info->callbacks->unattached_reloc)
5123 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
5124 return FALSE;
5125 indx = 0;
5129 /* If this is an inplace reloc, we must write the addend into the
5130 object file. */
5131 if (howto->partial_inplace && addend != 0)
5133 bfd_size_type size;
5134 bfd_reloc_status_type rstat;
5135 bfd_byte *buf;
5136 bfd_boolean ok;
5137 const char *sym_name;
5139 size = bfd_get_reloc_size (howto);
5140 buf = bfd_zmalloc (size);
5141 if (buf == NULL)
5142 return FALSE;
5143 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5144 switch (rstat)
5146 case bfd_reloc_ok:
5147 break;
5149 default:
5150 case bfd_reloc_outofrange:
5151 abort ();
5153 case bfd_reloc_overflow:
5154 if (link_order->type == bfd_section_reloc_link_order)
5155 sym_name = bfd_section_name (output_bfd,
5156 link_order->u.reloc.p->u.section);
5157 else
5158 sym_name = link_order->u.reloc.p->u.name;
5159 if (! ((*info->callbacks->reloc_overflow)
5160 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
5162 free (buf);
5163 return FALSE;
5165 break;
5167 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5168 link_order->offset, size);
5169 free (buf);
5170 if (! ok)
5171 return FALSE;
5174 /* The address of a reloc is relative to the section in a
5175 relocatable file, and is a virtual address in an executable
5176 file. */
5177 offset = link_order->offset;
5178 if (! info->relocatable)
5179 offset += output_section->vma;
5181 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5183 irel[i].r_offset = offset;
5184 irel[i].r_info = 0;
5185 irel[i].r_addend = 0;
5187 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5189 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5190 erel = rel_hdr->contents;
5191 if (rel_hdr->sh_type == SHT_REL)
5193 erel += (elf_section_data (output_section)->rel_count
5194 * sizeof (Elf_External_Rel));
5195 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5197 else
5199 irel[0].r_addend = addend;
5200 erel += (elf_section_data (output_section)->rel_count
5201 * sizeof (Elf_External_Rela));
5202 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5205 ++elf_section_data (output_section)->rel_count;
5207 return TRUE;
5210 /* Garbage collect unused sections. */
5212 static bfd_boolean elf_gc_sweep_symbol
5213 (struct elf_link_hash_entry *, void *);
5215 static bfd_boolean elf_gc_allocate_got_offsets
5216 (struct elf_link_hash_entry *, void *);
5218 /* The mark phase of garbage collection. For a given section, mark
5219 it and any sections in this section's group, and all the sections
5220 which define symbols to which it refers. */
5222 typedef asection * (*gc_mark_hook_fn)
5223 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5224 struct elf_link_hash_entry *, Elf_Internal_Sym *);
5226 static bfd_boolean
5227 elf_gc_mark (struct bfd_link_info *info,
5228 asection *sec,
5229 gc_mark_hook_fn gc_mark_hook)
5231 bfd_boolean ret;
5232 asection *group_sec;
5234 sec->gc_mark = 1;
5236 /* Mark all the sections in the group. */
5237 group_sec = elf_section_data (sec)->next_in_group;
5238 if (group_sec && !group_sec->gc_mark)
5239 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5240 return FALSE;
5242 /* Look through the section relocs. */
5243 ret = TRUE;
5244 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5246 Elf_Internal_Rela *relstart, *rel, *relend;
5247 Elf_Internal_Shdr *symtab_hdr;
5248 struct elf_link_hash_entry **sym_hashes;
5249 size_t nlocsyms;
5250 size_t extsymoff;
5251 bfd *input_bfd = sec->owner;
5252 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5253 Elf_Internal_Sym *isym = NULL;
5255 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5256 sym_hashes = elf_sym_hashes (input_bfd);
5258 /* Read the local symbols. */
5259 if (elf_bad_symtab (input_bfd))
5261 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5262 extsymoff = 0;
5264 else
5265 extsymoff = nlocsyms = symtab_hdr->sh_info;
5267 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5268 if (isym == NULL && nlocsyms != 0)
5270 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5271 NULL, NULL, NULL);
5272 if (isym == NULL)
5273 return FALSE;
5276 /* Read the relocations. */
5277 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5278 info->keep_memory);
5279 if (relstart == NULL)
5281 ret = FALSE;
5282 goto out1;
5284 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5286 for (rel = relstart; rel < relend; rel++)
5288 unsigned long r_symndx;
5289 asection *rsec;
5290 struct elf_link_hash_entry *h;
5292 r_symndx = ELF_R_SYM (rel->r_info);
5293 if (r_symndx == 0)
5294 continue;
5296 if (r_symndx >= nlocsyms
5297 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5299 h = sym_hashes[r_symndx - extsymoff];
5300 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5302 else
5304 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5307 if (rsec && !rsec->gc_mark)
5309 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5310 rsec->gc_mark = 1;
5311 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5313 ret = FALSE;
5314 goto out2;
5319 out2:
5320 if (elf_section_data (sec)->relocs != relstart)
5321 free (relstart);
5322 out1:
5323 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5325 if (! info->keep_memory)
5326 free (isym);
5327 else
5328 symtab_hdr->contents = (unsigned char *) isym;
5332 return ret;
5335 /* The sweep phase of garbage collection. Remove all garbage sections. */
5337 typedef bfd_boolean (*gc_sweep_hook_fn)
5338 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
5340 static bfd_boolean
5341 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
5343 bfd *sub;
5345 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5347 asection *o;
5349 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5350 continue;
5352 for (o = sub->sections; o != NULL; o = o->next)
5354 /* Keep special sections. Keep .debug sections. */
5355 if ((o->flags & SEC_LINKER_CREATED)
5356 || (o->flags & SEC_DEBUGGING))
5357 o->gc_mark = 1;
5359 if (o->gc_mark)
5360 continue;
5362 /* Skip sweeping sections already excluded. */
5363 if (o->flags & SEC_EXCLUDE)
5364 continue;
5366 /* Since this is early in the link process, it is simple
5367 to remove a section from the output. */
5368 o->flags |= SEC_EXCLUDE;
5370 /* But we also have to update some of the relocation
5371 info we collected before. */
5372 if (gc_sweep_hook
5373 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5375 Elf_Internal_Rela *internal_relocs;
5376 bfd_boolean r;
5378 internal_relocs
5379 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
5380 info->keep_memory);
5381 if (internal_relocs == NULL)
5382 return FALSE;
5384 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5386 if (elf_section_data (o)->relocs != internal_relocs)
5387 free (internal_relocs);
5389 if (!r)
5390 return FALSE;
5395 /* Remove the symbols that were in the swept sections from the dynamic
5396 symbol table. GCFIXME: Anyone know how to get them out of the
5397 static symbol table as well? */
5399 int i = 0;
5401 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
5403 elf_hash_table (info)->dynsymcount = i;
5406 return TRUE;
5409 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5411 static bfd_boolean
5412 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
5414 int *idx = idxptr;
5416 if (h->root.type == bfd_link_hash_warning)
5417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5419 if (h->dynindx != -1
5420 && ((h->root.type != bfd_link_hash_defined
5421 && h->root.type != bfd_link_hash_defweak)
5422 || h->root.u.def.section->gc_mark))
5423 h->dynindx = (*idx)++;
5425 return TRUE;
5428 /* Propagate collected vtable information. This is called through
5429 elf_link_hash_traverse. */
5431 static bfd_boolean
5432 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
5434 if (h->root.type == bfd_link_hash_warning)
5435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5437 /* Those that are not vtables. */
5438 if (h->vtable_parent == NULL)
5439 return TRUE;
5441 /* Those vtables that do not have parents, we cannot merge. */
5442 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5443 return TRUE;
5445 /* If we've already been done, exit. */
5446 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5447 return TRUE;
5449 /* Make sure the parent's table is up to date. */
5450 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5452 if (h->vtable_entries_used == NULL)
5454 /* None of this table's entries were referenced. Re-use the
5455 parent's table. */
5456 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5457 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5459 else
5461 size_t n;
5462 bfd_boolean *cu, *pu;
5464 /* Or the parent's entries into ours. */
5465 cu = h->vtable_entries_used;
5466 cu[-1] = TRUE;
5467 pu = h->vtable_parent->vtable_entries_used;
5468 if (pu != NULL)
5470 const struct elf_backend_data *bed;
5471 unsigned int log_file_align;
5473 bed = get_elf_backend_data (h->root.u.def.section->owner);
5474 log_file_align = bed->s->log_file_align;
5475 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5476 while (n--)
5478 if (*pu)
5479 *cu = TRUE;
5480 pu++;
5481 cu++;
5486 return TRUE;
5489 static bfd_boolean
5490 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
5492 asection *sec;
5493 bfd_vma hstart, hend;
5494 Elf_Internal_Rela *relstart, *relend, *rel;
5495 const struct elf_backend_data *bed;
5496 unsigned int log_file_align;
5498 if (h->root.type == bfd_link_hash_warning)
5499 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5501 /* Take care of both those symbols that do not describe vtables as
5502 well as those that are not loaded. */
5503 if (h->vtable_parent == NULL)
5504 return TRUE;
5506 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5507 || h->root.type == bfd_link_hash_defweak);
5509 sec = h->root.u.def.section;
5510 hstart = h->root.u.def.value;
5511 hend = hstart + h->size;
5513 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
5514 if (!relstart)
5515 return *(bfd_boolean *) okp = FALSE;
5516 bed = get_elf_backend_data (sec->owner);
5517 log_file_align = bed->s->log_file_align;
5519 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5521 for (rel = relstart; rel < relend; ++rel)
5522 if (rel->r_offset >= hstart && rel->r_offset < hend)
5524 /* If the entry is in use, do nothing. */
5525 if (h->vtable_entries_used
5526 && (rel->r_offset - hstart) < h->vtable_entries_size)
5528 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5529 if (h->vtable_entries_used[entry])
5530 continue;
5532 /* Otherwise, kill it. */
5533 rel->r_offset = rel->r_info = rel->r_addend = 0;
5536 return TRUE;
5539 /* Do mark and sweep of unused sections. */
5541 bfd_boolean
5542 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5544 bfd_boolean ok = TRUE;
5545 bfd *sub;
5546 asection * (*gc_mark_hook)
5547 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5548 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
5550 if (!get_elf_backend_data (abfd)->can_gc_sections
5551 || info->relocatable
5552 || info->emitrelocations
5553 || !is_elf_hash_table (info->hash)
5554 || elf_hash_table (info)->dynamic_sections_created)
5556 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5557 return TRUE;
5560 /* Apply transitive closure to the vtable entry usage info. */
5561 elf_link_hash_traverse (elf_hash_table (info),
5562 elf_gc_propagate_vtable_entries_used,
5563 &ok);
5564 if (!ok)
5565 return FALSE;
5567 /* Kill the vtable relocations that were not used. */
5568 elf_link_hash_traverse (elf_hash_table (info),
5569 elf_gc_smash_unused_vtentry_relocs,
5570 &ok);
5571 if (!ok)
5572 return FALSE;
5574 /* Grovel through relocs to find out who stays ... */
5576 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5577 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5579 asection *o;
5581 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5582 continue;
5584 for (o = sub->sections; o != NULL; o = o->next)
5586 if (o->flags & SEC_KEEP)
5587 if (!elf_gc_mark (info, o, gc_mark_hook))
5588 return FALSE;
5592 /* ... and mark SEC_EXCLUDE for those that go. */
5593 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5594 return FALSE;
5596 return TRUE;
5599 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5601 bfd_boolean
5602 elf_gc_record_vtinherit (bfd *abfd,
5603 asection *sec,
5604 struct elf_link_hash_entry *h,
5605 bfd_vma offset)
5607 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5608 struct elf_link_hash_entry **search, *child;
5609 bfd_size_type extsymcount;
5611 /* The sh_info field of the symtab header tells us where the
5612 external symbols start. We don't care about the local symbols at
5613 this point. */
5614 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5615 if (!elf_bad_symtab (abfd))
5616 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5618 sym_hashes = elf_sym_hashes (abfd);
5619 sym_hashes_end = sym_hashes + extsymcount;
5621 /* Hunt down the child symbol, which is in this section at the same
5622 offset as the relocation. */
5623 for (search = sym_hashes; search != sym_hashes_end; ++search)
5625 if ((child = *search) != NULL
5626 && (child->root.type == bfd_link_hash_defined
5627 || child->root.type == bfd_link_hash_defweak)
5628 && child->root.u.def.section == sec
5629 && child->root.u.def.value == offset)
5630 goto win;
5633 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
5634 bfd_archive_filename (abfd), sec->name,
5635 (unsigned long) offset);
5636 bfd_set_error (bfd_error_invalid_operation);
5637 return FALSE;
5639 win:
5640 if (!h)
5642 /* This *should* only be the absolute section. It could potentially
5643 be that someone has defined a non-global vtable though, which
5644 would be bad. It isn't worth paging in the local symbols to be
5645 sure though; that case should simply be handled by the assembler. */
5647 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5649 else
5650 child->vtable_parent = h;
5652 return TRUE;
5655 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5657 bfd_boolean
5658 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5659 asection *sec ATTRIBUTE_UNUSED,
5660 struct elf_link_hash_entry *h,
5661 bfd_vma addend)
5663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5664 unsigned int log_file_align = bed->s->log_file_align;
5666 if (addend >= h->vtable_entries_size)
5668 size_t size, bytes, file_align;
5669 bfd_boolean *ptr = h->vtable_entries_used;
5671 /* While the symbol is undefined, we have to be prepared to handle
5672 a zero size. */
5673 file_align = 1 << log_file_align;
5674 if (h->root.type == bfd_link_hash_undefined)
5675 size = addend + file_align;
5676 else
5678 size = h->size;
5679 if (addend >= size)
5681 /* Oops! We've got a reference past the defined end of
5682 the table. This is probably a bug -- shall we warn? */
5683 size = addend + file_align;
5686 size = (size + file_align - 1) & -file_align;
5688 /* Allocate one extra entry for use as a "done" flag for the
5689 consolidation pass. */
5690 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
5692 if (ptr)
5694 ptr = bfd_realloc (ptr - 1, bytes);
5696 if (ptr != NULL)
5698 size_t oldbytes;
5700 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
5701 * sizeof (bfd_boolean));
5702 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
5705 else
5706 ptr = bfd_zmalloc (bytes);
5708 if (ptr == NULL)
5709 return FALSE;
5711 /* And arrange for that done flag to be at index -1. */
5712 h->vtable_entries_used = ptr + 1;
5713 h->vtable_entries_size = size;
5716 h->vtable_entries_used[addend >> log_file_align] = TRUE;
5718 return TRUE;
5721 /* And an accompanying bit to work out final got entry offsets once
5722 we're done. Should be called from final_link. */
5724 bfd_boolean
5725 elf_gc_common_finalize_got_offsets (bfd *abfd,
5726 struct bfd_link_info *info)
5728 bfd *i;
5729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5730 bfd_vma gotoff;
5732 if (! is_elf_hash_table (info->hash))
5733 return FALSE;
5735 /* The GOT offset is relative to the .got section, but the GOT header is
5736 put into the .got.plt section, if the backend uses it. */
5737 if (bed->want_got_plt)
5738 gotoff = 0;
5739 else
5740 gotoff = bed->got_header_size;
5742 /* Do the local .got entries first. */
5743 for (i = info->input_bfds; i; i = i->link_next)
5745 bfd_signed_vma *local_got;
5746 bfd_size_type j, locsymcount;
5747 Elf_Internal_Shdr *symtab_hdr;
5749 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5750 continue;
5752 local_got = elf_local_got_refcounts (i);
5753 if (!local_got)
5754 continue;
5756 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5757 if (elf_bad_symtab (i))
5758 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5759 else
5760 locsymcount = symtab_hdr->sh_info;
5762 for (j = 0; j < locsymcount; ++j)
5764 if (local_got[j] > 0)
5766 local_got[j] = gotoff;
5767 gotoff += ARCH_SIZE / 8;
5769 else
5770 local_got[j] = (bfd_vma) -1;
5774 /* Then the global .got entries. .plt refcounts are handled by
5775 adjust_dynamic_symbol */
5776 elf_link_hash_traverse (elf_hash_table (info),
5777 elf_gc_allocate_got_offsets,
5778 &gotoff);
5779 return TRUE;
5782 /* We need a special top-level link routine to convert got reference counts
5783 to real got offsets. */
5785 static bfd_boolean
5786 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
5788 bfd_vma *off = offarg;
5790 if (h->root.type == bfd_link_hash_warning)
5791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5793 if (h->got.refcount > 0)
5795 h->got.offset = off[0];
5796 off[0] += ARCH_SIZE / 8;
5798 else
5799 h->got.offset = (bfd_vma) -1;
5801 return TRUE;
5804 /* Many folk need no more in the way of final link than this, once
5805 got entry reference counting is enabled. */
5807 bfd_boolean
5808 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
5810 if (!elf_gc_common_finalize_got_offsets (abfd, info))
5811 return FALSE;
5813 /* Invoke the regular ELF backend linker to do all the work. */
5814 return elf_bfd_final_link (abfd, info);
5817 /* This function will be called though elf_link_hash_traverse to store
5818 all hash value of the exported symbols in an array. */
5820 static bfd_boolean
5821 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5823 unsigned long **valuep = data;
5824 const char *name;
5825 char *p;
5826 unsigned long ha;
5827 char *alc = NULL;
5829 if (h->root.type == bfd_link_hash_warning)
5830 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5832 /* Ignore indirect symbols. These are added by the versioning code. */
5833 if (h->dynindx == -1)
5834 return TRUE;
5836 name = h->root.root.string;
5837 p = strchr (name, ELF_VER_CHR);
5838 if (p != NULL)
5840 alc = bfd_malloc (p - name + 1);
5841 memcpy (alc, name, p - name);
5842 alc[p - name] = '\0';
5843 name = alc;
5846 /* Compute the hash value. */
5847 ha = bfd_elf_hash (name);
5849 /* Store the found hash value in the array given as the argument. */
5850 *(*valuep)++ = ha;
5852 /* And store it in the struct so that we can put it in the hash table
5853 later. */
5854 h->elf_hash_value = ha;
5856 if (alc != NULL)
5857 free (alc);
5859 return TRUE;
5862 bfd_boolean
5863 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
5865 struct elf_reloc_cookie *rcookie = cookie;
5867 if (rcookie->bad_symtab)
5868 rcookie->rel = rcookie->rels;
5870 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
5872 unsigned long r_symndx;
5874 if (! rcookie->bad_symtab)
5875 if (rcookie->rel->r_offset > offset)
5876 return FALSE;
5877 if (rcookie->rel->r_offset != offset)
5878 continue;
5880 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
5881 if (r_symndx == SHN_UNDEF)
5882 return TRUE;
5884 if (r_symndx >= rcookie->locsymcount
5885 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
5887 struct elf_link_hash_entry *h;
5889 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
5891 while (h->root.type == bfd_link_hash_indirect
5892 || h->root.type == bfd_link_hash_warning)
5893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5895 if ((h->root.type == bfd_link_hash_defined
5896 || h->root.type == bfd_link_hash_defweak)
5897 && elf_discarded_section (h->root.u.def.section))
5898 return TRUE;
5899 else
5900 return FALSE;
5902 else
5904 /* It's not a relocation against a global symbol,
5905 but it could be a relocation against a local
5906 symbol for a discarded section. */
5907 asection *isec;
5908 Elf_Internal_Sym *isym;
5910 /* Need to: get the symbol; get the section. */
5911 isym = &rcookie->locsyms[r_symndx];
5912 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
5914 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
5915 if (isec != NULL && elf_discarded_section (isec))
5916 return TRUE;
5919 return FALSE;
5921 return FALSE;
5924 /* Discard unneeded references to discarded sections.
5925 Returns TRUE if any section's size was changed. */
5926 /* This function assumes that the relocations are in sorted order,
5927 which is true for all known assemblers. */
5929 bfd_boolean
5930 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
5932 struct elf_reloc_cookie cookie;
5933 asection *stab, *eh;
5934 Elf_Internal_Shdr *symtab_hdr;
5935 const struct elf_backend_data *bed;
5936 bfd *abfd;
5937 unsigned int count;
5938 bfd_boolean ret = FALSE;
5940 if (info->traditional_format
5941 || !is_elf_hash_table (info->hash))
5942 return FALSE;
5944 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
5946 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5947 continue;
5949 bed = get_elf_backend_data (abfd);
5951 if ((abfd->flags & DYNAMIC) != 0)
5952 continue;
5954 eh = bfd_get_section_by_name (abfd, ".eh_frame");
5955 if (info->relocatable
5956 || (eh != NULL
5957 && (eh->_raw_size == 0
5958 || bfd_is_abs_section (eh->output_section))))
5959 eh = NULL;
5961 stab = bfd_get_section_by_name (abfd, ".stab");
5962 if (stab != NULL
5963 && (stab->_raw_size == 0
5964 || bfd_is_abs_section (stab->output_section)
5965 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
5966 stab = NULL;
5968 if (stab == NULL
5969 && eh == NULL
5970 && bed->elf_backend_discard_info == NULL)
5971 continue;
5973 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5974 cookie.abfd = abfd;
5975 cookie.sym_hashes = elf_sym_hashes (abfd);
5976 cookie.bad_symtab = elf_bad_symtab (abfd);
5977 if (cookie.bad_symtab)
5979 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5980 cookie.extsymoff = 0;
5982 else
5984 cookie.locsymcount = symtab_hdr->sh_info;
5985 cookie.extsymoff = symtab_hdr->sh_info;
5988 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5989 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
5991 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5992 cookie.locsymcount, 0,
5993 NULL, NULL, NULL);
5994 if (cookie.locsyms == NULL)
5995 return FALSE;
5998 if (stab != NULL)
6000 cookie.rels = NULL;
6001 count = stab->reloc_count;
6002 if (count != 0)
6003 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6004 info->keep_memory);
6005 if (cookie.rels != NULL)
6007 cookie.rel = cookie.rels;
6008 cookie.relend = cookie.rels;
6009 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6010 if (_bfd_discard_section_stabs (abfd, stab,
6011 elf_section_data (stab)->sec_info,
6012 elf_reloc_symbol_deleted_p,
6013 &cookie))
6014 ret = TRUE;
6015 if (elf_section_data (stab)->relocs != cookie.rels)
6016 free (cookie.rels);
6020 if (eh != NULL)
6022 cookie.rels = NULL;
6023 count = eh->reloc_count;
6024 if (count != 0)
6025 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6026 info->keep_memory);
6027 cookie.rel = cookie.rels;
6028 cookie.relend = cookie.rels;
6029 if (cookie.rels != NULL)
6030 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6032 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6033 elf_reloc_symbol_deleted_p,
6034 &cookie))
6035 ret = TRUE;
6037 if (cookie.rels != NULL
6038 && elf_section_data (eh)->relocs != cookie.rels)
6039 free (cookie.rels);
6042 if (bed->elf_backend_discard_info != NULL
6043 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6044 ret = TRUE;
6046 if (cookie.locsyms != NULL
6047 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6049 if (! info->keep_memory)
6050 free (cookie.locsyms);
6051 else
6052 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6056 if (info->eh_frame_hdr
6057 && !info->relocatable
6058 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6059 ret = TRUE;
6061 return ret;
6064 static bfd_boolean
6065 elf_section_ignore_discarded_relocs (asection *sec)
6067 const struct elf_backend_data *bed;
6069 switch (sec->sec_info_type)
6071 case ELF_INFO_TYPE_STABS:
6072 case ELF_INFO_TYPE_EH_FRAME:
6073 return TRUE;
6074 default:
6075 break;
6078 bed = get_elf_backend_data (sec->owner);
6079 if (bed->elf_backend_ignore_discarded_relocs != NULL
6080 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6081 return TRUE;
6083 return FALSE;