* elf-bfd.h (is_elf_hash_table): Take hash tab rather than info arg.
[binutils.git] / bfd / elflink.h
blobb02f024bed850a6253ec51823ff44ccc1485c38e
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 static bfd_boolean elf_link_flush_output_syms
2838 (struct elf_final_link_info *);
2839 static bfd_boolean elf_link_output_extsym
2840 (struct elf_link_hash_entry *, void *);
2841 static bfd_boolean elf_link_input_bfd
2842 (struct elf_final_link_info *, bfd *);
2843 static bfd_boolean elf_reloc_link_order
2844 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
2846 /* This struct is used to pass information to elf_link_output_extsym. */
2848 struct elf_outext_info
2850 bfd_boolean failed;
2851 bfd_boolean localsyms;
2852 struct elf_final_link_info *finfo;
2855 /* When performing a relocatable link, the input relocations are
2856 preserved. But, if they reference global symbols, the indices
2857 referenced must be updated. Update all the relocations in
2858 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2860 static void
2861 elf_link_adjust_relocs (bfd *abfd,
2862 Elf_Internal_Shdr *rel_hdr,
2863 unsigned int count,
2864 struct elf_link_hash_entry **rel_hash)
2866 unsigned int i;
2867 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2868 bfd_byte *erela;
2869 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2870 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2872 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2874 swap_in = bed->s->swap_reloc_in;
2875 swap_out = bed->s->swap_reloc_out;
2877 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2879 swap_in = bed->s->swap_reloca_in;
2880 swap_out = bed->s->swap_reloca_out;
2882 else
2883 abort ();
2885 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2886 abort ();
2888 erela = rel_hdr->contents;
2889 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2891 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2892 unsigned int j;
2894 if (*rel_hash == NULL)
2895 continue;
2897 BFD_ASSERT ((*rel_hash)->indx >= 0);
2899 (*swap_in) (abfd, erela, irela);
2900 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2901 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2902 ELF_R_TYPE (irela[j].r_info));
2903 (*swap_out) (abfd, irela, erela);
2907 struct elf_link_sort_rela
2909 bfd_vma offset;
2910 enum elf_reloc_type_class type;
2911 /* We use this as an array of size int_rels_per_ext_rel. */
2912 Elf_Internal_Rela rela[1];
2915 static int
2916 elf_link_sort_cmp1 (const void *A, const void *B)
2918 const struct elf_link_sort_rela *a = A;
2919 const struct elf_link_sort_rela *b = B;
2920 int relativea, relativeb;
2922 relativea = a->type == reloc_class_relative;
2923 relativeb = b->type == reloc_class_relative;
2925 if (relativea < relativeb)
2926 return 1;
2927 if (relativea > relativeb)
2928 return -1;
2929 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2930 return -1;
2931 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2932 return 1;
2933 if (a->rela->r_offset < b->rela->r_offset)
2934 return -1;
2935 if (a->rela->r_offset > b->rela->r_offset)
2936 return 1;
2937 return 0;
2940 static int
2941 elf_link_sort_cmp2 (const void *A, const void *B)
2943 const struct elf_link_sort_rela *a = A;
2944 const struct elf_link_sort_rela *b = B;
2945 int copya, copyb;
2947 if (a->offset < b->offset)
2948 return -1;
2949 if (a->offset > b->offset)
2950 return 1;
2951 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2952 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
2953 if (copya < copyb)
2954 return -1;
2955 if (copya > copyb)
2956 return 1;
2957 if (a->rela->r_offset < b->rela->r_offset)
2958 return -1;
2959 if (a->rela->r_offset > b->rela->r_offset)
2960 return 1;
2961 return 0;
2964 static size_t
2965 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
2967 asection *reldyn;
2968 bfd_size_type count, size;
2969 size_t i, ret, sort_elt, ext_size;
2970 bfd_byte *sort, *s_non_relative, *p;
2971 struct elf_link_sort_rela *sq;
2972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2973 int i2e = bed->s->int_rels_per_ext_rel;
2974 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2975 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2976 struct bfd_link_order *lo;
2978 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2979 if (reldyn == NULL || reldyn->_raw_size == 0)
2981 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2982 if (reldyn == NULL || reldyn->_raw_size == 0)
2983 return 0;
2984 ext_size = sizeof (Elf_External_Rel);
2985 swap_in = bed->s->swap_reloc_in;
2986 swap_out = bed->s->swap_reloc_out;
2988 else
2990 ext_size = sizeof (Elf_External_Rela);
2991 swap_in = bed->s->swap_reloca_in;
2992 swap_out = bed->s->swap_reloca_out;
2994 count = reldyn->_raw_size / ext_size;
2996 size = 0;
2997 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2998 if (lo->type == bfd_indirect_link_order)
3000 asection *o = lo->u.indirect.section;
3001 size += o->_raw_size;
3004 if (size != reldyn->_raw_size)
3005 return 0;
3007 sort_elt = (sizeof (struct elf_link_sort_rela)
3008 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3009 sort = bfd_zmalloc (sort_elt * count);
3010 if (sort == NULL)
3012 (*info->callbacks->warning)
3013 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
3014 return 0;
3017 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3018 if (lo->type == bfd_indirect_link_order)
3020 bfd_byte *erel, *erelend;
3021 asection *o = lo->u.indirect.section;
3023 erel = o->contents;
3024 erelend = o->contents + o->_raw_size;
3025 p = sort + o->output_offset / ext_size * sort_elt;
3026 while (erel < erelend)
3028 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3029 (*swap_in) (abfd, erel, s->rela);
3030 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
3031 p += sort_elt;
3032 erel += ext_size;
3036 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
3038 for (i = 0, p = sort; i < count; i++, p += sort_elt)
3040 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3041 if (s->type != reloc_class_relative)
3042 break;
3044 ret = i;
3045 s_non_relative = p;
3047 sq = (struct elf_link_sort_rela *) s_non_relative;
3048 for (; i < count; i++, p += sort_elt)
3050 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
3051 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
3052 sq = sp;
3053 sp->offset = sq->rela->r_offset;
3056 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
3058 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3059 if (lo->type == bfd_indirect_link_order)
3061 bfd_byte *erel, *erelend;
3062 asection *o = lo->u.indirect.section;
3064 erel = o->contents;
3065 erelend = o->contents + o->_raw_size;
3066 p = sort + o->output_offset / ext_size * sort_elt;
3067 while (erel < erelend)
3069 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3070 (*swap_out) (abfd, s->rela, erel);
3071 p += sort_elt;
3072 erel += ext_size;
3076 free (sort);
3077 *psec = reldyn;
3078 return ret;
3081 /* Do the final step of an ELF link. */
3083 bfd_boolean
3084 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
3086 bfd_boolean dynamic;
3087 bfd_boolean emit_relocs;
3088 bfd *dynobj;
3089 struct elf_final_link_info finfo;
3090 register asection *o;
3091 register struct bfd_link_order *p;
3092 register bfd *sub;
3093 bfd_size_type max_contents_size;
3094 bfd_size_type max_external_reloc_size;
3095 bfd_size_type max_internal_reloc_count;
3096 bfd_size_type max_sym_count;
3097 bfd_size_type max_sym_shndx_count;
3098 file_ptr off;
3099 Elf_Internal_Sym elfsym;
3100 unsigned int i;
3101 Elf_Internal_Shdr *symtab_hdr;
3102 Elf_Internal_Shdr *symtab_shndx_hdr;
3103 Elf_Internal_Shdr *symstrtab_hdr;
3104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3105 struct elf_outext_info eoinfo;
3106 bfd_boolean merged;
3107 size_t relativecount = 0;
3108 asection *reldyn = 0;
3109 bfd_size_type amt;
3111 if (! is_elf_hash_table (info->hash))
3112 return FALSE;
3114 if (info->shared)
3115 abfd->flags |= DYNAMIC;
3117 dynamic = elf_hash_table (info)->dynamic_sections_created;
3118 dynobj = elf_hash_table (info)->dynobj;
3120 emit_relocs = (info->relocatable
3121 || info->emitrelocations
3122 || bed->elf_backend_emit_relocs);
3124 finfo.info = info;
3125 finfo.output_bfd = abfd;
3126 finfo.symstrtab = elf_stringtab_init ();
3127 if (finfo.symstrtab == NULL)
3128 return FALSE;
3130 if (! dynamic)
3132 finfo.dynsym_sec = NULL;
3133 finfo.hash_sec = NULL;
3134 finfo.symver_sec = NULL;
3136 else
3138 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3139 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3140 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3141 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3142 /* Note that it is OK if symver_sec is NULL. */
3145 finfo.contents = NULL;
3146 finfo.external_relocs = NULL;
3147 finfo.internal_relocs = NULL;
3148 finfo.external_syms = NULL;
3149 finfo.locsym_shndx = NULL;
3150 finfo.internal_syms = NULL;
3151 finfo.indices = NULL;
3152 finfo.sections = NULL;
3153 finfo.symbuf = NULL;
3154 finfo.symshndxbuf = NULL;
3155 finfo.symbuf_count = 0;
3156 finfo.shndxbuf_size = 0;
3158 /* Count up the number of relocations we will output for each output
3159 section, so that we know the sizes of the reloc sections. We
3160 also figure out some maximum sizes. */
3161 max_contents_size = 0;
3162 max_external_reloc_size = 0;
3163 max_internal_reloc_count = 0;
3164 max_sym_count = 0;
3165 max_sym_shndx_count = 0;
3166 merged = FALSE;
3167 for (o = abfd->sections; o != NULL; o = o->next)
3169 struct bfd_elf_section_data *esdo = elf_section_data (o);
3170 o->reloc_count = 0;
3172 for (p = o->link_order_head; p != NULL; p = p->next)
3174 unsigned int reloc_count = 0;
3175 struct bfd_elf_section_data *esdi = NULL;
3176 unsigned int *rel_count1;
3178 if (p->type == bfd_section_reloc_link_order
3179 || p->type == bfd_symbol_reloc_link_order)
3180 reloc_count = 1;
3181 else if (p->type == bfd_indirect_link_order)
3183 asection *sec;
3185 sec = p->u.indirect.section;
3186 esdi = elf_section_data (sec);
3188 /* Mark all sections which are to be included in the
3189 link. This will normally be every section. We need
3190 to do this so that we can identify any sections which
3191 the linker has decided to not include. */
3192 sec->linker_mark = TRUE;
3194 if (sec->flags & SEC_MERGE)
3195 merged = TRUE;
3197 if (info->relocatable || info->emitrelocations)
3198 reloc_count = sec->reloc_count;
3199 else if (bed->elf_backend_count_relocs)
3201 Elf_Internal_Rela * relocs;
3203 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3204 info->keep_memory);
3206 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
3208 if (elf_section_data (o)->relocs != relocs)
3209 free (relocs);
3212 if (sec->_raw_size > max_contents_size)
3213 max_contents_size = sec->_raw_size;
3214 if (sec->_cooked_size > max_contents_size)
3215 max_contents_size = sec->_cooked_size;
3217 /* We are interested in just local symbols, not all
3218 symbols. */
3219 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3220 && (sec->owner->flags & DYNAMIC) == 0)
3222 size_t sym_count;
3224 if (elf_bad_symtab (sec->owner))
3225 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3226 / sizeof (Elf_External_Sym));
3227 else
3228 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3230 if (sym_count > max_sym_count)
3231 max_sym_count = sym_count;
3233 if (sym_count > max_sym_shndx_count
3234 && elf_symtab_shndx (sec->owner) != 0)
3235 max_sym_shndx_count = sym_count;
3237 if ((sec->flags & SEC_RELOC) != 0)
3239 size_t ext_size;
3241 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3242 if (ext_size > max_external_reloc_size)
3243 max_external_reloc_size = ext_size;
3244 if (sec->reloc_count > max_internal_reloc_count)
3245 max_internal_reloc_count = sec->reloc_count;
3250 if (reloc_count == 0)
3251 continue;
3253 o->reloc_count += reloc_count;
3255 /* MIPS may have a mix of REL and RELA relocs on sections.
3256 To support this curious ABI we keep reloc counts in
3257 elf_section_data too. We must be careful to add the
3258 relocations from the input section to the right output
3259 count. FIXME: Get rid of one count. We have
3260 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3261 rel_count1 = &esdo->rel_count;
3262 if (esdi != NULL)
3264 bfd_boolean same_size;
3265 bfd_size_type entsize1;
3267 entsize1 = esdi->rel_hdr.sh_entsize;
3268 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3269 || entsize1 == sizeof (Elf_External_Rela));
3270 same_size = (!o->use_rela_p
3271 == (entsize1 == sizeof (Elf_External_Rel)));
3273 if (!same_size)
3274 rel_count1 = &esdo->rel_count2;
3276 if (esdi->rel_hdr2 != NULL)
3278 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3279 unsigned int alt_count;
3280 unsigned int *rel_count2;
3282 BFD_ASSERT (entsize2 != entsize1
3283 && (entsize2 == sizeof (Elf_External_Rel)
3284 || entsize2 == sizeof (Elf_External_Rela)));
3286 rel_count2 = &esdo->rel_count2;
3287 if (!same_size)
3288 rel_count2 = &esdo->rel_count;
3290 /* The following is probably too simplistic if the
3291 backend counts output relocs unusually. */
3292 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3293 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3294 *rel_count2 += alt_count;
3295 reloc_count -= alt_count;
3298 *rel_count1 += reloc_count;
3301 if (o->reloc_count > 0)
3302 o->flags |= SEC_RELOC;
3303 else
3305 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3306 set it (this is probably a bug) and if it is set
3307 assign_section_numbers will create a reloc section. */
3308 o->flags &=~ SEC_RELOC;
3311 /* If the SEC_ALLOC flag is not set, force the section VMA to
3312 zero. This is done in elf_fake_sections as well, but forcing
3313 the VMA to 0 here will ensure that relocs against these
3314 sections are handled correctly. */
3315 if ((o->flags & SEC_ALLOC) == 0
3316 && ! o->user_set_vma)
3317 o->vma = 0;
3320 if (! info->relocatable && merged)
3321 elf_link_hash_traverse (elf_hash_table (info),
3322 _bfd_elf_link_sec_merge_syms, abfd);
3324 /* Figure out the file positions for everything but the symbol table
3325 and the relocs. We set symcount to force assign_section_numbers
3326 to create a symbol table. */
3327 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3328 BFD_ASSERT (! abfd->output_has_begun);
3329 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3330 goto error_return;
3332 /* That created the reloc sections. Set their sizes, and assign
3333 them file positions, and allocate some buffers. */
3334 for (o = abfd->sections; o != NULL; o = o->next)
3336 if ((o->flags & SEC_RELOC) != 0)
3338 if (!(_bfd_elf_link_size_reloc_section
3339 (abfd, &elf_section_data (o)->rel_hdr, o)))
3340 goto error_return;
3342 if (elf_section_data (o)->rel_hdr2
3343 && !(_bfd_elf_link_size_reloc_section
3344 (abfd, elf_section_data (o)->rel_hdr2, o)))
3345 goto error_return;
3348 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3349 to count upwards while actually outputting the relocations. */
3350 elf_section_data (o)->rel_count = 0;
3351 elf_section_data (o)->rel_count2 = 0;
3354 _bfd_elf_assign_file_positions_for_relocs (abfd);
3356 /* We have now assigned file positions for all the sections except
3357 .symtab and .strtab. We start the .symtab section at the current
3358 file position, and write directly to it. We build the .strtab
3359 section in memory. */
3360 bfd_get_symcount (abfd) = 0;
3361 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3362 /* sh_name is set in prep_headers. */
3363 symtab_hdr->sh_type = SHT_SYMTAB;
3364 /* sh_flags, sh_addr and sh_size all start off zero. */
3365 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3366 /* sh_link is set in assign_section_numbers. */
3367 /* sh_info is set below. */
3368 /* sh_offset is set just below. */
3369 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3371 off = elf_tdata (abfd)->next_file_pos;
3372 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3374 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3375 incorrect. We do not yet know the size of the .symtab section.
3376 We correct next_file_pos below, after we do know the size. */
3378 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3379 continuously seeking to the right position in the file. */
3380 if (! info->keep_memory || max_sym_count < 20)
3381 finfo.symbuf_size = 20;
3382 else
3383 finfo.symbuf_size = max_sym_count;
3384 amt = finfo.symbuf_size;
3385 amt *= sizeof (Elf_External_Sym);
3386 finfo.symbuf = bfd_malloc (amt);
3387 if (finfo.symbuf == NULL)
3388 goto error_return;
3389 if (elf_numsections (abfd) > SHN_LORESERVE)
3391 /* Wild guess at number of output symbols. realloc'd as needed. */
3392 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3393 finfo.shndxbuf_size = amt;
3394 amt *= sizeof (Elf_External_Sym_Shndx);
3395 finfo.symshndxbuf = bfd_zmalloc (amt);
3396 if (finfo.symshndxbuf == NULL)
3397 goto error_return;
3400 /* Start writing out the symbol table. The first symbol is always a
3401 dummy symbol. */
3402 if (info->strip != strip_all
3403 || emit_relocs)
3405 elfsym.st_value = 0;
3406 elfsym.st_size = 0;
3407 elfsym.st_info = 0;
3408 elfsym.st_other = 0;
3409 elfsym.st_shndx = SHN_UNDEF;
3410 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr))
3411 goto error_return;
3414 #if 0
3415 /* Some standard ELF linkers do this, but we don't because it causes
3416 bootstrap comparison failures. */
3417 /* Output a file symbol for the output file as the second symbol.
3418 We output this even if we are discarding local symbols, although
3419 I'm not sure if this is correct. */
3420 elfsym.st_value = 0;
3421 elfsym.st_size = 0;
3422 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3423 elfsym.st_other = 0;
3424 elfsym.st_shndx = SHN_ABS;
3425 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3426 &elfsym, bfd_abs_section_ptr))
3427 goto error_return;
3428 #endif
3430 /* Output a symbol for each section. We output these even if we are
3431 discarding local symbols, since they are used for relocs. These
3432 symbols have no names. We store the index of each one in the
3433 index field of the section, so that we can find it again when
3434 outputting relocs. */
3435 if (info->strip != strip_all
3436 || emit_relocs)
3438 elfsym.st_size = 0;
3439 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3440 elfsym.st_other = 0;
3441 for (i = 1; i < elf_numsections (abfd); i++)
3443 o = section_from_elf_index (abfd, i);
3444 if (o != NULL)
3445 o->target_index = bfd_get_symcount (abfd);
3446 elfsym.st_shndx = i;
3447 if (info->relocatable || o == NULL)
3448 elfsym.st_value = 0;
3449 else
3450 elfsym.st_value = o->vma;
3451 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o))
3452 goto error_return;
3453 if (i == SHN_LORESERVE - 1)
3454 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3458 /* Allocate some memory to hold information read in from the input
3459 files. */
3460 if (max_contents_size != 0)
3462 finfo.contents = bfd_malloc (max_contents_size);
3463 if (finfo.contents == NULL)
3464 goto error_return;
3467 if (max_external_reloc_size != 0)
3469 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
3470 if (finfo.external_relocs == NULL)
3471 goto error_return;
3474 if (max_internal_reloc_count != 0)
3476 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3477 amt *= sizeof (Elf_Internal_Rela);
3478 finfo.internal_relocs = bfd_malloc (amt);
3479 if (finfo.internal_relocs == NULL)
3480 goto error_return;
3483 if (max_sym_count != 0)
3485 amt = max_sym_count * sizeof (Elf_External_Sym);
3486 finfo.external_syms = bfd_malloc (amt);
3487 if (finfo.external_syms == NULL)
3488 goto error_return;
3490 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3491 finfo.internal_syms = bfd_malloc (amt);
3492 if (finfo.internal_syms == NULL)
3493 goto error_return;
3495 amt = max_sym_count * sizeof (long);
3496 finfo.indices = bfd_malloc (amt);
3497 if (finfo.indices == NULL)
3498 goto error_return;
3500 amt = max_sym_count * sizeof (asection *);
3501 finfo.sections = bfd_malloc (amt);
3502 if (finfo.sections == NULL)
3503 goto error_return;
3506 if (max_sym_shndx_count != 0)
3508 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3509 finfo.locsym_shndx = bfd_malloc (amt);
3510 if (finfo.locsym_shndx == NULL)
3511 goto error_return;
3514 if (elf_hash_table (info)->tls_sec)
3516 bfd_vma base, end = 0;
3517 asection *sec;
3519 for (sec = elf_hash_table (info)->tls_sec;
3520 sec && (sec->flags & SEC_THREAD_LOCAL);
3521 sec = sec->next)
3523 bfd_vma size = sec->_raw_size;
3525 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3527 struct bfd_link_order *o;
3529 for (o = sec->link_order_head; o != NULL; o = o->next)
3530 if (size < o->offset + o->size)
3531 size = o->offset + o->size;
3533 end = sec->vma + size;
3535 base = elf_hash_table (info)->tls_sec->vma;
3536 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3537 elf_hash_table (info)->tls_size = end - base;
3540 /* Since ELF permits relocations to be against local symbols, we
3541 must have the local symbols available when we do the relocations.
3542 Since we would rather only read the local symbols once, and we
3543 would rather not keep them in memory, we handle all the
3544 relocations for a single input file at the same time.
3546 Unfortunately, there is no way to know the total number of local
3547 symbols until we have seen all of them, and the local symbol
3548 indices precede the global symbol indices. This means that when
3549 we are generating relocatable output, and we see a reloc against
3550 a global symbol, we can not know the symbol index until we have
3551 finished examining all the local symbols to see which ones we are
3552 going to output. To deal with this, we keep the relocations in
3553 memory, and don't output them until the end of the link. This is
3554 an unfortunate waste of memory, but I don't see a good way around
3555 it. Fortunately, it only happens when performing a relocatable
3556 link, which is not the common case. FIXME: If keep_memory is set
3557 we could write the relocs out and then read them again; I don't
3558 know how bad the memory loss will be. */
3560 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3561 sub->output_has_begun = FALSE;
3562 for (o = abfd->sections; o != NULL; o = o->next)
3564 for (p = o->link_order_head; p != NULL; p = p->next)
3566 if (p->type == bfd_indirect_link_order
3567 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3568 == bfd_target_elf_flavour)
3569 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3571 if (! sub->output_has_begun)
3573 if (! elf_link_input_bfd (&finfo, sub))
3574 goto error_return;
3575 sub->output_has_begun = TRUE;
3578 else if (p->type == bfd_section_reloc_link_order
3579 || p->type == bfd_symbol_reloc_link_order)
3581 if (! elf_reloc_link_order (abfd, info, o, p))
3582 goto error_return;
3584 else
3586 if (! _bfd_default_link_order (abfd, info, o, p))
3587 goto error_return;
3592 /* Output any global symbols that got converted to local in a
3593 version script or due to symbol visibility. We do this in a
3594 separate step since ELF requires all local symbols to appear
3595 prior to any global symbols. FIXME: We should only do this if
3596 some global symbols were, in fact, converted to become local.
3597 FIXME: Will this work correctly with the Irix 5 linker? */
3598 eoinfo.failed = FALSE;
3599 eoinfo.finfo = &finfo;
3600 eoinfo.localsyms = TRUE;
3601 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3602 &eoinfo);
3603 if (eoinfo.failed)
3604 return FALSE;
3606 /* That wrote out all the local symbols. Finish up the symbol table
3607 with the global symbols. Even if we want to strip everything we
3608 can, we still need to deal with those global symbols that got
3609 converted to local in a version script. */
3611 /* The sh_info field records the index of the first non local symbol. */
3612 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3614 if (dynamic
3615 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3617 Elf_Internal_Sym sym;
3618 Elf_External_Sym *dynsym =
3619 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3620 long last_local = 0;
3622 /* Write out the section symbols for the output sections. */
3623 if (info->shared)
3625 asection *s;
3627 sym.st_size = 0;
3628 sym.st_name = 0;
3629 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3630 sym.st_other = 0;
3632 for (s = abfd->sections; s != NULL; s = s->next)
3634 int indx;
3635 Elf_External_Sym *dest;
3637 indx = elf_section_data (s)->this_idx;
3638 BFD_ASSERT (indx > 0);
3639 sym.st_shndx = indx;
3640 sym.st_value = s->vma;
3641 dest = dynsym + elf_section_data (s)->dynindx;
3642 elf_swap_symbol_out (abfd, &sym, dest, 0);
3645 last_local = bfd_count_sections (abfd);
3648 /* Write out the local dynsyms. */
3649 if (elf_hash_table (info)->dynlocal)
3651 struct elf_link_local_dynamic_entry *e;
3652 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3654 asection *s;
3655 Elf_External_Sym *dest;
3657 sym.st_size = e->isym.st_size;
3658 sym.st_other = e->isym.st_other;
3660 /* Copy the internal symbol as is.
3661 Note that we saved a word of storage and overwrote
3662 the original st_name with the dynstr_index. */
3663 sym = e->isym;
3665 if (e->isym.st_shndx != SHN_UNDEF
3666 && (e->isym.st_shndx < SHN_LORESERVE
3667 || e->isym.st_shndx > SHN_HIRESERVE))
3669 s = bfd_section_from_elf_index (e->input_bfd,
3670 e->isym.st_shndx);
3672 sym.st_shndx =
3673 elf_section_data (s->output_section)->this_idx;
3674 sym.st_value = (s->output_section->vma
3675 + s->output_offset
3676 + e->isym.st_value);
3679 if (last_local < e->dynindx)
3680 last_local = e->dynindx;
3682 dest = dynsym + e->dynindx;
3683 elf_swap_symbol_out (abfd, &sym, dest, 0);
3687 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3688 last_local + 1;
3691 /* We get the global symbols from the hash table. */
3692 eoinfo.failed = FALSE;
3693 eoinfo.localsyms = FALSE;
3694 eoinfo.finfo = &finfo;
3695 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3696 &eoinfo);
3697 if (eoinfo.failed)
3698 return FALSE;
3700 /* If backend needs to output some symbols not present in the hash
3701 table, do it now. */
3702 if (bed->elf_backend_output_arch_syms)
3704 typedef bfd_boolean (*out_sym_func)
3705 (void *, const char *, Elf_Internal_Sym *, asection *);
3707 if (! ((*bed->elf_backend_output_arch_syms)
3708 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
3709 return FALSE;
3712 /* Flush all symbols to the file. */
3713 if (! elf_link_flush_output_syms (&finfo))
3714 return FALSE;
3716 /* Now we know the size of the symtab section. */
3717 off += symtab_hdr->sh_size;
3719 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3720 if (symtab_shndx_hdr->sh_name != 0)
3722 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3723 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3724 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3725 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3726 symtab_shndx_hdr->sh_size = amt;
3728 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3729 off, TRUE);
3731 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3732 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
3733 return FALSE;
3737 /* Finish up and write out the symbol string table (.strtab)
3738 section. */
3739 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3740 /* sh_name was set in prep_headers. */
3741 symstrtab_hdr->sh_type = SHT_STRTAB;
3742 symstrtab_hdr->sh_flags = 0;
3743 symstrtab_hdr->sh_addr = 0;
3744 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3745 symstrtab_hdr->sh_entsize = 0;
3746 symstrtab_hdr->sh_link = 0;
3747 symstrtab_hdr->sh_info = 0;
3748 /* sh_offset is set just below. */
3749 symstrtab_hdr->sh_addralign = 1;
3751 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3752 elf_tdata (abfd)->next_file_pos = off;
3754 if (bfd_get_symcount (abfd) > 0)
3756 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3757 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3758 return FALSE;
3761 /* Adjust the relocs to have the correct symbol indices. */
3762 for (o = abfd->sections; o != NULL; o = o->next)
3764 if ((o->flags & SEC_RELOC) == 0)
3765 continue;
3767 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3768 elf_section_data (o)->rel_count,
3769 elf_section_data (o)->rel_hashes);
3770 if (elf_section_data (o)->rel_hdr2 != NULL)
3771 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3772 elf_section_data (o)->rel_count2,
3773 (elf_section_data (o)->rel_hashes
3774 + elf_section_data (o)->rel_count));
3776 /* Set the reloc_count field to 0 to prevent write_relocs from
3777 trying to swap the relocs out itself. */
3778 o->reloc_count = 0;
3781 if (dynamic && info->combreloc && dynobj != NULL)
3782 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3784 /* If we are linking against a dynamic object, or generating a
3785 shared library, finish up the dynamic linking information. */
3786 if (dynamic)
3788 Elf_External_Dyn *dyncon, *dynconend;
3790 /* Fix up .dynamic entries. */
3791 o = bfd_get_section_by_name (dynobj, ".dynamic");
3792 BFD_ASSERT (o != NULL);
3794 dyncon = (Elf_External_Dyn *) o->contents;
3795 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3796 for (; dyncon < dynconend; dyncon++)
3798 Elf_Internal_Dyn dyn;
3799 const char *name;
3800 unsigned int type;
3802 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3804 switch (dyn.d_tag)
3806 default:
3807 break;
3808 case DT_NULL:
3809 if (relativecount > 0 && dyncon + 1 < dynconend)
3811 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3813 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3814 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3815 default: break;
3817 if (dyn.d_tag != DT_NULL)
3819 dyn.d_un.d_val = relativecount;
3820 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3821 relativecount = 0;
3824 break;
3825 case DT_INIT:
3826 name = info->init_function;
3827 goto get_sym;
3828 case DT_FINI:
3829 name = info->fini_function;
3830 get_sym:
3832 struct elf_link_hash_entry *h;
3834 h = elf_link_hash_lookup (elf_hash_table (info), name,
3835 FALSE, FALSE, TRUE);
3836 if (h != NULL
3837 && (h->root.type == bfd_link_hash_defined
3838 || h->root.type == bfd_link_hash_defweak))
3840 dyn.d_un.d_val = h->root.u.def.value;
3841 o = h->root.u.def.section;
3842 if (o->output_section != NULL)
3843 dyn.d_un.d_val += (o->output_section->vma
3844 + o->output_offset);
3845 else
3847 /* The symbol is imported from another shared
3848 library and does not apply to this one. */
3849 dyn.d_un.d_val = 0;
3852 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3855 break;
3857 case DT_PREINIT_ARRAYSZ:
3858 name = ".preinit_array";
3859 goto get_size;
3860 case DT_INIT_ARRAYSZ:
3861 name = ".init_array";
3862 goto get_size;
3863 case DT_FINI_ARRAYSZ:
3864 name = ".fini_array";
3865 get_size:
3866 o = bfd_get_section_by_name (abfd, name);
3867 if (o == NULL)
3869 (*_bfd_error_handler)
3870 (_("%s: could not find output section %s"),
3871 bfd_get_filename (abfd), name);
3872 goto error_return;
3874 if (o->_raw_size == 0)
3875 (*_bfd_error_handler)
3876 (_("warning: %s section has zero size"), name);
3877 dyn.d_un.d_val = o->_raw_size;
3878 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3879 break;
3881 case DT_PREINIT_ARRAY:
3882 name = ".preinit_array";
3883 goto get_vma;
3884 case DT_INIT_ARRAY:
3885 name = ".init_array";
3886 goto get_vma;
3887 case DT_FINI_ARRAY:
3888 name = ".fini_array";
3889 goto get_vma;
3891 case DT_HASH:
3892 name = ".hash";
3893 goto get_vma;
3894 case DT_STRTAB:
3895 name = ".dynstr";
3896 goto get_vma;
3897 case DT_SYMTAB:
3898 name = ".dynsym";
3899 goto get_vma;
3900 case DT_VERDEF:
3901 name = ".gnu.version_d";
3902 goto get_vma;
3903 case DT_VERNEED:
3904 name = ".gnu.version_r";
3905 goto get_vma;
3906 case DT_VERSYM:
3907 name = ".gnu.version";
3908 get_vma:
3909 o = bfd_get_section_by_name (abfd, name);
3910 if (o == NULL)
3912 (*_bfd_error_handler)
3913 (_("%s: could not find output section %s"),
3914 bfd_get_filename (abfd), name);
3915 goto error_return;
3917 dyn.d_un.d_ptr = o->vma;
3918 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3919 break;
3921 case DT_REL:
3922 case DT_RELA:
3923 case DT_RELSZ:
3924 case DT_RELASZ:
3925 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3926 type = SHT_REL;
3927 else
3928 type = SHT_RELA;
3929 dyn.d_un.d_val = 0;
3930 for (i = 1; i < elf_numsections (abfd); i++)
3932 Elf_Internal_Shdr *hdr;
3934 hdr = elf_elfsections (abfd)[i];
3935 if (hdr->sh_type == type
3936 && (hdr->sh_flags & SHF_ALLOC) != 0)
3938 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3939 dyn.d_un.d_val += hdr->sh_size;
3940 else
3942 if (dyn.d_un.d_val == 0
3943 || hdr->sh_addr < dyn.d_un.d_val)
3944 dyn.d_un.d_val = hdr->sh_addr;
3948 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3949 break;
3954 /* If we have created any dynamic sections, then output them. */
3955 if (dynobj != NULL)
3957 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3958 goto error_return;
3960 for (o = dynobj->sections; o != NULL; o = o->next)
3962 if ((o->flags & SEC_HAS_CONTENTS) == 0
3963 || o->_raw_size == 0
3964 || o->output_section == bfd_abs_section_ptr)
3965 continue;
3966 if ((o->flags & SEC_LINKER_CREATED) == 0)
3968 /* At this point, we are only interested in sections
3969 created by _bfd_elf_link_create_dynamic_sections. */
3970 continue;
3972 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3973 != SHT_STRTAB)
3974 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3976 if (! bfd_set_section_contents (abfd, o->output_section,
3977 o->contents,
3978 (file_ptr) o->output_offset,
3979 o->_raw_size))
3980 goto error_return;
3982 else
3984 /* The contents of the .dynstr section are actually in a
3985 stringtab. */
3986 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3987 if (bfd_seek (abfd, off, SEEK_SET) != 0
3988 || ! _bfd_elf_strtab_emit (abfd,
3989 elf_hash_table (info)->dynstr))
3990 goto error_return;
3995 if (info->relocatable)
3997 bfd_boolean failed = FALSE;
3999 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4000 if (failed)
4001 goto error_return;
4004 /* If we have optimized stabs strings, output them. */
4005 if (elf_hash_table (info)->stab_info != NULL)
4007 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4008 goto error_return;
4011 if (info->eh_frame_hdr)
4013 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
4014 goto error_return;
4017 if (finfo.symstrtab != NULL)
4018 _bfd_stringtab_free (finfo.symstrtab);
4019 if (finfo.contents != NULL)
4020 free (finfo.contents);
4021 if (finfo.external_relocs != NULL)
4022 free (finfo.external_relocs);
4023 if (finfo.internal_relocs != NULL)
4024 free (finfo.internal_relocs);
4025 if (finfo.external_syms != NULL)
4026 free (finfo.external_syms);
4027 if (finfo.locsym_shndx != NULL)
4028 free (finfo.locsym_shndx);
4029 if (finfo.internal_syms != NULL)
4030 free (finfo.internal_syms);
4031 if (finfo.indices != NULL)
4032 free (finfo.indices);
4033 if (finfo.sections != NULL)
4034 free (finfo.sections);
4035 if (finfo.symbuf != NULL)
4036 free (finfo.symbuf);
4037 if (finfo.symshndxbuf != NULL)
4038 free (finfo.symshndxbuf);
4039 for (o = abfd->sections; o != NULL; o = o->next)
4041 if ((o->flags & SEC_RELOC) != 0
4042 && elf_section_data (o)->rel_hashes != NULL)
4043 free (elf_section_data (o)->rel_hashes);
4046 elf_tdata (abfd)->linker = TRUE;
4048 return TRUE;
4050 error_return:
4051 if (finfo.symstrtab != NULL)
4052 _bfd_stringtab_free (finfo.symstrtab);
4053 if (finfo.contents != NULL)
4054 free (finfo.contents);
4055 if (finfo.external_relocs != NULL)
4056 free (finfo.external_relocs);
4057 if (finfo.internal_relocs != NULL)
4058 free (finfo.internal_relocs);
4059 if (finfo.external_syms != NULL)
4060 free (finfo.external_syms);
4061 if (finfo.locsym_shndx != NULL)
4062 free (finfo.locsym_shndx);
4063 if (finfo.internal_syms != NULL)
4064 free (finfo.internal_syms);
4065 if (finfo.indices != NULL)
4066 free (finfo.indices);
4067 if (finfo.sections != NULL)
4068 free (finfo.sections);
4069 if (finfo.symbuf != NULL)
4070 free (finfo.symbuf);
4071 if (finfo.symshndxbuf != NULL)
4072 free (finfo.symshndxbuf);
4073 for (o = abfd->sections; o != NULL; o = o->next)
4075 if ((o->flags & SEC_RELOC) != 0
4076 && elf_section_data (o)->rel_hashes != NULL)
4077 free (elf_section_data (o)->rel_hashes);
4080 return FALSE;
4083 /* Add a symbol to the output symbol table. */
4085 static bfd_boolean
4086 elf_link_output_sym (struct elf_final_link_info *finfo,
4087 const char *name,
4088 Elf_Internal_Sym *elfsym,
4089 asection *input_sec)
4091 Elf_External_Sym *dest;
4092 Elf_External_Sym_Shndx *destshndx;
4093 bfd_boolean (*output_symbol_hook)
4094 (bfd *, struct bfd_link_info *info, const char *,
4095 Elf_Internal_Sym *, asection *);
4097 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4098 elf_backend_link_output_symbol_hook;
4099 if (output_symbol_hook != NULL)
4101 if (! ((*output_symbol_hook)
4102 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
4103 return FALSE;
4106 if (name == NULL || *name == '\0')
4107 elfsym->st_name = 0;
4108 else if (input_sec->flags & SEC_EXCLUDE)
4109 elfsym->st_name = 0;
4110 else
4112 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
4113 name, TRUE, FALSE);
4114 if (elfsym->st_name == (unsigned long) -1)
4115 return FALSE;
4118 if (finfo->symbuf_count >= finfo->symbuf_size)
4120 if (! elf_link_flush_output_syms (finfo))
4121 return FALSE;
4124 dest = finfo->symbuf + finfo->symbuf_count;
4125 destshndx = finfo->symshndxbuf;
4126 if (destshndx != NULL)
4128 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
4130 bfd_size_type amt;
4132 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
4133 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
4134 if (destshndx == NULL)
4135 return FALSE;
4136 memset ((char *) destshndx + amt, 0, amt);
4137 finfo->shndxbuf_size *= 2;
4139 destshndx += bfd_get_symcount (finfo->output_bfd);
4142 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
4143 finfo->symbuf_count += 1;
4144 bfd_get_symcount (finfo->output_bfd) += 1;
4146 return TRUE;
4149 /* Flush the output symbols to the file. */
4151 static bfd_boolean
4152 elf_link_flush_output_syms (struct elf_final_link_info *finfo)
4154 if (finfo->symbuf_count > 0)
4156 Elf_Internal_Shdr *hdr;
4157 file_ptr pos;
4158 bfd_size_type amt;
4160 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4161 pos = hdr->sh_offset + hdr->sh_size;
4162 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
4163 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
4164 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
4165 return FALSE;
4167 hdr->sh_size += amt;
4168 finfo->symbuf_count = 0;
4171 return TRUE;
4174 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4175 allowing an unsatisfied unversioned symbol in the DSO to match a
4176 versioned symbol that would normally require an explicit version.
4177 We also handle the case that a DSO references a hidden symbol
4178 which may be satisfied by a versioned symbol in another DSO. */
4180 static bfd_boolean
4181 elf_link_check_versioned_symbol (struct bfd_link_info *info,
4182 struct elf_link_hash_entry *h)
4184 bfd *abfd;
4185 struct elf_link_loaded_list *loaded;
4187 if (!is_elf_hash_table (info->hash))
4188 return FALSE;
4190 switch (h->root.type)
4192 default:
4193 abfd = NULL;
4194 break;
4196 case bfd_link_hash_undefined:
4197 case bfd_link_hash_undefweak:
4198 abfd = h->root.u.undef.abfd;
4199 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
4200 return FALSE;
4201 break;
4203 case bfd_link_hash_defined:
4204 case bfd_link_hash_defweak:
4205 abfd = h->root.u.def.section->owner;
4206 break;
4208 case bfd_link_hash_common:
4209 abfd = h->root.u.c.p->section->owner;
4210 break;
4212 BFD_ASSERT (abfd != NULL);
4214 for (loaded = elf_hash_table (info)->loaded;
4215 loaded != NULL;
4216 loaded = loaded->next)
4218 bfd *input;
4219 Elf_Internal_Shdr *hdr;
4220 bfd_size_type symcount;
4221 bfd_size_type extsymcount;
4222 bfd_size_type extsymoff;
4223 Elf_Internal_Shdr *versymhdr;
4224 Elf_Internal_Sym *isym;
4225 Elf_Internal_Sym *isymend;
4226 Elf_Internal_Sym *isymbuf;
4227 Elf_External_Versym *ever;
4228 Elf_External_Versym *extversym;
4230 input = loaded->abfd;
4232 /* We check each DSO for a possible hidden versioned definition. */
4233 if (input == abfd
4234 || (input->flags & DYNAMIC) == 0
4235 || elf_dynversym (input) == 0)
4236 continue;
4238 hdr = &elf_tdata (input)->dynsymtab_hdr;
4240 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4241 if (elf_bad_symtab (input))
4243 extsymcount = symcount;
4244 extsymoff = 0;
4246 else
4248 extsymcount = symcount - hdr->sh_info;
4249 extsymoff = hdr->sh_info;
4252 if (extsymcount == 0)
4253 continue;
4255 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4256 NULL, NULL, NULL);
4257 if (isymbuf == NULL)
4258 return FALSE;
4260 /* Read in any version definitions. */
4261 versymhdr = &elf_tdata (input)->dynversym_hdr;
4262 extversym = bfd_malloc (versymhdr->sh_size);
4263 if (extversym == NULL)
4264 goto error_ret;
4266 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4267 || (bfd_bread (extversym, versymhdr->sh_size, input)
4268 != versymhdr->sh_size))
4270 free (extversym);
4271 error_ret:
4272 free (isymbuf);
4273 return FALSE;
4276 ever = extversym + extsymoff;
4277 isymend = isymbuf + extsymcount;
4278 for (isym = isymbuf; isym < isymend; isym++, ever++)
4280 const char *name;
4281 Elf_Internal_Versym iver;
4282 unsigned short version_index;
4284 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4285 || isym->st_shndx == SHN_UNDEF)
4286 continue;
4288 name = bfd_elf_string_from_elf_section (input,
4289 hdr->sh_link,
4290 isym->st_name);
4291 if (strcmp (name, h->root.root.string) != 0)
4292 continue;
4294 _bfd_elf_swap_versym_in (input, ever, &iver);
4296 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4298 /* If we have a non-hidden versioned sym, then it should
4299 have provided a definition for the undefined sym. */
4300 abort ();
4303 version_index = iver.vs_vers & VERSYM_VERSION;
4304 if (version_index == 1 || version_index == 2)
4306 /* This is the base or first version. We can use it. */
4307 free (extversym);
4308 free (isymbuf);
4309 return TRUE;
4313 free (extversym);
4314 free (isymbuf);
4317 return FALSE;
4320 /* Add an external symbol to the symbol table. This is called from
4321 the hash table traversal routine. When generating a shared object,
4322 we go through the symbol table twice. The first time we output
4323 anything that might have been forced to local scope in a version
4324 script. The second time we output the symbols that are still
4325 global symbols. */
4327 static bfd_boolean
4328 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
4330 struct elf_outext_info *eoinfo = data;
4331 struct elf_final_link_info *finfo = eoinfo->finfo;
4332 bfd_boolean strip;
4333 Elf_Internal_Sym sym;
4334 asection *input_sec;
4336 if (h->root.type == bfd_link_hash_warning)
4338 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4339 if (h->root.type == bfd_link_hash_new)
4340 return TRUE;
4343 /* Decide whether to output this symbol in this pass. */
4344 if (eoinfo->localsyms)
4346 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4347 return TRUE;
4349 else
4351 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4352 return TRUE;
4355 /* If we have an undefined symbol reference here then it must have
4356 come from a shared library that is being linked in. (Undefined
4357 references in regular files have already been handled). If we
4358 are reporting errors for this situation then do so now. */
4359 if (h->root.type == bfd_link_hash_undefined
4360 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4361 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4362 && ! elf_link_check_versioned_symbol (finfo->info, h)
4363 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
4365 if (! ((*finfo->info->callbacks->undefined_symbol)
4366 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4367 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
4369 eoinfo->failed = TRUE;
4370 return FALSE;
4374 /* We should also warn if a forced local symbol is referenced from
4375 shared libraries. */
4376 if (! finfo->info->relocatable
4377 && (! finfo->info->shared)
4378 && (h->elf_link_hash_flags
4379 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4380 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4381 && ! elf_link_check_versioned_symbol (finfo->info, h))
4383 (*_bfd_error_handler)
4384 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4385 bfd_get_filename (finfo->output_bfd),
4386 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4387 ? "internal"
4388 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4389 ? "hidden" : "local",
4390 h->root.root.string,
4391 bfd_archive_filename (h->root.u.def.section->owner));
4392 eoinfo->failed = TRUE;
4393 return FALSE;
4396 /* We don't want to output symbols that have never been mentioned by
4397 a regular file, or that we have been told to strip. However, if
4398 h->indx is set to -2, the symbol is used by a reloc and we must
4399 output it. */
4400 if (h->indx == -2)
4401 strip = FALSE;
4402 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4403 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4404 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4405 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4406 strip = TRUE;
4407 else if (finfo->info->strip == strip_all)
4408 strip = TRUE;
4409 else if (finfo->info->strip == strip_some
4410 && bfd_hash_lookup (finfo->info->keep_hash,
4411 h->root.root.string, FALSE, FALSE) == NULL)
4412 strip = TRUE;
4413 else if (finfo->info->strip_discarded
4414 && (h->root.type == bfd_link_hash_defined
4415 || h->root.type == bfd_link_hash_defweak)
4416 && elf_discarded_section (h->root.u.def.section))
4417 strip = TRUE;
4418 else
4419 strip = FALSE;
4421 /* If we're stripping it, and it's not a dynamic symbol, there's
4422 nothing else to do unless it is a forced local symbol. */
4423 if (strip
4424 && h->dynindx == -1
4425 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4426 return TRUE;
4428 sym.st_value = 0;
4429 sym.st_size = h->size;
4430 sym.st_other = h->other;
4431 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4432 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4433 else if (h->root.type == bfd_link_hash_undefweak
4434 || h->root.type == bfd_link_hash_defweak)
4435 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4436 else
4437 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4439 switch (h->root.type)
4441 default:
4442 case bfd_link_hash_new:
4443 case bfd_link_hash_warning:
4444 abort ();
4445 return FALSE;
4447 case bfd_link_hash_undefined:
4448 case bfd_link_hash_undefweak:
4449 input_sec = bfd_und_section_ptr;
4450 sym.st_shndx = SHN_UNDEF;
4451 break;
4453 case bfd_link_hash_defined:
4454 case bfd_link_hash_defweak:
4456 input_sec = h->root.u.def.section;
4457 if (input_sec->output_section != NULL)
4459 sym.st_shndx =
4460 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4461 input_sec->output_section);
4462 if (sym.st_shndx == SHN_BAD)
4464 (*_bfd_error_handler)
4465 (_("%s: could not find output section %s for input section %s"),
4466 bfd_get_filename (finfo->output_bfd),
4467 input_sec->output_section->name,
4468 input_sec->name);
4469 eoinfo->failed = TRUE;
4470 return FALSE;
4473 /* ELF symbols in relocatable files are section relative,
4474 but in nonrelocatable files they are virtual
4475 addresses. */
4476 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4477 if (! finfo->info->relocatable)
4479 sym.st_value += input_sec->output_section->vma;
4480 if (h->type == STT_TLS)
4482 /* STT_TLS symbols are relative to PT_TLS segment
4483 base. */
4484 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4485 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4489 else
4491 BFD_ASSERT (input_sec->owner == NULL
4492 || (input_sec->owner->flags & DYNAMIC) != 0);
4493 sym.st_shndx = SHN_UNDEF;
4494 input_sec = bfd_und_section_ptr;
4497 break;
4499 case bfd_link_hash_common:
4500 input_sec = h->root.u.c.p->section;
4501 sym.st_shndx = SHN_COMMON;
4502 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4503 break;
4505 case bfd_link_hash_indirect:
4506 /* These symbols are created by symbol versioning. They point
4507 to the decorated version of the name. For example, if the
4508 symbol foo@@GNU_1.2 is the default, which should be used when
4509 foo is used with no version, then we add an indirect symbol
4510 foo which points to foo@@GNU_1.2. We ignore these symbols,
4511 since the indirected symbol is already in the hash table. */
4512 return TRUE;
4515 /* Give the processor backend a chance to tweak the symbol value,
4516 and also to finish up anything that needs to be done for this
4517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4518 forced local syms when non-shared is due to a historical quirk. */
4519 if ((h->dynindx != -1
4520 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4521 && ((finfo->info->shared
4522 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4523 || h->root.type != bfd_link_hash_undefweak))
4524 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4525 && elf_hash_table (finfo->info)->dynamic_sections_created)
4527 const struct elf_backend_data *bed;
4529 bed = get_elf_backend_data (finfo->output_bfd);
4530 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4531 (finfo->output_bfd, finfo->info, h, &sym)))
4533 eoinfo->failed = TRUE;
4534 return FALSE;
4538 /* If we are marking the symbol as undefined, and there are no
4539 non-weak references to this symbol from a regular object, then
4540 mark the symbol as weak undefined; if there are non-weak
4541 references, mark the symbol as strong. We can't do this earlier,
4542 because it might not be marked as undefined until the
4543 finish_dynamic_symbol routine gets through with it. */
4544 if (sym.st_shndx == SHN_UNDEF
4545 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4546 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4547 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4549 int bindtype;
4551 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4552 bindtype = STB_GLOBAL;
4553 else
4554 bindtype = STB_WEAK;
4555 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4558 /* If a non-weak symbol with non-default visibility is not defined
4559 locally, it is a fatal error. */
4560 if (! finfo->info->relocatable
4561 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4562 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4563 && h->root.type == bfd_link_hash_undefined
4564 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4566 (*_bfd_error_handler)
4567 (_("%s: %s symbol `%s' isn't defined"),
4568 bfd_get_filename (finfo->output_bfd),
4569 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4570 ? "protected"
4571 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4572 ? "internal" : "hidden",
4573 h->root.root.string);
4574 eoinfo->failed = TRUE;
4575 return FALSE;
4578 /* If this symbol should be put in the .dynsym section, then put it
4579 there now. We already know the symbol index. We also fill in
4580 the entry in the .hash section. */
4581 if (h->dynindx != -1
4582 && elf_hash_table (finfo->info)->dynamic_sections_created)
4584 size_t bucketcount;
4585 size_t bucket;
4586 size_t hash_entry_size;
4587 bfd_byte *bucketpos;
4588 bfd_vma chain;
4589 Elf_External_Sym *esym;
4591 sym.st_name = h->dynstr_index;
4592 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4593 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
4595 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4596 bucket = h->elf_hash_value % bucketcount;
4597 hash_entry_size
4598 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4599 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4600 + (bucket + 2) * hash_entry_size);
4601 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4602 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
4603 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4604 ((bfd_byte *) finfo->hash_sec->contents
4605 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4607 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4609 Elf_Internal_Versym iversym;
4610 Elf_External_Versym *eversym;
4612 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4614 if (h->verinfo.verdef == NULL)
4615 iversym.vs_vers = 0;
4616 else
4617 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4619 else
4621 if (h->verinfo.vertree == NULL)
4622 iversym.vs_vers = 1;
4623 else
4624 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4627 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4628 iversym.vs_vers |= VERSYM_HIDDEN;
4630 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4631 eversym += h->dynindx;
4632 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4636 /* If we're stripping it, then it was just a dynamic symbol, and
4637 there's nothing else to do. */
4638 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4639 return TRUE;
4641 h->indx = bfd_get_symcount (finfo->output_bfd);
4643 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
4645 eoinfo->failed = TRUE;
4646 return FALSE;
4649 return TRUE;
4652 /* Link an input file into the linker output file. This function
4653 handles all the sections and relocations of the input file at once.
4654 This is so that we only have to read the local symbols once, and
4655 don't have to keep them in memory. */
4657 static bfd_boolean
4658 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
4660 bfd_boolean (*relocate_section)
4661 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4662 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
4663 bfd *output_bfd;
4664 Elf_Internal_Shdr *symtab_hdr;
4665 size_t locsymcount;
4666 size_t extsymoff;
4667 Elf_Internal_Sym *isymbuf;
4668 Elf_Internal_Sym *isym;
4669 Elf_Internal_Sym *isymend;
4670 long *pindex;
4671 asection **ppsection;
4672 asection *o;
4673 const struct elf_backend_data *bed;
4674 bfd_boolean emit_relocs;
4675 struct elf_link_hash_entry **sym_hashes;
4677 output_bfd = finfo->output_bfd;
4678 bed = get_elf_backend_data (output_bfd);
4679 relocate_section = bed->elf_backend_relocate_section;
4681 /* If this is a dynamic object, we don't want to do anything here:
4682 we don't want the local symbols, and we don't want the section
4683 contents. */
4684 if ((input_bfd->flags & DYNAMIC) != 0)
4685 return TRUE;
4687 emit_relocs = (finfo->info->relocatable
4688 || finfo->info->emitrelocations
4689 || bed->elf_backend_emit_relocs);
4691 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4692 if (elf_bad_symtab (input_bfd))
4694 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4695 extsymoff = 0;
4697 else
4699 locsymcount = symtab_hdr->sh_info;
4700 extsymoff = symtab_hdr->sh_info;
4703 /* Read the local symbols. */
4704 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4705 if (isymbuf == NULL && locsymcount != 0)
4707 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4708 finfo->internal_syms,
4709 finfo->external_syms,
4710 finfo->locsym_shndx);
4711 if (isymbuf == NULL)
4712 return FALSE;
4715 /* Find local symbol sections and adjust values of symbols in
4716 SEC_MERGE sections. Write out those local symbols we know are
4717 going into the output file. */
4718 isymend = isymbuf + locsymcount;
4719 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4720 isym < isymend;
4721 isym++, pindex++, ppsection++)
4723 asection *isec;
4724 const char *name;
4725 Elf_Internal_Sym osym;
4727 *pindex = -1;
4729 if (elf_bad_symtab (input_bfd))
4731 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4733 *ppsection = NULL;
4734 continue;
4738 if (isym->st_shndx == SHN_UNDEF)
4739 isec = bfd_und_section_ptr;
4740 else if (isym->st_shndx < SHN_LORESERVE
4741 || isym->st_shndx > SHN_HIRESERVE)
4743 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4744 if (isec
4745 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4746 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4747 isym->st_value =
4748 _bfd_merged_section_offset (output_bfd, &isec,
4749 elf_section_data (isec)->sec_info,
4750 isym->st_value, 0);
4752 else if (isym->st_shndx == SHN_ABS)
4753 isec = bfd_abs_section_ptr;
4754 else if (isym->st_shndx == SHN_COMMON)
4755 isec = bfd_com_section_ptr;
4756 else
4758 /* Who knows? */
4759 isec = NULL;
4762 *ppsection = isec;
4764 /* Don't output the first, undefined, symbol. */
4765 if (ppsection == finfo->sections)
4766 continue;
4768 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4770 /* We never output section symbols. Instead, we use the
4771 section symbol of the corresponding section in the output
4772 file. */
4773 continue;
4776 /* If we are stripping all symbols, we don't want to output this
4777 one. */
4778 if (finfo->info->strip == strip_all)
4779 continue;
4781 /* If we are discarding all local symbols, we don't want to
4782 output this one. If we are generating a relocatable output
4783 file, then some of the local symbols may be required by
4784 relocs; we output them below as we discover that they are
4785 needed. */
4786 if (finfo->info->discard == discard_all)
4787 continue;
4789 /* If this symbol is defined in a section which we are
4790 discarding, we don't need to keep it, but note that
4791 linker_mark is only reliable for sections that have contents.
4792 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4793 as well as linker_mark. */
4794 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4795 && isec != NULL
4796 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4797 || (! finfo->info->relocatable
4798 && (isec->flags & SEC_EXCLUDE) != 0)))
4799 continue;
4801 /* Get the name of the symbol. */
4802 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4803 isym->st_name);
4804 if (name == NULL)
4805 return FALSE;
4807 /* See if we are discarding symbols with this name. */
4808 if ((finfo->info->strip == strip_some
4809 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4810 == NULL))
4811 || (((finfo->info->discard == discard_sec_merge
4812 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4813 || finfo->info->discard == discard_l)
4814 && bfd_is_local_label_name (input_bfd, name)))
4815 continue;
4817 /* If we get here, we are going to output this symbol. */
4819 osym = *isym;
4821 /* Adjust the section index for the output file. */
4822 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4823 isec->output_section);
4824 if (osym.st_shndx == SHN_BAD)
4825 return FALSE;
4827 *pindex = bfd_get_symcount (output_bfd);
4829 /* ELF symbols in relocatable files are section relative, but
4830 in executable files they are virtual addresses. Note that
4831 this code assumes that all ELF sections have an associated
4832 BFD section with a reasonable value for output_offset; below
4833 we assume that they also have a reasonable value for
4834 output_section. Any special sections must be set up to meet
4835 these requirements. */
4836 osym.st_value += isec->output_offset;
4837 if (! finfo->info->relocatable)
4839 osym.st_value += isec->output_section->vma;
4840 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4842 /* STT_TLS symbols are relative to PT_TLS segment base. */
4843 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4844 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
4848 if (! elf_link_output_sym (finfo, name, &osym, isec))
4849 return FALSE;
4852 /* Relocate the contents of each section. */
4853 sym_hashes = elf_sym_hashes (input_bfd);
4854 for (o = input_bfd->sections; o != NULL; o = o->next)
4856 bfd_byte *contents;
4858 if (! o->linker_mark)
4860 /* This section was omitted from the link. */
4861 continue;
4864 if ((o->flags & SEC_HAS_CONTENTS) == 0
4865 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4866 continue;
4868 if ((o->flags & SEC_LINKER_CREATED) != 0)
4870 /* Section was created by _bfd_elf_link_create_dynamic_sections
4871 or somesuch. */
4872 continue;
4875 /* Get the contents of the section. They have been cached by a
4876 relaxation routine. Note that o is a section in an input
4877 file, so the contents field will not have been set by any of
4878 the routines which work on output files. */
4879 if (elf_section_data (o)->this_hdr.contents != NULL)
4880 contents = elf_section_data (o)->this_hdr.contents;
4881 else
4883 contents = finfo->contents;
4884 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4885 o->_raw_size))
4886 return FALSE;
4889 if ((o->flags & SEC_RELOC) != 0)
4891 Elf_Internal_Rela *internal_relocs;
4893 /* Get the swapped relocs. */
4894 internal_relocs
4895 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4896 finfo->internal_relocs, FALSE);
4897 if (internal_relocs == NULL
4898 && o->reloc_count > 0)
4899 return FALSE;
4901 /* Run through the relocs looking for any against symbols
4902 from discarded sections and section symbols from
4903 removed link-once sections. Complain about relocs
4904 against discarded sections. Zero relocs against removed
4905 link-once sections. Preserve debug information as much
4906 as we can. */
4907 if (!elf_section_ignore_discarded_relocs (o))
4909 Elf_Internal_Rela *rel, *relend;
4911 rel = internal_relocs;
4912 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4913 for ( ; rel < relend; rel++)
4915 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4916 asection *sec;
4918 if (r_symndx >= locsymcount
4919 || (elf_bad_symtab (input_bfd)
4920 && finfo->sections[r_symndx] == NULL))
4922 struct elf_link_hash_entry *h;
4924 h = sym_hashes[r_symndx - extsymoff];
4925 while (h->root.type == bfd_link_hash_indirect
4926 || h->root.type == bfd_link_hash_warning)
4927 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4929 /* Complain if the definition comes from a
4930 discarded section. */
4931 sec = h->root.u.def.section;
4932 if ((h->root.type == bfd_link_hash_defined
4933 || h->root.type == bfd_link_hash_defweak)
4934 && elf_discarded_section (sec))
4936 if ((o->flags & SEC_DEBUGGING) != 0)
4938 BFD_ASSERT (r_symndx != 0);
4939 /* Try to preserve debug information. */
4940 if ((o->flags & SEC_DEBUGGING) != 0
4941 && sec->kept_section != NULL
4942 && sec->_raw_size == sec->kept_section->_raw_size)
4943 h->root.u.def.section
4944 = sec->kept_section;
4945 else
4946 memset (rel, 0, sizeof (*rel));
4948 else
4949 finfo->info->callbacks->error_handler
4950 (LD_DEFINITION_IN_DISCARDED_SECTION,
4951 _("%T: discarded in section `%s' from %s\n"),
4952 h->root.root.string,
4953 h->root.root.string,
4954 h->root.u.def.section->name,
4955 bfd_archive_filename (h->root.u.def.section->owner));
4958 else
4960 sec = finfo->sections[r_symndx];
4962 if (sec != NULL && elf_discarded_section (sec))
4964 if ((o->flags & SEC_DEBUGGING) != 0
4965 || (sec->flags & SEC_LINK_ONCE) != 0)
4967 BFD_ASSERT (r_symndx != 0);
4968 /* Try to preserve debug information. */
4969 if ((o->flags & SEC_DEBUGGING) != 0
4970 && sec->kept_section != NULL
4971 && sec->_raw_size == sec->kept_section->_raw_size)
4972 finfo->sections[r_symndx]
4973 = sec->kept_section;
4974 else
4976 rel->r_info
4977 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4978 rel->r_addend = 0;
4981 else
4983 static int count;
4984 int ok;
4985 char *buf;
4987 ok = asprintf (&buf, "local symbol %d",
4988 count++);
4989 if (ok <= 0)
4990 buf = (char *) "local symbol";
4991 finfo->info->callbacks->error_handler
4992 (LD_DEFINITION_IN_DISCARDED_SECTION,
4993 _("%T: discarded in section `%s' from %s\n"),
4994 buf, buf, sec->name,
4995 bfd_archive_filename (input_bfd));
4996 if (ok != -1)
4997 free (buf);
5004 /* Relocate the section by invoking a back end routine.
5006 The back end routine is responsible for adjusting the
5007 section contents as necessary, and (if using Rela relocs
5008 and generating a relocatable output file) adjusting the
5009 reloc addend as necessary.
5011 The back end routine does not have to worry about setting
5012 the reloc address or the reloc symbol index.
5014 The back end routine is given a pointer to the swapped in
5015 internal symbols, and can access the hash table entries
5016 for the external symbols via elf_sym_hashes (input_bfd).
5018 When generating relocatable output, the back end routine
5019 must handle STB_LOCAL/STT_SECTION symbols specially. The
5020 output symbol is going to be a section symbol
5021 corresponding to the output section, which will require
5022 the addend to be adjusted. */
5024 if (! (*relocate_section) (output_bfd, finfo->info,
5025 input_bfd, o, contents,
5026 internal_relocs,
5027 isymbuf,
5028 finfo->sections))
5029 return FALSE;
5031 if (emit_relocs)
5033 Elf_Internal_Rela *irela;
5034 Elf_Internal_Rela *irelaend;
5035 bfd_vma last_offset;
5036 struct elf_link_hash_entry **rel_hash;
5037 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
5038 unsigned int next_erel;
5039 bfd_boolean (*reloc_emitter)
5040 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
5041 bfd_boolean rela_normal;
5043 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5044 rela_normal = (bed->rela_normal
5045 && (input_rel_hdr->sh_entsize
5046 == sizeof (Elf_External_Rela)));
5048 /* Adjust the reloc addresses and symbol indices. */
5050 irela = internal_relocs;
5051 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
5052 rel_hash = (elf_section_data (o->output_section)->rel_hashes
5053 + elf_section_data (o->output_section)->rel_count
5054 + elf_section_data (o->output_section)->rel_count2);
5055 last_offset = o->output_offset;
5056 if (!finfo->info->relocatable)
5057 last_offset += o->output_section->vma;
5058 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
5060 unsigned long r_symndx;
5061 asection *sec;
5062 Elf_Internal_Sym sym;
5064 if (next_erel == bed->s->int_rels_per_ext_rel)
5066 rel_hash++;
5067 next_erel = 0;
5070 irela->r_offset = _bfd_elf_section_offset (output_bfd,
5071 finfo->info, o,
5072 irela->r_offset);
5073 if (irela->r_offset >= (bfd_vma) -2)
5075 /* This is a reloc for a deleted entry or somesuch.
5076 Turn it into an R_*_NONE reloc, at the same
5077 offset as the last reloc. elf_eh_frame.c and
5078 elf_bfd_discard_info rely on reloc offsets
5079 being ordered. */
5080 irela->r_offset = last_offset;
5081 irela->r_info = 0;
5082 irela->r_addend = 0;
5083 continue;
5086 irela->r_offset += o->output_offset;
5088 /* Relocs in an executable have to be virtual addresses. */
5089 if (!finfo->info->relocatable)
5090 irela->r_offset += o->output_section->vma;
5092 last_offset = irela->r_offset;
5094 r_symndx = ELF_R_SYM (irela->r_info);
5095 if (r_symndx == STN_UNDEF)
5096 continue;
5098 if (r_symndx >= locsymcount
5099 || (elf_bad_symtab (input_bfd)
5100 && finfo->sections[r_symndx] == NULL))
5102 struct elf_link_hash_entry *rh;
5103 unsigned long indx;
5105 /* This is a reloc against a global symbol. We
5106 have not yet output all the local symbols, so
5107 we do not know the symbol index of any global
5108 symbol. We set the rel_hash entry for this
5109 reloc to point to the global hash table entry
5110 for this symbol. The symbol index is then
5111 set at the end of elf_bfd_final_link. */
5112 indx = r_symndx - extsymoff;
5113 rh = elf_sym_hashes (input_bfd)[indx];
5114 while (rh->root.type == bfd_link_hash_indirect
5115 || rh->root.type == bfd_link_hash_warning)
5116 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5118 /* Setting the index to -2 tells
5119 elf_link_output_extsym that this symbol is
5120 used by a reloc. */
5121 BFD_ASSERT (rh->indx < 0);
5122 rh->indx = -2;
5124 *rel_hash = rh;
5126 continue;
5129 /* This is a reloc against a local symbol. */
5131 *rel_hash = NULL;
5132 sym = isymbuf[r_symndx];
5133 sec = finfo->sections[r_symndx];
5134 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
5136 /* I suppose the backend ought to fill in the
5137 section of any STT_SECTION symbol against a
5138 processor specific section. If we have
5139 discarded a section, the output_section will
5140 be the absolute section. */
5141 if (bfd_is_abs_section (sec)
5142 || (sec != NULL
5143 && bfd_is_abs_section (sec->output_section)))
5144 r_symndx = 0;
5145 else if (sec == NULL || sec->owner == NULL)
5147 bfd_set_error (bfd_error_bad_value);
5148 return FALSE;
5150 else
5152 r_symndx = sec->output_section->target_index;
5153 BFD_ASSERT (r_symndx != 0);
5156 /* Adjust the addend according to where the
5157 section winds up in the output section. */
5158 if (rela_normal)
5159 irela->r_addend += sec->output_offset;
5161 else
5163 if (finfo->indices[r_symndx] == -1)
5165 unsigned long shlink;
5166 const char *name;
5167 asection *osec;
5169 if (finfo->info->strip == strip_all)
5171 /* You can't do ld -r -s. */
5172 bfd_set_error (bfd_error_invalid_operation);
5173 return FALSE;
5176 /* This symbol was skipped earlier, but
5177 since it is needed by a reloc, we
5178 must output it now. */
5179 shlink = symtab_hdr->sh_link;
5180 name = (bfd_elf_string_from_elf_section
5181 (input_bfd, shlink, sym.st_name));
5182 if (name == NULL)
5183 return FALSE;
5185 osec = sec->output_section;
5186 sym.st_shndx =
5187 _bfd_elf_section_from_bfd_section (output_bfd,
5188 osec);
5189 if (sym.st_shndx == SHN_BAD)
5190 return FALSE;
5192 sym.st_value += sec->output_offset;
5193 if (! finfo->info->relocatable)
5195 sym.st_value += osec->vma;
5196 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
5198 /* STT_TLS symbols are relative to PT_TLS
5199 segment base. */
5200 BFD_ASSERT (elf_hash_table (finfo->info)
5201 ->tls_sec != NULL);
5202 sym.st_value -= (elf_hash_table (finfo->info)
5203 ->tls_sec->vma);
5207 finfo->indices[r_symndx]
5208 = bfd_get_symcount (output_bfd);
5210 if (! elf_link_output_sym (finfo, name, &sym, sec))
5211 return FALSE;
5214 r_symndx = finfo->indices[r_symndx];
5217 irela->r_info = ELF_R_INFO (r_symndx,
5218 ELF_R_TYPE (irela->r_info));
5221 /* Swap out the relocs. */
5222 if (bed->elf_backend_emit_relocs
5223 && !(finfo->info->relocatable
5224 || finfo->info->emitrelocations))
5225 reloc_emitter = bed->elf_backend_emit_relocs;
5226 else
5227 reloc_emitter = _bfd_elf_link_output_relocs;
5229 if (input_rel_hdr->sh_size != 0
5230 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
5231 internal_relocs))
5232 return FALSE;
5234 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
5235 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
5237 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
5238 * bed->s->int_rels_per_ext_rel);
5239 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
5240 internal_relocs))
5241 return FALSE;
5246 /* Write out the modified section contents. */
5247 if (bed->elf_backend_write_section
5248 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
5250 /* Section written out. */
5252 else switch (o->sec_info_type)
5254 case ELF_INFO_TYPE_STABS:
5255 if (! (_bfd_write_section_stabs
5256 (output_bfd,
5257 &elf_hash_table (finfo->info)->stab_info,
5258 o, &elf_section_data (o)->sec_info, contents)))
5259 return FALSE;
5260 break;
5261 case ELF_INFO_TYPE_MERGE:
5262 if (! _bfd_write_merged_section (output_bfd, o,
5263 elf_section_data (o)->sec_info))
5264 return FALSE;
5265 break;
5266 case ELF_INFO_TYPE_EH_FRAME:
5268 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5269 o, contents))
5270 return FALSE;
5272 break;
5273 default:
5275 bfd_size_type sec_size;
5277 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5278 if (! (o->flags & SEC_EXCLUDE)
5279 && ! bfd_set_section_contents (output_bfd, o->output_section,
5280 contents,
5281 (file_ptr) o->output_offset,
5282 sec_size))
5283 return FALSE;
5285 break;
5289 return TRUE;
5292 /* Generate a reloc when linking an ELF file. This is a reloc
5293 requested by the linker, and does come from any input file. This
5294 is used to build constructor and destructor tables when linking
5295 with -Ur. */
5297 static bfd_boolean
5298 elf_reloc_link_order (bfd *output_bfd,
5299 struct bfd_link_info *info,
5300 asection *output_section,
5301 struct bfd_link_order *link_order)
5303 reloc_howto_type *howto;
5304 long indx;
5305 bfd_vma offset;
5306 bfd_vma addend;
5307 struct elf_link_hash_entry **rel_hash_ptr;
5308 Elf_Internal_Shdr *rel_hdr;
5309 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5310 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5311 bfd_byte *erel;
5312 unsigned int i;
5314 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5315 if (howto == NULL)
5317 bfd_set_error (bfd_error_bad_value);
5318 return FALSE;
5321 addend = link_order->u.reloc.p->addend;
5323 /* Figure out the symbol index. */
5324 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5325 + elf_section_data (output_section)->rel_count
5326 + elf_section_data (output_section)->rel_count2);
5327 if (link_order->type == bfd_section_reloc_link_order)
5329 indx = link_order->u.reloc.p->u.section->target_index;
5330 BFD_ASSERT (indx != 0);
5331 *rel_hash_ptr = NULL;
5333 else
5335 struct elf_link_hash_entry *h;
5337 /* Treat a reloc against a defined symbol as though it were
5338 actually against the section. */
5339 h = ((struct elf_link_hash_entry *)
5340 bfd_wrapped_link_hash_lookup (output_bfd, info,
5341 link_order->u.reloc.p->u.name,
5342 FALSE, FALSE, TRUE));
5343 if (h != NULL
5344 && (h->root.type == bfd_link_hash_defined
5345 || h->root.type == bfd_link_hash_defweak))
5347 asection *section;
5349 section = h->root.u.def.section;
5350 indx = section->output_section->target_index;
5351 *rel_hash_ptr = NULL;
5352 /* It seems that we ought to add the symbol value to the
5353 addend here, but in practice it has already been added
5354 because it was passed to constructor_callback. */
5355 addend += section->output_section->vma + section->output_offset;
5357 else if (h != NULL)
5359 /* Setting the index to -2 tells elf_link_output_extsym that
5360 this symbol is used by a reloc. */
5361 h->indx = -2;
5362 *rel_hash_ptr = h;
5363 indx = 0;
5365 else
5367 if (! ((*info->callbacks->unattached_reloc)
5368 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
5369 return FALSE;
5370 indx = 0;
5374 /* If this is an inplace reloc, we must write the addend into the
5375 object file. */
5376 if (howto->partial_inplace && addend != 0)
5378 bfd_size_type size;
5379 bfd_reloc_status_type rstat;
5380 bfd_byte *buf;
5381 bfd_boolean ok;
5382 const char *sym_name;
5384 size = bfd_get_reloc_size (howto);
5385 buf = bfd_zmalloc (size);
5386 if (buf == NULL)
5387 return FALSE;
5388 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5389 switch (rstat)
5391 case bfd_reloc_ok:
5392 break;
5394 default:
5395 case bfd_reloc_outofrange:
5396 abort ();
5398 case bfd_reloc_overflow:
5399 if (link_order->type == bfd_section_reloc_link_order)
5400 sym_name = bfd_section_name (output_bfd,
5401 link_order->u.reloc.p->u.section);
5402 else
5403 sym_name = link_order->u.reloc.p->u.name;
5404 if (! ((*info->callbacks->reloc_overflow)
5405 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
5407 free (buf);
5408 return FALSE;
5410 break;
5412 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5413 link_order->offset, size);
5414 free (buf);
5415 if (! ok)
5416 return FALSE;
5419 /* The address of a reloc is relative to the section in a
5420 relocatable file, and is a virtual address in an executable
5421 file. */
5422 offset = link_order->offset;
5423 if (! info->relocatable)
5424 offset += output_section->vma;
5426 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5428 irel[i].r_offset = offset;
5429 irel[i].r_info = 0;
5430 irel[i].r_addend = 0;
5432 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5434 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5435 erel = rel_hdr->contents;
5436 if (rel_hdr->sh_type == SHT_REL)
5438 erel += (elf_section_data (output_section)->rel_count
5439 * sizeof (Elf_External_Rel));
5440 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5442 else
5444 irel[0].r_addend = addend;
5445 erel += (elf_section_data (output_section)->rel_count
5446 * sizeof (Elf_External_Rela));
5447 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5450 ++elf_section_data (output_section)->rel_count;
5452 return TRUE;
5455 /* Garbage collect unused sections. */
5457 static bfd_boolean elf_gc_sweep_symbol
5458 (struct elf_link_hash_entry *, void *);
5460 static bfd_boolean elf_gc_allocate_got_offsets
5461 (struct elf_link_hash_entry *, void *);
5463 /* The mark phase of garbage collection. For a given section, mark
5464 it and any sections in this section's group, and all the sections
5465 which define symbols to which it refers. */
5467 typedef asection * (*gc_mark_hook_fn)
5468 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5469 struct elf_link_hash_entry *, Elf_Internal_Sym *);
5471 static bfd_boolean
5472 elf_gc_mark (struct bfd_link_info *info,
5473 asection *sec,
5474 gc_mark_hook_fn gc_mark_hook)
5476 bfd_boolean ret;
5477 asection *group_sec;
5479 sec->gc_mark = 1;
5481 /* Mark all the sections in the group. */
5482 group_sec = elf_section_data (sec)->next_in_group;
5483 if (group_sec && !group_sec->gc_mark)
5484 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5485 return FALSE;
5487 /* Look through the section relocs. */
5488 ret = TRUE;
5489 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5491 Elf_Internal_Rela *relstart, *rel, *relend;
5492 Elf_Internal_Shdr *symtab_hdr;
5493 struct elf_link_hash_entry **sym_hashes;
5494 size_t nlocsyms;
5495 size_t extsymoff;
5496 bfd *input_bfd = sec->owner;
5497 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5498 Elf_Internal_Sym *isym = NULL;
5500 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5501 sym_hashes = elf_sym_hashes (input_bfd);
5503 /* Read the local symbols. */
5504 if (elf_bad_symtab (input_bfd))
5506 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5507 extsymoff = 0;
5509 else
5510 extsymoff = nlocsyms = symtab_hdr->sh_info;
5512 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5513 if (isym == NULL && nlocsyms != 0)
5515 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5516 NULL, NULL, NULL);
5517 if (isym == NULL)
5518 return FALSE;
5521 /* Read the relocations. */
5522 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
5523 info->keep_memory);
5524 if (relstart == NULL)
5526 ret = FALSE;
5527 goto out1;
5529 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5531 for (rel = relstart; rel < relend; rel++)
5533 unsigned long r_symndx;
5534 asection *rsec;
5535 struct elf_link_hash_entry *h;
5537 r_symndx = ELF_R_SYM (rel->r_info);
5538 if (r_symndx == 0)
5539 continue;
5541 if (r_symndx >= nlocsyms
5542 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5544 h = sym_hashes[r_symndx - extsymoff];
5545 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5547 else
5549 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5552 if (rsec && !rsec->gc_mark)
5554 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5555 rsec->gc_mark = 1;
5556 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5558 ret = FALSE;
5559 goto out2;
5564 out2:
5565 if (elf_section_data (sec)->relocs != relstart)
5566 free (relstart);
5567 out1:
5568 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5570 if (! info->keep_memory)
5571 free (isym);
5572 else
5573 symtab_hdr->contents = (unsigned char *) isym;
5577 return ret;
5580 /* The sweep phase of garbage collection. Remove all garbage sections. */
5582 typedef bfd_boolean (*gc_sweep_hook_fn)
5583 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
5585 static bfd_boolean
5586 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
5588 bfd *sub;
5590 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5592 asection *o;
5594 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5595 continue;
5597 for (o = sub->sections; o != NULL; o = o->next)
5599 /* Keep special sections. Keep .debug sections. */
5600 if ((o->flags & SEC_LINKER_CREATED)
5601 || (o->flags & SEC_DEBUGGING))
5602 o->gc_mark = 1;
5604 if (o->gc_mark)
5605 continue;
5607 /* Skip sweeping sections already excluded. */
5608 if (o->flags & SEC_EXCLUDE)
5609 continue;
5611 /* Since this is early in the link process, it is simple
5612 to remove a section from the output. */
5613 o->flags |= SEC_EXCLUDE;
5615 /* But we also have to update some of the relocation
5616 info we collected before. */
5617 if (gc_sweep_hook
5618 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5620 Elf_Internal_Rela *internal_relocs;
5621 bfd_boolean r;
5623 internal_relocs
5624 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
5625 info->keep_memory);
5626 if (internal_relocs == NULL)
5627 return FALSE;
5629 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5631 if (elf_section_data (o)->relocs != internal_relocs)
5632 free (internal_relocs);
5634 if (!r)
5635 return FALSE;
5640 /* Remove the symbols that were in the swept sections from the dynamic
5641 symbol table. GCFIXME: Anyone know how to get them out of the
5642 static symbol table as well? */
5644 int i = 0;
5646 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
5648 elf_hash_table (info)->dynsymcount = i;
5651 return TRUE;
5654 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5656 static bfd_boolean
5657 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
5659 int *idx = idxptr;
5661 if (h->root.type == bfd_link_hash_warning)
5662 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5664 if (h->dynindx != -1
5665 && ((h->root.type != bfd_link_hash_defined
5666 && h->root.type != bfd_link_hash_defweak)
5667 || h->root.u.def.section->gc_mark))
5668 h->dynindx = (*idx)++;
5670 return TRUE;
5673 /* Propagate collected vtable information. This is called through
5674 elf_link_hash_traverse. */
5676 static bfd_boolean
5677 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
5679 if (h->root.type == bfd_link_hash_warning)
5680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5682 /* Those that are not vtables. */
5683 if (h->vtable_parent == NULL)
5684 return TRUE;
5686 /* Those vtables that do not have parents, we cannot merge. */
5687 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5688 return TRUE;
5690 /* If we've already been done, exit. */
5691 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5692 return TRUE;
5694 /* Make sure the parent's table is up to date. */
5695 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5697 if (h->vtable_entries_used == NULL)
5699 /* None of this table's entries were referenced. Re-use the
5700 parent's table. */
5701 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5702 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5704 else
5706 size_t n;
5707 bfd_boolean *cu, *pu;
5709 /* Or the parent's entries into ours. */
5710 cu = h->vtable_entries_used;
5711 cu[-1] = TRUE;
5712 pu = h->vtable_parent->vtable_entries_used;
5713 if (pu != NULL)
5715 const struct elf_backend_data *bed;
5716 unsigned int log_file_align;
5718 bed = get_elf_backend_data (h->root.u.def.section->owner);
5719 log_file_align = bed->s->log_file_align;
5720 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5721 while (n--)
5723 if (*pu)
5724 *cu = TRUE;
5725 pu++;
5726 cu++;
5731 return TRUE;
5734 static bfd_boolean
5735 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
5737 asection *sec;
5738 bfd_vma hstart, hend;
5739 Elf_Internal_Rela *relstart, *relend, *rel;
5740 const struct elf_backend_data *bed;
5741 unsigned int log_file_align;
5743 if (h->root.type == bfd_link_hash_warning)
5744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5746 /* Take care of both those symbols that do not describe vtables as
5747 well as those that are not loaded. */
5748 if (h->vtable_parent == NULL)
5749 return TRUE;
5751 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5752 || h->root.type == bfd_link_hash_defweak);
5754 sec = h->root.u.def.section;
5755 hstart = h->root.u.def.value;
5756 hend = hstart + h->size;
5758 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
5759 if (!relstart)
5760 return *(bfd_boolean *) okp = FALSE;
5761 bed = get_elf_backend_data (sec->owner);
5762 log_file_align = bed->s->log_file_align;
5764 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5766 for (rel = relstart; rel < relend; ++rel)
5767 if (rel->r_offset >= hstart && rel->r_offset < hend)
5769 /* If the entry is in use, do nothing. */
5770 if (h->vtable_entries_used
5771 && (rel->r_offset - hstart) < h->vtable_entries_size)
5773 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5774 if (h->vtable_entries_used[entry])
5775 continue;
5777 /* Otherwise, kill it. */
5778 rel->r_offset = rel->r_info = rel->r_addend = 0;
5781 return TRUE;
5784 /* Do mark and sweep of unused sections. */
5786 bfd_boolean
5787 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5789 bfd_boolean ok = TRUE;
5790 bfd *sub;
5791 asection * (*gc_mark_hook)
5792 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5793 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
5795 if (!get_elf_backend_data (abfd)->can_gc_sections
5796 || info->relocatable
5797 || info->emitrelocations
5798 || !is_elf_hash_table (info->hash)
5799 || elf_hash_table (info)->dynamic_sections_created)
5800 return TRUE;
5802 /* Apply transitive closure to the vtable entry usage info. */
5803 elf_link_hash_traverse (elf_hash_table (info),
5804 elf_gc_propagate_vtable_entries_used,
5805 &ok);
5806 if (!ok)
5807 return FALSE;
5809 /* Kill the vtable relocations that were not used. */
5810 elf_link_hash_traverse (elf_hash_table (info),
5811 elf_gc_smash_unused_vtentry_relocs,
5812 &ok);
5813 if (!ok)
5814 return FALSE;
5816 /* Grovel through relocs to find out who stays ... */
5818 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5819 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5821 asection *o;
5823 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5824 continue;
5826 for (o = sub->sections; o != NULL; o = o->next)
5828 if (o->flags & SEC_KEEP)
5829 if (!elf_gc_mark (info, o, gc_mark_hook))
5830 return FALSE;
5834 /* ... and mark SEC_EXCLUDE for those that go. */
5835 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5836 return FALSE;
5838 return TRUE;
5841 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5843 bfd_boolean
5844 elf_gc_record_vtinherit (bfd *abfd,
5845 asection *sec,
5846 struct elf_link_hash_entry *h,
5847 bfd_vma offset)
5849 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5850 struct elf_link_hash_entry **search, *child;
5851 bfd_size_type extsymcount;
5853 /* The sh_info field of the symtab header tells us where the
5854 external symbols start. We don't care about the local symbols at
5855 this point. */
5856 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5857 if (!elf_bad_symtab (abfd))
5858 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5860 sym_hashes = elf_sym_hashes (abfd);
5861 sym_hashes_end = sym_hashes + extsymcount;
5863 /* Hunt down the child symbol, which is in this section at the same
5864 offset as the relocation. */
5865 for (search = sym_hashes; search != sym_hashes_end; ++search)
5867 if ((child = *search) != NULL
5868 && (child->root.type == bfd_link_hash_defined
5869 || child->root.type == bfd_link_hash_defweak)
5870 && child->root.u.def.section == sec
5871 && child->root.u.def.value == offset)
5872 goto win;
5875 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
5876 bfd_archive_filename (abfd), sec->name,
5877 (unsigned long) offset);
5878 bfd_set_error (bfd_error_invalid_operation);
5879 return FALSE;
5881 win:
5882 if (!h)
5884 /* This *should* only be the absolute section. It could potentially
5885 be that someone has defined a non-global vtable though, which
5886 would be bad. It isn't worth paging in the local symbols to be
5887 sure though; that case should simply be handled by the assembler. */
5889 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5891 else
5892 child->vtable_parent = h;
5894 return TRUE;
5897 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5899 bfd_boolean
5900 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5901 asection *sec ATTRIBUTE_UNUSED,
5902 struct elf_link_hash_entry *h,
5903 bfd_vma addend)
5905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5906 unsigned int log_file_align = bed->s->log_file_align;
5908 if (addend >= h->vtable_entries_size)
5910 size_t size, bytes, file_align;
5911 bfd_boolean *ptr = h->vtable_entries_used;
5913 /* While the symbol is undefined, we have to be prepared to handle
5914 a zero size. */
5915 file_align = 1 << log_file_align;
5916 if (h->root.type == bfd_link_hash_undefined)
5917 size = addend + file_align;
5918 else
5920 size = h->size;
5921 if (addend >= size)
5923 /* Oops! We've got a reference past the defined end of
5924 the table. This is probably a bug -- shall we warn? */
5925 size = addend + file_align;
5928 size = (size + file_align - 1) & -file_align;
5930 /* Allocate one extra entry for use as a "done" flag for the
5931 consolidation pass. */
5932 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
5934 if (ptr)
5936 ptr = bfd_realloc (ptr - 1, bytes);
5938 if (ptr != NULL)
5940 size_t oldbytes;
5942 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
5943 * sizeof (bfd_boolean));
5944 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
5947 else
5948 ptr = bfd_zmalloc (bytes);
5950 if (ptr == NULL)
5951 return FALSE;
5953 /* And arrange for that done flag to be at index -1. */
5954 h->vtable_entries_used = ptr + 1;
5955 h->vtable_entries_size = size;
5958 h->vtable_entries_used[addend >> log_file_align] = TRUE;
5960 return TRUE;
5963 /* And an accompanying bit to work out final got entry offsets once
5964 we're done. Should be called from final_link. */
5966 bfd_boolean
5967 elf_gc_common_finalize_got_offsets (bfd *abfd,
5968 struct bfd_link_info *info)
5970 bfd *i;
5971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5972 bfd_vma gotoff;
5974 if (! is_elf_hash_table (info->hash))
5975 return FALSE;
5977 /* The GOT offset is relative to the .got section, but the GOT header is
5978 put into the .got.plt section, if the backend uses it. */
5979 if (bed->want_got_plt)
5980 gotoff = 0;
5981 else
5982 gotoff = bed->got_header_size;
5984 /* Do the local .got entries first. */
5985 for (i = info->input_bfds; i; i = i->link_next)
5987 bfd_signed_vma *local_got;
5988 bfd_size_type j, locsymcount;
5989 Elf_Internal_Shdr *symtab_hdr;
5991 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5992 continue;
5994 local_got = elf_local_got_refcounts (i);
5995 if (!local_got)
5996 continue;
5998 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5999 if (elf_bad_symtab (i))
6000 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6001 else
6002 locsymcount = symtab_hdr->sh_info;
6004 for (j = 0; j < locsymcount; ++j)
6006 if (local_got[j] > 0)
6008 local_got[j] = gotoff;
6009 gotoff += ARCH_SIZE / 8;
6011 else
6012 local_got[j] = (bfd_vma) -1;
6016 /* Then the global .got entries. .plt refcounts are handled by
6017 adjust_dynamic_symbol */
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 elf_gc_allocate_got_offsets,
6020 &gotoff);
6021 return TRUE;
6024 /* We need a special top-level link routine to convert got reference counts
6025 to real got offsets. */
6027 static bfd_boolean
6028 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
6030 bfd_vma *off = offarg;
6032 if (h->root.type == bfd_link_hash_warning)
6033 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6035 if (h->got.refcount > 0)
6037 h->got.offset = off[0];
6038 off[0] += ARCH_SIZE / 8;
6040 else
6041 h->got.offset = (bfd_vma) -1;
6043 return TRUE;
6046 /* Many folk need no more in the way of final link than this, once
6047 got entry reference counting is enabled. */
6049 bfd_boolean
6050 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
6052 if (!elf_gc_common_finalize_got_offsets (abfd, info))
6053 return FALSE;
6055 /* Invoke the regular ELF backend linker to do all the work. */
6056 return elf_bfd_final_link (abfd, info);
6059 /* This function will be called though elf_link_hash_traverse to store
6060 all hash value of the exported symbols in an array. */
6062 static bfd_boolean
6063 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6065 unsigned long **valuep = data;
6066 const char *name;
6067 char *p;
6068 unsigned long ha;
6069 char *alc = NULL;
6071 if (h->root.type == bfd_link_hash_warning)
6072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6074 /* Ignore indirect symbols. These are added by the versioning code. */
6075 if (h->dynindx == -1)
6076 return TRUE;
6078 name = h->root.root.string;
6079 p = strchr (name, ELF_VER_CHR);
6080 if (p != NULL)
6082 alc = bfd_malloc (p - name + 1);
6083 memcpy (alc, name, p - name);
6084 alc[p - name] = '\0';
6085 name = alc;
6088 /* Compute the hash value. */
6089 ha = bfd_elf_hash (name);
6091 /* Store the found hash value in the array given as the argument. */
6092 *(*valuep)++ = ha;
6094 /* And store it in the struct so that we can put it in the hash table
6095 later. */
6096 h->elf_hash_value = ha;
6098 if (alc != NULL)
6099 free (alc);
6101 return TRUE;
6104 bfd_boolean
6105 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
6107 struct elf_reloc_cookie *rcookie = cookie;
6109 if (rcookie->bad_symtab)
6110 rcookie->rel = rcookie->rels;
6112 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
6114 unsigned long r_symndx;
6116 if (! rcookie->bad_symtab)
6117 if (rcookie->rel->r_offset > offset)
6118 return FALSE;
6119 if (rcookie->rel->r_offset != offset)
6120 continue;
6122 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
6123 if (r_symndx == SHN_UNDEF)
6124 return TRUE;
6126 if (r_symndx >= rcookie->locsymcount
6127 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
6129 struct elf_link_hash_entry *h;
6131 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
6133 while (h->root.type == bfd_link_hash_indirect
6134 || h->root.type == bfd_link_hash_warning)
6135 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6137 if ((h->root.type == bfd_link_hash_defined
6138 || h->root.type == bfd_link_hash_defweak)
6139 && elf_discarded_section (h->root.u.def.section))
6140 return TRUE;
6141 else
6142 return FALSE;
6144 else
6146 /* It's not a relocation against a global symbol,
6147 but it could be a relocation against a local
6148 symbol for a discarded section. */
6149 asection *isec;
6150 Elf_Internal_Sym *isym;
6152 /* Need to: get the symbol; get the section. */
6153 isym = &rcookie->locsyms[r_symndx];
6154 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6156 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
6157 if (isec != NULL && elf_discarded_section (isec))
6158 return TRUE;
6161 return FALSE;
6163 return FALSE;
6166 /* Discard unneeded references to discarded sections.
6167 Returns TRUE if any section's size was changed. */
6168 /* This function assumes that the relocations are in sorted order,
6169 which is true for all known assemblers. */
6171 bfd_boolean
6172 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
6174 struct elf_reloc_cookie cookie;
6175 asection *stab, *eh;
6176 Elf_Internal_Shdr *symtab_hdr;
6177 const struct elf_backend_data *bed;
6178 bfd *abfd;
6179 unsigned int count;
6180 bfd_boolean ret = FALSE;
6182 if (info->traditional_format
6183 || !is_elf_hash_table (info->hash))
6184 return FALSE;
6186 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6188 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6189 continue;
6191 bed = get_elf_backend_data (abfd);
6193 if ((abfd->flags & DYNAMIC) != 0)
6194 continue;
6196 eh = bfd_get_section_by_name (abfd, ".eh_frame");
6197 if (info->relocatable
6198 || (eh != NULL
6199 && (eh->_raw_size == 0
6200 || bfd_is_abs_section (eh->output_section))))
6201 eh = NULL;
6203 stab = bfd_get_section_by_name (abfd, ".stab");
6204 if (stab != NULL
6205 && (stab->_raw_size == 0
6206 || bfd_is_abs_section (stab->output_section)
6207 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
6208 stab = NULL;
6210 if (stab == NULL
6211 && eh == NULL
6212 && bed->elf_backend_discard_info == NULL)
6213 continue;
6215 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6216 cookie.abfd = abfd;
6217 cookie.sym_hashes = elf_sym_hashes (abfd);
6218 cookie.bad_symtab = elf_bad_symtab (abfd);
6219 if (cookie.bad_symtab)
6221 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6222 cookie.extsymoff = 0;
6224 else
6226 cookie.locsymcount = symtab_hdr->sh_info;
6227 cookie.extsymoff = symtab_hdr->sh_info;
6230 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6231 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
6233 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6234 cookie.locsymcount, 0,
6235 NULL, NULL, NULL);
6236 if (cookie.locsyms == NULL)
6237 return FALSE;
6240 if (stab != NULL)
6242 cookie.rels = NULL;
6243 count = stab->reloc_count;
6244 if (count != 0)
6245 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6246 info->keep_memory);
6247 if (cookie.rels != NULL)
6249 cookie.rel = cookie.rels;
6250 cookie.relend = cookie.rels;
6251 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6252 if (_bfd_discard_section_stabs (abfd, stab,
6253 elf_section_data (stab)->sec_info,
6254 elf_reloc_symbol_deleted_p,
6255 &cookie))
6256 ret = TRUE;
6257 if (elf_section_data (stab)->relocs != cookie.rels)
6258 free (cookie.rels);
6262 if (eh != NULL)
6264 cookie.rels = NULL;
6265 count = eh->reloc_count;
6266 if (count != 0)
6267 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6268 info->keep_memory);
6269 cookie.rel = cookie.rels;
6270 cookie.relend = cookie.rels;
6271 if (cookie.rels != NULL)
6272 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6274 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6275 elf_reloc_symbol_deleted_p,
6276 &cookie))
6277 ret = TRUE;
6279 if (cookie.rels != NULL
6280 && elf_section_data (eh)->relocs != cookie.rels)
6281 free (cookie.rels);
6284 if (bed->elf_backend_discard_info != NULL
6285 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6286 ret = TRUE;
6288 if (cookie.locsyms != NULL
6289 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6291 if (! info->keep_memory)
6292 free (cookie.locsyms);
6293 else
6294 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6298 if (info->eh_frame_hdr
6299 && !info->relocatable
6300 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6301 ret = TRUE;
6303 return ret;
6306 static bfd_boolean
6307 elf_section_ignore_discarded_relocs (asection *sec)
6309 const struct elf_backend_data *bed;
6311 switch (sec->sec_info_type)
6313 case ELF_INFO_TYPE_STABS:
6314 case ELF_INFO_TYPE_EH_FRAME:
6315 return TRUE;
6316 default:
6317 break;
6320 bed = get_elf_backend_data (sec->owner);
6321 if (bed->elf_backend_ignore_discarded_relocs != NULL
6322 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6323 return TRUE;
6325 return FALSE;