2004-02-28 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / elflink.h
blob05f7c682b2f92c1e5137ba648e4046ad69ec71a9
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 struct elf_link_hash_table * hash_table;
99 bfd_size_type amt;
101 hash_table = elf_hash_table (info);
103 bed = get_elf_backend_data (abfd);
104 add_symbol_hook = bed->elf_add_symbol_hook;
105 collect = bed->collect;
107 if ((abfd->flags & DYNAMIC) == 0)
108 dynamic = FALSE;
109 else
111 dynamic = TRUE;
113 /* You can't use -r against a dynamic object. Also, there's no
114 hope of using a dynamic object which does not exactly match
115 the format of the output file. */
116 if (info->relocatable
117 || !is_elf_hash_table (hash_table)
118 || hash_table->root.creator != abfd->xvec)
120 bfd_set_error (bfd_error_invalid_operation);
121 goto error_return;
125 /* As a GNU extension, any input sections which are named
126 .gnu.warning.SYMBOL are treated as warning symbols for the given
127 symbol. This differs from .gnu.warning sections, which generate
128 warnings when they are included in an output file. */
129 if (info->executable)
131 asection *s;
133 for (s = abfd->sections; s != NULL; s = s->next)
135 const char *name;
137 name = bfd_get_section_name (abfd, s);
138 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
140 char *msg;
141 bfd_size_type sz;
142 bfd_size_type prefix_len;
143 const char * gnu_warning_prefix = _("warning: ");
145 name += sizeof ".gnu.warning." - 1;
147 /* If this is a shared object, then look up the symbol
148 in the hash table. If it is there, and it is already
149 been defined, then we will not be using the entry
150 from this shared object, so we don't need to warn.
151 FIXME: If we see the definition in a regular object
152 later on, we will warn, but we shouldn't. The only
153 fix is to keep track of what warnings we are supposed
154 to emit, and then handle them all at the end of the
155 link. */
156 if (dynamic)
158 struct elf_link_hash_entry *h;
160 h = elf_link_hash_lookup (hash_table, name,
161 FALSE, FALSE, TRUE);
163 /* FIXME: What about bfd_link_hash_common? */
164 if (h != NULL
165 && (h->root.type == bfd_link_hash_defined
166 || h->root.type == bfd_link_hash_defweak))
168 /* We don't want to issue this warning. Clobber
169 the section size so that the warning does not
170 get copied into the output file. */
171 s->_raw_size = 0;
172 continue;
176 sz = bfd_section_size (abfd, s);
177 prefix_len = strlen (gnu_warning_prefix);
178 msg = bfd_alloc (abfd, prefix_len + sz + 1);
179 if (msg == NULL)
180 goto error_return;
182 strcpy (msg, gnu_warning_prefix);
183 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
184 goto error_return;
186 msg[prefix_len + sz] = '\0';
188 if (! (_bfd_generic_link_add_one_symbol
189 (info, abfd, name, BSF_WARNING, s, 0, msg,
190 FALSE, collect, NULL)))
191 goto error_return;
193 if (! info->relocatable)
195 /* Clobber the section size so that the warning does
196 not get copied into the output file. */
197 s->_raw_size = 0;
203 dt_needed = FALSE;
204 if (! dynamic)
206 /* If we are creating a shared library, create all the dynamic
207 sections immediately. We need to attach them to something,
208 so we attach them to this BFD, provided it is the right
209 format. FIXME: If there are no input BFD's of the same
210 format as the output, we can't make a shared library. */
211 if (info->shared
212 && is_elf_hash_table (hash_table)
213 && hash_table->root.creator == abfd->xvec
214 && ! hash_table->dynamic_sections_created)
216 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
217 goto error_return;
220 else if (!is_elf_hash_table (hash_table))
221 goto error_return;
222 else
224 asection *s;
225 bfd_boolean add_needed;
226 const char *name;
227 bfd_size_type oldsize;
228 bfd_size_type strindex;
229 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
231 /* ld --just-symbols and dynamic objects don't mix very well.
232 Test for --just-symbols by looking at info set up by
233 _bfd_elf_link_just_syms. */
234 if ((s = abfd->sections) != NULL
235 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
236 goto error_return;
238 /* Find the name to use in a DT_NEEDED entry that refers to this
239 object. If the object has a DT_SONAME entry, we use it.
240 Otherwise, if the generic linker stuck something in
241 elf_dt_name, we use that. Otherwise, we just use the file
242 name. If the generic linker put a null string into
243 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
244 there is a DT_SONAME entry. */
245 add_needed = TRUE;
246 name = bfd_get_filename (abfd);
247 if (elf_dt_name (abfd) != NULL)
249 name = elf_dt_name (abfd);
250 if (*name == '\0')
252 if (elf_dt_soname (abfd) != NULL)
253 dt_needed = TRUE;
255 add_needed = FALSE;
258 s = bfd_get_section_by_name (abfd, ".dynamic");
259 if (s != NULL)
261 Elf_External_Dyn *dynbuf = NULL;
262 Elf_External_Dyn *extdyn;
263 Elf_External_Dyn *extdynend;
264 int elfsec;
265 unsigned long shlink;
267 dynbuf = bfd_malloc (s->_raw_size);
268 if (dynbuf == NULL)
269 goto error_return;
271 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
272 goto error_free_dyn;
274 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
275 if (elfsec == -1)
276 goto error_free_dyn;
277 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
279 extdyn = dynbuf;
280 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
281 for (; extdyn < extdynend; extdyn++)
283 Elf_Internal_Dyn dyn;
285 elf_swap_dyn_in (abfd, extdyn, &dyn);
286 if (dyn.d_tag == DT_SONAME)
288 unsigned int tagv = dyn.d_un.d_val;
289 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
290 if (name == NULL)
291 goto error_free_dyn;
293 if (dyn.d_tag == DT_NEEDED)
295 struct bfd_link_needed_list *n, **pn;
296 char *fnm, *anm;
297 unsigned int tagv = dyn.d_un.d_val;
299 amt = sizeof (struct bfd_link_needed_list);
300 n = bfd_alloc (abfd, amt);
301 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
302 if (n == NULL || fnm == NULL)
303 goto error_free_dyn;
304 amt = strlen (fnm) + 1;
305 anm = bfd_alloc (abfd, amt);
306 if (anm == NULL)
307 goto error_free_dyn;
308 memcpy (anm, fnm, amt);
309 n->name = anm;
310 n->by = abfd;
311 n->next = NULL;
312 for (pn = & hash_table->needed;
313 *pn != NULL;
314 pn = &(*pn)->next)
316 *pn = n;
318 if (dyn.d_tag == DT_RUNPATH)
320 struct bfd_link_needed_list *n, **pn;
321 char *fnm, *anm;
322 unsigned int tagv = dyn.d_un.d_val;
324 amt = sizeof (struct bfd_link_needed_list);
325 n = bfd_alloc (abfd, amt);
326 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
327 if (n == NULL || fnm == NULL)
328 goto error_free_dyn;
329 amt = strlen (fnm) + 1;
330 anm = bfd_alloc (abfd, amt);
331 if (anm == NULL)
332 goto error_free_dyn;
333 memcpy (anm, fnm, amt);
334 n->name = anm;
335 n->by = abfd;
336 n->next = NULL;
337 for (pn = & runpath;
338 *pn != NULL;
339 pn = &(*pn)->next)
341 *pn = n;
343 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
344 if (!runpath && dyn.d_tag == DT_RPATH)
346 struct bfd_link_needed_list *n, **pn;
347 char *fnm, *anm;
348 unsigned int tagv = dyn.d_un.d_val;
350 amt = sizeof (struct bfd_link_needed_list);
351 n = bfd_alloc (abfd, amt);
352 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
353 if (n == NULL || fnm == NULL)
354 goto error_free_dyn;
355 amt = strlen (fnm) + 1;
356 anm = bfd_alloc (abfd, amt);
357 if (anm == NULL)
359 error_free_dyn:
360 free (dynbuf);
361 goto error_return;
363 memcpy (anm, fnm, amt);
364 n->name = anm;
365 n->by = abfd;
366 n->next = NULL;
367 for (pn = & rpath;
368 *pn != NULL;
369 pn = &(*pn)->next)
371 *pn = n;
375 free (dynbuf);
378 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
379 frees all more recently bfd_alloc'd blocks as well. */
380 if (runpath)
381 rpath = runpath;
383 if (rpath)
385 struct bfd_link_needed_list **pn;
386 for (pn = & hash_table->runpath;
387 *pn != NULL;
388 pn = &(*pn)->next)
390 *pn = rpath;
393 /* We do not want to include any of the sections in a dynamic
394 object in the output file. We hack by simply clobbering the
395 list of sections in the BFD. This could be handled more
396 cleanly by, say, a new section flag; the existing
397 SEC_NEVER_LOAD flag is not the one we want, because that one
398 still implies that the section takes up space in the output
399 file. */
400 bfd_section_list_clear (abfd);
402 /* If this is the first dynamic object found in the link, create
403 the special sections required for dynamic linking. */
404 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
405 goto error_return;
407 if (add_needed)
409 /* Add a DT_NEEDED entry for this dynamic object. */
410 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
411 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
412 if (strindex == (bfd_size_type) -1)
413 goto error_return;
415 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
417 asection *sdyn;
418 Elf_External_Dyn *dyncon, *dynconend;
420 /* The hash table size did not change, which means that
421 the dynamic object name was already entered. If we
422 have already included this dynamic object in the
423 link, just ignore it. There is no reason to include
424 a particular dynamic object more than once. */
425 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
426 BFD_ASSERT (sdyn != NULL);
428 dyncon = (Elf_External_Dyn *) sdyn->contents;
429 dynconend = (Elf_External_Dyn *) (sdyn->contents +
430 sdyn->_raw_size);
431 for (; dyncon < dynconend; dyncon++)
433 Elf_Internal_Dyn dyn;
435 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
436 if (dyn.d_tag == DT_NEEDED
437 && dyn.d_un.d_val == strindex)
439 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
440 return TRUE;
445 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
446 goto error_return;
449 /* Save the SONAME, if there is one, because sometimes the
450 linker emulation code will need to know it. */
451 if (*name == '\0')
452 name = basename (bfd_get_filename (abfd));
453 elf_dt_name (abfd) = name;
456 /* If this is a dynamic object, we always link against the .dynsym
457 symbol table, not the .symtab symbol table. The dynamic linker
458 will only see the .dynsym symbol table, so there is no reason to
459 look at .symtab for a dynamic object. */
461 if (! dynamic || elf_dynsymtab (abfd) == 0)
462 hdr = &elf_tdata (abfd)->symtab_hdr;
463 else
464 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
466 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
468 /* The sh_info field of the symtab header tells us where the
469 external symbols start. We don't care about the local symbols at
470 this point. */
471 if (elf_bad_symtab (abfd))
473 extsymcount = symcount;
474 extsymoff = 0;
476 else
478 extsymcount = symcount - hdr->sh_info;
479 extsymoff = hdr->sh_info;
482 sym_hash = NULL;
483 if (extsymcount != 0)
485 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
486 NULL, NULL, NULL);
487 if (isymbuf == NULL)
488 goto error_return;
490 /* We store a pointer to the hash table entry for each external
491 symbol. */
492 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
493 sym_hash = bfd_alloc (abfd, amt);
494 if (sym_hash == NULL)
495 goto error_free_sym;
496 elf_sym_hashes (abfd) = sym_hash;
499 if (dynamic)
501 /* Read in any version definitions. */
502 if (! _bfd_elf_slurp_version_tables (abfd))
503 goto error_free_sym;
505 /* Read in the symbol versions, but don't bother to convert them
506 to internal format. */
507 if (elf_dynversym (abfd) != 0)
509 Elf_Internal_Shdr *versymhdr;
511 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
512 extversym = bfd_malloc (versymhdr->sh_size);
513 if (extversym == NULL)
514 goto error_free_sym;
515 amt = versymhdr->sh_size;
516 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
517 || bfd_bread (extversym, amt, abfd) != amt)
518 goto error_free_vers;
522 weaks = NULL;
524 ever = extversym != NULL ? extversym + extsymoff : NULL;
525 for (isym = isymbuf, isymend = isymbuf + extsymcount;
526 isym < isymend;
527 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
529 int bind;
530 bfd_vma value;
531 asection *sec;
532 flagword flags;
533 const char *name;
534 struct elf_link_hash_entry *h;
535 bfd_boolean definition;
536 bfd_boolean size_change_ok;
537 bfd_boolean type_change_ok;
538 bfd_boolean new_weakdef;
539 bfd_boolean override;
540 unsigned int old_alignment;
541 bfd *old_bfd;
543 override = FALSE;
545 flags = BSF_NO_FLAGS;
546 sec = NULL;
547 value = isym->st_value;
548 *sym_hash = NULL;
550 bind = ELF_ST_BIND (isym->st_info);
551 if (bind == STB_LOCAL)
553 /* This should be impossible, since ELF requires that all
554 global symbols follow all local symbols, and that sh_info
555 point to the first global symbol. Unfortunately, Irix 5
556 screws this up. */
557 continue;
559 else if (bind == STB_GLOBAL)
561 if (isym->st_shndx != SHN_UNDEF
562 && isym->st_shndx != SHN_COMMON)
563 flags = BSF_GLOBAL;
565 else if (bind == STB_WEAK)
566 flags = BSF_WEAK;
567 else
569 /* Leave it up to the processor backend. */
572 if (isym->st_shndx == SHN_UNDEF)
573 sec = bfd_und_section_ptr;
574 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
576 sec = section_from_elf_index (abfd, isym->st_shndx);
577 if (sec == NULL)
578 sec = bfd_abs_section_ptr;
579 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
580 value -= sec->vma;
582 else if (isym->st_shndx == SHN_ABS)
583 sec = bfd_abs_section_ptr;
584 else if (isym->st_shndx == SHN_COMMON)
586 sec = bfd_com_section_ptr;
587 /* What ELF calls the size we call the value. What ELF
588 calls the value we call the alignment. */
589 value = isym->st_size;
591 else
593 /* Leave it up to the processor backend. */
596 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
597 isym->st_name);
598 if (name == NULL)
599 goto error_free_vers;
601 if (isym->st_shndx == SHN_COMMON
602 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
604 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
606 if (tcomm == NULL)
608 tcomm = bfd_make_section (abfd, ".tcommon");
609 if (tcomm == NULL
610 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
611 | SEC_IS_COMMON
612 | SEC_LINKER_CREATED
613 | SEC_THREAD_LOCAL)))
614 goto error_free_vers;
616 sec = tcomm;
618 else if (add_symbol_hook)
620 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
621 &value))
622 goto error_free_vers;
624 /* The hook function sets the name to NULL if this symbol
625 should be skipped for some reason. */
626 if (name == NULL)
627 continue;
630 /* Sanity check that all possibilities were handled. */
631 if (sec == NULL)
633 bfd_set_error (bfd_error_bad_value);
634 goto error_free_vers;
637 if (bfd_is_und_section (sec)
638 || bfd_is_com_section (sec))
639 definition = FALSE;
640 else
641 definition = TRUE;
643 size_change_ok = FALSE;
644 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
645 old_alignment = 0;
646 old_bfd = NULL;
648 if (is_elf_hash_table (hash_table))
650 Elf_Internal_Versym iver;
651 unsigned int vernum = 0;
652 bfd_boolean skip;
654 if (ever != NULL)
656 _bfd_elf_swap_versym_in (abfd, ever, &iver);
657 vernum = iver.vs_vers & VERSYM_VERSION;
659 /* If this is a hidden symbol, or if it is not version
660 1, we append the version name to the symbol name.
661 However, we do not modify a non-hidden absolute
662 symbol, because it might be the version symbol
663 itself. FIXME: What if it isn't? */
664 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
665 || (vernum > 1 && ! bfd_is_abs_section (sec)))
667 const char *verstr;
668 size_t namelen, verlen, newlen;
669 char *newname, *p;
671 if (isym->st_shndx != SHN_UNDEF)
673 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
675 (*_bfd_error_handler)
676 (_("%s: %s: invalid version %u (max %d)"),
677 bfd_archive_filename (abfd), name, vernum,
678 elf_tdata (abfd)->dynverdef_hdr.sh_info);
679 bfd_set_error (bfd_error_bad_value);
680 goto error_free_vers;
682 else if (vernum > 1)
683 verstr =
684 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
685 else
686 verstr = "";
688 else
690 /* We cannot simply test for the number of
691 entries in the VERNEED section since the
692 numbers for the needed versions do not start
693 at 0. */
694 Elf_Internal_Verneed *t;
696 verstr = NULL;
697 for (t = elf_tdata (abfd)->verref;
698 t != NULL;
699 t = t->vn_nextref)
701 Elf_Internal_Vernaux *a;
703 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
705 if (a->vna_other == vernum)
707 verstr = a->vna_nodename;
708 break;
711 if (a != NULL)
712 break;
714 if (verstr == NULL)
716 (*_bfd_error_handler)
717 (_("%s: %s: invalid needed version %d"),
718 bfd_archive_filename (abfd), name, vernum);
719 bfd_set_error (bfd_error_bad_value);
720 goto error_free_vers;
724 namelen = strlen (name);
725 verlen = strlen (verstr);
726 newlen = namelen + verlen + 2;
727 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
728 && isym->st_shndx != SHN_UNDEF)
729 ++newlen;
731 newname = bfd_alloc (abfd, newlen);
732 if (newname == NULL)
733 goto error_free_vers;
734 memcpy (newname, name, namelen);
735 p = newname + namelen;
736 *p++ = ELF_VER_CHR;
737 /* If this is a defined non-hidden version symbol,
738 we add another @ to the name. This indicates the
739 default version of the symbol. */
740 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
741 && isym->st_shndx != SHN_UNDEF)
742 *p++ = ELF_VER_CHR;
743 memcpy (p, verstr, verlen + 1);
745 name = newname;
749 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
750 sym_hash, &skip, &override,
751 &type_change_ok, &size_change_ok,
752 dt_needed))
753 goto error_free_vers;
755 if (skip)
756 continue;
758 if (override)
759 definition = FALSE;
761 h = *sym_hash;
762 while (h->root.type == bfd_link_hash_indirect
763 || h->root.type == bfd_link_hash_warning)
764 h = (struct elf_link_hash_entry *) h->root.u.i.link;
766 /* Remember the old alignment if this is a common symbol, so
767 that we don't reduce the alignment later on. We can't
768 check later, because _bfd_generic_link_add_one_symbol
769 will set a default for the alignment which we want to
770 override. We also remember the old bfd where the existing
771 definition comes from. */
772 switch (h->root.type)
774 default:
775 break;
777 case bfd_link_hash_defined:
778 case bfd_link_hash_defweak:
779 old_bfd = h->root.u.def.section->owner;
780 break;
782 case bfd_link_hash_common:
783 old_bfd = h->root.u.c.p->section->owner;
784 old_alignment = h->root.u.c.p->alignment_power;
785 break;
788 if (elf_tdata (abfd)->verdef != NULL
789 && ! override
790 && vernum > 1
791 && definition)
792 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
795 if (! (_bfd_generic_link_add_one_symbol
796 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
797 (struct bfd_link_hash_entry **) sym_hash)))
798 goto error_free_vers;
800 h = *sym_hash;
801 while (h->root.type == bfd_link_hash_indirect
802 || h->root.type == bfd_link_hash_warning)
803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
804 *sym_hash = h;
806 new_weakdef = FALSE;
807 if (dynamic
808 && definition
809 && (flags & BSF_WEAK) != 0
810 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
811 && is_elf_hash_table (hash_table)
812 && h->weakdef == NULL)
814 /* Keep a list of all weak defined non function symbols from
815 a dynamic object, using the weakdef field. Later in this
816 function we will set the weakdef field to the correct
817 value. We only put non-function symbols from dynamic
818 objects on this list, because that happens to be the only
819 time we need to know the normal symbol corresponding to a
820 weak symbol, and the information is time consuming to
821 figure out. If the weakdef field is not already NULL,
822 then this symbol was already defined by some previous
823 dynamic object, and we will be using that previous
824 definition anyhow. */
826 h->weakdef = weaks;
827 weaks = h;
828 new_weakdef = TRUE;
831 /* Set the alignment of a common symbol. */
832 if (isym->st_shndx == SHN_COMMON
833 && h->root.type == bfd_link_hash_common)
835 unsigned int align;
837 align = bfd_log2 (isym->st_value);
838 if (align > old_alignment
839 /* Permit an alignment power of zero if an alignment of one
840 is specified and no other alignments have been specified. */
841 || (isym->st_value == 1 && old_alignment == 0))
842 h->root.u.c.p->alignment_power = align;
843 else
844 h->root.u.c.p->alignment_power = old_alignment;
847 if (is_elf_hash_table (hash_table))
849 int old_flags;
850 bfd_boolean dynsym;
851 int new_flag;
853 /* Check the alignment when a common symbol is involved. This
854 can change when a common symbol is overridden by a normal
855 definition or a common symbol is ignored due to the old
856 normal definition. We need to make sure the maximum
857 alignment is maintained. */
858 if ((old_alignment || isym->st_shndx == SHN_COMMON)
859 && h->root.type != bfd_link_hash_common)
861 unsigned int common_align;
862 unsigned int normal_align;
863 unsigned int symbol_align;
864 bfd *normal_bfd;
865 bfd *common_bfd;
867 symbol_align = ffs (h->root.u.def.value) - 1;
868 if (h->root.u.def.section->owner != NULL
869 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
871 normal_align = h->root.u.def.section->alignment_power;
872 if (normal_align > symbol_align)
873 normal_align = symbol_align;
875 else
876 normal_align = symbol_align;
878 if (old_alignment)
880 common_align = old_alignment;
881 common_bfd = old_bfd;
882 normal_bfd = abfd;
884 else
886 common_align = bfd_log2 (isym->st_value);
887 common_bfd = abfd;
888 normal_bfd = old_bfd;
891 if (normal_align < common_align)
892 (*_bfd_error_handler)
893 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
894 1 << normal_align,
895 name,
896 bfd_archive_filename (normal_bfd),
897 1 << common_align,
898 bfd_archive_filename (common_bfd));
901 /* Remember the symbol size and type. */
902 if (isym->st_size != 0
903 && (definition || h->size == 0))
905 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
906 (*_bfd_error_handler)
907 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
908 name, (unsigned long) h->size,
909 bfd_archive_filename (old_bfd),
910 (unsigned long) isym->st_size,
911 bfd_archive_filename (abfd));
913 h->size = isym->st_size;
916 /* If this is a common symbol, then we always want H->SIZE
917 to be the size of the common symbol. The code just above
918 won't fix the size if a common symbol becomes larger. We
919 don't warn about a size change here, because that is
920 covered by --warn-common. */
921 if (h->root.type == bfd_link_hash_common)
922 h->size = h->root.u.c.size;
924 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
925 && (definition || h->type == STT_NOTYPE))
927 if (h->type != STT_NOTYPE
928 && h->type != ELF_ST_TYPE (isym->st_info)
929 && ! type_change_ok)
930 (*_bfd_error_handler)
931 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
932 name, h->type, ELF_ST_TYPE (isym->st_info),
933 bfd_archive_filename (abfd));
935 h->type = ELF_ST_TYPE (isym->st_info);
938 /* If st_other has a processor-specific meaning, specific
939 code might be needed here. We never merge the visibility
940 attribute with the one from a dynamic object. */
941 if (bed->elf_backend_merge_symbol_attribute)
942 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
943 dynamic);
945 if (isym->st_other != 0 && !dynamic)
947 unsigned char hvis, symvis, other, nvis;
949 /* Take the balance of OTHER from the definition. */
950 other = (definition ? isym->st_other : h->other);
951 other &= ~ ELF_ST_VISIBILITY (-1);
953 /* Combine visibilities, using the most constraining one. */
954 hvis = ELF_ST_VISIBILITY (h->other);
955 symvis = ELF_ST_VISIBILITY (isym->st_other);
956 if (! hvis)
957 nvis = symvis;
958 else if (! symvis)
959 nvis = hvis;
960 else
961 nvis = hvis < symvis ? hvis : symvis;
963 h->other = other | nvis;
966 /* Set a flag in the hash table entry indicating the type of
967 reference or definition we just found. Keep a count of
968 the number of dynamic symbols we find. A dynamic symbol
969 is one which is referenced or defined by both a regular
970 object and a shared object. */
971 old_flags = h->elf_link_hash_flags;
972 dynsym = FALSE;
973 if (! dynamic)
975 if (! definition)
977 new_flag = ELF_LINK_HASH_REF_REGULAR;
978 if (bind != STB_WEAK)
979 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
981 else
982 new_flag = ELF_LINK_HASH_DEF_REGULAR;
983 if (! info->executable
984 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
985 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
986 dynsym = TRUE;
988 else
990 if (! definition)
991 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
992 else
993 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
994 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
995 | ELF_LINK_HASH_REF_REGULAR)) != 0
996 || (h->weakdef != NULL
997 && ! new_weakdef
998 && h->weakdef->dynindx != -1))
999 dynsym = TRUE;
1002 h->elf_link_hash_flags |= new_flag;
1004 /* Check to see if we need to add an indirect symbol for
1005 the default name. */
1006 if (definition || h->root.type == bfd_link_hash_common)
1007 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1008 &sec, &value, &dynsym,
1009 override, dt_needed))
1010 goto error_free_vers;
1012 if (definition && !dynamic)
1014 char *p = strchr (name, ELF_VER_CHR);
1015 if (p != NULL && p[1] != ELF_VER_CHR)
1017 /* Queue non-default versions so that .symver x, x@FOO
1018 aliases can be checked. */
1019 if (! nondeflt_vers)
1021 amt = (isymend - isym + 1)
1022 * sizeof (struct elf_link_hash_entry *);
1023 nondeflt_vers = bfd_malloc (amt);
1025 nondeflt_vers [nondeflt_vers_cnt++] = h;
1029 if (dynsym && h->dynindx == -1)
1031 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1032 goto error_free_vers;
1033 if (h->weakdef != NULL
1034 && ! new_weakdef
1035 && h->weakdef->dynindx == -1)
1037 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1038 goto error_free_vers;
1041 else if (dynsym && h->dynindx != -1)
1042 /* If the symbol already has a dynamic index, but
1043 visibility says it should not be visible, turn it into
1044 a local symbol. */
1045 switch (ELF_ST_VISIBILITY (h->other))
1047 case STV_INTERNAL:
1048 case STV_HIDDEN:
1049 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1050 break;
1053 if (dt_needed && definition
1054 && (h->elf_link_hash_flags
1055 & ELF_LINK_HASH_REF_REGULAR) != 0)
1057 bfd_size_type oldsize;
1058 bfd_size_type strindex;
1060 /* The symbol from a DT_NEEDED object is referenced from
1061 the regular object to create a dynamic executable. We
1062 have to make sure there is a DT_NEEDED entry for it. */
1064 dt_needed = FALSE;
1065 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1066 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1067 elf_dt_soname (abfd), FALSE);
1068 if (strindex == (bfd_size_type) -1)
1069 goto error_free_vers;
1071 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1073 asection *sdyn;
1074 Elf_External_Dyn *dyncon, *dynconend;
1076 sdyn = bfd_get_section_by_name (hash_table->dynobj,
1077 ".dynamic");
1078 BFD_ASSERT (sdyn != NULL);
1080 dyncon = (Elf_External_Dyn *) sdyn->contents;
1081 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1082 sdyn->_raw_size);
1083 for (; dyncon < dynconend; dyncon++)
1085 Elf_Internal_Dyn dyn;
1087 elf_swap_dyn_in (hash_table->dynobj,
1088 dyncon, &dyn);
1089 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1090 dyn.d_un.d_val != strindex);
1094 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1095 goto error_free_vers;
1100 /* Now that all the symbols from this input file are created, handle
1101 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1102 if (nondeflt_vers != NULL)
1104 bfd_size_type cnt, symidx;
1106 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
1108 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1109 char *shortname, *p;
1111 p = strchr (h->root.root.string, ELF_VER_CHR);
1112 if (p == NULL
1113 || (h->root.type != bfd_link_hash_defined
1114 && h->root.type != bfd_link_hash_defweak))
1115 continue;
1117 amt = p - h->root.root.string;
1118 shortname = bfd_malloc (amt + 1);
1119 memcpy (shortname, h->root.root.string, amt);
1120 shortname[amt] = '\0';
1122 hi = (struct elf_link_hash_entry *)
1123 bfd_link_hash_lookup (&hash_table->root, shortname,
1124 FALSE, FALSE, FALSE);
1125 if (hi != NULL
1126 && hi->root.type == h->root.type
1127 && hi->root.u.def.value == h->root.u.def.value
1128 && hi->root.u.def.section == h->root.u.def.section)
1130 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1131 hi->root.type = bfd_link_hash_indirect;
1132 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1133 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1134 sym_hash = elf_sym_hashes (abfd);
1135 if (sym_hash)
1136 for (symidx = 0; symidx < extsymcount; ++symidx)
1137 if (sym_hash[symidx] == hi)
1139 sym_hash[symidx] = h;
1140 break;
1143 free (shortname);
1145 free (nondeflt_vers);
1146 nondeflt_vers = NULL;
1149 if (extversym != NULL)
1151 free (extversym);
1152 extversym = NULL;
1155 if (isymbuf != NULL)
1156 free (isymbuf);
1157 isymbuf = NULL;
1159 /* Now set the weakdefs field correctly for all the weak defined
1160 symbols we found. The only way to do this is to search all the
1161 symbols. Since we only need the information for non functions in
1162 dynamic objects, that's the only time we actually put anything on
1163 the list WEAKS. We need this information so that if a regular
1164 object refers to a symbol defined weakly in a dynamic object, the
1165 real symbol in the dynamic object is also put in the dynamic
1166 symbols; we also must arrange for both symbols to point to the
1167 same memory location. We could handle the general case of symbol
1168 aliasing, but a general symbol alias can only be generated in
1169 assembler code, handling it correctly would be very time
1170 consuming, and other ELF linkers don't handle general aliasing
1171 either. */
1172 if (weaks != NULL)
1174 struct elf_link_hash_entry **hpp;
1175 struct elf_link_hash_entry **hppend;
1176 struct elf_link_hash_entry **sorted_sym_hash;
1177 struct elf_link_hash_entry *h;
1178 size_t sym_count;
1180 /* Since we have to search the whole symbol list for each weak
1181 defined symbol, search time for N weak defined symbols will be
1182 O(N^2). Binary search will cut it down to O(NlogN). */
1183 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1184 sorted_sym_hash = bfd_malloc (amt);
1185 if (sorted_sym_hash == NULL)
1186 goto error_return;
1187 sym_hash = sorted_sym_hash;
1188 hpp = elf_sym_hashes (abfd);
1189 hppend = hpp + extsymcount;
1190 sym_count = 0;
1191 for (; hpp < hppend; hpp++)
1193 h = *hpp;
1194 if (h != NULL
1195 && h->root.type == bfd_link_hash_defined
1196 && h->type != STT_FUNC)
1198 *sym_hash = h;
1199 sym_hash++;
1200 sym_count++;
1204 qsort (sorted_sym_hash, sym_count,
1205 sizeof (struct elf_link_hash_entry *),
1206 sort_symbol);
1208 while (weaks != NULL)
1210 struct elf_link_hash_entry *hlook;
1211 asection *slook;
1212 bfd_vma vlook;
1213 long ilook;
1214 size_t i, j, idx;
1216 hlook = weaks;
1217 weaks = hlook->weakdef;
1218 hlook->weakdef = NULL;
1220 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1221 || hlook->root.type == bfd_link_hash_defweak
1222 || hlook->root.type == bfd_link_hash_common
1223 || hlook->root.type == bfd_link_hash_indirect);
1224 slook = hlook->root.u.def.section;
1225 vlook = hlook->root.u.def.value;
1227 ilook = -1;
1228 i = 0;
1229 j = sym_count;
1230 while (i < j)
1232 bfd_signed_vma vdiff;
1233 idx = (i + j) / 2;
1234 h = sorted_sym_hash [idx];
1235 vdiff = vlook - h->root.u.def.value;
1236 if (vdiff < 0)
1237 j = idx;
1238 else if (vdiff > 0)
1239 i = idx + 1;
1240 else
1242 long sdiff = slook - h->root.u.def.section;
1243 if (sdiff < 0)
1244 j = idx;
1245 else if (sdiff > 0)
1246 i = idx + 1;
1247 else
1249 ilook = idx;
1250 break;
1255 /* We didn't find a value/section match. */
1256 if (ilook == -1)
1257 continue;
1259 for (i = ilook; i < sym_count; i++)
1261 h = sorted_sym_hash [i];
1263 /* Stop if value or section doesn't match. */
1264 if (h->root.u.def.value != vlook
1265 || h->root.u.def.section != slook)
1266 break;
1267 else if (h != hlook)
1269 hlook->weakdef = h;
1271 /* If the weak definition is in the list of dynamic
1272 symbols, make sure the real definition is put
1273 there as well. */
1274 if (hlook->dynindx != -1 && h->dynindx == -1)
1276 if (! _bfd_elf_link_record_dynamic_symbol (info,
1278 goto error_return;
1281 /* If the real definition is in the list of dynamic
1282 symbols, make sure the weak definition is put
1283 there as well. If we don't do this, then the
1284 dynamic loader might not merge the entries for the
1285 real definition and the weak definition. */
1286 if (h->dynindx != -1 && hlook->dynindx == -1)
1288 if (! _bfd_elf_link_record_dynamic_symbol (info,
1289 hlook))
1290 goto error_return;
1292 break;
1297 free (sorted_sym_hash);
1300 /* If this object is the same format as the output object, and it is
1301 not a shared library, then let the backend look through the
1302 relocs.
1304 This is required to build global offset table entries and to
1305 arrange for dynamic relocs. It is not required for the
1306 particular common case of linking non PIC code, even when linking
1307 against shared libraries, but unfortunately there is no way of
1308 knowing whether an object file has been compiled PIC or not.
1309 Looking through the relocs is not particularly time consuming.
1310 The problem is that we must either (1) keep the relocs in memory,
1311 which causes the linker to require additional runtime memory or
1312 (2) read the relocs twice from the input file, which wastes time.
1313 This would be a good case for using mmap.
1315 I have no idea how to handle linking PIC code into a file of a
1316 different format. It probably can't be done. */
1317 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1318 if (! dynamic
1319 && is_elf_hash_table (hash_table)
1320 && hash_table->root.creator == abfd->xvec
1321 && check_relocs != NULL)
1323 asection *o;
1325 for (o = abfd->sections; o != NULL; o = o->next)
1327 Elf_Internal_Rela *internal_relocs;
1328 bfd_boolean ok;
1330 if ((o->flags & SEC_RELOC) == 0
1331 || o->reloc_count == 0
1332 || ((info->strip == strip_all || info->strip == strip_debugger)
1333 && (o->flags & SEC_DEBUGGING) != 0)
1334 || bfd_is_abs_section (o->output_section))
1335 continue;
1337 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1338 info->keep_memory);
1339 if (internal_relocs == NULL)
1340 goto error_return;
1342 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1344 if (elf_section_data (o)->relocs != internal_relocs)
1345 free (internal_relocs);
1347 if (! ok)
1348 goto error_return;
1352 /* If this is a non-traditional link, try to optimize the handling
1353 of the .stab/.stabstr sections. */
1354 if (! dynamic
1355 && ! info->traditional_format
1356 && is_elf_hash_table (hash_table)
1357 && (info->strip != strip_all && info->strip != strip_debugger))
1359 asection *stabstr;
1361 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1362 if (stabstr != NULL)
1364 bfd_size_type string_offset = 0;
1365 asection *stab;
1367 for (stab = abfd->sections; stab; stab = stab->next)
1368 if (strncmp (".stab", stab->name, 5) == 0
1369 && (!stab->name[5] ||
1370 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1371 && (stab->flags & SEC_MERGE) == 0
1372 && !bfd_is_abs_section (stab->output_section))
1374 struct bfd_elf_section_data *secdata;
1376 secdata = elf_section_data (stab);
1377 if (! _bfd_link_section_stabs (abfd,
1378 & hash_table->stab_info,
1379 stab, stabstr,
1380 &secdata->sec_info,
1381 &string_offset))
1382 goto error_return;
1383 if (secdata->sec_info)
1384 stab->sec_info_type = ELF_INFO_TYPE_STABS;
1389 if (! info->relocatable
1390 && ! dynamic
1391 && is_elf_hash_table (hash_table))
1393 asection *s;
1395 for (s = abfd->sections; s != NULL; s = s->next)
1396 if ((s->flags & SEC_MERGE) != 0
1397 && !bfd_is_abs_section (s->output_section))
1399 struct bfd_elf_section_data *secdata;
1401 secdata = elf_section_data (s);
1402 if (! _bfd_merge_section (abfd,
1403 & hash_table->merge_info,
1404 s, &secdata->sec_info))
1405 goto error_return;
1406 else if (secdata->sec_info)
1407 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1411 if (is_elf_hash_table (hash_table))
1413 /* Add this bfd to the loaded list. */
1414 struct elf_link_loaded_list *n;
1416 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
1417 if (n == NULL)
1418 goto error_return;
1419 n->abfd = abfd;
1420 n->next = hash_table->loaded;
1421 hash_table->loaded = n;
1424 return TRUE;
1426 error_free_vers:
1427 if (nondeflt_vers != NULL)
1428 free (nondeflt_vers);
1429 if (extversym != NULL)
1430 free (extversym);
1431 error_free_sym:
1432 if (isymbuf != NULL)
1433 free (isymbuf);
1434 error_return:
1435 return FALSE;
1438 /* Add an entry to the .dynamic table. */
1440 bfd_boolean
1441 elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
1443 Elf_Internal_Dyn dyn;
1444 bfd *dynobj;
1445 asection *s;
1446 bfd_size_type newsize;
1447 bfd_byte *newcontents;
1449 if (! is_elf_hash_table (info->hash))
1450 return FALSE;
1452 dynobj = elf_hash_table (info)->dynobj;
1454 s = bfd_get_section_by_name (dynobj, ".dynamic");
1455 BFD_ASSERT (s != NULL);
1457 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1458 newcontents = bfd_realloc (s->contents, newsize);
1459 if (newcontents == NULL)
1460 return FALSE;
1462 dyn.d_tag = tag;
1463 dyn.d_un.d_val = val;
1464 elf_swap_dyn_out (dynobj, &dyn,
1465 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1467 s->_raw_size = newsize;
1468 s->contents = newcontents;
1470 return TRUE;
1473 /* Array used to determine the number of hash table buckets to use
1474 based on the number of symbols there are. If there are fewer than
1475 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1476 fewer than 37 we use 17 buckets, and so forth. We never use more
1477 than 32771 buckets. */
1479 static const size_t elf_buckets[] =
1481 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1482 16411, 32771, 0
1485 /* Compute bucket count for hashing table. We do not use a static set
1486 of possible tables sizes anymore. Instead we determine for all
1487 possible reasonable sizes of the table the outcome (i.e., the
1488 number of collisions etc) and choose the best solution. The
1489 weighting functions are not too simple to allow the table to grow
1490 without bounds. Instead one of the weighting factors is the size.
1491 Therefore the result is always a good payoff between few collisions
1492 (= short chain lengths) and table size. */
1493 static size_t
1494 compute_bucket_count (struct bfd_link_info *info)
1496 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1497 size_t best_size = 0;
1498 unsigned long int *hashcodes;
1499 unsigned long int *hashcodesp;
1500 unsigned long int i;
1501 bfd_size_type amt;
1503 /* Compute the hash values for all exported symbols. At the same
1504 time store the values in an array so that we could use them for
1505 optimizations. */
1506 amt = dynsymcount;
1507 amt *= sizeof (unsigned long int);
1508 hashcodes = bfd_malloc (amt);
1509 if (hashcodes == NULL)
1510 return 0;
1511 hashcodesp = hashcodes;
1513 /* Put all hash values in HASHCODES. */
1514 elf_link_hash_traverse (elf_hash_table (info),
1515 elf_collect_hash_codes, &hashcodesp);
1517 /* We have a problem here. The following code to optimize the table
1518 size requires an integer type with more the 32 bits. If
1519 BFD_HOST_U_64_BIT is set we know about such a type. */
1520 #ifdef BFD_HOST_U_64_BIT
1521 if (info->optimize)
1523 unsigned long int nsyms = hashcodesp - hashcodes;
1524 size_t minsize;
1525 size_t maxsize;
1526 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1527 unsigned long int *counts ;
1529 /* Possible optimization parameters: if we have NSYMS symbols we say
1530 that the hashing table must at least have NSYMS/4 and at most
1531 2*NSYMS buckets. */
1532 minsize = nsyms / 4;
1533 if (minsize == 0)
1534 minsize = 1;
1535 best_size = maxsize = nsyms * 2;
1537 /* Create array where we count the collisions in. We must use bfd_malloc
1538 since the size could be large. */
1539 amt = maxsize;
1540 amt *= sizeof (unsigned long int);
1541 counts = bfd_malloc (amt);
1542 if (counts == NULL)
1544 free (hashcodes);
1545 return 0;
1548 /* Compute the "optimal" size for the hash table. The criteria is a
1549 minimal chain length. The minor criteria is (of course) the size
1550 of the table. */
1551 for (i = minsize; i < maxsize; ++i)
1553 /* Walk through the array of hashcodes and count the collisions. */
1554 BFD_HOST_U_64_BIT max;
1555 unsigned long int j;
1556 unsigned long int fact;
1558 memset (counts, '\0', i * sizeof (unsigned long int));
1560 /* Determine how often each hash bucket is used. */
1561 for (j = 0; j < nsyms; ++j)
1562 ++counts[hashcodes[j] % i];
1564 /* For the weight function we need some information about the
1565 pagesize on the target. This is information need not be 100%
1566 accurate. Since this information is not available (so far) we
1567 define it here to a reasonable default value. If it is crucial
1568 to have a better value some day simply define this value. */
1569 # ifndef BFD_TARGET_PAGESIZE
1570 # define BFD_TARGET_PAGESIZE (4096)
1571 # endif
1573 /* We in any case need 2 + NSYMS entries for the size values and
1574 the chains. */
1575 max = (2 + nsyms) * (ARCH_SIZE / 8);
1577 # if 1
1578 /* Variant 1: optimize for short chains. We add the squares
1579 of all the chain lengths (which favors many small chain
1580 over a few long chains). */
1581 for (j = 0; j < i; ++j)
1582 max += counts[j] * counts[j];
1584 /* This adds penalties for the overall size of the table. */
1585 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1586 max *= fact * fact;
1587 # else
1588 /* Variant 2: Optimize a lot more for small table. Here we
1589 also add squares of the size but we also add penalties for
1590 empty slots (the +1 term). */
1591 for (j = 0; j < i; ++j)
1592 max += (1 + counts[j]) * (1 + counts[j]);
1594 /* The overall size of the table is considered, but not as
1595 strong as in variant 1, where it is squared. */
1596 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1597 max *= fact;
1598 # endif
1600 /* Compare with current best results. */
1601 if (max < best_chlen)
1603 best_chlen = max;
1604 best_size = i;
1608 free (counts);
1610 else
1611 #endif /* defined (BFD_HOST_U_64_BIT) */
1613 /* This is the fallback solution if no 64bit type is available or if we
1614 are not supposed to spend much time on optimizations. We select the
1615 bucket count using a fixed set of numbers. */
1616 for (i = 0; elf_buckets[i] != 0; i++)
1618 best_size = elf_buckets[i];
1619 if (dynsymcount < elf_buckets[i + 1])
1620 break;
1624 /* Free the arrays we needed. */
1625 free (hashcodes);
1627 return best_size;
1630 /* Set up the sizes and contents of the ELF dynamic sections. This is
1631 called by the ELF linker emulation before_allocation routine. We
1632 must set the sizes of the sections before the linker sets the
1633 addresses of the various sections. */
1635 bfd_boolean
1636 NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1637 const char *soname,
1638 const char *rpath,
1639 const char *filter_shlib,
1640 const char * const *auxiliary_filters,
1641 struct bfd_link_info *info,
1642 asection **sinterpptr,
1643 struct bfd_elf_version_tree *verdefs)
1645 bfd_size_type soname_indx;
1646 bfd *dynobj;
1647 const struct elf_backend_data *bed;
1648 struct elf_assign_sym_version_info asvinfo;
1650 *sinterpptr = NULL;
1652 soname_indx = (bfd_size_type) -1;
1654 if (!is_elf_hash_table (info->hash))
1655 return TRUE;
1657 if (info->execstack)
1658 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1659 else if (info->noexecstack)
1660 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1661 else
1663 bfd *inputobj;
1664 asection *notesec = NULL;
1665 int exec = 0;
1667 for (inputobj = info->input_bfds;
1668 inputobj;
1669 inputobj = inputobj->link_next)
1671 asection *s;
1673 if (inputobj->flags & DYNAMIC)
1674 continue;
1675 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1676 if (s)
1678 if (s->flags & SEC_CODE)
1679 exec = PF_X;
1680 notesec = s;
1682 else
1683 exec = PF_X;
1685 if (notesec)
1687 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1688 if (exec && info->relocatable
1689 && notesec->output_section != bfd_abs_section_ptr)
1690 notesec->output_section->flags |= SEC_CODE;
1694 /* Any syms created from now on start with -1 in
1695 got.refcount/offset and plt.refcount/offset. */
1696 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
1698 /* The backend may have to create some sections regardless of whether
1699 we're dynamic or not. */
1700 bed = get_elf_backend_data (output_bfd);
1701 if (bed->elf_backend_always_size_sections
1702 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1703 return FALSE;
1705 dynobj = elf_hash_table (info)->dynobj;
1707 /* If there were no dynamic objects in the link, there is nothing to
1708 do here. */
1709 if (dynobj == NULL)
1710 return TRUE;
1712 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1713 return FALSE;
1715 if (elf_hash_table (info)->dynamic_sections_created)
1717 struct elf_info_failed eif;
1718 struct elf_link_hash_entry *h;
1719 asection *dynstr;
1720 struct bfd_elf_version_tree *t;
1721 struct bfd_elf_version_expr *d;
1722 bfd_boolean all_defined;
1724 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1725 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
1727 if (soname != NULL)
1729 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1730 soname, TRUE);
1731 if (soname_indx == (bfd_size_type) -1
1732 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
1733 return FALSE;
1736 if (info->symbolic)
1738 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1739 return FALSE;
1740 info->flags |= DF_SYMBOLIC;
1743 if (rpath != NULL)
1745 bfd_size_type indx;
1747 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
1748 TRUE);
1749 if (info->new_dtags)
1750 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
1751 if (indx == (bfd_size_type) -1
1752 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
1753 || (info->new_dtags
1754 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
1755 return FALSE;
1758 if (filter_shlib != NULL)
1760 bfd_size_type indx;
1762 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1763 filter_shlib, TRUE);
1764 if (indx == (bfd_size_type) -1
1765 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
1766 return FALSE;
1769 if (auxiliary_filters != NULL)
1771 const char * const *p;
1773 for (p = auxiliary_filters; *p != NULL; p++)
1775 bfd_size_type indx;
1777 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1778 *p, TRUE);
1779 if (indx == (bfd_size_type) -1
1780 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
1781 return FALSE;
1785 eif.info = info;
1786 eif.verdefs = verdefs;
1787 eif.failed = FALSE;
1789 /* If we are supposed to export all symbols into the dynamic symbol
1790 table (this is not the normal case), then do so. */
1791 if (info->export_dynamic)
1793 elf_link_hash_traverse (elf_hash_table (info),
1794 _bfd_elf_export_symbol,
1795 &eif);
1796 if (eif.failed)
1797 return FALSE;
1800 /* Make all global versions with definition. */
1801 for (t = verdefs; t != NULL; t = t->next)
1802 for (d = t->globals.list; d != NULL; d = d->next)
1803 if (!d->symver && d->symbol)
1805 const char *verstr, *name;
1806 size_t namelen, verlen, newlen;
1807 char *newname, *p;
1808 struct elf_link_hash_entry *newh;
1810 name = d->symbol;
1811 namelen = strlen (name);
1812 verstr = t->name;
1813 verlen = strlen (verstr);
1814 newlen = namelen + verlen + 3;
1816 newname = bfd_malloc (newlen);
1817 if (newname == NULL)
1818 return FALSE;
1819 memcpy (newname, name, namelen);
1821 /* Check the hidden versioned definition. */
1822 p = newname + namelen;
1823 *p++ = ELF_VER_CHR;
1824 memcpy (p, verstr, verlen + 1);
1825 newh = elf_link_hash_lookup (elf_hash_table (info),
1826 newname, FALSE, FALSE,
1827 FALSE);
1828 if (newh == NULL
1829 || (newh->root.type != bfd_link_hash_defined
1830 && newh->root.type != bfd_link_hash_defweak))
1832 /* Check the default versioned definition. */
1833 *p++ = ELF_VER_CHR;
1834 memcpy (p, verstr, verlen + 1);
1835 newh = elf_link_hash_lookup (elf_hash_table (info),
1836 newname, FALSE, FALSE,
1837 FALSE);
1839 free (newname);
1841 /* Mark this version if there is a definition and it is
1842 not defined in a shared object. */
1843 if (newh != NULL
1844 && ((newh->elf_link_hash_flags
1845 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
1846 && (newh->root.type == bfd_link_hash_defined
1847 || newh->root.type == bfd_link_hash_defweak))
1848 d->symver = 1;
1851 /* Attach all the symbols to their version information. */
1852 asvinfo.output_bfd = output_bfd;
1853 asvinfo.info = info;
1854 asvinfo.verdefs = verdefs;
1855 asvinfo.failed = FALSE;
1857 elf_link_hash_traverse (elf_hash_table (info),
1858 _bfd_elf_link_assign_sym_version,
1859 &asvinfo);
1860 if (asvinfo.failed)
1861 return FALSE;
1863 if (!info->allow_undefined_version)
1865 /* Check if all global versions have a definition. */
1866 all_defined = TRUE;
1867 for (t = verdefs; t != NULL; t = t->next)
1868 for (d = t->globals.list; d != NULL; d = d->next)
1869 if (!d->symver && !d->script)
1871 (*_bfd_error_handler)
1872 (_("%s: undefined version: %s"),
1873 d->pattern, t->name);
1874 all_defined = FALSE;
1877 if (!all_defined)
1879 bfd_set_error (bfd_error_bad_value);
1880 return FALSE;
1884 /* Find all symbols which were defined in a dynamic object and make
1885 the backend pick a reasonable value for them. */
1886 elf_link_hash_traverse (elf_hash_table (info),
1887 _bfd_elf_adjust_dynamic_symbol,
1888 &eif);
1889 if (eif.failed)
1890 return FALSE;
1892 /* Add some entries to the .dynamic section. We fill in some of the
1893 values later, in elf_bfd_final_link, but we must add the entries
1894 now so that we know the final size of the .dynamic section. */
1896 /* If there are initialization and/or finalization functions to
1897 call then add the corresponding DT_INIT/DT_FINI entries. */
1898 h = (info->init_function
1899 ? elf_link_hash_lookup (elf_hash_table (info),
1900 info->init_function, FALSE,
1901 FALSE, FALSE)
1902 : NULL);
1903 if (h != NULL
1904 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1905 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1907 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
1908 return FALSE;
1910 h = (info->fini_function
1911 ? elf_link_hash_lookup (elf_hash_table (info),
1912 info->fini_function, FALSE,
1913 FALSE, FALSE)
1914 : NULL);
1915 if (h != NULL
1916 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1917 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1919 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
1920 return FALSE;
1923 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
1925 /* DT_PREINIT_ARRAY is not allowed in shared library. */
1926 if (! info->executable)
1928 bfd *sub;
1929 asection *o;
1931 for (sub = info->input_bfds; sub != NULL;
1932 sub = sub->link_next)
1933 for (o = sub->sections; o != NULL; o = o->next)
1934 if (elf_section_data (o)->this_hdr.sh_type
1935 == SHT_PREINIT_ARRAY)
1937 (*_bfd_error_handler)
1938 (_("%s: .preinit_array section is not allowed in DSO"),
1939 bfd_archive_filename (sub));
1940 break;
1943 bfd_set_error (bfd_error_nonrepresentable_section);
1944 return FALSE;
1947 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
1948 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
1949 return FALSE;
1951 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
1953 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
1954 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
1955 return FALSE;
1957 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
1959 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
1960 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
1961 return FALSE;
1964 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
1965 /* If .dynstr is excluded from the link, we don't want any of
1966 these tags. Strictly, we should be checking each section
1967 individually; This quick check covers for the case where
1968 someone does a /DISCARD/ : { *(*) }. */
1969 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
1971 bfd_size_type strsize;
1973 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
1974 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1975 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1976 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1977 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1978 || ! elf_add_dynamic_entry (info, DT_SYMENT,
1979 sizeof (Elf_External_Sym)))
1980 return FALSE;
1984 /* The backend must work out the sizes of all the other dynamic
1985 sections. */
1986 if (bed->elf_backend_size_dynamic_sections
1987 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1988 return FALSE;
1990 if (elf_hash_table (info)->dynamic_sections_created)
1992 bfd_size_type dynsymcount;
1993 asection *s;
1994 size_t bucketcount = 0;
1995 size_t hash_entry_size;
1996 unsigned int dtagcount;
1998 /* Set up the version definition section. */
1999 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2000 BFD_ASSERT (s != NULL);
2002 /* We may have created additional version definitions if we are
2003 just linking a regular application. */
2004 verdefs = asvinfo.verdefs;
2006 /* Skip anonymous version tag. */
2007 if (verdefs != NULL && verdefs->vernum == 0)
2008 verdefs = verdefs->next;
2010 if (verdefs == NULL)
2011 _bfd_strip_section_from_output (info, s);
2012 else
2014 unsigned int cdefs;
2015 bfd_size_type size;
2016 struct bfd_elf_version_tree *t;
2017 bfd_byte *p;
2018 Elf_Internal_Verdef def;
2019 Elf_Internal_Verdaux defaux;
2021 cdefs = 0;
2022 size = 0;
2024 /* Make space for the base version. */
2025 size += sizeof (Elf_External_Verdef);
2026 size += sizeof (Elf_External_Verdaux);
2027 ++cdefs;
2029 for (t = verdefs; t != NULL; t = t->next)
2031 struct bfd_elf_version_deps *n;
2033 size += sizeof (Elf_External_Verdef);
2034 size += sizeof (Elf_External_Verdaux);
2035 ++cdefs;
2037 for (n = t->deps; n != NULL; n = n->next)
2038 size += sizeof (Elf_External_Verdaux);
2041 s->_raw_size = size;
2042 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2043 if (s->contents == NULL && s->_raw_size != 0)
2044 return FALSE;
2046 /* Fill in the version definition section. */
2048 p = s->contents;
2050 def.vd_version = VER_DEF_CURRENT;
2051 def.vd_flags = VER_FLG_BASE;
2052 def.vd_ndx = 1;
2053 def.vd_cnt = 1;
2054 def.vd_aux = sizeof (Elf_External_Verdef);
2055 def.vd_next = (sizeof (Elf_External_Verdef)
2056 + sizeof (Elf_External_Verdaux));
2058 if (soname_indx != (bfd_size_type) -1)
2060 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2061 soname_indx);
2062 def.vd_hash = bfd_elf_hash (soname);
2063 defaux.vda_name = soname_indx;
2065 else
2067 const char *name;
2068 bfd_size_type indx;
2070 name = basename (output_bfd->filename);
2071 def.vd_hash = bfd_elf_hash (name);
2072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2073 name, FALSE);
2074 if (indx == (bfd_size_type) -1)
2075 return FALSE;
2076 defaux.vda_name = indx;
2078 defaux.vda_next = 0;
2080 _bfd_elf_swap_verdef_out (output_bfd, &def,
2081 (Elf_External_Verdef *) p);
2082 p += sizeof (Elf_External_Verdef);
2083 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2084 (Elf_External_Verdaux *) p);
2085 p += sizeof (Elf_External_Verdaux);
2087 for (t = verdefs; t != NULL; t = t->next)
2089 unsigned int cdeps;
2090 struct bfd_elf_version_deps *n;
2091 struct elf_link_hash_entry *h;
2092 struct bfd_link_hash_entry *bh;
2094 cdeps = 0;
2095 for (n = t->deps; n != NULL; n = n->next)
2096 ++cdeps;
2098 /* Add a symbol representing this version. */
2099 bh = NULL;
2100 if (! (_bfd_generic_link_add_one_symbol
2101 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2102 0, NULL, FALSE,
2103 get_elf_backend_data (dynobj)->collect, &bh)))
2104 return FALSE;
2105 h = (struct elf_link_hash_entry *) bh;
2106 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2107 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2108 h->type = STT_OBJECT;
2109 h->verinfo.vertree = t;
2111 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2112 return FALSE;
2114 def.vd_version = VER_DEF_CURRENT;
2115 def.vd_flags = 0;
2116 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
2117 def.vd_flags |= VER_FLG_WEAK;
2118 def.vd_ndx = t->vernum + 1;
2119 def.vd_cnt = cdeps + 1;
2120 def.vd_hash = bfd_elf_hash (t->name);
2121 def.vd_aux = sizeof (Elf_External_Verdef);
2122 if (t->next != NULL)
2123 def.vd_next = (sizeof (Elf_External_Verdef)
2124 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2125 else
2126 def.vd_next = 0;
2128 _bfd_elf_swap_verdef_out (output_bfd, &def,
2129 (Elf_External_Verdef *) p);
2130 p += sizeof (Elf_External_Verdef);
2132 defaux.vda_name = h->dynstr_index;
2133 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2134 h->dynstr_index);
2135 if (t->deps == NULL)
2136 defaux.vda_next = 0;
2137 else
2138 defaux.vda_next = sizeof (Elf_External_Verdaux);
2139 t->name_indx = defaux.vda_name;
2141 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2142 (Elf_External_Verdaux *) p);
2143 p += sizeof (Elf_External_Verdaux);
2145 for (n = t->deps; n != NULL; n = n->next)
2147 if (n->version_needed == NULL)
2149 /* This can happen if there was an error in the
2150 version script. */
2151 defaux.vda_name = 0;
2153 else
2155 defaux.vda_name = n->version_needed->name_indx;
2156 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2157 defaux.vda_name);
2159 if (n->next == NULL)
2160 defaux.vda_next = 0;
2161 else
2162 defaux.vda_next = sizeof (Elf_External_Verdaux);
2164 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2165 (Elf_External_Verdaux *) p);
2166 p += sizeof (Elf_External_Verdaux);
2170 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2171 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2172 return FALSE;
2174 elf_tdata (output_bfd)->cverdefs = cdefs;
2177 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
2179 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
2180 return FALSE;
2182 else if (info->flags & DF_BIND_NOW)
2184 if (! elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
2185 return FALSE;
2188 if (info->flags_1)
2190 if (info->executable)
2191 info->flags_1 &= ~ (DF_1_INITFIRST
2192 | DF_1_NODELETE
2193 | DF_1_NOOPEN);
2194 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
2195 return FALSE;
2198 /* Work out the size of the version reference section. */
2200 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2201 BFD_ASSERT (s != NULL);
2203 struct elf_find_verdep_info sinfo;
2205 sinfo.output_bfd = output_bfd;
2206 sinfo.info = info;
2207 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2208 if (sinfo.vers == 0)
2209 sinfo.vers = 1;
2210 sinfo.failed = FALSE;
2212 elf_link_hash_traverse (elf_hash_table (info),
2213 _bfd_elf_link_find_version_dependencies,
2214 &sinfo);
2216 if (elf_tdata (output_bfd)->verref == NULL)
2217 _bfd_strip_section_from_output (info, s);
2218 else
2220 Elf_Internal_Verneed *t;
2221 unsigned int size;
2222 unsigned int crefs;
2223 bfd_byte *p;
2225 /* Build the version definition section. */
2226 size = 0;
2227 crefs = 0;
2228 for (t = elf_tdata (output_bfd)->verref;
2229 t != NULL;
2230 t = t->vn_nextref)
2232 Elf_Internal_Vernaux *a;
2234 size += sizeof (Elf_External_Verneed);
2235 ++crefs;
2236 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2237 size += sizeof (Elf_External_Vernaux);
2240 s->_raw_size = size;
2241 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2242 if (s->contents == NULL)
2243 return FALSE;
2245 p = s->contents;
2246 for (t = elf_tdata (output_bfd)->verref;
2247 t != NULL;
2248 t = t->vn_nextref)
2250 unsigned int caux;
2251 Elf_Internal_Vernaux *a;
2252 bfd_size_type indx;
2254 caux = 0;
2255 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2256 ++caux;
2258 t->vn_version = VER_NEED_CURRENT;
2259 t->vn_cnt = caux;
2260 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2261 elf_dt_name (t->vn_bfd) != NULL
2262 ? elf_dt_name (t->vn_bfd)
2263 : basename (t->vn_bfd->filename),
2264 FALSE);
2265 if (indx == (bfd_size_type) -1)
2266 return FALSE;
2267 t->vn_file = indx;
2268 t->vn_aux = sizeof (Elf_External_Verneed);
2269 if (t->vn_nextref == NULL)
2270 t->vn_next = 0;
2271 else
2272 t->vn_next = (sizeof (Elf_External_Verneed)
2273 + caux * sizeof (Elf_External_Vernaux));
2275 _bfd_elf_swap_verneed_out (output_bfd, t,
2276 (Elf_External_Verneed *) p);
2277 p += sizeof (Elf_External_Verneed);
2279 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2281 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2282 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2283 a->vna_nodename, FALSE);
2284 if (indx == (bfd_size_type) -1)
2285 return FALSE;
2286 a->vna_name = indx;
2287 if (a->vna_nextptr == NULL)
2288 a->vna_next = 0;
2289 else
2290 a->vna_next = sizeof (Elf_External_Vernaux);
2292 _bfd_elf_swap_vernaux_out (output_bfd, a,
2293 (Elf_External_Vernaux *) p);
2294 p += sizeof (Elf_External_Vernaux);
2298 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2299 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2300 return FALSE;
2302 elf_tdata (output_bfd)->cverrefs = crefs;
2306 /* Assign dynsym indicies. In a shared library we generate a
2307 section symbol for each output section, which come first.
2308 Next come all of the back-end allocated local dynamic syms,
2309 followed by the rest of the global symbols. */
2311 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2313 /* Work out the size of the symbol version section. */
2314 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2315 BFD_ASSERT (s != NULL);
2316 if (dynsymcount == 0
2317 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2319 _bfd_strip_section_from_output (info, s);
2320 /* The DYNSYMCOUNT might have changed if we were going to
2321 output a dynamic symbol table entry for S. */
2322 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2324 else
2326 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2327 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2328 if (s->contents == NULL)
2329 return FALSE;
2331 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2332 return FALSE;
2335 /* Set the size of the .dynsym and .hash sections. We counted
2336 the number of dynamic symbols in elf_link_add_object_symbols.
2337 We will build the contents of .dynsym and .hash when we build
2338 the final symbol table, because until then we do not know the
2339 correct value to give the symbols. We built the .dynstr
2340 section as we went along in elf_link_add_object_symbols. */
2341 s = bfd_get_section_by_name (dynobj, ".dynsym");
2342 BFD_ASSERT (s != NULL);
2343 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2344 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2345 if (s->contents == NULL && s->_raw_size != 0)
2346 return FALSE;
2348 if (dynsymcount != 0)
2350 Elf_Internal_Sym isym;
2352 /* The first entry in .dynsym is a dummy symbol. */
2353 isym.st_value = 0;
2354 isym.st_size = 0;
2355 isym.st_name = 0;
2356 isym.st_info = 0;
2357 isym.st_other = 0;
2358 isym.st_shndx = 0;
2359 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
2362 /* Compute the size of the hashing table. As a side effect this
2363 computes the hash values for all the names we export. */
2364 bucketcount = compute_bucket_count (info);
2366 s = bfd_get_section_by_name (dynobj, ".hash");
2367 BFD_ASSERT (s != NULL);
2368 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2369 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
2370 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2371 if (s->contents == NULL)
2372 return FALSE;
2374 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2375 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
2376 s->contents + hash_entry_size);
2378 elf_hash_table (info)->bucketcount = bucketcount;
2380 s = bfd_get_section_by_name (dynobj, ".dynstr");
2381 BFD_ASSERT (s != NULL);
2383 elf_finalize_dynstr (output_bfd, info);
2385 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2387 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
2388 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2389 return FALSE;
2392 return TRUE;
2395 /* This function is used to adjust offsets into .dynstr for
2396 dynamic symbols. This is called via elf_link_hash_traverse. */
2398 static bfd_boolean
2399 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2401 struct elf_strtab_hash *dynstr = data;
2403 if (h->root.type == bfd_link_hash_warning)
2404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2406 if (h->dynindx != -1)
2407 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2408 return TRUE;
2411 /* Assign string offsets in .dynstr, update all structures referencing
2412 them. */
2414 static bfd_boolean
2415 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2417 struct elf_link_local_dynamic_entry *entry;
2418 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2419 bfd *dynobj = elf_hash_table (info)->dynobj;
2420 asection *sdyn;
2421 bfd_size_type size;
2422 Elf_External_Dyn *dyncon, *dynconend;
2424 _bfd_elf_strtab_finalize (dynstr);
2425 size = _bfd_elf_strtab_size (dynstr);
2427 /* Update all .dynamic entries referencing .dynstr strings. */
2428 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2429 BFD_ASSERT (sdyn != NULL);
2431 dyncon = (Elf_External_Dyn *) sdyn->contents;
2432 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2433 sdyn->_raw_size);
2434 for (; dyncon < dynconend; dyncon++)
2436 Elf_Internal_Dyn dyn;
2438 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2439 switch (dyn.d_tag)
2441 case DT_STRSZ:
2442 dyn.d_un.d_val = size;
2443 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2444 break;
2445 case DT_NEEDED:
2446 case DT_SONAME:
2447 case DT_RPATH:
2448 case DT_RUNPATH:
2449 case DT_FILTER:
2450 case DT_AUXILIARY:
2451 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2452 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2453 break;
2454 default:
2455 break;
2459 /* Now update local dynamic symbols. */
2460 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2461 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2462 entry->isym.st_name);
2464 /* And the rest of dynamic symbols. */
2465 elf_link_hash_traverse (elf_hash_table (info),
2466 elf_adjust_dynstr_offsets, dynstr);
2468 /* Adjust version definitions. */
2469 if (elf_tdata (output_bfd)->cverdefs)
2471 asection *s;
2472 bfd_byte *p;
2473 bfd_size_type i;
2474 Elf_Internal_Verdef def;
2475 Elf_Internal_Verdaux defaux;
2477 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2478 p = (bfd_byte *) s->contents;
2481 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2482 &def);
2483 p += sizeof (Elf_External_Verdef);
2484 for (i = 0; i < def.vd_cnt; ++i)
2486 _bfd_elf_swap_verdaux_in (output_bfd,
2487 (Elf_External_Verdaux *) p, &defaux);
2488 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2489 defaux.vda_name);
2490 _bfd_elf_swap_verdaux_out (output_bfd,
2491 &defaux, (Elf_External_Verdaux *) p);
2492 p += sizeof (Elf_External_Verdaux);
2495 while (def.vd_next);
2498 /* Adjust version references. */
2499 if (elf_tdata (output_bfd)->verref)
2501 asection *s;
2502 bfd_byte *p;
2503 bfd_size_type i;
2504 Elf_Internal_Verneed need;
2505 Elf_Internal_Vernaux needaux;
2507 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2508 p = (bfd_byte *) s->contents;
2511 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2512 &need);
2513 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2514 _bfd_elf_swap_verneed_out (output_bfd, &need,
2515 (Elf_External_Verneed *) p);
2516 p += sizeof (Elf_External_Verneed);
2517 for (i = 0; i < need.vn_cnt; ++i)
2519 _bfd_elf_swap_vernaux_in (output_bfd,
2520 (Elf_External_Vernaux *) p, &needaux);
2521 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2522 needaux.vna_name);
2523 _bfd_elf_swap_vernaux_out (output_bfd,
2524 &needaux,
2525 (Elf_External_Vernaux *) p);
2526 p += sizeof (Elf_External_Vernaux);
2529 while (need.vn_next);
2532 return TRUE;
2535 /* Final phase of ELF linker. */
2537 /* A structure we use to avoid passing large numbers of arguments. */
2539 struct elf_final_link_info
2541 /* General link information. */
2542 struct bfd_link_info *info;
2543 /* Output BFD. */
2544 bfd *output_bfd;
2545 /* Symbol string table. */
2546 struct bfd_strtab_hash *symstrtab;
2547 /* .dynsym section. */
2548 asection *dynsym_sec;
2549 /* .hash section. */
2550 asection *hash_sec;
2551 /* symbol version section (.gnu.version). */
2552 asection *symver_sec;
2553 /* Buffer large enough to hold contents of any section. */
2554 bfd_byte *contents;
2555 /* Buffer large enough to hold external relocs of any section. */
2556 void *external_relocs;
2557 /* Buffer large enough to hold internal relocs of any section. */
2558 Elf_Internal_Rela *internal_relocs;
2559 /* Buffer large enough to hold external local symbols of any input
2560 BFD. */
2561 Elf_External_Sym *external_syms;
2562 /* And a buffer for symbol section indices. */
2563 Elf_External_Sym_Shndx *locsym_shndx;
2564 /* Buffer large enough to hold internal local symbols of any input
2565 BFD. */
2566 Elf_Internal_Sym *internal_syms;
2567 /* Array large enough to hold a symbol index for each local symbol
2568 of any input BFD. */
2569 long *indices;
2570 /* Array large enough to hold a section pointer for each local
2571 symbol of any input BFD. */
2572 asection **sections;
2573 /* Buffer to hold swapped out symbols. */
2574 Elf_External_Sym *symbuf;
2575 /* And one for symbol section indices. */
2576 Elf_External_Sym_Shndx *symshndxbuf;
2577 /* Number of swapped out symbols in buffer. */
2578 size_t symbuf_count;
2579 /* Number of symbols which fit in symbuf. */
2580 size_t symbuf_size;
2581 /* And same for symshndxbuf. */
2582 size_t shndxbuf_size;
2585 static bfd_boolean elf_link_output_sym
2586 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2587 struct elf_link_hash_entry *);
2588 static bfd_boolean elf_link_flush_output_syms
2589 (struct elf_final_link_info *);
2590 static bfd_boolean elf_link_output_extsym
2591 (struct elf_link_hash_entry *, void *);
2592 static bfd_boolean elf_link_input_bfd
2593 (struct elf_final_link_info *, bfd *);
2594 static bfd_boolean elf_reloc_link_order
2595 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
2597 /* This struct is used to pass information to elf_link_output_extsym. */
2599 struct elf_outext_info
2601 bfd_boolean failed;
2602 bfd_boolean localsyms;
2603 struct elf_final_link_info *finfo;
2606 /* When performing a relocatable link, the input relocations are
2607 preserved. But, if they reference global symbols, the indices
2608 referenced must be updated. Update all the relocations in
2609 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2611 static void
2612 elf_link_adjust_relocs (bfd *abfd,
2613 Elf_Internal_Shdr *rel_hdr,
2614 unsigned int count,
2615 struct elf_link_hash_entry **rel_hash)
2617 unsigned int i;
2618 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2619 bfd_byte *erela;
2620 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2621 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2623 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2625 swap_in = bed->s->swap_reloc_in;
2626 swap_out = bed->s->swap_reloc_out;
2628 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2630 swap_in = bed->s->swap_reloca_in;
2631 swap_out = bed->s->swap_reloca_out;
2633 else
2634 abort ();
2636 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2637 abort ();
2639 erela = rel_hdr->contents;
2640 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2642 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2643 unsigned int j;
2645 if (*rel_hash == NULL)
2646 continue;
2648 BFD_ASSERT ((*rel_hash)->indx >= 0);
2650 (*swap_in) (abfd, erela, irela);
2651 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2652 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2653 ELF_R_TYPE (irela[j].r_info));
2654 (*swap_out) (abfd, irela, erela);
2658 struct elf_link_sort_rela
2660 bfd_vma offset;
2661 enum elf_reloc_type_class type;
2662 /* We use this as an array of size int_rels_per_ext_rel. */
2663 Elf_Internal_Rela rela[1];
2666 static int
2667 elf_link_sort_cmp1 (const void *A, const void *B)
2669 const struct elf_link_sort_rela *a = A;
2670 const struct elf_link_sort_rela *b = B;
2671 int relativea, relativeb;
2673 relativea = a->type == reloc_class_relative;
2674 relativeb = b->type == reloc_class_relative;
2676 if (relativea < relativeb)
2677 return 1;
2678 if (relativea > relativeb)
2679 return -1;
2680 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2681 return -1;
2682 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2683 return 1;
2684 if (a->rela->r_offset < b->rela->r_offset)
2685 return -1;
2686 if (a->rela->r_offset > b->rela->r_offset)
2687 return 1;
2688 return 0;
2691 static int
2692 elf_link_sort_cmp2 (const void *A, const void *B)
2694 const struct elf_link_sort_rela *a = A;
2695 const struct elf_link_sort_rela *b = B;
2696 int copya, copyb;
2698 if (a->offset < b->offset)
2699 return -1;
2700 if (a->offset > b->offset)
2701 return 1;
2702 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2703 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
2704 if (copya < copyb)
2705 return -1;
2706 if (copya > copyb)
2707 return 1;
2708 if (a->rela->r_offset < b->rela->r_offset)
2709 return -1;
2710 if (a->rela->r_offset > b->rela->r_offset)
2711 return 1;
2712 return 0;
2715 static size_t
2716 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
2718 asection *reldyn;
2719 bfd_size_type count, size;
2720 size_t i, ret, sort_elt, ext_size;
2721 bfd_byte *sort, *s_non_relative, *p;
2722 struct elf_link_sort_rela *sq;
2723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2724 int i2e = bed->s->int_rels_per_ext_rel;
2725 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2726 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2727 struct bfd_link_order *lo;
2729 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2730 if (reldyn == NULL || reldyn->_raw_size == 0)
2732 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2733 if (reldyn == NULL || reldyn->_raw_size == 0)
2734 return 0;
2735 ext_size = sizeof (Elf_External_Rel);
2736 swap_in = bed->s->swap_reloc_in;
2737 swap_out = bed->s->swap_reloc_out;
2739 else
2741 ext_size = sizeof (Elf_External_Rela);
2742 swap_in = bed->s->swap_reloca_in;
2743 swap_out = bed->s->swap_reloca_out;
2745 count = reldyn->_raw_size / ext_size;
2747 size = 0;
2748 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2749 if (lo->type == bfd_indirect_link_order)
2751 asection *o = lo->u.indirect.section;
2752 size += o->_raw_size;
2755 if (size != reldyn->_raw_size)
2756 return 0;
2758 sort_elt = (sizeof (struct elf_link_sort_rela)
2759 + (i2e - 1) * sizeof (Elf_Internal_Rela));
2760 sort = bfd_zmalloc (sort_elt * count);
2761 if (sort == NULL)
2763 (*info->callbacks->warning)
2764 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
2765 return 0;
2768 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2769 if (lo->type == bfd_indirect_link_order)
2771 bfd_byte *erel, *erelend;
2772 asection *o = lo->u.indirect.section;
2774 erel = o->contents;
2775 erelend = o->contents + o->_raw_size;
2776 p = sort + o->output_offset / ext_size * sort_elt;
2777 while (erel < erelend)
2779 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2780 (*swap_in) (abfd, erel, s->rela);
2781 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
2782 p += sort_elt;
2783 erel += ext_size;
2787 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
2789 for (i = 0, p = sort; i < count; i++, p += sort_elt)
2791 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2792 if (s->type != reloc_class_relative)
2793 break;
2795 ret = i;
2796 s_non_relative = p;
2798 sq = (struct elf_link_sort_rela *) s_non_relative;
2799 for (; i < count; i++, p += sort_elt)
2801 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
2802 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
2803 sq = sp;
2804 sp->offset = sq->rela->r_offset;
2807 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
2809 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2810 if (lo->type == bfd_indirect_link_order)
2812 bfd_byte *erel, *erelend;
2813 asection *o = lo->u.indirect.section;
2815 erel = o->contents;
2816 erelend = o->contents + o->_raw_size;
2817 p = sort + o->output_offset / ext_size * sort_elt;
2818 while (erel < erelend)
2820 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2821 (*swap_out) (abfd, s->rela, erel);
2822 p += sort_elt;
2823 erel += ext_size;
2827 free (sort);
2828 *psec = reldyn;
2829 return ret;
2832 /* Do the final step of an ELF link. */
2834 bfd_boolean
2835 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
2837 bfd_boolean dynamic;
2838 bfd_boolean emit_relocs;
2839 bfd *dynobj;
2840 struct elf_final_link_info finfo;
2841 register asection *o;
2842 register struct bfd_link_order *p;
2843 register bfd *sub;
2844 bfd_size_type max_contents_size;
2845 bfd_size_type max_external_reloc_size;
2846 bfd_size_type max_internal_reloc_count;
2847 bfd_size_type max_sym_count;
2848 bfd_size_type max_sym_shndx_count;
2849 file_ptr off;
2850 Elf_Internal_Sym elfsym;
2851 unsigned int i;
2852 Elf_Internal_Shdr *symtab_hdr;
2853 Elf_Internal_Shdr *symtab_shndx_hdr;
2854 Elf_Internal_Shdr *symstrtab_hdr;
2855 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2856 struct elf_outext_info eoinfo;
2857 bfd_boolean merged;
2858 size_t relativecount = 0;
2859 asection *reldyn = 0;
2860 bfd_size_type amt;
2862 if (! is_elf_hash_table (info->hash))
2863 return FALSE;
2865 if (info->shared)
2866 abfd->flags |= DYNAMIC;
2868 dynamic = elf_hash_table (info)->dynamic_sections_created;
2869 dynobj = elf_hash_table (info)->dynobj;
2871 emit_relocs = (info->relocatable
2872 || info->emitrelocations
2873 || bed->elf_backend_emit_relocs);
2875 finfo.info = info;
2876 finfo.output_bfd = abfd;
2877 finfo.symstrtab = elf_stringtab_init ();
2878 if (finfo.symstrtab == NULL)
2879 return FALSE;
2881 if (! dynamic)
2883 finfo.dynsym_sec = NULL;
2884 finfo.hash_sec = NULL;
2885 finfo.symver_sec = NULL;
2887 else
2889 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
2890 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
2891 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
2892 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
2893 /* Note that it is OK if symver_sec is NULL. */
2896 finfo.contents = NULL;
2897 finfo.external_relocs = NULL;
2898 finfo.internal_relocs = NULL;
2899 finfo.external_syms = NULL;
2900 finfo.locsym_shndx = NULL;
2901 finfo.internal_syms = NULL;
2902 finfo.indices = NULL;
2903 finfo.sections = NULL;
2904 finfo.symbuf = NULL;
2905 finfo.symshndxbuf = NULL;
2906 finfo.symbuf_count = 0;
2907 finfo.shndxbuf_size = 0;
2909 /* Count up the number of relocations we will output for each output
2910 section, so that we know the sizes of the reloc sections. We
2911 also figure out some maximum sizes. */
2912 max_contents_size = 0;
2913 max_external_reloc_size = 0;
2914 max_internal_reloc_count = 0;
2915 max_sym_count = 0;
2916 max_sym_shndx_count = 0;
2917 merged = FALSE;
2918 for (o = abfd->sections; o != NULL; o = o->next)
2920 struct bfd_elf_section_data *esdo = elf_section_data (o);
2921 o->reloc_count = 0;
2923 for (p = o->link_order_head; p != NULL; p = p->next)
2925 unsigned int reloc_count = 0;
2926 struct bfd_elf_section_data *esdi = NULL;
2927 unsigned int *rel_count1;
2929 if (p->type == bfd_section_reloc_link_order
2930 || p->type == bfd_symbol_reloc_link_order)
2931 reloc_count = 1;
2932 else if (p->type == bfd_indirect_link_order)
2934 asection *sec;
2936 sec = p->u.indirect.section;
2937 esdi = elf_section_data (sec);
2939 /* Mark all sections which are to be included in the
2940 link. This will normally be every section. We need
2941 to do this so that we can identify any sections which
2942 the linker has decided to not include. */
2943 sec->linker_mark = TRUE;
2945 if (sec->flags & SEC_MERGE)
2946 merged = TRUE;
2948 if (info->relocatable || info->emitrelocations)
2949 reloc_count = sec->reloc_count;
2950 else if (bed->elf_backend_count_relocs)
2952 Elf_Internal_Rela * relocs;
2954 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2955 info->keep_memory);
2957 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
2959 if (elf_section_data (o)->relocs != relocs)
2960 free (relocs);
2963 if (sec->_raw_size > max_contents_size)
2964 max_contents_size = sec->_raw_size;
2965 if (sec->_cooked_size > max_contents_size)
2966 max_contents_size = sec->_cooked_size;
2968 /* We are interested in just local symbols, not all
2969 symbols. */
2970 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
2971 && (sec->owner->flags & DYNAMIC) == 0)
2973 size_t sym_count;
2975 if (elf_bad_symtab (sec->owner))
2976 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
2977 / sizeof (Elf_External_Sym));
2978 else
2979 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
2981 if (sym_count > max_sym_count)
2982 max_sym_count = sym_count;
2984 if (sym_count > max_sym_shndx_count
2985 && elf_symtab_shndx (sec->owner) != 0)
2986 max_sym_shndx_count = sym_count;
2988 if ((sec->flags & SEC_RELOC) != 0)
2990 size_t ext_size;
2992 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
2993 if (ext_size > max_external_reloc_size)
2994 max_external_reloc_size = ext_size;
2995 if (sec->reloc_count > max_internal_reloc_count)
2996 max_internal_reloc_count = sec->reloc_count;
3001 if (reloc_count == 0)
3002 continue;
3004 o->reloc_count += reloc_count;
3006 /* MIPS may have a mix of REL and RELA relocs on sections.
3007 To support this curious ABI we keep reloc counts in
3008 elf_section_data too. We must be careful to add the
3009 relocations from the input section to the right output
3010 count. FIXME: Get rid of one count. We have
3011 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3012 rel_count1 = &esdo->rel_count;
3013 if (esdi != NULL)
3015 bfd_boolean same_size;
3016 bfd_size_type entsize1;
3018 entsize1 = esdi->rel_hdr.sh_entsize;
3019 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3020 || entsize1 == sizeof (Elf_External_Rela));
3021 same_size = (!o->use_rela_p
3022 == (entsize1 == sizeof (Elf_External_Rel)));
3024 if (!same_size)
3025 rel_count1 = &esdo->rel_count2;
3027 if (esdi->rel_hdr2 != NULL)
3029 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3030 unsigned int alt_count;
3031 unsigned int *rel_count2;
3033 BFD_ASSERT (entsize2 != entsize1
3034 && (entsize2 == sizeof (Elf_External_Rel)
3035 || entsize2 == sizeof (Elf_External_Rela)));
3037 rel_count2 = &esdo->rel_count2;
3038 if (!same_size)
3039 rel_count2 = &esdo->rel_count;
3041 /* The following is probably too simplistic if the
3042 backend counts output relocs unusually. */
3043 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3044 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3045 *rel_count2 += alt_count;
3046 reloc_count -= alt_count;
3049 *rel_count1 += reloc_count;
3052 if (o->reloc_count > 0)
3053 o->flags |= SEC_RELOC;
3054 else
3056 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3057 set it (this is probably a bug) and if it is set
3058 assign_section_numbers will create a reloc section. */
3059 o->flags &=~ SEC_RELOC;
3062 /* If the SEC_ALLOC flag is not set, force the section VMA to
3063 zero. This is done in elf_fake_sections as well, but forcing
3064 the VMA to 0 here will ensure that relocs against these
3065 sections are handled correctly. */
3066 if ((o->flags & SEC_ALLOC) == 0
3067 && ! o->user_set_vma)
3068 o->vma = 0;
3071 if (! info->relocatable && merged)
3072 elf_link_hash_traverse (elf_hash_table (info),
3073 _bfd_elf_link_sec_merge_syms, abfd);
3075 /* Figure out the file positions for everything but the symbol table
3076 and the relocs. We set symcount to force assign_section_numbers
3077 to create a symbol table. */
3078 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3079 BFD_ASSERT (! abfd->output_has_begun);
3080 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3081 goto error_return;
3083 /* That created the reloc sections. Set their sizes, and assign
3084 them file positions, and allocate some buffers. */
3085 for (o = abfd->sections; o != NULL; o = o->next)
3087 if ((o->flags & SEC_RELOC) != 0)
3089 if (!(_bfd_elf_link_size_reloc_section
3090 (abfd, &elf_section_data (o)->rel_hdr, o)))
3091 goto error_return;
3093 if (elf_section_data (o)->rel_hdr2
3094 && !(_bfd_elf_link_size_reloc_section
3095 (abfd, elf_section_data (o)->rel_hdr2, o)))
3096 goto error_return;
3099 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3100 to count upwards while actually outputting the relocations. */
3101 elf_section_data (o)->rel_count = 0;
3102 elf_section_data (o)->rel_count2 = 0;
3105 _bfd_elf_assign_file_positions_for_relocs (abfd);
3107 /* We have now assigned file positions for all the sections except
3108 .symtab and .strtab. We start the .symtab section at the current
3109 file position, and write directly to it. We build the .strtab
3110 section in memory. */
3111 bfd_get_symcount (abfd) = 0;
3112 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3113 /* sh_name is set in prep_headers. */
3114 symtab_hdr->sh_type = SHT_SYMTAB;
3115 /* sh_flags, sh_addr and sh_size all start off zero. */
3116 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3117 /* sh_link is set in assign_section_numbers. */
3118 /* sh_info is set below. */
3119 /* sh_offset is set just below. */
3120 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3122 off = elf_tdata (abfd)->next_file_pos;
3123 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3125 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3126 incorrect. We do not yet know the size of the .symtab section.
3127 We correct next_file_pos below, after we do know the size. */
3129 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3130 continuously seeking to the right position in the file. */
3131 if (! info->keep_memory || max_sym_count < 20)
3132 finfo.symbuf_size = 20;
3133 else
3134 finfo.symbuf_size = max_sym_count;
3135 amt = finfo.symbuf_size;
3136 amt *= sizeof (Elf_External_Sym);
3137 finfo.symbuf = bfd_malloc (amt);
3138 if (finfo.symbuf == NULL)
3139 goto error_return;
3140 if (elf_numsections (abfd) > SHN_LORESERVE)
3142 /* Wild guess at number of output symbols. realloc'd as needed. */
3143 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3144 finfo.shndxbuf_size = amt;
3145 amt *= sizeof (Elf_External_Sym_Shndx);
3146 finfo.symshndxbuf = bfd_zmalloc (amt);
3147 if (finfo.symshndxbuf == NULL)
3148 goto error_return;
3151 /* Start writing out the symbol table. The first symbol is always a
3152 dummy symbol. */
3153 if (info->strip != strip_all
3154 || emit_relocs)
3156 elfsym.st_value = 0;
3157 elfsym.st_size = 0;
3158 elfsym.st_info = 0;
3159 elfsym.st_other = 0;
3160 elfsym.st_shndx = SHN_UNDEF;
3161 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3162 NULL))
3163 goto error_return;
3166 #if 0
3167 /* Some standard ELF linkers do this, but we don't because it causes
3168 bootstrap comparison failures. */
3169 /* Output a file symbol for the output file as the second symbol.
3170 We output this even if we are discarding local symbols, although
3171 I'm not sure if this is correct. */
3172 elfsym.st_value = 0;
3173 elfsym.st_size = 0;
3174 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3175 elfsym.st_other = 0;
3176 elfsym.st_shndx = SHN_ABS;
3177 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3178 &elfsym, bfd_abs_section_ptr, NULL))
3179 goto error_return;
3180 #endif
3182 /* Output a symbol for each section. We output these even if we are
3183 discarding local symbols, since they are used for relocs. These
3184 symbols have no names. We store the index of each one in the
3185 index field of the section, so that we can find it again when
3186 outputting relocs. */
3187 if (info->strip != strip_all
3188 || emit_relocs)
3190 elfsym.st_size = 0;
3191 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3192 elfsym.st_other = 0;
3193 for (i = 1; i < elf_numsections (abfd); i++)
3195 o = section_from_elf_index (abfd, i);
3196 if (o != NULL)
3197 o->target_index = bfd_get_symcount (abfd);
3198 elfsym.st_shndx = i;
3199 if (info->relocatable || o == NULL)
3200 elfsym.st_value = 0;
3201 else
3202 elfsym.st_value = o->vma;
3203 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
3204 goto error_return;
3205 if (i == SHN_LORESERVE - 1)
3206 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3210 /* Allocate some memory to hold information read in from the input
3211 files. */
3212 if (max_contents_size != 0)
3214 finfo.contents = bfd_malloc (max_contents_size);
3215 if (finfo.contents == NULL)
3216 goto error_return;
3219 if (max_external_reloc_size != 0)
3221 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
3222 if (finfo.external_relocs == NULL)
3223 goto error_return;
3226 if (max_internal_reloc_count != 0)
3228 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3229 amt *= sizeof (Elf_Internal_Rela);
3230 finfo.internal_relocs = bfd_malloc (amt);
3231 if (finfo.internal_relocs == NULL)
3232 goto error_return;
3235 if (max_sym_count != 0)
3237 amt = max_sym_count * sizeof (Elf_External_Sym);
3238 finfo.external_syms = bfd_malloc (amt);
3239 if (finfo.external_syms == NULL)
3240 goto error_return;
3242 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3243 finfo.internal_syms = bfd_malloc (amt);
3244 if (finfo.internal_syms == NULL)
3245 goto error_return;
3247 amt = max_sym_count * sizeof (long);
3248 finfo.indices = bfd_malloc (amt);
3249 if (finfo.indices == NULL)
3250 goto error_return;
3252 amt = max_sym_count * sizeof (asection *);
3253 finfo.sections = bfd_malloc (amt);
3254 if (finfo.sections == NULL)
3255 goto error_return;
3258 if (max_sym_shndx_count != 0)
3260 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3261 finfo.locsym_shndx = bfd_malloc (amt);
3262 if (finfo.locsym_shndx == NULL)
3263 goto error_return;
3266 if (elf_hash_table (info)->tls_sec)
3268 bfd_vma base, end = 0;
3269 asection *sec;
3271 for (sec = elf_hash_table (info)->tls_sec;
3272 sec && (sec->flags & SEC_THREAD_LOCAL);
3273 sec = sec->next)
3275 bfd_vma size = sec->_raw_size;
3277 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3279 struct bfd_link_order *o;
3281 for (o = sec->link_order_head; o != NULL; o = o->next)
3282 if (size < o->offset + o->size)
3283 size = o->offset + o->size;
3285 end = sec->vma + size;
3287 base = elf_hash_table (info)->tls_sec->vma;
3288 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3289 elf_hash_table (info)->tls_size = end - base;
3292 /* Since ELF permits relocations to be against local symbols, we
3293 must have the local symbols available when we do the relocations.
3294 Since we would rather only read the local symbols once, and we
3295 would rather not keep them in memory, we handle all the
3296 relocations for a single input file at the same time.
3298 Unfortunately, there is no way to know the total number of local
3299 symbols until we have seen all of them, and the local symbol
3300 indices precede the global symbol indices. This means that when
3301 we are generating relocatable output, and we see a reloc against
3302 a global symbol, we can not know the symbol index until we have
3303 finished examining all the local symbols to see which ones we are
3304 going to output. To deal with this, we keep the relocations in
3305 memory, and don't output them until the end of the link. This is
3306 an unfortunate waste of memory, but I don't see a good way around
3307 it. Fortunately, it only happens when performing a relocatable
3308 link, which is not the common case. FIXME: If keep_memory is set
3309 we could write the relocs out and then read them again; I don't
3310 know how bad the memory loss will be. */
3312 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3313 sub->output_has_begun = FALSE;
3314 for (o = abfd->sections; o != NULL; o = o->next)
3316 for (p = o->link_order_head; p != NULL; p = p->next)
3318 if (p->type == bfd_indirect_link_order
3319 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3320 == bfd_target_elf_flavour)
3321 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3323 if (! sub->output_has_begun)
3325 if (! elf_link_input_bfd (&finfo, sub))
3326 goto error_return;
3327 sub->output_has_begun = TRUE;
3330 else if (p->type == bfd_section_reloc_link_order
3331 || p->type == bfd_symbol_reloc_link_order)
3333 if (! elf_reloc_link_order (abfd, info, o, p))
3334 goto error_return;
3336 else
3338 if (! _bfd_default_link_order (abfd, info, o, p))
3339 goto error_return;
3344 /* Output any global symbols that got converted to local in a
3345 version script or due to symbol visibility. We do this in a
3346 separate step since ELF requires all local symbols to appear
3347 prior to any global symbols. FIXME: We should only do this if
3348 some global symbols were, in fact, converted to become local.
3349 FIXME: Will this work correctly with the Irix 5 linker? */
3350 eoinfo.failed = FALSE;
3351 eoinfo.finfo = &finfo;
3352 eoinfo.localsyms = TRUE;
3353 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3354 &eoinfo);
3355 if (eoinfo.failed)
3356 return FALSE;
3358 /* That wrote out all the local symbols. Finish up the symbol table
3359 with the global symbols. Even if we want to strip everything we
3360 can, we still need to deal with those global symbols that got
3361 converted to local in a version script. */
3363 /* The sh_info field records the index of the first non local symbol. */
3364 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3366 if (dynamic
3367 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3369 Elf_Internal_Sym sym;
3370 Elf_External_Sym *dynsym =
3371 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3372 long last_local = 0;
3374 /* Write out the section symbols for the output sections. */
3375 if (info->shared)
3377 asection *s;
3379 sym.st_size = 0;
3380 sym.st_name = 0;
3381 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3382 sym.st_other = 0;
3384 for (s = abfd->sections; s != NULL; s = s->next)
3386 int indx;
3387 Elf_External_Sym *dest;
3389 indx = elf_section_data (s)->this_idx;
3390 BFD_ASSERT (indx > 0);
3391 sym.st_shndx = indx;
3392 sym.st_value = s->vma;
3393 dest = dynsym + elf_section_data (s)->dynindx;
3394 elf_swap_symbol_out (abfd, &sym, dest, 0);
3397 last_local = bfd_count_sections (abfd);
3400 /* Write out the local dynsyms. */
3401 if (elf_hash_table (info)->dynlocal)
3403 struct elf_link_local_dynamic_entry *e;
3404 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3406 asection *s;
3407 Elf_External_Sym *dest;
3409 sym.st_size = e->isym.st_size;
3410 sym.st_other = e->isym.st_other;
3412 /* Copy the internal symbol as is.
3413 Note that we saved a word of storage and overwrote
3414 the original st_name with the dynstr_index. */
3415 sym = e->isym;
3417 if (e->isym.st_shndx != SHN_UNDEF
3418 && (e->isym.st_shndx < SHN_LORESERVE
3419 || e->isym.st_shndx > SHN_HIRESERVE))
3421 s = bfd_section_from_elf_index (e->input_bfd,
3422 e->isym.st_shndx);
3424 sym.st_shndx =
3425 elf_section_data (s->output_section)->this_idx;
3426 sym.st_value = (s->output_section->vma
3427 + s->output_offset
3428 + e->isym.st_value);
3431 if (last_local < e->dynindx)
3432 last_local = e->dynindx;
3434 dest = dynsym + e->dynindx;
3435 elf_swap_symbol_out (abfd, &sym, dest, 0);
3439 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3440 last_local + 1;
3443 /* We get the global symbols from the hash table. */
3444 eoinfo.failed = FALSE;
3445 eoinfo.localsyms = FALSE;
3446 eoinfo.finfo = &finfo;
3447 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3448 &eoinfo);
3449 if (eoinfo.failed)
3450 return FALSE;
3452 /* If backend needs to output some symbols not present in the hash
3453 table, do it now. */
3454 if (bed->elf_backend_output_arch_syms)
3456 typedef bfd_boolean (*out_sym_func)
3457 (void *, const char *, Elf_Internal_Sym *, asection *,
3458 struct elf_link_hash_entry *);
3460 if (! ((*bed->elf_backend_output_arch_syms)
3461 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
3462 return FALSE;
3465 /* Flush all symbols to the file. */
3466 if (! elf_link_flush_output_syms (&finfo))
3467 return FALSE;
3469 /* Now we know the size of the symtab section. */
3470 off += symtab_hdr->sh_size;
3472 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3473 if (symtab_shndx_hdr->sh_name != 0)
3475 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3476 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3477 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3478 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3479 symtab_shndx_hdr->sh_size = amt;
3481 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3482 off, TRUE);
3484 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3485 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
3486 return FALSE;
3490 /* Finish up and write out the symbol string table (.strtab)
3491 section. */
3492 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3493 /* sh_name was set in prep_headers. */
3494 symstrtab_hdr->sh_type = SHT_STRTAB;
3495 symstrtab_hdr->sh_flags = 0;
3496 symstrtab_hdr->sh_addr = 0;
3497 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3498 symstrtab_hdr->sh_entsize = 0;
3499 symstrtab_hdr->sh_link = 0;
3500 symstrtab_hdr->sh_info = 0;
3501 /* sh_offset is set just below. */
3502 symstrtab_hdr->sh_addralign = 1;
3504 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3505 elf_tdata (abfd)->next_file_pos = off;
3507 if (bfd_get_symcount (abfd) > 0)
3509 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3510 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3511 return FALSE;
3514 /* Adjust the relocs to have the correct symbol indices. */
3515 for (o = abfd->sections; o != NULL; o = o->next)
3517 if ((o->flags & SEC_RELOC) == 0)
3518 continue;
3520 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3521 elf_section_data (o)->rel_count,
3522 elf_section_data (o)->rel_hashes);
3523 if (elf_section_data (o)->rel_hdr2 != NULL)
3524 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3525 elf_section_data (o)->rel_count2,
3526 (elf_section_data (o)->rel_hashes
3527 + elf_section_data (o)->rel_count));
3529 /* Set the reloc_count field to 0 to prevent write_relocs from
3530 trying to swap the relocs out itself. */
3531 o->reloc_count = 0;
3534 if (dynamic && info->combreloc && dynobj != NULL)
3535 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3537 /* If we are linking against a dynamic object, or generating a
3538 shared library, finish up the dynamic linking information. */
3539 if (dynamic)
3541 Elf_External_Dyn *dyncon, *dynconend;
3543 /* Fix up .dynamic entries. */
3544 o = bfd_get_section_by_name (dynobj, ".dynamic");
3545 BFD_ASSERT (o != NULL);
3547 dyncon = (Elf_External_Dyn *) o->contents;
3548 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3549 for (; dyncon < dynconend; dyncon++)
3551 Elf_Internal_Dyn dyn;
3552 const char *name;
3553 unsigned int type;
3555 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3557 switch (dyn.d_tag)
3559 default:
3560 break;
3561 case DT_NULL:
3562 if (relativecount > 0 && dyncon + 1 < dynconend)
3564 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3566 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3567 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3568 default: break;
3570 if (dyn.d_tag != DT_NULL)
3572 dyn.d_un.d_val = relativecount;
3573 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3574 relativecount = 0;
3577 break;
3578 case DT_INIT:
3579 name = info->init_function;
3580 goto get_sym;
3581 case DT_FINI:
3582 name = info->fini_function;
3583 get_sym:
3585 struct elf_link_hash_entry *h;
3587 h = elf_link_hash_lookup (elf_hash_table (info), name,
3588 FALSE, FALSE, TRUE);
3589 if (h != NULL
3590 && (h->root.type == bfd_link_hash_defined
3591 || h->root.type == bfd_link_hash_defweak))
3593 dyn.d_un.d_val = h->root.u.def.value;
3594 o = h->root.u.def.section;
3595 if (o->output_section != NULL)
3596 dyn.d_un.d_val += (o->output_section->vma
3597 + o->output_offset);
3598 else
3600 /* The symbol is imported from another shared
3601 library and does not apply to this one. */
3602 dyn.d_un.d_val = 0;
3605 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3608 break;
3610 case DT_PREINIT_ARRAYSZ:
3611 name = ".preinit_array";
3612 goto get_size;
3613 case DT_INIT_ARRAYSZ:
3614 name = ".init_array";
3615 goto get_size;
3616 case DT_FINI_ARRAYSZ:
3617 name = ".fini_array";
3618 get_size:
3619 o = bfd_get_section_by_name (abfd, name);
3620 if (o == NULL)
3622 (*_bfd_error_handler)
3623 (_("%s: could not find output section %s"),
3624 bfd_get_filename (abfd), name);
3625 goto error_return;
3627 if (o->_raw_size == 0)
3628 (*_bfd_error_handler)
3629 (_("warning: %s section has zero size"), name);
3630 dyn.d_un.d_val = o->_raw_size;
3631 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3632 break;
3634 case DT_PREINIT_ARRAY:
3635 name = ".preinit_array";
3636 goto get_vma;
3637 case DT_INIT_ARRAY:
3638 name = ".init_array";
3639 goto get_vma;
3640 case DT_FINI_ARRAY:
3641 name = ".fini_array";
3642 goto get_vma;
3644 case DT_HASH:
3645 name = ".hash";
3646 goto get_vma;
3647 case DT_STRTAB:
3648 name = ".dynstr";
3649 goto get_vma;
3650 case DT_SYMTAB:
3651 name = ".dynsym";
3652 goto get_vma;
3653 case DT_VERDEF:
3654 name = ".gnu.version_d";
3655 goto get_vma;
3656 case DT_VERNEED:
3657 name = ".gnu.version_r";
3658 goto get_vma;
3659 case DT_VERSYM:
3660 name = ".gnu.version";
3661 get_vma:
3662 o = bfd_get_section_by_name (abfd, name);
3663 if (o == NULL)
3665 (*_bfd_error_handler)
3666 (_("%s: could not find output section %s"),
3667 bfd_get_filename (abfd), name);
3668 goto error_return;
3670 dyn.d_un.d_ptr = o->vma;
3671 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3672 break;
3674 case DT_REL:
3675 case DT_RELA:
3676 case DT_RELSZ:
3677 case DT_RELASZ:
3678 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3679 type = SHT_REL;
3680 else
3681 type = SHT_RELA;
3682 dyn.d_un.d_val = 0;
3683 for (i = 1; i < elf_numsections (abfd); i++)
3685 Elf_Internal_Shdr *hdr;
3687 hdr = elf_elfsections (abfd)[i];
3688 if (hdr->sh_type == type
3689 && (hdr->sh_flags & SHF_ALLOC) != 0)
3691 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3692 dyn.d_un.d_val += hdr->sh_size;
3693 else
3695 if (dyn.d_un.d_val == 0
3696 || hdr->sh_addr < dyn.d_un.d_val)
3697 dyn.d_un.d_val = hdr->sh_addr;
3701 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3702 break;
3707 /* If we have created any dynamic sections, then output them. */
3708 if (dynobj != NULL)
3710 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3711 goto error_return;
3713 for (o = dynobj->sections; o != NULL; o = o->next)
3715 if ((o->flags & SEC_HAS_CONTENTS) == 0
3716 || o->_raw_size == 0
3717 || o->output_section == bfd_abs_section_ptr)
3718 continue;
3719 if ((o->flags & SEC_LINKER_CREATED) == 0)
3721 /* At this point, we are only interested in sections
3722 created by _bfd_elf_link_create_dynamic_sections. */
3723 continue;
3725 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3726 != SHT_STRTAB)
3727 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3729 if (! bfd_set_section_contents (abfd, o->output_section,
3730 o->contents,
3731 (file_ptr) o->output_offset,
3732 o->_raw_size))
3733 goto error_return;
3735 else
3737 /* The contents of the .dynstr section are actually in a
3738 stringtab. */
3739 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3740 if (bfd_seek (abfd, off, SEEK_SET) != 0
3741 || ! _bfd_elf_strtab_emit (abfd,
3742 elf_hash_table (info)->dynstr))
3743 goto error_return;
3748 if (info->relocatable)
3750 bfd_boolean failed = FALSE;
3752 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3753 if (failed)
3754 goto error_return;
3757 /* If we have optimized stabs strings, output them. */
3758 if (elf_hash_table (info)->stab_info != NULL)
3760 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3761 goto error_return;
3764 if (info->eh_frame_hdr)
3766 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
3767 goto error_return;
3770 if (finfo.symstrtab != NULL)
3771 _bfd_stringtab_free (finfo.symstrtab);
3772 if (finfo.contents != NULL)
3773 free (finfo.contents);
3774 if (finfo.external_relocs != NULL)
3775 free (finfo.external_relocs);
3776 if (finfo.internal_relocs != NULL)
3777 free (finfo.internal_relocs);
3778 if (finfo.external_syms != NULL)
3779 free (finfo.external_syms);
3780 if (finfo.locsym_shndx != NULL)
3781 free (finfo.locsym_shndx);
3782 if (finfo.internal_syms != NULL)
3783 free (finfo.internal_syms);
3784 if (finfo.indices != NULL)
3785 free (finfo.indices);
3786 if (finfo.sections != NULL)
3787 free (finfo.sections);
3788 if (finfo.symbuf != NULL)
3789 free (finfo.symbuf);
3790 if (finfo.symshndxbuf != NULL)
3791 free (finfo.symshndxbuf);
3792 for (o = abfd->sections; o != NULL; o = o->next)
3794 if ((o->flags & SEC_RELOC) != 0
3795 && elf_section_data (o)->rel_hashes != NULL)
3796 free (elf_section_data (o)->rel_hashes);
3799 elf_tdata (abfd)->linker = TRUE;
3801 return TRUE;
3803 error_return:
3804 if (finfo.symstrtab != NULL)
3805 _bfd_stringtab_free (finfo.symstrtab);
3806 if (finfo.contents != NULL)
3807 free (finfo.contents);
3808 if (finfo.external_relocs != NULL)
3809 free (finfo.external_relocs);
3810 if (finfo.internal_relocs != NULL)
3811 free (finfo.internal_relocs);
3812 if (finfo.external_syms != NULL)
3813 free (finfo.external_syms);
3814 if (finfo.locsym_shndx != NULL)
3815 free (finfo.locsym_shndx);
3816 if (finfo.internal_syms != NULL)
3817 free (finfo.internal_syms);
3818 if (finfo.indices != NULL)
3819 free (finfo.indices);
3820 if (finfo.sections != NULL)
3821 free (finfo.sections);
3822 if (finfo.symbuf != NULL)
3823 free (finfo.symbuf);
3824 if (finfo.symshndxbuf != NULL)
3825 free (finfo.symshndxbuf);
3826 for (o = abfd->sections; o != NULL; o = o->next)
3828 if ((o->flags & SEC_RELOC) != 0
3829 && elf_section_data (o)->rel_hashes != NULL)
3830 free (elf_section_data (o)->rel_hashes);
3833 return FALSE;
3836 /* Add a symbol to the output symbol table. */
3838 static bfd_boolean
3839 elf_link_output_sym (struct elf_final_link_info *finfo,
3840 const char *name,
3841 Elf_Internal_Sym *elfsym,
3842 asection *input_sec,
3843 struct elf_link_hash_entry *h)
3845 Elf_External_Sym *dest;
3846 Elf_External_Sym_Shndx *destshndx;
3847 bfd_boolean (*output_symbol_hook)
3848 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
3849 struct elf_link_hash_entry *);
3851 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3852 elf_backend_link_output_symbol_hook;
3853 if (output_symbol_hook != NULL)
3855 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
3856 return FALSE;
3859 if (name == NULL || *name == '\0')
3860 elfsym->st_name = 0;
3861 else if (input_sec->flags & SEC_EXCLUDE)
3862 elfsym->st_name = 0;
3863 else
3865 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
3866 name, TRUE, FALSE);
3867 if (elfsym->st_name == (unsigned long) -1)
3868 return FALSE;
3871 if (finfo->symbuf_count >= finfo->symbuf_size)
3873 if (! elf_link_flush_output_syms (finfo))
3874 return FALSE;
3877 dest = finfo->symbuf + finfo->symbuf_count;
3878 destshndx = finfo->symshndxbuf;
3879 if (destshndx != NULL)
3881 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
3883 bfd_size_type amt;
3885 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
3886 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
3887 if (destshndx == NULL)
3888 return FALSE;
3889 memset ((char *) destshndx + amt, 0, amt);
3890 finfo->shndxbuf_size *= 2;
3892 destshndx += bfd_get_symcount (finfo->output_bfd);
3895 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
3896 finfo->symbuf_count += 1;
3897 bfd_get_symcount (finfo->output_bfd) += 1;
3899 return TRUE;
3902 /* Flush the output symbols to the file. */
3904 static bfd_boolean
3905 elf_link_flush_output_syms (struct elf_final_link_info *finfo)
3907 if (finfo->symbuf_count > 0)
3909 Elf_Internal_Shdr *hdr;
3910 file_ptr pos;
3911 bfd_size_type amt;
3913 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
3914 pos = hdr->sh_offset + hdr->sh_size;
3915 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
3916 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
3917 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
3918 return FALSE;
3920 hdr->sh_size += amt;
3921 finfo->symbuf_count = 0;
3924 return TRUE;
3927 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
3928 allowing an unsatisfied unversioned symbol in the DSO to match a
3929 versioned symbol that would normally require an explicit version.
3930 We also handle the case that a DSO references a hidden symbol
3931 which may be satisfied by a versioned symbol in another DSO. */
3933 static bfd_boolean
3934 elf_link_check_versioned_symbol (struct bfd_link_info *info,
3935 struct elf_link_hash_entry *h)
3937 bfd *abfd;
3938 struct elf_link_loaded_list *loaded;
3940 if (!is_elf_hash_table (info->hash))
3941 return FALSE;
3943 switch (h->root.type)
3945 default:
3946 abfd = NULL;
3947 break;
3949 case bfd_link_hash_undefined:
3950 case bfd_link_hash_undefweak:
3951 abfd = h->root.u.undef.abfd;
3952 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
3953 return FALSE;
3954 break;
3956 case bfd_link_hash_defined:
3957 case bfd_link_hash_defweak:
3958 abfd = h->root.u.def.section->owner;
3959 break;
3961 case bfd_link_hash_common:
3962 abfd = h->root.u.c.p->section->owner;
3963 break;
3965 BFD_ASSERT (abfd != NULL);
3967 for (loaded = elf_hash_table (info)->loaded;
3968 loaded != NULL;
3969 loaded = loaded->next)
3971 bfd *input;
3972 Elf_Internal_Shdr *hdr;
3973 bfd_size_type symcount;
3974 bfd_size_type extsymcount;
3975 bfd_size_type extsymoff;
3976 Elf_Internal_Shdr *versymhdr;
3977 Elf_Internal_Sym *isym;
3978 Elf_Internal_Sym *isymend;
3979 Elf_Internal_Sym *isymbuf;
3980 Elf_External_Versym *ever;
3981 Elf_External_Versym *extversym;
3983 input = loaded->abfd;
3985 /* We check each DSO for a possible hidden versioned definition. */
3986 if (input == abfd
3987 || (input->flags & DYNAMIC) == 0
3988 || elf_dynversym (input) == 0)
3989 continue;
3991 hdr = &elf_tdata (input)->dynsymtab_hdr;
3993 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
3994 if (elf_bad_symtab (input))
3996 extsymcount = symcount;
3997 extsymoff = 0;
3999 else
4001 extsymcount = symcount - hdr->sh_info;
4002 extsymoff = hdr->sh_info;
4005 if (extsymcount == 0)
4006 continue;
4008 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4009 NULL, NULL, NULL);
4010 if (isymbuf == NULL)
4011 return FALSE;
4013 /* Read in any version definitions. */
4014 versymhdr = &elf_tdata (input)->dynversym_hdr;
4015 extversym = bfd_malloc (versymhdr->sh_size);
4016 if (extversym == NULL)
4017 goto error_ret;
4019 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4020 || (bfd_bread (extversym, versymhdr->sh_size, input)
4021 != versymhdr->sh_size))
4023 free (extversym);
4024 error_ret:
4025 free (isymbuf);
4026 return FALSE;
4029 ever = extversym + extsymoff;
4030 isymend = isymbuf + extsymcount;
4031 for (isym = isymbuf; isym < isymend; isym++, ever++)
4033 const char *name;
4034 Elf_Internal_Versym iver;
4035 unsigned short version_index;
4037 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4038 || isym->st_shndx == SHN_UNDEF)
4039 continue;
4041 name = bfd_elf_string_from_elf_section (input,
4042 hdr->sh_link,
4043 isym->st_name);
4044 if (strcmp (name, h->root.root.string) != 0)
4045 continue;
4047 _bfd_elf_swap_versym_in (input, ever, &iver);
4049 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4051 /* If we have a non-hidden versioned sym, then it should
4052 have provided a definition for the undefined sym. */
4053 abort ();
4056 version_index = iver.vs_vers & VERSYM_VERSION;
4057 if (version_index == 1 || version_index == 2)
4059 /* This is the base or first version. We can use it. */
4060 free (extversym);
4061 free (isymbuf);
4062 return TRUE;
4066 free (extversym);
4067 free (isymbuf);
4070 return FALSE;
4073 /* Add an external symbol to the symbol table. This is called from
4074 the hash table traversal routine. When generating a shared object,
4075 we go through the symbol table twice. The first time we output
4076 anything that might have been forced to local scope in a version
4077 script. The second time we output the symbols that are still
4078 global symbols. */
4080 static bfd_boolean
4081 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
4083 struct elf_outext_info *eoinfo = data;
4084 struct elf_final_link_info *finfo = eoinfo->finfo;
4085 bfd_boolean strip;
4086 Elf_Internal_Sym sym;
4087 asection *input_sec;
4089 if (h->root.type == bfd_link_hash_warning)
4091 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4092 if (h->root.type == bfd_link_hash_new)
4093 return TRUE;
4096 /* Decide whether to output this symbol in this pass. */
4097 if (eoinfo->localsyms)
4099 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4100 return TRUE;
4102 else
4104 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4105 return TRUE;
4108 /* If we have an undefined symbol reference here then it must have
4109 come from a shared library that is being linked in. (Undefined
4110 references in regular files have already been handled). If we
4111 are reporting errors for this situation then do so now. */
4112 if (h->root.type == bfd_link_hash_undefined
4113 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4114 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4115 && ! elf_link_check_versioned_symbol (finfo->info, h)
4116 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
4118 if (! ((*finfo->info->callbacks->undefined_symbol)
4119 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4120 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
4122 eoinfo->failed = TRUE;
4123 return FALSE;
4127 /* We should also warn if a forced local symbol is referenced from
4128 shared libraries. */
4129 if (! finfo->info->relocatable
4130 && (! finfo->info->shared)
4131 && (h->elf_link_hash_flags
4132 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4133 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4134 && ! elf_link_check_versioned_symbol (finfo->info, h))
4136 (*_bfd_error_handler)
4137 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4138 bfd_get_filename (finfo->output_bfd),
4139 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4140 ? "internal"
4141 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4142 ? "hidden" : "local",
4143 h->root.root.string,
4144 bfd_archive_filename (h->root.u.def.section->owner));
4145 eoinfo->failed = TRUE;
4146 return FALSE;
4149 /* We don't want to output symbols that have never been mentioned by
4150 a regular file, or that we have been told to strip. However, if
4151 h->indx is set to -2, the symbol is used by a reloc and we must
4152 output it. */
4153 if (h->indx == -2)
4154 strip = FALSE;
4155 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4156 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4157 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4158 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4159 strip = TRUE;
4160 else if (finfo->info->strip == strip_all)
4161 strip = TRUE;
4162 else if (finfo->info->strip == strip_some
4163 && bfd_hash_lookup (finfo->info->keep_hash,
4164 h->root.root.string, FALSE, FALSE) == NULL)
4165 strip = TRUE;
4166 else if (finfo->info->strip_discarded
4167 && (h->root.type == bfd_link_hash_defined
4168 || h->root.type == bfd_link_hash_defweak)
4169 && elf_discarded_section (h->root.u.def.section))
4170 strip = TRUE;
4171 else
4172 strip = FALSE;
4174 /* If we're stripping it, and it's not a dynamic symbol, there's
4175 nothing else to do unless it is a forced local symbol. */
4176 if (strip
4177 && h->dynindx == -1
4178 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4179 return TRUE;
4181 sym.st_value = 0;
4182 sym.st_size = h->size;
4183 sym.st_other = h->other;
4184 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4185 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4186 else if (h->root.type == bfd_link_hash_undefweak
4187 || h->root.type == bfd_link_hash_defweak)
4188 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4189 else
4190 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4192 switch (h->root.type)
4194 default:
4195 case bfd_link_hash_new:
4196 case bfd_link_hash_warning:
4197 abort ();
4198 return FALSE;
4200 case bfd_link_hash_undefined:
4201 case bfd_link_hash_undefweak:
4202 input_sec = bfd_und_section_ptr;
4203 sym.st_shndx = SHN_UNDEF;
4204 break;
4206 case bfd_link_hash_defined:
4207 case bfd_link_hash_defweak:
4209 input_sec = h->root.u.def.section;
4210 if (input_sec->output_section != NULL)
4212 sym.st_shndx =
4213 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4214 input_sec->output_section);
4215 if (sym.st_shndx == SHN_BAD)
4217 (*_bfd_error_handler)
4218 (_("%s: could not find output section %s for input section %s"),
4219 bfd_get_filename (finfo->output_bfd),
4220 input_sec->output_section->name,
4221 input_sec->name);
4222 eoinfo->failed = TRUE;
4223 return FALSE;
4226 /* ELF symbols in relocatable files are section relative,
4227 but in nonrelocatable files they are virtual
4228 addresses. */
4229 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4230 if (! finfo->info->relocatable)
4232 sym.st_value += input_sec->output_section->vma;
4233 if (h->type == STT_TLS)
4235 /* STT_TLS symbols are relative to PT_TLS segment
4236 base. */
4237 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4238 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4242 else
4244 BFD_ASSERT (input_sec->owner == NULL
4245 || (input_sec->owner->flags & DYNAMIC) != 0);
4246 sym.st_shndx = SHN_UNDEF;
4247 input_sec = bfd_und_section_ptr;
4250 break;
4252 case bfd_link_hash_common:
4253 input_sec = h->root.u.c.p->section;
4254 sym.st_shndx = SHN_COMMON;
4255 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4256 break;
4258 case bfd_link_hash_indirect:
4259 /* These symbols are created by symbol versioning. They point
4260 to the decorated version of the name. For example, if the
4261 symbol foo@@GNU_1.2 is the default, which should be used when
4262 foo is used with no version, then we add an indirect symbol
4263 foo which points to foo@@GNU_1.2. We ignore these symbols,
4264 since the indirected symbol is already in the hash table. */
4265 return TRUE;
4268 /* Give the processor backend a chance to tweak the symbol value,
4269 and also to finish up anything that needs to be done for this
4270 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4271 forced local syms when non-shared is due to a historical quirk. */
4272 if ((h->dynindx != -1
4273 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4274 && ((finfo->info->shared
4275 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4276 || h->root.type != bfd_link_hash_undefweak))
4277 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4278 && elf_hash_table (finfo->info)->dynamic_sections_created)
4280 const struct elf_backend_data *bed;
4282 bed = get_elf_backend_data (finfo->output_bfd);
4283 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4284 (finfo->output_bfd, finfo->info, h, &sym)))
4286 eoinfo->failed = TRUE;
4287 return FALSE;
4291 /* If we are marking the symbol as undefined, and there are no
4292 non-weak references to this symbol from a regular object, then
4293 mark the symbol as weak undefined; if there are non-weak
4294 references, mark the symbol as strong. We can't do this earlier,
4295 because it might not be marked as undefined until the
4296 finish_dynamic_symbol routine gets through with it. */
4297 if (sym.st_shndx == SHN_UNDEF
4298 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4299 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4300 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4302 int bindtype;
4304 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4305 bindtype = STB_GLOBAL;
4306 else
4307 bindtype = STB_WEAK;
4308 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4311 /* If a non-weak symbol with non-default visibility is not defined
4312 locally, it is a fatal error. */
4313 if (! finfo->info->relocatable
4314 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4315 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4316 && h->root.type == bfd_link_hash_undefined
4317 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4319 (*_bfd_error_handler)
4320 (_("%s: %s symbol `%s' isn't defined"),
4321 bfd_get_filename (finfo->output_bfd),
4322 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4323 ? "protected"
4324 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4325 ? "internal" : "hidden",
4326 h->root.root.string);
4327 eoinfo->failed = TRUE;
4328 return FALSE;
4331 /* If this symbol should be put in the .dynsym section, then put it
4332 there now. We already know the symbol index. We also fill in
4333 the entry in the .hash section. */
4334 if (h->dynindx != -1
4335 && elf_hash_table (finfo->info)->dynamic_sections_created)
4337 size_t bucketcount;
4338 size_t bucket;
4339 size_t hash_entry_size;
4340 bfd_byte *bucketpos;
4341 bfd_vma chain;
4342 Elf_External_Sym *esym;
4344 sym.st_name = h->dynstr_index;
4345 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4346 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
4348 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4349 bucket = h->elf_hash_value % bucketcount;
4350 hash_entry_size
4351 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4352 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4353 + (bucket + 2) * hash_entry_size);
4354 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4355 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
4356 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4357 ((bfd_byte *) finfo->hash_sec->contents
4358 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4360 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4362 Elf_Internal_Versym iversym;
4363 Elf_External_Versym *eversym;
4365 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4367 if (h->verinfo.verdef == NULL)
4368 iversym.vs_vers = 0;
4369 else
4370 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4372 else
4374 if (h->verinfo.vertree == NULL)
4375 iversym.vs_vers = 1;
4376 else
4377 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4380 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4381 iversym.vs_vers |= VERSYM_HIDDEN;
4383 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4384 eversym += h->dynindx;
4385 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4389 /* If we're stripping it, then it was just a dynamic symbol, and
4390 there's nothing else to do. */
4391 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4392 return TRUE;
4394 h->indx = bfd_get_symcount (finfo->output_bfd);
4396 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
4398 eoinfo->failed = TRUE;
4399 return FALSE;
4402 return TRUE;
4405 /* Link an input file into the linker output file. This function
4406 handles all the sections and relocations of the input file at once.
4407 This is so that we only have to read the local symbols once, and
4408 don't have to keep them in memory. */
4410 static bfd_boolean
4411 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
4413 bfd_boolean (*relocate_section)
4414 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4415 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
4416 bfd *output_bfd;
4417 Elf_Internal_Shdr *symtab_hdr;
4418 size_t locsymcount;
4419 size_t extsymoff;
4420 Elf_Internal_Sym *isymbuf;
4421 Elf_Internal_Sym *isym;
4422 Elf_Internal_Sym *isymend;
4423 long *pindex;
4424 asection **ppsection;
4425 asection *o;
4426 const struct elf_backend_data *bed;
4427 bfd_boolean emit_relocs;
4428 struct elf_link_hash_entry **sym_hashes;
4430 output_bfd = finfo->output_bfd;
4431 bed = get_elf_backend_data (output_bfd);
4432 relocate_section = bed->elf_backend_relocate_section;
4434 /* If this is a dynamic object, we don't want to do anything here:
4435 we don't want the local symbols, and we don't want the section
4436 contents. */
4437 if ((input_bfd->flags & DYNAMIC) != 0)
4438 return TRUE;
4440 emit_relocs = (finfo->info->relocatable
4441 || finfo->info->emitrelocations
4442 || bed->elf_backend_emit_relocs);
4444 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4445 if (elf_bad_symtab (input_bfd))
4447 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4448 extsymoff = 0;
4450 else
4452 locsymcount = symtab_hdr->sh_info;
4453 extsymoff = symtab_hdr->sh_info;
4456 /* Read the local symbols. */
4457 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4458 if (isymbuf == NULL && locsymcount != 0)
4460 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4461 finfo->internal_syms,
4462 finfo->external_syms,
4463 finfo->locsym_shndx);
4464 if (isymbuf == NULL)
4465 return FALSE;
4468 /* Find local symbol sections and adjust values of symbols in
4469 SEC_MERGE sections. Write out those local symbols we know are
4470 going into the output file. */
4471 isymend = isymbuf + locsymcount;
4472 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4473 isym < isymend;
4474 isym++, pindex++, ppsection++)
4476 asection *isec;
4477 const char *name;
4478 Elf_Internal_Sym osym;
4480 *pindex = -1;
4482 if (elf_bad_symtab (input_bfd))
4484 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4486 *ppsection = NULL;
4487 continue;
4491 if (isym->st_shndx == SHN_UNDEF)
4492 isec = bfd_und_section_ptr;
4493 else if (isym->st_shndx < SHN_LORESERVE
4494 || isym->st_shndx > SHN_HIRESERVE)
4496 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4497 if (isec
4498 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4499 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4500 isym->st_value =
4501 _bfd_merged_section_offset (output_bfd, &isec,
4502 elf_section_data (isec)->sec_info,
4503 isym->st_value, 0);
4505 else if (isym->st_shndx == SHN_ABS)
4506 isec = bfd_abs_section_ptr;
4507 else if (isym->st_shndx == SHN_COMMON)
4508 isec = bfd_com_section_ptr;
4509 else
4511 /* Who knows? */
4512 isec = NULL;
4515 *ppsection = isec;
4517 /* Don't output the first, undefined, symbol. */
4518 if (ppsection == finfo->sections)
4519 continue;
4521 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4523 /* We never output section symbols. Instead, we use the
4524 section symbol of the corresponding section in the output
4525 file. */
4526 continue;
4529 /* If we are stripping all symbols, we don't want to output this
4530 one. */
4531 if (finfo->info->strip == strip_all)
4532 continue;
4534 /* If we are discarding all local symbols, we don't want to
4535 output this one. If we are generating a relocatable output
4536 file, then some of the local symbols may be required by
4537 relocs; we output them below as we discover that they are
4538 needed. */
4539 if (finfo->info->discard == discard_all)
4540 continue;
4542 /* If this symbol is defined in a section which we are
4543 discarding, we don't need to keep it, but note that
4544 linker_mark is only reliable for sections that have contents.
4545 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4546 as well as linker_mark. */
4547 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4548 && isec != NULL
4549 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4550 || (! finfo->info->relocatable
4551 && (isec->flags & SEC_EXCLUDE) != 0)))
4552 continue;
4554 /* Get the name of the symbol. */
4555 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4556 isym->st_name);
4557 if (name == NULL)
4558 return FALSE;
4560 /* See if we are discarding symbols with this name. */
4561 if ((finfo->info->strip == strip_some
4562 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4563 == NULL))
4564 || (((finfo->info->discard == discard_sec_merge
4565 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4566 || finfo->info->discard == discard_l)
4567 && bfd_is_local_label_name (input_bfd, name)))
4568 continue;
4570 /* If we get here, we are going to output this symbol. */
4572 osym = *isym;
4574 /* Adjust the section index for the output file. */
4575 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4576 isec->output_section);
4577 if (osym.st_shndx == SHN_BAD)
4578 return FALSE;
4580 *pindex = bfd_get_symcount (output_bfd);
4582 /* ELF symbols in relocatable files are section relative, but
4583 in executable files they are virtual addresses. Note that
4584 this code assumes that all ELF sections have an associated
4585 BFD section with a reasonable value for output_offset; below
4586 we assume that they also have a reasonable value for
4587 output_section. Any special sections must be set up to meet
4588 these requirements. */
4589 osym.st_value += isec->output_offset;
4590 if (! finfo->info->relocatable)
4592 osym.st_value += isec->output_section->vma;
4593 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4595 /* STT_TLS symbols are relative to PT_TLS segment base. */
4596 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4597 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4601 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
4602 return FALSE;
4605 /* Relocate the contents of each section. */
4606 sym_hashes = elf_sym_hashes (input_bfd);
4607 for (o = input_bfd->sections; o != NULL; o = o->next)
4609 bfd_byte *contents;
4611 if (! o->linker_mark)
4613 /* This section was omitted from the link. */
4614 continue;
4617 if ((o->flags & SEC_HAS_CONTENTS) == 0
4618 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4619 continue;
4621 if ((o->flags & SEC_LINKER_CREATED) != 0)
4623 /* Section was created by _bfd_elf_link_create_dynamic_sections
4624 or somesuch. */
4625 continue;
4628 /* Get the contents of the section. They have been cached by a
4629 relaxation routine. Note that o is a section in an input
4630 file, so the contents field will not have been set by any of
4631 the routines which work on output files. */
4632 if (elf_section_data (o)->this_hdr.contents != NULL)
4633 contents = elf_section_data (o)->this_hdr.contents;
4634 else
4636 contents = finfo->contents;
4637 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4638 o->_raw_size))
4639 return FALSE;
4642 if ((o->flags & SEC_RELOC) != 0)
4644 Elf_Internal_Rela *internal_relocs;
4646 /* Get the swapped relocs. */
4647 internal_relocs
4648 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4649 finfo->internal_relocs, FALSE);
4650 if (internal_relocs == NULL
4651 && o->reloc_count > 0)
4652 return FALSE;
4654 /* Run through the relocs looking for any against symbols
4655 from discarded sections and section symbols from
4656 removed link-once sections. Complain about relocs
4657 against discarded sections. Zero relocs against removed
4658 link-once sections. Preserve debug information as much
4659 as we can. */
4660 if (!elf_section_ignore_discarded_relocs (o))
4662 Elf_Internal_Rela *rel, *relend;
4664 rel = internal_relocs;
4665 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4666 for ( ; rel < relend; rel++)
4668 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4669 asection *sec;
4671 if (r_symndx >= locsymcount
4672 || (elf_bad_symtab (input_bfd)
4673 && finfo->sections[r_symndx] == NULL))
4675 struct elf_link_hash_entry *h;
4677 h = sym_hashes[r_symndx - extsymoff];
4678 while (h->root.type == bfd_link_hash_indirect
4679 || h->root.type == bfd_link_hash_warning)
4680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4682 /* Complain if the definition comes from a
4683 discarded section. */
4684 sec = h->root.u.def.section;
4685 if ((h->root.type == bfd_link_hash_defined
4686 || h->root.type == bfd_link_hash_defweak)
4687 && elf_discarded_section (sec))
4689 if ((o->flags & SEC_DEBUGGING) != 0)
4691 BFD_ASSERT (r_symndx != 0);
4692 /* Try to preserve debug information. */
4693 if ((o->flags & SEC_DEBUGGING) != 0
4694 && sec->kept_section != NULL
4695 && sec->_raw_size == sec->kept_section->_raw_size)
4696 h->root.u.def.section
4697 = sec->kept_section;
4698 else
4699 memset (rel, 0, sizeof (*rel));
4701 else
4702 finfo->info->callbacks->error_handler
4703 (LD_DEFINITION_IN_DISCARDED_SECTION,
4704 _("%T: discarded in section `%s' from %s\n"),
4705 h->root.root.string,
4706 h->root.root.string,
4707 h->root.u.def.section->name,
4708 bfd_archive_filename (h->root.u.def.section->owner));
4711 else
4713 sec = finfo->sections[r_symndx];
4715 if (sec != NULL && elf_discarded_section (sec))
4717 if ((o->flags & SEC_DEBUGGING) != 0
4718 || (sec->flags & SEC_LINK_ONCE) != 0)
4720 BFD_ASSERT (r_symndx != 0);
4721 /* Try to preserve debug information. */
4722 if ((o->flags & SEC_DEBUGGING) != 0
4723 && sec->kept_section != NULL
4724 && sec->_raw_size == sec->kept_section->_raw_size)
4725 finfo->sections[r_symndx]
4726 = sec->kept_section;
4727 else
4729 rel->r_info
4730 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4731 rel->r_addend = 0;
4734 else
4736 static int count;
4737 int ok;
4738 char *buf;
4740 ok = asprintf (&buf, "local symbol %d",
4741 count++);
4742 if (ok <= 0)
4743 buf = (char *) "local symbol";
4744 finfo->info->callbacks->error_handler
4745 (LD_DEFINITION_IN_DISCARDED_SECTION,
4746 _("%T: discarded in section `%s' from %s\n"),
4747 buf, buf, sec->name,
4748 bfd_archive_filename (input_bfd));
4749 if (ok != -1)
4750 free (buf);
4757 /* Relocate the section by invoking a back end routine.
4759 The back end routine is responsible for adjusting the
4760 section contents as necessary, and (if using Rela relocs
4761 and generating a relocatable output file) adjusting the
4762 reloc addend as necessary.
4764 The back end routine does not have to worry about setting
4765 the reloc address or the reloc symbol index.
4767 The back end routine is given a pointer to the swapped in
4768 internal symbols, and can access the hash table entries
4769 for the external symbols via elf_sym_hashes (input_bfd).
4771 When generating relocatable output, the back end routine
4772 must handle STB_LOCAL/STT_SECTION symbols specially. The
4773 output symbol is going to be a section symbol
4774 corresponding to the output section, which will require
4775 the addend to be adjusted. */
4777 if (! (*relocate_section) (output_bfd, finfo->info,
4778 input_bfd, o, contents,
4779 internal_relocs,
4780 isymbuf,
4781 finfo->sections))
4782 return FALSE;
4784 if (emit_relocs)
4786 Elf_Internal_Rela *irela;
4787 Elf_Internal_Rela *irelaend;
4788 bfd_vma last_offset;
4789 struct elf_link_hash_entry **rel_hash;
4790 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4791 unsigned int next_erel;
4792 bfd_boolean (*reloc_emitter)
4793 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
4794 bfd_boolean rela_normal;
4796 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4797 rela_normal = (bed->rela_normal
4798 && (input_rel_hdr->sh_entsize
4799 == sizeof (Elf_External_Rela)));
4801 /* Adjust the reloc addresses and symbol indices. */
4803 irela = internal_relocs;
4804 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
4805 rel_hash = (elf_section_data (o->output_section)->rel_hashes
4806 + elf_section_data (o->output_section)->rel_count
4807 + elf_section_data (o->output_section)->rel_count2);
4808 last_offset = o->output_offset;
4809 if (!finfo->info->relocatable)
4810 last_offset += o->output_section->vma;
4811 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
4813 unsigned long r_symndx;
4814 asection *sec;
4815 Elf_Internal_Sym sym;
4817 if (next_erel == bed->s->int_rels_per_ext_rel)
4819 rel_hash++;
4820 next_erel = 0;
4823 irela->r_offset = _bfd_elf_section_offset (output_bfd,
4824 finfo->info, o,
4825 irela->r_offset);
4826 if (irela->r_offset >= (bfd_vma) -2)
4828 /* This is a reloc for a deleted entry or somesuch.
4829 Turn it into an R_*_NONE reloc, at the same
4830 offset as the last reloc. elf_eh_frame.c and
4831 elf_bfd_discard_info rely on reloc offsets
4832 being ordered. */
4833 irela->r_offset = last_offset;
4834 irela->r_info = 0;
4835 irela->r_addend = 0;
4836 continue;
4839 irela->r_offset += o->output_offset;
4841 /* Relocs in an executable have to be virtual addresses. */
4842 if (!finfo->info->relocatable)
4843 irela->r_offset += o->output_section->vma;
4845 last_offset = irela->r_offset;
4847 r_symndx = ELF_R_SYM (irela->r_info);
4848 if (r_symndx == STN_UNDEF)
4849 continue;
4851 if (r_symndx >= locsymcount
4852 || (elf_bad_symtab (input_bfd)
4853 && finfo->sections[r_symndx] == NULL))
4855 struct elf_link_hash_entry *rh;
4856 unsigned long indx;
4858 /* This is a reloc against a global symbol. We
4859 have not yet output all the local symbols, so
4860 we do not know the symbol index of any global
4861 symbol. We set the rel_hash entry for this
4862 reloc to point to the global hash table entry
4863 for this symbol. The symbol index is then
4864 set at the end of elf_bfd_final_link. */
4865 indx = r_symndx - extsymoff;
4866 rh = elf_sym_hashes (input_bfd)[indx];
4867 while (rh->root.type == bfd_link_hash_indirect
4868 || rh->root.type == bfd_link_hash_warning)
4869 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
4871 /* Setting the index to -2 tells
4872 elf_link_output_extsym that this symbol is
4873 used by a reloc. */
4874 BFD_ASSERT (rh->indx < 0);
4875 rh->indx = -2;
4877 *rel_hash = rh;
4879 continue;
4882 /* This is a reloc against a local symbol. */
4884 *rel_hash = NULL;
4885 sym = isymbuf[r_symndx];
4886 sec = finfo->sections[r_symndx];
4887 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
4889 /* I suppose the backend ought to fill in the
4890 section of any STT_SECTION symbol against a
4891 processor specific section. If we have
4892 discarded a section, the output_section will
4893 be the absolute section. */
4894 if (bfd_is_abs_section (sec)
4895 || (sec != NULL
4896 && bfd_is_abs_section (sec->output_section)))
4897 r_symndx = 0;
4898 else if (sec == NULL || sec->owner == NULL)
4900 bfd_set_error (bfd_error_bad_value);
4901 return FALSE;
4903 else
4905 r_symndx = sec->output_section->target_index;
4906 BFD_ASSERT (r_symndx != 0);
4909 /* Adjust the addend according to where the
4910 section winds up in the output section. */
4911 if (rela_normal)
4912 irela->r_addend += sec->output_offset;
4914 else
4916 if (finfo->indices[r_symndx] == -1)
4918 unsigned long shlink;
4919 const char *name;
4920 asection *osec;
4922 if (finfo->info->strip == strip_all)
4924 /* You can't do ld -r -s. */
4925 bfd_set_error (bfd_error_invalid_operation);
4926 return FALSE;
4929 /* This symbol was skipped earlier, but
4930 since it is needed by a reloc, we
4931 must output it now. */
4932 shlink = symtab_hdr->sh_link;
4933 name = (bfd_elf_string_from_elf_section
4934 (input_bfd, shlink, sym.st_name));
4935 if (name == NULL)
4936 return FALSE;
4938 osec = sec->output_section;
4939 sym.st_shndx =
4940 _bfd_elf_section_from_bfd_section (output_bfd,
4941 osec);
4942 if (sym.st_shndx == SHN_BAD)
4943 return FALSE;
4945 sym.st_value += sec->output_offset;
4946 if (! finfo->info->relocatable)
4948 sym.st_value += osec->vma;
4949 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
4951 /* STT_TLS symbols are relative to PT_TLS
4952 segment base. */
4953 BFD_ASSERT (elf_hash_table (finfo->info)
4954 ->tls_sec != NULL);
4955 sym.st_value -= (elf_hash_table (finfo->info)
4956 ->tls_sec->vma);
4960 finfo->indices[r_symndx]
4961 = bfd_get_symcount (output_bfd);
4963 if (! elf_link_output_sym (finfo, name, &sym, sec,
4964 NULL))
4965 return FALSE;
4968 r_symndx = finfo->indices[r_symndx];
4971 irela->r_info = ELF_R_INFO (r_symndx,
4972 ELF_R_TYPE (irela->r_info));
4975 /* Swap out the relocs. */
4976 if (bed->elf_backend_emit_relocs
4977 && !(finfo->info->relocatable
4978 || finfo->info->emitrelocations))
4979 reloc_emitter = bed->elf_backend_emit_relocs;
4980 else
4981 reloc_emitter = _bfd_elf_link_output_relocs;
4983 if (input_rel_hdr->sh_size != 0
4984 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
4985 internal_relocs))
4986 return FALSE;
4988 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
4989 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
4991 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
4992 * bed->s->int_rels_per_ext_rel);
4993 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
4994 internal_relocs))
4995 return FALSE;
5000 /* Write out the modified section contents. */
5001 if (bed->elf_backend_write_section
5002 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
5004 /* Section written out. */
5006 else switch (o->sec_info_type)
5008 case ELF_INFO_TYPE_STABS:
5009 if (! (_bfd_write_section_stabs
5010 (output_bfd,
5011 &elf_hash_table (finfo->info)->stab_info,
5012 o, &elf_section_data (o)->sec_info, contents)))
5013 return FALSE;
5014 break;
5015 case ELF_INFO_TYPE_MERGE:
5016 if (! _bfd_write_merged_section (output_bfd, o,
5017 elf_section_data (o)->sec_info))
5018 return FALSE;
5019 break;
5020 case ELF_INFO_TYPE_EH_FRAME:
5022 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5023 o, contents))
5024 return FALSE;
5026 break;
5027 default:
5029 bfd_size_type sec_size;
5031 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5032 if (! (o->flags & SEC_EXCLUDE)
5033 && ! bfd_set_section_contents (output_bfd, o->output_section,
5034 contents,
5035 (file_ptr) o->output_offset,
5036 sec_size))
5037 return FALSE;
5039 break;
5043 return TRUE;
5046 /* Generate a reloc when linking an ELF file. This is a reloc
5047 requested by the linker, and does come from any input file. This
5048 is used to build constructor and destructor tables when linking
5049 with -Ur. */
5051 static bfd_boolean
5052 elf_reloc_link_order (bfd *output_bfd,
5053 struct bfd_link_info *info,
5054 asection *output_section,
5055 struct bfd_link_order *link_order)
5057 reloc_howto_type *howto;
5058 long indx;
5059 bfd_vma offset;
5060 bfd_vma addend;
5061 struct elf_link_hash_entry **rel_hash_ptr;
5062 Elf_Internal_Shdr *rel_hdr;
5063 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5064 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5065 bfd_byte *erel;
5066 unsigned int i;
5068 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5069 if (howto == NULL)
5071 bfd_set_error (bfd_error_bad_value);
5072 return FALSE;
5075 addend = link_order->u.reloc.p->addend;
5077 /* Figure out the symbol index. */
5078 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5079 + elf_section_data (output_section)->rel_count
5080 + elf_section_data (output_section)->rel_count2);
5081 if (link_order->type == bfd_section_reloc_link_order)
5083 indx = link_order->u.reloc.p->u.section->target_index;
5084 BFD_ASSERT (indx != 0);
5085 *rel_hash_ptr = NULL;
5087 else
5089 struct elf_link_hash_entry *h;
5091 /* Treat a reloc against a defined symbol as though it were
5092 actually against the section. */
5093 h = ((struct elf_link_hash_entry *)
5094 bfd_wrapped_link_hash_lookup (output_bfd, info,
5095 link_order->u.reloc.p->u.name,
5096 FALSE, FALSE, TRUE));
5097 if (h != NULL
5098 && (h->root.type == bfd_link_hash_defined
5099 || h->root.type == bfd_link_hash_defweak))
5101 asection *section;
5103 section = h->root.u.def.section;
5104 indx = section->output_section->target_index;
5105 *rel_hash_ptr = NULL;
5106 /* It seems that we ought to add the symbol value to the
5107 addend here, but in practice it has already been added
5108 because it was passed to constructor_callback. */
5109 addend += section->output_section->vma + section->output_offset;
5111 else if (h != NULL)
5113 /* Setting the index to -2 tells elf_link_output_extsym that
5114 this symbol is used by a reloc. */
5115 h->indx = -2;
5116 *rel_hash_ptr = h;
5117 indx = 0;
5119 else
5121 if (! ((*info->callbacks->unattached_reloc)
5122 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
5123 return FALSE;
5124 indx = 0;
5128 /* If this is an inplace reloc, we must write the addend into the
5129 object file. */
5130 if (howto->partial_inplace && addend != 0)
5132 bfd_size_type size;
5133 bfd_reloc_status_type rstat;
5134 bfd_byte *buf;
5135 bfd_boolean ok;
5136 const char *sym_name;
5138 size = bfd_get_reloc_size (howto);
5139 buf = bfd_zmalloc (size);
5140 if (buf == NULL)
5141 return FALSE;
5142 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5143 switch (rstat)
5145 case bfd_reloc_ok:
5146 break;
5148 default:
5149 case bfd_reloc_outofrange:
5150 abort ();
5152 case bfd_reloc_overflow:
5153 if (link_order->type == bfd_section_reloc_link_order)
5154 sym_name = bfd_section_name (output_bfd,
5155 link_order->u.reloc.p->u.section);
5156 else
5157 sym_name = link_order->u.reloc.p->u.name;
5158 if (! ((*info->callbacks->reloc_overflow)
5159 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
5161 free (buf);
5162 return FALSE;
5164 break;
5166 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5167 link_order->offset, size);
5168 free (buf);
5169 if (! ok)
5170 return FALSE;
5173 /* The address of a reloc is relative to the section in a
5174 relocatable file, and is a virtual address in an executable
5175 file. */
5176 offset = link_order->offset;
5177 if (! info->relocatable)
5178 offset += output_section->vma;
5180 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5182 irel[i].r_offset = offset;
5183 irel[i].r_info = 0;
5184 irel[i].r_addend = 0;
5186 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5188 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5189 erel = rel_hdr->contents;
5190 if (rel_hdr->sh_type == SHT_REL)
5192 erel += (elf_section_data (output_section)->rel_count
5193 * sizeof (Elf_External_Rel));
5194 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5196 else
5198 irel[0].r_addend = addend;
5199 erel += (elf_section_data (output_section)->rel_count
5200 * sizeof (Elf_External_Rela));
5201 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5204 ++elf_section_data (output_section)->rel_count;
5206 return TRUE;
5209 /* Garbage collect unused sections. */
5211 static bfd_boolean elf_gc_sweep_symbol
5212 (struct elf_link_hash_entry *, void *);
5214 static bfd_boolean elf_gc_allocate_got_offsets
5215 (struct elf_link_hash_entry *, void *);
5217 /* The mark phase of garbage collection. For a given section, mark
5218 it and any sections in this section's group, and all the sections
5219 which define symbols to which it refers. */
5221 typedef asection * (*gc_mark_hook_fn)
5222 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5223 struct elf_link_hash_entry *, Elf_Internal_Sym *);
5225 static bfd_boolean
5226 elf_gc_mark (struct bfd_link_info *info,
5227 asection *sec,
5228 gc_mark_hook_fn gc_mark_hook)
5230 bfd_boolean ret;
5231 asection *group_sec;
5233 sec->gc_mark = 1;
5235 /* Mark all the sections in the group. */
5236 group_sec = elf_section_data (sec)->next_in_group;
5237 if (group_sec && !group_sec->gc_mark)
5238 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5239 return FALSE;
5241 /* Look through the section relocs. */
5242 ret = TRUE;
5243 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5245 Elf_Internal_Rela *relstart, *rel, *relend;
5246 Elf_Internal_Shdr *symtab_hdr;
5247 struct elf_link_hash_entry **sym_hashes;
5248 size_t nlocsyms;
5249 size_t extsymoff;
5250 bfd *input_bfd = sec->owner;
5251 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5252 Elf_Internal_Sym *isym = NULL;
5254 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5255 sym_hashes = elf_sym_hashes (input_bfd);
5257 /* Read the local symbols. */
5258 if (elf_bad_symtab (input_bfd))
5260 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5261 extsymoff = 0;
5263 else
5264 extsymoff = nlocsyms = symtab_hdr->sh_info;
5266 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5267 if (isym == NULL && nlocsyms != 0)
5269 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5270 NULL, NULL, NULL);
5271 if (isym == NULL)
5272 return FALSE;
5275 /* Read the relocations. */
5276 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5277 info->keep_memory);
5278 if (relstart == NULL)
5280 ret = FALSE;
5281 goto out1;
5283 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5285 for (rel = relstart; rel < relend; rel++)
5287 unsigned long r_symndx;
5288 asection *rsec;
5289 struct elf_link_hash_entry *h;
5291 r_symndx = ELF_R_SYM (rel->r_info);
5292 if (r_symndx == 0)
5293 continue;
5295 if (r_symndx >= nlocsyms
5296 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5298 h = sym_hashes[r_symndx - extsymoff];
5299 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5301 else
5303 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5306 if (rsec && !rsec->gc_mark)
5308 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5309 rsec->gc_mark = 1;
5310 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5312 ret = FALSE;
5313 goto out2;
5318 out2:
5319 if (elf_section_data (sec)->relocs != relstart)
5320 free (relstart);
5321 out1:
5322 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5324 if (! info->keep_memory)
5325 free (isym);
5326 else
5327 symtab_hdr->contents = (unsigned char *) isym;
5331 return ret;
5334 /* The sweep phase of garbage collection. Remove all garbage sections. */
5336 typedef bfd_boolean (*gc_sweep_hook_fn)
5337 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
5339 static bfd_boolean
5340 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
5342 bfd *sub;
5344 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5346 asection *o;
5348 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5349 continue;
5351 for (o = sub->sections; o != NULL; o = o->next)
5353 /* Keep special sections. Keep .debug sections. */
5354 if ((o->flags & SEC_LINKER_CREATED)
5355 || (o->flags & SEC_DEBUGGING))
5356 o->gc_mark = 1;
5358 if (o->gc_mark)
5359 continue;
5361 /* Skip sweeping sections already excluded. */
5362 if (o->flags & SEC_EXCLUDE)
5363 continue;
5365 /* Since this is early in the link process, it is simple
5366 to remove a section from the output. */
5367 o->flags |= SEC_EXCLUDE;
5369 /* But we also have to update some of the relocation
5370 info we collected before. */
5371 if (gc_sweep_hook
5372 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5374 Elf_Internal_Rela *internal_relocs;
5375 bfd_boolean r;
5377 internal_relocs
5378 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
5379 info->keep_memory);
5380 if (internal_relocs == NULL)
5381 return FALSE;
5383 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5385 if (elf_section_data (o)->relocs != internal_relocs)
5386 free (internal_relocs);
5388 if (!r)
5389 return FALSE;
5394 /* Remove the symbols that were in the swept sections from the dynamic
5395 symbol table. GCFIXME: Anyone know how to get them out of the
5396 static symbol table as well? */
5398 int i = 0;
5400 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
5402 elf_hash_table (info)->dynsymcount = i;
5405 return TRUE;
5408 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5410 static bfd_boolean
5411 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
5413 int *idx = idxptr;
5415 if (h->root.type == bfd_link_hash_warning)
5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5418 if (h->dynindx != -1
5419 && ((h->root.type != bfd_link_hash_defined
5420 && h->root.type != bfd_link_hash_defweak)
5421 || h->root.u.def.section->gc_mark))
5422 h->dynindx = (*idx)++;
5424 return TRUE;
5427 /* Propagate collected vtable information. This is called through
5428 elf_link_hash_traverse. */
5430 static bfd_boolean
5431 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
5433 if (h->root.type == bfd_link_hash_warning)
5434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5436 /* Those that are not vtables. */
5437 if (h->vtable_parent == NULL)
5438 return TRUE;
5440 /* Those vtables that do not have parents, we cannot merge. */
5441 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5442 return TRUE;
5444 /* If we've already been done, exit. */
5445 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5446 return TRUE;
5448 /* Make sure the parent's table is up to date. */
5449 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5451 if (h->vtable_entries_used == NULL)
5453 /* None of this table's entries were referenced. Re-use the
5454 parent's table. */
5455 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5456 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5458 else
5460 size_t n;
5461 bfd_boolean *cu, *pu;
5463 /* Or the parent's entries into ours. */
5464 cu = h->vtable_entries_used;
5465 cu[-1] = TRUE;
5466 pu = h->vtable_parent->vtable_entries_used;
5467 if (pu != NULL)
5469 const struct elf_backend_data *bed;
5470 unsigned int log_file_align;
5472 bed = get_elf_backend_data (h->root.u.def.section->owner);
5473 log_file_align = bed->s->log_file_align;
5474 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5475 while (n--)
5477 if (*pu)
5478 *cu = TRUE;
5479 pu++;
5480 cu++;
5485 return TRUE;
5488 static bfd_boolean
5489 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
5491 asection *sec;
5492 bfd_vma hstart, hend;
5493 Elf_Internal_Rela *relstart, *relend, *rel;
5494 const struct elf_backend_data *bed;
5495 unsigned int log_file_align;
5497 if (h->root.type == bfd_link_hash_warning)
5498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5500 /* Take care of both those symbols that do not describe vtables as
5501 well as those that are not loaded. */
5502 if (h->vtable_parent == NULL)
5503 return TRUE;
5505 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5506 || h->root.type == bfd_link_hash_defweak);
5508 sec = h->root.u.def.section;
5509 hstart = h->root.u.def.value;
5510 hend = hstart + h->size;
5512 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
5513 if (!relstart)
5514 return *(bfd_boolean *) okp = FALSE;
5515 bed = get_elf_backend_data (sec->owner);
5516 log_file_align = bed->s->log_file_align;
5518 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5520 for (rel = relstart; rel < relend; ++rel)
5521 if (rel->r_offset >= hstart && rel->r_offset < hend)
5523 /* If the entry is in use, do nothing. */
5524 if (h->vtable_entries_used
5525 && (rel->r_offset - hstart) < h->vtable_entries_size)
5527 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5528 if (h->vtable_entries_used[entry])
5529 continue;
5531 /* Otherwise, kill it. */
5532 rel->r_offset = rel->r_info = rel->r_addend = 0;
5535 return TRUE;
5538 /* Do mark and sweep of unused sections. */
5540 bfd_boolean
5541 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5543 bfd_boolean ok = TRUE;
5544 bfd *sub;
5545 asection * (*gc_mark_hook)
5546 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5547 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
5549 if (!get_elf_backend_data (abfd)->can_gc_sections
5550 || info->relocatable
5551 || info->emitrelocations
5552 || !is_elf_hash_table (info->hash)
5553 || elf_hash_table (info)->dynamic_sections_created)
5555 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5556 return TRUE;
5559 /* Apply transitive closure to the vtable entry usage info. */
5560 elf_link_hash_traverse (elf_hash_table (info),
5561 elf_gc_propagate_vtable_entries_used,
5562 &ok);
5563 if (!ok)
5564 return FALSE;
5566 /* Kill the vtable relocations that were not used. */
5567 elf_link_hash_traverse (elf_hash_table (info),
5568 elf_gc_smash_unused_vtentry_relocs,
5569 &ok);
5570 if (!ok)
5571 return FALSE;
5573 /* Grovel through relocs to find out who stays ... */
5575 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5576 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5578 asection *o;
5580 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5581 continue;
5583 for (o = sub->sections; o != NULL; o = o->next)
5585 if (o->flags & SEC_KEEP)
5586 if (!elf_gc_mark (info, o, gc_mark_hook))
5587 return FALSE;
5591 /* ... and mark SEC_EXCLUDE for those that go. */
5592 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5593 return FALSE;
5595 return TRUE;
5598 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5600 bfd_boolean
5601 elf_gc_record_vtinherit (bfd *abfd,
5602 asection *sec,
5603 struct elf_link_hash_entry *h,
5604 bfd_vma offset)
5606 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5607 struct elf_link_hash_entry **search, *child;
5608 bfd_size_type extsymcount;
5610 /* The sh_info field of the symtab header tells us where the
5611 external symbols start. We don't care about the local symbols at
5612 this point. */
5613 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5614 if (!elf_bad_symtab (abfd))
5615 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5617 sym_hashes = elf_sym_hashes (abfd);
5618 sym_hashes_end = sym_hashes + extsymcount;
5620 /* Hunt down the child symbol, which is in this section at the same
5621 offset as the relocation. */
5622 for (search = sym_hashes; search != sym_hashes_end; ++search)
5624 if ((child = *search) != NULL
5625 && (child->root.type == bfd_link_hash_defined
5626 || child->root.type == bfd_link_hash_defweak)
5627 && child->root.u.def.section == sec
5628 && child->root.u.def.value == offset)
5629 goto win;
5632 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
5633 bfd_archive_filename (abfd), sec->name,
5634 (unsigned long) offset);
5635 bfd_set_error (bfd_error_invalid_operation);
5636 return FALSE;
5638 win:
5639 if (!h)
5641 /* This *should* only be the absolute section. It could potentially
5642 be that someone has defined a non-global vtable though, which
5643 would be bad. It isn't worth paging in the local symbols to be
5644 sure though; that case should simply be handled by the assembler. */
5646 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5648 else
5649 child->vtable_parent = h;
5651 return TRUE;
5654 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5656 bfd_boolean
5657 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5658 asection *sec ATTRIBUTE_UNUSED,
5659 struct elf_link_hash_entry *h,
5660 bfd_vma addend)
5662 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5663 unsigned int log_file_align = bed->s->log_file_align;
5665 if (addend >= h->vtable_entries_size)
5667 size_t size, bytes, file_align;
5668 bfd_boolean *ptr = h->vtable_entries_used;
5670 /* While the symbol is undefined, we have to be prepared to handle
5671 a zero size. */
5672 file_align = 1 << log_file_align;
5673 if (h->root.type == bfd_link_hash_undefined)
5674 size = addend + file_align;
5675 else
5677 size = h->size;
5678 if (addend >= size)
5680 /* Oops! We've got a reference past the defined end of
5681 the table. This is probably a bug -- shall we warn? */
5682 size = addend + file_align;
5685 size = (size + file_align - 1) & -file_align;
5687 /* Allocate one extra entry for use as a "done" flag for the
5688 consolidation pass. */
5689 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
5691 if (ptr)
5693 ptr = bfd_realloc (ptr - 1, bytes);
5695 if (ptr != NULL)
5697 size_t oldbytes;
5699 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
5700 * sizeof (bfd_boolean));
5701 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
5704 else
5705 ptr = bfd_zmalloc (bytes);
5707 if (ptr == NULL)
5708 return FALSE;
5710 /* And arrange for that done flag to be at index -1. */
5711 h->vtable_entries_used = ptr + 1;
5712 h->vtable_entries_size = size;
5715 h->vtable_entries_used[addend >> log_file_align] = TRUE;
5717 return TRUE;
5720 /* And an accompanying bit to work out final got entry offsets once
5721 we're done. Should be called from final_link. */
5723 bfd_boolean
5724 elf_gc_common_finalize_got_offsets (bfd *abfd,
5725 struct bfd_link_info *info)
5727 bfd *i;
5728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5729 bfd_vma gotoff;
5731 if (! is_elf_hash_table (info->hash))
5732 return FALSE;
5734 /* The GOT offset is relative to the .got section, but the GOT header is
5735 put into the .got.plt section, if the backend uses it. */
5736 if (bed->want_got_plt)
5737 gotoff = 0;
5738 else
5739 gotoff = bed->got_header_size;
5741 /* Do the local .got entries first. */
5742 for (i = info->input_bfds; i; i = i->link_next)
5744 bfd_signed_vma *local_got;
5745 bfd_size_type j, locsymcount;
5746 Elf_Internal_Shdr *symtab_hdr;
5748 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5749 continue;
5751 local_got = elf_local_got_refcounts (i);
5752 if (!local_got)
5753 continue;
5755 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5756 if (elf_bad_symtab (i))
5757 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5758 else
5759 locsymcount = symtab_hdr->sh_info;
5761 for (j = 0; j < locsymcount; ++j)
5763 if (local_got[j] > 0)
5765 local_got[j] = gotoff;
5766 gotoff += ARCH_SIZE / 8;
5768 else
5769 local_got[j] = (bfd_vma) -1;
5773 /* Then the global .got entries. .plt refcounts are handled by
5774 adjust_dynamic_symbol */
5775 elf_link_hash_traverse (elf_hash_table (info),
5776 elf_gc_allocate_got_offsets,
5777 &gotoff);
5778 return TRUE;
5781 /* We need a special top-level link routine to convert got reference counts
5782 to real got offsets. */
5784 static bfd_boolean
5785 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
5787 bfd_vma *off = offarg;
5789 if (h->root.type == bfd_link_hash_warning)
5790 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5792 if (h->got.refcount > 0)
5794 h->got.offset = off[0];
5795 off[0] += ARCH_SIZE / 8;
5797 else
5798 h->got.offset = (bfd_vma) -1;
5800 return TRUE;
5803 /* Many folk need no more in the way of final link than this, once
5804 got entry reference counting is enabled. */
5806 bfd_boolean
5807 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
5809 if (!elf_gc_common_finalize_got_offsets (abfd, info))
5810 return FALSE;
5812 /* Invoke the regular ELF backend linker to do all the work. */
5813 return elf_bfd_final_link (abfd, info);
5816 /* This function will be called though elf_link_hash_traverse to store
5817 all hash value of the exported symbols in an array. */
5819 static bfd_boolean
5820 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5822 unsigned long **valuep = data;
5823 const char *name;
5824 char *p;
5825 unsigned long ha;
5826 char *alc = NULL;
5828 if (h->root.type == bfd_link_hash_warning)
5829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5831 /* Ignore indirect symbols. These are added by the versioning code. */
5832 if (h->dynindx == -1)
5833 return TRUE;
5835 name = h->root.root.string;
5836 p = strchr (name, ELF_VER_CHR);
5837 if (p != NULL)
5839 alc = bfd_malloc (p - name + 1);
5840 memcpy (alc, name, p - name);
5841 alc[p - name] = '\0';
5842 name = alc;
5845 /* Compute the hash value. */
5846 ha = bfd_elf_hash (name);
5848 /* Store the found hash value in the array given as the argument. */
5849 *(*valuep)++ = ha;
5851 /* And store it in the struct so that we can put it in the hash table
5852 later. */
5853 h->elf_hash_value = ha;
5855 if (alc != NULL)
5856 free (alc);
5858 return TRUE;
5861 bfd_boolean
5862 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
5864 struct elf_reloc_cookie *rcookie = cookie;
5866 if (rcookie->bad_symtab)
5867 rcookie->rel = rcookie->rels;
5869 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
5871 unsigned long r_symndx;
5873 if (! rcookie->bad_symtab)
5874 if (rcookie->rel->r_offset > offset)
5875 return FALSE;
5876 if (rcookie->rel->r_offset != offset)
5877 continue;
5879 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
5880 if (r_symndx == SHN_UNDEF)
5881 return TRUE;
5883 if (r_symndx >= rcookie->locsymcount
5884 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
5886 struct elf_link_hash_entry *h;
5888 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
5890 while (h->root.type == bfd_link_hash_indirect
5891 || h->root.type == bfd_link_hash_warning)
5892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5894 if ((h->root.type == bfd_link_hash_defined
5895 || h->root.type == bfd_link_hash_defweak)
5896 && elf_discarded_section (h->root.u.def.section))
5897 return TRUE;
5898 else
5899 return FALSE;
5901 else
5903 /* It's not a relocation against a global symbol,
5904 but it could be a relocation against a local
5905 symbol for a discarded section. */
5906 asection *isec;
5907 Elf_Internal_Sym *isym;
5909 /* Need to: get the symbol; get the section. */
5910 isym = &rcookie->locsyms[r_symndx];
5911 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
5913 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
5914 if (isec != NULL && elf_discarded_section (isec))
5915 return TRUE;
5918 return FALSE;
5920 return FALSE;
5923 /* Discard unneeded references to discarded sections.
5924 Returns TRUE if any section's size was changed. */
5925 /* This function assumes that the relocations are in sorted order,
5926 which is true for all known assemblers. */
5928 bfd_boolean
5929 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
5931 struct elf_reloc_cookie cookie;
5932 asection *stab, *eh;
5933 Elf_Internal_Shdr *symtab_hdr;
5934 const struct elf_backend_data *bed;
5935 bfd *abfd;
5936 unsigned int count;
5937 bfd_boolean ret = FALSE;
5939 if (info->traditional_format
5940 || !is_elf_hash_table (info->hash))
5941 return FALSE;
5943 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
5945 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5946 continue;
5948 bed = get_elf_backend_data (abfd);
5950 if ((abfd->flags & DYNAMIC) != 0)
5951 continue;
5953 eh = bfd_get_section_by_name (abfd, ".eh_frame");
5954 if (info->relocatable
5955 || (eh != NULL
5956 && (eh->_raw_size == 0
5957 || bfd_is_abs_section (eh->output_section))))
5958 eh = NULL;
5960 stab = bfd_get_section_by_name (abfd, ".stab");
5961 if (stab != NULL
5962 && (stab->_raw_size == 0
5963 || bfd_is_abs_section (stab->output_section)
5964 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
5965 stab = NULL;
5967 if (stab == NULL
5968 && eh == NULL
5969 && bed->elf_backend_discard_info == NULL)
5970 continue;
5972 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5973 cookie.abfd = abfd;
5974 cookie.sym_hashes = elf_sym_hashes (abfd);
5975 cookie.bad_symtab = elf_bad_symtab (abfd);
5976 if (cookie.bad_symtab)
5978 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5979 cookie.extsymoff = 0;
5981 else
5983 cookie.locsymcount = symtab_hdr->sh_info;
5984 cookie.extsymoff = symtab_hdr->sh_info;
5987 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5988 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
5990 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5991 cookie.locsymcount, 0,
5992 NULL, NULL, NULL);
5993 if (cookie.locsyms == NULL)
5994 return FALSE;
5997 if (stab != NULL)
5999 cookie.rels = NULL;
6000 count = stab->reloc_count;
6001 if (count != 0)
6002 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6003 info->keep_memory);
6004 if (cookie.rels != NULL)
6006 cookie.rel = cookie.rels;
6007 cookie.relend = cookie.rels;
6008 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6009 if (_bfd_discard_section_stabs (abfd, stab,
6010 elf_section_data (stab)->sec_info,
6011 elf_reloc_symbol_deleted_p,
6012 &cookie))
6013 ret = TRUE;
6014 if (elf_section_data (stab)->relocs != cookie.rels)
6015 free (cookie.rels);
6019 if (eh != NULL)
6021 cookie.rels = NULL;
6022 count = eh->reloc_count;
6023 if (count != 0)
6024 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6025 info->keep_memory);
6026 cookie.rel = cookie.rels;
6027 cookie.relend = cookie.rels;
6028 if (cookie.rels != NULL)
6029 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6031 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6032 elf_reloc_symbol_deleted_p,
6033 &cookie))
6034 ret = TRUE;
6036 if (cookie.rels != NULL
6037 && elf_section_data (eh)->relocs != cookie.rels)
6038 free (cookie.rels);
6041 if (bed->elf_backend_discard_info != NULL
6042 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6043 ret = TRUE;
6045 if (cookie.locsyms != NULL
6046 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6048 if (! info->keep_memory)
6049 free (cookie.locsyms);
6050 else
6051 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6055 if (info->eh_frame_hdr
6056 && !info->relocatable
6057 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6058 ret = TRUE;
6060 return ret;
6063 static bfd_boolean
6064 elf_section_ignore_discarded_relocs (asection *sec)
6066 const struct elf_backend_data *bed;
6068 switch (sec->sec_info_type)
6070 case ELF_INFO_TYPE_STABS:
6071 case ELF_INFO_TYPE_EH_FRAME:
6072 return TRUE;
6073 default:
6074 break;
6077 bed = get_elf_backend_data (sec->owner);
6078 if (bed->elf_backend_ignore_discarded_relocs != NULL
6079 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6080 return TRUE;
6082 return FALSE;