* elf.c (_bfd_elf_make_section_from_shdr): Set SEC_THREAD_LOCAL
[binutils.git] / bfd / elflink.h
blob5046416a75900c62e8b10415f65ad5a2e2d182ec
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* ELF linker code. */
23 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
26 struct elf_info_failed
28 boolean failed;
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_link_read_relocs_from_section
65 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
66 static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info *));
68 static boolean elf_link_output_relocs
69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
70 static boolean elf_link_size_reloc_section
71 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
72 static void elf_link_adjust_relocs
73 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
74 struct elf_link_hash_entry **));
75 static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77 static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79 static size_t elf_link_sort_relocs
80 PARAMS ((bfd *, struct bfd_link_info *, asection **));
81 static boolean elf_section_ignore_discarded_relocs
82 PARAMS ((asection *));
84 /* Given an ELF BFD, add symbols to the global hash table as
85 appropriate. */
87 boolean
88 elf_bfd_link_add_symbols (abfd, info)
89 bfd *abfd;
90 struct bfd_link_info *info;
92 switch (bfd_get_format (abfd))
94 case bfd_object:
95 return elf_link_add_object_symbols (abfd, info);
96 case bfd_archive:
97 return elf_link_add_archive_symbols (abfd, info);
98 default:
99 bfd_set_error (bfd_error_wrong_format);
100 return false;
104 /* Return true iff this is a non-common, definition of a non-function symbol. */
105 static boolean
106 is_global_data_symbol_definition (abfd, sym)
107 bfd * abfd ATTRIBUTE_UNUSED;
108 Elf_Internal_Sym * sym;
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
112 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
113 return false;
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
117 return false;
119 /* If the section is undefined, then so is the symbol. */
120 if (sym->st_shndx == SHN_UNDEF)
121 return false;
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym->st_shndx == SHN_COMMON)
126 return false;
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
131 /* FIXME - this function is not coded yet:
133 return _bfd_is_global_symbol_definition (abfd, sym);
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
138 return false;
140 return true;
143 /* Search the symbol table of the archive element of the archive ABFD
144 whose archive map contains a mention of SYMDEF, and determine if
145 the symbol is defined in this element. */
146 static boolean
147 elf_link_is_defined_archive_symbol (abfd, symdef)
148 bfd * abfd;
149 carsym * symdef;
151 Elf_Internal_Shdr * hdr;
152 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 (sym.st_shndx == SHN_COMMON && ELF_ST_TYPE (sym.st_info) == STT_TLS)
1728 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1730 if (tcomm == NULL)
1732 tcomm = bfd_make_section (abfd, ".tcommon");
1733 if (tcomm == NULL
1734 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1735 | SEC_IS_COMMON
1736 | SEC_LINKER_CREATED
1737 | SEC_THREAD_LOCAL)))
1738 goto error_return;
1740 sec = tcomm;
1742 else if (add_symbol_hook)
1744 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1745 &value))
1746 goto error_return;
1748 /* The hook function sets the name to NULL if this symbol
1749 should be skipped for some reason. */
1750 if (name == (const char *) NULL)
1751 continue;
1754 /* Sanity check that all possibilities were handled. */
1755 if (sec == (asection *) NULL)
1757 bfd_set_error (bfd_error_bad_value);
1758 goto error_return;
1761 if (bfd_is_und_section (sec)
1762 || bfd_is_com_section (sec))
1763 definition = false;
1764 else
1765 definition = true;
1767 size_change_ok = false;
1768 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1769 old_alignment = 0;
1770 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1772 Elf_Internal_Versym iver;
1773 unsigned int vernum = 0;
1775 if (ever != NULL)
1777 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1778 vernum = iver.vs_vers & VERSYM_VERSION;
1780 /* If this is a hidden symbol, or if it is not version
1781 1, we append the version name to the symbol name.
1782 However, we do not modify a non-hidden absolute
1783 symbol, because it might be the version symbol
1784 itself. FIXME: What if it isn't? */
1785 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1786 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1788 const char *verstr;
1789 unsigned int namelen;
1790 bfd_size_type newlen;
1791 char *newname, *p;
1793 if (sym.st_shndx != SHN_UNDEF)
1795 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1797 (*_bfd_error_handler)
1798 (_("%s: %s: invalid version %u (max %d)"),
1799 bfd_archive_filename (abfd), name, vernum,
1800 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1801 bfd_set_error (bfd_error_bad_value);
1802 goto error_return;
1804 else if (vernum > 1)
1805 verstr =
1806 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1807 else
1808 verstr = "";
1810 else
1812 /* We cannot simply test for the number of
1813 entries in the VERNEED section since the
1814 numbers for the needed versions do not start
1815 at 0. */
1816 Elf_Internal_Verneed *t;
1818 verstr = NULL;
1819 for (t = elf_tdata (abfd)->verref;
1820 t != NULL;
1821 t = t->vn_nextref)
1823 Elf_Internal_Vernaux *a;
1825 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1827 if (a->vna_other == vernum)
1829 verstr = a->vna_nodename;
1830 break;
1833 if (a != NULL)
1834 break;
1836 if (verstr == NULL)
1838 (*_bfd_error_handler)
1839 (_("%s: %s: invalid needed version %d"),
1840 bfd_archive_filename (abfd), name, vernum);
1841 bfd_set_error (bfd_error_bad_value);
1842 goto error_return;
1846 namelen = strlen (name);
1847 newlen = namelen + strlen (verstr) + 2;
1848 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1849 ++newlen;
1851 newname = (char *) bfd_alloc (abfd, newlen);
1852 if (newname == NULL)
1853 goto error_return;
1854 strcpy (newname, name);
1855 p = newname + namelen;
1856 *p++ = ELF_VER_CHR;
1857 /* If this is a defined non-hidden version symbol,
1858 we add another @ to the name. This indicates the
1859 default version of the symbol. */
1860 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1861 && sym.st_shndx != SHN_UNDEF)
1862 *p++ = ELF_VER_CHR;
1863 strcpy (p, verstr);
1865 name = newname;
1869 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1870 sym_hash, &override, &type_change_ok,
1871 &size_change_ok, dt_needed))
1872 goto error_return;
1874 if (override)
1875 definition = false;
1877 h = *sym_hash;
1878 while (h->root.type == bfd_link_hash_indirect
1879 || h->root.type == bfd_link_hash_warning)
1880 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1882 /* Remember the old alignment if this is a common symbol, so
1883 that we don't reduce the alignment later on. We can't
1884 check later, because _bfd_generic_link_add_one_symbol
1885 will set a default for the alignment which we want to
1886 override. */
1887 if (h->root.type == bfd_link_hash_common)
1888 old_alignment = h->root.u.c.p->alignment_power;
1890 if (elf_tdata (abfd)->verdef != NULL
1891 && ! override
1892 && vernum > 1
1893 && definition)
1894 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1897 if (! (_bfd_generic_link_add_one_symbol
1898 (info, abfd, name, flags, sec, value, (const char *) NULL,
1899 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1900 goto error_return;
1902 h = *sym_hash;
1903 while (h->root.type == bfd_link_hash_indirect
1904 || h->root.type == bfd_link_hash_warning)
1905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1906 *sym_hash = h;
1908 new_weakdef = false;
1909 if (dynamic
1910 && definition
1911 && (flags & BSF_WEAK) != 0
1912 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1913 && info->hash->creator->flavour == bfd_target_elf_flavour
1914 && h->weakdef == NULL)
1916 /* Keep a list of all weak defined non function symbols from
1917 a dynamic object, using the weakdef field. Later in this
1918 function we will set the weakdef field to the correct
1919 value. We only put non-function symbols from dynamic
1920 objects on this list, because that happens to be the only
1921 time we need to know the normal symbol corresponding to a
1922 weak symbol, and the information is time consuming to
1923 figure out. If the weakdef field is not already NULL,
1924 then this symbol was already defined by some previous
1925 dynamic object, and we will be using that previous
1926 definition anyhow. */
1928 h->weakdef = weaks;
1929 weaks = h;
1930 new_weakdef = true;
1933 /* Set the alignment of a common symbol. */
1934 if (sym.st_shndx == SHN_COMMON
1935 && h->root.type == bfd_link_hash_common)
1937 unsigned int align;
1939 align = bfd_log2 (sym.st_value);
1940 if (align > old_alignment
1941 /* Permit an alignment power of zero if an alignment of one
1942 is specified and no other alignments have been specified. */
1943 || (sym.st_value == 1 && old_alignment == 0))
1944 h->root.u.c.p->alignment_power = align;
1947 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1949 int old_flags;
1950 boolean dynsym;
1951 int new_flag;
1953 /* Remember the symbol size and type. */
1954 if (sym.st_size != 0
1955 && (definition || h->size == 0))
1957 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1958 (*_bfd_error_handler)
1959 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1960 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1961 bfd_archive_filename (abfd));
1963 h->size = sym.st_size;
1966 /* If this is a common symbol, then we always want H->SIZE
1967 to be the size of the common symbol. The code just above
1968 won't fix the size if a common symbol becomes larger. We
1969 don't warn about a size change here, because that is
1970 covered by --warn-common. */
1971 if (h->root.type == bfd_link_hash_common)
1972 h->size = h->root.u.c.size;
1974 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1975 && (definition || h->type == STT_NOTYPE))
1977 if (h->type != STT_NOTYPE
1978 && h->type != ELF_ST_TYPE (sym.st_info)
1979 && ! type_change_ok)
1980 (*_bfd_error_handler)
1981 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1982 name, h->type, ELF_ST_TYPE (sym.st_info),
1983 bfd_archive_filename (abfd));
1985 h->type = ELF_ST_TYPE (sym.st_info);
1988 /* If st_other has a processor-specific meaning, specific code
1989 might be needed here. */
1990 if (sym.st_other != 0)
1992 /* Combine visibilities, using the most constraining one. */
1993 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1994 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1996 if (symvis && (hvis > symvis || hvis == 0))
1997 h->other = sym.st_other;
1999 /* If neither has visibility, use the st_other of the
2000 definition. This is an arbitrary choice, since the
2001 other bits have no general meaning. */
2002 if (!symvis && !hvis
2003 && (definition || h->other == 0))
2004 h->other = sym.st_other;
2007 /* Set a flag in the hash table entry indicating the type of
2008 reference or definition we just found. Keep a count of
2009 the number of dynamic symbols we find. A dynamic symbol
2010 is one which is referenced or defined by both a regular
2011 object and a shared object. */
2012 old_flags = h->elf_link_hash_flags;
2013 dynsym = false;
2014 if (! dynamic)
2016 if (! definition)
2018 new_flag = ELF_LINK_HASH_REF_REGULAR;
2019 if (bind != STB_WEAK)
2020 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2022 else
2023 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2024 if (info->shared
2025 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2026 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2027 dynsym = true;
2029 else
2031 if (! definition)
2032 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2033 else
2034 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2035 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2036 | ELF_LINK_HASH_REF_REGULAR)) != 0
2037 || (h->weakdef != NULL
2038 && ! new_weakdef
2039 && h->weakdef->dynindx != -1))
2040 dynsym = true;
2043 h->elf_link_hash_flags |= new_flag;
2045 /* Check to see if we need to add an indirect symbol for
2046 the default name. */
2047 if (definition || h->root.type == bfd_link_hash_common)
2048 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2049 &sec, &value, &dynsym,
2050 override, dt_needed))
2051 goto error_return;
2053 if (dynsym && h->dynindx == -1)
2055 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2056 goto error_return;
2057 if (h->weakdef != NULL
2058 && ! new_weakdef
2059 && h->weakdef->dynindx == -1)
2061 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2062 goto error_return;
2065 else if (dynsym && h->dynindx != -1)
2066 /* If the symbol already has a dynamic index, but
2067 visibility says it should not be visible, turn it into
2068 a local symbol. */
2069 switch (ELF_ST_VISIBILITY (h->other))
2071 case STV_INTERNAL:
2072 case STV_HIDDEN:
2073 (*bed->elf_backend_hide_symbol) (info, h, true);
2074 break;
2077 if (dt_needed && definition
2078 && (h->elf_link_hash_flags
2079 & ELF_LINK_HASH_REF_REGULAR) != 0)
2081 bfd_size_type oldsize;
2082 bfd_size_type strindex;
2084 if (! is_elf_hash_table (info))
2085 goto error_return;
2087 /* The symbol from a DT_NEEDED object is referenced from
2088 the regular object to create a dynamic executable. We
2089 have to make sure there is a DT_NEEDED entry for it. */
2091 dt_needed = false;
2092 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2093 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2094 elf_dt_soname (abfd), false);
2095 if (strindex == (bfd_size_type) -1)
2096 goto error_return;
2098 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2100 asection *sdyn;
2101 Elf_External_Dyn *dyncon, *dynconend;
2103 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2104 ".dynamic");
2105 BFD_ASSERT (sdyn != NULL);
2107 dyncon = (Elf_External_Dyn *) sdyn->contents;
2108 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2109 sdyn->_raw_size);
2110 for (; dyncon < dynconend; dyncon++)
2112 Elf_Internal_Dyn dyn;
2114 elf_swap_dyn_in (hash_table->dynobj,
2115 dyncon, &dyn);
2116 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2117 dyn.d_un.d_val != strindex);
2121 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2122 goto error_return;
2127 /* Now set the weakdefs field correctly for all the weak defined
2128 symbols we found. The only way to do this is to search all the
2129 symbols. Since we only need the information for non functions in
2130 dynamic objects, that's the only time we actually put anything on
2131 the list WEAKS. We need this information so that if a regular
2132 object refers to a symbol defined weakly in a dynamic object, the
2133 real symbol in the dynamic object is also put in the dynamic
2134 symbols; we also must arrange for both symbols to point to the
2135 same memory location. We could handle the general case of symbol
2136 aliasing, but a general symbol alias can only be generated in
2137 assembler code, handling it correctly would be very time
2138 consuming, and other ELF linkers don't handle general aliasing
2139 either. */
2140 while (weaks != NULL)
2142 struct elf_link_hash_entry *hlook;
2143 asection *slook;
2144 bfd_vma vlook;
2145 struct elf_link_hash_entry **hpp;
2146 struct elf_link_hash_entry **hppend;
2148 hlook = weaks;
2149 weaks = hlook->weakdef;
2150 hlook->weakdef = NULL;
2152 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2153 || hlook->root.type == bfd_link_hash_defweak
2154 || hlook->root.type == bfd_link_hash_common
2155 || hlook->root.type == bfd_link_hash_indirect);
2156 slook = hlook->root.u.def.section;
2157 vlook = hlook->root.u.def.value;
2159 hpp = elf_sym_hashes (abfd);
2160 hppend = hpp + extsymcount;
2161 for (; hpp < hppend; hpp++)
2163 struct elf_link_hash_entry *h;
2165 h = *hpp;
2166 if (h != NULL && h != hlook
2167 && h->root.type == bfd_link_hash_defined
2168 && h->root.u.def.section == slook
2169 && h->root.u.def.value == vlook)
2171 hlook->weakdef = h;
2173 /* If the weak definition is in the list of dynamic
2174 symbols, make sure the real definition is put there
2175 as well. */
2176 if (hlook->dynindx != -1
2177 && h->dynindx == -1)
2179 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2180 goto error_return;
2183 /* If the real definition is in the list of dynamic
2184 symbols, make sure the weak definition is put there
2185 as well. If we don't do this, then the dynamic
2186 loader might not merge the entries for the real
2187 definition and the weak definition. */
2188 if (h->dynindx != -1
2189 && hlook->dynindx == -1)
2191 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2192 goto error_return;
2195 break;
2200 if (buf != NULL)
2202 free (buf);
2203 buf = NULL;
2206 if (extversym != NULL)
2208 free (extversym);
2209 extversym = NULL;
2212 /* If this object is the same format as the output object, and it is
2213 not a shared library, then let the backend look through the
2214 relocs.
2216 This is required to build global offset table entries and to
2217 arrange for dynamic relocs. It is not required for the
2218 particular common case of linking non PIC code, even when linking
2219 against shared libraries, but unfortunately there is no way of
2220 knowing whether an object file has been compiled PIC or not.
2221 Looking through the relocs is not particularly time consuming.
2222 The problem is that we must either (1) keep the relocs in memory,
2223 which causes the linker to require additional runtime memory or
2224 (2) read the relocs twice from the input file, which wastes time.
2225 This would be a good case for using mmap.
2227 I have no idea how to handle linking PIC code into a file of a
2228 different format. It probably can't be done. */
2229 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2230 if (! dynamic
2231 && abfd->xvec == info->hash->creator
2232 && check_relocs != NULL)
2234 asection *o;
2236 for (o = abfd->sections; o != NULL; o = o->next)
2238 Elf_Internal_Rela *internal_relocs;
2239 boolean ok;
2241 if ((o->flags & SEC_RELOC) == 0
2242 || o->reloc_count == 0
2243 || ((info->strip == strip_all || info->strip == strip_debugger)
2244 && (o->flags & SEC_DEBUGGING) != 0)
2245 || bfd_is_abs_section (o->output_section))
2246 continue;
2248 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2249 (abfd, o, (PTR) NULL,
2250 (Elf_Internal_Rela *) NULL,
2251 info->keep_memory));
2252 if (internal_relocs == NULL)
2253 goto error_return;
2255 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2257 if (! info->keep_memory)
2258 free (internal_relocs);
2260 if (! ok)
2261 goto error_return;
2265 /* If this is a non-traditional, non-relocateable link, try to
2266 optimize the handling of the .stab/.stabstr sections. */
2267 if (! dynamic
2268 && ! info->relocateable
2269 && ! info->traditional_format
2270 && info->hash->creator->flavour == bfd_target_elf_flavour
2271 && is_elf_hash_table (info)
2272 && (info->strip != strip_all && info->strip != strip_debugger))
2274 asection *stab, *stabstr;
2276 stab = bfd_get_section_by_name (abfd, ".stab");
2277 if (stab != NULL
2278 && (stab->flags & SEC_MERGE) == 0
2279 && !bfd_is_abs_section (stab->output_section))
2281 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2283 if (stabstr != NULL)
2285 struct bfd_elf_section_data *secdata;
2287 secdata = elf_section_data (stab);
2288 if (! _bfd_link_section_stabs (abfd,
2289 & hash_table->stab_info,
2290 stab, stabstr,
2291 &secdata->sec_info))
2292 goto error_return;
2293 if (secdata->sec_info)
2294 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2299 if (! info->relocateable && ! dynamic
2300 && is_elf_hash_table (info))
2302 asection *s;
2304 for (s = abfd->sections; s != NULL; s = s->next)
2305 if ((s->flags & SEC_MERGE) != 0
2306 && !bfd_is_abs_section (s->output_section))
2308 struct bfd_elf_section_data *secdata;
2310 secdata = elf_section_data (s);
2311 if (! _bfd_merge_section (abfd,
2312 & hash_table->merge_info,
2313 s, &secdata->sec_info))
2314 goto error_return;
2315 else if (secdata->sec_info)
2316 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2320 return true;
2322 error_return:
2323 if (buf != NULL)
2324 free (buf);
2325 if (dynbuf != NULL)
2326 free (dynbuf);
2327 if (extversym != NULL)
2328 free (extversym);
2329 return false;
2332 /* Create some sections which will be filled in with dynamic linking
2333 information. ABFD is an input file which requires dynamic sections
2334 to be created. The dynamic sections take up virtual memory space
2335 when the final executable is run, so we need to create them before
2336 addresses are assigned to the output sections. We work out the
2337 actual contents and size of these sections later. */
2339 boolean
2340 elf_link_create_dynamic_sections (abfd, info)
2341 bfd *abfd;
2342 struct bfd_link_info *info;
2344 flagword flags;
2345 register asection *s;
2346 struct elf_link_hash_entry *h;
2347 struct elf_backend_data *bed;
2349 if (! is_elf_hash_table (info))
2350 return false;
2352 if (elf_hash_table (info)->dynamic_sections_created)
2353 return true;
2355 /* Make sure that all dynamic sections use the same input BFD. */
2356 if (elf_hash_table (info)->dynobj == NULL)
2357 elf_hash_table (info)->dynobj = abfd;
2358 else
2359 abfd = elf_hash_table (info)->dynobj;
2361 /* Note that we set the SEC_IN_MEMORY flag for all of these
2362 sections. */
2363 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2364 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2366 /* A dynamically linked executable has a .interp section, but a
2367 shared library does not. */
2368 if (! info->shared)
2370 s = bfd_make_section (abfd, ".interp");
2371 if (s == NULL
2372 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2373 return false;
2376 if (! info->traditional_format
2377 && info->hash->creator->flavour == bfd_target_elf_flavour)
2379 s = bfd_make_section (abfd, ".eh_frame_hdr");
2380 if (s == NULL
2381 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2382 || ! bfd_set_section_alignment (abfd, s, 2))
2383 return false;
2386 /* Create sections to hold version informations. These are removed
2387 if they are not needed. */
2388 s = bfd_make_section (abfd, ".gnu.version_d");
2389 if (s == NULL
2390 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2391 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2392 return false;
2394 s = bfd_make_section (abfd, ".gnu.version");
2395 if (s == NULL
2396 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2397 || ! bfd_set_section_alignment (abfd, s, 1))
2398 return false;
2400 s = bfd_make_section (abfd, ".gnu.version_r");
2401 if (s == NULL
2402 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2403 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2404 return false;
2406 s = bfd_make_section (abfd, ".dynsym");
2407 if (s == NULL
2408 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2409 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2410 return false;
2412 s = bfd_make_section (abfd, ".dynstr");
2413 if (s == NULL
2414 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2415 return false;
2417 /* Create a strtab to hold the dynamic symbol names. */
2418 if (elf_hash_table (info)->dynstr == NULL)
2420 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2421 if (elf_hash_table (info)->dynstr == NULL)
2422 return false;
2425 s = bfd_make_section (abfd, ".dynamic");
2426 if (s == NULL
2427 || ! bfd_set_section_flags (abfd, s, flags)
2428 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2429 return false;
2431 /* The special symbol _DYNAMIC is always set to the start of the
2432 .dynamic section. This call occurs before we have processed the
2433 symbols for any dynamic object, so we don't have to worry about
2434 overriding a dynamic definition. We could set _DYNAMIC in a
2435 linker script, but we only want to define it if we are, in fact,
2436 creating a .dynamic section. We don't want to define it if there
2437 is no .dynamic section, since on some ELF platforms the start up
2438 code examines it to decide how to initialize the process. */
2439 h = NULL;
2440 if (! (_bfd_generic_link_add_one_symbol
2441 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2442 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2443 (struct bfd_link_hash_entry **) &h)))
2444 return false;
2445 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2446 h->type = STT_OBJECT;
2448 if (info->shared
2449 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2450 return false;
2452 bed = get_elf_backend_data (abfd);
2454 s = bfd_make_section (abfd, ".hash");
2455 if (s == NULL
2456 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2457 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2458 return false;
2459 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2461 /* Let the backend create the rest of the sections. This lets the
2462 backend set the right flags. The backend will normally create
2463 the .got and .plt sections. */
2464 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2465 return false;
2467 elf_hash_table (info)->dynamic_sections_created = true;
2469 return true;
2472 /* Add an entry to the .dynamic table. */
2474 boolean
2475 elf_add_dynamic_entry (info, tag, val)
2476 struct bfd_link_info *info;
2477 bfd_vma tag;
2478 bfd_vma val;
2480 Elf_Internal_Dyn dyn;
2481 bfd *dynobj;
2482 asection *s;
2483 bfd_size_type newsize;
2484 bfd_byte *newcontents;
2486 if (! is_elf_hash_table (info))
2487 return false;
2489 dynobj = elf_hash_table (info)->dynobj;
2491 s = bfd_get_section_by_name (dynobj, ".dynamic");
2492 BFD_ASSERT (s != NULL);
2494 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2495 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2496 if (newcontents == NULL)
2497 return false;
2499 dyn.d_tag = tag;
2500 dyn.d_un.d_val = val;
2501 elf_swap_dyn_out (dynobj, &dyn,
2502 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2504 s->_raw_size = newsize;
2505 s->contents = newcontents;
2507 return true;
2510 /* Record a new local dynamic symbol. */
2512 boolean
2513 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2514 struct bfd_link_info *info;
2515 bfd *input_bfd;
2516 long input_indx;
2518 struct elf_link_local_dynamic_entry *entry;
2519 struct elf_link_hash_table *eht;
2520 struct elf_strtab_hash *dynstr;
2521 Elf_External_Sym esym;
2522 Elf_External_Sym_Shndx eshndx;
2523 Elf_External_Sym_Shndx *shndx;
2524 unsigned long dynstr_index;
2525 char *name;
2526 file_ptr pos;
2527 bfd_size_type amt;
2529 if (! is_elf_hash_table (info))
2530 return false;
2532 /* See if the entry exists already. */
2533 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2534 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2535 return true;
2537 entry = (struct elf_link_local_dynamic_entry *)
2538 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2539 if (entry == NULL)
2540 return false;
2542 /* Go find the symbol, so that we can find it's name. */
2543 amt = sizeof (Elf_External_Sym);
2544 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2545 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2546 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2547 return false;
2548 shndx = NULL;
2549 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2551 amt = sizeof (Elf_External_Sym_Shndx);
2552 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2553 pos += input_indx * amt;
2554 shndx = &eshndx;
2555 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2556 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2557 return false;
2559 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
2561 name = (bfd_elf_string_from_elf_section
2562 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2563 entry->isym.st_name));
2565 dynstr = elf_hash_table (info)->dynstr;
2566 if (dynstr == NULL)
2568 /* Create a strtab to hold the dynamic symbol names. */
2569 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2570 if (dynstr == NULL)
2571 return false;
2574 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2575 if (dynstr_index == (unsigned long) -1)
2576 return false;
2577 entry->isym.st_name = dynstr_index;
2579 eht = elf_hash_table (info);
2581 entry->next = eht->dynlocal;
2582 eht->dynlocal = entry;
2583 entry->input_bfd = input_bfd;
2584 entry->input_indx = input_indx;
2585 eht->dynsymcount++;
2587 /* Whatever binding the symbol had before, it's now local. */
2588 entry->isym.st_info
2589 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2591 /* The dynindx will be set at the end of size_dynamic_sections. */
2593 return true;
2596 /* Read and swap the relocs from the section indicated by SHDR. This
2597 may be either a REL or a RELA section. The relocations are
2598 translated into RELA relocations and stored in INTERNAL_RELOCS,
2599 which should have already been allocated to contain enough space.
2600 The EXTERNAL_RELOCS are a buffer where the external form of the
2601 relocations should be stored.
2603 Returns false if something goes wrong. */
2605 static boolean
2606 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2607 internal_relocs)
2608 bfd *abfd;
2609 Elf_Internal_Shdr *shdr;
2610 PTR external_relocs;
2611 Elf_Internal_Rela *internal_relocs;
2613 struct elf_backend_data *bed;
2614 bfd_size_type amt;
2616 /* If there aren't any relocations, that's OK. */
2617 if (!shdr)
2618 return true;
2620 /* Position ourselves at the start of the section. */
2621 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2622 return false;
2624 /* Read the relocations. */
2625 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2626 return false;
2628 bed = get_elf_backend_data (abfd);
2630 /* Convert the external relocations to the internal format. */
2631 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2633 Elf_External_Rel *erel;
2634 Elf_External_Rel *erelend;
2635 Elf_Internal_Rela *irela;
2636 Elf_Internal_Rel *irel;
2638 erel = (Elf_External_Rel *) external_relocs;
2639 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2640 irela = internal_relocs;
2641 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2642 irel = bfd_alloc (abfd, amt);
2643 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2645 unsigned int i;
2647 if (bed->s->swap_reloc_in)
2648 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2649 else
2650 elf_swap_reloc_in (abfd, erel, irel);
2652 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2654 irela[i].r_offset = irel[i].r_offset;
2655 irela[i].r_info = irel[i].r_info;
2656 irela[i].r_addend = 0;
2660 else
2662 Elf_External_Rela *erela;
2663 Elf_External_Rela *erelaend;
2664 Elf_Internal_Rela *irela;
2666 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2668 erela = (Elf_External_Rela *) external_relocs;
2669 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2670 irela = internal_relocs;
2671 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2673 if (bed->s->swap_reloca_in)
2674 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2675 else
2676 elf_swap_reloca_in (abfd, erela, irela);
2680 return true;
2683 /* Read and swap the relocs for a section O. They may have been
2684 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2685 not NULL, they are used as buffers to read into. They are known to
2686 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2687 the return value is allocated using either malloc or bfd_alloc,
2688 according to the KEEP_MEMORY argument. If O has two relocation
2689 sections (both REL and RELA relocations), then the REL_HDR
2690 relocations will appear first in INTERNAL_RELOCS, followed by the
2691 REL_HDR2 relocations. */
2693 Elf_Internal_Rela *
2694 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2695 keep_memory)
2696 bfd *abfd;
2697 asection *o;
2698 PTR external_relocs;
2699 Elf_Internal_Rela *internal_relocs;
2700 boolean keep_memory;
2702 Elf_Internal_Shdr *rel_hdr;
2703 PTR alloc1 = NULL;
2704 Elf_Internal_Rela *alloc2 = NULL;
2705 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2707 if (elf_section_data (o)->relocs != NULL)
2708 return elf_section_data (o)->relocs;
2710 if (o->reloc_count == 0)
2711 return NULL;
2713 rel_hdr = &elf_section_data (o)->rel_hdr;
2715 if (internal_relocs == NULL)
2717 bfd_size_type size;
2719 size = o->reloc_count;
2720 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2721 if (keep_memory)
2722 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2723 else
2724 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2725 if (internal_relocs == NULL)
2726 goto error_return;
2729 if (external_relocs == NULL)
2731 bfd_size_type size = rel_hdr->sh_size;
2733 if (elf_section_data (o)->rel_hdr2)
2734 size += elf_section_data (o)->rel_hdr2->sh_size;
2735 alloc1 = (PTR) bfd_malloc (size);
2736 if (alloc1 == NULL)
2737 goto error_return;
2738 external_relocs = alloc1;
2741 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2742 external_relocs,
2743 internal_relocs))
2744 goto error_return;
2745 if (!elf_link_read_relocs_from_section
2746 (abfd,
2747 elf_section_data (o)->rel_hdr2,
2748 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2749 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2750 * bed->s->int_rels_per_ext_rel)))
2751 goto error_return;
2753 /* Cache the results for next time, if we can. */
2754 if (keep_memory)
2755 elf_section_data (o)->relocs = internal_relocs;
2757 if (alloc1 != NULL)
2758 free (alloc1);
2760 /* Don't free alloc2, since if it was allocated we are passing it
2761 back (under the name of internal_relocs). */
2763 return internal_relocs;
2765 error_return:
2766 if (alloc1 != NULL)
2767 free (alloc1);
2768 if (alloc2 != NULL)
2769 free (alloc2);
2770 return NULL;
2773 /* Record an assignment to a symbol made by a linker script. We need
2774 this in case some dynamic object refers to this symbol. */
2776 boolean
2777 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2778 bfd *output_bfd ATTRIBUTE_UNUSED;
2779 struct bfd_link_info *info;
2780 const char *name;
2781 boolean provide;
2783 struct elf_link_hash_entry *h;
2785 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2786 return true;
2788 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2789 if (h == NULL)
2790 return false;
2792 if (h->root.type == bfd_link_hash_new)
2793 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2795 /* If this symbol is being provided by the linker script, and it is
2796 currently defined by a dynamic object, but not by a regular
2797 object, then mark it as undefined so that the generic linker will
2798 force the correct value. */
2799 if (provide
2800 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2801 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2802 h->root.type = bfd_link_hash_undefined;
2804 /* If this symbol is not being provided by the linker script, and it is
2805 currently defined by a dynamic object, but not by a regular object,
2806 then clear out any version information because the symbol will not be
2807 associated with the dynamic object any more. */
2808 if (!provide
2809 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2810 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2811 h->verinfo.verdef = NULL;
2813 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2815 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2816 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2817 || info->shared)
2818 && h->dynindx == -1)
2820 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2821 return false;
2823 /* If this is a weak defined symbol, and we know a corresponding
2824 real symbol from the same dynamic object, make sure the real
2825 symbol is also made into a dynamic symbol. */
2826 if (h->weakdef != NULL
2827 && h->weakdef->dynindx == -1)
2829 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2830 return false;
2834 return true;
2837 /* This structure is used to pass information to
2838 elf_link_assign_sym_version. */
2840 struct elf_assign_sym_version_info
2842 /* Output BFD. */
2843 bfd *output_bfd;
2844 /* General link information. */
2845 struct bfd_link_info *info;
2846 /* Version tree. */
2847 struct bfd_elf_version_tree *verdefs;
2848 /* Whether we had a failure. */
2849 boolean failed;
2852 /* This structure is used to pass information to
2853 elf_link_find_version_dependencies. */
2855 struct elf_find_verdep_info
2857 /* Output BFD. */
2858 bfd *output_bfd;
2859 /* General link information. */
2860 struct bfd_link_info *info;
2861 /* The number of dependencies. */
2862 unsigned int vers;
2863 /* Whether we had a failure. */
2864 boolean failed;
2867 /* Array used to determine the number of hash table buckets to use
2868 based on the number of symbols there are. If there are fewer than
2869 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2870 fewer than 37 we use 17 buckets, and so forth. We never use more
2871 than 32771 buckets. */
2873 static const size_t elf_buckets[] =
2875 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2876 16411, 32771, 0
2879 /* Compute bucket count for hashing table. We do not use a static set
2880 of possible tables sizes anymore. Instead we determine for all
2881 possible reasonable sizes of the table the outcome (i.e., the
2882 number of collisions etc) and choose the best solution. The
2883 weighting functions are not too simple to allow the table to grow
2884 without bounds. Instead one of the weighting factors is the size.
2885 Therefore the result is always a good payoff between few collisions
2886 (= short chain lengths) and table size. */
2887 static size_t
2888 compute_bucket_count (info)
2889 struct bfd_link_info *info;
2891 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2892 size_t best_size = 0;
2893 unsigned long int *hashcodes;
2894 unsigned long int *hashcodesp;
2895 unsigned long int i;
2896 bfd_size_type amt;
2898 /* Compute the hash values for all exported symbols. At the same
2899 time store the values in an array so that we could use them for
2900 optimizations. */
2901 amt = dynsymcount;
2902 amt *= sizeof (unsigned long int);
2903 hashcodes = (unsigned long int *) bfd_malloc (amt);
2904 if (hashcodes == NULL)
2905 return 0;
2906 hashcodesp = hashcodes;
2908 /* Put all hash values in HASHCODES. */
2909 elf_link_hash_traverse (elf_hash_table (info),
2910 elf_collect_hash_codes, &hashcodesp);
2912 /* We have a problem here. The following code to optimize the table
2913 size requires an integer type with more the 32 bits. If
2914 BFD_HOST_U_64_BIT is set we know about such a type. */
2915 #ifdef BFD_HOST_U_64_BIT
2916 if (info->optimize == true)
2918 unsigned long int nsyms = hashcodesp - hashcodes;
2919 size_t minsize;
2920 size_t maxsize;
2921 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2922 unsigned long int *counts ;
2924 /* Possible optimization parameters: if we have NSYMS symbols we say
2925 that the hashing table must at least have NSYMS/4 and at most
2926 2*NSYMS buckets. */
2927 minsize = nsyms / 4;
2928 if (minsize == 0)
2929 minsize = 1;
2930 best_size = maxsize = nsyms * 2;
2932 /* Create array where we count the collisions in. We must use bfd_malloc
2933 since the size could be large. */
2934 amt = maxsize;
2935 amt *= sizeof (unsigned long int);
2936 counts = (unsigned long int *) bfd_malloc (amt);
2937 if (counts == NULL)
2939 free (hashcodes);
2940 return 0;
2943 /* Compute the "optimal" size for the hash table. The criteria is a
2944 minimal chain length. The minor criteria is (of course) the size
2945 of the table. */
2946 for (i = minsize; i < maxsize; ++i)
2948 /* Walk through the array of hashcodes and count the collisions. */
2949 BFD_HOST_U_64_BIT max;
2950 unsigned long int j;
2951 unsigned long int fact;
2953 memset (counts, '\0', i * sizeof (unsigned long int));
2955 /* Determine how often each hash bucket is used. */
2956 for (j = 0; j < nsyms; ++j)
2957 ++counts[hashcodes[j] % i];
2959 /* For the weight function we need some information about the
2960 pagesize on the target. This is information need not be 100%
2961 accurate. Since this information is not available (so far) we
2962 define it here to a reasonable default value. If it is crucial
2963 to have a better value some day simply define this value. */
2964 # ifndef BFD_TARGET_PAGESIZE
2965 # define BFD_TARGET_PAGESIZE (4096)
2966 # endif
2968 /* We in any case need 2 + NSYMS entries for the size values and
2969 the chains. */
2970 max = (2 + nsyms) * (ARCH_SIZE / 8);
2972 # if 1
2973 /* Variant 1: optimize for short chains. We add the squares
2974 of all the chain lengths (which favous many small chain
2975 over a few long chains). */
2976 for (j = 0; j < i; ++j)
2977 max += counts[j] * counts[j];
2979 /* This adds penalties for the overall size of the table. */
2980 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2981 max *= fact * fact;
2982 # else
2983 /* Variant 2: Optimize a lot more for small table. Here we
2984 also add squares of the size but we also add penalties for
2985 empty slots (the +1 term). */
2986 for (j = 0; j < i; ++j)
2987 max += (1 + counts[j]) * (1 + counts[j]);
2989 /* The overall size of the table is considered, but not as
2990 strong as in variant 1, where it is squared. */
2991 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2992 max *= fact;
2993 # endif
2995 /* Compare with current best results. */
2996 if (max < best_chlen)
2998 best_chlen = max;
2999 best_size = i;
3003 free (counts);
3005 else
3006 #endif /* defined (BFD_HOST_U_64_BIT) */
3008 /* This is the fallback solution if no 64bit type is available or if we
3009 are not supposed to spend much time on optimizations. We select the
3010 bucket count using a fixed set of numbers. */
3011 for (i = 0; elf_buckets[i] != 0; i++)
3013 best_size = elf_buckets[i];
3014 if (dynsymcount < elf_buckets[i + 1])
3015 break;
3019 /* Free the arrays we needed. */
3020 free (hashcodes);
3022 return best_size;
3025 /* Set up the sizes and contents of the ELF dynamic sections. This is
3026 called by the ELF linker emulation before_allocation routine. We
3027 must set the sizes of the sections before the linker sets the
3028 addresses of the various sections. */
3030 boolean
3031 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3032 filter_shlib,
3033 auxiliary_filters, info, sinterpptr,
3034 verdefs)
3035 bfd *output_bfd;
3036 const char *soname;
3037 const char *rpath;
3038 const char *filter_shlib;
3039 const char * const *auxiliary_filters;
3040 struct bfd_link_info *info;
3041 asection **sinterpptr;
3042 struct bfd_elf_version_tree *verdefs;
3044 bfd_size_type soname_indx;
3045 bfd *dynobj;
3046 struct elf_backend_data *bed;
3047 struct elf_assign_sym_version_info asvinfo;
3049 *sinterpptr = NULL;
3051 soname_indx = (bfd_size_type) -1;
3053 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3054 return true;
3056 if (! is_elf_hash_table (info))
3057 return false;
3059 /* Any syms created from now on start with -1 in
3060 got.refcount/offset and plt.refcount/offset. */
3061 elf_hash_table (info)->init_refcount = -1;
3063 /* The backend may have to create some sections regardless of whether
3064 we're dynamic or not. */
3065 bed = get_elf_backend_data (output_bfd);
3066 if (bed->elf_backend_always_size_sections
3067 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3068 return false;
3070 dynobj = elf_hash_table (info)->dynobj;
3072 /* If there were no dynamic objects in the link, there is nothing to
3073 do here. */
3074 if (dynobj == NULL)
3075 return true;
3077 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3078 return false;
3080 if (elf_hash_table (info)->dynamic_sections_created)
3082 struct elf_info_failed eif;
3083 struct elf_link_hash_entry *h;
3084 asection *dynstr;
3086 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3087 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3089 if (soname != NULL)
3091 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3092 soname, true);
3093 if (soname_indx == (bfd_size_type) -1
3094 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3095 soname_indx))
3096 return false;
3099 if (info->symbolic)
3101 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3102 (bfd_vma) 0))
3103 return false;
3104 info->flags |= DF_SYMBOLIC;
3107 if (rpath != NULL)
3109 bfd_size_type indx;
3111 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3112 true);
3113 if (info->new_dtags)
3114 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3115 if (indx == (bfd_size_type) -1
3116 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3117 || (info->new_dtags
3118 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3119 indx)))
3120 return false;
3123 if (filter_shlib != NULL)
3125 bfd_size_type indx;
3127 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3128 filter_shlib, true);
3129 if (indx == (bfd_size_type) -1
3130 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3131 return false;
3134 if (auxiliary_filters != NULL)
3136 const char * const *p;
3138 for (p = auxiliary_filters; *p != NULL; p++)
3140 bfd_size_type indx;
3142 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3143 *p, true);
3144 if (indx == (bfd_size_type) -1
3145 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3146 indx))
3147 return false;
3151 eif.info = info;
3152 eif.verdefs = verdefs;
3153 eif.failed = false;
3155 /* If we are supposed to export all symbols into the dynamic symbol
3156 table (this is not the normal case), then do so. */
3157 if (info->export_dynamic)
3159 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3160 (PTR) &eif);
3161 if (eif.failed)
3162 return false;
3165 /* Attach all the symbols to their version information. */
3166 asvinfo.output_bfd = output_bfd;
3167 asvinfo.info = info;
3168 asvinfo.verdefs = verdefs;
3169 asvinfo.failed = false;
3171 elf_link_hash_traverse (elf_hash_table (info),
3172 elf_link_assign_sym_version,
3173 (PTR) &asvinfo);
3174 if (asvinfo.failed)
3175 return false;
3177 /* Find all symbols which were defined in a dynamic object and make
3178 the backend pick a reasonable value for them. */
3179 elf_link_hash_traverse (elf_hash_table (info),
3180 elf_adjust_dynamic_symbol,
3181 (PTR) &eif);
3182 if (eif.failed)
3183 return false;
3185 /* Add some entries to the .dynamic section. We fill in some of the
3186 values later, in elf_bfd_final_link, but we must add the entries
3187 now so that we know the final size of the .dynamic section. */
3189 /* If there are initialization and/or finalization functions to
3190 call then add the corresponding DT_INIT/DT_FINI entries. */
3191 h = (info->init_function
3192 ? elf_link_hash_lookup (elf_hash_table (info),
3193 info->init_function, false,
3194 false, false)
3195 : NULL);
3196 if (h != NULL
3197 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3198 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3200 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3201 return false;
3203 h = (info->fini_function
3204 ? elf_link_hash_lookup (elf_hash_table (info),
3205 info->fini_function, false,
3206 false, false)
3207 : NULL);
3208 if (h != NULL
3209 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3210 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3212 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3213 return false;
3216 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3218 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3219 if (info->shared)
3221 bfd *sub;
3222 asection *o;
3224 for (sub = info->input_bfds; sub != NULL;
3225 sub = sub->link_next)
3226 for (o = sub->sections; o != NULL; o = o->next)
3227 if (elf_section_data (o)->this_hdr.sh_type
3228 == SHT_PREINIT_ARRAY)
3230 (*_bfd_error_handler)
3231 (_("%s: .preinit_array section is not allowed in DSO"),
3232 bfd_archive_filename (sub));
3233 break;
3236 bfd_set_error (bfd_error_nonrepresentable_section);
3237 return false;
3240 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3241 (bfd_vma) 0)
3242 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3243 (bfd_vma) 0))
3244 return false;
3246 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3248 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3249 (bfd_vma) 0)
3250 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3251 (bfd_vma) 0))
3252 return false;
3254 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3256 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3257 (bfd_vma) 0)
3258 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3259 (bfd_vma) 0))
3260 return false;
3263 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3264 /* If .dynstr is excluded from the link, we don't want any of
3265 these tags. Strictly, we should be checking each section
3266 individually; This quick check covers for the case where
3267 someone does a /DISCARD/ : { *(*) }. */
3268 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3270 bfd_size_type strsize;
3272 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3273 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3274 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3275 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3276 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3277 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3278 (bfd_vma) sizeof (Elf_External_Sym)))
3279 return false;
3283 /* The backend must work out the sizes of all the other dynamic
3284 sections. */
3285 if (bed->elf_backend_size_dynamic_sections
3286 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3287 return false;
3289 if (elf_hash_table (info)->dynamic_sections_created)
3291 bfd_size_type dynsymcount;
3292 asection *s;
3293 size_t bucketcount = 0;
3294 size_t hash_entry_size;
3295 unsigned int dtagcount;
3297 /* Set up the version definition section. */
3298 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3299 BFD_ASSERT (s != NULL);
3301 /* We may have created additional version definitions if we are
3302 just linking a regular application. */
3303 verdefs = asvinfo.verdefs;
3305 /* Skip anonymous version tag. */
3306 if (verdefs != NULL && verdefs->vernum == 0)
3307 verdefs = verdefs->next;
3309 if (verdefs == NULL)
3310 _bfd_strip_section_from_output (info, s);
3311 else
3313 unsigned int cdefs;
3314 bfd_size_type size;
3315 struct bfd_elf_version_tree *t;
3316 bfd_byte *p;
3317 Elf_Internal_Verdef def;
3318 Elf_Internal_Verdaux defaux;
3320 cdefs = 0;
3321 size = 0;
3323 /* Make space for the base version. */
3324 size += sizeof (Elf_External_Verdef);
3325 size += sizeof (Elf_External_Verdaux);
3326 ++cdefs;
3328 for (t = verdefs; t != NULL; t = t->next)
3330 struct bfd_elf_version_deps *n;
3332 size += sizeof (Elf_External_Verdef);
3333 size += sizeof (Elf_External_Verdaux);
3334 ++cdefs;
3336 for (n = t->deps; n != NULL; n = n->next)
3337 size += sizeof (Elf_External_Verdaux);
3340 s->_raw_size = size;
3341 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3342 if (s->contents == NULL && s->_raw_size != 0)
3343 return false;
3345 /* Fill in the version definition section. */
3347 p = s->contents;
3349 def.vd_version = VER_DEF_CURRENT;
3350 def.vd_flags = VER_FLG_BASE;
3351 def.vd_ndx = 1;
3352 def.vd_cnt = 1;
3353 def.vd_aux = sizeof (Elf_External_Verdef);
3354 def.vd_next = (sizeof (Elf_External_Verdef)
3355 + sizeof (Elf_External_Verdaux));
3357 if (soname_indx != (bfd_size_type) -1)
3359 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3360 soname_indx);
3361 def.vd_hash = bfd_elf_hash (soname);
3362 defaux.vda_name = soname_indx;
3364 else
3366 const char *name;
3367 bfd_size_type indx;
3369 name = basename (output_bfd->filename);
3370 def.vd_hash = bfd_elf_hash (name);
3371 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3372 name, false);
3373 if (indx == (bfd_size_type) -1)
3374 return false;
3375 defaux.vda_name = indx;
3377 defaux.vda_next = 0;
3379 _bfd_elf_swap_verdef_out (output_bfd, &def,
3380 (Elf_External_Verdef *) p);
3381 p += sizeof (Elf_External_Verdef);
3382 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3383 (Elf_External_Verdaux *) p);
3384 p += sizeof (Elf_External_Verdaux);
3386 for (t = verdefs; t != NULL; t = t->next)
3388 unsigned int cdeps;
3389 struct bfd_elf_version_deps *n;
3390 struct elf_link_hash_entry *h;
3392 cdeps = 0;
3393 for (n = t->deps; n != NULL; n = n->next)
3394 ++cdeps;
3396 /* Add a symbol representing this version. */
3397 h = NULL;
3398 if (! (_bfd_generic_link_add_one_symbol
3399 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3400 (bfd_vma) 0, (const char *) NULL, false,
3401 get_elf_backend_data (dynobj)->collect,
3402 (struct bfd_link_hash_entry **) &h)))
3403 return false;
3404 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3405 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3406 h->type = STT_OBJECT;
3407 h->verinfo.vertree = t;
3409 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3410 return false;
3412 def.vd_version = VER_DEF_CURRENT;
3413 def.vd_flags = 0;
3414 if (t->globals == NULL && t->locals == NULL && ! t->used)
3415 def.vd_flags |= VER_FLG_WEAK;
3416 def.vd_ndx = t->vernum + 1;
3417 def.vd_cnt = cdeps + 1;
3418 def.vd_hash = bfd_elf_hash (t->name);
3419 def.vd_aux = sizeof (Elf_External_Verdef);
3420 if (t->next != NULL)
3421 def.vd_next = (sizeof (Elf_External_Verdef)
3422 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3423 else
3424 def.vd_next = 0;
3426 _bfd_elf_swap_verdef_out (output_bfd, &def,
3427 (Elf_External_Verdef *) p);
3428 p += sizeof (Elf_External_Verdef);
3430 defaux.vda_name = h->dynstr_index;
3431 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3432 h->dynstr_index);
3433 if (t->deps == NULL)
3434 defaux.vda_next = 0;
3435 else
3436 defaux.vda_next = sizeof (Elf_External_Verdaux);
3437 t->name_indx = defaux.vda_name;
3439 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3440 (Elf_External_Verdaux *) p);
3441 p += sizeof (Elf_External_Verdaux);
3443 for (n = t->deps; n != NULL; n = n->next)
3445 if (n->version_needed == NULL)
3447 /* This can happen if there was an error in the
3448 version script. */
3449 defaux.vda_name = 0;
3451 else
3453 defaux.vda_name = n->version_needed->name_indx;
3454 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3455 defaux.vda_name);
3457 if (n->next == NULL)
3458 defaux.vda_next = 0;
3459 else
3460 defaux.vda_next = sizeof (Elf_External_Verdaux);
3462 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3463 (Elf_External_Verdaux *) p);
3464 p += sizeof (Elf_External_Verdaux);
3468 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3469 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3470 (bfd_vma) cdefs))
3471 return false;
3473 elf_tdata (output_bfd)->cverdefs = cdefs;
3476 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
3478 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3479 return false;
3482 if (info->flags_1)
3484 if (! info->shared)
3485 info->flags_1 &= ~ (DF_1_INITFIRST
3486 | DF_1_NODELETE
3487 | DF_1_NOOPEN);
3488 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3489 info->flags_1))
3490 return false;
3493 /* Work out the size of the version reference section. */
3495 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3496 BFD_ASSERT (s != NULL);
3498 struct elf_find_verdep_info sinfo;
3500 sinfo.output_bfd = output_bfd;
3501 sinfo.info = info;
3502 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3503 if (sinfo.vers == 0)
3504 sinfo.vers = 1;
3505 sinfo.failed = false;
3507 elf_link_hash_traverse (elf_hash_table (info),
3508 elf_link_find_version_dependencies,
3509 (PTR) &sinfo);
3511 if (elf_tdata (output_bfd)->verref == NULL)
3512 _bfd_strip_section_from_output (info, s);
3513 else
3515 Elf_Internal_Verneed *t;
3516 unsigned int size;
3517 unsigned int crefs;
3518 bfd_byte *p;
3520 /* Build the version definition section. */
3521 size = 0;
3522 crefs = 0;
3523 for (t = elf_tdata (output_bfd)->verref;
3524 t != NULL;
3525 t = t->vn_nextref)
3527 Elf_Internal_Vernaux *a;
3529 size += sizeof (Elf_External_Verneed);
3530 ++crefs;
3531 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3532 size += sizeof (Elf_External_Vernaux);
3535 s->_raw_size = size;
3536 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3537 if (s->contents == NULL)
3538 return false;
3540 p = s->contents;
3541 for (t = elf_tdata (output_bfd)->verref;
3542 t != NULL;
3543 t = t->vn_nextref)
3545 unsigned int caux;
3546 Elf_Internal_Vernaux *a;
3547 bfd_size_type indx;
3549 caux = 0;
3550 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3551 ++caux;
3553 t->vn_version = VER_NEED_CURRENT;
3554 t->vn_cnt = caux;
3555 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3556 elf_dt_name (t->vn_bfd) != NULL
3557 ? elf_dt_name (t->vn_bfd)
3558 : basename (t->vn_bfd->filename),
3559 false);
3560 if (indx == (bfd_size_type) -1)
3561 return false;
3562 t->vn_file = indx;
3563 t->vn_aux = sizeof (Elf_External_Verneed);
3564 if (t->vn_nextref == NULL)
3565 t->vn_next = 0;
3566 else
3567 t->vn_next = (sizeof (Elf_External_Verneed)
3568 + caux * sizeof (Elf_External_Vernaux));
3570 _bfd_elf_swap_verneed_out (output_bfd, t,
3571 (Elf_External_Verneed *) p);
3572 p += sizeof (Elf_External_Verneed);
3574 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3576 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3577 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3578 a->vna_nodename, false);
3579 if (indx == (bfd_size_type) -1)
3580 return false;
3581 a->vna_name = indx;
3582 if (a->vna_nextptr == NULL)
3583 a->vna_next = 0;
3584 else
3585 a->vna_next = sizeof (Elf_External_Vernaux);
3587 _bfd_elf_swap_vernaux_out (output_bfd, a,
3588 (Elf_External_Vernaux *) p);
3589 p += sizeof (Elf_External_Vernaux);
3593 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3594 (bfd_vma) 0)
3595 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3596 (bfd_vma) crefs))
3597 return false;
3599 elf_tdata (output_bfd)->cverrefs = crefs;
3603 /* Assign dynsym indicies. In a shared library we generate a
3604 section symbol for each output section, which come first.
3605 Next come all of the back-end allocated local dynamic syms,
3606 followed by the rest of the global symbols. */
3608 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3610 /* Work out the size of the symbol version section. */
3611 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3612 BFD_ASSERT (s != NULL);
3613 if (dynsymcount == 0
3614 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3616 _bfd_strip_section_from_output (info, s);
3617 /* The DYNSYMCOUNT might have changed if we were going to
3618 output a dynamic symbol table entry for S. */
3619 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3621 else
3623 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3624 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3625 if (s->contents == NULL)
3626 return false;
3628 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3629 return false;
3632 /* Set the size of the .dynsym and .hash sections. We counted
3633 the number of dynamic symbols in elf_link_add_object_symbols.
3634 We will build the contents of .dynsym and .hash when we build
3635 the final symbol table, because until then we do not know the
3636 correct value to give the symbols. We built the .dynstr
3637 section as we went along in elf_link_add_object_symbols. */
3638 s = bfd_get_section_by_name (dynobj, ".dynsym");
3639 BFD_ASSERT (s != NULL);
3640 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3641 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3642 if (s->contents == NULL && s->_raw_size != 0)
3643 return false;
3645 if (dynsymcount != 0)
3647 Elf_Internal_Sym isym;
3649 /* The first entry in .dynsym is a dummy symbol. */
3650 isym.st_value = 0;
3651 isym.st_size = 0;
3652 isym.st_name = 0;
3653 isym.st_info = 0;
3654 isym.st_other = 0;
3655 isym.st_shndx = 0;
3656 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3659 /* Compute the size of the hashing table. As a side effect this
3660 computes the hash values for all the names we export. */
3661 bucketcount = compute_bucket_count (info);
3663 s = bfd_get_section_by_name (dynobj, ".hash");
3664 BFD_ASSERT (s != NULL);
3665 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3666 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3667 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3668 if (s->contents == NULL)
3669 return false;
3670 memset (s->contents, 0, (size_t) s->_raw_size);
3672 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3673 s->contents);
3674 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3675 s->contents + hash_entry_size);
3677 elf_hash_table (info)->bucketcount = bucketcount;
3679 s = bfd_get_section_by_name (dynobj, ".dynstr");
3680 BFD_ASSERT (s != NULL);
3682 elf_finalize_dynstr (output_bfd, info);
3684 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3686 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3687 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3688 return false;
3691 return true;
3694 /* This function is used to adjust offsets into .dynstr for
3695 dynamic symbols. This is called via elf_link_hash_traverse. */
3697 static boolean elf_adjust_dynstr_offsets
3698 PARAMS ((struct elf_link_hash_entry *, PTR));
3700 static boolean
3701 elf_adjust_dynstr_offsets (h, data)
3702 struct elf_link_hash_entry *h;
3703 PTR data;
3705 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3707 if (h->root.type == bfd_link_hash_warning)
3708 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3710 if (h->dynindx != -1)
3711 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3712 return true;
3715 /* Assign string offsets in .dynstr, update all structures referencing
3716 them. */
3718 static boolean
3719 elf_finalize_dynstr (output_bfd, info)
3720 bfd *output_bfd;
3721 struct bfd_link_info *info;
3723 struct elf_link_local_dynamic_entry *entry;
3724 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3725 bfd *dynobj = elf_hash_table (info)->dynobj;
3726 asection *sdyn;
3727 bfd_size_type size;
3728 Elf_External_Dyn *dyncon, *dynconend;
3730 _bfd_elf_strtab_finalize (dynstr);
3731 size = _bfd_elf_strtab_size (dynstr);
3733 /* Update all .dynamic entries referencing .dynstr strings. */
3734 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3735 BFD_ASSERT (sdyn != NULL);
3737 dyncon = (Elf_External_Dyn *) sdyn->contents;
3738 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3739 sdyn->_raw_size);
3740 for (; dyncon < dynconend; dyncon++)
3742 Elf_Internal_Dyn dyn;
3744 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3745 switch (dyn.d_tag)
3747 case DT_STRSZ:
3748 dyn.d_un.d_val = size;
3749 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3750 break;
3751 case DT_NEEDED:
3752 case DT_SONAME:
3753 case DT_RPATH:
3754 case DT_RUNPATH:
3755 case DT_FILTER:
3756 case DT_AUXILIARY:
3757 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3758 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3759 break;
3760 default:
3761 break;
3765 /* Now update local dynamic symbols. */
3766 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3767 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3768 entry->isym.st_name);
3770 /* And the rest of dynamic symbols. */
3771 elf_link_hash_traverse (elf_hash_table (info),
3772 elf_adjust_dynstr_offsets, dynstr);
3774 /* Adjust version definitions. */
3775 if (elf_tdata (output_bfd)->cverdefs)
3777 asection *s;
3778 bfd_byte *p;
3779 bfd_size_type i;
3780 Elf_Internal_Verdef def;
3781 Elf_Internal_Verdaux defaux;
3783 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3784 p = (bfd_byte *) s->contents;
3787 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3788 &def);
3789 p += sizeof (Elf_External_Verdef);
3790 for (i = 0; i < def.vd_cnt; ++i)
3792 _bfd_elf_swap_verdaux_in (output_bfd,
3793 (Elf_External_Verdaux *) p, &defaux);
3794 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3795 defaux.vda_name);
3796 _bfd_elf_swap_verdaux_out (output_bfd,
3797 &defaux, (Elf_External_Verdaux *) p);
3798 p += sizeof (Elf_External_Verdaux);
3801 while (def.vd_next);
3804 /* Adjust version references. */
3805 if (elf_tdata (output_bfd)->verref)
3807 asection *s;
3808 bfd_byte *p;
3809 bfd_size_type i;
3810 Elf_Internal_Verneed need;
3811 Elf_Internal_Vernaux needaux;
3813 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3814 p = (bfd_byte *) s->contents;
3817 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3818 &need);
3819 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3820 _bfd_elf_swap_verneed_out (output_bfd, &need,
3821 (Elf_External_Verneed *) p);
3822 p += sizeof (Elf_External_Verneed);
3823 for (i = 0; i < need.vn_cnt; ++i)
3825 _bfd_elf_swap_vernaux_in (output_bfd,
3826 (Elf_External_Vernaux *) p, &needaux);
3827 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3828 needaux.vna_name);
3829 _bfd_elf_swap_vernaux_out (output_bfd,
3830 &needaux,
3831 (Elf_External_Vernaux *) p);
3832 p += sizeof (Elf_External_Vernaux);
3835 while (need.vn_next);
3838 return true;
3841 /* Fix up the flags for a symbol. This handles various cases which
3842 can only be fixed after all the input files are seen. This is
3843 currently called by both adjust_dynamic_symbol and
3844 assign_sym_version, which is unnecessary but perhaps more robust in
3845 the face of future changes. */
3847 static boolean
3848 elf_fix_symbol_flags (h, eif)
3849 struct elf_link_hash_entry *h;
3850 struct elf_info_failed *eif;
3852 /* If this symbol was mentioned in a non-ELF file, try to set
3853 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3854 permit a non-ELF file to correctly refer to a symbol defined in
3855 an ELF dynamic object. */
3856 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3858 while (h->root.type == bfd_link_hash_indirect)
3859 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3861 if (h->root.type != bfd_link_hash_defined
3862 && h->root.type != bfd_link_hash_defweak)
3863 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3864 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3865 else
3867 if (h->root.u.def.section->owner != NULL
3868 && (bfd_get_flavour (h->root.u.def.section->owner)
3869 == bfd_target_elf_flavour))
3870 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3871 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3872 else
3873 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3876 if (h->dynindx == -1
3877 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3878 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3880 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3882 eif->failed = true;
3883 return false;
3887 else
3889 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3890 was first seen in a non-ELF file. Fortunately, if the symbol
3891 was first seen in an ELF file, we're probably OK unless the
3892 symbol was defined in a non-ELF file. Catch that case here.
3893 FIXME: We're still in trouble if the symbol was first seen in
3894 a dynamic object, and then later in a non-ELF regular object. */
3895 if ((h->root.type == bfd_link_hash_defined
3896 || h->root.type == bfd_link_hash_defweak)
3897 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3898 && (h->root.u.def.section->owner != NULL
3899 ? (bfd_get_flavour (h->root.u.def.section->owner)
3900 != bfd_target_elf_flavour)
3901 : (bfd_is_abs_section (h->root.u.def.section)
3902 && (h->elf_link_hash_flags
3903 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3904 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3907 /* If this is a final link, and the symbol was defined as a common
3908 symbol in a regular object file, and there was no definition in
3909 any dynamic object, then the linker will have allocated space for
3910 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3911 flag will not have been set. */
3912 if (h->root.type == bfd_link_hash_defined
3913 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3914 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3915 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3916 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3917 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3919 /* If -Bsymbolic was used (which means to bind references to global
3920 symbols to the definition within the shared object), and this
3921 symbol was defined in a regular object, then it actually doesn't
3922 need a PLT entry, and we can accomplish that by forcing it local.
3923 Likewise, if the symbol has hidden or internal visibility.
3924 FIXME: It might be that we also do not need a PLT for other
3925 non-hidden visibilities, but we would have to tell that to the
3926 backend specifically; we can't just clear PLT-related data here. */
3927 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3928 && eif->info->shared
3929 && is_elf_hash_table (eif->info)
3930 && (eif->info->symbolic
3931 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3932 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3933 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3935 struct elf_backend_data *bed;
3936 boolean force_local;
3938 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3940 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3941 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3942 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3945 /* If this is a weak defined symbol in a dynamic object, and we know
3946 the real definition in the dynamic object, copy interesting flags
3947 over to the real definition. */
3948 if (h->weakdef != NULL)
3950 struct elf_link_hash_entry *weakdef;
3952 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3953 || h->root.type == bfd_link_hash_defweak);
3954 weakdef = h->weakdef;
3955 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3956 || weakdef->root.type == bfd_link_hash_defweak);
3957 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3959 /* If the real definition is defined by a regular object file,
3960 don't do anything special. See the longer description in
3961 elf_adjust_dynamic_symbol, below. */
3962 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3963 h->weakdef = NULL;
3964 else
3966 struct elf_backend_data *bed;
3968 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3969 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3973 return true;
3976 /* Make the backend pick a good value for a dynamic symbol. This is
3977 called via elf_link_hash_traverse, and also calls itself
3978 recursively. */
3980 static boolean
3981 elf_adjust_dynamic_symbol (h, data)
3982 struct elf_link_hash_entry *h;
3983 PTR data;
3985 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3986 bfd *dynobj;
3987 struct elf_backend_data *bed;
3989 if (h->root.type == bfd_link_hash_warning)
3991 h->plt.offset = (bfd_vma) -1;
3992 h->got.offset = (bfd_vma) -1;
3994 /* When warning symbols are created, they **replace** the "real"
3995 entry in the hash table, thus we never get to see the real
3996 symbol in a hash traversal. So look at it now. */
3997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4000 /* Ignore indirect symbols. These are added by the versioning code. */
4001 if (h->root.type == bfd_link_hash_indirect)
4002 return true;
4004 if (! is_elf_hash_table (eif->info))
4005 return false;
4007 /* Fix the symbol flags. */
4008 if (! elf_fix_symbol_flags (h, eif))
4009 return false;
4011 /* If this symbol does not require a PLT entry, and it is not
4012 defined by a dynamic object, or is not referenced by a regular
4013 object, ignore it. We do have to handle a weak defined symbol,
4014 even if no regular object refers to it, if we decided to add it
4015 to the dynamic symbol table. FIXME: Do we normally need to worry
4016 about symbols which are defined by one dynamic object and
4017 referenced by another one? */
4018 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4019 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4020 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4021 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4022 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4024 h->plt.offset = (bfd_vma) -1;
4025 return true;
4028 /* If we've already adjusted this symbol, don't do it again. This
4029 can happen via a recursive call. */
4030 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4031 return true;
4033 /* Don't look at this symbol again. Note that we must set this
4034 after checking the above conditions, because we may look at a
4035 symbol once, decide not to do anything, and then get called
4036 recursively later after REF_REGULAR is set below. */
4037 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4039 /* If this is a weak definition, and we know a real definition, and
4040 the real symbol is not itself defined by a regular object file,
4041 then get a good value for the real definition. We handle the
4042 real symbol first, for the convenience of the backend routine.
4044 Note that there is a confusing case here. If the real definition
4045 is defined by a regular object file, we don't get the real symbol
4046 from the dynamic object, but we do get the weak symbol. If the
4047 processor backend uses a COPY reloc, then if some routine in the
4048 dynamic object changes the real symbol, we will not see that
4049 change in the corresponding weak symbol. This is the way other
4050 ELF linkers work as well, and seems to be a result of the shared
4051 library model.
4053 I will clarify this issue. Most SVR4 shared libraries define the
4054 variable _timezone and define timezone as a weak synonym. The
4055 tzset call changes _timezone. If you write
4056 extern int timezone;
4057 int _timezone = 5;
4058 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4059 you might expect that, since timezone is a synonym for _timezone,
4060 the same number will print both times. However, if the processor
4061 backend uses a COPY reloc, then actually timezone will be copied
4062 into your process image, and, since you define _timezone
4063 yourself, _timezone will not. Thus timezone and _timezone will
4064 wind up at different memory locations. The tzset call will set
4065 _timezone, leaving timezone unchanged. */
4067 if (h->weakdef != NULL)
4069 /* If we get to this point, we know there is an implicit
4070 reference by a regular object file via the weak symbol H.
4071 FIXME: Is this really true? What if the traversal finds
4072 H->WEAKDEF before it finds H? */
4073 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4075 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4076 return false;
4079 /* If a symbol has no type and no size and does not require a PLT
4080 entry, then we are probably about to do the wrong thing here: we
4081 are probably going to create a COPY reloc for an empty object.
4082 This case can arise when a shared object is built with assembly
4083 code, and the assembly code fails to set the symbol type. */
4084 if (h->size == 0
4085 && h->type == STT_NOTYPE
4086 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4087 (*_bfd_error_handler)
4088 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4089 h->root.root.string);
4091 dynobj = elf_hash_table (eif->info)->dynobj;
4092 bed = get_elf_backend_data (dynobj);
4093 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4095 eif->failed = true;
4096 return false;
4099 return true;
4102 /* This routine is used to export all defined symbols into the dynamic
4103 symbol table. It is called via elf_link_hash_traverse. */
4105 static boolean
4106 elf_export_symbol (h, data)
4107 struct elf_link_hash_entry *h;
4108 PTR data;
4110 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4112 /* Ignore indirect symbols. These are added by the versioning code. */
4113 if (h->root.type == bfd_link_hash_indirect)
4114 return true;
4116 if (h->root.type == bfd_link_hash_warning)
4117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4119 if (h->dynindx == -1
4120 && (h->elf_link_hash_flags
4121 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4123 struct bfd_elf_version_tree *t;
4124 struct bfd_elf_version_expr *d;
4126 for (t = eif->verdefs; t != NULL; t = t->next)
4128 if (t->globals != NULL)
4130 for (d = t->globals; d != NULL; d = d->next)
4132 if ((*d->match) (d, h->root.root.string))
4133 goto doit;
4137 if (t->locals != NULL)
4139 for (d = t->locals ; d != NULL; d = d->next)
4141 if ((*d->match) (d, h->root.root.string))
4142 return true;
4147 if (!eif->verdefs)
4149 doit:
4150 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4152 eif->failed = true;
4153 return false;
4158 return true;
4161 /* Look through the symbols which are defined in other shared
4162 libraries and referenced here. Update the list of version
4163 dependencies. This will be put into the .gnu.version_r section.
4164 This function is called via elf_link_hash_traverse. */
4166 static boolean
4167 elf_link_find_version_dependencies (h, data)
4168 struct elf_link_hash_entry *h;
4169 PTR data;
4171 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4172 Elf_Internal_Verneed *t;
4173 Elf_Internal_Vernaux *a;
4174 bfd_size_type amt;
4176 if (h->root.type == bfd_link_hash_warning)
4177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4179 /* We only care about symbols defined in shared objects with version
4180 information. */
4181 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4182 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4183 || h->dynindx == -1
4184 || h->verinfo.verdef == NULL)
4185 return true;
4187 /* See if we already know about this version. */
4188 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4190 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4191 continue;
4193 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4194 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4195 return true;
4197 break;
4200 /* This is a new version. Add it to tree we are building. */
4202 if (t == NULL)
4204 amt = sizeof *t;
4205 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4206 if (t == NULL)
4208 rinfo->failed = true;
4209 return false;
4212 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4213 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4214 elf_tdata (rinfo->output_bfd)->verref = t;
4217 amt = sizeof *a;
4218 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4220 /* Note that we are copying a string pointer here, and testing it
4221 above. If bfd_elf_string_from_elf_section is ever changed to
4222 discard the string data when low in memory, this will have to be
4223 fixed. */
4224 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4226 a->vna_flags = h->verinfo.verdef->vd_flags;
4227 a->vna_nextptr = t->vn_auxptr;
4229 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4230 ++rinfo->vers;
4232 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4234 t->vn_auxptr = a;
4236 return true;
4239 /* Figure out appropriate versions for all the symbols. We may not
4240 have the version number script until we have read all of the input
4241 files, so until that point we don't know which symbols should be
4242 local. This function is called via elf_link_hash_traverse. */
4244 static boolean
4245 elf_link_assign_sym_version (h, data)
4246 struct elf_link_hash_entry *h;
4247 PTR data;
4249 struct elf_assign_sym_version_info *sinfo;
4250 struct bfd_link_info *info;
4251 struct elf_backend_data *bed;
4252 struct elf_info_failed eif;
4253 char *p;
4254 bfd_size_type amt;
4256 sinfo = (struct elf_assign_sym_version_info *) data;
4257 info = sinfo->info;
4259 if (h->root.type == bfd_link_hash_warning)
4260 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4262 /* Fix the symbol flags. */
4263 eif.failed = false;
4264 eif.info = info;
4265 if (! elf_fix_symbol_flags (h, &eif))
4267 if (eif.failed)
4268 sinfo->failed = true;
4269 return false;
4272 /* We only need version numbers for symbols defined in regular
4273 objects. */
4274 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4275 return true;
4277 bed = get_elf_backend_data (sinfo->output_bfd);
4278 p = strchr (h->root.root.string, ELF_VER_CHR);
4279 if (p != NULL && h->verinfo.vertree == NULL)
4281 struct bfd_elf_version_tree *t;
4282 boolean hidden;
4284 hidden = true;
4286 /* There are two consecutive ELF_VER_CHR characters if this is
4287 not a hidden symbol. */
4288 ++p;
4289 if (*p == ELF_VER_CHR)
4291 hidden = false;
4292 ++p;
4295 /* If there is no version string, we can just return out. */
4296 if (*p == '\0')
4298 if (hidden)
4299 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4300 return true;
4303 /* Look for the version. If we find it, it is no longer weak. */
4304 for (t = sinfo->verdefs; t != NULL; t = t->next)
4306 if (strcmp (t->name, p) == 0)
4308 size_t len;
4309 char *alc;
4310 struct bfd_elf_version_expr *d;
4312 len = p - h->root.root.string;
4313 alc = bfd_malloc ((bfd_size_type) len);
4314 if (alc == NULL)
4315 return false;
4316 strncpy (alc, h->root.root.string, len - 1);
4317 alc[len - 1] = '\0';
4318 if (alc[len - 2] == ELF_VER_CHR)
4319 alc[len - 2] = '\0';
4321 h->verinfo.vertree = t;
4322 t->used = true;
4323 d = NULL;
4325 if (t->globals != NULL)
4327 for (d = t->globals; d != NULL; d = d->next)
4328 if ((*d->match) (d, alc))
4329 break;
4332 /* See if there is anything to force this symbol to
4333 local scope. */
4334 if (d == NULL && t->locals != NULL)
4336 for (d = t->locals; d != NULL; d = d->next)
4338 if ((*d->match) (d, alc))
4340 if (h->dynindx != -1
4341 && info->shared
4342 && ! info->export_dynamic)
4344 (*bed->elf_backend_hide_symbol) (info, h, true);
4347 break;
4352 free (alc);
4353 break;
4357 /* If we are building an application, we need to create a
4358 version node for this version. */
4359 if (t == NULL && ! info->shared)
4361 struct bfd_elf_version_tree **pp;
4362 int version_index;
4364 /* If we aren't going to export this symbol, we don't need
4365 to worry about it. */
4366 if (h->dynindx == -1)
4367 return true;
4369 amt = sizeof *t;
4370 t = ((struct bfd_elf_version_tree *)
4371 bfd_alloc (sinfo->output_bfd, amt));
4372 if (t == NULL)
4374 sinfo->failed = true;
4375 return false;
4378 t->next = NULL;
4379 t->name = p;
4380 t->globals = NULL;
4381 t->locals = NULL;
4382 t->deps = NULL;
4383 t->name_indx = (unsigned int) -1;
4384 t->used = true;
4386 version_index = 1;
4387 /* Don't count anonymous version tag. */
4388 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4389 version_index = 0;
4390 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4391 ++version_index;
4392 t->vernum = version_index;
4394 *pp = t;
4396 h->verinfo.vertree = t;
4398 else if (t == NULL)
4400 /* We could not find the version for a symbol when
4401 generating a shared archive. Return an error. */
4402 (*_bfd_error_handler)
4403 (_("%s: undefined versioned symbol name %s"),
4404 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4405 bfd_set_error (bfd_error_bad_value);
4406 sinfo->failed = true;
4407 return false;
4410 if (hidden)
4411 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4414 /* If we don't have a version for this symbol, see if we can find
4415 something. */
4416 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4418 struct bfd_elf_version_tree *t;
4419 struct bfd_elf_version_tree *deflt;
4420 struct bfd_elf_version_expr *d;
4422 /* See if can find what version this symbol is in. If the
4423 symbol is supposed to be local, then don't actually register
4424 it. */
4425 deflt = NULL;
4426 for (t = sinfo->verdefs; t != NULL; t = t->next)
4428 if (t->globals != NULL)
4430 for (d = t->globals; d != NULL; d = d->next)
4432 if ((*d->match) (d, h->root.root.string))
4434 h->verinfo.vertree = t;
4435 break;
4439 if (d != NULL)
4440 break;
4443 if (t->locals != NULL)
4445 for (d = t->locals; d != NULL; d = d->next)
4447 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4448 deflt = t;
4449 else if ((*d->match) (d, h->root.root.string))
4451 h->verinfo.vertree = t;
4452 if (h->dynindx != -1
4453 && info->shared
4454 && ! info->export_dynamic)
4456 (*bed->elf_backend_hide_symbol) (info, h, true);
4458 break;
4462 if (d != NULL)
4463 break;
4467 if (deflt != NULL && h->verinfo.vertree == NULL)
4469 h->verinfo.vertree = deflt;
4470 if (h->dynindx != -1
4471 && info->shared
4472 && ! info->export_dynamic)
4474 (*bed->elf_backend_hide_symbol) (info, h, true);
4479 return true;
4482 /* Final phase of ELF linker. */
4484 /* A structure we use to avoid passing large numbers of arguments. */
4486 struct elf_final_link_info
4488 /* General link information. */
4489 struct bfd_link_info *info;
4490 /* Output BFD. */
4491 bfd *output_bfd;
4492 /* Symbol string table. */
4493 struct bfd_strtab_hash *symstrtab;
4494 /* .dynsym section. */
4495 asection *dynsym_sec;
4496 /* .hash section. */
4497 asection *hash_sec;
4498 /* symbol version section (.gnu.version). */
4499 asection *symver_sec;
4500 /* first SHF_TLS section (if any). */
4501 asection *first_tls_sec;
4502 /* Buffer large enough to hold contents of any section. */
4503 bfd_byte *contents;
4504 /* Buffer large enough to hold external relocs of any section. */
4505 PTR external_relocs;
4506 /* Buffer large enough to hold internal relocs of any section. */
4507 Elf_Internal_Rela *internal_relocs;
4508 /* Buffer large enough to hold external local symbols of any input
4509 BFD. */
4510 Elf_External_Sym *external_syms;
4511 /* And a buffer for symbol section indices. */
4512 Elf_External_Sym_Shndx *locsym_shndx;
4513 /* Buffer large enough to hold internal local symbols of any input
4514 BFD. */
4515 Elf_Internal_Sym *internal_syms;
4516 /* Array large enough to hold a symbol index for each local symbol
4517 of any input BFD. */
4518 long *indices;
4519 /* Array large enough to hold a section pointer for each local
4520 symbol of any input BFD. */
4521 asection **sections;
4522 /* Buffer to hold swapped out symbols. */
4523 Elf_External_Sym *symbuf;
4524 /* And one for symbol section indices. */
4525 Elf_External_Sym_Shndx *symshndxbuf;
4526 /* Number of swapped out symbols in buffer. */
4527 size_t symbuf_count;
4528 /* Number of symbols which fit in symbuf. */
4529 size_t symbuf_size;
4532 static boolean elf_link_output_sym
4533 PARAMS ((struct elf_final_link_info *, const char *,
4534 Elf_Internal_Sym *, asection *));
4535 static boolean elf_link_flush_output_syms
4536 PARAMS ((struct elf_final_link_info *));
4537 static boolean elf_link_output_extsym
4538 PARAMS ((struct elf_link_hash_entry *, PTR));
4539 static boolean elf_link_sec_merge_syms
4540 PARAMS ((struct elf_link_hash_entry *, PTR));
4541 static boolean elf_link_input_bfd
4542 PARAMS ((struct elf_final_link_info *, bfd *));
4543 static boolean elf_reloc_link_order
4544 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4545 struct bfd_link_order *));
4547 /* This struct is used to pass information to elf_link_output_extsym. */
4549 struct elf_outext_info
4551 boolean failed;
4552 boolean localsyms;
4553 struct elf_final_link_info *finfo;
4556 /* Compute the size of, and allocate space for, REL_HDR which is the
4557 section header for a section containing relocations for O. */
4559 static boolean
4560 elf_link_size_reloc_section (abfd, rel_hdr, o)
4561 bfd *abfd;
4562 Elf_Internal_Shdr *rel_hdr;
4563 asection *o;
4565 bfd_size_type reloc_count;
4566 bfd_size_type num_rel_hashes;
4568 /* Figure out how many relocations there will be. */
4569 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4570 reloc_count = elf_section_data (o)->rel_count;
4571 else
4572 reloc_count = elf_section_data (o)->rel_count2;
4574 num_rel_hashes = o->reloc_count;
4575 if (num_rel_hashes < reloc_count)
4576 num_rel_hashes = reloc_count;
4578 /* That allows us to calculate the size of the section. */
4579 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4581 /* The contents field must last into write_object_contents, so we
4582 allocate it with bfd_alloc rather than malloc. Also since we
4583 cannot be sure that the contents will actually be filled in,
4584 we zero the allocated space. */
4585 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4586 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4587 return false;
4589 /* We only allocate one set of hash entries, so we only do it the
4590 first time we are called. */
4591 if (elf_section_data (o)->rel_hashes == NULL
4592 && num_rel_hashes)
4594 struct elf_link_hash_entry **p;
4596 p = ((struct elf_link_hash_entry **)
4597 bfd_zmalloc (num_rel_hashes
4598 * sizeof (struct elf_link_hash_entry *)));
4599 if (p == NULL)
4600 return false;
4602 elf_section_data (o)->rel_hashes = p;
4605 return true;
4608 /* When performing a relocateable link, the input relocations are
4609 preserved. But, if they reference global symbols, the indices
4610 referenced must be updated. Update all the relocations in
4611 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4613 static void
4614 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4615 bfd *abfd;
4616 Elf_Internal_Shdr *rel_hdr;
4617 unsigned int count;
4618 struct elf_link_hash_entry **rel_hash;
4620 unsigned int i;
4621 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4622 Elf_Internal_Rel *irel;
4623 Elf_Internal_Rela *irela;
4624 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4626 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4627 if (irel == NULL)
4629 (*_bfd_error_handler) (_("Error: out of memory"));
4630 abort ();
4633 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4634 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4635 if (irela == NULL)
4637 (*_bfd_error_handler) (_("Error: out of memory"));
4638 abort ();
4641 for (i = 0; i < count; i++, rel_hash++)
4643 if (*rel_hash == NULL)
4644 continue;
4646 BFD_ASSERT ((*rel_hash)->indx >= 0);
4648 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4650 Elf_External_Rel *erel;
4651 unsigned int j;
4653 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4654 if (bed->s->swap_reloc_in)
4655 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4656 else
4657 elf_swap_reloc_in (abfd, erel, irel);
4659 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4660 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4661 ELF_R_TYPE (irel[j].r_info));
4663 if (bed->s->swap_reloc_out)
4664 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4665 else
4666 elf_swap_reloc_out (abfd, irel, erel);
4668 else
4670 Elf_External_Rela *erela;
4671 unsigned int j;
4673 BFD_ASSERT (rel_hdr->sh_entsize
4674 == sizeof (Elf_External_Rela));
4676 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4677 if (bed->s->swap_reloca_in)
4678 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4679 else
4680 elf_swap_reloca_in (abfd, erela, irela);
4682 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4683 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4684 ELF_R_TYPE (irela[j].r_info));
4686 if (bed->s->swap_reloca_out)
4687 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4688 else
4689 elf_swap_reloca_out (abfd, irela, erela);
4693 free (irel);
4694 free (irela);
4697 struct elf_link_sort_rela {
4698 bfd_vma offset;
4699 enum elf_reloc_type_class type;
4700 union {
4701 Elf_Internal_Rel rel;
4702 Elf_Internal_Rela rela;
4703 } u;
4706 static int
4707 elf_link_sort_cmp1 (A, B)
4708 const PTR A;
4709 const PTR B;
4711 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4712 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4713 int relativea, relativeb;
4715 relativea = a->type == reloc_class_relative;
4716 relativeb = b->type == reloc_class_relative;
4718 if (relativea < relativeb)
4719 return 1;
4720 if (relativea > relativeb)
4721 return -1;
4722 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4723 return -1;
4724 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4725 return 1;
4726 if (a->u.rel.r_offset < b->u.rel.r_offset)
4727 return -1;
4728 if (a->u.rel.r_offset > b->u.rel.r_offset)
4729 return 1;
4730 return 0;
4733 static int
4734 elf_link_sort_cmp2 (A, B)
4735 const PTR A;
4736 const PTR B;
4738 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4739 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4740 int copya, copyb;
4742 if (a->offset < b->offset)
4743 return -1;
4744 if (a->offset > b->offset)
4745 return 1;
4746 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4747 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4748 if (copya < copyb)
4749 return -1;
4750 if (copya > copyb)
4751 return 1;
4752 if (a->u.rel.r_offset < b->u.rel.r_offset)
4753 return -1;
4754 if (a->u.rel.r_offset > b->u.rel.r_offset)
4755 return 1;
4756 return 0;
4759 static size_t
4760 elf_link_sort_relocs (abfd, info, psec)
4761 bfd *abfd;
4762 struct bfd_link_info *info;
4763 asection **psec;
4765 bfd *dynobj = elf_hash_table (info)->dynobj;
4766 asection *reldyn, *o;
4767 boolean rel = false;
4768 bfd_size_type count, size;
4769 size_t i, j, ret;
4770 struct elf_link_sort_rela *rela;
4771 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4773 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4774 if (reldyn == NULL || reldyn->_raw_size == 0)
4776 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4777 if (reldyn == NULL || reldyn->_raw_size == 0)
4778 return 0;
4779 rel = true;
4780 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4782 else
4783 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4785 size = 0;
4786 for (o = dynobj->sections; o != NULL; o = o->next)
4787 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4788 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4789 && o->output_section == reldyn)
4790 size += o->_raw_size;
4792 if (size != reldyn->_raw_size)
4793 return 0;
4795 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4796 if (rela == NULL)
4798 (*info->callbacks->warning)
4799 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4800 (bfd_vma) 0);
4801 return 0;
4804 for (o = dynobj->sections; o != NULL; o = o->next)
4805 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4806 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4807 && o->output_section == reldyn)
4809 if (rel)
4811 Elf_External_Rel *erel, *erelend;
4812 struct elf_link_sort_rela *s;
4814 erel = (Elf_External_Rel *) o->contents;
4815 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4816 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4817 for (; erel < erelend; erel++, s++)
4819 if (bed->s->swap_reloc_in)
4820 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4821 else
4822 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4824 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4827 else
4829 Elf_External_Rela *erela, *erelaend;
4830 struct elf_link_sort_rela *s;
4832 erela = (Elf_External_Rela *) o->contents;
4833 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4834 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4835 for (; erela < erelaend; erela++, s++)
4837 if (bed->s->swap_reloca_in)
4838 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4839 &s->u.rela);
4840 else
4841 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4843 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4848 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4849 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4851 for (i = ret, j = ret; i < count; i++)
4853 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4854 j = i;
4855 rela[i].offset = rela[j].u.rel.r_offset;
4857 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4859 for (o = dynobj->sections; o != NULL; o = o->next)
4860 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4861 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4862 && o->output_section == reldyn)
4864 if (rel)
4866 Elf_External_Rel *erel, *erelend;
4867 struct elf_link_sort_rela *s;
4869 erel = (Elf_External_Rel *) o->contents;
4870 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4871 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4872 for (; erel < erelend; erel++, s++)
4874 if (bed->s->swap_reloc_out)
4875 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4876 (bfd_byte *) erel);
4877 else
4878 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4881 else
4883 Elf_External_Rela *erela, *erelaend;
4884 struct elf_link_sort_rela *s;
4886 erela = (Elf_External_Rela *) o->contents;
4887 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4888 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4889 for (; erela < erelaend; erela++, s++)
4891 if (bed->s->swap_reloca_out)
4892 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4893 (bfd_byte *) erela);
4894 else
4895 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4900 free (rela);
4901 *psec = reldyn;
4902 return ret;
4905 /* Do the final step of an ELF link. */
4907 boolean
4908 elf_bfd_final_link (abfd, info)
4909 bfd *abfd;
4910 struct bfd_link_info *info;
4912 boolean dynamic;
4913 boolean emit_relocs;
4914 bfd *dynobj;
4915 struct elf_final_link_info finfo;
4916 register asection *o;
4917 register struct bfd_link_order *p;
4918 register bfd *sub;
4919 bfd_size_type max_contents_size;
4920 bfd_size_type max_external_reloc_size;
4921 bfd_size_type max_internal_reloc_count;
4922 bfd_size_type max_sym_count;
4923 bfd_size_type max_sym_shndx_count;
4924 file_ptr off;
4925 Elf_Internal_Sym elfsym;
4926 unsigned int i;
4927 Elf_Internal_Shdr *symtab_hdr;
4928 Elf_Internal_Shdr *symstrtab_hdr;
4929 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4930 struct elf_outext_info eoinfo;
4931 boolean merged;
4932 size_t relativecount = 0;
4933 asection *reldyn = 0;
4934 bfd_size_type amt;
4936 if (! is_elf_hash_table (info))
4937 return false;
4939 if (info->shared)
4940 abfd->flags |= DYNAMIC;
4942 dynamic = elf_hash_table (info)->dynamic_sections_created;
4943 dynobj = elf_hash_table (info)->dynobj;
4945 emit_relocs = (info->relocateable
4946 || info->emitrelocations
4947 || bed->elf_backend_emit_relocs);
4949 finfo.info = info;
4950 finfo.output_bfd = abfd;
4951 finfo.symstrtab = elf_stringtab_init ();
4952 if (finfo.symstrtab == NULL)
4953 return false;
4955 if (! dynamic)
4957 finfo.dynsym_sec = NULL;
4958 finfo.hash_sec = NULL;
4959 finfo.symver_sec = NULL;
4961 else
4963 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4964 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4965 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4966 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4967 /* Note that it is OK if symver_sec is NULL. */
4970 finfo.contents = NULL;
4971 finfo.external_relocs = NULL;
4972 finfo.internal_relocs = NULL;
4973 finfo.external_syms = NULL;
4974 finfo.locsym_shndx = NULL;
4975 finfo.internal_syms = NULL;
4976 finfo.indices = NULL;
4977 finfo.sections = NULL;
4978 finfo.symbuf = NULL;
4979 finfo.symshndxbuf = NULL;
4980 finfo.symbuf_count = 0;
4981 finfo.first_tls_sec = NULL;
4982 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4983 if ((o->flags & SEC_THREAD_LOCAL) != 0
4984 && (o->flags & SEC_LOAD) != 0)
4986 finfo.first_tls_sec = o;
4987 break;
4990 /* Count up the number of relocations we will output for each output
4991 section, so that we know the sizes of the reloc sections. We
4992 also figure out some maximum sizes. */
4993 max_contents_size = 0;
4994 max_external_reloc_size = 0;
4995 max_internal_reloc_count = 0;
4996 max_sym_count = 0;
4997 max_sym_shndx_count = 0;
4998 merged = false;
4999 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5001 o->reloc_count = 0;
5003 for (p = o->link_order_head; p != NULL; p = p->next)
5005 if (p->type == bfd_section_reloc_link_order
5006 || p->type == bfd_symbol_reloc_link_order)
5007 ++o->reloc_count;
5008 else if (p->type == bfd_indirect_link_order)
5010 asection *sec;
5012 sec = p->u.indirect.section;
5014 /* Mark all sections which are to be included in the
5015 link. This will normally be every section. We need
5016 to do this so that we can identify any sections which
5017 the linker has decided to not include. */
5018 sec->linker_mark = true;
5020 if (sec->flags & SEC_MERGE)
5021 merged = true;
5023 if (info->relocateable || info->emitrelocations)
5024 o->reloc_count += sec->reloc_count;
5025 else if (bed->elf_backend_count_relocs)
5027 Elf_Internal_Rela * relocs;
5029 relocs = (NAME(_bfd_elf,link_read_relocs)
5030 (abfd, sec, (PTR) NULL,
5031 (Elf_Internal_Rela *) NULL, info->keep_memory));
5033 o->reloc_count
5034 += (*bed->elf_backend_count_relocs) (sec, relocs);
5036 if (!info->keep_memory)
5037 free (relocs);
5040 if (sec->_raw_size > max_contents_size)
5041 max_contents_size = sec->_raw_size;
5042 if (sec->_cooked_size > max_contents_size)
5043 max_contents_size = sec->_cooked_size;
5045 /* We are interested in just local symbols, not all
5046 symbols. */
5047 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5048 && (sec->owner->flags & DYNAMIC) == 0)
5050 size_t sym_count;
5052 if (elf_bad_symtab (sec->owner))
5053 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5054 / sizeof (Elf_External_Sym));
5055 else
5056 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5058 if (sym_count > max_sym_count)
5059 max_sym_count = sym_count;
5061 if (sym_count > max_sym_shndx_count
5062 && elf_symtab_shndx (sec->owner) != 0)
5063 max_sym_shndx_count = sym_count;
5065 if ((sec->flags & SEC_RELOC) != 0)
5067 size_t ext_size;
5069 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5070 if (ext_size > max_external_reloc_size)
5071 max_external_reloc_size = ext_size;
5072 if (sec->reloc_count > max_internal_reloc_count)
5073 max_internal_reloc_count = sec->reloc_count;
5079 if (o->reloc_count > 0)
5080 o->flags |= SEC_RELOC;
5081 else
5083 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5084 set it (this is probably a bug) and if it is set
5085 assign_section_numbers will create a reloc section. */
5086 o->flags &=~ SEC_RELOC;
5089 /* If the SEC_ALLOC flag is not set, force the section VMA to
5090 zero. This is done in elf_fake_sections as well, but forcing
5091 the VMA to 0 here will ensure that relocs against these
5092 sections are handled correctly. */
5093 if ((o->flags & SEC_ALLOC) == 0
5094 && ! o->user_set_vma)
5095 o->vma = 0;
5098 if (! info->relocateable && merged)
5099 elf_link_hash_traverse (elf_hash_table (info),
5100 elf_link_sec_merge_syms, (PTR) abfd);
5102 /* Figure out the file positions for everything but the symbol table
5103 and the relocs. We set symcount to force assign_section_numbers
5104 to create a symbol table. */
5105 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5106 BFD_ASSERT (! abfd->output_has_begun);
5107 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5108 goto error_return;
5110 /* Figure out how many relocations we will have in each section.
5111 Just using RELOC_COUNT isn't good enough since that doesn't
5112 maintain a separate value for REL vs. RELA relocations. */
5113 if (emit_relocs)
5114 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5115 for (o = sub->sections; o != NULL; o = o->next)
5117 asection *output_section;
5119 if (! o->linker_mark)
5121 /* This section was omitted from the link. */
5122 continue;
5125 output_section = o->output_section;
5127 if (output_section != NULL
5128 && (o->flags & SEC_RELOC) != 0)
5130 struct bfd_elf_section_data *esdi
5131 = elf_section_data (o);
5132 struct bfd_elf_section_data *esdo
5133 = elf_section_data (output_section);
5134 unsigned int *rel_count;
5135 unsigned int *rel_count2;
5136 bfd_size_type entsize;
5137 bfd_size_type entsize2;
5139 /* We must be careful to add the relocations from the
5140 input section to the right output count. */
5141 entsize = esdi->rel_hdr.sh_entsize;
5142 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5143 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5144 || entsize == sizeof (Elf_External_Rela))
5145 && entsize2 != entsize
5146 && (entsize2 == 0
5147 || entsize2 == sizeof (Elf_External_Rel)
5148 || entsize2 == sizeof (Elf_External_Rela)));
5149 if (entsize == esdo->rel_hdr.sh_entsize)
5151 rel_count = &esdo->rel_count;
5152 rel_count2 = &esdo->rel_count2;
5154 else
5156 rel_count = &esdo->rel_count2;
5157 rel_count2 = &esdo->rel_count;
5160 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5161 if (esdi->rel_hdr2)
5162 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5163 output_section->flags |= SEC_RELOC;
5167 /* That created the reloc sections. Set their sizes, and assign
5168 them file positions, and allocate some buffers. */
5169 for (o = abfd->sections; o != NULL; o = o->next)
5171 if ((o->flags & SEC_RELOC) != 0)
5173 if (!elf_link_size_reloc_section (abfd,
5174 &elf_section_data (o)->rel_hdr,
5176 goto error_return;
5178 if (elf_section_data (o)->rel_hdr2
5179 && !elf_link_size_reloc_section (abfd,
5180 elf_section_data (o)->rel_hdr2,
5182 goto error_return;
5185 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5186 to count upwards while actually outputting the relocations. */
5187 elf_section_data (o)->rel_count = 0;
5188 elf_section_data (o)->rel_count2 = 0;
5191 _bfd_elf_assign_file_positions_for_relocs (abfd);
5193 /* We have now assigned file positions for all the sections except
5194 .symtab and .strtab. We start the .symtab section at the current
5195 file position, and write directly to it. We build the .strtab
5196 section in memory. */
5197 bfd_get_symcount (abfd) = 0;
5198 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5199 /* sh_name is set in prep_headers. */
5200 symtab_hdr->sh_type = SHT_SYMTAB;
5201 symtab_hdr->sh_flags = 0;
5202 symtab_hdr->sh_addr = 0;
5203 symtab_hdr->sh_size = 0;
5204 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5205 /* sh_link is set in assign_section_numbers. */
5206 /* sh_info is set below. */
5207 /* sh_offset is set just below. */
5208 symtab_hdr->sh_addralign = bed->s->file_align;
5210 off = elf_tdata (abfd)->next_file_pos;
5211 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5213 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5214 incorrect. We do not yet know the size of the .symtab section.
5215 We correct next_file_pos below, after we do know the size. */
5217 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5218 continuously seeking to the right position in the file. */
5219 if (! info->keep_memory || max_sym_count < 20)
5220 finfo.symbuf_size = 20;
5221 else
5222 finfo.symbuf_size = max_sym_count;
5223 amt = finfo.symbuf_size;
5224 amt *= sizeof (Elf_External_Sym);
5225 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5226 if (finfo.symbuf == NULL)
5227 goto error_return;
5228 if (elf_numsections (abfd) > SHN_LORESERVE)
5230 amt = finfo.symbuf_size;
5231 amt *= sizeof (Elf_External_Sym_Shndx);
5232 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5233 if (finfo.symshndxbuf == NULL)
5234 goto error_return;
5237 /* Start writing out the symbol table. The first symbol is always a
5238 dummy symbol. */
5239 if (info->strip != strip_all
5240 || emit_relocs)
5242 elfsym.st_value = 0;
5243 elfsym.st_size = 0;
5244 elfsym.st_info = 0;
5245 elfsym.st_other = 0;
5246 elfsym.st_shndx = SHN_UNDEF;
5247 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5248 &elfsym, bfd_und_section_ptr))
5249 goto error_return;
5252 #if 0
5253 /* Some standard ELF linkers do this, but we don't because it causes
5254 bootstrap comparison failures. */
5255 /* Output a file symbol for the output file as the second symbol.
5256 We output this even if we are discarding local symbols, although
5257 I'm not sure if this is correct. */
5258 elfsym.st_value = 0;
5259 elfsym.st_size = 0;
5260 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5261 elfsym.st_other = 0;
5262 elfsym.st_shndx = SHN_ABS;
5263 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5264 &elfsym, bfd_abs_section_ptr))
5265 goto error_return;
5266 #endif
5268 /* Output a symbol for each section. We output these even if we are
5269 discarding local symbols, since they are used for relocs. These
5270 symbols have no names. We store the index of each one in the
5271 index field of the section, so that we can find it again when
5272 outputting relocs. */
5273 if (info->strip != strip_all
5274 || emit_relocs)
5276 elfsym.st_size = 0;
5277 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5278 elfsym.st_other = 0;
5279 for (i = 1; i < elf_numsections (abfd); i++)
5281 o = section_from_elf_index (abfd, i);
5282 if (o != NULL)
5283 o->target_index = bfd_get_symcount (abfd);
5284 elfsym.st_shndx = i;
5285 if (info->relocateable || o == NULL)
5286 elfsym.st_value = 0;
5287 else
5288 elfsym.st_value = o->vma;
5289 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5290 &elfsym, o))
5291 goto error_return;
5292 if (i == SHN_LORESERVE)
5293 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5297 /* Allocate some memory to hold information read in from the input
5298 files. */
5299 if (max_contents_size != 0)
5301 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5302 if (finfo.contents == NULL)
5303 goto error_return;
5306 if (max_external_reloc_size != 0)
5308 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5309 if (finfo.external_relocs == NULL)
5310 goto error_return;
5313 if (max_internal_reloc_count != 0)
5315 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5316 amt *= sizeof (Elf_Internal_Rela);
5317 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5318 if (finfo.internal_relocs == NULL)
5319 goto error_return;
5322 if (max_sym_count != 0)
5324 amt = max_sym_count * sizeof (Elf_External_Sym);
5325 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5326 if (finfo.external_syms == NULL)
5327 goto error_return;
5329 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5330 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5331 if (finfo.internal_syms == NULL)
5332 goto error_return;
5334 amt = max_sym_count * sizeof (long);
5335 finfo.indices = (long *) bfd_malloc (amt);
5336 if (finfo.indices == NULL)
5337 goto error_return;
5339 amt = max_sym_count * sizeof (asection *);
5340 finfo.sections = (asection **) bfd_malloc (amt);
5341 if (finfo.sections == NULL)
5342 goto error_return;
5345 if (max_sym_shndx_count != 0)
5347 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5348 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5349 if (finfo.locsym_shndx == NULL)
5350 goto error_return;
5353 if (finfo.first_tls_sec)
5355 unsigned int align = 0;
5356 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5357 asection *sec;
5359 for (sec = finfo.first_tls_sec;
5360 sec && (sec->flags & SEC_THREAD_LOCAL);
5361 sec = sec->next)
5363 bfd_vma size = sec->_raw_size;
5365 if (bfd_get_section_alignment (abfd, sec) > align)
5366 align = bfd_get_section_alignment (abfd, sec);
5367 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5369 struct bfd_link_order *o;
5371 size = 0;
5372 for (o = sec->link_order_head; o != NULL; o = o->next)
5373 if (size < o->offset + o->size)
5374 size = o->offset + o->size;
5376 end = sec->vma + size;
5378 elf_hash_table (info)->tls_segment
5379 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5380 if (elf_hash_table (info)->tls_segment == NULL)
5381 goto error_return;
5382 elf_hash_table (info)->tls_segment->start = base;
5383 elf_hash_table (info)->tls_segment->size = end - base;
5384 elf_hash_table (info)->tls_segment->align = align;
5387 /* Since ELF permits relocations to be against local symbols, we
5388 must have the local symbols available when we do the relocations.
5389 Since we would rather only read the local symbols once, and we
5390 would rather not keep them in memory, we handle all the
5391 relocations for a single input file at the same time.
5393 Unfortunately, there is no way to know the total number of local
5394 symbols until we have seen all of them, and the local symbol
5395 indices precede the global symbol indices. This means that when
5396 we are generating relocateable output, and we see a reloc against
5397 a global symbol, we can not know the symbol index until we have
5398 finished examining all the local symbols to see which ones we are
5399 going to output. To deal with this, we keep the relocations in
5400 memory, and don't output them until the end of the link. This is
5401 an unfortunate waste of memory, but I don't see a good way around
5402 it. Fortunately, it only happens when performing a relocateable
5403 link, which is not the common case. FIXME: If keep_memory is set
5404 we could write the relocs out and then read them again; I don't
5405 know how bad the memory loss will be. */
5407 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5408 sub->output_has_begun = false;
5409 for (o = abfd->sections; o != NULL; o = o->next)
5411 for (p = o->link_order_head; p != NULL; p = p->next)
5413 if (p->type == bfd_indirect_link_order
5414 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
5415 == bfd_target_elf_flavour)
5416 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
5418 if (! sub->output_has_begun)
5420 if (! elf_link_input_bfd (&finfo, sub))
5421 goto error_return;
5422 sub->output_has_begun = true;
5425 else if (p->type == bfd_section_reloc_link_order
5426 || p->type == bfd_symbol_reloc_link_order)
5428 if (! elf_reloc_link_order (abfd, info, o, p))
5429 goto error_return;
5431 else
5433 if (! _bfd_default_link_order (abfd, info, o, p))
5434 goto error_return;
5439 /* Output any global symbols that got converted to local in a
5440 version script or due to symbol visibility. We do this in a
5441 separate step since ELF requires all local symbols to appear
5442 prior to any global symbols. FIXME: We should only do this if
5443 some global symbols were, in fact, converted to become local.
5444 FIXME: Will this work correctly with the Irix 5 linker? */
5445 eoinfo.failed = false;
5446 eoinfo.finfo = &finfo;
5447 eoinfo.localsyms = true;
5448 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5449 (PTR) &eoinfo);
5450 if (eoinfo.failed)
5451 return false;
5453 /* That wrote out all the local symbols. Finish up the symbol table
5454 with the global symbols. Even if we want to strip everything we
5455 can, we still need to deal with those global symbols that got
5456 converted to local in a version script. */
5458 /* The sh_info field records the index of the first non local symbol. */
5459 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5461 if (dynamic
5462 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5464 Elf_Internal_Sym sym;
5465 Elf_External_Sym *dynsym =
5466 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5467 long last_local = 0;
5469 /* Write out the section symbols for the output sections. */
5470 if (info->shared)
5472 asection *s;
5474 sym.st_size = 0;
5475 sym.st_name = 0;
5476 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5477 sym.st_other = 0;
5479 for (s = abfd->sections; s != NULL; s = s->next)
5481 int indx;
5482 Elf_External_Sym *dest;
5484 indx = elf_section_data (s)->this_idx;
5485 BFD_ASSERT (indx > 0);
5486 sym.st_shndx = indx;
5487 sym.st_value = s->vma;
5488 dest = dynsym + elf_section_data (s)->dynindx;
5489 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5492 last_local = bfd_count_sections (abfd);
5495 /* Write out the local dynsyms. */
5496 if (elf_hash_table (info)->dynlocal)
5498 struct elf_link_local_dynamic_entry *e;
5499 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5501 asection *s;
5502 Elf_External_Sym *dest;
5504 sym.st_size = e->isym.st_size;
5505 sym.st_other = e->isym.st_other;
5507 /* Copy the internal symbol as is.
5508 Note that we saved a word of storage and overwrote
5509 the original st_name with the dynstr_index. */
5510 sym = e->isym;
5512 if (e->isym.st_shndx != SHN_UNDEF
5513 && (e->isym.st_shndx < SHN_LORESERVE
5514 || e->isym.st_shndx > SHN_HIRESERVE))
5516 s = bfd_section_from_elf_index (e->input_bfd,
5517 e->isym.st_shndx);
5519 sym.st_shndx =
5520 elf_section_data (s->output_section)->this_idx;
5521 sym.st_value = (s->output_section->vma
5522 + s->output_offset
5523 + e->isym.st_value);
5526 if (last_local < e->dynindx)
5527 last_local = e->dynindx;
5529 dest = dynsym + e->dynindx;
5530 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5534 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5535 last_local + 1;
5538 /* We get the global symbols from the hash table. */
5539 eoinfo.failed = false;
5540 eoinfo.localsyms = false;
5541 eoinfo.finfo = &finfo;
5542 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5543 (PTR) &eoinfo);
5544 if (eoinfo.failed)
5545 return false;
5547 /* If backend needs to output some symbols not present in the hash
5548 table, do it now. */
5549 if (bed->elf_backend_output_arch_syms)
5551 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5552 Elf_Internal_Sym *,
5553 asection *));
5555 if (! ((*bed->elf_backend_output_arch_syms)
5556 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5557 return false;
5560 /* Flush all symbols to the file. */
5561 if (! elf_link_flush_output_syms (&finfo))
5562 return false;
5564 /* Now we know the size of the symtab section. */
5565 off += symtab_hdr->sh_size;
5567 /* Finish up and write out the symbol string table (.strtab)
5568 section. */
5569 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5570 /* sh_name was set in prep_headers. */
5571 symstrtab_hdr->sh_type = SHT_STRTAB;
5572 symstrtab_hdr->sh_flags = 0;
5573 symstrtab_hdr->sh_addr = 0;
5574 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5575 symstrtab_hdr->sh_entsize = 0;
5576 symstrtab_hdr->sh_link = 0;
5577 symstrtab_hdr->sh_info = 0;
5578 /* sh_offset is set just below. */
5579 symstrtab_hdr->sh_addralign = 1;
5581 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5582 elf_tdata (abfd)->next_file_pos = off;
5584 if (bfd_get_symcount (abfd) > 0)
5586 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5587 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5588 return false;
5591 /* Adjust the relocs to have the correct symbol indices. */
5592 for (o = abfd->sections; o != NULL; o = o->next)
5594 if ((o->flags & SEC_RELOC) == 0)
5595 continue;
5597 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5598 elf_section_data (o)->rel_count,
5599 elf_section_data (o)->rel_hashes);
5600 if (elf_section_data (o)->rel_hdr2 != NULL)
5601 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5602 elf_section_data (o)->rel_count2,
5603 (elf_section_data (o)->rel_hashes
5604 + elf_section_data (o)->rel_count));
5606 /* Set the reloc_count field to 0 to prevent write_relocs from
5607 trying to swap the relocs out itself. */
5608 o->reloc_count = 0;
5611 if (dynamic && info->combreloc && dynobj != NULL)
5612 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5614 /* If we are linking against a dynamic object, or generating a
5615 shared library, finish up the dynamic linking information. */
5616 if (dynamic)
5618 Elf_External_Dyn *dyncon, *dynconend;
5620 /* Fix up .dynamic entries. */
5621 o = bfd_get_section_by_name (dynobj, ".dynamic");
5622 BFD_ASSERT (o != NULL);
5624 dyncon = (Elf_External_Dyn *) o->contents;
5625 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5626 for (; dyncon < dynconend; dyncon++)
5628 Elf_Internal_Dyn dyn;
5629 const char *name;
5630 unsigned int type;
5632 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5634 switch (dyn.d_tag)
5636 default:
5637 break;
5638 case DT_NULL:
5639 if (relativecount > 0 && dyncon + 1 < dynconend)
5641 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5643 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5644 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5645 default: break;
5647 if (dyn.d_tag != DT_NULL)
5649 dyn.d_un.d_val = relativecount;
5650 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5651 relativecount = 0;
5654 break;
5655 case DT_INIT:
5656 name = info->init_function;
5657 goto get_sym;
5658 case DT_FINI:
5659 name = info->fini_function;
5660 get_sym:
5662 struct elf_link_hash_entry *h;
5664 h = elf_link_hash_lookup (elf_hash_table (info), name,
5665 false, false, true);
5666 if (h != NULL
5667 && (h->root.type == bfd_link_hash_defined
5668 || h->root.type == bfd_link_hash_defweak))
5670 dyn.d_un.d_val = h->root.u.def.value;
5671 o = h->root.u.def.section;
5672 if (o->output_section != NULL)
5673 dyn.d_un.d_val += (o->output_section->vma
5674 + o->output_offset);
5675 else
5677 /* The symbol is imported from another shared
5678 library and does not apply to this one. */
5679 dyn.d_un.d_val = 0;
5682 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5685 break;
5687 case DT_PREINIT_ARRAYSZ:
5688 name = ".preinit_array";
5689 goto get_size;
5690 case DT_INIT_ARRAYSZ:
5691 name = ".init_array";
5692 goto get_size;
5693 case DT_FINI_ARRAYSZ:
5694 name = ".fini_array";
5695 get_size:
5696 o = bfd_get_section_by_name (abfd, name);
5697 BFD_ASSERT (o != NULL);
5698 if (o->_raw_size == 0)
5699 (*_bfd_error_handler)
5700 (_("warning: %s section has zero size"), name);
5701 dyn.d_un.d_val = o->_raw_size;
5702 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5703 break;
5705 case DT_PREINIT_ARRAY:
5706 name = ".preinit_array";
5707 goto get_vma;
5708 case DT_INIT_ARRAY:
5709 name = ".init_array";
5710 goto get_vma;
5711 case DT_FINI_ARRAY:
5712 name = ".fini_array";
5713 goto get_vma;
5715 case DT_HASH:
5716 name = ".hash";
5717 goto get_vma;
5718 case DT_STRTAB:
5719 name = ".dynstr";
5720 goto get_vma;
5721 case DT_SYMTAB:
5722 name = ".dynsym";
5723 goto get_vma;
5724 case DT_VERDEF:
5725 name = ".gnu.version_d";
5726 goto get_vma;
5727 case DT_VERNEED:
5728 name = ".gnu.version_r";
5729 goto get_vma;
5730 case DT_VERSYM:
5731 name = ".gnu.version";
5732 get_vma:
5733 o = bfd_get_section_by_name (abfd, name);
5734 BFD_ASSERT (o != NULL);
5735 dyn.d_un.d_ptr = o->vma;
5736 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5737 break;
5739 case DT_REL:
5740 case DT_RELA:
5741 case DT_RELSZ:
5742 case DT_RELASZ:
5743 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5744 type = SHT_REL;
5745 else
5746 type = SHT_RELA;
5747 dyn.d_un.d_val = 0;
5748 for (i = 1; i < elf_numsections (abfd); i++)
5750 Elf_Internal_Shdr *hdr;
5752 hdr = elf_elfsections (abfd)[i];
5753 if (hdr->sh_type == type
5754 && (hdr->sh_flags & SHF_ALLOC) != 0)
5756 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5757 dyn.d_un.d_val += hdr->sh_size;
5758 else
5760 if (dyn.d_un.d_val == 0
5761 || hdr->sh_addr < dyn.d_un.d_val)
5762 dyn.d_un.d_val = hdr->sh_addr;
5766 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5767 break;
5772 /* If we have created any dynamic sections, then output them. */
5773 if (dynobj != NULL)
5775 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5776 goto error_return;
5778 for (o = dynobj->sections; o != NULL; o = o->next)
5780 if ((o->flags & SEC_HAS_CONTENTS) == 0
5781 || o->_raw_size == 0
5782 || o->output_section == bfd_abs_section_ptr)
5783 continue;
5784 if ((o->flags & SEC_LINKER_CREATED) == 0)
5786 /* At this point, we are only interested in sections
5787 created by elf_link_create_dynamic_sections. */
5788 continue;
5790 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5791 != SHT_STRTAB)
5792 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5794 if (! bfd_set_section_contents (abfd, o->output_section,
5795 o->contents,
5796 (file_ptr) o->output_offset,
5797 o->_raw_size))
5798 goto error_return;
5800 else
5802 /* The contents of the .dynstr section are actually in a
5803 stringtab. */
5804 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5805 if (bfd_seek (abfd, off, SEEK_SET) != 0
5806 || ! _bfd_elf_strtab_emit (abfd,
5807 elf_hash_table (info)->dynstr))
5808 goto error_return;
5813 /* If we have optimized stabs strings, output them. */
5814 if (elf_hash_table (info)->stab_info != NULL)
5816 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5817 goto error_return;
5820 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5822 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5823 ".eh_frame_hdr");
5824 if (o
5825 && (elf_section_data (o)->sec_info_type
5826 == ELF_INFO_TYPE_EH_FRAME_HDR))
5828 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5829 goto error_return;
5833 if (finfo.symstrtab != NULL)
5834 _bfd_stringtab_free (finfo.symstrtab);
5835 if (finfo.contents != NULL)
5836 free (finfo.contents);
5837 if (finfo.external_relocs != NULL)
5838 free (finfo.external_relocs);
5839 if (finfo.internal_relocs != NULL)
5840 free (finfo.internal_relocs);
5841 if (finfo.external_syms != NULL)
5842 free (finfo.external_syms);
5843 if (finfo.locsym_shndx != NULL)
5844 free (finfo.locsym_shndx);
5845 if (finfo.internal_syms != NULL)
5846 free (finfo.internal_syms);
5847 if (finfo.indices != NULL)
5848 free (finfo.indices);
5849 if (finfo.sections != NULL)
5850 free (finfo.sections);
5851 if (finfo.symbuf != NULL)
5852 free (finfo.symbuf);
5853 if (finfo.symshndxbuf != NULL)
5854 free (finfo.symbuf);
5855 for (o = abfd->sections; o != NULL; o = o->next)
5857 if ((o->flags & SEC_RELOC) != 0
5858 && elf_section_data (o)->rel_hashes != NULL)
5859 free (elf_section_data (o)->rel_hashes);
5862 elf_tdata (abfd)->linker = true;
5864 return true;
5866 error_return:
5867 if (finfo.symstrtab != NULL)
5868 _bfd_stringtab_free (finfo.symstrtab);
5869 if (finfo.contents != NULL)
5870 free (finfo.contents);
5871 if (finfo.external_relocs != NULL)
5872 free (finfo.external_relocs);
5873 if (finfo.internal_relocs != NULL)
5874 free (finfo.internal_relocs);
5875 if (finfo.external_syms != NULL)
5876 free (finfo.external_syms);
5877 if (finfo.locsym_shndx != NULL)
5878 free (finfo.locsym_shndx);
5879 if (finfo.internal_syms != NULL)
5880 free (finfo.internal_syms);
5881 if (finfo.indices != NULL)
5882 free (finfo.indices);
5883 if (finfo.sections != NULL)
5884 free (finfo.sections);
5885 if (finfo.symbuf != NULL)
5886 free (finfo.symbuf);
5887 if (finfo.symshndxbuf != NULL)
5888 free (finfo.symbuf);
5889 for (o = abfd->sections; o != NULL; o = o->next)
5891 if ((o->flags & SEC_RELOC) != 0
5892 && elf_section_data (o)->rel_hashes != NULL)
5893 free (elf_section_data (o)->rel_hashes);
5896 return false;
5899 /* Add a symbol to the output symbol table. */
5901 static boolean
5902 elf_link_output_sym (finfo, name, elfsym, input_sec)
5903 struct elf_final_link_info *finfo;
5904 const char *name;
5905 Elf_Internal_Sym *elfsym;
5906 asection *input_sec;
5908 Elf_External_Sym *dest;
5909 Elf_External_Sym_Shndx *destshndx;
5911 boolean (*output_symbol_hook) PARAMS ((bfd *,
5912 struct bfd_link_info *info,
5913 const char *,
5914 Elf_Internal_Sym *,
5915 asection *));
5917 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5918 elf_backend_link_output_symbol_hook;
5919 if (output_symbol_hook != NULL)
5921 if (! ((*output_symbol_hook)
5922 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5923 return false;
5926 if (name == (const char *) NULL || *name == '\0')
5927 elfsym->st_name = 0;
5928 else if (input_sec->flags & SEC_EXCLUDE)
5929 elfsym->st_name = 0;
5930 else
5932 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5933 name, true, false);
5934 if (elfsym->st_name == (unsigned long) -1)
5935 return false;
5938 if (finfo->symbuf_count >= finfo->symbuf_size)
5940 if (! elf_link_flush_output_syms (finfo))
5941 return false;
5944 dest = finfo->symbuf + finfo->symbuf_count;
5945 destshndx = finfo->symshndxbuf;
5946 if (destshndx != NULL)
5947 destshndx += finfo->symbuf_count;
5948 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5949 ++finfo->symbuf_count;
5951 ++ bfd_get_symcount (finfo->output_bfd);
5953 return true;
5956 /* Flush the output symbols to the file. */
5958 static boolean
5959 elf_link_flush_output_syms (finfo)
5960 struct elf_final_link_info *finfo;
5962 if (finfo->symbuf_count > 0)
5964 Elf_Internal_Shdr *hdr;
5965 file_ptr pos;
5966 bfd_size_type amt;
5968 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5969 pos = hdr->sh_offset + hdr->sh_size;
5970 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5971 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5972 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5973 return false;
5975 hdr->sh_size += amt;
5977 if (finfo->symshndxbuf != NULL)
5979 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5980 pos = hdr->sh_offset + hdr->sh_size;
5981 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5982 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5983 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5984 != amt))
5985 return false;
5987 hdr->sh_size += amt;
5990 finfo->symbuf_count = 0;
5993 return true;
5996 /* Adjust all external symbols pointing into SEC_MERGE sections
5997 to reflect the object merging within the sections. */
5999 static boolean
6000 elf_link_sec_merge_syms (h, data)
6001 struct elf_link_hash_entry *h;
6002 PTR data;
6004 asection *sec;
6006 if (h->root.type == bfd_link_hash_warning)
6007 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6009 if ((h->root.type == bfd_link_hash_defined
6010 || h->root.type == bfd_link_hash_defweak)
6011 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
6012 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6014 bfd *output_bfd = (bfd *) data;
6016 h->root.u.def.value =
6017 _bfd_merged_section_offset (output_bfd,
6018 &h->root.u.def.section,
6019 elf_section_data (sec)->sec_info,
6020 h->root.u.def.value, (bfd_vma) 0);
6023 return true;
6026 /* Add an external symbol to the symbol table. This is called from
6027 the hash table traversal routine. When generating a shared object,
6028 we go through the symbol table twice. The first time we output
6029 anything that might have been forced to local scope in a version
6030 script. The second time we output the symbols that are still
6031 global symbols. */
6033 static boolean
6034 elf_link_output_extsym (h, data)
6035 struct elf_link_hash_entry *h;
6036 PTR data;
6038 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6039 struct elf_final_link_info *finfo = eoinfo->finfo;
6040 boolean strip;
6041 Elf_Internal_Sym sym;
6042 asection *input_sec;
6044 if (h->root.type == bfd_link_hash_warning)
6046 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6047 if (h->root.type == bfd_link_hash_new)
6048 return true;
6051 /* Decide whether to output this symbol in this pass. */
6052 if (eoinfo->localsyms)
6054 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6055 return true;
6057 else
6059 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6060 return true;
6063 /* If we are not creating a shared library, and this symbol is
6064 referenced by a shared library but is not defined anywhere, then
6065 warn that it is undefined. If we do not do this, the runtime
6066 linker will complain that the symbol is undefined when the
6067 program is run. We don't have to worry about symbols that are
6068 referenced by regular files, because we will already have issued
6069 warnings for them. */
6070 if (! finfo->info->relocateable
6071 && ! finfo->info->allow_shlib_undefined
6072 && ! finfo->info->shared
6073 && h->root.type == bfd_link_hash_undefined
6074 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6075 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6077 if (! ((*finfo->info->callbacks->undefined_symbol)
6078 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6079 (asection *) NULL, (bfd_vma) 0, true)))
6081 eoinfo->failed = true;
6082 return false;
6086 /* We don't want to output symbols that have never been mentioned by
6087 a regular file, or that we have been told to strip. However, if
6088 h->indx is set to -2, the symbol is used by a reloc and we must
6089 output it. */
6090 if (h->indx == -2)
6091 strip = false;
6092 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6093 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6094 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6095 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6096 strip = true;
6097 else if (finfo->info->strip == strip_all
6098 || (finfo->info->strip == strip_some
6099 && bfd_hash_lookup (finfo->info->keep_hash,
6100 h->root.root.string,
6101 false, false) == NULL))
6102 strip = true;
6103 else
6104 strip = false;
6106 /* If we're stripping it, and it's not a dynamic symbol, there's
6107 nothing else to do unless it is a forced local symbol. */
6108 if (strip
6109 && h->dynindx == -1
6110 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6111 return true;
6113 sym.st_value = 0;
6114 sym.st_size = h->size;
6115 sym.st_other = h->other;
6116 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6117 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6118 else if (h->root.type == bfd_link_hash_undefweak
6119 || h->root.type == bfd_link_hash_defweak)
6120 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6121 else
6122 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6124 switch (h->root.type)
6126 default:
6127 case bfd_link_hash_new:
6128 case bfd_link_hash_warning:
6129 abort ();
6130 return false;
6132 case bfd_link_hash_undefined:
6133 case bfd_link_hash_undefweak:
6134 input_sec = bfd_und_section_ptr;
6135 sym.st_shndx = SHN_UNDEF;
6136 break;
6138 case bfd_link_hash_defined:
6139 case bfd_link_hash_defweak:
6141 input_sec = h->root.u.def.section;
6142 if (input_sec->output_section != NULL)
6144 sym.st_shndx =
6145 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6146 input_sec->output_section);
6147 if (sym.st_shndx == SHN_BAD)
6149 (*_bfd_error_handler)
6150 (_("%s: could not find output section %s for input section %s"),
6151 bfd_get_filename (finfo->output_bfd),
6152 input_sec->output_section->name,
6153 input_sec->name);
6154 eoinfo->failed = true;
6155 return false;
6158 /* ELF symbols in relocateable files are section relative,
6159 but in nonrelocateable files they are virtual
6160 addresses. */
6161 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6162 if (! finfo->info->relocateable)
6164 sym.st_value += input_sec->output_section->vma;
6165 if (h->type == STT_TLS)
6167 /* STT_TLS symbols are relative to PT_TLS segment
6168 base. */
6169 BFD_ASSERT (finfo->first_tls_sec != NULL);
6170 sym.st_value -= finfo->first_tls_sec->vma;
6174 else
6176 BFD_ASSERT (input_sec->owner == NULL
6177 || (input_sec->owner->flags & DYNAMIC) != 0);
6178 sym.st_shndx = SHN_UNDEF;
6179 input_sec = bfd_und_section_ptr;
6182 break;
6184 case bfd_link_hash_common:
6185 input_sec = h->root.u.c.p->section;
6186 sym.st_shndx = SHN_COMMON;
6187 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6188 break;
6190 case bfd_link_hash_indirect:
6191 /* These symbols are created by symbol versioning. They point
6192 to the decorated version of the name. For example, if the
6193 symbol foo@@GNU_1.2 is the default, which should be used when
6194 foo is used with no version, then we add an indirect symbol
6195 foo which points to foo@@GNU_1.2. We ignore these symbols,
6196 since the indirected symbol is already in the hash table. */
6197 return true;
6200 /* Give the processor backend a chance to tweak the symbol value,
6201 and also to finish up anything that needs to be done for this
6202 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6203 forced local syms when non-shared is due to a historical quirk. */
6204 if ((h->dynindx != -1
6205 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6206 && (finfo->info->shared
6207 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6208 && elf_hash_table (finfo->info)->dynamic_sections_created)
6210 struct elf_backend_data *bed;
6212 bed = get_elf_backend_data (finfo->output_bfd);
6213 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6214 (finfo->output_bfd, finfo->info, h, &sym)))
6216 eoinfo->failed = true;
6217 return false;
6221 /* If we are marking the symbol as undefined, and there are no
6222 non-weak references to this symbol from a regular object, then
6223 mark the symbol as weak undefined; if there are non-weak
6224 references, mark the symbol as strong. We can't do this earlier,
6225 because it might not be marked as undefined until the
6226 finish_dynamic_symbol routine gets through with it. */
6227 if (sym.st_shndx == SHN_UNDEF
6228 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6229 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6230 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6232 int bindtype;
6234 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6235 bindtype = STB_GLOBAL;
6236 else
6237 bindtype = STB_WEAK;
6238 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6241 /* If a symbol is not defined locally, we clear the visibility
6242 field. */
6243 if (! finfo->info->relocateable
6244 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6245 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6247 /* If this symbol should be put in the .dynsym section, then put it
6248 there now. We have already know the symbol index. We also fill
6249 in the entry in the .hash section. */
6250 if (h->dynindx != -1
6251 && elf_hash_table (finfo->info)->dynamic_sections_created)
6253 size_t bucketcount;
6254 size_t bucket;
6255 size_t hash_entry_size;
6256 bfd_byte *bucketpos;
6257 bfd_vma chain;
6258 Elf_External_Sym *esym;
6260 sym.st_name = h->dynstr_index;
6261 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6262 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6264 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6265 bucket = h->elf_hash_value % bucketcount;
6266 hash_entry_size
6267 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6268 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6269 + (bucket + 2) * hash_entry_size);
6270 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6271 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6272 bucketpos);
6273 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6274 ((bfd_byte *) finfo->hash_sec->contents
6275 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6277 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6279 Elf_Internal_Versym iversym;
6280 Elf_External_Versym *eversym;
6282 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6284 if (h->verinfo.verdef == NULL)
6285 iversym.vs_vers = 0;
6286 else
6287 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6289 else
6291 if (h->verinfo.vertree == NULL)
6292 iversym.vs_vers = 1;
6293 else
6294 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6297 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6298 iversym.vs_vers |= VERSYM_HIDDEN;
6300 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6301 eversym += h->dynindx;
6302 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6306 /* If we're stripping it, then it was just a dynamic symbol, and
6307 there's nothing else to do. */
6308 if (strip)
6309 return true;
6311 h->indx = bfd_get_symcount (finfo->output_bfd);
6313 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6315 eoinfo->failed = true;
6316 return false;
6319 return true;
6322 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6323 originated from the section given by INPUT_REL_HDR) to the
6324 OUTPUT_BFD. */
6326 static boolean
6327 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6328 internal_relocs)
6329 bfd *output_bfd;
6330 asection *input_section;
6331 Elf_Internal_Shdr *input_rel_hdr;
6332 Elf_Internal_Rela *internal_relocs;
6334 Elf_Internal_Rela *irela;
6335 Elf_Internal_Rela *irelaend;
6336 Elf_Internal_Shdr *output_rel_hdr;
6337 asection *output_section;
6338 unsigned int *rel_countp = NULL;
6339 struct elf_backend_data *bed;
6340 bfd_size_type amt;
6342 output_section = input_section->output_section;
6343 output_rel_hdr = NULL;
6345 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6346 == input_rel_hdr->sh_entsize)
6348 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6349 rel_countp = &elf_section_data (output_section)->rel_count;
6351 else if (elf_section_data (output_section)->rel_hdr2
6352 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6353 == input_rel_hdr->sh_entsize))
6355 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6356 rel_countp = &elf_section_data (output_section)->rel_count2;
6358 else
6360 (*_bfd_error_handler) (
6361 _("%s: relocation size mismatch in %s section %s"),
6362 bfd_get_filename (output_bfd),
6363 bfd_archive_filename (input_section->owner),
6364 input_section->name);
6365 bfd_set_error (bfd_error_wrong_object_format);
6366 return false;
6369 bed = get_elf_backend_data (output_bfd);
6370 irela = internal_relocs;
6371 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6372 * bed->s->int_rels_per_ext_rel;
6374 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6376 Elf_External_Rel *erel;
6377 Elf_Internal_Rel *irel;
6379 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6380 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6381 if (irel == NULL)
6383 (*_bfd_error_handler) (_("Error: out of memory"));
6384 abort ();
6387 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6388 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6390 unsigned int i;
6392 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6394 irel[i].r_offset = irela[i].r_offset;
6395 irel[i].r_info = irela[i].r_info;
6396 BFD_ASSERT (irela[i].r_addend == 0);
6399 if (bed->s->swap_reloc_out)
6400 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6401 else
6402 elf_swap_reloc_out (output_bfd, irel, erel);
6405 free (irel);
6407 else
6409 Elf_External_Rela *erela;
6411 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6413 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6414 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6415 if (bed->s->swap_reloca_out)
6416 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6417 else
6418 elf_swap_reloca_out (output_bfd, irela, erela);
6421 /* Bump the counter, so that we know where to add the next set of
6422 relocations. */
6423 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6425 return true;
6428 /* Link an input file into the linker output file. This function
6429 handles all the sections and relocations of the input file at once.
6430 This is so that we only have to read the local symbols once, and
6431 don't have to keep them in memory. */
6433 static boolean
6434 elf_link_input_bfd (finfo, input_bfd)
6435 struct elf_final_link_info *finfo;
6436 bfd *input_bfd;
6438 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6439 bfd *, asection *, bfd_byte *,
6440 Elf_Internal_Rela *,
6441 Elf_Internal_Sym *, asection **));
6442 bfd *output_bfd;
6443 Elf_Internal_Shdr *symtab_hdr;
6444 Elf_Internal_Shdr *shndx_hdr;
6445 size_t locsymcount;
6446 size_t extsymoff;
6447 Elf_External_Sym *external_syms;
6448 Elf_External_Sym *esym;
6449 Elf_External_Sym *esymend;
6450 Elf_External_Sym_Shndx *shndx_buf;
6451 Elf_External_Sym_Shndx *shndx;
6452 Elf_Internal_Sym *isym;
6453 long *pindex;
6454 asection **ppsection;
6455 asection *o;
6456 struct elf_backend_data *bed;
6457 boolean emit_relocs;
6458 struct elf_link_hash_entry **sym_hashes;
6460 output_bfd = finfo->output_bfd;
6461 bed = get_elf_backend_data (output_bfd);
6462 relocate_section = bed->elf_backend_relocate_section;
6464 /* If this is a dynamic object, we don't want to do anything here:
6465 we don't want the local symbols, and we don't want the section
6466 contents. */
6467 if ((input_bfd->flags & DYNAMIC) != 0)
6468 return true;
6470 emit_relocs = (finfo->info->relocateable
6471 || finfo->info->emitrelocations
6472 || bed->elf_backend_emit_relocs);
6474 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6475 if (elf_bad_symtab (input_bfd))
6477 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6478 extsymoff = 0;
6480 else
6482 locsymcount = symtab_hdr->sh_info;
6483 extsymoff = symtab_hdr->sh_info;
6486 /* Read the local symbols. */
6487 if (symtab_hdr->contents != NULL)
6488 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6489 else if (locsymcount == 0)
6490 external_syms = NULL;
6491 else
6493 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6494 external_syms = finfo->external_syms;
6495 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6496 || bfd_bread (external_syms, amt, input_bfd) != amt)
6497 return false;
6500 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6501 shndx_buf = NULL;
6502 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6504 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6505 shndx_buf = finfo->locsym_shndx;
6506 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6507 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6508 return false;
6511 /* Swap in the local symbols and write out the ones which we know
6512 are going into the output file. */
6513 for (esym = external_syms, esymend = esym + locsymcount,
6514 isym = finfo->internal_syms, pindex = finfo->indices,
6515 ppsection = finfo->sections, shndx = shndx_buf;
6516 esym < esymend;
6517 esym++, isym++, pindex++, ppsection++,
6518 shndx = (shndx != NULL ? shndx + 1 : NULL))
6520 asection *isec;
6521 const char *name;
6522 Elf_Internal_Sym osym;
6524 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
6525 *pindex = -1;
6527 if (elf_bad_symtab (input_bfd))
6529 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6531 *ppsection = NULL;
6532 continue;
6536 if (isym->st_shndx == SHN_UNDEF)
6537 isec = bfd_und_section_ptr;
6538 else if (isym->st_shndx < SHN_LORESERVE
6539 || isym->st_shndx > SHN_HIRESERVE)
6541 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6542 if (isec
6543 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6544 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6545 isym->st_value =
6546 _bfd_merged_section_offset (output_bfd, &isec,
6547 elf_section_data (isec)->sec_info,
6548 isym->st_value, (bfd_vma) 0);
6550 else if (isym->st_shndx == SHN_ABS)
6551 isec = bfd_abs_section_ptr;
6552 else if (isym->st_shndx == SHN_COMMON)
6553 isec = bfd_com_section_ptr;
6554 else
6556 /* Who knows? */
6557 isec = NULL;
6560 *ppsection = isec;
6562 /* Don't output the first, undefined, symbol. */
6563 if (esym == external_syms)
6564 continue;
6566 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6568 /* We never output section symbols. Instead, we use the
6569 section symbol of the corresponding section in the output
6570 file. */
6571 continue;
6574 /* If we are stripping all symbols, we don't want to output this
6575 one. */
6576 if (finfo->info->strip == strip_all)
6577 continue;
6579 /* If we are discarding all local symbols, we don't want to
6580 output this one. If we are generating a relocateable output
6581 file, then some of the local symbols may be required by
6582 relocs; we output them below as we discover that they are
6583 needed. */
6584 if (finfo->info->discard == discard_all)
6585 continue;
6587 /* If this symbol is defined in a section which we are
6588 discarding, we don't need to keep it, but note that
6589 linker_mark is only reliable for sections that have contents.
6590 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6591 as well as linker_mark. */
6592 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6593 && isec != NULL
6594 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6595 || (! finfo->info->relocateable
6596 && (isec->flags & SEC_EXCLUDE) != 0)))
6597 continue;
6599 /* Get the name of the symbol. */
6600 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6601 isym->st_name);
6602 if (name == NULL)
6603 return false;
6605 /* See if we are discarding symbols with this name. */
6606 if ((finfo->info->strip == strip_some
6607 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6608 == NULL))
6609 || (((finfo->info->discard == discard_sec_merge
6610 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6611 || finfo->info->discard == discard_l)
6612 && bfd_is_local_label_name (input_bfd, name)))
6613 continue;
6615 /* If we get here, we are going to output this symbol. */
6617 osym = *isym;
6619 /* Adjust the section index for the output file. */
6620 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6621 isec->output_section);
6622 if (osym.st_shndx == SHN_BAD)
6623 return false;
6625 *pindex = bfd_get_symcount (output_bfd);
6627 /* ELF symbols in relocateable files are section relative, but
6628 in executable files they are virtual addresses. Note that
6629 this code assumes that all ELF sections have an associated
6630 BFD section with a reasonable value for output_offset; below
6631 we assume that they also have a reasonable value for
6632 output_section. Any special sections must be set up to meet
6633 these requirements. */
6634 osym.st_value += isec->output_offset;
6635 if (! finfo->info->relocateable)
6637 osym.st_value += isec->output_section->vma;
6638 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6640 /* STT_TLS symbols are relative to PT_TLS segment base. */
6641 BFD_ASSERT (finfo->first_tls_sec != NULL);
6642 osym.st_value -= finfo->first_tls_sec->vma;
6646 if (! elf_link_output_sym (finfo, name, &osym, isec))
6647 return false;
6650 /* Relocate the contents of each section. */
6651 sym_hashes = elf_sym_hashes (input_bfd);
6652 for (o = input_bfd->sections; o != NULL; o = o->next)
6654 bfd_byte *contents;
6656 if (! o->linker_mark)
6658 /* This section was omitted from the link. */
6659 continue;
6662 if ((o->flags & SEC_HAS_CONTENTS) == 0
6663 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6664 continue;
6666 if ((o->flags & SEC_LINKER_CREATED) != 0)
6668 /* Section was created by elf_link_create_dynamic_sections
6669 or somesuch. */
6670 continue;
6673 /* Get the contents of the section. They have been cached by a
6674 relaxation routine. Note that o is a section in an input
6675 file, so the contents field will not have been set by any of
6676 the routines which work on output files. */
6677 if (elf_section_data (o)->this_hdr.contents != NULL)
6678 contents = elf_section_data (o)->this_hdr.contents;
6679 else
6681 contents = finfo->contents;
6682 if (! bfd_get_section_contents (input_bfd, o, contents,
6683 (file_ptr) 0, o->_raw_size))
6684 return false;
6687 if ((o->flags & SEC_RELOC) != 0)
6689 Elf_Internal_Rela *internal_relocs;
6691 /* Get the swapped relocs. */
6692 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6693 (input_bfd, o, finfo->external_relocs,
6694 finfo->internal_relocs, false));
6695 if (internal_relocs == NULL
6696 && o->reloc_count > 0)
6697 return false;
6699 /* Run through the relocs looking for any against symbols
6700 from discarded sections and section symbols from
6701 removed link-once sections. Complain about relocs
6702 against discarded sections. Zero relocs against removed
6703 link-once sections. We should really complain if
6704 anything in the final link tries to use it, but
6705 DWARF-based exception handling might have an entry in
6706 .eh_frame to describe a routine in the linkonce section,
6707 and it turns out to be hard to remove the .eh_frame
6708 entry too. FIXME. */
6709 if (!finfo->info->relocateable
6710 && !elf_section_ignore_discarded_relocs (o))
6712 Elf_Internal_Rela *rel, *relend;
6714 rel = internal_relocs;
6715 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6716 for ( ; rel < relend; rel++)
6718 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6720 if (r_symndx >= locsymcount
6721 || (elf_bad_symtab (input_bfd)
6722 && finfo->sections[r_symndx] == NULL))
6724 struct elf_link_hash_entry *h;
6726 h = sym_hashes[r_symndx - extsymoff];
6727 while (h->root.type == bfd_link_hash_indirect
6728 || h->root.type == bfd_link_hash_warning)
6729 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6731 /* Complain if the definition comes from a
6732 discarded section. */
6733 if ((h->root.type == bfd_link_hash_defined
6734 || h->root.type == bfd_link_hash_defweak)
6735 && elf_discarded_section (h->root.u.def.section))
6737 #if BFD_VERSION_DATE < 20031005
6738 if ((o->flags & SEC_DEBUGGING) != 0)
6740 #if BFD_VERSION_DATE > 20021005
6741 (*finfo->info->callbacks->warning)
6742 (finfo->info,
6743 _("warning: relocation against removed section; zeroing"),
6744 NULL, input_bfd, o, rel->r_offset);
6745 #endif
6746 BFD_ASSERT (r_symndx != 0);
6747 memset (rel, 0, sizeof (*rel));
6749 else
6750 #endif
6752 if (! ((*finfo->info->callbacks->undefined_symbol)
6753 (finfo->info, h->root.root.string,
6754 input_bfd, o, rel->r_offset,
6755 true)))
6756 return false;
6760 else
6762 asection *sec = finfo->sections[r_symndx];
6764 if (sec != NULL && elf_discarded_section (sec))
6766 #if BFD_VERSION_DATE < 20031005
6767 if ((o->flags & SEC_DEBUGGING) != 0
6768 || (sec->flags & SEC_LINK_ONCE) != 0)
6770 #if BFD_VERSION_DATE > 20021005
6771 (*finfo->info->callbacks->warning)
6772 (finfo->info,
6773 _("warning: relocation against removed section"),
6774 NULL, input_bfd, o, rel->r_offset);
6775 #endif
6776 BFD_ASSERT (r_symndx != 0);
6777 rel->r_info
6778 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6779 rel->r_addend = 0;
6781 else
6782 #endif
6784 boolean ok;
6785 const char *msg
6786 = _("local symbols in discarded section %s");
6787 bfd_size_type amt
6788 = strlen (sec->name) + strlen (msg) - 1;
6789 char *buf = (char *) bfd_malloc (amt);
6791 if (buf != NULL)
6792 sprintf (buf, msg, sec->name);
6793 else
6794 buf = (char *) sec->name;
6795 ok = (*finfo->info->callbacks
6796 ->undefined_symbol) (finfo->info, buf,
6797 input_bfd, o,
6798 rel->r_offset,
6799 true);
6800 if (buf != sec->name)
6801 free (buf);
6802 if (!ok)
6803 return false;
6810 /* Relocate the section by invoking a back end routine.
6812 The back end routine is responsible for adjusting the
6813 section contents as necessary, and (if using Rela relocs
6814 and generating a relocateable output file) adjusting the
6815 reloc addend as necessary.
6817 The back end routine does not have to worry about setting
6818 the reloc address or the reloc symbol index.
6820 The back end routine is given a pointer to the swapped in
6821 internal symbols, and can access the hash table entries
6822 for the external symbols via elf_sym_hashes (input_bfd).
6824 When generating relocateable output, the back end routine
6825 must handle STB_LOCAL/STT_SECTION symbols specially. The
6826 output symbol is going to be a section symbol
6827 corresponding to the output section, which will require
6828 the addend to be adjusted. */
6830 if (! (*relocate_section) (output_bfd, finfo->info,
6831 input_bfd, o, contents,
6832 internal_relocs,
6833 finfo->internal_syms,
6834 finfo->sections))
6835 return false;
6837 if (emit_relocs)
6839 Elf_Internal_Rela *irela;
6840 Elf_Internal_Rela *irelaend;
6841 struct elf_link_hash_entry **rel_hash;
6842 Elf_Internal_Shdr *input_rel_hdr;
6843 unsigned int next_erel;
6844 boolean (*reloc_emitter) PARAMS ((bfd *, asection *,
6845 Elf_Internal_Shdr *,
6846 Elf_Internal_Rela *));
6847 boolean rela_normal;
6849 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6850 rela_normal = (bed->rela_normal
6851 && (input_rel_hdr->sh_entsize
6852 == sizeof (Elf_External_Rela)));
6854 /* Adjust the reloc addresses and symbol indices. */
6856 irela = internal_relocs;
6857 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6858 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6859 + elf_section_data (o->output_section)->rel_count
6860 + elf_section_data (o->output_section)->rel_count2);
6861 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6863 unsigned long r_symndx;
6864 asection *sec;
6866 if (next_erel == bed->s->int_rels_per_ext_rel)
6868 rel_hash++;
6869 next_erel = 0;
6872 irela->r_offset += o->output_offset;
6874 /* Relocs in an executable have to be virtual addresses. */
6875 if (!finfo->info->relocateable)
6876 irela->r_offset += o->output_section->vma;
6878 r_symndx = ELF_R_SYM (irela->r_info);
6880 if (r_symndx == 0)
6881 continue;
6883 if (r_symndx >= locsymcount
6884 || (elf_bad_symtab (input_bfd)
6885 && finfo->sections[r_symndx] == NULL))
6887 struct elf_link_hash_entry *rh;
6888 unsigned long indx;
6890 /* This is a reloc against a global symbol. We
6891 have not yet output all the local symbols, so
6892 we do not know the symbol index of any global
6893 symbol. We set the rel_hash entry for this
6894 reloc to point to the global hash table entry
6895 for this symbol. The symbol index is then
6896 set at the end of elf_bfd_final_link. */
6897 indx = r_symndx - extsymoff;
6898 rh = elf_sym_hashes (input_bfd)[indx];
6899 while (rh->root.type == bfd_link_hash_indirect
6900 || rh->root.type == bfd_link_hash_warning)
6901 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6903 /* Setting the index to -2 tells
6904 elf_link_output_extsym that this symbol is
6905 used by a reloc. */
6906 BFD_ASSERT (rh->indx < 0);
6907 rh->indx = -2;
6909 *rel_hash = rh;
6911 continue;
6914 /* This is a reloc against a local symbol. */
6916 *rel_hash = NULL;
6917 isym = finfo->internal_syms + r_symndx;
6918 sec = finfo->sections[r_symndx];
6919 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6921 /* I suppose the backend ought to fill in the
6922 section of any STT_SECTION symbol against a
6923 processor specific section. If we have
6924 discarded a section, the output_section will
6925 be the absolute section. */
6926 if (bfd_is_abs_section (sec)
6927 || (sec != NULL
6928 && bfd_is_abs_section (sec->output_section)))
6929 r_symndx = 0;
6930 else if (sec == NULL || sec->owner == NULL)
6932 bfd_set_error (bfd_error_bad_value);
6933 return false;
6935 else
6937 r_symndx = sec->output_section->target_index;
6938 BFD_ASSERT (r_symndx != 0);
6941 /* Adjust the addend according to where the
6942 section winds up in the output section. */
6943 if (rela_normal)
6944 irela->r_addend += sec->output_offset;
6946 else
6948 if (finfo->indices[r_symndx] == -1)
6950 unsigned long shlink;
6951 const char *name;
6952 asection *osec;
6954 if (finfo->info->strip == strip_all)
6956 /* You can't do ld -r -s. */
6957 bfd_set_error (bfd_error_invalid_operation);
6958 return false;
6961 /* This symbol was skipped earlier, but
6962 since it is needed by a reloc, we
6963 must output it now. */
6964 shlink = symtab_hdr->sh_link;
6965 name = (bfd_elf_string_from_elf_section
6966 (input_bfd, shlink, isym->st_name));
6967 if (name == NULL)
6968 return false;
6970 osec = sec->output_section;
6971 isym->st_shndx =
6972 _bfd_elf_section_from_bfd_section (output_bfd,
6973 osec);
6974 if (isym->st_shndx == SHN_BAD)
6975 return false;
6977 isym->st_value += sec->output_offset;
6978 if (! finfo->info->relocateable)
6980 isym->st_value += osec->vma;
6981 if (ELF_ST_TYPE (isym->st_info) == STT_TLS)
6983 /* STT_TLS symbols are relative to PT_TLS
6984 segment base. */
6985 BFD_ASSERT (finfo->first_tls_sec != NULL);
6986 isym->st_value -= finfo->first_tls_sec->vma;
6990 finfo->indices[r_symndx]
6991 = bfd_get_symcount (output_bfd);
6993 if (! elf_link_output_sym (finfo, name, isym, sec))
6994 return false;
6997 r_symndx = finfo->indices[r_symndx];
7000 irela->r_info = ELF_R_INFO (r_symndx,
7001 ELF_R_TYPE (irela->r_info));
7004 /* Swap out the relocs. */
7005 if (bed->elf_backend_emit_relocs
7006 && !(finfo->info->relocateable
7007 || finfo->info->emitrelocations))
7008 reloc_emitter = bed->elf_backend_emit_relocs;
7009 else
7010 reloc_emitter = elf_link_output_relocs;
7012 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7013 internal_relocs))
7014 return false;
7016 input_rel_hdr = elf_section_data (o)->rel_hdr2;
7017 if (input_rel_hdr)
7019 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7020 * bed->s->int_rels_per_ext_rel);
7021 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7022 internal_relocs))
7023 return false;
7029 /* Write out the modified section contents. */
7030 if (bed->elf_backend_write_section
7031 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7033 /* Section written out. */
7035 else switch (elf_section_data (o)->sec_info_type)
7037 case ELF_INFO_TYPE_STABS:
7038 if (! (_bfd_write_section_stabs
7039 (output_bfd,
7040 &elf_hash_table (finfo->info)->stab_info,
7041 o, &elf_section_data (o)->sec_info, contents)))
7042 return false;
7043 break;
7044 case ELF_INFO_TYPE_MERGE:
7045 if (! (_bfd_write_merged_section
7046 (output_bfd, o, elf_section_data (o)->sec_info)))
7047 return false;
7048 break;
7049 case ELF_INFO_TYPE_EH_FRAME:
7051 asection *ehdrsec;
7053 ehdrsec
7054 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
7055 ".eh_frame_hdr");
7056 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
7057 contents)))
7058 return false;
7060 break;
7061 default:
7063 bfd_size_type sec_size;
7065 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7066 if (! (o->flags & SEC_EXCLUDE)
7067 && ! bfd_set_section_contents (output_bfd, o->output_section,
7068 contents,
7069 (file_ptr) o->output_offset,
7070 sec_size))
7071 return false;
7073 break;
7077 return true;
7080 /* Generate a reloc when linking an ELF file. This is a reloc
7081 requested by the linker, and does come from any input file. This
7082 is used to build constructor and destructor tables when linking
7083 with -Ur. */
7085 static boolean
7086 elf_reloc_link_order (output_bfd, info, output_section, link_order)
7087 bfd *output_bfd;
7088 struct bfd_link_info *info;
7089 asection *output_section;
7090 struct bfd_link_order *link_order;
7092 reloc_howto_type *howto;
7093 long indx;
7094 bfd_vma offset;
7095 bfd_vma addend;
7096 struct elf_link_hash_entry **rel_hash_ptr;
7097 Elf_Internal_Shdr *rel_hdr;
7098 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7100 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7101 if (howto == NULL)
7103 bfd_set_error (bfd_error_bad_value);
7104 return false;
7107 addend = link_order->u.reloc.p->addend;
7109 /* Figure out the symbol index. */
7110 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7111 + elf_section_data (output_section)->rel_count
7112 + elf_section_data (output_section)->rel_count2);
7113 if (link_order->type == bfd_section_reloc_link_order)
7115 indx = link_order->u.reloc.p->u.section->target_index;
7116 BFD_ASSERT (indx != 0);
7117 *rel_hash_ptr = NULL;
7119 else
7121 struct elf_link_hash_entry *h;
7123 /* Treat a reloc against a defined symbol as though it were
7124 actually against the section. */
7125 h = ((struct elf_link_hash_entry *)
7126 bfd_wrapped_link_hash_lookup (output_bfd, info,
7127 link_order->u.reloc.p->u.name,
7128 false, false, true));
7129 if (h != NULL
7130 && (h->root.type == bfd_link_hash_defined
7131 || h->root.type == bfd_link_hash_defweak))
7133 asection *section;
7135 section = h->root.u.def.section;
7136 indx = section->output_section->target_index;
7137 *rel_hash_ptr = NULL;
7138 /* It seems that we ought to add the symbol value to the
7139 addend here, but in practice it has already been added
7140 because it was passed to constructor_callback. */
7141 addend += section->output_section->vma + section->output_offset;
7143 else if (h != NULL)
7145 /* Setting the index to -2 tells elf_link_output_extsym that
7146 this symbol is used by a reloc. */
7147 h->indx = -2;
7148 *rel_hash_ptr = h;
7149 indx = 0;
7151 else
7153 if (! ((*info->callbacks->unattached_reloc)
7154 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7155 (asection *) NULL, (bfd_vma) 0)))
7156 return false;
7157 indx = 0;
7161 /* If this is an inplace reloc, we must write the addend into the
7162 object file. */
7163 if (howto->partial_inplace && addend != 0)
7165 bfd_size_type size;
7166 bfd_reloc_status_type rstat;
7167 bfd_byte *buf;
7168 boolean ok;
7169 const char *sym_name;
7171 size = bfd_get_reloc_size (howto);
7172 buf = (bfd_byte *) bfd_zmalloc (size);
7173 if (buf == (bfd_byte *) NULL)
7174 return false;
7175 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
7176 switch (rstat)
7178 case bfd_reloc_ok:
7179 break;
7181 default:
7182 case bfd_reloc_outofrange:
7183 abort ();
7185 case bfd_reloc_overflow:
7186 if (link_order->type == bfd_section_reloc_link_order)
7187 sym_name = bfd_section_name (output_bfd,
7188 link_order->u.reloc.p->u.section);
7189 else
7190 sym_name = link_order->u.reloc.p->u.name;
7191 if (! ((*info->callbacks->reloc_overflow)
7192 (info, sym_name, howto->name, addend,
7193 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7195 free (buf);
7196 return false;
7198 break;
7200 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7201 (file_ptr) link_order->offset, size);
7202 free (buf);
7203 if (! ok)
7204 return false;
7207 /* The address of a reloc is relative to the section in a
7208 relocateable file, and is a virtual address in an executable
7209 file. */
7210 offset = link_order->offset;
7211 if (! info->relocateable)
7212 offset += output_section->vma;
7214 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7216 if (rel_hdr->sh_type == SHT_REL)
7218 bfd_size_type size;
7219 Elf_Internal_Rel *irel;
7220 Elf_External_Rel *erel;
7221 unsigned int i;
7223 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7224 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7225 if (irel == NULL)
7226 return false;
7228 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7229 irel[i].r_offset = offset;
7230 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7232 erel = ((Elf_External_Rel *) rel_hdr->contents
7233 + elf_section_data (output_section)->rel_count);
7235 if (bed->s->swap_reloc_out)
7236 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7237 else
7238 elf_swap_reloc_out (output_bfd, irel, erel);
7240 free (irel);
7242 else
7244 bfd_size_type size;
7245 Elf_Internal_Rela *irela;
7246 Elf_External_Rela *erela;
7247 unsigned int i;
7249 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7250 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7251 if (irela == NULL)
7252 return false;
7254 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7255 irela[i].r_offset = offset;
7256 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7257 irela[0].r_addend = addend;
7259 erela = ((Elf_External_Rela *) rel_hdr->contents
7260 + elf_section_data (output_section)->rel_count);
7262 if (bed->s->swap_reloca_out)
7263 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7264 else
7265 elf_swap_reloca_out (output_bfd, irela, erela);
7268 ++elf_section_data (output_section)->rel_count;
7270 return true;
7273 /* Allocate a pointer to live in a linker created section. */
7275 boolean
7276 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7277 bfd *abfd;
7278 struct bfd_link_info *info;
7279 elf_linker_section_t *lsect;
7280 struct elf_link_hash_entry *h;
7281 const Elf_Internal_Rela *rel;
7283 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7284 elf_linker_section_pointers_t *linker_section_ptr;
7285 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7286 bfd_size_type amt;
7288 BFD_ASSERT (lsect != NULL);
7290 /* Is this a global symbol? */
7291 if (h != NULL)
7293 /* Has this symbol already been allocated? If so, our work is done. */
7294 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7295 rel->r_addend,
7296 lsect->which))
7297 return true;
7299 ptr_linker_section_ptr = &h->linker_section_pointer;
7300 /* Make sure this symbol is output as a dynamic symbol. */
7301 if (h->dynindx == -1)
7303 if (! elf_link_record_dynamic_symbol (info, h))
7304 return false;
7307 if (lsect->rel_section)
7308 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7310 else
7312 /* Allocation of a pointer to a local symbol. */
7313 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7315 /* Allocate a table to hold the local symbols if first time. */
7316 if (!ptr)
7318 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7319 register unsigned int i;
7321 amt = num_symbols;
7322 amt *= sizeof (elf_linker_section_pointers_t *);
7323 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7325 if (!ptr)
7326 return false;
7328 elf_local_ptr_offsets (abfd) = ptr;
7329 for (i = 0; i < num_symbols; i++)
7330 ptr[i] = (elf_linker_section_pointers_t *) 0;
7333 /* Has this symbol already been allocated? If so, our work is done. */
7334 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7335 rel->r_addend,
7336 lsect->which))
7337 return true;
7339 ptr_linker_section_ptr = &ptr[r_symndx];
7341 if (info->shared)
7343 /* If we are generating a shared object, we need to
7344 output a R_<xxx>_RELATIVE reloc so that the
7345 dynamic linker can adjust this GOT entry. */
7346 BFD_ASSERT (lsect->rel_section != NULL);
7347 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7351 /* Allocate space for a pointer in the linker section, and allocate
7352 a new pointer record from internal memory. */
7353 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7354 amt = sizeof (elf_linker_section_pointers_t);
7355 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7357 if (!linker_section_ptr)
7358 return false;
7360 linker_section_ptr->next = *ptr_linker_section_ptr;
7361 linker_section_ptr->addend = rel->r_addend;
7362 linker_section_ptr->which = lsect->which;
7363 linker_section_ptr->written_address_p = false;
7364 *ptr_linker_section_ptr = linker_section_ptr;
7366 #if 0
7367 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7369 linker_section_ptr->offset = (lsect->section->_raw_size
7370 - lsect->hole_size + (ARCH_SIZE / 8));
7371 lsect->hole_offset += ARCH_SIZE / 8;
7372 lsect->sym_offset += ARCH_SIZE / 8;
7373 if (lsect->sym_hash)
7375 /* Bump up symbol value if needed. */
7376 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7377 #ifdef DEBUG
7378 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7379 lsect->sym_hash->root.root.string,
7380 (long) ARCH_SIZE / 8,
7381 (long) lsect->sym_hash->root.u.def.value);
7382 #endif
7385 else
7386 #endif
7387 linker_section_ptr->offset = lsect->section->_raw_size;
7389 lsect->section->_raw_size += ARCH_SIZE / 8;
7391 #ifdef DEBUG
7392 fprintf (stderr,
7393 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7394 lsect->name, (long) linker_section_ptr->offset,
7395 (long) lsect->section->_raw_size);
7396 #endif
7398 return true;
7401 #if ARCH_SIZE==64
7402 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7403 #endif
7404 #if ARCH_SIZE==32
7405 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7406 #endif
7408 /* Fill in the address for a pointer generated in a linker section. */
7410 bfd_vma
7411 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7412 relocation, rel, relative_reloc)
7413 bfd *output_bfd;
7414 bfd *input_bfd;
7415 struct bfd_link_info *info;
7416 elf_linker_section_t *lsect;
7417 struct elf_link_hash_entry *h;
7418 bfd_vma relocation;
7419 const Elf_Internal_Rela *rel;
7420 int relative_reloc;
7422 elf_linker_section_pointers_t *linker_section_ptr;
7424 BFD_ASSERT (lsect != NULL);
7426 if (h != NULL)
7428 /* Handle global symbol. */
7429 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7430 (h->linker_section_pointer,
7431 rel->r_addend,
7432 lsect->which));
7434 BFD_ASSERT (linker_section_ptr != NULL);
7436 if (! elf_hash_table (info)->dynamic_sections_created
7437 || (info->shared
7438 && info->symbolic
7439 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7441 /* This is actually a static link, or it is a
7442 -Bsymbolic link and the symbol is defined
7443 locally. We must initialize this entry in the
7444 global section.
7446 When doing a dynamic link, we create a .rela.<xxx>
7447 relocation entry to initialize the value. This
7448 is done in the finish_dynamic_symbol routine. */
7449 if (!linker_section_ptr->written_address_p)
7451 linker_section_ptr->written_address_p = true;
7452 bfd_put_ptr (output_bfd,
7453 relocation + linker_section_ptr->addend,
7454 (lsect->section->contents
7455 + linker_section_ptr->offset));
7459 else
7461 /* Handle local symbol. */
7462 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7463 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7464 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7465 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7466 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7467 rel->r_addend,
7468 lsect->which));
7470 BFD_ASSERT (linker_section_ptr != NULL);
7472 /* Write out pointer if it hasn't been rewritten out before. */
7473 if (!linker_section_ptr->written_address_p)
7475 linker_section_ptr->written_address_p = true;
7476 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7477 lsect->section->contents + linker_section_ptr->offset);
7479 if (info->shared)
7481 asection *srel = lsect->rel_section;
7482 Elf_Internal_Rela *outrel;
7483 Elf_External_Rela *erel;
7484 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7485 unsigned int i;
7486 bfd_size_type amt;
7488 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7489 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7490 if (outrel == NULL)
7492 (*_bfd_error_handler) (_("Error: out of memory"));
7493 return 0;
7496 /* We need to generate a relative reloc for the dynamic
7497 linker. */
7498 if (!srel)
7500 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7501 lsect->rel_name);
7502 lsect->rel_section = srel;
7505 BFD_ASSERT (srel != NULL);
7507 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7508 outrel[i].r_offset = (lsect->section->output_section->vma
7509 + lsect->section->output_offset
7510 + linker_section_ptr->offset);
7511 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7512 outrel[0].r_addend = 0;
7513 erel = (Elf_External_Rela *) lsect->section->contents;
7514 erel += elf_section_data (lsect->section)->rel_count;
7515 elf_swap_reloca_out (output_bfd, outrel, erel);
7516 ++elf_section_data (lsect->section)->rel_count;
7518 free (outrel);
7523 relocation = (lsect->section->output_offset
7524 + linker_section_ptr->offset
7525 - lsect->hole_offset
7526 - lsect->sym_offset);
7528 #ifdef DEBUG
7529 fprintf (stderr,
7530 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7531 lsect->name, (long) relocation, (long) relocation);
7532 #endif
7534 /* Subtract out the addend, because it will get added back in by the normal
7535 processing. */
7536 return relocation - linker_section_ptr->addend;
7539 /* Garbage collect unused sections. */
7541 static boolean elf_gc_mark
7542 PARAMS ((struct bfd_link_info *info, asection *sec,
7543 asection * (*gc_mark_hook)
7544 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7545 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7547 static boolean elf_gc_sweep
7548 PARAMS ((struct bfd_link_info *info,
7549 boolean (*gc_sweep_hook)
7550 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7551 const Elf_Internal_Rela *relocs))));
7553 static boolean elf_gc_sweep_symbol
7554 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7556 static boolean elf_gc_allocate_got_offsets
7557 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7559 static boolean elf_gc_propagate_vtable_entries_used
7560 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7562 static boolean elf_gc_smash_unused_vtentry_relocs
7563 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7565 /* The mark phase of garbage collection. For a given section, mark
7566 it and any sections in this section's group, and all the sections
7567 which define symbols to which it refers. */
7569 static boolean
7570 elf_gc_mark (info, sec, gc_mark_hook)
7571 struct bfd_link_info *info;
7572 asection *sec;
7573 asection * (*gc_mark_hook)
7574 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7575 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7577 boolean ret;
7578 asection *group_sec;
7580 sec->gc_mark = 1;
7582 /* Mark all the sections in the group. */
7583 group_sec = elf_section_data (sec)->next_in_group;
7584 if (group_sec && !group_sec->gc_mark)
7585 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7586 return false;
7588 /* Look through the section relocs. */
7589 ret = true;
7590 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7592 Elf_Internal_Rela *relstart, *rel, *relend;
7593 Elf_Internal_Shdr *symtab_hdr;
7594 Elf_Internal_Shdr *shndx_hdr;
7595 struct elf_link_hash_entry **sym_hashes;
7596 size_t nlocsyms;
7597 size_t extsymoff;
7598 Elf_External_Sym *locsyms, *freesyms = NULL;
7599 Elf_External_Sym_Shndx *locsym_shndx;
7600 bfd *input_bfd = sec->owner;
7601 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7603 /* GCFIXME: how to arrange so that relocs and symbols are not
7604 reread continually? */
7606 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7607 sym_hashes = elf_sym_hashes (input_bfd);
7609 /* Read the local symbols. */
7610 if (elf_bad_symtab (input_bfd))
7612 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7613 extsymoff = 0;
7615 else
7616 extsymoff = nlocsyms = symtab_hdr->sh_info;
7618 if (symtab_hdr->contents)
7619 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7620 else if (nlocsyms == 0)
7621 locsyms = NULL;
7622 else
7624 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7625 locsyms = freesyms = bfd_malloc (amt);
7626 if (freesyms == NULL
7627 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7628 || bfd_bread (locsyms, amt, input_bfd) != amt)
7630 ret = false;
7631 goto out1;
7635 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7636 locsym_shndx = NULL;
7637 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7639 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7640 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7641 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7642 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7643 return false;
7646 /* Read the relocations. */
7647 relstart = (NAME(_bfd_elf,link_read_relocs)
7648 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7649 info->keep_memory));
7650 if (relstart == NULL)
7652 ret = false;
7653 goto out1;
7655 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7657 for (rel = relstart; rel < relend; rel++)
7659 unsigned long r_symndx;
7660 asection *rsec;
7661 struct elf_link_hash_entry *h;
7662 Elf_Internal_Sym s;
7664 r_symndx = ELF_R_SYM (rel->r_info);
7665 if (r_symndx == 0)
7666 continue;
7668 if (elf_bad_symtab (sec->owner))
7670 elf_swap_symbol_in (input_bfd,
7671 locsyms + r_symndx,
7672 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7673 &s);
7674 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7675 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7676 else
7678 h = sym_hashes[r_symndx - extsymoff];
7679 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7682 else if (r_symndx >= nlocsyms)
7684 h = sym_hashes[r_symndx - extsymoff];
7685 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7687 else
7689 elf_swap_symbol_in (input_bfd,
7690 locsyms + r_symndx,
7691 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7692 &s);
7693 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7696 if (rsec && !rsec->gc_mark)
7698 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7699 rsec->gc_mark = 1;
7700 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7702 ret = false;
7703 goto out2;
7708 out2:
7709 if (!info->keep_memory)
7710 free (relstart);
7711 out1:
7712 if (freesyms)
7713 free (freesyms);
7716 return ret;
7719 /* The sweep phase of garbage collection. Remove all garbage sections. */
7721 static boolean
7722 elf_gc_sweep (info, gc_sweep_hook)
7723 struct bfd_link_info *info;
7724 boolean (*gc_sweep_hook)
7725 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7726 const Elf_Internal_Rela *relocs));
7728 bfd *sub;
7730 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7732 asection *o;
7734 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7735 continue;
7737 for (o = sub->sections; o != NULL; o = o->next)
7739 /* Keep special sections. Keep .debug sections. */
7740 if ((o->flags & SEC_LINKER_CREATED)
7741 || (o->flags & SEC_DEBUGGING))
7742 o->gc_mark = 1;
7744 if (o->gc_mark)
7745 continue;
7747 /* Skip sweeping sections already excluded. */
7748 if (o->flags & SEC_EXCLUDE)
7749 continue;
7751 /* Since this is early in the link process, it is simple
7752 to remove a section from the output. */
7753 o->flags |= SEC_EXCLUDE;
7755 /* But we also have to update some of the relocation
7756 info we collected before. */
7757 if (gc_sweep_hook
7758 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7760 Elf_Internal_Rela *internal_relocs;
7761 boolean r;
7763 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7764 (o->owner, o, NULL, NULL, info->keep_memory));
7765 if (internal_relocs == NULL)
7766 return false;
7768 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7770 if (!info->keep_memory)
7771 free (internal_relocs);
7773 if (!r)
7774 return false;
7779 /* Remove the symbols that were in the swept sections from the dynamic
7780 symbol table. GCFIXME: Anyone know how to get them out of the
7781 static symbol table as well? */
7783 int i = 0;
7785 elf_link_hash_traverse (elf_hash_table (info),
7786 elf_gc_sweep_symbol,
7787 (PTR) &i);
7789 elf_hash_table (info)->dynsymcount = i;
7792 return true;
7795 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7797 static boolean
7798 elf_gc_sweep_symbol (h, idxptr)
7799 struct elf_link_hash_entry *h;
7800 PTR idxptr;
7802 int *idx = (int *) idxptr;
7804 if (h->root.type == bfd_link_hash_warning)
7805 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7807 if (h->dynindx != -1
7808 && ((h->root.type != bfd_link_hash_defined
7809 && h->root.type != bfd_link_hash_defweak)
7810 || h->root.u.def.section->gc_mark))
7811 h->dynindx = (*idx)++;
7813 return true;
7816 /* Propogate collected vtable information. This is called through
7817 elf_link_hash_traverse. */
7819 static boolean
7820 elf_gc_propagate_vtable_entries_used (h, okp)
7821 struct elf_link_hash_entry *h;
7822 PTR okp;
7824 if (h->root.type == bfd_link_hash_warning)
7825 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7827 /* Those that are not vtables. */
7828 if (h->vtable_parent == NULL)
7829 return true;
7831 /* Those vtables that do not have parents, we cannot merge. */
7832 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7833 return true;
7835 /* If we've already been done, exit. */
7836 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7837 return true;
7839 /* Make sure the parent's table is up to date. */
7840 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7842 if (h->vtable_entries_used == NULL)
7844 /* None of this table's entries were referenced. Re-use the
7845 parent's table. */
7846 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7847 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7849 else
7851 size_t n;
7852 boolean *cu, *pu;
7854 /* Or the parent's entries into ours. */
7855 cu = h->vtable_entries_used;
7856 cu[-1] = true;
7857 pu = h->vtable_parent->vtable_entries_used;
7858 if (pu != NULL)
7860 asection *sec = h->root.u.def.section;
7861 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7862 int file_align = bed->s->file_align;
7864 n = h->vtable_parent->vtable_entries_size / file_align;
7865 while (n--)
7867 if (*pu)
7868 *cu = true;
7869 pu++;
7870 cu++;
7875 return true;
7878 static boolean
7879 elf_gc_smash_unused_vtentry_relocs (h, okp)
7880 struct elf_link_hash_entry *h;
7881 PTR okp;
7883 asection *sec;
7884 bfd_vma hstart, hend;
7885 Elf_Internal_Rela *relstart, *relend, *rel;
7886 struct elf_backend_data *bed;
7887 int file_align;
7889 if (h->root.type == bfd_link_hash_warning)
7890 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7892 /* Take care of both those symbols that do not describe vtables as
7893 well as those that are not loaded. */
7894 if (h->vtable_parent == NULL)
7895 return true;
7897 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7898 || h->root.type == bfd_link_hash_defweak);
7900 sec = h->root.u.def.section;
7901 hstart = h->root.u.def.value;
7902 hend = hstart + h->size;
7904 relstart = (NAME(_bfd_elf,link_read_relocs)
7905 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7906 if (!relstart)
7907 return *(boolean *) okp = false;
7908 bed = get_elf_backend_data (sec->owner);
7909 file_align = bed->s->file_align;
7911 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7913 for (rel = relstart; rel < relend; ++rel)
7914 if (rel->r_offset >= hstart && rel->r_offset < hend)
7916 /* If the entry is in use, do nothing. */
7917 if (h->vtable_entries_used
7918 && (rel->r_offset - hstart) < h->vtable_entries_size)
7920 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7921 if (h->vtable_entries_used[entry])
7922 continue;
7924 /* Otherwise, kill it. */
7925 rel->r_offset = rel->r_info = rel->r_addend = 0;
7928 return true;
7931 /* Do mark and sweep of unused sections. */
7933 boolean
7934 elf_gc_sections (abfd, info)
7935 bfd *abfd;
7936 struct bfd_link_info *info;
7938 boolean ok = true;
7939 bfd *sub;
7940 asection * (*gc_mark_hook)
7941 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7942 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7944 if (!get_elf_backend_data (abfd)->can_gc_sections
7945 || info->relocateable || info->emitrelocations
7946 || elf_hash_table (info)->dynamic_sections_created)
7947 return true;
7949 /* Apply transitive closure to the vtable entry usage info. */
7950 elf_link_hash_traverse (elf_hash_table (info),
7951 elf_gc_propagate_vtable_entries_used,
7952 (PTR) &ok);
7953 if (!ok)
7954 return false;
7956 /* Kill the vtable relocations that were not used. */
7957 elf_link_hash_traverse (elf_hash_table (info),
7958 elf_gc_smash_unused_vtentry_relocs,
7959 (PTR) &ok);
7960 if (!ok)
7961 return false;
7963 /* Grovel through relocs to find out who stays ... */
7965 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7966 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7968 asection *o;
7970 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7971 continue;
7973 for (o = sub->sections; o != NULL; o = o->next)
7975 if (o->flags & SEC_KEEP)
7976 if (!elf_gc_mark (info, o, gc_mark_hook))
7977 return false;
7981 /* ... and mark SEC_EXCLUDE for those that go. */
7982 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7983 return false;
7985 return true;
7988 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7990 boolean
7991 elf_gc_record_vtinherit (abfd, sec, h, offset)
7992 bfd *abfd;
7993 asection *sec;
7994 struct elf_link_hash_entry *h;
7995 bfd_vma offset;
7997 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7998 struct elf_link_hash_entry **search, *child;
7999 bfd_size_type extsymcount;
8001 /* The sh_info field of the symtab header tells us where the
8002 external symbols start. We don't care about the local symbols at
8003 this point. */
8004 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8005 if (!elf_bad_symtab (abfd))
8006 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8008 sym_hashes = elf_sym_hashes (abfd);
8009 sym_hashes_end = sym_hashes + extsymcount;
8011 /* Hunt down the child symbol, which is in this section at the same
8012 offset as the relocation. */
8013 for (search = sym_hashes; search != sym_hashes_end; ++search)
8015 if ((child = *search) != NULL
8016 && (child->root.type == bfd_link_hash_defined
8017 || child->root.type == bfd_link_hash_defweak)
8018 && child->root.u.def.section == sec
8019 && child->root.u.def.value == offset)
8020 goto win;
8023 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8024 bfd_archive_filename (abfd), sec->name,
8025 (unsigned long) offset);
8026 bfd_set_error (bfd_error_invalid_operation);
8027 return false;
8029 win:
8030 if (!h)
8032 /* This *should* only be the absolute section. It could potentially
8033 be that someone has defined a non-global vtable though, which
8034 would be bad. It isn't worth paging in the local symbols to be
8035 sure though; that case should simply be handled by the assembler. */
8037 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8039 else
8040 child->vtable_parent = h;
8042 return true;
8045 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8047 boolean
8048 elf_gc_record_vtentry (abfd, sec, h, addend)
8049 bfd *abfd ATTRIBUTE_UNUSED;
8050 asection *sec ATTRIBUTE_UNUSED;
8051 struct elf_link_hash_entry *h;
8052 bfd_vma addend;
8054 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8055 int file_align = bed->s->file_align;
8057 if (addend >= h->vtable_entries_size)
8059 size_t size, bytes;
8060 boolean *ptr = h->vtable_entries_used;
8062 /* While the symbol is undefined, we have to be prepared to handle
8063 a zero size. */
8064 if (h->root.type == bfd_link_hash_undefined)
8065 size = addend;
8066 else
8068 size = h->size;
8069 if (size < addend)
8071 /* Oops! We've got a reference past the defined end of
8072 the table. This is probably a bug -- shall we warn? */
8073 size = addend;
8077 /* Allocate one extra entry for use as a "done" flag for the
8078 consolidation pass. */
8079 bytes = (size / file_align + 1) * sizeof (boolean);
8081 if (ptr)
8083 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
8085 if (ptr != NULL)
8087 size_t oldbytes;
8089 oldbytes = ((h->vtable_entries_size / file_align + 1)
8090 * sizeof (boolean));
8091 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8094 else
8095 ptr = bfd_zmalloc ((bfd_size_type) bytes);
8097 if (ptr == NULL)
8098 return false;
8100 /* And arrange for that done flag to be at index -1. */
8101 h->vtable_entries_used = ptr + 1;
8102 h->vtable_entries_size = size;
8105 h->vtable_entries_used[addend / file_align] = true;
8107 return true;
8110 /* And an accompanying bit to work out final got entry offsets once
8111 we're done. Should be called from final_link. */
8113 boolean
8114 elf_gc_common_finalize_got_offsets (abfd, info)
8115 bfd *abfd;
8116 struct bfd_link_info *info;
8118 bfd *i;
8119 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8120 bfd_vma gotoff;
8122 /* The GOT offset is relative to the .got section, but the GOT header is
8123 put into the .got.plt section, if the backend uses it. */
8124 if (bed->want_got_plt)
8125 gotoff = 0;
8126 else
8127 gotoff = bed->got_header_size;
8129 /* Do the local .got entries first. */
8130 for (i = info->input_bfds; i; i = i->link_next)
8132 bfd_signed_vma *local_got;
8133 bfd_size_type j, locsymcount;
8134 Elf_Internal_Shdr *symtab_hdr;
8136 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8137 continue;
8139 local_got = elf_local_got_refcounts (i);
8140 if (!local_got)
8141 continue;
8143 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8144 if (elf_bad_symtab (i))
8145 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8146 else
8147 locsymcount = symtab_hdr->sh_info;
8149 for (j = 0; j < locsymcount; ++j)
8151 if (local_got[j] > 0)
8153 local_got[j] = gotoff;
8154 gotoff += ARCH_SIZE / 8;
8156 else
8157 local_got[j] = (bfd_vma) -1;
8161 /* Then the global .got entries. .plt refcounts are handled by
8162 adjust_dynamic_symbol */
8163 elf_link_hash_traverse (elf_hash_table (info),
8164 elf_gc_allocate_got_offsets,
8165 (PTR) &gotoff);
8166 return true;
8169 /* We need a special top-level link routine to convert got reference counts
8170 to real got offsets. */
8172 static boolean
8173 elf_gc_allocate_got_offsets (h, offarg)
8174 struct elf_link_hash_entry *h;
8175 PTR offarg;
8177 bfd_vma *off = (bfd_vma *) offarg;
8179 if (h->root.type == bfd_link_hash_warning)
8180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8182 if (h->got.refcount > 0)
8184 h->got.offset = off[0];
8185 off[0] += ARCH_SIZE / 8;
8187 else
8188 h->got.offset = (bfd_vma) -1;
8190 return true;
8193 /* Many folk need no more in the way of final link than this, once
8194 got entry reference counting is enabled. */
8196 boolean
8197 elf_gc_common_final_link (abfd, info)
8198 bfd *abfd;
8199 struct bfd_link_info *info;
8201 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8202 return false;
8204 /* Invoke the regular ELF backend linker to do all the work. */
8205 return elf_bfd_final_link (abfd, info);
8208 /* This function will be called though elf_link_hash_traverse to store
8209 all hash value of the exported symbols in an array. */
8211 static boolean
8212 elf_collect_hash_codes (h, data)
8213 struct elf_link_hash_entry *h;
8214 PTR data;
8216 unsigned long **valuep = (unsigned long **) data;
8217 const char *name;
8218 char *p;
8219 unsigned long ha;
8220 char *alc = NULL;
8222 if (h->root.type == bfd_link_hash_warning)
8223 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8225 /* Ignore indirect symbols. These are added by the versioning code. */
8226 if (h->dynindx == -1)
8227 return true;
8229 name = h->root.root.string;
8230 p = strchr (name, ELF_VER_CHR);
8231 if (p != NULL)
8233 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8234 memcpy (alc, name, (size_t) (p - name));
8235 alc[p - name] = '\0';
8236 name = alc;
8239 /* Compute the hash value. */
8240 ha = bfd_elf_hash (name);
8242 /* Store the found hash value in the array given as the argument. */
8243 *(*valuep)++ = ha;
8245 /* And store it in the struct so that we can put it in the hash table
8246 later. */
8247 h->elf_hash_value = ha;
8249 if (alc != NULL)
8250 free (alc);
8252 return true;
8255 boolean
8256 elf_reloc_symbol_deleted_p (offset, cookie)
8257 bfd_vma offset;
8258 PTR cookie;
8260 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8262 if (rcookie->bad_symtab)
8263 rcookie->rel = rcookie->rels;
8265 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8267 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8268 Elf_Internal_Sym isym;
8270 if (! rcookie->bad_symtab)
8271 if (rcookie->rel->r_offset > offset)
8272 return false;
8273 if (rcookie->rel->r_offset != offset)
8274 continue;
8276 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8278 Elf_External_Sym *lsym;
8279 Elf_External_Sym_Shndx *lshndx;
8281 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8282 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8283 if (lshndx != NULL)
8284 lshndx += r_symndx;
8285 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8288 if (r_symndx >= rcookie->locsymcount
8289 || (rcookie->locsyms
8290 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8292 struct elf_link_hash_entry *h;
8294 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8296 while (h->root.type == bfd_link_hash_indirect
8297 || h->root.type == bfd_link_hash_warning)
8298 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8300 if ((h->root.type == bfd_link_hash_defined
8301 || h->root.type == bfd_link_hash_defweak)
8302 && elf_discarded_section (h->root.u.def.section))
8303 return true;
8304 else
8305 return false;
8307 else if (rcookie->locsyms)
8309 /* It's not a relocation against a global symbol,
8310 but it could be a relocation against a local
8311 symbol for a discarded section. */
8312 asection *isec;
8314 /* Need to: get the symbol; get the section. */
8315 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8317 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8318 if (isec != NULL && elf_discarded_section (isec))
8319 return true;
8322 return false;
8324 return false;
8327 /* Discard unneeded references to discarded sections.
8328 Returns true if any section's size was changed. */
8329 /* This function assumes that the relocations are in sorted order,
8330 which is true for all known assemblers. */
8332 boolean
8333 elf_bfd_discard_info (output_bfd, info)
8334 bfd *output_bfd;
8335 struct bfd_link_info *info;
8337 struct elf_reloc_cookie cookie;
8338 asection *stab, *eh, *ehdr;
8339 Elf_Internal_Shdr *symtab_hdr;
8340 Elf_Internal_Shdr *shndx_hdr;
8341 Elf_External_Sym *freesyms;
8342 struct elf_backend_data *bed;
8343 bfd *abfd;
8344 boolean ret = false;
8345 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8347 if (info->relocateable
8348 || info->traditional_format
8349 || info->hash->creator->flavour != bfd_target_elf_flavour
8350 || ! is_elf_hash_table (info))
8351 return false;
8353 ehdr = NULL;
8354 if (elf_hash_table (info)->dynobj != NULL)
8355 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8356 ".eh_frame_hdr");
8358 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8360 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8361 continue;
8363 bed = get_elf_backend_data (abfd);
8365 if ((abfd->flags & DYNAMIC) != 0)
8366 continue;
8368 eh = NULL;
8369 if (ehdr)
8371 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8372 if (eh && (eh->_raw_size == 0
8373 || bfd_is_abs_section (eh->output_section)))
8374 eh = NULL;
8377 stab = NULL;
8378 if (!strip)
8380 stab = bfd_get_section_by_name (abfd, ".stab");
8381 if (stab && (stab->_raw_size == 0
8382 || bfd_is_abs_section (stab->output_section)))
8383 stab = NULL;
8385 if ((! stab
8386 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8387 && ! eh
8388 && (strip || ! bed->elf_backend_discard_info))
8389 continue;
8391 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8392 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8394 cookie.abfd = abfd;
8395 cookie.sym_hashes = elf_sym_hashes (abfd);
8396 cookie.bad_symtab = elf_bad_symtab (abfd);
8397 if (cookie.bad_symtab)
8399 cookie.locsymcount =
8400 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8401 cookie.extsymoff = 0;
8403 else
8405 cookie.locsymcount = symtab_hdr->sh_info;
8406 cookie.extsymoff = symtab_hdr->sh_info;
8409 freesyms = NULL;
8410 if (symtab_hdr->contents)
8411 cookie.locsyms = (void *) symtab_hdr->contents;
8412 else if (cookie.locsymcount == 0)
8413 cookie.locsyms = NULL;
8414 else
8416 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8417 cookie.locsyms = bfd_malloc (amt);
8418 if (cookie.locsyms == NULL)
8419 return false;
8420 freesyms = cookie.locsyms;
8421 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8422 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8424 error_ret_free_loc:
8425 free (cookie.locsyms);
8426 return false;
8430 cookie.locsym_shndx = NULL;
8431 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8433 bfd_size_type amt;
8434 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8435 cookie.locsym_shndx = bfd_malloc (amt);
8436 if (cookie.locsym_shndx == NULL)
8437 goto error_ret_free_loc;
8438 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8439 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8441 free (cookie.locsym_shndx);
8442 goto error_ret_free_loc;
8446 if (stab)
8448 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8449 (abfd, stab, (PTR) NULL,
8450 (Elf_Internal_Rela *) NULL,
8451 info->keep_memory));
8452 if (cookie.rels)
8454 cookie.rel = cookie.rels;
8455 cookie.relend =
8456 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8457 if (_bfd_discard_section_stabs (abfd, stab,
8458 elf_section_data (stab)->sec_info,
8459 elf_reloc_symbol_deleted_p,
8460 &cookie))
8461 ret = true;
8462 if (! info->keep_memory)
8463 free (cookie.rels);
8467 if (eh)
8469 cookie.rels = NULL;
8470 cookie.rel = NULL;
8471 cookie.relend = NULL;
8472 if (eh->reloc_count)
8473 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8474 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8475 info->keep_memory));
8476 if (cookie.rels)
8478 cookie.rel = cookie.rels;
8479 cookie.relend =
8480 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8482 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8483 elf_reloc_symbol_deleted_p,
8484 &cookie))
8485 ret = true;
8486 if (! info->keep_memory)
8487 free (cookie.rels);
8490 if (bed->elf_backend_discard_info)
8492 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8493 ret = true;
8496 if (cookie.locsym_shndx != NULL)
8497 free (cookie.locsym_shndx);
8499 if (freesyms != NULL)
8500 free (freesyms);
8503 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
8504 ret = true;
8505 return ret;
8508 static boolean
8509 elf_section_ignore_discarded_relocs (sec)
8510 asection *sec;
8512 struct elf_backend_data *bed;
8514 switch (elf_section_data (sec)->sec_info_type)
8516 case ELF_INFO_TYPE_STABS:
8517 case ELF_INFO_TYPE_EH_FRAME:
8518 return true;
8519 default:
8520 break;
8523 bed = get_elf_backend_data (sec->owner);
8524 if (bed->elf_backend_ignore_discarded_relocs != NULL
8525 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8526 return true;
8528 return false;