* config/tc-ia64.c (ia64_cons_fix_new): Handle 8 byte iplt reloc
[binutils.git] / bfd / elflink.h
blobd43d8612d15edfbfd8332e8df2a6b333d3fc8f58
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 void 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 Elf_Internal_Shdr * shndx_hdr;
153 Elf_External_Sym * esym;
154 Elf_External_Sym * esymend;
155 Elf_External_Sym * buf = NULL;
156 Elf_External_Sym_Shndx * shndx_buf = NULL;
157 Elf_External_Sym_Shndx * shndx;
158 bfd_size_type symcount;
159 bfd_size_type extsymcount;
160 bfd_size_type extsymoff;
161 boolean result = false;
162 file_ptr pos;
163 bfd_size_type amt;
165 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
166 if (abfd == (bfd *) NULL)
167 return false;
169 if (! bfd_check_format (abfd, bfd_object))
170 return false;
172 /* If we have already included the element containing this symbol in the
173 link then we do not need to include it again. Just claim that any symbol
174 it contains is not a definition, so that our caller will not decide to
175 (re)include this element. */
176 if (abfd->archive_pass)
177 return false;
179 /* Select the appropriate symbol table. */
180 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
182 hdr = &elf_tdata (abfd)->symtab_hdr;
183 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
185 else
187 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
188 shndx_hdr = NULL;
191 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
193 /* The sh_info field of the symtab header tells us where the
194 external symbols start. We don't care about the local symbols. */
195 if (elf_bad_symtab (abfd))
197 extsymcount = symcount;
198 extsymoff = 0;
200 else
202 extsymcount = symcount - hdr->sh_info;
203 extsymoff = hdr->sh_info;
206 amt = extsymcount * sizeof (Elf_External_Sym);
207 buf = (Elf_External_Sym *) bfd_malloc (amt);
208 if (buf == NULL && extsymcount != 0)
209 return false;
211 /* Read in the symbol table.
212 FIXME: This ought to be cached somewhere. */
213 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
214 if (bfd_seek (abfd, pos, SEEK_SET) != 0
215 || bfd_bread ((PTR) buf, amt, abfd) != amt)
216 goto error_exit;
218 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
220 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
221 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
222 if (shndx_buf == NULL && extsymcount != 0)
223 goto error_exit;
225 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
226 if (bfd_seek (abfd, pos, SEEK_SET) != 0
227 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
228 goto error_exit;
231 /* Scan the symbol table looking for SYMDEF. */
232 esymend = buf + extsymcount;
233 for (esym = buf, shndx = shndx_buf;
234 esym < esymend;
235 esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
237 Elf_Internal_Sym sym;
238 const char * name;
240 elf_swap_symbol_in (abfd, esym, shndx, &sym);
242 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
243 if (name == (const char *) NULL)
244 break;
246 if (strcmp (name, symdef->name) == 0)
248 result = is_global_data_symbol_definition (abfd, & sym);
249 break;
253 error_exit:
254 if (shndx_buf != NULL)
255 free (shndx_buf);
256 if (buf != NULL)
257 free (buf);
259 return result;
262 /* Add symbols from an ELF archive file to the linker hash table. We
263 don't use _bfd_generic_link_add_archive_symbols because of a
264 problem which arises on UnixWare. The UnixWare libc.so is an
265 archive which includes an entry libc.so.1 which defines a bunch of
266 symbols. The libc.so archive also includes a number of other
267 object files, which also define symbols, some of which are the same
268 as those defined in libc.so.1. Correct linking requires that we
269 consider each object file in turn, and include it if it defines any
270 symbols we need. _bfd_generic_link_add_archive_symbols does not do
271 this; it looks through the list of undefined symbols, and includes
272 any object file which defines them. When this algorithm is used on
273 UnixWare, it winds up pulling in libc.so.1 early and defining a
274 bunch of symbols. This means that some of the other objects in the
275 archive are not included in the link, which is incorrect since they
276 precede libc.so.1 in the archive.
278 Fortunately, ELF archive handling is simpler than that done by
279 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
280 oddities. In ELF, if we find a symbol in the archive map, and the
281 symbol is currently undefined, we know that we must pull in that
282 object file.
284 Unfortunately, we do have to make multiple passes over the symbol
285 table until nothing further is resolved. */
287 static boolean
288 elf_link_add_archive_symbols (abfd, info)
289 bfd *abfd;
290 struct bfd_link_info *info;
292 symindex c;
293 boolean *defined = NULL;
294 boolean *included = NULL;
295 carsym *symdefs;
296 boolean loop;
297 bfd_size_type amt;
299 if (! bfd_has_map (abfd))
301 /* An empty archive is a special case. */
302 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
303 return true;
304 bfd_set_error (bfd_error_no_armap);
305 return false;
308 /* Keep track of all symbols we know to be already defined, and all
309 files we know to be already included. This is to speed up the
310 second and subsequent passes. */
311 c = bfd_ardata (abfd)->symdef_count;
312 if (c == 0)
313 return true;
314 amt = c;
315 amt *= sizeof (boolean);
316 defined = (boolean *) bfd_malloc (amt);
317 included = (boolean *) bfd_malloc (amt);
318 if (defined == (boolean *) NULL || included == (boolean *) NULL)
319 goto error_return;
320 memset (defined, 0, (size_t) amt);
321 memset (included, 0, (size_t) amt);
323 symdefs = bfd_ardata (abfd)->symdefs;
327 file_ptr last;
328 symindex i;
329 carsym *symdef;
330 carsym *symdefend;
332 loop = false;
333 last = -1;
335 symdef = symdefs;
336 symdefend = symdef + c;
337 for (i = 0; symdef < symdefend; symdef++, i++)
339 struct elf_link_hash_entry *h;
340 bfd *element;
341 struct bfd_link_hash_entry *undefs_tail;
342 symindex mark;
344 if (defined[i] || included[i])
345 continue;
346 if (symdef->file_offset == last)
348 included[i] = true;
349 continue;
352 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
353 false, false, false);
355 if (h == NULL)
357 char *p, *copy;
359 /* If this is a default version (the name contains @@),
360 look up the symbol again without the version. The
361 effect is that references to the symbol without the
362 version will be matched by the default symbol in the
363 archive. */
365 p = strchr (symdef->name, ELF_VER_CHR);
366 if (p == NULL || p[1] != ELF_VER_CHR)
367 continue;
369 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
370 if (copy == NULL)
371 goto error_return;
372 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
373 copy[p - symdef->name] = '\0';
375 h = elf_link_hash_lookup (elf_hash_table (info), copy,
376 false, false, false);
378 bfd_release (abfd, copy);
381 if (h == NULL)
382 continue;
384 if (h->root.type == bfd_link_hash_common)
386 /* We currently have a common symbol. The archive map contains
387 a reference to this symbol, so we may want to include it. We
388 only want to include it however, if this archive element
389 contains a definition of the symbol, not just another common
390 declaration of it.
392 Unfortunately some archivers (including GNU ar) will put
393 declarations of common symbols into their archive maps, as
394 well as real definitions, so we cannot just go by the archive
395 map alone. Instead we must read in the element's symbol
396 table and check that to see what kind of symbol definition
397 this is. */
398 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
399 continue;
401 else if (h->root.type != bfd_link_hash_undefined)
403 if (h->root.type != bfd_link_hash_undefweak)
404 defined[i] = true;
405 continue;
408 /* We need to include this archive member. */
409 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
410 if (element == (bfd *) NULL)
411 goto error_return;
413 if (! bfd_check_format (element, bfd_object))
414 goto error_return;
416 /* Doublecheck that we have not included this object
417 already--it should be impossible, but there may be
418 something wrong with the archive. */
419 if (element->archive_pass != 0)
421 bfd_set_error (bfd_error_bad_value);
422 goto error_return;
424 element->archive_pass = 1;
426 undefs_tail = info->hash->undefs_tail;
428 if (! (*info->callbacks->add_archive_element) (info, element,
429 symdef->name))
430 goto error_return;
431 if (! elf_link_add_object_symbols (element, info))
432 goto error_return;
434 /* If there are any new undefined symbols, we need to make
435 another pass through the archive in order to see whether
436 they can be defined. FIXME: This isn't perfect, because
437 common symbols wind up on undefs_tail and because an
438 undefined symbol which is defined later on in this pass
439 does not require another pass. This isn't a bug, but it
440 does make the code less efficient than it could be. */
441 if (undefs_tail != info->hash->undefs_tail)
442 loop = true;
444 /* Look backward to mark all symbols from this object file
445 which we have already seen in this pass. */
446 mark = i;
449 included[mark] = true;
450 if (mark == 0)
451 break;
452 --mark;
454 while (symdefs[mark].file_offset == symdef->file_offset);
456 /* We mark subsequent symbols from this object file as we go
457 on through the loop. */
458 last = symdef->file_offset;
461 while (loop);
463 free (defined);
464 free (included);
466 return true;
468 error_return:
469 if (defined != (boolean *) NULL)
470 free (defined);
471 if (included != (boolean *) NULL)
472 free (included);
473 return false;
476 /* This function is called when we want to define a new symbol. It
477 handles the various cases which arise when we find a definition in
478 a dynamic object, or when there is already a definition in a
479 dynamic object. The new symbol is described by NAME, SYM, PSEC,
480 and PVALUE. We set SYM_HASH to the hash table entry. We set
481 OVERRIDE if the old symbol is overriding a new definition. We set
482 TYPE_CHANGE_OK if it is OK for the type to change. We set
483 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
484 change, we mean that we shouldn't warn if the type or size does
485 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
486 a shared object. */
488 static boolean
489 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
490 override, type_change_ok, size_change_ok, dt_needed)
491 bfd *abfd;
492 struct bfd_link_info *info;
493 const char *name;
494 Elf_Internal_Sym *sym;
495 asection **psec;
496 bfd_vma *pvalue;
497 struct elf_link_hash_entry **sym_hash;
498 boolean *override;
499 boolean *type_change_ok;
500 boolean *size_change_ok;
501 boolean dt_needed;
503 asection *sec;
504 struct elf_link_hash_entry *h;
505 int bind;
506 bfd *oldbfd;
507 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
509 *override = false;
511 sec = *psec;
512 bind = ELF_ST_BIND (sym->st_info);
514 if (! bfd_is_und_section (sec))
515 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
516 else
517 h = ((struct elf_link_hash_entry *)
518 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
519 if (h == NULL)
520 return false;
521 *sym_hash = h;
523 /* This code is for coping with dynamic objects, and is only useful
524 if we are doing an ELF link. */
525 if (info->hash->creator != abfd->xvec)
526 return true;
528 /* For merging, we only care about real symbols. */
530 while (h->root.type == bfd_link_hash_indirect
531 || h->root.type == bfd_link_hash_warning)
532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
534 /* If we just created the symbol, mark it as being an ELF symbol.
535 Other than that, there is nothing to do--there is no merge issue
536 with a newly defined symbol--so we just return. */
538 if (h->root.type == bfd_link_hash_new)
540 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
541 return true;
544 /* OLDBFD is a BFD associated with the existing symbol. */
546 switch (h->root.type)
548 default:
549 oldbfd = NULL;
550 break;
552 case bfd_link_hash_undefined:
553 case bfd_link_hash_undefweak:
554 oldbfd = h->root.u.undef.abfd;
555 break;
557 case bfd_link_hash_defined:
558 case bfd_link_hash_defweak:
559 oldbfd = h->root.u.def.section->owner;
560 break;
562 case bfd_link_hash_common:
563 oldbfd = h->root.u.c.p->section->owner;
564 break;
567 /* In cases involving weak versioned symbols, we may wind up trying
568 to merge a symbol with itself. Catch that here, to avoid the
569 confusion that results if we try to override a symbol with
570 itself. The additional tests catch cases like
571 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
572 dynamic object, which we do want to handle here. */
573 if (abfd == oldbfd
574 && ((abfd->flags & DYNAMIC) == 0
575 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
576 return true;
578 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
579 respectively, is from a dynamic object. */
581 if ((abfd->flags & DYNAMIC) != 0)
582 newdyn = true;
583 else
584 newdyn = false;
586 if (oldbfd != NULL)
587 olddyn = (oldbfd->flags & DYNAMIC) != 0;
588 else
590 asection *hsec;
592 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
593 indices used by MIPS ELF. */
594 switch (h->root.type)
596 default:
597 hsec = NULL;
598 break;
600 case bfd_link_hash_defined:
601 case bfd_link_hash_defweak:
602 hsec = h->root.u.def.section;
603 break;
605 case bfd_link_hash_common:
606 hsec = h->root.u.c.p->section;
607 break;
610 if (hsec == NULL)
611 olddyn = false;
612 else
613 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
616 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
617 respectively, appear to be a definition rather than reference. */
619 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
620 newdef = false;
621 else
622 newdef = true;
624 if (h->root.type == bfd_link_hash_undefined
625 || h->root.type == bfd_link_hash_undefweak
626 || h->root.type == bfd_link_hash_common)
627 olddef = false;
628 else
629 olddef = true;
631 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
632 symbol, respectively, appears to be a common symbol in a dynamic
633 object. If a symbol appears in an uninitialized section, and is
634 not weak, and is not a function, then it may be a common symbol
635 which was resolved when the dynamic object was created. We want
636 to treat such symbols specially, because they raise special
637 considerations when setting the symbol size: if the symbol
638 appears as a common symbol in a regular object, and the size in
639 the regular object is larger, we must make sure that we use the
640 larger size. This problematic case can always be avoided in C,
641 but it must be handled correctly when using Fortran shared
642 libraries.
644 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
645 likewise for OLDDYNCOMMON and OLDDEF.
647 Note that this test is just a heuristic, and that it is quite
648 possible to have an uninitialized symbol in a shared object which
649 is really a definition, rather than a common symbol. This could
650 lead to some minor confusion when the symbol really is a common
651 symbol in some regular object. However, I think it will be
652 harmless. */
654 if (newdyn
655 && newdef
656 && (sec->flags & SEC_ALLOC) != 0
657 && (sec->flags & SEC_LOAD) == 0
658 && sym->st_size > 0
659 && bind != STB_WEAK
660 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
661 newdyncommon = true;
662 else
663 newdyncommon = false;
665 if (olddyn
666 && olddef
667 && h->root.type == bfd_link_hash_defined
668 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
669 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
670 && (h->root.u.def.section->flags & SEC_LOAD) == 0
671 && h->size > 0
672 && h->type != STT_FUNC)
673 olddyncommon = true;
674 else
675 olddyncommon = false;
677 /* It's OK to change the type if either the existing symbol or the
678 new symbol is weak unless it comes from a DT_NEEDED entry of
679 a shared object, in which case, the DT_NEEDED entry may not be
680 required at the run time. */
682 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
683 || h->root.type == bfd_link_hash_undefweak
684 || bind == STB_WEAK)
685 *type_change_ok = true;
687 /* It's OK to change the size if either the existing symbol or the
688 new symbol is weak, or if the old symbol is undefined. */
690 if (*type_change_ok
691 || h->root.type == bfd_link_hash_undefined)
692 *size_change_ok = true;
694 /* If both the old and the new symbols look like common symbols in a
695 dynamic object, set the size of the symbol to the larger of the
696 two. */
698 if (olddyncommon
699 && newdyncommon
700 && sym->st_size != h->size)
702 /* Since we think we have two common symbols, issue a multiple
703 common warning if desired. Note that we only warn if the
704 size is different. If the size is the same, we simply let
705 the old symbol override the new one as normally happens with
706 symbols defined in dynamic objects. */
708 if (! ((*info->callbacks->multiple_common)
709 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
710 h->size, abfd, bfd_link_hash_common, sym->st_size)))
711 return false;
713 if (sym->st_size > h->size)
714 h->size = sym->st_size;
716 *size_change_ok = true;
719 /* If we are looking at a dynamic object, and we have found a
720 definition, we need to see if the symbol was already defined by
721 some other object. If so, we want to use the existing
722 definition, and we do not want to report a multiple symbol
723 definition error; we do this by clobbering *PSEC to be
724 bfd_und_section_ptr.
726 We treat a common symbol as a definition if the symbol in the
727 shared library is a function, since common symbols always
728 represent variables; this can cause confusion in principle, but
729 any such confusion would seem to indicate an erroneous program or
730 shared library. We also permit a common symbol in a regular
731 object to override a weak symbol in a shared object.
733 We prefer a non-weak definition in a shared library to a weak
734 definition in the executable unless it comes from a DT_NEEDED
735 entry of a shared object, in which case, the DT_NEEDED entry
736 may not be required at the run time. */
738 if (newdyn
739 && newdef
740 && (olddef
741 || (h->root.type == bfd_link_hash_common
742 && (bind == STB_WEAK
743 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
744 && (h->root.type != bfd_link_hash_defweak
745 || dt_needed
746 || bind == STB_WEAK))
748 *override = true;
749 newdef = false;
750 newdyncommon = false;
752 *psec = sec = bfd_und_section_ptr;
753 *size_change_ok = true;
755 /* If we get here when the old symbol is a common symbol, then
756 we are explicitly letting it override a weak symbol or
757 function in a dynamic object, and we don't want to warn about
758 a type change. If the old symbol is a defined symbol, a type
759 change warning may still be appropriate. */
761 if (h->root.type == bfd_link_hash_common)
762 *type_change_ok = true;
765 /* Handle the special case of an old common symbol merging with a
766 new symbol which looks like a common symbol in a shared object.
767 We change *PSEC and *PVALUE to make the new symbol look like a
768 common symbol, and let _bfd_generic_link_add_one_symbol will do
769 the right thing. */
771 if (newdyncommon
772 && h->root.type == bfd_link_hash_common)
774 *override = true;
775 newdef = false;
776 newdyncommon = false;
777 *pvalue = sym->st_size;
778 *psec = sec = bfd_com_section_ptr;
779 *size_change_ok = true;
782 /* If the old symbol is from a dynamic object, and the new symbol is
783 a definition which is not from a dynamic object, then the new
784 symbol overrides the old symbol. Symbols from regular files
785 always take precedence over symbols from dynamic objects, even if
786 they are defined after the dynamic object in the link.
788 As above, we again permit a common symbol in a regular object to
789 override a definition in a shared object if the shared object
790 symbol is a function or is weak.
792 As above, we permit a non-weak definition in a shared object to
793 override a weak definition in a regular object. */
795 if (! newdyn
796 && (newdef
797 || (bfd_is_com_section (sec)
798 && (h->root.type == bfd_link_hash_defweak
799 || h->type == STT_FUNC)))
800 && olddyn
801 && olddef
802 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
803 && (bind != STB_WEAK
804 || h->root.type == bfd_link_hash_defweak))
806 /* Change the hash table entry to undefined, and let
807 _bfd_generic_link_add_one_symbol do the right thing with the
808 new definition. */
810 h->root.type = bfd_link_hash_undefined;
811 h->root.u.undef.abfd = h->root.u.def.section->owner;
812 *size_change_ok = true;
814 olddef = false;
815 olddyncommon = false;
817 /* We again permit a type change when a common symbol may be
818 overriding a function. */
820 if (bfd_is_com_section (sec))
821 *type_change_ok = true;
823 /* This union may have been set to be non-NULL when this symbol
824 was seen in a dynamic object. We must force the union to be
825 NULL, so that it is correct for a regular symbol. */
827 h->verinfo.vertree = NULL;
829 /* In this special case, if H is the target of an indirection,
830 we want the caller to frob with H rather than with the
831 indirect symbol. That will permit the caller to redefine the
832 target of the indirection, rather than the indirect symbol
833 itself. FIXME: This will break the -y option if we store a
834 symbol with a different name. */
835 *sym_hash = h;
838 /* Handle the special case of a new common symbol merging with an
839 old symbol that looks like it might be a common symbol defined in
840 a shared object. Note that we have already handled the case in
841 which a new common symbol should simply override the definition
842 in the shared library. */
844 if (! newdyn
845 && bfd_is_com_section (sec)
846 && olddyncommon)
848 /* It would be best if we could set the hash table entry to a
849 common symbol, but we don't know what to use for the section
850 or the alignment. */
851 if (! ((*info->callbacks->multiple_common)
852 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
853 h->size, abfd, bfd_link_hash_common, sym->st_size)))
854 return false;
856 /* If the predumed common symbol in the dynamic object is
857 larger, pretend that the new symbol has its size. */
859 if (h->size > *pvalue)
860 *pvalue = h->size;
862 /* FIXME: We no longer know the alignment required by the symbol
863 in the dynamic object, so we just wind up using the one from
864 the regular object. */
866 olddef = false;
867 olddyncommon = false;
869 h->root.type = bfd_link_hash_undefined;
870 h->root.u.undef.abfd = h->root.u.def.section->owner;
872 *size_change_ok = true;
873 *type_change_ok = true;
875 h->verinfo.vertree = NULL;
878 /* Handle the special case of a weak definition in a regular object
879 followed by a non-weak definition in a shared object. In this
880 case, we prefer the definition in the shared object unless it
881 comes from a DT_NEEDED entry of a shared object, in which case,
882 the DT_NEEDED entry may not be required at the run time. */
883 if (olddef
884 && ! dt_needed
885 && h->root.type == bfd_link_hash_defweak
886 && newdef
887 && newdyn
888 && bind != STB_WEAK)
890 /* To make this work we have to frob the flags so that the rest
891 of the code does not think we are using the regular
892 definition. */
893 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
894 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
895 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
896 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
897 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
898 | ELF_LINK_HASH_DEF_DYNAMIC);
900 /* If H is the target of an indirection, we want the caller to
901 use H rather than the indirect symbol. Otherwise if we are
902 defining a new indirect symbol we will wind up attaching it
903 to the entry we are overriding. */
904 *sym_hash = h;
907 /* Handle the special case of a non-weak definition in a shared
908 object followed by a weak definition in a regular object. In
909 this case we prefer to definition in the shared object. To make
910 this work we have to tell the caller to not treat the new symbol
911 as a definition. */
912 if (olddef
913 && olddyn
914 && h->root.type != bfd_link_hash_defweak
915 && newdef
916 && ! newdyn
917 && bind == STB_WEAK)
918 *override = true;
920 return true;
923 /* This function is called to create an indirect symbol from the
924 default for the symbol with the default version if needed. The
925 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
926 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
927 indicates if it comes from a DT_NEEDED entry of a shared object. */
929 static boolean
930 elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
931 dynsym, override, dt_needed)
932 bfd *abfd;
933 struct bfd_link_info *info;
934 struct elf_link_hash_entry *h;
935 const char *name;
936 Elf_Internal_Sym *sym;
937 asection **sec;
938 bfd_vma *value;
939 boolean *dynsym;
940 boolean override;
941 boolean dt_needed;
943 boolean type_change_ok;
944 boolean size_change_ok;
945 char *shortname;
946 struct elf_link_hash_entry *hi;
947 struct elf_backend_data *bed;
948 boolean collect;
949 boolean dynamic;
950 char *p;
952 /* If this symbol has a version, and it is the default version, we
953 create an indirect symbol from the default name to the fully
954 decorated name. This will cause external references which do not
955 specify a version to be bound to this version of the symbol. */
956 p = strchr (name, ELF_VER_CHR);
957 if (p == NULL || p[1] != ELF_VER_CHR)
958 return true;
960 if (override)
962 /* We are overridden by an old defition. We need to check if we
963 need to crreate the indirect symbol from the default name. */
964 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
965 false, false);
966 BFD_ASSERT (hi != NULL);
967 if (hi == h)
968 return true;
969 while (hi->root.type == bfd_link_hash_indirect
970 || hi->root.type == bfd_link_hash_warning)
972 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
973 if (hi == h)
974 return true;
978 bed = get_elf_backend_data (abfd);
979 collect = bed->collect;
980 dynamic = (abfd->flags & DYNAMIC) != 0;
982 shortname = bfd_hash_allocate (&info->hash->table,
983 (size_t) (p - name + 1));
984 if (shortname == NULL)
985 return false;
986 strncpy (shortname, name, (size_t) (p - name));
987 shortname [p - name] = '\0';
989 /* We are going to create a new symbol. Merge it with any existing
990 symbol with this name. For the purposes of the merge, act as
991 though we were defining the symbol we just defined, although we
992 actually going to define an indirect symbol. */
993 type_change_ok = false;
994 size_change_ok = false;
995 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
996 &hi, &override, &type_change_ok,
997 &size_change_ok, dt_needed))
998 return false;
1000 if (! override)
1002 if (! (_bfd_generic_link_add_one_symbol
1003 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1004 (bfd_vma) 0, name, false, collect,
1005 (struct bfd_link_hash_entry **) &hi)))
1006 return false;
1008 else
1010 /* In this case the symbol named SHORTNAME is overriding the
1011 indirect symbol we want to add. We were planning on making
1012 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1013 is the name without a version. NAME is the fully versioned
1014 name, and it is the default version.
1016 Overriding means that we already saw a definition for the
1017 symbol SHORTNAME in a regular object, and it is overriding
1018 the symbol defined in the dynamic object.
1020 When this happens, we actually want to change NAME, the
1021 symbol we just added, to refer to SHORTNAME. This will cause
1022 references to NAME in the shared object to become references
1023 to SHORTNAME in the regular object. This is what we expect
1024 when we override a function in a shared object: that the
1025 references in the shared object will be mapped to the
1026 definition in the regular object. */
1028 while (hi->root.type == bfd_link_hash_indirect
1029 || hi->root.type == bfd_link_hash_warning)
1030 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1032 h->root.type = bfd_link_hash_indirect;
1033 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1034 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1036 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1037 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1038 if (hi->elf_link_hash_flags
1039 & (ELF_LINK_HASH_REF_REGULAR
1040 | ELF_LINK_HASH_DEF_REGULAR))
1042 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1043 return false;
1047 /* Now set HI to H, so that the following code will set the
1048 other fields correctly. */
1049 hi = h;
1052 /* If there is a duplicate definition somewhere, then HI may not
1053 point to an indirect symbol. We will have reported an error to
1054 the user in that case. */
1056 if (hi->root.type == bfd_link_hash_indirect)
1058 struct elf_link_hash_entry *ht;
1060 /* If the symbol became indirect, then we assume that we have
1061 not seen a definition before. */
1062 BFD_ASSERT ((hi->elf_link_hash_flags
1063 & (ELF_LINK_HASH_DEF_DYNAMIC
1064 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1066 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1067 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1069 /* See if the new flags lead us to realize that the symbol must
1070 be dynamic. */
1071 if (! *dynsym)
1073 if (! dynamic)
1075 if (info->shared
1076 || ((hi->elf_link_hash_flags
1077 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1078 *dynsym = true;
1080 else
1082 if ((hi->elf_link_hash_flags
1083 & ELF_LINK_HASH_REF_REGULAR) != 0)
1084 *dynsym = true;
1089 /* We also need to define an indirection from the nondefault version
1090 of the symbol. */
1092 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1093 if (shortname == NULL)
1094 return false;
1095 strncpy (shortname, name, (size_t) (p - name));
1096 strcpy (shortname + (p - name), p + 1);
1098 /* Once again, merge with any existing symbol. */
1099 type_change_ok = false;
1100 size_change_ok = false;
1101 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1102 &hi, &override, &type_change_ok,
1103 &size_change_ok, dt_needed))
1104 return false;
1106 if (override)
1108 /* Here SHORTNAME is a versioned name, so we don't expect to see
1109 the type of override we do in the case above. */
1110 (*_bfd_error_handler)
1111 (_("%s: warning: unexpected redefinition of `%s'"),
1112 bfd_archive_filename (abfd), shortname);
1114 else
1116 if (! (_bfd_generic_link_add_one_symbol
1117 (info, abfd, shortname, BSF_INDIRECT,
1118 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1119 collect, (struct bfd_link_hash_entry **) &hi)))
1120 return false;
1122 /* If there is a duplicate definition somewhere, then HI may not
1123 point to an indirect symbol. We will have reported an error
1124 to the user in that case. */
1126 if (hi->root.type == bfd_link_hash_indirect)
1128 /* If the symbol became indirect, then we assume that we have
1129 not seen a definition before. */
1130 BFD_ASSERT ((hi->elf_link_hash_flags
1131 & (ELF_LINK_HASH_DEF_DYNAMIC
1132 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1134 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1136 /* See if the new flags lead us to realize that the symbol
1137 must be dynamic. */
1138 if (! *dynsym)
1140 if (! dynamic)
1142 if (info->shared
1143 || ((hi->elf_link_hash_flags
1144 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1145 *dynsym = true;
1147 else
1149 if ((hi->elf_link_hash_flags
1150 & ELF_LINK_HASH_REF_REGULAR) != 0)
1151 *dynsym = true;
1157 return true;
1160 /* Add symbols from an ELF object file to the linker hash table. */
1162 static boolean
1163 elf_link_add_object_symbols (abfd, info)
1164 bfd *abfd;
1165 struct bfd_link_info *info;
1167 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1168 const Elf_Internal_Sym *,
1169 const char **, flagword *,
1170 asection **, bfd_vma *));
1171 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1172 asection *, const Elf_Internal_Rela *));
1173 boolean collect;
1174 Elf_Internal_Shdr *hdr;
1175 Elf_Internal_Shdr *shndx_hdr;
1176 bfd_size_type symcount;
1177 bfd_size_type extsymcount;
1178 bfd_size_type extsymoff;
1179 Elf_External_Sym *buf = NULL;
1180 Elf_External_Sym_Shndx *shndx_buf = NULL;
1181 Elf_External_Sym_Shndx *shndx;
1182 struct elf_link_hash_entry **sym_hash;
1183 boolean dynamic;
1184 Elf_External_Versym *extversym = NULL;
1185 Elf_External_Versym *ever;
1186 Elf_External_Dyn *dynbuf = NULL;
1187 struct elf_link_hash_entry *weaks;
1188 Elf_External_Sym *esym;
1189 Elf_External_Sym *esymend;
1190 struct elf_backend_data *bed;
1191 boolean dt_needed;
1192 struct elf_link_hash_table * hash_table;
1193 file_ptr pos;
1194 bfd_size_type amt;
1196 hash_table = elf_hash_table (info);
1198 bed = get_elf_backend_data (abfd);
1199 add_symbol_hook = bed->elf_add_symbol_hook;
1200 collect = bed->collect;
1202 if ((abfd->flags & DYNAMIC) == 0)
1203 dynamic = false;
1204 else
1206 dynamic = true;
1208 /* You can't use -r against a dynamic object. Also, there's no
1209 hope of using a dynamic object which does not exactly match
1210 the format of the output file. */
1211 if (info->relocateable || info->hash->creator != abfd->xvec)
1213 bfd_set_error (bfd_error_invalid_operation);
1214 goto error_return;
1218 /* As a GNU extension, any input sections which are named
1219 .gnu.warning.SYMBOL are treated as warning symbols for the given
1220 symbol. This differs from .gnu.warning sections, which generate
1221 warnings when they are included in an output file. */
1222 if (! info->shared)
1224 asection *s;
1226 for (s = abfd->sections; s != NULL; s = s->next)
1228 const char *name;
1230 name = bfd_get_section_name (abfd, s);
1231 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1233 char *msg;
1234 bfd_size_type sz;
1236 name += sizeof ".gnu.warning." - 1;
1238 /* If this is a shared object, then look up the symbol
1239 in the hash table. If it is there, and it is already
1240 been defined, then we will not be using the entry
1241 from this shared object, so we don't need to warn.
1242 FIXME: If we see the definition in a regular object
1243 later on, we will warn, but we shouldn't. The only
1244 fix is to keep track of what warnings we are supposed
1245 to emit, and then handle them all at the end of the
1246 link. */
1247 if (dynamic && abfd->xvec == info->hash->creator)
1249 struct elf_link_hash_entry *h;
1251 h = elf_link_hash_lookup (hash_table, name,
1252 false, false, true);
1254 /* FIXME: What about bfd_link_hash_common? */
1255 if (h != NULL
1256 && (h->root.type == bfd_link_hash_defined
1257 || h->root.type == bfd_link_hash_defweak))
1259 /* We don't want to issue this warning. Clobber
1260 the section size so that the warning does not
1261 get copied into the output file. */
1262 s->_raw_size = 0;
1263 continue;
1267 sz = bfd_section_size (abfd, s);
1268 msg = (char *) bfd_alloc (abfd, sz + 1);
1269 if (msg == NULL)
1270 goto error_return;
1272 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1273 goto error_return;
1275 msg[sz] = '\0';
1277 if (! (_bfd_generic_link_add_one_symbol
1278 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1279 false, collect, (struct bfd_link_hash_entry **) NULL)))
1280 goto error_return;
1282 if (! info->relocateable)
1284 /* Clobber the section size so that the warning does
1285 not get copied into the output file. */
1286 s->_raw_size = 0;
1292 /* If this is a dynamic object, we always link against the .dynsym
1293 symbol table, not the .symtab symbol table. The dynamic linker
1294 will only see the .dynsym symbol table, so there is no reason to
1295 look at .symtab for a dynamic object. */
1297 if (! dynamic || elf_dynsymtab (abfd) == 0)
1299 hdr = &elf_tdata (abfd)->symtab_hdr;
1300 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1302 else
1304 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1305 shndx_hdr = NULL;
1308 if (dynamic)
1310 /* Read in any version definitions. */
1312 if (! _bfd_elf_slurp_version_tables (abfd))
1313 goto error_return;
1315 /* Read in the symbol versions, but don't bother to convert them
1316 to internal format. */
1317 if (elf_dynversym (abfd) != 0)
1319 Elf_Internal_Shdr *versymhdr;
1321 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1322 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1323 if (extversym == NULL)
1324 goto error_return;
1325 amt = versymhdr->sh_size;
1326 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1327 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1328 goto error_return;
1332 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1334 /* The sh_info field of the symtab header tells us where the
1335 external symbols start. We don't care about the local symbols at
1336 this point. */
1337 if (elf_bad_symtab (abfd))
1339 extsymcount = symcount;
1340 extsymoff = 0;
1342 else
1344 extsymcount = symcount - hdr->sh_info;
1345 extsymoff = hdr->sh_info;
1348 amt = extsymcount * sizeof (Elf_External_Sym);
1349 buf = (Elf_External_Sym *) bfd_malloc (amt);
1350 if (buf == NULL && extsymcount != 0)
1351 goto error_return;
1353 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1355 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1356 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1357 if (shndx_buf == NULL && extsymcount != 0)
1358 goto error_return;
1361 /* We store a pointer to the hash table entry for each external
1362 symbol. */
1363 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1364 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1365 if (sym_hash == NULL)
1366 goto error_return;
1367 elf_sym_hashes (abfd) = sym_hash;
1369 dt_needed = false;
1371 if (! dynamic)
1373 /* If we are creating a shared library, create all the dynamic
1374 sections immediately. We need to attach them to something,
1375 so we attach them to this BFD, provided it is the right
1376 format. FIXME: If there are no input BFD's of the same
1377 format as the output, we can't make a shared library. */
1378 if (info->shared
1379 && is_elf_hash_table (info)
1380 && ! hash_table->dynamic_sections_created
1381 && abfd->xvec == info->hash->creator)
1383 if (! elf_link_create_dynamic_sections (abfd, info))
1384 goto error_return;
1387 else if (! is_elf_hash_table (info))
1388 goto error_return;
1389 else
1391 asection *s;
1392 boolean add_needed;
1393 const char *name;
1394 bfd_size_type oldsize;
1395 bfd_size_type strindex;
1397 /* Find the name to use in a DT_NEEDED entry that refers to this
1398 object. If the object has a DT_SONAME entry, we use it.
1399 Otherwise, if the generic linker stuck something in
1400 elf_dt_name, we use that. Otherwise, we just use the file
1401 name. If the generic linker put a null string into
1402 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1403 there is a DT_SONAME entry. */
1404 add_needed = true;
1405 name = bfd_get_filename (abfd);
1406 if (elf_dt_name (abfd) != NULL)
1408 name = elf_dt_name (abfd);
1409 if (*name == '\0')
1411 if (elf_dt_soname (abfd) != NULL)
1412 dt_needed = true;
1414 add_needed = false;
1417 s = bfd_get_section_by_name (abfd, ".dynamic");
1418 if (s != NULL)
1420 Elf_External_Dyn *extdyn;
1421 Elf_External_Dyn *extdynend;
1422 int elfsec;
1423 unsigned long shlink;
1424 int rpath;
1425 int runpath;
1427 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1428 if (dynbuf == NULL)
1429 goto error_return;
1431 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1432 (file_ptr) 0, s->_raw_size))
1433 goto error_return;
1435 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1436 if (elfsec == -1)
1437 goto error_return;
1438 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1441 /* The shared libraries distributed with hpux11 have a bogus
1442 sh_link field for the ".dynamic" section. This code detects
1443 when SHLINK refers to a section that is not a string table
1444 and tries to find the string table for the ".dynsym" section
1445 instead. */
1446 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1447 if (shdr->sh_type != SHT_STRTAB)
1449 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1450 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1451 if (elfdsec == -1)
1452 goto error_return;
1453 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1457 extdyn = dynbuf;
1458 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1459 rpath = 0;
1460 runpath = 0;
1461 for (; extdyn < extdynend; extdyn++)
1463 Elf_Internal_Dyn dyn;
1465 elf_swap_dyn_in (abfd, extdyn, &dyn);
1466 if (dyn.d_tag == DT_SONAME)
1468 unsigned int tagv = dyn.d_un.d_val;
1469 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1470 if (name == NULL)
1471 goto error_return;
1473 if (dyn.d_tag == DT_NEEDED)
1475 struct bfd_link_needed_list *n, **pn;
1476 char *fnm, *anm;
1477 unsigned int tagv = dyn.d_un.d_val;
1479 amt = sizeof (struct bfd_link_needed_list);
1480 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1481 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1482 if (n == NULL || fnm == NULL)
1483 goto error_return;
1484 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1485 if (anm == NULL)
1486 goto error_return;
1487 strcpy (anm, fnm);
1488 n->name = anm;
1489 n->by = abfd;
1490 n->next = NULL;
1491 for (pn = & hash_table->needed;
1492 *pn != NULL;
1493 pn = &(*pn)->next)
1495 *pn = n;
1497 if (dyn.d_tag == DT_RUNPATH)
1499 struct bfd_link_needed_list *n, **pn;
1500 char *fnm, *anm;
1501 unsigned int tagv = dyn.d_un.d_val;
1503 /* When we see DT_RPATH before DT_RUNPATH, we have
1504 to clear runpath. Do _NOT_ bfd_release, as that
1505 frees all more recently bfd_alloc'd blocks as
1506 well. */
1507 if (rpath && hash_table->runpath)
1508 hash_table->runpath = NULL;
1510 amt = sizeof (struct bfd_link_needed_list);
1511 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1512 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1513 if (n == NULL || fnm == NULL)
1514 goto error_return;
1515 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1516 if (anm == NULL)
1517 goto error_return;
1518 strcpy (anm, fnm);
1519 n->name = anm;
1520 n->by = abfd;
1521 n->next = NULL;
1522 for (pn = & hash_table->runpath;
1523 *pn != NULL;
1524 pn = &(*pn)->next)
1526 *pn = n;
1527 runpath = 1;
1528 rpath = 0;
1530 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1531 if (!runpath && dyn.d_tag == DT_RPATH)
1533 struct bfd_link_needed_list *n, **pn;
1534 char *fnm, *anm;
1535 unsigned int tagv = dyn.d_un.d_val;
1537 amt = sizeof (struct bfd_link_needed_list);
1538 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1539 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1540 if (n == NULL || fnm == NULL)
1541 goto error_return;
1542 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1543 if (anm == NULL)
1544 goto error_return;
1545 strcpy (anm, fnm);
1546 n->name = anm;
1547 n->by = abfd;
1548 n->next = NULL;
1549 for (pn = & hash_table->runpath;
1550 *pn != NULL;
1551 pn = &(*pn)->next)
1553 *pn = n;
1554 rpath = 1;
1558 free (dynbuf);
1559 dynbuf = NULL;
1562 /* We do not want to include any of the sections in a dynamic
1563 object in the output file. We hack by simply clobbering the
1564 list of sections in the BFD. This could be handled more
1565 cleanly by, say, a new section flag; the existing
1566 SEC_NEVER_LOAD flag is not the one we want, because that one
1567 still implies that the section takes up space in the output
1568 file. */
1569 bfd_section_list_clear (abfd);
1571 /* If this is the first dynamic object found in the link, create
1572 the special sections required for dynamic linking. */
1573 if (! hash_table->dynamic_sections_created)
1574 if (! elf_link_create_dynamic_sections (abfd, info))
1575 goto error_return;
1577 if (add_needed)
1579 /* Add a DT_NEEDED entry for this dynamic object. */
1580 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1581 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1582 if (strindex == (bfd_size_type) -1)
1583 goto error_return;
1585 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1587 asection *sdyn;
1588 Elf_External_Dyn *dyncon, *dynconend;
1590 /* The hash table size did not change, which means that
1591 the dynamic object name was already entered. If we
1592 have already included this dynamic object in the
1593 link, just ignore it. There is no reason to include
1594 a particular dynamic object more than once. */
1595 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1596 BFD_ASSERT (sdyn != NULL);
1598 dyncon = (Elf_External_Dyn *) sdyn->contents;
1599 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1600 sdyn->_raw_size);
1601 for (; dyncon < dynconend; dyncon++)
1603 Elf_Internal_Dyn dyn;
1605 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1606 if (dyn.d_tag == DT_NEEDED
1607 && dyn.d_un.d_val == strindex)
1609 if (buf != NULL)
1610 free (buf);
1611 if (extversym != NULL)
1612 free (extversym);
1613 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1614 return true;
1619 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1620 goto error_return;
1623 /* Save the SONAME, if there is one, because sometimes the
1624 linker emulation code will need to know it. */
1625 if (*name == '\0')
1626 name = basename (bfd_get_filename (abfd));
1627 elf_dt_name (abfd) = name;
1630 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1631 amt = extsymcount * sizeof (Elf_External_Sym);
1632 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1633 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1634 goto error_return;
1636 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1638 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1639 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
1640 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1641 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1642 goto error_return;
1645 weaks = NULL;
1647 ever = extversym != NULL ? extversym + extsymoff : NULL;
1648 esymend = buf + extsymcount;
1649 for (esym = buf, shndx = shndx_buf;
1650 esym < esymend;
1651 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
1652 shndx = (shndx != NULL ? shndx + 1 : NULL))
1654 Elf_Internal_Sym sym;
1655 int bind;
1656 bfd_vma value;
1657 asection *sec;
1658 flagword flags;
1659 const char *name;
1660 struct elf_link_hash_entry *h;
1661 boolean definition;
1662 boolean size_change_ok, type_change_ok;
1663 boolean new_weakdef;
1664 unsigned int old_alignment;
1665 boolean override;
1667 override = false;
1669 elf_swap_symbol_in (abfd, esym, shndx, &sym);
1671 flags = BSF_NO_FLAGS;
1672 sec = NULL;
1673 value = sym.st_value;
1674 *sym_hash = NULL;
1676 bind = ELF_ST_BIND (sym.st_info);
1677 if (bind == STB_LOCAL)
1679 /* This should be impossible, since ELF requires that all
1680 global symbols follow all local symbols, and that sh_info
1681 point to the first global symbol. Unfortunatealy, Irix 5
1682 screws this up. */
1683 continue;
1685 else if (bind == STB_GLOBAL)
1687 if (sym.st_shndx != SHN_UNDEF
1688 && sym.st_shndx != SHN_COMMON)
1689 flags = BSF_GLOBAL;
1691 else if (bind == STB_WEAK)
1692 flags = BSF_WEAK;
1693 else
1695 /* Leave it up to the processor backend. */
1698 if (sym.st_shndx == SHN_UNDEF)
1699 sec = bfd_und_section_ptr;
1700 else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
1702 sec = section_from_elf_index (abfd, sym.st_shndx);
1703 if (sec == NULL)
1704 sec = bfd_abs_section_ptr;
1705 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1706 value -= sec->vma;
1708 else if (sym.st_shndx == SHN_ABS)
1709 sec = bfd_abs_section_ptr;
1710 else if (sym.st_shndx == SHN_COMMON)
1712 sec = bfd_com_section_ptr;
1713 /* What ELF calls the size we call the value. What ELF
1714 calls the value we call the alignment. */
1715 value = sym.st_size;
1717 else
1719 /* Leave it up to the processor backend. */
1722 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1723 if (name == (const char *) NULL)
1724 goto error_return;
1726 if (add_symbol_hook)
1728 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1729 &value))
1730 goto error_return;
1732 /* The hook function sets the name to NULL if this symbol
1733 should be skipped for some reason. */
1734 if (name == (const char *) NULL)
1735 continue;
1738 /* Sanity check that all possibilities were handled. */
1739 if (sec == (asection *) NULL)
1741 bfd_set_error (bfd_error_bad_value);
1742 goto error_return;
1745 if (bfd_is_und_section (sec)
1746 || bfd_is_com_section (sec))
1747 definition = false;
1748 else
1749 definition = true;
1751 size_change_ok = false;
1752 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1753 old_alignment = 0;
1754 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1756 Elf_Internal_Versym iver;
1757 unsigned int vernum = 0;
1759 if (ever != NULL)
1761 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1762 vernum = iver.vs_vers & VERSYM_VERSION;
1764 /* If this is a hidden symbol, or if it is not version
1765 1, we append the version name to the symbol name.
1766 However, we do not modify a non-hidden absolute
1767 symbol, because it might be the version symbol
1768 itself. FIXME: What if it isn't? */
1769 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1770 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1772 const char *verstr;
1773 unsigned int namelen;
1774 bfd_size_type newlen;
1775 char *newname, *p;
1777 if (sym.st_shndx != SHN_UNDEF)
1779 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1781 (*_bfd_error_handler)
1782 (_("%s: %s: invalid version %u (max %d)"),
1783 bfd_archive_filename (abfd), name, vernum,
1784 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1785 bfd_set_error (bfd_error_bad_value);
1786 goto error_return;
1788 else if (vernum > 1)
1789 verstr =
1790 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1791 else
1792 verstr = "";
1794 else
1796 /* We cannot simply test for the number of
1797 entries in the VERNEED section since the
1798 numbers for the needed versions do not start
1799 at 0. */
1800 Elf_Internal_Verneed *t;
1802 verstr = NULL;
1803 for (t = elf_tdata (abfd)->verref;
1804 t != NULL;
1805 t = t->vn_nextref)
1807 Elf_Internal_Vernaux *a;
1809 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1811 if (a->vna_other == vernum)
1813 verstr = a->vna_nodename;
1814 break;
1817 if (a != NULL)
1818 break;
1820 if (verstr == NULL)
1822 (*_bfd_error_handler)
1823 (_("%s: %s: invalid needed version %d"),
1824 bfd_archive_filename (abfd), name, vernum);
1825 bfd_set_error (bfd_error_bad_value);
1826 goto error_return;
1830 namelen = strlen (name);
1831 newlen = namelen + strlen (verstr) + 2;
1832 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1833 ++newlen;
1835 newname = (char *) bfd_alloc (abfd, newlen);
1836 if (newname == NULL)
1837 goto error_return;
1838 strcpy (newname, name);
1839 p = newname + namelen;
1840 *p++ = ELF_VER_CHR;
1841 /* If this is a defined non-hidden version symbol,
1842 we add another @ to the name. This indicates the
1843 default version of the symbol. */
1844 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1845 && sym.st_shndx != SHN_UNDEF)
1846 *p++ = ELF_VER_CHR;
1847 strcpy (p, verstr);
1849 name = newname;
1853 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1854 sym_hash, &override, &type_change_ok,
1855 &size_change_ok, dt_needed))
1856 goto error_return;
1858 if (override)
1859 definition = false;
1861 h = *sym_hash;
1862 while (h->root.type == bfd_link_hash_indirect
1863 || h->root.type == bfd_link_hash_warning)
1864 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1866 /* Remember the old alignment if this is a common symbol, so
1867 that we don't reduce the alignment later on. We can't
1868 check later, because _bfd_generic_link_add_one_symbol
1869 will set a default for the alignment which we want to
1870 override. */
1871 if (h->root.type == bfd_link_hash_common)
1872 old_alignment = h->root.u.c.p->alignment_power;
1874 if (elf_tdata (abfd)->verdef != NULL
1875 && ! override
1876 && vernum > 1
1877 && definition)
1878 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1881 if (! (_bfd_generic_link_add_one_symbol
1882 (info, abfd, name, flags, sec, value, (const char *) NULL,
1883 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1884 goto error_return;
1886 h = *sym_hash;
1887 while (h->root.type == bfd_link_hash_indirect
1888 || h->root.type == bfd_link_hash_warning)
1889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1890 *sym_hash = h;
1892 new_weakdef = false;
1893 if (dynamic
1894 && definition
1895 && (flags & BSF_WEAK) != 0
1896 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1897 && info->hash->creator->flavour == bfd_target_elf_flavour
1898 && h->weakdef == NULL)
1900 /* Keep a list of all weak defined non function symbols from
1901 a dynamic object, using the weakdef field. Later in this
1902 function we will set the weakdef field to the correct
1903 value. We only put non-function symbols from dynamic
1904 objects on this list, because that happens to be the only
1905 time we need to know the normal symbol corresponding to a
1906 weak symbol, and the information is time consuming to
1907 figure out. If the weakdef field is not already NULL,
1908 then this symbol was already defined by some previous
1909 dynamic object, and we will be using that previous
1910 definition anyhow. */
1912 h->weakdef = weaks;
1913 weaks = h;
1914 new_weakdef = true;
1917 /* Set the alignment of a common symbol. */
1918 if (sym.st_shndx == SHN_COMMON
1919 && h->root.type == bfd_link_hash_common)
1921 unsigned int align;
1923 align = bfd_log2 (sym.st_value);
1924 if (align > old_alignment
1925 /* Permit an alignment power of zero if an alignment of one
1926 is specified and no other alignments have been specified. */
1927 || (sym.st_value == 1 && old_alignment == 0))
1928 h->root.u.c.p->alignment_power = align;
1931 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1933 int old_flags;
1934 boolean dynsym;
1935 int new_flag;
1937 /* Remember the symbol size and type. */
1938 if (sym.st_size != 0
1939 && (definition || h->size == 0))
1941 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1942 (*_bfd_error_handler)
1943 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1944 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1945 bfd_archive_filename (abfd));
1947 h->size = sym.st_size;
1950 /* If this is a common symbol, then we always want H->SIZE
1951 to be the size of the common symbol. The code just above
1952 won't fix the size if a common symbol becomes larger. We
1953 don't warn about a size change here, because that is
1954 covered by --warn-common. */
1955 if (h->root.type == bfd_link_hash_common)
1956 h->size = h->root.u.c.size;
1958 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1959 && (definition || h->type == STT_NOTYPE))
1961 if (h->type != STT_NOTYPE
1962 && h->type != ELF_ST_TYPE (sym.st_info)
1963 && ! type_change_ok)
1964 (*_bfd_error_handler)
1965 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1966 name, h->type, ELF_ST_TYPE (sym.st_info),
1967 bfd_archive_filename (abfd));
1969 h->type = ELF_ST_TYPE (sym.st_info);
1972 /* If st_other has a processor-specific meaning, specific code
1973 might be needed here. */
1974 if (sym.st_other != 0)
1976 /* Combine visibilities, using the most constraining one. */
1977 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1978 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1980 if (symvis && (hvis > symvis || hvis == 0))
1981 h->other = sym.st_other;
1983 /* If neither has visibility, use the st_other of the
1984 definition. This is an arbitrary choice, since the
1985 other bits have no general meaning. */
1986 if (!symvis && !hvis
1987 && (definition || h->other == 0))
1988 h->other = sym.st_other;
1991 /* Set a flag in the hash table entry indicating the type of
1992 reference or definition we just found. Keep a count of
1993 the number of dynamic symbols we find. A dynamic symbol
1994 is one which is referenced or defined by both a regular
1995 object and a shared object. */
1996 old_flags = h->elf_link_hash_flags;
1997 dynsym = false;
1998 if (! dynamic)
2000 if (! definition)
2002 new_flag = ELF_LINK_HASH_REF_REGULAR;
2003 if (bind != STB_WEAK)
2004 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2006 else
2007 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2008 if (info->shared
2009 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2010 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2011 dynsym = true;
2013 else
2015 if (! definition)
2016 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2017 else
2018 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2019 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2020 | ELF_LINK_HASH_REF_REGULAR)) != 0
2021 || (h->weakdef != NULL
2022 && ! new_weakdef
2023 && h->weakdef->dynindx != -1))
2024 dynsym = true;
2027 h->elf_link_hash_flags |= new_flag;
2029 /* Check to see if we need to add an indirect symbol for
2030 the default name. */
2031 if (definition || h->root.type == bfd_link_hash_common)
2032 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2033 &sec, &value, &dynsym,
2034 override, dt_needed))
2035 goto error_return;
2037 if (dynsym && h->dynindx == -1)
2039 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2040 goto error_return;
2041 if (h->weakdef != NULL
2042 && ! new_weakdef
2043 && h->weakdef->dynindx == -1)
2045 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2046 goto error_return;
2049 else if (dynsym && h->dynindx != -1)
2050 /* If the symbol already has a dynamic index, but
2051 visibility says it should not be visible, turn it into
2052 a local symbol. */
2053 switch (ELF_ST_VISIBILITY (h->other))
2055 case STV_INTERNAL:
2056 case STV_HIDDEN:
2057 (*bed->elf_backend_hide_symbol) (info, h, true);
2058 break;
2061 if (dt_needed && definition
2062 && (h->elf_link_hash_flags
2063 & ELF_LINK_HASH_REF_REGULAR) != 0)
2065 bfd_size_type oldsize;
2066 bfd_size_type strindex;
2068 if (! is_elf_hash_table (info))
2069 goto error_return;
2071 /* The symbol from a DT_NEEDED object is referenced from
2072 the regular object to create a dynamic executable. We
2073 have to make sure there is a DT_NEEDED entry for it. */
2075 dt_needed = false;
2076 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2077 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2078 elf_dt_soname (abfd), false);
2079 if (strindex == (bfd_size_type) -1)
2080 goto error_return;
2082 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2084 asection *sdyn;
2085 Elf_External_Dyn *dyncon, *dynconend;
2087 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2088 ".dynamic");
2089 BFD_ASSERT (sdyn != NULL);
2091 dyncon = (Elf_External_Dyn *) sdyn->contents;
2092 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2093 sdyn->_raw_size);
2094 for (; dyncon < dynconend; dyncon++)
2096 Elf_Internal_Dyn dyn;
2098 elf_swap_dyn_in (hash_table->dynobj,
2099 dyncon, &dyn);
2100 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2101 dyn.d_un.d_val != strindex);
2105 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2106 goto error_return;
2111 /* Now set the weakdefs field correctly for all the weak defined
2112 symbols we found. The only way to do this is to search all the
2113 symbols. Since we only need the information for non functions in
2114 dynamic objects, that's the only time we actually put anything on
2115 the list WEAKS. We need this information so that if a regular
2116 object refers to a symbol defined weakly in a dynamic object, the
2117 real symbol in the dynamic object is also put in the dynamic
2118 symbols; we also must arrange for both symbols to point to the
2119 same memory location. We could handle the general case of symbol
2120 aliasing, but a general symbol alias can only be generated in
2121 assembler code, handling it correctly would be very time
2122 consuming, and other ELF linkers don't handle general aliasing
2123 either. */
2124 while (weaks != NULL)
2126 struct elf_link_hash_entry *hlook;
2127 asection *slook;
2128 bfd_vma vlook;
2129 struct elf_link_hash_entry **hpp;
2130 struct elf_link_hash_entry **hppend;
2132 hlook = weaks;
2133 weaks = hlook->weakdef;
2134 hlook->weakdef = NULL;
2136 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2137 || hlook->root.type == bfd_link_hash_defweak
2138 || hlook->root.type == bfd_link_hash_common
2139 || hlook->root.type == bfd_link_hash_indirect);
2140 slook = hlook->root.u.def.section;
2141 vlook = hlook->root.u.def.value;
2143 hpp = elf_sym_hashes (abfd);
2144 hppend = hpp + extsymcount;
2145 for (; hpp < hppend; hpp++)
2147 struct elf_link_hash_entry *h;
2149 h = *hpp;
2150 if (h != NULL && h != hlook
2151 && h->root.type == bfd_link_hash_defined
2152 && h->root.u.def.section == slook
2153 && h->root.u.def.value == vlook)
2155 hlook->weakdef = h;
2157 /* If the weak definition is in the list of dynamic
2158 symbols, make sure the real definition is put there
2159 as well. */
2160 if (hlook->dynindx != -1
2161 && h->dynindx == -1)
2163 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2164 goto error_return;
2167 /* If the real definition is in the list of dynamic
2168 symbols, make sure the weak definition is put there
2169 as well. If we don't do this, then the dynamic
2170 loader might not merge the entries for the real
2171 definition and the weak definition. */
2172 if (h->dynindx != -1
2173 && hlook->dynindx == -1)
2175 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2176 goto error_return;
2179 break;
2184 if (buf != NULL)
2186 free (buf);
2187 buf = NULL;
2190 if (extversym != NULL)
2192 free (extversym);
2193 extversym = NULL;
2196 /* If this object is the same format as the output object, and it is
2197 not a shared library, then let the backend look through the
2198 relocs.
2200 This is required to build global offset table entries and to
2201 arrange for dynamic relocs. It is not required for the
2202 particular common case of linking non PIC code, even when linking
2203 against shared libraries, but unfortunately there is no way of
2204 knowing whether an object file has been compiled PIC or not.
2205 Looking through the relocs is not particularly time consuming.
2206 The problem is that we must either (1) keep the relocs in memory,
2207 which causes the linker to require additional runtime memory or
2208 (2) read the relocs twice from the input file, which wastes time.
2209 This would be a good case for using mmap.
2211 I have no idea how to handle linking PIC code into a file of a
2212 different format. It probably can't be done. */
2213 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2214 if (! dynamic
2215 && abfd->xvec == info->hash->creator
2216 && check_relocs != NULL)
2218 asection *o;
2220 for (o = abfd->sections; o != NULL; o = o->next)
2222 Elf_Internal_Rela *internal_relocs;
2223 boolean ok;
2225 if ((o->flags & SEC_RELOC) == 0
2226 || o->reloc_count == 0
2227 || ((info->strip == strip_all || info->strip == strip_debugger)
2228 && (o->flags & SEC_DEBUGGING) != 0)
2229 || bfd_is_abs_section (o->output_section))
2230 continue;
2232 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2233 (abfd, o, (PTR) NULL,
2234 (Elf_Internal_Rela *) NULL,
2235 info->keep_memory));
2236 if (internal_relocs == NULL)
2237 goto error_return;
2239 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2241 if (! info->keep_memory)
2242 free (internal_relocs);
2244 if (! ok)
2245 goto error_return;
2249 /* If this is a non-traditional, non-relocateable link, try to
2250 optimize the handling of the .stab/.stabstr sections. */
2251 if (! dynamic
2252 && ! info->relocateable
2253 && ! info->traditional_format
2254 && info->hash->creator->flavour == bfd_target_elf_flavour
2255 && is_elf_hash_table (info)
2256 && (info->strip != strip_all && info->strip != strip_debugger))
2258 asection *stab, *stabstr;
2260 stab = bfd_get_section_by_name (abfd, ".stab");
2261 if (stab != NULL && !(stab->flags & SEC_MERGE))
2263 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2265 if (stabstr != NULL)
2267 struct bfd_elf_section_data *secdata;
2269 secdata = elf_section_data (stab);
2270 if (! _bfd_link_section_stabs (abfd,
2271 & hash_table->stab_info,
2272 stab, stabstr,
2273 &secdata->sec_info))
2274 goto error_return;
2275 if (secdata->sec_info)
2276 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2281 if (! info->relocateable && ! dynamic
2282 && is_elf_hash_table (info))
2284 asection *s;
2286 for (s = abfd->sections; s != NULL; s = s->next)
2287 if (s->flags & SEC_MERGE)
2289 struct bfd_elf_section_data *secdata;
2291 secdata = elf_section_data (s);
2292 if (! _bfd_merge_section (abfd,
2293 & hash_table->merge_info,
2294 s, &secdata->sec_info))
2295 goto error_return;
2296 else if (secdata->sec_info)
2297 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2301 return true;
2303 error_return:
2304 if (buf != NULL)
2305 free (buf);
2306 if (dynbuf != NULL)
2307 free (dynbuf);
2308 if (extversym != NULL)
2309 free (extversym);
2310 return false;
2313 /* Create some sections which will be filled in with dynamic linking
2314 information. ABFD is an input file which requires dynamic sections
2315 to be created. The dynamic sections take up virtual memory space
2316 when the final executable is run, so we need to create them before
2317 addresses are assigned to the output sections. We work out the
2318 actual contents and size of these sections later. */
2320 boolean
2321 elf_link_create_dynamic_sections (abfd, info)
2322 bfd *abfd;
2323 struct bfd_link_info *info;
2325 flagword flags;
2326 register asection *s;
2327 struct elf_link_hash_entry *h;
2328 struct elf_backend_data *bed;
2330 if (! is_elf_hash_table (info))
2331 return false;
2333 if (elf_hash_table (info)->dynamic_sections_created)
2334 return true;
2336 /* Make sure that all dynamic sections use the same input BFD. */
2337 if (elf_hash_table (info)->dynobj == NULL)
2338 elf_hash_table (info)->dynobj = abfd;
2339 else
2340 abfd = elf_hash_table (info)->dynobj;
2342 /* Note that we set the SEC_IN_MEMORY flag for all of these
2343 sections. */
2344 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2345 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2347 /* A dynamically linked executable has a .interp section, but a
2348 shared library does not. */
2349 if (! info->shared)
2351 s = bfd_make_section (abfd, ".interp");
2352 if (s == NULL
2353 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2354 return false;
2357 if (! info->traditional_format
2358 && info->hash->creator->flavour == bfd_target_elf_flavour)
2360 s = bfd_make_section (abfd, ".eh_frame_hdr");
2361 if (s == NULL
2362 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2363 || ! bfd_set_section_alignment (abfd, s, 2))
2364 return false;
2367 /* Create sections to hold version informations. These are removed
2368 if they are not needed. */
2369 s = bfd_make_section (abfd, ".gnu.version_d");
2370 if (s == NULL
2371 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2372 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2373 return false;
2375 s = bfd_make_section (abfd, ".gnu.version");
2376 if (s == NULL
2377 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2378 || ! bfd_set_section_alignment (abfd, s, 1))
2379 return false;
2381 s = bfd_make_section (abfd, ".gnu.version_r");
2382 if (s == NULL
2383 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2384 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2385 return false;
2387 s = bfd_make_section (abfd, ".dynsym");
2388 if (s == NULL
2389 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2390 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2391 return false;
2393 s = bfd_make_section (abfd, ".dynstr");
2394 if (s == NULL
2395 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2396 return false;
2398 /* Create a strtab to hold the dynamic symbol names. */
2399 if (elf_hash_table (info)->dynstr == NULL)
2401 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2402 if (elf_hash_table (info)->dynstr == NULL)
2403 return false;
2406 s = bfd_make_section (abfd, ".dynamic");
2407 if (s == NULL
2408 || ! bfd_set_section_flags (abfd, s, flags)
2409 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2410 return false;
2412 /* The special symbol _DYNAMIC is always set to the start of the
2413 .dynamic section. This call occurs before we have processed the
2414 symbols for any dynamic object, so we don't have to worry about
2415 overriding a dynamic definition. We could set _DYNAMIC in a
2416 linker script, but we only want to define it if we are, in fact,
2417 creating a .dynamic section. We don't want to define it if there
2418 is no .dynamic section, since on some ELF platforms the start up
2419 code examines it to decide how to initialize the process. */
2420 h = NULL;
2421 if (! (_bfd_generic_link_add_one_symbol
2422 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2423 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2424 (struct bfd_link_hash_entry **) &h)))
2425 return false;
2426 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2427 h->type = STT_OBJECT;
2429 if (info->shared
2430 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2431 return false;
2433 bed = get_elf_backend_data (abfd);
2435 s = bfd_make_section (abfd, ".hash");
2436 if (s == NULL
2437 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2438 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2439 return false;
2440 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2442 /* Let the backend create the rest of the sections. This lets the
2443 backend set the right flags. The backend will normally create
2444 the .got and .plt sections. */
2445 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2446 return false;
2448 elf_hash_table (info)->dynamic_sections_created = true;
2450 return true;
2453 /* Add an entry to the .dynamic table. */
2455 boolean
2456 elf_add_dynamic_entry (info, tag, val)
2457 struct bfd_link_info *info;
2458 bfd_vma tag;
2459 bfd_vma val;
2461 Elf_Internal_Dyn dyn;
2462 bfd *dynobj;
2463 asection *s;
2464 bfd_size_type newsize;
2465 bfd_byte *newcontents;
2467 if (! is_elf_hash_table (info))
2468 return false;
2470 dynobj = elf_hash_table (info)->dynobj;
2472 s = bfd_get_section_by_name (dynobj, ".dynamic");
2473 BFD_ASSERT (s != NULL);
2475 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2476 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2477 if (newcontents == NULL)
2478 return false;
2480 dyn.d_tag = tag;
2481 dyn.d_un.d_val = val;
2482 elf_swap_dyn_out (dynobj, &dyn,
2483 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2485 s->_raw_size = newsize;
2486 s->contents = newcontents;
2488 return true;
2491 /* Record a new local dynamic symbol. */
2493 boolean
2494 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2495 struct bfd_link_info *info;
2496 bfd *input_bfd;
2497 long input_indx;
2499 struct elf_link_local_dynamic_entry *entry;
2500 struct elf_link_hash_table *eht;
2501 struct elf_strtab_hash *dynstr;
2502 Elf_External_Sym esym;
2503 Elf_External_Sym_Shndx eshndx;
2504 Elf_External_Sym_Shndx *shndx;
2505 unsigned long dynstr_index;
2506 char *name;
2507 file_ptr pos;
2508 bfd_size_type amt;
2510 if (! is_elf_hash_table (info))
2511 return false;
2513 /* See if the entry exists already. */
2514 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2515 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2516 return true;
2518 entry = (struct elf_link_local_dynamic_entry *)
2519 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2520 if (entry == NULL)
2521 return false;
2523 /* Go find the symbol, so that we can find it's name. */
2524 amt = sizeof (Elf_External_Sym);
2525 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2526 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2527 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2528 return false;
2529 shndx = NULL;
2530 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2532 amt = sizeof (Elf_External_Sym_Shndx);
2533 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2534 pos += input_indx * amt;
2535 shndx = &eshndx;
2536 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2537 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2538 return false;
2540 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
2542 name = (bfd_elf_string_from_elf_section
2543 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2544 entry->isym.st_name));
2546 dynstr = elf_hash_table (info)->dynstr;
2547 if (dynstr == NULL)
2549 /* Create a strtab to hold the dynamic symbol names. */
2550 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2551 if (dynstr == NULL)
2552 return false;
2555 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2556 if (dynstr_index == (unsigned long) -1)
2557 return false;
2558 entry->isym.st_name = dynstr_index;
2560 eht = elf_hash_table (info);
2562 entry->next = eht->dynlocal;
2563 eht->dynlocal = entry;
2564 entry->input_bfd = input_bfd;
2565 entry->input_indx = input_indx;
2566 eht->dynsymcount++;
2568 /* Whatever binding the symbol had before, it's now local. */
2569 entry->isym.st_info
2570 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2572 /* The dynindx will be set at the end of size_dynamic_sections. */
2574 return true;
2577 /* Read and swap the relocs from the section indicated by SHDR. This
2578 may be either a REL or a RELA section. The relocations are
2579 translated into RELA relocations and stored in INTERNAL_RELOCS,
2580 which should have already been allocated to contain enough space.
2581 The EXTERNAL_RELOCS are a buffer where the external form of the
2582 relocations should be stored.
2584 Returns false if something goes wrong. */
2586 static boolean
2587 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2588 internal_relocs)
2589 bfd *abfd;
2590 Elf_Internal_Shdr *shdr;
2591 PTR external_relocs;
2592 Elf_Internal_Rela *internal_relocs;
2594 struct elf_backend_data *bed;
2595 bfd_size_type amt;
2597 /* If there aren't any relocations, that's OK. */
2598 if (!shdr)
2599 return true;
2601 /* Position ourselves at the start of the section. */
2602 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2603 return false;
2605 /* Read the relocations. */
2606 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2607 return false;
2609 bed = get_elf_backend_data (abfd);
2611 /* Convert the external relocations to the internal format. */
2612 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2614 Elf_External_Rel *erel;
2615 Elf_External_Rel *erelend;
2616 Elf_Internal_Rela *irela;
2617 Elf_Internal_Rel *irel;
2619 erel = (Elf_External_Rel *) external_relocs;
2620 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2621 irela = internal_relocs;
2622 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2623 irel = bfd_alloc (abfd, amt);
2624 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2626 unsigned int i;
2628 if (bed->s->swap_reloc_in)
2629 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2630 else
2631 elf_swap_reloc_in (abfd, erel, irel);
2633 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2635 irela[i].r_offset = irel[i].r_offset;
2636 irela[i].r_info = irel[i].r_info;
2637 irela[i].r_addend = 0;
2641 else
2643 Elf_External_Rela *erela;
2644 Elf_External_Rela *erelaend;
2645 Elf_Internal_Rela *irela;
2647 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2649 erela = (Elf_External_Rela *) external_relocs;
2650 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2651 irela = internal_relocs;
2652 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2654 if (bed->s->swap_reloca_in)
2655 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2656 else
2657 elf_swap_reloca_in (abfd, erela, irela);
2661 return true;
2664 /* Read and swap the relocs for a section O. They may have been
2665 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2666 not NULL, they are used as buffers to read into. They are known to
2667 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2668 the return value is allocated using either malloc or bfd_alloc,
2669 according to the KEEP_MEMORY argument. If O has two relocation
2670 sections (both REL and RELA relocations), then the REL_HDR
2671 relocations will appear first in INTERNAL_RELOCS, followed by the
2672 REL_HDR2 relocations. */
2674 Elf_Internal_Rela *
2675 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2676 keep_memory)
2677 bfd *abfd;
2678 asection *o;
2679 PTR external_relocs;
2680 Elf_Internal_Rela *internal_relocs;
2681 boolean keep_memory;
2683 Elf_Internal_Shdr *rel_hdr;
2684 PTR alloc1 = NULL;
2685 Elf_Internal_Rela *alloc2 = NULL;
2686 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2688 if (elf_section_data (o)->relocs != NULL)
2689 return elf_section_data (o)->relocs;
2691 if (o->reloc_count == 0)
2692 return NULL;
2694 rel_hdr = &elf_section_data (o)->rel_hdr;
2696 if (internal_relocs == NULL)
2698 bfd_size_type size;
2700 size = o->reloc_count;
2701 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2702 if (keep_memory)
2703 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2704 else
2705 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2706 if (internal_relocs == NULL)
2707 goto error_return;
2710 if (external_relocs == NULL)
2712 bfd_size_type size = rel_hdr->sh_size;
2714 if (elf_section_data (o)->rel_hdr2)
2715 size += elf_section_data (o)->rel_hdr2->sh_size;
2716 alloc1 = (PTR) bfd_malloc (size);
2717 if (alloc1 == NULL)
2718 goto error_return;
2719 external_relocs = alloc1;
2722 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2723 external_relocs,
2724 internal_relocs))
2725 goto error_return;
2726 if (!elf_link_read_relocs_from_section
2727 (abfd,
2728 elf_section_data (o)->rel_hdr2,
2729 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2730 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2731 * bed->s->int_rels_per_ext_rel)))
2732 goto error_return;
2734 /* Cache the results for next time, if we can. */
2735 if (keep_memory)
2736 elf_section_data (o)->relocs = internal_relocs;
2738 if (alloc1 != NULL)
2739 free (alloc1);
2741 /* Don't free alloc2, since if it was allocated we are passing it
2742 back (under the name of internal_relocs). */
2744 return internal_relocs;
2746 error_return:
2747 if (alloc1 != NULL)
2748 free (alloc1);
2749 if (alloc2 != NULL)
2750 free (alloc2);
2751 return NULL;
2754 /* Record an assignment to a symbol made by a linker script. We need
2755 this in case some dynamic object refers to this symbol. */
2757 boolean
2758 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2759 bfd *output_bfd ATTRIBUTE_UNUSED;
2760 struct bfd_link_info *info;
2761 const char *name;
2762 boolean provide;
2764 struct elf_link_hash_entry *h;
2766 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2767 return true;
2769 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2770 if (h == NULL)
2771 return false;
2773 if (h->root.type == bfd_link_hash_new)
2774 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2776 /* If this symbol is being provided by the linker script, and it is
2777 currently defined by a dynamic object, but not by a regular
2778 object, then mark it as undefined so that the generic linker will
2779 force the correct value. */
2780 if (provide
2781 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2782 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2783 h->root.type = bfd_link_hash_undefined;
2785 /* If this symbol is not being provided by the linker script, and it is
2786 currently defined by a dynamic object, but not by a regular object,
2787 then clear out any version information because the symbol will not be
2788 associated with the dynamic object any more. */
2789 if (!provide
2790 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2791 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2792 h->verinfo.verdef = NULL;
2794 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2796 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2797 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2798 || info->shared)
2799 && h->dynindx == -1)
2801 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2802 return false;
2804 /* If this is a weak defined symbol, and we know a corresponding
2805 real symbol from the same dynamic object, make sure the real
2806 symbol is also made into a dynamic symbol. */
2807 if (h->weakdef != NULL
2808 && h->weakdef->dynindx == -1)
2810 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2811 return false;
2815 return true;
2818 /* This structure is used to pass information to
2819 elf_link_assign_sym_version. */
2821 struct elf_assign_sym_version_info
2823 /* Output BFD. */
2824 bfd *output_bfd;
2825 /* General link information. */
2826 struct bfd_link_info *info;
2827 /* Version tree. */
2828 struct bfd_elf_version_tree *verdefs;
2829 /* Whether we had a failure. */
2830 boolean failed;
2833 /* This structure is used to pass information to
2834 elf_link_find_version_dependencies. */
2836 struct elf_find_verdep_info
2838 /* Output BFD. */
2839 bfd *output_bfd;
2840 /* General link information. */
2841 struct bfd_link_info *info;
2842 /* The number of dependencies. */
2843 unsigned int vers;
2844 /* Whether we had a failure. */
2845 boolean failed;
2848 /* Array used to determine the number of hash table buckets to use
2849 based on the number of symbols there are. If there are fewer than
2850 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2851 fewer than 37 we use 17 buckets, and so forth. We never use more
2852 than 32771 buckets. */
2854 static const size_t elf_buckets[] =
2856 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2857 16411, 32771, 0
2860 /* Compute bucket count for hashing table. We do not use a static set
2861 of possible tables sizes anymore. Instead we determine for all
2862 possible reasonable sizes of the table the outcome (i.e., the
2863 number of collisions etc) and choose the best solution. The
2864 weighting functions are not too simple to allow the table to grow
2865 without bounds. Instead one of the weighting factors is the size.
2866 Therefore the result is always a good payoff between few collisions
2867 (= short chain lengths) and table size. */
2868 static size_t
2869 compute_bucket_count (info)
2870 struct bfd_link_info *info;
2872 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2873 size_t best_size = 0;
2874 unsigned long int *hashcodes;
2875 unsigned long int *hashcodesp;
2876 unsigned long int i;
2877 bfd_size_type amt;
2879 /* Compute the hash values for all exported symbols. At the same
2880 time store the values in an array so that we could use them for
2881 optimizations. */
2882 amt = dynsymcount;
2883 amt *= sizeof (unsigned long int);
2884 hashcodes = (unsigned long int *) bfd_malloc (amt);
2885 if (hashcodes == NULL)
2886 return 0;
2887 hashcodesp = hashcodes;
2889 /* Put all hash values in HASHCODES. */
2890 elf_link_hash_traverse (elf_hash_table (info),
2891 elf_collect_hash_codes, &hashcodesp);
2893 /* We have a problem here. The following code to optimize the table
2894 size requires an integer type with more the 32 bits. If
2895 BFD_HOST_U_64_BIT is set we know about such a type. */
2896 #ifdef BFD_HOST_U_64_BIT
2897 if (info->optimize == true)
2899 unsigned long int nsyms = hashcodesp - hashcodes;
2900 size_t minsize;
2901 size_t maxsize;
2902 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2903 unsigned long int *counts ;
2905 /* Possible optimization parameters: if we have NSYMS symbols we say
2906 that the hashing table must at least have NSYMS/4 and at most
2907 2*NSYMS buckets. */
2908 minsize = nsyms / 4;
2909 if (minsize == 0)
2910 minsize = 1;
2911 best_size = maxsize = nsyms * 2;
2913 /* Create array where we count the collisions in. We must use bfd_malloc
2914 since the size could be large. */
2915 amt = maxsize;
2916 amt *= sizeof (unsigned long int);
2917 counts = (unsigned long int *) bfd_malloc (amt);
2918 if (counts == NULL)
2920 free (hashcodes);
2921 return 0;
2924 /* Compute the "optimal" size for the hash table. The criteria is a
2925 minimal chain length. The minor criteria is (of course) the size
2926 of the table. */
2927 for (i = minsize; i < maxsize; ++i)
2929 /* Walk through the array of hashcodes and count the collisions. */
2930 BFD_HOST_U_64_BIT max;
2931 unsigned long int j;
2932 unsigned long int fact;
2934 memset (counts, '\0', i * sizeof (unsigned long int));
2936 /* Determine how often each hash bucket is used. */
2937 for (j = 0; j < nsyms; ++j)
2938 ++counts[hashcodes[j] % i];
2940 /* For the weight function we need some information about the
2941 pagesize on the target. This is information need not be 100%
2942 accurate. Since this information is not available (so far) we
2943 define it here to a reasonable default value. If it is crucial
2944 to have a better value some day simply define this value. */
2945 # ifndef BFD_TARGET_PAGESIZE
2946 # define BFD_TARGET_PAGESIZE (4096)
2947 # endif
2949 /* We in any case need 2 + NSYMS entries for the size values and
2950 the chains. */
2951 max = (2 + nsyms) * (ARCH_SIZE / 8);
2953 # if 1
2954 /* Variant 1: optimize for short chains. We add the squares
2955 of all the chain lengths (which favous many small chain
2956 over a few long chains). */
2957 for (j = 0; j < i; ++j)
2958 max += counts[j] * counts[j];
2960 /* This adds penalties for the overall size of the table. */
2961 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2962 max *= fact * fact;
2963 # else
2964 /* Variant 2: Optimize a lot more for small table. Here we
2965 also add squares of the size but we also add penalties for
2966 empty slots (the +1 term). */
2967 for (j = 0; j < i; ++j)
2968 max += (1 + counts[j]) * (1 + counts[j]);
2970 /* The overall size of the table is considered, but not as
2971 strong as in variant 1, where it is squared. */
2972 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2973 max *= fact;
2974 # endif
2976 /* Compare with current best results. */
2977 if (max < best_chlen)
2979 best_chlen = max;
2980 best_size = i;
2984 free (counts);
2986 else
2987 #endif /* defined (BFD_HOST_U_64_BIT) */
2989 /* This is the fallback solution if no 64bit type is available or if we
2990 are not supposed to spend much time on optimizations. We select the
2991 bucket count using a fixed set of numbers. */
2992 for (i = 0; elf_buckets[i] != 0; i++)
2994 best_size = elf_buckets[i];
2995 if (dynsymcount < elf_buckets[i + 1])
2996 break;
3000 /* Free the arrays we needed. */
3001 free (hashcodes);
3003 return best_size;
3006 /* Set up the sizes and contents of the ELF dynamic sections. This is
3007 called by the ELF linker emulation before_allocation routine. We
3008 must set the sizes of the sections before the linker sets the
3009 addresses of the various sections. */
3011 boolean
3012 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3013 filter_shlib,
3014 auxiliary_filters, info, sinterpptr,
3015 verdefs)
3016 bfd *output_bfd;
3017 const char *soname;
3018 const char *rpath;
3019 const char *filter_shlib;
3020 const char * const *auxiliary_filters;
3021 struct bfd_link_info *info;
3022 asection **sinterpptr;
3023 struct bfd_elf_version_tree *verdefs;
3025 bfd_size_type soname_indx;
3026 bfd *dynobj;
3027 struct elf_backend_data *bed;
3028 struct elf_assign_sym_version_info asvinfo;
3030 *sinterpptr = NULL;
3032 soname_indx = (bfd_size_type) -1;
3034 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3035 return true;
3037 if (! is_elf_hash_table (info))
3038 return false;
3040 /* Any syms created from now on start with -1 in
3041 got.refcount/offset and plt.refcount/offset. */
3042 elf_hash_table (info)->init_refcount = -1;
3044 /* The backend may have to create some sections regardless of whether
3045 we're dynamic or not. */
3046 bed = get_elf_backend_data (output_bfd);
3047 if (bed->elf_backend_always_size_sections
3048 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3049 return false;
3051 dynobj = elf_hash_table (info)->dynobj;
3053 /* If there were no dynamic objects in the link, there is nothing to
3054 do here. */
3055 if (dynobj == NULL)
3056 return true;
3058 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3059 return false;
3061 if (elf_hash_table (info)->dynamic_sections_created)
3063 struct elf_info_failed eif;
3064 struct elf_link_hash_entry *h;
3065 asection *dynstr;
3067 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3068 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3070 if (soname != NULL)
3072 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3073 soname, true);
3074 if (soname_indx == (bfd_size_type) -1
3075 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3076 soname_indx))
3077 return false;
3080 if (info->symbolic)
3082 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3083 (bfd_vma) 0))
3084 return false;
3085 info->flags |= DF_SYMBOLIC;
3088 if (rpath != NULL)
3090 bfd_size_type indx;
3092 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3093 true);
3094 if (info->new_dtags)
3095 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3096 if (indx == (bfd_size_type) -1
3097 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3098 || (info->new_dtags
3099 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3100 indx)))
3101 return false;
3104 if (filter_shlib != NULL)
3106 bfd_size_type indx;
3108 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3109 filter_shlib, true);
3110 if (indx == (bfd_size_type) -1
3111 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3112 return false;
3115 if (auxiliary_filters != NULL)
3117 const char * const *p;
3119 for (p = auxiliary_filters; *p != NULL; p++)
3121 bfd_size_type indx;
3123 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3124 *p, true);
3125 if (indx == (bfd_size_type) -1
3126 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3127 indx))
3128 return false;
3132 eif.info = info;
3133 eif.verdefs = verdefs;
3134 eif.failed = false;
3136 /* If we are supposed to export all symbols into the dynamic symbol
3137 table (this is not the normal case), then do so. */
3138 if (info->export_dynamic)
3140 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3141 (PTR) &eif);
3142 if (eif.failed)
3143 return false;
3146 /* Attach all the symbols to their version information. */
3147 asvinfo.output_bfd = output_bfd;
3148 asvinfo.info = info;
3149 asvinfo.verdefs = verdefs;
3150 asvinfo.failed = false;
3152 elf_link_hash_traverse (elf_hash_table (info),
3153 elf_link_assign_sym_version,
3154 (PTR) &asvinfo);
3155 if (asvinfo.failed)
3156 return false;
3158 /* Find all symbols which were defined in a dynamic object and make
3159 the backend pick a reasonable value for them. */
3160 elf_link_hash_traverse (elf_hash_table (info),
3161 elf_adjust_dynamic_symbol,
3162 (PTR) &eif);
3163 if (eif.failed)
3164 return false;
3166 /* Add some entries to the .dynamic section. We fill in some of the
3167 values later, in elf_bfd_final_link, but we must add the entries
3168 now so that we know the final size of the .dynamic section. */
3170 /* If there are initialization and/or finalization functions to
3171 call then add the corresponding DT_INIT/DT_FINI entries. */
3172 h = (info->init_function
3173 ? elf_link_hash_lookup (elf_hash_table (info),
3174 info->init_function, false,
3175 false, false)
3176 : NULL);
3177 if (h != NULL
3178 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3179 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3181 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3182 return false;
3184 h = (info->fini_function
3185 ? elf_link_hash_lookup (elf_hash_table (info),
3186 info->fini_function, false,
3187 false, false)
3188 : NULL);
3189 if (h != NULL
3190 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3191 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3193 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3194 return false;
3197 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3199 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3200 if (info->shared)
3202 bfd *sub;
3203 asection *o;
3205 for (sub = info->input_bfds; sub != NULL;
3206 sub = sub->link_next)
3207 for (o = sub->sections; o != NULL; o = o->next)
3208 if (elf_section_data (o)->this_hdr.sh_type
3209 == SHT_PREINIT_ARRAY)
3211 (*_bfd_error_handler)
3212 (_("%s: .preinit_array section is not allowed in DSO"),
3213 bfd_archive_filename (sub));
3214 break;
3217 bfd_set_error (bfd_error_nonrepresentable_section);
3218 return false;
3221 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3222 (bfd_vma) 0)
3223 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3224 (bfd_vma) 0))
3225 return false;
3227 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3229 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3230 (bfd_vma) 0)
3231 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3232 (bfd_vma) 0))
3233 return false;
3235 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3237 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3238 (bfd_vma) 0)
3239 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3240 (bfd_vma) 0))
3241 return false;
3244 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3245 /* If .dynstr is excluded from the link, we don't want any of
3246 these tags. Strictly, we should be checking each section
3247 individually; This quick check covers for the case where
3248 someone does a /DISCARD/ : { *(*) }. */
3249 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3251 bfd_size_type strsize;
3253 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3254 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3255 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3256 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3257 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3258 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3259 (bfd_vma) sizeof (Elf_External_Sym)))
3260 return false;
3264 /* The backend must work out the sizes of all the other dynamic
3265 sections. */
3266 if (bed->elf_backend_size_dynamic_sections
3267 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3268 return false;
3270 if (elf_hash_table (info)->dynamic_sections_created)
3272 bfd_size_type dynsymcount;
3273 asection *s;
3274 size_t bucketcount = 0;
3275 size_t hash_entry_size;
3276 unsigned int dtagcount;
3278 /* Set up the version definition section. */
3279 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3280 BFD_ASSERT (s != NULL);
3282 /* We may have created additional version definitions if we are
3283 just linking a regular application. */
3284 verdefs = asvinfo.verdefs;
3286 /* Skip anonymous version tag. */
3287 if (verdefs != NULL && verdefs->vernum == 0)
3288 verdefs = verdefs->next;
3290 if (verdefs == NULL)
3291 _bfd_strip_section_from_output (info, s);
3292 else
3294 unsigned int cdefs;
3295 bfd_size_type size;
3296 struct bfd_elf_version_tree *t;
3297 bfd_byte *p;
3298 Elf_Internal_Verdef def;
3299 Elf_Internal_Verdaux defaux;
3301 cdefs = 0;
3302 size = 0;
3304 /* Make space for the base version. */
3305 size += sizeof (Elf_External_Verdef);
3306 size += sizeof (Elf_External_Verdaux);
3307 ++cdefs;
3309 for (t = verdefs; t != NULL; t = t->next)
3311 struct bfd_elf_version_deps *n;
3313 size += sizeof (Elf_External_Verdef);
3314 size += sizeof (Elf_External_Verdaux);
3315 ++cdefs;
3317 for (n = t->deps; n != NULL; n = n->next)
3318 size += sizeof (Elf_External_Verdaux);
3321 s->_raw_size = size;
3322 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3323 if (s->contents == NULL && s->_raw_size != 0)
3324 return false;
3326 /* Fill in the version definition section. */
3328 p = s->contents;
3330 def.vd_version = VER_DEF_CURRENT;
3331 def.vd_flags = VER_FLG_BASE;
3332 def.vd_ndx = 1;
3333 def.vd_cnt = 1;
3334 def.vd_aux = sizeof (Elf_External_Verdef);
3335 def.vd_next = (sizeof (Elf_External_Verdef)
3336 + sizeof (Elf_External_Verdaux));
3338 if (soname_indx != (bfd_size_type) -1)
3340 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3341 soname_indx);
3342 def.vd_hash = bfd_elf_hash (soname);
3343 defaux.vda_name = soname_indx;
3345 else
3347 const char *name;
3348 bfd_size_type indx;
3350 name = basename (output_bfd->filename);
3351 def.vd_hash = bfd_elf_hash (name);
3352 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3353 name, false);
3354 if (indx == (bfd_size_type) -1)
3355 return false;
3356 defaux.vda_name = indx;
3358 defaux.vda_next = 0;
3360 _bfd_elf_swap_verdef_out (output_bfd, &def,
3361 (Elf_External_Verdef *) p);
3362 p += sizeof (Elf_External_Verdef);
3363 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3364 (Elf_External_Verdaux *) p);
3365 p += sizeof (Elf_External_Verdaux);
3367 for (t = verdefs; t != NULL; t = t->next)
3369 unsigned int cdeps;
3370 struct bfd_elf_version_deps *n;
3371 struct elf_link_hash_entry *h;
3373 cdeps = 0;
3374 for (n = t->deps; n != NULL; n = n->next)
3375 ++cdeps;
3377 /* Add a symbol representing this version. */
3378 h = NULL;
3379 if (! (_bfd_generic_link_add_one_symbol
3380 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3381 (bfd_vma) 0, (const char *) NULL, false,
3382 get_elf_backend_data (dynobj)->collect,
3383 (struct bfd_link_hash_entry **) &h)))
3384 return false;
3385 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3386 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3387 h->type = STT_OBJECT;
3388 h->verinfo.vertree = t;
3390 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3391 return false;
3393 def.vd_version = VER_DEF_CURRENT;
3394 def.vd_flags = 0;
3395 if (t->globals == NULL && t->locals == NULL && ! t->used)
3396 def.vd_flags |= VER_FLG_WEAK;
3397 def.vd_ndx = t->vernum + 1;
3398 def.vd_cnt = cdeps + 1;
3399 def.vd_hash = bfd_elf_hash (t->name);
3400 def.vd_aux = sizeof (Elf_External_Verdef);
3401 if (t->next != NULL)
3402 def.vd_next = (sizeof (Elf_External_Verdef)
3403 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3404 else
3405 def.vd_next = 0;
3407 _bfd_elf_swap_verdef_out (output_bfd, &def,
3408 (Elf_External_Verdef *) p);
3409 p += sizeof (Elf_External_Verdef);
3411 defaux.vda_name = h->dynstr_index;
3412 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3413 h->dynstr_index);
3414 if (t->deps == NULL)
3415 defaux.vda_next = 0;
3416 else
3417 defaux.vda_next = sizeof (Elf_External_Verdaux);
3418 t->name_indx = defaux.vda_name;
3420 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3421 (Elf_External_Verdaux *) p);
3422 p += sizeof (Elf_External_Verdaux);
3424 for (n = t->deps; n != NULL; n = n->next)
3426 if (n->version_needed == NULL)
3428 /* This can happen if there was an error in the
3429 version script. */
3430 defaux.vda_name = 0;
3432 else
3434 defaux.vda_name = n->version_needed->name_indx;
3435 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3436 defaux.vda_name);
3438 if (n->next == NULL)
3439 defaux.vda_next = 0;
3440 else
3441 defaux.vda_next = sizeof (Elf_External_Verdaux);
3443 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3444 (Elf_External_Verdaux *) p);
3445 p += sizeof (Elf_External_Verdaux);
3449 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3450 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3451 (bfd_vma) cdefs))
3452 return false;
3454 elf_tdata (output_bfd)->cverdefs = cdefs;
3457 if (info->new_dtags && info->flags)
3459 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3460 return false;
3463 if (info->flags_1)
3465 if (! info->shared)
3466 info->flags_1 &= ~ (DF_1_INITFIRST
3467 | DF_1_NODELETE
3468 | DF_1_NOOPEN);
3469 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3470 info->flags_1))
3471 return false;
3474 /* Work out the size of the version reference section. */
3476 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3477 BFD_ASSERT (s != NULL);
3479 struct elf_find_verdep_info sinfo;
3481 sinfo.output_bfd = output_bfd;
3482 sinfo.info = info;
3483 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3484 if (sinfo.vers == 0)
3485 sinfo.vers = 1;
3486 sinfo.failed = false;
3488 elf_link_hash_traverse (elf_hash_table (info),
3489 elf_link_find_version_dependencies,
3490 (PTR) &sinfo);
3492 if (elf_tdata (output_bfd)->verref == NULL)
3493 _bfd_strip_section_from_output (info, s);
3494 else
3496 Elf_Internal_Verneed *t;
3497 unsigned int size;
3498 unsigned int crefs;
3499 bfd_byte *p;
3501 /* Build the version definition section. */
3502 size = 0;
3503 crefs = 0;
3504 for (t = elf_tdata (output_bfd)->verref;
3505 t != NULL;
3506 t = t->vn_nextref)
3508 Elf_Internal_Vernaux *a;
3510 size += sizeof (Elf_External_Verneed);
3511 ++crefs;
3512 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3513 size += sizeof (Elf_External_Vernaux);
3516 s->_raw_size = size;
3517 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3518 if (s->contents == NULL)
3519 return false;
3521 p = s->contents;
3522 for (t = elf_tdata (output_bfd)->verref;
3523 t != NULL;
3524 t = t->vn_nextref)
3526 unsigned int caux;
3527 Elf_Internal_Vernaux *a;
3528 bfd_size_type indx;
3530 caux = 0;
3531 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3532 ++caux;
3534 t->vn_version = VER_NEED_CURRENT;
3535 t->vn_cnt = caux;
3536 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3537 elf_dt_name (t->vn_bfd) != NULL
3538 ? elf_dt_name (t->vn_bfd)
3539 : basename (t->vn_bfd->filename),
3540 false);
3541 if (indx == (bfd_size_type) -1)
3542 return false;
3543 t->vn_file = indx;
3544 t->vn_aux = sizeof (Elf_External_Verneed);
3545 if (t->vn_nextref == NULL)
3546 t->vn_next = 0;
3547 else
3548 t->vn_next = (sizeof (Elf_External_Verneed)
3549 + caux * sizeof (Elf_External_Vernaux));
3551 _bfd_elf_swap_verneed_out (output_bfd, t,
3552 (Elf_External_Verneed *) p);
3553 p += sizeof (Elf_External_Verneed);
3555 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3557 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3558 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3559 a->vna_nodename, false);
3560 if (indx == (bfd_size_type) -1)
3561 return false;
3562 a->vna_name = indx;
3563 if (a->vna_nextptr == NULL)
3564 a->vna_next = 0;
3565 else
3566 a->vna_next = sizeof (Elf_External_Vernaux);
3568 _bfd_elf_swap_vernaux_out (output_bfd, a,
3569 (Elf_External_Vernaux *) p);
3570 p += sizeof (Elf_External_Vernaux);
3574 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3575 (bfd_vma) 0)
3576 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3577 (bfd_vma) crefs))
3578 return false;
3580 elf_tdata (output_bfd)->cverrefs = crefs;
3584 /* Assign dynsym indicies. In a shared library we generate a
3585 section symbol for each output section, which come first.
3586 Next come all of the back-end allocated local dynamic syms,
3587 followed by the rest of the global symbols. */
3589 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3591 /* Work out the size of the symbol version section. */
3592 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3593 BFD_ASSERT (s != NULL);
3594 if (dynsymcount == 0
3595 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3597 _bfd_strip_section_from_output (info, s);
3598 /* The DYNSYMCOUNT might have changed if we were going to
3599 output a dynamic symbol table entry for S. */
3600 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3602 else
3604 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3605 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3606 if (s->contents == NULL)
3607 return false;
3609 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3610 return false;
3613 /* Set the size of the .dynsym and .hash sections. We counted
3614 the number of dynamic symbols in elf_link_add_object_symbols.
3615 We will build the contents of .dynsym and .hash when we build
3616 the final symbol table, because until then we do not know the
3617 correct value to give the symbols. We built the .dynstr
3618 section as we went along in elf_link_add_object_symbols. */
3619 s = bfd_get_section_by_name (dynobj, ".dynsym");
3620 BFD_ASSERT (s != NULL);
3621 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3622 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3623 if (s->contents == NULL && s->_raw_size != 0)
3624 return false;
3626 if (dynsymcount != 0)
3628 Elf_Internal_Sym isym;
3630 /* The first entry in .dynsym is a dummy symbol. */
3631 isym.st_value = 0;
3632 isym.st_size = 0;
3633 isym.st_name = 0;
3634 isym.st_info = 0;
3635 isym.st_other = 0;
3636 isym.st_shndx = 0;
3637 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3640 /* Compute the size of the hashing table. As a side effect this
3641 computes the hash values for all the names we export. */
3642 bucketcount = compute_bucket_count (info);
3644 s = bfd_get_section_by_name (dynobj, ".hash");
3645 BFD_ASSERT (s != NULL);
3646 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3647 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3648 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3649 if (s->contents == NULL)
3650 return false;
3651 memset (s->contents, 0, (size_t) s->_raw_size);
3653 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3654 s->contents);
3655 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3656 s->contents + hash_entry_size);
3658 elf_hash_table (info)->bucketcount = bucketcount;
3660 s = bfd_get_section_by_name (dynobj, ".dynstr");
3661 BFD_ASSERT (s != NULL);
3663 elf_finalize_dynstr (output_bfd, info);
3665 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3667 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3668 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3669 return false;
3672 return true;
3675 /* This function is used to adjust offsets into .dynstr for
3676 dynamic symbols. This is called via elf_link_hash_traverse. */
3678 static boolean elf_adjust_dynstr_offsets
3679 PARAMS ((struct elf_link_hash_entry *, PTR));
3681 static boolean
3682 elf_adjust_dynstr_offsets (h, data)
3683 struct elf_link_hash_entry *h;
3684 PTR data;
3686 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3688 if (h->root.type == bfd_link_hash_warning)
3689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3691 if (h->dynindx != -1)
3692 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3693 return true;
3696 /* Assign string offsets in .dynstr, update all structures referencing
3697 them. */
3699 static boolean
3700 elf_finalize_dynstr (output_bfd, info)
3701 bfd *output_bfd;
3702 struct bfd_link_info *info;
3704 struct elf_link_local_dynamic_entry *entry;
3705 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3706 bfd *dynobj = elf_hash_table (info)->dynobj;
3707 asection *sdyn;
3708 bfd_size_type size;
3709 Elf_External_Dyn *dyncon, *dynconend;
3711 _bfd_elf_strtab_finalize (dynstr);
3712 size = _bfd_elf_strtab_size (dynstr);
3714 /* Update all .dynamic entries referencing .dynstr strings. */
3715 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3716 BFD_ASSERT (sdyn != NULL);
3718 dyncon = (Elf_External_Dyn *) sdyn->contents;
3719 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3720 sdyn->_raw_size);
3721 for (; dyncon < dynconend; dyncon++)
3723 Elf_Internal_Dyn dyn;
3725 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3726 switch (dyn.d_tag)
3728 case DT_STRSZ:
3729 dyn.d_un.d_val = size;
3730 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3731 break;
3732 case DT_NEEDED:
3733 case DT_SONAME:
3734 case DT_RPATH:
3735 case DT_RUNPATH:
3736 case DT_FILTER:
3737 case DT_AUXILIARY:
3738 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3739 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3740 break;
3741 default:
3742 break;
3746 /* Now update local dynamic symbols. */
3747 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3748 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3749 entry->isym.st_name);
3751 /* And the rest of dynamic symbols. */
3752 elf_link_hash_traverse (elf_hash_table (info),
3753 elf_adjust_dynstr_offsets, dynstr);
3755 /* Adjust version definitions. */
3756 if (elf_tdata (output_bfd)->cverdefs)
3758 asection *s;
3759 bfd_byte *p;
3760 bfd_size_type i;
3761 Elf_Internal_Verdef def;
3762 Elf_Internal_Verdaux defaux;
3764 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3765 p = (bfd_byte *) s->contents;
3768 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3769 &def);
3770 p += sizeof (Elf_External_Verdef);
3771 for (i = 0; i < def.vd_cnt; ++i)
3773 _bfd_elf_swap_verdaux_in (output_bfd,
3774 (Elf_External_Verdaux *) p, &defaux);
3775 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3776 defaux.vda_name);
3777 _bfd_elf_swap_verdaux_out (output_bfd,
3778 &defaux, (Elf_External_Verdaux *) p);
3779 p += sizeof (Elf_External_Verdaux);
3782 while (def.vd_next);
3785 /* Adjust version references. */
3786 if (elf_tdata (output_bfd)->verref)
3788 asection *s;
3789 bfd_byte *p;
3790 bfd_size_type i;
3791 Elf_Internal_Verneed need;
3792 Elf_Internal_Vernaux needaux;
3794 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3795 p = (bfd_byte *) s->contents;
3798 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3799 &need);
3800 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3801 _bfd_elf_swap_verneed_out (output_bfd, &need,
3802 (Elf_External_Verneed *) p);
3803 p += sizeof (Elf_External_Verneed);
3804 for (i = 0; i < need.vn_cnt; ++i)
3806 _bfd_elf_swap_vernaux_in (output_bfd,
3807 (Elf_External_Vernaux *) p, &needaux);
3808 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3809 needaux.vna_name);
3810 _bfd_elf_swap_vernaux_out (output_bfd,
3811 &needaux,
3812 (Elf_External_Vernaux *) p);
3813 p += sizeof (Elf_External_Vernaux);
3816 while (need.vn_next);
3819 return true;
3822 /* Fix up the flags for a symbol. This handles various cases which
3823 can only be fixed after all the input files are seen. This is
3824 currently called by both adjust_dynamic_symbol and
3825 assign_sym_version, which is unnecessary but perhaps more robust in
3826 the face of future changes. */
3828 static boolean
3829 elf_fix_symbol_flags (h, eif)
3830 struct elf_link_hash_entry *h;
3831 struct elf_info_failed *eif;
3833 /* If this symbol was mentioned in a non-ELF file, try to set
3834 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3835 permit a non-ELF file to correctly refer to a symbol defined in
3836 an ELF dynamic object. */
3837 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3839 while (h->root.type == bfd_link_hash_indirect)
3840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3842 if (h->root.type != bfd_link_hash_defined
3843 && h->root.type != bfd_link_hash_defweak)
3844 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3845 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3846 else
3848 if (h->root.u.def.section->owner != NULL
3849 && (bfd_get_flavour (h->root.u.def.section->owner)
3850 == bfd_target_elf_flavour))
3851 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3852 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3853 else
3854 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3857 if (h->dynindx == -1
3858 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3859 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3861 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3863 eif->failed = true;
3864 return false;
3868 else
3870 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3871 was first seen in a non-ELF file. Fortunately, if the symbol
3872 was first seen in an ELF file, we're probably OK unless the
3873 symbol was defined in a non-ELF file. Catch that case here.
3874 FIXME: We're still in trouble if the symbol was first seen in
3875 a dynamic object, and then later in a non-ELF regular object. */
3876 if ((h->root.type == bfd_link_hash_defined
3877 || h->root.type == bfd_link_hash_defweak)
3878 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3879 && (h->root.u.def.section->owner != NULL
3880 ? (bfd_get_flavour (h->root.u.def.section->owner)
3881 != bfd_target_elf_flavour)
3882 : (bfd_is_abs_section (h->root.u.def.section)
3883 && (h->elf_link_hash_flags
3884 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3885 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3888 /* If this is a final link, and the symbol was defined as a common
3889 symbol in a regular object file, and there was no definition in
3890 any dynamic object, then the linker will have allocated space for
3891 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3892 flag will not have been set. */
3893 if (h->root.type == bfd_link_hash_defined
3894 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3895 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3896 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3897 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3898 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3900 /* If -Bsymbolic was used (which means to bind references to global
3901 symbols to the definition within the shared object), and this
3902 symbol was defined in a regular object, then it actually doesn't
3903 need a PLT entry, and we can accomplish that by forcing it local.
3904 Likewise, if the symbol has hidden or internal visibility.
3905 FIXME: It might be that we also do not need a PLT for other
3906 non-hidden visibilities, but we would have to tell that to the
3907 backend specifically; we can't just clear PLT-related data here. */
3908 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3909 && eif->info->shared
3910 && is_elf_hash_table (eif->info)
3911 && (eif->info->symbolic
3912 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3913 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3914 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3916 struct elf_backend_data *bed;
3917 boolean force_local;
3919 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3921 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3922 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3923 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3926 /* If this is a weak defined symbol in a dynamic object, and we know
3927 the real definition in the dynamic object, copy interesting flags
3928 over to the real definition. */
3929 if (h->weakdef != NULL)
3931 struct elf_link_hash_entry *weakdef;
3933 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3934 || h->root.type == bfd_link_hash_defweak);
3935 weakdef = h->weakdef;
3936 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3937 || weakdef->root.type == bfd_link_hash_defweak);
3938 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3940 /* If the real definition is defined by a regular object file,
3941 don't do anything special. See the longer description in
3942 elf_adjust_dynamic_symbol, below. */
3943 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3944 h->weakdef = NULL;
3945 else
3947 struct elf_backend_data *bed;
3949 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3950 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3954 return true;
3957 /* Make the backend pick a good value for a dynamic symbol. This is
3958 called via elf_link_hash_traverse, and also calls itself
3959 recursively. */
3961 static boolean
3962 elf_adjust_dynamic_symbol (h, data)
3963 struct elf_link_hash_entry *h;
3964 PTR data;
3966 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3967 bfd *dynobj;
3968 struct elf_backend_data *bed;
3970 if (h->root.type == bfd_link_hash_warning)
3972 h->plt.offset = (bfd_vma) -1;
3973 h->got.offset = (bfd_vma) -1;
3975 /* When warning symbols are created, they **replace** the "real"
3976 entry in the hash table, thus we never get to see the real
3977 symbol in a hash traversal. So look at it now. */
3978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3981 /* Ignore indirect symbols. These are added by the versioning code. */
3982 if (h->root.type == bfd_link_hash_indirect)
3983 return true;
3985 if (! is_elf_hash_table (eif->info))
3986 return false;
3988 /* Fix the symbol flags. */
3989 if (! elf_fix_symbol_flags (h, eif))
3990 return false;
3992 /* If this symbol does not require a PLT entry, and it is not
3993 defined by a dynamic object, or is not referenced by a regular
3994 object, ignore it. We do have to handle a weak defined symbol,
3995 even if no regular object refers to it, if we decided to add it
3996 to the dynamic symbol table. FIXME: Do we normally need to worry
3997 about symbols which are defined by one dynamic object and
3998 referenced by another one? */
3999 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4000 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4001 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4002 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4003 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4005 h->plt.offset = (bfd_vma) -1;
4006 return true;
4009 /* If we've already adjusted this symbol, don't do it again. This
4010 can happen via a recursive call. */
4011 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4012 return true;
4014 /* Don't look at this symbol again. Note that we must set this
4015 after checking the above conditions, because we may look at a
4016 symbol once, decide not to do anything, and then get called
4017 recursively later after REF_REGULAR is set below. */
4018 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4020 /* If this is a weak definition, and we know a real definition, and
4021 the real symbol is not itself defined by a regular object file,
4022 then get a good value for the real definition. We handle the
4023 real symbol first, for the convenience of the backend routine.
4025 Note that there is a confusing case here. If the real definition
4026 is defined by a regular object file, we don't get the real symbol
4027 from the dynamic object, but we do get the weak symbol. If the
4028 processor backend uses a COPY reloc, then if some routine in the
4029 dynamic object changes the real symbol, we will not see that
4030 change in the corresponding weak symbol. This is the way other
4031 ELF linkers work as well, and seems to be a result of the shared
4032 library model.
4034 I will clarify this issue. Most SVR4 shared libraries define the
4035 variable _timezone and define timezone as a weak synonym. The
4036 tzset call changes _timezone. If you write
4037 extern int timezone;
4038 int _timezone = 5;
4039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4040 you might expect that, since timezone is a synonym for _timezone,
4041 the same number will print both times. However, if the processor
4042 backend uses a COPY reloc, then actually timezone will be copied
4043 into your process image, and, since you define _timezone
4044 yourself, _timezone will not. Thus timezone and _timezone will
4045 wind up at different memory locations. The tzset call will set
4046 _timezone, leaving timezone unchanged. */
4048 if (h->weakdef != NULL)
4050 /* If we get to this point, we know there is an implicit
4051 reference by a regular object file via the weak symbol H.
4052 FIXME: Is this really true? What if the traversal finds
4053 H->WEAKDEF before it finds H? */
4054 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4056 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4057 return false;
4060 /* If a symbol has no type and no size and does not require a PLT
4061 entry, then we are probably about to do the wrong thing here: we
4062 are probably going to create a COPY reloc for an empty object.
4063 This case can arise when a shared object is built with assembly
4064 code, and the assembly code fails to set the symbol type. */
4065 if (h->size == 0
4066 && h->type == STT_NOTYPE
4067 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4068 (*_bfd_error_handler)
4069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4070 h->root.root.string);
4072 dynobj = elf_hash_table (eif->info)->dynobj;
4073 bed = get_elf_backend_data (dynobj);
4074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4076 eif->failed = true;
4077 return false;
4080 return true;
4083 /* This routine is used to export all defined symbols into the dynamic
4084 symbol table. It is called via elf_link_hash_traverse. */
4086 static boolean
4087 elf_export_symbol (h, data)
4088 struct elf_link_hash_entry *h;
4089 PTR data;
4091 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4093 /* Ignore indirect symbols. These are added by the versioning code. */
4094 if (h->root.type == bfd_link_hash_indirect)
4095 return true;
4097 if (h->root.type == bfd_link_hash_warning)
4098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4100 if (h->dynindx == -1
4101 && (h->elf_link_hash_flags
4102 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4104 struct bfd_elf_version_tree *t;
4105 struct bfd_elf_version_expr *d;
4107 for (t = eif->verdefs; t != NULL; t = t->next)
4109 if (t->globals != NULL)
4111 for (d = t->globals; d != NULL; d = d->next)
4113 if ((*d->match) (d, h->root.root.string))
4114 goto doit;
4118 if (t->locals != NULL)
4120 for (d = t->locals ; d != NULL; d = d->next)
4122 if ((*d->match) (d, h->root.root.string))
4123 return true;
4128 if (!eif->verdefs)
4130 doit:
4131 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4133 eif->failed = true;
4134 return false;
4139 return true;
4142 /* Look through the symbols which are defined in other shared
4143 libraries and referenced here. Update the list of version
4144 dependencies. This will be put into the .gnu.version_r section.
4145 This function is called via elf_link_hash_traverse. */
4147 static boolean
4148 elf_link_find_version_dependencies (h, data)
4149 struct elf_link_hash_entry *h;
4150 PTR data;
4152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4153 Elf_Internal_Verneed *t;
4154 Elf_Internal_Vernaux *a;
4155 bfd_size_type amt;
4157 if (h->root.type == bfd_link_hash_warning)
4158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4160 /* We only care about symbols defined in shared objects with version
4161 information. */
4162 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4163 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4164 || h->dynindx == -1
4165 || h->verinfo.verdef == NULL)
4166 return true;
4168 /* See if we already know about this version. */
4169 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4171 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4172 continue;
4174 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4175 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4176 return true;
4178 break;
4181 /* This is a new version. Add it to tree we are building. */
4183 if (t == NULL)
4185 amt = sizeof *t;
4186 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4187 if (t == NULL)
4189 rinfo->failed = true;
4190 return false;
4193 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4194 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4195 elf_tdata (rinfo->output_bfd)->verref = t;
4198 amt = sizeof *a;
4199 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4201 /* Note that we are copying a string pointer here, and testing it
4202 above. If bfd_elf_string_from_elf_section is ever changed to
4203 discard the string data when low in memory, this will have to be
4204 fixed. */
4205 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4207 a->vna_flags = h->verinfo.verdef->vd_flags;
4208 a->vna_nextptr = t->vn_auxptr;
4210 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4211 ++rinfo->vers;
4213 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4215 t->vn_auxptr = a;
4217 return true;
4220 /* Figure out appropriate versions for all the symbols. We may not
4221 have the version number script until we have read all of the input
4222 files, so until that point we don't know which symbols should be
4223 local. This function is called via elf_link_hash_traverse. */
4225 static boolean
4226 elf_link_assign_sym_version (h, data)
4227 struct elf_link_hash_entry *h;
4228 PTR data;
4230 struct elf_assign_sym_version_info *sinfo;
4231 struct bfd_link_info *info;
4232 struct elf_backend_data *bed;
4233 struct elf_info_failed eif;
4234 char *p;
4235 bfd_size_type amt;
4237 sinfo = (struct elf_assign_sym_version_info *) data;
4238 info = sinfo->info;
4240 if (h->root.type == bfd_link_hash_warning)
4241 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4243 /* Fix the symbol flags. */
4244 eif.failed = false;
4245 eif.info = info;
4246 if (! elf_fix_symbol_flags (h, &eif))
4248 if (eif.failed)
4249 sinfo->failed = true;
4250 return false;
4253 /* We only need version numbers for symbols defined in regular
4254 objects. */
4255 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4256 return true;
4258 bed = get_elf_backend_data (sinfo->output_bfd);
4259 p = strchr (h->root.root.string, ELF_VER_CHR);
4260 if (p != NULL && h->verinfo.vertree == NULL)
4262 struct bfd_elf_version_tree *t;
4263 boolean hidden;
4265 hidden = true;
4267 /* There are two consecutive ELF_VER_CHR characters if this is
4268 not a hidden symbol. */
4269 ++p;
4270 if (*p == ELF_VER_CHR)
4272 hidden = false;
4273 ++p;
4276 /* If there is no version string, we can just return out. */
4277 if (*p == '\0')
4279 if (hidden)
4280 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4281 return true;
4284 /* Look for the version. If we find it, it is no longer weak. */
4285 for (t = sinfo->verdefs; t != NULL; t = t->next)
4287 if (strcmp (t->name, p) == 0)
4289 size_t len;
4290 char *alc;
4291 struct bfd_elf_version_expr *d;
4293 len = p - h->root.root.string;
4294 alc = bfd_malloc ((bfd_size_type) len);
4295 if (alc == NULL)
4296 return false;
4297 strncpy (alc, h->root.root.string, len - 1);
4298 alc[len - 1] = '\0';
4299 if (alc[len - 2] == ELF_VER_CHR)
4300 alc[len - 2] = '\0';
4302 h->verinfo.vertree = t;
4303 t->used = true;
4304 d = NULL;
4306 if (t->globals != NULL)
4308 for (d = t->globals; d != NULL; d = d->next)
4309 if ((*d->match) (d, alc))
4310 break;
4313 /* See if there is anything to force this symbol to
4314 local scope. */
4315 if (d == NULL && t->locals != NULL)
4317 for (d = t->locals; d != NULL; d = d->next)
4319 if ((*d->match) (d, alc))
4321 if (h->dynindx != -1
4322 && info->shared
4323 && ! info->export_dynamic)
4325 (*bed->elf_backend_hide_symbol) (info, h, true);
4328 break;
4333 free (alc);
4334 break;
4338 /* If we are building an application, we need to create a
4339 version node for this version. */
4340 if (t == NULL && ! info->shared)
4342 struct bfd_elf_version_tree **pp;
4343 int version_index;
4345 /* If we aren't going to export this symbol, we don't need
4346 to worry about it. */
4347 if (h->dynindx == -1)
4348 return true;
4350 amt = sizeof *t;
4351 t = ((struct bfd_elf_version_tree *)
4352 bfd_alloc (sinfo->output_bfd, amt));
4353 if (t == NULL)
4355 sinfo->failed = true;
4356 return false;
4359 t->next = NULL;
4360 t->name = p;
4361 t->globals = NULL;
4362 t->locals = NULL;
4363 t->deps = NULL;
4364 t->name_indx = (unsigned int) -1;
4365 t->used = true;
4367 version_index = 1;
4368 /* Don't count anonymous version tag. */
4369 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4370 version_index = 0;
4371 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4372 ++version_index;
4373 t->vernum = version_index;
4375 *pp = t;
4377 h->verinfo.vertree = t;
4379 else if (t == NULL)
4381 /* We could not find the version for a symbol when
4382 generating a shared archive. Return an error. */
4383 (*_bfd_error_handler)
4384 (_("%s: undefined versioned symbol name %s"),
4385 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4386 bfd_set_error (bfd_error_bad_value);
4387 sinfo->failed = true;
4388 return false;
4391 if (hidden)
4392 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4395 /* If we don't have a version for this symbol, see if we can find
4396 something. */
4397 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4399 struct bfd_elf_version_tree *t;
4400 struct bfd_elf_version_tree *deflt;
4401 struct bfd_elf_version_expr *d;
4403 /* See if can find what version this symbol is in. If the
4404 symbol is supposed to be local, then don't actually register
4405 it. */
4406 deflt = NULL;
4407 for (t = sinfo->verdefs; t != NULL; t = t->next)
4409 if (t->globals != NULL)
4411 for (d = t->globals; d != NULL; d = d->next)
4413 if ((*d->match) (d, h->root.root.string))
4415 h->verinfo.vertree = t;
4416 break;
4420 if (d != NULL)
4421 break;
4424 if (t->locals != NULL)
4426 for (d = t->locals; d != NULL; d = d->next)
4428 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4429 deflt = t;
4430 else if ((*d->match) (d, h->root.root.string))
4432 h->verinfo.vertree = t;
4433 if (h->dynindx != -1
4434 && info->shared
4435 && ! info->export_dynamic)
4437 (*bed->elf_backend_hide_symbol) (info, h, true);
4439 break;
4443 if (d != NULL)
4444 break;
4448 if (deflt != NULL && h->verinfo.vertree == NULL)
4450 h->verinfo.vertree = deflt;
4451 if (h->dynindx != -1
4452 && info->shared
4453 && ! info->export_dynamic)
4455 (*bed->elf_backend_hide_symbol) (info, h, true);
4460 return true;
4463 /* Final phase of ELF linker. */
4465 /* A structure we use to avoid passing large numbers of arguments. */
4467 struct elf_final_link_info
4469 /* General link information. */
4470 struct bfd_link_info *info;
4471 /* Output BFD. */
4472 bfd *output_bfd;
4473 /* Symbol string table. */
4474 struct bfd_strtab_hash *symstrtab;
4475 /* .dynsym section. */
4476 asection *dynsym_sec;
4477 /* .hash section. */
4478 asection *hash_sec;
4479 /* symbol version section (.gnu.version). */
4480 asection *symver_sec;
4481 /* Buffer large enough to hold contents of any section. */
4482 bfd_byte *contents;
4483 /* Buffer large enough to hold external relocs of any section. */
4484 PTR external_relocs;
4485 /* Buffer large enough to hold internal relocs of any section. */
4486 Elf_Internal_Rela *internal_relocs;
4487 /* Buffer large enough to hold external local symbols of any input
4488 BFD. */
4489 Elf_External_Sym *external_syms;
4490 /* And a buffer for symbol section indices. */
4491 Elf_External_Sym_Shndx *locsym_shndx;
4492 /* Buffer large enough to hold internal local symbols of any input
4493 BFD. */
4494 Elf_Internal_Sym *internal_syms;
4495 /* Array large enough to hold a symbol index for each local symbol
4496 of any input BFD. */
4497 long *indices;
4498 /* Array large enough to hold a section pointer for each local
4499 symbol of any input BFD. */
4500 asection **sections;
4501 /* Buffer to hold swapped out symbols. */
4502 Elf_External_Sym *symbuf;
4503 /* And one for symbol section indices. */
4504 Elf_External_Sym_Shndx *symshndxbuf;
4505 /* Number of swapped out symbols in buffer. */
4506 size_t symbuf_count;
4507 /* Number of symbols which fit in symbuf. */
4508 size_t symbuf_size;
4511 static boolean elf_link_output_sym
4512 PARAMS ((struct elf_final_link_info *, const char *,
4513 Elf_Internal_Sym *, asection *));
4514 static boolean elf_link_flush_output_syms
4515 PARAMS ((struct elf_final_link_info *));
4516 static boolean elf_link_output_extsym
4517 PARAMS ((struct elf_link_hash_entry *, PTR));
4518 static boolean elf_link_sec_merge_syms
4519 PARAMS ((struct elf_link_hash_entry *, PTR));
4520 static boolean elf_link_input_bfd
4521 PARAMS ((struct elf_final_link_info *, bfd *));
4522 static boolean elf_reloc_link_order
4523 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4524 struct bfd_link_order *));
4526 /* This struct is used to pass information to elf_link_output_extsym. */
4528 struct elf_outext_info
4530 boolean failed;
4531 boolean localsyms;
4532 struct elf_final_link_info *finfo;
4535 /* Compute the size of, and allocate space for, REL_HDR which is the
4536 section header for a section containing relocations for O. */
4538 static boolean
4539 elf_link_size_reloc_section (abfd, rel_hdr, o)
4540 bfd *abfd;
4541 Elf_Internal_Shdr *rel_hdr;
4542 asection *o;
4544 bfd_size_type reloc_count;
4545 bfd_size_type num_rel_hashes;
4547 /* Figure out how many relocations there will be. */
4548 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4549 reloc_count = elf_section_data (o)->rel_count;
4550 else
4551 reloc_count = elf_section_data (o)->rel_count2;
4553 num_rel_hashes = o->reloc_count;
4554 if (num_rel_hashes < reloc_count)
4555 num_rel_hashes = reloc_count;
4557 /* That allows us to calculate the size of the section. */
4558 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4560 /* The contents field must last into write_object_contents, so we
4561 allocate it with bfd_alloc rather than malloc. Also since we
4562 cannot be sure that the contents will actually be filled in,
4563 we zero the allocated space. */
4564 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4565 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4566 return false;
4568 /* We only allocate one set of hash entries, so we only do it the
4569 first time we are called. */
4570 if (elf_section_data (o)->rel_hashes == NULL
4571 && num_rel_hashes)
4573 struct elf_link_hash_entry **p;
4575 p = ((struct elf_link_hash_entry **)
4576 bfd_zmalloc (num_rel_hashes
4577 * sizeof (struct elf_link_hash_entry *)));
4578 if (p == NULL)
4579 return false;
4581 elf_section_data (o)->rel_hashes = p;
4584 return true;
4587 /* When performing a relocateable link, the input relocations are
4588 preserved. But, if they reference global symbols, the indices
4589 referenced must be updated. Update all the relocations in
4590 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4592 static void
4593 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4594 bfd *abfd;
4595 Elf_Internal_Shdr *rel_hdr;
4596 unsigned int count;
4597 struct elf_link_hash_entry **rel_hash;
4599 unsigned int i;
4600 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4601 Elf_Internal_Rel *irel;
4602 Elf_Internal_Rela *irela;
4603 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4605 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4606 if (irel == NULL)
4608 (*_bfd_error_handler) (_("Error: out of memory"));
4609 abort ();
4612 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4613 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4614 if (irela == NULL)
4616 (*_bfd_error_handler) (_("Error: out of memory"));
4617 abort ();
4620 for (i = 0; i < count; i++, rel_hash++)
4622 if (*rel_hash == NULL)
4623 continue;
4625 BFD_ASSERT ((*rel_hash)->indx >= 0);
4627 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4629 Elf_External_Rel *erel;
4630 unsigned int j;
4632 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4633 if (bed->s->swap_reloc_in)
4634 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4635 else
4636 elf_swap_reloc_in (abfd, erel, irel);
4638 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4639 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4640 ELF_R_TYPE (irel[j].r_info));
4642 if (bed->s->swap_reloc_out)
4643 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4644 else
4645 elf_swap_reloc_out (abfd, irel, erel);
4647 else
4649 Elf_External_Rela *erela;
4650 unsigned int j;
4652 BFD_ASSERT (rel_hdr->sh_entsize
4653 == sizeof (Elf_External_Rela));
4655 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4656 if (bed->s->swap_reloca_in)
4657 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4658 else
4659 elf_swap_reloca_in (abfd, erela, irela);
4661 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4662 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4663 ELF_R_TYPE (irela[j].r_info));
4665 if (bed->s->swap_reloca_out)
4666 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4667 else
4668 elf_swap_reloca_out (abfd, irela, erela);
4672 free (irel);
4673 free (irela);
4676 struct elf_link_sort_rela {
4677 bfd_vma offset;
4678 enum elf_reloc_type_class type;
4679 union {
4680 Elf_Internal_Rel rel;
4681 Elf_Internal_Rela rela;
4682 } u;
4685 static int
4686 elf_link_sort_cmp1 (A, B)
4687 const PTR A;
4688 const PTR B;
4690 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4691 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4692 int relativea, relativeb;
4694 relativea = a->type == reloc_class_relative;
4695 relativeb = b->type == reloc_class_relative;
4697 if (relativea < relativeb)
4698 return 1;
4699 if (relativea > relativeb)
4700 return -1;
4701 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4702 return -1;
4703 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4704 return 1;
4705 if (a->u.rel.r_offset < b->u.rel.r_offset)
4706 return -1;
4707 if (a->u.rel.r_offset > b->u.rel.r_offset)
4708 return 1;
4709 return 0;
4712 static int
4713 elf_link_sort_cmp2 (A, B)
4714 const PTR A;
4715 const PTR B;
4717 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4718 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4719 int copya, copyb;
4721 if (a->offset < b->offset)
4722 return -1;
4723 if (a->offset > b->offset)
4724 return 1;
4725 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4726 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4727 if (copya < copyb)
4728 return -1;
4729 if (copya > copyb)
4730 return 1;
4731 if (a->u.rel.r_offset < b->u.rel.r_offset)
4732 return -1;
4733 if (a->u.rel.r_offset > b->u.rel.r_offset)
4734 return 1;
4735 return 0;
4738 static size_t
4739 elf_link_sort_relocs (abfd, info, psec)
4740 bfd *abfd;
4741 struct bfd_link_info *info;
4742 asection **psec;
4744 bfd *dynobj = elf_hash_table (info)->dynobj;
4745 asection *reldyn, *o;
4746 boolean rel = false;
4747 bfd_size_type count, size;
4748 size_t i, j, ret;
4749 struct elf_link_sort_rela *rela;
4750 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4752 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4753 if (reldyn == NULL || reldyn->_raw_size == 0)
4755 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4756 if (reldyn == NULL || reldyn->_raw_size == 0)
4757 return 0;
4758 rel = true;
4759 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4761 else
4762 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4764 size = 0;
4765 for (o = dynobj->sections; o != NULL; o = o->next)
4766 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4767 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4768 && o->output_section == reldyn)
4769 size += o->_raw_size;
4771 if (size != reldyn->_raw_size)
4772 return 0;
4774 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4775 if (rela == NULL)
4777 (*info->callbacks->warning)
4778 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4779 (bfd_vma) 0);
4780 return 0;
4783 for (o = dynobj->sections; o != NULL; o = o->next)
4784 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4785 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4786 && o->output_section == reldyn)
4788 if (rel)
4790 Elf_External_Rel *erel, *erelend;
4791 struct elf_link_sort_rela *s;
4793 erel = (Elf_External_Rel *) o->contents;
4794 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4795 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4796 for (; erel < erelend; erel++, s++)
4798 if (bed->s->swap_reloc_in)
4799 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4800 else
4801 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4803 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4806 else
4808 Elf_External_Rela *erela, *erelaend;
4809 struct elf_link_sort_rela *s;
4811 erela = (Elf_External_Rela *) o->contents;
4812 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4813 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4814 for (; erela < erelaend; erela++, s++)
4816 if (bed->s->swap_reloca_in)
4817 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4818 &s->u.rela);
4819 else
4820 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4822 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4827 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4828 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4830 for (i = ret, j = ret; i < count; i++)
4832 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4833 j = i;
4834 rela[i].offset = rela[j].u.rel.r_offset;
4836 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4838 for (o = dynobj->sections; o != NULL; o = o->next)
4839 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4840 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4841 && o->output_section == reldyn)
4843 if (rel)
4845 Elf_External_Rel *erel, *erelend;
4846 struct elf_link_sort_rela *s;
4848 erel = (Elf_External_Rel *) o->contents;
4849 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4850 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4851 for (; erel < erelend; erel++, s++)
4853 if (bed->s->swap_reloc_out)
4854 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4855 (bfd_byte *) erel);
4856 else
4857 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4860 else
4862 Elf_External_Rela *erela, *erelaend;
4863 struct elf_link_sort_rela *s;
4865 erela = (Elf_External_Rela *) o->contents;
4866 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4867 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4868 for (; erela < erelaend; erela++, s++)
4870 if (bed->s->swap_reloca_out)
4871 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4872 (bfd_byte *) erela);
4873 else
4874 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4879 free (rela);
4880 *psec = reldyn;
4881 return ret;
4884 /* Do the final step of an ELF link. */
4886 boolean
4887 elf_bfd_final_link (abfd, info)
4888 bfd *abfd;
4889 struct bfd_link_info *info;
4891 boolean dynamic;
4892 boolean emit_relocs;
4893 bfd *dynobj;
4894 struct elf_final_link_info finfo;
4895 register asection *o;
4896 register struct bfd_link_order *p;
4897 register bfd *sub;
4898 bfd_size_type max_contents_size;
4899 bfd_size_type max_external_reloc_size;
4900 bfd_size_type max_internal_reloc_count;
4901 bfd_size_type max_sym_count;
4902 bfd_size_type max_sym_shndx_count;
4903 file_ptr off;
4904 Elf_Internal_Sym elfsym;
4905 unsigned int i;
4906 Elf_Internal_Shdr *symtab_hdr;
4907 Elf_Internal_Shdr *symstrtab_hdr;
4908 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4909 struct elf_outext_info eoinfo;
4910 boolean merged;
4911 size_t relativecount = 0;
4912 asection *reldyn = 0;
4913 bfd_size_type amt;
4915 if (! is_elf_hash_table (info))
4916 return false;
4918 if (info->shared)
4919 abfd->flags |= DYNAMIC;
4921 dynamic = elf_hash_table (info)->dynamic_sections_created;
4922 dynobj = elf_hash_table (info)->dynobj;
4924 emit_relocs = (info->relocateable
4925 || info->emitrelocations
4926 || bed->elf_backend_emit_relocs);
4928 finfo.info = info;
4929 finfo.output_bfd = abfd;
4930 finfo.symstrtab = elf_stringtab_init ();
4931 if (finfo.symstrtab == NULL)
4932 return false;
4934 if (! dynamic)
4936 finfo.dynsym_sec = NULL;
4937 finfo.hash_sec = NULL;
4938 finfo.symver_sec = NULL;
4940 else
4942 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4943 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4944 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4945 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4946 /* Note that it is OK if symver_sec is NULL. */
4949 finfo.contents = NULL;
4950 finfo.external_relocs = NULL;
4951 finfo.internal_relocs = NULL;
4952 finfo.external_syms = NULL;
4953 finfo.locsym_shndx = NULL;
4954 finfo.internal_syms = NULL;
4955 finfo.indices = NULL;
4956 finfo.sections = NULL;
4957 finfo.symbuf = NULL;
4958 finfo.symshndxbuf = NULL;
4959 finfo.symbuf_count = 0;
4961 /* Count up the number of relocations we will output for each output
4962 section, so that we know the sizes of the reloc sections. We
4963 also figure out some maximum sizes. */
4964 max_contents_size = 0;
4965 max_external_reloc_size = 0;
4966 max_internal_reloc_count = 0;
4967 max_sym_count = 0;
4968 max_sym_shndx_count = 0;
4969 merged = false;
4970 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4972 o->reloc_count = 0;
4974 for (p = o->link_order_head; p != NULL; p = p->next)
4976 if (p->type == bfd_section_reloc_link_order
4977 || p->type == bfd_symbol_reloc_link_order)
4978 ++o->reloc_count;
4979 else if (p->type == bfd_indirect_link_order)
4981 asection *sec;
4983 sec = p->u.indirect.section;
4985 /* Mark all sections which are to be included in the
4986 link. This will normally be every section. We need
4987 to do this so that we can identify any sections which
4988 the linker has decided to not include. */
4989 sec->linker_mark = true;
4991 if (sec->flags & SEC_MERGE)
4992 merged = true;
4994 if (info->relocateable || info->emitrelocations)
4995 o->reloc_count += sec->reloc_count;
4996 else if (bed->elf_backend_count_relocs)
4998 Elf_Internal_Rela * relocs;
5000 relocs = (NAME(_bfd_elf,link_read_relocs)
5001 (abfd, sec, (PTR) NULL,
5002 (Elf_Internal_Rela *) NULL, info->keep_memory));
5004 o->reloc_count
5005 += (*bed->elf_backend_count_relocs) (sec, relocs);
5007 if (!info->keep_memory)
5008 free (relocs);
5011 if (sec->_raw_size > max_contents_size)
5012 max_contents_size = sec->_raw_size;
5013 if (sec->_cooked_size > max_contents_size)
5014 max_contents_size = sec->_cooked_size;
5016 /* We are interested in just local symbols, not all
5017 symbols. */
5018 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5019 && (sec->owner->flags & DYNAMIC) == 0)
5021 size_t sym_count;
5023 if (elf_bad_symtab (sec->owner))
5024 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5025 / sizeof (Elf_External_Sym));
5026 else
5027 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5029 if (sym_count > max_sym_count)
5030 max_sym_count = sym_count;
5032 if (sym_count > max_sym_shndx_count
5033 && elf_symtab_shndx (sec->owner) != 0)
5034 max_sym_shndx_count = sym_count;
5036 if ((sec->flags & SEC_RELOC) != 0)
5038 size_t ext_size;
5040 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5041 if (ext_size > max_external_reloc_size)
5042 max_external_reloc_size = ext_size;
5043 if (sec->reloc_count > max_internal_reloc_count)
5044 max_internal_reloc_count = sec->reloc_count;
5050 if (o->reloc_count > 0)
5051 o->flags |= SEC_RELOC;
5052 else
5054 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5055 set it (this is probably a bug) and if it is set
5056 assign_section_numbers will create a reloc section. */
5057 o->flags &=~ SEC_RELOC;
5060 /* If the SEC_ALLOC flag is not set, force the section VMA to
5061 zero. This is done in elf_fake_sections as well, but forcing
5062 the VMA to 0 here will ensure that relocs against these
5063 sections are handled correctly. */
5064 if ((o->flags & SEC_ALLOC) == 0
5065 && ! o->user_set_vma)
5066 o->vma = 0;
5069 if (! info->relocateable && merged)
5070 elf_link_hash_traverse (elf_hash_table (info),
5071 elf_link_sec_merge_syms, (PTR) abfd);
5073 /* Figure out the file positions for everything but the symbol table
5074 and the relocs. We set symcount to force assign_section_numbers
5075 to create a symbol table. */
5076 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5077 BFD_ASSERT (! abfd->output_has_begun);
5078 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5079 goto error_return;
5081 /* Figure out how many relocations we will have in each section.
5082 Just using RELOC_COUNT isn't good enough since that doesn't
5083 maintain a separate value for REL vs. RELA relocations. */
5084 if (emit_relocs)
5085 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5086 for (o = sub->sections; o != NULL; o = o->next)
5088 asection *output_section;
5090 if (! o->linker_mark)
5092 /* This section was omitted from the link. */
5093 continue;
5096 output_section = o->output_section;
5098 if (output_section != NULL
5099 && (o->flags & SEC_RELOC) != 0)
5101 struct bfd_elf_section_data *esdi
5102 = elf_section_data (o);
5103 struct bfd_elf_section_data *esdo
5104 = elf_section_data (output_section);
5105 unsigned int *rel_count;
5106 unsigned int *rel_count2;
5107 bfd_size_type entsize;
5108 bfd_size_type entsize2;
5110 /* We must be careful to add the relocations from the
5111 input section to the right output count. */
5112 entsize = esdi->rel_hdr.sh_entsize;
5113 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5114 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5115 || entsize == sizeof (Elf_External_Rela))
5116 && entsize2 != entsize
5117 && (entsize2 == 0
5118 || entsize2 == sizeof (Elf_External_Rel)
5119 || entsize2 == sizeof (Elf_External_Rela)));
5120 if (entsize == esdo->rel_hdr.sh_entsize)
5122 rel_count = &esdo->rel_count;
5123 rel_count2 = &esdo->rel_count2;
5125 else
5127 rel_count = &esdo->rel_count2;
5128 rel_count2 = &esdo->rel_count;
5131 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5132 if (esdi->rel_hdr2)
5133 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5134 output_section->flags |= SEC_RELOC;
5138 /* That created the reloc sections. Set their sizes, and assign
5139 them file positions, and allocate some buffers. */
5140 for (o = abfd->sections; o != NULL; o = o->next)
5142 if ((o->flags & SEC_RELOC) != 0)
5144 if (!elf_link_size_reloc_section (abfd,
5145 &elf_section_data (o)->rel_hdr,
5147 goto error_return;
5149 if (elf_section_data (o)->rel_hdr2
5150 && !elf_link_size_reloc_section (abfd,
5151 elf_section_data (o)->rel_hdr2,
5153 goto error_return;
5156 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5157 to count upwards while actually outputting the relocations. */
5158 elf_section_data (o)->rel_count = 0;
5159 elf_section_data (o)->rel_count2 = 0;
5162 _bfd_elf_assign_file_positions_for_relocs (abfd);
5164 /* We have now assigned file positions for all the sections except
5165 .symtab and .strtab. We start the .symtab section at the current
5166 file position, and write directly to it. We build the .strtab
5167 section in memory. */
5168 bfd_get_symcount (abfd) = 0;
5169 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5170 /* sh_name is set in prep_headers. */
5171 symtab_hdr->sh_type = SHT_SYMTAB;
5172 symtab_hdr->sh_flags = 0;
5173 symtab_hdr->sh_addr = 0;
5174 symtab_hdr->sh_size = 0;
5175 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5176 /* sh_link is set in assign_section_numbers. */
5177 /* sh_info is set below. */
5178 /* sh_offset is set just below. */
5179 symtab_hdr->sh_addralign = bed->s->file_align;
5181 off = elf_tdata (abfd)->next_file_pos;
5182 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5184 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5185 incorrect. We do not yet know the size of the .symtab section.
5186 We correct next_file_pos below, after we do know the size. */
5188 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5189 continuously seeking to the right position in the file. */
5190 if (! info->keep_memory || max_sym_count < 20)
5191 finfo.symbuf_size = 20;
5192 else
5193 finfo.symbuf_size = max_sym_count;
5194 amt = finfo.symbuf_size;
5195 amt *= sizeof (Elf_External_Sym);
5196 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5197 if (finfo.symbuf == NULL)
5198 goto error_return;
5199 if (elf_numsections (abfd) > SHN_LORESERVE)
5201 amt = finfo.symbuf_size;
5202 amt *= sizeof (Elf_External_Sym_Shndx);
5203 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5204 if (finfo.symshndxbuf == NULL)
5205 goto error_return;
5208 /* Start writing out the symbol table. The first symbol is always a
5209 dummy symbol. */
5210 if (info->strip != strip_all
5211 || emit_relocs)
5213 elfsym.st_value = 0;
5214 elfsym.st_size = 0;
5215 elfsym.st_info = 0;
5216 elfsym.st_other = 0;
5217 elfsym.st_shndx = SHN_UNDEF;
5218 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5219 &elfsym, bfd_und_section_ptr))
5220 goto error_return;
5223 #if 0
5224 /* Some standard ELF linkers do this, but we don't because it causes
5225 bootstrap comparison failures. */
5226 /* Output a file symbol for the output file as the second symbol.
5227 We output this even if we are discarding local symbols, although
5228 I'm not sure if this is correct. */
5229 elfsym.st_value = 0;
5230 elfsym.st_size = 0;
5231 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5232 elfsym.st_other = 0;
5233 elfsym.st_shndx = SHN_ABS;
5234 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5235 &elfsym, bfd_abs_section_ptr))
5236 goto error_return;
5237 #endif
5239 /* Output a symbol for each section. We output these even if we are
5240 discarding local symbols, since they are used for relocs. These
5241 symbols have no names. We store the index of each one in the
5242 index field of the section, so that we can find it again when
5243 outputting relocs. */
5244 if (info->strip != strip_all
5245 || emit_relocs)
5247 elfsym.st_size = 0;
5248 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5249 elfsym.st_other = 0;
5250 for (i = 1; i < elf_numsections (abfd); i++)
5252 o = section_from_elf_index (abfd, i);
5253 if (o != NULL)
5254 o->target_index = bfd_get_symcount (abfd);
5255 elfsym.st_shndx = i;
5256 if (info->relocateable || o == NULL)
5257 elfsym.st_value = 0;
5258 else
5259 elfsym.st_value = o->vma;
5260 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5261 &elfsym, o))
5262 goto error_return;
5263 if (i == SHN_LORESERVE)
5264 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5268 /* Allocate some memory to hold information read in from the input
5269 files. */
5270 if (max_contents_size != 0)
5272 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5273 if (finfo.contents == NULL)
5274 goto error_return;
5277 if (max_external_reloc_size != 0)
5279 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5280 if (finfo.external_relocs == NULL)
5281 goto error_return;
5284 if (max_internal_reloc_count != 0)
5286 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5287 amt *= sizeof (Elf_Internal_Rela);
5288 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5289 if (finfo.internal_relocs == NULL)
5290 goto error_return;
5293 if (max_sym_count != 0)
5295 amt = max_sym_count * sizeof (Elf_External_Sym);
5296 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5297 if (finfo.external_syms == NULL)
5298 goto error_return;
5300 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5301 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5302 if (finfo.internal_syms == NULL)
5303 goto error_return;
5305 amt = max_sym_count * sizeof (long);
5306 finfo.indices = (long *) bfd_malloc (amt);
5307 if (finfo.indices == NULL)
5308 goto error_return;
5310 amt = max_sym_count * sizeof (asection *);
5311 finfo.sections = (asection **) bfd_malloc (amt);
5312 if (finfo.sections == NULL)
5313 goto error_return;
5316 if (max_sym_shndx_count != 0)
5318 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5319 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5320 if (finfo.locsym_shndx == NULL)
5321 goto error_return;
5324 /* Since ELF permits relocations to be against local symbols, we
5325 must have the local symbols available when we do the relocations.
5326 Since we would rather only read the local symbols once, and we
5327 would rather not keep them in memory, we handle all the
5328 relocations for a single input file at the same time.
5330 Unfortunately, there is no way to know the total number of local
5331 symbols until we have seen all of them, and the local symbol
5332 indices precede the global symbol indices. This means that when
5333 we are generating relocateable output, and we see a reloc against
5334 a global symbol, we can not know the symbol index until we have
5335 finished examining all the local symbols to see which ones we are
5336 going to output. To deal with this, we keep the relocations in
5337 memory, and don't output them until the end of the link. This is
5338 an unfortunate waste of memory, but I don't see a good way around
5339 it. Fortunately, it only happens when performing a relocateable
5340 link, which is not the common case. FIXME: If keep_memory is set
5341 we could write the relocs out and then read them again; I don't
5342 know how bad the memory loss will be. */
5344 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5345 sub->output_has_begun = false;
5346 for (o = abfd->sections; o != NULL; o = o->next)
5348 for (p = o->link_order_head; p != NULL; p = p->next)
5350 Elf_Internal_Shdr *rhdr;
5352 if (p->type == bfd_indirect_link_order
5353 && (bfd_get_flavour (p->u.indirect.section->owner)
5354 == bfd_target_elf_flavour)
5355 && (((rhdr = &elf_section_data (p->u.indirect.section)->rel_hdr)
5356 ->sh_entsize == 0)
5357 || rhdr->sh_entsize == sizeof (Elf_External_Rel)
5358 || rhdr->sh_entsize == sizeof (Elf_External_Rela))
5359 && (((rhdr = elf_section_data (p->u.indirect.section)->rel_hdr2)
5360 == NULL)
5361 || rhdr->sh_entsize == sizeof (Elf_External_Rel)
5362 || rhdr->sh_entsize == sizeof (Elf_External_Rela)))
5364 sub = p->u.indirect.section->owner;
5365 if (! sub->output_has_begun)
5367 if (! elf_link_input_bfd (&finfo, sub))
5368 goto error_return;
5369 sub->output_has_begun = true;
5372 else if (p->type == bfd_section_reloc_link_order
5373 || p->type == bfd_symbol_reloc_link_order)
5375 if (! elf_reloc_link_order (abfd, info, o, p))
5376 goto error_return;
5378 else
5380 if (! _bfd_default_link_order (abfd, info, o, p))
5381 goto error_return;
5386 /* Output any global symbols that got converted to local in a
5387 version script or due to symbol visibility. We do this in a
5388 separate step since ELF requires all local symbols to appear
5389 prior to any global symbols. FIXME: We should only do this if
5390 some global symbols were, in fact, converted to become local.
5391 FIXME: Will this work correctly with the Irix 5 linker? */
5392 eoinfo.failed = false;
5393 eoinfo.finfo = &finfo;
5394 eoinfo.localsyms = true;
5395 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5396 (PTR) &eoinfo);
5397 if (eoinfo.failed)
5398 return false;
5400 /* That wrote out all the local symbols. Finish up the symbol table
5401 with the global symbols. Even if we want to strip everything we
5402 can, we still need to deal with those global symbols that got
5403 converted to local in a version script. */
5405 /* The sh_info field records the index of the first non local symbol. */
5406 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5408 if (dynamic
5409 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5411 Elf_Internal_Sym sym;
5412 Elf_External_Sym *dynsym =
5413 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5414 long last_local = 0;
5416 /* Write out the section symbols for the output sections. */
5417 if (info->shared)
5419 asection *s;
5421 sym.st_size = 0;
5422 sym.st_name = 0;
5423 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5424 sym.st_other = 0;
5426 for (s = abfd->sections; s != NULL; s = s->next)
5428 int indx;
5429 Elf_External_Sym *dest;
5431 indx = elf_section_data (s)->this_idx;
5432 BFD_ASSERT (indx > 0);
5433 sym.st_shndx = indx;
5434 sym.st_value = s->vma;
5435 dest = dynsym + elf_section_data (s)->dynindx;
5436 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5439 last_local = bfd_count_sections (abfd);
5442 /* Write out the local dynsyms. */
5443 if (elf_hash_table (info)->dynlocal)
5445 struct elf_link_local_dynamic_entry *e;
5446 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5448 asection *s;
5449 Elf_External_Sym *dest;
5451 sym.st_size = e->isym.st_size;
5452 sym.st_other = e->isym.st_other;
5454 /* Copy the internal symbol as is.
5455 Note that we saved a word of storage and overwrote
5456 the original st_name with the dynstr_index. */
5457 sym = e->isym;
5459 if (e->isym.st_shndx != SHN_UNDEF
5460 && (e->isym.st_shndx < SHN_LORESERVE
5461 || e->isym.st_shndx > SHN_HIRESERVE))
5463 s = bfd_section_from_elf_index (e->input_bfd,
5464 e->isym.st_shndx);
5466 sym.st_shndx =
5467 elf_section_data (s->output_section)->this_idx;
5468 sym.st_value = (s->output_section->vma
5469 + s->output_offset
5470 + e->isym.st_value);
5473 if (last_local < e->dynindx)
5474 last_local = e->dynindx;
5476 dest = dynsym + e->dynindx;
5477 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5481 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5482 last_local + 1;
5485 /* We get the global symbols from the hash table. */
5486 eoinfo.failed = false;
5487 eoinfo.localsyms = false;
5488 eoinfo.finfo = &finfo;
5489 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5490 (PTR) &eoinfo);
5491 if (eoinfo.failed)
5492 return false;
5494 /* If backend needs to output some symbols not present in the hash
5495 table, do it now. */
5496 if (bed->elf_backend_output_arch_syms)
5498 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5499 Elf_Internal_Sym *,
5500 asection *));
5502 if (! ((*bed->elf_backend_output_arch_syms)
5503 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5504 return false;
5507 /* Flush all symbols to the file. */
5508 if (! elf_link_flush_output_syms (&finfo))
5509 return false;
5511 /* Now we know the size of the symtab section. */
5512 off += symtab_hdr->sh_size;
5514 /* Finish up and write out the symbol string table (.strtab)
5515 section. */
5516 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5517 /* sh_name was set in prep_headers. */
5518 symstrtab_hdr->sh_type = SHT_STRTAB;
5519 symstrtab_hdr->sh_flags = 0;
5520 symstrtab_hdr->sh_addr = 0;
5521 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5522 symstrtab_hdr->sh_entsize = 0;
5523 symstrtab_hdr->sh_link = 0;
5524 symstrtab_hdr->sh_info = 0;
5525 /* sh_offset is set just below. */
5526 symstrtab_hdr->sh_addralign = 1;
5528 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5529 elf_tdata (abfd)->next_file_pos = off;
5531 if (bfd_get_symcount (abfd) > 0)
5533 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5534 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5535 return false;
5538 /* Adjust the relocs to have the correct symbol indices. */
5539 for (o = abfd->sections; o != NULL; o = o->next)
5541 if ((o->flags & SEC_RELOC) == 0)
5542 continue;
5544 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5545 elf_section_data (o)->rel_count,
5546 elf_section_data (o)->rel_hashes);
5547 if (elf_section_data (o)->rel_hdr2 != NULL)
5548 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5549 elf_section_data (o)->rel_count2,
5550 (elf_section_data (o)->rel_hashes
5551 + elf_section_data (o)->rel_count));
5553 /* Set the reloc_count field to 0 to prevent write_relocs from
5554 trying to swap the relocs out itself. */
5555 o->reloc_count = 0;
5558 if (dynamic && info->combreloc && dynobj != NULL)
5559 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5561 /* If we are linking against a dynamic object, or generating a
5562 shared library, finish up the dynamic linking information. */
5563 if (dynamic)
5565 Elf_External_Dyn *dyncon, *dynconend;
5567 /* Fix up .dynamic entries. */
5568 o = bfd_get_section_by_name (dynobj, ".dynamic");
5569 BFD_ASSERT (o != NULL);
5571 dyncon = (Elf_External_Dyn *) o->contents;
5572 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5573 for (; dyncon < dynconend; dyncon++)
5575 Elf_Internal_Dyn dyn;
5576 const char *name;
5577 unsigned int type;
5579 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5581 switch (dyn.d_tag)
5583 default:
5584 break;
5585 case DT_NULL:
5586 if (relativecount > 0 && dyncon + 1 < dynconend)
5588 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5590 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5591 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5592 default: break;
5594 if (dyn.d_tag != DT_NULL)
5596 dyn.d_un.d_val = relativecount;
5597 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5598 relativecount = 0;
5601 break;
5602 case DT_INIT:
5603 name = info->init_function;
5604 goto get_sym;
5605 case DT_FINI:
5606 name = info->fini_function;
5607 get_sym:
5609 struct elf_link_hash_entry *h;
5611 h = elf_link_hash_lookup (elf_hash_table (info), name,
5612 false, false, true);
5613 if (h != NULL
5614 && (h->root.type == bfd_link_hash_defined
5615 || h->root.type == bfd_link_hash_defweak))
5617 dyn.d_un.d_val = h->root.u.def.value;
5618 o = h->root.u.def.section;
5619 if (o->output_section != NULL)
5620 dyn.d_un.d_val += (o->output_section->vma
5621 + o->output_offset);
5622 else
5624 /* The symbol is imported from another shared
5625 library and does not apply to this one. */
5626 dyn.d_un.d_val = 0;
5629 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5632 break;
5634 case DT_PREINIT_ARRAYSZ:
5635 name = ".preinit_array";
5636 goto get_size;
5637 case DT_INIT_ARRAYSZ:
5638 name = ".init_array";
5639 goto get_size;
5640 case DT_FINI_ARRAYSZ:
5641 name = ".fini_array";
5642 get_size:
5643 o = bfd_get_section_by_name (abfd, name);
5644 BFD_ASSERT (o != NULL);
5645 if (o->_raw_size == 0)
5646 (*_bfd_error_handler)
5647 (_("warning: %s section has zero size"), name);
5648 dyn.d_un.d_val = o->_raw_size;
5649 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5650 break;
5652 case DT_PREINIT_ARRAY:
5653 name = ".preinit_array";
5654 goto get_vma;
5655 case DT_INIT_ARRAY:
5656 name = ".init_array";
5657 goto get_vma;
5658 case DT_FINI_ARRAY:
5659 name = ".fini_array";
5660 goto get_vma;
5662 case DT_HASH:
5663 name = ".hash";
5664 goto get_vma;
5665 case DT_STRTAB:
5666 name = ".dynstr";
5667 goto get_vma;
5668 case DT_SYMTAB:
5669 name = ".dynsym";
5670 goto get_vma;
5671 case DT_VERDEF:
5672 name = ".gnu.version_d";
5673 goto get_vma;
5674 case DT_VERNEED:
5675 name = ".gnu.version_r";
5676 goto get_vma;
5677 case DT_VERSYM:
5678 name = ".gnu.version";
5679 get_vma:
5680 o = bfd_get_section_by_name (abfd, name);
5681 BFD_ASSERT (o != NULL);
5682 dyn.d_un.d_ptr = o->vma;
5683 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5684 break;
5686 case DT_REL:
5687 case DT_RELA:
5688 case DT_RELSZ:
5689 case DT_RELASZ:
5690 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5691 type = SHT_REL;
5692 else
5693 type = SHT_RELA;
5694 dyn.d_un.d_val = 0;
5695 for (i = 1; i < elf_numsections (abfd); i++)
5697 Elf_Internal_Shdr *hdr;
5699 hdr = elf_elfsections (abfd)[i];
5700 if (hdr->sh_type == type
5701 && (hdr->sh_flags & SHF_ALLOC) != 0)
5703 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5704 dyn.d_un.d_val += hdr->sh_size;
5705 else
5707 if (dyn.d_un.d_val == 0
5708 || hdr->sh_addr < dyn.d_un.d_val)
5709 dyn.d_un.d_val = hdr->sh_addr;
5713 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5714 break;
5719 /* If we have created any dynamic sections, then output them. */
5720 if (dynobj != NULL)
5722 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5723 goto error_return;
5725 for (o = dynobj->sections; o != NULL; o = o->next)
5727 if ((o->flags & SEC_HAS_CONTENTS) == 0
5728 || o->_raw_size == 0
5729 || o->output_section == bfd_abs_section_ptr)
5730 continue;
5731 if ((o->flags & SEC_LINKER_CREATED) == 0)
5733 /* At this point, we are only interested in sections
5734 created by elf_link_create_dynamic_sections. */
5735 continue;
5737 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5738 != SHT_STRTAB)
5739 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5741 if (! bfd_set_section_contents (abfd, o->output_section,
5742 o->contents,
5743 (file_ptr) o->output_offset,
5744 o->_raw_size))
5745 goto error_return;
5747 else
5749 /* The contents of the .dynstr section are actually in a
5750 stringtab. */
5751 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5752 if (bfd_seek (abfd, off, SEEK_SET) != 0
5753 || ! _bfd_elf_strtab_emit (abfd,
5754 elf_hash_table (info)->dynstr))
5755 goto error_return;
5760 /* If we have optimized stabs strings, output them. */
5761 if (elf_hash_table (info)->stab_info != NULL)
5763 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5764 goto error_return;
5767 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5769 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5770 ".eh_frame_hdr");
5771 if (o
5772 && (elf_section_data (o)->sec_info_type
5773 == ELF_INFO_TYPE_EH_FRAME_HDR))
5775 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5776 goto error_return;
5780 if (finfo.symstrtab != NULL)
5781 _bfd_stringtab_free (finfo.symstrtab);
5782 if (finfo.contents != NULL)
5783 free (finfo.contents);
5784 if (finfo.external_relocs != NULL)
5785 free (finfo.external_relocs);
5786 if (finfo.internal_relocs != NULL)
5787 free (finfo.internal_relocs);
5788 if (finfo.external_syms != NULL)
5789 free (finfo.external_syms);
5790 if (finfo.locsym_shndx != NULL)
5791 free (finfo.locsym_shndx);
5792 if (finfo.internal_syms != NULL)
5793 free (finfo.internal_syms);
5794 if (finfo.indices != NULL)
5795 free (finfo.indices);
5796 if (finfo.sections != NULL)
5797 free (finfo.sections);
5798 if (finfo.symbuf != NULL)
5799 free (finfo.symbuf);
5800 if (finfo.symshndxbuf != NULL)
5801 free (finfo.symbuf);
5802 for (o = abfd->sections; o != NULL; o = o->next)
5804 if ((o->flags & SEC_RELOC) != 0
5805 && elf_section_data (o)->rel_hashes != NULL)
5806 free (elf_section_data (o)->rel_hashes);
5809 elf_tdata (abfd)->linker = true;
5811 return true;
5813 error_return:
5814 if (finfo.symstrtab != NULL)
5815 _bfd_stringtab_free (finfo.symstrtab);
5816 if (finfo.contents != NULL)
5817 free (finfo.contents);
5818 if (finfo.external_relocs != NULL)
5819 free (finfo.external_relocs);
5820 if (finfo.internal_relocs != NULL)
5821 free (finfo.internal_relocs);
5822 if (finfo.external_syms != NULL)
5823 free (finfo.external_syms);
5824 if (finfo.locsym_shndx != NULL)
5825 free (finfo.locsym_shndx);
5826 if (finfo.internal_syms != NULL)
5827 free (finfo.internal_syms);
5828 if (finfo.indices != NULL)
5829 free (finfo.indices);
5830 if (finfo.sections != NULL)
5831 free (finfo.sections);
5832 if (finfo.symbuf != NULL)
5833 free (finfo.symbuf);
5834 if (finfo.symshndxbuf != NULL)
5835 free (finfo.symbuf);
5836 for (o = abfd->sections; o != NULL; o = o->next)
5838 if ((o->flags & SEC_RELOC) != 0
5839 && elf_section_data (o)->rel_hashes != NULL)
5840 free (elf_section_data (o)->rel_hashes);
5843 return false;
5846 /* Add a symbol to the output symbol table. */
5848 static boolean
5849 elf_link_output_sym (finfo, name, elfsym, input_sec)
5850 struct elf_final_link_info *finfo;
5851 const char *name;
5852 Elf_Internal_Sym *elfsym;
5853 asection *input_sec;
5855 Elf_External_Sym *dest;
5856 Elf_External_Sym_Shndx *destshndx;
5858 boolean (*output_symbol_hook) PARAMS ((bfd *,
5859 struct bfd_link_info *info,
5860 const char *,
5861 Elf_Internal_Sym *,
5862 asection *));
5864 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5865 elf_backend_link_output_symbol_hook;
5866 if (output_symbol_hook != NULL)
5868 if (! ((*output_symbol_hook)
5869 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5870 return false;
5873 if (name == (const char *) NULL || *name == '\0')
5874 elfsym->st_name = 0;
5875 else if (input_sec->flags & SEC_EXCLUDE)
5876 elfsym->st_name = 0;
5877 else
5879 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5880 name, true, false);
5881 if (elfsym->st_name == (unsigned long) -1)
5882 return false;
5885 if (finfo->symbuf_count >= finfo->symbuf_size)
5887 if (! elf_link_flush_output_syms (finfo))
5888 return false;
5891 dest = finfo->symbuf + finfo->symbuf_count;
5892 destshndx = finfo->symshndxbuf;
5893 if (destshndx != NULL)
5894 destshndx += finfo->symbuf_count;
5895 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5896 ++finfo->symbuf_count;
5898 ++ bfd_get_symcount (finfo->output_bfd);
5900 return true;
5903 /* Flush the output symbols to the file. */
5905 static boolean
5906 elf_link_flush_output_syms (finfo)
5907 struct elf_final_link_info *finfo;
5909 if (finfo->symbuf_count > 0)
5911 Elf_Internal_Shdr *hdr;
5912 file_ptr pos;
5913 bfd_size_type amt;
5915 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5916 pos = hdr->sh_offset + hdr->sh_size;
5917 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5918 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5919 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5920 return false;
5922 hdr->sh_size += amt;
5924 if (finfo->symshndxbuf != NULL)
5926 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5927 pos = hdr->sh_offset + hdr->sh_size;
5928 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5929 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5930 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5931 != amt))
5932 return false;
5934 hdr->sh_size += amt;
5937 finfo->symbuf_count = 0;
5940 return true;
5943 /* Adjust all external symbols pointing into SEC_MERGE sections
5944 to reflect the object merging within the sections. */
5946 static boolean
5947 elf_link_sec_merge_syms (h, data)
5948 struct elf_link_hash_entry *h;
5949 PTR data;
5951 asection *sec;
5953 if (h->root.type == bfd_link_hash_warning)
5954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5956 if ((h->root.type == bfd_link_hash_defined
5957 || h->root.type == bfd_link_hash_defweak)
5958 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5959 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
5961 bfd *output_bfd = (bfd *) data;
5963 h->root.u.def.value =
5964 _bfd_merged_section_offset (output_bfd,
5965 &h->root.u.def.section,
5966 elf_section_data (sec)->sec_info,
5967 h->root.u.def.value, (bfd_vma) 0);
5970 return true;
5973 /* Add an external symbol to the symbol table. This is called from
5974 the hash table traversal routine. When generating a shared object,
5975 we go through the symbol table twice. The first time we output
5976 anything that might have been forced to local scope in a version
5977 script. The second time we output the symbols that are still
5978 global symbols. */
5980 static boolean
5981 elf_link_output_extsym (h, data)
5982 struct elf_link_hash_entry *h;
5983 PTR data;
5985 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5986 struct elf_final_link_info *finfo = eoinfo->finfo;
5987 boolean strip;
5988 Elf_Internal_Sym sym;
5989 asection *input_sec;
5991 if (h->root.type == bfd_link_hash_warning)
5993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5994 if (h->root.type == bfd_link_hash_new)
5995 return true;
5998 /* Decide whether to output this symbol in this pass. */
5999 if (eoinfo->localsyms)
6001 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6002 return true;
6004 else
6006 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6007 return true;
6010 /* If we are not creating a shared library, and this symbol is
6011 referenced by a shared library but is not defined anywhere, then
6012 warn that it is undefined. If we do not do this, the runtime
6013 linker will complain that the symbol is undefined when the
6014 program is run. We don't have to worry about symbols that are
6015 referenced by regular files, because we will already have issued
6016 warnings for them. */
6017 if (! finfo->info->relocateable
6018 && ! finfo->info->allow_shlib_undefined
6019 && ! finfo->info->shared
6020 && h->root.type == bfd_link_hash_undefined
6021 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6022 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6024 if (! ((*finfo->info->callbacks->undefined_symbol)
6025 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6026 (asection *) NULL, (bfd_vma) 0, true)))
6028 eoinfo->failed = true;
6029 return false;
6033 /* We don't want to output symbols that have never been mentioned by
6034 a regular file, or that we have been told to strip. However, if
6035 h->indx is set to -2, the symbol is used by a reloc and we must
6036 output it. */
6037 if (h->indx == -2)
6038 strip = false;
6039 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6040 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6041 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6042 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6043 strip = true;
6044 else if (finfo->info->strip == strip_all
6045 || (finfo->info->strip == strip_some
6046 && bfd_hash_lookup (finfo->info->keep_hash,
6047 h->root.root.string,
6048 false, false) == NULL))
6049 strip = true;
6050 else
6051 strip = false;
6053 /* If we're stripping it, and it's not a dynamic symbol, there's
6054 nothing else to do unless it is a forced local symbol. */
6055 if (strip
6056 && h->dynindx == -1
6057 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6058 return true;
6060 sym.st_value = 0;
6061 sym.st_size = h->size;
6062 sym.st_other = h->other;
6063 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6064 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6065 else if (h->root.type == bfd_link_hash_undefweak
6066 || h->root.type == bfd_link_hash_defweak)
6067 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6068 else
6069 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6071 switch (h->root.type)
6073 default:
6074 case bfd_link_hash_new:
6075 case bfd_link_hash_warning:
6076 abort ();
6077 return false;
6079 case bfd_link_hash_undefined:
6080 case bfd_link_hash_undefweak:
6081 input_sec = bfd_und_section_ptr;
6082 sym.st_shndx = SHN_UNDEF;
6083 break;
6085 case bfd_link_hash_defined:
6086 case bfd_link_hash_defweak:
6088 input_sec = h->root.u.def.section;
6089 if (input_sec->output_section != NULL)
6091 sym.st_shndx =
6092 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6093 input_sec->output_section);
6094 if (sym.st_shndx == SHN_BAD)
6096 (*_bfd_error_handler)
6097 (_("%s: could not find output section %s for input section %s"),
6098 bfd_get_filename (finfo->output_bfd),
6099 input_sec->output_section->name,
6100 input_sec->name);
6101 eoinfo->failed = true;
6102 return false;
6105 /* ELF symbols in relocateable files are section relative,
6106 but in nonrelocateable files they are virtual
6107 addresses. */
6108 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6109 if (! finfo->info->relocateable)
6110 sym.st_value += input_sec->output_section->vma;
6112 else
6114 BFD_ASSERT (input_sec->owner == NULL
6115 || (input_sec->owner->flags & DYNAMIC) != 0);
6116 sym.st_shndx = SHN_UNDEF;
6117 input_sec = bfd_und_section_ptr;
6120 break;
6122 case bfd_link_hash_common:
6123 input_sec = h->root.u.c.p->section;
6124 sym.st_shndx = SHN_COMMON;
6125 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6126 break;
6128 case bfd_link_hash_indirect:
6129 /* These symbols are created by symbol versioning. They point
6130 to the decorated version of the name. For example, if the
6131 symbol foo@@GNU_1.2 is the default, which should be used when
6132 foo is used with no version, then we add an indirect symbol
6133 foo which points to foo@@GNU_1.2. We ignore these symbols,
6134 since the indirected symbol is already in the hash table. */
6135 return true;
6138 /* Give the processor backend a chance to tweak the symbol value,
6139 and also to finish up anything that needs to be done for this
6140 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6141 forced local syms when non-shared is due to a historical quirk. */
6142 if ((h->dynindx != -1
6143 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6144 && (finfo->info->shared
6145 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6146 && elf_hash_table (finfo->info)->dynamic_sections_created)
6148 struct elf_backend_data *bed;
6150 bed = get_elf_backend_data (finfo->output_bfd);
6151 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6152 (finfo->output_bfd, finfo->info, h, &sym)))
6154 eoinfo->failed = true;
6155 return false;
6159 /* If we are marking the symbol as undefined, and there are no
6160 non-weak references to this symbol from a regular object, then
6161 mark the symbol as weak undefined; if there are non-weak
6162 references, mark the symbol as strong. We can't do this earlier,
6163 because it might not be marked as undefined until the
6164 finish_dynamic_symbol routine gets through with it. */
6165 if (sym.st_shndx == SHN_UNDEF
6166 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6167 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6168 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6170 int bindtype;
6172 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6173 bindtype = STB_GLOBAL;
6174 else
6175 bindtype = STB_WEAK;
6176 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6179 /* If a symbol is not defined locally, we clear the visibility
6180 field. */
6181 if (! finfo->info->relocateable
6182 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6183 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6185 /* If this symbol should be put in the .dynsym section, then put it
6186 there now. We have already know the symbol index. We also fill
6187 in the entry in the .hash section. */
6188 if (h->dynindx != -1
6189 && elf_hash_table (finfo->info)->dynamic_sections_created)
6191 size_t bucketcount;
6192 size_t bucket;
6193 size_t hash_entry_size;
6194 bfd_byte *bucketpos;
6195 bfd_vma chain;
6196 Elf_External_Sym *esym;
6198 sym.st_name = h->dynstr_index;
6199 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6200 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6202 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6203 bucket = h->elf_hash_value % bucketcount;
6204 hash_entry_size
6205 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6206 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6207 + (bucket + 2) * hash_entry_size);
6208 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6209 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6210 bucketpos);
6211 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6212 ((bfd_byte *) finfo->hash_sec->contents
6213 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6215 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6217 Elf_Internal_Versym iversym;
6218 Elf_External_Versym *eversym;
6220 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6222 if (h->verinfo.verdef == NULL)
6223 iversym.vs_vers = 0;
6224 else
6225 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6227 else
6229 if (h->verinfo.vertree == NULL)
6230 iversym.vs_vers = 1;
6231 else
6232 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6235 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6236 iversym.vs_vers |= VERSYM_HIDDEN;
6238 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6239 eversym += h->dynindx;
6240 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6244 /* If we're stripping it, then it was just a dynamic symbol, and
6245 there's nothing else to do. */
6246 if (strip)
6247 return true;
6249 h->indx = bfd_get_symcount (finfo->output_bfd);
6251 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6253 eoinfo->failed = true;
6254 return false;
6257 return true;
6260 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6261 originated from the section given by INPUT_REL_HDR) to the
6262 OUTPUT_BFD. */
6264 static void
6265 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6266 internal_relocs)
6267 bfd *output_bfd;
6268 asection *input_section;
6269 Elf_Internal_Shdr *input_rel_hdr;
6270 Elf_Internal_Rela *internal_relocs;
6272 Elf_Internal_Rela *irela;
6273 Elf_Internal_Rela *irelaend;
6274 Elf_Internal_Shdr *output_rel_hdr;
6275 asection *output_section;
6276 unsigned int *rel_countp = NULL;
6277 struct elf_backend_data *bed;
6278 bfd_size_type amt;
6280 output_section = input_section->output_section;
6281 output_rel_hdr = NULL;
6283 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6284 == input_rel_hdr->sh_entsize)
6286 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6287 rel_countp = &elf_section_data (output_section)->rel_count;
6289 else if (elf_section_data (output_section)->rel_hdr2
6290 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6291 == input_rel_hdr->sh_entsize))
6293 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6294 rel_countp = &elf_section_data (output_section)->rel_count2;
6297 BFD_ASSERT (output_rel_hdr != NULL);
6299 bed = get_elf_backend_data (output_bfd);
6300 irela = internal_relocs;
6301 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6302 * bed->s->int_rels_per_ext_rel;
6304 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6306 Elf_External_Rel *erel;
6307 Elf_Internal_Rel *irel;
6309 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6310 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6311 if (irel == NULL)
6313 (*_bfd_error_handler) (_("Error: out of memory"));
6314 abort ();
6317 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6318 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6320 unsigned int i;
6322 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6324 irel[i].r_offset = irela[i].r_offset;
6325 irel[i].r_info = irela[i].r_info;
6326 BFD_ASSERT (irela[i].r_addend == 0);
6329 if (bed->s->swap_reloc_out)
6330 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6331 else
6332 elf_swap_reloc_out (output_bfd, irel, erel);
6335 free (irel);
6337 else
6339 Elf_External_Rela *erela;
6341 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6343 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6344 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6345 if (bed->s->swap_reloca_out)
6346 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6347 else
6348 elf_swap_reloca_out (output_bfd, irela, erela);
6351 /* Bump the counter, so that we know where to add the next set of
6352 relocations. */
6353 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6356 /* Link an input file into the linker output file. This function
6357 handles all the sections and relocations of the input file at once.
6358 This is so that we only have to read the local symbols once, and
6359 don't have to keep them in memory. */
6361 static boolean
6362 elf_link_input_bfd (finfo, input_bfd)
6363 struct elf_final_link_info *finfo;
6364 bfd *input_bfd;
6366 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6367 bfd *, asection *, bfd_byte *,
6368 Elf_Internal_Rela *,
6369 Elf_Internal_Sym *, asection **));
6370 bfd *output_bfd;
6371 Elf_Internal_Shdr *symtab_hdr;
6372 Elf_Internal_Shdr *shndx_hdr;
6373 size_t locsymcount;
6374 size_t extsymoff;
6375 Elf_External_Sym *external_syms;
6376 Elf_External_Sym *esym;
6377 Elf_External_Sym *esymend;
6378 Elf_External_Sym_Shndx *shndx_buf;
6379 Elf_External_Sym_Shndx *shndx;
6380 Elf_Internal_Sym *isym;
6381 long *pindex;
6382 asection **ppsection;
6383 asection *o;
6384 struct elf_backend_data *bed;
6385 boolean emit_relocs;
6386 struct elf_link_hash_entry **sym_hashes;
6388 output_bfd = finfo->output_bfd;
6389 bed = get_elf_backend_data (output_bfd);
6390 relocate_section = bed->elf_backend_relocate_section;
6392 /* If this is a dynamic object, we don't want to do anything here:
6393 we don't want the local symbols, and we don't want the section
6394 contents. */
6395 if ((input_bfd->flags & DYNAMIC) != 0)
6396 return true;
6398 emit_relocs = (finfo->info->relocateable
6399 || finfo->info->emitrelocations
6400 || bed->elf_backend_emit_relocs);
6402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6403 if (elf_bad_symtab (input_bfd))
6405 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6406 extsymoff = 0;
6408 else
6410 locsymcount = symtab_hdr->sh_info;
6411 extsymoff = symtab_hdr->sh_info;
6414 /* Read the local symbols. */
6415 if (symtab_hdr->contents != NULL)
6416 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6417 else if (locsymcount == 0)
6418 external_syms = NULL;
6419 else
6421 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6422 external_syms = finfo->external_syms;
6423 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6424 || bfd_bread (external_syms, amt, input_bfd) != amt)
6425 return false;
6428 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6429 shndx_buf = NULL;
6430 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6432 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6433 shndx_buf = finfo->locsym_shndx;
6434 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6435 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6436 return false;
6439 /* Swap in the local symbols and write out the ones which we know
6440 are going into the output file. */
6441 for (esym = external_syms, esymend = esym + locsymcount,
6442 isym = finfo->internal_syms, pindex = finfo->indices,
6443 ppsection = finfo->sections, shndx = shndx_buf;
6444 esym < esymend;
6445 esym++, isym++, pindex++, ppsection++,
6446 shndx = (shndx != NULL ? shndx + 1 : NULL))
6448 asection *isec;
6449 const char *name;
6450 Elf_Internal_Sym osym;
6452 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
6453 *pindex = -1;
6455 if (elf_bad_symtab (input_bfd))
6457 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6459 *ppsection = NULL;
6460 continue;
6464 if (isym->st_shndx == SHN_UNDEF)
6465 isec = bfd_und_section_ptr;
6466 else if (isym->st_shndx < SHN_LORESERVE
6467 || isym->st_shndx > SHN_HIRESERVE)
6469 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6470 if (isec
6471 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6472 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6473 isym->st_value =
6474 _bfd_merged_section_offset (output_bfd, &isec,
6475 elf_section_data (isec)->sec_info,
6476 isym->st_value, (bfd_vma) 0);
6478 else if (isym->st_shndx == SHN_ABS)
6479 isec = bfd_abs_section_ptr;
6480 else if (isym->st_shndx == SHN_COMMON)
6481 isec = bfd_com_section_ptr;
6482 else
6484 /* Who knows? */
6485 isec = NULL;
6488 *ppsection = isec;
6490 /* Don't output the first, undefined, symbol. */
6491 if (esym == external_syms)
6492 continue;
6494 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6496 /* We never output section symbols. Instead, we use the
6497 section symbol of the corresponding section in the output
6498 file. */
6499 continue;
6502 /* If we are stripping all symbols, we don't want to output this
6503 one. */
6504 if (finfo->info->strip == strip_all)
6505 continue;
6507 /* If we are discarding all local symbols, we don't want to
6508 output this one. If we are generating a relocateable output
6509 file, then some of the local symbols may be required by
6510 relocs; we output them below as we discover that they are
6511 needed. */
6512 if (finfo->info->discard == discard_all)
6513 continue;
6515 /* If this symbol is defined in a section which we are
6516 discarding, we don't need to keep it, but note that
6517 linker_mark is only reliable for sections that have contents.
6518 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6519 as well as linker_mark. */
6520 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6521 && isec != NULL
6522 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6523 || (! finfo->info->relocateable
6524 && (isec->flags & SEC_EXCLUDE) != 0)))
6525 continue;
6527 /* Get the name of the symbol. */
6528 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6529 isym->st_name);
6530 if (name == NULL)
6531 return false;
6533 /* See if we are discarding symbols with this name. */
6534 if ((finfo->info->strip == strip_some
6535 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6536 == NULL))
6537 || (((finfo->info->discard == discard_sec_merge
6538 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6539 || finfo->info->discard == discard_l)
6540 && bfd_is_local_label_name (input_bfd, name)))
6541 continue;
6543 /* If we get here, we are going to output this symbol. */
6545 osym = *isym;
6547 /* Adjust the section index for the output file. */
6548 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6549 isec->output_section);
6550 if (osym.st_shndx == SHN_BAD)
6551 return false;
6553 *pindex = bfd_get_symcount (output_bfd);
6555 /* ELF symbols in relocateable files are section relative, but
6556 in executable files they are virtual addresses. Note that
6557 this code assumes that all ELF sections have an associated
6558 BFD section with a reasonable value for output_offset; below
6559 we assume that they also have a reasonable value for
6560 output_section. Any special sections must be set up to meet
6561 these requirements. */
6562 osym.st_value += isec->output_offset;
6563 if (! finfo->info->relocateable)
6564 osym.st_value += isec->output_section->vma;
6566 if (! elf_link_output_sym (finfo, name, &osym, isec))
6567 return false;
6570 /* Relocate the contents of each section. */
6571 sym_hashes = elf_sym_hashes (input_bfd);
6572 for (o = input_bfd->sections; o != NULL; o = o->next)
6574 bfd_byte *contents;
6576 if (! o->linker_mark)
6578 /* This section was omitted from the link. */
6579 continue;
6582 if ((o->flags & SEC_HAS_CONTENTS) == 0
6583 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6584 continue;
6586 if ((o->flags & SEC_LINKER_CREATED) != 0)
6588 /* Section was created by elf_link_create_dynamic_sections
6589 or somesuch. */
6590 continue;
6593 /* Get the contents of the section. They have been cached by a
6594 relaxation routine. Note that o is a section in an input
6595 file, so the contents field will not have been set by any of
6596 the routines which work on output files. */
6597 if (elf_section_data (o)->this_hdr.contents != NULL)
6598 contents = elf_section_data (o)->this_hdr.contents;
6599 else
6601 contents = finfo->contents;
6602 if (! bfd_get_section_contents (input_bfd, o, contents,
6603 (file_ptr) 0, o->_raw_size))
6604 return false;
6607 if ((o->flags & SEC_RELOC) != 0)
6609 Elf_Internal_Rela *internal_relocs;
6611 /* Get the swapped relocs. */
6612 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6613 (input_bfd, o, finfo->external_relocs,
6614 finfo->internal_relocs, false));
6615 if (internal_relocs == NULL
6616 && o->reloc_count > 0)
6617 return false;
6619 /* Run through the relocs looking for any against symbols
6620 from discarded sections and section symbols from
6621 removed link-once sections. Complain about relocs
6622 against discarded sections. Zero relocs against removed
6623 link-once sections. We should really complain if
6624 anything in the final link tries to use it, but
6625 DWARF-based exception handling might have an entry in
6626 .eh_frame to describe a routine in the linkonce section,
6627 and it turns out to be hard to remove the .eh_frame
6628 entry too. FIXME. */
6629 if (!finfo->info->relocateable
6630 && !elf_section_ignore_discarded_relocs (o))
6632 Elf_Internal_Rela *rel, *relend;
6634 rel = internal_relocs;
6635 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6636 for ( ; rel < relend; rel++)
6638 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6640 if (r_symndx >= locsymcount
6641 || (elf_bad_symtab (input_bfd)
6642 && finfo->sections[r_symndx] == NULL))
6644 struct elf_link_hash_entry *h;
6646 h = sym_hashes[r_symndx - extsymoff];
6647 while (h->root.type == bfd_link_hash_indirect
6648 || h->root.type == bfd_link_hash_warning)
6649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6651 /* Complain if the definition comes from a
6652 discarded section. */
6653 if ((h->root.type == bfd_link_hash_defined
6654 || h->root.type == bfd_link_hash_defweak)
6655 && elf_discarded_section (h->root.u.def.section))
6657 #if BFD_VERSION_DATE < 20031005
6658 if ((o->flags & SEC_DEBUGGING) != 0)
6660 #if BFD_VERSION_DATE > 20021005
6661 (*finfo->info->callbacks->warning)
6662 (finfo->info,
6663 _("warning: relocation against removed section; zeroing"),
6664 NULL, input_bfd, o, rel->r_offset);
6665 #endif
6666 BFD_ASSERT (r_symndx != 0);
6667 memset (rel, 0, sizeof (*rel));
6669 else
6670 #endif
6672 if (! ((*finfo->info->callbacks->undefined_symbol)
6673 (finfo->info, h->root.root.string,
6674 input_bfd, o, rel->r_offset,
6675 true)))
6676 return false;
6680 else
6682 asection *sec = finfo->sections[r_symndx];
6684 if (sec != NULL && elf_discarded_section (sec))
6686 #if BFD_VERSION_DATE < 20031005
6687 if ((o->flags & SEC_DEBUGGING) != 0
6688 || (sec->flags & SEC_LINK_ONCE) != 0)
6690 #if BFD_VERSION_DATE > 20021005
6691 (*finfo->info->callbacks->warning)
6692 (finfo->info,
6693 _("warning: relocation against removed section"),
6694 NULL, input_bfd, o, rel->r_offset);
6695 #endif
6696 BFD_ASSERT (r_symndx != 0);
6697 rel->r_info
6698 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6699 rel->r_addend = 0;
6701 else
6702 #endif
6704 boolean ok;
6705 const char *msg
6706 = _("local symbols in discarded section %s");
6707 bfd_size_type amt
6708 = strlen (sec->name) + strlen (msg) - 1;
6709 char *buf = (char *) bfd_malloc (amt);
6711 if (buf != NULL)
6712 sprintf (buf, msg, sec->name);
6713 else
6714 buf = (char *) sec->name;
6715 ok = (*finfo->info->callbacks
6716 ->undefined_symbol) (finfo->info, buf,
6717 input_bfd, o,
6718 rel->r_offset,
6719 true);
6720 if (buf != sec->name)
6721 free (buf);
6722 if (!ok)
6723 return false;
6730 /* Relocate the section by invoking a back end routine.
6732 The back end routine is responsible for adjusting the
6733 section contents as necessary, and (if using Rela relocs
6734 and generating a relocateable output file) adjusting the
6735 reloc addend as necessary.
6737 The back end routine does not have to worry about setting
6738 the reloc address or the reloc symbol index.
6740 The back end routine is given a pointer to the swapped in
6741 internal symbols, and can access the hash table entries
6742 for the external symbols via elf_sym_hashes (input_bfd).
6744 When generating relocateable output, the back end routine
6745 must handle STB_LOCAL/STT_SECTION symbols specially. The
6746 output symbol is going to be a section symbol
6747 corresponding to the output section, which will require
6748 the addend to be adjusted. */
6750 if (! (*relocate_section) (output_bfd, finfo->info,
6751 input_bfd, o, contents,
6752 internal_relocs,
6753 finfo->internal_syms,
6754 finfo->sections))
6755 return false;
6757 if (emit_relocs)
6759 Elf_Internal_Rela *irela;
6760 Elf_Internal_Rela *irelaend;
6761 struct elf_link_hash_entry **rel_hash;
6762 Elf_Internal_Shdr *input_rel_hdr;
6763 unsigned int next_erel;
6764 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6765 Elf_Internal_Shdr *,
6766 Elf_Internal_Rela *));
6768 /* Adjust the reloc addresses and symbol indices. */
6770 irela = internal_relocs;
6771 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6772 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6773 + elf_section_data (o->output_section)->rel_count
6774 + elf_section_data (o->output_section)->rel_count2);
6775 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6777 unsigned long r_symndx;
6778 asection *sec;
6780 if (next_erel == bed->s->int_rels_per_ext_rel)
6782 rel_hash++;
6783 next_erel = 0;
6786 irela->r_offset += o->output_offset;
6788 /* Relocs in an executable have to be virtual addresses. */
6789 if (finfo->info->emitrelocations)
6790 irela->r_offset += o->output_section->vma;
6792 r_symndx = ELF_R_SYM (irela->r_info);
6794 if (r_symndx == 0)
6795 continue;
6797 if (r_symndx >= locsymcount
6798 || (elf_bad_symtab (input_bfd)
6799 && finfo->sections[r_symndx] == NULL))
6801 struct elf_link_hash_entry *rh;
6802 unsigned long indx;
6804 /* This is a reloc against a global symbol. We
6805 have not yet output all the local symbols, so
6806 we do not know the symbol index of any global
6807 symbol. We set the rel_hash entry for this
6808 reloc to point to the global hash table entry
6809 for this symbol. The symbol index is then
6810 set at the end of elf_bfd_final_link. */
6811 indx = r_symndx - extsymoff;
6812 rh = elf_sym_hashes (input_bfd)[indx];
6813 while (rh->root.type == bfd_link_hash_indirect
6814 || rh->root.type == bfd_link_hash_warning)
6815 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6817 /* Setting the index to -2 tells
6818 elf_link_output_extsym that this symbol is
6819 used by a reloc. */
6820 BFD_ASSERT (rh->indx < 0);
6821 rh->indx = -2;
6823 *rel_hash = rh;
6825 continue;
6828 /* This is a reloc against a local symbol. */
6830 *rel_hash = NULL;
6831 isym = finfo->internal_syms + r_symndx;
6832 sec = finfo->sections[r_symndx];
6833 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6835 /* I suppose the backend ought to fill in the
6836 section of any STT_SECTION symbol against a
6837 processor specific section. If we have
6838 discarded a section, the output_section will
6839 be the absolute section. */
6840 if (sec != NULL
6841 && (bfd_is_abs_section (sec)
6842 || (sec->output_section != NULL
6843 && bfd_is_abs_section (sec->output_section))))
6844 r_symndx = 0;
6845 else if (sec == NULL || sec->owner == NULL)
6847 bfd_set_error (bfd_error_bad_value);
6848 return false;
6850 else
6852 r_symndx = sec->output_section->target_index;
6853 BFD_ASSERT (r_symndx != 0);
6856 else
6858 if (finfo->indices[r_symndx] == -1)
6860 unsigned long shlink;
6861 const char *name;
6862 asection *osec;
6864 if (finfo->info->strip == strip_all)
6866 /* You can't do ld -r -s. */
6867 bfd_set_error (bfd_error_invalid_operation);
6868 return false;
6871 /* This symbol was skipped earlier, but
6872 since it is needed by a reloc, we
6873 must output it now. */
6874 shlink = symtab_hdr->sh_link;
6875 name = (bfd_elf_string_from_elf_section
6876 (input_bfd, shlink, isym->st_name));
6877 if (name == NULL)
6878 return false;
6880 osec = sec->output_section;
6881 isym->st_shndx =
6882 _bfd_elf_section_from_bfd_section (output_bfd,
6883 osec);
6884 if (isym->st_shndx == SHN_BAD)
6885 return false;
6887 isym->st_value += sec->output_offset;
6888 if (! finfo->info->relocateable)
6889 isym->st_value += osec->vma;
6891 finfo->indices[r_symndx]
6892 = bfd_get_symcount (output_bfd);
6894 if (! elf_link_output_sym (finfo, name, isym, sec))
6895 return false;
6898 r_symndx = finfo->indices[r_symndx];
6901 irela->r_info = ELF_R_INFO (r_symndx,
6902 ELF_R_TYPE (irela->r_info));
6905 /* Swap out the relocs. */
6906 if (bed->elf_backend_emit_relocs
6907 && !(finfo->info->relocateable
6908 || finfo->info->emitrelocations))
6909 reloc_emitter = bed->elf_backend_emit_relocs;
6910 else
6911 reloc_emitter = elf_link_output_relocs;
6913 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6914 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6916 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6917 if (input_rel_hdr)
6919 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6920 * bed->s->int_rels_per_ext_rel);
6921 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6927 /* Write out the modified section contents. */
6928 if (bed->elf_backend_write_section
6929 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6931 /* Section written out. */
6933 else switch (elf_section_data (o)->sec_info_type)
6935 case ELF_INFO_TYPE_STABS:
6936 if (! (_bfd_write_section_stabs
6937 (output_bfd,
6938 &elf_hash_table (finfo->info)->stab_info,
6939 o, &elf_section_data (o)->sec_info, contents)))
6940 return false;
6941 break;
6942 case ELF_INFO_TYPE_MERGE:
6943 if (! (_bfd_write_merged_section
6944 (output_bfd, o, elf_section_data (o)->sec_info)))
6945 return false;
6946 break;
6947 case ELF_INFO_TYPE_EH_FRAME:
6949 asection *ehdrsec;
6951 ehdrsec
6952 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6953 ".eh_frame_hdr");
6954 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6955 contents)))
6956 return false;
6958 break;
6959 default:
6961 bfd_size_type sec_size;
6963 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6964 if (! (o->flags & SEC_EXCLUDE)
6965 && ! bfd_set_section_contents (output_bfd, o->output_section,
6966 contents,
6967 (file_ptr) o->output_offset,
6968 sec_size))
6969 return false;
6971 break;
6975 return true;
6978 /* Generate a reloc when linking an ELF file. This is a reloc
6979 requested by the linker, and does come from any input file. This
6980 is used to build constructor and destructor tables when linking
6981 with -Ur. */
6983 static boolean
6984 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6985 bfd *output_bfd;
6986 struct bfd_link_info *info;
6987 asection *output_section;
6988 struct bfd_link_order *link_order;
6990 reloc_howto_type *howto;
6991 long indx;
6992 bfd_vma offset;
6993 bfd_vma addend;
6994 struct elf_link_hash_entry **rel_hash_ptr;
6995 Elf_Internal_Shdr *rel_hdr;
6996 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6998 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6999 if (howto == NULL)
7001 bfd_set_error (bfd_error_bad_value);
7002 return false;
7005 addend = link_order->u.reloc.p->addend;
7007 /* Figure out the symbol index. */
7008 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7009 + elf_section_data (output_section)->rel_count
7010 + elf_section_data (output_section)->rel_count2);
7011 if (link_order->type == bfd_section_reloc_link_order)
7013 indx = link_order->u.reloc.p->u.section->target_index;
7014 BFD_ASSERT (indx != 0);
7015 *rel_hash_ptr = NULL;
7017 else
7019 struct elf_link_hash_entry *h;
7021 /* Treat a reloc against a defined symbol as though it were
7022 actually against the section. */
7023 h = ((struct elf_link_hash_entry *)
7024 bfd_wrapped_link_hash_lookup (output_bfd, info,
7025 link_order->u.reloc.p->u.name,
7026 false, false, true));
7027 if (h != NULL
7028 && (h->root.type == bfd_link_hash_defined
7029 || h->root.type == bfd_link_hash_defweak))
7031 asection *section;
7033 section = h->root.u.def.section;
7034 indx = section->output_section->target_index;
7035 *rel_hash_ptr = NULL;
7036 /* It seems that we ought to add the symbol value to the
7037 addend here, but in practice it has already been added
7038 because it was passed to constructor_callback. */
7039 addend += section->output_section->vma + section->output_offset;
7041 else if (h != NULL)
7043 /* Setting the index to -2 tells elf_link_output_extsym that
7044 this symbol is used by a reloc. */
7045 h->indx = -2;
7046 *rel_hash_ptr = h;
7047 indx = 0;
7049 else
7051 if (! ((*info->callbacks->unattached_reloc)
7052 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7053 (asection *) NULL, (bfd_vma) 0)))
7054 return false;
7055 indx = 0;
7059 /* If this is an inplace reloc, we must write the addend into the
7060 object file. */
7061 if (howto->partial_inplace && addend != 0)
7063 bfd_size_type size;
7064 bfd_reloc_status_type rstat;
7065 bfd_byte *buf;
7066 boolean ok;
7067 const char *sym_name;
7069 size = bfd_get_reloc_size (howto);
7070 buf = (bfd_byte *) bfd_zmalloc (size);
7071 if (buf == (bfd_byte *) NULL)
7072 return false;
7073 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
7074 switch (rstat)
7076 case bfd_reloc_ok:
7077 break;
7079 default:
7080 case bfd_reloc_outofrange:
7081 abort ();
7083 case bfd_reloc_overflow:
7084 if (link_order->type == bfd_section_reloc_link_order)
7085 sym_name = bfd_section_name (output_bfd,
7086 link_order->u.reloc.p->u.section);
7087 else
7088 sym_name = link_order->u.reloc.p->u.name;
7089 if (! ((*info->callbacks->reloc_overflow)
7090 (info, sym_name, howto->name, addend,
7091 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7093 free (buf);
7094 return false;
7096 break;
7098 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7099 (file_ptr) link_order->offset, size);
7100 free (buf);
7101 if (! ok)
7102 return false;
7105 /* The address of a reloc is relative to the section in a
7106 relocateable file, and is a virtual address in an executable
7107 file. */
7108 offset = link_order->offset;
7109 if (! info->relocateable)
7110 offset += output_section->vma;
7112 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7114 if (rel_hdr->sh_type == SHT_REL)
7116 bfd_size_type size;
7117 Elf_Internal_Rel *irel;
7118 Elf_External_Rel *erel;
7119 unsigned int i;
7121 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7122 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7123 if (irel == NULL)
7124 return false;
7126 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7127 irel[i].r_offset = offset;
7128 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7130 erel = ((Elf_External_Rel *) rel_hdr->contents
7131 + elf_section_data (output_section)->rel_count);
7133 if (bed->s->swap_reloc_out)
7134 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7135 else
7136 elf_swap_reloc_out (output_bfd, irel, erel);
7138 free (irel);
7140 else
7142 bfd_size_type size;
7143 Elf_Internal_Rela *irela;
7144 Elf_External_Rela *erela;
7145 unsigned int i;
7147 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7148 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7149 if (irela == NULL)
7150 return false;
7152 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7153 irela[i].r_offset = offset;
7154 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7155 irela[0].r_addend = addend;
7157 erela = ((Elf_External_Rela *) rel_hdr->contents
7158 + elf_section_data (output_section)->rel_count);
7160 if (bed->s->swap_reloca_out)
7161 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7162 else
7163 elf_swap_reloca_out (output_bfd, irela, erela);
7166 ++elf_section_data (output_section)->rel_count;
7168 return true;
7171 /* Allocate a pointer to live in a linker created section. */
7173 boolean
7174 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7175 bfd *abfd;
7176 struct bfd_link_info *info;
7177 elf_linker_section_t *lsect;
7178 struct elf_link_hash_entry *h;
7179 const Elf_Internal_Rela *rel;
7181 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7182 elf_linker_section_pointers_t *linker_section_ptr;
7183 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7184 bfd_size_type amt;
7186 BFD_ASSERT (lsect != NULL);
7188 /* Is this a global symbol? */
7189 if (h != NULL)
7191 /* Has this symbol already been allocated? If so, our work is done. */
7192 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7193 rel->r_addend,
7194 lsect->which))
7195 return true;
7197 ptr_linker_section_ptr = &h->linker_section_pointer;
7198 /* Make sure this symbol is output as a dynamic symbol. */
7199 if (h->dynindx == -1)
7201 if (! elf_link_record_dynamic_symbol (info, h))
7202 return false;
7205 if (lsect->rel_section)
7206 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7208 else
7210 /* Allocation of a pointer to a local symbol. */
7211 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7213 /* Allocate a table to hold the local symbols if first time. */
7214 if (!ptr)
7216 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7217 register unsigned int i;
7219 amt = num_symbols;
7220 amt *= sizeof (elf_linker_section_pointers_t *);
7221 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7223 if (!ptr)
7224 return false;
7226 elf_local_ptr_offsets (abfd) = ptr;
7227 for (i = 0; i < num_symbols; i++)
7228 ptr[i] = (elf_linker_section_pointers_t *) 0;
7231 /* Has this symbol already been allocated? If so, our work is done. */
7232 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7233 rel->r_addend,
7234 lsect->which))
7235 return true;
7237 ptr_linker_section_ptr = &ptr[r_symndx];
7239 if (info->shared)
7241 /* If we are generating a shared object, we need to
7242 output a R_<xxx>_RELATIVE reloc so that the
7243 dynamic linker can adjust this GOT entry. */
7244 BFD_ASSERT (lsect->rel_section != NULL);
7245 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7249 /* Allocate space for a pointer in the linker section, and allocate
7250 a new pointer record from internal memory. */
7251 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7252 amt = sizeof (elf_linker_section_pointers_t);
7253 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7255 if (!linker_section_ptr)
7256 return false;
7258 linker_section_ptr->next = *ptr_linker_section_ptr;
7259 linker_section_ptr->addend = rel->r_addend;
7260 linker_section_ptr->which = lsect->which;
7261 linker_section_ptr->written_address_p = false;
7262 *ptr_linker_section_ptr = linker_section_ptr;
7264 #if 0
7265 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7267 linker_section_ptr->offset = (lsect->section->_raw_size
7268 - lsect->hole_size + (ARCH_SIZE / 8));
7269 lsect->hole_offset += ARCH_SIZE / 8;
7270 lsect->sym_offset += ARCH_SIZE / 8;
7271 if (lsect->sym_hash)
7273 /* Bump up symbol value if needed. */
7274 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7275 #ifdef DEBUG
7276 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7277 lsect->sym_hash->root.root.string,
7278 (long) ARCH_SIZE / 8,
7279 (long) lsect->sym_hash->root.u.def.value);
7280 #endif
7283 else
7284 #endif
7285 linker_section_ptr->offset = lsect->section->_raw_size;
7287 lsect->section->_raw_size += ARCH_SIZE / 8;
7289 #ifdef DEBUG
7290 fprintf (stderr,
7291 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7292 lsect->name, (long) linker_section_ptr->offset,
7293 (long) lsect->section->_raw_size);
7294 #endif
7296 return true;
7299 #if ARCH_SIZE==64
7300 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7301 #endif
7302 #if ARCH_SIZE==32
7303 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7304 #endif
7306 /* Fill in the address for a pointer generated in a linker section. */
7308 bfd_vma
7309 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7310 relocation, rel, relative_reloc)
7311 bfd *output_bfd;
7312 bfd *input_bfd;
7313 struct bfd_link_info *info;
7314 elf_linker_section_t *lsect;
7315 struct elf_link_hash_entry *h;
7316 bfd_vma relocation;
7317 const Elf_Internal_Rela *rel;
7318 int relative_reloc;
7320 elf_linker_section_pointers_t *linker_section_ptr;
7322 BFD_ASSERT (lsect != NULL);
7324 if (h != NULL)
7326 /* Handle global symbol. */
7327 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7328 (h->linker_section_pointer,
7329 rel->r_addend,
7330 lsect->which));
7332 BFD_ASSERT (linker_section_ptr != NULL);
7334 if (! elf_hash_table (info)->dynamic_sections_created
7335 || (info->shared
7336 && info->symbolic
7337 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7339 /* This is actually a static link, or it is a
7340 -Bsymbolic link and the symbol is defined
7341 locally. We must initialize this entry in the
7342 global section.
7344 When doing a dynamic link, we create a .rela.<xxx>
7345 relocation entry to initialize the value. This
7346 is done in the finish_dynamic_symbol routine. */
7347 if (!linker_section_ptr->written_address_p)
7349 linker_section_ptr->written_address_p = true;
7350 bfd_put_ptr (output_bfd,
7351 relocation + linker_section_ptr->addend,
7352 (lsect->section->contents
7353 + linker_section_ptr->offset));
7357 else
7359 /* Handle local symbol. */
7360 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7361 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7362 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7363 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7364 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7365 rel->r_addend,
7366 lsect->which));
7368 BFD_ASSERT (linker_section_ptr != NULL);
7370 /* Write out pointer if it hasn't been rewritten out before. */
7371 if (!linker_section_ptr->written_address_p)
7373 linker_section_ptr->written_address_p = true;
7374 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7375 lsect->section->contents + linker_section_ptr->offset);
7377 if (info->shared)
7379 asection *srel = lsect->rel_section;
7380 Elf_Internal_Rela *outrel;
7381 Elf_External_Rela *erel;
7382 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7383 unsigned int i;
7384 bfd_size_type amt;
7386 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7387 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7388 if (outrel == NULL)
7390 (*_bfd_error_handler) (_("Error: out of memory"));
7391 return 0;
7394 /* We need to generate a relative reloc for the dynamic
7395 linker. */
7396 if (!srel)
7398 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7399 lsect->rel_name);
7400 lsect->rel_section = srel;
7403 BFD_ASSERT (srel != NULL);
7405 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7406 outrel[i].r_offset = (lsect->section->output_section->vma
7407 + lsect->section->output_offset
7408 + linker_section_ptr->offset);
7409 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7410 outrel[0].r_addend = 0;
7411 erel = (Elf_External_Rela *) lsect->section->contents;
7412 erel += elf_section_data (lsect->section)->rel_count;
7413 elf_swap_reloca_out (output_bfd, outrel, erel);
7414 ++elf_section_data (lsect->section)->rel_count;
7416 free (outrel);
7421 relocation = (lsect->section->output_offset
7422 + linker_section_ptr->offset
7423 - lsect->hole_offset
7424 - lsect->sym_offset);
7426 #ifdef DEBUG
7427 fprintf (stderr,
7428 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7429 lsect->name, (long) relocation, (long) relocation);
7430 #endif
7432 /* Subtract out the addend, because it will get added back in by the normal
7433 processing. */
7434 return relocation - linker_section_ptr->addend;
7437 /* Garbage collect unused sections. */
7439 static boolean elf_gc_mark
7440 PARAMS ((struct bfd_link_info *info, asection *sec,
7441 asection * (*gc_mark_hook)
7442 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7443 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7445 static boolean elf_gc_sweep
7446 PARAMS ((struct bfd_link_info *info,
7447 boolean (*gc_sweep_hook)
7448 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7449 const Elf_Internal_Rela *relocs))));
7451 static boolean elf_gc_sweep_symbol
7452 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7454 static boolean elf_gc_allocate_got_offsets
7455 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7457 static boolean elf_gc_propagate_vtable_entries_used
7458 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7460 static boolean elf_gc_smash_unused_vtentry_relocs
7461 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7463 /* The mark phase of garbage collection. For a given section, mark
7464 it and any sections in this section's group, and all the sections
7465 which define symbols to which it refers. */
7467 static boolean
7468 elf_gc_mark (info, sec, gc_mark_hook)
7469 struct bfd_link_info *info;
7470 asection *sec;
7471 asection * (*gc_mark_hook)
7472 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7473 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7475 boolean ret;
7476 asection *group_sec;
7478 sec->gc_mark = 1;
7480 /* Mark all the sections in the group. */
7481 group_sec = elf_section_data (sec)->next_in_group;
7482 if (group_sec && !group_sec->gc_mark)
7483 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7484 return false;
7486 /* Look through the section relocs. */
7487 ret = true;
7488 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7490 Elf_Internal_Rela *relstart, *rel, *relend;
7491 Elf_Internal_Shdr *symtab_hdr;
7492 Elf_Internal_Shdr *shndx_hdr;
7493 struct elf_link_hash_entry **sym_hashes;
7494 size_t nlocsyms;
7495 size_t extsymoff;
7496 Elf_External_Sym *locsyms, *freesyms = NULL;
7497 Elf_External_Sym_Shndx *locsym_shndx;
7498 bfd *input_bfd = sec->owner;
7499 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7501 /* GCFIXME: how to arrange so that relocs and symbols are not
7502 reread continually? */
7504 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7505 sym_hashes = elf_sym_hashes (input_bfd);
7507 /* Read the local symbols. */
7508 if (elf_bad_symtab (input_bfd))
7510 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7511 extsymoff = 0;
7513 else
7514 extsymoff = nlocsyms = symtab_hdr->sh_info;
7516 if (symtab_hdr->contents)
7517 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7518 else if (nlocsyms == 0)
7519 locsyms = NULL;
7520 else
7522 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7523 locsyms = freesyms = bfd_malloc (amt);
7524 if (freesyms == NULL
7525 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7526 || bfd_bread (locsyms, amt, input_bfd) != amt)
7528 ret = false;
7529 goto out1;
7533 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7534 locsym_shndx = NULL;
7535 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7537 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7538 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7539 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7540 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7541 return false;
7544 /* Read the relocations. */
7545 relstart = (NAME(_bfd_elf,link_read_relocs)
7546 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7547 info->keep_memory));
7548 if (relstart == NULL)
7550 ret = false;
7551 goto out1;
7553 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7555 for (rel = relstart; rel < relend; rel++)
7557 unsigned long r_symndx;
7558 asection *rsec;
7559 struct elf_link_hash_entry *h;
7560 Elf_Internal_Sym s;
7562 r_symndx = ELF_R_SYM (rel->r_info);
7563 if (r_symndx == 0)
7564 continue;
7566 if (elf_bad_symtab (sec->owner))
7568 elf_swap_symbol_in (input_bfd,
7569 locsyms + r_symndx,
7570 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7571 &s);
7572 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7573 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7574 else
7576 h = sym_hashes[r_symndx - extsymoff];
7577 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7580 else if (r_symndx >= nlocsyms)
7582 h = sym_hashes[r_symndx - extsymoff];
7583 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7585 else
7587 elf_swap_symbol_in (input_bfd,
7588 locsyms + r_symndx,
7589 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7590 &s);
7591 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7594 if (rsec && !rsec->gc_mark)
7596 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7597 rsec->gc_mark = 1;
7598 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7600 ret = false;
7601 goto out2;
7606 out2:
7607 if (!info->keep_memory)
7608 free (relstart);
7609 out1:
7610 if (freesyms)
7611 free (freesyms);
7614 return ret;
7617 /* The sweep phase of garbage collection. Remove all garbage sections. */
7619 static boolean
7620 elf_gc_sweep (info, gc_sweep_hook)
7621 struct bfd_link_info *info;
7622 boolean (*gc_sweep_hook)
7623 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7624 const Elf_Internal_Rela *relocs));
7626 bfd *sub;
7628 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7630 asection *o;
7632 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7633 continue;
7635 for (o = sub->sections; o != NULL; o = o->next)
7637 /* Keep special sections. Keep .debug sections. */
7638 if ((o->flags & SEC_LINKER_CREATED)
7639 || (o->flags & SEC_DEBUGGING))
7640 o->gc_mark = 1;
7642 if (o->gc_mark)
7643 continue;
7645 /* Skip sweeping sections already excluded. */
7646 if (o->flags & SEC_EXCLUDE)
7647 continue;
7649 /* Since this is early in the link process, it is simple
7650 to remove a section from the output. */
7651 o->flags |= SEC_EXCLUDE;
7653 /* But we also have to update some of the relocation
7654 info we collected before. */
7655 if (gc_sweep_hook
7656 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7658 Elf_Internal_Rela *internal_relocs;
7659 boolean r;
7661 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7662 (o->owner, o, NULL, NULL, info->keep_memory));
7663 if (internal_relocs == NULL)
7664 return false;
7666 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7668 if (!info->keep_memory)
7669 free (internal_relocs);
7671 if (!r)
7672 return false;
7677 /* Remove the symbols that were in the swept sections from the dynamic
7678 symbol table. GCFIXME: Anyone know how to get them out of the
7679 static symbol table as well? */
7681 int i = 0;
7683 elf_link_hash_traverse (elf_hash_table (info),
7684 elf_gc_sweep_symbol,
7685 (PTR) &i);
7687 elf_hash_table (info)->dynsymcount = i;
7690 return true;
7693 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7695 static boolean
7696 elf_gc_sweep_symbol (h, idxptr)
7697 struct elf_link_hash_entry *h;
7698 PTR idxptr;
7700 int *idx = (int *) idxptr;
7702 if (h->root.type == bfd_link_hash_warning)
7703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7705 if (h->dynindx != -1
7706 && ((h->root.type != bfd_link_hash_defined
7707 && h->root.type != bfd_link_hash_defweak)
7708 || h->root.u.def.section->gc_mark))
7709 h->dynindx = (*idx)++;
7711 return true;
7714 /* Propogate collected vtable information. This is called through
7715 elf_link_hash_traverse. */
7717 static boolean
7718 elf_gc_propagate_vtable_entries_used (h, okp)
7719 struct elf_link_hash_entry *h;
7720 PTR okp;
7722 if (h->root.type == bfd_link_hash_warning)
7723 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7725 /* Those that are not vtables. */
7726 if (h->vtable_parent == NULL)
7727 return true;
7729 /* Those vtables that do not have parents, we cannot merge. */
7730 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7731 return true;
7733 /* If we've already been done, exit. */
7734 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7735 return true;
7737 /* Make sure the parent's table is up to date. */
7738 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7740 if (h->vtable_entries_used == NULL)
7742 /* None of this table's entries were referenced. Re-use the
7743 parent's table. */
7744 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7745 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7747 else
7749 size_t n;
7750 boolean *cu, *pu;
7752 /* Or the parent's entries into ours. */
7753 cu = h->vtable_entries_used;
7754 cu[-1] = true;
7755 pu = h->vtable_parent->vtable_entries_used;
7756 if (pu != NULL)
7758 asection *sec = h->root.u.def.section;
7759 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7760 int file_align = bed->s->file_align;
7762 n = h->vtable_parent->vtable_entries_size / file_align;
7763 while (n--)
7765 if (*pu)
7766 *cu = true;
7767 pu++;
7768 cu++;
7773 return true;
7776 static boolean
7777 elf_gc_smash_unused_vtentry_relocs (h, okp)
7778 struct elf_link_hash_entry *h;
7779 PTR okp;
7781 asection *sec;
7782 bfd_vma hstart, hend;
7783 Elf_Internal_Rela *relstart, *relend, *rel;
7784 struct elf_backend_data *bed;
7785 int file_align;
7787 if (h->root.type == bfd_link_hash_warning)
7788 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7790 /* Take care of both those symbols that do not describe vtables as
7791 well as those that are not loaded. */
7792 if (h->vtable_parent == NULL)
7793 return true;
7795 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7796 || h->root.type == bfd_link_hash_defweak);
7798 sec = h->root.u.def.section;
7799 hstart = h->root.u.def.value;
7800 hend = hstart + h->size;
7802 relstart = (NAME(_bfd_elf,link_read_relocs)
7803 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7804 if (!relstart)
7805 return *(boolean *) okp = false;
7806 bed = get_elf_backend_data (sec->owner);
7807 file_align = bed->s->file_align;
7809 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7811 for (rel = relstart; rel < relend; ++rel)
7812 if (rel->r_offset >= hstart && rel->r_offset < hend)
7814 /* If the entry is in use, do nothing. */
7815 if (h->vtable_entries_used
7816 && (rel->r_offset - hstart) < h->vtable_entries_size)
7818 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7819 if (h->vtable_entries_used[entry])
7820 continue;
7822 /* Otherwise, kill it. */
7823 rel->r_offset = rel->r_info = rel->r_addend = 0;
7826 return true;
7829 /* Do mark and sweep of unused sections. */
7831 boolean
7832 elf_gc_sections (abfd, info)
7833 bfd *abfd;
7834 struct bfd_link_info *info;
7836 boolean ok = true;
7837 bfd *sub;
7838 asection * (*gc_mark_hook)
7839 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7840 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7842 if (!get_elf_backend_data (abfd)->can_gc_sections
7843 || info->relocateable || info->emitrelocations
7844 || elf_hash_table (info)->dynamic_sections_created)
7845 return true;
7847 /* Apply transitive closure to the vtable entry usage info. */
7848 elf_link_hash_traverse (elf_hash_table (info),
7849 elf_gc_propagate_vtable_entries_used,
7850 (PTR) &ok);
7851 if (!ok)
7852 return false;
7854 /* Kill the vtable relocations that were not used. */
7855 elf_link_hash_traverse (elf_hash_table (info),
7856 elf_gc_smash_unused_vtentry_relocs,
7857 (PTR) &ok);
7858 if (!ok)
7859 return false;
7861 /* Grovel through relocs to find out who stays ... */
7863 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7864 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7866 asection *o;
7868 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7869 continue;
7871 for (o = sub->sections; o != NULL; o = o->next)
7873 if (o->flags & SEC_KEEP)
7874 if (!elf_gc_mark (info, o, gc_mark_hook))
7875 return false;
7879 /* ... and mark SEC_EXCLUDE for those that go. */
7880 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7881 return false;
7883 return true;
7886 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7888 boolean
7889 elf_gc_record_vtinherit (abfd, sec, h, offset)
7890 bfd *abfd;
7891 asection *sec;
7892 struct elf_link_hash_entry *h;
7893 bfd_vma offset;
7895 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7896 struct elf_link_hash_entry **search, *child;
7897 bfd_size_type extsymcount;
7899 /* The sh_info field of the symtab header tells us where the
7900 external symbols start. We don't care about the local symbols at
7901 this point. */
7902 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7903 if (!elf_bad_symtab (abfd))
7904 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7906 sym_hashes = elf_sym_hashes (abfd);
7907 sym_hashes_end = sym_hashes + extsymcount;
7909 /* Hunt down the child symbol, which is in this section at the same
7910 offset as the relocation. */
7911 for (search = sym_hashes; search != sym_hashes_end; ++search)
7913 if ((child = *search) != NULL
7914 && (child->root.type == bfd_link_hash_defined
7915 || child->root.type == bfd_link_hash_defweak)
7916 && child->root.u.def.section == sec
7917 && child->root.u.def.value == offset)
7918 goto win;
7921 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7922 bfd_archive_filename (abfd), sec->name,
7923 (unsigned long) offset);
7924 bfd_set_error (bfd_error_invalid_operation);
7925 return false;
7927 win:
7928 if (!h)
7930 /* This *should* only be the absolute section. It could potentially
7931 be that someone has defined a non-global vtable though, which
7932 would be bad. It isn't worth paging in the local symbols to be
7933 sure though; that case should simply be handled by the assembler. */
7935 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7937 else
7938 child->vtable_parent = h;
7940 return true;
7943 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7945 boolean
7946 elf_gc_record_vtentry (abfd, sec, h, addend)
7947 bfd *abfd ATTRIBUTE_UNUSED;
7948 asection *sec ATTRIBUTE_UNUSED;
7949 struct elf_link_hash_entry *h;
7950 bfd_vma addend;
7952 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7953 int file_align = bed->s->file_align;
7955 if (addend >= h->vtable_entries_size)
7957 size_t size, bytes;
7958 boolean *ptr = h->vtable_entries_used;
7960 /* While the symbol is undefined, we have to be prepared to handle
7961 a zero size. */
7962 if (h->root.type == bfd_link_hash_undefined)
7963 size = addend;
7964 else
7966 size = h->size;
7967 if (size < addend)
7969 /* Oops! We've got a reference past the defined end of
7970 the table. This is probably a bug -- shall we warn? */
7971 size = addend;
7975 /* Allocate one extra entry for use as a "done" flag for the
7976 consolidation pass. */
7977 bytes = (size / file_align + 1) * sizeof (boolean);
7979 if (ptr)
7981 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7983 if (ptr != NULL)
7985 size_t oldbytes;
7987 oldbytes = ((h->vtable_entries_size / file_align + 1)
7988 * sizeof (boolean));
7989 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7992 else
7993 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7995 if (ptr == NULL)
7996 return false;
7998 /* And arrange for that done flag to be at index -1. */
7999 h->vtable_entries_used = ptr + 1;
8000 h->vtable_entries_size = size;
8003 h->vtable_entries_used[addend / file_align] = true;
8005 return true;
8008 /* And an accompanying bit to work out final got entry offsets once
8009 we're done. Should be called from final_link. */
8011 boolean
8012 elf_gc_common_finalize_got_offsets (abfd, info)
8013 bfd *abfd;
8014 struct bfd_link_info *info;
8016 bfd *i;
8017 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8018 bfd_vma gotoff;
8020 /* The GOT offset is relative to the .got section, but the GOT header is
8021 put into the .got.plt section, if the backend uses it. */
8022 if (bed->want_got_plt)
8023 gotoff = 0;
8024 else
8025 gotoff = bed->got_header_size;
8027 /* Do the local .got entries first. */
8028 for (i = info->input_bfds; i; i = i->link_next)
8030 bfd_signed_vma *local_got;
8031 bfd_size_type j, locsymcount;
8032 Elf_Internal_Shdr *symtab_hdr;
8034 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8035 continue;
8037 local_got = elf_local_got_refcounts (i);
8038 if (!local_got)
8039 continue;
8041 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8042 if (elf_bad_symtab (i))
8043 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8044 else
8045 locsymcount = symtab_hdr->sh_info;
8047 for (j = 0; j < locsymcount; ++j)
8049 if (local_got[j] > 0)
8051 local_got[j] = gotoff;
8052 gotoff += ARCH_SIZE / 8;
8054 else
8055 local_got[j] = (bfd_vma) -1;
8059 /* Then the global .got entries. .plt refcounts are handled by
8060 adjust_dynamic_symbol */
8061 elf_link_hash_traverse (elf_hash_table (info),
8062 elf_gc_allocate_got_offsets,
8063 (PTR) &gotoff);
8064 return true;
8067 /* We need a special top-level link routine to convert got reference counts
8068 to real got offsets. */
8070 static boolean
8071 elf_gc_allocate_got_offsets (h, offarg)
8072 struct elf_link_hash_entry *h;
8073 PTR offarg;
8075 bfd_vma *off = (bfd_vma *) offarg;
8077 if (h->root.type == bfd_link_hash_warning)
8078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8080 if (h->got.refcount > 0)
8082 h->got.offset = off[0];
8083 off[0] += ARCH_SIZE / 8;
8085 else
8086 h->got.offset = (bfd_vma) -1;
8088 return true;
8091 /* Many folk need no more in the way of final link than this, once
8092 got entry reference counting is enabled. */
8094 boolean
8095 elf_gc_common_final_link (abfd, info)
8096 bfd *abfd;
8097 struct bfd_link_info *info;
8099 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8100 return false;
8102 /* Invoke the regular ELF backend linker to do all the work. */
8103 return elf_bfd_final_link (abfd, info);
8106 /* This function will be called though elf_link_hash_traverse to store
8107 all hash value of the exported symbols in an array. */
8109 static boolean
8110 elf_collect_hash_codes (h, data)
8111 struct elf_link_hash_entry *h;
8112 PTR data;
8114 unsigned long **valuep = (unsigned long **) data;
8115 const char *name;
8116 char *p;
8117 unsigned long ha;
8118 char *alc = NULL;
8120 if (h->root.type == bfd_link_hash_warning)
8121 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8123 /* Ignore indirect symbols. These are added by the versioning code. */
8124 if (h->dynindx == -1)
8125 return true;
8127 name = h->root.root.string;
8128 p = strchr (name, ELF_VER_CHR);
8129 if (p != NULL)
8131 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8132 memcpy (alc, name, (size_t) (p - name));
8133 alc[p - name] = '\0';
8134 name = alc;
8137 /* Compute the hash value. */
8138 ha = bfd_elf_hash (name);
8140 /* Store the found hash value in the array given as the argument. */
8141 *(*valuep)++ = ha;
8143 /* And store it in the struct so that we can put it in the hash table
8144 later. */
8145 h->elf_hash_value = ha;
8147 if (alc != NULL)
8148 free (alc);
8150 return true;
8153 boolean
8154 elf_reloc_symbol_deleted_p (offset, cookie)
8155 bfd_vma offset;
8156 PTR cookie;
8158 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8160 if (rcookie->bad_symtab)
8161 rcookie->rel = rcookie->rels;
8163 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8165 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8166 Elf_Internal_Sym isym;
8168 if (! rcookie->bad_symtab)
8169 if (rcookie->rel->r_offset > offset)
8170 return false;
8171 if (rcookie->rel->r_offset != offset)
8172 continue;
8174 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8176 Elf_External_Sym *lsym;
8177 Elf_External_Sym_Shndx *lshndx;
8179 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8180 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8181 if (lshndx != NULL)
8182 lshndx += r_symndx;
8183 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8186 if (r_symndx >= rcookie->locsymcount
8187 || (rcookie->locsyms
8188 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8190 struct elf_link_hash_entry *h;
8192 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8194 while (h->root.type == bfd_link_hash_indirect
8195 || h->root.type == bfd_link_hash_warning)
8196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8198 if ((h->root.type == bfd_link_hash_defined
8199 || h->root.type == bfd_link_hash_defweak)
8200 && elf_discarded_section (h->root.u.def.section))
8201 return true;
8202 else
8203 return false;
8205 else if (rcookie->locsyms)
8207 /* It's not a relocation against a global symbol,
8208 but it could be a relocation against a local
8209 symbol for a discarded section. */
8210 asection *isec;
8212 /* Need to: get the symbol; get the section. */
8213 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8215 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8216 if (isec != NULL && elf_discarded_section (isec))
8217 return true;
8220 return false;
8222 return false;
8225 /* Discard unneeded references to discarded sections.
8226 Returns true if any section's size was changed. */
8227 /* This function assumes that the relocations are in sorted order,
8228 which is true for all known assemblers. */
8230 boolean
8231 elf_bfd_discard_info (output_bfd, info)
8232 bfd *output_bfd;
8233 struct bfd_link_info *info;
8235 struct elf_reloc_cookie cookie;
8236 asection *stab, *eh, *ehdr;
8237 Elf_Internal_Shdr *symtab_hdr;
8238 Elf_Internal_Shdr *shndx_hdr;
8239 Elf_External_Sym *freesyms;
8240 struct elf_backend_data *bed;
8241 bfd *abfd;
8242 boolean ret = false;
8243 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8245 if (info->relocateable
8246 || info->traditional_format
8247 || info->hash->creator->flavour != bfd_target_elf_flavour
8248 || ! is_elf_hash_table (info))
8249 return false;
8251 ehdr = NULL;
8252 if (elf_hash_table (info)->dynobj != NULL)
8253 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8254 ".eh_frame_hdr");
8256 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8258 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8259 continue;
8261 bed = get_elf_backend_data (abfd);
8263 if ((abfd->flags & DYNAMIC) != 0)
8264 continue;
8266 eh = NULL;
8267 if (ehdr)
8269 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8270 if (eh && eh->_raw_size == 0)
8271 eh = NULL;
8274 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
8275 if ((! stab
8276 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8277 && ! eh
8278 && (strip || ! bed->elf_backend_discard_info))
8279 continue;
8281 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8282 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8284 cookie.abfd = abfd;
8285 cookie.sym_hashes = elf_sym_hashes (abfd);
8286 cookie.bad_symtab = elf_bad_symtab (abfd);
8287 if (cookie.bad_symtab)
8289 cookie.locsymcount =
8290 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8291 cookie.extsymoff = 0;
8293 else
8295 cookie.locsymcount = symtab_hdr->sh_info;
8296 cookie.extsymoff = symtab_hdr->sh_info;
8299 freesyms = NULL;
8300 if (symtab_hdr->contents)
8301 cookie.locsyms = (void *) symtab_hdr->contents;
8302 else if (cookie.locsymcount == 0)
8303 cookie.locsyms = NULL;
8304 else
8306 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8307 cookie.locsyms = bfd_malloc (amt);
8308 if (cookie.locsyms == NULL)
8309 return false;
8310 freesyms = cookie.locsyms;
8311 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8312 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8314 error_ret_free_loc:
8315 free (cookie.locsyms);
8316 return false;
8320 cookie.locsym_shndx = NULL;
8321 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8323 bfd_size_type amt;
8324 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8325 cookie.locsym_shndx = bfd_malloc (amt);
8326 if (cookie.locsym_shndx == NULL)
8327 goto error_ret_free_loc;
8328 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8329 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8331 free (cookie.locsym_shndx);
8332 goto error_ret_free_loc;
8336 if (stab)
8338 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8339 (abfd, stab, (PTR) NULL,
8340 (Elf_Internal_Rela *) NULL,
8341 info->keep_memory));
8342 if (cookie.rels)
8344 cookie.rel = cookie.rels;
8345 cookie.relend =
8346 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8347 if (_bfd_discard_section_stabs (abfd, stab,
8348 elf_section_data (stab)->sec_info,
8349 elf_reloc_symbol_deleted_p,
8350 &cookie))
8351 ret = true;
8352 if (! info->keep_memory)
8353 free (cookie.rels);
8357 if (eh)
8359 cookie.rels = NULL;
8360 cookie.rel = NULL;
8361 cookie.relend = NULL;
8362 if (eh->reloc_count)
8363 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8364 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8365 info->keep_memory));
8366 if (cookie.rels)
8368 cookie.rel = cookie.rels;
8369 cookie.relend =
8370 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8372 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8373 elf_reloc_symbol_deleted_p,
8374 &cookie))
8375 ret = true;
8376 if (! info->keep_memory)
8377 free (cookie.rels);
8380 if (bed->elf_backend_discard_info)
8382 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8383 ret = true;
8386 if (cookie.locsym_shndx != NULL)
8387 free (cookie.locsym_shndx);
8389 if (freesyms != NULL)
8390 free (freesyms);
8393 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
8394 ret = true;
8395 return ret;
8398 static boolean
8399 elf_section_ignore_discarded_relocs (sec)
8400 asection *sec;
8402 struct elf_backend_data *bed;
8404 switch (elf_section_data (sec)->sec_info_type)
8406 case ELF_INFO_TYPE_STABS:
8407 case ELF_INFO_TYPE_EH_FRAME:
8408 return true;
8409 default:
8410 break;
8413 bed = get_elf_backend_data (sec->owner);
8414 if (bed->elf_backend_ignore_discarded_relocs != NULL
8415 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8416 return true;
8418 return false;