oops - omitted from previous delta
[binutils.git] / bfd / elflink.h
blob12f91ef4ef543ba50170cbe231dc42967edcbd87
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* ELF linker code. */
23 /* 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 bfd_boolean failed;
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static bfd_boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static bfd_boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static bfd_boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static bfd_boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static bfd_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 **, bfd_boolean *, bfd_boolean *,
45 bfd_boolean *, bfd_boolean));
46 static bfd_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 bfd_boolean *, bfd_boolean, bfd_boolean));
50 static bfd_boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static bfd_boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static bfd_boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static bfd_boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static bfd_boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static bfd_boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static bfd_boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static bfd_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 bfd_boolean elf_link_output_relocs
69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
70 static bfd_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 bfd_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 bfd_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 bfd_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 bfd_boolean
147 elf_link_is_defined_archive_symbol (abfd, symdef)
148 bfd * abfd;
149 carsym * symdef;
151 Elf_Internal_Shdr * hdr;
152 bfd_size_type symcount;
153 bfd_size_type extsymcount;
154 bfd_size_type extsymoff;
155 Elf_Internal_Sym *isymbuf;
156 Elf_Internal_Sym *isym;
157 Elf_Internal_Sym *isymend;
158 bfd_boolean result;
160 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
161 if (abfd == (bfd *) NULL)
162 return FALSE;
164 if (! bfd_check_format (abfd, bfd_object))
165 return FALSE;
167 /* If we have already included the element containing this symbol in the
168 link then we do not need to include it again. Just claim that any symbol
169 it contains is not a definition, so that our caller will not decide to
170 (re)include this element. */
171 if (abfd->archive_pass)
172 return FALSE;
174 /* Select the appropriate symbol table. */
175 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
176 hdr = &elf_tdata (abfd)->symtab_hdr;
177 else
178 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
180 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
182 /* The sh_info field of the symtab header tells us where the
183 external symbols start. We don't care about the local symbols. */
184 if (elf_bad_symtab (abfd))
186 extsymcount = symcount;
187 extsymoff = 0;
189 else
191 extsymcount = symcount - hdr->sh_info;
192 extsymoff = hdr->sh_info;
195 if (extsymcount == 0)
196 return FALSE;
198 /* Read in the symbol table. */
199 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
200 NULL, NULL, NULL);
201 if (isymbuf == NULL)
202 return FALSE;
204 /* Scan the symbol table looking for SYMDEF. */
205 result = FALSE;
206 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
208 const char *name;
210 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
211 isym->st_name);
212 if (name == (const char *) NULL)
213 break;
215 if (strcmp (name, symdef->name) == 0)
217 result = is_global_data_symbol_definition (abfd, isym);
218 break;
222 free (isymbuf);
224 return result;
227 /* Add symbols from an ELF archive file to the linker hash table. We
228 don't use _bfd_generic_link_add_archive_symbols because of a
229 problem which arises on UnixWare. The UnixWare libc.so is an
230 archive which includes an entry libc.so.1 which defines a bunch of
231 symbols. The libc.so archive also includes a number of other
232 object files, which also define symbols, some of which are the same
233 as those defined in libc.so.1. Correct linking requires that we
234 consider each object file in turn, and include it if it defines any
235 symbols we need. _bfd_generic_link_add_archive_symbols does not do
236 this; it looks through the list of undefined symbols, and includes
237 any object file which defines them. When this algorithm is used on
238 UnixWare, it winds up pulling in libc.so.1 early and defining a
239 bunch of symbols. This means that some of the other objects in the
240 archive are not included in the link, which is incorrect since they
241 precede libc.so.1 in the archive.
243 Fortunately, ELF archive handling is simpler than that done by
244 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
245 oddities. In ELF, if we find a symbol in the archive map, and the
246 symbol is currently undefined, we know that we must pull in that
247 object file.
249 Unfortunately, we do have to make multiple passes over the symbol
250 table until nothing further is resolved. */
252 static bfd_boolean
253 elf_link_add_archive_symbols (abfd, info)
254 bfd *abfd;
255 struct bfd_link_info *info;
257 symindex c;
258 bfd_boolean *defined = NULL;
259 bfd_boolean *included = NULL;
260 carsym *symdefs;
261 bfd_boolean loop;
262 bfd_size_type amt;
264 if (! bfd_has_map (abfd))
266 /* An empty archive is a special case. */
267 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
268 return TRUE;
269 bfd_set_error (bfd_error_no_armap);
270 return FALSE;
273 /* Keep track of all symbols we know to be already defined, and all
274 files we know to be already included. This is to speed up the
275 second and subsequent passes. */
276 c = bfd_ardata (abfd)->symdef_count;
277 if (c == 0)
278 return TRUE;
279 amt = c;
280 amt *= sizeof (bfd_boolean);
281 defined = (bfd_boolean *) bfd_zmalloc (amt);
282 included = (bfd_boolean *) bfd_zmalloc (amt);
283 if (defined == (bfd_boolean *) NULL || included == (bfd_boolean *) NULL)
284 goto error_return;
286 symdefs = bfd_ardata (abfd)->symdefs;
290 file_ptr last;
291 symindex i;
292 carsym *symdef;
293 carsym *symdefend;
295 loop = FALSE;
296 last = -1;
298 symdef = symdefs;
299 symdefend = symdef + c;
300 for (i = 0; symdef < symdefend; symdef++, i++)
302 struct elf_link_hash_entry *h;
303 bfd *element;
304 struct bfd_link_hash_entry *undefs_tail;
305 symindex mark;
307 if (defined[i] || included[i])
308 continue;
309 if (symdef->file_offset == last)
311 included[i] = TRUE;
312 continue;
315 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
316 FALSE, FALSE, FALSE);
318 if (h == NULL)
320 char *p, *copy;
321 size_t len, first;
323 /* If this is a default version (the name contains @@),
324 look up the symbol again with only one `@' as well
325 as without the version. The effect is that references
326 to the symbol with and without the version will be
327 matched by the default symbol in the archive. */
329 p = strchr (symdef->name, ELF_VER_CHR);
330 if (p == NULL || p[1] != ELF_VER_CHR)
331 continue;
333 /* First check with only one `@'. */
334 len = strlen (symdef->name);
335 copy = bfd_alloc (abfd, (bfd_size_type) len);
336 if (copy == NULL)
337 goto error_return;
338 first = p - symdef->name + 1;
339 memcpy (copy, symdef->name, first);
340 memcpy (copy + first, symdef->name + first + 1, len - first);
342 h = elf_link_hash_lookup (elf_hash_table (info), copy,
343 FALSE, FALSE, FALSE);
345 if (h == NULL)
347 /* We also need to check references to the symbol
348 without the version. */
350 copy[first - 1] = '\0';
351 h = elf_link_hash_lookup (elf_hash_table (info),
352 copy, FALSE, FALSE, FALSE);
355 bfd_release (abfd, copy);
358 if (h == NULL)
359 continue;
361 if (h->root.type == bfd_link_hash_common)
363 /* We currently have a common symbol. The archive map contains
364 a reference to this symbol, so we may want to include it. We
365 only want to include it however, if this archive element
366 contains a definition of the symbol, not just another common
367 declaration of it.
369 Unfortunately some archivers (including GNU ar) will put
370 declarations of common symbols into their archive maps, as
371 well as real definitions, so we cannot just go by the archive
372 map alone. Instead we must read in the element's symbol
373 table and check that to see what kind of symbol definition
374 this is. */
375 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
376 continue;
378 else if (h->root.type != bfd_link_hash_undefined)
380 if (h->root.type != bfd_link_hash_undefweak)
381 defined[i] = TRUE;
382 continue;
385 /* We need to include this archive member. */
386 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
387 if (element == (bfd *) NULL)
388 goto error_return;
390 if (! bfd_check_format (element, bfd_object))
391 goto error_return;
393 /* Doublecheck that we have not included this object
394 already--it should be impossible, but there may be
395 something wrong with the archive. */
396 if (element->archive_pass != 0)
398 bfd_set_error (bfd_error_bad_value);
399 goto error_return;
401 element->archive_pass = 1;
403 undefs_tail = info->hash->undefs_tail;
405 if (! (*info->callbacks->add_archive_element) (info, element,
406 symdef->name))
407 goto error_return;
408 if (! elf_link_add_object_symbols (element, info))
409 goto error_return;
411 /* If there are any new undefined symbols, we need to make
412 another pass through the archive in order to see whether
413 they can be defined. FIXME: This isn't perfect, because
414 common symbols wind up on undefs_tail and because an
415 undefined symbol which is defined later on in this pass
416 does not require another pass. This isn't a bug, but it
417 does make the code less efficient than it could be. */
418 if (undefs_tail != info->hash->undefs_tail)
419 loop = TRUE;
421 /* Look backward to mark all symbols from this object file
422 which we have already seen in this pass. */
423 mark = i;
426 included[mark] = TRUE;
427 if (mark == 0)
428 break;
429 --mark;
431 while (symdefs[mark].file_offset == symdef->file_offset);
433 /* We mark subsequent symbols from this object file as we go
434 on through the loop. */
435 last = symdef->file_offset;
438 while (loop);
440 free (defined);
441 free (included);
443 return TRUE;
445 error_return:
446 if (defined != (bfd_boolean *) NULL)
447 free (defined);
448 if (included != (bfd_boolean *) NULL)
449 free (included);
450 return FALSE;
453 /* This function is called when we want to define a new symbol. It
454 handles the various cases which arise when we find a definition in
455 a dynamic object, or when there is already a definition in a
456 dynamic object. The new symbol is described by NAME, SYM, PSEC,
457 and PVALUE. We set SYM_HASH to the hash table entry. We set
458 OVERRIDE if the old symbol is overriding a new definition. We set
459 TYPE_CHANGE_OK if it is OK for the type to change. We set
460 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
461 change, we mean that we shouldn't warn if the type or size does
462 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
463 a shared object. */
465 static bfd_boolean
466 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
467 override, type_change_ok, size_change_ok, dt_needed)
468 bfd *abfd;
469 struct bfd_link_info *info;
470 const char *name;
471 Elf_Internal_Sym *sym;
472 asection **psec;
473 bfd_vma *pvalue;
474 struct elf_link_hash_entry **sym_hash;
475 bfd_boolean *override;
476 bfd_boolean *type_change_ok;
477 bfd_boolean *size_change_ok;
478 bfd_boolean dt_needed;
480 asection *sec;
481 struct elf_link_hash_entry *h;
482 int bind;
483 bfd *oldbfd;
484 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
486 *override = FALSE;
488 sec = *psec;
489 bind = ELF_ST_BIND (sym->st_info);
491 if (! bfd_is_und_section (sec))
492 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
493 else
494 h = ((struct elf_link_hash_entry *)
495 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
496 if (h == NULL)
497 return FALSE;
498 *sym_hash = h;
500 /* This code is for coping with dynamic objects, and is only useful
501 if we are doing an ELF link. */
502 if (info->hash->creator != abfd->xvec)
503 return TRUE;
505 /* For merging, we only care about real symbols. */
507 while (h->root.type == bfd_link_hash_indirect
508 || h->root.type == bfd_link_hash_warning)
509 h = (struct elf_link_hash_entry *) h->root.u.i.link;
511 /* If we just created the symbol, mark it as being an ELF symbol.
512 Other than that, there is nothing to do--there is no merge issue
513 with a newly defined symbol--so we just return. */
515 if (h->root.type == bfd_link_hash_new)
517 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
518 return TRUE;
521 /* OLDBFD is a BFD associated with the existing symbol. */
523 switch (h->root.type)
525 default:
526 oldbfd = NULL;
527 break;
529 case bfd_link_hash_undefined:
530 case bfd_link_hash_undefweak:
531 oldbfd = h->root.u.undef.abfd;
532 break;
534 case bfd_link_hash_defined:
535 case bfd_link_hash_defweak:
536 oldbfd = h->root.u.def.section->owner;
537 break;
539 case bfd_link_hash_common:
540 oldbfd = h->root.u.c.p->section->owner;
541 break;
544 /* In cases involving weak versioned symbols, we may wind up trying
545 to merge a symbol with itself. Catch that here, to avoid the
546 confusion that results if we try to override a symbol with
547 itself. The additional tests catch cases like
548 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
549 dynamic object, which we do want to handle here. */
550 if (abfd == oldbfd
551 && ((abfd->flags & DYNAMIC) == 0
552 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
553 return TRUE;
555 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
556 respectively, is from a dynamic object. */
558 if ((abfd->flags & DYNAMIC) != 0)
559 newdyn = TRUE;
560 else
561 newdyn = FALSE;
563 if (oldbfd != NULL)
564 olddyn = (oldbfd->flags & DYNAMIC) != 0;
565 else
567 asection *hsec;
569 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
570 indices used by MIPS ELF. */
571 switch (h->root.type)
573 default:
574 hsec = NULL;
575 break;
577 case bfd_link_hash_defined:
578 case bfd_link_hash_defweak:
579 hsec = h->root.u.def.section;
580 break;
582 case bfd_link_hash_common:
583 hsec = h->root.u.c.p->section;
584 break;
587 if (hsec == NULL)
588 olddyn = FALSE;
589 else
590 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
593 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
594 respectively, appear to be a definition rather than reference. */
596 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
597 newdef = FALSE;
598 else
599 newdef = TRUE;
601 if (h->root.type == bfd_link_hash_undefined
602 || h->root.type == bfd_link_hash_undefweak
603 || h->root.type == bfd_link_hash_common)
604 olddef = FALSE;
605 else
606 olddef = TRUE;
608 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
609 symbol, respectively, appears to be a common symbol in a dynamic
610 object. If a symbol appears in an uninitialized section, and is
611 not weak, and is not a function, then it may be a common symbol
612 which was resolved when the dynamic object was created. We want
613 to treat such symbols specially, because they raise special
614 considerations when setting the symbol size: if the symbol
615 appears as a common symbol in a regular object, and the size in
616 the regular object is larger, we must make sure that we use the
617 larger size. This problematic case can always be avoided in C,
618 but it must be handled correctly when using Fortran shared
619 libraries.
621 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
622 likewise for OLDDYNCOMMON and OLDDEF.
624 Note that this test is just a heuristic, and that it is quite
625 possible to have an uninitialized symbol in a shared object which
626 is really a definition, rather than a common symbol. This could
627 lead to some minor confusion when the symbol really is a common
628 symbol in some regular object. However, I think it will be
629 harmless. */
631 if (newdyn
632 && newdef
633 && (sec->flags & SEC_ALLOC) != 0
634 && (sec->flags & SEC_LOAD) == 0
635 && sym->st_size > 0
636 && bind != STB_WEAK
637 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
638 newdyncommon = TRUE;
639 else
640 newdyncommon = FALSE;
642 if (olddyn
643 && olddef
644 && h->root.type == bfd_link_hash_defined
645 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
646 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
647 && (h->root.u.def.section->flags & SEC_LOAD) == 0
648 && h->size > 0
649 && h->type != STT_FUNC)
650 olddyncommon = TRUE;
651 else
652 olddyncommon = FALSE;
654 /* It's OK to change the type if either the existing symbol or the
655 new symbol is weak unless it comes from a DT_NEEDED entry of
656 a shared object, in which case, the DT_NEEDED entry may not be
657 required at the run time. */
659 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
660 || h->root.type == bfd_link_hash_undefweak
661 || bind == STB_WEAK)
662 *type_change_ok = TRUE;
664 /* It's OK to change the size if either the existing symbol or the
665 new symbol is weak, or if the old symbol is undefined. */
667 if (*type_change_ok
668 || h->root.type == bfd_link_hash_undefined)
669 *size_change_ok = TRUE;
671 /* If both the old and the new symbols look like common symbols in a
672 dynamic object, set the size of the symbol to the larger of the
673 two. */
675 if (olddyncommon
676 && newdyncommon
677 && sym->st_size != h->size)
679 /* Since we think we have two common symbols, issue a multiple
680 common warning if desired. Note that we only warn if the
681 size is different. If the size is the same, we simply let
682 the old symbol override the new one as normally happens with
683 symbols defined in dynamic objects. */
685 if (! ((*info->callbacks->multiple_common)
686 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
687 h->size, abfd, bfd_link_hash_common, sym->st_size)))
688 return FALSE;
690 if (sym->st_size > h->size)
691 h->size = sym->st_size;
693 *size_change_ok = TRUE;
696 /* If we are looking at a dynamic object, and we have found a
697 definition, we need to see if the symbol was already defined by
698 some other object. If so, we want to use the existing
699 definition, and we do not want to report a multiple symbol
700 definition error; we do this by clobbering *PSEC to be
701 bfd_und_section_ptr.
703 We treat a common symbol as a definition if the symbol in the
704 shared library is a function, since common symbols always
705 represent variables; this can cause confusion in principle, but
706 any such confusion would seem to indicate an erroneous program or
707 shared library. We also permit a common symbol in a regular
708 object to override a weak symbol in a shared object.
710 We prefer a non-weak definition in a shared library to a weak
711 definition in the executable unless it comes from a DT_NEEDED
712 entry of a shared object, in which case, the DT_NEEDED entry
713 may not be required at the run time. */
715 if (newdyn
716 && newdef
717 && (olddef
718 || (h->root.type == bfd_link_hash_common
719 && (bind == STB_WEAK
720 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
721 && (h->root.type != bfd_link_hash_defweak
722 || dt_needed
723 || bind == STB_WEAK))
725 *override = TRUE;
726 newdef = FALSE;
727 newdyncommon = FALSE;
729 *psec = sec = bfd_und_section_ptr;
730 *size_change_ok = TRUE;
732 /* If we get here when the old symbol is a common symbol, then
733 we are explicitly letting it override a weak symbol or
734 function in a dynamic object, and we don't want to warn about
735 a type change. If the old symbol is a defined symbol, a type
736 change warning may still be appropriate. */
738 if (h->root.type == bfd_link_hash_common)
739 *type_change_ok = TRUE;
742 /* Handle the special case of an old common symbol merging with a
743 new symbol which looks like a common symbol in a shared object.
744 We change *PSEC and *PVALUE to make the new symbol look like a
745 common symbol, and let _bfd_generic_link_add_one_symbol will do
746 the right thing. */
748 if (newdyncommon
749 && h->root.type == bfd_link_hash_common)
751 *override = TRUE;
752 newdef = FALSE;
753 newdyncommon = FALSE;
754 *pvalue = sym->st_size;
755 *psec = sec = bfd_com_section_ptr;
756 *size_change_ok = TRUE;
759 /* If the old symbol is from a dynamic object, and the new symbol is
760 a definition which is not from a dynamic object, then the new
761 symbol overrides the old symbol. Symbols from regular files
762 always take precedence over symbols from dynamic objects, even if
763 they are defined after the dynamic object in the link.
765 As above, we again permit a common symbol in a regular object to
766 override a definition in a shared object if the shared object
767 symbol is a function or is weak.
769 As above, we permit a non-weak definition in a shared object to
770 override a weak definition in a regular object. */
772 if (! newdyn
773 && (newdef
774 || (bfd_is_com_section (sec)
775 && (h->root.type == bfd_link_hash_defweak
776 || h->type == STT_FUNC)))
777 && olddyn
778 && olddef
779 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
780 && (bind != STB_WEAK
781 || h->root.type == bfd_link_hash_defweak))
783 /* Change the hash table entry to undefined, and let
784 _bfd_generic_link_add_one_symbol do the right thing with the
785 new definition. */
787 h->root.type = bfd_link_hash_undefined;
788 h->root.u.undef.abfd = h->root.u.def.section->owner;
789 *size_change_ok = TRUE;
791 olddef = FALSE;
792 olddyncommon = FALSE;
794 /* We again permit a type change when a common symbol may be
795 overriding a function. */
797 if (bfd_is_com_section (sec))
798 *type_change_ok = TRUE;
800 /* This union may have been set to be non-NULL when this symbol
801 was seen in a dynamic object. We must force the union to be
802 NULL, so that it is correct for a regular symbol. */
804 h->verinfo.vertree = NULL;
806 /* In this special case, if H is the target of an indirection,
807 we want the caller to frob with H rather than with the
808 indirect symbol. That will permit the caller to redefine the
809 target of the indirection, rather than the indirect symbol
810 itself. FIXME: This will break the -y option if we store a
811 symbol with a different name. */
812 *sym_hash = h;
815 /* Handle the special case of a new common symbol merging with an
816 old symbol that looks like it might be a common symbol defined in
817 a shared object. Note that we have already handled the case in
818 which a new common symbol should simply override the definition
819 in the shared library. */
821 if (! newdyn
822 && bfd_is_com_section (sec)
823 && olddyncommon)
825 /* It would be best if we could set the hash table entry to a
826 common symbol, but we don't know what to use for the section
827 or the alignment. */
828 if (! ((*info->callbacks->multiple_common)
829 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
830 h->size, abfd, bfd_link_hash_common, sym->st_size)))
831 return FALSE;
833 /* If the predumed common symbol in the dynamic object is
834 larger, pretend that the new symbol has its size. */
836 if (h->size > *pvalue)
837 *pvalue = h->size;
839 /* FIXME: We no longer know the alignment required by the symbol
840 in the dynamic object, so we just wind up using the one from
841 the regular object. */
843 olddef = FALSE;
844 olddyncommon = FALSE;
846 h->root.type = bfd_link_hash_undefined;
847 h->root.u.undef.abfd = h->root.u.def.section->owner;
849 *size_change_ok = TRUE;
850 *type_change_ok = TRUE;
852 h->verinfo.vertree = NULL;
855 /* Handle the special case of a weak definition in a regular object
856 followed by a non-weak definition in a shared object. In this
857 case, we prefer the definition in the shared object unless it
858 comes from a DT_NEEDED entry of a shared object, in which case,
859 the DT_NEEDED entry may not be required at the run time. */
860 if (olddef
861 && ! dt_needed
862 && h->root.type == bfd_link_hash_defweak
863 && newdef
864 && newdyn
865 && bind != STB_WEAK)
867 /* To make this work we have to frob the flags so that the rest
868 of the code does not think we are using the regular
869 definition. */
870 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
871 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
872 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
873 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
874 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
875 | ELF_LINK_HASH_DEF_DYNAMIC);
877 /* If H is the target of an indirection, we want the caller to
878 use H rather than the indirect symbol. Otherwise if we are
879 defining a new indirect symbol we will wind up attaching it
880 to the entry we are overriding. */
881 *sym_hash = h;
884 /* Handle the special case of a non-weak definition in a shared
885 object followed by a weak definition in a regular object. In
886 this case we prefer to definition in the shared object. To make
887 this work we have to tell the caller to not treat the new symbol
888 as a definition. */
889 if (olddef
890 && olddyn
891 && h->root.type != bfd_link_hash_defweak
892 && newdef
893 && ! newdyn
894 && bind == STB_WEAK)
895 *override = TRUE;
897 return TRUE;
900 /* This function is called to create an indirect symbol from the
901 default for the symbol with the default version if needed. The
902 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
903 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
904 indicates if it comes from a DT_NEEDED entry of a shared object. */
906 static bfd_boolean
907 elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
908 dynsym, override, dt_needed)
909 bfd *abfd;
910 struct bfd_link_info *info;
911 struct elf_link_hash_entry *h;
912 const char *name;
913 Elf_Internal_Sym *sym;
914 asection **psec;
915 bfd_vma *value;
916 bfd_boolean *dynsym;
917 bfd_boolean override;
918 bfd_boolean dt_needed;
920 bfd_boolean type_change_ok;
921 bfd_boolean size_change_ok;
922 char *shortname;
923 struct elf_link_hash_entry *hi;
924 struct bfd_link_hash_entry *bh;
925 struct elf_backend_data *bed;
926 bfd_boolean collect;
927 bfd_boolean dynamic;
928 char *p;
929 size_t len, shortlen;
930 asection *sec;
932 /* If this symbol has a version, and it is the default version, we
933 create an indirect symbol from the default name to the fully
934 decorated name. This will cause external references which do not
935 specify a version to be bound to this version of the symbol. */
936 p = strchr (name, ELF_VER_CHR);
937 if (p == NULL || p[1] != ELF_VER_CHR)
938 return TRUE;
940 if (override)
942 /* We are overridden by an old defition. We need to check if we
943 need to create the indirect symbol from the default name. */
944 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
945 FALSE, FALSE);
946 BFD_ASSERT (hi != NULL);
947 if (hi == h)
948 return TRUE;
949 while (hi->root.type == bfd_link_hash_indirect
950 || hi->root.type == bfd_link_hash_warning)
952 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
953 if (hi == h)
954 return TRUE;
958 bed = get_elf_backend_data (abfd);
959 collect = bed->collect;
960 dynamic = (abfd->flags & DYNAMIC) != 0;
962 shortlen = p - name;
963 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
964 if (shortname == NULL)
965 return FALSE;
966 memcpy (shortname, name, shortlen);
967 shortname[shortlen] = '\0';
969 /* We are going to create a new symbol. Merge it with any existing
970 symbol with this name. For the purposes of the merge, act as
971 though we were defining the symbol we just defined, although we
972 actually going to define an indirect symbol. */
973 type_change_ok = FALSE;
974 size_change_ok = FALSE;
975 sec = *psec;
976 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
977 &hi, &override, &type_change_ok,
978 &size_change_ok, dt_needed))
979 return FALSE;
981 if (! override)
983 bh = &hi->root;
984 if (! (_bfd_generic_link_add_one_symbol
985 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
986 (bfd_vma) 0, name, FALSE, collect, &bh)))
987 return FALSE;
988 hi = (struct elf_link_hash_entry *) bh;
990 else
992 /* In this case the symbol named SHORTNAME is overriding the
993 indirect symbol we want to add. We were planning on making
994 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
995 is the name without a version. NAME is the fully versioned
996 name, and it is the default version.
998 Overriding means that we already saw a definition for the
999 symbol SHORTNAME in a regular object, and it is overriding
1000 the symbol defined in the dynamic object.
1002 When this happens, we actually want to change NAME, the
1003 symbol we just added, to refer to SHORTNAME. This will cause
1004 references to NAME in the shared object to become references
1005 to SHORTNAME in the regular object. This is what we expect
1006 when we override a function in a shared object: that the
1007 references in the shared object will be mapped to the
1008 definition in the regular object. */
1010 while (hi->root.type == bfd_link_hash_indirect
1011 || hi->root.type == bfd_link_hash_warning)
1012 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1014 h->root.type = bfd_link_hash_indirect;
1015 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1016 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1018 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1019 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1020 if (hi->elf_link_hash_flags
1021 & (ELF_LINK_HASH_REF_REGULAR
1022 | ELF_LINK_HASH_DEF_REGULAR))
1024 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1025 return FALSE;
1029 /* Now set HI to H, so that the following code will set the
1030 other fields correctly. */
1031 hi = h;
1034 /* If there is a duplicate definition somewhere, then HI may not
1035 point to an indirect symbol. We will have reported an error to
1036 the user in that case. */
1038 if (hi->root.type == bfd_link_hash_indirect)
1040 struct elf_link_hash_entry *ht;
1042 /* If the symbol became indirect, then we assume that we have
1043 not seen a definition before. */
1044 BFD_ASSERT ((hi->elf_link_hash_flags
1045 & (ELF_LINK_HASH_DEF_DYNAMIC
1046 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1048 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1049 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1051 /* See if the new flags lead us to realize that the symbol must
1052 be dynamic. */
1053 if (! *dynsym)
1055 if (! dynamic)
1057 if (info->shared
1058 || ((hi->elf_link_hash_flags
1059 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1060 *dynsym = TRUE;
1062 else
1064 if ((hi->elf_link_hash_flags
1065 & ELF_LINK_HASH_REF_REGULAR) != 0)
1066 *dynsym = TRUE;
1071 /* We also need to define an indirection from the nondefault version
1072 of the symbol. */
1074 len = strlen (name);
1075 shortname = bfd_hash_allocate (&info->hash->table, len);
1076 if (shortname == NULL)
1077 return FALSE;
1078 memcpy (shortname, name, shortlen);
1079 memcpy (shortname + shortlen, p + 1, len - shortlen);
1081 /* Once again, merge with any existing symbol. */
1082 type_change_ok = FALSE;
1083 size_change_ok = FALSE;
1084 sec = *psec;
1085 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1086 &hi, &override, &type_change_ok,
1087 &size_change_ok, dt_needed))
1088 return FALSE;
1090 if (override)
1092 /* Here SHORTNAME is a versioned name, so we don't expect to see
1093 the type of override we do in the case above unless it is
1094 overridden by a versioned definiton. */
1095 if (hi->root.type != bfd_link_hash_defined
1096 && hi->root.type != bfd_link_hash_defweak)
1097 (*_bfd_error_handler)
1098 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1099 bfd_archive_filename (abfd), shortname);
1101 else
1103 bh = &hi->root;
1104 if (! (_bfd_generic_link_add_one_symbol
1105 (info, abfd, shortname, BSF_INDIRECT,
1106 bfd_ind_section_ptr, (bfd_vma) 0, name, FALSE, collect, &bh)))
1107 return FALSE;
1108 hi = (struct elf_link_hash_entry *) bh;
1110 /* If there is a duplicate definition somewhere, then HI may not
1111 point to an indirect symbol. We will have reported an error
1112 to the user in that case. */
1114 if (hi->root.type == bfd_link_hash_indirect)
1116 /* If the symbol became indirect, then we assume that we have
1117 not seen a definition before. */
1118 BFD_ASSERT ((hi->elf_link_hash_flags
1119 & (ELF_LINK_HASH_DEF_DYNAMIC
1120 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1122 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1124 /* See if the new flags lead us to realize that the symbol
1125 must be dynamic. */
1126 if (! *dynsym)
1128 if (! dynamic)
1130 if (info->shared
1131 || ((hi->elf_link_hash_flags
1132 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1133 *dynsym = TRUE;
1135 else
1137 if ((hi->elf_link_hash_flags
1138 & ELF_LINK_HASH_REF_REGULAR) != 0)
1139 *dynsym = TRUE;
1145 return TRUE;
1148 /* Add symbols from an ELF object file to the linker hash table. */
1150 static bfd_boolean
1151 elf_link_add_object_symbols (abfd, info)
1152 bfd *abfd;
1153 struct bfd_link_info *info;
1155 bfd_boolean (*add_symbol_hook)
1156 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
1157 const char **, flagword *, asection **, bfd_vma *));
1158 bfd_boolean (*check_relocs)
1159 PARAMS ((bfd *, struct bfd_link_info *, asection *,
1160 const Elf_Internal_Rela *));
1161 bfd_boolean collect;
1162 Elf_Internal_Shdr *hdr;
1163 bfd_size_type symcount;
1164 bfd_size_type extsymcount;
1165 bfd_size_type extsymoff;
1166 struct elf_link_hash_entry **sym_hash;
1167 bfd_boolean dynamic;
1168 Elf_External_Versym *extversym = NULL;
1169 Elf_External_Versym *ever;
1170 struct elf_link_hash_entry *weaks;
1171 struct elf_link_hash_entry **nondeflt_vers = NULL;
1172 bfd_size_type nondeflt_vers_cnt = 0;
1173 Elf_Internal_Sym *isymbuf = NULL;
1174 Elf_Internal_Sym *isym;
1175 Elf_Internal_Sym *isymend;
1176 struct elf_backend_data *bed;
1177 bfd_boolean dt_needed;
1178 struct elf_link_hash_table * hash_table;
1179 bfd_size_type amt;
1181 hash_table = elf_hash_table (info);
1183 bed = get_elf_backend_data (abfd);
1184 add_symbol_hook = bed->elf_add_symbol_hook;
1185 collect = bed->collect;
1187 if ((abfd->flags & DYNAMIC) == 0)
1188 dynamic = FALSE;
1189 else
1191 dynamic = TRUE;
1193 /* You can't use -r against a dynamic object. Also, there's no
1194 hope of using a dynamic object which does not exactly match
1195 the format of the output file. */
1196 if (info->relocateable || info->hash->creator != abfd->xvec)
1198 bfd_set_error (bfd_error_invalid_operation);
1199 goto error_return;
1203 /* As a GNU extension, any input sections which are named
1204 .gnu.warning.SYMBOL are treated as warning symbols for the given
1205 symbol. This differs from .gnu.warning sections, which generate
1206 warnings when they are included in an output file. */
1207 if (! info->shared)
1209 asection *s;
1211 for (s = abfd->sections; s != NULL; s = s->next)
1213 const char *name;
1215 name = bfd_get_section_name (abfd, s);
1216 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1218 char *msg;
1219 bfd_size_type sz;
1221 name += sizeof ".gnu.warning." - 1;
1223 /* If this is a shared object, then look up the symbol
1224 in the hash table. If it is there, and it is already
1225 been defined, then we will not be using the entry
1226 from this shared object, so we don't need to warn.
1227 FIXME: If we see the definition in a regular object
1228 later on, we will warn, but we shouldn't. The only
1229 fix is to keep track of what warnings we are supposed
1230 to emit, and then handle them all at the end of the
1231 link. */
1232 if (dynamic && abfd->xvec == info->hash->creator)
1234 struct elf_link_hash_entry *h;
1236 h = elf_link_hash_lookup (hash_table, name,
1237 FALSE, FALSE, TRUE);
1239 /* FIXME: What about bfd_link_hash_common? */
1240 if (h != NULL
1241 && (h->root.type == bfd_link_hash_defined
1242 || h->root.type == bfd_link_hash_defweak))
1244 /* We don't want to issue this warning. Clobber
1245 the section size so that the warning does not
1246 get copied into the output file. */
1247 s->_raw_size = 0;
1248 continue;
1252 sz = bfd_section_size (abfd, s);
1253 msg = (char *) bfd_alloc (abfd, sz + 1);
1254 if (msg == NULL)
1255 goto error_return;
1257 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1258 goto error_return;
1260 msg[sz] = '\0';
1262 if (! (_bfd_generic_link_add_one_symbol
1263 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1264 FALSE, collect, (struct bfd_link_hash_entry **) NULL)))
1265 goto error_return;
1267 if (! info->relocateable)
1269 /* Clobber the section size so that the warning does
1270 not get copied into the output file. */
1271 s->_raw_size = 0;
1277 dt_needed = FALSE;
1278 if (! dynamic)
1280 /* If we are creating a shared library, create all the dynamic
1281 sections immediately. We need to attach them to something,
1282 so we attach them to this BFD, provided it is the right
1283 format. FIXME: If there are no input BFD's of the same
1284 format as the output, we can't make a shared library. */
1285 if (info->shared
1286 && is_elf_hash_table (info)
1287 && ! hash_table->dynamic_sections_created
1288 && abfd->xvec == info->hash->creator)
1290 if (! elf_link_create_dynamic_sections (abfd, info))
1291 goto error_return;
1294 else if (! is_elf_hash_table (info))
1295 goto error_return;
1296 else
1298 asection *s;
1299 bfd_boolean add_needed;
1300 const char *name;
1301 bfd_size_type oldsize;
1302 bfd_size_type strindex;
1303 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
1305 /* ld --just-symbols and dynamic objects don't mix very well.
1306 Test for --just-symbols by looking at info set up by
1307 _bfd_elf_link_just_syms. */
1308 if ((s = abfd->sections) != NULL
1309 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
1310 goto error_return;
1312 /* Find the name to use in a DT_NEEDED entry that refers to this
1313 object. If the object has a DT_SONAME entry, we use it.
1314 Otherwise, if the generic linker stuck something in
1315 elf_dt_name, we use that. Otherwise, we just use the file
1316 name. If the generic linker put a null string into
1317 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1318 there is a DT_SONAME entry. */
1319 add_needed = TRUE;
1320 name = bfd_get_filename (abfd);
1321 if (elf_dt_name (abfd) != NULL)
1323 name = elf_dt_name (abfd);
1324 if (*name == '\0')
1326 if (elf_dt_soname (abfd) != NULL)
1327 dt_needed = TRUE;
1329 add_needed = FALSE;
1332 s = bfd_get_section_by_name (abfd, ".dynamic");
1333 if (s != NULL)
1335 Elf_External_Dyn *dynbuf = NULL;
1336 Elf_External_Dyn *extdyn;
1337 Elf_External_Dyn *extdynend;
1338 int elfsec;
1339 unsigned long shlink;
1341 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1342 if (dynbuf == NULL)
1343 goto error_return;
1345 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1346 (file_ptr) 0, s->_raw_size))
1347 goto error_free_dyn;
1349 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1350 if (elfsec == -1)
1351 goto error_free_dyn;
1352 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1354 extdyn = dynbuf;
1355 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1356 for (; extdyn < extdynend; extdyn++)
1358 Elf_Internal_Dyn dyn;
1360 elf_swap_dyn_in (abfd, extdyn, &dyn);
1361 if (dyn.d_tag == DT_SONAME)
1363 unsigned int tagv = dyn.d_un.d_val;
1364 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1365 if (name == NULL)
1366 goto error_free_dyn;
1368 if (dyn.d_tag == DT_NEEDED)
1370 struct bfd_link_needed_list *n, **pn;
1371 char *fnm, *anm;
1372 unsigned int tagv = dyn.d_un.d_val;
1374 amt = sizeof (struct bfd_link_needed_list);
1375 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1376 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1377 if (n == NULL || fnm == NULL)
1378 goto error_free_dyn;
1379 amt = strlen (fnm) + 1;
1380 anm = bfd_alloc (abfd, amt);
1381 if (anm == NULL)
1382 goto error_free_dyn;
1383 memcpy (anm, fnm, (size_t) amt);
1384 n->name = anm;
1385 n->by = abfd;
1386 n->next = NULL;
1387 for (pn = & hash_table->needed;
1388 *pn != NULL;
1389 pn = &(*pn)->next)
1391 *pn = n;
1393 if (dyn.d_tag == DT_RUNPATH)
1395 struct bfd_link_needed_list *n, **pn;
1396 char *fnm, *anm;
1397 unsigned int tagv = dyn.d_un.d_val;
1399 amt = sizeof (struct bfd_link_needed_list);
1400 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1401 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1402 if (n == NULL || fnm == NULL)
1403 goto error_free_dyn;
1404 amt = strlen (fnm) + 1;
1405 anm = bfd_alloc (abfd, amt);
1406 if (anm == NULL)
1407 goto error_free_dyn;
1408 memcpy (anm, fnm, (size_t) amt);
1409 n->name = anm;
1410 n->by = abfd;
1411 n->next = NULL;
1412 for (pn = & runpath;
1413 *pn != NULL;
1414 pn = &(*pn)->next)
1416 *pn = n;
1418 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1419 if (!runpath && dyn.d_tag == DT_RPATH)
1421 struct bfd_link_needed_list *n, **pn;
1422 char *fnm, *anm;
1423 unsigned int tagv = dyn.d_un.d_val;
1425 amt = sizeof (struct bfd_link_needed_list);
1426 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1427 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1428 if (n == NULL || fnm == NULL)
1429 goto error_free_dyn;
1430 amt = strlen (fnm) + 1;
1431 anm = bfd_alloc (abfd, amt);
1432 if (anm == NULL)
1434 error_free_dyn:
1435 free (dynbuf);
1436 goto error_return;
1438 memcpy (anm, fnm, (size_t) amt);
1439 n->name = anm;
1440 n->by = abfd;
1441 n->next = NULL;
1442 for (pn = & rpath;
1443 *pn != NULL;
1444 pn = &(*pn)->next)
1446 *pn = n;
1450 free (dynbuf);
1453 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1454 frees all more recently bfd_alloc'd blocks as well. */
1455 if (runpath)
1456 rpath = runpath;
1458 if (rpath)
1460 struct bfd_link_needed_list **pn;
1461 for (pn = & hash_table->runpath;
1462 *pn != NULL;
1463 pn = &(*pn)->next)
1465 *pn = rpath;
1468 /* We do not want to include any of the sections in a dynamic
1469 object in the output file. We hack by simply clobbering the
1470 list of sections in the BFD. This could be handled more
1471 cleanly by, say, a new section flag; the existing
1472 SEC_NEVER_LOAD flag is not the one we want, because that one
1473 still implies that the section takes up space in the output
1474 file. */
1475 bfd_section_list_clear (abfd);
1477 /* If this is the first dynamic object found in the link, create
1478 the special sections required for dynamic linking. */
1479 if (! hash_table->dynamic_sections_created)
1480 if (! elf_link_create_dynamic_sections (abfd, info))
1481 goto error_return;
1483 if (add_needed)
1485 /* Add a DT_NEEDED entry for this dynamic object. */
1486 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1487 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
1488 if (strindex == (bfd_size_type) -1)
1489 goto error_return;
1491 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1493 asection *sdyn;
1494 Elf_External_Dyn *dyncon, *dynconend;
1496 /* The hash table size did not change, which means that
1497 the dynamic object name was already entered. If we
1498 have already included this dynamic object in the
1499 link, just ignore it. There is no reason to include
1500 a particular dynamic object more than once. */
1501 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1502 BFD_ASSERT (sdyn != NULL);
1504 dyncon = (Elf_External_Dyn *) sdyn->contents;
1505 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1506 sdyn->_raw_size);
1507 for (; dyncon < dynconend; dyncon++)
1509 Elf_Internal_Dyn dyn;
1511 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1512 if (dyn.d_tag == DT_NEEDED
1513 && dyn.d_un.d_val == strindex)
1515 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1516 return TRUE;
1521 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1522 goto error_return;
1525 /* Save the SONAME, if there is one, because sometimes the
1526 linker emulation code will need to know it. */
1527 if (*name == '\0')
1528 name = basename (bfd_get_filename (abfd));
1529 elf_dt_name (abfd) = name;
1532 /* If this is a dynamic object, we always link against the .dynsym
1533 symbol table, not the .symtab symbol table. The dynamic linker
1534 will only see the .dynsym symbol table, so there is no reason to
1535 look at .symtab for a dynamic object. */
1537 if (! dynamic || elf_dynsymtab (abfd) == 0)
1538 hdr = &elf_tdata (abfd)->symtab_hdr;
1539 else
1540 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1542 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1544 /* The sh_info field of the symtab header tells us where the
1545 external symbols start. We don't care about the local symbols at
1546 this point. */
1547 if (elf_bad_symtab (abfd))
1549 extsymcount = symcount;
1550 extsymoff = 0;
1552 else
1554 extsymcount = symcount - hdr->sh_info;
1555 extsymoff = hdr->sh_info;
1558 sym_hash = NULL;
1559 if (extsymcount != 0)
1561 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
1562 NULL, NULL, NULL);
1563 if (isymbuf == NULL)
1564 goto error_return;
1566 /* We store a pointer to the hash table entry for each external
1567 symbol. */
1568 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1569 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1570 if (sym_hash == NULL)
1571 goto error_free_sym;
1572 elf_sym_hashes (abfd) = sym_hash;
1575 if (dynamic)
1577 /* Read in any version definitions. */
1578 if (! _bfd_elf_slurp_version_tables (abfd))
1579 goto error_free_sym;
1581 /* Read in the symbol versions, but don't bother to convert them
1582 to internal format. */
1583 if (elf_dynversym (abfd) != 0)
1585 Elf_Internal_Shdr *versymhdr;
1587 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1588 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1589 if (extversym == NULL)
1590 goto error_free_sym;
1591 amt = versymhdr->sh_size;
1592 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1593 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1594 goto error_free_vers;
1598 weaks = NULL;
1600 ever = extversym != NULL ? extversym + extsymoff : NULL;
1601 for (isym = isymbuf, isymend = isymbuf + extsymcount;
1602 isym < isymend;
1603 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1605 int bind;
1606 bfd_vma value;
1607 asection *sec;
1608 flagword flags;
1609 const char *name;
1610 struct elf_link_hash_entry *h;
1611 bfd_boolean definition;
1612 bfd_boolean size_change_ok;
1613 bfd_boolean type_change_ok;
1614 bfd_boolean new_weakdef;
1615 bfd_boolean override;
1616 unsigned int old_alignment;
1617 bfd *old_bfd;
1619 override = FALSE;
1621 flags = BSF_NO_FLAGS;
1622 sec = NULL;
1623 value = isym->st_value;
1624 *sym_hash = NULL;
1626 bind = ELF_ST_BIND (isym->st_info);
1627 if (bind == STB_LOCAL)
1629 /* This should be impossible, since ELF requires that all
1630 global symbols follow all local symbols, and that sh_info
1631 point to the first global symbol. Unfortunatealy, Irix 5
1632 screws this up. */
1633 continue;
1635 else if (bind == STB_GLOBAL)
1637 if (isym->st_shndx != SHN_UNDEF
1638 && isym->st_shndx != SHN_COMMON)
1639 flags = BSF_GLOBAL;
1641 else if (bind == STB_WEAK)
1642 flags = BSF_WEAK;
1643 else
1645 /* Leave it up to the processor backend. */
1648 if (isym->st_shndx == SHN_UNDEF)
1649 sec = bfd_und_section_ptr;
1650 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
1652 sec = section_from_elf_index (abfd, isym->st_shndx);
1653 if (sec == NULL)
1654 sec = bfd_abs_section_ptr;
1655 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1656 value -= sec->vma;
1658 else if (isym->st_shndx == SHN_ABS)
1659 sec = bfd_abs_section_ptr;
1660 else if (isym->st_shndx == SHN_COMMON)
1662 sec = bfd_com_section_ptr;
1663 /* What ELF calls the size we call the value. What ELF
1664 calls the value we call the alignment. */
1665 value = isym->st_size;
1667 else
1669 /* Leave it up to the processor backend. */
1672 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
1673 isym->st_name);
1674 if (name == (const char *) NULL)
1675 goto error_free_vers;
1677 if (isym->st_shndx == SHN_COMMON
1678 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
1680 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1682 if (tcomm == NULL)
1684 tcomm = bfd_make_section (abfd, ".tcommon");
1685 if (tcomm == NULL
1686 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1687 | SEC_IS_COMMON
1688 | SEC_LINKER_CREATED
1689 | SEC_THREAD_LOCAL)))
1690 goto error_free_vers;
1692 sec = tcomm;
1694 else if (add_symbol_hook)
1696 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
1697 &value))
1698 goto error_free_vers;
1700 /* The hook function sets the name to NULL if this symbol
1701 should be skipped for some reason. */
1702 if (name == (const char *) NULL)
1703 continue;
1706 /* Sanity check that all possibilities were handled. */
1707 if (sec == (asection *) NULL)
1709 bfd_set_error (bfd_error_bad_value);
1710 goto error_free_vers;
1713 if (bfd_is_und_section (sec)
1714 || bfd_is_com_section (sec))
1715 definition = FALSE;
1716 else
1717 definition = TRUE;
1719 size_change_ok = FALSE;
1720 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1721 old_alignment = 0;
1722 old_bfd = NULL;
1724 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1726 Elf_Internal_Versym iver;
1727 unsigned int vernum = 0;
1729 if (ever != NULL)
1731 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1732 vernum = iver.vs_vers & VERSYM_VERSION;
1734 /* If this is a hidden symbol, or if it is not version
1735 1, we append the version name to the symbol name.
1736 However, we do not modify a non-hidden absolute
1737 symbol, because it might be the version symbol
1738 itself. FIXME: What if it isn't? */
1739 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1740 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1742 const char *verstr;
1743 size_t namelen, verlen, newlen;
1744 char *newname, *p;
1746 if (isym->st_shndx != SHN_UNDEF)
1748 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1750 (*_bfd_error_handler)
1751 (_("%s: %s: invalid version %u (max %d)"),
1752 bfd_archive_filename (abfd), name, vernum,
1753 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1754 bfd_set_error (bfd_error_bad_value);
1755 goto error_free_vers;
1757 else if (vernum > 1)
1758 verstr =
1759 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1760 else
1761 verstr = "";
1763 else
1765 /* We cannot simply test for the number of
1766 entries in the VERNEED section since the
1767 numbers for the needed versions do not start
1768 at 0. */
1769 Elf_Internal_Verneed *t;
1771 verstr = NULL;
1772 for (t = elf_tdata (abfd)->verref;
1773 t != NULL;
1774 t = t->vn_nextref)
1776 Elf_Internal_Vernaux *a;
1778 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1780 if (a->vna_other == vernum)
1782 verstr = a->vna_nodename;
1783 break;
1786 if (a != NULL)
1787 break;
1789 if (verstr == NULL)
1791 (*_bfd_error_handler)
1792 (_("%s: %s: invalid needed version %d"),
1793 bfd_archive_filename (abfd), name, vernum);
1794 bfd_set_error (bfd_error_bad_value);
1795 goto error_free_vers;
1799 namelen = strlen (name);
1800 verlen = strlen (verstr);
1801 newlen = namelen + verlen + 2;
1802 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1803 && isym->st_shndx != SHN_UNDEF)
1804 ++newlen;
1806 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
1807 if (newname == NULL)
1808 goto error_free_vers;
1809 memcpy (newname, name, namelen);
1810 p = newname + namelen;
1811 *p++ = ELF_VER_CHR;
1812 /* If this is a defined non-hidden version symbol,
1813 we add another @ to the name. This indicates the
1814 default version of the symbol. */
1815 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1816 && isym->st_shndx != SHN_UNDEF)
1817 *p++ = ELF_VER_CHR;
1818 memcpy (p, verstr, verlen + 1);
1820 name = newname;
1824 if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1825 sym_hash, &override, &type_change_ok,
1826 &size_change_ok, dt_needed))
1827 goto error_free_vers;
1829 if (override)
1830 definition = FALSE;
1832 h = *sym_hash;
1833 while (h->root.type == bfd_link_hash_indirect
1834 || h->root.type == bfd_link_hash_warning)
1835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1837 /* Remember the old alignment if this is a common symbol, so
1838 that we don't reduce the alignment later on. We can't
1839 check later, because _bfd_generic_link_add_one_symbol
1840 will set a default for the alignment which we want to
1841 override. We also remember the old bfd where the existing
1842 definition comes from. */
1843 switch (h->root.type)
1845 default:
1846 break;
1848 case bfd_link_hash_defined:
1849 case bfd_link_hash_defweak:
1850 old_bfd = h->root.u.def.section->owner;
1851 break;
1853 case bfd_link_hash_common:
1854 old_bfd = h->root.u.c.p->section->owner;
1855 old_alignment = h->root.u.c.p->alignment_power;
1856 break;
1859 if (elf_tdata (abfd)->verdef != NULL
1860 && ! override
1861 && vernum > 1
1862 && definition)
1863 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1866 if (! (_bfd_generic_link_add_one_symbol
1867 (info, abfd, name, flags, sec, value, (const char *) NULL,
1868 FALSE, collect, (struct bfd_link_hash_entry **) sym_hash)))
1869 goto error_free_vers;
1871 h = *sym_hash;
1872 while (h->root.type == bfd_link_hash_indirect
1873 || h->root.type == bfd_link_hash_warning)
1874 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1875 *sym_hash = h;
1877 new_weakdef = FALSE;
1878 if (dynamic
1879 && definition
1880 && (flags & BSF_WEAK) != 0
1881 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
1882 && info->hash->creator->flavour == bfd_target_elf_flavour
1883 && h->weakdef == NULL)
1885 /* Keep a list of all weak defined non function symbols from
1886 a dynamic object, using the weakdef field. Later in this
1887 function we will set the weakdef field to the correct
1888 value. We only put non-function symbols from dynamic
1889 objects on this list, because that happens to be the only
1890 time we need to know the normal symbol corresponding to a
1891 weak symbol, and the information is time consuming to
1892 figure out. If the weakdef field is not already NULL,
1893 then this symbol was already defined by some previous
1894 dynamic object, and we will be using that previous
1895 definition anyhow. */
1897 h->weakdef = weaks;
1898 weaks = h;
1899 new_weakdef = TRUE;
1902 /* Set the alignment of a common symbol. */
1903 if (isym->st_shndx == SHN_COMMON
1904 && h->root.type == bfd_link_hash_common)
1906 unsigned int align;
1908 align = bfd_log2 (isym->st_value);
1909 if (align > old_alignment
1910 /* Permit an alignment power of zero if an alignment of one
1911 is specified and no other alignments have been specified. */
1912 || (isym->st_value == 1 && old_alignment == 0))
1913 h->root.u.c.p->alignment_power = align;
1914 else
1915 h->root.u.c.p->alignment_power = old_alignment;
1918 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1920 int old_flags;
1921 bfd_boolean dynsym;
1922 int new_flag;
1924 /* Check the alignment when a common symbol is involved. This
1925 can change when a common symbol is overriden by a normal
1926 definition or a common symbol is ignored due to the old
1927 normal definition. We need to make sure the maximum
1928 alignment is maintained. */
1929 if ((old_alignment || isym->st_shndx == SHN_COMMON)
1930 && h->root.type != bfd_link_hash_common)
1932 unsigned int common_align;
1933 unsigned int normal_align;
1934 unsigned int symbol_align;
1935 bfd *normal_bfd;
1936 bfd *common_bfd;
1938 symbol_align = ffs (h->root.u.def.value) - 1;
1939 if ((h->root.u.def.section->owner->flags & DYNAMIC) == 0)
1941 normal_align = h->root.u.def.section->alignment_power;
1942 if (normal_align > symbol_align)
1943 normal_align = symbol_align;
1945 else
1946 normal_align = symbol_align;
1948 if (old_alignment)
1950 common_align = old_alignment;
1951 common_bfd = old_bfd;
1952 normal_bfd = abfd;
1954 else
1956 common_align = bfd_log2 (isym->st_value);
1957 common_bfd = abfd;
1958 normal_bfd = old_bfd;
1961 if (normal_align < common_align)
1962 (*_bfd_error_handler)
1963 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1964 1 << normal_align,
1965 name,
1966 bfd_archive_filename (normal_bfd),
1967 1 << common_align,
1968 bfd_archive_filename (common_bfd));
1971 /* Remember the symbol size and type. */
1972 if (isym->st_size != 0
1973 && (definition || h->size == 0))
1975 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
1976 (*_bfd_error_handler)
1977 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1978 name, (unsigned long) h->size,
1979 bfd_archive_filename (old_bfd),
1980 (unsigned long) isym->st_size,
1981 bfd_archive_filename (abfd));
1983 h->size = isym->st_size;
1986 /* If this is a common symbol, then we always want H->SIZE
1987 to be the size of the common symbol. The code just above
1988 won't fix the size if a common symbol becomes larger. We
1989 don't warn about a size change here, because that is
1990 covered by --warn-common. */
1991 if (h->root.type == bfd_link_hash_common)
1992 h->size = h->root.u.c.size;
1994 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
1995 && (definition || h->type == STT_NOTYPE))
1997 if (h->type != STT_NOTYPE
1998 && h->type != ELF_ST_TYPE (isym->st_info)
1999 && ! type_change_ok)
2000 (*_bfd_error_handler)
2001 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2002 name, h->type, ELF_ST_TYPE (isym->st_info),
2003 bfd_archive_filename (abfd));
2005 h->type = ELF_ST_TYPE (isym->st_info);
2008 /* If st_other has a processor-specific meaning, specific code
2009 might be needed here. */
2010 if (isym->st_other != 0)
2012 unsigned char hvis, symvis, other, nvis;
2014 /* Take the balance of OTHER from the definition. */
2015 other = (definition ? isym->st_other : h->other);
2016 other &= ~ ELF_ST_VISIBILITY (-1);
2018 /* Combine visibilities, using the most constraining one. */
2019 hvis = ELF_ST_VISIBILITY (h->other);
2020 symvis = ELF_ST_VISIBILITY (isym->st_other);
2021 if (! hvis)
2022 nvis = symvis;
2023 else if (! symvis)
2024 nvis = hvis;
2025 else
2026 nvis = hvis < symvis ? hvis : symvis;
2028 h->other = other | nvis;
2031 /* Set a flag in the hash table entry indicating the type of
2032 reference or definition we just found. Keep a count of
2033 the number of dynamic symbols we find. A dynamic symbol
2034 is one which is referenced or defined by both a regular
2035 object and a shared object. */
2036 old_flags = h->elf_link_hash_flags;
2037 dynsym = FALSE;
2038 if (! dynamic)
2040 if (! definition)
2042 new_flag = ELF_LINK_HASH_REF_REGULAR;
2043 if (bind != STB_WEAK)
2044 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2046 else
2047 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2048 if (info->shared
2049 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2050 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2051 dynsym = TRUE;
2053 else
2055 if (! definition)
2056 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2057 else
2058 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2059 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2060 | ELF_LINK_HASH_REF_REGULAR)) != 0
2061 || (h->weakdef != NULL
2062 && ! new_weakdef
2063 && h->weakdef->dynindx != -1))
2064 dynsym = TRUE;
2067 h->elf_link_hash_flags |= new_flag;
2069 /* Check to see if we need to add an indirect symbol for
2070 the default name. */
2071 if (definition || h->root.type == bfd_link_hash_common)
2072 if (! elf_add_default_symbol (abfd, info, h, name, isym,
2073 &sec, &value, &dynsym,
2074 override, dt_needed))
2075 goto error_free_vers;
2077 if (definition && (abfd->flags & DYNAMIC) == 0)
2079 char *p = strchr (name, ELF_VER_CHR);
2080 if (p != NULL && p[1] != ELF_VER_CHR)
2082 /* Queue non-default versions so that .symver x, x@FOO
2083 aliases can be checked. */
2084 if (! nondeflt_vers)
2086 amt = (isymend - isym + 1)
2087 * sizeof (struct elf_link_hash_entry *);
2088 nondeflt_vers = bfd_malloc (amt);
2090 nondeflt_vers [nondeflt_vers_cnt++] = h;
2094 if (dynsym && h->dynindx == -1)
2096 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2097 goto error_free_vers;
2098 if (h->weakdef != NULL
2099 && ! new_weakdef
2100 && h->weakdef->dynindx == -1)
2102 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2103 goto error_free_vers;
2106 else if (dynsym && h->dynindx != -1)
2107 /* If the symbol already has a dynamic index, but
2108 visibility says it should not be visible, turn it into
2109 a local symbol. */
2110 switch (ELF_ST_VISIBILITY (h->other))
2112 case STV_INTERNAL:
2113 case STV_HIDDEN:
2114 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2115 break;
2118 if (dt_needed && definition
2119 && (h->elf_link_hash_flags
2120 & ELF_LINK_HASH_REF_REGULAR) != 0)
2122 bfd_size_type oldsize;
2123 bfd_size_type strindex;
2125 if (! is_elf_hash_table (info))
2126 goto error_free_vers;
2128 /* The symbol from a DT_NEEDED object is referenced from
2129 the regular object to create a dynamic executable. We
2130 have to make sure there is a DT_NEEDED entry for it. */
2132 dt_needed = FALSE;
2133 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2134 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2135 elf_dt_soname (abfd), FALSE);
2136 if (strindex == (bfd_size_type) -1)
2137 goto error_free_vers;
2139 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2141 asection *sdyn;
2142 Elf_External_Dyn *dyncon, *dynconend;
2144 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2145 ".dynamic");
2146 BFD_ASSERT (sdyn != NULL);
2148 dyncon = (Elf_External_Dyn *) sdyn->contents;
2149 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2150 sdyn->_raw_size);
2151 for (; dyncon < dynconend; dyncon++)
2153 Elf_Internal_Dyn dyn;
2155 elf_swap_dyn_in (hash_table->dynobj,
2156 dyncon, &dyn);
2157 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2158 dyn.d_un.d_val != strindex);
2162 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2163 goto error_free_vers;
2168 /* Now that all the symbols from this input file are created, handle
2169 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2170 if (nondeflt_vers != NULL)
2172 bfd_size_type cnt, symidx;
2174 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
2176 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
2177 char *shortname, *p;
2179 p = strchr (h->root.root.string, ELF_VER_CHR);
2180 if (p == NULL
2181 || (h->root.type != bfd_link_hash_defined
2182 && h->root.type != bfd_link_hash_defweak))
2183 continue;
2185 amt = p - h->root.root.string;
2186 shortname = bfd_malloc (amt + 1);
2187 memcpy (shortname, h->root.root.string, amt);
2188 shortname[amt] = '\0';
2190 hi = (struct elf_link_hash_entry *)
2191 bfd_link_hash_lookup (info->hash, shortname,
2192 FALSE, FALSE, FALSE);
2193 if (hi != NULL
2194 && hi->root.type == h->root.type
2195 && hi->root.u.def.value == h->root.u.def.value
2196 && hi->root.u.def.section == h->root.u.def.section)
2198 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
2199 hi->root.type = bfd_link_hash_indirect;
2200 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
2201 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
2202 sym_hash = elf_sym_hashes (abfd);
2203 if (sym_hash)
2204 for (symidx = 0; symidx < extsymcount; ++symidx)
2205 if (sym_hash[symidx] == hi)
2207 sym_hash[symidx] = h;
2208 break;
2211 free (shortname);
2213 free (nondeflt_vers);
2214 nondeflt_vers = NULL;
2217 if (extversym != NULL)
2219 free (extversym);
2220 extversym = NULL;
2223 if (isymbuf != NULL)
2224 free (isymbuf);
2225 isymbuf = NULL;
2227 /* Now set the weakdefs field correctly for all the weak defined
2228 symbols we found. The only way to do this is to search all the
2229 symbols. Since we only need the information for non functions in
2230 dynamic objects, that's the only time we actually put anything on
2231 the list WEAKS. We need this information so that if a regular
2232 object refers to a symbol defined weakly in a dynamic object, the
2233 real symbol in the dynamic object is also put in the dynamic
2234 symbols; we also must arrange for both symbols to point to the
2235 same memory location. We could handle the general case of symbol
2236 aliasing, but a general symbol alias can only be generated in
2237 assembler code, handling it correctly would be very time
2238 consuming, and other ELF linkers don't handle general aliasing
2239 either. */
2240 while (weaks != NULL)
2242 struct elf_link_hash_entry *hlook;
2243 asection *slook;
2244 bfd_vma vlook;
2245 struct elf_link_hash_entry **hpp;
2246 struct elf_link_hash_entry **hppend;
2248 hlook = weaks;
2249 weaks = hlook->weakdef;
2250 hlook->weakdef = NULL;
2252 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2253 || hlook->root.type == bfd_link_hash_defweak
2254 || hlook->root.type == bfd_link_hash_common
2255 || hlook->root.type == bfd_link_hash_indirect);
2256 slook = hlook->root.u.def.section;
2257 vlook = hlook->root.u.def.value;
2259 hpp = elf_sym_hashes (abfd);
2260 hppend = hpp + extsymcount;
2261 for (; hpp < hppend; hpp++)
2263 struct elf_link_hash_entry *h;
2265 h = *hpp;
2266 if (h != NULL && h != hlook
2267 && h->root.type == bfd_link_hash_defined
2268 && h->root.u.def.section == slook
2269 && h->root.u.def.value == vlook)
2271 hlook->weakdef = h;
2273 /* If the weak definition is in the list of dynamic
2274 symbols, make sure the real definition is put there
2275 as well. */
2276 if (hlook->dynindx != -1
2277 && h->dynindx == -1)
2279 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2280 goto error_return;
2283 /* If the real definition is in the list of dynamic
2284 symbols, make sure the weak definition is put there
2285 as well. If we don't do this, then the dynamic
2286 loader might not merge the entries for the real
2287 definition and the weak definition. */
2288 if (h->dynindx != -1
2289 && hlook->dynindx == -1)
2291 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2292 goto error_return;
2294 break;
2299 /* If this object is the same format as the output object, and it is
2300 not a shared library, then let the backend look through the
2301 relocs.
2303 This is required to build global offset table entries and to
2304 arrange for dynamic relocs. It is not required for the
2305 particular common case of linking non PIC code, even when linking
2306 against shared libraries, but unfortunately there is no way of
2307 knowing whether an object file has been compiled PIC or not.
2308 Looking through the relocs is not particularly time consuming.
2309 The problem is that we must either (1) keep the relocs in memory,
2310 which causes the linker to require additional runtime memory or
2311 (2) read the relocs twice from the input file, which wastes time.
2312 This would be a good case for using mmap.
2314 I have no idea how to handle linking PIC code into a file of a
2315 different format. It probably can't be done. */
2316 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2317 if (! dynamic
2318 && abfd->xvec == info->hash->creator
2319 && check_relocs != NULL)
2321 asection *o;
2323 for (o = abfd->sections; o != NULL; o = o->next)
2325 Elf_Internal_Rela *internal_relocs;
2326 bfd_boolean ok;
2328 if ((o->flags & SEC_RELOC) == 0
2329 || o->reloc_count == 0
2330 || ((info->strip == strip_all || info->strip == strip_debugger)
2331 && (o->flags & SEC_DEBUGGING) != 0)
2332 || bfd_is_abs_section (o->output_section))
2333 continue;
2335 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2336 (abfd, o, (PTR) NULL,
2337 (Elf_Internal_Rela *) NULL,
2338 info->keep_memory));
2339 if (internal_relocs == NULL)
2340 goto error_return;
2342 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2344 if (elf_section_data (o)->relocs != internal_relocs)
2345 free (internal_relocs);
2347 if (! ok)
2348 goto error_return;
2352 /* If this is a non-traditional link, try to optimize the handling
2353 of the .stab/.stabstr sections. */
2354 if (! dynamic
2355 && ! info->traditional_format
2356 && info->hash->creator->flavour == bfd_target_elf_flavour
2357 && is_elf_hash_table (info)
2358 && (info->strip != strip_all && info->strip != strip_debugger))
2360 asection *stab, *stabstr;
2362 stab = bfd_get_section_by_name (abfd, ".stab");
2363 if (stab != NULL
2364 && (stab->flags & SEC_MERGE) == 0
2365 && !bfd_is_abs_section (stab->output_section))
2367 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2369 if (stabstr != NULL)
2371 struct bfd_elf_section_data *secdata;
2373 secdata = elf_section_data (stab);
2374 if (! _bfd_link_section_stabs (abfd,
2375 & hash_table->stab_info,
2376 stab, stabstr,
2377 &secdata->sec_info))
2378 goto error_return;
2379 if (secdata->sec_info)
2380 stab->sec_info_type = ELF_INFO_TYPE_STABS;
2385 if (! info->relocateable && ! dynamic
2386 && is_elf_hash_table (info))
2388 asection *s;
2390 for (s = abfd->sections; s != NULL; s = s->next)
2391 if ((s->flags & SEC_MERGE) != 0
2392 && !bfd_is_abs_section (s->output_section))
2394 struct bfd_elf_section_data *secdata;
2396 secdata = elf_section_data (s);
2397 if (! _bfd_merge_section (abfd,
2398 & hash_table->merge_info,
2399 s, &secdata->sec_info))
2400 goto error_return;
2401 else if (secdata->sec_info)
2402 s->sec_info_type = ELF_INFO_TYPE_MERGE;
2406 if (is_elf_hash_table (info))
2408 /* Add this bfd to the loaded list. */
2409 struct elf_link_loaded_list *n;
2411 n = ((struct elf_link_loaded_list *)
2412 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
2413 if (n == NULL)
2414 goto error_return;
2415 n->abfd = abfd;
2416 n->next = hash_table->loaded;
2417 hash_table->loaded = n;
2420 return TRUE;
2422 error_free_vers:
2423 if (nondeflt_vers != NULL)
2424 free (nondeflt_vers);
2425 if (extversym != NULL)
2426 free (extversym);
2427 error_free_sym:
2428 if (isymbuf != NULL)
2429 free (isymbuf);
2430 error_return:
2431 return FALSE;
2434 /* Create some sections which will be filled in with dynamic linking
2435 information. ABFD is an input file which requires dynamic sections
2436 to be created. The dynamic sections take up virtual memory space
2437 when the final executable is run, so we need to create them before
2438 addresses are assigned to the output sections. We work out the
2439 actual contents and size of these sections later. */
2441 bfd_boolean
2442 elf_link_create_dynamic_sections (abfd, info)
2443 bfd *abfd;
2444 struct bfd_link_info *info;
2446 flagword flags;
2447 register asection *s;
2448 struct elf_link_hash_entry *h;
2449 struct bfd_link_hash_entry *bh;
2450 struct elf_backend_data *bed;
2452 if (! is_elf_hash_table (info))
2453 return FALSE;
2455 if (elf_hash_table (info)->dynamic_sections_created)
2456 return TRUE;
2458 /* Make sure that all dynamic sections use the same input BFD. */
2459 if (elf_hash_table (info)->dynobj == NULL)
2460 elf_hash_table (info)->dynobj = abfd;
2461 else
2462 abfd = elf_hash_table (info)->dynobj;
2464 /* Note that we set the SEC_IN_MEMORY flag for all of these
2465 sections. */
2466 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2467 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2469 /* A dynamically linked executable has a .interp section, but a
2470 shared library does not. */
2471 if (! info->shared)
2473 s = bfd_make_section (abfd, ".interp");
2474 if (s == NULL
2475 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2476 return FALSE;
2479 if (! info->traditional_format
2480 && info->hash->creator->flavour == bfd_target_elf_flavour)
2482 s = bfd_make_section (abfd, ".eh_frame_hdr");
2483 if (s == NULL
2484 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2485 || ! bfd_set_section_alignment (abfd, s, 2))
2486 return FALSE;
2487 elf_hash_table (info)->eh_info.hdr_sec = s;
2490 /* Create sections to hold version informations. These are removed
2491 if they are not needed. */
2492 s = bfd_make_section (abfd, ".gnu.version_d");
2493 if (s == NULL
2494 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2495 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2496 return FALSE;
2498 s = bfd_make_section (abfd, ".gnu.version");
2499 if (s == NULL
2500 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2501 || ! bfd_set_section_alignment (abfd, s, 1))
2502 return FALSE;
2504 s = bfd_make_section (abfd, ".gnu.version_r");
2505 if (s == NULL
2506 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2507 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2508 return FALSE;
2510 s = bfd_make_section (abfd, ".dynsym");
2511 if (s == NULL
2512 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2513 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2514 return FALSE;
2516 s = bfd_make_section (abfd, ".dynstr");
2517 if (s == NULL
2518 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2519 return FALSE;
2521 /* Create a strtab to hold the dynamic symbol names. */
2522 if (elf_hash_table (info)->dynstr == NULL)
2524 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2525 if (elf_hash_table (info)->dynstr == NULL)
2526 return FALSE;
2529 s = bfd_make_section (abfd, ".dynamic");
2530 if (s == NULL
2531 || ! bfd_set_section_flags (abfd, s, flags)
2532 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2533 return FALSE;
2535 /* The special symbol _DYNAMIC is always set to the start of the
2536 .dynamic section. This call occurs before we have processed the
2537 symbols for any dynamic object, so we don't have to worry about
2538 overriding a dynamic definition. We could set _DYNAMIC in a
2539 linker script, but we only want to define it if we are, in fact,
2540 creating a .dynamic section. We don't want to define it if there
2541 is no .dynamic section, since on some ELF platforms the start up
2542 code examines it to decide how to initialize the process. */
2543 bh = NULL;
2544 if (! (_bfd_generic_link_add_one_symbol
2545 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2546 (const char *) 0, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2547 return FALSE;
2548 h = (struct elf_link_hash_entry *) bh;
2549 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2550 h->type = STT_OBJECT;
2552 if (info->shared
2553 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2554 return FALSE;
2556 bed = get_elf_backend_data (abfd);
2558 s = bfd_make_section (abfd, ".hash");
2559 if (s == NULL
2560 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2561 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2562 return FALSE;
2563 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2565 /* Let the backend create the rest of the sections. This lets the
2566 backend set the right flags. The backend will normally create
2567 the .got and .plt sections. */
2568 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2569 return FALSE;
2571 elf_hash_table (info)->dynamic_sections_created = TRUE;
2573 return TRUE;
2576 /* Add an entry to the .dynamic table. */
2578 bfd_boolean
2579 elf_add_dynamic_entry (info, tag, val)
2580 struct bfd_link_info *info;
2581 bfd_vma tag;
2582 bfd_vma val;
2584 Elf_Internal_Dyn dyn;
2585 bfd *dynobj;
2586 asection *s;
2587 bfd_size_type newsize;
2588 bfd_byte *newcontents;
2590 if (! is_elf_hash_table (info))
2591 return FALSE;
2593 dynobj = elf_hash_table (info)->dynobj;
2595 s = bfd_get_section_by_name (dynobj, ".dynamic");
2596 BFD_ASSERT (s != NULL);
2598 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2599 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2600 if (newcontents == NULL)
2601 return FALSE;
2603 dyn.d_tag = tag;
2604 dyn.d_un.d_val = val;
2605 elf_swap_dyn_out (dynobj, &dyn,
2606 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2608 s->_raw_size = newsize;
2609 s->contents = newcontents;
2611 return TRUE;
2614 /* Read and swap the relocs from the section indicated by SHDR. This
2615 may be either a REL or a RELA section. The relocations are
2616 translated into RELA relocations and stored in INTERNAL_RELOCS,
2617 which should have already been allocated to contain enough space.
2618 The EXTERNAL_RELOCS are a buffer where the external form of the
2619 relocations should be stored.
2621 Returns FALSE if something goes wrong. */
2623 static bfd_boolean
2624 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2625 internal_relocs)
2626 bfd *abfd;
2627 Elf_Internal_Shdr *shdr;
2628 PTR external_relocs;
2629 Elf_Internal_Rela *internal_relocs;
2631 struct elf_backend_data *bed;
2632 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
2633 const bfd_byte *erela;
2634 const bfd_byte *erelaend;
2635 Elf_Internal_Rela *irela;
2637 /* If there aren't any relocations, that's OK. */
2638 if (!shdr)
2639 return TRUE;
2641 /* Position ourselves at the start of the section. */
2642 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2643 return FALSE;
2645 /* Read the relocations. */
2646 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2647 return FALSE;
2649 bed = get_elf_backend_data (abfd);
2651 /* Convert the external relocations to the internal format. */
2652 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2653 swap_in = bed->s->swap_reloc_in;
2654 else if (shdr->sh_entsize == sizeof (Elf_External_Rela))
2655 swap_in = bed->s->swap_reloca_in;
2656 else
2658 bfd_set_error (bfd_error_wrong_format);
2659 return FALSE;
2662 erela = external_relocs;
2663 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
2664 irela = internal_relocs;
2665 while (erela < erelaend)
2667 (*swap_in) (abfd, erela, irela);
2668 irela += bed->s->int_rels_per_ext_rel;
2669 erela += shdr->sh_entsize;
2672 return TRUE;
2675 /* Read and swap the relocs for a section O. They may have been
2676 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2677 not NULL, they are used as buffers to read into. They are known to
2678 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2679 the return value is allocated using either malloc or bfd_alloc,
2680 according to the KEEP_MEMORY argument. If O has two relocation
2681 sections (both REL and RELA relocations), then the REL_HDR
2682 relocations will appear first in INTERNAL_RELOCS, followed by the
2683 REL_HDR2 relocations. */
2685 Elf_Internal_Rela *
2686 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2687 keep_memory)
2688 bfd *abfd;
2689 asection *o;
2690 PTR external_relocs;
2691 Elf_Internal_Rela *internal_relocs;
2692 bfd_boolean keep_memory;
2694 Elf_Internal_Shdr *rel_hdr;
2695 PTR alloc1 = NULL;
2696 Elf_Internal_Rela *alloc2 = NULL;
2697 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2699 if (elf_section_data (o)->relocs != NULL)
2700 return elf_section_data (o)->relocs;
2702 if (o->reloc_count == 0)
2703 return NULL;
2705 rel_hdr = &elf_section_data (o)->rel_hdr;
2707 if (internal_relocs == NULL)
2709 bfd_size_type size;
2711 size = o->reloc_count;
2712 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2713 if (keep_memory)
2714 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2715 else
2716 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2717 if (internal_relocs == NULL)
2718 goto error_return;
2721 if (external_relocs == NULL)
2723 bfd_size_type size = rel_hdr->sh_size;
2725 if (elf_section_data (o)->rel_hdr2)
2726 size += elf_section_data (o)->rel_hdr2->sh_size;
2727 alloc1 = (PTR) bfd_malloc (size);
2728 if (alloc1 == NULL)
2729 goto error_return;
2730 external_relocs = alloc1;
2733 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2734 external_relocs,
2735 internal_relocs))
2736 goto error_return;
2737 if (!elf_link_read_relocs_from_section
2738 (abfd,
2739 elf_section_data (o)->rel_hdr2,
2740 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2741 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2742 * bed->s->int_rels_per_ext_rel)))
2743 goto error_return;
2745 /* Cache the results for next time, if we can. */
2746 if (keep_memory)
2747 elf_section_data (o)->relocs = internal_relocs;
2749 if (alloc1 != NULL)
2750 free (alloc1);
2752 /* Don't free alloc2, since if it was allocated we are passing it
2753 back (under the name of internal_relocs). */
2755 return internal_relocs;
2757 error_return:
2758 if (alloc1 != NULL)
2759 free (alloc1);
2760 if (alloc2 != NULL)
2761 free (alloc2);
2762 return NULL;
2765 /* Record an assignment to a symbol made by a linker script. We need
2766 this in case some dynamic object refers to this symbol. */
2768 bfd_boolean
2769 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2770 bfd *output_bfd ATTRIBUTE_UNUSED;
2771 struct bfd_link_info *info;
2772 const char *name;
2773 bfd_boolean provide;
2775 struct elf_link_hash_entry *h;
2777 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2778 return TRUE;
2780 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
2781 if (h == NULL)
2782 return FALSE;
2784 if (h->root.type == bfd_link_hash_new)
2785 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2787 /* If this symbol is being provided by the linker script, and it is
2788 currently defined by a dynamic object, but not by a regular
2789 object, then mark it as undefined so that the generic linker will
2790 force the correct value. */
2791 if (provide
2792 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2793 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2794 h->root.type = bfd_link_hash_undefined;
2796 /* If this symbol is not being provided by the linker script, and it is
2797 currently defined by a dynamic object, but not by a regular object,
2798 then clear out any version information because the symbol will not be
2799 associated with the dynamic object any more. */
2800 if (!provide
2801 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2802 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2803 h->verinfo.verdef = NULL;
2805 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2807 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2808 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2809 || info->shared)
2810 && h->dynindx == -1)
2812 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2813 return FALSE;
2815 /* If this is a weak defined symbol, and we know a corresponding
2816 real symbol from the same dynamic object, make sure the real
2817 symbol is also made into a dynamic symbol. */
2818 if (h->weakdef != NULL
2819 && h->weakdef->dynindx == -1)
2821 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2822 return FALSE;
2826 return TRUE;
2829 /* This structure is used to pass information to
2830 elf_link_assign_sym_version. */
2832 struct elf_assign_sym_version_info
2834 /* Output BFD. */
2835 bfd *output_bfd;
2836 /* General link information. */
2837 struct bfd_link_info *info;
2838 /* Version tree. */
2839 struct bfd_elf_version_tree *verdefs;
2840 /* Whether we had a failure. */
2841 bfd_boolean failed;
2844 /* This structure is used to pass information to
2845 elf_link_find_version_dependencies. */
2847 struct elf_find_verdep_info
2849 /* Output BFD. */
2850 bfd *output_bfd;
2851 /* General link information. */
2852 struct bfd_link_info *info;
2853 /* The number of dependencies. */
2854 unsigned int vers;
2855 /* Whether we had a failure. */
2856 bfd_boolean failed;
2859 /* Array used to determine the number of hash table buckets to use
2860 based on the number of symbols there are. If there are fewer than
2861 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2862 fewer than 37 we use 17 buckets, and so forth. We never use more
2863 than 32771 buckets. */
2865 static const size_t elf_buckets[] =
2867 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2868 16411, 32771, 0
2871 /* Compute bucket count for hashing table. We do not use a static set
2872 of possible tables sizes anymore. Instead we determine for all
2873 possible reasonable sizes of the table the outcome (i.e., the
2874 number of collisions etc) and choose the best solution. The
2875 weighting functions are not too simple to allow the table to grow
2876 without bounds. Instead one of the weighting factors is the size.
2877 Therefore the result is always a good payoff between few collisions
2878 (= short chain lengths) and table size. */
2879 static size_t
2880 compute_bucket_count (info)
2881 struct bfd_link_info *info;
2883 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2884 size_t best_size = 0;
2885 unsigned long int *hashcodes;
2886 unsigned long int *hashcodesp;
2887 unsigned long int i;
2888 bfd_size_type amt;
2890 /* Compute the hash values for all exported symbols. At the same
2891 time store the values in an array so that we could use them for
2892 optimizations. */
2893 amt = dynsymcount;
2894 amt *= sizeof (unsigned long int);
2895 hashcodes = (unsigned long int *) bfd_malloc (amt);
2896 if (hashcodes == NULL)
2897 return 0;
2898 hashcodesp = hashcodes;
2900 /* Put all hash values in HASHCODES. */
2901 elf_link_hash_traverse (elf_hash_table (info),
2902 elf_collect_hash_codes, &hashcodesp);
2904 /* We have a problem here. The following code to optimize the table
2905 size requires an integer type with more the 32 bits. If
2906 BFD_HOST_U_64_BIT is set we know about such a type. */
2907 #ifdef BFD_HOST_U_64_BIT
2908 if (info->optimize)
2910 unsigned long int nsyms = hashcodesp - hashcodes;
2911 size_t minsize;
2912 size_t maxsize;
2913 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2914 unsigned long int *counts ;
2916 /* Possible optimization parameters: if we have NSYMS symbols we say
2917 that the hashing table must at least have NSYMS/4 and at most
2918 2*NSYMS buckets. */
2919 minsize = nsyms / 4;
2920 if (minsize == 0)
2921 minsize = 1;
2922 best_size = maxsize = nsyms * 2;
2924 /* Create array where we count the collisions in. We must use bfd_malloc
2925 since the size could be large. */
2926 amt = maxsize;
2927 amt *= sizeof (unsigned long int);
2928 counts = (unsigned long int *) bfd_malloc (amt);
2929 if (counts == NULL)
2931 free (hashcodes);
2932 return 0;
2935 /* Compute the "optimal" size for the hash table. The criteria is a
2936 minimal chain length. The minor criteria is (of course) the size
2937 of the table. */
2938 for (i = minsize; i < maxsize; ++i)
2940 /* Walk through the array of hashcodes and count the collisions. */
2941 BFD_HOST_U_64_BIT max;
2942 unsigned long int j;
2943 unsigned long int fact;
2945 memset (counts, '\0', i * sizeof (unsigned long int));
2947 /* Determine how often each hash bucket is used. */
2948 for (j = 0; j < nsyms; ++j)
2949 ++counts[hashcodes[j] % i];
2951 /* For the weight function we need some information about the
2952 pagesize on the target. This is information need not be 100%
2953 accurate. Since this information is not available (so far) we
2954 define it here to a reasonable default value. If it is crucial
2955 to have a better value some day simply define this value. */
2956 # ifndef BFD_TARGET_PAGESIZE
2957 # define BFD_TARGET_PAGESIZE (4096)
2958 # endif
2960 /* We in any case need 2 + NSYMS entries for the size values and
2961 the chains. */
2962 max = (2 + nsyms) * (ARCH_SIZE / 8);
2964 # if 1
2965 /* Variant 1: optimize for short chains. We add the squares
2966 of all the chain lengths (which favous many small chain
2967 over a few long chains). */
2968 for (j = 0; j < i; ++j)
2969 max += counts[j] * counts[j];
2971 /* This adds penalties for the overall size of the table. */
2972 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2973 max *= fact * fact;
2974 # else
2975 /* Variant 2: Optimize a lot more for small table. Here we
2976 also add squares of the size but we also add penalties for
2977 empty slots (the +1 term). */
2978 for (j = 0; j < i; ++j)
2979 max += (1 + counts[j]) * (1 + counts[j]);
2981 /* The overall size of the table is considered, but not as
2982 strong as in variant 1, where it is squared. */
2983 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2984 max *= fact;
2985 # endif
2987 /* Compare with current best results. */
2988 if (max < best_chlen)
2990 best_chlen = max;
2991 best_size = i;
2995 free (counts);
2997 else
2998 #endif /* defined (BFD_HOST_U_64_BIT) */
3000 /* This is the fallback solution if no 64bit type is available or if we
3001 are not supposed to spend much time on optimizations. We select the
3002 bucket count using a fixed set of numbers. */
3003 for (i = 0; elf_buckets[i] != 0; i++)
3005 best_size = elf_buckets[i];
3006 if (dynsymcount < elf_buckets[i + 1])
3007 break;
3011 /* Free the arrays we needed. */
3012 free (hashcodes);
3014 return best_size;
3017 /* Set up the sizes and contents of the ELF dynamic sections. This is
3018 called by the ELF linker emulation before_allocation routine. We
3019 must set the sizes of the sections before the linker sets the
3020 addresses of the various sections. */
3022 bfd_boolean
3023 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3024 filter_shlib,
3025 auxiliary_filters, info, sinterpptr,
3026 verdefs)
3027 bfd *output_bfd;
3028 const char *soname;
3029 const char *rpath;
3030 const char *filter_shlib;
3031 const char * const *auxiliary_filters;
3032 struct bfd_link_info *info;
3033 asection **sinterpptr;
3034 struct bfd_elf_version_tree *verdefs;
3036 bfd_size_type soname_indx;
3037 bfd *dynobj;
3038 struct elf_backend_data *bed;
3039 struct elf_assign_sym_version_info asvinfo;
3041 *sinterpptr = NULL;
3043 soname_indx = (bfd_size_type) -1;
3045 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3046 return TRUE;
3048 if (! is_elf_hash_table (info))
3049 return TRUE;
3051 /* Any syms created from now on start with -1 in
3052 got.refcount/offset and plt.refcount/offset. */
3053 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
3055 /* The backend may have to create some sections regardless of whether
3056 we're dynamic or not. */
3057 bed = get_elf_backend_data (output_bfd);
3058 if (bed->elf_backend_always_size_sections
3059 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3060 return FALSE;
3062 dynobj = elf_hash_table (info)->dynobj;
3064 /* If there were no dynamic objects in the link, there is nothing to
3065 do here. */
3066 if (dynobj == NULL)
3067 return TRUE;
3069 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3070 return FALSE;
3072 if (elf_hash_table (info)->dynamic_sections_created)
3074 struct elf_info_failed eif;
3075 struct elf_link_hash_entry *h;
3076 asection *dynstr;
3077 struct bfd_elf_version_tree *t;
3078 struct bfd_elf_version_expr *d;
3079 bfd_boolean all_defined;
3081 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3082 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3084 if (soname != NULL)
3086 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3087 soname, TRUE);
3088 if (soname_indx == (bfd_size_type) -1
3089 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3090 soname_indx))
3091 return FALSE;
3094 if (info->symbolic)
3096 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3097 (bfd_vma) 0))
3098 return FALSE;
3099 info->flags |= DF_SYMBOLIC;
3102 if (rpath != NULL)
3104 bfd_size_type indx;
3106 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3107 TRUE);
3108 if (info->new_dtags)
3109 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3110 if (indx == (bfd_size_type) -1
3111 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3112 || (info->new_dtags
3113 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3114 indx)))
3115 return FALSE;
3118 if (filter_shlib != NULL)
3120 bfd_size_type indx;
3122 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3123 filter_shlib, TRUE);
3124 if (indx == (bfd_size_type) -1
3125 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3126 return FALSE;
3129 if (auxiliary_filters != NULL)
3131 const char * const *p;
3133 for (p = auxiliary_filters; *p != NULL; p++)
3135 bfd_size_type indx;
3137 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3138 *p, TRUE);
3139 if (indx == (bfd_size_type) -1
3140 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3141 indx))
3142 return FALSE;
3146 eif.info = info;
3147 eif.verdefs = verdefs;
3148 eif.failed = FALSE;
3150 /* If we are supposed to export all symbols into the dynamic symbol
3151 table (this is not the normal case), then do so. */
3152 if (info->export_dynamic)
3154 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3155 (PTR) &eif);
3156 if (eif.failed)
3157 return FALSE;
3160 /* Make all global versions with definiton. */
3161 for (t = verdefs; t != NULL; t = t->next)
3162 for (d = t->globals; d != NULL; d = d->next)
3163 if (!d->symver && strchr (d->pattern, '*') == NULL)
3165 const char *verstr, *name;
3166 size_t namelen, verlen, newlen;
3167 char *newname, *p;
3168 struct elf_link_hash_entry *newh;
3170 name = d->pattern;
3171 namelen = strlen (name);
3172 verstr = t->name;
3173 verlen = strlen (verstr);
3174 newlen = namelen + verlen + 3;
3176 newname = (char *) bfd_malloc ((bfd_size_type) newlen);
3177 if (newname == NULL)
3178 return FALSE;
3179 memcpy (newname, name, namelen);
3181 /* Check the hidden versioned definition. */
3182 p = newname + namelen;
3183 *p++ = ELF_VER_CHR;
3184 memcpy (p, verstr, verlen + 1);
3185 newh = elf_link_hash_lookup (elf_hash_table (info),
3186 newname, FALSE, FALSE,
3187 FALSE);
3188 if (newh == NULL
3189 || (newh->root.type != bfd_link_hash_defined
3190 && newh->root.type != bfd_link_hash_defweak))
3192 /* Check the default versioned definition. */
3193 *p++ = ELF_VER_CHR;
3194 memcpy (p, verstr, verlen + 1);
3195 newh = elf_link_hash_lookup (elf_hash_table (info),
3196 newname, FALSE, FALSE,
3197 FALSE);
3199 free (newname);
3201 /* Mark this version if there is a definition and it is
3202 not defined in a shared object. */
3203 if (newh != NULL
3204 && ((newh->elf_link_hash_flags
3205 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
3206 && (newh->root.type == bfd_link_hash_defined
3207 || newh->root.type == bfd_link_hash_defweak))
3208 d->symver = 1;
3211 /* Attach all the symbols to their version information. */
3212 asvinfo.output_bfd = output_bfd;
3213 asvinfo.info = info;
3214 asvinfo.verdefs = verdefs;
3215 asvinfo.failed = FALSE;
3217 elf_link_hash_traverse (elf_hash_table (info),
3218 elf_link_assign_sym_version,
3219 (PTR) &asvinfo);
3220 if (asvinfo.failed)
3221 return FALSE;
3223 if (!info->allow_undefined_version)
3225 /* Check if all global versions have a definiton. */
3226 all_defined = TRUE;
3227 for (t = verdefs; t != NULL; t = t->next)
3228 for (d = t->globals; d != NULL; d = d->next)
3229 if (!d->symver && !d->script
3230 && strchr (d->pattern, '*') == NULL)
3232 (*_bfd_error_handler)
3233 (_("%s: undefined version: %s"),
3234 d->pattern, t->name);
3235 all_defined = FALSE;
3238 if (!all_defined)
3240 bfd_set_error (bfd_error_bad_value);
3241 return FALSE;
3245 /* Find all symbols which were defined in a dynamic object and make
3246 the backend pick a reasonable value for them. */
3247 elf_link_hash_traverse (elf_hash_table (info),
3248 elf_adjust_dynamic_symbol,
3249 (PTR) &eif);
3250 if (eif.failed)
3251 return FALSE;
3253 /* Add some entries to the .dynamic section. We fill in some of the
3254 values later, in elf_bfd_final_link, but we must add the entries
3255 now so that we know the final size of the .dynamic section. */
3257 /* If there are initialization and/or finalization functions to
3258 call then add the corresponding DT_INIT/DT_FINI entries. */
3259 h = (info->init_function
3260 ? elf_link_hash_lookup (elf_hash_table (info),
3261 info->init_function, FALSE,
3262 FALSE, FALSE)
3263 : NULL);
3264 if (h != NULL
3265 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3266 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3268 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3269 return FALSE;
3271 h = (info->fini_function
3272 ? elf_link_hash_lookup (elf_hash_table (info),
3273 info->fini_function, FALSE,
3274 FALSE, FALSE)
3275 : NULL);
3276 if (h != NULL
3277 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3278 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3280 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3281 return FALSE;
3284 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3286 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3287 if (info->shared)
3289 bfd *sub;
3290 asection *o;
3292 for (sub = info->input_bfds; sub != NULL;
3293 sub = sub->link_next)
3294 for (o = sub->sections; o != NULL; o = o->next)
3295 if (elf_section_data (o)->this_hdr.sh_type
3296 == SHT_PREINIT_ARRAY)
3298 (*_bfd_error_handler)
3299 (_("%s: .preinit_array section is not allowed in DSO"),
3300 bfd_archive_filename (sub));
3301 break;
3304 bfd_set_error (bfd_error_nonrepresentable_section);
3305 return FALSE;
3308 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3309 (bfd_vma) 0)
3310 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3311 (bfd_vma) 0))
3312 return FALSE;
3314 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3316 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3317 (bfd_vma) 0)
3318 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3319 (bfd_vma) 0))
3320 return FALSE;
3322 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3324 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3325 (bfd_vma) 0)
3326 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3327 (bfd_vma) 0))
3328 return FALSE;
3331 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3332 /* If .dynstr is excluded from the link, we don't want any of
3333 these tags. Strictly, we should be checking each section
3334 individually; This quick check covers for the case where
3335 someone does a /DISCARD/ : { *(*) }. */
3336 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3338 bfd_size_type strsize;
3340 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3341 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3342 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3343 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3344 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3345 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3346 (bfd_vma) sizeof (Elf_External_Sym)))
3347 return FALSE;
3351 /* The backend must work out the sizes of all the other dynamic
3352 sections. */
3353 if (bed->elf_backend_size_dynamic_sections
3354 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3355 return FALSE;
3357 if (elf_hash_table (info)->dynamic_sections_created)
3359 bfd_size_type dynsymcount;
3360 asection *s;
3361 size_t bucketcount = 0;
3362 size_t hash_entry_size;
3363 unsigned int dtagcount;
3365 /* Set up the version definition section. */
3366 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3367 BFD_ASSERT (s != NULL);
3369 /* We may have created additional version definitions if we are
3370 just linking a regular application. */
3371 verdefs = asvinfo.verdefs;
3373 /* Skip anonymous version tag. */
3374 if (verdefs != NULL && verdefs->vernum == 0)
3375 verdefs = verdefs->next;
3377 if (verdefs == NULL)
3378 _bfd_strip_section_from_output (info, s);
3379 else
3381 unsigned int cdefs;
3382 bfd_size_type size;
3383 struct bfd_elf_version_tree *t;
3384 bfd_byte *p;
3385 Elf_Internal_Verdef def;
3386 Elf_Internal_Verdaux defaux;
3388 cdefs = 0;
3389 size = 0;
3391 /* Make space for the base version. */
3392 size += sizeof (Elf_External_Verdef);
3393 size += sizeof (Elf_External_Verdaux);
3394 ++cdefs;
3396 for (t = verdefs; t != NULL; t = t->next)
3398 struct bfd_elf_version_deps *n;
3400 size += sizeof (Elf_External_Verdef);
3401 size += sizeof (Elf_External_Verdaux);
3402 ++cdefs;
3404 for (n = t->deps; n != NULL; n = n->next)
3405 size += sizeof (Elf_External_Verdaux);
3408 s->_raw_size = size;
3409 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3410 if (s->contents == NULL && s->_raw_size != 0)
3411 return FALSE;
3413 /* Fill in the version definition section. */
3415 p = s->contents;
3417 def.vd_version = VER_DEF_CURRENT;
3418 def.vd_flags = VER_FLG_BASE;
3419 def.vd_ndx = 1;
3420 def.vd_cnt = 1;
3421 def.vd_aux = sizeof (Elf_External_Verdef);
3422 def.vd_next = (sizeof (Elf_External_Verdef)
3423 + sizeof (Elf_External_Verdaux));
3425 if (soname_indx != (bfd_size_type) -1)
3427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3428 soname_indx);
3429 def.vd_hash = bfd_elf_hash (soname);
3430 defaux.vda_name = soname_indx;
3432 else
3434 const char *name;
3435 bfd_size_type indx;
3437 name = basename (output_bfd->filename);
3438 def.vd_hash = bfd_elf_hash (name);
3439 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3440 name, FALSE);
3441 if (indx == (bfd_size_type) -1)
3442 return FALSE;
3443 defaux.vda_name = indx;
3445 defaux.vda_next = 0;
3447 _bfd_elf_swap_verdef_out (output_bfd, &def,
3448 (Elf_External_Verdef *) p);
3449 p += sizeof (Elf_External_Verdef);
3450 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3451 (Elf_External_Verdaux *) p);
3452 p += sizeof (Elf_External_Verdaux);
3454 for (t = verdefs; t != NULL; t = t->next)
3456 unsigned int cdeps;
3457 struct bfd_elf_version_deps *n;
3458 struct elf_link_hash_entry *h;
3459 struct bfd_link_hash_entry *bh;
3461 cdeps = 0;
3462 for (n = t->deps; n != NULL; n = n->next)
3463 ++cdeps;
3465 /* Add a symbol representing this version. */
3466 bh = NULL;
3467 if (! (_bfd_generic_link_add_one_symbol
3468 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3469 (bfd_vma) 0, (const char *) NULL, FALSE,
3470 get_elf_backend_data (dynobj)->collect, &bh)))
3471 return FALSE;
3472 h = (struct elf_link_hash_entry *) bh;
3473 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3474 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3475 h->type = STT_OBJECT;
3476 h->verinfo.vertree = t;
3478 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3479 return FALSE;
3481 def.vd_version = VER_DEF_CURRENT;
3482 def.vd_flags = 0;
3483 if (t->globals == NULL && t->locals == NULL && ! t->used)
3484 def.vd_flags |= VER_FLG_WEAK;
3485 def.vd_ndx = t->vernum + 1;
3486 def.vd_cnt = cdeps + 1;
3487 def.vd_hash = bfd_elf_hash (t->name);
3488 def.vd_aux = sizeof (Elf_External_Verdef);
3489 if (t->next != NULL)
3490 def.vd_next = (sizeof (Elf_External_Verdef)
3491 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3492 else
3493 def.vd_next = 0;
3495 _bfd_elf_swap_verdef_out (output_bfd, &def,
3496 (Elf_External_Verdef *) p);
3497 p += sizeof (Elf_External_Verdef);
3499 defaux.vda_name = h->dynstr_index;
3500 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3501 h->dynstr_index);
3502 if (t->deps == NULL)
3503 defaux.vda_next = 0;
3504 else
3505 defaux.vda_next = sizeof (Elf_External_Verdaux);
3506 t->name_indx = defaux.vda_name;
3508 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3509 (Elf_External_Verdaux *) p);
3510 p += sizeof (Elf_External_Verdaux);
3512 for (n = t->deps; n != NULL; n = n->next)
3514 if (n->version_needed == NULL)
3516 /* This can happen if there was an error in the
3517 version script. */
3518 defaux.vda_name = 0;
3520 else
3522 defaux.vda_name = n->version_needed->name_indx;
3523 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3524 defaux.vda_name);
3526 if (n->next == NULL)
3527 defaux.vda_next = 0;
3528 else
3529 defaux.vda_next = sizeof (Elf_External_Verdaux);
3531 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3532 (Elf_External_Verdaux *) p);
3533 p += sizeof (Elf_External_Verdaux);
3537 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3538 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3539 (bfd_vma) cdefs))
3540 return FALSE;
3542 elf_tdata (output_bfd)->cverdefs = cdefs;
3545 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
3547 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3548 return FALSE;
3551 if (info->flags_1)
3553 if (! info->shared)
3554 info->flags_1 &= ~ (DF_1_INITFIRST
3555 | DF_1_NODELETE
3556 | DF_1_NOOPEN);
3557 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3558 info->flags_1))
3559 return FALSE;
3562 /* Work out the size of the version reference section. */
3564 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3565 BFD_ASSERT (s != NULL);
3567 struct elf_find_verdep_info sinfo;
3569 sinfo.output_bfd = output_bfd;
3570 sinfo.info = info;
3571 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3572 if (sinfo.vers == 0)
3573 sinfo.vers = 1;
3574 sinfo.failed = FALSE;
3576 elf_link_hash_traverse (elf_hash_table (info),
3577 elf_link_find_version_dependencies,
3578 (PTR) &sinfo);
3580 if (elf_tdata (output_bfd)->verref == NULL)
3581 _bfd_strip_section_from_output (info, s);
3582 else
3584 Elf_Internal_Verneed *t;
3585 unsigned int size;
3586 unsigned int crefs;
3587 bfd_byte *p;
3589 /* Build the version definition section. */
3590 size = 0;
3591 crefs = 0;
3592 for (t = elf_tdata (output_bfd)->verref;
3593 t != NULL;
3594 t = t->vn_nextref)
3596 Elf_Internal_Vernaux *a;
3598 size += sizeof (Elf_External_Verneed);
3599 ++crefs;
3600 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3601 size += sizeof (Elf_External_Vernaux);
3604 s->_raw_size = size;
3605 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3606 if (s->contents == NULL)
3607 return FALSE;
3609 p = s->contents;
3610 for (t = elf_tdata (output_bfd)->verref;
3611 t != NULL;
3612 t = t->vn_nextref)
3614 unsigned int caux;
3615 Elf_Internal_Vernaux *a;
3616 bfd_size_type indx;
3618 caux = 0;
3619 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3620 ++caux;
3622 t->vn_version = VER_NEED_CURRENT;
3623 t->vn_cnt = caux;
3624 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3625 elf_dt_name (t->vn_bfd) != NULL
3626 ? elf_dt_name (t->vn_bfd)
3627 : basename (t->vn_bfd->filename),
3628 FALSE);
3629 if (indx == (bfd_size_type) -1)
3630 return FALSE;
3631 t->vn_file = indx;
3632 t->vn_aux = sizeof (Elf_External_Verneed);
3633 if (t->vn_nextref == NULL)
3634 t->vn_next = 0;
3635 else
3636 t->vn_next = (sizeof (Elf_External_Verneed)
3637 + caux * sizeof (Elf_External_Vernaux));
3639 _bfd_elf_swap_verneed_out (output_bfd, t,
3640 (Elf_External_Verneed *) p);
3641 p += sizeof (Elf_External_Verneed);
3643 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3645 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3646 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3647 a->vna_nodename, FALSE);
3648 if (indx == (bfd_size_type) -1)
3649 return FALSE;
3650 a->vna_name = indx;
3651 if (a->vna_nextptr == NULL)
3652 a->vna_next = 0;
3653 else
3654 a->vna_next = sizeof (Elf_External_Vernaux);
3656 _bfd_elf_swap_vernaux_out (output_bfd, a,
3657 (Elf_External_Vernaux *) p);
3658 p += sizeof (Elf_External_Vernaux);
3662 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3663 (bfd_vma) 0)
3664 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3665 (bfd_vma) crefs))
3666 return FALSE;
3668 elf_tdata (output_bfd)->cverrefs = crefs;
3672 /* Assign dynsym indicies. In a shared library we generate a
3673 section symbol for each output section, which come first.
3674 Next come all of the back-end allocated local dynamic syms,
3675 followed by the rest of the global symbols. */
3677 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3679 /* Work out the size of the symbol version section. */
3680 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3681 BFD_ASSERT (s != NULL);
3682 if (dynsymcount == 0
3683 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3685 _bfd_strip_section_from_output (info, s);
3686 /* The DYNSYMCOUNT might have changed if we were going to
3687 output a dynamic symbol table entry for S. */
3688 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3690 else
3692 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3693 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3694 if (s->contents == NULL)
3695 return FALSE;
3697 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3698 return FALSE;
3701 /* Set the size of the .dynsym and .hash sections. We counted
3702 the number of dynamic symbols in elf_link_add_object_symbols.
3703 We will build the contents of .dynsym and .hash when we build
3704 the final symbol table, because until then we do not know the
3705 correct value to give the symbols. We built the .dynstr
3706 section as we went along in elf_link_add_object_symbols. */
3707 s = bfd_get_section_by_name (dynobj, ".dynsym");
3708 BFD_ASSERT (s != NULL);
3709 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3710 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3711 if (s->contents == NULL && s->_raw_size != 0)
3712 return FALSE;
3714 if (dynsymcount != 0)
3716 Elf_Internal_Sym isym;
3718 /* The first entry in .dynsym is a dummy symbol. */
3719 isym.st_value = 0;
3720 isym.st_size = 0;
3721 isym.st_name = 0;
3722 isym.st_info = 0;
3723 isym.st_other = 0;
3724 isym.st_shndx = 0;
3725 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3728 /* Compute the size of the hashing table. As a side effect this
3729 computes the hash values for all the names we export. */
3730 bucketcount = compute_bucket_count (info);
3732 s = bfd_get_section_by_name (dynobj, ".hash");
3733 BFD_ASSERT (s != NULL);
3734 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3735 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3736 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3737 if (s->contents == NULL)
3738 return FALSE;
3740 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3741 s->contents);
3742 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3743 s->contents + hash_entry_size);
3745 elf_hash_table (info)->bucketcount = bucketcount;
3747 s = bfd_get_section_by_name (dynobj, ".dynstr");
3748 BFD_ASSERT (s != NULL);
3750 elf_finalize_dynstr (output_bfd, info);
3752 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3754 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3755 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3756 return FALSE;
3759 return TRUE;
3762 /* This function is used to adjust offsets into .dynstr for
3763 dynamic symbols. This is called via elf_link_hash_traverse. */
3765 static bfd_boolean elf_adjust_dynstr_offsets
3766 PARAMS ((struct elf_link_hash_entry *, PTR));
3768 static bfd_boolean
3769 elf_adjust_dynstr_offsets (h, data)
3770 struct elf_link_hash_entry *h;
3771 PTR data;
3773 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3775 if (h->root.type == bfd_link_hash_warning)
3776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3778 if (h->dynindx != -1)
3779 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3780 return TRUE;
3783 /* Assign string offsets in .dynstr, update all structures referencing
3784 them. */
3786 static bfd_boolean
3787 elf_finalize_dynstr (output_bfd, info)
3788 bfd *output_bfd;
3789 struct bfd_link_info *info;
3791 struct elf_link_local_dynamic_entry *entry;
3792 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3793 bfd *dynobj = elf_hash_table (info)->dynobj;
3794 asection *sdyn;
3795 bfd_size_type size;
3796 Elf_External_Dyn *dyncon, *dynconend;
3798 _bfd_elf_strtab_finalize (dynstr);
3799 size = _bfd_elf_strtab_size (dynstr);
3801 /* Update all .dynamic entries referencing .dynstr strings. */
3802 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3803 BFD_ASSERT (sdyn != NULL);
3805 dyncon = (Elf_External_Dyn *) sdyn->contents;
3806 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3807 sdyn->_raw_size);
3808 for (; dyncon < dynconend; dyncon++)
3810 Elf_Internal_Dyn dyn;
3812 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3813 switch (dyn.d_tag)
3815 case DT_STRSZ:
3816 dyn.d_un.d_val = size;
3817 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3818 break;
3819 case DT_NEEDED:
3820 case DT_SONAME:
3821 case DT_RPATH:
3822 case DT_RUNPATH:
3823 case DT_FILTER:
3824 case DT_AUXILIARY:
3825 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3826 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3827 break;
3828 default:
3829 break;
3833 /* Now update local dynamic symbols. */
3834 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3835 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3836 entry->isym.st_name);
3838 /* And the rest of dynamic symbols. */
3839 elf_link_hash_traverse (elf_hash_table (info),
3840 elf_adjust_dynstr_offsets, dynstr);
3842 /* Adjust version definitions. */
3843 if (elf_tdata (output_bfd)->cverdefs)
3845 asection *s;
3846 bfd_byte *p;
3847 bfd_size_type i;
3848 Elf_Internal_Verdef def;
3849 Elf_Internal_Verdaux defaux;
3851 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3852 p = (bfd_byte *) s->contents;
3855 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3856 &def);
3857 p += sizeof (Elf_External_Verdef);
3858 for (i = 0; i < def.vd_cnt; ++i)
3860 _bfd_elf_swap_verdaux_in (output_bfd,
3861 (Elf_External_Verdaux *) p, &defaux);
3862 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3863 defaux.vda_name);
3864 _bfd_elf_swap_verdaux_out (output_bfd,
3865 &defaux, (Elf_External_Verdaux *) p);
3866 p += sizeof (Elf_External_Verdaux);
3869 while (def.vd_next);
3872 /* Adjust version references. */
3873 if (elf_tdata (output_bfd)->verref)
3875 asection *s;
3876 bfd_byte *p;
3877 bfd_size_type i;
3878 Elf_Internal_Verneed need;
3879 Elf_Internal_Vernaux needaux;
3881 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3882 p = (bfd_byte *) s->contents;
3885 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3886 &need);
3887 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3888 _bfd_elf_swap_verneed_out (output_bfd, &need,
3889 (Elf_External_Verneed *) p);
3890 p += sizeof (Elf_External_Verneed);
3891 for (i = 0; i < need.vn_cnt; ++i)
3893 _bfd_elf_swap_vernaux_in (output_bfd,
3894 (Elf_External_Vernaux *) p, &needaux);
3895 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3896 needaux.vna_name);
3897 _bfd_elf_swap_vernaux_out (output_bfd,
3898 &needaux,
3899 (Elf_External_Vernaux *) p);
3900 p += sizeof (Elf_External_Vernaux);
3903 while (need.vn_next);
3906 return TRUE;
3909 /* Fix up the flags for a symbol. This handles various cases which
3910 can only be fixed after all the input files are seen. This is
3911 currently called by both adjust_dynamic_symbol and
3912 assign_sym_version, which is unnecessary but perhaps more robust in
3913 the face of future changes. */
3915 static bfd_boolean
3916 elf_fix_symbol_flags (h, eif)
3917 struct elf_link_hash_entry *h;
3918 struct elf_info_failed *eif;
3920 /* If this symbol was mentioned in a non-ELF file, try to set
3921 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3922 permit a non-ELF file to correctly refer to a symbol defined in
3923 an ELF dynamic object. */
3924 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3926 while (h->root.type == bfd_link_hash_indirect)
3927 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3929 if (h->root.type != bfd_link_hash_defined
3930 && h->root.type != bfd_link_hash_defweak)
3931 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3932 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3933 else
3935 if (h->root.u.def.section->owner != NULL
3936 && (bfd_get_flavour (h->root.u.def.section->owner)
3937 == bfd_target_elf_flavour))
3938 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3939 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3940 else
3941 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3944 if (h->dynindx == -1
3945 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3946 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3948 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3950 eif->failed = TRUE;
3951 return FALSE;
3955 else
3957 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3958 was first seen in a non-ELF file. Fortunately, if the symbol
3959 was first seen in an ELF file, we're probably OK unless the
3960 symbol was defined in a non-ELF file. Catch that case here.
3961 FIXME: We're still in trouble if the symbol was first seen in
3962 a dynamic object, and then later in a non-ELF regular object. */
3963 if ((h->root.type == bfd_link_hash_defined
3964 || h->root.type == bfd_link_hash_defweak)
3965 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3966 && (h->root.u.def.section->owner != NULL
3967 ? (bfd_get_flavour (h->root.u.def.section->owner)
3968 != bfd_target_elf_flavour)
3969 : (bfd_is_abs_section (h->root.u.def.section)
3970 && (h->elf_link_hash_flags
3971 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3972 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3975 /* If this is a final link, and the symbol was defined as a common
3976 symbol in a regular object file, and there was no definition in
3977 any dynamic object, then the linker will have allocated space for
3978 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3979 flag will not have been set. */
3980 if (h->root.type == bfd_link_hash_defined
3981 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3982 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3983 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3984 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3985 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3987 /* If -Bsymbolic was used (which means to bind references to global
3988 symbols to the definition within the shared object), and this
3989 symbol was defined in a regular object, then it actually doesn't
3990 need a PLT entry, and we can accomplish that by forcing it local.
3991 Likewise, if the symbol has hidden or internal visibility.
3992 FIXME: It might be that we also do not need a PLT for other
3993 non-hidden visibilities, but we would have to tell that to the
3994 backend specifically; we can't just clear PLT-related data here. */
3995 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3996 && eif->info->shared
3997 && is_elf_hash_table (eif->info)
3998 && (eif->info->symbolic
3999 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4000 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
4001 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4003 struct elf_backend_data *bed;
4004 bfd_boolean force_local;
4006 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
4008 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4009 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
4010 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
4013 /* If this is a weak defined symbol in a dynamic object, and we know
4014 the real definition in the dynamic object, copy interesting flags
4015 over to the real definition. */
4016 if (h->weakdef != NULL)
4018 struct elf_link_hash_entry *weakdef;
4020 weakdef = h->weakdef;
4021 if (h->root.type == bfd_link_hash_indirect)
4022 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4024 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4025 || h->root.type == bfd_link_hash_defweak);
4026 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
4027 || weakdef->root.type == bfd_link_hash_defweak);
4028 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
4030 /* If the real definition is defined by a regular object file,
4031 don't do anything special. See the longer description in
4032 elf_adjust_dynamic_symbol, below. */
4033 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4034 h->weakdef = NULL;
4035 else
4037 struct elf_backend_data *bed;
4039 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
4040 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
4044 return TRUE;
4047 /* Make the backend pick a good value for a dynamic symbol. This is
4048 called via elf_link_hash_traverse, and also calls itself
4049 recursively. */
4051 static bfd_boolean
4052 elf_adjust_dynamic_symbol (h, data)
4053 struct elf_link_hash_entry *h;
4054 PTR data;
4056 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4057 bfd *dynobj;
4058 struct elf_backend_data *bed;
4060 if (! is_elf_hash_table (eif->info))
4061 return FALSE;
4063 if (h->root.type == bfd_link_hash_warning)
4065 h->plt = elf_hash_table (eif->info)->init_offset;
4066 h->got = elf_hash_table (eif->info)->init_offset;
4068 /* When warning symbols are created, they **replace** the "real"
4069 entry in the hash table, thus we never get to see the real
4070 symbol in a hash traversal. So look at it now. */
4071 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4074 /* Ignore indirect symbols. These are added by the versioning code. */
4075 if (h->root.type == bfd_link_hash_indirect)
4076 return TRUE;
4078 /* Fix the symbol flags. */
4079 if (! elf_fix_symbol_flags (h, eif))
4080 return FALSE;
4082 /* If this symbol does not require a PLT entry, and it is not
4083 defined by a dynamic object, or is not referenced by a regular
4084 object, ignore it. We do have to handle a weak defined symbol,
4085 even if no regular object refers to it, if we decided to add it
4086 to the dynamic symbol table. FIXME: Do we normally need to worry
4087 about symbols which are defined by one dynamic object and
4088 referenced by another one? */
4089 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4090 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4091 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4092 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4093 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4095 h->plt = elf_hash_table (eif->info)->init_offset;
4096 return TRUE;
4099 /* If we've already adjusted this symbol, don't do it again. This
4100 can happen via a recursive call. */
4101 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4102 return TRUE;
4104 /* Don't look at this symbol again. Note that we must set this
4105 after checking the above conditions, because we may look at a
4106 symbol once, decide not to do anything, and then get called
4107 recursively later after REF_REGULAR is set below. */
4108 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4110 /* If this is a weak definition, and we know a real definition, and
4111 the real symbol is not itself defined by a regular object file,
4112 then get a good value for the real definition. We handle the
4113 real symbol first, for the convenience of the backend routine.
4115 Note that there is a confusing case here. If the real definition
4116 is defined by a regular object file, we don't get the real symbol
4117 from the dynamic object, but we do get the weak symbol. If the
4118 processor backend uses a COPY reloc, then if some routine in the
4119 dynamic object changes the real symbol, we will not see that
4120 change in the corresponding weak symbol. This is the way other
4121 ELF linkers work as well, and seems to be a result of the shared
4122 library model.
4124 I will clarify this issue. Most SVR4 shared libraries define the
4125 variable _timezone and define timezone as a weak synonym. The
4126 tzset call changes _timezone. If you write
4127 extern int timezone;
4128 int _timezone = 5;
4129 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4130 you might expect that, since timezone is a synonym for _timezone,
4131 the same number will print both times. However, if the processor
4132 backend uses a COPY reloc, then actually timezone will be copied
4133 into your process image, and, since you define _timezone
4134 yourself, _timezone will not. Thus timezone and _timezone will
4135 wind up at different memory locations. The tzset call will set
4136 _timezone, leaving timezone unchanged. */
4138 if (h->weakdef != NULL)
4140 /* If we get to this point, we know there is an implicit
4141 reference by a regular object file via the weak symbol H.
4142 FIXME: Is this really true? What if the traversal finds
4143 H->WEAKDEF before it finds H? */
4144 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4146 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4147 return FALSE;
4150 /* If a symbol has no type and no size and does not require a PLT
4151 entry, then we are probably about to do the wrong thing here: we
4152 are probably going to create a COPY reloc for an empty object.
4153 This case can arise when a shared object is built with assembly
4154 code, and the assembly code fails to set the symbol type. */
4155 if (h->size == 0
4156 && h->type == STT_NOTYPE
4157 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4158 (*_bfd_error_handler)
4159 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4160 h->root.root.string);
4162 dynobj = elf_hash_table (eif->info)->dynobj;
4163 bed = get_elf_backend_data (dynobj);
4164 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4166 eif->failed = TRUE;
4167 return FALSE;
4170 return TRUE;
4173 /* This routine is used to export all defined symbols into the dynamic
4174 symbol table. It is called via elf_link_hash_traverse. */
4176 static bfd_boolean
4177 elf_export_symbol (h, data)
4178 struct elf_link_hash_entry *h;
4179 PTR data;
4181 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4183 /* Ignore indirect symbols. These are added by the versioning code. */
4184 if (h->root.type == bfd_link_hash_indirect)
4185 return TRUE;
4187 if (h->root.type == bfd_link_hash_warning)
4188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4190 if (h->dynindx == -1
4191 && (h->elf_link_hash_flags
4192 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4194 struct bfd_elf_version_tree *t;
4195 struct bfd_elf_version_expr *d;
4197 for (t = eif->verdefs; t != NULL; t = t->next)
4199 if (t->globals != NULL)
4201 for (d = t->globals; d != NULL; d = d->next)
4203 if ((*d->match) (d, h->root.root.string))
4204 goto doit;
4208 if (t->locals != NULL)
4210 for (d = t->locals ; d != NULL; d = d->next)
4212 if ((*d->match) (d, h->root.root.string))
4213 return TRUE;
4218 if (!eif->verdefs)
4220 doit:
4221 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4223 eif->failed = TRUE;
4224 return FALSE;
4229 return TRUE;
4232 /* Look through the symbols which are defined in other shared
4233 libraries and referenced here. Update the list of version
4234 dependencies. This will be put into the .gnu.version_r section.
4235 This function is called via elf_link_hash_traverse. */
4237 static bfd_boolean
4238 elf_link_find_version_dependencies (h, data)
4239 struct elf_link_hash_entry *h;
4240 PTR data;
4242 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4243 Elf_Internal_Verneed *t;
4244 Elf_Internal_Vernaux *a;
4245 bfd_size_type amt;
4247 if (h->root.type == bfd_link_hash_warning)
4248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4250 /* We only care about symbols defined in shared objects with version
4251 information. */
4252 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4253 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4254 || h->dynindx == -1
4255 || h->verinfo.verdef == NULL)
4256 return TRUE;
4258 /* See if we already know about this version. */
4259 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4261 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4262 continue;
4264 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4265 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4266 return TRUE;
4268 break;
4271 /* This is a new version. Add it to tree we are building. */
4273 if (t == NULL)
4275 amt = sizeof *t;
4276 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4277 if (t == NULL)
4279 rinfo->failed = TRUE;
4280 return FALSE;
4283 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4284 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4285 elf_tdata (rinfo->output_bfd)->verref = t;
4288 amt = sizeof *a;
4289 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4291 /* Note that we are copying a string pointer here, and testing it
4292 above. If bfd_elf_string_from_elf_section is ever changed to
4293 discard the string data when low in memory, this will have to be
4294 fixed. */
4295 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4297 a->vna_flags = h->verinfo.verdef->vd_flags;
4298 a->vna_nextptr = t->vn_auxptr;
4300 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4301 ++rinfo->vers;
4303 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4305 t->vn_auxptr = a;
4307 return TRUE;
4310 /* Figure out appropriate versions for all the symbols. We may not
4311 have the version number script until we have read all of the input
4312 files, so until that point we don't know which symbols should be
4313 local. This function is called via elf_link_hash_traverse. */
4315 static bfd_boolean
4316 elf_link_assign_sym_version (h, data)
4317 struct elf_link_hash_entry *h;
4318 PTR data;
4320 struct elf_assign_sym_version_info *sinfo;
4321 struct bfd_link_info *info;
4322 struct elf_backend_data *bed;
4323 struct elf_info_failed eif;
4324 char *p;
4325 bfd_size_type amt;
4327 sinfo = (struct elf_assign_sym_version_info *) data;
4328 info = sinfo->info;
4330 if (h->root.type == bfd_link_hash_warning)
4331 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4333 /* Fix the symbol flags. */
4334 eif.failed = FALSE;
4335 eif.info = info;
4336 if (! elf_fix_symbol_flags (h, &eif))
4338 if (eif.failed)
4339 sinfo->failed = TRUE;
4340 return FALSE;
4343 /* We only need version numbers for symbols defined in regular
4344 objects. */
4345 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4346 return TRUE;
4348 bed = get_elf_backend_data (sinfo->output_bfd);
4349 p = strchr (h->root.root.string, ELF_VER_CHR);
4350 if (p != NULL && h->verinfo.vertree == NULL)
4352 struct bfd_elf_version_tree *t;
4353 bfd_boolean hidden;
4355 hidden = TRUE;
4357 /* There are two consecutive ELF_VER_CHR characters if this is
4358 not a hidden symbol. */
4359 ++p;
4360 if (*p == ELF_VER_CHR)
4362 hidden = FALSE;
4363 ++p;
4366 /* If there is no version string, we can just return out. */
4367 if (*p == '\0')
4369 if (hidden)
4370 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4371 return TRUE;
4374 /* Look for the version. If we find it, it is no longer weak. */
4375 for (t = sinfo->verdefs; t != NULL; t = t->next)
4377 if (strcmp (t->name, p) == 0)
4379 size_t len;
4380 char *alc;
4381 struct bfd_elf_version_expr *d;
4383 len = p - h->root.root.string;
4384 alc = bfd_malloc ((bfd_size_type) len);
4385 if (alc == NULL)
4386 return FALSE;
4387 memcpy (alc, h->root.root.string, len - 1);
4388 alc[len - 1] = '\0';
4389 if (alc[len - 2] == ELF_VER_CHR)
4390 alc[len - 2] = '\0';
4392 h->verinfo.vertree = t;
4393 t->used = TRUE;
4394 d = NULL;
4396 if (t->globals != NULL)
4398 for (d = t->globals; d != NULL; d = d->next)
4399 if ((*d->match) (d, alc))
4400 break;
4403 /* See if there is anything to force this symbol to
4404 local scope. */
4405 if (d == NULL && t->locals != NULL)
4407 for (d = t->locals; d != NULL; d = d->next)
4409 if ((*d->match) (d, alc))
4411 if (h->dynindx != -1
4412 && info->shared
4413 && ! info->export_dynamic)
4415 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4418 break;
4423 free (alc);
4424 break;
4428 /* If we are building an application, we need to create a
4429 version node for this version. */
4430 if (t == NULL && ! info->shared)
4432 struct bfd_elf_version_tree **pp;
4433 int version_index;
4435 /* If we aren't going to export this symbol, we don't need
4436 to worry about it. */
4437 if (h->dynindx == -1)
4438 return TRUE;
4440 amt = sizeof *t;
4441 t = ((struct bfd_elf_version_tree *)
4442 bfd_alloc (sinfo->output_bfd, amt));
4443 if (t == NULL)
4445 sinfo->failed = TRUE;
4446 return FALSE;
4449 t->next = NULL;
4450 t->name = p;
4451 t->globals = NULL;
4452 t->locals = NULL;
4453 t->deps = NULL;
4454 t->name_indx = (unsigned int) -1;
4455 t->used = TRUE;
4457 version_index = 1;
4458 /* Don't count anonymous version tag. */
4459 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4460 version_index = 0;
4461 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4462 ++version_index;
4463 t->vernum = version_index;
4465 *pp = t;
4467 h->verinfo.vertree = t;
4469 else if (t == NULL)
4471 /* We could not find the version for a symbol when
4472 generating a shared archive. Return an error. */
4473 (*_bfd_error_handler)
4474 (_("%s: undefined versioned symbol name %s"),
4475 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4476 bfd_set_error (bfd_error_bad_value);
4477 sinfo->failed = TRUE;
4478 return FALSE;
4481 if (hidden)
4482 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4485 /* If we don't have a version for this symbol, see if we can find
4486 something. */
4487 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4489 struct bfd_elf_version_tree *t;
4490 struct bfd_elf_version_tree *local_ver;
4491 struct bfd_elf_version_expr *d;
4493 /* See if can find what version this symbol is in. If the
4494 symbol is supposed to be local, then don't actually register
4495 it. */
4496 local_ver = NULL;
4497 for (t = sinfo->verdefs; t != NULL; t = t->next)
4499 if (t->globals != NULL)
4501 bfd_boolean matched;
4503 matched = FALSE;
4504 for (d = t->globals; d != NULL; d = d->next)
4506 if ((*d->match) (d, h->root.root.string))
4508 if (d->symver)
4509 matched = TRUE;
4510 else
4512 /* There is a version without definition. Make
4513 the symbol the default definition for this
4514 version. */
4515 h->verinfo.vertree = t;
4516 local_ver = NULL;
4517 d->script = 1;
4518 break;
4523 if (d != NULL)
4524 break;
4525 else if (matched)
4526 /* There is no undefined version for this symbol. Hide the
4527 default one. */
4528 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4531 if (t->locals != NULL)
4533 for (d = t->locals; d != NULL; d = d->next)
4535 /* If the match is "*", keep looking for a more
4536 explicit, perhaps even global, match. */
4537 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4538 local_ver = t;
4539 else if ((*d->match) (d, h->root.root.string))
4541 local_ver = t;
4542 break;
4546 if (d != NULL)
4547 break;
4551 if (local_ver != NULL)
4553 h->verinfo.vertree = local_ver;
4554 if (h->dynindx != -1
4555 && info->shared
4556 && ! info->export_dynamic)
4558 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4563 return TRUE;
4566 /* Final phase of ELF linker. */
4568 /* A structure we use to avoid passing large numbers of arguments. */
4570 struct elf_final_link_info
4572 /* General link information. */
4573 struct bfd_link_info *info;
4574 /* Output BFD. */
4575 bfd *output_bfd;
4576 /* Symbol string table. */
4577 struct bfd_strtab_hash *symstrtab;
4578 /* .dynsym section. */
4579 asection *dynsym_sec;
4580 /* .hash section. */
4581 asection *hash_sec;
4582 /* symbol version section (.gnu.version). */
4583 asection *symver_sec;
4584 /* first SHF_TLS section (if any). */
4585 asection *first_tls_sec;
4586 /* Buffer large enough to hold contents of any section. */
4587 bfd_byte *contents;
4588 /* Buffer large enough to hold external relocs of any section. */
4589 PTR external_relocs;
4590 /* Buffer large enough to hold internal relocs of any section. */
4591 Elf_Internal_Rela *internal_relocs;
4592 /* Buffer large enough to hold external local symbols of any input
4593 BFD. */
4594 Elf_External_Sym *external_syms;
4595 /* And a buffer for symbol section indices. */
4596 Elf_External_Sym_Shndx *locsym_shndx;
4597 /* Buffer large enough to hold internal local symbols of any input
4598 BFD. */
4599 Elf_Internal_Sym *internal_syms;
4600 /* Array large enough to hold a symbol index for each local symbol
4601 of any input BFD. */
4602 long *indices;
4603 /* Array large enough to hold a section pointer for each local
4604 symbol of any input BFD. */
4605 asection **sections;
4606 /* Buffer to hold swapped out symbols. */
4607 Elf_External_Sym *symbuf;
4608 /* And one for symbol section indices. */
4609 Elf_External_Sym_Shndx *symshndxbuf;
4610 /* Number of swapped out symbols in buffer. */
4611 size_t symbuf_count;
4612 /* Number of symbols which fit in symbuf. */
4613 size_t symbuf_size;
4614 /* And same for symshndxbuf. */
4615 size_t shndxbuf_size;
4618 static bfd_boolean elf_link_output_sym
4619 PARAMS ((struct elf_final_link_info *, const char *,
4620 Elf_Internal_Sym *, asection *));
4621 static bfd_boolean elf_link_flush_output_syms
4622 PARAMS ((struct elf_final_link_info *));
4623 static bfd_boolean elf_link_output_extsym
4624 PARAMS ((struct elf_link_hash_entry *, PTR));
4625 static bfd_boolean elf_link_sec_merge_syms
4626 PARAMS ((struct elf_link_hash_entry *, PTR));
4627 static bfd_boolean elf_link_check_versioned_symbol
4628 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
4629 static bfd_boolean elf_link_input_bfd
4630 PARAMS ((struct elf_final_link_info *, bfd *));
4631 static bfd_boolean elf_reloc_link_order
4632 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4633 struct bfd_link_order *));
4635 /* This struct is used to pass information to elf_link_output_extsym. */
4637 struct elf_outext_info
4639 bfd_boolean failed;
4640 bfd_boolean localsyms;
4641 struct elf_final_link_info *finfo;
4644 /* Compute the size of, and allocate space for, REL_HDR which is the
4645 section header for a section containing relocations for O. */
4647 static bfd_boolean
4648 elf_link_size_reloc_section (abfd, rel_hdr, o)
4649 bfd *abfd;
4650 Elf_Internal_Shdr *rel_hdr;
4651 asection *o;
4653 bfd_size_type reloc_count;
4654 bfd_size_type num_rel_hashes;
4656 /* Figure out how many relocations there will be. */
4657 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4658 reloc_count = elf_section_data (o)->rel_count;
4659 else
4660 reloc_count = elf_section_data (o)->rel_count2;
4662 num_rel_hashes = o->reloc_count;
4663 if (num_rel_hashes < reloc_count)
4664 num_rel_hashes = reloc_count;
4666 /* That allows us to calculate the size of the section. */
4667 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4669 /* The contents field must last into write_object_contents, so we
4670 allocate it with bfd_alloc rather than malloc. Also since we
4671 cannot be sure that the contents will actually be filled in,
4672 we zero the allocated space. */
4673 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4674 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4675 return FALSE;
4677 /* We only allocate one set of hash entries, so we only do it the
4678 first time we are called. */
4679 if (elf_section_data (o)->rel_hashes == NULL
4680 && num_rel_hashes)
4682 struct elf_link_hash_entry **p;
4684 p = ((struct elf_link_hash_entry **)
4685 bfd_zmalloc (num_rel_hashes
4686 * sizeof (struct elf_link_hash_entry *)));
4687 if (p == NULL)
4688 return FALSE;
4690 elf_section_data (o)->rel_hashes = p;
4693 return TRUE;
4696 /* When performing a relocateable link, the input relocations are
4697 preserved. But, if they reference global symbols, the indices
4698 referenced must be updated. Update all the relocations in
4699 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4701 static void
4702 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4703 bfd *abfd;
4704 Elf_Internal_Shdr *rel_hdr;
4705 unsigned int count;
4706 struct elf_link_hash_entry **rel_hash;
4708 unsigned int i;
4709 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4710 bfd_byte *erela;
4711 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
4712 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
4714 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4716 swap_in = bed->s->swap_reloc_in;
4717 swap_out = bed->s->swap_reloc_out;
4719 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
4721 swap_in = bed->s->swap_reloca_in;
4722 swap_out = bed->s->swap_reloca_out;
4724 else
4725 abort ();
4727 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
4728 abort ();
4730 erela = rel_hdr->contents;
4731 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
4733 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
4734 unsigned int j;
4736 if (*rel_hash == NULL)
4737 continue;
4739 BFD_ASSERT ((*rel_hash)->indx >= 0);
4741 (*swap_in) (abfd, erela, irela);
4742 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4743 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4744 ELF_R_TYPE (irela[j].r_info));
4745 (*swap_out) (abfd, irela, erela);
4749 struct elf_link_sort_rela
4751 bfd_vma offset;
4752 enum elf_reloc_type_class type;
4753 /* We use this as an array of size int_rels_per_ext_rel. */
4754 Elf_Internal_Rela rela[1];
4757 static int
4758 elf_link_sort_cmp1 (A, B)
4759 const PTR A;
4760 const PTR B;
4762 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4763 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4764 int relativea, relativeb;
4766 relativea = a->type == reloc_class_relative;
4767 relativeb = b->type == reloc_class_relative;
4769 if (relativea < relativeb)
4770 return 1;
4771 if (relativea > relativeb)
4772 return -1;
4773 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
4774 return -1;
4775 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
4776 return 1;
4777 if (a->rela->r_offset < b->rela->r_offset)
4778 return -1;
4779 if (a->rela->r_offset > b->rela->r_offset)
4780 return 1;
4781 return 0;
4784 static int
4785 elf_link_sort_cmp2 (A, B)
4786 const PTR A;
4787 const PTR B;
4789 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4790 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4791 int copya, copyb;
4793 if (a->offset < b->offset)
4794 return -1;
4795 if (a->offset > b->offset)
4796 return 1;
4797 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4798 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4799 if (copya < copyb)
4800 return -1;
4801 if (copya > copyb)
4802 return 1;
4803 if (a->rela->r_offset < b->rela->r_offset)
4804 return -1;
4805 if (a->rela->r_offset > b->rela->r_offset)
4806 return 1;
4807 return 0;
4810 static size_t
4811 elf_link_sort_relocs (abfd, info, psec)
4812 bfd *abfd;
4813 struct bfd_link_info *info;
4814 asection **psec;
4816 bfd *dynobj = elf_hash_table (info)->dynobj;
4817 asection *reldyn, *o;
4818 bfd_size_type count, size;
4819 size_t i, ret, sort_elt, ext_size;
4820 bfd_byte *sort, *s_non_relative, *p;
4821 struct elf_link_sort_rela *sq;
4822 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4823 int i2e = bed->s->int_rels_per_ext_rel;
4824 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
4825 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
4827 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4828 if (reldyn == NULL || reldyn->_raw_size == 0)
4830 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4831 if (reldyn == NULL || reldyn->_raw_size == 0)
4832 return 0;
4833 ext_size = sizeof (Elf_External_Rel);
4834 swap_in = bed->s->swap_reloc_in;
4835 swap_out = bed->s->swap_reloc_out;
4837 else
4839 ext_size = sizeof (Elf_External_Rela);
4840 swap_in = bed->s->swap_reloca_in;
4841 swap_out = bed->s->swap_reloca_out;
4843 count = reldyn->_raw_size / ext_size;
4845 size = 0;
4846 for (o = dynobj->sections; o != NULL; o = o->next)
4847 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4848 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4849 && o->output_section == reldyn)
4850 size += o->_raw_size;
4852 if (size != reldyn->_raw_size)
4853 return 0;
4855 sort_elt = (sizeof (struct elf_link_sort_rela)
4856 + (i2e - 1) * sizeof (Elf_Internal_Rela));
4857 sort = bfd_zmalloc (sort_elt * count);
4858 if (sort == NULL)
4860 (*info->callbacks->warning)
4861 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4862 (bfd_vma) 0);
4863 return 0;
4866 for (o = dynobj->sections; o != NULL; o = o->next)
4867 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4868 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4869 && o->output_section == reldyn)
4871 bfd_byte *erel, *erelend;
4873 erel = o->contents;
4874 erelend = o->contents + o->_raw_size;
4875 p = sort + o->output_offset / ext_size * sort_elt;
4876 while (erel < erelend)
4878 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4879 (*swap_in) (abfd, erel, s->rela);
4880 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
4881 p += sort_elt;
4882 erel += ext_size;
4886 qsort (sort, (size_t) count, sort_elt, elf_link_sort_cmp1);
4888 for (i = 0, p = sort; i < count; i++, p += sort_elt)
4890 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4891 if (s->type != reloc_class_relative)
4892 break;
4894 ret = i;
4895 s_non_relative = p;
4897 sq = (struct elf_link_sort_rela *) s_non_relative;
4898 for (; i < count; i++, p += sort_elt)
4900 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
4901 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
4902 sq = sp;
4903 sp->offset = sq->rela->r_offset;
4906 qsort (s_non_relative, (size_t) count - ret, sort_elt, elf_link_sort_cmp2);
4908 for (o = dynobj->sections; o != NULL; o = o->next)
4909 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4910 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4911 && o->output_section == reldyn)
4913 bfd_byte *erel, *erelend;
4915 erel = o->contents;
4916 erelend = o->contents + o->_raw_size;
4917 p = sort + o->output_offset / ext_size * sort_elt;
4918 while (erel < erelend)
4920 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4921 (*swap_out) (abfd, s->rela, erel);
4922 p += sort_elt;
4923 erel += ext_size;
4927 free (sort);
4928 *psec = reldyn;
4929 return ret;
4932 /* Do the final step of an ELF link. */
4934 bfd_boolean
4935 elf_bfd_final_link (abfd, info)
4936 bfd *abfd;
4937 struct bfd_link_info *info;
4939 bfd_boolean dynamic;
4940 bfd_boolean emit_relocs;
4941 bfd *dynobj;
4942 struct elf_final_link_info finfo;
4943 register asection *o;
4944 register struct bfd_link_order *p;
4945 register bfd *sub;
4946 bfd_size_type max_contents_size;
4947 bfd_size_type max_external_reloc_size;
4948 bfd_size_type max_internal_reloc_count;
4949 bfd_size_type max_sym_count;
4950 bfd_size_type max_sym_shndx_count;
4951 file_ptr off;
4952 Elf_Internal_Sym elfsym;
4953 unsigned int i;
4954 Elf_Internal_Shdr *symtab_hdr;
4955 Elf_Internal_Shdr *symtab_shndx_hdr;
4956 Elf_Internal_Shdr *symstrtab_hdr;
4957 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4958 struct elf_outext_info eoinfo;
4959 bfd_boolean merged;
4960 size_t relativecount = 0;
4961 asection *reldyn = 0;
4962 bfd_size_type amt;
4964 if (! is_elf_hash_table (info))
4965 return FALSE;
4967 if (info->shared)
4968 abfd->flags |= DYNAMIC;
4970 dynamic = elf_hash_table (info)->dynamic_sections_created;
4971 dynobj = elf_hash_table (info)->dynobj;
4973 emit_relocs = (info->relocateable
4974 || info->emitrelocations
4975 || bed->elf_backend_emit_relocs);
4977 finfo.info = info;
4978 finfo.output_bfd = abfd;
4979 finfo.symstrtab = elf_stringtab_init ();
4980 if (finfo.symstrtab == NULL)
4981 return FALSE;
4983 if (! dynamic)
4985 finfo.dynsym_sec = NULL;
4986 finfo.hash_sec = NULL;
4987 finfo.symver_sec = NULL;
4989 else
4991 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4992 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4993 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4994 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4995 /* Note that it is OK if symver_sec is NULL. */
4998 finfo.contents = NULL;
4999 finfo.external_relocs = NULL;
5000 finfo.internal_relocs = NULL;
5001 finfo.external_syms = NULL;
5002 finfo.locsym_shndx = NULL;
5003 finfo.internal_syms = NULL;
5004 finfo.indices = NULL;
5005 finfo.sections = NULL;
5006 finfo.symbuf = NULL;
5007 finfo.symshndxbuf = NULL;
5008 finfo.symbuf_count = 0;
5009 finfo.shndxbuf_size = 0;
5010 finfo.first_tls_sec = NULL;
5011 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5012 if ((o->flags & SEC_THREAD_LOCAL) != 0
5013 && (o->flags & SEC_LOAD) != 0)
5015 finfo.first_tls_sec = o;
5016 break;
5019 /* Count up the number of relocations we will output for each output
5020 section, so that we know the sizes of the reloc sections. We
5021 also figure out some maximum sizes. */
5022 max_contents_size = 0;
5023 max_external_reloc_size = 0;
5024 max_internal_reloc_count = 0;
5025 max_sym_count = 0;
5026 max_sym_shndx_count = 0;
5027 merged = FALSE;
5028 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5030 struct bfd_elf_section_data *esdo = elf_section_data (o);
5031 o->reloc_count = 0;
5033 for (p = o->link_order_head; p != NULL; p = p->next)
5035 unsigned int reloc_count = 0;
5036 struct bfd_elf_section_data *esdi = NULL;
5037 unsigned int *rel_count1;
5039 if (p->type == bfd_section_reloc_link_order
5040 || p->type == bfd_symbol_reloc_link_order)
5041 reloc_count = 1;
5042 else if (p->type == bfd_indirect_link_order)
5044 asection *sec;
5046 sec = p->u.indirect.section;
5047 esdi = elf_section_data (sec);
5049 /* Mark all sections which are to be included in the
5050 link. This will normally be every section. We need
5051 to do this so that we can identify any sections which
5052 the linker has decided to not include. */
5053 sec->linker_mark = TRUE;
5055 if (sec->flags & SEC_MERGE)
5056 merged = TRUE;
5058 if (info->relocateable || info->emitrelocations)
5059 reloc_count = sec->reloc_count;
5060 else if (bed->elf_backend_count_relocs)
5062 Elf_Internal_Rela * relocs;
5064 relocs = (NAME(_bfd_elf,link_read_relocs)
5065 (abfd, sec, (PTR) NULL,
5066 (Elf_Internal_Rela *) NULL, info->keep_memory));
5068 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
5070 if (elf_section_data (o)->relocs != relocs)
5071 free (relocs);
5074 if (sec->_raw_size > max_contents_size)
5075 max_contents_size = sec->_raw_size;
5076 if (sec->_cooked_size > max_contents_size)
5077 max_contents_size = sec->_cooked_size;
5079 /* We are interested in just local symbols, not all
5080 symbols. */
5081 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5082 && (sec->owner->flags & DYNAMIC) == 0)
5084 size_t sym_count;
5086 if (elf_bad_symtab (sec->owner))
5087 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5088 / sizeof (Elf_External_Sym));
5089 else
5090 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5092 if (sym_count > max_sym_count)
5093 max_sym_count = sym_count;
5095 if (sym_count > max_sym_shndx_count
5096 && elf_symtab_shndx (sec->owner) != 0)
5097 max_sym_shndx_count = sym_count;
5099 if ((sec->flags & SEC_RELOC) != 0)
5101 size_t ext_size;
5103 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5104 if (ext_size > max_external_reloc_size)
5105 max_external_reloc_size = ext_size;
5106 if (sec->reloc_count > max_internal_reloc_count)
5107 max_internal_reloc_count = sec->reloc_count;
5112 if (reloc_count == 0)
5113 continue;
5115 o->reloc_count += reloc_count;
5117 /* MIPS may have a mix of REL and RELA relocs on sections.
5118 To support this curious ABI we keep reloc counts in
5119 elf_section_data too. We must be careful to add the
5120 relocations from the input section to the right output
5121 count. FIXME: Get rid of one count. We have
5122 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5123 rel_count1 = &esdo->rel_count;
5124 if (esdi != NULL)
5126 bfd_boolean same_size;
5127 bfd_size_type entsize1;
5129 entsize1 = esdi->rel_hdr.sh_entsize;
5130 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
5131 || entsize1 == sizeof (Elf_External_Rela));
5132 same_size = (!o->use_rela_p
5133 == (entsize1 == sizeof (Elf_External_Rel)));
5135 if (!same_size)
5136 rel_count1 = &esdo->rel_count2;
5138 if (esdi->rel_hdr2 != NULL)
5140 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
5141 unsigned int alt_count;
5142 unsigned int *rel_count2;
5144 BFD_ASSERT (entsize2 != entsize1
5145 && (entsize2 == sizeof (Elf_External_Rel)
5146 || entsize2 == sizeof (Elf_External_Rela)));
5148 rel_count2 = &esdo->rel_count2;
5149 if (!same_size)
5150 rel_count2 = &esdo->rel_count;
5152 /* The following is probably too simplistic if the
5153 backend counts output relocs unusually. */
5154 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
5155 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5156 *rel_count2 += alt_count;
5157 reloc_count -= alt_count;
5160 *rel_count1 += reloc_count;
5163 if (o->reloc_count > 0)
5164 o->flags |= SEC_RELOC;
5165 else
5167 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5168 set it (this is probably a bug) and if it is set
5169 assign_section_numbers will create a reloc section. */
5170 o->flags &=~ SEC_RELOC;
5173 /* If the SEC_ALLOC flag is not set, force the section VMA to
5174 zero. This is done in elf_fake_sections as well, but forcing
5175 the VMA to 0 here will ensure that relocs against these
5176 sections are handled correctly. */
5177 if ((o->flags & SEC_ALLOC) == 0
5178 && ! o->user_set_vma)
5179 o->vma = 0;
5182 if (! info->relocateable && merged)
5183 elf_link_hash_traverse (elf_hash_table (info),
5184 elf_link_sec_merge_syms, (PTR) abfd);
5186 /* Figure out the file positions for everything but the symbol table
5187 and the relocs. We set symcount to force assign_section_numbers
5188 to create a symbol table. */
5189 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5190 BFD_ASSERT (! abfd->output_has_begun);
5191 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5192 goto error_return;
5194 /* That created the reloc sections. Set their sizes, and assign
5195 them file positions, and allocate some buffers. */
5196 for (o = abfd->sections; o != NULL; o = o->next)
5198 if ((o->flags & SEC_RELOC) != 0)
5200 if (!elf_link_size_reloc_section (abfd,
5201 &elf_section_data (o)->rel_hdr,
5203 goto error_return;
5205 if (elf_section_data (o)->rel_hdr2
5206 && !elf_link_size_reloc_section (abfd,
5207 elf_section_data (o)->rel_hdr2,
5209 goto error_return;
5212 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5213 to count upwards while actually outputting the relocations. */
5214 elf_section_data (o)->rel_count = 0;
5215 elf_section_data (o)->rel_count2 = 0;
5218 _bfd_elf_assign_file_positions_for_relocs (abfd);
5220 /* We have now assigned file positions for all the sections except
5221 .symtab and .strtab. We start the .symtab section at the current
5222 file position, and write directly to it. We build the .strtab
5223 section in memory. */
5224 bfd_get_symcount (abfd) = 0;
5225 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5226 /* sh_name is set in prep_headers. */
5227 symtab_hdr->sh_type = SHT_SYMTAB;
5228 /* sh_flags, sh_addr and sh_size all start off zero. */
5229 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5230 /* sh_link is set in assign_section_numbers. */
5231 /* sh_info is set below. */
5232 /* sh_offset is set just below. */
5233 symtab_hdr->sh_addralign = bed->s->file_align;
5235 off = elf_tdata (abfd)->next_file_pos;
5236 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
5238 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5239 incorrect. We do not yet know the size of the .symtab section.
5240 We correct next_file_pos below, after we do know the size. */
5242 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5243 continuously seeking to the right position in the file. */
5244 if (! info->keep_memory || max_sym_count < 20)
5245 finfo.symbuf_size = 20;
5246 else
5247 finfo.symbuf_size = max_sym_count;
5248 amt = finfo.symbuf_size;
5249 amt *= sizeof (Elf_External_Sym);
5250 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5251 if (finfo.symbuf == NULL)
5252 goto error_return;
5253 if (elf_numsections (abfd) > SHN_LORESERVE)
5255 /* Wild guess at number of output symbols. realloc'd as needed. */
5256 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
5257 finfo.shndxbuf_size = amt;
5258 amt *= sizeof (Elf_External_Sym_Shndx);
5259 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
5260 if (finfo.symshndxbuf == NULL)
5261 goto error_return;
5264 /* Start writing out the symbol table. The first symbol is always a
5265 dummy symbol. */
5266 if (info->strip != strip_all
5267 || emit_relocs)
5269 elfsym.st_value = 0;
5270 elfsym.st_size = 0;
5271 elfsym.st_info = 0;
5272 elfsym.st_other = 0;
5273 elfsym.st_shndx = SHN_UNDEF;
5274 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5275 &elfsym, bfd_und_section_ptr))
5276 goto error_return;
5279 #if 0
5280 /* Some standard ELF linkers do this, but we don't because it causes
5281 bootstrap comparison failures. */
5282 /* Output a file symbol for the output file as the second symbol.
5283 We output this even if we are discarding local symbols, although
5284 I'm not sure if this is correct. */
5285 elfsym.st_value = 0;
5286 elfsym.st_size = 0;
5287 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5288 elfsym.st_other = 0;
5289 elfsym.st_shndx = SHN_ABS;
5290 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5291 &elfsym, bfd_abs_section_ptr))
5292 goto error_return;
5293 #endif
5295 /* Output a symbol for each section. We output these even if we are
5296 discarding local symbols, since they are used for relocs. These
5297 symbols have no names. We store the index of each one in the
5298 index field of the section, so that we can find it again when
5299 outputting relocs. */
5300 if (info->strip != strip_all
5301 || emit_relocs)
5303 elfsym.st_size = 0;
5304 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5305 elfsym.st_other = 0;
5306 for (i = 1; i < elf_numsections (abfd); i++)
5308 o = section_from_elf_index (abfd, i);
5309 if (o != NULL)
5310 o->target_index = bfd_get_symcount (abfd);
5311 elfsym.st_shndx = i;
5312 if (info->relocateable || o == NULL)
5313 elfsym.st_value = 0;
5314 else
5315 elfsym.st_value = o->vma;
5316 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5317 &elfsym, o))
5318 goto error_return;
5319 if (i == SHN_LORESERVE - 1)
5320 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5324 /* Allocate some memory to hold information read in from the input
5325 files. */
5326 if (max_contents_size != 0)
5328 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5329 if (finfo.contents == NULL)
5330 goto error_return;
5333 if (max_external_reloc_size != 0)
5335 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5336 if (finfo.external_relocs == NULL)
5337 goto error_return;
5340 if (max_internal_reloc_count != 0)
5342 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5343 amt *= sizeof (Elf_Internal_Rela);
5344 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5345 if (finfo.internal_relocs == NULL)
5346 goto error_return;
5349 if (max_sym_count != 0)
5351 amt = max_sym_count * sizeof (Elf_External_Sym);
5352 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5353 if (finfo.external_syms == NULL)
5354 goto error_return;
5356 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5357 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5358 if (finfo.internal_syms == NULL)
5359 goto error_return;
5361 amt = max_sym_count * sizeof (long);
5362 finfo.indices = (long *) bfd_malloc (amt);
5363 if (finfo.indices == NULL)
5364 goto error_return;
5366 amt = max_sym_count * sizeof (asection *);
5367 finfo.sections = (asection **) bfd_malloc (amt);
5368 if (finfo.sections == NULL)
5369 goto error_return;
5372 if (max_sym_shndx_count != 0)
5374 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5375 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5376 if (finfo.locsym_shndx == NULL)
5377 goto error_return;
5380 if (finfo.first_tls_sec)
5382 unsigned int align = 0;
5383 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5384 asection *sec;
5386 for (sec = finfo.first_tls_sec;
5387 sec && (sec->flags & SEC_THREAD_LOCAL);
5388 sec = sec->next)
5390 bfd_vma size = sec->_raw_size;
5392 if (bfd_get_section_alignment (abfd, sec) > align)
5393 align = bfd_get_section_alignment (abfd, sec);
5394 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5396 struct bfd_link_order *o;
5398 size = 0;
5399 for (o = sec->link_order_head; o != NULL; o = o->next)
5400 if (size < o->offset + o->size)
5401 size = o->offset + o->size;
5403 end = sec->vma + size;
5405 elf_hash_table (info)->tls_segment
5406 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5407 if (elf_hash_table (info)->tls_segment == NULL)
5408 goto error_return;
5409 elf_hash_table (info)->tls_segment->start = base;
5410 elf_hash_table (info)->tls_segment->size = end - base;
5411 elf_hash_table (info)->tls_segment->align = align;
5414 /* Since ELF permits relocations to be against local symbols, we
5415 must have the local symbols available when we do the relocations.
5416 Since we would rather only read the local symbols once, and we
5417 would rather not keep them in memory, we handle all the
5418 relocations for a single input file at the same time.
5420 Unfortunately, there is no way to know the total number of local
5421 symbols until we have seen all of them, and the local symbol
5422 indices precede the global symbol indices. This means that when
5423 we are generating relocateable output, and we see a reloc against
5424 a global symbol, we can not know the symbol index until we have
5425 finished examining all the local symbols to see which ones we are
5426 going to output. To deal with this, we keep the relocations in
5427 memory, and don't output them until the end of the link. This is
5428 an unfortunate waste of memory, but I don't see a good way around
5429 it. Fortunately, it only happens when performing a relocateable
5430 link, which is not the common case. FIXME: If keep_memory is set
5431 we could write the relocs out and then read them again; I don't
5432 know how bad the memory loss will be. */
5434 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5435 sub->output_has_begun = FALSE;
5436 for (o = abfd->sections; o != NULL; o = o->next)
5438 for (p = o->link_order_head; p != NULL; p = p->next)
5440 if (p->type == bfd_indirect_link_order
5441 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
5442 == bfd_target_elf_flavour)
5443 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
5445 if (! sub->output_has_begun)
5447 if (! elf_link_input_bfd (&finfo, sub))
5448 goto error_return;
5449 sub->output_has_begun = TRUE;
5452 else if (p->type == bfd_section_reloc_link_order
5453 || p->type == bfd_symbol_reloc_link_order)
5455 if (! elf_reloc_link_order (abfd, info, o, p))
5456 goto error_return;
5458 else
5460 if (! _bfd_default_link_order (abfd, info, o, p))
5461 goto error_return;
5466 /* Output any global symbols that got converted to local in a
5467 version script or due to symbol visibility. We do this in a
5468 separate step since ELF requires all local symbols to appear
5469 prior to any global symbols. FIXME: We should only do this if
5470 some global symbols were, in fact, converted to become local.
5471 FIXME: Will this work correctly with the Irix 5 linker? */
5472 eoinfo.failed = FALSE;
5473 eoinfo.finfo = &finfo;
5474 eoinfo.localsyms = TRUE;
5475 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5476 (PTR) &eoinfo);
5477 if (eoinfo.failed)
5478 return FALSE;
5480 /* That wrote out all the local symbols. Finish up the symbol table
5481 with the global symbols. Even if we want to strip everything we
5482 can, we still need to deal with those global symbols that got
5483 converted to local in a version script. */
5485 /* The sh_info field records the index of the first non local symbol. */
5486 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5488 if (dynamic
5489 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5491 Elf_Internal_Sym sym;
5492 Elf_External_Sym *dynsym =
5493 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5494 long last_local = 0;
5496 /* Write out the section symbols for the output sections. */
5497 if (info->shared)
5499 asection *s;
5501 sym.st_size = 0;
5502 sym.st_name = 0;
5503 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5504 sym.st_other = 0;
5506 for (s = abfd->sections; s != NULL; s = s->next)
5508 int indx;
5509 Elf_External_Sym *dest;
5511 indx = elf_section_data (s)->this_idx;
5512 BFD_ASSERT (indx > 0);
5513 sym.st_shndx = indx;
5514 sym.st_value = s->vma;
5515 dest = dynsym + elf_section_data (s)->dynindx;
5516 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5519 last_local = bfd_count_sections (abfd);
5522 /* Write out the local dynsyms. */
5523 if (elf_hash_table (info)->dynlocal)
5525 struct elf_link_local_dynamic_entry *e;
5526 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5528 asection *s;
5529 Elf_External_Sym *dest;
5531 sym.st_size = e->isym.st_size;
5532 sym.st_other = e->isym.st_other;
5534 /* Copy the internal symbol as is.
5535 Note that we saved a word of storage and overwrote
5536 the original st_name with the dynstr_index. */
5537 sym = e->isym;
5539 if (e->isym.st_shndx != SHN_UNDEF
5540 && (e->isym.st_shndx < SHN_LORESERVE
5541 || e->isym.st_shndx > SHN_HIRESERVE))
5543 s = bfd_section_from_elf_index (e->input_bfd,
5544 e->isym.st_shndx);
5546 sym.st_shndx =
5547 elf_section_data (s->output_section)->this_idx;
5548 sym.st_value = (s->output_section->vma
5549 + s->output_offset
5550 + e->isym.st_value);
5553 if (last_local < e->dynindx)
5554 last_local = e->dynindx;
5556 dest = dynsym + e->dynindx;
5557 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5561 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5562 last_local + 1;
5565 /* We get the global symbols from the hash table. */
5566 eoinfo.failed = FALSE;
5567 eoinfo.localsyms = FALSE;
5568 eoinfo.finfo = &finfo;
5569 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5570 (PTR) &eoinfo);
5571 if (eoinfo.failed)
5572 return FALSE;
5574 /* If backend needs to output some symbols not present in the hash
5575 table, do it now. */
5576 if (bed->elf_backend_output_arch_syms)
5578 typedef bfd_boolean (*out_sym_func)
5579 PARAMS ((PTR, const char *, Elf_Internal_Sym *, asection *));
5581 if (! ((*bed->elf_backend_output_arch_syms)
5582 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5583 return FALSE;
5586 /* Flush all symbols to the file. */
5587 if (! elf_link_flush_output_syms (&finfo))
5588 return FALSE;
5590 /* Now we know the size of the symtab section. */
5591 off += symtab_hdr->sh_size;
5593 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5594 if (symtab_shndx_hdr->sh_name != 0)
5596 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5597 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5598 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5599 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
5600 symtab_shndx_hdr->sh_size = amt;
5602 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
5603 off, TRUE);
5605 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
5606 || (bfd_bwrite ((PTR) finfo.symshndxbuf, amt, abfd) != amt))
5607 return FALSE;
5611 /* Finish up and write out the symbol string table (.strtab)
5612 section. */
5613 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5614 /* sh_name was set in prep_headers. */
5615 symstrtab_hdr->sh_type = SHT_STRTAB;
5616 symstrtab_hdr->sh_flags = 0;
5617 symstrtab_hdr->sh_addr = 0;
5618 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5619 symstrtab_hdr->sh_entsize = 0;
5620 symstrtab_hdr->sh_link = 0;
5621 symstrtab_hdr->sh_info = 0;
5622 /* sh_offset is set just below. */
5623 symstrtab_hdr->sh_addralign = 1;
5625 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
5626 elf_tdata (abfd)->next_file_pos = off;
5628 if (bfd_get_symcount (abfd) > 0)
5630 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5631 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5632 return FALSE;
5635 /* Adjust the relocs to have the correct symbol indices. */
5636 for (o = abfd->sections; o != NULL; o = o->next)
5638 if ((o->flags & SEC_RELOC) == 0)
5639 continue;
5641 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5642 elf_section_data (o)->rel_count,
5643 elf_section_data (o)->rel_hashes);
5644 if (elf_section_data (o)->rel_hdr2 != NULL)
5645 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5646 elf_section_data (o)->rel_count2,
5647 (elf_section_data (o)->rel_hashes
5648 + elf_section_data (o)->rel_count));
5650 /* Set the reloc_count field to 0 to prevent write_relocs from
5651 trying to swap the relocs out itself. */
5652 o->reloc_count = 0;
5655 if (dynamic && info->combreloc && dynobj != NULL)
5656 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5658 /* If we are linking against a dynamic object, or generating a
5659 shared library, finish up the dynamic linking information. */
5660 if (dynamic)
5662 Elf_External_Dyn *dyncon, *dynconend;
5664 /* Fix up .dynamic entries. */
5665 o = bfd_get_section_by_name (dynobj, ".dynamic");
5666 BFD_ASSERT (o != NULL);
5668 dyncon = (Elf_External_Dyn *) o->contents;
5669 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5670 for (; dyncon < dynconend; dyncon++)
5672 Elf_Internal_Dyn dyn;
5673 const char *name;
5674 unsigned int type;
5676 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5678 switch (dyn.d_tag)
5680 default:
5681 break;
5682 case DT_NULL:
5683 if (relativecount > 0 && dyncon + 1 < dynconend)
5685 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5687 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5688 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5689 default: break;
5691 if (dyn.d_tag != DT_NULL)
5693 dyn.d_un.d_val = relativecount;
5694 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5695 relativecount = 0;
5698 break;
5699 case DT_INIT:
5700 name = info->init_function;
5701 goto get_sym;
5702 case DT_FINI:
5703 name = info->fini_function;
5704 get_sym:
5706 struct elf_link_hash_entry *h;
5708 h = elf_link_hash_lookup (elf_hash_table (info), name,
5709 FALSE, FALSE, TRUE);
5710 if (h != NULL
5711 && (h->root.type == bfd_link_hash_defined
5712 || h->root.type == bfd_link_hash_defweak))
5714 dyn.d_un.d_val = h->root.u.def.value;
5715 o = h->root.u.def.section;
5716 if (o->output_section != NULL)
5717 dyn.d_un.d_val += (o->output_section->vma
5718 + o->output_offset);
5719 else
5721 /* The symbol is imported from another shared
5722 library and does not apply to this one. */
5723 dyn.d_un.d_val = 0;
5726 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5729 break;
5731 case DT_PREINIT_ARRAYSZ:
5732 name = ".preinit_array";
5733 goto get_size;
5734 case DT_INIT_ARRAYSZ:
5735 name = ".init_array";
5736 goto get_size;
5737 case DT_FINI_ARRAYSZ:
5738 name = ".fini_array";
5739 get_size:
5740 o = bfd_get_section_by_name (abfd, name);
5741 if (o == NULL)
5743 (*_bfd_error_handler)
5744 (_("%s: could not find output section %s"),
5745 bfd_get_filename (abfd), name);
5746 goto error_return;
5748 if (o->_raw_size == 0)
5749 (*_bfd_error_handler)
5750 (_("warning: %s section has zero size"), name);
5751 dyn.d_un.d_val = o->_raw_size;
5752 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5753 break;
5755 case DT_PREINIT_ARRAY:
5756 name = ".preinit_array";
5757 goto get_vma;
5758 case DT_INIT_ARRAY:
5759 name = ".init_array";
5760 goto get_vma;
5761 case DT_FINI_ARRAY:
5762 name = ".fini_array";
5763 goto get_vma;
5765 case DT_HASH:
5766 name = ".hash";
5767 goto get_vma;
5768 case DT_STRTAB:
5769 name = ".dynstr";
5770 goto get_vma;
5771 case DT_SYMTAB:
5772 name = ".dynsym";
5773 goto get_vma;
5774 case DT_VERDEF:
5775 name = ".gnu.version_d";
5776 goto get_vma;
5777 case DT_VERNEED:
5778 name = ".gnu.version_r";
5779 goto get_vma;
5780 case DT_VERSYM:
5781 name = ".gnu.version";
5782 get_vma:
5783 o = bfd_get_section_by_name (abfd, name);
5784 if (o == NULL)
5786 (*_bfd_error_handler)
5787 (_("%s: could not find output section %s"),
5788 bfd_get_filename (abfd), name);
5789 goto error_return;
5791 dyn.d_un.d_ptr = o->vma;
5792 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5793 break;
5795 case DT_REL:
5796 case DT_RELA:
5797 case DT_RELSZ:
5798 case DT_RELASZ:
5799 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5800 type = SHT_REL;
5801 else
5802 type = SHT_RELA;
5803 dyn.d_un.d_val = 0;
5804 for (i = 1; i < elf_numsections (abfd); i++)
5806 Elf_Internal_Shdr *hdr;
5808 hdr = elf_elfsections (abfd)[i];
5809 if (hdr->sh_type == type
5810 && (hdr->sh_flags & SHF_ALLOC) != 0)
5812 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5813 dyn.d_un.d_val += hdr->sh_size;
5814 else
5816 if (dyn.d_un.d_val == 0
5817 || hdr->sh_addr < dyn.d_un.d_val)
5818 dyn.d_un.d_val = hdr->sh_addr;
5822 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5823 break;
5828 /* If we have created any dynamic sections, then output them. */
5829 if (dynobj != NULL)
5831 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5832 goto error_return;
5834 for (o = dynobj->sections; o != NULL; o = o->next)
5836 if ((o->flags & SEC_HAS_CONTENTS) == 0
5837 || o->_raw_size == 0
5838 || o->output_section == bfd_abs_section_ptr)
5839 continue;
5840 if ((o->flags & SEC_LINKER_CREATED) == 0)
5842 /* At this point, we are only interested in sections
5843 created by elf_link_create_dynamic_sections. */
5844 continue;
5846 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5847 != SHT_STRTAB)
5848 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5850 if (! bfd_set_section_contents (abfd, o->output_section,
5851 o->contents,
5852 (file_ptr) o->output_offset,
5853 o->_raw_size))
5854 goto error_return;
5856 else
5858 /* The contents of the .dynstr section are actually in a
5859 stringtab. */
5860 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5861 if (bfd_seek (abfd, off, SEEK_SET) != 0
5862 || ! _bfd_elf_strtab_emit (abfd,
5863 elf_hash_table (info)->dynstr))
5864 goto error_return;
5869 if (info->relocateable)
5871 bfd_boolean failed = FALSE;
5873 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5874 if (failed)
5875 goto error_return;
5878 /* If we have optimized stabs strings, output them. */
5879 if (elf_hash_table (info)->stab_info != NULL)
5881 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5882 goto error_return;
5885 if (info->eh_frame_hdr)
5887 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
5888 goto error_return;
5891 if (finfo.symstrtab != NULL)
5892 _bfd_stringtab_free (finfo.symstrtab);
5893 if (finfo.contents != NULL)
5894 free (finfo.contents);
5895 if (finfo.external_relocs != NULL)
5896 free (finfo.external_relocs);
5897 if (finfo.internal_relocs != NULL)
5898 free (finfo.internal_relocs);
5899 if (finfo.external_syms != NULL)
5900 free (finfo.external_syms);
5901 if (finfo.locsym_shndx != NULL)
5902 free (finfo.locsym_shndx);
5903 if (finfo.internal_syms != NULL)
5904 free (finfo.internal_syms);
5905 if (finfo.indices != NULL)
5906 free (finfo.indices);
5907 if (finfo.sections != NULL)
5908 free (finfo.sections);
5909 if (finfo.symbuf != NULL)
5910 free (finfo.symbuf);
5911 if (finfo.symshndxbuf != NULL)
5912 free (finfo.symshndxbuf);
5913 for (o = abfd->sections; o != NULL; o = o->next)
5915 if ((o->flags & SEC_RELOC) != 0
5916 && elf_section_data (o)->rel_hashes != NULL)
5917 free (elf_section_data (o)->rel_hashes);
5920 elf_tdata (abfd)->linker = TRUE;
5922 return TRUE;
5924 error_return:
5925 if (finfo.symstrtab != NULL)
5926 _bfd_stringtab_free (finfo.symstrtab);
5927 if (finfo.contents != NULL)
5928 free (finfo.contents);
5929 if (finfo.external_relocs != NULL)
5930 free (finfo.external_relocs);
5931 if (finfo.internal_relocs != NULL)
5932 free (finfo.internal_relocs);
5933 if (finfo.external_syms != NULL)
5934 free (finfo.external_syms);
5935 if (finfo.locsym_shndx != NULL)
5936 free (finfo.locsym_shndx);
5937 if (finfo.internal_syms != NULL)
5938 free (finfo.internal_syms);
5939 if (finfo.indices != NULL)
5940 free (finfo.indices);
5941 if (finfo.sections != NULL)
5942 free (finfo.sections);
5943 if (finfo.symbuf != NULL)
5944 free (finfo.symbuf);
5945 if (finfo.symshndxbuf != NULL)
5946 free (finfo.symshndxbuf);
5947 for (o = abfd->sections; o != NULL; o = o->next)
5949 if ((o->flags & SEC_RELOC) != 0
5950 && elf_section_data (o)->rel_hashes != NULL)
5951 free (elf_section_data (o)->rel_hashes);
5954 return FALSE;
5957 /* Add a symbol to the output symbol table. */
5959 static bfd_boolean
5960 elf_link_output_sym (finfo, name, elfsym, input_sec)
5961 struct elf_final_link_info *finfo;
5962 const char *name;
5963 Elf_Internal_Sym *elfsym;
5964 asection *input_sec;
5966 Elf_External_Sym *dest;
5967 Elf_External_Sym_Shndx *destshndx;
5968 bfd_boolean (*output_symbol_hook)
5969 PARAMS ((bfd *, struct bfd_link_info *info, const char *,
5970 Elf_Internal_Sym *, asection *));
5972 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5973 elf_backend_link_output_symbol_hook;
5974 if (output_symbol_hook != NULL)
5976 if (! ((*output_symbol_hook)
5977 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5978 return FALSE;
5981 if (name == (const char *) NULL || *name == '\0')
5982 elfsym->st_name = 0;
5983 else if (input_sec->flags & SEC_EXCLUDE)
5984 elfsym->st_name = 0;
5985 else
5987 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5988 name, TRUE, FALSE);
5989 if (elfsym->st_name == (unsigned long) -1)
5990 return FALSE;
5993 if (finfo->symbuf_count >= finfo->symbuf_size)
5995 if (! elf_link_flush_output_syms (finfo))
5996 return FALSE;
5999 dest = finfo->symbuf + finfo->symbuf_count;
6000 destshndx = finfo->symshndxbuf;
6001 if (destshndx != NULL)
6003 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6005 bfd_size_type amt;
6007 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6008 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6009 if (destshndx == NULL)
6010 return FALSE;
6011 memset ((char *) destshndx + amt, 0, amt);
6012 finfo->shndxbuf_size *= 2;
6014 destshndx += bfd_get_symcount (finfo->output_bfd);
6017 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
6018 finfo->symbuf_count += 1;
6019 bfd_get_symcount (finfo->output_bfd) += 1;
6021 return TRUE;
6024 /* Flush the output symbols to the file. */
6026 static bfd_boolean
6027 elf_link_flush_output_syms (finfo)
6028 struct elf_final_link_info *finfo;
6030 if (finfo->symbuf_count > 0)
6032 Elf_Internal_Shdr *hdr;
6033 file_ptr pos;
6034 bfd_size_type amt;
6036 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6037 pos = hdr->sh_offset + hdr->sh_size;
6038 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
6039 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6040 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
6041 return FALSE;
6043 hdr->sh_size += amt;
6044 finfo->symbuf_count = 0;
6047 return TRUE;
6050 /* Adjust all external symbols pointing into SEC_MERGE sections
6051 to reflect the object merging within the sections. */
6053 static bfd_boolean
6054 elf_link_sec_merge_syms (h, data)
6055 struct elf_link_hash_entry *h;
6056 PTR data;
6058 asection *sec;
6060 if (h->root.type == bfd_link_hash_warning)
6061 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6063 if ((h->root.type == bfd_link_hash_defined
6064 || h->root.type == bfd_link_hash_defweak)
6065 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
6066 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
6068 bfd *output_bfd = (bfd *) data;
6070 h->root.u.def.value =
6071 _bfd_merged_section_offset (output_bfd,
6072 &h->root.u.def.section,
6073 elf_section_data (sec)->sec_info,
6074 h->root.u.def.value, (bfd_vma) 0);
6077 return TRUE;
6080 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6081 allowing an unsatisfied unversioned symbol in the DSO to match a
6082 versioned symbol that would normally require an explicit version. */
6084 static bfd_boolean
6085 elf_link_check_versioned_symbol (info, h)
6086 struct bfd_link_info *info;
6087 struct elf_link_hash_entry *h;
6089 bfd *undef_bfd = h->root.u.undef.abfd;
6090 struct elf_link_loaded_list *loaded;
6092 if ((undef_bfd->flags & DYNAMIC) == 0
6093 || info->hash->creator->flavour != bfd_target_elf_flavour
6094 || elf_dt_soname (h->root.u.undef.abfd) == NULL)
6095 return FALSE;
6097 for (loaded = elf_hash_table (info)->loaded;
6098 loaded != NULL;
6099 loaded = loaded->next)
6101 bfd *input;
6102 Elf_Internal_Shdr *hdr;
6103 bfd_size_type symcount;
6104 bfd_size_type extsymcount;
6105 bfd_size_type extsymoff;
6106 Elf_Internal_Shdr *versymhdr;
6107 Elf_Internal_Sym *isym;
6108 Elf_Internal_Sym *isymend;
6109 Elf_Internal_Sym *isymbuf;
6110 Elf_External_Versym *ever;
6111 Elf_External_Versym *extversym;
6113 input = loaded->abfd;
6115 /* We check each DSO for a possible hidden versioned definition. */
6116 if (input == undef_bfd
6117 || (input->flags & DYNAMIC) == 0
6118 || elf_dynversym (input) == 0)
6119 continue;
6121 hdr = &elf_tdata (input)->dynsymtab_hdr;
6123 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
6124 if (elf_bad_symtab (input))
6126 extsymcount = symcount;
6127 extsymoff = 0;
6129 else
6131 extsymcount = symcount - hdr->sh_info;
6132 extsymoff = hdr->sh_info;
6135 if (extsymcount == 0)
6136 continue;
6138 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6139 NULL, NULL, NULL);
6140 if (isymbuf == NULL)
6141 return FALSE;
6143 /* Read in any version definitions. */
6144 versymhdr = &elf_tdata (input)->dynversym_hdr;
6145 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
6146 if (extversym == NULL)
6147 goto error_ret;
6149 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6150 || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
6151 != versymhdr->sh_size))
6153 free (extversym);
6154 error_ret:
6155 free (isymbuf);
6156 return FALSE;
6159 ever = extversym + extsymoff;
6160 isymend = isymbuf + extsymcount;
6161 for (isym = isymbuf; isym < isymend; isym++, ever++)
6163 const char *name;
6164 Elf_Internal_Versym iver;
6166 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6167 || isym->st_shndx == SHN_UNDEF)
6168 continue;
6170 name = bfd_elf_string_from_elf_section (input,
6171 hdr->sh_link,
6172 isym->st_name);
6173 if (strcmp (name, h->root.root.string) != 0)
6174 continue;
6176 _bfd_elf_swap_versym_in (input, ever, &iver);
6178 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6180 /* If we have a non-hidden versioned sym, then it should
6181 have provided a definition for the undefined sym. */
6182 abort ();
6185 if ((iver.vs_vers & VERSYM_VERSION) == 2)
6187 /* This is the oldest (default) sym. We can use it. */
6188 free (extversym);
6189 free (isymbuf);
6190 return TRUE;
6194 free (extversym);
6195 free (isymbuf);
6198 return FALSE;
6201 /* Add an external symbol to the symbol table. This is called from
6202 the hash table traversal routine. When generating a shared object,
6203 we go through the symbol table twice. The first time we output
6204 anything that might have been forced to local scope in a version
6205 script. The second time we output the symbols that are still
6206 global symbols. */
6208 static bfd_boolean
6209 elf_link_output_extsym (h, data)
6210 struct elf_link_hash_entry *h;
6211 PTR data;
6213 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6214 struct elf_final_link_info *finfo = eoinfo->finfo;
6215 bfd_boolean strip;
6216 Elf_Internal_Sym sym;
6217 asection *input_sec;
6219 if (h->root.type == bfd_link_hash_warning)
6221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6222 if (h->root.type == bfd_link_hash_new)
6223 return TRUE;
6226 /* Decide whether to output this symbol in this pass. */
6227 if (eoinfo->localsyms)
6229 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6230 return TRUE;
6232 else
6234 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6235 return TRUE;
6238 /* If we are not creating a shared library, and this symbol is
6239 referenced by a shared library but is not defined anywhere, then
6240 warn that it is undefined. If we do not do this, the runtime
6241 linker will complain that the symbol is undefined when the
6242 program is run. We don't have to worry about symbols that are
6243 referenced by regular files, because we will already have issued
6244 warnings for them. */
6245 if (! finfo->info->relocateable
6246 && (! finfo->info->shared || ! finfo->info->allow_shlib_undefined)
6247 && h->root.type == bfd_link_hash_undefined
6248 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6249 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6250 && ! elf_link_check_versioned_symbol (finfo->info, h))
6252 if (! ((*finfo->info->callbacks->undefined_symbol)
6253 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6254 (asection *) NULL, (bfd_vma) 0, TRUE)))
6256 eoinfo->failed = TRUE;
6257 return FALSE;
6261 /* We don't want to output symbols that have never been mentioned by
6262 a regular file, or that we have been told to strip. However, if
6263 h->indx is set to -2, the symbol is used by a reloc and we must
6264 output it. */
6265 if (h->indx == -2)
6266 strip = FALSE;
6267 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6268 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6269 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6270 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6271 strip = TRUE;
6272 else if (finfo->info->strip == strip_all)
6273 strip = TRUE;
6274 else if (finfo->info->strip == strip_some
6275 && bfd_hash_lookup (finfo->info->keep_hash,
6276 h->root.root.string, FALSE, FALSE) == NULL)
6277 strip = TRUE;
6278 else if (finfo->info->strip_discarded
6279 && (h->root.type == bfd_link_hash_defined
6280 || h->root.type == bfd_link_hash_defweak)
6281 && elf_discarded_section (h->root.u.def.section))
6282 strip = TRUE;
6283 else
6284 strip = FALSE;
6286 /* If we're stripping it, and it's not a dynamic symbol, there's
6287 nothing else to do unless it is a forced local symbol. */
6288 if (strip
6289 && h->dynindx == -1
6290 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6291 return TRUE;
6293 sym.st_value = 0;
6294 sym.st_size = h->size;
6295 sym.st_other = h->other;
6296 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6297 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6298 else if (h->root.type == bfd_link_hash_undefweak
6299 || h->root.type == bfd_link_hash_defweak)
6300 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6301 else
6302 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6304 switch (h->root.type)
6306 default:
6307 case bfd_link_hash_new:
6308 case bfd_link_hash_warning:
6309 abort ();
6310 return FALSE;
6312 case bfd_link_hash_undefined:
6313 case bfd_link_hash_undefweak:
6314 input_sec = bfd_und_section_ptr;
6315 sym.st_shndx = SHN_UNDEF;
6316 break;
6318 case bfd_link_hash_defined:
6319 case bfd_link_hash_defweak:
6321 input_sec = h->root.u.def.section;
6322 if (input_sec->output_section != NULL)
6324 sym.st_shndx =
6325 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6326 input_sec->output_section);
6327 if (sym.st_shndx == SHN_BAD)
6329 (*_bfd_error_handler)
6330 (_("%s: could not find output section %s for input section %s"),
6331 bfd_get_filename (finfo->output_bfd),
6332 input_sec->output_section->name,
6333 input_sec->name);
6334 eoinfo->failed = TRUE;
6335 return FALSE;
6338 /* ELF symbols in relocateable files are section relative,
6339 but in nonrelocateable files they are virtual
6340 addresses. */
6341 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6342 if (! finfo->info->relocateable)
6344 sym.st_value += input_sec->output_section->vma;
6345 if (h->type == STT_TLS)
6347 /* STT_TLS symbols are relative to PT_TLS segment
6348 base. */
6349 BFD_ASSERT (finfo->first_tls_sec != NULL);
6350 sym.st_value -= finfo->first_tls_sec->vma;
6354 else
6356 BFD_ASSERT (input_sec->owner == NULL
6357 || (input_sec->owner->flags & DYNAMIC) != 0);
6358 sym.st_shndx = SHN_UNDEF;
6359 input_sec = bfd_und_section_ptr;
6362 break;
6364 case bfd_link_hash_common:
6365 input_sec = h->root.u.c.p->section;
6366 sym.st_shndx = SHN_COMMON;
6367 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6368 break;
6370 case bfd_link_hash_indirect:
6371 /* These symbols are created by symbol versioning. They point
6372 to the decorated version of the name. For example, if the
6373 symbol foo@@GNU_1.2 is the default, which should be used when
6374 foo is used with no version, then we add an indirect symbol
6375 foo which points to foo@@GNU_1.2. We ignore these symbols,
6376 since the indirected symbol is already in the hash table. */
6377 return TRUE;
6380 /* Give the processor backend a chance to tweak the symbol value,
6381 and also to finish up anything that needs to be done for this
6382 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6383 forced local syms when non-shared is due to a historical quirk. */
6384 if ((h->dynindx != -1
6385 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6386 && (finfo->info->shared
6387 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6388 && elf_hash_table (finfo->info)->dynamic_sections_created)
6390 struct elf_backend_data *bed;
6392 bed = get_elf_backend_data (finfo->output_bfd);
6393 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6394 (finfo->output_bfd, finfo->info, h, &sym)))
6396 eoinfo->failed = TRUE;
6397 return FALSE;
6401 /* If we are marking the symbol as undefined, and there are no
6402 non-weak references to this symbol from a regular object, then
6403 mark the symbol as weak undefined; if there are non-weak
6404 references, mark the symbol as strong. We can't do this earlier,
6405 because it might not be marked as undefined until the
6406 finish_dynamic_symbol routine gets through with it. */
6407 if (sym.st_shndx == SHN_UNDEF
6408 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6409 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6410 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6412 int bindtype;
6414 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6415 bindtype = STB_GLOBAL;
6416 else
6417 bindtype = STB_WEAK;
6418 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6421 /* If a symbol is not defined locally, we clear the visibility field. */
6422 if (! finfo->info->relocateable
6423 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6424 sym.st_other &= ~ ELF_ST_VISIBILITY (-1);
6426 /* If this symbol should be put in the .dynsym section, then put it
6427 there now. We already know the symbol index. We also fill in
6428 the entry in the .hash section. */
6429 if (h->dynindx != -1
6430 && elf_hash_table (finfo->info)->dynamic_sections_created)
6432 size_t bucketcount;
6433 size_t bucket;
6434 size_t hash_entry_size;
6435 bfd_byte *bucketpos;
6436 bfd_vma chain;
6437 Elf_External_Sym *esym;
6439 sym.st_name = h->dynstr_index;
6440 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6441 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6443 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6444 bucket = h->elf_hash_value % bucketcount;
6445 hash_entry_size
6446 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6447 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6448 + (bucket + 2) * hash_entry_size);
6449 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6450 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6451 bucketpos);
6452 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6453 ((bfd_byte *) finfo->hash_sec->contents
6454 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6456 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6458 Elf_Internal_Versym iversym;
6459 Elf_External_Versym *eversym;
6461 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6463 if (h->verinfo.verdef == NULL)
6464 iversym.vs_vers = 0;
6465 else
6466 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6468 else
6470 if (h->verinfo.vertree == NULL)
6471 iversym.vs_vers = 1;
6472 else
6473 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6476 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6477 iversym.vs_vers |= VERSYM_HIDDEN;
6479 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6480 eversym += h->dynindx;
6481 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6485 /* If we're stripping it, then it was just a dynamic symbol, and
6486 there's nothing else to do. */
6487 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6488 return TRUE;
6490 h->indx = bfd_get_symcount (finfo->output_bfd);
6492 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6494 eoinfo->failed = TRUE;
6495 return FALSE;
6498 return TRUE;
6501 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6502 originated from the section given by INPUT_REL_HDR) to the
6503 OUTPUT_BFD. */
6505 static bfd_boolean
6506 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6507 internal_relocs)
6508 bfd *output_bfd;
6509 asection *input_section;
6510 Elf_Internal_Shdr *input_rel_hdr;
6511 Elf_Internal_Rela *internal_relocs;
6513 Elf_Internal_Rela *irela;
6514 Elf_Internal_Rela *irelaend;
6515 bfd_byte *erel;
6516 Elf_Internal_Shdr *output_rel_hdr;
6517 asection *output_section;
6518 unsigned int *rel_countp = NULL;
6519 struct elf_backend_data *bed;
6520 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
6522 output_section = input_section->output_section;
6523 output_rel_hdr = NULL;
6525 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6526 == input_rel_hdr->sh_entsize)
6528 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6529 rel_countp = &elf_section_data (output_section)->rel_count;
6531 else if (elf_section_data (output_section)->rel_hdr2
6532 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6533 == input_rel_hdr->sh_entsize))
6535 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6536 rel_countp = &elf_section_data (output_section)->rel_count2;
6538 else
6540 (*_bfd_error_handler)
6541 (_("%s: relocation size mismatch in %s section %s"),
6542 bfd_get_filename (output_bfd),
6543 bfd_archive_filename (input_section->owner),
6544 input_section->name);
6545 bfd_set_error (bfd_error_wrong_object_format);
6546 return FALSE;
6549 bed = get_elf_backend_data (output_bfd);
6550 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6551 swap_out = bed->s->swap_reloc_out;
6552 else if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
6553 swap_out = bed->s->swap_reloca_out;
6554 else
6555 abort ();
6557 erel = output_rel_hdr->contents;
6558 erel += *rel_countp * input_rel_hdr->sh_entsize;
6559 irela = internal_relocs;
6560 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
6561 * bed->s->int_rels_per_ext_rel);
6562 while (irela < irelaend)
6564 (*swap_out) (output_bfd, irela, erel);
6565 irela += bed->s->int_rels_per_ext_rel;
6566 erel += input_rel_hdr->sh_entsize;
6569 /* Bump the counter, so that we know where to add the next set of
6570 relocations. */
6571 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6573 return TRUE;
6576 /* Link an input file into the linker output file. This function
6577 handles all the sections and relocations of the input file at once.
6578 This is so that we only have to read the local symbols once, and
6579 don't have to keep them in memory. */
6581 static bfd_boolean
6582 elf_link_input_bfd (finfo, input_bfd)
6583 struct elf_final_link_info *finfo;
6584 bfd *input_bfd;
6586 bfd_boolean (*relocate_section)
6587 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6588 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
6589 bfd *output_bfd;
6590 Elf_Internal_Shdr *symtab_hdr;
6591 size_t locsymcount;
6592 size_t extsymoff;
6593 Elf_Internal_Sym *isymbuf;
6594 Elf_Internal_Sym *isym;
6595 Elf_Internal_Sym *isymend;
6596 long *pindex;
6597 asection **ppsection;
6598 asection *o;
6599 struct elf_backend_data *bed;
6600 bfd_boolean emit_relocs;
6601 struct elf_link_hash_entry **sym_hashes;
6603 output_bfd = finfo->output_bfd;
6604 bed = get_elf_backend_data (output_bfd);
6605 relocate_section = bed->elf_backend_relocate_section;
6607 /* If this is a dynamic object, we don't want to do anything here:
6608 we don't want the local symbols, and we don't want the section
6609 contents. */
6610 if ((input_bfd->flags & DYNAMIC) != 0)
6611 return TRUE;
6613 emit_relocs = (finfo->info->relocateable
6614 || finfo->info->emitrelocations
6615 || bed->elf_backend_emit_relocs);
6617 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6618 if (elf_bad_symtab (input_bfd))
6620 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6621 extsymoff = 0;
6623 else
6625 locsymcount = symtab_hdr->sh_info;
6626 extsymoff = symtab_hdr->sh_info;
6629 /* Read the local symbols. */
6630 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6631 if (isymbuf == NULL && locsymcount != 0)
6633 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6634 finfo->internal_syms,
6635 finfo->external_syms,
6636 finfo->locsym_shndx);
6637 if (isymbuf == NULL)
6638 return FALSE;
6641 /* Find local symbol sections and adjust values of symbols in
6642 SEC_MERGE sections. Write out those local symbols we know are
6643 going into the output file. */
6644 isymend = isymbuf + locsymcount;
6645 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6646 isym < isymend;
6647 isym++, pindex++, ppsection++)
6649 asection *isec;
6650 const char *name;
6651 Elf_Internal_Sym osym;
6653 *pindex = -1;
6655 if (elf_bad_symtab (input_bfd))
6657 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6659 *ppsection = NULL;
6660 continue;
6664 if (isym->st_shndx == SHN_UNDEF)
6665 isec = bfd_und_section_ptr;
6666 else if (isym->st_shndx < SHN_LORESERVE
6667 || isym->st_shndx > SHN_HIRESERVE)
6669 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6670 if (isec
6671 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6672 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6673 isym->st_value =
6674 _bfd_merged_section_offset (output_bfd, &isec,
6675 elf_section_data (isec)->sec_info,
6676 isym->st_value, (bfd_vma) 0);
6678 else if (isym->st_shndx == SHN_ABS)
6679 isec = bfd_abs_section_ptr;
6680 else if (isym->st_shndx == SHN_COMMON)
6681 isec = bfd_com_section_ptr;
6682 else
6684 /* Who knows? */
6685 isec = NULL;
6688 *ppsection = isec;
6690 /* Don't output the first, undefined, symbol. */
6691 if (ppsection == finfo->sections)
6692 continue;
6694 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6696 /* We never output section symbols. Instead, we use the
6697 section symbol of the corresponding section in the output
6698 file. */
6699 continue;
6702 /* If we are stripping all symbols, we don't want to output this
6703 one. */
6704 if (finfo->info->strip == strip_all)
6705 continue;
6707 /* If we are discarding all local symbols, we don't want to
6708 output this one. If we are generating a relocateable output
6709 file, then some of the local symbols may be required by
6710 relocs; we output them below as we discover that they are
6711 needed. */
6712 if (finfo->info->discard == discard_all)
6713 continue;
6715 /* If this symbol is defined in a section which we are
6716 discarding, we don't need to keep it, but note that
6717 linker_mark is only reliable for sections that have contents.
6718 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6719 as well as linker_mark. */
6720 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6721 && isec != NULL
6722 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6723 || (! finfo->info->relocateable
6724 && (isec->flags & SEC_EXCLUDE) != 0)))
6725 continue;
6727 /* Get the name of the symbol. */
6728 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6729 isym->st_name);
6730 if (name == NULL)
6731 return FALSE;
6733 /* See if we are discarding symbols with this name. */
6734 if ((finfo->info->strip == strip_some
6735 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6736 == NULL))
6737 || (((finfo->info->discard == discard_sec_merge
6738 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6739 || finfo->info->discard == discard_l)
6740 && bfd_is_local_label_name (input_bfd, name)))
6741 continue;
6743 /* If we get here, we are going to output this symbol. */
6745 osym = *isym;
6747 /* Adjust the section index for the output file. */
6748 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6749 isec->output_section);
6750 if (osym.st_shndx == SHN_BAD)
6751 return FALSE;
6753 *pindex = bfd_get_symcount (output_bfd);
6755 /* ELF symbols in relocateable files are section relative, but
6756 in executable files they are virtual addresses. Note that
6757 this code assumes that all ELF sections have an associated
6758 BFD section with a reasonable value for output_offset; below
6759 we assume that they also have a reasonable value for
6760 output_section. Any special sections must be set up to meet
6761 these requirements. */
6762 osym.st_value += isec->output_offset;
6763 if (! finfo->info->relocateable)
6765 osym.st_value += isec->output_section->vma;
6766 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6768 /* STT_TLS symbols are relative to PT_TLS segment base. */
6769 BFD_ASSERT (finfo->first_tls_sec != NULL);
6770 osym.st_value -= finfo->first_tls_sec->vma;
6774 if (! elf_link_output_sym (finfo, name, &osym, isec))
6775 return FALSE;
6778 /* Relocate the contents of each section. */
6779 sym_hashes = elf_sym_hashes (input_bfd);
6780 for (o = input_bfd->sections; o != NULL; o = o->next)
6782 bfd_byte *contents;
6784 if (! o->linker_mark)
6786 /* This section was omitted from the link. */
6787 continue;
6790 if ((o->flags & SEC_HAS_CONTENTS) == 0
6791 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6792 continue;
6794 if ((o->flags & SEC_LINKER_CREATED) != 0)
6796 /* Section was created by elf_link_create_dynamic_sections
6797 or somesuch. */
6798 continue;
6801 /* Get the contents of the section. They have been cached by a
6802 relaxation routine. Note that o is a section in an input
6803 file, so the contents field will not have been set by any of
6804 the routines which work on output files. */
6805 if (elf_section_data (o)->this_hdr.contents != NULL)
6806 contents = elf_section_data (o)->this_hdr.contents;
6807 else
6809 contents = finfo->contents;
6810 if (! bfd_get_section_contents (input_bfd, o, contents,
6811 (file_ptr) 0, o->_raw_size))
6812 return FALSE;
6815 if ((o->flags & SEC_RELOC) != 0)
6817 Elf_Internal_Rela *internal_relocs;
6819 /* Get the swapped relocs. */
6820 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6821 (input_bfd, o, finfo->external_relocs,
6822 finfo->internal_relocs, FALSE));
6823 if (internal_relocs == NULL
6824 && o->reloc_count > 0)
6825 return FALSE;
6827 /* Run through the relocs looking for any against symbols
6828 from discarded sections and section symbols from
6829 removed link-once sections. Complain about relocs
6830 against discarded sections. Zero relocs against removed
6831 link-once sections. */
6832 if (!finfo->info->relocateable
6833 && !elf_section_ignore_discarded_relocs (o))
6835 Elf_Internal_Rela *rel, *relend;
6837 rel = internal_relocs;
6838 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6839 for ( ; rel < relend; rel++)
6841 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6843 if (r_symndx >= locsymcount
6844 || (elf_bad_symtab (input_bfd)
6845 && finfo->sections[r_symndx] == NULL))
6847 struct elf_link_hash_entry *h;
6849 h = sym_hashes[r_symndx - extsymoff];
6850 while (h->root.type == bfd_link_hash_indirect
6851 || h->root.type == bfd_link_hash_warning)
6852 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6854 /* Complain if the definition comes from a
6855 discarded section. */
6856 if ((h->root.type == bfd_link_hash_defined
6857 || h->root.type == bfd_link_hash_defweak)
6858 && elf_discarded_section (h->root.u.def.section))
6860 if ((o->flags & SEC_DEBUGGING) != 0)
6862 BFD_ASSERT (r_symndx != 0);
6863 memset (rel, 0, sizeof (*rel));
6865 else
6867 if (! ((*finfo->info->callbacks->undefined_symbol)
6868 (finfo->info, h->root.root.string,
6869 input_bfd, o, rel->r_offset,
6870 TRUE)))
6871 return FALSE;
6875 else
6877 asection *sec = finfo->sections[r_symndx];
6879 if (sec != NULL && elf_discarded_section (sec))
6881 if ((o->flags & SEC_DEBUGGING) != 0
6882 || (sec->flags & SEC_LINK_ONCE) != 0)
6884 BFD_ASSERT (r_symndx != 0);
6885 rel->r_info
6886 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6887 rel->r_addend = 0;
6889 else
6891 bfd_boolean ok;
6892 const char *msg
6893 = _("local symbols in discarded section %s");
6894 bfd_size_type amt
6895 = strlen (sec->name) + strlen (msg) - 1;
6896 char *buf = (char *) bfd_malloc (amt);
6898 if (buf != NULL)
6899 sprintf (buf, msg, sec->name);
6900 else
6901 buf = (char *) sec->name;
6902 ok = (*finfo->info->callbacks
6903 ->undefined_symbol) (finfo->info, buf,
6904 input_bfd, o,
6905 rel->r_offset,
6906 TRUE);
6907 if (buf != sec->name)
6908 free (buf);
6909 if (!ok)
6910 return FALSE;
6917 /* Relocate the section by invoking a back end routine.
6919 The back end routine is responsible for adjusting the
6920 section contents as necessary, and (if using Rela relocs
6921 and generating a relocateable output file) adjusting the
6922 reloc addend as necessary.
6924 The back end routine does not have to worry about setting
6925 the reloc address or the reloc symbol index.
6927 The back end routine is given a pointer to the swapped in
6928 internal symbols, and can access the hash table entries
6929 for the external symbols via elf_sym_hashes (input_bfd).
6931 When generating relocateable output, the back end routine
6932 must handle STB_LOCAL/STT_SECTION symbols specially. The
6933 output symbol is going to be a section symbol
6934 corresponding to the output section, which will require
6935 the addend to be adjusted. */
6937 if (! (*relocate_section) (output_bfd, finfo->info,
6938 input_bfd, o, contents,
6939 internal_relocs,
6940 isymbuf,
6941 finfo->sections))
6942 return FALSE;
6944 if (emit_relocs)
6946 Elf_Internal_Rela *irela;
6947 Elf_Internal_Rela *irelaend;
6948 bfd_vma last_offset;
6949 struct elf_link_hash_entry **rel_hash;
6950 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6951 unsigned int next_erel;
6952 bfd_boolean (*reloc_emitter)
6953 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *,
6954 Elf_Internal_Rela *));
6955 bfd_boolean rela_normal;
6957 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6958 rela_normal = (bed->rela_normal
6959 && (input_rel_hdr->sh_entsize
6960 == sizeof (Elf_External_Rela)));
6962 /* Adjust the reloc addresses and symbol indices. */
6964 irela = internal_relocs;
6965 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6966 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6967 + elf_section_data (o->output_section)->rel_count
6968 + elf_section_data (o->output_section)->rel_count2);
6969 last_offset = o->output_offset;
6970 if (!finfo->info->relocateable)
6971 last_offset += o->output_section->vma;
6972 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6974 unsigned long r_symndx;
6975 asection *sec;
6976 Elf_Internal_Sym sym;
6978 if (next_erel == bed->s->int_rels_per_ext_rel)
6980 rel_hash++;
6981 next_erel = 0;
6984 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6985 finfo->info, o,
6986 irela->r_offset);
6987 if (irela->r_offset >= (bfd_vma) -2)
6989 /* This is a reloc for a deleted entry or somesuch.
6990 Turn it into an R_*_NONE reloc, at the same
6991 offset as the last reloc. elf_eh_frame.c and
6992 elf_bfd_discard_info rely on reloc offsets
6993 being ordered. */
6994 irela->r_offset = last_offset;
6995 irela->r_info = 0;
6996 irela->r_addend = 0;
6997 continue;
7000 irela->r_offset += o->output_offset;
7002 /* Relocs in an executable have to be virtual addresses. */
7003 if (!finfo->info->relocateable)
7004 irela->r_offset += o->output_section->vma;
7006 last_offset = irela->r_offset;
7008 r_symndx = ELF_R_SYM (irela->r_info);
7009 if (r_symndx == STN_UNDEF)
7010 continue;
7012 if (r_symndx >= locsymcount
7013 || (elf_bad_symtab (input_bfd)
7014 && finfo->sections[r_symndx] == NULL))
7016 struct elf_link_hash_entry *rh;
7017 unsigned long indx;
7019 /* This is a reloc against a global symbol. We
7020 have not yet output all the local symbols, so
7021 we do not know the symbol index of any global
7022 symbol. We set the rel_hash entry for this
7023 reloc to point to the global hash table entry
7024 for this symbol. The symbol index is then
7025 set at the end of elf_bfd_final_link. */
7026 indx = r_symndx - extsymoff;
7027 rh = elf_sym_hashes (input_bfd)[indx];
7028 while (rh->root.type == bfd_link_hash_indirect
7029 || rh->root.type == bfd_link_hash_warning)
7030 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7032 /* Setting the index to -2 tells
7033 elf_link_output_extsym that this symbol is
7034 used by a reloc. */
7035 BFD_ASSERT (rh->indx < 0);
7036 rh->indx = -2;
7038 *rel_hash = rh;
7040 continue;
7043 /* This is a reloc against a local symbol. */
7045 *rel_hash = NULL;
7046 sym = isymbuf[r_symndx];
7047 sec = finfo->sections[r_symndx];
7048 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7050 /* I suppose the backend ought to fill in the
7051 section of any STT_SECTION symbol against a
7052 processor specific section. If we have
7053 discarded a section, the output_section will
7054 be the absolute section. */
7055 if (bfd_is_abs_section (sec)
7056 || (sec != NULL
7057 && bfd_is_abs_section (sec->output_section)))
7058 r_symndx = 0;
7059 else if (sec == NULL || sec->owner == NULL)
7061 bfd_set_error (bfd_error_bad_value);
7062 return FALSE;
7064 else
7066 r_symndx = sec->output_section->target_index;
7067 BFD_ASSERT (r_symndx != 0);
7070 /* Adjust the addend according to where the
7071 section winds up in the output section. */
7072 if (rela_normal)
7073 irela->r_addend += sec->output_offset;
7075 else
7077 if (finfo->indices[r_symndx] == -1)
7079 unsigned long shlink;
7080 const char *name;
7081 asection *osec;
7083 if (finfo->info->strip == strip_all)
7085 /* You can't do ld -r -s. */
7086 bfd_set_error (bfd_error_invalid_operation);
7087 return FALSE;
7090 /* This symbol was skipped earlier, but
7091 since it is needed by a reloc, we
7092 must output it now. */
7093 shlink = symtab_hdr->sh_link;
7094 name = (bfd_elf_string_from_elf_section
7095 (input_bfd, shlink, sym.st_name));
7096 if (name == NULL)
7097 return FALSE;
7099 osec = sec->output_section;
7100 sym.st_shndx =
7101 _bfd_elf_section_from_bfd_section (output_bfd,
7102 osec);
7103 if (sym.st_shndx == SHN_BAD)
7104 return FALSE;
7106 sym.st_value += sec->output_offset;
7107 if (! finfo->info->relocateable)
7109 sym.st_value += osec->vma;
7110 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7112 /* STT_TLS symbols are relative to PT_TLS
7113 segment base. */
7114 BFD_ASSERT (finfo->first_tls_sec != NULL);
7115 sym.st_value -= finfo->first_tls_sec->vma;
7119 finfo->indices[r_symndx]
7120 = bfd_get_symcount (output_bfd);
7122 if (! elf_link_output_sym (finfo, name, &sym, sec))
7123 return FALSE;
7126 r_symndx = finfo->indices[r_symndx];
7129 irela->r_info = ELF_R_INFO (r_symndx,
7130 ELF_R_TYPE (irela->r_info));
7133 /* Swap out the relocs. */
7134 if (bed->elf_backend_emit_relocs
7135 && !(finfo->info->relocateable
7136 || finfo->info->emitrelocations))
7137 reloc_emitter = bed->elf_backend_emit_relocs;
7138 else
7139 reloc_emitter = elf_link_output_relocs;
7141 if (input_rel_hdr->sh_size != 0
7142 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7143 internal_relocs))
7144 return FALSE;
7146 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7147 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7149 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7150 * bed->s->int_rels_per_ext_rel);
7151 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7152 internal_relocs))
7153 return FALSE;
7158 /* Write out the modified section contents. */
7159 if (bed->elf_backend_write_section
7160 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7162 /* Section written out. */
7164 else switch (o->sec_info_type)
7166 case ELF_INFO_TYPE_STABS:
7167 if (! (_bfd_write_section_stabs
7168 (output_bfd,
7169 &elf_hash_table (finfo->info)->stab_info,
7170 o, &elf_section_data (o)->sec_info, contents)))
7171 return FALSE;
7172 break;
7173 case ELF_INFO_TYPE_MERGE:
7174 if (! _bfd_write_merged_section (output_bfd, o,
7175 elf_section_data (o)->sec_info))
7176 return FALSE;
7177 break;
7178 case ELF_INFO_TYPE_EH_FRAME:
7180 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7181 o, contents))
7182 return FALSE;
7184 break;
7185 default:
7187 bfd_size_type sec_size;
7189 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7190 if (! (o->flags & SEC_EXCLUDE)
7191 && ! bfd_set_section_contents (output_bfd, o->output_section,
7192 contents,
7193 (file_ptr) o->output_offset,
7194 sec_size))
7195 return FALSE;
7197 break;
7201 return TRUE;
7204 /* Generate a reloc when linking an ELF file. This is a reloc
7205 requested by the linker, and does come from any input file. This
7206 is used to build constructor and destructor tables when linking
7207 with -Ur. */
7209 static bfd_boolean
7210 elf_reloc_link_order (output_bfd, info, output_section, link_order)
7211 bfd *output_bfd;
7212 struct bfd_link_info *info;
7213 asection *output_section;
7214 struct bfd_link_order *link_order;
7216 reloc_howto_type *howto;
7217 long indx;
7218 bfd_vma offset;
7219 bfd_vma addend;
7220 struct elf_link_hash_entry **rel_hash_ptr;
7221 Elf_Internal_Shdr *rel_hdr;
7222 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7223 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7224 bfd_byte *erel;
7225 unsigned int i;
7227 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7228 if (howto == NULL)
7230 bfd_set_error (bfd_error_bad_value);
7231 return FALSE;
7234 addend = link_order->u.reloc.p->addend;
7236 /* Figure out the symbol index. */
7237 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7238 + elf_section_data (output_section)->rel_count
7239 + elf_section_data (output_section)->rel_count2);
7240 if (link_order->type == bfd_section_reloc_link_order)
7242 indx = link_order->u.reloc.p->u.section->target_index;
7243 BFD_ASSERT (indx != 0);
7244 *rel_hash_ptr = NULL;
7246 else
7248 struct elf_link_hash_entry *h;
7250 /* Treat a reloc against a defined symbol as though it were
7251 actually against the section. */
7252 h = ((struct elf_link_hash_entry *)
7253 bfd_wrapped_link_hash_lookup (output_bfd, info,
7254 link_order->u.reloc.p->u.name,
7255 FALSE, FALSE, TRUE));
7256 if (h != NULL
7257 && (h->root.type == bfd_link_hash_defined
7258 || h->root.type == bfd_link_hash_defweak))
7260 asection *section;
7262 section = h->root.u.def.section;
7263 indx = section->output_section->target_index;
7264 *rel_hash_ptr = NULL;
7265 /* It seems that we ought to add the symbol value to the
7266 addend here, but in practice it has already been added
7267 because it was passed to constructor_callback. */
7268 addend += section->output_section->vma + section->output_offset;
7270 else if (h != NULL)
7272 /* Setting the index to -2 tells elf_link_output_extsym that
7273 this symbol is used by a reloc. */
7274 h->indx = -2;
7275 *rel_hash_ptr = h;
7276 indx = 0;
7278 else
7280 if (! ((*info->callbacks->unattached_reloc)
7281 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7282 (asection *) NULL, (bfd_vma) 0)))
7283 return FALSE;
7284 indx = 0;
7288 /* If this is an inplace reloc, we must write the addend into the
7289 object file. */
7290 if (howto->partial_inplace && addend != 0)
7292 bfd_size_type size;
7293 bfd_reloc_status_type rstat;
7294 bfd_byte *buf;
7295 bfd_boolean ok;
7296 const char *sym_name;
7298 size = bfd_get_reloc_size (howto);
7299 buf = (bfd_byte *) bfd_zmalloc (size);
7300 if (buf == (bfd_byte *) NULL)
7301 return FALSE;
7302 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
7303 switch (rstat)
7305 case bfd_reloc_ok:
7306 break;
7308 default:
7309 case bfd_reloc_outofrange:
7310 abort ();
7312 case bfd_reloc_overflow:
7313 if (link_order->type == bfd_section_reloc_link_order)
7314 sym_name = bfd_section_name (output_bfd,
7315 link_order->u.reloc.p->u.section);
7316 else
7317 sym_name = link_order->u.reloc.p->u.name;
7318 if (! ((*info->callbacks->reloc_overflow)
7319 (info, sym_name, howto->name, addend,
7320 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7322 free (buf);
7323 return FALSE;
7325 break;
7327 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7328 (file_ptr) link_order->offset, size);
7329 free (buf);
7330 if (! ok)
7331 return FALSE;
7334 /* The address of a reloc is relative to the section in a
7335 relocateable file, and is a virtual address in an executable
7336 file. */
7337 offset = link_order->offset;
7338 if (! info->relocateable)
7339 offset += output_section->vma;
7341 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7343 irel[i].r_offset = offset;
7344 irel[i].r_info = 0;
7345 irel[i].r_addend = 0;
7347 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7349 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7350 erel = rel_hdr->contents;
7351 if (rel_hdr->sh_type == SHT_REL)
7353 erel += (elf_section_data (output_section)->rel_count
7354 * sizeof (Elf_External_Rel));
7355 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7357 else
7359 irel[0].r_addend = addend;
7360 erel += (elf_section_data (output_section)->rel_count
7361 * sizeof (Elf_External_Rela));
7362 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7365 ++elf_section_data (output_section)->rel_count;
7367 return TRUE;
7370 /* Allocate a pointer to live in a linker created section. */
7372 bfd_boolean
7373 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7374 bfd *abfd;
7375 struct bfd_link_info *info;
7376 elf_linker_section_t *lsect;
7377 struct elf_link_hash_entry *h;
7378 const Elf_Internal_Rela *rel;
7380 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7381 elf_linker_section_pointers_t *linker_section_ptr;
7382 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7383 bfd_size_type amt;
7385 BFD_ASSERT (lsect != NULL);
7387 /* Is this a global symbol? */
7388 if (h != NULL)
7390 /* Has this symbol already been allocated? If so, our work is done. */
7391 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7392 rel->r_addend,
7393 lsect->which))
7394 return TRUE;
7396 ptr_linker_section_ptr = &h->linker_section_pointer;
7397 /* Make sure this symbol is output as a dynamic symbol. */
7398 if (h->dynindx == -1)
7400 if (! elf_link_record_dynamic_symbol (info, h))
7401 return FALSE;
7404 if (lsect->rel_section)
7405 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7407 else
7409 /* Allocation of a pointer to a local symbol. */
7410 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7412 /* Allocate a table to hold the local symbols if first time. */
7413 if (!ptr)
7415 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7416 register unsigned int i;
7418 amt = num_symbols;
7419 amt *= sizeof (elf_linker_section_pointers_t *);
7420 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7422 if (!ptr)
7423 return FALSE;
7425 elf_local_ptr_offsets (abfd) = ptr;
7426 for (i = 0; i < num_symbols; i++)
7427 ptr[i] = (elf_linker_section_pointers_t *) 0;
7430 /* Has this symbol already been allocated? If so, our work is done. */
7431 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7432 rel->r_addend,
7433 lsect->which))
7434 return TRUE;
7436 ptr_linker_section_ptr = &ptr[r_symndx];
7438 if (info->shared)
7440 /* If we are generating a shared object, we need to
7441 output a R_<xxx>_RELATIVE reloc so that the
7442 dynamic linker can adjust this GOT entry. */
7443 BFD_ASSERT (lsect->rel_section != NULL);
7444 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7448 /* Allocate space for a pointer in the linker section, and allocate
7449 a new pointer record from internal memory. */
7450 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7451 amt = sizeof (elf_linker_section_pointers_t);
7452 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7454 if (!linker_section_ptr)
7455 return FALSE;
7457 linker_section_ptr->next = *ptr_linker_section_ptr;
7458 linker_section_ptr->addend = rel->r_addend;
7459 linker_section_ptr->which = lsect->which;
7460 linker_section_ptr->written_address_p = FALSE;
7461 *ptr_linker_section_ptr = linker_section_ptr;
7463 #if 0
7464 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7466 linker_section_ptr->offset = (lsect->section->_raw_size
7467 - lsect->hole_size + (ARCH_SIZE / 8));
7468 lsect->hole_offset += ARCH_SIZE / 8;
7469 lsect->sym_offset += ARCH_SIZE / 8;
7470 if (lsect->sym_hash)
7472 /* Bump up symbol value if needed. */
7473 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7474 #ifdef DEBUG
7475 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7476 lsect->sym_hash->root.root.string,
7477 (long) ARCH_SIZE / 8,
7478 (long) lsect->sym_hash->root.u.def.value);
7479 #endif
7482 else
7483 #endif
7484 linker_section_ptr->offset = lsect->section->_raw_size;
7486 lsect->section->_raw_size += ARCH_SIZE / 8;
7488 #ifdef DEBUG
7489 fprintf (stderr,
7490 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7491 lsect->name, (long) linker_section_ptr->offset,
7492 (long) lsect->section->_raw_size);
7493 #endif
7495 return TRUE;
7498 #if ARCH_SIZE==64
7499 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7500 #endif
7501 #if ARCH_SIZE==32
7502 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7503 #endif
7505 /* Fill in the address for a pointer generated in a linker section. */
7507 bfd_vma
7508 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7509 relocation, rel, relative_reloc)
7510 bfd *output_bfd;
7511 bfd *input_bfd;
7512 struct bfd_link_info *info;
7513 elf_linker_section_t *lsect;
7514 struct elf_link_hash_entry *h;
7515 bfd_vma relocation;
7516 const Elf_Internal_Rela *rel;
7517 int relative_reloc;
7519 elf_linker_section_pointers_t *linker_section_ptr;
7521 BFD_ASSERT (lsect != NULL);
7523 if (h != NULL)
7525 /* Handle global symbol. */
7526 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7527 (h->linker_section_pointer,
7528 rel->r_addend,
7529 lsect->which));
7531 BFD_ASSERT (linker_section_ptr != NULL);
7533 if (! elf_hash_table (info)->dynamic_sections_created
7534 || (info->shared
7535 && info->symbolic
7536 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7538 /* This is actually a static link, or it is a
7539 -Bsymbolic link and the symbol is defined
7540 locally. We must initialize this entry in the
7541 global section.
7543 When doing a dynamic link, we create a .rela.<xxx>
7544 relocation entry to initialize the value. This
7545 is done in the finish_dynamic_symbol routine. */
7546 if (!linker_section_ptr->written_address_p)
7548 linker_section_ptr->written_address_p = TRUE;
7549 bfd_put_ptr (output_bfd,
7550 relocation + linker_section_ptr->addend,
7551 (lsect->section->contents
7552 + linker_section_ptr->offset));
7556 else
7558 /* Handle local symbol. */
7559 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7560 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7561 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7562 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7563 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7564 rel->r_addend,
7565 lsect->which));
7567 BFD_ASSERT (linker_section_ptr != NULL);
7569 /* Write out pointer if it hasn't been rewritten out before. */
7570 if (!linker_section_ptr->written_address_p)
7572 linker_section_ptr->written_address_p = TRUE;
7573 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7574 lsect->section->contents + linker_section_ptr->offset);
7576 if (info->shared)
7578 asection *srel = lsect->rel_section;
7579 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
7580 bfd_byte *erel;
7581 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7582 unsigned int i;
7584 /* We need to generate a relative reloc for the dynamic
7585 linker. */
7586 if (!srel)
7588 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7589 lsect->rel_name);
7590 lsect->rel_section = srel;
7593 BFD_ASSERT (srel != NULL);
7595 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7597 outrel[i].r_offset = (lsect->section->output_section->vma
7598 + lsect->section->output_offset
7599 + linker_section_ptr->offset);
7600 outrel[i].r_info = 0;
7601 outrel[i].r_addend = 0;
7603 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7604 erel = lsect->section->contents;
7605 erel += (elf_section_data (lsect->section)->rel_count++
7606 * sizeof (Elf_External_Rela));
7607 elf_swap_reloca_out (output_bfd, outrel, erel);
7612 relocation = (lsect->section->output_offset
7613 + linker_section_ptr->offset
7614 - lsect->hole_offset
7615 - lsect->sym_offset);
7617 #ifdef DEBUG
7618 fprintf (stderr,
7619 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7620 lsect->name, (long) relocation, (long) relocation);
7621 #endif
7623 /* Subtract out the addend, because it will get added back in by the normal
7624 processing. */
7625 return relocation - linker_section_ptr->addend;
7628 /* Garbage collect unused sections. */
7630 static bfd_boolean elf_gc_mark
7631 PARAMS ((struct bfd_link_info *, asection *,
7632 asection * (*) (asection *, struct bfd_link_info *,
7633 Elf_Internal_Rela *, struct elf_link_hash_entry *,
7634 Elf_Internal_Sym *)));
7636 static bfd_boolean elf_gc_sweep
7637 PARAMS ((struct bfd_link_info *,
7638 bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *,
7639 const Elf_Internal_Rela *)));
7641 static bfd_boolean elf_gc_sweep_symbol
7642 PARAMS ((struct elf_link_hash_entry *, PTR));
7644 static bfd_boolean elf_gc_allocate_got_offsets
7645 PARAMS ((struct elf_link_hash_entry *, PTR));
7647 static bfd_boolean elf_gc_propagate_vtable_entries_used
7648 PARAMS ((struct elf_link_hash_entry *, PTR));
7650 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7651 PARAMS ((struct elf_link_hash_entry *, PTR));
7653 /* The mark phase of garbage collection. For a given section, mark
7654 it and any sections in this section's group, and all the sections
7655 which define symbols to which it refers. */
7657 typedef asection * (*gc_mark_hook_fn)
7658 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
7659 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7661 static bfd_boolean
7662 elf_gc_mark (info, sec, gc_mark_hook)
7663 struct bfd_link_info *info;
7664 asection *sec;
7665 gc_mark_hook_fn gc_mark_hook;
7667 bfd_boolean ret;
7668 asection *group_sec;
7670 sec->gc_mark = 1;
7672 /* Mark all the sections in the group. */
7673 group_sec = elf_section_data (sec)->next_in_group;
7674 if (group_sec && !group_sec->gc_mark)
7675 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7676 return FALSE;
7678 /* Look through the section relocs. */
7679 ret = TRUE;
7680 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7682 Elf_Internal_Rela *relstart, *rel, *relend;
7683 Elf_Internal_Shdr *symtab_hdr;
7684 struct elf_link_hash_entry **sym_hashes;
7685 size_t nlocsyms;
7686 size_t extsymoff;
7687 bfd *input_bfd = sec->owner;
7688 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7689 Elf_Internal_Sym *isym = NULL;
7691 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7692 sym_hashes = elf_sym_hashes (input_bfd);
7694 /* Read the local symbols. */
7695 if (elf_bad_symtab (input_bfd))
7697 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7698 extsymoff = 0;
7700 else
7701 extsymoff = nlocsyms = symtab_hdr->sh_info;
7703 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
7704 if (isym == NULL && nlocsyms != 0)
7706 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
7707 NULL, NULL, NULL);
7708 if (isym == NULL)
7709 return FALSE;
7712 /* Read the relocations. */
7713 relstart = (NAME(_bfd_elf,link_read_relocs)
7714 (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL,
7715 info->keep_memory));
7716 if (relstart == NULL)
7718 ret = FALSE;
7719 goto out1;
7721 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7723 for (rel = relstart; rel < relend; rel++)
7725 unsigned long r_symndx;
7726 asection *rsec;
7727 struct elf_link_hash_entry *h;
7729 r_symndx = ELF_R_SYM (rel->r_info);
7730 if (r_symndx == 0)
7731 continue;
7733 if (r_symndx >= nlocsyms
7734 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
7736 h = sym_hashes[r_symndx - extsymoff];
7737 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
7739 else
7741 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
7744 if (rsec && !rsec->gc_mark)
7746 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7747 rsec->gc_mark = 1;
7748 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7750 ret = FALSE;
7751 goto out2;
7756 out2:
7757 if (elf_section_data (sec)->relocs != relstart)
7758 free (relstart);
7759 out1:
7760 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
7762 if (! info->keep_memory)
7763 free (isym);
7764 else
7765 symtab_hdr->contents = (unsigned char *) isym;
7769 return ret;
7772 /* The sweep phase of garbage collection. Remove all garbage sections. */
7774 typedef bfd_boolean (*gc_sweep_hook_fn)
7775 PARAMS ((bfd *, struct bfd_link_info *, asection *,
7776 const Elf_Internal_Rela *));
7778 static bfd_boolean
7779 elf_gc_sweep (info, gc_sweep_hook)
7780 struct bfd_link_info *info;
7781 gc_sweep_hook_fn gc_sweep_hook;
7783 bfd *sub;
7785 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7787 asection *o;
7789 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7790 continue;
7792 for (o = sub->sections; o != NULL; o = o->next)
7794 /* Keep special sections. Keep .debug sections. */
7795 if ((o->flags & SEC_LINKER_CREATED)
7796 || (o->flags & SEC_DEBUGGING))
7797 o->gc_mark = 1;
7799 if (o->gc_mark)
7800 continue;
7802 /* Skip sweeping sections already excluded. */
7803 if (o->flags & SEC_EXCLUDE)
7804 continue;
7806 /* Since this is early in the link process, it is simple
7807 to remove a section from the output. */
7808 o->flags |= SEC_EXCLUDE;
7810 /* But we also have to update some of the relocation
7811 info we collected before. */
7812 if (gc_sweep_hook
7813 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7815 Elf_Internal_Rela *internal_relocs;
7816 bfd_boolean r;
7818 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7819 (o->owner, o, NULL, NULL, info->keep_memory));
7820 if (internal_relocs == NULL)
7821 return FALSE;
7823 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7825 if (elf_section_data (o)->relocs != internal_relocs)
7826 free (internal_relocs);
7828 if (!r)
7829 return FALSE;
7834 /* Remove the symbols that were in the swept sections from the dynamic
7835 symbol table. GCFIXME: Anyone know how to get them out of the
7836 static symbol table as well? */
7838 int i = 0;
7840 elf_link_hash_traverse (elf_hash_table (info),
7841 elf_gc_sweep_symbol,
7842 (PTR) &i);
7844 elf_hash_table (info)->dynsymcount = i;
7847 return TRUE;
7850 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7852 static bfd_boolean
7853 elf_gc_sweep_symbol (h, idxptr)
7854 struct elf_link_hash_entry *h;
7855 PTR idxptr;
7857 int *idx = (int *) idxptr;
7859 if (h->root.type == bfd_link_hash_warning)
7860 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7862 if (h->dynindx != -1
7863 && ((h->root.type != bfd_link_hash_defined
7864 && h->root.type != bfd_link_hash_defweak)
7865 || h->root.u.def.section->gc_mark))
7866 h->dynindx = (*idx)++;
7868 return TRUE;
7871 /* Propogate collected vtable information. This is called through
7872 elf_link_hash_traverse. */
7874 static bfd_boolean
7875 elf_gc_propagate_vtable_entries_used (h, okp)
7876 struct elf_link_hash_entry *h;
7877 PTR okp;
7879 if (h->root.type == bfd_link_hash_warning)
7880 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7882 /* Those that are not vtables. */
7883 if (h->vtable_parent == NULL)
7884 return TRUE;
7886 /* Those vtables that do not have parents, we cannot merge. */
7887 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7888 return TRUE;
7890 /* If we've already been done, exit. */
7891 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7892 return TRUE;
7894 /* Make sure the parent's table is up to date. */
7895 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7897 if (h->vtable_entries_used == NULL)
7899 /* None of this table's entries were referenced. Re-use the
7900 parent's table. */
7901 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7902 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7904 else
7906 size_t n;
7907 bfd_boolean *cu, *pu;
7909 /* Or the parent's entries into ours. */
7910 cu = h->vtable_entries_used;
7911 cu[-1] = TRUE;
7912 pu = h->vtable_parent->vtable_entries_used;
7913 if (pu != NULL)
7915 asection *sec = h->root.u.def.section;
7916 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7917 int file_align = bed->s->file_align;
7919 n = h->vtable_parent->vtable_entries_size / file_align;
7920 while (n--)
7922 if (*pu)
7923 *cu = TRUE;
7924 pu++;
7925 cu++;
7930 return TRUE;
7933 static bfd_boolean
7934 elf_gc_smash_unused_vtentry_relocs (h, okp)
7935 struct elf_link_hash_entry *h;
7936 PTR okp;
7938 asection *sec;
7939 bfd_vma hstart, hend;
7940 Elf_Internal_Rela *relstart, *relend, *rel;
7941 struct elf_backend_data *bed;
7942 int file_align;
7944 if (h->root.type == bfd_link_hash_warning)
7945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7947 /* Take care of both those symbols that do not describe vtables as
7948 well as those that are not loaded. */
7949 if (h->vtable_parent == NULL)
7950 return TRUE;
7952 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7953 || h->root.type == bfd_link_hash_defweak);
7955 sec = h->root.u.def.section;
7956 hstart = h->root.u.def.value;
7957 hend = hstart + h->size;
7959 relstart = (NAME(_bfd_elf,link_read_relocs)
7960 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, TRUE));
7961 if (!relstart)
7962 return *(bfd_boolean *) okp = FALSE;
7963 bed = get_elf_backend_data (sec->owner);
7964 file_align = bed->s->file_align;
7966 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7968 for (rel = relstart; rel < relend; ++rel)
7969 if (rel->r_offset >= hstart && rel->r_offset < hend)
7971 /* If the entry is in use, do nothing. */
7972 if (h->vtable_entries_used
7973 && (rel->r_offset - hstart) < h->vtable_entries_size)
7975 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7976 if (h->vtable_entries_used[entry])
7977 continue;
7979 /* Otherwise, kill it. */
7980 rel->r_offset = rel->r_info = rel->r_addend = 0;
7983 return TRUE;
7986 /* Do mark and sweep of unused sections. */
7988 bfd_boolean
7989 elf_gc_sections (abfd, info)
7990 bfd *abfd;
7991 struct bfd_link_info *info;
7993 bfd_boolean ok = TRUE;
7994 bfd *sub;
7995 asection * (*gc_mark_hook)
7996 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
7997 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7999 if (!get_elf_backend_data (abfd)->can_gc_sections
8000 || info->relocateable || info->emitrelocations
8001 || elf_hash_table (info)->dynamic_sections_created)
8002 return TRUE;
8004 /* Apply transitive closure to the vtable entry usage info. */
8005 elf_link_hash_traverse (elf_hash_table (info),
8006 elf_gc_propagate_vtable_entries_used,
8007 (PTR) &ok);
8008 if (!ok)
8009 return FALSE;
8011 /* Kill the vtable relocations that were not used. */
8012 elf_link_hash_traverse (elf_hash_table (info),
8013 elf_gc_smash_unused_vtentry_relocs,
8014 (PTR) &ok);
8015 if (!ok)
8016 return FALSE;
8018 /* Grovel through relocs to find out who stays ... */
8020 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8021 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8023 asection *o;
8025 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8026 continue;
8028 for (o = sub->sections; o != NULL; o = o->next)
8030 if (o->flags & SEC_KEEP)
8031 if (!elf_gc_mark (info, o, gc_mark_hook))
8032 return FALSE;
8036 /* ... and mark SEC_EXCLUDE for those that go. */
8037 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8038 return FALSE;
8040 return TRUE;
8043 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8045 bfd_boolean
8046 elf_gc_record_vtinherit (abfd, sec, h, offset)
8047 bfd *abfd;
8048 asection *sec;
8049 struct elf_link_hash_entry *h;
8050 bfd_vma offset;
8052 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8053 struct elf_link_hash_entry **search, *child;
8054 bfd_size_type extsymcount;
8056 /* The sh_info field of the symtab header tells us where the
8057 external symbols start. We don't care about the local symbols at
8058 this point. */
8059 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8060 if (!elf_bad_symtab (abfd))
8061 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8063 sym_hashes = elf_sym_hashes (abfd);
8064 sym_hashes_end = sym_hashes + extsymcount;
8066 /* Hunt down the child symbol, which is in this section at the same
8067 offset as the relocation. */
8068 for (search = sym_hashes; search != sym_hashes_end; ++search)
8070 if ((child = *search) != NULL
8071 && (child->root.type == bfd_link_hash_defined
8072 || child->root.type == bfd_link_hash_defweak)
8073 && child->root.u.def.section == sec
8074 && child->root.u.def.value == offset)
8075 goto win;
8078 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8079 bfd_archive_filename (abfd), sec->name,
8080 (unsigned long) offset);
8081 bfd_set_error (bfd_error_invalid_operation);
8082 return FALSE;
8084 win:
8085 if (!h)
8087 /* This *should* only be the absolute section. It could potentially
8088 be that someone has defined a non-global vtable though, which
8089 would be bad. It isn't worth paging in the local symbols to be
8090 sure though; that case should simply be handled by the assembler. */
8092 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8094 else
8095 child->vtable_parent = h;
8097 return TRUE;
8100 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8102 bfd_boolean
8103 elf_gc_record_vtentry (abfd, sec, h, addend)
8104 bfd *abfd ATTRIBUTE_UNUSED;
8105 asection *sec ATTRIBUTE_UNUSED;
8106 struct elf_link_hash_entry *h;
8107 bfd_vma addend;
8109 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8110 int file_align = bed->s->file_align;
8112 if (addend >= h->vtable_entries_size)
8114 size_t size, bytes;
8115 bfd_boolean *ptr = h->vtable_entries_used;
8117 /* While the symbol is undefined, we have to be prepared to handle
8118 a zero size. */
8119 if (h->root.type == bfd_link_hash_undefined)
8120 size = addend;
8121 else
8123 size = h->size;
8124 if (size < addend)
8126 /* Oops! We've got a reference past the defined end of
8127 the table. This is probably a bug -- shall we warn? */
8128 size = addend;
8132 /* Allocate one extra entry for use as a "done" flag for the
8133 consolidation pass. */
8134 bytes = (size / file_align + 1) * sizeof (bfd_boolean);
8136 if (ptr)
8138 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
8140 if (ptr != NULL)
8142 size_t oldbytes;
8144 oldbytes = ((h->vtable_entries_size / file_align + 1)
8145 * sizeof (bfd_boolean));
8146 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8149 else
8150 ptr = bfd_zmalloc ((bfd_size_type) bytes);
8152 if (ptr == NULL)
8153 return FALSE;
8155 /* And arrange for that done flag to be at index -1. */
8156 h->vtable_entries_used = ptr + 1;
8157 h->vtable_entries_size = size;
8160 h->vtable_entries_used[addend / file_align] = TRUE;
8162 return TRUE;
8165 /* And an accompanying bit to work out final got entry offsets once
8166 we're done. Should be called from final_link. */
8168 bfd_boolean
8169 elf_gc_common_finalize_got_offsets (abfd, info)
8170 bfd *abfd;
8171 struct bfd_link_info *info;
8173 bfd *i;
8174 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8175 bfd_vma gotoff;
8177 /* The GOT offset is relative to the .got section, but the GOT header is
8178 put into the .got.plt section, if the backend uses it. */
8179 if (bed->want_got_plt)
8180 gotoff = 0;
8181 else
8182 gotoff = bed->got_header_size;
8184 /* Do the local .got entries first. */
8185 for (i = info->input_bfds; i; i = i->link_next)
8187 bfd_signed_vma *local_got;
8188 bfd_size_type j, locsymcount;
8189 Elf_Internal_Shdr *symtab_hdr;
8191 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8192 continue;
8194 local_got = elf_local_got_refcounts (i);
8195 if (!local_got)
8196 continue;
8198 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8199 if (elf_bad_symtab (i))
8200 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8201 else
8202 locsymcount = symtab_hdr->sh_info;
8204 for (j = 0; j < locsymcount; ++j)
8206 if (local_got[j] > 0)
8208 local_got[j] = gotoff;
8209 gotoff += ARCH_SIZE / 8;
8211 else
8212 local_got[j] = (bfd_vma) -1;
8216 /* Then the global .got entries. .plt refcounts are handled by
8217 adjust_dynamic_symbol */
8218 elf_link_hash_traverse (elf_hash_table (info),
8219 elf_gc_allocate_got_offsets,
8220 (PTR) &gotoff);
8221 return TRUE;
8224 /* We need a special top-level link routine to convert got reference counts
8225 to real got offsets. */
8227 static bfd_boolean
8228 elf_gc_allocate_got_offsets (h, offarg)
8229 struct elf_link_hash_entry *h;
8230 PTR offarg;
8232 bfd_vma *off = (bfd_vma *) offarg;
8234 if (h->root.type == bfd_link_hash_warning)
8235 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8237 if (h->got.refcount > 0)
8239 h->got.offset = off[0];
8240 off[0] += ARCH_SIZE / 8;
8242 else
8243 h->got.offset = (bfd_vma) -1;
8245 return TRUE;
8248 /* Many folk need no more in the way of final link than this, once
8249 got entry reference counting is enabled. */
8251 bfd_boolean
8252 elf_gc_common_final_link (abfd, info)
8253 bfd *abfd;
8254 struct bfd_link_info *info;
8256 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8257 return FALSE;
8259 /* Invoke the regular ELF backend linker to do all the work. */
8260 return elf_bfd_final_link (abfd, info);
8263 /* This function will be called though elf_link_hash_traverse to store
8264 all hash value of the exported symbols in an array. */
8266 static bfd_boolean
8267 elf_collect_hash_codes (h, data)
8268 struct elf_link_hash_entry *h;
8269 PTR data;
8271 unsigned long **valuep = (unsigned long **) data;
8272 const char *name;
8273 char *p;
8274 unsigned long ha;
8275 char *alc = NULL;
8277 if (h->root.type == bfd_link_hash_warning)
8278 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8280 /* Ignore indirect symbols. These are added by the versioning code. */
8281 if (h->dynindx == -1)
8282 return TRUE;
8284 name = h->root.root.string;
8285 p = strchr (name, ELF_VER_CHR);
8286 if (p != NULL)
8288 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8289 memcpy (alc, name, (size_t) (p - name));
8290 alc[p - name] = '\0';
8291 name = alc;
8294 /* Compute the hash value. */
8295 ha = bfd_elf_hash (name);
8297 /* Store the found hash value in the array given as the argument. */
8298 *(*valuep)++ = ha;
8300 /* And store it in the struct so that we can put it in the hash table
8301 later. */
8302 h->elf_hash_value = ha;
8304 if (alc != NULL)
8305 free (alc);
8307 return TRUE;
8310 bfd_boolean
8311 elf_reloc_symbol_deleted_p (offset, cookie)
8312 bfd_vma offset;
8313 PTR cookie;
8315 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8317 if (rcookie->bad_symtab)
8318 rcookie->rel = rcookie->rels;
8320 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8322 unsigned long r_symndx;
8324 if (! rcookie->bad_symtab)
8325 if (rcookie->rel->r_offset > offset)
8326 return FALSE;
8327 if (rcookie->rel->r_offset != offset)
8328 continue;
8330 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8331 if (r_symndx == SHN_UNDEF)
8332 return TRUE;
8334 if (r_symndx >= rcookie->locsymcount
8335 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
8337 struct elf_link_hash_entry *h;
8339 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8341 while (h->root.type == bfd_link_hash_indirect
8342 || h->root.type == bfd_link_hash_warning)
8343 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8345 if ((h->root.type == bfd_link_hash_defined
8346 || h->root.type == bfd_link_hash_defweak)
8347 && elf_discarded_section (h->root.u.def.section))
8348 return TRUE;
8349 else
8350 return FALSE;
8352 else
8354 /* It's not a relocation against a global symbol,
8355 but it could be a relocation against a local
8356 symbol for a discarded section. */
8357 asection *isec;
8358 Elf_Internal_Sym *isym;
8360 /* Need to: get the symbol; get the section. */
8361 isym = &rcookie->locsyms[r_symndx];
8362 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8364 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
8365 if (isec != NULL && elf_discarded_section (isec))
8366 return TRUE;
8369 return FALSE;
8371 return FALSE;
8374 /* Discard unneeded references to discarded sections.
8375 Returns TRUE if any section's size was changed. */
8376 /* This function assumes that the relocations are in sorted order,
8377 which is true for all known assemblers. */
8379 bfd_boolean
8380 elf_bfd_discard_info (output_bfd, info)
8381 bfd *output_bfd;
8382 struct bfd_link_info *info;
8384 struct elf_reloc_cookie cookie;
8385 asection *stab, *eh;
8386 Elf_Internal_Shdr *symtab_hdr;
8387 struct elf_backend_data *bed;
8388 bfd *abfd;
8389 unsigned int count;
8390 bfd_boolean ret = FALSE;
8392 if (info->traditional_format
8393 || info->hash->creator->flavour != bfd_target_elf_flavour
8394 || ! is_elf_hash_table (info))
8395 return FALSE;
8397 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8399 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8400 continue;
8402 bed = get_elf_backend_data (abfd);
8404 if ((abfd->flags & DYNAMIC) != 0)
8405 continue;
8407 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8408 if (info->relocateable
8409 || (eh != NULL
8410 && (eh->_raw_size == 0
8411 || bfd_is_abs_section (eh->output_section))))
8412 eh = NULL;
8414 stab = bfd_get_section_by_name (abfd, ".stab");
8415 if (stab != NULL
8416 && (stab->_raw_size == 0
8417 || bfd_is_abs_section (stab->output_section)
8418 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
8419 stab = NULL;
8421 if (stab == NULL
8422 && eh == NULL
8423 && bed->elf_backend_discard_info == NULL)
8424 continue;
8426 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8427 cookie.abfd = abfd;
8428 cookie.sym_hashes = elf_sym_hashes (abfd);
8429 cookie.bad_symtab = elf_bad_symtab (abfd);
8430 if (cookie.bad_symtab)
8432 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8433 cookie.extsymoff = 0;
8435 else
8437 cookie.locsymcount = symtab_hdr->sh_info;
8438 cookie.extsymoff = symtab_hdr->sh_info;
8441 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8442 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
8444 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8445 cookie.locsymcount, 0,
8446 NULL, NULL, NULL);
8447 if (cookie.locsyms == NULL)
8448 return FALSE;
8451 if (stab != NULL)
8453 cookie.rels = NULL;
8454 count = stab->reloc_count;
8455 if (count != 0)
8456 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8457 (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8458 info->keep_memory));
8459 if (cookie.rels != NULL)
8461 cookie.rel = cookie.rels;
8462 cookie.relend = cookie.rels;
8463 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8464 if (_bfd_discard_section_stabs (abfd, stab,
8465 elf_section_data (stab)->sec_info,
8466 elf_reloc_symbol_deleted_p,
8467 &cookie))
8468 ret = TRUE;
8469 if (elf_section_data (stab)->relocs != cookie.rels)
8470 free (cookie.rels);
8474 if (eh != NULL)
8476 cookie.rels = NULL;
8477 count = eh->reloc_count;
8478 if (count != 0)
8479 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8480 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8481 info->keep_memory));
8482 cookie.rel = cookie.rels;
8483 cookie.relend = cookie.rels;
8484 if (cookie.rels != NULL)
8485 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8487 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
8488 elf_reloc_symbol_deleted_p,
8489 &cookie))
8490 ret = TRUE;
8492 if (cookie.rels != NULL
8493 && elf_section_data (eh)->relocs != cookie.rels)
8494 free (cookie.rels);
8497 if (bed->elf_backend_discard_info != NULL
8498 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
8499 ret = TRUE;
8501 if (cookie.locsyms != NULL
8502 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
8504 if (! info->keep_memory)
8505 free (cookie.locsyms);
8506 else
8507 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
8511 if (info->eh_frame_hdr
8512 && !info->relocateable
8513 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
8514 ret = TRUE;
8516 return ret;
8519 static bfd_boolean
8520 elf_section_ignore_discarded_relocs (sec)
8521 asection *sec;
8523 struct elf_backend_data *bed;
8525 switch (sec->sec_info_type)
8527 case ELF_INFO_TYPE_STABS:
8528 case ELF_INFO_TYPE_EH_FRAME:
8529 return TRUE;
8530 default:
8531 break;
8534 bed = get_elf_backend_data (sec->owner);
8535 if (bed->elf_backend_ignore_discarded_relocs != NULL
8536 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8537 return TRUE;
8539 return FALSE;