Convert ia64-gen to use getopt(). Add standard GNU options plus --srcdir.
[binutils.git] / bfd / elflink.h
blobc116ed5d44aa9de8beac05223cdf4760ff83bb3c
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
26 struct elf_info_failed
28 boolean failed;
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_link_read_relocs_from_section
65 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
66 static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info *));
68 static boolean elf_link_output_relocs
69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
70 static boolean elf_link_size_reloc_section
71 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
72 static void elf_link_adjust_relocs
73 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
74 struct elf_link_hash_entry **));
75 static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77 static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79 static size_t elf_link_sort_relocs
80 PARAMS ((bfd *, struct bfd_link_info *, asection **));
81 static boolean elf_section_ignore_discarded_relocs
82 PARAMS ((asection *));
84 /* Given an ELF BFD, add symbols to the global hash table as
85 appropriate. */
87 boolean
88 elf_bfd_link_add_symbols (abfd, info)
89 bfd *abfd;
90 struct bfd_link_info *info;
92 switch (bfd_get_format (abfd))
94 case bfd_object:
95 return elf_link_add_object_symbols (abfd, info);
96 case bfd_archive:
97 return elf_link_add_archive_symbols (abfd, info);
98 default:
99 bfd_set_error (bfd_error_wrong_format);
100 return false;
104 /* Return true iff this is a non-common, definition of a non-function symbol. */
105 static boolean
106 is_global_data_symbol_definition (abfd, sym)
107 bfd * abfd ATTRIBUTE_UNUSED;
108 Elf_Internal_Sym * sym;
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
112 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
113 return false;
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
117 return false;
119 /* If the section is undefined, then so is the symbol. */
120 if (sym->st_shndx == SHN_UNDEF)
121 return false;
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym->st_shndx == SHN_COMMON)
126 return false;
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
131 /* FIXME - this function is not coded yet:
133 return _bfd_is_global_symbol_definition (abfd, sym);
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
138 return false;
140 return true;
143 /* Search the symbol table of the archive element of the archive ABFD
144 whose archive map contains a mention of SYMDEF, and determine if
145 the symbol is defined in this element. */
146 static boolean
147 elf_link_is_defined_archive_symbol (abfd, symdef)
148 bfd * abfd;
149 carsym * symdef;
151 Elf_Internal_Shdr * hdr;
152 bfd_size_type symcount;
153 bfd_size_type extsymcount;
154 bfd_size_type extsymoff;
155 Elf_Internal_Sym *isymbuf;
156 Elf_Internal_Sym *isym;
157 Elf_Internal_Sym *isymend;
158 boolean result;
160 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
161 if (abfd == (bfd *) NULL)
162 return false;
164 if (! bfd_check_format (abfd, bfd_object))
165 return false;
167 /* If we have already included the element containing this symbol in the
168 link then we do not need to include it again. Just claim that any symbol
169 it contains is not a definition, so that our caller will not decide to
170 (re)include this element. */
171 if (abfd->archive_pass)
172 return false;
174 /* Select the appropriate symbol table. */
175 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
176 hdr = &elf_tdata (abfd)->symtab_hdr;
177 else
178 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
180 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
182 /* The sh_info field of the symtab header tells us where the
183 external symbols start. We don't care about the local symbols. */
184 if (elf_bad_symtab (abfd))
186 extsymcount = symcount;
187 extsymoff = 0;
189 else
191 extsymcount = symcount - hdr->sh_info;
192 extsymoff = hdr->sh_info;
195 if (extsymcount == 0)
196 return false;
198 /* Read in the symbol table. */
199 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
200 NULL, NULL, NULL);
201 if (isymbuf == NULL)
202 return false;
204 /* Scan the symbol table looking for SYMDEF. */
205 result = false;
206 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
208 const char *name;
210 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
211 isym->st_name);
212 if (name == (const char *) NULL)
213 break;
215 if (strcmp (name, symdef->name) == 0)
217 result = is_global_data_symbol_definition (abfd, isym);
218 break;
222 free (isymbuf);
224 return result;
227 /* Add symbols from an ELF archive file to the linker hash table. We
228 don't use _bfd_generic_link_add_archive_symbols because of a
229 problem which arises on UnixWare. The UnixWare libc.so is an
230 archive which includes an entry libc.so.1 which defines a bunch of
231 symbols. The libc.so archive also includes a number of other
232 object files, which also define symbols, some of which are the same
233 as those defined in libc.so.1. Correct linking requires that we
234 consider each object file in turn, and include it if it defines any
235 symbols we need. _bfd_generic_link_add_archive_symbols does not do
236 this; it looks through the list of undefined symbols, and includes
237 any object file which defines them. When this algorithm is used on
238 UnixWare, it winds up pulling in libc.so.1 early and defining a
239 bunch of symbols. This means that some of the other objects in the
240 archive are not included in the link, which is incorrect since they
241 precede libc.so.1 in the archive.
243 Fortunately, ELF archive handling is simpler than that done by
244 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
245 oddities. In ELF, if we find a symbol in the archive map, and the
246 symbol is currently undefined, we know that we must pull in that
247 object file.
249 Unfortunately, we do have to make multiple passes over the symbol
250 table until nothing further is resolved. */
252 static boolean
253 elf_link_add_archive_symbols (abfd, info)
254 bfd *abfd;
255 struct bfd_link_info *info;
257 symindex c;
258 boolean *defined = NULL;
259 boolean *included = NULL;
260 carsym *symdefs;
261 boolean loop;
262 bfd_size_type amt;
264 if (! bfd_has_map (abfd))
266 /* An empty archive is a special case. */
267 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
268 return true;
269 bfd_set_error (bfd_error_no_armap);
270 return false;
273 /* Keep track of all symbols we know to be already defined, and all
274 files we know to be already included. This is to speed up the
275 second and subsequent passes. */
276 c = bfd_ardata (abfd)->symdef_count;
277 if (c == 0)
278 return true;
279 amt = c;
280 amt *= sizeof (boolean);
281 defined = (boolean *) bfd_zmalloc (amt);
282 included = (boolean *) bfd_zmalloc (amt);
283 if (defined == (boolean *) NULL || included == (boolean *) NULL)
284 goto error_return;
286 symdefs = bfd_ardata (abfd)->symdefs;
290 file_ptr last;
291 symindex i;
292 carsym *symdef;
293 carsym *symdefend;
295 loop = false;
296 last = -1;
298 symdef = symdefs;
299 symdefend = symdef + c;
300 for (i = 0; symdef < symdefend; symdef++, i++)
302 struct elf_link_hash_entry *h;
303 bfd *element;
304 struct bfd_link_hash_entry *undefs_tail;
305 symindex mark;
307 if (defined[i] || included[i])
308 continue;
309 if (symdef->file_offset == last)
311 included[i] = true;
312 continue;
315 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
316 false, false, false);
318 if (h == NULL)
320 char *p, *copy;
321 size_t len, first;
323 /* If this is a default version (the name contains @@),
324 look up the symbol again with only one `@' as well
325 as without the version. The effect is that references
326 to the symbol with and without the version will be
327 matched by the default symbol in the archive. */
329 p = strchr (symdef->name, ELF_VER_CHR);
330 if (p == NULL || p[1] != ELF_VER_CHR)
331 continue;
333 /* First check with only one `@'. */
334 len = strlen (symdef->name);
335 copy = bfd_alloc (abfd, (bfd_size_type) len);
336 if (copy == NULL)
337 goto error_return;
338 first = p - symdef->name + 1;
339 memcpy (copy, symdef->name, first);
340 memcpy (copy + first, symdef->name + first + 1, len - first);
342 h = elf_link_hash_lookup (elf_hash_table (info), copy,
343 false, false, false);
345 if (h == NULL)
347 /* We also need to check references to the symbol
348 without the version. */
350 copy[first - 1] = '\0';
351 h = elf_link_hash_lookup (elf_hash_table (info),
352 copy, false, false, false);
355 bfd_release (abfd, copy);
358 if (h == NULL)
359 continue;
361 if (h->root.type == bfd_link_hash_common)
363 /* We currently have a common symbol. The archive map contains
364 a reference to this symbol, so we may want to include it. We
365 only want to include it however, if this archive element
366 contains a definition of the symbol, not just another common
367 declaration of it.
369 Unfortunately some archivers (including GNU ar) will put
370 declarations of common symbols into their archive maps, as
371 well as real definitions, so we cannot just go by the archive
372 map alone. Instead we must read in the element's symbol
373 table and check that to see what kind of symbol definition
374 this is. */
375 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
376 continue;
378 else if (h->root.type != bfd_link_hash_undefined)
380 if (h->root.type != bfd_link_hash_undefweak)
381 defined[i] = true;
382 continue;
385 /* We need to include this archive member. */
386 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
387 if (element == (bfd *) NULL)
388 goto error_return;
390 if (! bfd_check_format (element, bfd_object))
391 goto error_return;
393 /* Doublecheck that we have not included this object
394 already--it should be impossible, but there may be
395 something wrong with the archive. */
396 if (element->archive_pass != 0)
398 bfd_set_error (bfd_error_bad_value);
399 goto error_return;
401 element->archive_pass = 1;
403 undefs_tail = info->hash->undefs_tail;
405 if (! (*info->callbacks->add_archive_element) (info, element,
406 symdef->name))
407 goto error_return;
408 if (! elf_link_add_object_symbols (element, info))
409 goto error_return;
411 /* If there are any new undefined symbols, we need to make
412 another pass through the archive in order to see whether
413 they can be defined. FIXME: This isn't perfect, because
414 common symbols wind up on undefs_tail and because an
415 undefined symbol which is defined later on in this pass
416 does not require another pass. This isn't a bug, but it
417 does make the code less efficient than it could be. */
418 if (undefs_tail != info->hash->undefs_tail)
419 loop = true;
421 /* Look backward to mark all symbols from this object file
422 which we have already seen in this pass. */
423 mark = i;
426 included[mark] = true;
427 if (mark == 0)
428 break;
429 --mark;
431 while (symdefs[mark].file_offset == symdef->file_offset);
433 /* We mark subsequent symbols from this object file as we go
434 on through the loop. */
435 last = symdef->file_offset;
438 while (loop);
440 free (defined);
441 free (included);
443 return true;
445 error_return:
446 if (defined != (boolean *) NULL)
447 free (defined);
448 if (included != (boolean *) NULL)
449 free (included);
450 return false;
453 /* This function is called when we want to define a new symbol. It
454 handles the various cases which arise when we find a definition in
455 a dynamic object, or when there is already a definition in a
456 dynamic object. The new symbol is described by NAME, SYM, PSEC,
457 and PVALUE. We set SYM_HASH to the hash table entry. We set
458 OVERRIDE if the old symbol is overriding a new definition. We set
459 TYPE_CHANGE_OK if it is OK for the type to change. We set
460 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
461 change, we mean that we shouldn't warn if the type or size does
462 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
463 a shared object. */
465 static boolean
466 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
467 override, type_change_ok, size_change_ok, dt_needed)
468 bfd *abfd;
469 struct bfd_link_info *info;
470 const char *name;
471 Elf_Internal_Sym *sym;
472 asection **psec;
473 bfd_vma *pvalue;
474 struct elf_link_hash_entry **sym_hash;
475 boolean *override;
476 boolean *type_change_ok;
477 boolean *size_change_ok;
478 boolean dt_needed;
480 asection *sec;
481 struct elf_link_hash_entry *h;
482 int bind;
483 bfd *oldbfd;
484 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
486 *override = false;
488 sec = *psec;
489 bind = ELF_ST_BIND (sym->st_info);
491 if (! bfd_is_und_section (sec))
492 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
493 else
494 h = ((struct elf_link_hash_entry *)
495 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
496 if (h == NULL)
497 return false;
498 *sym_hash = h;
500 /* This code is for coping with dynamic objects, and is only useful
501 if we are doing an ELF link. */
502 if (info->hash->creator != abfd->xvec)
503 return true;
505 /* For merging, we only care about real symbols. */
507 while (h->root.type == bfd_link_hash_indirect
508 || h->root.type == bfd_link_hash_warning)
509 h = (struct elf_link_hash_entry *) h->root.u.i.link;
511 /* If we just created the symbol, mark it as being an ELF symbol.
512 Other than that, there is nothing to do--there is no merge issue
513 with a newly defined symbol--so we just return. */
515 if (h->root.type == bfd_link_hash_new)
517 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
518 return true;
521 /* OLDBFD is a BFD associated with the existing symbol. */
523 switch (h->root.type)
525 default:
526 oldbfd = NULL;
527 break;
529 case bfd_link_hash_undefined:
530 case bfd_link_hash_undefweak:
531 oldbfd = h->root.u.undef.abfd;
532 break;
534 case bfd_link_hash_defined:
535 case bfd_link_hash_defweak:
536 oldbfd = h->root.u.def.section->owner;
537 break;
539 case bfd_link_hash_common:
540 oldbfd = h->root.u.c.p->section->owner;
541 break;
544 /* In cases involving weak versioned symbols, we may wind up trying
545 to merge a symbol with itself. Catch that here, to avoid the
546 confusion that results if we try to override a symbol with
547 itself. The additional tests catch cases like
548 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
549 dynamic object, which we do want to handle here. */
550 if (abfd == oldbfd
551 && ((abfd->flags & DYNAMIC) == 0
552 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
553 return true;
555 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
556 respectively, is from a dynamic object. */
558 if ((abfd->flags & DYNAMIC) != 0)
559 newdyn = true;
560 else
561 newdyn = false;
563 if (oldbfd != NULL)
564 olddyn = (oldbfd->flags & DYNAMIC) != 0;
565 else
567 asection *hsec;
569 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
570 indices used by MIPS ELF. */
571 switch (h->root.type)
573 default:
574 hsec = NULL;
575 break;
577 case bfd_link_hash_defined:
578 case bfd_link_hash_defweak:
579 hsec = h->root.u.def.section;
580 break;
582 case bfd_link_hash_common:
583 hsec = h->root.u.c.p->section;
584 break;
587 if (hsec == NULL)
588 olddyn = false;
589 else
590 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
593 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
594 respectively, appear to be a definition rather than reference. */
596 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
597 newdef = false;
598 else
599 newdef = true;
601 if (h->root.type == bfd_link_hash_undefined
602 || h->root.type == bfd_link_hash_undefweak
603 || h->root.type == bfd_link_hash_common)
604 olddef = false;
605 else
606 olddef = true;
608 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
609 symbol, respectively, appears to be a common symbol in a dynamic
610 object. If a symbol appears in an uninitialized section, and is
611 not weak, and is not a function, then it may be a common symbol
612 which was resolved when the dynamic object was created. We want
613 to treat such symbols specially, because they raise special
614 considerations when setting the symbol size: if the symbol
615 appears as a common symbol in a regular object, and the size in
616 the regular object is larger, we must make sure that we use the
617 larger size. This problematic case can always be avoided in C,
618 but it must be handled correctly when using Fortran shared
619 libraries.
621 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
622 likewise for OLDDYNCOMMON and OLDDEF.
624 Note that this test is just a heuristic, and that it is quite
625 possible to have an uninitialized symbol in a shared object which
626 is really a definition, rather than a common symbol. This could
627 lead to some minor confusion when the symbol really is a common
628 symbol in some regular object. However, I think it will be
629 harmless. */
631 if (newdyn
632 && newdef
633 && (sec->flags & SEC_ALLOC) != 0
634 && (sec->flags & SEC_LOAD) == 0
635 && sym->st_size > 0
636 && bind != STB_WEAK
637 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
638 newdyncommon = true;
639 else
640 newdyncommon = false;
642 if (olddyn
643 && olddef
644 && h->root.type == bfd_link_hash_defined
645 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
646 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
647 && (h->root.u.def.section->flags & SEC_LOAD) == 0
648 && h->size > 0
649 && h->type != STT_FUNC)
650 olddyncommon = true;
651 else
652 olddyncommon = false;
654 /* It's OK to change the type if either the existing symbol or the
655 new symbol is weak unless it comes from a DT_NEEDED entry of
656 a shared object, in which case, the DT_NEEDED entry may not be
657 required at the run time. */
659 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
660 || h->root.type == bfd_link_hash_undefweak
661 || bind == STB_WEAK)
662 *type_change_ok = true;
664 /* It's OK to change the size if either the existing symbol or the
665 new symbol is weak, or if the old symbol is undefined. */
667 if (*type_change_ok
668 || h->root.type == bfd_link_hash_undefined)
669 *size_change_ok = true;
671 /* If both the old and the new symbols look like common symbols in a
672 dynamic object, set the size of the symbol to the larger of the
673 two. */
675 if (olddyncommon
676 && newdyncommon
677 && sym->st_size != h->size)
679 /* Since we think we have two common symbols, issue a multiple
680 common warning if desired. Note that we only warn if the
681 size is different. If the size is the same, we simply let
682 the old symbol override the new one as normally happens with
683 symbols defined in dynamic objects. */
685 if (! ((*info->callbacks->multiple_common)
686 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
687 h->size, abfd, bfd_link_hash_common, sym->st_size)))
688 return false;
690 if (sym->st_size > h->size)
691 h->size = sym->st_size;
693 *size_change_ok = true;
696 /* If we are looking at a dynamic object, and we have found a
697 definition, we need to see if the symbol was already defined by
698 some other object. If so, we want to use the existing
699 definition, and we do not want to report a multiple symbol
700 definition error; we do this by clobbering *PSEC to be
701 bfd_und_section_ptr.
703 We treat a common symbol as a definition if the symbol in the
704 shared library is a function, since common symbols always
705 represent variables; this can cause confusion in principle, but
706 any such confusion would seem to indicate an erroneous program or
707 shared library. We also permit a common symbol in a regular
708 object to override a weak symbol in a shared object.
710 We prefer a non-weak definition in a shared library to a weak
711 definition in the executable unless it comes from a DT_NEEDED
712 entry of a shared object, in which case, the DT_NEEDED entry
713 may not be required at the run time. */
715 if (newdyn
716 && newdef
717 && (olddef
718 || (h->root.type == bfd_link_hash_common
719 && (bind == STB_WEAK
720 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
721 && (h->root.type != bfd_link_hash_defweak
722 || dt_needed
723 || bind == STB_WEAK))
725 *override = true;
726 newdef = false;
727 newdyncommon = false;
729 *psec = sec = bfd_und_section_ptr;
730 *size_change_ok = true;
732 /* If we get here when the old symbol is a common symbol, then
733 we are explicitly letting it override a weak symbol or
734 function in a dynamic object, and we don't want to warn about
735 a type change. If the old symbol is a defined symbol, a type
736 change warning may still be appropriate. */
738 if (h->root.type == bfd_link_hash_common)
739 *type_change_ok = true;
742 /* Handle the special case of an old common symbol merging with a
743 new symbol which looks like a common symbol in a shared object.
744 We change *PSEC and *PVALUE to make the new symbol look like a
745 common symbol, and let _bfd_generic_link_add_one_symbol will do
746 the right thing. */
748 if (newdyncommon
749 && h->root.type == bfd_link_hash_common)
751 *override = true;
752 newdef = false;
753 newdyncommon = false;
754 *pvalue = sym->st_size;
755 *psec = sec = bfd_com_section_ptr;
756 *size_change_ok = true;
759 /* If the old symbol is from a dynamic object, and the new symbol is
760 a definition which is not from a dynamic object, then the new
761 symbol overrides the old symbol. Symbols from regular files
762 always take precedence over symbols from dynamic objects, even if
763 they are defined after the dynamic object in the link.
765 As above, we again permit a common symbol in a regular object to
766 override a definition in a shared object if the shared object
767 symbol is a function or is weak.
769 As above, we permit a non-weak definition in a shared object to
770 override a weak definition in a regular object. */
772 if (! newdyn
773 && (newdef
774 || (bfd_is_com_section (sec)
775 && (h->root.type == bfd_link_hash_defweak
776 || h->type == STT_FUNC)))
777 && olddyn
778 && olddef
779 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
780 && (bind != STB_WEAK
781 || h->root.type == bfd_link_hash_defweak))
783 /* Change the hash table entry to undefined, and let
784 _bfd_generic_link_add_one_symbol do the right thing with the
785 new definition. */
787 h->root.type = bfd_link_hash_undefined;
788 h->root.u.undef.abfd = h->root.u.def.section->owner;
789 *size_change_ok = true;
791 olddef = false;
792 olddyncommon = false;
794 /* We again permit a type change when a common symbol may be
795 overriding a function. */
797 if (bfd_is_com_section (sec))
798 *type_change_ok = true;
800 /* This union may have been set to be non-NULL when this symbol
801 was seen in a dynamic object. We must force the union to be
802 NULL, so that it is correct for a regular symbol. */
804 h->verinfo.vertree = NULL;
806 /* In this special case, if H is the target of an indirection,
807 we want the caller to frob with H rather than with the
808 indirect symbol. That will permit the caller to redefine the
809 target of the indirection, rather than the indirect symbol
810 itself. FIXME: This will break the -y option if we store a
811 symbol with a different name. */
812 *sym_hash = h;
815 /* Handle the special case of a new common symbol merging with an
816 old symbol that looks like it might be a common symbol defined in
817 a shared object. Note that we have already handled the case in
818 which a new common symbol should simply override the definition
819 in the shared library. */
821 if (! newdyn
822 && bfd_is_com_section (sec)
823 && olddyncommon)
825 /* It would be best if we could set the hash table entry to a
826 common symbol, but we don't know what to use for the section
827 or the alignment. */
828 if (! ((*info->callbacks->multiple_common)
829 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
830 h->size, abfd, bfd_link_hash_common, sym->st_size)))
831 return false;
833 /* If the predumed common symbol in the dynamic object is
834 larger, pretend that the new symbol has its size. */
836 if (h->size > *pvalue)
837 *pvalue = h->size;
839 /* FIXME: We no longer know the alignment required by the symbol
840 in the dynamic object, so we just wind up using the one from
841 the regular object. */
843 olddef = false;
844 olddyncommon = false;
846 h->root.type = bfd_link_hash_undefined;
847 h->root.u.undef.abfd = h->root.u.def.section->owner;
849 *size_change_ok = true;
850 *type_change_ok = true;
852 h->verinfo.vertree = NULL;
855 /* Handle the special case of a weak definition in a regular object
856 followed by a non-weak definition in a shared object. In this
857 case, we prefer the definition in the shared object unless it
858 comes from a DT_NEEDED entry of a shared object, in which case,
859 the DT_NEEDED entry may not be required at the run time. */
860 if (olddef
861 && ! dt_needed
862 && h->root.type == bfd_link_hash_defweak
863 && newdef
864 && newdyn
865 && bind != STB_WEAK)
867 /* To make this work we have to frob the flags so that the rest
868 of the code does not think we are using the regular
869 definition. */
870 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
871 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
872 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
873 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
874 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
875 | ELF_LINK_HASH_DEF_DYNAMIC);
877 /* If H is the target of an indirection, we want the caller to
878 use H rather than the indirect symbol. Otherwise if we are
879 defining a new indirect symbol we will wind up attaching it
880 to the entry we are overriding. */
881 *sym_hash = h;
884 /* Handle the special case of a non-weak definition in a shared
885 object followed by a weak definition in a regular object. In
886 this case we prefer to definition in the shared object. To make
887 this work we have to tell the caller to not treat the new symbol
888 as a definition. */
889 if (olddef
890 && olddyn
891 && h->root.type != bfd_link_hash_defweak
892 && newdef
893 && ! newdyn
894 && bind == STB_WEAK)
895 *override = true;
897 return true;
900 /* This function is called to create an indirect symbol from the
901 default for the symbol with the default version if needed. The
902 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
903 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
904 indicates if it comes from a DT_NEEDED entry of a shared object. */
906 static boolean
907 elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
908 dynsym, override, dt_needed)
909 bfd *abfd;
910 struct bfd_link_info *info;
911 struct elf_link_hash_entry *h;
912 const char *name;
913 Elf_Internal_Sym *sym;
914 asection **psec;
915 bfd_vma *value;
916 boolean *dynsym;
917 boolean override;
918 boolean dt_needed;
920 boolean type_change_ok;
921 boolean size_change_ok;
922 char *shortname;
923 struct elf_link_hash_entry *hi;
924 struct bfd_link_hash_entry *bh;
925 struct elf_backend_data *bed;
926 boolean collect;
927 boolean dynamic;
928 char *p;
929 size_t len, shortlen;
930 asection *sec;
932 /* If this symbol has a version, and it is the default version, we
933 create an indirect symbol from the default name to the fully
934 decorated name. This will cause external references which do not
935 specify a version to be bound to this version of the symbol. */
936 p = strchr (name, ELF_VER_CHR);
937 if (p == NULL || p[1] != ELF_VER_CHR)
938 return true;
940 if (override)
942 /* We are overridden by an old defition. We need to check if we
943 need to create the indirect symbol from the default name. */
944 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
945 false, false);
946 BFD_ASSERT (hi != NULL);
947 if (hi == h)
948 return true;
949 while (hi->root.type == bfd_link_hash_indirect
950 || hi->root.type == bfd_link_hash_warning)
952 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
953 if (hi == h)
954 return true;
958 bed = get_elf_backend_data (abfd);
959 collect = bed->collect;
960 dynamic = (abfd->flags & DYNAMIC) != 0;
962 shortlen = p - name;
963 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
964 if (shortname == NULL)
965 return false;
966 memcpy (shortname, name, shortlen);
967 shortname[shortlen] = '\0';
969 /* We are going to create a new symbol. Merge it with any existing
970 symbol with this name. For the purposes of the merge, act as
971 though we were defining the symbol we just defined, although we
972 actually going to define an indirect symbol. */
973 type_change_ok = false;
974 size_change_ok = false;
975 sec = *psec;
976 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
977 &hi, &override, &type_change_ok,
978 &size_change_ok, dt_needed))
979 return false;
981 if (! override)
983 bh = &hi->root;
984 if (! (_bfd_generic_link_add_one_symbol
985 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
986 (bfd_vma) 0, name, false, collect, &bh)))
987 return false;
988 hi = (struct elf_link_hash_entry *) bh;
990 else
992 /* In this case the symbol named SHORTNAME is overriding the
993 indirect symbol we want to add. We were planning on making
994 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
995 is the name without a version. NAME is the fully versioned
996 name, and it is the default version.
998 Overriding means that we already saw a definition for the
999 symbol SHORTNAME in a regular object, and it is overriding
1000 the symbol defined in the dynamic object.
1002 When this happens, we actually want to change NAME, the
1003 symbol we just added, to refer to SHORTNAME. This will cause
1004 references to NAME in the shared object to become references
1005 to SHORTNAME in the regular object. This is what we expect
1006 when we override a function in a shared object: that the
1007 references in the shared object will be mapped to the
1008 definition in the regular object. */
1010 while (hi->root.type == bfd_link_hash_indirect
1011 || hi->root.type == bfd_link_hash_warning)
1012 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1014 h->root.type = bfd_link_hash_indirect;
1015 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1016 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1018 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1019 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1020 if (hi->elf_link_hash_flags
1021 & (ELF_LINK_HASH_REF_REGULAR
1022 | ELF_LINK_HASH_DEF_REGULAR))
1024 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1025 return false;
1029 /* Now set HI to H, so that the following code will set the
1030 other fields correctly. */
1031 hi = h;
1034 /* If there is a duplicate definition somewhere, then HI may not
1035 point to an indirect symbol. We will have reported an error to
1036 the user in that case. */
1038 if (hi->root.type == bfd_link_hash_indirect)
1040 struct elf_link_hash_entry *ht;
1042 /* If the symbol became indirect, then we assume that we have
1043 not seen a definition before. */
1044 BFD_ASSERT ((hi->elf_link_hash_flags
1045 & (ELF_LINK_HASH_DEF_DYNAMIC
1046 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1048 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1049 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1051 /* See if the new flags lead us to realize that the symbol must
1052 be dynamic. */
1053 if (! *dynsym)
1055 if (! dynamic)
1057 if (info->shared
1058 || ((hi->elf_link_hash_flags
1059 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1060 *dynsym = true;
1062 else
1064 if ((hi->elf_link_hash_flags
1065 & ELF_LINK_HASH_REF_REGULAR) != 0)
1066 *dynsym = true;
1071 /* We also need to define an indirection from the nondefault version
1072 of the symbol. */
1074 len = strlen (name);
1075 shortname = bfd_hash_allocate (&info->hash->table, len);
1076 if (shortname == NULL)
1077 return false;
1078 memcpy (shortname, name, shortlen);
1079 memcpy (shortname + shortlen, p + 1, len - shortlen);
1081 /* Once again, merge with any existing symbol. */
1082 type_change_ok = false;
1083 size_change_ok = false;
1084 sec = *psec;
1085 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1086 &hi, &override, &type_change_ok,
1087 &size_change_ok, dt_needed))
1088 return false;
1090 if (override)
1092 /* Here SHORTNAME is a versioned name, so we don't expect to see
1093 the type of override we do in the case above unless it is
1094 overridden by a versioned definiton. */
1095 if (hi->root.type != bfd_link_hash_defined
1096 && hi->root.type != bfd_link_hash_defweak)
1097 (*_bfd_error_handler)
1098 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1099 bfd_archive_filename (abfd), shortname);
1101 else
1103 bh = &hi->root;
1104 if (! (_bfd_generic_link_add_one_symbol
1105 (info, abfd, shortname, BSF_INDIRECT,
1106 bfd_ind_section_ptr, (bfd_vma) 0, name, false, collect, &bh)))
1107 return false;
1108 hi = (struct elf_link_hash_entry *) bh;
1110 /* If there is a duplicate definition somewhere, then HI may not
1111 point to an indirect symbol. We will have reported an error
1112 to the user in that case. */
1114 if (hi->root.type == bfd_link_hash_indirect)
1116 /* If the symbol became indirect, then we assume that we have
1117 not seen a definition before. */
1118 BFD_ASSERT ((hi->elf_link_hash_flags
1119 & (ELF_LINK_HASH_DEF_DYNAMIC
1120 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1122 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1124 /* See if the new flags lead us to realize that the symbol
1125 must be dynamic. */
1126 if (! *dynsym)
1128 if (! dynamic)
1130 if (info->shared
1131 || ((hi->elf_link_hash_flags
1132 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1133 *dynsym = true;
1135 else
1137 if ((hi->elf_link_hash_flags
1138 & ELF_LINK_HASH_REF_REGULAR) != 0)
1139 *dynsym = true;
1145 return true;
1148 /* Add symbols from an ELF object file to the linker hash table. */
1150 static boolean
1151 elf_link_add_object_symbols (abfd, info)
1152 bfd *abfd;
1153 struct bfd_link_info *info;
1155 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1156 const Elf_Internal_Sym *,
1157 const char **, flagword *,
1158 asection **, bfd_vma *));
1159 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1160 asection *, const Elf_Internal_Rela *));
1161 boolean collect;
1162 Elf_Internal_Shdr *hdr;
1163 bfd_size_type symcount;
1164 bfd_size_type extsymcount;
1165 bfd_size_type extsymoff;
1166 struct elf_link_hash_entry **sym_hash;
1167 boolean dynamic;
1168 Elf_External_Versym *extversym = NULL;
1169 Elf_External_Versym *ever;
1170 struct elf_link_hash_entry *weaks;
1171 Elf_Internal_Sym *isymbuf = NULL;
1172 Elf_Internal_Sym *isym;
1173 Elf_Internal_Sym *isymend;
1174 struct elf_backend_data *bed;
1175 boolean dt_needed;
1176 struct elf_link_hash_table * hash_table;
1177 bfd_size_type amt;
1179 hash_table = elf_hash_table (info);
1181 bed = get_elf_backend_data (abfd);
1182 add_symbol_hook = bed->elf_add_symbol_hook;
1183 collect = bed->collect;
1185 if ((abfd->flags & DYNAMIC) == 0)
1186 dynamic = false;
1187 else
1189 dynamic = true;
1191 /* You can't use -r against a dynamic object. Also, there's no
1192 hope of using a dynamic object which does not exactly match
1193 the format of the output file. */
1194 if (info->relocateable || info->hash->creator != abfd->xvec)
1196 bfd_set_error (bfd_error_invalid_operation);
1197 goto error_return;
1201 /* As a GNU extension, any input sections which are named
1202 .gnu.warning.SYMBOL are treated as warning symbols for the given
1203 symbol. This differs from .gnu.warning sections, which generate
1204 warnings when they are included in an output file. */
1205 if (! info->shared)
1207 asection *s;
1209 for (s = abfd->sections; s != NULL; s = s->next)
1211 const char *name;
1213 name = bfd_get_section_name (abfd, s);
1214 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1216 char *msg;
1217 bfd_size_type sz;
1219 name += sizeof ".gnu.warning." - 1;
1221 /* If this is a shared object, then look up the symbol
1222 in the hash table. If it is there, and it is already
1223 been defined, then we will not be using the entry
1224 from this shared object, so we don't need to warn.
1225 FIXME: If we see the definition in a regular object
1226 later on, we will warn, but we shouldn't. The only
1227 fix is to keep track of what warnings we are supposed
1228 to emit, and then handle them all at the end of the
1229 link. */
1230 if (dynamic && abfd->xvec == info->hash->creator)
1232 struct elf_link_hash_entry *h;
1234 h = elf_link_hash_lookup (hash_table, name,
1235 false, false, true);
1237 /* FIXME: What about bfd_link_hash_common? */
1238 if (h != NULL
1239 && (h->root.type == bfd_link_hash_defined
1240 || h->root.type == bfd_link_hash_defweak))
1242 /* We don't want to issue this warning. Clobber
1243 the section size so that the warning does not
1244 get copied into the output file. */
1245 s->_raw_size = 0;
1246 continue;
1250 sz = bfd_section_size (abfd, s);
1251 msg = (char *) bfd_alloc (abfd, sz + 1);
1252 if (msg == NULL)
1253 goto error_return;
1255 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1256 goto error_return;
1258 msg[sz] = '\0';
1260 if (! (_bfd_generic_link_add_one_symbol
1261 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1262 false, collect, (struct bfd_link_hash_entry **) NULL)))
1263 goto error_return;
1265 if (! info->relocateable)
1267 /* Clobber the section size so that the warning does
1268 not get copied into the output file. */
1269 s->_raw_size = 0;
1275 dt_needed = false;
1276 if (! dynamic)
1278 /* If we are creating a shared library, create all the dynamic
1279 sections immediately. We need to attach them to something,
1280 so we attach them to this BFD, provided it is the right
1281 format. FIXME: If there are no input BFD's of the same
1282 format as the output, we can't make a shared library. */
1283 if (info->shared
1284 && is_elf_hash_table (info)
1285 && ! hash_table->dynamic_sections_created
1286 && abfd->xvec == info->hash->creator)
1288 if (! elf_link_create_dynamic_sections (abfd, info))
1289 goto error_return;
1292 else if (! is_elf_hash_table (info))
1293 goto error_return;
1294 else
1296 asection *s;
1297 boolean add_needed;
1298 const char *name;
1299 bfd_size_type oldsize;
1300 bfd_size_type strindex;
1301 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
1303 /* ld --just-symbols and dynamic objects don't mix very well.
1304 Test for --just-symbols by looking at info set up by
1305 _bfd_elf_link_just_syms. */
1306 if ((s = abfd->sections) != NULL
1307 && elf_section_data (s)->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
1308 goto error_return;
1310 /* Find the name to use in a DT_NEEDED entry that refers to this
1311 object. If the object has a DT_SONAME entry, we use it.
1312 Otherwise, if the generic linker stuck something in
1313 elf_dt_name, we use that. Otherwise, we just use the file
1314 name. If the generic linker put a null string into
1315 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1316 there is a DT_SONAME entry. */
1317 add_needed = true;
1318 name = bfd_get_filename (abfd);
1319 if (elf_dt_name (abfd) != NULL)
1321 name = elf_dt_name (abfd);
1322 if (*name == '\0')
1324 if (elf_dt_soname (abfd) != NULL)
1325 dt_needed = true;
1327 add_needed = false;
1330 s = bfd_get_section_by_name (abfd, ".dynamic");
1331 if (s != NULL)
1333 Elf_External_Dyn *dynbuf = NULL;
1334 Elf_External_Dyn *extdyn;
1335 Elf_External_Dyn *extdynend;
1336 int elfsec;
1337 unsigned long shlink;
1339 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1340 if (dynbuf == NULL)
1341 goto error_return;
1343 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1344 (file_ptr) 0, s->_raw_size))
1345 goto error_free_dyn;
1347 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1348 if (elfsec == -1)
1349 goto error_free_dyn;
1350 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1352 extdyn = dynbuf;
1353 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1354 for (; extdyn < extdynend; extdyn++)
1356 Elf_Internal_Dyn dyn;
1358 elf_swap_dyn_in (abfd, extdyn, &dyn);
1359 if (dyn.d_tag == DT_SONAME)
1361 unsigned int tagv = dyn.d_un.d_val;
1362 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1363 if (name == NULL)
1364 goto error_free_dyn;
1366 if (dyn.d_tag == DT_NEEDED)
1368 struct bfd_link_needed_list *n, **pn;
1369 char *fnm, *anm;
1370 unsigned int tagv = dyn.d_un.d_val;
1372 amt = sizeof (struct bfd_link_needed_list);
1373 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1374 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1375 if (n == NULL || fnm == NULL)
1376 goto error_free_dyn;
1377 amt = strlen (fnm) + 1;
1378 anm = bfd_alloc (abfd, amt);
1379 if (anm == NULL)
1380 goto error_free_dyn;
1381 memcpy (anm, fnm, (size_t) amt);
1382 n->name = anm;
1383 n->by = abfd;
1384 n->next = NULL;
1385 for (pn = & hash_table->needed;
1386 *pn != NULL;
1387 pn = &(*pn)->next)
1389 *pn = n;
1391 if (dyn.d_tag == DT_RUNPATH)
1393 struct bfd_link_needed_list *n, **pn;
1394 char *fnm, *anm;
1395 unsigned int tagv = dyn.d_un.d_val;
1397 amt = sizeof (struct bfd_link_needed_list);
1398 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1399 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1400 if (n == NULL || fnm == NULL)
1401 goto error_free_dyn;
1402 amt = strlen (fnm) + 1;
1403 anm = bfd_alloc (abfd, amt);
1404 if (anm == NULL)
1405 goto error_free_dyn;
1406 memcpy (anm, fnm, (size_t) amt);
1407 n->name = anm;
1408 n->by = abfd;
1409 n->next = NULL;
1410 for (pn = & runpath;
1411 *pn != NULL;
1412 pn = &(*pn)->next)
1414 *pn = n;
1416 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1417 if (!runpath && dyn.d_tag == DT_RPATH)
1419 struct bfd_link_needed_list *n, **pn;
1420 char *fnm, *anm;
1421 unsigned int tagv = dyn.d_un.d_val;
1423 amt = sizeof (struct bfd_link_needed_list);
1424 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1425 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1426 if (n == NULL || fnm == NULL)
1427 goto error_free_dyn;
1428 amt = strlen (fnm) + 1;
1429 anm = bfd_alloc (abfd, amt);
1430 if (anm == NULL)
1432 error_free_dyn:
1433 free (dynbuf);
1434 goto error_return;
1436 memcpy (anm, fnm, (size_t) amt);
1437 n->name = anm;
1438 n->by = abfd;
1439 n->next = NULL;
1440 for (pn = & rpath;
1441 *pn != NULL;
1442 pn = &(*pn)->next)
1444 *pn = n;
1448 free (dynbuf);
1451 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1452 frees all more recently bfd_alloc'd blocks as well. */
1453 if (runpath)
1454 rpath = runpath;
1456 if (rpath)
1458 struct bfd_link_needed_list **pn;
1459 for (pn = & hash_table->runpath;
1460 *pn != NULL;
1461 pn = &(*pn)->next)
1463 *pn = rpath;
1466 /* We do not want to include any of the sections in a dynamic
1467 object in the output file. We hack by simply clobbering the
1468 list of sections in the BFD. This could be handled more
1469 cleanly by, say, a new section flag; the existing
1470 SEC_NEVER_LOAD flag is not the one we want, because that one
1471 still implies that the section takes up space in the output
1472 file. */
1473 bfd_section_list_clear (abfd);
1475 /* If this is the first dynamic object found in the link, create
1476 the special sections required for dynamic linking. */
1477 if (! hash_table->dynamic_sections_created)
1478 if (! elf_link_create_dynamic_sections (abfd, info))
1479 goto error_return;
1481 if (add_needed)
1483 /* Add a DT_NEEDED entry for this dynamic object. */
1484 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1485 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1486 if (strindex == (bfd_size_type) -1)
1487 goto error_return;
1489 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1491 asection *sdyn;
1492 Elf_External_Dyn *dyncon, *dynconend;
1494 /* The hash table size did not change, which means that
1495 the dynamic object name was already entered. If we
1496 have already included this dynamic object in the
1497 link, just ignore it. There is no reason to include
1498 a particular dynamic object more than once. */
1499 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1500 BFD_ASSERT (sdyn != NULL);
1502 dyncon = (Elf_External_Dyn *) sdyn->contents;
1503 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1504 sdyn->_raw_size);
1505 for (; dyncon < dynconend; dyncon++)
1507 Elf_Internal_Dyn dyn;
1509 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1510 if (dyn.d_tag == DT_NEEDED
1511 && dyn.d_un.d_val == strindex)
1513 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1514 return true;
1519 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1520 goto error_return;
1523 /* Save the SONAME, if there is one, because sometimes the
1524 linker emulation code will need to know it. */
1525 if (*name == '\0')
1526 name = basename (bfd_get_filename (abfd));
1527 elf_dt_name (abfd) = name;
1530 /* If this is a dynamic object, we always link against the .dynsym
1531 symbol table, not the .symtab symbol table. The dynamic linker
1532 will only see the .dynsym symbol table, so there is no reason to
1533 look at .symtab for a dynamic object. */
1535 if (! dynamic || elf_dynsymtab (abfd) == 0)
1536 hdr = &elf_tdata (abfd)->symtab_hdr;
1537 else
1538 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1540 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1542 /* The sh_info field of the symtab header tells us where the
1543 external symbols start. We don't care about the local symbols at
1544 this point. */
1545 if (elf_bad_symtab (abfd))
1547 extsymcount = symcount;
1548 extsymoff = 0;
1550 else
1552 extsymcount = symcount - hdr->sh_info;
1553 extsymoff = hdr->sh_info;
1556 sym_hash = NULL;
1557 if (extsymcount != 0)
1559 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
1560 NULL, NULL, NULL);
1561 if (isymbuf == NULL)
1562 goto error_return;
1564 /* We store a pointer to the hash table entry for each external
1565 symbol. */
1566 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1567 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1568 if (sym_hash == NULL)
1569 goto error_free_sym;
1570 elf_sym_hashes (abfd) = sym_hash;
1573 if (dynamic)
1575 /* Read in any version definitions. */
1576 if (! _bfd_elf_slurp_version_tables (abfd))
1577 goto error_free_sym;
1579 /* Read in the symbol versions, but don't bother to convert them
1580 to internal format. */
1581 if (elf_dynversym (abfd) != 0)
1583 Elf_Internal_Shdr *versymhdr;
1585 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1586 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1587 if (extversym == NULL)
1588 goto error_free_sym;
1589 amt = versymhdr->sh_size;
1590 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1591 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1592 goto error_free_vers;
1596 weaks = NULL;
1598 ever = extversym != NULL ? extversym + extsymoff : NULL;
1599 for (isym = isymbuf, isymend = isymbuf + extsymcount;
1600 isym < isymend;
1601 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1603 int bind;
1604 bfd_vma value;
1605 asection *sec;
1606 flagword flags;
1607 const char *name;
1608 struct elf_link_hash_entry *h;
1609 boolean definition;
1610 boolean size_change_ok, type_change_ok;
1611 boolean new_weakdef;
1612 unsigned int old_alignment;
1613 boolean override;
1615 override = false;
1617 flags = BSF_NO_FLAGS;
1618 sec = NULL;
1619 value = isym->st_value;
1620 *sym_hash = NULL;
1622 bind = ELF_ST_BIND (isym->st_info);
1623 if (bind == STB_LOCAL)
1625 /* This should be impossible, since ELF requires that all
1626 global symbols follow all local symbols, and that sh_info
1627 point to the first global symbol. Unfortunatealy, Irix 5
1628 screws this up. */
1629 continue;
1631 else if (bind == STB_GLOBAL)
1633 if (isym->st_shndx != SHN_UNDEF
1634 && isym->st_shndx != SHN_COMMON)
1635 flags = BSF_GLOBAL;
1637 else if (bind == STB_WEAK)
1638 flags = BSF_WEAK;
1639 else
1641 /* Leave it up to the processor backend. */
1644 if (isym->st_shndx == SHN_UNDEF)
1645 sec = bfd_und_section_ptr;
1646 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
1648 sec = section_from_elf_index (abfd, isym->st_shndx);
1649 if (sec == NULL)
1650 sec = bfd_abs_section_ptr;
1651 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1652 value -= sec->vma;
1654 else if (isym->st_shndx == SHN_ABS)
1655 sec = bfd_abs_section_ptr;
1656 else if (isym->st_shndx == SHN_COMMON)
1658 sec = bfd_com_section_ptr;
1659 /* What ELF calls the size we call the value. What ELF
1660 calls the value we call the alignment. */
1661 value = isym->st_size;
1663 else
1665 /* Leave it up to the processor backend. */
1668 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
1669 isym->st_name);
1670 if (name == (const char *) NULL)
1671 goto error_free_vers;
1673 if (isym->st_shndx == SHN_COMMON
1674 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
1676 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1678 if (tcomm == NULL)
1680 tcomm = bfd_make_section (abfd, ".tcommon");
1681 if (tcomm == NULL
1682 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1683 | SEC_IS_COMMON
1684 | SEC_LINKER_CREATED
1685 | SEC_THREAD_LOCAL)))
1686 goto error_free_vers;
1688 sec = tcomm;
1690 else if (add_symbol_hook)
1692 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
1693 &value))
1694 goto error_free_vers;
1696 /* The hook function sets the name to NULL if this symbol
1697 should be skipped for some reason. */
1698 if (name == (const char *) NULL)
1699 continue;
1702 /* Sanity check that all possibilities were handled. */
1703 if (sec == (asection *) NULL)
1705 bfd_set_error (bfd_error_bad_value);
1706 goto error_free_vers;
1709 if (bfd_is_und_section (sec)
1710 || bfd_is_com_section (sec))
1711 definition = false;
1712 else
1713 definition = true;
1715 size_change_ok = false;
1716 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1717 old_alignment = 0;
1718 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1720 Elf_Internal_Versym iver;
1721 unsigned int vernum = 0;
1723 if (ever != NULL)
1725 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1726 vernum = iver.vs_vers & VERSYM_VERSION;
1728 /* If this is a hidden symbol, or if it is not version
1729 1, we append the version name to the symbol name.
1730 However, we do not modify a non-hidden absolute
1731 symbol, because it might be the version symbol
1732 itself. FIXME: What if it isn't? */
1733 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1734 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1736 const char *verstr;
1737 size_t namelen, verlen, newlen;
1738 char *newname, *p;
1740 if (isym->st_shndx != SHN_UNDEF)
1742 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1744 (*_bfd_error_handler)
1745 (_("%s: %s: invalid version %u (max %d)"),
1746 bfd_archive_filename (abfd), name, vernum,
1747 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1748 bfd_set_error (bfd_error_bad_value);
1749 goto error_free_vers;
1751 else if (vernum > 1)
1752 verstr =
1753 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1754 else
1755 verstr = "";
1757 else
1759 /* We cannot simply test for the number of
1760 entries in the VERNEED section since the
1761 numbers for the needed versions do not start
1762 at 0. */
1763 Elf_Internal_Verneed *t;
1765 verstr = NULL;
1766 for (t = elf_tdata (abfd)->verref;
1767 t != NULL;
1768 t = t->vn_nextref)
1770 Elf_Internal_Vernaux *a;
1772 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1774 if (a->vna_other == vernum)
1776 verstr = a->vna_nodename;
1777 break;
1780 if (a != NULL)
1781 break;
1783 if (verstr == NULL)
1785 (*_bfd_error_handler)
1786 (_("%s: %s: invalid needed version %d"),
1787 bfd_archive_filename (abfd), name, vernum);
1788 bfd_set_error (bfd_error_bad_value);
1789 goto error_free_vers;
1793 namelen = strlen (name);
1794 verlen = strlen (verstr);
1795 newlen = namelen + verlen + 2;
1796 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1797 && isym->st_shndx != SHN_UNDEF)
1798 ++newlen;
1800 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
1801 if (newname == NULL)
1802 goto error_free_vers;
1803 memcpy (newname, name, namelen);
1804 p = newname + namelen;
1805 *p++ = ELF_VER_CHR;
1806 /* If this is a defined non-hidden version symbol,
1807 we add another @ to the name. This indicates the
1808 default version of the symbol. */
1809 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1810 && isym->st_shndx != SHN_UNDEF)
1811 *p++ = ELF_VER_CHR;
1812 memcpy (p, verstr, verlen + 1);
1814 name = newname;
1818 if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1819 sym_hash, &override, &type_change_ok,
1820 &size_change_ok, dt_needed))
1821 goto error_free_vers;
1823 if (override)
1824 definition = false;
1826 h = *sym_hash;
1827 while (h->root.type == bfd_link_hash_indirect
1828 || h->root.type == bfd_link_hash_warning)
1829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1831 /* Remember the old alignment if this is a common symbol, so
1832 that we don't reduce the alignment later on. We can't
1833 check later, because _bfd_generic_link_add_one_symbol
1834 will set a default for the alignment which we want to
1835 override. */
1836 if (h->root.type == bfd_link_hash_common)
1837 old_alignment = h->root.u.c.p->alignment_power;
1839 if (elf_tdata (abfd)->verdef != NULL
1840 && ! override
1841 && vernum > 1
1842 && definition)
1843 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1846 if (! (_bfd_generic_link_add_one_symbol
1847 (info, abfd, name, flags, sec, value, (const char *) NULL,
1848 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1849 goto error_free_vers;
1851 h = *sym_hash;
1852 while (h->root.type == bfd_link_hash_indirect
1853 || h->root.type == bfd_link_hash_warning)
1854 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1855 *sym_hash = h;
1857 new_weakdef = false;
1858 if (dynamic
1859 && definition
1860 && (flags & BSF_WEAK) != 0
1861 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
1862 && info->hash->creator->flavour == bfd_target_elf_flavour
1863 && h->weakdef == NULL)
1865 /* Keep a list of all weak defined non function symbols from
1866 a dynamic object, using the weakdef field. Later in this
1867 function we will set the weakdef field to the correct
1868 value. We only put non-function symbols from dynamic
1869 objects on this list, because that happens to be the only
1870 time we need to know the normal symbol corresponding to a
1871 weak symbol, and the information is time consuming to
1872 figure out. If the weakdef field is not already NULL,
1873 then this symbol was already defined by some previous
1874 dynamic object, and we will be using that previous
1875 definition anyhow. */
1877 h->weakdef = weaks;
1878 weaks = h;
1879 new_weakdef = true;
1882 /* Set the alignment of a common symbol. */
1883 if (isym->st_shndx == SHN_COMMON
1884 && h->root.type == bfd_link_hash_common)
1886 unsigned int align;
1888 align = bfd_log2 (isym->st_value);
1889 if (align > old_alignment
1890 /* Permit an alignment power of zero if an alignment of one
1891 is specified and no other alignments have been specified. */
1892 || (isym->st_value == 1 && old_alignment == 0))
1893 h->root.u.c.p->alignment_power = align;
1896 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1898 int old_flags;
1899 boolean dynsym;
1900 int new_flag;
1902 /* Remember the symbol size and type. */
1903 if (isym->st_size != 0
1904 && (definition || h->size == 0))
1906 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
1907 (*_bfd_error_handler)
1908 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1909 name, (unsigned long) h->size,
1910 (unsigned long) isym->st_size, bfd_archive_filename (abfd));
1912 h->size = isym->st_size;
1915 /* If this is a common symbol, then we always want H->SIZE
1916 to be the size of the common symbol. The code just above
1917 won't fix the size if a common symbol becomes larger. We
1918 don't warn about a size change here, because that is
1919 covered by --warn-common. */
1920 if (h->root.type == bfd_link_hash_common)
1921 h->size = h->root.u.c.size;
1923 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
1924 && (definition || h->type == STT_NOTYPE))
1926 if (h->type != STT_NOTYPE
1927 && h->type != ELF_ST_TYPE (isym->st_info)
1928 && ! type_change_ok)
1929 (*_bfd_error_handler)
1930 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1931 name, h->type, ELF_ST_TYPE (isym->st_info),
1932 bfd_archive_filename (abfd));
1934 h->type = ELF_ST_TYPE (isym->st_info);
1937 /* If st_other has a processor-specific meaning, specific code
1938 might be needed here. */
1939 if (isym->st_other != 0)
1941 /* Combine visibilities, using the most constraining one. */
1942 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1943 unsigned char symvis = ELF_ST_VISIBILITY (isym->st_other);
1945 if (symvis && (hvis > symvis || hvis == 0))
1946 h->other = isym->st_other;
1948 /* If neither has visibility, use the st_other of the
1949 definition. This is an arbitrary choice, since the
1950 other bits have no general meaning. */
1951 if (!symvis && !hvis
1952 && (definition || h->other == 0))
1953 h->other = isym->st_other;
1956 /* Set a flag in the hash table entry indicating the type of
1957 reference or definition we just found. Keep a count of
1958 the number of dynamic symbols we find. A dynamic symbol
1959 is one which is referenced or defined by both a regular
1960 object and a shared object. */
1961 old_flags = h->elf_link_hash_flags;
1962 dynsym = false;
1963 if (! dynamic)
1965 if (! definition)
1967 new_flag = ELF_LINK_HASH_REF_REGULAR;
1968 if (bind != STB_WEAK)
1969 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1971 else
1972 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1973 if (info->shared
1974 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1975 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1976 dynsym = true;
1978 else
1980 if (! definition)
1981 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1982 else
1983 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1984 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1985 | ELF_LINK_HASH_REF_REGULAR)) != 0
1986 || (h->weakdef != NULL
1987 && ! new_weakdef
1988 && h->weakdef->dynindx != -1))
1989 dynsym = true;
1992 h->elf_link_hash_flags |= new_flag;
1994 /* Check to see if we need to add an indirect symbol for
1995 the default name. */
1996 if (definition || h->root.type == bfd_link_hash_common)
1997 if (! elf_add_default_symbol (abfd, info, h, name, isym,
1998 &sec, &value, &dynsym,
1999 override, dt_needed))
2000 goto error_free_vers;
2002 if (dynsym && h->dynindx == -1)
2004 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2005 goto error_free_vers;
2006 if (h->weakdef != NULL
2007 && ! new_weakdef
2008 && h->weakdef->dynindx == -1)
2010 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2011 goto error_free_vers;
2014 else if (dynsym && h->dynindx != -1)
2015 /* If the symbol already has a dynamic index, but
2016 visibility says it should not be visible, turn it into
2017 a local symbol. */
2018 switch (ELF_ST_VISIBILITY (h->other))
2020 case STV_INTERNAL:
2021 case STV_HIDDEN:
2022 (*bed->elf_backend_hide_symbol) (info, h, true);
2023 break;
2026 if (dt_needed && definition
2027 && (h->elf_link_hash_flags
2028 & ELF_LINK_HASH_REF_REGULAR) != 0)
2030 bfd_size_type oldsize;
2031 bfd_size_type strindex;
2033 if (! is_elf_hash_table (info))
2034 goto error_free_vers;
2036 /* The symbol from a DT_NEEDED object is referenced from
2037 the regular object to create a dynamic executable. We
2038 have to make sure there is a DT_NEEDED entry for it. */
2040 dt_needed = false;
2041 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2042 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2043 elf_dt_soname (abfd), false);
2044 if (strindex == (bfd_size_type) -1)
2045 goto error_free_vers;
2047 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2049 asection *sdyn;
2050 Elf_External_Dyn *dyncon, *dynconend;
2052 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2053 ".dynamic");
2054 BFD_ASSERT (sdyn != NULL);
2056 dyncon = (Elf_External_Dyn *) sdyn->contents;
2057 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2058 sdyn->_raw_size);
2059 for (; dyncon < dynconend; dyncon++)
2061 Elf_Internal_Dyn dyn;
2063 elf_swap_dyn_in (hash_table->dynobj,
2064 dyncon, &dyn);
2065 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2066 dyn.d_un.d_val != strindex);
2070 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2071 goto error_free_vers;
2076 if (extversym != NULL)
2078 free (extversym);
2079 extversym = NULL;
2082 if (isymbuf != NULL)
2083 free (isymbuf);
2084 isymbuf = NULL;
2086 /* Now set the weakdefs field correctly for all the weak defined
2087 symbols we found. The only way to do this is to search all the
2088 symbols. Since we only need the information for non functions in
2089 dynamic objects, that's the only time we actually put anything on
2090 the list WEAKS. We need this information so that if a regular
2091 object refers to a symbol defined weakly in a dynamic object, the
2092 real symbol in the dynamic object is also put in the dynamic
2093 symbols; we also must arrange for both symbols to point to the
2094 same memory location. We could handle the general case of symbol
2095 aliasing, but a general symbol alias can only be generated in
2096 assembler code, handling it correctly would be very time
2097 consuming, and other ELF linkers don't handle general aliasing
2098 either. */
2099 while (weaks != NULL)
2101 struct elf_link_hash_entry *hlook;
2102 asection *slook;
2103 bfd_vma vlook;
2104 struct elf_link_hash_entry **hpp;
2105 struct elf_link_hash_entry **hppend;
2107 hlook = weaks;
2108 weaks = hlook->weakdef;
2109 hlook->weakdef = NULL;
2111 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2112 || hlook->root.type == bfd_link_hash_defweak
2113 || hlook->root.type == bfd_link_hash_common
2114 || hlook->root.type == bfd_link_hash_indirect);
2115 slook = hlook->root.u.def.section;
2116 vlook = hlook->root.u.def.value;
2118 hpp = elf_sym_hashes (abfd);
2119 hppend = hpp + extsymcount;
2120 for (; hpp < hppend; hpp++)
2122 struct elf_link_hash_entry *h;
2124 h = *hpp;
2125 if (h != NULL && h != hlook
2126 && h->root.type == bfd_link_hash_defined
2127 && h->root.u.def.section == slook
2128 && h->root.u.def.value == vlook)
2130 hlook->weakdef = h;
2132 /* If the weak definition is in the list of dynamic
2133 symbols, make sure the real definition is put there
2134 as well. */
2135 if (hlook->dynindx != -1
2136 && h->dynindx == -1)
2138 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2139 goto error_return;
2142 /* If the real definition is in the list of dynamic
2143 symbols, make sure the weak definition is put there
2144 as well. If we don't do this, then the dynamic
2145 loader might not merge the entries for the real
2146 definition and the weak definition. */
2147 if (h->dynindx != -1
2148 && hlook->dynindx == -1)
2150 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2151 goto error_return;
2153 break;
2158 /* If this object is the same format as the output object, and it is
2159 not a shared library, then let the backend look through the
2160 relocs.
2162 This is required to build global offset table entries and to
2163 arrange for dynamic relocs. It is not required for the
2164 particular common case of linking non PIC code, even when linking
2165 against shared libraries, but unfortunately there is no way of
2166 knowing whether an object file has been compiled PIC or not.
2167 Looking through the relocs is not particularly time consuming.
2168 The problem is that we must either (1) keep the relocs in memory,
2169 which causes the linker to require additional runtime memory or
2170 (2) read the relocs twice from the input file, which wastes time.
2171 This would be a good case for using mmap.
2173 I have no idea how to handle linking PIC code into a file of a
2174 different format. It probably can't be done. */
2175 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2176 if (! dynamic
2177 && abfd->xvec == info->hash->creator
2178 && check_relocs != NULL)
2180 asection *o;
2182 for (o = abfd->sections; o != NULL; o = o->next)
2184 Elf_Internal_Rela *internal_relocs;
2185 boolean ok;
2187 if ((o->flags & SEC_RELOC) == 0
2188 || o->reloc_count == 0
2189 || ((info->strip == strip_all || info->strip == strip_debugger)
2190 && (o->flags & SEC_DEBUGGING) != 0)
2191 || bfd_is_abs_section (o->output_section))
2192 continue;
2194 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2195 (abfd, o, (PTR) NULL,
2196 (Elf_Internal_Rela *) NULL,
2197 info->keep_memory));
2198 if (internal_relocs == NULL)
2199 goto error_return;
2201 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2203 if (elf_section_data (o)->relocs != internal_relocs)
2204 free (internal_relocs);
2206 if (! ok)
2207 goto error_return;
2211 /* If this is a non-traditional, non-relocateable link, try to
2212 optimize the handling of the .stab/.stabstr sections. */
2213 if (! dynamic
2214 && ! info->relocateable
2215 && ! info->traditional_format
2216 && info->hash->creator->flavour == bfd_target_elf_flavour
2217 && is_elf_hash_table (info)
2218 && (info->strip != strip_all && info->strip != strip_debugger))
2220 asection *stab, *stabstr;
2222 stab = bfd_get_section_by_name (abfd, ".stab");
2223 if (stab != NULL
2224 && (stab->flags & SEC_MERGE) == 0
2225 && !bfd_is_abs_section (stab->output_section))
2227 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2229 if (stabstr != NULL)
2231 struct bfd_elf_section_data *secdata;
2233 secdata = elf_section_data (stab);
2234 if (! _bfd_link_section_stabs (abfd,
2235 & hash_table->stab_info,
2236 stab, stabstr,
2237 &secdata->sec_info))
2238 goto error_return;
2239 if (secdata->sec_info)
2240 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2245 if (! info->relocateable && ! dynamic
2246 && is_elf_hash_table (info))
2248 asection *s;
2250 for (s = abfd->sections; s != NULL; s = s->next)
2251 if ((s->flags & SEC_MERGE) != 0
2252 && !bfd_is_abs_section (s->output_section))
2254 struct bfd_elf_section_data *secdata;
2256 secdata = elf_section_data (s);
2257 if (! _bfd_merge_section (abfd,
2258 & hash_table->merge_info,
2259 s, &secdata->sec_info))
2260 goto error_return;
2261 else if (secdata->sec_info)
2262 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2266 if (is_elf_hash_table (info))
2268 /* Add this bfd to the loaded list. */
2269 struct elf_link_loaded_list *n;
2271 n = ((struct elf_link_loaded_list *)
2272 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
2273 if (n == NULL)
2274 goto error_return;
2275 n->abfd = abfd;
2276 n->next = hash_table->loaded;
2277 hash_table->loaded = n;
2280 return true;
2282 error_free_vers:
2283 if (extversym != NULL)
2284 free (extversym);
2285 error_free_sym:
2286 if (isymbuf != NULL)
2287 free (isymbuf);
2288 error_return:
2289 return false;
2292 /* Create some sections which will be filled in with dynamic linking
2293 information. ABFD is an input file which requires dynamic sections
2294 to be created. The dynamic sections take up virtual memory space
2295 when the final executable is run, so we need to create them before
2296 addresses are assigned to the output sections. We work out the
2297 actual contents and size of these sections later. */
2299 boolean
2300 elf_link_create_dynamic_sections (abfd, info)
2301 bfd *abfd;
2302 struct bfd_link_info *info;
2304 flagword flags;
2305 register asection *s;
2306 struct elf_link_hash_entry *h;
2307 struct bfd_link_hash_entry *bh;
2308 struct elf_backend_data *bed;
2310 if (! is_elf_hash_table (info))
2311 return false;
2313 if (elf_hash_table (info)->dynamic_sections_created)
2314 return true;
2316 /* Make sure that all dynamic sections use the same input BFD. */
2317 if (elf_hash_table (info)->dynobj == NULL)
2318 elf_hash_table (info)->dynobj = abfd;
2319 else
2320 abfd = elf_hash_table (info)->dynobj;
2322 /* Note that we set the SEC_IN_MEMORY flag for all of these
2323 sections. */
2324 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2325 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2327 /* A dynamically linked executable has a .interp section, but a
2328 shared library does not. */
2329 if (! info->shared)
2331 s = bfd_make_section (abfd, ".interp");
2332 if (s == NULL
2333 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2334 return false;
2337 if (! info->traditional_format
2338 && info->hash->creator->flavour == bfd_target_elf_flavour)
2340 s = bfd_make_section (abfd, ".eh_frame_hdr");
2341 if (s == NULL
2342 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2343 || ! bfd_set_section_alignment (abfd, s, 2))
2344 return false;
2347 /* Create sections to hold version informations. These are removed
2348 if they are not needed. */
2349 s = bfd_make_section (abfd, ".gnu.version_d");
2350 if (s == NULL
2351 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2352 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2353 return false;
2355 s = bfd_make_section (abfd, ".gnu.version");
2356 if (s == NULL
2357 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2358 || ! bfd_set_section_alignment (abfd, s, 1))
2359 return false;
2361 s = bfd_make_section (abfd, ".gnu.version_r");
2362 if (s == NULL
2363 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2364 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2365 return false;
2367 s = bfd_make_section (abfd, ".dynsym");
2368 if (s == NULL
2369 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2370 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2371 return false;
2373 s = bfd_make_section (abfd, ".dynstr");
2374 if (s == NULL
2375 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2376 return false;
2378 /* Create a strtab to hold the dynamic symbol names. */
2379 if (elf_hash_table (info)->dynstr == NULL)
2381 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2382 if (elf_hash_table (info)->dynstr == NULL)
2383 return false;
2386 s = bfd_make_section (abfd, ".dynamic");
2387 if (s == NULL
2388 || ! bfd_set_section_flags (abfd, s, flags)
2389 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2390 return false;
2392 /* The special symbol _DYNAMIC is always set to the start of the
2393 .dynamic section. This call occurs before we have processed the
2394 symbols for any dynamic object, so we don't have to worry about
2395 overriding a dynamic definition. We could set _DYNAMIC in a
2396 linker script, but we only want to define it if we are, in fact,
2397 creating a .dynamic section. We don't want to define it if there
2398 is no .dynamic section, since on some ELF platforms the start up
2399 code examines it to decide how to initialize the process. */
2400 bh = NULL;
2401 if (! (_bfd_generic_link_add_one_symbol
2402 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2403 (const char *) 0, false, get_elf_backend_data (abfd)->collect, &bh)))
2404 return false;
2405 h = (struct elf_link_hash_entry *) bh;
2406 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2407 h->type = STT_OBJECT;
2409 if (info->shared
2410 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2411 return false;
2413 bed = get_elf_backend_data (abfd);
2415 s = bfd_make_section (abfd, ".hash");
2416 if (s == NULL
2417 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2418 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2419 return false;
2420 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2422 /* Let the backend create the rest of the sections. This lets the
2423 backend set the right flags. The backend will normally create
2424 the .got and .plt sections. */
2425 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2426 return false;
2428 elf_hash_table (info)->dynamic_sections_created = true;
2430 return true;
2433 /* Add an entry to the .dynamic table. */
2435 boolean
2436 elf_add_dynamic_entry (info, tag, val)
2437 struct bfd_link_info *info;
2438 bfd_vma tag;
2439 bfd_vma val;
2441 Elf_Internal_Dyn dyn;
2442 bfd *dynobj;
2443 asection *s;
2444 bfd_size_type newsize;
2445 bfd_byte *newcontents;
2447 if (! is_elf_hash_table (info))
2448 return false;
2450 dynobj = elf_hash_table (info)->dynobj;
2452 s = bfd_get_section_by_name (dynobj, ".dynamic");
2453 BFD_ASSERT (s != NULL);
2455 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2456 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2457 if (newcontents == NULL)
2458 return false;
2460 dyn.d_tag = tag;
2461 dyn.d_un.d_val = val;
2462 elf_swap_dyn_out (dynobj, &dyn,
2463 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2465 s->_raw_size = newsize;
2466 s->contents = newcontents;
2468 return true;
2471 /* Read and swap the relocs from the section indicated by SHDR. This
2472 may be either a REL or a RELA section. The relocations are
2473 translated into RELA relocations and stored in INTERNAL_RELOCS,
2474 which should have already been allocated to contain enough space.
2475 The EXTERNAL_RELOCS are a buffer where the external form of the
2476 relocations should be stored.
2478 Returns false if something goes wrong. */
2480 static boolean
2481 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2482 internal_relocs)
2483 bfd *abfd;
2484 Elf_Internal_Shdr *shdr;
2485 PTR external_relocs;
2486 Elf_Internal_Rela *internal_relocs;
2488 struct elf_backend_data *bed;
2489 bfd_size_type amt;
2491 /* If there aren't any relocations, that's OK. */
2492 if (!shdr)
2493 return true;
2495 /* Position ourselves at the start of the section. */
2496 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2497 return false;
2499 /* Read the relocations. */
2500 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2501 return false;
2503 bed = get_elf_backend_data (abfd);
2505 /* Convert the external relocations to the internal format. */
2506 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2508 Elf_External_Rel *erel;
2509 Elf_External_Rel *erelend;
2510 Elf_Internal_Rela *irela;
2511 Elf_Internal_Rel *irel;
2513 erel = (Elf_External_Rel *) external_relocs;
2514 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2515 irela = internal_relocs;
2516 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2517 irel = bfd_alloc (abfd, amt);
2518 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2520 unsigned int i;
2522 if (bed->s->swap_reloc_in)
2523 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2524 else
2525 elf_swap_reloc_in (abfd, erel, irel);
2527 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2529 irela[i].r_offset = irel[i].r_offset;
2530 irela[i].r_info = irel[i].r_info;
2531 irela[i].r_addend = 0;
2535 else
2537 Elf_External_Rela *erela;
2538 Elf_External_Rela *erelaend;
2539 Elf_Internal_Rela *irela;
2541 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2543 erela = (Elf_External_Rela *) external_relocs;
2544 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2545 irela = internal_relocs;
2546 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2548 if (bed->s->swap_reloca_in)
2549 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2550 else
2551 elf_swap_reloca_in (abfd, erela, irela);
2555 return true;
2558 /* Read and swap the relocs for a section O. They may have been
2559 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2560 not NULL, they are used as buffers to read into. They are known to
2561 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2562 the return value is allocated using either malloc or bfd_alloc,
2563 according to the KEEP_MEMORY argument. If O has two relocation
2564 sections (both REL and RELA relocations), then the REL_HDR
2565 relocations will appear first in INTERNAL_RELOCS, followed by the
2566 REL_HDR2 relocations. */
2568 Elf_Internal_Rela *
2569 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2570 keep_memory)
2571 bfd *abfd;
2572 asection *o;
2573 PTR external_relocs;
2574 Elf_Internal_Rela *internal_relocs;
2575 boolean keep_memory;
2577 Elf_Internal_Shdr *rel_hdr;
2578 PTR alloc1 = NULL;
2579 Elf_Internal_Rela *alloc2 = NULL;
2580 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2582 if (elf_section_data (o)->relocs != NULL)
2583 return elf_section_data (o)->relocs;
2585 if (o->reloc_count == 0)
2586 return NULL;
2588 rel_hdr = &elf_section_data (o)->rel_hdr;
2590 if (internal_relocs == NULL)
2592 bfd_size_type size;
2594 size = o->reloc_count;
2595 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2596 if (keep_memory)
2597 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2598 else
2599 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2600 if (internal_relocs == NULL)
2601 goto error_return;
2604 if (external_relocs == NULL)
2606 bfd_size_type size = rel_hdr->sh_size;
2608 if (elf_section_data (o)->rel_hdr2)
2609 size += elf_section_data (o)->rel_hdr2->sh_size;
2610 alloc1 = (PTR) bfd_malloc (size);
2611 if (alloc1 == NULL)
2612 goto error_return;
2613 external_relocs = alloc1;
2616 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2617 external_relocs,
2618 internal_relocs))
2619 goto error_return;
2620 if (!elf_link_read_relocs_from_section
2621 (abfd,
2622 elf_section_data (o)->rel_hdr2,
2623 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2624 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2625 * bed->s->int_rels_per_ext_rel)))
2626 goto error_return;
2628 /* Cache the results for next time, if we can. */
2629 if (keep_memory)
2630 elf_section_data (o)->relocs = internal_relocs;
2632 if (alloc1 != NULL)
2633 free (alloc1);
2635 /* Don't free alloc2, since if it was allocated we are passing it
2636 back (under the name of internal_relocs). */
2638 return internal_relocs;
2640 error_return:
2641 if (alloc1 != NULL)
2642 free (alloc1);
2643 if (alloc2 != NULL)
2644 free (alloc2);
2645 return NULL;
2648 /* Record an assignment to a symbol made by a linker script. We need
2649 this in case some dynamic object refers to this symbol. */
2651 boolean
2652 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2653 bfd *output_bfd ATTRIBUTE_UNUSED;
2654 struct bfd_link_info *info;
2655 const char *name;
2656 boolean provide;
2658 struct elf_link_hash_entry *h;
2660 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2661 return true;
2663 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2664 if (h == NULL)
2665 return false;
2667 if (h->root.type == bfd_link_hash_new)
2668 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2670 /* If this symbol is being provided by the linker script, and it is
2671 currently defined by a dynamic object, but not by a regular
2672 object, then mark it as undefined so that the generic linker will
2673 force the correct value. */
2674 if (provide
2675 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2676 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2677 h->root.type = bfd_link_hash_undefined;
2679 /* If this symbol is not being provided by the linker script, and it is
2680 currently defined by a dynamic object, but not by a regular object,
2681 then clear out any version information because the symbol will not be
2682 associated with the dynamic object any more. */
2683 if (!provide
2684 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2685 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2686 h->verinfo.verdef = NULL;
2688 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2690 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2691 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2692 || info->shared)
2693 && h->dynindx == -1)
2695 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2696 return false;
2698 /* If this is a weak defined symbol, and we know a corresponding
2699 real symbol from the same dynamic object, make sure the real
2700 symbol is also made into a dynamic symbol. */
2701 if (h->weakdef != NULL
2702 && h->weakdef->dynindx == -1)
2704 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2705 return false;
2709 return true;
2712 /* This structure is used to pass information to
2713 elf_link_assign_sym_version. */
2715 struct elf_assign_sym_version_info
2717 /* Output BFD. */
2718 bfd *output_bfd;
2719 /* General link information. */
2720 struct bfd_link_info *info;
2721 /* Version tree. */
2722 struct bfd_elf_version_tree *verdefs;
2723 /* Whether we had a failure. */
2724 boolean failed;
2727 /* This structure is used to pass information to
2728 elf_link_find_version_dependencies. */
2730 struct elf_find_verdep_info
2732 /* Output BFD. */
2733 bfd *output_bfd;
2734 /* General link information. */
2735 struct bfd_link_info *info;
2736 /* The number of dependencies. */
2737 unsigned int vers;
2738 /* Whether we had a failure. */
2739 boolean failed;
2742 /* Array used to determine the number of hash table buckets to use
2743 based on the number of symbols there are. If there are fewer than
2744 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2745 fewer than 37 we use 17 buckets, and so forth. We never use more
2746 than 32771 buckets. */
2748 static const size_t elf_buckets[] =
2750 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2751 16411, 32771, 0
2754 /* Compute bucket count for hashing table. We do not use a static set
2755 of possible tables sizes anymore. Instead we determine for all
2756 possible reasonable sizes of the table the outcome (i.e., the
2757 number of collisions etc) and choose the best solution. The
2758 weighting functions are not too simple to allow the table to grow
2759 without bounds. Instead one of the weighting factors is the size.
2760 Therefore the result is always a good payoff between few collisions
2761 (= short chain lengths) and table size. */
2762 static size_t
2763 compute_bucket_count (info)
2764 struct bfd_link_info *info;
2766 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2767 size_t best_size = 0;
2768 unsigned long int *hashcodes;
2769 unsigned long int *hashcodesp;
2770 unsigned long int i;
2771 bfd_size_type amt;
2773 /* Compute the hash values for all exported symbols. At the same
2774 time store the values in an array so that we could use them for
2775 optimizations. */
2776 amt = dynsymcount;
2777 amt *= sizeof (unsigned long int);
2778 hashcodes = (unsigned long int *) bfd_malloc (amt);
2779 if (hashcodes == NULL)
2780 return 0;
2781 hashcodesp = hashcodes;
2783 /* Put all hash values in HASHCODES. */
2784 elf_link_hash_traverse (elf_hash_table (info),
2785 elf_collect_hash_codes, &hashcodesp);
2787 /* We have a problem here. The following code to optimize the table
2788 size requires an integer type with more the 32 bits. If
2789 BFD_HOST_U_64_BIT is set we know about such a type. */
2790 #ifdef BFD_HOST_U_64_BIT
2791 if (info->optimize)
2793 unsigned long int nsyms = hashcodesp - hashcodes;
2794 size_t minsize;
2795 size_t maxsize;
2796 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2797 unsigned long int *counts ;
2799 /* Possible optimization parameters: if we have NSYMS symbols we say
2800 that the hashing table must at least have NSYMS/4 and at most
2801 2*NSYMS buckets. */
2802 minsize = nsyms / 4;
2803 if (minsize == 0)
2804 minsize = 1;
2805 best_size = maxsize = nsyms * 2;
2807 /* Create array where we count the collisions in. We must use bfd_malloc
2808 since the size could be large. */
2809 amt = maxsize;
2810 amt *= sizeof (unsigned long int);
2811 counts = (unsigned long int *) bfd_malloc (amt);
2812 if (counts == NULL)
2814 free (hashcodes);
2815 return 0;
2818 /* Compute the "optimal" size for the hash table. The criteria is a
2819 minimal chain length. The minor criteria is (of course) the size
2820 of the table. */
2821 for (i = minsize; i < maxsize; ++i)
2823 /* Walk through the array of hashcodes and count the collisions. */
2824 BFD_HOST_U_64_BIT max;
2825 unsigned long int j;
2826 unsigned long int fact;
2828 memset (counts, '\0', i * sizeof (unsigned long int));
2830 /* Determine how often each hash bucket is used. */
2831 for (j = 0; j < nsyms; ++j)
2832 ++counts[hashcodes[j] % i];
2834 /* For the weight function we need some information about the
2835 pagesize on the target. This is information need not be 100%
2836 accurate. Since this information is not available (so far) we
2837 define it here to a reasonable default value. If it is crucial
2838 to have a better value some day simply define this value. */
2839 # ifndef BFD_TARGET_PAGESIZE
2840 # define BFD_TARGET_PAGESIZE (4096)
2841 # endif
2843 /* We in any case need 2 + NSYMS entries for the size values and
2844 the chains. */
2845 max = (2 + nsyms) * (ARCH_SIZE / 8);
2847 # if 1
2848 /* Variant 1: optimize for short chains. We add the squares
2849 of all the chain lengths (which favous many small chain
2850 over a few long chains). */
2851 for (j = 0; j < i; ++j)
2852 max += counts[j] * counts[j];
2854 /* This adds penalties for the overall size of the table. */
2855 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2856 max *= fact * fact;
2857 # else
2858 /* Variant 2: Optimize a lot more for small table. Here we
2859 also add squares of the size but we also add penalties for
2860 empty slots (the +1 term). */
2861 for (j = 0; j < i; ++j)
2862 max += (1 + counts[j]) * (1 + counts[j]);
2864 /* The overall size of the table is considered, but not as
2865 strong as in variant 1, where it is squared. */
2866 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2867 max *= fact;
2868 # endif
2870 /* Compare with current best results. */
2871 if (max < best_chlen)
2873 best_chlen = max;
2874 best_size = i;
2878 free (counts);
2880 else
2881 #endif /* defined (BFD_HOST_U_64_BIT) */
2883 /* This is the fallback solution if no 64bit type is available or if we
2884 are not supposed to spend much time on optimizations. We select the
2885 bucket count using a fixed set of numbers. */
2886 for (i = 0; elf_buckets[i] != 0; i++)
2888 best_size = elf_buckets[i];
2889 if (dynsymcount < elf_buckets[i + 1])
2890 break;
2894 /* Free the arrays we needed. */
2895 free (hashcodes);
2897 return best_size;
2900 /* Set up the sizes and contents of the ELF dynamic sections. This is
2901 called by the ELF linker emulation before_allocation routine. We
2902 must set the sizes of the sections before the linker sets the
2903 addresses of the various sections. */
2905 boolean
2906 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2907 filter_shlib,
2908 auxiliary_filters, info, sinterpptr,
2909 verdefs)
2910 bfd *output_bfd;
2911 const char *soname;
2912 const char *rpath;
2913 const char *filter_shlib;
2914 const char * const *auxiliary_filters;
2915 struct bfd_link_info *info;
2916 asection **sinterpptr;
2917 struct bfd_elf_version_tree *verdefs;
2919 bfd_size_type soname_indx;
2920 bfd *dynobj;
2921 struct elf_backend_data *bed;
2922 struct elf_assign_sym_version_info asvinfo;
2924 *sinterpptr = NULL;
2926 soname_indx = (bfd_size_type) -1;
2928 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2929 return true;
2931 if (! is_elf_hash_table (info))
2932 return true;
2934 /* Any syms created from now on start with -1 in
2935 got.refcount/offset and plt.refcount/offset. */
2936 elf_hash_table (info)->init_refcount = -1;
2938 /* The backend may have to create some sections regardless of whether
2939 we're dynamic or not. */
2940 bed = get_elf_backend_data (output_bfd);
2941 if (bed->elf_backend_always_size_sections
2942 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2943 return false;
2945 dynobj = elf_hash_table (info)->dynobj;
2947 /* If there were no dynamic objects in the link, there is nothing to
2948 do here. */
2949 if (dynobj == NULL)
2950 return true;
2952 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
2953 return false;
2955 if (elf_hash_table (info)->dynamic_sections_created)
2957 struct elf_info_failed eif;
2958 struct elf_link_hash_entry *h;
2959 asection *dynstr;
2960 struct bfd_elf_version_tree *t;
2961 struct bfd_elf_version_expr *d;
2962 boolean all_defined;
2964 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2965 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2967 if (soname != NULL)
2969 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2970 soname, true);
2971 if (soname_indx == (bfd_size_type) -1
2972 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
2973 soname_indx))
2974 return false;
2977 if (info->symbolic)
2979 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
2980 (bfd_vma) 0))
2981 return false;
2982 info->flags |= DF_SYMBOLIC;
2985 if (rpath != NULL)
2987 bfd_size_type indx;
2989 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
2990 true);
2991 if (info->new_dtags)
2992 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
2993 if (indx == (bfd_size_type) -1
2994 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
2995 || (info->new_dtags
2996 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
2997 indx)))
2998 return false;
3001 if (filter_shlib != NULL)
3003 bfd_size_type indx;
3005 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3006 filter_shlib, true);
3007 if (indx == (bfd_size_type) -1
3008 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3009 return false;
3012 if (auxiliary_filters != NULL)
3014 const char * const *p;
3016 for (p = auxiliary_filters; *p != NULL; p++)
3018 bfd_size_type indx;
3020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3021 *p, true);
3022 if (indx == (bfd_size_type) -1
3023 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3024 indx))
3025 return false;
3029 eif.info = info;
3030 eif.verdefs = verdefs;
3031 eif.failed = false;
3033 /* If we are supposed to export all symbols into the dynamic symbol
3034 table (this is not the normal case), then do so. */
3035 if (info->export_dynamic)
3037 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3038 (PTR) &eif);
3039 if (eif.failed)
3040 return false;
3043 /* Make all global versions with definiton. */
3044 for (t = verdefs; t != NULL; t = t->next)
3045 for (d = t->globals; d != NULL; d = d->next)
3046 if (!d->symver && strchr (d->pattern, '*') == NULL)
3048 const char *verstr, *name;
3049 size_t namelen, verlen, newlen;
3050 char *newname, *p;
3051 struct elf_link_hash_entry *newh;
3053 name = d->pattern;
3054 namelen = strlen (name);
3055 verstr = t->name;
3056 verlen = strlen (verstr);
3057 newlen = namelen + verlen + 3;
3059 newname = (char *) bfd_malloc ((bfd_size_type) newlen);
3060 if (newname == NULL)
3061 return false;
3062 memcpy (newname, name, namelen);
3064 /* Check the hidden versioned definition. */
3065 p = newname + namelen;
3066 *p++ = ELF_VER_CHR;
3067 memcpy (p, verstr, verlen + 1);
3068 newh = elf_link_hash_lookup (elf_hash_table (info),
3069 newname, false, false,
3070 false);
3071 if (newh == NULL
3072 || (newh->root.type != bfd_link_hash_defined
3073 && newh->root.type != bfd_link_hash_defweak))
3075 /* Check the default versioned definition. */
3076 *p++ = ELF_VER_CHR;
3077 memcpy (p, verstr, verlen + 1);
3078 newh = elf_link_hash_lookup (elf_hash_table (info),
3079 newname, false, false,
3080 false);
3082 free (newname);
3084 /* Mark this version if there is a definition and it is
3085 not defined in a shared object. */
3086 if (newh != NULL
3087 && ((newh->elf_link_hash_flags
3088 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
3089 && (newh->root.type == bfd_link_hash_defined
3090 || newh->root.type == bfd_link_hash_defweak))
3091 d->symver = 1;
3094 /* Attach all the symbols to their version information. */
3095 asvinfo.output_bfd = output_bfd;
3096 asvinfo.info = info;
3097 asvinfo.verdefs = verdefs;
3098 asvinfo.failed = false;
3100 elf_link_hash_traverse (elf_hash_table (info),
3101 elf_link_assign_sym_version,
3102 (PTR) &asvinfo);
3103 if (asvinfo.failed)
3104 return false;
3106 if (!info->allow_undefined_version)
3108 /* Check if all global versions have a definiton. */
3109 all_defined = true;
3110 for (t = verdefs; t != NULL; t = t->next)
3111 for (d = t->globals; d != NULL; d = d->next)
3112 if (!d->symver && !d->script
3113 && strchr (d->pattern, '*') == NULL)
3115 (*_bfd_error_handler)
3116 (_("%s: undefined version: %s"),
3117 d->pattern, t->name);
3118 all_defined = false;
3121 if (!all_defined)
3123 bfd_set_error (bfd_error_bad_value);
3124 return false;
3128 /* Find all symbols which were defined in a dynamic object and make
3129 the backend pick a reasonable value for them. */
3130 elf_link_hash_traverse (elf_hash_table (info),
3131 elf_adjust_dynamic_symbol,
3132 (PTR) &eif);
3133 if (eif.failed)
3134 return false;
3136 /* Add some entries to the .dynamic section. We fill in some of the
3137 values later, in elf_bfd_final_link, but we must add the entries
3138 now so that we know the final size of the .dynamic section. */
3140 /* If there are initialization and/or finalization functions to
3141 call then add the corresponding DT_INIT/DT_FINI entries. */
3142 h = (info->init_function
3143 ? elf_link_hash_lookup (elf_hash_table (info),
3144 info->init_function, false,
3145 false, false)
3146 : NULL);
3147 if (h != NULL
3148 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3149 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3151 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3152 return false;
3154 h = (info->fini_function
3155 ? elf_link_hash_lookup (elf_hash_table (info),
3156 info->fini_function, false,
3157 false, false)
3158 : NULL);
3159 if (h != NULL
3160 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3161 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3163 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3164 return false;
3167 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3169 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3170 if (info->shared)
3172 bfd *sub;
3173 asection *o;
3175 for (sub = info->input_bfds; sub != NULL;
3176 sub = sub->link_next)
3177 for (o = sub->sections; o != NULL; o = o->next)
3178 if (elf_section_data (o)->this_hdr.sh_type
3179 == SHT_PREINIT_ARRAY)
3181 (*_bfd_error_handler)
3182 (_("%s: .preinit_array section is not allowed in DSO"),
3183 bfd_archive_filename (sub));
3184 break;
3187 bfd_set_error (bfd_error_nonrepresentable_section);
3188 return false;
3191 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3192 (bfd_vma) 0)
3193 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3194 (bfd_vma) 0))
3195 return false;
3197 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3199 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3200 (bfd_vma) 0)
3201 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3202 (bfd_vma) 0))
3203 return false;
3205 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3207 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3208 (bfd_vma) 0)
3209 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3210 (bfd_vma) 0))
3211 return false;
3214 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3215 /* If .dynstr is excluded from the link, we don't want any of
3216 these tags. Strictly, we should be checking each section
3217 individually; This quick check covers for the case where
3218 someone does a /DISCARD/ : { *(*) }. */
3219 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3221 bfd_size_type strsize;
3223 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3224 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3225 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3226 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3227 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3228 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3229 (bfd_vma) sizeof (Elf_External_Sym)))
3230 return false;
3234 /* The backend must work out the sizes of all the other dynamic
3235 sections. */
3236 if (bed->elf_backend_size_dynamic_sections
3237 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3238 return false;
3240 if (elf_hash_table (info)->dynamic_sections_created)
3242 bfd_size_type dynsymcount;
3243 asection *s;
3244 size_t bucketcount = 0;
3245 size_t hash_entry_size;
3246 unsigned int dtagcount;
3248 /* Set up the version definition section. */
3249 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3250 BFD_ASSERT (s != NULL);
3252 /* We may have created additional version definitions if we are
3253 just linking a regular application. */
3254 verdefs = asvinfo.verdefs;
3256 /* Skip anonymous version tag. */
3257 if (verdefs != NULL && verdefs->vernum == 0)
3258 verdefs = verdefs->next;
3260 if (verdefs == NULL)
3261 _bfd_strip_section_from_output (info, s);
3262 else
3264 unsigned int cdefs;
3265 bfd_size_type size;
3266 struct bfd_elf_version_tree *t;
3267 bfd_byte *p;
3268 Elf_Internal_Verdef def;
3269 Elf_Internal_Verdaux defaux;
3271 cdefs = 0;
3272 size = 0;
3274 /* Make space for the base version. */
3275 size += sizeof (Elf_External_Verdef);
3276 size += sizeof (Elf_External_Verdaux);
3277 ++cdefs;
3279 for (t = verdefs; t != NULL; t = t->next)
3281 struct bfd_elf_version_deps *n;
3283 size += sizeof (Elf_External_Verdef);
3284 size += sizeof (Elf_External_Verdaux);
3285 ++cdefs;
3287 for (n = t->deps; n != NULL; n = n->next)
3288 size += sizeof (Elf_External_Verdaux);
3291 s->_raw_size = size;
3292 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3293 if (s->contents == NULL && s->_raw_size != 0)
3294 return false;
3296 /* Fill in the version definition section. */
3298 p = s->contents;
3300 def.vd_version = VER_DEF_CURRENT;
3301 def.vd_flags = VER_FLG_BASE;
3302 def.vd_ndx = 1;
3303 def.vd_cnt = 1;
3304 def.vd_aux = sizeof (Elf_External_Verdef);
3305 def.vd_next = (sizeof (Elf_External_Verdef)
3306 + sizeof (Elf_External_Verdaux));
3308 if (soname_indx != (bfd_size_type) -1)
3310 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3311 soname_indx);
3312 def.vd_hash = bfd_elf_hash (soname);
3313 defaux.vda_name = soname_indx;
3315 else
3317 const char *name;
3318 bfd_size_type indx;
3320 name = basename (output_bfd->filename);
3321 def.vd_hash = bfd_elf_hash (name);
3322 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3323 name, false);
3324 if (indx == (bfd_size_type) -1)
3325 return false;
3326 defaux.vda_name = indx;
3328 defaux.vda_next = 0;
3330 _bfd_elf_swap_verdef_out (output_bfd, &def,
3331 (Elf_External_Verdef *) p);
3332 p += sizeof (Elf_External_Verdef);
3333 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3334 (Elf_External_Verdaux *) p);
3335 p += sizeof (Elf_External_Verdaux);
3337 for (t = verdefs; t != NULL; t = t->next)
3339 unsigned int cdeps;
3340 struct bfd_elf_version_deps *n;
3341 struct elf_link_hash_entry *h;
3342 struct bfd_link_hash_entry *bh;
3344 cdeps = 0;
3345 for (n = t->deps; n != NULL; n = n->next)
3346 ++cdeps;
3348 /* Add a symbol representing this version. */
3349 bh = NULL;
3350 if (! (_bfd_generic_link_add_one_symbol
3351 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3352 (bfd_vma) 0, (const char *) NULL, false,
3353 get_elf_backend_data (dynobj)->collect, &bh)))
3354 return false;
3355 h = (struct elf_link_hash_entry *) bh;
3356 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3357 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3358 h->type = STT_OBJECT;
3359 h->verinfo.vertree = t;
3361 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3362 return false;
3364 def.vd_version = VER_DEF_CURRENT;
3365 def.vd_flags = 0;
3366 if (t->globals == NULL && t->locals == NULL && ! t->used)
3367 def.vd_flags |= VER_FLG_WEAK;
3368 def.vd_ndx = t->vernum + 1;
3369 def.vd_cnt = cdeps + 1;
3370 def.vd_hash = bfd_elf_hash (t->name);
3371 def.vd_aux = sizeof (Elf_External_Verdef);
3372 if (t->next != NULL)
3373 def.vd_next = (sizeof (Elf_External_Verdef)
3374 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3375 else
3376 def.vd_next = 0;
3378 _bfd_elf_swap_verdef_out (output_bfd, &def,
3379 (Elf_External_Verdef *) p);
3380 p += sizeof (Elf_External_Verdef);
3382 defaux.vda_name = h->dynstr_index;
3383 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3384 h->dynstr_index);
3385 if (t->deps == NULL)
3386 defaux.vda_next = 0;
3387 else
3388 defaux.vda_next = sizeof (Elf_External_Verdaux);
3389 t->name_indx = defaux.vda_name;
3391 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3392 (Elf_External_Verdaux *) p);
3393 p += sizeof (Elf_External_Verdaux);
3395 for (n = t->deps; n != NULL; n = n->next)
3397 if (n->version_needed == NULL)
3399 /* This can happen if there was an error in the
3400 version script. */
3401 defaux.vda_name = 0;
3403 else
3405 defaux.vda_name = n->version_needed->name_indx;
3406 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3407 defaux.vda_name);
3409 if (n->next == NULL)
3410 defaux.vda_next = 0;
3411 else
3412 defaux.vda_next = sizeof (Elf_External_Verdaux);
3414 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3415 (Elf_External_Verdaux *) p);
3416 p += sizeof (Elf_External_Verdaux);
3420 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3421 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3422 (bfd_vma) cdefs))
3423 return false;
3425 elf_tdata (output_bfd)->cverdefs = cdefs;
3428 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
3430 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3431 return false;
3434 if (info->flags_1)
3436 if (! info->shared)
3437 info->flags_1 &= ~ (DF_1_INITFIRST
3438 | DF_1_NODELETE
3439 | DF_1_NOOPEN);
3440 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3441 info->flags_1))
3442 return false;
3445 /* Work out the size of the version reference section. */
3447 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3448 BFD_ASSERT (s != NULL);
3450 struct elf_find_verdep_info sinfo;
3452 sinfo.output_bfd = output_bfd;
3453 sinfo.info = info;
3454 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3455 if (sinfo.vers == 0)
3456 sinfo.vers = 1;
3457 sinfo.failed = false;
3459 elf_link_hash_traverse (elf_hash_table (info),
3460 elf_link_find_version_dependencies,
3461 (PTR) &sinfo);
3463 if (elf_tdata (output_bfd)->verref == NULL)
3464 _bfd_strip_section_from_output (info, s);
3465 else
3467 Elf_Internal_Verneed *t;
3468 unsigned int size;
3469 unsigned int crefs;
3470 bfd_byte *p;
3472 /* Build the version definition section. */
3473 size = 0;
3474 crefs = 0;
3475 for (t = elf_tdata (output_bfd)->verref;
3476 t != NULL;
3477 t = t->vn_nextref)
3479 Elf_Internal_Vernaux *a;
3481 size += sizeof (Elf_External_Verneed);
3482 ++crefs;
3483 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3484 size += sizeof (Elf_External_Vernaux);
3487 s->_raw_size = size;
3488 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3489 if (s->contents == NULL)
3490 return false;
3492 p = s->contents;
3493 for (t = elf_tdata (output_bfd)->verref;
3494 t != NULL;
3495 t = t->vn_nextref)
3497 unsigned int caux;
3498 Elf_Internal_Vernaux *a;
3499 bfd_size_type indx;
3501 caux = 0;
3502 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3503 ++caux;
3505 t->vn_version = VER_NEED_CURRENT;
3506 t->vn_cnt = caux;
3507 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3508 elf_dt_name (t->vn_bfd) != NULL
3509 ? elf_dt_name (t->vn_bfd)
3510 : basename (t->vn_bfd->filename),
3511 false);
3512 if (indx == (bfd_size_type) -1)
3513 return false;
3514 t->vn_file = indx;
3515 t->vn_aux = sizeof (Elf_External_Verneed);
3516 if (t->vn_nextref == NULL)
3517 t->vn_next = 0;
3518 else
3519 t->vn_next = (sizeof (Elf_External_Verneed)
3520 + caux * sizeof (Elf_External_Vernaux));
3522 _bfd_elf_swap_verneed_out (output_bfd, t,
3523 (Elf_External_Verneed *) p);
3524 p += sizeof (Elf_External_Verneed);
3526 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3528 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3529 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3530 a->vna_nodename, false);
3531 if (indx == (bfd_size_type) -1)
3532 return false;
3533 a->vna_name = indx;
3534 if (a->vna_nextptr == NULL)
3535 a->vna_next = 0;
3536 else
3537 a->vna_next = sizeof (Elf_External_Vernaux);
3539 _bfd_elf_swap_vernaux_out (output_bfd, a,
3540 (Elf_External_Vernaux *) p);
3541 p += sizeof (Elf_External_Vernaux);
3545 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3546 (bfd_vma) 0)
3547 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3548 (bfd_vma) crefs))
3549 return false;
3551 elf_tdata (output_bfd)->cverrefs = crefs;
3555 /* Assign dynsym indicies. In a shared library we generate a
3556 section symbol for each output section, which come first.
3557 Next come all of the back-end allocated local dynamic syms,
3558 followed by the rest of the global symbols. */
3560 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3562 /* Work out the size of the symbol version section. */
3563 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3564 BFD_ASSERT (s != NULL);
3565 if (dynsymcount == 0
3566 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3568 _bfd_strip_section_from_output (info, s);
3569 /* The DYNSYMCOUNT might have changed if we were going to
3570 output a dynamic symbol table entry for S. */
3571 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3573 else
3575 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3576 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3577 if (s->contents == NULL)
3578 return false;
3580 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3581 return false;
3584 /* Set the size of the .dynsym and .hash sections. We counted
3585 the number of dynamic symbols in elf_link_add_object_symbols.
3586 We will build the contents of .dynsym and .hash when we build
3587 the final symbol table, because until then we do not know the
3588 correct value to give the symbols. We built the .dynstr
3589 section as we went along in elf_link_add_object_symbols. */
3590 s = bfd_get_section_by_name (dynobj, ".dynsym");
3591 BFD_ASSERT (s != NULL);
3592 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3593 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3594 if (s->contents == NULL && s->_raw_size != 0)
3595 return false;
3597 if (dynsymcount != 0)
3599 Elf_Internal_Sym isym;
3601 /* The first entry in .dynsym is a dummy symbol. */
3602 isym.st_value = 0;
3603 isym.st_size = 0;
3604 isym.st_name = 0;
3605 isym.st_info = 0;
3606 isym.st_other = 0;
3607 isym.st_shndx = 0;
3608 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3611 /* Compute the size of the hashing table. As a side effect this
3612 computes the hash values for all the names we export. */
3613 bucketcount = compute_bucket_count (info);
3615 s = bfd_get_section_by_name (dynobj, ".hash");
3616 BFD_ASSERT (s != NULL);
3617 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3618 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3619 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3620 if (s->contents == NULL)
3621 return false;
3623 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3624 s->contents);
3625 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3626 s->contents + hash_entry_size);
3628 elf_hash_table (info)->bucketcount = bucketcount;
3630 s = bfd_get_section_by_name (dynobj, ".dynstr");
3631 BFD_ASSERT (s != NULL);
3633 elf_finalize_dynstr (output_bfd, info);
3635 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3637 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3638 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3639 return false;
3642 return true;
3645 /* This function is used to adjust offsets into .dynstr for
3646 dynamic symbols. This is called via elf_link_hash_traverse. */
3648 static boolean elf_adjust_dynstr_offsets
3649 PARAMS ((struct elf_link_hash_entry *, PTR));
3651 static boolean
3652 elf_adjust_dynstr_offsets (h, data)
3653 struct elf_link_hash_entry *h;
3654 PTR data;
3656 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3658 if (h->root.type == bfd_link_hash_warning)
3659 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3661 if (h->dynindx != -1)
3662 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3663 return true;
3666 /* Assign string offsets in .dynstr, update all structures referencing
3667 them. */
3669 static boolean
3670 elf_finalize_dynstr (output_bfd, info)
3671 bfd *output_bfd;
3672 struct bfd_link_info *info;
3674 struct elf_link_local_dynamic_entry *entry;
3675 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3676 bfd *dynobj = elf_hash_table (info)->dynobj;
3677 asection *sdyn;
3678 bfd_size_type size;
3679 Elf_External_Dyn *dyncon, *dynconend;
3681 _bfd_elf_strtab_finalize (dynstr);
3682 size = _bfd_elf_strtab_size (dynstr);
3684 /* Update all .dynamic entries referencing .dynstr strings. */
3685 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3686 BFD_ASSERT (sdyn != NULL);
3688 dyncon = (Elf_External_Dyn *) sdyn->contents;
3689 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3690 sdyn->_raw_size);
3691 for (; dyncon < dynconend; dyncon++)
3693 Elf_Internal_Dyn dyn;
3695 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3696 switch (dyn.d_tag)
3698 case DT_STRSZ:
3699 dyn.d_un.d_val = size;
3700 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3701 break;
3702 case DT_NEEDED:
3703 case DT_SONAME:
3704 case DT_RPATH:
3705 case DT_RUNPATH:
3706 case DT_FILTER:
3707 case DT_AUXILIARY:
3708 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3709 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3710 break;
3711 default:
3712 break;
3716 /* Now update local dynamic symbols. */
3717 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3718 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3719 entry->isym.st_name);
3721 /* And the rest of dynamic symbols. */
3722 elf_link_hash_traverse (elf_hash_table (info),
3723 elf_adjust_dynstr_offsets, dynstr);
3725 /* Adjust version definitions. */
3726 if (elf_tdata (output_bfd)->cverdefs)
3728 asection *s;
3729 bfd_byte *p;
3730 bfd_size_type i;
3731 Elf_Internal_Verdef def;
3732 Elf_Internal_Verdaux defaux;
3734 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3735 p = (bfd_byte *) s->contents;
3738 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3739 &def);
3740 p += sizeof (Elf_External_Verdef);
3741 for (i = 0; i < def.vd_cnt; ++i)
3743 _bfd_elf_swap_verdaux_in (output_bfd,
3744 (Elf_External_Verdaux *) p, &defaux);
3745 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3746 defaux.vda_name);
3747 _bfd_elf_swap_verdaux_out (output_bfd,
3748 &defaux, (Elf_External_Verdaux *) p);
3749 p += sizeof (Elf_External_Verdaux);
3752 while (def.vd_next);
3755 /* Adjust version references. */
3756 if (elf_tdata (output_bfd)->verref)
3758 asection *s;
3759 bfd_byte *p;
3760 bfd_size_type i;
3761 Elf_Internal_Verneed need;
3762 Elf_Internal_Vernaux needaux;
3764 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3765 p = (bfd_byte *) s->contents;
3768 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3769 &need);
3770 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3771 _bfd_elf_swap_verneed_out (output_bfd, &need,
3772 (Elf_External_Verneed *) p);
3773 p += sizeof (Elf_External_Verneed);
3774 for (i = 0; i < need.vn_cnt; ++i)
3776 _bfd_elf_swap_vernaux_in (output_bfd,
3777 (Elf_External_Vernaux *) p, &needaux);
3778 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3779 needaux.vna_name);
3780 _bfd_elf_swap_vernaux_out (output_bfd,
3781 &needaux,
3782 (Elf_External_Vernaux *) p);
3783 p += sizeof (Elf_External_Vernaux);
3786 while (need.vn_next);
3789 return true;
3792 /* Fix up the flags for a symbol. This handles various cases which
3793 can only be fixed after all the input files are seen. This is
3794 currently called by both adjust_dynamic_symbol and
3795 assign_sym_version, which is unnecessary but perhaps more robust in
3796 the face of future changes. */
3798 static boolean
3799 elf_fix_symbol_flags (h, eif)
3800 struct elf_link_hash_entry *h;
3801 struct elf_info_failed *eif;
3803 /* If this symbol was mentioned in a non-ELF file, try to set
3804 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3805 permit a non-ELF file to correctly refer to a symbol defined in
3806 an ELF dynamic object. */
3807 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3809 while (h->root.type == bfd_link_hash_indirect)
3810 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3812 if (h->root.type != bfd_link_hash_defined
3813 && h->root.type != bfd_link_hash_defweak)
3814 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3815 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3816 else
3818 if (h->root.u.def.section->owner != NULL
3819 && (bfd_get_flavour (h->root.u.def.section->owner)
3820 == bfd_target_elf_flavour))
3821 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3822 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3823 else
3824 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3827 if (h->dynindx == -1
3828 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3829 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3831 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3833 eif->failed = true;
3834 return false;
3838 else
3840 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3841 was first seen in a non-ELF file. Fortunately, if the symbol
3842 was first seen in an ELF file, we're probably OK unless the
3843 symbol was defined in a non-ELF file. Catch that case here.
3844 FIXME: We're still in trouble if the symbol was first seen in
3845 a dynamic object, and then later in a non-ELF regular object. */
3846 if ((h->root.type == bfd_link_hash_defined
3847 || h->root.type == bfd_link_hash_defweak)
3848 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3849 && (h->root.u.def.section->owner != NULL
3850 ? (bfd_get_flavour (h->root.u.def.section->owner)
3851 != bfd_target_elf_flavour)
3852 : (bfd_is_abs_section (h->root.u.def.section)
3853 && (h->elf_link_hash_flags
3854 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3855 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3858 /* If this is a final link, and the symbol was defined as a common
3859 symbol in a regular object file, and there was no definition in
3860 any dynamic object, then the linker will have allocated space for
3861 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3862 flag will not have been set. */
3863 if (h->root.type == bfd_link_hash_defined
3864 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3865 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3866 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3867 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3868 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3870 /* If -Bsymbolic was used (which means to bind references to global
3871 symbols to the definition within the shared object), and this
3872 symbol was defined in a regular object, then it actually doesn't
3873 need a PLT entry, and we can accomplish that by forcing it local.
3874 Likewise, if the symbol has hidden or internal visibility.
3875 FIXME: It might be that we also do not need a PLT for other
3876 non-hidden visibilities, but we would have to tell that to the
3877 backend specifically; we can't just clear PLT-related data here. */
3878 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3879 && eif->info->shared
3880 && is_elf_hash_table (eif->info)
3881 && (eif->info->symbolic
3882 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3883 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3884 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3886 struct elf_backend_data *bed;
3887 boolean force_local;
3889 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3891 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3892 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3893 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3896 /* If this is a weak defined symbol in a dynamic object, and we know
3897 the real definition in the dynamic object, copy interesting flags
3898 over to the real definition. */
3899 if (h->weakdef != NULL)
3901 struct elf_link_hash_entry *weakdef;
3903 weakdef = h->weakdef;
3904 if (h->root.type == bfd_link_hash_indirect)
3905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3907 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3908 || h->root.type == bfd_link_hash_defweak);
3909 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3910 || weakdef->root.type == bfd_link_hash_defweak);
3911 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3913 /* If the real definition is defined by a regular object file,
3914 don't do anything special. See the longer description in
3915 elf_adjust_dynamic_symbol, below. */
3916 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3917 h->weakdef = NULL;
3918 else
3920 struct elf_backend_data *bed;
3922 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3923 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
3927 return true;
3930 /* Make the backend pick a good value for a dynamic symbol. This is
3931 called via elf_link_hash_traverse, and also calls itself
3932 recursively. */
3934 static boolean
3935 elf_adjust_dynamic_symbol (h, data)
3936 struct elf_link_hash_entry *h;
3937 PTR data;
3939 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3940 bfd *dynobj;
3941 struct elf_backend_data *bed;
3943 if (h->root.type == bfd_link_hash_warning)
3945 h->plt.offset = (bfd_vma) -1;
3946 h->got.offset = (bfd_vma) -1;
3948 /* When warning symbols are created, they **replace** the "real"
3949 entry in the hash table, thus we never get to see the real
3950 symbol in a hash traversal. So look at it now. */
3951 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3954 /* Ignore indirect symbols. These are added by the versioning code. */
3955 if (h->root.type == bfd_link_hash_indirect)
3956 return true;
3958 if (! is_elf_hash_table (eif->info))
3959 return false;
3961 /* Fix the symbol flags. */
3962 if (! elf_fix_symbol_flags (h, eif))
3963 return false;
3965 /* If this symbol does not require a PLT entry, and it is not
3966 defined by a dynamic object, or is not referenced by a regular
3967 object, ignore it. We do have to handle a weak defined symbol,
3968 even if no regular object refers to it, if we decided to add it
3969 to the dynamic symbol table. FIXME: Do we normally need to worry
3970 about symbols which are defined by one dynamic object and
3971 referenced by another one? */
3972 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3973 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3974 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3975 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3976 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3978 h->plt.offset = (bfd_vma) -1;
3979 return true;
3982 /* If we've already adjusted this symbol, don't do it again. This
3983 can happen via a recursive call. */
3984 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3985 return true;
3987 /* Don't look at this symbol again. Note that we must set this
3988 after checking the above conditions, because we may look at a
3989 symbol once, decide not to do anything, and then get called
3990 recursively later after REF_REGULAR is set below. */
3991 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3993 /* If this is a weak definition, and we know a real definition, and
3994 the real symbol is not itself defined by a regular object file,
3995 then get a good value for the real definition. We handle the
3996 real symbol first, for the convenience of the backend routine.
3998 Note that there is a confusing case here. If the real definition
3999 is defined by a regular object file, we don't get the real symbol
4000 from the dynamic object, but we do get the weak symbol. If the
4001 processor backend uses a COPY reloc, then if some routine in the
4002 dynamic object changes the real symbol, we will not see that
4003 change in the corresponding weak symbol. This is the way other
4004 ELF linkers work as well, and seems to be a result of the shared
4005 library model.
4007 I will clarify this issue. Most SVR4 shared libraries define the
4008 variable _timezone and define timezone as a weak synonym. The
4009 tzset call changes _timezone. If you write
4010 extern int timezone;
4011 int _timezone = 5;
4012 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4013 you might expect that, since timezone is a synonym for _timezone,
4014 the same number will print both times. However, if the processor
4015 backend uses a COPY reloc, then actually timezone will be copied
4016 into your process image, and, since you define _timezone
4017 yourself, _timezone will not. Thus timezone and _timezone will
4018 wind up at different memory locations. The tzset call will set
4019 _timezone, leaving timezone unchanged. */
4021 if (h->weakdef != NULL)
4023 /* If we get to this point, we know there is an implicit
4024 reference by a regular object file via the weak symbol H.
4025 FIXME: Is this really true? What if the traversal finds
4026 H->WEAKDEF before it finds H? */
4027 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4029 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4030 return false;
4033 /* If a symbol has no type and no size and does not require a PLT
4034 entry, then we are probably about to do the wrong thing here: we
4035 are probably going to create a COPY reloc for an empty object.
4036 This case can arise when a shared object is built with assembly
4037 code, and the assembly code fails to set the symbol type. */
4038 if (h->size == 0
4039 && h->type == STT_NOTYPE
4040 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4041 (*_bfd_error_handler)
4042 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4043 h->root.root.string);
4045 dynobj = elf_hash_table (eif->info)->dynobj;
4046 bed = get_elf_backend_data (dynobj);
4047 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4049 eif->failed = true;
4050 return false;
4053 return true;
4056 /* This routine is used to export all defined symbols into the dynamic
4057 symbol table. It is called via elf_link_hash_traverse. */
4059 static boolean
4060 elf_export_symbol (h, data)
4061 struct elf_link_hash_entry *h;
4062 PTR data;
4064 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4066 /* Ignore indirect symbols. These are added by the versioning code. */
4067 if (h->root.type == bfd_link_hash_indirect)
4068 return true;
4070 if (h->root.type == bfd_link_hash_warning)
4071 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4073 if (h->dynindx == -1
4074 && (h->elf_link_hash_flags
4075 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4077 struct bfd_elf_version_tree *t;
4078 struct bfd_elf_version_expr *d;
4080 for (t = eif->verdefs; t != NULL; t = t->next)
4082 if (t->globals != NULL)
4084 for (d = t->globals; d != NULL; d = d->next)
4086 if ((*d->match) (d, h->root.root.string))
4087 goto doit;
4091 if (t->locals != NULL)
4093 for (d = t->locals ; d != NULL; d = d->next)
4095 if ((*d->match) (d, h->root.root.string))
4096 return true;
4101 if (!eif->verdefs)
4103 doit:
4104 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4106 eif->failed = true;
4107 return false;
4112 return true;
4115 /* Look through the symbols which are defined in other shared
4116 libraries and referenced here. Update the list of version
4117 dependencies. This will be put into the .gnu.version_r section.
4118 This function is called via elf_link_hash_traverse. */
4120 static boolean
4121 elf_link_find_version_dependencies (h, data)
4122 struct elf_link_hash_entry *h;
4123 PTR data;
4125 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4126 Elf_Internal_Verneed *t;
4127 Elf_Internal_Vernaux *a;
4128 bfd_size_type amt;
4130 if (h->root.type == bfd_link_hash_warning)
4131 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4133 /* We only care about symbols defined in shared objects with version
4134 information. */
4135 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4136 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4137 || h->dynindx == -1
4138 || h->verinfo.verdef == NULL)
4139 return true;
4141 /* See if we already know about this version. */
4142 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4144 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4145 continue;
4147 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4148 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4149 return true;
4151 break;
4154 /* This is a new version. Add it to tree we are building. */
4156 if (t == NULL)
4158 amt = sizeof *t;
4159 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4160 if (t == NULL)
4162 rinfo->failed = true;
4163 return false;
4166 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4167 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4168 elf_tdata (rinfo->output_bfd)->verref = t;
4171 amt = sizeof *a;
4172 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4174 /* Note that we are copying a string pointer here, and testing it
4175 above. If bfd_elf_string_from_elf_section is ever changed to
4176 discard the string data when low in memory, this will have to be
4177 fixed. */
4178 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4180 a->vna_flags = h->verinfo.verdef->vd_flags;
4181 a->vna_nextptr = t->vn_auxptr;
4183 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4184 ++rinfo->vers;
4186 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4188 t->vn_auxptr = a;
4190 return true;
4193 /* Figure out appropriate versions for all the symbols. We may not
4194 have the version number script until we have read all of the input
4195 files, so until that point we don't know which symbols should be
4196 local. This function is called via elf_link_hash_traverse. */
4198 static boolean
4199 elf_link_assign_sym_version (h, data)
4200 struct elf_link_hash_entry *h;
4201 PTR data;
4203 struct elf_assign_sym_version_info *sinfo;
4204 struct bfd_link_info *info;
4205 struct elf_backend_data *bed;
4206 struct elf_info_failed eif;
4207 char *p;
4208 bfd_size_type amt;
4210 sinfo = (struct elf_assign_sym_version_info *) data;
4211 info = sinfo->info;
4213 if (h->root.type == bfd_link_hash_warning)
4214 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4216 /* Fix the symbol flags. */
4217 eif.failed = false;
4218 eif.info = info;
4219 if (! elf_fix_symbol_flags (h, &eif))
4221 if (eif.failed)
4222 sinfo->failed = true;
4223 return false;
4226 /* We only need version numbers for symbols defined in regular
4227 objects. */
4228 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4229 return true;
4231 bed = get_elf_backend_data (sinfo->output_bfd);
4232 p = strchr (h->root.root.string, ELF_VER_CHR);
4233 if (p != NULL && h->verinfo.vertree == NULL)
4235 struct bfd_elf_version_tree *t;
4236 boolean hidden;
4238 hidden = true;
4240 /* There are two consecutive ELF_VER_CHR characters if this is
4241 not a hidden symbol. */
4242 ++p;
4243 if (*p == ELF_VER_CHR)
4245 hidden = false;
4246 ++p;
4249 /* If there is no version string, we can just return out. */
4250 if (*p == '\0')
4252 if (hidden)
4253 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4254 return true;
4257 /* Look for the version. If we find it, it is no longer weak. */
4258 for (t = sinfo->verdefs; t != NULL; t = t->next)
4260 if (strcmp (t->name, p) == 0)
4262 size_t len;
4263 char *alc;
4264 struct bfd_elf_version_expr *d;
4266 len = p - h->root.root.string;
4267 alc = bfd_malloc ((bfd_size_type) len);
4268 if (alc == NULL)
4269 return false;
4270 memcpy (alc, h->root.root.string, len - 1);
4271 alc[len - 1] = '\0';
4272 if (alc[len - 2] == ELF_VER_CHR)
4273 alc[len - 2] = '\0';
4275 h->verinfo.vertree = t;
4276 t->used = true;
4277 d = NULL;
4279 if (t->globals != NULL)
4281 for (d = t->globals; d != NULL; d = d->next)
4282 if ((*d->match) (d, alc))
4283 break;
4286 /* See if there is anything to force this symbol to
4287 local scope. */
4288 if (d == NULL && t->locals != NULL)
4290 for (d = t->locals; d != NULL; d = d->next)
4292 if ((*d->match) (d, alc))
4294 if (h->dynindx != -1
4295 && info->shared
4296 && ! info->export_dynamic)
4298 (*bed->elf_backend_hide_symbol) (info, h, true);
4301 break;
4306 free (alc);
4307 break;
4311 /* If we are building an application, we need to create a
4312 version node for this version. */
4313 if (t == NULL && ! info->shared)
4315 struct bfd_elf_version_tree **pp;
4316 int version_index;
4318 /* If we aren't going to export this symbol, we don't need
4319 to worry about it. */
4320 if (h->dynindx == -1)
4321 return true;
4323 amt = sizeof *t;
4324 t = ((struct bfd_elf_version_tree *)
4325 bfd_alloc (sinfo->output_bfd, amt));
4326 if (t == NULL)
4328 sinfo->failed = true;
4329 return false;
4332 t->next = NULL;
4333 t->name = p;
4334 t->globals = NULL;
4335 t->locals = NULL;
4336 t->deps = NULL;
4337 t->name_indx = (unsigned int) -1;
4338 t->used = true;
4340 version_index = 1;
4341 /* Don't count anonymous version tag. */
4342 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4343 version_index = 0;
4344 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4345 ++version_index;
4346 t->vernum = version_index;
4348 *pp = t;
4350 h->verinfo.vertree = t;
4352 else if (t == NULL)
4354 /* We could not find the version for a symbol when
4355 generating a shared archive. Return an error. */
4356 (*_bfd_error_handler)
4357 (_("%s: undefined versioned symbol name %s"),
4358 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4359 bfd_set_error (bfd_error_bad_value);
4360 sinfo->failed = true;
4361 return false;
4364 if (hidden)
4365 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4368 /* If we don't have a version for this symbol, see if we can find
4369 something. */
4370 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4372 struct bfd_elf_version_tree *t;
4373 struct bfd_elf_version_tree *local_ver;
4374 struct bfd_elf_version_expr *d;
4376 /* See if can find what version this symbol is in. If the
4377 symbol is supposed to be local, then don't actually register
4378 it. */
4379 local_ver = NULL;
4380 for (t = sinfo->verdefs; t != NULL; t = t->next)
4382 if (t->globals != NULL)
4384 boolean matched;
4386 matched = false;
4387 for (d = t->globals; d != NULL; d = d->next)
4389 if ((*d->match) (d, h->root.root.string))
4391 if (d->symver)
4392 matched = true;
4393 else
4395 /* There is a version without definition. Make
4396 the symbol the default definition for this
4397 version. */
4398 h->verinfo.vertree = t;
4399 local_ver = NULL;
4400 d->script = 1;
4401 break;
4406 if (d != NULL)
4407 break;
4408 else if (matched)
4409 /* There is no undefined version for this symbol. Hide the
4410 default one. */
4411 (*bed->elf_backend_hide_symbol) (info, h, true);
4414 if (t->locals != NULL)
4416 for (d = t->locals; d != NULL; d = d->next)
4418 /* If the match is "*", keep looking for a more
4419 explicit, perhaps even global, match. */
4420 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4421 local_ver = t;
4422 else if ((*d->match) (d, h->root.root.string))
4424 local_ver = t;
4425 break;
4429 if (d != NULL)
4430 break;
4434 if (local_ver != NULL)
4436 h->verinfo.vertree = local_ver;
4437 if (h->dynindx != -1
4438 && info->shared
4439 && ! info->export_dynamic)
4441 (*bed->elf_backend_hide_symbol) (info, h, true);
4446 return true;
4449 /* Final phase of ELF linker. */
4451 /* A structure we use to avoid passing large numbers of arguments. */
4453 struct elf_final_link_info
4455 /* General link information. */
4456 struct bfd_link_info *info;
4457 /* Output BFD. */
4458 bfd *output_bfd;
4459 /* Symbol string table. */
4460 struct bfd_strtab_hash *symstrtab;
4461 /* .dynsym section. */
4462 asection *dynsym_sec;
4463 /* .hash section. */
4464 asection *hash_sec;
4465 /* symbol version section (.gnu.version). */
4466 asection *symver_sec;
4467 /* first SHF_TLS section (if any). */
4468 asection *first_tls_sec;
4469 /* Buffer large enough to hold contents of any section. */
4470 bfd_byte *contents;
4471 /* Buffer large enough to hold external relocs of any section. */
4472 PTR external_relocs;
4473 /* Buffer large enough to hold internal relocs of any section. */
4474 Elf_Internal_Rela *internal_relocs;
4475 /* Buffer large enough to hold external local symbols of any input
4476 BFD. */
4477 Elf_External_Sym *external_syms;
4478 /* And a buffer for symbol section indices. */
4479 Elf_External_Sym_Shndx *locsym_shndx;
4480 /* Buffer large enough to hold internal local symbols of any input
4481 BFD. */
4482 Elf_Internal_Sym *internal_syms;
4483 /* Array large enough to hold a symbol index for each local symbol
4484 of any input BFD. */
4485 long *indices;
4486 /* Array large enough to hold a section pointer for each local
4487 symbol of any input BFD. */
4488 asection **sections;
4489 /* Buffer to hold swapped out symbols. */
4490 Elf_External_Sym *symbuf;
4491 /* And one for symbol section indices. */
4492 Elf_External_Sym_Shndx *symshndxbuf;
4493 /* Number of swapped out symbols in buffer. */
4494 size_t symbuf_count;
4495 /* Number of symbols which fit in symbuf. */
4496 size_t symbuf_size;
4497 /* And same for symshndxbuf. */
4498 size_t shndxbuf_size;
4501 static boolean elf_link_output_sym
4502 PARAMS ((struct elf_final_link_info *, const char *,
4503 Elf_Internal_Sym *, asection *));
4504 static boolean elf_link_flush_output_syms
4505 PARAMS ((struct elf_final_link_info *));
4506 static boolean elf_link_output_extsym
4507 PARAMS ((struct elf_link_hash_entry *, PTR));
4508 static boolean elf_link_sec_merge_syms
4509 PARAMS ((struct elf_link_hash_entry *, PTR));
4510 static boolean elf_link_check_versioned_symbol
4511 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
4512 static boolean elf_link_input_bfd
4513 PARAMS ((struct elf_final_link_info *, bfd *));
4514 static boolean elf_reloc_link_order
4515 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4516 struct bfd_link_order *));
4518 /* This struct is used to pass information to elf_link_output_extsym. */
4520 struct elf_outext_info
4522 boolean failed;
4523 boolean localsyms;
4524 struct elf_final_link_info *finfo;
4527 /* Compute the size of, and allocate space for, REL_HDR which is the
4528 section header for a section containing relocations for O. */
4530 static boolean
4531 elf_link_size_reloc_section (abfd, rel_hdr, o)
4532 bfd *abfd;
4533 Elf_Internal_Shdr *rel_hdr;
4534 asection *o;
4536 bfd_size_type reloc_count;
4537 bfd_size_type num_rel_hashes;
4539 /* Figure out how many relocations there will be. */
4540 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4541 reloc_count = elf_section_data (o)->rel_count;
4542 else
4543 reloc_count = elf_section_data (o)->rel_count2;
4545 num_rel_hashes = o->reloc_count;
4546 if (num_rel_hashes < reloc_count)
4547 num_rel_hashes = reloc_count;
4549 /* That allows us to calculate the size of the section. */
4550 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4552 /* The contents field must last into write_object_contents, so we
4553 allocate it with bfd_alloc rather than malloc. Also since we
4554 cannot be sure that the contents will actually be filled in,
4555 we zero the allocated space. */
4556 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4557 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4558 return false;
4560 /* We only allocate one set of hash entries, so we only do it the
4561 first time we are called. */
4562 if (elf_section_data (o)->rel_hashes == NULL
4563 && num_rel_hashes)
4565 struct elf_link_hash_entry **p;
4567 p = ((struct elf_link_hash_entry **)
4568 bfd_zmalloc (num_rel_hashes
4569 * sizeof (struct elf_link_hash_entry *)));
4570 if (p == NULL)
4571 return false;
4573 elf_section_data (o)->rel_hashes = p;
4576 return true;
4579 /* When performing a relocateable link, the input relocations are
4580 preserved. But, if they reference global symbols, the indices
4581 referenced must be updated. Update all the relocations in
4582 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4584 static void
4585 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4586 bfd *abfd;
4587 Elf_Internal_Shdr *rel_hdr;
4588 unsigned int count;
4589 struct elf_link_hash_entry **rel_hash;
4591 unsigned int i;
4592 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4593 Elf_Internal_Rel *irel;
4594 Elf_Internal_Rela *irela;
4595 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4597 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4598 if (irel == NULL)
4600 (*_bfd_error_handler) (_("Error: out of memory"));
4601 abort ();
4604 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4605 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4606 if (irela == NULL)
4608 (*_bfd_error_handler) (_("Error: out of memory"));
4609 abort ();
4612 for (i = 0; i < count; i++, rel_hash++)
4614 if (*rel_hash == NULL)
4615 continue;
4617 BFD_ASSERT ((*rel_hash)->indx >= 0);
4619 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4621 Elf_External_Rel *erel;
4622 unsigned int j;
4624 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4625 if (bed->s->swap_reloc_in)
4626 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4627 else
4628 elf_swap_reloc_in (abfd, erel, irel);
4630 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4631 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4632 ELF_R_TYPE (irel[j].r_info));
4634 if (bed->s->swap_reloc_out)
4635 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4636 else
4637 elf_swap_reloc_out (abfd, irel, erel);
4639 else
4641 Elf_External_Rela *erela;
4642 unsigned int j;
4644 BFD_ASSERT (rel_hdr->sh_entsize
4645 == sizeof (Elf_External_Rela));
4647 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4648 if (bed->s->swap_reloca_in)
4649 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4650 else
4651 elf_swap_reloca_in (abfd, erela, irela);
4653 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4654 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4655 ELF_R_TYPE (irela[j].r_info));
4657 if (bed->s->swap_reloca_out)
4658 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4659 else
4660 elf_swap_reloca_out (abfd, irela, erela);
4664 free (irel);
4665 free (irela);
4668 struct elf_link_sort_rela
4670 bfd_vma offset;
4671 enum elf_reloc_type_class type;
4672 union
4674 /* We use these as arrays of size int_rels_per_ext_rel. */
4675 Elf_Internal_Rel rel[1];
4676 Elf_Internal_Rela rela[1];
4677 } u;
4680 static int
4681 elf_link_sort_cmp1 (A, B)
4682 const PTR A;
4683 const PTR B;
4685 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4686 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4687 int relativea, relativeb;
4689 relativea = a->type == reloc_class_relative;
4690 relativeb = b->type == reloc_class_relative;
4692 if (relativea < relativeb)
4693 return 1;
4694 if (relativea > relativeb)
4695 return -1;
4696 if (ELF_R_SYM (a->u.rel->r_info) < ELF_R_SYM (b->u.rel->r_info))
4697 return -1;
4698 if (ELF_R_SYM (a->u.rel->r_info) > ELF_R_SYM (b->u.rel->r_info))
4699 return 1;
4700 if (a->u.rel->r_offset < b->u.rel->r_offset)
4701 return -1;
4702 if (a->u.rel->r_offset > b->u.rel->r_offset)
4703 return 1;
4704 return 0;
4707 static int
4708 elf_link_sort_cmp2 (A, B)
4709 const PTR A;
4710 const PTR B;
4712 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4713 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4714 int copya, copyb;
4716 if (a->offset < b->offset)
4717 return -1;
4718 if (a->offset > b->offset)
4719 return 1;
4720 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4721 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4722 if (copya < copyb)
4723 return -1;
4724 if (copya > copyb)
4725 return 1;
4726 if (a->u.rel->r_offset < b->u.rel->r_offset)
4727 return -1;
4728 if (a->u.rel->r_offset > b->u.rel->r_offset)
4729 return 1;
4730 return 0;
4733 static size_t
4734 elf_link_sort_relocs (abfd, info, psec)
4735 bfd *abfd;
4736 struct bfd_link_info *info;
4737 asection **psec;
4739 bfd *dynobj = elf_hash_table (info)->dynobj;
4740 asection *reldyn, *o;
4741 boolean rel = false;
4742 bfd_size_type count, size;
4743 size_t i, j, ret;
4744 struct elf_link_sort_rela *rela;
4745 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4746 int i2e = bed->s->int_rels_per_ext_rel;
4748 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4749 if (reldyn == NULL || reldyn->_raw_size == 0)
4751 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4752 if (reldyn == NULL || reldyn->_raw_size == 0)
4753 return 0;
4754 rel = true;
4755 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4757 else
4758 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4760 size = 0;
4761 for (o = dynobj->sections; o != NULL; o = o->next)
4762 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4763 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4764 && o->output_section == reldyn)
4765 size += o->_raw_size;
4767 if (size != reldyn->_raw_size)
4768 return 0;
4770 /* We waste some memory here when N = i2e is greater than 1, since
4771 we allocate space for N * sizeof (*rela) where sizeof (*rela) +
4772 (N - 1) * sizeof (Elf_Internal_Rel/Rela) would do. Also, we use
4773 rela[k] only when k is a multiple of N, and then we index the
4774 array within the union, such that rela[k].u.rel[i], i < N, is the
4775 (i+1)th internal relocation corresponding to the (k/N)th external
4776 relocation. This is done such that the relocation swap-in and
4777 swap-out functions can gen pointers to arrays of internal
4778 relocations that form a single external relocation.
4780 If C permitted arrays of structures with dynamic sizes, we could
4781 do better, but trying to avoid wasting space at the end of the
4782 chunk from rela[k] to rela[k+N-1] would require us to allocate a
4783 separate array of pointers and since most ports have N == 1, this
4784 would be more wasteful. */
4785 rela = (struct elf_link_sort_rela *) bfd_zmalloc
4786 (sizeof (*rela) * count * i2e);
4787 if (rela == NULL)
4789 (*info->callbacks->warning)
4790 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4791 (bfd_vma) 0);
4792 return 0;
4795 for (o = dynobj->sections; o != NULL; o = o->next)
4796 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4797 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4798 && o->output_section == reldyn)
4800 if (rel)
4802 Elf_External_Rel *erel, *erelend;
4803 struct elf_link_sort_rela *s;
4805 erel = (Elf_External_Rel *) o->contents;
4806 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4807 s = rela + (o->output_offset / sizeof (Elf_External_Rel) * i2e);
4808 for (; erel < erelend; erel++, s += i2e)
4810 if (bed->s->swap_reloc_in)
4811 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel,
4812 s->u.rel);
4813 else
4814 elf_swap_reloc_in (abfd, erel, s->u.rel);
4816 s->type = (*bed->elf_backend_reloc_type_class) (s->u.rela);
4819 else
4821 Elf_External_Rela *erela, *erelaend;
4822 struct elf_link_sort_rela *s;
4824 erela = (Elf_External_Rela *) o->contents;
4825 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4826 s = rela + (o->output_offset / sizeof (Elf_External_Rela) * i2e);
4827 for (; erela < erelaend; erela++, s += i2e)
4829 if (bed->s->swap_reloca_in)
4830 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4831 s->u.rela);
4832 else
4833 elf_swap_reloca_in (dynobj, erela, s->u.rela);
4835 s->type = (*bed->elf_backend_reloc_type_class) (s->u.rela);
4840 qsort (rela, (size_t) count, sizeof (*rela) * i2e, elf_link_sort_cmp1);
4841 for (ret = 0; ret < count * i2e && rela[ret].type == reloc_class_relative;
4842 ret += i2e)
4844 for (i = ret, j = ret; i < count * i2e; i += i2e)
4846 if (ELF_R_SYM (rela[i].u.rel->r_info)
4847 != ELF_R_SYM (rela[j].u.rel->r_info))
4848 j = i;
4849 rela[i].offset = rela[j].u.rel->r_offset;
4851 ret /= i2e;
4852 qsort (rela + ret, (size_t) count - ret,
4853 sizeof (*rela) * i2e, elf_link_sort_cmp2);
4855 for (o = dynobj->sections; o != NULL; o = o->next)
4856 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4857 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4858 && o->output_section == reldyn)
4860 if (rel)
4862 Elf_External_Rel *erel, *erelend;
4863 struct elf_link_sort_rela *s;
4865 erel = (Elf_External_Rel *) o->contents;
4866 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4867 s = rela + (o->output_offset / sizeof (Elf_External_Rel) * i2e);
4868 for (; erel < erelend; erel++, s += i2e)
4870 if (bed->s->swap_reloc_out)
4871 (*bed->s->swap_reloc_out) (abfd, s->u.rel,
4872 (bfd_byte *) erel);
4873 else
4874 elf_swap_reloc_out (abfd, s->u.rel, erel);
4877 else
4879 Elf_External_Rela *erela, *erelaend;
4880 struct elf_link_sort_rela *s;
4882 erela = (Elf_External_Rela *) o->contents;
4883 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4884 s = rela + (o->output_offset / sizeof (Elf_External_Rela) * i2e);
4885 for (; erela < erelaend; erela++, s += i2e)
4887 if (bed->s->swap_reloca_out)
4888 (*bed->s->swap_reloca_out) (dynobj, s->u.rela,
4889 (bfd_byte *) erela);
4890 else
4891 elf_swap_reloca_out (dynobj, s->u.rela, erela);
4896 free (rela);
4897 *psec = reldyn;
4898 return ret;
4901 /* Do the final step of an ELF link. */
4903 boolean
4904 elf_bfd_final_link (abfd, info)
4905 bfd *abfd;
4906 struct bfd_link_info *info;
4908 boolean dynamic;
4909 boolean emit_relocs;
4910 bfd *dynobj;
4911 struct elf_final_link_info finfo;
4912 register asection *o;
4913 register struct bfd_link_order *p;
4914 register bfd *sub;
4915 bfd_size_type max_contents_size;
4916 bfd_size_type max_external_reloc_size;
4917 bfd_size_type max_internal_reloc_count;
4918 bfd_size_type max_sym_count;
4919 bfd_size_type max_sym_shndx_count;
4920 file_ptr off;
4921 Elf_Internal_Sym elfsym;
4922 unsigned int i;
4923 Elf_Internal_Shdr *symtab_hdr;
4924 Elf_Internal_Shdr *symtab_shndx_hdr;
4925 Elf_Internal_Shdr *symstrtab_hdr;
4926 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4927 struct elf_outext_info eoinfo;
4928 boolean merged;
4929 size_t relativecount = 0;
4930 asection *reldyn = 0;
4931 bfd_size_type amt;
4933 if (! is_elf_hash_table (info))
4934 return false;
4936 if (info->shared)
4937 abfd->flags |= DYNAMIC;
4939 dynamic = elf_hash_table (info)->dynamic_sections_created;
4940 dynobj = elf_hash_table (info)->dynobj;
4942 emit_relocs = (info->relocateable
4943 || info->emitrelocations
4944 || bed->elf_backend_emit_relocs);
4946 finfo.info = info;
4947 finfo.output_bfd = abfd;
4948 finfo.symstrtab = elf_stringtab_init ();
4949 if (finfo.symstrtab == NULL)
4950 return false;
4952 if (! dynamic)
4954 finfo.dynsym_sec = NULL;
4955 finfo.hash_sec = NULL;
4956 finfo.symver_sec = NULL;
4958 else
4960 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4961 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4962 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4963 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4964 /* Note that it is OK if symver_sec is NULL. */
4967 finfo.contents = NULL;
4968 finfo.external_relocs = NULL;
4969 finfo.internal_relocs = NULL;
4970 finfo.external_syms = NULL;
4971 finfo.locsym_shndx = NULL;
4972 finfo.internal_syms = NULL;
4973 finfo.indices = NULL;
4974 finfo.sections = NULL;
4975 finfo.symbuf = NULL;
4976 finfo.symshndxbuf = NULL;
4977 finfo.symbuf_count = 0;
4978 finfo.shndxbuf_size = 0;
4979 finfo.first_tls_sec = NULL;
4980 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4981 if ((o->flags & SEC_THREAD_LOCAL) != 0
4982 && (o->flags & SEC_LOAD) != 0)
4984 finfo.first_tls_sec = o;
4985 break;
4988 /* Count up the number of relocations we will output for each output
4989 section, so that we know the sizes of the reloc sections. We
4990 also figure out some maximum sizes. */
4991 max_contents_size = 0;
4992 max_external_reloc_size = 0;
4993 max_internal_reloc_count = 0;
4994 max_sym_count = 0;
4995 max_sym_shndx_count = 0;
4996 merged = false;
4997 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4999 o->reloc_count = 0;
5001 for (p = o->link_order_head; p != NULL; p = p->next)
5003 if (p->type == bfd_section_reloc_link_order
5004 || p->type == bfd_symbol_reloc_link_order)
5005 ++o->reloc_count;
5006 else if (p->type == bfd_indirect_link_order)
5008 asection *sec;
5010 sec = p->u.indirect.section;
5012 /* Mark all sections which are to be included in the
5013 link. This will normally be every section. We need
5014 to do this so that we can identify any sections which
5015 the linker has decided to not include. */
5016 sec->linker_mark = true;
5018 if (sec->flags & SEC_MERGE)
5019 merged = true;
5021 if (info->relocateable || info->emitrelocations)
5022 o->reloc_count += sec->reloc_count;
5023 else if (bed->elf_backend_count_relocs)
5025 Elf_Internal_Rela * relocs;
5027 relocs = (NAME(_bfd_elf,link_read_relocs)
5028 (abfd, sec, (PTR) NULL,
5029 (Elf_Internal_Rela *) NULL, info->keep_memory));
5031 o->reloc_count
5032 += (*bed->elf_backend_count_relocs) (sec, relocs);
5034 if (elf_section_data (o)->relocs != relocs)
5035 free (relocs);
5038 if (sec->_raw_size > max_contents_size)
5039 max_contents_size = sec->_raw_size;
5040 if (sec->_cooked_size > max_contents_size)
5041 max_contents_size = sec->_cooked_size;
5043 /* We are interested in just local symbols, not all
5044 symbols. */
5045 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5046 && (sec->owner->flags & DYNAMIC) == 0)
5048 size_t sym_count;
5050 if (elf_bad_symtab (sec->owner))
5051 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5052 / sizeof (Elf_External_Sym));
5053 else
5054 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5056 if (sym_count > max_sym_count)
5057 max_sym_count = sym_count;
5059 if (sym_count > max_sym_shndx_count
5060 && elf_symtab_shndx (sec->owner) != 0)
5061 max_sym_shndx_count = sym_count;
5063 if ((sec->flags & SEC_RELOC) != 0)
5065 size_t ext_size;
5067 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5068 if (ext_size > max_external_reloc_size)
5069 max_external_reloc_size = ext_size;
5070 if (sec->reloc_count > max_internal_reloc_count)
5071 max_internal_reloc_count = sec->reloc_count;
5077 if (o->reloc_count > 0)
5078 o->flags |= SEC_RELOC;
5079 else
5081 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5082 set it (this is probably a bug) and if it is set
5083 assign_section_numbers will create a reloc section. */
5084 o->flags &=~ SEC_RELOC;
5087 /* If the SEC_ALLOC flag is not set, force the section VMA to
5088 zero. This is done in elf_fake_sections as well, but forcing
5089 the VMA to 0 here will ensure that relocs against these
5090 sections are handled correctly. */
5091 if ((o->flags & SEC_ALLOC) == 0
5092 && ! o->user_set_vma)
5093 o->vma = 0;
5096 if (! info->relocateable && merged)
5097 elf_link_hash_traverse (elf_hash_table (info),
5098 elf_link_sec_merge_syms, (PTR) abfd);
5100 /* Figure out the file positions for everything but the symbol table
5101 and the relocs. We set symcount to force assign_section_numbers
5102 to create a symbol table. */
5103 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5104 BFD_ASSERT (! abfd->output_has_begun);
5105 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5106 goto error_return;
5108 /* Figure out how many relocations we will have in each section.
5109 Just using RELOC_COUNT isn't good enough since that doesn't
5110 maintain a separate value for REL vs. RELA relocations. */
5111 if (emit_relocs)
5112 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5113 for (o = sub->sections; o != NULL; o = o->next)
5115 asection *output_section;
5117 if (! o->linker_mark)
5119 /* This section was omitted from the link. */
5120 continue;
5123 output_section = o->output_section;
5125 if (output_section != NULL
5126 && (o->flags & SEC_RELOC) != 0)
5128 struct bfd_elf_section_data *esdi
5129 = elf_section_data (o);
5130 struct bfd_elf_section_data *esdo
5131 = elf_section_data (output_section);
5132 unsigned int *rel_count;
5133 unsigned int *rel_count2;
5134 bfd_size_type entsize;
5135 bfd_size_type entsize2;
5137 /* We must be careful to add the relocations from the
5138 input section to the right output count. */
5139 entsize = esdi->rel_hdr.sh_entsize;
5140 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5141 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5142 || entsize == sizeof (Elf_External_Rela))
5143 && entsize2 != entsize
5144 && (entsize2 == 0
5145 || entsize2 == sizeof (Elf_External_Rel)
5146 || entsize2 == sizeof (Elf_External_Rela)));
5147 if (entsize == esdo->rel_hdr.sh_entsize)
5149 rel_count = &esdo->rel_count;
5150 rel_count2 = &esdo->rel_count2;
5152 else
5154 rel_count = &esdo->rel_count2;
5155 rel_count2 = &esdo->rel_count;
5158 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5159 if (esdi->rel_hdr2)
5160 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5161 output_section->flags |= SEC_RELOC;
5165 /* That created the reloc sections. Set their sizes, and assign
5166 them file positions, and allocate some buffers. */
5167 for (o = abfd->sections; o != NULL; o = o->next)
5169 if ((o->flags & SEC_RELOC) != 0)
5171 if (!elf_link_size_reloc_section (abfd,
5172 &elf_section_data (o)->rel_hdr,
5174 goto error_return;
5176 if (elf_section_data (o)->rel_hdr2
5177 && !elf_link_size_reloc_section (abfd,
5178 elf_section_data (o)->rel_hdr2,
5180 goto error_return;
5183 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5184 to count upwards while actually outputting the relocations. */
5185 elf_section_data (o)->rel_count = 0;
5186 elf_section_data (o)->rel_count2 = 0;
5189 _bfd_elf_assign_file_positions_for_relocs (abfd);
5191 /* We have now assigned file positions for all the sections except
5192 .symtab and .strtab. We start the .symtab section at the current
5193 file position, and write directly to it. We build the .strtab
5194 section in memory. */
5195 bfd_get_symcount (abfd) = 0;
5196 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5197 /* sh_name is set in prep_headers. */
5198 symtab_hdr->sh_type = SHT_SYMTAB;
5199 /* sh_flags, sh_addr and sh_size all start off zero. */
5200 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5201 /* sh_link is set in assign_section_numbers. */
5202 /* sh_info is set below. */
5203 /* sh_offset is set just below. */
5204 symtab_hdr->sh_addralign = bed->s->file_align;
5206 off = elf_tdata (abfd)->next_file_pos;
5207 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5209 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5210 incorrect. We do not yet know the size of the .symtab section.
5211 We correct next_file_pos below, after we do know the size. */
5213 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5214 continuously seeking to the right position in the file. */
5215 if (! info->keep_memory || max_sym_count < 20)
5216 finfo.symbuf_size = 20;
5217 else
5218 finfo.symbuf_size = max_sym_count;
5219 amt = finfo.symbuf_size;
5220 amt *= sizeof (Elf_External_Sym);
5221 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5222 if (finfo.symbuf == NULL)
5223 goto error_return;
5224 if (elf_numsections (abfd) > SHN_LORESERVE)
5226 /* Wild guess at number of output symbols. realloc'd as needed. */
5227 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
5228 finfo.shndxbuf_size = amt;
5229 amt *= sizeof (Elf_External_Sym_Shndx);
5230 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
5231 if (finfo.symshndxbuf == NULL)
5232 goto error_return;
5235 /* Start writing out the symbol table. The first symbol is always a
5236 dummy symbol. */
5237 if (info->strip != strip_all
5238 || emit_relocs)
5240 elfsym.st_value = 0;
5241 elfsym.st_size = 0;
5242 elfsym.st_info = 0;
5243 elfsym.st_other = 0;
5244 elfsym.st_shndx = SHN_UNDEF;
5245 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5246 &elfsym, bfd_und_section_ptr))
5247 goto error_return;
5250 #if 0
5251 /* Some standard ELF linkers do this, but we don't because it causes
5252 bootstrap comparison failures. */
5253 /* Output a file symbol for the output file as the second symbol.
5254 We output this even if we are discarding local symbols, although
5255 I'm not sure if this is correct. */
5256 elfsym.st_value = 0;
5257 elfsym.st_size = 0;
5258 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5259 elfsym.st_other = 0;
5260 elfsym.st_shndx = SHN_ABS;
5261 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5262 &elfsym, bfd_abs_section_ptr))
5263 goto error_return;
5264 #endif
5266 /* Output a symbol for each section. We output these even if we are
5267 discarding local symbols, since they are used for relocs. These
5268 symbols have no names. We store the index of each one in the
5269 index field of the section, so that we can find it again when
5270 outputting relocs. */
5271 if (info->strip != strip_all
5272 || emit_relocs)
5274 elfsym.st_size = 0;
5275 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5276 elfsym.st_other = 0;
5277 for (i = 1; i < elf_numsections (abfd); i++)
5279 o = section_from_elf_index (abfd, i);
5280 if (o != NULL)
5281 o->target_index = bfd_get_symcount (abfd);
5282 elfsym.st_shndx = i;
5283 if (info->relocateable || o == NULL)
5284 elfsym.st_value = 0;
5285 else
5286 elfsym.st_value = o->vma;
5287 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5288 &elfsym, o))
5289 goto error_return;
5290 if (i == SHN_LORESERVE - 1)
5291 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5295 /* Allocate some memory to hold information read in from the input
5296 files. */
5297 if (max_contents_size != 0)
5299 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5300 if (finfo.contents == NULL)
5301 goto error_return;
5304 if (max_external_reloc_size != 0)
5306 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5307 if (finfo.external_relocs == NULL)
5308 goto error_return;
5311 if (max_internal_reloc_count != 0)
5313 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5314 amt *= sizeof (Elf_Internal_Rela);
5315 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5316 if (finfo.internal_relocs == NULL)
5317 goto error_return;
5320 if (max_sym_count != 0)
5322 amt = max_sym_count * sizeof (Elf_External_Sym);
5323 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5324 if (finfo.external_syms == NULL)
5325 goto error_return;
5327 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5328 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5329 if (finfo.internal_syms == NULL)
5330 goto error_return;
5332 amt = max_sym_count * sizeof (long);
5333 finfo.indices = (long *) bfd_malloc (amt);
5334 if (finfo.indices == NULL)
5335 goto error_return;
5337 amt = max_sym_count * sizeof (asection *);
5338 finfo.sections = (asection **) bfd_malloc (amt);
5339 if (finfo.sections == NULL)
5340 goto error_return;
5343 if (max_sym_shndx_count != 0)
5345 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5346 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5347 if (finfo.locsym_shndx == NULL)
5348 goto error_return;
5351 if (finfo.first_tls_sec)
5353 unsigned int align = 0;
5354 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5355 asection *sec;
5357 for (sec = finfo.first_tls_sec;
5358 sec && (sec->flags & SEC_THREAD_LOCAL);
5359 sec = sec->next)
5361 bfd_vma size = sec->_raw_size;
5363 if (bfd_get_section_alignment (abfd, sec) > align)
5364 align = bfd_get_section_alignment (abfd, sec);
5365 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5367 struct bfd_link_order *o;
5369 size = 0;
5370 for (o = sec->link_order_head; o != NULL; o = o->next)
5371 if (size < o->offset + o->size)
5372 size = o->offset + o->size;
5374 end = sec->vma + size;
5376 elf_hash_table (info)->tls_segment
5377 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5378 if (elf_hash_table (info)->tls_segment == NULL)
5379 goto error_return;
5380 elf_hash_table (info)->tls_segment->start = base;
5381 elf_hash_table (info)->tls_segment->size = end - base;
5382 elf_hash_table (info)->tls_segment->align = align;
5385 /* Since ELF permits relocations to be against local symbols, we
5386 must have the local symbols available when we do the relocations.
5387 Since we would rather only read the local symbols once, and we
5388 would rather not keep them in memory, we handle all the
5389 relocations for a single input file at the same time.
5391 Unfortunately, there is no way to know the total number of local
5392 symbols until we have seen all of them, and the local symbol
5393 indices precede the global symbol indices. This means that when
5394 we are generating relocateable output, and we see a reloc against
5395 a global symbol, we can not know the symbol index until we have
5396 finished examining all the local symbols to see which ones we are
5397 going to output. To deal with this, we keep the relocations in
5398 memory, and don't output them until the end of the link. This is
5399 an unfortunate waste of memory, but I don't see a good way around
5400 it. Fortunately, it only happens when performing a relocateable
5401 link, which is not the common case. FIXME: If keep_memory is set
5402 we could write the relocs out and then read them again; I don't
5403 know how bad the memory loss will be. */
5405 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5406 sub->output_has_begun = false;
5407 for (o = abfd->sections; o != NULL; o = o->next)
5409 for (p = o->link_order_head; p != NULL; p = p->next)
5411 if (p->type == bfd_indirect_link_order
5412 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
5413 == bfd_target_elf_flavour)
5414 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
5416 if (! sub->output_has_begun)
5418 if (! elf_link_input_bfd (&finfo, sub))
5419 goto error_return;
5420 sub->output_has_begun = true;
5423 else if (p->type == bfd_section_reloc_link_order
5424 || p->type == bfd_symbol_reloc_link_order)
5426 if (! elf_reloc_link_order (abfd, info, o, p))
5427 goto error_return;
5429 else
5431 if (! _bfd_default_link_order (abfd, info, o, p))
5432 goto error_return;
5437 /* Output any global symbols that got converted to local in a
5438 version script or due to symbol visibility. We do this in a
5439 separate step since ELF requires all local symbols to appear
5440 prior to any global symbols. FIXME: We should only do this if
5441 some global symbols were, in fact, converted to become local.
5442 FIXME: Will this work correctly with the Irix 5 linker? */
5443 eoinfo.failed = false;
5444 eoinfo.finfo = &finfo;
5445 eoinfo.localsyms = true;
5446 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5447 (PTR) &eoinfo);
5448 if (eoinfo.failed)
5449 return false;
5451 /* That wrote out all the local symbols. Finish up the symbol table
5452 with the global symbols. Even if we want to strip everything we
5453 can, we still need to deal with those global symbols that got
5454 converted to local in a version script. */
5456 /* The sh_info field records the index of the first non local symbol. */
5457 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5459 if (dynamic
5460 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5462 Elf_Internal_Sym sym;
5463 Elf_External_Sym *dynsym =
5464 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5465 long last_local = 0;
5467 /* Write out the section symbols for the output sections. */
5468 if (info->shared)
5470 asection *s;
5472 sym.st_size = 0;
5473 sym.st_name = 0;
5474 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5475 sym.st_other = 0;
5477 for (s = abfd->sections; s != NULL; s = s->next)
5479 int indx;
5480 Elf_External_Sym *dest;
5482 indx = elf_section_data (s)->this_idx;
5483 BFD_ASSERT (indx > 0);
5484 sym.st_shndx = indx;
5485 sym.st_value = s->vma;
5486 dest = dynsym + elf_section_data (s)->dynindx;
5487 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5490 last_local = bfd_count_sections (abfd);
5493 /* Write out the local dynsyms. */
5494 if (elf_hash_table (info)->dynlocal)
5496 struct elf_link_local_dynamic_entry *e;
5497 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5499 asection *s;
5500 Elf_External_Sym *dest;
5502 sym.st_size = e->isym.st_size;
5503 sym.st_other = e->isym.st_other;
5505 /* Copy the internal symbol as is.
5506 Note that we saved a word of storage and overwrote
5507 the original st_name with the dynstr_index. */
5508 sym = e->isym;
5510 if (e->isym.st_shndx != SHN_UNDEF
5511 && (e->isym.st_shndx < SHN_LORESERVE
5512 || e->isym.st_shndx > SHN_HIRESERVE))
5514 s = bfd_section_from_elf_index (e->input_bfd,
5515 e->isym.st_shndx);
5517 sym.st_shndx =
5518 elf_section_data (s->output_section)->this_idx;
5519 sym.st_value = (s->output_section->vma
5520 + s->output_offset
5521 + e->isym.st_value);
5524 if (last_local < e->dynindx)
5525 last_local = e->dynindx;
5527 dest = dynsym + e->dynindx;
5528 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5532 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5533 last_local + 1;
5536 /* We get the global symbols from the hash table. */
5537 eoinfo.failed = false;
5538 eoinfo.localsyms = false;
5539 eoinfo.finfo = &finfo;
5540 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5541 (PTR) &eoinfo);
5542 if (eoinfo.failed)
5543 return false;
5545 /* If backend needs to output some symbols not present in the hash
5546 table, do it now. */
5547 if (bed->elf_backend_output_arch_syms)
5549 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5550 Elf_Internal_Sym *,
5551 asection *));
5553 if (! ((*bed->elf_backend_output_arch_syms)
5554 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5555 return false;
5558 /* Flush all symbols to the file. */
5559 if (! elf_link_flush_output_syms (&finfo))
5560 return false;
5562 /* Now we know the size of the symtab section. */
5563 off += symtab_hdr->sh_size;
5565 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5566 if (symtab_shndx_hdr->sh_name != 0)
5568 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5569 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5570 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5571 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
5572 symtab_shndx_hdr->sh_size = amt;
5574 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
5575 off, true);
5577 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
5578 || (bfd_bwrite ((PTR) finfo.symshndxbuf, amt, abfd) != amt))
5579 return false;
5583 /* Finish up and write out the symbol string table (.strtab)
5584 section. */
5585 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5586 /* sh_name was set in prep_headers. */
5587 symstrtab_hdr->sh_type = SHT_STRTAB;
5588 symstrtab_hdr->sh_flags = 0;
5589 symstrtab_hdr->sh_addr = 0;
5590 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5591 symstrtab_hdr->sh_entsize = 0;
5592 symstrtab_hdr->sh_link = 0;
5593 symstrtab_hdr->sh_info = 0;
5594 /* sh_offset is set just below. */
5595 symstrtab_hdr->sh_addralign = 1;
5597 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5598 elf_tdata (abfd)->next_file_pos = off;
5600 if (bfd_get_symcount (abfd) > 0)
5602 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5603 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5604 return false;
5607 /* Adjust the relocs to have the correct symbol indices. */
5608 for (o = abfd->sections; o != NULL; o = o->next)
5610 if ((o->flags & SEC_RELOC) == 0)
5611 continue;
5613 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5614 elf_section_data (o)->rel_count,
5615 elf_section_data (o)->rel_hashes);
5616 if (elf_section_data (o)->rel_hdr2 != NULL)
5617 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5618 elf_section_data (o)->rel_count2,
5619 (elf_section_data (o)->rel_hashes
5620 + elf_section_data (o)->rel_count));
5622 /* Set the reloc_count field to 0 to prevent write_relocs from
5623 trying to swap the relocs out itself. */
5624 o->reloc_count = 0;
5627 if (dynamic && info->combreloc && dynobj != NULL)
5628 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5630 /* If we are linking against a dynamic object, or generating a
5631 shared library, finish up the dynamic linking information. */
5632 if (dynamic)
5634 Elf_External_Dyn *dyncon, *dynconend;
5636 /* Fix up .dynamic entries. */
5637 o = bfd_get_section_by_name (dynobj, ".dynamic");
5638 BFD_ASSERT (o != NULL);
5640 dyncon = (Elf_External_Dyn *) o->contents;
5641 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5642 for (; dyncon < dynconend; dyncon++)
5644 Elf_Internal_Dyn dyn;
5645 const char *name;
5646 unsigned int type;
5648 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5650 switch (dyn.d_tag)
5652 default:
5653 break;
5654 case DT_NULL:
5655 if (relativecount > 0 && dyncon + 1 < dynconend)
5657 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5659 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5660 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5661 default: break;
5663 if (dyn.d_tag != DT_NULL)
5665 dyn.d_un.d_val = relativecount;
5666 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5667 relativecount = 0;
5670 break;
5671 case DT_INIT:
5672 name = info->init_function;
5673 goto get_sym;
5674 case DT_FINI:
5675 name = info->fini_function;
5676 get_sym:
5678 struct elf_link_hash_entry *h;
5680 h = elf_link_hash_lookup (elf_hash_table (info), name,
5681 false, false, true);
5682 if (h != NULL
5683 && (h->root.type == bfd_link_hash_defined
5684 || h->root.type == bfd_link_hash_defweak))
5686 dyn.d_un.d_val = h->root.u.def.value;
5687 o = h->root.u.def.section;
5688 if (o->output_section != NULL)
5689 dyn.d_un.d_val += (o->output_section->vma
5690 + o->output_offset);
5691 else
5693 /* The symbol is imported from another shared
5694 library and does not apply to this one. */
5695 dyn.d_un.d_val = 0;
5698 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5701 break;
5703 case DT_PREINIT_ARRAYSZ:
5704 name = ".preinit_array";
5705 goto get_size;
5706 case DT_INIT_ARRAYSZ:
5707 name = ".init_array";
5708 goto get_size;
5709 case DT_FINI_ARRAYSZ:
5710 name = ".fini_array";
5711 get_size:
5712 o = bfd_get_section_by_name (abfd, name);
5713 if (o == NULL)
5715 (*_bfd_error_handler)
5716 (_("%s: could not find output section %s"),
5717 bfd_get_filename (abfd), name);
5718 goto error_return;
5720 if (o->_raw_size == 0)
5721 (*_bfd_error_handler)
5722 (_("warning: %s section has zero size"), name);
5723 dyn.d_un.d_val = o->_raw_size;
5724 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5725 break;
5727 case DT_PREINIT_ARRAY:
5728 name = ".preinit_array";
5729 goto get_vma;
5730 case DT_INIT_ARRAY:
5731 name = ".init_array";
5732 goto get_vma;
5733 case DT_FINI_ARRAY:
5734 name = ".fini_array";
5735 goto get_vma;
5737 case DT_HASH:
5738 name = ".hash";
5739 goto get_vma;
5740 case DT_STRTAB:
5741 name = ".dynstr";
5742 goto get_vma;
5743 case DT_SYMTAB:
5744 name = ".dynsym";
5745 goto get_vma;
5746 case DT_VERDEF:
5747 name = ".gnu.version_d";
5748 goto get_vma;
5749 case DT_VERNEED:
5750 name = ".gnu.version_r";
5751 goto get_vma;
5752 case DT_VERSYM:
5753 name = ".gnu.version";
5754 get_vma:
5755 o = bfd_get_section_by_name (abfd, name);
5756 if (o == NULL)
5758 (*_bfd_error_handler)
5759 (_("%s: could not find output section %s"),
5760 bfd_get_filename (abfd), name);
5761 goto error_return;
5763 dyn.d_un.d_ptr = o->vma;
5764 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5765 break;
5767 case DT_REL:
5768 case DT_RELA:
5769 case DT_RELSZ:
5770 case DT_RELASZ:
5771 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5772 type = SHT_REL;
5773 else
5774 type = SHT_RELA;
5775 dyn.d_un.d_val = 0;
5776 for (i = 1; i < elf_numsections (abfd); i++)
5778 Elf_Internal_Shdr *hdr;
5780 hdr = elf_elfsections (abfd)[i];
5781 if (hdr->sh_type == type
5782 && (hdr->sh_flags & SHF_ALLOC) != 0)
5784 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5785 dyn.d_un.d_val += hdr->sh_size;
5786 else
5788 if (dyn.d_un.d_val == 0
5789 || hdr->sh_addr < dyn.d_un.d_val)
5790 dyn.d_un.d_val = hdr->sh_addr;
5794 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5795 break;
5800 /* If we have created any dynamic sections, then output them. */
5801 if (dynobj != NULL)
5803 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5804 goto error_return;
5806 for (o = dynobj->sections; o != NULL; o = o->next)
5808 if ((o->flags & SEC_HAS_CONTENTS) == 0
5809 || o->_raw_size == 0
5810 || o->output_section == bfd_abs_section_ptr)
5811 continue;
5812 if ((o->flags & SEC_LINKER_CREATED) == 0)
5814 /* At this point, we are only interested in sections
5815 created by elf_link_create_dynamic_sections. */
5816 continue;
5818 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5819 != SHT_STRTAB)
5820 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5822 if (! bfd_set_section_contents (abfd, o->output_section,
5823 o->contents,
5824 (file_ptr) o->output_offset,
5825 o->_raw_size))
5826 goto error_return;
5828 else
5830 /* The contents of the .dynstr section are actually in a
5831 stringtab. */
5832 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5833 if (bfd_seek (abfd, off, SEEK_SET) != 0
5834 || ! _bfd_elf_strtab_emit (abfd,
5835 elf_hash_table (info)->dynstr))
5836 goto error_return;
5841 if (info->relocateable)
5843 boolean failed = false;
5845 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5846 if (failed)
5847 goto error_return;
5850 /* If we have optimized stabs strings, output them. */
5851 if (elf_hash_table (info)->stab_info != NULL)
5853 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5854 goto error_return;
5857 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5859 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5860 ".eh_frame_hdr");
5861 if (o
5862 && (elf_section_data (o)->sec_info_type
5863 == ELF_INFO_TYPE_EH_FRAME_HDR))
5865 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5866 goto error_return;
5870 if (finfo.symstrtab != NULL)
5871 _bfd_stringtab_free (finfo.symstrtab);
5872 if (finfo.contents != NULL)
5873 free (finfo.contents);
5874 if (finfo.external_relocs != NULL)
5875 free (finfo.external_relocs);
5876 if (finfo.internal_relocs != NULL)
5877 free (finfo.internal_relocs);
5878 if (finfo.external_syms != NULL)
5879 free (finfo.external_syms);
5880 if (finfo.locsym_shndx != NULL)
5881 free (finfo.locsym_shndx);
5882 if (finfo.internal_syms != NULL)
5883 free (finfo.internal_syms);
5884 if (finfo.indices != NULL)
5885 free (finfo.indices);
5886 if (finfo.sections != NULL)
5887 free (finfo.sections);
5888 if (finfo.symbuf != NULL)
5889 free (finfo.symbuf);
5890 if (finfo.symshndxbuf != NULL)
5891 free (finfo.symshndxbuf);
5892 for (o = abfd->sections; o != NULL; o = o->next)
5894 if ((o->flags & SEC_RELOC) != 0
5895 && elf_section_data (o)->rel_hashes != NULL)
5896 free (elf_section_data (o)->rel_hashes);
5899 elf_tdata (abfd)->linker = true;
5901 return true;
5903 error_return:
5904 if (finfo.symstrtab != NULL)
5905 _bfd_stringtab_free (finfo.symstrtab);
5906 if (finfo.contents != NULL)
5907 free (finfo.contents);
5908 if (finfo.external_relocs != NULL)
5909 free (finfo.external_relocs);
5910 if (finfo.internal_relocs != NULL)
5911 free (finfo.internal_relocs);
5912 if (finfo.external_syms != NULL)
5913 free (finfo.external_syms);
5914 if (finfo.locsym_shndx != NULL)
5915 free (finfo.locsym_shndx);
5916 if (finfo.internal_syms != NULL)
5917 free (finfo.internal_syms);
5918 if (finfo.indices != NULL)
5919 free (finfo.indices);
5920 if (finfo.sections != NULL)
5921 free (finfo.sections);
5922 if (finfo.symbuf != NULL)
5923 free (finfo.symbuf);
5924 if (finfo.symshndxbuf != NULL)
5925 free (finfo.symshndxbuf);
5926 for (o = abfd->sections; o != NULL; o = o->next)
5928 if ((o->flags & SEC_RELOC) != 0
5929 && elf_section_data (o)->rel_hashes != NULL)
5930 free (elf_section_data (o)->rel_hashes);
5933 return false;
5936 /* Add a symbol to the output symbol table. */
5938 static boolean
5939 elf_link_output_sym (finfo, name, elfsym, input_sec)
5940 struct elf_final_link_info *finfo;
5941 const char *name;
5942 Elf_Internal_Sym *elfsym;
5943 asection *input_sec;
5945 Elf_External_Sym *dest;
5946 Elf_External_Sym_Shndx *destshndx;
5948 boolean (*output_symbol_hook) PARAMS ((bfd *,
5949 struct bfd_link_info *info,
5950 const char *,
5951 Elf_Internal_Sym *,
5952 asection *));
5954 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5955 elf_backend_link_output_symbol_hook;
5956 if (output_symbol_hook != NULL)
5958 if (! ((*output_symbol_hook)
5959 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5960 return false;
5963 if (name == (const char *) NULL || *name == '\0')
5964 elfsym->st_name = 0;
5965 else if (input_sec->flags & SEC_EXCLUDE)
5966 elfsym->st_name = 0;
5967 else
5969 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5970 name, true, false);
5971 if (elfsym->st_name == (unsigned long) -1)
5972 return false;
5975 if (finfo->symbuf_count >= finfo->symbuf_size)
5977 if (! elf_link_flush_output_syms (finfo))
5978 return false;
5981 dest = finfo->symbuf + finfo->symbuf_count;
5982 destshndx = finfo->symshndxbuf;
5983 if (destshndx != NULL)
5985 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5987 bfd_size_type amt;
5989 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5990 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5991 if (destshndx == NULL)
5992 return false;
5993 memset ((char *) destshndx + amt, 0, amt);
5994 finfo->shndxbuf_size *= 2;
5996 destshndx += bfd_get_symcount (finfo->output_bfd);
5999 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
6000 finfo->symbuf_count += 1;
6001 bfd_get_symcount (finfo->output_bfd) += 1;
6003 return true;
6006 /* Flush the output symbols to the file. */
6008 static boolean
6009 elf_link_flush_output_syms (finfo)
6010 struct elf_final_link_info *finfo;
6012 if (finfo->symbuf_count > 0)
6014 Elf_Internal_Shdr *hdr;
6015 file_ptr pos;
6016 bfd_size_type amt;
6018 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6019 pos = hdr->sh_offset + hdr->sh_size;
6020 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
6021 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6022 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
6023 return false;
6025 hdr->sh_size += amt;
6026 finfo->symbuf_count = 0;
6029 return true;
6032 /* Adjust all external symbols pointing into SEC_MERGE sections
6033 to reflect the object merging within the sections. */
6035 static boolean
6036 elf_link_sec_merge_syms (h, data)
6037 struct elf_link_hash_entry *h;
6038 PTR data;
6040 asection *sec;
6042 if (h->root.type == bfd_link_hash_warning)
6043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6045 if ((h->root.type == bfd_link_hash_defined
6046 || h->root.type == bfd_link_hash_defweak)
6047 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
6048 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6050 bfd *output_bfd = (bfd *) data;
6052 h->root.u.def.value =
6053 _bfd_merged_section_offset (output_bfd,
6054 &h->root.u.def.section,
6055 elf_section_data (sec)->sec_info,
6056 h->root.u.def.value, (bfd_vma) 0);
6059 return true;
6062 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6063 allowing an unsatisfied unversioned symbol in the DSO to match a
6064 versioned symbol that would normally require an explicit version. */
6066 static boolean
6067 elf_link_check_versioned_symbol (info, h)
6068 struct bfd_link_info *info;
6069 struct elf_link_hash_entry *h;
6071 bfd *undef_bfd = h->root.u.undef.abfd;
6072 struct elf_link_loaded_list *loaded;
6074 if ((undef_bfd->flags & DYNAMIC) == 0
6075 || info->hash->creator->flavour != bfd_target_elf_flavour
6076 || elf_dt_soname (h->root.u.undef.abfd) == NULL)
6077 return false;
6079 for (loaded = elf_hash_table (info)->loaded;
6080 loaded != NULL;
6081 loaded = loaded->next)
6083 bfd *input;
6084 Elf_Internal_Shdr *hdr;
6085 bfd_size_type symcount;
6086 bfd_size_type extsymcount;
6087 bfd_size_type extsymoff;
6088 Elf_Internal_Shdr *versymhdr;
6089 Elf_Internal_Sym *isym;
6090 Elf_Internal_Sym *isymend;
6091 Elf_Internal_Sym *isymbuf;
6092 Elf_External_Versym *ever;
6093 Elf_External_Versym *extversym;
6095 input = loaded->abfd;
6097 /* We check each DSO for a possible hidden versioned definition. */
6098 if (input == undef_bfd
6099 || (input->flags & DYNAMIC) == 0
6100 || elf_dynversym (input) == 0)
6101 continue;
6103 hdr = &elf_tdata (input)->dynsymtab_hdr;
6105 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
6106 if (elf_bad_symtab (input))
6108 extsymcount = symcount;
6109 extsymoff = 0;
6111 else
6113 extsymcount = symcount - hdr->sh_info;
6114 extsymoff = hdr->sh_info;
6117 if (extsymcount == 0)
6118 continue;
6120 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6121 NULL, NULL, NULL);
6122 if (isymbuf == NULL)
6123 return false;
6125 /* Read in any version definitions. */
6126 versymhdr = &elf_tdata (input)->dynversym_hdr;
6127 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
6128 if (extversym == NULL)
6129 goto error_ret;
6131 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6132 || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
6133 != versymhdr->sh_size))
6135 free (extversym);
6136 error_ret:
6137 free (isymbuf);
6138 return false;
6141 ever = extversym + extsymoff;
6142 isymend = isymbuf + extsymcount;
6143 for (isym = isymbuf; isym < isymend; isym++, ever++)
6145 const char *name;
6146 Elf_Internal_Versym iver;
6148 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6149 || isym->st_shndx == SHN_UNDEF)
6150 continue;
6152 name = bfd_elf_string_from_elf_section (input,
6153 hdr->sh_link,
6154 isym->st_name);
6155 if (strcmp (name, h->root.root.string) != 0)
6156 continue;
6158 _bfd_elf_swap_versym_in (input, ever, &iver);
6160 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6162 /* If we have a non-hidden versioned sym, then it should
6163 have provided a definition for the undefined sym. */
6164 abort ();
6167 if ((iver.vs_vers & VERSYM_VERSION) == 2)
6169 /* This is the oldest (default) sym. We can use it. */
6170 free (extversym);
6171 free (isymbuf);
6172 return true;
6176 free (extversym);
6177 free (isymbuf);
6180 return false;
6183 /* Add an external symbol to the symbol table. This is called from
6184 the hash table traversal routine. When generating a shared object,
6185 we go through the symbol table twice. The first time we output
6186 anything that might have been forced to local scope in a version
6187 script. The second time we output the symbols that are still
6188 global symbols. */
6190 static boolean
6191 elf_link_output_extsym (h, data)
6192 struct elf_link_hash_entry *h;
6193 PTR data;
6195 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6196 struct elf_final_link_info *finfo = eoinfo->finfo;
6197 boolean strip;
6198 Elf_Internal_Sym sym;
6199 asection *input_sec;
6201 if (h->root.type == bfd_link_hash_warning)
6203 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6204 if (h->root.type == bfd_link_hash_new)
6205 return true;
6208 /* Decide whether to output this symbol in this pass. */
6209 if (eoinfo->localsyms)
6211 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6212 return true;
6214 else
6216 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6217 return true;
6220 /* If we are not creating a shared library, and this symbol is
6221 referenced by a shared library but is not defined anywhere, then
6222 warn that it is undefined. If we do not do this, the runtime
6223 linker will complain that the symbol is undefined when the
6224 program is run. We don't have to worry about symbols that are
6225 referenced by regular files, because we will already have issued
6226 warnings for them. */
6227 if (! finfo->info->relocateable
6228 && ! finfo->info->allow_shlib_undefined
6229 && ! finfo->info->shared
6230 && h->root.type == bfd_link_hash_undefined
6231 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6232 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6233 && ! elf_link_check_versioned_symbol (finfo->info, h))
6235 if (! ((*finfo->info->callbacks->undefined_symbol)
6236 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6237 (asection *) NULL, (bfd_vma) 0, true)))
6239 eoinfo->failed = true;
6240 return false;
6244 /* We don't want to output symbols that have never been mentioned by
6245 a regular file, or that we have been told to strip. However, if
6246 h->indx is set to -2, the symbol is used by a reloc and we must
6247 output it. */
6248 if (h->indx == -2)
6249 strip = false;
6250 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6251 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6252 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6253 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6254 strip = true;
6255 else if (finfo->info->strip == strip_all
6256 || (finfo->info->strip == strip_some
6257 && bfd_hash_lookup (finfo->info->keep_hash,
6258 h->root.root.string,
6259 false, false) == NULL))
6260 strip = true;
6261 else
6262 strip = false;
6264 /* If we're stripping it, and it's not a dynamic symbol, there's
6265 nothing else to do unless it is a forced local symbol. */
6266 if (strip
6267 && h->dynindx == -1
6268 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6269 return true;
6271 sym.st_value = 0;
6272 sym.st_size = h->size;
6273 sym.st_other = h->other;
6274 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6275 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6276 else if (h->root.type == bfd_link_hash_undefweak
6277 || h->root.type == bfd_link_hash_defweak)
6278 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6279 else
6280 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6282 switch (h->root.type)
6284 default:
6285 case bfd_link_hash_new:
6286 case bfd_link_hash_warning:
6287 abort ();
6288 return false;
6290 case bfd_link_hash_undefined:
6291 case bfd_link_hash_undefweak:
6292 input_sec = bfd_und_section_ptr;
6293 sym.st_shndx = SHN_UNDEF;
6294 break;
6296 case bfd_link_hash_defined:
6297 case bfd_link_hash_defweak:
6299 input_sec = h->root.u.def.section;
6300 if (input_sec->output_section != NULL)
6302 sym.st_shndx =
6303 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6304 input_sec->output_section);
6305 if (sym.st_shndx == SHN_BAD)
6307 (*_bfd_error_handler)
6308 (_("%s: could not find output section %s for input section %s"),
6309 bfd_get_filename (finfo->output_bfd),
6310 input_sec->output_section->name,
6311 input_sec->name);
6312 eoinfo->failed = true;
6313 return false;
6316 /* ELF symbols in relocateable files are section relative,
6317 but in nonrelocateable files they are virtual
6318 addresses. */
6319 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6320 if (! finfo->info->relocateable)
6322 sym.st_value += input_sec->output_section->vma;
6323 if (h->type == STT_TLS)
6325 /* STT_TLS symbols are relative to PT_TLS segment
6326 base. */
6327 BFD_ASSERT (finfo->first_tls_sec != NULL);
6328 sym.st_value -= finfo->first_tls_sec->vma;
6332 else
6334 BFD_ASSERT (input_sec->owner == NULL
6335 || (input_sec->owner->flags & DYNAMIC) != 0);
6336 sym.st_shndx = SHN_UNDEF;
6337 input_sec = bfd_und_section_ptr;
6340 break;
6342 case bfd_link_hash_common:
6343 input_sec = h->root.u.c.p->section;
6344 sym.st_shndx = SHN_COMMON;
6345 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6346 break;
6348 case bfd_link_hash_indirect:
6349 /* These symbols are created by symbol versioning. They point
6350 to the decorated version of the name. For example, if the
6351 symbol foo@@GNU_1.2 is the default, which should be used when
6352 foo is used with no version, then we add an indirect symbol
6353 foo which points to foo@@GNU_1.2. We ignore these symbols,
6354 since the indirected symbol is already in the hash table. */
6355 return true;
6358 /* Give the processor backend a chance to tweak the symbol value,
6359 and also to finish up anything that needs to be done for this
6360 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6361 forced local syms when non-shared is due to a historical quirk. */
6362 if ((h->dynindx != -1
6363 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6364 && (finfo->info->shared
6365 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6366 && elf_hash_table (finfo->info)->dynamic_sections_created)
6368 struct elf_backend_data *bed;
6370 bed = get_elf_backend_data (finfo->output_bfd);
6371 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6372 (finfo->output_bfd, finfo->info, h, &sym)))
6374 eoinfo->failed = true;
6375 return false;
6379 /* If we are marking the symbol as undefined, and there are no
6380 non-weak references to this symbol from a regular object, then
6381 mark the symbol as weak undefined; if there are non-weak
6382 references, mark the symbol as strong. We can't do this earlier,
6383 because it might not be marked as undefined until the
6384 finish_dynamic_symbol routine gets through with it. */
6385 if (sym.st_shndx == SHN_UNDEF
6386 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6387 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6388 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6390 int bindtype;
6392 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6393 bindtype = STB_GLOBAL;
6394 else
6395 bindtype = STB_WEAK;
6396 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6399 /* If a symbol is not defined locally, we clear the visibility
6400 field. */
6401 if (! finfo->info->relocateable
6402 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6403 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6405 /* If this symbol should be put in the .dynsym section, then put it
6406 there now. We already know the symbol index. We also fill in
6407 the entry in the .hash section. */
6408 if (h->dynindx != -1
6409 && elf_hash_table (finfo->info)->dynamic_sections_created)
6411 size_t bucketcount;
6412 size_t bucket;
6413 size_t hash_entry_size;
6414 bfd_byte *bucketpos;
6415 bfd_vma chain;
6416 Elf_External_Sym *esym;
6418 sym.st_name = h->dynstr_index;
6419 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6420 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6422 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6423 bucket = h->elf_hash_value % bucketcount;
6424 hash_entry_size
6425 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6426 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6427 + (bucket + 2) * hash_entry_size);
6428 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6429 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6430 bucketpos);
6431 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6432 ((bfd_byte *) finfo->hash_sec->contents
6433 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6435 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6437 Elf_Internal_Versym iversym;
6438 Elf_External_Versym *eversym;
6440 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6442 if (h->verinfo.verdef == NULL)
6443 iversym.vs_vers = 0;
6444 else
6445 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6447 else
6449 if (h->verinfo.vertree == NULL)
6450 iversym.vs_vers = 1;
6451 else
6452 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6455 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6456 iversym.vs_vers |= VERSYM_HIDDEN;
6458 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6459 eversym += h->dynindx;
6460 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6464 /* If we're stripping it, then it was just a dynamic symbol, and
6465 there's nothing else to do. */
6466 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6467 return true;
6469 h->indx = bfd_get_symcount (finfo->output_bfd);
6471 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6473 eoinfo->failed = true;
6474 return false;
6477 return true;
6480 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6481 originated from the section given by INPUT_REL_HDR) to the
6482 OUTPUT_BFD. */
6484 static boolean
6485 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6486 internal_relocs)
6487 bfd *output_bfd;
6488 asection *input_section;
6489 Elf_Internal_Shdr *input_rel_hdr;
6490 Elf_Internal_Rela *internal_relocs;
6492 Elf_Internal_Rela *irela;
6493 Elf_Internal_Rela *irelaend;
6494 Elf_Internal_Shdr *output_rel_hdr;
6495 asection *output_section;
6496 unsigned int *rel_countp = NULL;
6497 struct elf_backend_data *bed;
6498 bfd_size_type amt;
6500 output_section = input_section->output_section;
6501 output_rel_hdr = NULL;
6503 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6504 == input_rel_hdr->sh_entsize)
6506 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6507 rel_countp = &elf_section_data (output_section)->rel_count;
6509 else if (elf_section_data (output_section)->rel_hdr2
6510 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6511 == input_rel_hdr->sh_entsize))
6513 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6514 rel_countp = &elf_section_data (output_section)->rel_count2;
6516 else
6518 (*_bfd_error_handler)
6519 (_("%s: relocation size mismatch in %s section %s"),
6520 bfd_get_filename (output_bfd),
6521 bfd_archive_filename (input_section->owner),
6522 input_section->name);
6523 bfd_set_error (bfd_error_wrong_object_format);
6524 return false;
6527 bed = get_elf_backend_data (output_bfd);
6528 irela = internal_relocs;
6529 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
6530 * bed->s->int_rels_per_ext_rel);
6532 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6534 Elf_External_Rel *erel;
6535 Elf_Internal_Rel *irel;
6537 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6538 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6539 if (irel == NULL)
6541 (*_bfd_error_handler) (_("Error: out of memory"));
6542 abort ();
6545 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6546 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6548 unsigned int i;
6550 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6552 irel[i].r_offset = irela[i].r_offset;
6553 irel[i].r_info = irela[i].r_info;
6554 BFD_ASSERT (irela[i].r_addend == 0);
6557 if (bed->s->swap_reloc_out)
6558 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6559 else
6560 elf_swap_reloc_out (output_bfd, irel, erel);
6563 free (irel);
6565 else
6567 Elf_External_Rela *erela;
6569 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6571 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6572 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6573 if (bed->s->swap_reloca_out)
6574 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6575 else
6576 elf_swap_reloca_out (output_bfd, irela, erela);
6579 /* Bump the counter, so that we know where to add the next set of
6580 relocations. */
6581 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6583 return true;
6586 /* Link an input file into the linker output file. This function
6587 handles all the sections and relocations of the input file at once.
6588 This is so that we only have to read the local symbols once, and
6589 don't have to keep them in memory. */
6591 static boolean
6592 elf_link_input_bfd (finfo, input_bfd)
6593 struct elf_final_link_info *finfo;
6594 bfd *input_bfd;
6596 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6597 bfd *, asection *, bfd_byte *,
6598 Elf_Internal_Rela *,
6599 Elf_Internal_Sym *, asection **));
6600 bfd *output_bfd;
6601 Elf_Internal_Shdr *symtab_hdr;
6602 size_t locsymcount;
6603 size_t extsymoff;
6604 Elf_Internal_Sym *isymbuf;
6605 Elf_Internal_Sym *isym;
6606 Elf_Internal_Sym *isymend;
6607 long *pindex;
6608 asection **ppsection;
6609 asection *o;
6610 struct elf_backend_data *bed;
6611 boolean emit_relocs;
6612 struct elf_link_hash_entry **sym_hashes;
6614 output_bfd = finfo->output_bfd;
6615 bed = get_elf_backend_data (output_bfd);
6616 relocate_section = bed->elf_backend_relocate_section;
6618 /* If this is a dynamic object, we don't want to do anything here:
6619 we don't want the local symbols, and we don't want the section
6620 contents. */
6621 if ((input_bfd->flags & DYNAMIC) != 0)
6622 return true;
6624 emit_relocs = (finfo->info->relocateable
6625 || finfo->info->emitrelocations
6626 || bed->elf_backend_emit_relocs);
6628 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6629 if (elf_bad_symtab (input_bfd))
6631 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6632 extsymoff = 0;
6634 else
6636 locsymcount = symtab_hdr->sh_info;
6637 extsymoff = symtab_hdr->sh_info;
6640 /* Read the local symbols. */
6641 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6642 if (isymbuf == NULL && locsymcount != 0)
6644 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6645 finfo->internal_syms,
6646 finfo->external_syms,
6647 finfo->locsym_shndx);
6648 if (isymbuf == NULL)
6649 return false;
6652 /* Find local symbol sections and adjust values of symbols in
6653 SEC_MERGE sections. Write out those local symbols we know are
6654 going into the output file. */
6655 isymend = isymbuf + locsymcount;
6656 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6657 isym < isymend;
6658 isym++, pindex++, ppsection++)
6660 asection *isec;
6661 const char *name;
6662 Elf_Internal_Sym osym;
6664 *pindex = -1;
6666 if (elf_bad_symtab (input_bfd))
6668 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6670 *ppsection = NULL;
6671 continue;
6675 if (isym->st_shndx == SHN_UNDEF)
6676 isec = bfd_und_section_ptr;
6677 else if (isym->st_shndx < SHN_LORESERVE
6678 || isym->st_shndx > SHN_HIRESERVE)
6680 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6681 if (isec
6682 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6683 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6684 isym->st_value =
6685 _bfd_merged_section_offset (output_bfd, &isec,
6686 elf_section_data (isec)->sec_info,
6687 isym->st_value, (bfd_vma) 0);
6689 else if (isym->st_shndx == SHN_ABS)
6690 isec = bfd_abs_section_ptr;
6691 else if (isym->st_shndx == SHN_COMMON)
6692 isec = bfd_com_section_ptr;
6693 else
6695 /* Who knows? */
6696 isec = NULL;
6699 *ppsection = isec;
6701 /* Don't output the first, undefined, symbol. */
6702 if (ppsection == finfo->sections)
6703 continue;
6705 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6707 /* We never output section symbols. Instead, we use the
6708 section symbol of the corresponding section in the output
6709 file. */
6710 continue;
6713 /* If we are stripping all symbols, we don't want to output this
6714 one. */
6715 if (finfo->info->strip == strip_all)
6716 continue;
6718 /* If we are discarding all local symbols, we don't want to
6719 output this one. If we are generating a relocateable output
6720 file, then some of the local symbols may be required by
6721 relocs; we output them below as we discover that they are
6722 needed. */
6723 if (finfo->info->discard == discard_all)
6724 continue;
6726 /* If this symbol is defined in a section which we are
6727 discarding, we don't need to keep it, but note that
6728 linker_mark is only reliable for sections that have contents.
6729 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6730 as well as linker_mark. */
6731 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6732 && isec != NULL
6733 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6734 || (! finfo->info->relocateable
6735 && (isec->flags & SEC_EXCLUDE) != 0)))
6736 continue;
6738 /* Get the name of the symbol. */
6739 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6740 isym->st_name);
6741 if (name == NULL)
6742 return false;
6744 /* See if we are discarding symbols with this name. */
6745 if ((finfo->info->strip == strip_some
6746 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6747 == NULL))
6748 || (((finfo->info->discard == discard_sec_merge
6749 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6750 || finfo->info->discard == discard_l)
6751 && bfd_is_local_label_name (input_bfd, name)))
6752 continue;
6754 /* If we get here, we are going to output this symbol. */
6756 osym = *isym;
6758 /* Adjust the section index for the output file. */
6759 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6760 isec->output_section);
6761 if (osym.st_shndx == SHN_BAD)
6762 return false;
6764 *pindex = bfd_get_symcount (output_bfd);
6766 /* ELF symbols in relocateable files are section relative, but
6767 in executable files they are virtual addresses. Note that
6768 this code assumes that all ELF sections have an associated
6769 BFD section with a reasonable value for output_offset; below
6770 we assume that they also have a reasonable value for
6771 output_section. Any special sections must be set up to meet
6772 these requirements. */
6773 osym.st_value += isec->output_offset;
6774 if (! finfo->info->relocateable)
6776 osym.st_value += isec->output_section->vma;
6777 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6779 /* STT_TLS symbols are relative to PT_TLS segment base. */
6780 BFD_ASSERT (finfo->first_tls_sec != NULL);
6781 osym.st_value -= finfo->first_tls_sec->vma;
6785 if (! elf_link_output_sym (finfo, name, &osym, isec))
6786 return false;
6789 /* Relocate the contents of each section. */
6790 sym_hashes = elf_sym_hashes (input_bfd);
6791 for (o = input_bfd->sections; o != NULL; o = o->next)
6793 bfd_byte *contents;
6795 if (! o->linker_mark)
6797 /* This section was omitted from the link. */
6798 continue;
6801 if ((o->flags & SEC_HAS_CONTENTS) == 0
6802 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6803 continue;
6805 if ((o->flags & SEC_LINKER_CREATED) != 0)
6807 /* Section was created by elf_link_create_dynamic_sections
6808 or somesuch. */
6809 continue;
6812 /* Get the contents of the section. They have been cached by a
6813 relaxation routine. Note that o is a section in an input
6814 file, so the contents field will not have been set by any of
6815 the routines which work on output files. */
6816 if (elf_section_data (o)->this_hdr.contents != NULL)
6817 contents = elf_section_data (o)->this_hdr.contents;
6818 else
6820 contents = finfo->contents;
6821 if (! bfd_get_section_contents (input_bfd, o, contents,
6822 (file_ptr) 0, o->_raw_size))
6823 return false;
6826 if ((o->flags & SEC_RELOC) != 0)
6828 Elf_Internal_Rela *internal_relocs;
6830 /* Get the swapped relocs. */
6831 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6832 (input_bfd, o, finfo->external_relocs,
6833 finfo->internal_relocs, false));
6834 if (internal_relocs == NULL
6835 && o->reloc_count > 0)
6836 return false;
6838 /* Run through the relocs looking for any against symbols
6839 from discarded sections and section symbols from
6840 removed link-once sections. Complain about relocs
6841 against discarded sections. Zero relocs against removed
6842 link-once sections. We should really complain if
6843 anything in the final link tries to use it, but
6844 DWARF-based exception handling might have an entry in
6845 .eh_frame to describe a routine in the linkonce section,
6846 and it turns out to be hard to remove the .eh_frame
6847 entry too. FIXME. */
6848 if (!finfo->info->relocateable
6849 && !elf_section_ignore_discarded_relocs (o))
6851 Elf_Internal_Rela *rel, *relend;
6853 rel = internal_relocs;
6854 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6855 for ( ; rel < relend; rel++)
6857 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6859 if (r_symndx >= locsymcount
6860 || (elf_bad_symtab (input_bfd)
6861 && finfo->sections[r_symndx] == NULL))
6863 struct elf_link_hash_entry *h;
6865 h = sym_hashes[r_symndx - extsymoff];
6866 while (h->root.type == bfd_link_hash_indirect
6867 || h->root.type == bfd_link_hash_warning)
6868 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6870 /* Complain if the definition comes from a
6871 discarded section. */
6872 if ((h->root.type == bfd_link_hash_defined
6873 || h->root.type == bfd_link_hash_defweak)
6874 && elf_discarded_section (h->root.u.def.section))
6876 if ((o->flags & SEC_DEBUGGING) != 0)
6878 BFD_ASSERT (r_symndx != 0);
6879 memset (rel, 0, sizeof (*rel));
6881 else
6883 if (! ((*finfo->info->callbacks->undefined_symbol)
6884 (finfo->info, h->root.root.string,
6885 input_bfd, o, rel->r_offset,
6886 true)))
6887 return false;
6891 else
6893 asection *sec = finfo->sections[r_symndx];
6895 if (sec != NULL && elf_discarded_section (sec))
6897 if ((o->flags & SEC_DEBUGGING) != 0
6898 || (sec->flags & SEC_LINK_ONCE) != 0)
6900 BFD_ASSERT (r_symndx != 0);
6901 rel->r_info
6902 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6903 rel->r_addend = 0;
6905 else
6907 boolean ok;
6908 const char *msg
6909 = _("local symbols in discarded section %s");
6910 bfd_size_type amt
6911 = strlen (sec->name) + strlen (msg) - 1;
6912 char *buf = (char *) bfd_malloc (amt);
6914 if (buf != NULL)
6915 sprintf (buf, msg, sec->name);
6916 else
6917 buf = (char *) sec->name;
6918 ok = (*finfo->info->callbacks
6919 ->undefined_symbol) (finfo->info, buf,
6920 input_bfd, o,
6921 rel->r_offset,
6922 true);
6923 if (buf != sec->name)
6924 free (buf);
6925 if (!ok)
6926 return false;
6933 /* Relocate the section by invoking a back end routine.
6935 The back end routine is responsible for adjusting the
6936 section contents as necessary, and (if using Rela relocs
6937 and generating a relocateable output file) adjusting the
6938 reloc addend as necessary.
6940 The back end routine does not have to worry about setting
6941 the reloc address or the reloc symbol index.
6943 The back end routine is given a pointer to the swapped in
6944 internal symbols, and can access the hash table entries
6945 for the external symbols via elf_sym_hashes (input_bfd).
6947 When generating relocateable output, the back end routine
6948 must handle STB_LOCAL/STT_SECTION symbols specially. The
6949 output symbol is going to be a section symbol
6950 corresponding to the output section, which will require
6951 the addend to be adjusted. */
6953 if (! (*relocate_section) (output_bfd, finfo->info,
6954 input_bfd, o, contents,
6955 internal_relocs,
6956 isymbuf,
6957 finfo->sections))
6958 return false;
6960 if (emit_relocs)
6962 Elf_Internal_Rela *irela;
6963 Elf_Internal_Rela *irelaend;
6964 struct elf_link_hash_entry **rel_hash;
6965 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6966 unsigned int next_erel;
6967 boolean (*reloc_emitter) PARAMS ((bfd *, asection *,
6968 Elf_Internal_Shdr *,
6969 Elf_Internal_Rela *));
6970 boolean rela_normal;
6972 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6973 rela_normal = (bed->rela_normal
6974 && (input_rel_hdr->sh_entsize
6975 == sizeof (Elf_External_Rela)));
6977 /* Adjust the reloc addresses and symbol indices. */
6979 irela = internal_relocs;
6980 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6981 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6982 + elf_section_data (o->output_section)->rel_count
6983 + elf_section_data (o->output_section)->rel_count2);
6984 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6986 unsigned long r_symndx;
6987 asection *sec;
6988 Elf_Internal_Sym sym;
6990 if (next_erel == bed->s->int_rels_per_ext_rel)
6992 rel_hash++;
6993 next_erel = 0;
6996 irela->r_offset += o->output_offset;
6998 /* Relocs in an executable have to be virtual addresses. */
6999 if (!finfo->info->relocateable)
7000 irela->r_offset += o->output_section->vma;
7002 r_symndx = ELF_R_SYM (irela->r_info);
7004 if (r_symndx == 0)
7005 continue;
7007 if (r_symndx >= locsymcount
7008 || (elf_bad_symtab (input_bfd)
7009 && finfo->sections[r_symndx] == NULL))
7011 struct elf_link_hash_entry *rh;
7012 unsigned long indx;
7014 /* This is a reloc against a global symbol. We
7015 have not yet output all the local symbols, so
7016 we do not know the symbol index of any global
7017 symbol. We set the rel_hash entry for this
7018 reloc to point to the global hash table entry
7019 for this symbol. The symbol index is then
7020 set at the end of elf_bfd_final_link. */
7021 indx = r_symndx - extsymoff;
7022 rh = elf_sym_hashes (input_bfd)[indx];
7023 while (rh->root.type == bfd_link_hash_indirect
7024 || rh->root.type == bfd_link_hash_warning)
7025 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7027 /* Setting the index to -2 tells
7028 elf_link_output_extsym that this symbol is
7029 used by a reloc. */
7030 BFD_ASSERT (rh->indx < 0);
7031 rh->indx = -2;
7033 *rel_hash = rh;
7035 continue;
7038 /* This is a reloc against a local symbol. */
7040 *rel_hash = NULL;
7041 sym = isymbuf[r_symndx];
7042 sec = finfo->sections[r_symndx];
7043 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7045 /* I suppose the backend ought to fill in the
7046 section of any STT_SECTION symbol against a
7047 processor specific section. If we have
7048 discarded a section, the output_section will
7049 be the absolute section. */
7050 if (bfd_is_abs_section (sec)
7051 || (sec != NULL
7052 && bfd_is_abs_section (sec->output_section)))
7053 r_symndx = 0;
7054 else if (sec == NULL || sec->owner == NULL)
7056 bfd_set_error (bfd_error_bad_value);
7057 return false;
7059 else
7061 r_symndx = sec->output_section->target_index;
7062 BFD_ASSERT (r_symndx != 0);
7065 /* Adjust the addend according to where the
7066 section winds up in the output section. */
7067 if (rela_normal)
7068 irela->r_addend += sec->output_offset;
7070 else
7072 if (finfo->indices[r_symndx] == -1)
7074 unsigned long shlink;
7075 const char *name;
7076 asection *osec;
7078 if (finfo->info->strip == strip_all)
7080 /* You can't do ld -r -s. */
7081 bfd_set_error (bfd_error_invalid_operation);
7082 return false;
7085 /* This symbol was skipped earlier, but
7086 since it is needed by a reloc, we
7087 must output it now. */
7088 shlink = symtab_hdr->sh_link;
7089 name = (bfd_elf_string_from_elf_section
7090 (input_bfd, shlink, sym.st_name));
7091 if (name == NULL)
7092 return false;
7094 osec = sec->output_section;
7095 sym.st_shndx =
7096 _bfd_elf_section_from_bfd_section (output_bfd,
7097 osec);
7098 if (sym.st_shndx == SHN_BAD)
7099 return false;
7101 sym.st_value += sec->output_offset;
7102 if (! finfo->info->relocateable)
7104 sym.st_value += osec->vma;
7105 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7107 /* STT_TLS symbols are relative to PT_TLS
7108 segment base. */
7109 BFD_ASSERT (finfo->first_tls_sec != NULL);
7110 sym.st_value -= finfo->first_tls_sec->vma;
7114 finfo->indices[r_symndx]
7115 = bfd_get_symcount (output_bfd);
7117 if (! elf_link_output_sym (finfo, name, &sym, sec))
7118 return false;
7121 r_symndx = finfo->indices[r_symndx];
7124 irela->r_info = ELF_R_INFO (r_symndx,
7125 ELF_R_TYPE (irela->r_info));
7128 /* Swap out the relocs. */
7129 if (bed->elf_backend_emit_relocs
7130 && !(finfo->info->relocateable
7131 || finfo->info->emitrelocations))
7132 reloc_emitter = bed->elf_backend_emit_relocs;
7133 else
7134 reloc_emitter = elf_link_output_relocs;
7136 if (input_rel_hdr->sh_size != 0
7137 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7138 internal_relocs))
7139 return false;
7141 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7142 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7144 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7145 * bed->s->int_rels_per_ext_rel);
7146 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7147 internal_relocs))
7148 return false;
7153 /* Write out the modified section contents. */
7154 if (bed->elf_backend_write_section
7155 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7157 /* Section written out. */
7159 else switch (elf_section_data (o)->sec_info_type)
7161 case ELF_INFO_TYPE_STABS:
7162 if (! (_bfd_write_section_stabs
7163 (output_bfd,
7164 &elf_hash_table (finfo->info)->stab_info,
7165 o, &elf_section_data (o)->sec_info, contents)))
7166 return false;
7167 break;
7168 case ELF_INFO_TYPE_MERGE:
7169 if (! (_bfd_write_merged_section
7170 (output_bfd, o, elf_section_data (o)->sec_info)))
7171 return false;
7172 break;
7173 case ELF_INFO_TYPE_EH_FRAME:
7175 asection *ehdrsec;
7177 ehdrsec
7178 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
7179 ".eh_frame_hdr");
7180 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
7181 contents)))
7182 return false;
7184 break;
7185 default:
7187 bfd_size_type sec_size;
7189 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7190 if (! (o->flags & SEC_EXCLUDE)
7191 && ! bfd_set_section_contents (output_bfd, o->output_section,
7192 contents,
7193 (file_ptr) o->output_offset,
7194 sec_size))
7195 return false;
7197 break;
7201 return true;
7204 /* Generate a reloc when linking an ELF file. This is a reloc
7205 requested by the linker, and does come from any input file. This
7206 is used to build constructor and destructor tables when linking
7207 with -Ur. */
7209 static boolean
7210 elf_reloc_link_order (output_bfd, info, output_section, link_order)
7211 bfd *output_bfd;
7212 struct bfd_link_info *info;
7213 asection *output_section;
7214 struct bfd_link_order *link_order;
7216 reloc_howto_type *howto;
7217 long indx;
7218 bfd_vma offset;
7219 bfd_vma addend;
7220 struct elf_link_hash_entry **rel_hash_ptr;
7221 Elf_Internal_Shdr *rel_hdr;
7222 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7224 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7225 if (howto == NULL)
7227 bfd_set_error (bfd_error_bad_value);
7228 return false;
7231 addend = link_order->u.reloc.p->addend;
7233 /* Figure out the symbol index. */
7234 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7235 + elf_section_data (output_section)->rel_count
7236 + elf_section_data (output_section)->rel_count2);
7237 if (link_order->type == bfd_section_reloc_link_order)
7239 indx = link_order->u.reloc.p->u.section->target_index;
7240 BFD_ASSERT (indx != 0);
7241 *rel_hash_ptr = NULL;
7243 else
7245 struct elf_link_hash_entry *h;
7247 /* Treat a reloc against a defined symbol as though it were
7248 actually against the section. */
7249 h = ((struct elf_link_hash_entry *)
7250 bfd_wrapped_link_hash_lookup (output_bfd, info,
7251 link_order->u.reloc.p->u.name,
7252 false, false, true));
7253 if (h != NULL
7254 && (h->root.type == bfd_link_hash_defined
7255 || h->root.type == bfd_link_hash_defweak))
7257 asection *section;
7259 section = h->root.u.def.section;
7260 indx = section->output_section->target_index;
7261 *rel_hash_ptr = NULL;
7262 /* It seems that we ought to add the symbol value to the
7263 addend here, but in practice it has already been added
7264 because it was passed to constructor_callback. */
7265 addend += section->output_section->vma + section->output_offset;
7267 else if (h != NULL)
7269 /* Setting the index to -2 tells elf_link_output_extsym that
7270 this symbol is used by a reloc. */
7271 h->indx = -2;
7272 *rel_hash_ptr = h;
7273 indx = 0;
7275 else
7277 if (! ((*info->callbacks->unattached_reloc)
7278 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7279 (asection *) NULL, (bfd_vma) 0)))
7280 return false;
7281 indx = 0;
7285 /* If this is an inplace reloc, we must write the addend into the
7286 object file. */
7287 if (howto->partial_inplace && addend != 0)
7289 bfd_size_type size;
7290 bfd_reloc_status_type rstat;
7291 bfd_byte *buf;
7292 boolean ok;
7293 const char *sym_name;
7295 size = bfd_get_reloc_size (howto);
7296 buf = (bfd_byte *) bfd_zmalloc (size);
7297 if (buf == (bfd_byte *) NULL)
7298 return false;
7299 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
7300 switch (rstat)
7302 case bfd_reloc_ok:
7303 break;
7305 default:
7306 case bfd_reloc_outofrange:
7307 abort ();
7309 case bfd_reloc_overflow:
7310 if (link_order->type == bfd_section_reloc_link_order)
7311 sym_name = bfd_section_name (output_bfd,
7312 link_order->u.reloc.p->u.section);
7313 else
7314 sym_name = link_order->u.reloc.p->u.name;
7315 if (! ((*info->callbacks->reloc_overflow)
7316 (info, sym_name, howto->name, addend,
7317 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7319 free (buf);
7320 return false;
7322 break;
7324 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7325 (file_ptr) link_order->offset, size);
7326 free (buf);
7327 if (! ok)
7328 return false;
7331 /* The address of a reloc is relative to the section in a
7332 relocateable file, and is a virtual address in an executable
7333 file. */
7334 offset = link_order->offset;
7335 if (! info->relocateable)
7336 offset += output_section->vma;
7338 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7340 if (rel_hdr->sh_type == SHT_REL)
7342 bfd_size_type size;
7343 Elf_Internal_Rel *irel;
7344 Elf_External_Rel *erel;
7345 unsigned int i;
7347 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7348 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7349 if (irel == NULL)
7350 return false;
7352 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7353 irel[i].r_offset = offset;
7354 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7356 erel = ((Elf_External_Rel *) rel_hdr->contents
7357 + elf_section_data (output_section)->rel_count);
7359 if (bed->s->swap_reloc_out)
7360 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7361 else
7362 elf_swap_reloc_out (output_bfd, irel, erel);
7364 free (irel);
7366 else
7368 bfd_size_type size;
7369 Elf_Internal_Rela *irela;
7370 Elf_External_Rela *erela;
7371 unsigned int i;
7373 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7374 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7375 if (irela == NULL)
7376 return false;
7378 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7379 irela[i].r_offset = offset;
7380 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7381 irela[0].r_addend = addend;
7383 erela = ((Elf_External_Rela *) rel_hdr->contents
7384 + elf_section_data (output_section)->rel_count);
7386 if (bed->s->swap_reloca_out)
7387 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7388 else
7389 elf_swap_reloca_out (output_bfd, irela, erela);
7392 ++elf_section_data (output_section)->rel_count;
7394 return true;
7397 /* Allocate a pointer to live in a linker created section. */
7399 boolean
7400 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7401 bfd *abfd;
7402 struct bfd_link_info *info;
7403 elf_linker_section_t *lsect;
7404 struct elf_link_hash_entry *h;
7405 const Elf_Internal_Rela *rel;
7407 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7408 elf_linker_section_pointers_t *linker_section_ptr;
7409 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7410 bfd_size_type amt;
7412 BFD_ASSERT (lsect != NULL);
7414 /* Is this a global symbol? */
7415 if (h != NULL)
7417 /* Has this symbol already been allocated? If so, our work is done. */
7418 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7419 rel->r_addend,
7420 lsect->which))
7421 return true;
7423 ptr_linker_section_ptr = &h->linker_section_pointer;
7424 /* Make sure this symbol is output as a dynamic symbol. */
7425 if (h->dynindx == -1)
7427 if (! elf_link_record_dynamic_symbol (info, h))
7428 return false;
7431 if (lsect->rel_section)
7432 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7434 else
7436 /* Allocation of a pointer to a local symbol. */
7437 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7439 /* Allocate a table to hold the local symbols if first time. */
7440 if (!ptr)
7442 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7443 register unsigned int i;
7445 amt = num_symbols;
7446 amt *= sizeof (elf_linker_section_pointers_t *);
7447 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7449 if (!ptr)
7450 return false;
7452 elf_local_ptr_offsets (abfd) = ptr;
7453 for (i = 0; i < num_symbols; i++)
7454 ptr[i] = (elf_linker_section_pointers_t *) 0;
7457 /* Has this symbol already been allocated? If so, our work is done. */
7458 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7459 rel->r_addend,
7460 lsect->which))
7461 return true;
7463 ptr_linker_section_ptr = &ptr[r_symndx];
7465 if (info->shared)
7467 /* If we are generating a shared object, we need to
7468 output a R_<xxx>_RELATIVE reloc so that the
7469 dynamic linker can adjust this GOT entry. */
7470 BFD_ASSERT (lsect->rel_section != NULL);
7471 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7475 /* Allocate space for a pointer in the linker section, and allocate
7476 a new pointer record from internal memory. */
7477 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7478 amt = sizeof (elf_linker_section_pointers_t);
7479 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7481 if (!linker_section_ptr)
7482 return false;
7484 linker_section_ptr->next = *ptr_linker_section_ptr;
7485 linker_section_ptr->addend = rel->r_addend;
7486 linker_section_ptr->which = lsect->which;
7487 linker_section_ptr->written_address_p = false;
7488 *ptr_linker_section_ptr = linker_section_ptr;
7490 #if 0
7491 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7493 linker_section_ptr->offset = (lsect->section->_raw_size
7494 - lsect->hole_size + (ARCH_SIZE / 8));
7495 lsect->hole_offset += ARCH_SIZE / 8;
7496 lsect->sym_offset += ARCH_SIZE / 8;
7497 if (lsect->sym_hash)
7499 /* Bump up symbol value if needed. */
7500 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7501 #ifdef DEBUG
7502 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7503 lsect->sym_hash->root.root.string,
7504 (long) ARCH_SIZE / 8,
7505 (long) lsect->sym_hash->root.u.def.value);
7506 #endif
7509 else
7510 #endif
7511 linker_section_ptr->offset = lsect->section->_raw_size;
7513 lsect->section->_raw_size += ARCH_SIZE / 8;
7515 #ifdef DEBUG
7516 fprintf (stderr,
7517 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7518 lsect->name, (long) linker_section_ptr->offset,
7519 (long) lsect->section->_raw_size);
7520 #endif
7522 return true;
7525 #if ARCH_SIZE==64
7526 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7527 #endif
7528 #if ARCH_SIZE==32
7529 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7530 #endif
7532 /* Fill in the address for a pointer generated in a linker section. */
7534 bfd_vma
7535 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7536 relocation, rel, relative_reloc)
7537 bfd *output_bfd;
7538 bfd *input_bfd;
7539 struct bfd_link_info *info;
7540 elf_linker_section_t *lsect;
7541 struct elf_link_hash_entry *h;
7542 bfd_vma relocation;
7543 const Elf_Internal_Rela *rel;
7544 int relative_reloc;
7546 elf_linker_section_pointers_t *linker_section_ptr;
7548 BFD_ASSERT (lsect != NULL);
7550 if (h != NULL)
7552 /* Handle global symbol. */
7553 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7554 (h->linker_section_pointer,
7555 rel->r_addend,
7556 lsect->which));
7558 BFD_ASSERT (linker_section_ptr != NULL);
7560 if (! elf_hash_table (info)->dynamic_sections_created
7561 || (info->shared
7562 && info->symbolic
7563 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7565 /* This is actually a static link, or it is a
7566 -Bsymbolic link and the symbol is defined
7567 locally. We must initialize this entry in the
7568 global section.
7570 When doing a dynamic link, we create a .rela.<xxx>
7571 relocation entry to initialize the value. This
7572 is done in the finish_dynamic_symbol routine. */
7573 if (!linker_section_ptr->written_address_p)
7575 linker_section_ptr->written_address_p = true;
7576 bfd_put_ptr (output_bfd,
7577 relocation + linker_section_ptr->addend,
7578 (lsect->section->contents
7579 + linker_section_ptr->offset));
7583 else
7585 /* Handle local symbol. */
7586 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7587 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7588 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7589 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7590 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7591 rel->r_addend,
7592 lsect->which));
7594 BFD_ASSERT (linker_section_ptr != NULL);
7596 /* Write out pointer if it hasn't been rewritten out before. */
7597 if (!linker_section_ptr->written_address_p)
7599 linker_section_ptr->written_address_p = true;
7600 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7601 lsect->section->contents + linker_section_ptr->offset);
7603 if (info->shared)
7605 asection *srel = lsect->rel_section;
7606 Elf_Internal_Rela *outrel;
7607 Elf_External_Rela *erel;
7608 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7609 unsigned int i;
7610 bfd_size_type amt;
7612 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7613 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7614 if (outrel == NULL)
7616 (*_bfd_error_handler) (_("Error: out of memory"));
7617 return 0;
7620 /* We need to generate a relative reloc for the dynamic
7621 linker. */
7622 if (!srel)
7624 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7625 lsect->rel_name);
7626 lsect->rel_section = srel;
7629 BFD_ASSERT (srel != NULL);
7631 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7632 outrel[i].r_offset = (lsect->section->output_section->vma
7633 + lsect->section->output_offset
7634 + linker_section_ptr->offset);
7635 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7636 outrel[0].r_addend = 0;
7637 erel = (Elf_External_Rela *) lsect->section->contents;
7638 erel += elf_section_data (lsect->section)->rel_count;
7639 elf_swap_reloca_out (output_bfd, outrel, erel);
7640 ++elf_section_data (lsect->section)->rel_count;
7642 free (outrel);
7647 relocation = (lsect->section->output_offset
7648 + linker_section_ptr->offset
7649 - lsect->hole_offset
7650 - lsect->sym_offset);
7652 #ifdef DEBUG
7653 fprintf (stderr,
7654 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7655 lsect->name, (long) relocation, (long) relocation);
7656 #endif
7658 /* Subtract out the addend, because it will get added back in by the normal
7659 processing. */
7660 return relocation - linker_section_ptr->addend;
7663 /* Garbage collect unused sections. */
7665 static boolean elf_gc_mark
7666 PARAMS ((struct bfd_link_info *, asection *,
7667 asection * (*) (asection *, struct bfd_link_info *,
7668 Elf_Internal_Rela *, struct elf_link_hash_entry *,
7669 Elf_Internal_Sym *)));
7671 static boolean elf_gc_sweep
7672 PARAMS ((struct bfd_link_info *,
7673 boolean (*) (bfd *, struct bfd_link_info *, asection *,
7674 const Elf_Internal_Rela *)));
7676 static boolean elf_gc_sweep_symbol
7677 PARAMS ((struct elf_link_hash_entry *, PTR));
7679 static boolean elf_gc_allocate_got_offsets
7680 PARAMS ((struct elf_link_hash_entry *, PTR));
7682 static boolean elf_gc_propagate_vtable_entries_used
7683 PARAMS ((struct elf_link_hash_entry *, PTR));
7685 static boolean elf_gc_smash_unused_vtentry_relocs
7686 PARAMS ((struct elf_link_hash_entry *, PTR));
7688 /* The mark phase of garbage collection. For a given section, mark
7689 it and any sections in this section's group, and all the sections
7690 which define symbols to which it refers. */
7692 static boolean
7693 elf_gc_mark (info, sec, gc_mark_hook)
7694 struct bfd_link_info *info;
7695 asection *sec;
7696 asection * (*gc_mark_hook) PARAMS ((asection *, struct bfd_link_info *,
7697 Elf_Internal_Rela *,
7698 struct elf_link_hash_entry *,
7699 Elf_Internal_Sym *));
7701 boolean ret;
7702 asection *group_sec;
7704 sec->gc_mark = 1;
7706 /* Mark all the sections in the group. */
7707 group_sec = elf_section_data (sec)->next_in_group;
7708 if (group_sec && !group_sec->gc_mark)
7709 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7710 return false;
7712 /* Look through the section relocs. */
7713 ret = true;
7714 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7716 Elf_Internal_Rela *relstart, *rel, *relend;
7717 Elf_Internal_Shdr *symtab_hdr;
7718 struct elf_link_hash_entry **sym_hashes;
7719 size_t nlocsyms;
7720 size_t extsymoff;
7721 bfd *input_bfd = sec->owner;
7722 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7723 Elf_Internal_Sym *isym = NULL;
7725 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7726 sym_hashes = elf_sym_hashes (input_bfd);
7728 /* Read the local symbols. */
7729 if (elf_bad_symtab (input_bfd))
7731 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7732 extsymoff = 0;
7734 else
7735 extsymoff = nlocsyms = symtab_hdr->sh_info;
7737 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
7738 if (isym == NULL && nlocsyms != 0)
7740 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
7741 NULL, NULL, NULL);
7742 if (isym == NULL)
7743 return false;
7746 /* Read the relocations. */
7747 relstart = (NAME(_bfd_elf,link_read_relocs)
7748 (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL,
7749 info->keep_memory));
7750 if (relstart == NULL)
7752 ret = false;
7753 goto out1;
7755 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7757 for (rel = relstart; rel < relend; rel++)
7759 unsigned long r_symndx;
7760 asection *rsec;
7761 struct elf_link_hash_entry *h;
7763 r_symndx = ELF_R_SYM (rel->r_info);
7764 if (r_symndx == 0)
7765 continue;
7767 if (r_symndx >= nlocsyms
7768 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
7770 h = sym_hashes[r_symndx - extsymoff];
7771 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
7773 else
7775 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
7778 if (rsec && !rsec->gc_mark)
7780 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7781 rsec->gc_mark = 1;
7782 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7784 ret = false;
7785 goto out2;
7790 out2:
7791 if (elf_section_data (sec)->relocs != relstart)
7792 free (relstart);
7793 out1:
7794 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
7796 if (! info->keep_memory)
7797 free (isym);
7798 else
7799 symtab_hdr->contents = (unsigned char *) isym;
7803 return ret;
7806 /* The sweep phase of garbage collection. Remove all garbage sections. */
7808 static boolean
7809 elf_gc_sweep (info, gc_sweep_hook)
7810 struct bfd_link_info *info;
7811 boolean (*gc_sweep_hook) PARAMS ((bfd *, struct bfd_link_info *,
7812 asection *, const Elf_Internal_Rela *));
7814 bfd *sub;
7816 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7818 asection *o;
7820 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7821 continue;
7823 for (o = sub->sections; o != NULL; o = o->next)
7825 /* Keep special sections. Keep .debug sections. */
7826 if ((o->flags & SEC_LINKER_CREATED)
7827 || (o->flags & SEC_DEBUGGING))
7828 o->gc_mark = 1;
7830 if (o->gc_mark)
7831 continue;
7833 /* Skip sweeping sections already excluded. */
7834 if (o->flags & SEC_EXCLUDE)
7835 continue;
7837 /* Since this is early in the link process, it is simple
7838 to remove a section from the output. */
7839 o->flags |= SEC_EXCLUDE;
7841 /* But we also have to update some of the relocation
7842 info we collected before. */
7843 if (gc_sweep_hook
7844 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7846 Elf_Internal_Rela *internal_relocs;
7847 boolean r;
7849 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7850 (o->owner, o, NULL, NULL, info->keep_memory));
7851 if (internal_relocs == NULL)
7852 return false;
7854 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7856 if (elf_section_data (o)->relocs != internal_relocs)
7857 free (internal_relocs);
7859 if (!r)
7860 return false;
7865 /* Remove the symbols that were in the swept sections from the dynamic
7866 symbol table. GCFIXME: Anyone know how to get them out of the
7867 static symbol table as well? */
7869 int i = 0;
7871 elf_link_hash_traverse (elf_hash_table (info),
7872 elf_gc_sweep_symbol,
7873 (PTR) &i);
7875 elf_hash_table (info)->dynsymcount = i;
7878 return true;
7881 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7883 static boolean
7884 elf_gc_sweep_symbol (h, idxptr)
7885 struct elf_link_hash_entry *h;
7886 PTR idxptr;
7888 int *idx = (int *) idxptr;
7890 if (h->root.type == bfd_link_hash_warning)
7891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7893 if (h->dynindx != -1
7894 && ((h->root.type != bfd_link_hash_defined
7895 && h->root.type != bfd_link_hash_defweak)
7896 || h->root.u.def.section->gc_mark))
7897 h->dynindx = (*idx)++;
7899 return true;
7902 /* Propogate collected vtable information. This is called through
7903 elf_link_hash_traverse. */
7905 static boolean
7906 elf_gc_propagate_vtable_entries_used (h, okp)
7907 struct elf_link_hash_entry *h;
7908 PTR okp;
7910 if (h->root.type == bfd_link_hash_warning)
7911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7913 /* Those that are not vtables. */
7914 if (h->vtable_parent == NULL)
7915 return true;
7917 /* Those vtables that do not have parents, we cannot merge. */
7918 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7919 return true;
7921 /* If we've already been done, exit. */
7922 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7923 return true;
7925 /* Make sure the parent's table is up to date. */
7926 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7928 if (h->vtable_entries_used == NULL)
7930 /* None of this table's entries were referenced. Re-use the
7931 parent's table. */
7932 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7933 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7935 else
7937 size_t n;
7938 boolean *cu, *pu;
7940 /* Or the parent's entries into ours. */
7941 cu = h->vtable_entries_used;
7942 cu[-1] = true;
7943 pu = h->vtable_parent->vtable_entries_used;
7944 if (pu != NULL)
7946 asection *sec = h->root.u.def.section;
7947 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7948 int file_align = bed->s->file_align;
7950 n = h->vtable_parent->vtable_entries_size / file_align;
7951 while (n--)
7953 if (*pu)
7954 *cu = true;
7955 pu++;
7956 cu++;
7961 return true;
7964 static boolean
7965 elf_gc_smash_unused_vtentry_relocs (h, okp)
7966 struct elf_link_hash_entry *h;
7967 PTR okp;
7969 asection *sec;
7970 bfd_vma hstart, hend;
7971 Elf_Internal_Rela *relstart, *relend, *rel;
7972 struct elf_backend_data *bed;
7973 int file_align;
7975 if (h->root.type == bfd_link_hash_warning)
7976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7978 /* Take care of both those symbols that do not describe vtables as
7979 well as those that are not loaded. */
7980 if (h->vtable_parent == NULL)
7981 return true;
7983 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7984 || h->root.type == bfd_link_hash_defweak);
7986 sec = h->root.u.def.section;
7987 hstart = h->root.u.def.value;
7988 hend = hstart + h->size;
7990 relstart = (NAME(_bfd_elf,link_read_relocs)
7991 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7992 if (!relstart)
7993 return *(boolean *) okp = false;
7994 bed = get_elf_backend_data (sec->owner);
7995 file_align = bed->s->file_align;
7997 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7999 for (rel = relstart; rel < relend; ++rel)
8000 if (rel->r_offset >= hstart && rel->r_offset < hend)
8002 /* If the entry is in use, do nothing. */
8003 if (h->vtable_entries_used
8004 && (rel->r_offset - hstart) < h->vtable_entries_size)
8006 bfd_vma entry = (rel->r_offset - hstart) / file_align;
8007 if (h->vtable_entries_used[entry])
8008 continue;
8010 /* Otherwise, kill it. */
8011 rel->r_offset = rel->r_info = rel->r_addend = 0;
8014 return true;
8017 /* Do mark and sweep of unused sections. */
8019 boolean
8020 elf_gc_sections (abfd, info)
8021 bfd *abfd;
8022 struct bfd_link_info *info;
8024 boolean ok = true;
8025 bfd *sub;
8026 asection * (*gc_mark_hook)
8027 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8028 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
8030 if (!get_elf_backend_data (abfd)->can_gc_sections
8031 || info->relocateable || info->emitrelocations
8032 || elf_hash_table (info)->dynamic_sections_created)
8033 return true;
8035 /* Apply transitive closure to the vtable entry usage info. */
8036 elf_link_hash_traverse (elf_hash_table (info),
8037 elf_gc_propagate_vtable_entries_used,
8038 (PTR) &ok);
8039 if (!ok)
8040 return false;
8042 /* Kill the vtable relocations that were not used. */
8043 elf_link_hash_traverse (elf_hash_table (info),
8044 elf_gc_smash_unused_vtentry_relocs,
8045 (PTR) &ok);
8046 if (!ok)
8047 return false;
8049 /* Grovel through relocs to find out who stays ... */
8051 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8052 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8054 asection *o;
8056 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8057 continue;
8059 for (o = sub->sections; o != NULL; o = o->next)
8061 if (o->flags & SEC_KEEP)
8062 if (!elf_gc_mark (info, o, gc_mark_hook))
8063 return false;
8067 /* ... and mark SEC_EXCLUDE for those that go. */
8068 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8069 return false;
8071 return true;
8074 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8076 boolean
8077 elf_gc_record_vtinherit (abfd, sec, h, offset)
8078 bfd *abfd;
8079 asection *sec;
8080 struct elf_link_hash_entry *h;
8081 bfd_vma offset;
8083 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8084 struct elf_link_hash_entry **search, *child;
8085 bfd_size_type extsymcount;
8087 /* The sh_info field of the symtab header tells us where the
8088 external symbols start. We don't care about the local symbols at
8089 this point. */
8090 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8091 if (!elf_bad_symtab (abfd))
8092 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8094 sym_hashes = elf_sym_hashes (abfd);
8095 sym_hashes_end = sym_hashes + extsymcount;
8097 /* Hunt down the child symbol, which is in this section at the same
8098 offset as the relocation. */
8099 for (search = sym_hashes; search != sym_hashes_end; ++search)
8101 if ((child = *search) != NULL
8102 && (child->root.type == bfd_link_hash_defined
8103 || child->root.type == bfd_link_hash_defweak)
8104 && child->root.u.def.section == sec
8105 && child->root.u.def.value == offset)
8106 goto win;
8109 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8110 bfd_archive_filename (abfd), sec->name,
8111 (unsigned long) offset);
8112 bfd_set_error (bfd_error_invalid_operation);
8113 return false;
8115 win:
8116 if (!h)
8118 /* This *should* only be the absolute section. It could potentially
8119 be that someone has defined a non-global vtable though, which
8120 would be bad. It isn't worth paging in the local symbols to be
8121 sure though; that case should simply be handled by the assembler. */
8123 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8125 else
8126 child->vtable_parent = h;
8128 return true;
8131 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8133 boolean
8134 elf_gc_record_vtentry (abfd, sec, h, addend)
8135 bfd *abfd ATTRIBUTE_UNUSED;
8136 asection *sec ATTRIBUTE_UNUSED;
8137 struct elf_link_hash_entry *h;
8138 bfd_vma addend;
8140 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8141 int file_align = bed->s->file_align;
8143 if (addend >= h->vtable_entries_size)
8145 size_t size, bytes;
8146 boolean *ptr = h->vtable_entries_used;
8148 /* While the symbol is undefined, we have to be prepared to handle
8149 a zero size. */
8150 if (h->root.type == bfd_link_hash_undefined)
8151 size = addend;
8152 else
8154 size = h->size;
8155 if (size < addend)
8157 /* Oops! We've got a reference past the defined end of
8158 the table. This is probably a bug -- shall we warn? */
8159 size = addend;
8163 /* Allocate one extra entry for use as a "done" flag for the
8164 consolidation pass. */
8165 bytes = (size / file_align + 1) * sizeof (boolean);
8167 if (ptr)
8169 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
8171 if (ptr != NULL)
8173 size_t oldbytes;
8175 oldbytes = ((h->vtable_entries_size / file_align + 1)
8176 * sizeof (boolean));
8177 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8180 else
8181 ptr = bfd_zmalloc ((bfd_size_type) bytes);
8183 if (ptr == NULL)
8184 return false;
8186 /* And arrange for that done flag to be at index -1. */
8187 h->vtable_entries_used = ptr + 1;
8188 h->vtable_entries_size = size;
8191 h->vtable_entries_used[addend / file_align] = true;
8193 return true;
8196 /* And an accompanying bit to work out final got entry offsets once
8197 we're done. Should be called from final_link. */
8199 boolean
8200 elf_gc_common_finalize_got_offsets (abfd, info)
8201 bfd *abfd;
8202 struct bfd_link_info *info;
8204 bfd *i;
8205 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8206 bfd_vma gotoff;
8208 /* The GOT offset is relative to the .got section, but the GOT header is
8209 put into the .got.plt section, if the backend uses it. */
8210 if (bed->want_got_plt)
8211 gotoff = 0;
8212 else
8213 gotoff = bed->got_header_size;
8215 /* Do the local .got entries first. */
8216 for (i = info->input_bfds; i; i = i->link_next)
8218 bfd_signed_vma *local_got;
8219 bfd_size_type j, locsymcount;
8220 Elf_Internal_Shdr *symtab_hdr;
8222 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8223 continue;
8225 local_got = elf_local_got_refcounts (i);
8226 if (!local_got)
8227 continue;
8229 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8230 if (elf_bad_symtab (i))
8231 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8232 else
8233 locsymcount = symtab_hdr->sh_info;
8235 for (j = 0; j < locsymcount; ++j)
8237 if (local_got[j] > 0)
8239 local_got[j] = gotoff;
8240 gotoff += ARCH_SIZE / 8;
8242 else
8243 local_got[j] = (bfd_vma) -1;
8247 /* Then the global .got entries. .plt refcounts are handled by
8248 adjust_dynamic_symbol */
8249 elf_link_hash_traverse (elf_hash_table (info),
8250 elf_gc_allocate_got_offsets,
8251 (PTR) &gotoff);
8252 return true;
8255 /* We need a special top-level link routine to convert got reference counts
8256 to real got offsets. */
8258 static boolean
8259 elf_gc_allocate_got_offsets (h, offarg)
8260 struct elf_link_hash_entry *h;
8261 PTR offarg;
8263 bfd_vma *off = (bfd_vma *) offarg;
8265 if (h->root.type == bfd_link_hash_warning)
8266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8268 if (h->got.refcount > 0)
8270 h->got.offset = off[0];
8271 off[0] += ARCH_SIZE / 8;
8273 else
8274 h->got.offset = (bfd_vma) -1;
8276 return true;
8279 /* Many folk need no more in the way of final link than this, once
8280 got entry reference counting is enabled. */
8282 boolean
8283 elf_gc_common_final_link (abfd, info)
8284 bfd *abfd;
8285 struct bfd_link_info *info;
8287 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8288 return false;
8290 /* Invoke the regular ELF backend linker to do all the work. */
8291 return elf_bfd_final_link (abfd, info);
8294 /* This function will be called though elf_link_hash_traverse to store
8295 all hash value of the exported symbols in an array. */
8297 static boolean
8298 elf_collect_hash_codes (h, data)
8299 struct elf_link_hash_entry *h;
8300 PTR data;
8302 unsigned long **valuep = (unsigned long **) data;
8303 const char *name;
8304 char *p;
8305 unsigned long ha;
8306 char *alc = NULL;
8308 if (h->root.type == bfd_link_hash_warning)
8309 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8311 /* Ignore indirect symbols. These are added by the versioning code. */
8312 if (h->dynindx == -1)
8313 return true;
8315 name = h->root.root.string;
8316 p = strchr (name, ELF_VER_CHR);
8317 if (p != NULL)
8319 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8320 memcpy (alc, name, (size_t) (p - name));
8321 alc[p - name] = '\0';
8322 name = alc;
8325 /* Compute the hash value. */
8326 ha = bfd_elf_hash (name);
8328 /* Store the found hash value in the array given as the argument. */
8329 *(*valuep)++ = ha;
8331 /* And store it in the struct so that we can put it in the hash table
8332 later. */
8333 h->elf_hash_value = ha;
8335 if (alc != NULL)
8336 free (alc);
8338 return true;
8341 boolean
8342 elf_reloc_symbol_deleted_p (offset, cookie)
8343 bfd_vma offset;
8344 PTR cookie;
8346 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8348 if (rcookie->bad_symtab)
8349 rcookie->rel = rcookie->rels;
8351 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8353 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8355 if (! rcookie->bad_symtab)
8356 if (rcookie->rel->r_offset > offset)
8357 return false;
8358 if (rcookie->rel->r_offset != offset)
8359 continue;
8361 if (r_symndx >= rcookie->locsymcount
8362 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
8364 struct elf_link_hash_entry *h;
8366 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8368 while (h->root.type == bfd_link_hash_indirect
8369 || h->root.type == bfd_link_hash_warning)
8370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8372 if ((h->root.type == bfd_link_hash_defined
8373 || h->root.type == bfd_link_hash_defweak)
8374 && elf_discarded_section (h->root.u.def.section))
8375 return true;
8376 else
8377 return false;
8379 else
8381 /* It's not a relocation against a global symbol,
8382 but it could be a relocation against a local
8383 symbol for a discarded section. */
8384 asection *isec;
8385 Elf_Internal_Sym *isym;
8387 /* Need to: get the symbol; get the section. */
8388 isym = &rcookie->locsyms[r_symndx];
8389 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8391 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
8392 if (isec != NULL && elf_discarded_section (isec))
8393 return true;
8396 return false;
8398 return false;
8401 /* Discard unneeded references to discarded sections.
8402 Returns true if any section's size was changed. */
8403 /* This function assumes that the relocations are in sorted order,
8404 which is true for all known assemblers. */
8406 boolean
8407 elf_bfd_discard_info (output_bfd, info)
8408 bfd *output_bfd;
8409 struct bfd_link_info *info;
8411 struct elf_reloc_cookie cookie;
8412 asection *stab, *eh, *ehdr;
8413 Elf_Internal_Shdr *symtab_hdr;
8414 struct elf_backend_data *bed;
8415 bfd *abfd;
8416 boolean ret = false;
8417 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8419 if (info->relocateable
8420 || info->traditional_format
8421 || info->hash->creator->flavour != bfd_target_elf_flavour
8422 || ! is_elf_hash_table (info))
8423 return false;
8425 ehdr = NULL;
8426 if (elf_hash_table (info)->dynobj != NULL)
8427 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8428 ".eh_frame_hdr");
8430 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8432 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8433 continue;
8435 bed = get_elf_backend_data (abfd);
8437 if ((abfd->flags & DYNAMIC) != 0)
8438 continue;
8440 eh = NULL;
8441 if (ehdr)
8443 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8444 if (eh && (eh->_raw_size == 0
8445 || bfd_is_abs_section (eh->output_section)))
8446 eh = NULL;
8449 stab = NULL;
8450 if (!strip)
8452 stab = bfd_get_section_by_name (abfd, ".stab");
8453 if (stab && (stab->_raw_size == 0
8454 || bfd_is_abs_section (stab->output_section)))
8455 stab = NULL;
8457 if ((! stab
8458 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8459 && ! eh
8460 && (strip || ! bed->elf_backend_discard_info))
8461 continue;
8463 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8464 cookie.abfd = abfd;
8465 cookie.sym_hashes = elf_sym_hashes (abfd);
8466 cookie.bad_symtab = elf_bad_symtab (abfd);
8467 if (cookie.bad_symtab)
8469 cookie.locsymcount =
8470 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8471 cookie.extsymoff = 0;
8473 else
8475 cookie.locsymcount = symtab_hdr->sh_info;
8476 cookie.extsymoff = symtab_hdr->sh_info;
8479 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8480 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
8482 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8483 cookie.locsymcount, 0,
8484 NULL, NULL, NULL);
8485 if (cookie.locsyms == NULL)
8486 return false;
8489 if (stab)
8491 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8492 (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8493 info->keep_memory));
8494 if (cookie.rels)
8496 cookie.rel = cookie.rels;
8497 cookie.relend =
8498 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8499 if (_bfd_discard_section_stabs (abfd, stab,
8500 elf_section_data (stab)->sec_info,
8501 elf_reloc_symbol_deleted_p,
8502 &cookie))
8503 ret = true;
8504 if (elf_section_data (stab)->relocs != cookie.rels)
8505 free (cookie.rels);
8509 if (eh)
8511 cookie.rels = NULL;
8512 cookie.rel = NULL;
8513 cookie.relend = NULL;
8514 if (eh->reloc_count)
8515 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8516 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8517 info->keep_memory));
8518 if (cookie.rels)
8520 cookie.rel = cookie.rels;
8521 cookie.relend =
8522 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8524 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8525 elf_reloc_symbol_deleted_p,
8526 &cookie))
8528 /* Relocs have been edited. Ensure edited version is
8529 used later in relocate_section. */
8530 elf_section_data (eh)->relocs = cookie.rels;
8531 ret = true;
8533 if (cookie.rels && elf_section_data (eh)->relocs != cookie.rels)
8534 free (cookie.rels);
8537 if (bed->elf_backend_discard_info)
8539 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8540 ret = true;
8543 if (cookie.locsyms != NULL
8544 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
8546 if (! info->keep_memory)
8547 free (cookie.locsyms);
8548 else
8549 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
8553 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
8554 ret = true;
8555 return ret;
8558 static boolean
8559 elf_section_ignore_discarded_relocs (sec)
8560 asection *sec;
8562 struct elf_backend_data *bed;
8564 switch (elf_section_data (sec)->sec_info_type)
8566 case ELF_INFO_TYPE_STABS:
8567 case ELF_INFO_TYPE_EH_FRAME:
8568 return true;
8569 default:
8570 break;
8573 bed = get_elf_backend_data (sec->owner);
8574 if (bed->elf_backend_ignore_discarded_relocs != NULL
8575 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8576 return true;
8578 return false;