* gas/config/tc-arm.c (do_vfp_reg2_from_sp2): Rename from
[binutils.git] / bfd / elflink.h
blobc37d7398d0113af7c59984a651e4219b7e04532d
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_link_add_archive_symbols (bfd *,
27 struct bfd_link_info *);
28 static bfd_boolean elf_finalize_dynstr (bfd *, struct bfd_link_info *);
29 static bfd_boolean elf_collect_hash_codes (struct elf_link_hash_entry *,
30 void *);
31 static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
33 /* Given an ELF BFD, add symbols to the global hash table as
34 appropriate. */
36 bfd_boolean
37 elf_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
39 switch (bfd_get_format (abfd))
41 case bfd_object:
42 return elf_link_add_object_symbols (abfd, info);
43 case bfd_archive:
44 return elf_link_add_archive_symbols (abfd, info);
45 default:
46 bfd_set_error (bfd_error_wrong_format);
47 return FALSE;
51 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
52 static bfd_boolean
53 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
54 Elf_Internal_Sym *sym)
56 /* Local symbols do not count, but target specific ones might. */
57 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
58 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
59 return FALSE;
61 /* Function symbols do not count. */
62 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
63 return FALSE;
65 /* If the section is undefined, then so is the symbol. */
66 if (sym->st_shndx == SHN_UNDEF)
67 return FALSE;
69 /* If the symbol is defined in the common section, then
70 it is a common definition and so does not count. */
71 if (sym->st_shndx == SHN_COMMON)
72 return FALSE;
74 /* If the symbol is in a target specific section then we
75 must rely upon the backend to tell us what it is. */
76 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
77 /* FIXME - this function is not coded yet:
79 return _bfd_is_global_symbol_definition (abfd, sym);
81 Instead for now assume that the definition is not global,
82 Even if this is wrong, at least the linker will behave
83 in the same way that it used to do. */
84 return FALSE;
86 return TRUE;
89 /* Search the symbol table of the archive element of the archive ABFD
90 whose archive map contains a mention of SYMDEF, and determine if
91 the symbol is defined in this element. */
92 static bfd_boolean
93 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
95 Elf_Internal_Shdr * hdr;
96 bfd_size_type symcount;
97 bfd_size_type extsymcount;
98 bfd_size_type extsymoff;
99 Elf_Internal_Sym *isymbuf;
100 Elf_Internal_Sym *isym;
101 Elf_Internal_Sym *isymend;
102 bfd_boolean result;
104 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
105 if (abfd == NULL)
106 return FALSE;
108 if (! bfd_check_format (abfd, bfd_object))
109 return FALSE;
111 /* If we have already included the element containing this symbol in the
112 link then we do not need to include it again. Just claim that any symbol
113 it contains is not a definition, so that our caller will not decide to
114 (re)include this element. */
115 if (abfd->archive_pass)
116 return FALSE;
118 /* Select the appropriate symbol table. */
119 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
120 hdr = &elf_tdata (abfd)->symtab_hdr;
121 else
122 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
124 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
126 /* The sh_info field of the symtab header tells us where the
127 external symbols start. We don't care about the local symbols. */
128 if (elf_bad_symtab (abfd))
130 extsymcount = symcount;
131 extsymoff = 0;
133 else
135 extsymcount = symcount - hdr->sh_info;
136 extsymoff = hdr->sh_info;
139 if (extsymcount == 0)
140 return FALSE;
142 /* Read in the symbol table. */
143 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
144 NULL, NULL, NULL);
145 if (isymbuf == NULL)
146 return FALSE;
148 /* Scan the symbol table looking for SYMDEF. */
149 result = FALSE;
150 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
152 const char *name;
154 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
155 isym->st_name);
156 if (name == NULL)
157 break;
159 if (strcmp (name, symdef->name) == 0)
161 result = is_global_data_symbol_definition (abfd, isym);
162 break;
166 free (isymbuf);
168 return result;
171 /* Add symbols from an ELF archive file to the linker hash table. We
172 don't use _bfd_generic_link_add_archive_symbols because of a
173 problem which arises on UnixWare. The UnixWare libc.so is an
174 archive which includes an entry libc.so.1 which defines a bunch of
175 symbols. The libc.so archive also includes a number of other
176 object files, which also define symbols, some of which are the same
177 as those defined in libc.so.1. Correct linking requires that we
178 consider each object file in turn, and include it if it defines any
179 symbols we need. _bfd_generic_link_add_archive_symbols does not do
180 this; it looks through the list of undefined symbols, and includes
181 any object file which defines them. When this algorithm is used on
182 UnixWare, it winds up pulling in libc.so.1 early and defining a
183 bunch of symbols. This means that some of the other objects in the
184 archive are not included in the link, which is incorrect since they
185 precede libc.so.1 in the archive.
187 Fortunately, ELF archive handling is simpler than that done by
188 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
189 oddities. In ELF, if we find a symbol in the archive map, and the
190 symbol is currently undefined, we know that we must pull in that
191 object file.
193 Unfortunately, we do have to make multiple passes over the symbol
194 table until nothing further is resolved. */
196 static bfd_boolean
197 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
199 symindex c;
200 bfd_boolean *defined = NULL;
201 bfd_boolean *included = NULL;
202 carsym *symdefs;
203 bfd_boolean loop;
204 bfd_size_type amt;
206 if (! bfd_has_map (abfd))
208 /* An empty archive is a special case. */
209 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
210 return TRUE;
211 bfd_set_error (bfd_error_no_armap);
212 return FALSE;
215 /* Keep track of all symbols we know to be already defined, and all
216 files we know to be already included. This is to speed up the
217 second and subsequent passes. */
218 c = bfd_ardata (abfd)->symdef_count;
219 if (c == 0)
220 return TRUE;
221 amt = c;
222 amt *= sizeof (bfd_boolean);
223 defined = bfd_zmalloc (amt);
224 included = bfd_zmalloc (amt);
225 if (defined == NULL || included == NULL)
226 goto error_return;
228 symdefs = bfd_ardata (abfd)->symdefs;
232 file_ptr last;
233 symindex i;
234 carsym *symdef;
235 carsym *symdefend;
237 loop = FALSE;
238 last = -1;
240 symdef = symdefs;
241 symdefend = symdef + c;
242 for (i = 0; symdef < symdefend; symdef++, i++)
244 struct elf_link_hash_entry *h;
245 bfd *element;
246 struct bfd_link_hash_entry *undefs_tail;
247 symindex mark;
249 if (defined[i] || included[i])
250 continue;
251 if (symdef->file_offset == last)
253 included[i] = TRUE;
254 continue;
257 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
258 FALSE, FALSE, FALSE);
260 if (h == NULL)
262 char *p, *copy;
263 size_t len, first;
265 /* If this is a default version (the name contains @@),
266 look up the symbol again with only one `@' as well
267 as without the version. The effect is that references
268 to the symbol with and without the version will be
269 matched by the default symbol in the archive. */
271 p = strchr (symdef->name, ELF_VER_CHR);
272 if (p == NULL || p[1] != ELF_VER_CHR)
273 continue;
275 /* First check with only one `@'. */
276 len = strlen (symdef->name);
277 copy = bfd_alloc (abfd, len);
278 if (copy == NULL)
279 goto error_return;
280 first = p - symdef->name + 1;
281 memcpy (copy, symdef->name, first);
282 memcpy (copy + first, symdef->name + first + 1, len - first);
284 h = elf_link_hash_lookup (elf_hash_table (info), copy,
285 FALSE, FALSE, FALSE);
287 if (h == NULL)
289 /* We also need to check references to the symbol
290 without the version. */
292 copy[first - 1] = '\0';
293 h = elf_link_hash_lookup (elf_hash_table (info),
294 copy, FALSE, FALSE, FALSE);
297 bfd_release (abfd, copy);
300 if (h == NULL)
301 continue;
303 if (h->root.type == bfd_link_hash_common)
305 /* We currently have a common symbol. The archive map contains
306 a reference to this symbol, so we may want to include it. We
307 only want to include it however, if this archive element
308 contains a definition of the symbol, not just another common
309 declaration of it.
311 Unfortunately some archivers (including GNU ar) will put
312 declarations of common symbols into their archive maps, as
313 well as real definitions, so we cannot just go by the archive
314 map alone. Instead we must read in the element's symbol
315 table and check that to see what kind of symbol definition
316 this is. */
317 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
318 continue;
320 else if (h->root.type != bfd_link_hash_undefined)
322 if (h->root.type != bfd_link_hash_undefweak)
323 defined[i] = TRUE;
324 continue;
327 /* We need to include this archive member. */
328 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
329 if (element == NULL)
330 goto error_return;
332 if (! bfd_check_format (element, bfd_object))
333 goto error_return;
335 /* Doublecheck that we have not included this object
336 already--it should be impossible, but there may be
337 something wrong with the archive. */
338 if (element->archive_pass != 0)
340 bfd_set_error (bfd_error_bad_value);
341 goto error_return;
343 element->archive_pass = 1;
345 undefs_tail = info->hash->undefs_tail;
347 if (! (*info->callbacks->add_archive_element) (info, element,
348 symdef->name))
349 goto error_return;
350 if (! elf_link_add_object_symbols (element, info))
351 goto error_return;
353 /* If there are any new undefined symbols, we need to make
354 another pass through the archive in order to see whether
355 they can be defined. FIXME: This isn't perfect, because
356 common symbols wind up on undefs_tail and because an
357 undefined symbol which is defined later on in this pass
358 does not require another pass. This isn't a bug, but it
359 does make the code less efficient than it could be. */
360 if (undefs_tail != info->hash->undefs_tail)
361 loop = TRUE;
363 /* Look backward to mark all symbols from this object file
364 which we have already seen in this pass. */
365 mark = i;
368 included[mark] = TRUE;
369 if (mark == 0)
370 break;
371 --mark;
373 while (symdefs[mark].file_offset == symdef->file_offset);
375 /* We mark subsequent symbols from this object file as we go
376 on through the loop. */
377 last = symdef->file_offset;
380 while (loop);
382 free (defined);
383 free (included);
385 return TRUE;
387 error_return:
388 if (defined != NULL)
389 free (defined);
390 if (included != NULL)
391 free (included);
392 return FALSE;
395 /* Add symbols from an ELF object file to the linker hash table. */
397 static bfd_boolean
398 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
400 bfd_boolean (*add_symbol_hook)
401 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
402 const char **, flagword *, asection **, bfd_vma *);
403 bfd_boolean (*check_relocs)
404 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
405 bfd_boolean collect;
406 Elf_Internal_Shdr *hdr;
407 bfd_size_type symcount;
408 bfd_size_type extsymcount;
409 bfd_size_type extsymoff;
410 struct elf_link_hash_entry **sym_hash;
411 bfd_boolean dynamic;
412 Elf_External_Versym *extversym = NULL;
413 Elf_External_Versym *ever;
414 struct elf_link_hash_entry *weaks;
415 struct elf_link_hash_entry **nondeflt_vers = NULL;
416 bfd_size_type nondeflt_vers_cnt = 0;
417 Elf_Internal_Sym *isymbuf = NULL;
418 Elf_Internal_Sym *isym;
419 Elf_Internal_Sym *isymend;
420 const struct elf_backend_data *bed;
421 bfd_boolean dt_needed;
422 struct elf_link_hash_table * hash_table;
423 bfd_size_type amt;
425 hash_table = elf_hash_table (info);
427 bed = get_elf_backend_data (abfd);
428 add_symbol_hook = bed->elf_add_symbol_hook;
429 collect = bed->collect;
431 if ((abfd->flags & DYNAMIC) == 0)
432 dynamic = FALSE;
433 else
435 dynamic = TRUE;
437 /* You can't use -r against a dynamic object. Also, there's no
438 hope of using a dynamic object which does not exactly match
439 the format of the output file. */
440 if (info->relocatable
441 || !is_elf_hash_table (hash_table)
442 || hash_table->root.creator != abfd->xvec)
444 bfd_set_error (bfd_error_invalid_operation);
445 goto error_return;
449 /* As a GNU extension, any input sections which are named
450 .gnu.warning.SYMBOL are treated as warning symbols for the given
451 symbol. This differs from .gnu.warning sections, which generate
452 warnings when they are included in an output file. */
453 if (info->executable)
455 asection *s;
457 for (s = abfd->sections; s != NULL; s = s->next)
459 const char *name;
461 name = bfd_get_section_name (abfd, s);
462 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
464 char *msg;
465 bfd_size_type sz;
466 bfd_size_type prefix_len;
467 const char * gnu_warning_prefix = _("warning: ");
469 name += sizeof ".gnu.warning." - 1;
471 /* If this is a shared object, then look up the symbol
472 in the hash table. If it is there, and it is already
473 been defined, then we will not be using the entry
474 from this shared object, so we don't need to warn.
475 FIXME: If we see the definition in a regular object
476 later on, we will warn, but we shouldn't. The only
477 fix is to keep track of what warnings we are supposed
478 to emit, and then handle them all at the end of the
479 link. */
480 if (dynamic)
482 struct elf_link_hash_entry *h;
484 h = elf_link_hash_lookup (hash_table, name,
485 FALSE, FALSE, TRUE);
487 /* FIXME: What about bfd_link_hash_common? */
488 if (h != NULL
489 && (h->root.type == bfd_link_hash_defined
490 || h->root.type == bfd_link_hash_defweak))
492 /* We don't want to issue this warning. Clobber
493 the section size so that the warning does not
494 get copied into the output file. */
495 s->_raw_size = 0;
496 continue;
500 sz = bfd_section_size (abfd, s);
501 prefix_len = strlen (gnu_warning_prefix);
502 msg = bfd_alloc (abfd, prefix_len + sz + 1);
503 if (msg == NULL)
504 goto error_return;
506 strcpy (msg, gnu_warning_prefix);
507 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
508 goto error_return;
510 msg[prefix_len + sz] = '\0';
512 if (! (_bfd_generic_link_add_one_symbol
513 (info, abfd, name, BSF_WARNING, s, 0, msg,
514 FALSE, collect, NULL)))
515 goto error_return;
517 if (! info->relocatable)
519 /* Clobber the section size so that the warning does
520 not get copied into the output file. */
521 s->_raw_size = 0;
527 dt_needed = FALSE;
528 if (! dynamic)
530 /* If we are creating a shared library, create all the dynamic
531 sections immediately. We need to attach them to something,
532 so we attach them to this BFD, provided it is the right
533 format. FIXME: If there are no input BFD's of the same
534 format as the output, we can't make a shared library. */
535 if (info->shared
536 && is_elf_hash_table (hash_table)
537 && hash_table->root.creator == abfd->xvec
538 && ! hash_table->dynamic_sections_created)
540 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
541 goto error_return;
544 else if (!is_elf_hash_table (hash_table))
545 goto error_return;
546 else
548 asection *s;
549 bfd_boolean add_needed;
550 const char *name;
551 bfd_size_type oldsize;
552 bfd_size_type strindex;
553 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
555 /* ld --just-symbols and dynamic objects don't mix very well.
556 Test for --just-symbols by looking at info set up by
557 _bfd_elf_link_just_syms. */
558 if ((s = abfd->sections) != NULL
559 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
560 goto error_return;
562 /* Find the name to use in a DT_NEEDED entry that refers to this
563 object. If the object has a DT_SONAME entry, we use it.
564 Otherwise, if the generic linker stuck something in
565 elf_dt_name, we use that. Otherwise, we just use the file
566 name. If the generic linker put a null string into
567 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
568 there is a DT_SONAME entry. */
569 add_needed = TRUE;
570 name = bfd_get_filename (abfd);
571 if (elf_dt_name (abfd) != NULL)
573 name = elf_dt_name (abfd);
574 if (*name == '\0')
576 if (elf_dt_soname (abfd) != NULL)
577 dt_needed = TRUE;
579 add_needed = FALSE;
582 s = bfd_get_section_by_name (abfd, ".dynamic");
583 if (s != NULL)
585 Elf_External_Dyn *dynbuf = NULL;
586 Elf_External_Dyn *extdyn;
587 Elf_External_Dyn *extdynend;
588 int elfsec;
589 unsigned long shlink;
591 dynbuf = bfd_malloc (s->_raw_size);
592 if (dynbuf == NULL)
593 goto error_return;
595 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
596 goto error_free_dyn;
598 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
599 if (elfsec == -1)
600 goto error_free_dyn;
601 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
603 extdyn = dynbuf;
604 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
605 for (; extdyn < extdynend; extdyn++)
607 Elf_Internal_Dyn dyn;
609 elf_swap_dyn_in (abfd, extdyn, &dyn);
610 if (dyn.d_tag == DT_SONAME)
612 unsigned int tagv = dyn.d_un.d_val;
613 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
614 if (name == NULL)
615 goto error_free_dyn;
617 if (dyn.d_tag == DT_NEEDED)
619 struct bfd_link_needed_list *n, **pn;
620 char *fnm, *anm;
621 unsigned int tagv = dyn.d_un.d_val;
623 amt = sizeof (struct bfd_link_needed_list);
624 n = bfd_alloc (abfd, amt);
625 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
626 if (n == NULL || fnm == NULL)
627 goto error_free_dyn;
628 amt = strlen (fnm) + 1;
629 anm = bfd_alloc (abfd, amt);
630 if (anm == NULL)
631 goto error_free_dyn;
632 memcpy (anm, fnm, amt);
633 n->name = anm;
634 n->by = abfd;
635 n->next = NULL;
636 for (pn = & hash_table->needed;
637 *pn != NULL;
638 pn = &(*pn)->next)
640 *pn = n;
642 if (dyn.d_tag == DT_RUNPATH)
644 struct bfd_link_needed_list *n, **pn;
645 char *fnm, *anm;
646 unsigned int tagv = dyn.d_un.d_val;
648 amt = sizeof (struct bfd_link_needed_list);
649 n = bfd_alloc (abfd, amt);
650 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
651 if (n == NULL || fnm == NULL)
652 goto error_free_dyn;
653 amt = strlen (fnm) + 1;
654 anm = bfd_alloc (abfd, amt);
655 if (anm == NULL)
656 goto error_free_dyn;
657 memcpy (anm, fnm, amt);
658 n->name = anm;
659 n->by = abfd;
660 n->next = NULL;
661 for (pn = & runpath;
662 *pn != NULL;
663 pn = &(*pn)->next)
665 *pn = n;
667 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
668 if (!runpath && dyn.d_tag == DT_RPATH)
670 struct bfd_link_needed_list *n, **pn;
671 char *fnm, *anm;
672 unsigned int tagv = dyn.d_un.d_val;
674 amt = sizeof (struct bfd_link_needed_list);
675 n = bfd_alloc (abfd, amt);
676 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
677 if (n == NULL || fnm == NULL)
678 goto error_free_dyn;
679 amt = strlen (fnm) + 1;
680 anm = bfd_alloc (abfd, amt);
681 if (anm == NULL)
683 error_free_dyn:
684 free (dynbuf);
685 goto error_return;
687 memcpy (anm, fnm, amt);
688 n->name = anm;
689 n->by = abfd;
690 n->next = NULL;
691 for (pn = & rpath;
692 *pn != NULL;
693 pn = &(*pn)->next)
695 *pn = n;
699 free (dynbuf);
702 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
703 frees all more recently bfd_alloc'd blocks as well. */
704 if (runpath)
705 rpath = runpath;
707 if (rpath)
709 struct bfd_link_needed_list **pn;
710 for (pn = & hash_table->runpath;
711 *pn != NULL;
712 pn = &(*pn)->next)
714 *pn = rpath;
717 /* We do not want to include any of the sections in a dynamic
718 object in the output file. We hack by simply clobbering the
719 list of sections in the BFD. This could be handled more
720 cleanly by, say, a new section flag; the existing
721 SEC_NEVER_LOAD flag is not the one we want, because that one
722 still implies that the section takes up space in the output
723 file. */
724 bfd_section_list_clear (abfd);
726 /* If this is the first dynamic object found in the link, create
727 the special sections required for dynamic linking. */
728 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
729 goto error_return;
731 if (add_needed)
733 /* Add a DT_NEEDED entry for this dynamic object. */
734 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
735 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
736 if (strindex == (bfd_size_type) -1)
737 goto error_return;
739 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
741 asection *sdyn;
742 Elf_External_Dyn *dyncon, *dynconend;
744 /* The hash table size did not change, which means that
745 the dynamic object name was already entered. If we
746 have already included this dynamic object in the
747 link, just ignore it. There is no reason to include
748 a particular dynamic object more than once. */
749 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
750 BFD_ASSERT (sdyn != NULL);
752 dyncon = (Elf_External_Dyn *) sdyn->contents;
753 dynconend = (Elf_External_Dyn *) (sdyn->contents +
754 sdyn->_raw_size);
755 for (; dyncon < dynconend; dyncon++)
757 Elf_Internal_Dyn dyn;
759 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
760 if (dyn.d_tag == DT_NEEDED
761 && dyn.d_un.d_val == strindex)
763 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
764 return TRUE;
769 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
770 goto error_return;
773 /* Save the SONAME, if there is one, because sometimes the
774 linker emulation code will need to know it. */
775 if (*name == '\0')
776 name = basename (bfd_get_filename (abfd));
777 elf_dt_name (abfd) = name;
780 /* If this is a dynamic object, we always link against the .dynsym
781 symbol table, not the .symtab symbol table. The dynamic linker
782 will only see the .dynsym symbol table, so there is no reason to
783 look at .symtab for a dynamic object. */
785 if (! dynamic || elf_dynsymtab (abfd) == 0)
786 hdr = &elf_tdata (abfd)->symtab_hdr;
787 else
788 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
790 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
792 /* The sh_info field of the symtab header tells us where the
793 external symbols start. We don't care about the local symbols at
794 this point. */
795 if (elf_bad_symtab (abfd))
797 extsymcount = symcount;
798 extsymoff = 0;
800 else
802 extsymcount = symcount - hdr->sh_info;
803 extsymoff = hdr->sh_info;
806 sym_hash = NULL;
807 if (extsymcount != 0)
809 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
810 NULL, NULL, NULL);
811 if (isymbuf == NULL)
812 goto error_return;
814 /* We store a pointer to the hash table entry for each external
815 symbol. */
816 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
817 sym_hash = bfd_alloc (abfd, amt);
818 if (sym_hash == NULL)
819 goto error_free_sym;
820 elf_sym_hashes (abfd) = sym_hash;
823 if (dynamic)
825 /* Read in any version definitions. */
826 if (! _bfd_elf_slurp_version_tables (abfd))
827 goto error_free_sym;
829 /* Read in the symbol versions, but don't bother to convert them
830 to internal format. */
831 if (elf_dynversym (abfd) != 0)
833 Elf_Internal_Shdr *versymhdr;
835 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
836 extversym = bfd_malloc (versymhdr->sh_size);
837 if (extversym == NULL)
838 goto error_free_sym;
839 amt = versymhdr->sh_size;
840 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
841 || bfd_bread (extversym, amt, abfd) != amt)
842 goto error_free_vers;
846 weaks = NULL;
848 ever = extversym != NULL ? extversym + extsymoff : NULL;
849 for (isym = isymbuf, isymend = isymbuf + extsymcount;
850 isym < isymend;
851 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
853 int bind;
854 bfd_vma value;
855 asection *sec;
856 flagword flags;
857 const char *name;
858 struct elf_link_hash_entry *h;
859 bfd_boolean definition;
860 bfd_boolean size_change_ok;
861 bfd_boolean type_change_ok;
862 bfd_boolean new_weakdef;
863 bfd_boolean override;
864 unsigned int old_alignment;
865 bfd *old_bfd;
867 override = FALSE;
869 flags = BSF_NO_FLAGS;
870 sec = NULL;
871 value = isym->st_value;
872 *sym_hash = NULL;
874 bind = ELF_ST_BIND (isym->st_info);
875 if (bind == STB_LOCAL)
877 /* This should be impossible, since ELF requires that all
878 global symbols follow all local symbols, and that sh_info
879 point to the first global symbol. Unfortunately, Irix 5
880 screws this up. */
881 continue;
883 else if (bind == STB_GLOBAL)
885 if (isym->st_shndx != SHN_UNDEF
886 && isym->st_shndx != SHN_COMMON)
887 flags = BSF_GLOBAL;
889 else if (bind == STB_WEAK)
890 flags = BSF_WEAK;
891 else
893 /* Leave it up to the processor backend. */
896 if (isym->st_shndx == SHN_UNDEF)
897 sec = bfd_und_section_ptr;
898 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
900 sec = section_from_elf_index (abfd, isym->st_shndx);
901 if (sec == NULL)
902 sec = bfd_abs_section_ptr;
903 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
904 value -= sec->vma;
906 else if (isym->st_shndx == SHN_ABS)
907 sec = bfd_abs_section_ptr;
908 else if (isym->st_shndx == SHN_COMMON)
910 sec = bfd_com_section_ptr;
911 /* What ELF calls the size we call the value. What ELF
912 calls the value we call the alignment. */
913 value = isym->st_size;
915 else
917 /* Leave it up to the processor backend. */
920 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
921 isym->st_name);
922 if (name == NULL)
923 goto error_free_vers;
925 if (isym->st_shndx == SHN_COMMON
926 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
928 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
930 if (tcomm == NULL)
932 tcomm = bfd_make_section (abfd, ".tcommon");
933 if (tcomm == NULL
934 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
935 | SEC_IS_COMMON
936 | SEC_LINKER_CREATED
937 | SEC_THREAD_LOCAL)))
938 goto error_free_vers;
940 sec = tcomm;
942 else if (add_symbol_hook)
944 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
945 &value))
946 goto error_free_vers;
948 /* The hook function sets the name to NULL if this symbol
949 should be skipped for some reason. */
950 if (name == NULL)
951 continue;
954 /* Sanity check that all possibilities were handled. */
955 if (sec == NULL)
957 bfd_set_error (bfd_error_bad_value);
958 goto error_free_vers;
961 if (bfd_is_und_section (sec)
962 || bfd_is_com_section (sec))
963 definition = FALSE;
964 else
965 definition = TRUE;
967 size_change_ok = FALSE;
968 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
969 old_alignment = 0;
970 old_bfd = NULL;
972 if (is_elf_hash_table (hash_table))
974 Elf_Internal_Versym iver;
975 unsigned int vernum = 0;
976 bfd_boolean skip;
978 if (ever != NULL)
980 _bfd_elf_swap_versym_in (abfd, ever, &iver);
981 vernum = iver.vs_vers & VERSYM_VERSION;
983 /* If this is a hidden symbol, or if it is not version
984 1, we append the version name to the symbol name.
985 However, we do not modify a non-hidden absolute
986 symbol, because it might be the version symbol
987 itself. FIXME: What if it isn't? */
988 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
989 || (vernum > 1 && ! bfd_is_abs_section (sec)))
991 const char *verstr;
992 size_t namelen, verlen, newlen;
993 char *newname, *p;
995 if (isym->st_shndx != SHN_UNDEF)
997 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
999 (*_bfd_error_handler)
1000 (_("%s: %s: invalid version %u (max %d)"),
1001 bfd_archive_filename (abfd), name, vernum,
1002 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1003 bfd_set_error (bfd_error_bad_value);
1004 goto error_free_vers;
1006 else if (vernum > 1)
1007 verstr =
1008 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1009 else
1010 verstr = "";
1012 else
1014 /* We cannot simply test for the number of
1015 entries in the VERNEED section since the
1016 numbers for the needed versions do not start
1017 at 0. */
1018 Elf_Internal_Verneed *t;
1020 verstr = NULL;
1021 for (t = elf_tdata (abfd)->verref;
1022 t != NULL;
1023 t = t->vn_nextref)
1025 Elf_Internal_Vernaux *a;
1027 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1029 if (a->vna_other == vernum)
1031 verstr = a->vna_nodename;
1032 break;
1035 if (a != NULL)
1036 break;
1038 if (verstr == NULL)
1040 (*_bfd_error_handler)
1041 (_("%s: %s: invalid needed version %d"),
1042 bfd_archive_filename (abfd), name, vernum);
1043 bfd_set_error (bfd_error_bad_value);
1044 goto error_free_vers;
1048 namelen = strlen (name);
1049 verlen = strlen (verstr);
1050 newlen = namelen + verlen + 2;
1051 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1052 && isym->st_shndx != SHN_UNDEF)
1053 ++newlen;
1055 newname = bfd_alloc (abfd, newlen);
1056 if (newname == NULL)
1057 goto error_free_vers;
1058 memcpy (newname, name, namelen);
1059 p = newname + namelen;
1060 *p++ = ELF_VER_CHR;
1061 /* If this is a defined non-hidden version symbol,
1062 we add another @ to the name. This indicates the
1063 default version of the symbol. */
1064 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1065 && isym->st_shndx != SHN_UNDEF)
1066 *p++ = ELF_VER_CHR;
1067 memcpy (p, verstr, verlen + 1);
1069 name = newname;
1073 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1074 sym_hash, &skip, &override,
1075 &type_change_ok, &size_change_ok,
1076 dt_needed))
1077 goto error_free_vers;
1079 if (skip)
1080 continue;
1082 if (override)
1083 definition = FALSE;
1085 h = *sym_hash;
1086 while (h->root.type == bfd_link_hash_indirect
1087 || h->root.type == bfd_link_hash_warning)
1088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1090 /* Remember the old alignment if this is a common symbol, so
1091 that we don't reduce the alignment later on. We can't
1092 check later, because _bfd_generic_link_add_one_symbol
1093 will set a default for the alignment which we want to
1094 override. We also remember the old bfd where the existing
1095 definition comes from. */
1096 switch (h->root.type)
1098 default:
1099 break;
1101 case bfd_link_hash_defined:
1102 case bfd_link_hash_defweak:
1103 old_bfd = h->root.u.def.section->owner;
1104 break;
1106 case bfd_link_hash_common:
1107 old_bfd = h->root.u.c.p->section->owner;
1108 old_alignment = h->root.u.c.p->alignment_power;
1109 break;
1112 if (elf_tdata (abfd)->verdef != NULL
1113 && ! override
1114 && vernum > 1
1115 && definition)
1116 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1119 if (! (_bfd_generic_link_add_one_symbol
1120 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
1121 (struct bfd_link_hash_entry **) sym_hash)))
1122 goto error_free_vers;
1124 h = *sym_hash;
1125 while (h->root.type == bfd_link_hash_indirect
1126 || h->root.type == bfd_link_hash_warning)
1127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1128 *sym_hash = h;
1130 new_weakdef = FALSE;
1131 if (dynamic
1132 && definition
1133 && (flags & BSF_WEAK) != 0
1134 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
1135 && is_elf_hash_table (hash_table)
1136 && h->weakdef == NULL)
1138 /* Keep a list of all weak defined non function symbols from
1139 a dynamic object, using the weakdef field. Later in this
1140 function we will set the weakdef field to the correct
1141 value. We only put non-function symbols from dynamic
1142 objects on this list, because that happens to be the only
1143 time we need to know the normal symbol corresponding to a
1144 weak symbol, and the information is time consuming to
1145 figure out. If the weakdef field is not already NULL,
1146 then this symbol was already defined by some previous
1147 dynamic object, and we will be using that previous
1148 definition anyhow. */
1150 h->weakdef = weaks;
1151 weaks = h;
1152 new_weakdef = TRUE;
1155 /* Set the alignment of a common symbol. */
1156 if (isym->st_shndx == SHN_COMMON
1157 && h->root.type == bfd_link_hash_common)
1159 unsigned int align;
1161 align = bfd_log2 (isym->st_value);
1162 if (align > old_alignment
1163 /* Permit an alignment power of zero if an alignment of one
1164 is specified and no other alignments have been specified. */
1165 || (isym->st_value == 1 && old_alignment == 0))
1166 h->root.u.c.p->alignment_power = align;
1167 else
1168 h->root.u.c.p->alignment_power = old_alignment;
1171 if (is_elf_hash_table (hash_table))
1173 int old_flags;
1174 bfd_boolean dynsym;
1175 int new_flag;
1177 /* Check the alignment when a common symbol is involved. This
1178 can change when a common symbol is overridden by a normal
1179 definition or a common symbol is ignored due to the old
1180 normal definition. We need to make sure the maximum
1181 alignment is maintained. */
1182 if ((old_alignment || isym->st_shndx == SHN_COMMON)
1183 && h->root.type != bfd_link_hash_common)
1185 unsigned int common_align;
1186 unsigned int normal_align;
1187 unsigned int symbol_align;
1188 bfd *normal_bfd;
1189 bfd *common_bfd;
1191 symbol_align = ffs (h->root.u.def.value) - 1;
1192 if (h->root.u.def.section->owner != NULL
1193 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
1195 normal_align = h->root.u.def.section->alignment_power;
1196 if (normal_align > symbol_align)
1197 normal_align = symbol_align;
1199 else
1200 normal_align = symbol_align;
1202 if (old_alignment)
1204 common_align = old_alignment;
1205 common_bfd = old_bfd;
1206 normal_bfd = abfd;
1208 else
1210 common_align = bfd_log2 (isym->st_value);
1211 common_bfd = abfd;
1212 normal_bfd = old_bfd;
1215 if (normal_align < common_align)
1216 (*_bfd_error_handler)
1217 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1218 1 << normal_align,
1219 name,
1220 bfd_archive_filename (normal_bfd),
1221 1 << common_align,
1222 bfd_archive_filename (common_bfd));
1225 /* Remember the symbol size and type. */
1226 if (isym->st_size != 0
1227 && (definition || h->size == 0))
1229 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
1230 (*_bfd_error_handler)
1231 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1232 name, (unsigned long) h->size,
1233 bfd_archive_filename (old_bfd),
1234 (unsigned long) isym->st_size,
1235 bfd_archive_filename (abfd));
1237 h->size = isym->st_size;
1240 /* If this is a common symbol, then we always want H->SIZE
1241 to be the size of the common symbol. The code just above
1242 won't fix the size if a common symbol becomes larger. We
1243 don't warn about a size change here, because that is
1244 covered by --warn-common. */
1245 if (h->root.type == bfd_link_hash_common)
1246 h->size = h->root.u.c.size;
1248 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
1249 && (definition || h->type == STT_NOTYPE))
1251 if (h->type != STT_NOTYPE
1252 && h->type != ELF_ST_TYPE (isym->st_info)
1253 && ! type_change_ok)
1254 (*_bfd_error_handler)
1255 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1256 name, h->type, ELF_ST_TYPE (isym->st_info),
1257 bfd_archive_filename (abfd));
1259 h->type = ELF_ST_TYPE (isym->st_info);
1262 /* If st_other has a processor-specific meaning, specific
1263 code might be needed here. We never merge the visibility
1264 attribute with the one from a dynamic object. */
1265 if (bed->elf_backend_merge_symbol_attribute)
1266 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1267 dynamic);
1269 if (isym->st_other != 0 && !dynamic)
1271 unsigned char hvis, symvis, other, nvis;
1273 /* Take the balance of OTHER from the definition. */
1274 other = (definition ? isym->st_other : h->other);
1275 other &= ~ ELF_ST_VISIBILITY (-1);
1277 /* Combine visibilities, using the most constraining one. */
1278 hvis = ELF_ST_VISIBILITY (h->other);
1279 symvis = ELF_ST_VISIBILITY (isym->st_other);
1280 if (! hvis)
1281 nvis = symvis;
1282 else if (! symvis)
1283 nvis = hvis;
1284 else
1285 nvis = hvis < symvis ? hvis : symvis;
1287 h->other = other | nvis;
1290 /* Set a flag in the hash table entry indicating the type of
1291 reference or definition we just found. Keep a count of
1292 the number of dynamic symbols we find. A dynamic symbol
1293 is one which is referenced or defined by both a regular
1294 object and a shared object. */
1295 old_flags = h->elf_link_hash_flags;
1296 dynsym = FALSE;
1297 if (! dynamic)
1299 if (! definition)
1301 new_flag = ELF_LINK_HASH_REF_REGULAR;
1302 if (bind != STB_WEAK)
1303 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1305 else
1306 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1307 if (! info->executable
1308 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1309 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1310 dynsym = TRUE;
1312 else
1314 if (! definition)
1315 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1316 else
1317 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1318 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1319 | ELF_LINK_HASH_REF_REGULAR)) != 0
1320 || (h->weakdef != NULL
1321 && ! new_weakdef
1322 && h->weakdef->dynindx != -1))
1323 dynsym = TRUE;
1326 h->elf_link_hash_flags |= new_flag;
1328 /* Check to see if we need to add an indirect symbol for
1329 the default name. */
1330 if (definition || h->root.type == bfd_link_hash_common)
1331 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1332 &sec, &value, &dynsym,
1333 override, dt_needed))
1334 goto error_free_vers;
1336 if (definition && !dynamic)
1338 char *p = strchr (name, ELF_VER_CHR);
1339 if (p != NULL && p[1] != ELF_VER_CHR)
1341 /* Queue non-default versions so that .symver x, x@FOO
1342 aliases can be checked. */
1343 if (! nondeflt_vers)
1345 amt = (isymend - isym + 1)
1346 * sizeof (struct elf_link_hash_entry *);
1347 nondeflt_vers = bfd_malloc (amt);
1349 nondeflt_vers [nondeflt_vers_cnt++] = h;
1353 if (dynsym && h->dynindx == -1)
1355 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1356 goto error_free_vers;
1357 if (h->weakdef != NULL
1358 && ! new_weakdef
1359 && h->weakdef->dynindx == -1)
1361 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1362 goto error_free_vers;
1365 else if (dynsym && h->dynindx != -1)
1366 /* If the symbol already has a dynamic index, but
1367 visibility says it should not be visible, turn it into
1368 a local symbol. */
1369 switch (ELF_ST_VISIBILITY (h->other))
1371 case STV_INTERNAL:
1372 case STV_HIDDEN:
1373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1374 break;
1377 if (dt_needed && definition
1378 && (h->elf_link_hash_flags
1379 & ELF_LINK_HASH_REF_REGULAR) != 0)
1381 bfd_size_type oldsize;
1382 bfd_size_type strindex;
1384 /* The symbol from a DT_NEEDED object is referenced from
1385 the regular object to create a dynamic executable. We
1386 have to make sure there is a DT_NEEDED entry for it. */
1388 dt_needed = FALSE;
1389 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1390 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1391 elf_dt_soname (abfd), FALSE);
1392 if (strindex == (bfd_size_type) -1)
1393 goto error_free_vers;
1395 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1397 asection *sdyn;
1398 Elf_External_Dyn *dyncon, *dynconend;
1400 sdyn = bfd_get_section_by_name (hash_table->dynobj,
1401 ".dynamic");
1402 BFD_ASSERT (sdyn != NULL);
1404 dyncon = (Elf_External_Dyn *) sdyn->contents;
1405 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1406 sdyn->_raw_size);
1407 for (; dyncon < dynconend; dyncon++)
1409 Elf_Internal_Dyn dyn;
1411 elf_swap_dyn_in (hash_table->dynobj,
1412 dyncon, &dyn);
1413 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1414 dyn.d_un.d_val != strindex);
1418 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1419 goto error_free_vers;
1424 /* Now that all the symbols from this input file are created, handle
1425 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1426 if (nondeflt_vers != NULL)
1428 bfd_size_type cnt, symidx;
1430 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
1432 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1433 char *shortname, *p;
1435 p = strchr (h->root.root.string, ELF_VER_CHR);
1436 if (p == NULL
1437 || (h->root.type != bfd_link_hash_defined
1438 && h->root.type != bfd_link_hash_defweak))
1439 continue;
1441 amt = p - h->root.root.string;
1442 shortname = bfd_malloc (amt + 1);
1443 memcpy (shortname, h->root.root.string, amt);
1444 shortname[amt] = '\0';
1446 hi = (struct elf_link_hash_entry *)
1447 bfd_link_hash_lookup (&hash_table->root, shortname,
1448 FALSE, FALSE, FALSE);
1449 if (hi != NULL
1450 && hi->root.type == h->root.type
1451 && hi->root.u.def.value == h->root.u.def.value
1452 && hi->root.u.def.section == h->root.u.def.section)
1454 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1455 hi->root.type = bfd_link_hash_indirect;
1456 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1457 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1458 sym_hash = elf_sym_hashes (abfd);
1459 if (sym_hash)
1460 for (symidx = 0; symidx < extsymcount; ++symidx)
1461 if (sym_hash[symidx] == hi)
1463 sym_hash[symidx] = h;
1464 break;
1467 free (shortname);
1469 free (nondeflt_vers);
1470 nondeflt_vers = NULL;
1473 if (extversym != NULL)
1475 free (extversym);
1476 extversym = NULL;
1479 if (isymbuf != NULL)
1480 free (isymbuf);
1481 isymbuf = NULL;
1483 /* Now set the weakdefs field correctly for all the weak defined
1484 symbols we found. The only way to do this is to search all the
1485 symbols. Since we only need the information for non functions in
1486 dynamic objects, that's the only time we actually put anything on
1487 the list WEAKS. We need this information so that if a regular
1488 object refers to a symbol defined weakly in a dynamic object, the
1489 real symbol in the dynamic object is also put in the dynamic
1490 symbols; we also must arrange for both symbols to point to the
1491 same memory location. We could handle the general case of symbol
1492 aliasing, but a general symbol alias can only be generated in
1493 assembler code, handling it correctly would be very time
1494 consuming, and other ELF linkers don't handle general aliasing
1495 either. */
1496 while (weaks != NULL)
1498 struct elf_link_hash_entry *hlook;
1499 asection *slook;
1500 bfd_vma vlook;
1501 struct elf_link_hash_entry **hpp;
1502 struct elf_link_hash_entry **hppend;
1504 hlook = weaks;
1505 weaks = hlook->weakdef;
1506 hlook->weakdef = NULL;
1508 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1509 || hlook->root.type == bfd_link_hash_defweak
1510 || hlook->root.type == bfd_link_hash_common
1511 || hlook->root.type == bfd_link_hash_indirect);
1512 slook = hlook->root.u.def.section;
1513 vlook = hlook->root.u.def.value;
1515 hpp = elf_sym_hashes (abfd);
1516 hppend = hpp + extsymcount;
1517 for (; hpp < hppend; hpp++)
1519 struct elf_link_hash_entry *h;
1521 h = *hpp;
1522 if (h != NULL && h != hlook
1523 && h->root.type == bfd_link_hash_defined
1524 && h->root.u.def.section == slook
1525 && h->root.u.def.value == vlook)
1527 hlook->weakdef = h;
1529 /* If the weak definition is in the list of dynamic
1530 symbols, make sure the real definition is put there
1531 as well. */
1532 if (hlook->dynindx != -1
1533 && h->dynindx == -1)
1535 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1536 goto error_return;
1539 /* If the real definition is in the list of dynamic
1540 symbols, make sure the weak definition is put there
1541 as well. If we don't do this, then the dynamic
1542 loader might not merge the entries for the real
1543 definition and the weak definition. */
1544 if (h->dynindx != -1
1545 && hlook->dynindx == -1)
1547 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1548 goto error_return;
1550 break;
1555 /* If this object is the same format as the output object, and it is
1556 not a shared library, then let the backend look through the
1557 relocs.
1559 This is required to build global offset table entries and to
1560 arrange for dynamic relocs. It is not required for the
1561 particular common case of linking non PIC code, even when linking
1562 against shared libraries, but unfortunately there is no way of
1563 knowing whether an object file has been compiled PIC or not.
1564 Looking through the relocs is not particularly time consuming.
1565 The problem is that we must either (1) keep the relocs in memory,
1566 which causes the linker to require additional runtime memory or
1567 (2) read the relocs twice from the input file, which wastes time.
1568 This would be a good case for using mmap.
1570 I have no idea how to handle linking PIC code into a file of a
1571 different format. It probably can't be done. */
1572 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1573 if (! dynamic
1574 && is_elf_hash_table (hash_table)
1575 && hash_table->root.creator == abfd->xvec
1576 && check_relocs != NULL)
1578 asection *o;
1580 for (o = abfd->sections; o != NULL; o = o->next)
1582 Elf_Internal_Rela *internal_relocs;
1583 bfd_boolean ok;
1585 if ((o->flags & SEC_RELOC) == 0
1586 || o->reloc_count == 0
1587 || ((info->strip == strip_all || info->strip == strip_debugger)
1588 && (o->flags & SEC_DEBUGGING) != 0)
1589 || bfd_is_abs_section (o->output_section))
1590 continue;
1592 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1593 info->keep_memory);
1594 if (internal_relocs == NULL)
1595 goto error_return;
1597 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1599 if (elf_section_data (o)->relocs != internal_relocs)
1600 free (internal_relocs);
1602 if (! ok)
1603 goto error_return;
1607 /* If this is a non-traditional link, try to optimize the handling
1608 of the .stab/.stabstr sections. */
1609 if (! dynamic
1610 && ! info->traditional_format
1611 && is_elf_hash_table (hash_table)
1612 && (info->strip != strip_all && info->strip != strip_debugger))
1614 asection *stabstr;
1616 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1617 if (stabstr != NULL)
1619 bfd_size_type string_offset = 0;
1620 asection *stab;
1622 for (stab = abfd->sections; stab; stab = stab->next)
1623 if (strncmp (".stab", stab->name, 5) == 0
1624 && (!stab->name[5] ||
1625 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1626 && (stab->flags & SEC_MERGE) == 0
1627 && !bfd_is_abs_section (stab->output_section))
1629 struct bfd_elf_section_data *secdata;
1631 secdata = elf_section_data (stab);
1632 if (! _bfd_link_section_stabs (abfd,
1633 & hash_table->stab_info,
1634 stab, stabstr,
1635 &secdata->sec_info,
1636 &string_offset))
1637 goto error_return;
1638 if (secdata->sec_info)
1639 stab->sec_info_type = ELF_INFO_TYPE_STABS;
1644 if (! info->relocatable
1645 && ! dynamic
1646 && is_elf_hash_table (hash_table))
1648 asection *s;
1650 for (s = abfd->sections; s != NULL; s = s->next)
1651 if ((s->flags & SEC_MERGE) != 0
1652 && !bfd_is_abs_section (s->output_section))
1654 struct bfd_elf_section_data *secdata;
1656 secdata = elf_section_data (s);
1657 if (! _bfd_merge_section (abfd,
1658 & hash_table->merge_info,
1659 s, &secdata->sec_info))
1660 goto error_return;
1661 else if (secdata->sec_info)
1662 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1666 if (is_elf_hash_table (hash_table))
1668 /* Add this bfd to the loaded list. */
1669 struct elf_link_loaded_list *n;
1671 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
1672 if (n == NULL)
1673 goto error_return;
1674 n->abfd = abfd;
1675 n->next = hash_table->loaded;
1676 hash_table->loaded = n;
1679 return TRUE;
1681 error_free_vers:
1682 if (nondeflt_vers != NULL)
1683 free (nondeflt_vers);
1684 if (extversym != NULL)
1685 free (extversym);
1686 error_free_sym:
1687 if (isymbuf != NULL)
1688 free (isymbuf);
1689 error_return:
1690 return FALSE;
1693 /* Add an entry to the .dynamic table. */
1695 bfd_boolean
1696 elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
1698 Elf_Internal_Dyn dyn;
1699 bfd *dynobj;
1700 asection *s;
1701 bfd_size_type newsize;
1702 bfd_byte *newcontents;
1704 if (! is_elf_hash_table (info->hash))
1705 return FALSE;
1707 dynobj = elf_hash_table (info)->dynobj;
1709 s = bfd_get_section_by_name (dynobj, ".dynamic");
1710 BFD_ASSERT (s != NULL);
1712 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1713 newcontents = bfd_realloc (s->contents, newsize);
1714 if (newcontents == NULL)
1715 return FALSE;
1717 dyn.d_tag = tag;
1718 dyn.d_un.d_val = val;
1719 elf_swap_dyn_out (dynobj, &dyn,
1720 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1722 s->_raw_size = newsize;
1723 s->contents = newcontents;
1725 return TRUE;
1728 /* Array used to determine the number of hash table buckets to use
1729 based on the number of symbols there are. If there are fewer than
1730 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1731 fewer than 37 we use 17 buckets, and so forth. We never use more
1732 than 32771 buckets. */
1734 static const size_t elf_buckets[] =
1736 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1737 16411, 32771, 0
1740 /* Compute bucket count for hashing table. We do not use a static set
1741 of possible tables sizes anymore. Instead we determine for all
1742 possible reasonable sizes of the table the outcome (i.e., the
1743 number of collisions etc) and choose the best solution. The
1744 weighting functions are not too simple to allow the table to grow
1745 without bounds. Instead one of the weighting factors is the size.
1746 Therefore the result is always a good payoff between few collisions
1747 (= short chain lengths) and table size. */
1748 static size_t
1749 compute_bucket_count (struct bfd_link_info *info)
1751 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1752 size_t best_size = 0;
1753 unsigned long int *hashcodes;
1754 unsigned long int *hashcodesp;
1755 unsigned long int i;
1756 bfd_size_type amt;
1758 /* Compute the hash values for all exported symbols. At the same
1759 time store the values in an array so that we could use them for
1760 optimizations. */
1761 amt = dynsymcount;
1762 amt *= sizeof (unsigned long int);
1763 hashcodes = bfd_malloc (amt);
1764 if (hashcodes == NULL)
1765 return 0;
1766 hashcodesp = hashcodes;
1768 /* Put all hash values in HASHCODES. */
1769 elf_link_hash_traverse (elf_hash_table (info),
1770 elf_collect_hash_codes, &hashcodesp);
1772 /* We have a problem here. The following code to optimize the table
1773 size requires an integer type with more the 32 bits. If
1774 BFD_HOST_U_64_BIT is set we know about such a type. */
1775 #ifdef BFD_HOST_U_64_BIT
1776 if (info->optimize)
1778 unsigned long int nsyms = hashcodesp - hashcodes;
1779 size_t minsize;
1780 size_t maxsize;
1781 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1782 unsigned long int *counts ;
1784 /* Possible optimization parameters: if we have NSYMS symbols we say
1785 that the hashing table must at least have NSYMS/4 and at most
1786 2*NSYMS buckets. */
1787 minsize = nsyms / 4;
1788 if (minsize == 0)
1789 minsize = 1;
1790 best_size = maxsize = nsyms * 2;
1792 /* Create array where we count the collisions in. We must use bfd_malloc
1793 since the size could be large. */
1794 amt = maxsize;
1795 amt *= sizeof (unsigned long int);
1796 counts = bfd_malloc (amt);
1797 if (counts == NULL)
1799 free (hashcodes);
1800 return 0;
1803 /* Compute the "optimal" size for the hash table. The criteria is a
1804 minimal chain length. The minor criteria is (of course) the size
1805 of the table. */
1806 for (i = minsize; i < maxsize; ++i)
1808 /* Walk through the array of hashcodes and count the collisions. */
1809 BFD_HOST_U_64_BIT max;
1810 unsigned long int j;
1811 unsigned long int fact;
1813 memset (counts, '\0', i * sizeof (unsigned long int));
1815 /* Determine how often each hash bucket is used. */
1816 for (j = 0; j < nsyms; ++j)
1817 ++counts[hashcodes[j] % i];
1819 /* For the weight function we need some information about the
1820 pagesize on the target. This is information need not be 100%
1821 accurate. Since this information is not available (so far) we
1822 define it here to a reasonable default value. If it is crucial
1823 to have a better value some day simply define this value. */
1824 # ifndef BFD_TARGET_PAGESIZE
1825 # define BFD_TARGET_PAGESIZE (4096)
1826 # endif
1828 /* We in any case need 2 + NSYMS entries for the size values and
1829 the chains. */
1830 max = (2 + nsyms) * (ARCH_SIZE / 8);
1832 # if 1
1833 /* Variant 1: optimize for short chains. We add the squares
1834 of all the chain lengths (which favors many small chain
1835 over a few long chains). */
1836 for (j = 0; j < i; ++j)
1837 max += counts[j] * counts[j];
1839 /* This adds penalties for the overall size of the table. */
1840 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1841 max *= fact * fact;
1842 # else
1843 /* Variant 2: Optimize a lot more for small table. Here we
1844 also add squares of the size but we also add penalties for
1845 empty slots (the +1 term). */
1846 for (j = 0; j < i; ++j)
1847 max += (1 + counts[j]) * (1 + counts[j]);
1849 /* The overall size of the table is considered, but not as
1850 strong as in variant 1, where it is squared. */
1851 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1852 max *= fact;
1853 # endif
1855 /* Compare with current best results. */
1856 if (max < best_chlen)
1858 best_chlen = max;
1859 best_size = i;
1863 free (counts);
1865 else
1866 #endif /* defined (BFD_HOST_U_64_BIT) */
1868 /* This is the fallback solution if no 64bit type is available or if we
1869 are not supposed to spend much time on optimizations. We select the
1870 bucket count using a fixed set of numbers. */
1871 for (i = 0; elf_buckets[i] != 0; i++)
1873 best_size = elf_buckets[i];
1874 if (dynsymcount < elf_buckets[i + 1])
1875 break;
1879 /* Free the arrays we needed. */
1880 free (hashcodes);
1882 return best_size;
1885 /* Set up the sizes and contents of the ELF dynamic sections. This is
1886 called by the ELF linker emulation before_allocation routine. We
1887 must set the sizes of the sections before the linker sets the
1888 addresses of the various sections. */
1890 bfd_boolean
1891 NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1892 const char *soname,
1893 const char *rpath,
1894 const char *filter_shlib,
1895 const char * const *auxiliary_filters,
1896 struct bfd_link_info *info,
1897 asection **sinterpptr,
1898 struct bfd_elf_version_tree *verdefs)
1900 bfd_size_type soname_indx;
1901 bfd *dynobj;
1902 const struct elf_backend_data *bed;
1903 struct elf_assign_sym_version_info asvinfo;
1905 *sinterpptr = NULL;
1907 soname_indx = (bfd_size_type) -1;
1909 if (!is_elf_hash_table (info->hash))
1910 return TRUE;
1912 if (info->execstack)
1913 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1914 else if (info->noexecstack)
1915 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1916 else
1918 bfd *inputobj;
1919 asection *notesec = NULL;
1920 int exec = 0;
1922 for (inputobj = info->input_bfds;
1923 inputobj;
1924 inputobj = inputobj->link_next)
1926 asection *s;
1928 if (inputobj->flags & DYNAMIC)
1929 continue;
1930 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1931 if (s)
1933 if (s->flags & SEC_CODE)
1934 exec = PF_X;
1935 notesec = s;
1937 else
1938 exec = PF_X;
1940 if (notesec)
1942 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1943 if (exec && info->relocatable
1944 && notesec->output_section != bfd_abs_section_ptr)
1945 notesec->output_section->flags |= SEC_CODE;
1949 /* Any syms created from now on start with -1 in
1950 got.refcount/offset and plt.refcount/offset. */
1951 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
1953 /* The backend may have to create some sections regardless of whether
1954 we're dynamic or not. */
1955 bed = get_elf_backend_data (output_bfd);
1956 if (bed->elf_backend_always_size_sections
1957 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1958 return FALSE;
1960 dynobj = elf_hash_table (info)->dynobj;
1962 /* If there were no dynamic objects in the link, there is nothing to
1963 do here. */
1964 if (dynobj == NULL)
1965 return TRUE;
1967 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1968 return FALSE;
1970 if (elf_hash_table (info)->dynamic_sections_created)
1972 struct elf_info_failed eif;
1973 struct elf_link_hash_entry *h;
1974 asection *dynstr;
1975 struct bfd_elf_version_tree *t;
1976 struct bfd_elf_version_expr *d;
1977 bfd_boolean all_defined;
1979 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1980 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
1982 if (soname != NULL)
1984 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1985 soname, TRUE);
1986 if (soname_indx == (bfd_size_type) -1
1987 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
1988 return FALSE;
1991 if (info->symbolic)
1993 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1994 return FALSE;
1995 info->flags |= DF_SYMBOLIC;
1998 if (rpath != NULL)
2000 bfd_size_type indx;
2002 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
2003 TRUE);
2004 if (info->new_dtags)
2005 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
2006 if (indx == (bfd_size_type) -1
2007 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
2008 || (info->new_dtags
2009 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
2010 return FALSE;
2013 if (filter_shlib != NULL)
2015 bfd_size_type indx;
2017 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2018 filter_shlib, TRUE);
2019 if (indx == (bfd_size_type) -1
2020 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
2021 return FALSE;
2024 if (auxiliary_filters != NULL)
2026 const char * const *p;
2028 for (p = auxiliary_filters; *p != NULL; p++)
2030 bfd_size_type indx;
2032 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2033 *p, TRUE);
2034 if (indx == (bfd_size_type) -1
2035 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
2036 return FALSE;
2040 eif.info = info;
2041 eif.verdefs = verdefs;
2042 eif.failed = FALSE;
2044 /* If we are supposed to export all symbols into the dynamic symbol
2045 table (this is not the normal case), then do so. */
2046 if (info->export_dynamic)
2048 elf_link_hash_traverse (elf_hash_table (info),
2049 _bfd_elf_export_symbol,
2050 &eif);
2051 if (eif.failed)
2052 return FALSE;
2055 /* Make all global versions with definition. */
2056 for (t = verdefs; t != NULL; t = t->next)
2057 for (d = t->globals.list; d != NULL; d = d->next)
2058 if (!d->symver && d->symbol)
2060 const char *verstr, *name;
2061 size_t namelen, verlen, newlen;
2062 char *newname, *p;
2063 struct elf_link_hash_entry *newh;
2065 name = d->symbol;
2066 namelen = strlen (name);
2067 verstr = t->name;
2068 verlen = strlen (verstr);
2069 newlen = namelen + verlen + 3;
2071 newname = bfd_malloc (newlen);
2072 if (newname == NULL)
2073 return FALSE;
2074 memcpy (newname, name, namelen);
2076 /* Check the hidden versioned definition. */
2077 p = newname + namelen;
2078 *p++ = ELF_VER_CHR;
2079 memcpy (p, verstr, verlen + 1);
2080 newh = elf_link_hash_lookup (elf_hash_table (info),
2081 newname, FALSE, FALSE,
2082 FALSE);
2083 if (newh == NULL
2084 || (newh->root.type != bfd_link_hash_defined
2085 && newh->root.type != bfd_link_hash_defweak))
2087 /* Check the default versioned definition. */
2088 *p++ = ELF_VER_CHR;
2089 memcpy (p, verstr, verlen + 1);
2090 newh = elf_link_hash_lookup (elf_hash_table (info),
2091 newname, FALSE, FALSE,
2092 FALSE);
2094 free (newname);
2096 /* Mark this version if there is a definition and it is
2097 not defined in a shared object. */
2098 if (newh != NULL
2099 && ((newh->elf_link_hash_flags
2100 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
2101 && (newh->root.type == bfd_link_hash_defined
2102 || newh->root.type == bfd_link_hash_defweak))
2103 d->symver = 1;
2106 /* Attach all the symbols to their version information. */
2107 asvinfo.output_bfd = output_bfd;
2108 asvinfo.info = info;
2109 asvinfo.verdefs = verdefs;
2110 asvinfo.failed = FALSE;
2112 elf_link_hash_traverse (elf_hash_table (info),
2113 _bfd_elf_link_assign_sym_version,
2114 &asvinfo);
2115 if (asvinfo.failed)
2116 return FALSE;
2118 if (!info->allow_undefined_version)
2120 /* Check if all global versions have a definition. */
2121 all_defined = TRUE;
2122 for (t = verdefs; t != NULL; t = t->next)
2123 for (d = t->globals.list; d != NULL; d = d->next)
2124 if (!d->symver && !d->script)
2126 (*_bfd_error_handler)
2127 (_("%s: undefined version: %s"),
2128 d->pattern, t->name);
2129 all_defined = FALSE;
2132 if (!all_defined)
2134 bfd_set_error (bfd_error_bad_value);
2135 return FALSE;
2139 /* Find all symbols which were defined in a dynamic object and make
2140 the backend pick a reasonable value for them. */
2141 elf_link_hash_traverse (elf_hash_table (info),
2142 _bfd_elf_adjust_dynamic_symbol,
2143 &eif);
2144 if (eif.failed)
2145 return FALSE;
2147 /* Add some entries to the .dynamic section. We fill in some of the
2148 values later, in elf_bfd_final_link, but we must add the entries
2149 now so that we know the final size of the .dynamic section. */
2151 /* If there are initialization and/or finalization functions to
2152 call then add the corresponding DT_INIT/DT_FINI entries. */
2153 h = (info->init_function
2154 ? elf_link_hash_lookup (elf_hash_table (info),
2155 info->init_function, FALSE,
2156 FALSE, FALSE)
2157 : NULL);
2158 if (h != NULL
2159 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2160 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2162 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
2163 return FALSE;
2165 h = (info->fini_function
2166 ? elf_link_hash_lookup (elf_hash_table (info),
2167 info->fini_function, FALSE,
2168 FALSE, FALSE)
2169 : NULL);
2170 if (h != NULL
2171 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2172 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2174 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
2175 return FALSE;
2178 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
2180 /* DT_PREINIT_ARRAY is not allowed in shared library. */
2181 if (! info->executable)
2183 bfd *sub;
2184 asection *o;
2186 for (sub = info->input_bfds; sub != NULL;
2187 sub = sub->link_next)
2188 for (o = sub->sections; o != NULL; o = o->next)
2189 if (elf_section_data (o)->this_hdr.sh_type
2190 == SHT_PREINIT_ARRAY)
2192 (*_bfd_error_handler)
2193 (_("%s: .preinit_array section is not allowed in DSO"),
2194 bfd_archive_filename (sub));
2195 break;
2198 bfd_set_error (bfd_error_nonrepresentable_section);
2199 return FALSE;
2202 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
2203 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
2204 return FALSE;
2206 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
2208 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
2209 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
2210 return FALSE;
2212 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
2214 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
2215 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
2216 return FALSE;
2219 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
2220 /* If .dynstr is excluded from the link, we don't want any of
2221 these tags. Strictly, we should be checking each section
2222 individually; This quick check covers for the case where
2223 someone does a /DISCARD/ : { *(*) }. */
2224 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
2226 bfd_size_type strsize;
2228 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2229 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
2230 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
2231 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
2232 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
2233 || ! elf_add_dynamic_entry (info, DT_SYMENT,
2234 sizeof (Elf_External_Sym)))
2235 return FALSE;
2239 /* The backend must work out the sizes of all the other dynamic
2240 sections. */
2241 if (bed->elf_backend_size_dynamic_sections
2242 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
2243 return FALSE;
2245 if (elf_hash_table (info)->dynamic_sections_created)
2247 bfd_size_type dynsymcount;
2248 asection *s;
2249 size_t bucketcount = 0;
2250 size_t hash_entry_size;
2251 unsigned int dtagcount;
2253 /* Set up the version definition section. */
2254 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2255 BFD_ASSERT (s != NULL);
2257 /* We may have created additional version definitions if we are
2258 just linking a regular application. */
2259 verdefs = asvinfo.verdefs;
2261 /* Skip anonymous version tag. */
2262 if (verdefs != NULL && verdefs->vernum == 0)
2263 verdefs = verdefs->next;
2265 if (verdefs == NULL)
2266 _bfd_strip_section_from_output (info, s);
2267 else
2269 unsigned int cdefs;
2270 bfd_size_type size;
2271 struct bfd_elf_version_tree *t;
2272 bfd_byte *p;
2273 Elf_Internal_Verdef def;
2274 Elf_Internal_Verdaux defaux;
2276 cdefs = 0;
2277 size = 0;
2279 /* Make space for the base version. */
2280 size += sizeof (Elf_External_Verdef);
2281 size += sizeof (Elf_External_Verdaux);
2282 ++cdefs;
2284 for (t = verdefs; t != NULL; t = t->next)
2286 struct bfd_elf_version_deps *n;
2288 size += sizeof (Elf_External_Verdef);
2289 size += sizeof (Elf_External_Verdaux);
2290 ++cdefs;
2292 for (n = t->deps; n != NULL; n = n->next)
2293 size += sizeof (Elf_External_Verdaux);
2296 s->_raw_size = size;
2297 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2298 if (s->contents == NULL && s->_raw_size != 0)
2299 return FALSE;
2301 /* Fill in the version definition section. */
2303 p = s->contents;
2305 def.vd_version = VER_DEF_CURRENT;
2306 def.vd_flags = VER_FLG_BASE;
2307 def.vd_ndx = 1;
2308 def.vd_cnt = 1;
2309 def.vd_aux = sizeof (Elf_External_Verdef);
2310 def.vd_next = (sizeof (Elf_External_Verdef)
2311 + sizeof (Elf_External_Verdaux));
2313 if (soname_indx != (bfd_size_type) -1)
2315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2316 soname_indx);
2317 def.vd_hash = bfd_elf_hash (soname);
2318 defaux.vda_name = soname_indx;
2320 else
2322 const char *name;
2323 bfd_size_type indx;
2325 name = basename (output_bfd->filename);
2326 def.vd_hash = bfd_elf_hash (name);
2327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2328 name, FALSE);
2329 if (indx == (bfd_size_type) -1)
2330 return FALSE;
2331 defaux.vda_name = indx;
2333 defaux.vda_next = 0;
2335 _bfd_elf_swap_verdef_out (output_bfd, &def,
2336 (Elf_External_Verdef *) p);
2337 p += sizeof (Elf_External_Verdef);
2338 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2339 (Elf_External_Verdaux *) p);
2340 p += sizeof (Elf_External_Verdaux);
2342 for (t = verdefs; t != NULL; t = t->next)
2344 unsigned int cdeps;
2345 struct bfd_elf_version_deps *n;
2346 struct elf_link_hash_entry *h;
2347 struct bfd_link_hash_entry *bh;
2349 cdeps = 0;
2350 for (n = t->deps; n != NULL; n = n->next)
2351 ++cdeps;
2353 /* Add a symbol representing this version. */
2354 bh = NULL;
2355 if (! (_bfd_generic_link_add_one_symbol
2356 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2357 0, NULL, FALSE,
2358 get_elf_backend_data (dynobj)->collect, &bh)))
2359 return FALSE;
2360 h = (struct elf_link_hash_entry *) bh;
2361 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2362 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2363 h->type = STT_OBJECT;
2364 h->verinfo.vertree = t;
2366 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2367 return FALSE;
2369 def.vd_version = VER_DEF_CURRENT;
2370 def.vd_flags = 0;
2371 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
2372 def.vd_flags |= VER_FLG_WEAK;
2373 def.vd_ndx = t->vernum + 1;
2374 def.vd_cnt = cdeps + 1;
2375 def.vd_hash = bfd_elf_hash (t->name);
2376 def.vd_aux = sizeof (Elf_External_Verdef);
2377 if (t->next != NULL)
2378 def.vd_next = (sizeof (Elf_External_Verdef)
2379 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2380 else
2381 def.vd_next = 0;
2383 _bfd_elf_swap_verdef_out (output_bfd, &def,
2384 (Elf_External_Verdef *) p);
2385 p += sizeof (Elf_External_Verdef);
2387 defaux.vda_name = h->dynstr_index;
2388 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2389 h->dynstr_index);
2390 if (t->deps == NULL)
2391 defaux.vda_next = 0;
2392 else
2393 defaux.vda_next = sizeof (Elf_External_Verdaux);
2394 t->name_indx = defaux.vda_name;
2396 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2397 (Elf_External_Verdaux *) p);
2398 p += sizeof (Elf_External_Verdaux);
2400 for (n = t->deps; n != NULL; n = n->next)
2402 if (n->version_needed == NULL)
2404 /* This can happen if there was an error in the
2405 version script. */
2406 defaux.vda_name = 0;
2408 else
2410 defaux.vda_name = n->version_needed->name_indx;
2411 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2412 defaux.vda_name);
2414 if (n->next == NULL)
2415 defaux.vda_next = 0;
2416 else
2417 defaux.vda_next = sizeof (Elf_External_Verdaux);
2419 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2420 (Elf_External_Verdaux *) p);
2421 p += sizeof (Elf_External_Verdaux);
2425 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2426 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2427 return FALSE;
2429 elf_tdata (output_bfd)->cverdefs = cdefs;
2432 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
2434 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
2435 return FALSE;
2438 if (info->flags_1)
2440 if (info->executable)
2441 info->flags_1 &= ~ (DF_1_INITFIRST
2442 | DF_1_NODELETE
2443 | DF_1_NOOPEN);
2444 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
2445 return FALSE;
2448 /* Work out the size of the version reference section. */
2450 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2451 BFD_ASSERT (s != NULL);
2453 struct elf_find_verdep_info sinfo;
2455 sinfo.output_bfd = output_bfd;
2456 sinfo.info = info;
2457 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2458 if (sinfo.vers == 0)
2459 sinfo.vers = 1;
2460 sinfo.failed = FALSE;
2462 elf_link_hash_traverse (elf_hash_table (info),
2463 _bfd_elf_link_find_version_dependencies,
2464 &sinfo);
2466 if (elf_tdata (output_bfd)->verref == NULL)
2467 _bfd_strip_section_from_output (info, s);
2468 else
2470 Elf_Internal_Verneed *t;
2471 unsigned int size;
2472 unsigned int crefs;
2473 bfd_byte *p;
2475 /* Build the version definition section. */
2476 size = 0;
2477 crefs = 0;
2478 for (t = elf_tdata (output_bfd)->verref;
2479 t != NULL;
2480 t = t->vn_nextref)
2482 Elf_Internal_Vernaux *a;
2484 size += sizeof (Elf_External_Verneed);
2485 ++crefs;
2486 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2487 size += sizeof (Elf_External_Vernaux);
2490 s->_raw_size = size;
2491 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2492 if (s->contents == NULL)
2493 return FALSE;
2495 p = s->contents;
2496 for (t = elf_tdata (output_bfd)->verref;
2497 t != NULL;
2498 t = t->vn_nextref)
2500 unsigned int caux;
2501 Elf_Internal_Vernaux *a;
2502 bfd_size_type indx;
2504 caux = 0;
2505 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2506 ++caux;
2508 t->vn_version = VER_NEED_CURRENT;
2509 t->vn_cnt = caux;
2510 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2511 elf_dt_name (t->vn_bfd) != NULL
2512 ? elf_dt_name (t->vn_bfd)
2513 : basename (t->vn_bfd->filename),
2514 FALSE);
2515 if (indx == (bfd_size_type) -1)
2516 return FALSE;
2517 t->vn_file = indx;
2518 t->vn_aux = sizeof (Elf_External_Verneed);
2519 if (t->vn_nextref == NULL)
2520 t->vn_next = 0;
2521 else
2522 t->vn_next = (sizeof (Elf_External_Verneed)
2523 + caux * sizeof (Elf_External_Vernaux));
2525 _bfd_elf_swap_verneed_out (output_bfd, t,
2526 (Elf_External_Verneed *) p);
2527 p += sizeof (Elf_External_Verneed);
2529 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2531 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2533 a->vna_nodename, FALSE);
2534 if (indx == (bfd_size_type) -1)
2535 return FALSE;
2536 a->vna_name = indx;
2537 if (a->vna_nextptr == NULL)
2538 a->vna_next = 0;
2539 else
2540 a->vna_next = sizeof (Elf_External_Vernaux);
2542 _bfd_elf_swap_vernaux_out (output_bfd, a,
2543 (Elf_External_Vernaux *) p);
2544 p += sizeof (Elf_External_Vernaux);
2548 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2549 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2550 return FALSE;
2552 elf_tdata (output_bfd)->cverrefs = crefs;
2556 /* Assign dynsym indicies. In a shared library we generate a
2557 section symbol for each output section, which come first.
2558 Next come all of the back-end allocated local dynamic syms,
2559 followed by the rest of the global symbols. */
2561 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2563 /* Work out the size of the symbol version section. */
2564 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2565 BFD_ASSERT (s != NULL);
2566 if (dynsymcount == 0
2567 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2569 _bfd_strip_section_from_output (info, s);
2570 /* The DYNSYMCOUNT might have changed if we were going to
2571 output a dynamic symbol table entry for S. */
2572 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2574 else
2576 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2577 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2578 if (s->contents == NULL)
2579 return FALSE;
2581 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2582 return FALSE;
2585 /* Set the size of the .dynsym and .hash sections. We counted
2586 the number of dynamic symbols in elf_link_add_object_symbols.
2587 We will build the contents of .dynsym and .hash when we build
2588 the final symbol table, because until then we do not know the
2589 correct value to give the symbols. We built the .dynstr
2590 section as we went along in elf_link_add_object_symbols. */
2591 s = bfd_get_section_by_name (dynobj, ".dynsym");
2592 BFD_ASSERT (s != NULL);
2593 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2594 s->contents = bfd_alloc (output_bfd, s->_raw_size);
2595 if (s->contents == NULL && s->_raw_size != 0)
2596 return FALSE;
2598 if (dynsymcount != 0)
2600 Elf_Internal_Sym isym;
2602 /* The first entry in .dynsym is a dummy symbol. */
2603 isym.st_value = 0;
2604 isym.st_size = 0;
2605 isym.st_name = 0;
2606 isym.st_info = 0;
2607 isym.st_other = 0;
2608 isym.st_shndx = 0;
2609 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
2612 /* Compute the size of the hashing table. As a side effect this
2613 computes the hash values for all the names we export. */
2614 bucketcount = compute_bucket_count (info);
2616 s = bfd_get_section_by_name (dynobj, ".hash");
2617 BFD_ASSERT (s != NULL);
2618 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2619 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
2620 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
2621 if (s->contents == NULL)
2622 return FALSE;
2624 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2625 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
2626 s->contents + hash_entry_size);
2628 elf_hash_table (info)->bucketcount = bucketcount;
2630 s = bfd_get_section_by_name (dynobj, ".dynstr");
2631 BFD_ASSERT (s != NULL);
2633 elf_finalize_dynstr (output_bfd, info);
2635 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2637 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
2638 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2639 return FALSE;
2642 return TRUE;
2645 /* This function is used to adjust offsets into .dynstr for
2646 dynamic symbols. This is called via elf_link_hash_traverse. */
2648 static bfd_boolean
2649 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2651 struct elf_strtab_hash *dynstr = data;
2653 if (h->root.type == bfd_link_hash_warning)
2654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2656 if (h->dynindx != -1)
2657 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2658 return TRUE;
2661 /* Assign string offsets in .dynstr, update all structures referencing
2662 them. */
2664 static bfd_boolean
2665 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2667 struct elf_link_local_dynamic_entry *entry;
2668 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2669 bfd *dynobj = elf_hash_table (info)->dynobj;
2670 asection *sdyn;
2671 bfd_size_type size;
2672 Elf_External_Dyn *dyncon, *dynconend;
2674 _bfd_elf_strtab_finalize (dynstr);
2675 size = _bfd_elf_strtab_size (dynstr);
2677 /* Update all .dynamic entries referencing .dynstr strings. */
2678 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2679 BFD_ASSERT (sdyn != NULL);
2681 dyncon = (Elf_External_Dyn *) sdyn->contents;
2682 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2683 sdyn->_raw_size);
2684 for (; dyncon < dynconend; dyncon++)
2686 Elf_Internal_Dyn dyn;
2688 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2689 switch (dyn.d_tag)
2691 case DT_STRSZ:
2692 dyn.d_un.d_val = size;
2693 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2694 break;
2695 case DT_NEEDED:
2696 case DT_SONAME:
2697 case DT_RPATH:
2698 case DT_RUNPATH:
2699 case DT_FILTER:
2700 case DT_AUXILIARY:
2701 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2702 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2703 break;
2704 default:
2705 break;
2709 /* Now update local dynamic symbols. */
2710 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2711 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2712 entry->isym.st_name);
2714 /* And the rest of dynamic symbols. */
2715 elf_link_hash_traverse (elf_hash_table (info),
2716 elf_adjust_dynstr_offsets, dynstr);
2718 /* Adjust version definitions. */
2719 if (elf_tdata (output_bfd)->cverdefs)
2721 asection *s;
2722 bfd_byte *p;
2723 bfd_size_type i;
2724 Elf_Internal_Verdef def;
2725 Elf_Internal_Verdaux defaux;
2727 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2728 p = (bfd_byte *) s->contents;
2731 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2732 &def);
2733 p += sizeof (Elf_External_Verdef);
2734 for (i = 0; i < def.vd_cnt; ++i)
2736 _bfd_elf_swap_verdaux_in (output_bfd,
2737 (Elf_External_Verdaux *) p, &defaux);
2738 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2739 defaux.vda_name);
2740 _bfd_elf_swap_verdaux_out (output_bfd,
2741 &defaux, (Elf_External_Verdaux *) p);
2742 p += sizeof (Elf_External_Verdaux);
2745 while (def.vd_next);
2748 /* Adjust version references. */
2749 if (elf_tdata (output_bfd)->verref)
2751 asection *s;
2752 bfd_byte *p;
2753 bfd_size_type i;
2754 Elf_Internal_Verneed need;
2755 Elf_Internal_Vernaux needaux;
2757 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2758 p = (bfd_byte *) s->contents;
2761 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2762 &need);
2763 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2764 _bfd_elf_swap_verneed_out (output_bfd, &need,
2765 (Elf_External_Verneed *) p);
2766 p += sizeof (Elf_External_Verneed);
2767 for (i = 0; i < need.vn_cnt; ++i)
2769 _bfd_elf_swap_vernaux_in (output_bfd,
2770 (Elf_External_Vernaux *) p, &needaux);
2771 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2772 needaux.vna_name);
2773 _bfd_elf_swap_vernaux_out (output_bfd,
2774 &needaux,
2775 (Elf_External_Vernaux *) p);
2776 p += sizeof (Elf_External_Vernaux);
2779 while (need.vn_next);
2782 return TRUE;
2785 /* Final phase of ELF linker. */
2787 /* A structure we use to avoid passing large numbers of arguments. */
2789 struct elf_final_link_info
2791 /* General link information. */
2792 struct bfd_link_info *info;
2793 /* Output BFD. */
2794 bfd *output_bfd;
2795 /* Symbol string table. */
2796 struct bfd_strtab_hash *symstrtab;
2797 /* .dynsym section. */
2798 asection *dynsym_sec;
2799 /* .hash section. */
2800 asection *hash_sec;
2801 /* symbol version section (.gnu.version). */
2802 asection *symver_sec;
2803 /* Buffer large enough to hold contents of any section. */
2804 bfd_byte *contents;
2805 /* Buffer large enough to hold external relocs of any section. */
2806 void *external_relocs;
2807 /* Buffer large enough to hold internal relocs of any section. */
2808 Elf_Internal_Rela *internal_relocs;
2809 /* Buffer large enough to hold external local symbols of any input
2810 BFD. */
2811 Elf_External_Sym *external_syms;
2812 /* And a buffer for symbol section indices. */
2813 Elf_External_Sym_Shndx *locsym_shndx;
2814 /* Buffer large enough to hold internal local symbols of any input
2815 BFD. */
2816 Elf_Internal_Sym *internal_syms;
2817 /* Array large enough to hold a symbol index for each local symbol
2818 of any input BFD. */
2819 long *indices;
2820 /* Array large enough to hold a section pointer for each local
2821 symbol of any input BFD. */
2822 asection **sections;
2823 /* Buffer to hold swapped out symbols. */
2824 Elf_External_Sym *symbuf;
2825 /* And one for symbol section indices. */
2826 Elf_External_Sym_Shndx *symshndxbuf;
2827 /* Number of swapped out symbols in buffer. */
2828 size_t symbuf_count;
2829 /* Number of symbols which fit in symbuf. */
2830 size_t symbuf_size;
2831 /* And same for symshndxbuf. */
2832 size_t shndxbuf_size;
2835 static bfd_boolean elf_link_output_sym
2836 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2837 struct elf_link_hash_entry *);
2838 static bfd_boolean elf_link_flush_output_syms
2839 (struct elf_final_link_info *);
2840 static bfd_boolean elf_link_output_extsym
2841 (struct elf_link_hash_entry *, void *);
2842 static bfd_boolean elf_link_input_bfd
2843 (struct elf_final_link_info *, bfd *);
2844 static bfd_boolean elf_reloc_link_order
2845 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
2847 /* This struct is used to pass information to elf_link_output_extsym. */
2849 struct elf_outext_info
2851 bfd_boolean failed;
2852 bfd_boolean localsyms;
2853 struct elf_final_link_info *finfo;
2856 /* When performing a relocatable link, the input relocations are
2857 preserved. But, if they reference global symbols, the indices
2858 referenced must be updated. Update all the relocations in
2859 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2861 static void
2862 elf_link_adjust_relocs (bfd *abfd,
2863 Elf_Internal_Shdr *rel_hdr,
2864 unsigned int count,
2865 struct elf_link_hash_entry **rel_hash)
2867 unsigned int i;
2868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2869 bfd_byte *erela;
2870 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2871 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2873 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2875 swap_in = bed->s->swap_reloc_in;
2876 swap_out = bed->s->swap_reloc_out;
2878 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2880 swap_in = bed->s->swap_reloca_in;
2881 swap_out = bed->s->swap_reloca_out;
2883 else
2884 abort ();
2886 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2887 abort ();
2889 erela = rel_hdr->contents;
2890 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2892 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2893 unsigned int j;
2895 if (*rel_hash == NULL)
2896 continue;
2898 BFD_ASSERT ((*rel_hash)->indx >= 0);
2900 (*swap_in) (abfd, erela, irela);
2901 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2902 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2903 ELF_R_TYPE (irela[j].r_info));
2904 (*swap_out) (abfd, irela, erela);
2908 struct elf_link_sort_rela
2910 bfd_vma offset;
2911 enum elf_reloc_type_class type;
2912 /* We use this as an array of size int_rels_per_ext_rel. */
2913 Elf_Internal_Rela rela[1];
2916 static int
2917 elf_link_sort_cmp1 (const void *A, const void *B)
2919 const struct elf_link_sort_rela *a = A;
2920 const struct elf_link_sort_rela *b = B;
2921 int relativea, relativeb;
2923 relativea = a->type == reloc_class_relative;
2924 relativeb = b->type == reloc_class_relative;
2926 if (relativea < relativeb)
2927 return 1;
2928 if (relativea > relativeb)
2929 return -1;
2930 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2931 return -1;
2932 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2933 return 1;
2934 if (a->rela->r_offset < b->rela->r_offset)
2935 return -1;
2936 if (a->rela->r_offset > b->rela->r_offset)
2937 return 1;
2938 return 0;
2941 static int
2942 elf_link_sort_cmp2 (const void *A, const void *B)
2944 const struct elf_link_sort_rela *a = A;
2945 const struct elf_link_sort_rela *b = B;
2946 int copya, copyb;
2948 if (a->offset < b->offset)
2949 return -1;
2950 if (a->offset > b->offset)
2951 return 1;
2952 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2953 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
2954 if (copya < copyb)
2955 return -1;
2956 if (copya > copyb)
2957 return 1;
2958 if (a->rela->r_offset < b->rela->r_offset)
2959 return -1;
2960 if (a->rela->r_offset > b->rela->r_offset)
2961 return 1;
2962 return 0;
2965 static size_t
2966 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
2968 asection *reldyn;
2969 bfd_size_type count, size;
2970 size_t i, ret, sort_elt, ext_size;
2971 bfd_byte *sort, *s_non_relative, *p;
2972 struct elf_link_sort_rela *sq;
2973 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2974 int i2e = bed->s->int_rels_per_ext_rel;
2975 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2976 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2977 struct bfd_link_order *lo;
2979 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2980 if (reldyn == NULL || reldyn->_raw_size == 0)
2982 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2983 if (reldyn == NULL || reldyn->_raw_size == 0)
2984 return 0;
2985 ext_size = sizeof (Elf_External_Rel);
2986 swap_in = bed->s->swap_reloc_in;
2987 swap_out = bed->s->swap_reloc_out;
2989 else
2991 ext_size = sizeof (Elf_External_Rela);
2992 swap_in = bed->s->swap_reloca_in;
2993 swap_out = bed->s->swap_reloca_out;
2995 count = reldyn->_raw_size / ext_size;
2997 size = 0;
2998 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2999 if (lo->type == bfd_indirect_link_order)
3001 asection *o = lo->u.indirect.section;
3002 size += o->_raw_size;
3005 if (size != reldyn->_raw_size)
3006 return 0;
3008 sort_elt = (sizeof (struct elf_link_sort_rela)
3009 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3010 sort = bfd_zmalloc (sort_elt * count);
3011 if (sort == NULL)
3013 (*info->callbacks->warning)
3014 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
3015 return 0;
3018 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3019 if (lo->type == bfd_indirect_link_order)
3021 bfd_byte *erel, *erelend;
3022 asection *o = lo->u.indirect.section;
3024 erel = o->contents;
3025 erelend = o->contents + o->_raw_size;
3026 p = sort + o->output_offset / ext_size * sort_elt;
3027 while (erel < erelend)
3029 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3030 (*swap_in) (abfd, erel, s->rela);
3031 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
3032 p += sort_elt;
3033 erel += ext_size;
3037 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
3039 for (i = 0, p = sort; i < count; i++, p += sort_elt)
3041 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3042 if (s->type != reloc_class_relative)
3043 break;
3045 ret = i;
3046 s_non_relative = p;
3048 sq = (struct elf_link_sort_rela *) s_non_relative;
3049 for (; i < count; i++, p += sort_elt)
3051 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
3052 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
3053 sq = sp;
3054 sp->offset = sq->rela->r_offset;
3057 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
3059 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3060 if (lo->type == bfd_indirect_link_order)
3062 bfd_byte *erel, *erelend;
3063 asection *o = lo->u.indirect.section;
3065 erel = o->contents;
3066 erelend = o->contents + o->_raw_size;
3067 p = sort + o->output_offset / ext_size * sort_elt;
3068 while (erel < erelend)
3070 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3071 (*swap_out) (abfd, s->rela, erel);
3072 p += sort_elt;
3073 erel += ext_size;
3077 free (sort);
3078 *psec = reldyn;
3079 return ret;
3082 /* Do the final step of an ELF link. */
3084 bfd_boolean
3085 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
3087 bfd_boolean dynamic;
3088 bfd_boolean emit_relocs;
3089 bfd *dynobj;
3090 struct elf_final_link_info finfo;
3091 register asection *o;
3092 register struct bfd_link_order *p;
3093 register bfd *sub;
3094 bfd_size_type max_contents_size;
3095 bfd_size_type max_external_reloc_size;
3096 bfd_size_type max_internal_reloc_count;
3097 bfd_size_type max_sym_count;
3098 bfd_size_type max_sym_shndx_count;
3099 file_ptr off;
3100 Elf_Internal_Sym elfsym;
3101 unsigned int i;
3102 Elf_Internal_Shdr *symtab_hdr;
3103 Elf_Internal_Shdr *symtab_shndx_hdr;
3104 Elf_Internal_Shdr *symstrtab_hdr;
3105 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3106 struct elf_outext_info eoinfo;
3107 bfd_boolean merged;
3108 size_t relativecount = 0;
3109 asection *reldyn = 0;
3110 bfd_size_type amt;
3112 if (! is_elf_hash_table (info->hash))
3113 return FALSE;
3115 if (info->shared)
3116 abfd->flags |= DYNAMIC;
3118 dynamic = elf_hash_table (info)->dynamic_sections_created;
3119 dynobj = elf_hash_table (info)->dynobj;
3121 emit_relocs = (info->relocatable
3122 || info->emitrelocations
3123 || bed->elf_backend_emit_relocs);
3125 finfo.info = info;
3126 finfo.output_bfd = abfd;
3127 finfo.symstrtab = elf_stringtab_init ();
3128 if (finfo.symstrtab == NULL)
3129 return FALSE;
3131 if (! dynamic)
3133 finfo.dynsym_sec = NULL;
3134 finfo.hash_sec = NULL;
3135 finfo.symver_sec = NULL;
3137 else
3139 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3140 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3141 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3142 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3143 /* Note that it is OK if symver_sec is NULL. */
3146 finfo.contents = NULL;
3147 finfo.external_relocs = NULL;
3148 finfo.internal_relocs = NULL;
3149 finfo.external_syms = NULL;
3150 finfo.locsym_shndx = NULL;
3151 finfo.internal_syms = NULL;
3152 finfo.indices = NULL;
3153 finfo.sections = NULL;
3154 finfo.symbuf = NULL;
3155 finfo.symshndxbuf = NULL;
3156 finfo.symbuf_count = 0;
3157 finfo.shndxbuf_size = 0;
3159 /* Count up the number of relocations we will output for each output
3160 section, so that we know the sizes of the reloc sections. We
3161 also figure out some maximum sizes. */
3162 max_contents_size = 0;
3163 max_external_reloc_size = 0;
3164 max_internal_reloc_count = 0;
3165 max_sym_count = 0;
3166 max_sym_shndx_count = 0;
3167 merged = FALSE;
3168 for (o = abfd->sections; o != NULL; o = o->next)
3170 struct bfd_elf_section_data *esdo = elf_section_data (o);
3171 o->reloc_count = 0;
3173 for (p = o->link_order_head; p != NULL; p = p->next)
3175 unsigned int reloc_count = 0;
3176 struct bfd_elf_section_data *esdi = NULL;
3177 unsigned int *rel_count1;
3179 if (p->type == bfd_section_reloc_link_order
3180 || p->type == bfd_symbol_reloc_link_order)
3181 reloc_count = 1;
3182 else if (p->type == bfd_indirect_link_order)
3184 asection *sec;
3186 sec = p->u.indirect.section;
3187 esdi = elf_section_data (sec);
3189 /* Mark all sections which are to be included in the
3190 link. This will normally be every section. We need
3191 to do this so that we can identify any sections which
3192 the linker has decided to not include. */
3193 sec->linker_mark = TRUE;
3195 if (sec->flags & SEC_MERGE)
3196 merged = TRUE;
3198 if (info->relocatable || info->emitrelocations)
3199 reloc_count = sec->reloc_count;
3200 else if (bed->elf_backend_count_relocs)
3202 Elf_Internal_Rela * relocs;
3204 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3205 info->keep_memory);
3207 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
3209 if (elf_section_data (o)->relocs != relocs)
3210 free (relocs);
3213 if (sec->_raw_size > max_contents_size)
3214 max_contents_size = sec->_raw_size;
3215 if (sec->_cooked_size > max_contents_size)
3216 max_contents_size = sec->_cooked_size;
3218 /* We are interested in just local symbols, not all
3219 symbols. */
3220 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3221 && (sec->owner->flags & DYNAMIC) == 0)
3223 size_t sym_count;
3225 if (elf_bad_symtab (sec->owner))
3226 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3227 / sizeof (Elf_External_Sym));
3228 else
3229 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3231 if (sym_count > max_sym_count)
3232 max_sym_count = sym_count;
3234 if (sym_count > max_sym_shndx_count
3235 && elf_symtab_shndx (sec->owner) != 0)
3236 max_sym_shndx_count = sym_count;
3238 if ((sec->flags & SEC_RELOC) != 0)
3240 size_t ext_size;
3242 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3243 if (ext_size > max_external_reloc_size)
3244 max_external_reloc_size = ext_size;
3245 if (sec->reloc_count > max_internal_reloc_count)
3246 max_internal_reloc_count = sec->reloc_count;
3251 if (reloc_count == 0)
3252 continue;
3254 o->reloc_count += reloc_count;
3256 /* MIPS may have a mix of REL and RELA relocs on sections.
3257 To support this curious ABI we keep reloc counts in
3258 elf_section_data too. We must be careful to add the
3259 relocations from the input section to the right output
3260 count. FIXME: Get rid of one count. We have
3261 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3262 rel_count1 = &esdo->rel_count;
3263 if (esdi != NULL)
3265 bfd_boolean same_size;
3266 bfd_size_type entsize1;
3268 entsize1 = esdi->rel_hdr.sh_entsize;
3269 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3270 || entsize1 == sizeof (Elf_External_Rela));
3271 same_size = (!o->use_rela_p
3272 == (entsize1 == sizeof (Elf_External_Rel)));
3274 if (!same_size)
3275 rel_count1 = &esdo->rel_count2;
3277 if (esdi->rel_hdr2 != NULL)
3279 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3280 unsigned int alt_count;
3281 unsigned int *rel_count2;
3283 BFD_ASSERT (entsize2 != entsize1
3284 && (entsize2 == sizeof (Elf_External_Rel)
3285 || entsize2 == sizeof (Elf_External_Rela)));
3287 rel_count2 = &esdo->rel_count2;
3288 if (!same_size)
3289 rel_count2 = &esdo->rel_count;
3291 /* The following is probably too simplistic if the
3292 backend counts output relocs unusually. */
3293 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3294 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3295 *rel_count2 += alt_count;
3296 reloc_count -= alt_count;
3299 *rel_count1 += reloc_count;
3302 if (o->reloc_count > 0)
3303 o->flags |= SEC_RELOC;
3304 else
3306 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3307 set it (this is probably a bug) and if it is set
3308 assign_section_numbers will create a reloc section. */
3309 o->flags &=~ SEC_RELOC;
3312 /* If the SEC_ALLOC flag is not set, force the section VMA to
3313 zero. This is done in elf_fake_sections as well, but forcing
3314 the VMA to 0 here will ensure that relocs against these
3315 sections are handled correctly. */
3316 if ((o->flags & SEC_ALLOC) == 0
3317 && ! o->user_set_vma)
3318 o->vma = 0;
3321 if (! info->relocatable && merged)
3322 elf_link_hash_traverse (elf_hash_table (info),
3323 _bfd_elf_link_sec_merge_syms, abfd);
3325 /* Figure out the file positions for everything but the symbol table
3326 and the relocs. We set symcount to force assign_section_numbers
3327 to create a symbol table. */
3328 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3329 BFD_ASSERT (! abfd->output_has_begun);
3330 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3331 goto error_return;
3333 /* That created the reloc sections. Set their sizes, and assign
3334 them file positions, and allocate some buffers. */
3335 for (o = abfd->sections; o != NULL; o = o->next)
3337 if ((o->flags & SEC_RELOC) != 0)
3339 if (!(_bfd_elf_link_size_reloc_section
3340 (abfd, &elf_section_data (o)->rel_hdr, o)))
3341 goto error_return;
3343 if (elf_section_data (o)->rel_hdr2
3344 && !(_bfd_elf_link_size_reloc_section
3345 (abfd, elf_section_data (o)->rel_hdr2, o)))
3346 goto error_return;
3349 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3350 to count upwards while actually outputting the relocations. */
3351 elf_section_data (o)->rel_count = 0;
3352 elf_section_data (o)->rel_count2 = 0;
3355 _bfd_elf_assign_file_positions_for_relocs (abfd);
3357 /* We have now assigned file positions for all the sections except
3358 .symtab and .strtab. We start the .symtab section at the current
3359 file position, and write directly to it. We build the .strtab
3360 section in memory. */
3361 bfd_get_symcount (abfd) = 0;
3362 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3363 /* sh_name is set in prep_headers. */
3364 symtab_hdr->sh_type = SHT_SYMTAB;
3365 /* sh_flags, sh_addr and sh_size all start off zero. */
3366 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3367 /* sh_link is set in assign_section_numbers. */
3368 /* sh_info is set below. */
3369 /* sh_offset is set just below. */
3370 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3372 off = elf_tdata (abfd)->next_file_pos;
3373 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3375 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3376 incorrect. We do not yet know the size of the .symtab section.
3377 We correct next_file_pos below, after we do know the size. */
3379 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3380 continuously seeking to the right position in the file. */
3381 if (! info->keep_memory || max_sym_count < 20)
3382 finfo.symbuf_size = 20;
3383 else
3384 finfo.symbuf_size = max_sym_count;
3385 amt = finfo.symbuf_size;
3386 amt *= sizeof (Elf_External_Sym);
3387 finfo.symbuf = bfd_malloc (amt);
3388 if (finfo.symbuf == NULL)
3389 goto error_return;
3390 if (elf_numsections (abfd) > SHN_LORESERVE)
3392 /* Wild guess at number of output symbols. realloc'd as needed. */
3393 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3394 finfo.shndxbuf_size = amt;
3395 amt *= sizeof (Elf_External_Sym_Shndx);
3396 finfo.symshndxbuf = bfd_zmalloc (amt);
3397 if (finfo.symshndxbuf == NULL)
3398 goto error_return;
3401 /* Start writing out the symbol table. The first symbol is always a
3402 dummy symbol. */
3403 if (info->strip != strip_all
3404 || emit_relocs)
3406 elfsym.st_value = 0;
3407 elfsym.st_size = 0;
3408 elfsym.st_info = 0;
3409 elfsym.st_other = 0;
3410 elfsym.st_shndx = SHN_UNDEF;
3411 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3412 NULL))
3413 goto error_return;
3416 #if 0
3417 /* Some standard ELF linkers do this, but we don't because it causes
3418 bootstrap comparison failures. */
3419 /* Output a file symbol for the output file as the second symbol.
3420 We output this even if we are discarding local symbols, although
3421 I'm not sure if this is correct. */
3422 elfsym.st_value = 0;
3423 elfsym.st_size = 0;
3424 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3425 elfsym.st_other = 0;
3426 elfsym.st_shndx = SHN_ABS;
3427 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3428 &elfsym, bfd_abs_section_ptr, NULL))
3429 goto error_return;
3430 #endif
3432 /* Output a symbol for each section. We output these even if we are
3433 discarding local symbols, since they are used for relocs. These
3434 symbols have no names. We store the index of each one in the
3435 index field of the section, so that we can find it again when
3436 outputting relocs. */
3437 if (info->strip != strip_all
3438 || emit_relocs)
3440 elfsym.st_size = 0;
3441 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3442 elfsym.st_other = 0;
3443 for (i = 1; i < elf_numsections (abfd); i++)
3445 o = section_from_elf_index (abfd, i);
3446 if (o != NULL)
3447 o->target_index = bfd_get_symcount (abfd);
3448 elfsym.st_shndx = i;
3449 if (info->relocatable || o == NULL)
3450 elfsym.st_value = 0;
3451 else
3452 elfsym.st_value = o->vma;
3453 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
3454 goto error_return;
3455 if (i == SHN_LORESERVE - 1)
3456 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3460 /* Allocate some memory to hold information read in from the input
3461 files. */
3462 if (max_contents_size != 0)
3464 finfo.contents = bfd_malloc (max_contents_size);
3465 if (finfo.contents == NULL)
3466 goto error_return;
3469 if (max_external_reloc_size != 0)
3471 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
3472 if (finfo.external_relocs == NULL)
3473 goto error_return;
3476 if (max_internal_reloc_count != 0)
3478 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3479 amt *= sizeof (Elf_Internal_Rela);
3480 finfo.internal_relocs = bfd_malloc (amt);
3481 if (finfo.internal_relocs == NULL)
3482 goto error_return;
3485 if (max_sym_count != 0)
3487 amt = max_sym_count * sizeof (Elf_External_Sym);
3488 finfo.external_syms = bfd_malloc (amt);
3489 if (finfo.external_syms == NULL)
3490 goto error_return;
3492 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3493 finfo.internal_syms = bfd_malloc (amt);
3494 if (finfo.internal_syms == NULL)
3495 goto error_return;
3497 amt = max_sym_count * sizeof (long);
3498 finfo.indices = bfd_malloc (amt);
3499 if (finfo.indices == NULL)
3500 goto error_return;
3502 amt = max_sym_count * sizeof (asection *);
3503 finfo.sections = bfd_malloc (amt);
3504 if (finfo.sections == NULL)
3505 goto error_return;
3508 if (max_sym_shndx_count != 0)
3510 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3511 finfo.locsym_shndx = bfd_malloc (amt);
3512 if (finfo.locsym_shndx == NULL)
3513 goto error_return;
3516 if (elf_hash_table (info)->tls_sec)
3518 bfd_vma base, end = 0;
3519 asection *sec;
3521 for (sec = elf_hash_table (info)->tls_sec;
3522 sec && (sec->flags & SEC_THREAD_LOCAL);
3523 sec = sec->next)
3525 bfd_vma size = sec->_raw_size;
3527 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3529 struct bfd_link_order *o;
3531 for (o = sec->link_order_head; o != NULL; o = o->next)
3532 if (size < o->offset + o->size)
3533 size = o->offset + o->size;
3535 end = sec->vma + size;
3537 base = elf_hash_table (info)->tls_sec->vma;
3538 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3539 elf_hash_table (info)->tls_size = end - base;
3542 /* Since ELF permits relocations to be against local symbols, we
3543 must have the local symbols available when we do the relocations.
3544 Since we would rather only read the local symbols once, and we
3545 would rather not keep them in memory, we handle all the
3546 relocations for a single input file at the same time.
3548 Unfortunately, there is no way to know the total number of local
3549 symbols until we have seen all of them, and the local symbol
3550 indices precede the global symbol indices. This means that when
3551 we are generating relocatable output, and we see a reloc against
3552 a global symbol, we can not know the symbol index until we have
3553 finished examining all the local symbols to see which ones we are
3554 going to output. To deal with this, we keep the relocations in
3555 memory, and don't output them until the end of the link. This is
3556 an unfortunate waste of memory, but I don't see a good way around
3557 it. Fortunately, it only happens when performing a relocatable
3558 link, which is not the common case. FIXME: If keep_memory is set
3559 we could write the relocs out and then read them again; I don't
3560 know how bad the memory loss will be. */
3562 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3563 sub->output_has_begun = FALSE;
3564 for (o = abfd->sections; o != NULL; o = o->next)
3566 for (p = o->link_order_head; p != NULL; p = p->next)
3568 if (p->type == bfd_indirect_link_order
3569 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3570 == bfd_target_elf_flavour)
3571 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3573 if (! sub->output_has_begun)
3575 if (! elf_link_input_bfd (&finfo, sub))
3576 goto error_return;
3577 sub->output_has_begun = TRUE;
3580 else if (p->type == bfd_section_reloc_link_order
3581 || p->type == bfd_symbol_reloc_link_order)
3583 if (! elf_reloc_link_order (abfd, info, o, p))
3584 goto error_return;
3586 else
3588 if (! _bfd_default_link_order (abfd, info, o, p))
3589 goto error_return;
3594 /* Output any global symbols that got converted to local in a
3595 version script or due to symbol visibility. We do this in a
3596 separate step since ELF requires all local symbols to appear
3597 prior to any global symbols. FIXME: We should only do this if
3598 some global symbols were, in fact, converted to become local.
3599 FIXME: Will this work correctly with the Irix 5 linker? */
3600 eoinfo.failed = FALSE;
3601 eoinfo.finfo = &finfo;
3602 eoinfo.localsyms = TRUE;
3603 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3604 &eoinfo);
3605 if (eoinfo.failed)
3606 return FALSE;
3608 /* That wrote out all the local symbols. Finish up the symbol table
3609 with the global symbols. Even if we want to strip everything we
3610 can, we still need to deal with those global symbols that got
3611 converted to local in a version script. */
3613 /* The sh_info field records the index of the first non local symbol. */
3614 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3616 if (dynamic
3617 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3619 Elf_Internal_Sym sym;
3620 Elf_External_Sym *dynsym =
3621 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3622 long last_local = 0;
3624 /* Write out the section symbols for the output sections. */
3625 if (info->shared)
3627 asection *s;
3629 sym.st_size = 0;
3630 sym.st_name = 0;
3631 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3632 sym.st_other = 0;
3634 for (s = abfd->sections; s != NULL; s = s->next)
3636 int indx;
3637 Elf_External_Sym *dest;
3639 indx = elf_section_data (s)->this_idx;
3640 BFD_ASSERT (indx > 0);
3641 sym.st_shndx = indx;
3642 sym.st_value = s->vma;
3643 dest = dynsym + elf_section_data (s)->dynindx;
3644 elf_swap_symbol_out (abfd, &sym, dest, 0);
3647 last_local = bfd_count_sections (abfd);
3650 /* Write out the local dynsyms. */
3651 if (elf_hash_table (info)->dynlocal)
3653 struct elf_link_local_dynamic_entry *e;
3654 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3656 asection *s;
3657 Elf_External_Sym *dest;
3659 sym.st_size = e->isym.st_size;
3660 sym.st_other = e->isym.st_other;
3662 /* Copy the internal symbol as is.
3663 Note that we saved a word of storage and overwrote
3664 the original st_name with the dynstr_index. */
3665 sym = e->isym;
3667 if (e->isym.st_shndx != SHN_UNDEF
3668 && (e->isym.st_shndx < SHN_LORESERVE
3669 || e->isym.st_shndx > SHN_HIRESERVE))
3671 s = bfd_section_from_elf_index (e->input_bfd,
3672 e->isym.st_shndx);
3674 sym.st_shndx =
3675 elf_section_data (s->output_section)->this_idx;
3676 sym.st_value = (s->output_section->vma
3677 + s->output_offset
3678 + e->isym.st_value);
3681 if (last_local < e->dynindx)
3682 last_local = e->dynindx;
3684 dest = dynsym + e->dynindx;
3685 elf_swap_symbol_out (abfd, &sym, dest, 0);
3689 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3690 last_local + 1;
3693 /* We get the global symbols from the hash table. */
3694 eoinfo.failed = FALSE;
3695 eoinfo.localsyms = FALSE;
3696 eoinfo.finfo = &finfo;
3697 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3698 &eoinfo);
3699 if (eoinfo.failed)
3700 return FALSE;
3702 /* If backend needs to output some symbols not present in the hash
3703 table, do it now. */
3704 if (bed->elf_backend_output_arch_syms)
3706 typedef bfd_boolean (*out_sym_func)
3707 (void *, const char *, Elf_Internal_Sym *, asection *,
3708 struct elf_link_hash_entry *);
3710 if (! ((*bed->elf_backend_output_arch_syms)
3711 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
3712 return FALSE;
3715 /* Flush all symbols to the file. */
3716 if (! elf_link_flush_output_syms (&finfo))
3717 return FALSE;
3719 /* Now we know the size of the symtab section. */
3720 off += symtab_hdr->sh_size;
3722 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3723 if (symtab_shndx_hdr->sh_name != 0)
3725 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3726 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3727 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3728 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3729 symtab_shndx_hdr->sh_size = amt;
3731 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3732 off, TRUE);
3734 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3735 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
3736 return FALSE;
3740 /* Finish up and write out the symbol string table (.strtab)
3741 section. */
3742 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3743 /* sh_name was set in prep_headers. */
3744 symstrtab_hdr->sh_type = SHT_STRTAB;
3745 symstrtab_hdr->sh_flags = 0;
3746 symstrtab_hdr->sh_addr = 0;
3747 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3748 symstrtab_hdr->sh_entsize = 0;
3749 symstrtab_hdr->sh_link = 0;
3750 symstrtab_hdr->sh_info = 0;
3751 /* sh_offset is set just below. */
3752 symstrtab_hdr->sh_addralign = 1;
3754 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3755 elf_tdata (abfd)->next_file_pos = off;
3757 if (bfd_get_symcount (abfd) > 0)
3759 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3760 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3761 return FALSE;
3764 /* Adjust the relocs to have the correct symbol indices. */
3765 for (o = abfd->sections; o != NULL; o = o->next)
3767 if ((o->flags & SEC_RELOC) == 0)
3768 continue;
3770 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3771 elf_section_data (o)->rel_count,
3772 elf_section_data (o)->rel_hashes);
3773 if (elf_section_data (o)->rel_hdr2 != NULL)
3774 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3775 elf_section_data (o)->rel_count2,
3776 (elf_section_data (o)->rel_hashes
3777 + elf_section_data (o)->rel_count));
3779 /* Set the reloc_count field to 0 to prevent write_relocs from
3780 trying to swap the relocs out itself. */
3781 o->reloc_count = 0;
3784 if (dynamic && info->combreloc && dynobj != NULL)
3785 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3787 /* If we are linking against a dynamic object, or generating a
3788 shared library, finish up the dynamic linking information. */
3789 if (dynamic)
3791 Elf_External_Dyn *dyncon, *dynconend;
3793 /* Fix up .dynamic entries. */
3794 o = bfd_get_section_by_name (dynobj, ".dynamic");
3795 BFD_ASSERT (o != NULL);
3797 dyncon = (Elf_External_Dyn *) o->contents;
3798 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3799 for (; dyncon < dynconend; dyncon++)
3801 Elf_Internal_Dyn dyn;
3802 const char *name;
3803 unsigned int type;
3805 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3807 switch (dyn.d_tag)
3809 default:
3810 break;
3811 case DT_NULL:
3812 if (relativecount > 0 && dyncon + 1 < dynconend)
3814 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3816 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3817 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3818 default: break;
3820 if (dyn.d_tag != DT_NULL)
3822 dyn.d_un.d_val = relativecount;
3823 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3824 relativecount = 0;
3827 break;
3828 case DT_INIT:
3829 name = info->init_function;
3830 goto get_sym;
3831 case DT_FINI:
3832 name = info->fini_function;
3833 get_sym:
3835 struct elf_link_hash_entry *h;
3837 h = elf_link_hash_lookup (elf_hash_table (info), name,
3838 FALSE, FALSE, TRUE);
3839 if (h != NULL
3840 && (h->root.type == bfd_link_hash_defined
3841 || h->root.type == bfd_link_hash_defweak))
3843 dyn.d_un.d_val = h->root.u.def.value;
3844 o = h->root.u.def.section;
3845 if (o->output_section != NULL)
3846 dyn.d_un.d_val += (o->output_section->vma
3847 + o->output_offset);
3848 else
3850 /* The symbol is imported from another shared
3851 library and does not apply to this one. */
3852 dyn.d_un.d_val = 0;
3855 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3858 break;
3860 case DT_PREINIT_ARRAYSZ:
3861 name = ".preinit_array";
3862 goto get_size;
3863 case DT_INIT_ARRAYSZ:
3864 name = ".init_array";
3865 goto get_size;
3866 case DT_FINI_ARRAYSZ:
3867 name = ".fini_array";
3868 get_size:
3869 o = bfd_get_section_by_name (abfd, name);
3870 if (o == NULL)
3872 (*_bfd_error_handler)
3873 (_("%s: could not find output section %s"),
3874 bfd_get_filename (abfd), name);
3875 goto error_return;
3877 if (o->_raw_size == 0)
3878 (*_bfd_error_handler)
3879 (_("warning: %s section has zero size"), name);
3880 dyn.d_un.d_val = o->_raw_size;
3881 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3882 break;
3884 case DT_PREINIT_ARRAY:
3885 name = ".preinit_array";
3886 goto get_vma;
3887 case DT_INIT_ARRAY:
3888 name = ".init_array";
3889 goto get_vma;
3890 case DT_FINI_ARRAY:
3891 name = ".fini_array";
3892 goto get_vma;
3894 case DT_HASH:
3895 name = ".hash";
3896 goto get_vma;
3897 case DT_STRTAB:
3898 name = ".dynstr";
3899 goto get_vma;
3900 case DT_SYMTAB:
3901 name = ".dynsym";
3902 goto get_vma;
3903 case DT_VERDEF:
3904 name = ".gnu.version_d";
3905 goto get_vma;
3906 case DT_VERNEED:
3907 name = ".gnu.version_r";
3908 goto get_vma;
3909 case DT_VERSYM:
3910 name = ".gnu.version";
3911 get_vma:
3912 o = bfd_get_section_by_name (abfd, name);
3913 if (o == NULL)
3915 (*_bfd_error_handler)
3916 (_("%s: could not find output section %s"),
3917 bfd_get_filename (abfd), name);
3918 goto error_return;
3920 dyn.d_un.d_ptr = o->vma;
3921 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3922 break;
3924 case DT_REL:
3925 case DT_RELA:
3926 case DT_RELSZ:
3927 case DT_RELASZ:
3928 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3929 type = SHT_REL;
3930 else
3931 type = SHT_RELA;
3932 dyn.d_un.d_val = 0;
3933 for (i = 1; i < elf_numsections (abfd); i++)
3935 Elf_Internal_Shdr *hdr;
3937 hdr = elf_elfsections (abfd)[i];
3938 if (hdr->sh_type == type
3939 && (hdr->sh_flags & SHF_ALLOC) != 0)
3941 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3942 dyn.d_un.d_val += hdr->sh_size;
3943 else
3945 if (dyn.d_un.d_val == 0
3946 || hdr->sh_addr < dyn.d_un.d_val)
3947 dyn.d_un.d_val = hdr->sh_addr;
3951 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3952 break;
3957 /* If we have created any dynamic sections, then output them. */
3958 if (dynobj != NULL)
3960 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3961 goto error_return;
3963 for (o = dynobj->sections; o != NULL; o = o->next)
3965 if ((o->flags & SEC_HAS_CONTENTS) == 0
3966 || o->_raw_size == 0
3967 || o->output_section == bfd_abs_section_ptr)
3968 continue;
3969 if ((o->flags & SEC_LINKER_CREATED) == 0)
3971 /* At this point, we are only interested in sections
3972 created by _bfd_elf_link_create_dynamic_sections. */
3973 continue;
3975 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3976 != SHT_STRTAB)
3977 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3979 if (! bfd_set_section_contents (abfd, o->output_section,
3980 o->contents,
3981 (file_ptr) o->output_offset,
3982 o->_raw_size))
3983 goto error_return;
3985 else
3987 /* The contents of the .dynstr section are actually in a
3988 stringtab. */
3989 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3990 if (bfd_seek (abfd, off, SEEK_SET) != 0
3991 || ! _bfd_elf_strtab_emit (abfd,
3992 elf_hash_table (info)->dynstr))
3993 goto error_return;
3998 if (info->relocatable)
4000 bfd_boolean failed = FALSE;
4002 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4003 if (failed)
4004 goto error_return;
4007 /* If we have optimized stabs strings, output them. */
4008 if (elf_hash_table (info)->stab_info != NULL)
4010 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4011 goto error_return;
4014 if (info->eh_frame_hdr)
4016 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
4017 goto error_return;
4020 if (finfo.symstrtab != NULL)
4021 _bfd_stringtab_free (finfo.symstrtab);
4022 if (finfo.contents != NULL)
4023 free (finfo.contents);
4024 if (finfo.external_relocs != NULL)
4025 free (finfo.external_relocs);
4026 if (finfo.internal_relocs != NULL)
4027 free (finfo.internal_relocs);
4028 if (finfo.external_syms != NULL)
4029 free (finfo.external_syms);
4030 if (finfo.locsym_shndx != NULL)
4031 free (finfo.locsym_shndx);
4032 if (finfo.internal_syms != NULL)
4033 free (finfo.internal_syms);
4034 if (finfo.indices != NULL)
4035 free (finfo.indices);
4036 if (finfo.sections != NULL)
4037 free (finfo.sections);
4038 if (finfo.symbuf != NULL)
4039 free (finfo.symbuf);
4040 if (finfo.symshndxbuf != NULL)
4041 free (finfo.symshndxbuf);
4042 for (o = abfd->sections; o != NULL; o = o->next)
4044 if ((o->flags & SEC_RELOC) != 0
4045 && elf_section_data (o)->rel_hashes != NULL)
4046 free (elf_section_data (o)->rel_hashes);
4049 elf_tdata (abfd)->linker = TRUE;
4051 return TRUE;
4053 error_return:
4054 if (finfo.symstrtab != NULL)
4055 _bfd_stringtab_free (finfo.symstrtab);
4056 if (finfo.contents != NULL)
4057 free (finfo.contents);
4058 if (finfo.external_relocs != NULL)
4059 free (finfo.external_relocs);
4060 if (finfo.internal_relocs != NULL)
4061 free (finfo.internal_relocs);
4062 if (finfo.external_syms != NULL)
4063 free (finfo.external_syms);
4064 if (finfo.locsym_shndx != NULL)
4065 free (finfo.locsym_shndx);
4066 if (finfo.internal_syms != NULL)
4067 free (finfo.internal_syms);
4068 if (finfo.indices != NULL)
4069 free (finfo.indices);
4070 if (finfo.sections != NULL)
4071 free (finfo.sections);
4072 if (finfo.symbuf != NULL)
4073 free (finfo.symbuf);
4074 if (finfo.symshndxbuf != NULL)
4075 free (finfo.symshndxbuf);
4076 for (o = abfd->sections; o != NULL; o = o->next)
4078 if ((o->flags & SEC_RELOC) != 0
4079 && elf_section_data (o)->rel_hashes != NULL)
4080 free (elf_section_data (o)->rel_hashes);
4083 return FALSE;
4086 /* Add a symbol to the output symbol table. */
4088 static bfd_boolean
4089 elf_link_output_sym (struct elf_final_link_info *finfo,
4090 const char *name,
4091 Elf_Internal_Sym *elfsym,
4092 asection *input_sec,
4093 struct elf_link_hash_entry *h)
4095 Elf_External_Sym *dest;
4096 Elf_External_Sym_Shndx *destshndx;
4097 bfd_boolean (*output_symbol_hook)
4098 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
4099 struct elf_link_hash_entry *);
4101 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4102 elf_backend_link_output_symbol_hook;
4103 if (output_symbol_hook != NULL)
4105 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
4106 return FALSE;
4109 if (name == NULL || *name == '\0')
4110 elfsym->st_name = 0;
4111 else if (input_sec->flags & SEC_EXCLUDE)
4112 elfsym->st_name = 0;
4113 else
4115 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
4116 name, TRUE, FALSE);
4117 if (elfsym->st_name == (unsigned long) -1)
4118 return FALSE;
4121 if (finfo->symbuf_count >= finfo->symbuf_size)
4123 if (! elf_link_flush_output_syms (finfo))
4124 return FALSE;
4127 dest = finfo->symbuf + finfo->symbuf_count;
4128 destshndx = finfo->symshndxbuf;
4129 if (destshndx != NULL)
4131 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
4133 bfd_size_type amt;
4135 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
4136 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
4137 if (destshndx == NULL)
4138 return FALSE;
4139 memset ((char *) destshndx + amt, 0, amt);
4140 finfo->shndxbuf_size *= 2;
4142 destshndx += bfd_get_symcount (finfo->output_bfd);
4145 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
4146 finfo->symbuf_count += 1;
4147 bfd_get_symcount (finfo->output_bfd) += 1;
4149 return TRUE;
4152 /* Flush the output symbols to the file. */
4154 static bfd_boolean
4155 elf_link_flush_output_syms (struct elf_final_link_info *finfo)
4157 if (finfo->symbuf_count > 0)
4159 Elf_Internal_Shdr *hdr;
4160 file_ptr pos;
4161 bfd_size_type amt;
4163 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4164 pos = hdr->sh_offset + hdr->sh_size;
4165 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
4166 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
4167 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
4168 return FALSE;
4170 hdr->sh_size += amt;
4171 finfo->symbuf_count = 0;
4174 return TRUE;
4177 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4178 allowing an unsatisfied unversioned symbol in the DSO to match a
4179 versioned symbol that would normally require an explicit version.
4180 We also handle the case that a DSO references a hidden symbol
4181 which may be satisfied by a versioned symbol in another DSO. */
4183 static bfd_boolean
4184 elf_link_check_versioned_symbol (struct bfd_link_info *info,
4185 struct elf_link_hash_entry *h)
4187 bfd *abfd;
4188 struct elf_link_loaded_list *loaded;
4190 if (!is_elf_hash_table (info->hash))
4191 return FALSE;
4193 switch (h->root.type)
4195 default:
4196 abfd = NULL;
4197 break;
4199 case bfd_link_hash_undefined:
4200 case bfd_link_hash_undefweak:
4201 abfd = h->root.u.undef.abfd;
4202 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
4203 return FALSE;
4204 break;
4206 case bfd_link_hash_defined:
4207 case bfd_link_hash_defweak:
4208 abfd = h->root.u.def.section->owner;
4209 break;
4211 case bfd_link_hash_common:
4212 abfd = h->root.u.c.p->section->owner;
4213 break;
4215 BFD_ASSERT (abfd != NULL);
4217 for (loaded = elf_hash_table (info)->loaded;
4218 loaded != NULL;
4219 loaded = loaded->next)
4221 bfd *input;
4222 Elf_Internal_Shdr *hdr;
4223 bfd_size_type symcount;
4224 bfd_size_type extsymcount;
4225 bfd_size_type extsymoff;
4226 Elf_Internal_Shdr *versymhdr;
4227 Elf_Internal_Sym *isym;
4228 Elf_Internal_Sym *isymend;
4229 Elf_Internal_Sym *isymbuf;
4230 Elf_External_Versym *ever;
4231 Elf_External_Versym *extversym;
4233 input = loaded->abfd;
4235 /* We check each DSO for a possible hidden versioned definition. */
4236 if (input == abfd
4237 || (input->flags & DYNAMIC) == 0
4238 || elf_dynversym (input) == 0)
4239 continue;
4241 hdr = &elf_tdata (input)->dynsymtab_hdr;
4243 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4244 if (elf_bad_symtab (input))
4246 extsymcount = symcount;
4247 extsymoff = 0;
4249 else
4251 extsymcount = symcount - hdr->sh_info;
4252 extsymoff = hdr->sh_info;
4255 if (extsymcount == 0)
4256 continue;
4258 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4259 NULL, NULL, NULL);
4260 if (isymbuf == NULL)
4261 return FALSE;
4263 /* Read in any version definitions. */
4264 versymhdr = &elf_tdata (input)->dynversym_hdr;
4265 extversym = bfd_malloc (versymhdr->sh_size);
4266 if (extversym == NULL)
4267 goto error_ret;
4269 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4270 || (bfd_bread (extversym, versymhdr->sh_size, input)
4271 != versymhdr->sh_size))
4273 free (extversym);
4274 error_ret:
4275 free (isymbuf);
4276 return FALSE;
4279 ever = extversym + extsymoff;
4280 isymend = isymbuf + extsymcount;
4281 for (isym = isymbuf; isym < isymend; isym++, ever++)
4283 const char *name;
4284 Elf_Internal_Versym iver;
4285 unsigned short version_index;
4287 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4288 || isym->st_shndx == SHN_UNDEF)
4289 continue;
4291 name = bfd_elf_string_from_elf_section (input,
4292 hdr->sh_link,
4293 isym->st_name);
4294 if (strcmp (name, h->root.root.string) != 0)
4295 continue;
4297 _bfd_elf_swap_versym_in (input, ever, &iver);
4299 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4301 /* If we have a non-hidden versioned sym, then it should
4302 have provided a definition for the undefined sym. */
4303 abort ();
4306 version_index = iver.vs_vers & VERSYM_VERSION;
4307 if (version_index == 1 || version_index == 2)
4309 /* This is the base or first version. We can use it. */
4310 free (extversym);
4311 free (isymbuf);
4312 return TRUE;
4316 free (extversym);
4317 free (isymbuf);
4320 return FALSE;
4323 /* Add an external symbol to the symbol table. This is called from
4324 the hash table traversal routine. When generating a shared object,
4325 we go through the symbol table twice. The first time we output
4326 anything that might have been forced to local scope in a version
4327 script. The second time we output the symbols that are still
4328 global symbols. */
4330 static bfd_boolean
4331 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
4333 struct elf_outext_info *eoinfo = data;
4334 struct elf_final_link_info *finfo = eoinfo->finfo;
4335 bfd_boolean strip;
4336 Elf_Internal_Sym sym;
4337 asection *input_sec;
4339 if (h->root.type == bfd_link_hash_warning)
4341 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4342 if (h->root.type == bfd_link_hash_new)
4343 return TRUE;
4346 /* Decide whether to output this symbol in this pass. */
4347 if (eoinfo->localsyms)
4349 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4350 return TRUE;
4352 else
4354 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4355 return TRUE;
4358 /* If we have an undefined symbol reference here then it must have
4359 come from a shared library that is being linked in. (Undefined
4360 references in regular files have already been handled). If we
4361 are reporting errors for this situation then do so now. */
4362 if (h->root.type == bfd_link_hash_undefined
4363 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4364 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4365 && ! elf_link_check_versioned_symbol (finfo->info, h)
4366 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
4368 if (! ((*finfo->info->callbacks->undefined_symbol)
4369 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4370 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
4372 eoinfo->failed = TRUE;
4373 return FALSE;
4377 /* We should also warn if a forced local symbol is referenced from
4378 shared libraries. */
4379 if (! finfo->info->relocatable
4380 && (! finfo->info->shared)
4381 && (h->elf_link_hash_flags
4382 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4383 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4384 && ! elf_link_check_versioned_symbol (finfo->info, h))
4386 (*_bfd_error_handler)
4387 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4388 bfd_get_filename (finfo->output_bfd),
4389 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4390 ? "internal"
4391 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4392 ? "hidden" : "local",
4393 h->root.root.string,
4394 bfd_archive_filename (h->root.u.def.section->owner));
4395 eoinfo->failed = TRUE;
4396 return FALSE;
4399 /* We don't want to output symbols that have never been mentioned by
4400 a regular file, or that we have been told to strip. However, if
4401 h->indx is set to -2, the symbol is used by a reloc and we must
4402 output it. */
4403 if (h->indx == -2)
4404 strip = FALSE;
4405 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4406 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4407 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4408 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4409 strip = TRUE;
4410 else if (finfo->info->strip == strip_all)
4411 strip = TRUE;
4412 else if (finfo->info->strip == strip_some
4413 && bfd_hash_lookup (finfo->info->keep_hash,
4414 h->root.root.string, FALSE, FALSE) == NULL)
4415 strip = TRUE;
4416 else if (finfo->info->strip_discarded
4417 && (h->root.type == bfd_link_hash_defined
4418 || h->root.type == bfd_link_hash_defweak)
4419 && elf_discarded_section (h->root.u.def.section))
4420 strip = TRUE;
4421 else
4422 strip = FALSE;
4424 /* If we're stripping it, and it's not a dynamic symbol, there's
4425 nothing else to do unless it is a forced local symbol. */
4426 if (strip
4427 && h->dynindx == -1
4428 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4429 return TRUE;
4431 sym.st_value = 0;
4432 sym.st_size = h->size;
4433 sym.st_other = h->other;
4434 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4435 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4436 else if (h->root.type == bfd_link_hash_undefweak
4437 || h->root.type == bfd_link_hash_defweak)
4438 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4439 else
4440 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4442 switch (h->root.type)
4444 default:
4445 case bfd_link_hash_new:
4446 case bfd_link_hash_warning:
4447 abort ();
4448 return FALSE;
4450 case bfd_link_hash_undefined:
4451 case bfd_link_hash_undefweak:
4452 input_sec = bfd_und_section_ptr;
4453 sym.st_shndx = SHN_UNDEF;
4454 break;
4456 case bfd_link_hash_defined:
4457 case bfd_link_hash_defweak:
4459 input_sec = h->root.u.def.section;
4460 if (input_sec->output_section != NULL)
4462 sym.st_shndx =
4463 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4464 input_sec->output_section);
4465 if (sym.st_shndx == SHN_BAD)
4467 (*_bfd_error_handler)
4468 (_("%s: could not find output section %s for input section %s"),
4469 bfd_get_filename (finfo->output_bfd),
4470 input_sec->output_section->name,
4471 input_sec->name);
4472 eoinfo->failed = TRUE;
4473 return FALSE;
4476 /* ELF symbols in relocatable files are section relative,
4477 but in nonrelocatable files they are virtual
4478 addresses. */
4479 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4480 if (! finfo->info->relocatable)
4482 sym.st_value += input_sec->output_section->vma;
4483 if (h->type == STT_TLS)
4485 /* STT_TLS symbols are relative to PT_TLS segment
4486 base. */
4487 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4488 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4492 else
4494 BFD_ASSERT (input_sec->owner == NULL
4495 || (input_sec->owner->flags & DYNAMIC) != 0);
4496 sym.st_shndx = SHN_UNDEF;
4497 input_sec = bfd_und_section_ptr;
4500 break;
4502 case bfd_link_hash_common:
4503 input_sec = h->root.u.c.p->section;
4504 sym.st_shndx = SHN_COMMON;
4505 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4506 break;
4508 case bfd_link_hash_indirect:
4509 /* These symbols are created by symbol versioning. They point
4510 to the decorated version of the name. For example, if the
4511 symbol foo@@GNU_1.2 is the default, which should be used when
4512 foo is used with no version, then we add an indirect symbol
4513 foo which points to foo@@GNU_1.2. We ignore these symbols,
4514 since the indirected symbol is already in the hash table. */
4515 return TRUE;
4518 /* Give the processor backend a chance to tweak the symbol value,
4519 and also to finish up anything that needs to be done for this
4520 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4521 forced local syms when non-shared is due to a historical quirk. */
4522 if ((h->dynindx != -1
4523 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4524 && ((finfo->info->shared
4525 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4526 || h->root.type != bfd_link_hash_undefweak))
4527 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4528 && elf_hash_table (finfo->info)->dynamic_sections_created)
4530 const struct elf_backend_data *bed;
4532 bed = get_elf_backend_data (finfo->output_bfd);
4533 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4534 (finfo->output_bfd, finfo->info, h, &sym)))
4536 eoinfo->failed = TRUE;
4537 return FALSE;
4541 /* If we are marking the symbol as undefined, and there are no
4542 non-weak references to this symbol from a regular object, then
4543 mark the symbol as weak undefined; if there are non-weak
4544 references, mark the symbol as strong. We can't do this earlier,
4545 because it might not be marked as undefined until the
4546 finish_dynamic_symbol routine gets through with it. */
4547 if (sym.st_shndx == SHN_UNDEF
4548 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4549 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4550 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4552 int bindtype;
4554 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4555 bindtype = STB_GLOBAL;
4556 else
4557 bindtype = STB_WEAK;
4558 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4561 /* If a non-weak symbol with non-default visibility is not defined
4562 locally, it is a fatal error. */
4563 if (! finfo->info->relocatable
4564 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4565 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4566 && h->root.type == bfd_link_hash_undefined
4567 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4569 (*_bfd_error_handler)
4570 (_("%s: %s symbol `%s' isn't defined"),
4571 bfd_get_filename (finfo->output_bfd),
4572 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4573 ? "protected"
4574 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4575 ? "internal" : "hidden",
4576 h->root.root.string);
4577 eoinfo->failed = TRUE;
4578 return FALSE;
4581 /* If this symbol should be put in the .dynsym section, then put it
4582 there now. We already know the symbol index. We also fill in
4583 the entry in the .hash section. */
4584 if (h->dynindx != -1
4585 && elf_hash_table (finfo->info)->dynamic_sections_created)
4587 size_t bucketcount;
4588 size_t bucket;
4589 size_t hash_entry_size;
4590 bfd_byte *bucketpos;
4591 bfd_vma chain;
4592 Elf_External_Sym *esym;
4594 sym.st_name = h->dynstr_index;
4595 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4596 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
4598 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4599 bucket = h->elf_hash_value % bucketcount;
4600 hash_entry_size
4601 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4602 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4603 + (bucket + 2) * hash_entry_size);
4604 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4605 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
4606 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4607 ((bfd_byte *) finfo->hash_sec->contents
4608 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4610 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4612 Elf_Internal_Versym iversym;
4613 Elf_External_Versym *eversym;
4615 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4617 if (h->verinfo.verdef == NULL)
4618 iversym.vs_vers = 0;
4619 else
4620 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4622 else
4624 if (h->verinfo.vertree == NULL)
4625 iversym.vs_vers = 1;
4626 else
4627 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4630 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4631 iversym.vs_vers |= VERSYM_HIDDEN;
4633 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4634 eversym += h->dynindx;
4635 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4639 /* If we're stripping it, then it was just a dynamic symbol, and
4640 there's nothing else to do. */
4641 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4642 return TRUE;
4644 h->indx = bfd_get_symcount (finfo->output_bfd);
4646 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
4648 eoinfo->failed = TRUE;
4649 return FALSE;
4652 return TRUE;
4655 /* Link an input file into the linker output file. This function
4656 handles all the sections and relocations of the input file at once.
4657 This is so that we only have to read the local symbols once, and
4658 don't have to keep them in memory. */
4660 static bfd_boolean
4661 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
4663 bfd_boolean (*relocate_section)
4664 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4665 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
4666 bfd *output_bfd;
4667 Elf_Internal_Shdr *symtab_hdr;
4668 size_t locsymcount;
4669 size_t extsymoff;
4670 Elf_Internal_Sym *isymbuf;
4671 Elf_Internal_Sym *isym;
4672 Elf_Internal_Sym *isymend;
4673 long *pindex;
4674 asection **ppsection;
4675 asection *o;
4676 const struct elf_backend_data *bed;
4677 bfd_boolean emit_relocs;
4678 struct elf_link_hash_entry **sym_hashes;
4680 output_bfd = finfo->output_bfd;
4681 bed = get_elf_backend_data (output_bfd);
4682 relocate_section = bed->elf_backend_relocate_section;
4684 /* If this is a dynamic object, we don't want to do anything here:
4685 we don't want the local symbols, and we don't want the section
4686 contents. */
4687 if ((input_bfd->flags & DYNAMIC) != 0)
4688 return TRUE;
4690 emit_relocs = (finfo->info->relocatable
4691 || finfo->info->emitrelocations
4692 || bed->elf_backend_emit_relocs);
4694 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4695 if (elf_bad_symtab (input_bfd))
4697 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4698 extsymoff = 0;
4700 else
4702 locsymcount = symtab_hdr->sh_info;
4703 extsymoff = symtab_hdr->sh_info;
4706 /* Read the local symbols. */
4707 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4708 if (isymbuf == NULL && locsymcount != 0)
4710 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4711 finfo->internal_syms,
4712 finfo->external_syms,
4713 finfo->locsym_shndx);
4714 if (isymbuf == NULL)
4715 return FALSE;
4718 /* Find local symbol sections and adjust values of symbols in
4719 SEC_MERGE sections. Write out those local symbols we know are
4720 going into the output file. */
4721 isymend = isymbuf + locsymcount;
4722 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4723 isym < isymend;
4724 isym++, pindex++, ppsection++)
4726 asection *isec;
4727 const char *name;
4728 Elf_Internal_Sym osym;
4730 *pindex = -1;
4732 if (elf_bad_symtab (input_bfd))
4734 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4736 *ppsection = NULL;
4737 continue;
4741 if (isym->st_shndx == SHN_UNDEF)
4742 isec = bfd_und_section_ptr;
4743 else if (isym->st_shndx < SHN_LORESERVE
4744 || isym->st_shndx > SHN_HIRESERVE)
4746 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4747 if (isec
4748 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4749 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4750 isym->st_value =
4751 _bfd_merged_section_offset (output_bfd, &isec,
4752 elf_section_data (isec)->sec_info,
4753 isym->st_value, 0);
4755 else if (isym->st_shndx == SHN_ABS)
4756 isec = bfd_abs_section_ptr;
4757 else if (isym->st_shndx == SHN_COMMON)
4758 isec = bfd_com_section_ptr;
4759 else
4761 /* Who knows? */
4762 isec = NULL;
4765 *ppsection = isec;
4767 /* Don't output the first, undefined, symbol. */
4768 if (ppsection == finfo->sections)
4769 continue;
4771 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4773 /* We never output section symbols. Instead, we use the
4774 section symbol of the corresponding section in the output
4775 file. */
4776 continue;
4779 /* If we are stripping all symbols, we don't want to output this
4780 one. */
4781 if (finfo->info->strip == strip_all)
4782 continue;
4784 /* If we are discarding all local symbols, we don't want to
4785 output this one. If we are generating a relocatable output
4786 file, then some of the local symbols may be required by
4787 relocs; we output them below as we discover that they are
4788 needed. */
4789 if (finfo->info->discard == discard_all)
4790 continue;
4792 /* If this symbol is defined in a section which we are
4793 discarding, we don't need to keep it, but note that
4794 linker_mark is only reliable for sections that have contents.
4795 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4796 as well as linker_mark. */
4797 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4798 && isec != NULL
4799 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4800 || (! finfo->info->relocatable
4801 && (isec->flags & SEC_EXCLUDE) != 0)))
4802 continue;
4804 /* Get the name of the symbol. */
4805 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4806 isym->st_name);
4807 if (name == NULL)
4808 return FALSE;
4810 /* See if we are discarding symbols with this name. */
4811 if ((finfo->info->strip == strip_some
4812 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4813 == NULL))
4814 || (((finfo->info->discard == discard_sec_merge
4815 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4816 || finfo->info->discard == discard_l)
4817 && bfd_is_local_label_name (input_bfd, name)))
4818 continue;
4820 /* If we get here, we are going to output this symbol. */
4822 osym = *isym;
4824 /* Adjust the section index for the output file. */
4825 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4826 isec->output_section);
4827 if (osym.st_shndx == SHN_BAD)
4828 return FALSE;
4830 *pindex = bfd_get_symcount (output_bfd);
4832 /* ELF symbols in relocatable files are section relative, but
4833 in executable files they are virtual addresses. Note that
4834 this code assumes that all ELF sections have an associated
4835 BFD section with a reasonable value for output_offset; below
4836 we assume that they also have a reasonable value for
4837 output_section. Any special sections must be set up to meet
4838 these requirements. */
4839 osym.st_value += isec->output_offset;
4840 if (! finfo->info->relocatable)
4842 osym.st_value += isec->output_section->vma;
4843 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4845 /* STT_TLS symbols are relative to PT_TLS segment base. */
4846 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4847 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4851 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
4852 return FALSE;
4855 /* Relocate the contents of each section. */
4856 sym_hashes = elf_sym_hashes (input_bfd);
4857 for (o = input_bfd->sections; o != NULL; o = o->next)
4859 bfd_byte *contents;
4861 if (! o->linker_mark)
4863 /* This section was omitted from the link. */
4864 continue;
4867 if ((o->flags & SEC_HAS_CONTENTS) == 0
4868 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4869 continue;
4871 if ((o->flags & SEC_LINKER_CREATED) != 0)
4873 /* Section was created by _bfd_elf_link_create_dynamic_sections
4874 or somesuch. */
4875 continue;
4878 /* Get the contents of the section. They have been cached by a
4879 relaxation routine. Note that o is a section in an input
4880 file, so the contents field will not have been set by any of
4881 the routines which work on output files. */
4882 if (elf_section_data (o)->this_hdr.contents != NULL)
4883 contents = elf_section_data (o)->this_hdr.contents;
4884 else
4886 contents = finfo->contents;
4887 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4888 o->_raw_size))
4889 return FALSE;
4892 if ((o->flags & SEC_RELOC) != 0)
4894 Elf_Internal_Rela *internal_relocs;
4896 /* Get the swapped relocs. */
4897 internal_relocs
4898 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4899 finfo->internal_relocs, FALSE);
4900 if (internal_relocs == NULL
4901 && o->reloc_count > 0)
4902 return FALSE;
4904 /* Run through the relocs looking for any against symbols
4905 from discarded sections and section symbols from
4906 removed link-once sections. Complain about relocs
4907 against discarded sections. Zero relocs against removed
4908 link-once sections. Preserve debug information as much
4909 as we can. */
4910 if (!elf_section_ignore_discarded_relocs (o))
4912 Elf_Internal_Rela *rel, *relend;
4914 rel = internal_relocs;
4915 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4916 for ( ; rel < relend; rel++)
4918 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4919 asection *sec;
4921 if (r_symndx >= locsymcount
4922 || (elf_bad_symtab (input_bfd)
4923 && finfo->sections[r_symndx] == NULL))
4925 struct elf_link_hash_entry *h;
4927 h = sym_hashes[r_symndx - extsymoff];
4928 while (h->root.type == bfd_link_hash_indirect
4929 || h->root.type == bfd_link_hash_warning)
4930 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4932 /* Complain if the definition comes from a
4933 discarded section. */
4934 sec = h->root.u.def.section;
4935 if ((h->root.type == bfd_link_hash_defined
4936 || h->root.type == bfd_link_hash_defweak)
4937 && elf_discarded_section (sec))
4939 if ((o->flags & SEC_DEBUGGING) != 0)
4941 BFD_ASSERT (r_symndx != 0);
4942 /* Try to preserve debug information. */
4943 if ((o->flags & SEC_DEBUGGING) != 0
4944 && sec->kept_section != NULL
4945 && sec->_raw_size == sec->kept_section->_raw_size)
4946 h->root.u.def.section
4947 = sec->kept_section;
4948 else
4949 memset (rel, 0, sizeof (*rel));
4951 else
4952 finfo->info->callbacks->error_handler
4953 (LD_DEFINITION_IN_DISCARDED_SECTION,
4954 _("%T: discarded in section `%s' from %s\n"),
4955 h->root.root.string,
4956 h->root.root.string,
4957 h->root.u.def.section->name,
4958 bfd_archive_filename (h->root.u.def.section->owner));
4961 else
4963 sec = finfo->sections[r_symndx];
4965 if (sec != NULL && elf_discarded_section (sec))
4967 if ((o->flags & SEC_DEBUGGING) != 0
4968 || (sec->flags & SEC_LINK_ONCE) != 0)
4970 BFD_ASSERT (r_symndx != 0);
4971 /* Try to preserve debug information. */
4972 if ((o->flags & SEC_DEBUGGING) != 0
4973 && sec->kept_section != NULL
4974 && sec->_raw_size == sec->kept_section->_raw_size)
4975 finfo->sections[r_symndx]
4976 = sec->kept_section;
4977 else
4979 rel->r_info
4980 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4981 rel->r_addend = 0;
4984 else
4986 static int count;
4987 int ok;
4988 char *buf;
4990 ok = asprintf (&buf, "local symbol %d",
4991 count++);
4992 if (ok <= 0)
4993 buf = (char *) "local symbol";
4994 finfo->info->callbacks->error_handler
4995 (LD_DEFINITION_IN_DISCARDED_SECTION,
4996 _("%T: discarded in section `%s' from %s\n"),
4997 buf, buf, sec->name,
4998 bfd_archive_filename (input_bfd));
4999 if (ok != -1)
5000 free (buf);
5007 /* Relocate the section by invoking a back end routine.
5009 The back end routine is responsible for adjusting the
5010 section contents as necessary, and (if using Rela relocs
5011 and generating a relocatable output file) adjusting the
5012 reloc addend as necessary.
5014 The back end routine does not have to worry about setting
5015 the reloc address or the reloc symbol index.
5017 The back end routine is given a pointer to the swapped in
5018 internal symbols, and can access the hash table entries
5019 for the external symbols via elf_sym_hashes (input_bfd).
5021 When generating relocatable output, the back end routine
5022 must handle STB_LOCAL/STT_SECTION symbols specially. The
5023 output symbol is going to be a section symbol
5024 corresponding to the output section, which will require
5025 the addend to be adjusted. */
5027 if (! (*relocate_section) (output_bfd, finfo->info,
5028 input_bfd, o, contents,
5029 internal_relocs,
5030 isymbuf,
5031 finfo->sections))
5032 return FALSE;
5034 if (emit_relocs)
5036 Elf_Internal_Rela *irela;
5037 Elf_Internal_Rela *irelaend;
5038 bfd_vma last_offset;
5039 struct elf_link_hash_entry **rel_hash;
5040 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
5041 unsigned int next_erel;
5042 bfd_boolean (*reloc_emitter)
5043 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
5044 bfd_boolean rela_normal;
5046 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5047 rela_normal = (bed->rela_normal
5048 && (input_rel_hdr->sh_entsize
5049 == sizeof (Elf_External_Rela)));
5051 /* Adjust the reloc addresses and symbol indices. */
5053 irela = internal_relocs;
5054 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
5055 rel_hash = (elf_section_data (o->output_section)->rel_hashes
5056 + elf_section_data (o->output_section)->rel_count
5057 + elf_section_data (o->output_section)->rel_count2);
5058 last_offset = o->output_offset;
5059 if (!finfo->info->relocatable)
5060 last_offset += o->output_section->vma;
5061 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
5063 unsigned long r_symndx;
5064 asection *sec;
5065 Elf_Internal_Sym sym;
5067 if (next_erel == bed->s->int_rels_per_ext_rel)
5069 rel_hash++;
5070 next_erel = 0;
5073 irela->r_offset = _bfd_elf_section_offset (output_bfd,
5074 finfo->info, o,
5075 irela->r_offset);
5076 if (irela->r_offset >= (bfd_vma) -2)
5078 /* This is a reloc for a deleted entry or somesuch.
5079 Turn it into an R_*_NONE reloc, at the same
5080 offset as the last reloc. elf_eh_frame.c and
5081 elf_bfd_discard_info rely on reloc offsets
5082 being ordered. */
5083 irela->r_offset = last_offset;
5084 irela->r_info = 0;
5085 irela->r_addend = 0;
5086 continue;
5089 irela->r_offset += o->output_offset;
5091 /* Relocs in an executable have to be virtual addresses. */
5092 if (!finfo->info->relocatable)
5093 irela->r_offset += o->output_section->vma;
5095 last_offset = irela->r_offset;
5097 r_symndx = ELF_R_SYM (irela->r_info);
5098 if (r_symndx == STN_UNDEF)
5099 continue;
5101 if (r_symndx >= locsymcount
5102 || (elf_bad_symtab (input_bfd)
5103 && finfo->sections[r_symndx] == NULL))
5105 struct elf_link_hash_entry *rh;
5106 unsigned long indx;
5108 /* This is a reloc against a global symbol. We
5109 have not yet output all the local symbols, so
5110 we do not know the symbol index of any global
5111 symbol. We set the rel_hash entry for this
5112 reloc to point to the global hash table entry
5113 for this symbol. The symbol index is then
5114 set at the end of elf_bfd_final_link. */
5115 indx = r_symndx - extsymoff;
5116 rh = elf_sym_hashes (input_bfd)[indx];
5117 while (rh->root.type == bfd_link_hash_indirect
5118 || rh->root.type == bfd_link_hash_warning)
5119 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5121 /* Setting the index to -2 tells
5122 elf_link_output_extsym that this symbol is
5123 used by a reloc. */
5124 BFD_ASSERT (rh->indx < 0);
5125 rh->indx = -2;
5127 *rel_hash = rh;
5129 continue;
5132 /* This is a reloc against a local symbol. */
5134 *rel_hash = NULL;
5135 sym = isymbuf[r_symndx];
5136 sec = finfo->sections[r_symndx];
5137 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
5139 /* I suppose the backend ought to fill in the
5140 section of any STT_SECTION symbol against a
5141 processor specific section. If we have
5142 discarded a section, the output_section will
5143 be the absolute section. */
5144 if (bfd_is_abs_section (sec)
5145 || (sec != NULL
5146 && bfd_is_abs_section (sec->output_section)))
5147 r_symndx = 0;
5148 else if (sec == NULL || sec->owner == NULL)
5150 bfd_set_error (bfd_error_bad_value);
5151 return FALSE;
5153 else
5155 r_symndx = sec->output_section->target_index;
5156 BFD_ASSERT (r_symndx != 0);
5159 /* Adjust the addend according to where the
5160 section winds up in the output section. */
5161 if (rela_normal)
5162 irela->r_addend += sec->output_offset;
5164 else
5166 if (finfo->indices[r_symndx] == -1)
5168 unsigned long shlink;
5169 const char *name;
5170 asection *osec;
5172 if (finfo->info->strip == strip_all)
5174 /* You can't do ld -r -s. */
5175 bfd_set_error (bfd_error_invalid_operation);
5176 return FALSE;
5179 /* This symbol was skipped earlier, but
5180 since it is needed by a reloc, we
5181 must output it now. */
5182 shlink = symtab_hdr->sh_link;
5183 name = (bfd_elf_string_from_elf_section
5184 (input_bfd, shlink, sym.st_name));
5185 if (name == NULL)
5186 return FALSE;
5188 osec = sec->output_section;
5189 sym.st_shndx =
5190 _bfd_elf_section_from_bfd_section (output_bfd,
5191 osec);
5192 if (sym.st_shndx == SHN_BAD)
5193 return FALSE;
5195 sym.st_value += sec->output_offset;
5196 if (! finfo->info->relocatable)
5198 sym.st_value += osec->vma;
5199 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
5201 /* STT_TLS symbols are relative to PT_TLS
5202 segment base. */
5203 BFD_ASSERT (elf_hash_table (finfo->info)
5204 ->tls_sec != NULL);
5205 sym.st_value -= (elf_hash_table (finfo->info)
5206 ->tls_sec->vma);
5210 finfo->indices[r_symndx]
5211 = bfd_get_symcount (output_bfd);
5213 if (! elf_link_output_sym (finfo, name, &sym, sec,
5214 NULL))
5215 return FALSE;
5218 r_symndx = finfo->indices[r_symndx];
5221 irela->r_info = ELF_R_INFO (r_symndx,
5222 ELF_R_TYPE (irela->r_info));
5225 /* Swap out the relocs. */
5226 if (bed->elf_backend_emit_relocs
5227 && !(finfo->info->relocatable
5228 || finfo->info->emitrelocations))
5229 reloc_emitter = bed->elf_backend_emit_relocs;
5230 else
5231 reloc_emitter = _bfd_elf_link_output_relocs;
5233 if (input_rel_hdr->sh_size != 0
5234 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
5235 internal_relocs))
5236 return FALSE;
5238 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
5239 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
5241 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
5242 * bed->s->int_rels_per_ext_rel);
5243 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
5244 internal_relocs))
5245 return FALSE;
5250 /* Write out the modified section contents. */
5251 if (bed->elf_backend_write_section
5252 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
5254 /* Section written out. */
5256 else switch (o->sec_info_type)
5258 case ELF_INFO_TYPE_STABS:
5259 if (! (_bfd_write_section_stabs
5260 (output_bfd,
5261 &elf_hash_table (finfo->info)->stab_info,
5262 o, &elf_section_data (o)->sec_info, contents)))
5263 return FALSE;
5264 break;
5265 case ELF_INFO_TYPE_MERGE:
5266 if (! _bfd_write_merged_section (output_bfd, o,
5267 elf_section_data (o)->sec_info))
5268 return FALSE;
5269 break;
5270 case ELF_INFO_TYPE_EH_FRAME:
5272 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5273 o, contents))
5274 return FALSE;
5276 break;
5277 default:
5279 bfd_size_type sec_size;
5281 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5282 if (! (o->flags & SEC_EXCLUDE)
5283 && ! bfd_set_section_contents (output_bfd, o->output_section,
5284 contents,
5285 (file_ptr) o->output_offset,
5286 sec_size))
5287 return FALSE;
5289 break;
5293 return TRUE;
5296 /* Generate a reloc when linking an ELF file. This is a reloc
5297 requested by the linker, and does come from any input file. This
5298 is used to build constructor and destructor tables when linking
5299 with -Ur. */
5301 static bfd_boolean
5302 elf_reloc_link_order (bfd *output_bfd,
5303 struct bfd_link_info *info,
5304 asection *output_section,
5305 struct bfd_link_order *link_order)
5307 reloc_howto_type *howto;
5308 long indx;
5309 bfd_vma offset;
5310 bfd_vma addend;
5311 struct elf_link_hash_entry **rel_hash_ptr;
5312 Elf_Internal_Shdr *rel_hdr;
5313 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5314 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5315 bfd_byte *erel;
5316 unsigned int i;
5318 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5319 if (howto == NULL)
5321 bfd_set_error (bfd_error_bad_value);
5322 return FALSE;
5325 addend = link_order->u.reloc.p->addend;
5327 /* Figure out the symbol index. */
5328 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5329 + elf_section_data (output_section)->rel_count
5330 + elf_section_data (output_section)->rel_count2);
5331 if (link_order->type == bfd_section_reloc_link_order)
5333 indx = link_order->u.reloc.p->u.section->target_index;
5334 BFD_ASSERT (indx != 0);
5335 *rel_hash_ptr = NULL;
5337 else
5339 struct elf_link_hash_entry *h;
5341 /* Treat a reloc against a defined symbol as though it were
5342 actually against the section. */
5343 h = ((struct elf_link_hash_entry *)
5344 bfd_wrapped_link_hash_lookup (output_bfd, info,
5345 link_order->u.reloc.p->u.name,
5346 FALSE, FALSE, TRUE));
5347 if (h != NULL
5348 && (h->root.type == bfd_link_hash_defined
5349 || h->root.type == bfd_link_hash_defweak))
5351 asection *section;
5353 section = h->root.u.def.section;
5354 indx = section->output_section->target_index;
5355 *rel_hash_ptr = NULL;
5356 /* It seems that we ought to add the symbol value to the
5357 addend here, but in practice it has already been added
5358 because it was passed to constructor_callback. */
5359 addend += section->output_section->vma + section->output_offset;
5361 else if (h != NULL)
5363 /* Setting the index to -2 tells elf_link_output_extsym that
5364 this symbol is used by a reloc. */
5365 h->indx = -2;
5366 *rel_hash_ptr = h;
5367 indx = 0;
5369 else
5371 if (! ((*info->callbacks->unattached_reloc)
5372 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
5373 return FALSE;
5374 indx = 0;
5378 /* If this is an inplace reloc, we must write the addend into the
5379 object file. */
5380 if (howto->partial_inplace && addend != 0)
5382 bfd_size_type size;
5383 bfd_reloc_status_type rstat;
5384 bfd_byte *buf;
5385 bfd_boolean ok;
5386 const char *sym_name;
5388 size = bfd_get_reloc_size (howto);
5389 buf = bfd_zmalloc (size);
5390 if (buf == NULL)
5391 return FALSE;
5392 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5393 switch (rstat)
5395 case bfd_reloc_ok:
5396 break;
5398 default:
5399 case bfd_reloc_outofrange:
5400 abort ();
5402 case bfd_reloc_overflow:
5403 if (link_order->type == bfd_section_reloc_link_order)
5404 sym_name = bfd_section_name (output_bfd,
5405 link_order->u.reloc.p->u.section);
5406 else
5407 sym_name = link_order->u.reloc.p->u.name;
5408 if (! ((*info->callbacks->reloc_overflow)
5409 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
5411 free (buf);
5412 return FALSE;
5414 break;
5416 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5417 link_order->offset, size);
5418 free (buf);
5419 if (! ok)
5420 return FALSE;
5423 /* The address of a reloc is relative to the section in a
5424 relocatable file, and is a virtual address in an executable
5425 file. */
5426 offset = link_order->offset;
5427 if (! info->relocatable)
5428 offset += output_section->vma;
5430 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5432 irel[i].r_offset = offset;
5433 irel[i].r_info = 0;
5434 irel[i].r_addend = 0;
5436 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5438 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5439 erel = rel_hdr->contents;
5440 if (rel_hdr->sh_type == SHT_REL)
5442 erel += (elf_section_data (output_section)->rel_count
5443 * sizeof (Elf_External_Rel));
5444 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5446 else
5448 irel[0].r_addend = addend;
5449 erel += (elf_section_data (output_section)->rel_count
5450 * sizeof (Elf_External_Rela));
5451 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5454 ++elf_section_data (output_section)->rel_count;
5456 return TRUE;
5459 /* Garbage collect unused sections. */
5461 static bfd_boolean elf_gc_sweep_symbol
5462 (struct elf_link_hash_entry *, void *);
5464 static bfd_boolean elf_gc_allocate_got_offsets
5465 (struct elf_link_hash_entry *, void *);
5467 /* The mark phase of garbage collection. For a given section, mark
5468 it and any sections in this section's group, and all the sections
5469 which define symbols to which it refers. */
5471 typedef asection * (*gc_mark_hook_fn)
5472 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5473 struct elf_link_hash_entry *, Elf_Internal_Sym *);
5475 static bfd_boolean
5476 elf_gc_mark (struct bfd_link_info *info,
5477 asection *sec,
5478 gc_mark_hook_fn gc_mark_hook)
5480 bfd_boolean ret;
5481 asection *group_sec;
5483 sec->gc_mark = 1;
5485 /* Mark all the sections in the group. */
5486 group_sec = elf_section_data (sec)->next_in_group;
5487 if (group_sec && !group_sec->gc_mark)
5488 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5489 return FALSE;
5491 /* Look through the section relocs. */
5492 ret = TRUE;
5493 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5495 Elf_Internal_Rela *relstart, *rel, *relend;
5496 Elf_Internal_Shdr *symtab_hdr;
5497 struct elf_link_hash_entry **sym_hashes;
5498 size_t nlocsyms;
5499 size_t extsymoff;
5500 bfd *input_bfd = sec->owner;
5501 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5502 Elf_Internal_Sym *isym = NULL;
5504 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5505 sym_hashes = elf_sym_hashes (input_bfd);
5507 /* Read the local symbols. */
5508 if (elf_bad_symtab (input_bfd))
5510 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5511 extsymoff = 0;
5513 else
5514 extsymoff = nlocsyms = symtab_hdr->sh_info;
5516 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5517 if (isym == NULL && nlocsyms != 0)
5519 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5520 NULL, NULL, NULL);
5521 if (isym == NULL)
5522 return FALSE;
5525 /* Read the relocations. */
5526 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5527 info->keep_memory);
5528 if (relstart == NULL)
5530 ret = FALSE;
5531 goto out1;
5533 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5535 for (rel = relstart; rel < relend; rel++)
5537 unsigned long r_symndx;
5538 asection *rsec;
5539 struct elf_link_hash_entry *h;
5541 r_symndx = ELF_R_SYM (rel->r_info);
5542 if (r_symndx == 0)
5543 continue;
5545 if (r_symndx >= nlocsyms
5546 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5548 h = sym_hashes[r_symndx - extsymoff];
5549 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5551 else
5553 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5556 if (rsec && !rsec->gc_mark)
5558 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5559 rsec->gc_mark = 1;
5560 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5562 ret = FALSE;
5563 goto out2;
5568 out2:
5569 if (elf_section_data (sec)->relocs != relstart)
5570 free (relstart);
5571 out1:
5572 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5574 if (! info->keep_memory)
5575 free (isym);
5576 else
5577 symtab_hdr->contents = (unsigned char *) isym;
5581 return ret;
5584 /* The sweep phase of garbage collection. Remove all garbage sections. */
5586 typedef bfd_boolean (*gc_sweep_hook_fn)
5587 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
5589 static bfd_boolean
5590 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
5592 bfd *sub;
5594 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5596 asection *o;
5598 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5599 continue;
5601 for (o = sub->sections; o != NULL; o = o->next)
5603 /* Keep special sections. Keep .debug sections. */
5604 if ((o->flags & SEC_LINKER_CREATED)
5605 || (o->flags & SEC_DEBUGGING))
5606 o->gc_mark = 1;
5608 if (o->gc_mark)
5609 continue;
5611 /* Skip sweeping sections already excluded. */
5612 if (o->flags & SEC_EXCLUDE)
5613 continue;
5615 /* Since this is early in the link process, it is simple
5616 to remove a section from the output. */
5617 o->flags |= SEC_EXCLUDE;
5619 /* But we also have to update some of the relocation
5620 info we collected before. */
5621 if (gc_sweep_hook
5622 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5624 Elf_Internal_Rela *internal_relocs;
5625 bfd_boolean r;
5627 internal_relocs
5628 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
5629 info->keep_memory);
5630 if (internal_relocs == NULL)
5631 return FALSE;
5633 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5635 if (elf_section_data (o)->relocs != internal_relocs)
5636 free (internal_relocs);
5638 if (!r)
5639 return FALSE;
5644 /* Remove the symbols that were in the swept sections from the dynamic
5645 symbol table. GCFIXME: Anyone know how to get them out of the
5646 static symbol table as well? */
5648 int i = 0;
5650 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
5652 elf_hash_table (info)->dynsymcount = i;
5655 return TRUE;
5658 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5660 static bfd_boolean
5661 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
5663 int *idx = idxptr;
5665 if (h->root.type == bfd_link_hash_warning)
5666 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5668 if (h->dynindx != -1
5669 && ((h->root.type != bfd_link_hash_defined
5670 && h->root.type != bfd_link_hash_defweak)
5671 || h->root.u.def.section->gc_mark))
5672 h->dynindx = (*idx)++;
5674 return TRUE;
5677 /* Propagate collected vtable information. This is called through
5678 elf_link_hash_traverse. */
5680 static bfd_boolean
5681 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
5683 if (h->root.type == bfd_link_hash_warning)
5684 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5686 /* Those that are not vtables. */
5687 if (h->vtable_parent == NULL)
5688 return TRUE;
5690 /* Those vtables that do not have parents, we cannot merge. */
5691 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5692 return TRUE;
5694 /* If we've already been done, exit. */
5695 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5696 return TRUE;
5698 /* Make sure the parent's table is up to date. */
5699 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5701 if (h->vtable_entries_used == NULL)
5703 /* None of this table's entries were referenced. Re-use the
5704 parent's table. */
5705 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5706 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5708 else
5710 size_t n;
5711 bfd_boolean *cu, *pu;
5713 /* Or the parent's entries into ours. */
5714 cu = h->vtable_entries_used;
5715 cu[-1] = TRUE;
5716 pu = h->vtable_parent->vtable_entries_used;
5717 if (pu != NULL)
5719 const struct elf_backend_data *bed;
5720 unsigned int log_file_align;
5722 bed = get_elf_backend_data (h->root.u.def.section->owner);
5723 log_file_align = bed->s->log_file_align;
5724 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5725 while (n--)
5727 if (*pu)
5728 *cu = TRUE;
5729 pu++;
5730 cu++;
5735 return TRUE;
5738 static bfd_boolean
5739 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
5741 asection *sec;
5742 bfd_vma hstart, hend;
5743 Elf_Internal_Rela *relstart, *relend, *rel;
5744 const struct elf_backend_data *bed;
5745 unsigned int log_file_align;
5747 if (h->root.type == bfd_link_hash_warning)
5748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5750 /* Take care of both those symbols that do not describe vtables as
5751 well as those that are not loaded. */
5752 if (h->vtable_parent == NULL)
5753 return TRUE;
5755 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5756 || h->root.type == bfd_link_hash_defweak);
5758 sec = h->root.u.def.section;
5759 hstart = h->root.u.def.value;
5760 hend = hstart + h->size;
5762 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
5763 if (!relstart)
5764 return *(bfd_boolean *) okp = FALSE;
5765 bed = get_elf_backend_data (sec->owner);
5766 log_file_align = bed->s->log_file_align;
5768 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5770 for (rel = relstart; rel < relend; ++rel)
5771 if (rel->r_offset >= hstart && rel->r_offset < hend)
5773 /* If the entry is in use, do nothing. */
5774 if (h->vtable_entries_used
5775 && (rel->r_offset - hstart) < h->vtable_entries_size)
5777 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5778 if (h->vtable_entries_used[entry])
5779 continue;
5781 /* Otherwise, kill it. */
5782 rel->r_offset = rel->r_info = rel->r_addend = 0;
5785 return TRUE;
5788 /* Do mark and sweep of unused sections. */
5790 bfd_boolean
5791 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5793 bfd_boolean ok = TRUE;
5794 bfd *sub;
5795 asection * (*gc_mark_hook)
5796 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5797 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
5799 if (!get_elf_backend_data (abfd)->can_gc_sections
5800 || info->relocatable
5801 || info->emitrelocations
5802 || !is_elf_hash_table (info->hash)
5803 || elf_hash_table (info)->dynamic_sections_created)
5805 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5806 return TRUE;
5809 /* Apply transitive closure to the vtable entry usage info. */
5810 elf_link_hash_traverse (elf_hash_table (info),
5811 elf_gc_propagate_vtable_entries_used,
5812 &ok);
5813 if (!ok)
5814 return FALSE;
5816 /* Kill the vtable relocations that were not used. */
5817 elf_link_hash_traverse (elf_hash_table (info),
5818 elf_gc_smash_unused_vtentry_relocs,
5819 &ok);
5820 if (!ok)
5821 return FALSE;
5823 /* Grovel through relocs to find out who stays ... */
5825 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5826 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5828 asection *o;
5830 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5831 continue;
5833 for (o = sub->sections; o != NULL; o = o->next)
5835 if (o->flags & SEC_KEEP)
5836 if (!elf_gc_mark (info, o, gc_mark_hook))
5837 return FALSE;
5841 /* ... and mark SEC_EXCLUDE for those that go. */
5842 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5843 return FALSE;
5845 return TRUE;
5848 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5850 bfd_boolean
5851 elf_gc_record_vtinherit (bfd *abfd,
5852 asection *sec,
5853 struct elf_link_hash_entry *h,
5854 bfd_vma offset)
5856 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5857 struct elf_link_hash_entry **search, *child;
5858 bfd_size_type extsymcount;
5860 /* The sh_info field of the symtab header tells us where the
5861 external symbols start. We don't care about the local symbols at
5862 this point. */
5863 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5864 if (!elf_bad_symtab (abfd))
5865 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5867 sym_hashes = elf_sym_hashes (abfd);
5868 sym_hashes_end = sym_hashes + extsymcount;
5870 /* Hunt down the child symbol, which is in this section at the same
5871 offset as the relocation. */
5872 for (search = sym_hashes; search != sym_hashes_end; ++search)
5874 if ((child = *search) != NULL
5875 && (child->root.type == bfd_link_hash_defined
5876 || child->root.type == bfd_link_hash_defweak)
5877 && child->root.u.def.section == sec
5878 && child->root.u.def.value == offset)
5879 goto win;
5882 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
5883 bfd_archive_filename (abfd), sec->name,
5884 (unsigned long) offset);
5885 bfd_set_error (bfd_error_invalid_operation);
5886 return FALSE;
5888 win:
5889 if (!h)
5891 /* This *should* only be the absolute section. It could potentially
5892 be that someone has defined a non-global vtable though, which
5893 would be bad. It isn't worth paging in the local symbols to be
5894 sure though; that case should simply be handled by the assembler. */
5896 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5898 else
5899 child->vtable_parent = h;
5901 return TRUE;
5904 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5906 bfd_boolean
5907 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5908 asection *sec ATTRIBUTE_UNUSED,
5909 struct elf_link_hash_entry *h,
5910 bfd_vma addend)
5912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5913 unsigned int log_file_align = bed->s->log_file_align;
5915 if (addend >= h->vtable_entries_size)
5917 size_t size, bytes, file_align;
5918 bfd_boolean *ptr = h->vtable_entries_used;
5920 /* While the symbol is undefined, we have to be prepared to handle
5921 a zero size. */
5922 file_align = 1 << log_file_align;
5923 if (h->root.type == bfd_link_hash_undefined)
5924 size = addend + file_align;
5925 else
5927 size = h->size;
5928 if (addend >= size)
5930 /* Oops! We've got a reference past the defined end of
5931 the table. This is probably a bug -- shall we warn? */
5932 size = addend + file_align;
5935 size = (size + file_align - 1) & -file_align;
5937 /* Allocate one extra entry for use as a "done" flag for the
5938 consolidation pass. */
5939 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
5941 if (ptr)
5943 ptr = bfd_realloc (ptr - 1, bytes);
5945 if (ptr != NULL)
5947 size_t oldbytes;
5949 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
5950 * sizeof (bfd_boolean));
5951 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
5954 else
5955 ptr = bfd_zmalloc (bytes);
5957 if (ptr == NULL)
5958 return FALSE;
5960 /* And arrange for that done flag to be at index -1. */
5961 h->vtable_entries_used = ptr + 1;
5962 h->vtable_entries_size = size;
5965 h->vtable_entries_used[addend >> log_file_align] = TRUE;
5967 return TRUE;
5970 /* And an accompanying bit to work out final got entry offsets once
5971 we're done. Should be called from final_link. */
5973 bfd_boolean
5974 elf_gc_common_finalize_got_offsets (bfd *abfd,
5975 struct bfd_link_info *info)
5977 bfd *i;
5978 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5979 bfd_vma gotoff;
5981 if (! is_elf_hash_table (info->hash))
5982 return FALSE;
5984 /* The GOT offset is relative to the .got section, but the GOT header is
5985 put into the .got.plt section, if the backend uses it. */
5986 if (bed->want_got_plt)
5987 gotoff = 0;
5988 else
5989 gotoff = bed->got_header_size;
5991 /* Do the local .got entries first. */
5992 for (i = info->input_bfds; i; i = i->link_next)
5994 bfd_signed_vma *local_got;
5995 bfd_size_type j, locsymcount;
5996 Elf_Internal_Shdr *symtab_hdr;
5998 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5999 continue;
6001 local_got = elf_local_got_refcounts (i);
6002 if (!local_got)
6003 continue;
6005 symtab_hdr = &elf_tdata (i)->symtab_hdr;
6006 if (elf_bad_symtab (i))
6007 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6008 else
6009 locsymcount = symtab_hdr->sh_info;
6011 for (j = 0; j < locsymcount; ++j)
6013 if (local_got[j] > 0)
6015 local_got[j] = gotoff;
6016 gotoff += ARCH_SIZE / 8;
6018 else
6019 local_got[j] = (bfd_vma) -1;
6023 /* Then the global .got entries. .plt refcounts are handled by
6024 adjust_dynamic_symbol */
6025 elf_link_hash_traverse (elf_hash_table (info),
6026 elf_gc_allocate_got_offsets,
6027 &gotoff);
6028 return TRUE;
6031 /* We need a special top-level link routine to convert got reference counts
6032 to real got offsets. */
6034 static bfd_boolean
6035 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
6037 bfd_vma *off = offarg;
6039 if (h->root.type == bfd_link_hash_warning)
6040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6042 if (h->got.refcount > 0)
6044 h->got.offset = off[0];
6045 off[0] += ARCH_SIZE / 8;
6047 else
6048 h->got.offset = (bfd_vma) -1;
6050 return TRUE;
6053 /* Many folk need no more in the way of final link than this, once
6054 got entry reference counting is enabled. */
6056 bfd_boolean
6057 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
6059 if (!elf_gc_common_finalize_got_offsets (abfd, info))
6060 return FALSE;
6062 /* Invoke the regular ELF backend linker to do all the work. */
6063 return elf_bfd_final_link (abfd, info);
6066 /* This function will be called though elf_link_hash_traverse to store
6067 all hash value of the exported symbols in an array. */
6069 static bfd_boolean
6070 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6072 unsigned long **valuep = data;
6073 const char *name;
6074 char *p;
6075 unsigned long ha;
6076 char *alc = NULL;
6078 if (h->root.type == bfd_link_hash_warning)
6079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6081 /* Ignore indirect symbols. These are added by the versioning code. */
6082 if (h->dynindx == -1)
6083 return TRUE;
6085 name = h->root.root.string;
6086 p = strchr (name, ELF_VER_CHR);
6087 if (p != NULL)
6089 alc = bfd_malloc (p - name + 1);
6090 memcpy (alc, name, p - name);
6091 alc[p - name] = '\0';
6092 name = alc;
6095 /* Compute the hash value. */
6096 ha = bfd_elf_hash (name);
6098 /* Store the found hash value in the array given as the argument. */
6099 *(*valuep)++ = ha;
6101 /* And store it in the struct so that we can put it in the hash table
6102 later. */
6103 h->elf_hash_value = ha;
6105 if (alc != NULL)
6106 free (alc);
6108 return TRUE;
6111 bfd_boolean
6112 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
6114 struct elf_reloc_cookie *rcookie = cookie;
6116 if (rcookie->bad_symtab)
6117 rcookie->rel = rcookie->rels;
6119 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
6121 unsigned long r_symndx;
6123 if (! rcookie->bad_symtab)
6124 if (rcookie->rel->r_offset > offset)
6125 return FALSE;
6126 if (rcookie->rel->r_offset != offset)
6127 continue;
6129 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
6130 if (r_symndx == SHN_UNDEF)
6131 return TRUE;
6133 if (r_symndx >= rcookie->locsymcount
6134 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
6136 struct elf_link_hash_entry *h;
6138 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
6140 while (h->root.type == bfd_link_hash_indirect
6141 || h->root.type == bfd_link_hash_warning)
6142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6144 if ((h->root.type == bfd_link_hash_defined
6145 || h->root.type == bfd_link_hash_defweak)
6146 && elf_discarded_section (h->root.u.def.section))
6147 return TRUE;
6148 else
6149 return FALSE;
6151 else
6153 /* It's not a relocation against a global symbol,
6154 but it could be a relocation against a local
6155 symbol for a discarded section. */
6156 asection *isec;
6157 Elf_Internal_Sym *isym;
6159 /* Need to: get the symbol; get the section. */
6160 isym = &rcookie->locsyms[r_symndx];
6161 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6163 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
6164 if (isec != NULL && elf_discarded_section (isec))
6165 return TRUE;
6168 return FALSE;
6170 return FALSE;
6173 /* Discard unneeded references to discarded sections.
6174 Returns TRUE if any section's size was changed. */
6175 /* This function assumes that the relocations are in sorted order,
6176 which is true for all known assemblers. */
6178 bfd_boolean
6179 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
6181 struct elf_reloc_cookie cookie;
6182 asection *stab, *eh;
6183 Elf_Internal_Shdr *symtab_hdr;
6184 const struct elf_backend_data *bed;
6185 bfd *abfd;
6186 unsigned int count;
6187 bfd_boolean ret = FALSE;
6189 if (info->traditional_format
6190 || !is_elf_hash_table (info->hash))
6191 return FALSE;
6193 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6195 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6196 continue;
6198 bed = get_elf_backend_data (abfd);
6200 if ((abfd->flags & DYNAMIC) != 0)
6201 continue;
6203 eh = bfd_get_section_by_name (abfd, ".eh_frame");
6204 if (info->relocatable
6205 || (eh != NULL
6206 && (eh->_raw_size == 0
6207 || bfd_is_abs_section (eh->output_section))))
6208 eh = NULL;
6210 stab = bfd_get_section_by_name (abfd, ".stab");
6211 if (stab != NULL
6212 && (stab->_raw_size == 0
6213 || bfd_is_abs_section (stab->output_section)
6214 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
6215 stab = NULL;
6217 if (stab == NULL
6218 && eh == NULL
6219 && bed->elf_backend_discard_info == NULL)
6220 continue;
6222 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6223 cookie.abfd = abfd;
6224 cookie.sym_hashes = elf_sym_hashes (abfd);
6225 cookie.bad_symtab = elf_bad_symtab (abfd);
6226 if (cookie.bad_symtab)
6228 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6229 cookie.extsymoff = 0;
6231 else
6233 cookie.locsymcount = symtab_hdr->sh_info;
6234 cookie.extsymoff = symtab_hdr->sh_info;
6237 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6238 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
6240 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6241 cookie.locsymcount, 0,
6242 NULL, NULL, NULL);
6243 if (cookie.locsyms == NULL)
6244 return FALSE;
6247 if (stab != NULL)
6249 cookie.rels = NULL;
6250 count = stab->reloc_count;
6251 if (count != 0)
6252 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6253 info->keep_memory);
6254 if (cookie.rels != NULL)
6256 cookie.rel = cookie.rels;
6257 cookie.relend = cookie.rels;
6258 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6259 if (_bfd_discard_section_stabs (abfd, stab,
6260 elf_section_data (stab)->sec_info,
6261 elf_reloc_symbol_deleted_p,
6262 &cookie))
6263 ret = TRUE;
6264 if (elf_section_data (stab)->relocs != cookie.rels)
6265 free (cookie.rels);
6269 if (eh != NULL)
6271 cookie.rels = NULL;
6272 count = eh->reloc_count;
6273 if (count != 0)
6274 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6275 info->keep_memory);
6276 cookie.rel = cookie.rels;
6277 cookie.relend = cookie.rels;
6278 if (cookie.rels != NULL)
6279 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6281 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6282 elf_reloc_symbol_deleted_p,
6283 &cookie))
6284 ret = TRUE;
6286 if (cookie.rels != NULL
6287 && elf_section_data (eh)->relocs != cookie.rels)
6288 free (cookie.rels);
6291 if (bed->elf_backend_discard_info != NULL
6292 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6293 ret = TRUE;
6295 if (cookie.locsyms != NULL
6296 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6298 if (! info->keep_memory)
6299 free (cookie.locsyms);
6300 else
6301 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6305 if (info->eh_frame_hdr
6306 && !info->relocatable
6307 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6308 ret = TRUE;
6310 return ret;
6313 static bfd_boolean
6314 elf_section_ignore_discarded_relocs (asection *sec)
6316 const struct elf_backend_data *bed;
6318 switch (sec->sec_info_type)
6320 case ELF_INFO_TYPE_STABS:
6321 case ELF_INFO_TYPE_EH_FRAME:
6322 return TRUE;
6323 default:
6324 break;
6327 bed = get_elf_backend_data (sec->owner);
6328 if (bed->elf_backend_ignore_discarded_relocs != NULL
6329 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6330 return TRUE;
6332 return FALSE;