2002-02-18 David O'Brien <obrien@FreeBSD.org>
[binutils.git] / bfd / elflink.h
blob47927d3830e46e6468cc415b1c5fe573b8415b8b
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* ELF linker code. */
23 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
26 struct elf_info_failed
28 boolean failed;
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_find_version_dependencies
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_link_assign_sym_version
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_collect_hash_codes
65 PARAMS ((struct elf_link_hash_entry *, PTR));
66 static boolean elf_link_read_relocs_from_section
67 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
68 static size_t compute_bucket_count
69 PARAMS ((struct bfd_link_info *));
70 static void elf_link_output_relocs
71 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
72 static boolean elf_link_size_reloc_section
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static void elf_link_adjust_relocs
75 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
76 struct elf_link_hash_entry **));
77 static int elf_link_sort_cmp1
78 PARAMS ((const void *, const void *));
79 static int elf_link_sort_cmp2
80 PARAMS ((const void *, const void *));
81 static size_t elf_link_sort_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection **));
83 static boolean elf_section_ignore_discarded_relocs
84 PARAMS ((asection *));
86 /* Given an ELF BFD, add symbols to the global hash table as
87 appropriate. */
89 boolean
90 elf_bfd_link_add_symbols (abfd, info)
91 bfd *abfd;
92 struct bfd_link_info *info;
94 switch (bfd_get_format (abfd))
96 case bfd_object:
97 return elf_link_add_object_symbols (abfd, info);
98 case bfd_archive:
99 return elf_link_add_archive_symbols (abfd, info);
100 default:
101 bfd_set_error (bfd_error_wrong_format);
102 return false;
106 /* Return true iff this is a non-common, definition of a non-function symbol. */
107 static boolean
108 is_global_data_symbol_definition (abfd, sym)
109 bfd * abfd ATTRIBUTE_UNUSED;
110 Elf_Internal_Sym * sym;
112 /* Local symbols do not count, but target specific ones might. */
113 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
114 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
115 return false;
117 /* Function symbols do not count. */
118 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
119 return false;
121 /* If the section is undefined, then so is the symbol. */
122 if (sym->st_shndx == SHN_UNDEF)
123 return false;
125 /* If the symbol is defined in the common section, then
126 it is a common definition and so does not count. */
127 if (sym->st_shndx == SHN_COMMON)
128 return false;
130 /* If the symbol is in a target specific section then we
131 must rely upon the backend to tell us what it is. */
132 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
133 /* FIXME - this function is not coded yet:
135 return _bfd_is_global_symbol_definition (abfd, sym);
137 Instead for now assume that the definition is not global,
138 Even if this is wrong, at least the linker will behave
139 in the same way that it used to do. */
140 return false;
142 return true;
145 /* Search the symbol table of the archive element of the archive ABFD
146 whose archive map contains a mention of SYMDEF, and determine if
147 the symbol is defined in this element. */
148 static boolean
149 elf_link_is_defined_archive_symbol (abfd, symdef)
150 bfd * abfd;
151 carsym * symdef;
153 Elf_Internal_Shdr * hdr;
154 Elf_Internal_Shdr * shndx_hdr;
155 Elf_External_Sym * esym;
156 Elf_External_Sym * esymend;
157 Elf_External_Sym * buf = NULL;
158 Elf_External_Sym_Shndx * shndx_buf = NULL;
159 Elf_External_Sym_Shndx * shndx;
160 bfd_size_type symcount;
161 bfd_size_type extsymcount;
162 bfd_size_type extsymoff;
163 boolean result = false;
164 file_ptr pos;
165 bfd_size_type amt;
167 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
168 if (abfd == (bfd *) NULL)
169 return false;
171 if (! bfd_check_format (abfd, bfd_object))
172 return false;
174 /* If we have already included the element containing this symbol in the
175 link then we do not need to include it again. Just claim that any symbol
176 it contains is not a definition, so that our caller will not decide to
177 (re)include this element. */
178 if (abfd->archive_pass)
179 return false;
181 /* Select the appropriate symbol table. */
182 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
184 hdr = &elf_tdata (abfd)->symtab_hdr;
185 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
187 else
189 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
190 shndx_hdr = NULL;
193 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
195 /* The sh_info field of the symtab header tells us where the
196 external symbols start. We don't care about the local symbols. */
197 if (elf_bad_symtab (abfd))
199 extsymcount = symcount;
200 extsymoff = 0;
202 else
204 extsymcount = symcount - hdr->sh_info;
205 extsymoff = hdr->sh_info;
208 amt = extsymcount * sizeof (Elf_External_Sym);
209 buf = (Elf_External_Sym *) bfd_malloc (amt);
210 if (buf == NULL && extsymcount != 0)
211 return false;
213 /* Read in the symbol table.
214 FIXME: This ought to be cached somewhere. */
215 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
216 if (bfd_seek (abfd, pos, SEEK_SET) != 0
217 || bfd_bread ((PTR) buf, amt, abfd) != amt)
218 goto error_exit;
220 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
222 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
223 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
224 if (shndx_buf == NULL && extsymcount != 0)
225 goto error_exit;
227 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
228 if (bfd_seek (abfd, pos, SEEK_SET) != 0
229 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
230 goto error_exit;
233 /* Scan the symbol table looking for SYMDEF. */
234 esymend = buf + extsymcount;
235 for (esym = buf, shndx = shndx_buf;
236 esym < esymend;
237 esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
239 Elf_Internal_Sym sym;
240 const char * name;
242 elf_swap_symbol_in (abfd, esym, shndx, &sym);
244 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
245 if (name == (const char *) NULL)
246 break;
248 if (strcmp (name, symdef->name) == 0)
250 result = is_global_data_symbol_definition (abfd, & sym);
251 break;
255 error_exit:
256 if (shndx_buf != NULL)
257 free (shndx_buf);
258 if (buf != NULL)
259 free (buf);
261 return result;
264 /* Add symbols from an ELF archive file to the linker hash table. We
265 don't use _bfd_generic_link_add_archive_symbols because of a
266 problem which arises on UnixWare. The UnixWare libc.so is an
267 archive which includes an entry libc.so.1 which defines a bunch of
268 symbols. The libc.so archive also includes a number of other
269 object files, which also define symbols, some of which are the same
270 as those defined in libc.so.1. Correct linking requires that we
271 consider each object file in turn, and include it if it defines any
272 symbols we need. _bfd_generic_link_add_archive_symbols does not do
273 this; it looks through the list of undefined symbols, and includes
274 any object file which defines them. When this algorithm is used on
275 UnixWare, it winds up pulling in libc.so.1 early and defining a
276 bunch of symbols. This means that some of the other objects in the
277 archive are not included in the link, which is incorrect since they
278 precede libc.so.1 in the archive.
280 Fortunately, ELF archive handling is simpler than that done by
281 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
282 oddities. In ELF, if we find a symbol in the archive map, and the
283 symbol is currently undefined, we know that we must pull in that
284 object file.
286 Unfortunately, we do have to make multiple passes over the symbol
287 table until nothing further is resolved. */
289 static boolean
290 elf_link_add_archive_symbols (abfd, info)
291 bfd *abfd;
292 struct bfd_link_info *info;
294 symindex c;
295 boolean *defined = NULL;
296 boolean *included = NULL;
297 carsym *symdefs;
298 boolean loop;
299 bfd_size_type amt;
301 if (! bfd_has_map (abfd))
303 /* An empty archive is a special case. */
304 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
305 return true;
306 bfd_set_error (bfd_error_no_armap);
307 return false;
310 /* Keep track of all symbols we know to be already defined, and all
311 files we know to be already included. This is to speed up the
312 second and subsequent passes. */
313 c = bfd_ardata (abfd)->symdef_count;
314 if (c == 0)
315 return true;
316 amt = c;
317 amt *= sizeof (boolean);
318 defined = (boolean *) bfd_malloc (amt);
319 included = (boolean *) bfd_malloc (amt);
320 if (defined == (boolean *) NULL || included == (boolean *) NULL)
321 goto error_return;
322 memset (defined, 0, (size_t) amt);
323 memset (included, 0, (size_t) amt);
325 symdefs = bfd_ardata (abfd)->symdefs;
329 file_ptr last;
330 symindex i;
331 carsym *symdef;
332 carsym *symdefend;
334 loop = false;
335 last = -1;
337 symdef = symdefs;
338 symdefend = symdef + c;
339 for (i = 0; symdef < symdefend; symdef++, i++)
341 struct elf_link_hash_entry *h;
342 bfd *element;
343 struct bfd_link_hash_entry *undefs_tail;
344 symindex mark;
346 if (defined[i] || included[i])
347 continue;
348 if (symdef->file_offset == last)
350 included[i] = true;
351 continue;
354 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
355 false, false, false);
357 if (h == NULL)
359 char *p, *copy;
361 /* If this is a default version (the name contains @@),
362 look up the symbol again without the version. The
363 effect is that references to the symbol without the
364 version will be matched by the default symbol in the
365 archive. */
367 p = strchr (symdef->name, ELF_VER_CHR);
368 if (p == NULL || p[1] != ELF_VER_CHR)
369 continue;
371 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
372 if (copy == NULL)
373 goto error_return;
374 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
375 copy[p - symdef->name] = '\0';
377 h = elf_link_hash_lookup (elf_hash_table (info), copy,
378 false, false, false);
380 bfd_release (abfd, copy);
383 if (h == NULL)
384 continue;
386 if (h->root.type == bfd_link_hash_common)
388 /* We currently have a common symbol. The archive map contains
389 a reference to this symbol, so we may want to include it. We
390 only want to include it however, if this archive element
391 contains a definition of the symbol, not just another common
392 declaration of it.
394 Unfortunately some archivers (including GNU ar) will put
395 declarations of common symbols into their archive maps, as
396 well as real definitions, so we cannot just go by the archive
397 map alone. Instead we must read in the element's symbol
398 table and check that to see what kind of symbol definition
399 this is. */
400 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
401 continue;
403 else if (h->root.type != bfd_link_hash_undefined)
405 if (h->root.type != bfd_link_hash_undefweak)
406 defined[i] = true;
407 continue;
410 /* We need to include this archive member. */
411 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
412 if (element == (bfd *) NULL)
413 goto error_return;
415 if (! bfd_check_format (element, bfd_object))
416 goto error_return;
418 /* Doublecheck that we have not included this object
419 already--it should be impossible, but there may be
420 something wrong with the archive. */
421 if (element->archive_pass != 0)
423 bfd_set_error (bfd_error_bad_value);
424 goto error_return;
426 element->archive_pass = 1;
428 undefs_tail = info->hash->undefs_tail;
430 if (! (*info->callbacks->add_archive_element) (info, element,
431 symdef->name))
432 goto error_return;
433 if (! elf_link_add_object_symbols (element, info))
434 goto error_return;
436 /* If there are any new undefined symbols, we need to make
437 another pass through the archive in order to see whether
438 they can be defined. FIXME: This isn't perfect, because
439 common symbols wind up on undefs_tail and because an
440 undefined symbol which is defined later on in this pass
441 does not require another pass. This isn't a bug, but it
442 does make the code less efficient than it could be. */
443 if (undefs_tail != info->hash->undefs_tail)
444 loop = true;
446 /* Look backward to mark all symbols from this object file
447 which we have already seen in this pass. */
448 mark = i;
451 included[mark] = true;
452 if (mark == 0)
453 break;
454 --mark;
456 while (symdefs[mark].file_offset == symdef->file_offset);
458 /* We mark subsequent symbols from this object file as we go
459 on through the loop. */
460 last = symdef->file_offset;
463 while (loop);
465 free (defined);
466 free (included);
468 return true;
470 error_return:
471 if (defined != (boolean *) NULL)
472 free (defined);
473 if (included != (boolean *) NULL)
474 free (included);
475 return false;
478 /* This function is called when we want to define a new symbol. It
479 handles the various cases which arise when we find a definition in
480 a dynamic object, or when there is already a definition in a
481 dynamic object. The new symbol is described by NAME, SYM, PSEC,
482 and PVALUE. We set SYM_HASH to the hash table entry. We set
483 OVERRIDE if the old symbol is overriding a new definition. We set
484 TYPE_CHANGE_OK if it is OK for the type to change. We set
485 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
486 change, we mean that we shouldn't warn if the type or size does
487 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
488 a shared object. */
490 static boolean
491 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
492 override, type_change_ok, size_change_ok, dt_needed)
493 bfd *abfd;
494 struct bfd_link_info *info;
495 const char *name;
496 Elf_Internal_Sym *sym;
497 asection **psec;
498 bfd_vma *pvalue;
499 struct elf_link_hash_entry **sym_hash;
500 boolean *override;
501 boolean *type_change_ok;
502 boolean *size_change_ok;
503 boolean dt_needed;
505 asection *sec;
506 struct elf_link_hash_entry *h;
507 int bind;
508 bfd *oldbfd;
509 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
511 *override = false;
513 sec = *psec;
514 bind = ELF_ST_BIND (sym->st_info);
516 if (! bfd_is_und_section (sec))
517 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
518 else
519 h = ((struct elf_link_hash_entry *)
520 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
521 if (h == NULL)
522 return false;
523 *sym_hash = h;
525 /* This code is for coping with dynamic objects, and is only useful
526 if we are doing an ELF link. */
527 if (info->hash->creator != abfd->xvec)
528 return true;
530 /* For merging, we only care about real symbols. */
532 while (h->root.type == bfd_link_hash_indirect
533 || h->root.type == bfd_link_hash_warning)
534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
536 /* If we just created the symbol, mark it as being an ELF symbol.
537 Other than that, there is nothing to do--there is no merge issue
538 with a newly defined symbol--so we just return. */
540 if (h->root.type == bfd_link_hash_new)
542 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
543 return true;
546 /* OLDBFD is a BFD associated with the existing symbol. */
548 switch (h->root.type)
550 default:
551 oldbfd = NULL;
552 break;
554 case bfd_link_hash_undefined:
555 case bfd_link_hash_undefweak:
556 oldbfd = h->root.u.undef.abfd;
557 break;
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 oldbfd = h->root.u.def.section->owner;
562 break;
564 case bfd_link_hash_common:
565 oldbfd = h->root.u.c.p->section->owner;
566 break;
569 /* In cases involving weak versioned symbols, we may wind up trying
570 to merge a symbol with itself. Catch that here, to avoid the
571 confusion that results if we try to override a symbol with
572 itself. The additional tests catch cases like
573 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
574 dynamic object, which we do want to handle here. */
575 if (abfd == oldbfd
576 && ((abfd->flags & DYNAMIC) == 0
577 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
578 return true;
580 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
581 respectively, is from a dynamic object. */
583 if ((abfd->flags & DYNAMIC) != 0)
584 newdyn = true;
585 else
586 newdyn = false;
588 if (oldbfd != NULL)
589 olddyn = (oldbfd->flags & DYNAMIC) != 0;
590 else
592 asection *hsec;
594 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
595 indices used by MIPS ELF. */
596 switch (h->root.type)
598 default:
599 hsec = NULL;
600 break;
602 case bfd_link_hash_defined:
603 case bfd_link_hash_defweak:
604 hsec = h->root.u.def.section;
605 break;
607 case bfd_link_hash_common:
608 hsec = h->root.u.c.p->section;
609 break;
612 if (hsec == NULL)
613 olddyn = false;
614 else
615 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
618 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
619 respectively, appear to be a definition rather than reference. */
621 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
622 newdef = false;
623 else
624 newdef = true;
626 if (h->root.type == bfd_link_hash_undefined
627 || h->root.type == bfd_link_hash_undefweak
628 || h->root.type == bfd_link_hash_common)
629 olddef = false;
630 else
631 olddef = true;
633 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
634 symbol, respectively, appears to be a common symbol in a dynamic
635 object. If a symbol appears in an uninitialized section, and is
636 not weak, and is not a function, then it may be a common symbol
637 which was resolved when the dynamic object was created. We want
638 to treat such symbols specially, because they raise special
639 considerations when setting the symbol size: if the symbol
640 appears as a common symbol in a regular object, and the size in
641 the regular object is larger, we must make sure that we use the
642 larger size. This problematic case can always be avoided in C,
643 but it must be handled correctly when using Fortran shared
644 libraries.
646 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
647 likewise for OLDDYNCOMMON and OLDDEF.
649 Note that this test is just a heuristic, and that it is quite
650 possible to have an uninitialized symbol in a shared object which
651 is really a definition, rather than a common symbol. This could
652 lead to some minor confusion when the symbol really is a common
653 symbol in some regular object. However, I think it will be
654 harmless. */
656 if (newdyn
657 && newdef
658 && (sec->flags & SEC_ALLOC) != 0
659 && (sec->flags & SEC_LOAD) == 0
660 && sym->st_size > 0
661 && bind != STB_WEAK
662 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
663 newdyncommon = true;
664 else
665 newdyncommon = false;
667 if (olddyn
668 && olddef
669 && h->root.type == bfd_link_hash_defined
670 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
671 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
672 && (h->root.u.def.section->flags & SEC_LOAD) == 0
673 && h->size > 0
674 && h->type != STT_FUNC)
675 olddyncommon = true;
676 else
677 olddyncommon = false;
679 /* It's OK to change the type if either the existing symbol or the
680 new symbol is weak unless it comes from a DT_NEEDED entry of
681 a shared object, in which case, the DT_NEEDED entry may not be
682 required at the run time. */
684 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
685 || h->root.type == bfd_link_hash_undefweak
686 || bind == STB_WEAK)
687 *type_change_ok = true;
689 /* It's OK to change the size if either the existing symbol or the
690 new symbol is weak, or if the old symbol is undefined. */
692 if (*type_change_ok
693 || h->root.type == bfd_link_hash_undefined)
694 *size_change_ok = true;
696 /* If both the old and the new symbols look like common symbols in a
697 dynamic object, set the size of the symbol to the larger of the
698 two. */
700 if (olddyncommon
701 && newdyncommon
702 && sym->st_size != h->size)
704 /* Since we think we have two common symbols, issue a multiple
705 common warning if desired. Note that we only warn if the
706 size is different. If the size is the same, we simply let
707 the old symbol override the new one as normally happens with
708 symbols defined in dynamic objects. */
710 if (! ((*info->callbacks->multiple_common)
711 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
712 h->size, abfd, bfd_link_hash_common, sym->st_size)))
713 return false;
715 if (sym->st_size > h->size)
716 h->size = sym->st_size;
718 *size_change_ok = true;
721 /* If we are looking at a dynamic object, and we have found a
722 definition, we need to see if the symbol was already defined by
723 some other object. If so, we want to use the existing
724 definition, and we do not want to report a multiple symbol
725 definition error; we do this by clobbering *PSEC to be
726 bfd_und_section_ptr.
728 We treat a common symbol as a definition if the symbol in the
729 shared library is a function, since common symbols always
730 represent variables; this can cause confusion in principle, but
731 any such confusion would seem to indicate an erroneous program or
732 shared library. We also permit a common symbol in a regular
733 object to override a weak symbol in a shared object.
735 We prefer a non-weak definition in a shared library to a weak
736 definition in the executable unless it comes from a DT_NEEDED
737 entry of a shared object, in which case, the DT_NEEDED entry
738 may not be required at the run time. */
740 if (newdyn
741 && newdef
742 && (olddef
743 || (h->root.type == bfd_link_hash_common
744 && (bind == STB_WEAK
745 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
746 && (h->root.type != bfd_link_hash_defweak
747 || dt_needed
748 || bind == STB_WEAK))
750 *override = true;
751 newdef = false;
752 newdyncommon = false;
754 *psec = sec = bfd_und_section_ptr;
755 *size_change_ok = true;
757 /* If we get here when the old symbol is a common symbol, then
758 we are explicitly letting it override a weak symbol or
759 function in a dynamic object, and we don't want to warn about
760 a type change. If the old symbol is a defined symbol, a type
761 change warning may still be appropriate. */
763 if (h->root.type == bfd_link_hash_common)
764 *type_change_ok = true;
767 /* Handle the special case of an old common symbol merging with a
768 new symbol which looks like a common symbol in a shared object.
769 We change *PSEC and *PVALUE to make the new symbol look like a
770 common symbol, and let _bfd_generic_link_add_one_symbol will do
771 the right thing. */
773 if (newdyncommon
774 && h->root.type == bfd_link_hash_common)
776 *override = true;
777 newdef = false;
778 newdyncommon = false;
779 *pvalue = sym->st_size;
780 *psec = sec = bfd_com_section_ptr;
781 *size_change_ok = true;
784 /* If the old symbol is from a dynamic object, and the new symbol is
785 a definition which is not from a dynamic object, then the new
786 symbol overrides the old symbol. Symbols from regular files
787 always take precedence over symbols from dynamic objects, even if
788 they are defined after the dynamic object in the link.
790 As above, we again permit a common symbol in a regular object to
791 override a definition in a shared object if the shared object
792 symbol is a function or is weak.
794 As above, we permit a non-weak definition in a shared object to
795 override a weak definition in a regular object. */
797 if (! newdyn
798 && (newdef
799 || (bfd_is_com_section (sec)
800 && (h->root.type == bfd_link_hash_defweak
801 || h->type == STT_FUNC)))
802 && olddyn
803 && olddef
804 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
805 && (bind != STB_WEAK
806 || h->root.type == bfd_link_hash_defweak))
808 /* Change the hash table entry to undefined, and let
809 _bfd_generic_link_add_one_symbol do the right thing with the
810 new definition. */
812 h->root.type = bfd_link_hash_undefined;
813 h->root.u.undef.abfd = h->root.u.def.section->owner;
814 *size_change_ok = true;
816 olddef = false;
817 olddyncommon = false;
819 /* We again permit a type change when a common symbol may be
820 overriding a function. */
822 if (bfd_is_com_section (sec))
823 *type_change_ok = true;
825 /* This union may have been set to be non-NULL when this symbol
826 was seen in a dynamic object. We must force the union to be
827 NULL, so that it is correct for a regular symbol. */
829 h->verinfo.vertree = NULL;
831 /* In this special case, if H is the target of an indirection,
832 we want the caller to frob with H rather than with the
833 indirect symbol. That will permit the caller to redefine the
834 target of the indirection, rather than the indirect symbol
835 itself. FIXME: This will break the -y option if we store a
836 symbol with a different name. */
837 *sym_hash = h;
840 /* Handle the special case of a new common symbol merging with an
841 old symbol that looks like it might be a common symbol defined in
842 a shared object. Note that we have already handled the case in
843 which a new common symbol should simply override the definition
844 in the shared library. */
846 if (! newdyn
847 && bfd_is_com_section (sec)
848 && olddyncommon)
850 /* It would be best if we could set the hash table entry to a
851 common symbol, but we don't know what to use for the section
852 or the alignment. */
853 if (! ((*info->callbacks->multiple_common)
854 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
855 h->size, abfd, bfd_link_hash_common, sym->st_size)))
856 return false;
858 /* If the predumed common symbol in the dynamic object is
859 larger, pretend that the new symbol has its size. */
861 if (h->size > *pvalue)
862 *pvalue = h->size;
864 /* FIXME: We no longer know the alignment required by the symbol
865 in the dynamic object, so we just wind up using the one from
866 the regular object. */
868 olddef = false;
869 olddyncommon = false;
871 h->root.type = bfd_link_hash_undefined;
872 h->root.u.undef.abfd = h->root.u.def.section->owner;
874 *size_change_ok = true;
875 *type_change_ok = true;
877 h->verinfo.vertree = NULL;
880 /* Handle the special case of a weak definition in a regular object
881 followed by a non-weak definition in a shared object. In this
882 case, we prefer the definition in the shared object unless it
883 comes from a DT_NEEDED entry of a shared object, in which case,
884 the DT_NEEDED entry may not be required at the run time. */
885 if (olddef
886 && ! dt_needed
887 && h->root.type == bfd_link_hash_defweak
888 && newdef
889 && newdyn
890 && bind != STB_WEAK)
892 /* To make this work we have to frob the flags so that the rest
893 of the code does not think we are using the regular
894 definition. */
895 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
896 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
897 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
898 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
899 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
900 | ELF_LINK_HASH_DEF_DYNAMIC);
902 /* If H is the target of an indirection, we want the caller to
903 use H rather than the indirect symbol. Otherwise if we are
904 defining a new indirect symbol we will wind up attaching it
905 to the entry we are overriding. */
906 *sym_hash = h;
909 /* Handle the special case of a non-weak definition in a shared
910 object followed by a weak definition in a regular object. In
911 this case we prefer to definition in the shared object. To make
912 this work we have to tell the caller to not treat the new symbol
913 as a definition. */
914 if (olddef
915 && olddyn
916 && h->root.type != bfd_link_hash_defweak
917 && newdef
918 && ! newdyn
919 && bind == STB_WEAK)
920 *override = true;
922 return true;
925 /* This function is called to create an indirect symbol from the
926 default for the symbol with the default version if needed. The
927 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
928 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
929 indicates if it comes from a DT_NEEDED entry of a shared object. */
931 static boolean
932 elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
933 dynsym, override, dt_needed)
934 bfd *abfd;
935 struct bfd_link_info *info;
936 struct elf_link_hash_entry *h;
937 const char *name;
938 Elf_Internal_Sym *sym;
939 asection **sec;
940 bfd_vma *value;
941 boolean *dynsym;
942 boolean override;
943 boolean dt_needed;
945 boolean type_change_ok;
946 boolean size_change_ok;
947 char *shortname;
948 struct elf_link_hash_entry *hi;
949 struct elf_backend_data *bed;
950 boolean collect;
951 boolean dynamic;
952 char *p;
954 /* If this symbol has a version, and it is the default version, we
955 create an indirect symbol from the default name to the fully
956 decorated name. This will cause external references which do not
957 specify a version to be bound to this version of the symbol. */
958 p = strchr (name, ELF_VER_CHR);
959 if (p == NULL || p[1] != ELF_VER_CHR)
960 return true;
962 if (override)
964 /* We are overridden by an old defition. We need to check if we
965 need to crreate the indirect symbol from the default name. */
966 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
967 false, false);
968 BFD_ASSERT (hi != NULL);
969 if (hi == h)
970 return true;
971 while (hi->root.type == bfd_link_hash_indirect
972 || hi->root.type == bfd_link_hash_warning)
974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
975 if (hi == h)
976 return true;
980 bed = get_elf_backend_data (abfd);
981 collect = bed->collect;
982 dynamic = (abfd->flags & DYNAMIC) != 0;
984 shortname = bfd_hash_allocate (&info->hash->table,
985 (size_t) (p - name + 1));
986 if (shortname == NULL)
987 return false;
988 strncpy (shortname, name, (size_t) (p - name));
989 shortname [p - name] = '\0';
991 /* We are going to create a new symbol. Merge it with any existing
992 symbol with this name. For the purposes of the merge, act as
993 though we were defining the symbol we just defined, although we
994 actually going to define an indirect symbol. */
995 type_change_ok = false;
996 size_change_ok = false;
997 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
998 &hi, &override, &type_change_ok,
999 &size_change_ok, dt_needed))
1000 return false;
1002 if (! override)
1004 if (! (_bfd_generic_link_add_one_symbol
1005 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1006 (bfd_vma) 0, name, false, collect,
1007 (struct bfd_link_hash_entry **) &hi)))
1008 return false;
1010 else
1012 /* In this case the symbol named SHORTNAME is overriding the
1013 indirect symbol we want to add. We were planning on making
1014 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1015 is the name without a version. NAME is the fully versioned
1016 name, and it is the default version.
1018 Overriding means that we already saw a definition for the
1019 symbol SHORTNAME in a regular object, and it is overriding
1020 the symbol defined in the dynamic object.
1022 When this happens, we actually want to change NAME, the
1023 symbol we just added, to refer to SHORTNAME. This will cause
1024 references to NAME in the shared object to become references
1025 to SHORTNAME in the regular object. This is what we expect
1026 when we override a function in a shared object: that the
1027 references in the shared object will be mapped to the
1028 definition in the regular object. */
1030 while (hi->root.type == bfd_link_hash_indirect
1031 || hi->root.type == bfd_link_hash_warning)
1032 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1034 h->root.type = bfd_link_hash_indirect;
1035 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1036 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1038 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1039 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1040 if (hi->elf_link_hash_flags
1041 & (ELF_LINK_HASH_REF_REGULAR
1042 | ELF_LINK_HASH_DEF_REGULAR))
1044 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1045 return false;
1049 /* Now set HI to H, so that the following code will set the
1050 other fields correctly. */
1051 hi = h;
1054 /* If there is a duplicate definition somewhere, then HI may not
1055 point to an indirect symbol. We will have reported an error to
1056 the user in that case. */
1058 if (hi->root.type == bfd_link_hash_indirect)
1060 struct elf_link_hash_entry *ht;
1062 /* If the symbol became indirect, then we assume that we have
1063 not seen a definition before. */
1064 BFD_ASSERT ((hi->elf_link_hash_flags
1065 & (ELF_LINK_HASH_DEF_DYNAMIC
1066 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1068 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1069 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1071 /* See if the new flags lead us to realize that the symbol must
1072 be dynamic. */
1073 if (! *dynsym)
1075 if (! dynamic)
1077 if (info->shared
1078 || ((hi->elf_link_hash_flags
1079 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1080 *dynsym = true;
1082 else
1084 if ((hi->elf_link_hash_flags
1085 & ELF_LINK_HASH_REF_REGULAR) != 0)
1086 *dynsym = true;
1091 /* We also need to define an indirection from the nondefault version
1092 of the symbol. */
1094 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1095 if (shortname == NULL)
1096 return false;
1097 strncpy (shortname, name, (size_t) (p - name));
1098 strcpy (shortname + (p - name), p + 1);
1100 /* Once again, merge with any existing symbol. */
1101 type_change_ok = false;
1102 size_change_ok = false;
1103 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1104 &hi, &override, &type_change_ok,
1105 &size_change_ok, dt_needed))
1106 return false;
1108 if (override)
1110 /* Here SHORTNAME is a versioned name, so we don't expect to see
1111 the type of override we do in the case above. */
1112 (*_bfd_error_handler)
1113 (_("%s: warning: unexpected redefinition of `%s'"),
1114 bfd_archive_filename (abfd), shortname);
1116 else
1118 if (! (_bfd_generic_link_add_one_symbol
1119 (info, abfd, shortname, BSF_INDIRECT,
1120 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1121 collect, (struct bfd_link_hash_entry **) &hi)))
1122 return false;
1124 /* If there is a duplicate definition somewhere, then HI may not
1125 point to an indirect symbol. We will have reported an error
1126 to the user in that case. */
1128 if (hi->root.type == bfd_link_hash_indirect)
1130 /* If the symbol became indirect, then we assume that we have
1131 not seen a definition before. */
1132 BFD_ASSERT ((hi->elf_link_hash_flags
1133 & (ELF_LINK_HASH_DEF_DYNAMIC
1134 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1136 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1138 /* See if the new flags lead us to realize that the symbol
1139 must be dynamic. */
1140 if (! *dynsym)
1142 if (! dynamic)
1144 if (info->shared
1145 || ((hi->elf_link_hash_flags
1146 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1147 *dynsym = true;
1149 else
1151 if ((hi->elf_link_hash_flags
1152 & ELF_LINK_HASH_REF_REGULAR) != 0)
1153 *dynsym = true;
1159 return true;
1162 /* Add symbols from an ELF object file to the linker hash table. */
1164 static boolean
1165 elf_link_add_object_symbols (abfd, info)
1166 bfd *abfd;
1167 struct bfd_link_info *info;
1169 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1170 const Elf_Internal_Sym *,
1171 const char **, flagword *,
1172 asection **, bfd_vma *));
1173 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1174 asection *, const Elf_Internal_Rela *));
1175 boolean collect;
1176 Elf_Internal_Shdr *hdr;
1177 Elf_Internal_Shdr *shndx_hdr;
1178 bfd_size_type symcount;
1179 bfd_size_type extsymcount;
1180 bfd_size_type extsymoff;
1181 Elf_External_Sym *buf = NULL;
1182 Elf_External_Sym_Shndx *shndx_buf = NULL;
1183 Elf_External_Sym_Shndx *shndx;
1184 struct elf_link_hash_entry **sym_hash;
1185 boolean dynamic;
1186 Elf_External_Versym *extversym = NULL;
1187 Elf_External_Versym *ever;
1188 Elf_External_Dyn *dynbuf = NULL;
1189 struct elf_link_hash_entry *weaks;
1190 Elf_External_Sym *esym;
1191 Elf_External_Sym *esymend;
1192 struct elf_backend_data *bed;
1193 boolean dt_needed;
1194 struct elf_link_hash_table * hash_table;
1195 file_ptr pos;
1196 bfd_size_type amt;
1198 hash_table = elf_hash_table (info);
1200 bed = get_elf_backend_data (abfd);
1201 add_symbol_hook = bed->elf_add_symbol_hook;
1202 collect = bed->collect;
1204 if ((abfd->flags & DYNAMIC) == 0)
1205 dynamic = false;
1206 else
1208 dynamic = true;
1210 /* You can't use -r against a dynamic object. Also, there's no
1211 hope of using a dynamic object which does not exactly match
1212 the format of the output file. */
1213 if (info->relocateable || info->hash->creator != abfd->xvec)
1215 bfd_set_error (bfd_error_invalid_operation);
1216 goto error_return;
1220 /* As a GNU extension, any input sections which are named
1221 .gnu.warning.SYMBOL are treated as warning symbols for the given
1222 symbol. This differs from .gnu.warning sections, which generate
1223 warnings when they are included in an output file. */
1224 if (! info->shared)
1226 asection *s;
1228 for (s = abfd->sections; s != NULL; s = s->next)
1230 const char *name;
1232 name = bfd_get_section_name (abfd, s);
1233 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1235 char *msg;
1236 bfd_size_type sz;
1238 name += sizeof ".gnu.warning." - 1;
1240 /* If this is a shared object, then look up the symbol
1241 in the hash table. If it is there, and it is already
1242 been defined, then we will not be using the entry
1243 from this shared object, so we don't need to warn.
1244 FIXME: If we see the definition in a regular object
1245 later on, we will warn, but we shouldn't. The only
1246 fix is to keep track of what warnings we are supposed
1247 to emit, and then handle them all at the end of the
1248 link. */
1249 if (dynamic && abfd->xvec == info->hash->creator)
1251 struct elf_link_hash_entry *h;
1253 h = elf_link_hash_lookup (hash_table, name,
1254 false, false, true);
1256 /* FIXME: What about bfd_link_hash_common? */
1257 if (h != NULL
1258 && (h->root.type == bfd_link_hash_defined
1259 || h->root.type == bfd_link_hash_defweak))
1261 /* We don't want to issue this warning. Clobber
1262 the section size so that the warning does not
1263 get copied into the output file. */
1264 s->_raw_size = 0;
1265 continue;
1269 sz = bfd_section_size (abfd, s);
1270 msg = (char *) bfd_alloc (abfd, sz + 1);
1271 if (msg == NULL)
1272 goto error_return;
1274 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1275 goto error_return;
1277 msg[sz] = '\0';
1279 if (! (_bfd_generic_link_add_one_symbol
1280 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1281 false, collect, (struct bfd_link_hash_entry **) NULL)))
1282 goto error_return;
1284 if (! info->relocateable)
1286 /* Clobber the section size so that the warning does
1287 not get copied into the output file. */
1288 s->_raw_size = 0;
1294 /* If this is a dynamic object, we always link against the .dynsym
1295 symbol table, not the .symtab symbol table. The dynamic linker
1296 will only see the .dynsym symbol table, so there is no reason to
1297 look at .symtab for a dynamic object. */
1299 if (! dynamic || elf_dynsymtab (abfd) == 0)
1301 hdr = &elf_tdata (abfd)->symtab_hdr;
1302 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1304 else
1306 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1307 shndx_hdr = NULL;
1310 if (dynamic)
1312 /* Read in any version definitions. */
1314 if (! _bfd_elf_slurp_version_tables (abfd))
1315 goto error_return;
1317 /* Read in the symbol versions, but don't bother to convert them
1318 to internal format. */
1319 if (elf_dynversym (abfd) != 0)
1321 Elf_Internal_Shdr *versymhdr;
1323 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1324 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1325 if (extversym == NULL)
1326 goto error_return;
1327 amt = versymhdr->sh_size;
1328 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1329 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1330 goto error_return;
1334 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1336 /* The sh_info field of the symtab header tells us where the
1337 external symbols start. We don't care about the local symbols at
1338 this point. */
1339 if (elf_bad_symtab (abfd))
1341 extsymcount = symcount;
1342 extsymoff = 0;
1344 else
1346 extsymcount = symcount - hdr->sh_info;
1347 extsymoff = hdr->sh_info;
1350 amt = extsymcount * sizeof (Elf_External_Sym);
1351 buf = (Elf_External_Sym *) bfd_malloc (amt);
1352 if (buf == NULL && extsymcount != 0)
1353 goto error_return;
1355 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1357 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1358 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1359 if (shndx_buf == NULL && extsymcount != 0)
1360 goto error_return;
1363 /* We store a pointer to the hash table entry for each external
1364 symbol. */
1365 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1366 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1367 if (sym_hash == NULL)
1368 goto error_return;
1369 elf_sym_hashes (abfd) = sym_hash;
1371 dt_needed = false;
1373 if (! dynamic)
1375 /* If we are creating a shared library, create all the dynamic
1376 sections immediately. We need to attach them to something,
1377 so we attach them to this BFD, provided it is the right
1378 format. FIXME: If there are no input BFD's of the same
1379 format as the output, we can't make a shared library. */
1380 if (info->shared
1381 && is_elf_hash_table (info)
1382 && ! hash_table->dynamic_sections_created
1383 && abfd->xvec == info->hash->creator)
1385 if (! elf_link_create_dynamic_sections (abfd, info))
1386 goto error_return;
1389 else if (! is_elf_hash_table (info))
1390 goto error_return;
1391 else
1393 asection *s;
1394 boolean add_needed;
1395 const char *name;
1396 bfd_size_type oldsize;
1397 bfd_size_type strindex;
1399 /* Find the name to use in a DT_NEEDED entry that refers to this
1400 object. If the object has a DT_SONAME entry, we use it.
1401 Otherwise, if the generic linker stuck something in
1402 elf_dt_name, we use that. Otherwise, we just use the file
1403 name. If the generic linker put a null string into
1404 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1405 there is a DT_SONAME entry. */
1406 add_needed = true;
1407 name = bfd_get_filename (abfd);
1408 if (elf_dt_name (abfd) != NULL)
1410 name = elf_dt_name (abfd);
1411 if (*name == '\0')
1413 if (elf_dt_soname (abfd) != NULL)
1414 dt_needed = true;
1416 add_needed = false;
1419 s = bfd_get_section_by_name (abfd, ".dynamic");
1420 if (s != NULL)
1422 Elf_External_Dyn *extdyn;
1423 Elf_External_Dyn *extdynend;
1424 int elfsec;
1425 unsigned long shlink;
1426 int rpath;
1427 int runpath;
1429 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1430 if (dynbuf == NULL)
1431 goto error_return;
1433 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1434 (file_ptr) 0, s->_raw_size))
1435 goto error_return;
1437 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1438 if (elfsec == -1)
1439 goto error_return;
1440 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1443 /* The shared libraries distributed with hpux11 have a bogus
1444 sh_link field for the ".dynamic" section. This code detects
1445 when SHLINK refers to a section that is not a string table
1446 and tries to find the string table for the ".dynsym" section
1447 instead. */
1448 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1449 if (shdr->sh_type != SHT_STRTAB)
1451 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1452 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1453 if (elfdsec == -1)
1454 goto error_return;
1455 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1459 extdyn = dynbuf;
1460 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1461 rpath = 0;
1462 runpath = 0;
1463 for (; extdyn < extdynend; extdyn++)
1465 Elf_Internal_Dyn dyn;
1467 elf_swap_dyn_in (abfd, extdyn, &dyn);
1468 if (dyn.d_tag == DT_SONAME)
1470 unsigned int tagv = dyn.d_un.d_val;
1471 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1472 if (name == NULL)
1473 goto error_return;
1475 if (dyn.d_tag == DT_NEEDED)
1477 struct bfd_link_needed_list *n, **pn;
1478 char *fnm, *anm;
1479 unsigned int tagv = dyn.d_un.d_val;
1481 amt = sizeof (struct bfd_link_needed_list);
1482 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1483 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1484 if (n == NULL || fnm == NULL)
1485 goto error_return;
1486 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1487 if (anm == NULL)
1488 goto error_return;
1489 strcpy (anm, fnm);
1490 n->name = anm;
1491 n->by = abfd;
1492 n->next = NULL;
1493 for (pn = & hash_table->needed;
1494 *pn != NULL;
1495 pn = &(*pn)->next)
1497 *pn = n;
1499 if (dyn.d_tag == DT_RUNPATH)
1501 struct bfd_link_needed_list *n, **pn;
1502 char *fnm, *anm;
1503 unsigned int tagv = dyn.d_un.d_val;
1505 /* When we see DT_RPATH before DT_RUNPATH, we have
1506 to clear runpath. Do _NOT_ bfd_release, as that
1507 frees all more recently bfd_alloc'd blocks as
1508 well. */
1509 if (rpath && hash_table->runpath)
1510 hash_table->runpath = NULL;
1512 amt = sizeof (struct bfd_link_needed_list);
1513 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1514 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1515 if (n == NULL || fnm == NULL)
1516 goto error_return;
1517 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1518 if (anm == NULL)
1519 goto error_return;
1520 strcpy (anm, fnm);
1521 n->name = anm;
1522 n->by = abfd;
1523 n->next = NULL;
1524 for (pn = & hash_table->runpath;
1525 *pn != NULL;
1526 pn = &(*pn)->next)
1528 *pn = n;
1529 runpath = 1;
1530 rpath = 0;
1532 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1533 if (!runpath && dyn.d_tag == DT_RPATH)
1535 struct bfd_link_needed_list *n, **pn;
1536 char *fnm, *anm;
1537 unsigned int tagv = dyn.d_un.d_val;
1539 amt = sizeof (struct bfd_link_needed_list);
1540 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1541 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1542 if (n == NULL || fnm == NULL)
1543 goto error_return;
1544 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1545 if (anm == NULL)
1546 goto error_return;
1547 strcpy (anm, fnm);
1548 n->name = anm;
1549 n->by = abfd;
1550 n->next = NULL;
1551 for (pn = & hash_table->runpath;
1552 *pn != NULL;
1553 pn = &(*pn)->next)
1555 *pn = n;
1556 rpath = 1;
1560 free (dynbuf);
1561 dynbuf = NULL;
1564 /* We do not want to include any of the sections in a dynamic
1565 object in the output file. We hack by simply clobbering the
1566 list of sections in the BFD. This could be handled more
1567 cleanly by, say, a new section flag; the existing
1568 SEC_NEVER_LOAD flag is not the one we want, because that one
1569 still implies that the section takes up space in the output
1570 file. */
1571 bfd_section_list_clear (abfd);
1573 /* If this is the first dynamic object found in the link, create
1574 the special sections required for dynamic linking. */
1575 if (! hash_table->dynamic_sections_created)
1576 if (! elf_link_create_dynamic_sections (abfd, info))
1577 goto error_return;
1579 if (add_needed)
1581 /* Add a DT_NEEDED entry for this dynamic object. */
1582 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1583 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1584 if (strindex == (bfd_size_type) -1)
1585 goto error_return;
1587 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1589 asection *sdyn;
1590 Elf_External_Dyn *dyncon, *dynconend;
1592 /* The hash table size did not change, which means that
1593 the dynamic object name was already entered. If we
1594 have already included this dynamic object in the
1595 link, just ignore it. There is no reason to include
1596 a particular dynamic object more than once. */
1597 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1598 BFD_ASSERT (sdyn != NULL);
1600 dyncon = (Elf_External_Dyn *) sdyn->contents;
1601 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1602 sdyn->_raw_size);
1603 for (; dyncon < dynconend; dyncon++)
1605 Elf_Internal_Dyn dyn;
1607 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1608 if (dyn.d_tag == DT_NEEDED
1609 && dyn.d_un.d_val == strindex)
1611 if (buf != NULL)
1612 free (buf);
1613 if (extversym != NULL)
1614 free (extversym);
1615 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1616 return true;
1621 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1622 goto error_return;
1625 /* Save the SONAME, if there is one, because sometimes the
1626 linker emulation code will need to know it. */
1627 if (*name == '\0')
1628 name = basename (bfd_get_filename (abfd));
1629 elf_dt_name (abfd) = name;
1632 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1633 amt = extsymcount * sizeof (Elf_External_Sym);
1634 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1635 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1636 goto error_return;
1638 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1640 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1641 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
1642 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1643 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1644 goto error_return;
1647 weaks = NULL;
1649 ever = extversym != NULL ? extversym + extsymoff : NULL;
1650 esymend = buf + extsymcount;
1651 for (esym = buf, shndx = shndx_buf;
1652 esym < esymend;
1653 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
1654 shndx = (shndx != NULL ? shndx + 1 : NULL))
1656 Elf_Internal_Sym sym;
1657 int bind;
1658 bfd_vma value;
1659 asection *sec;
1660 flagword flags;
1661 const char *name;
1662 struct elf_link_hash_entry *h;
1663 boolean definition;
1664 boolean size_change_ok, type_change_ok;
1665 boolean new_weakdef;
1666 unsigned int old_alignment;
1667 boolean override;
1669 override = false;
1671 elf_swap_symbol_in (abfd, esym, shndx, &sym);
1673 flags = BSF_NO_FLAGS;
1674 sec = NULL;
1675 value = sym.st_value;
1676 *sym_hash = NULL;
1678 bind = ELF_ST_BIND (sym.st_info);
1679 if (bind == STB_LOCAL)
1681 /* This should be impossible, since ELF requires that all
1682 global symbols follow all local symbols, and that sh_info
1683 point to the first global symbol. Unfortunatealy, Irix 5
1684 screws this up. */
1685 continue;
1687 else if (bind == STB_GLOBAL)
1689 if (sym.st_shndx != SHN_UNDEF
1690 && sym.st_shndx != SHN_COMMON)
1691 flags = BSF_GLOBAL;
1693 else if (bind == STB_WEAK)
1694 flags = BSF_WEAK;
1695 else
1697 /* Leave it up to the processor backend. */
1700 if (sym.st_shndx == SHN_UNDEF)
1701 sec = bfd_und_section_ptr;
1702 else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
1704 sec = section_from_elf_index (abfd, sym.st_shndx);
1705 if (sec == NULL)
1706 sec = bfd_abs_section_ptr;
1707 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1708 value -= sec->vma;
1710 else if (sym.st_shndx == SHN_ABS)
1711 sec = bfd_abs_section_ptr;
1712 else if (sym.st_shndx == SHN_COMMON)
1714 sec = bfd_com_section_ptr;
1715 /* What ELF calls the size we call the value. What ELF
1716 calls the value we call the alignment. */
1717 value = sym.st_size;
1719 else
1721 /* Leave it up to the processor backend. */
1724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1725 if (name == (const char *) NULL)
1726 goto error_return;
1728 if (add_symbol_hook)
1730 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1731 &value))
1732 goto error_return;
1734 /* The hook function sets the name to NULL if this symbol
1735 should be skipped for some reason. */
1736 if (name == (const char *) NULL)
1737 continue;
1740 /* Sanity check that all possibilities were handled. */
1741 if (sec == (asection *) NULL)
1743 bfd_set_error (bfd_error_bad_value);
1744 goto error_return;
1747 if (bfd_is_und_section (sec)
1748 || bfd_is_com_section (sec))
1749 definition = false;
1750 else
1751 definition = true;
1753 size_change_ok = false;
1754 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1755 old_alignment = 0;
1756 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1758 Elf_Internal_Versym iver;
1759 unsigned int vernum = 0;
1761 if (ever != NULL)
1763 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1764 vernum = iver.vs_vers & VERSYM_VERSION;
1766 /* If this is a hidden symbol, or if it is not version
1767 1, we append the version name to the symbol name.
1768 However, we do not modify a non-hidden absolute
1769 symbol, because it might be the version symbol
1770 itself. FIXME: What if it isn't? */
1771 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1772 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1774 const char *verstr;
1775 unsigned int namelen;
1776 bfd_size_type newlen;
1777 char *newname, *p;
1779 if (sym.st_shndx != SHN_UNDEF)
1781 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1783 (*_bfd_error_handler)
1784 (_("%s: %s: invalid version %u (max %d)"),
1785 bfd_archive_filename (abfd), name, vernum,
1786 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1787 bfd_set_error (bfd_error_bad_value);
1788 goto error_return;
1790 else if (vernum > 1)
1791 verstr =
1792 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1793 else
1794 verstr = "";
1796 else
1798 /* We cannot simply test for the number of
1799 entries in the VERNEED section since the
1800 numbers for the needed versions do not start
1801 at 0. */
1802 Elf_Internal_Verneed *t;
1804 verstr = NULL;
1805 for (t = elf_tdata (abfd)->verref;
1806 t != NULL;
1807 t = t->vn_nextref)
1809 Elf_Internal_Vernaux *a;
1811 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1813 if (a->vna_other == vernum)
1815 verstr = a->vna_nodename;
1816 break;
1819 if (a != NULL)
1820 break;
1822 if (verstr == NULL)
1824 (*_bfd_error_handler)
1825 (_("%s: %s: invalid needed version %d"),
1826 bfd_archive_filename (abfd), name, vernum);
1827 bfd_set_error (bfd_error_bad_value);
1828 goto error_return;
1832 namelen = strlen (name);
1833 newlen = namelen + strlen (verstr) + 2;
1834 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1835 ++newlen;
1837 newname = (char *) bfd_alloc (abfd, newlen);
1838 if (newname == NULL)
1839 goto error_return;
1840 strcpy (newname, name);
1841 p = newname + namelen;
1842 *p++ = ELF_VER_CHR;
1843 /* If this is a defined non-hidden version symbol,
1844 we add another @ to the name. This indicates the
1845 default version of the symbol. */
1846 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1847 && sym.st_shndx != SHN_UNDEF)
1848 *p++ = ELF_VER_CHR;
1849 strcpy (p, verstr);
1851 name = newname;
1855 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1856 sym_hash, &override, &type_change_ok,
1857 &size_change_ok, dt_needed))
1858 goto error_return;
1860 if (override)
1861 definition = false;
1863 h = *sym_hash;
1864 while (h->root.type == bfd_link_hash_indirect
1865 || h->root.type == bfd_link_hash_warning)
1866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1868 /* Remember the old alignment if this is a common symbol, so
1869 that we don't reduce the alignment later on. We can't
1870 check later, because _bfd_generic_link_add_one_symbol
1871 will set a default for the alignment which we want to
1872 override. */
1873 if (h->root.type == bfd_link_hash_common)
1874 old_alignment = h->root.u.c.p->alignment_power;
1876 if (elf_tdata (abfd)->verdef != NULL
1877 && ! override
1878 && vernum > 1
1879 && definition)
1880 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1883 if (! (_bfd_generic_link_add_one_symbol
1884 (info, abfd, name, flags, sec, value, (const char *) NULL,
1885 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1886 goto error_return;
1888 h = *sym_hash;
1889 while (h->root.type == bfd_link_hash_indirect
1890 || h->root.type == bfd_link_hash_warning)
1891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1892 *sym_hash = h;
1894 new_weakdef = false;
1895 if (dynamic
1896 && definition
1897 && (flags & BSF_WEAK) != 0
1898 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1899 && info->hash->creator->flavour == bfd_target_elf_flavour
1900 && h->weakdef == NULL)
1902 /* Keep a list of all weak defined non function symbols from
1903 a dynamic object, using the weakdef field. Later in this
1904 function we will set the weakdef field to the correct
1905 value. We only put non-function symbols from dynamic
1906 objects on this list, because that happens to be the only
1907 time we need to know the normal symbol corresponding to a
1908 weak symbol, and the information is time consuming to
1909 figure out. If the weakdef field is not already NULL,
1910 then this symbol was already defined by some previous
1911 dynamic object, and we will be using that previous
1912 definition anyhow. */
1914 h->weakdef = weaks;
1915 weaks = h;
1916 new_weakdef = true;
1919 /* Set the alignment of a common symbol. */
1920 if (sym.st_shndx == SHN_COMMON
1921 && h->root.type == bfd_link_hash_common)
1923 unsigned int align;
1925 align = bfd_log2 (sym.st_value);
1926 if (align > old_alignment
1927 /* Permit an alignment power of zero if an alignment of one
1928 is specified and no other alignments have been specified. */
1929 || (sym.st_value == 1 && old_alignment == 0))
1930 h->root.u.c.p->alignment_power = align;
1933 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1935 int old_flags;
1936 boolean dynsym;
1937 int new_flag;
1939 /* Remember the symbol size and type. */
1940 if (sym.st_size != 0
1941 && (definition || h->size == 0))
1943 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1944 (*_bfd_error_handler)
1945 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1946 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1947 bfd_archive_filename (abfd));
1949 h->size = sym.st_size;
1952 /* If this is a common symbol, then we always want H->SIZE
1953 to be the size of the common symbol. The code just above
1954 won't fix the size if a common symbol becomes larger. We
1955 don't warn about a size change here, because that is
1956 covered by --warn-common. */
1957 if (h->root.type == bfd_link_hash_common)
1958 h->size = h->root.u.c.size;
1960 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1961 && (definition || h->type == STT_NOTYPE))
1963 if (h->type != STT_NOTYPE
1964 && h->type != ELF_ST_TYPE (sym.st_info)
1965 && ! type_change_ok)
1966 (*_bfd_error_handler)
1967 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1968 name, h->type, ELF_ST_TYPE (sym.st_info),
1969 bfd_archive_filename (abfd));
1971 h->type = ELF_ST_TYPE (sym.st_info);
1974 /* If st_other has a processor-specific meaning, specific code
1975 might be needed here. */
1976 if (sym.st_other != 0)
1978 /* Combine visibilities, using the most constraining one. */
1979 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1980 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1982 if (symvis && (hvis > symvis || hvis == 0))
1983 h->other = sym.st_other;
1985 /* If neither has visibility, use the st_other of the
1986 definition. This is an arbitrary choice, since the
1987 other bits have no general meaning. */
1988 if (!symvis && !hvis
1989 && (definition || h->other == 0))
1990 h->other = sym.st_other;
1993 /* Set a flag in the hash table entry indicating the type of
1994 reference or definition we just found. Keep a count of
1995 the number of dynamic symbols we find. A dynamic symbol
1996 is one which is referenced or defined by both a regular
1997 object and a shared object. */
1998 old_flags = h->elf_link_hash_flags;
1999 dynsym = false;
2000 if (! dynamic)
2002 if (! definition)
2004 new_flag = ELF_LINK_HASH_REF_REGULAR;
2005 if (bind != STB_WEAK)
2006 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2008 else
2009 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2010 if (info->shared
2011 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2012 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2013 dynsym = true;
2015 else
2017 if (! definition)
2018 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2019 else
2020 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2021 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2022 | ELF_LINK_HASH_REF_REGULAR)) != 0
2023 || (h->weakdef != NULL
2024 && ! new_weakdef
2025 && h->weakdef->dynindx != -1))
2026 dynsym = true;
2029 h->elf_link_hash_flags |= new_flag;
2031 /* Check to see if we need to add an indirect symbol for
2032 the default name. */
2033 if (definition || h->root.type == bfd_link_hash_common)
2034 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2035 &sec, &value, &dynsym,
2036 override, dt_needed))
2037 goto error_return;
2039 if (dynsym && h->dynindx == -1)
2041 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2042 goto error_return;
2043 if (h->weakdef != NULL
2044 && ! new_weakdef
2045 && h->weakdef->dynindx == -1)
2047 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2048 goto error_return;
2051 else if (dynsym && h->dynindx != -1)
2052 /* If the symbol already has a dynamic index, but
2053 visibility says it should not be visible, turn it into
2054 a local symbol. */
2055 switch (ELF_ST_VISIBILITY (h->other))
2057 case STV_INTERNAL:
2058 case STV_HIDDEN:
2059 (*bed->elf_backend_hide_symbol) (info, h, true);
2060 break;
2063 if (dt_needed && definition
2064 && (h->elf_link_hash_flags
2065 & ELF_LINK_HASH_REF_REGULAR) != 0)
2067 bfd_size_type oldsize;
2068 bfd_size_type strindex;
2070 if (! is_elf_hash_table (info))
2071 goto error_return;
2073 /* The symbol from a DT_NEEDED object is referenced from
2074 the regular object to create a dynamic executable. We
2075 have to make sure there is a DT_NEEDED entry for it. */
2077 dt_needed = false;
2078 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2079 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2080 elf_dt_soname (abfd), false);
2081 if (strindex == (bfd_size_type) -1)
2082 goto error_return;
2084 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2086 asection *sdyn;
2087 Elf_External_Dyn *dyncon, *dynconend;
2089 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2090 ".dynamic");
2091 BFD_ASSERT (sdyn != NULL);
2093 dyncon = (Elf_External_Dyn *) sdyn->contents;
2094 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2095 sdyn->_raw_size);
2096 for (; dyncon < dynconend; dyncon++)
2098 Elf_Internal_Dyn dyn;
2100 elf_swap_dyn_in (hash_table->dynobj,
2101 dyncon, &dyn);
2102 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2103 dyn.d_un.d_val != strindex);
2107 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2108 goto error_return;
2113 /* Now set the weakdefs field correctly for all the weak defined
2114 symbols we found. The only way to do this is to search all the
2115 symbols. Since we only need the information for non functions in
2116 dynamic objects, that's the only time we actually put anything on
2117 the list WEAKS. We need this information so that if a regular
2118 object refers to a symbol defined weakly in a dynamic object, the
2119 real symbol in the dynamic object is also put in the dynamic
2120 symbols; we also must arrange for both symbols to point to the
2121 same memory location. We could handle the general case of symbol
2122 aliasing, but a general symbol alias can only be generated in
2123 assembler code, handling it correctly would be very time
2124 consuming, and other ELF linkers don't handle general aliasing
2125 either. */
2126 while (weaks != NULL)
2128 struct elf_link_hash_entry *hlook;
2129 asection *slook;
2130 bfd_vma vlook;
2131 struct elf_link_hash_entry **hpp;
2132 struct elf_link_hash_entry **hppend;
2134 hlook = weaks;
2135 weaks = hlook->weakdef;
2136 hlook->weakdef = NULL;
2138 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2139 || hlook->root.type == bfd_link_hash_defweak
2140 || hlook->root.type == bfd_link_hash_common
2141 || hlook->root.type == bfd_link_hash_indirect);
2142 slook = hlook->root.u.def.section;
2143 vlook = hlook->root.u.def.value;
2145 hpp = elf_sym_hashes (abfd);
2146 hppend = hpp + extsymcount;
2147 for (; hpp < hppend; hpp++)
2149 struct elf_link_hash_entry *h;
2151 h = *hpp;
2152 if (h != NULL && h != hlook
2153 && h->root.type == bfd_link_hash_defined
2154 && h->root.u.def.section == slook
2155 && h->root.u.def.value == vlook)
2157 hlook->weakdef = h;
2159 /* If the weak definition is in the list of dynamic
2160 symbols, make sure the real definition is put there
2161 as well. */
2162 if (hlook->dynindx != -1
2163 && h->dynindx == -1)
2165 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2166 goto error_return;
2169 /* If the real definition is in the list of dynamic
2170 symbols, make sure the weak definition is put there
2171 as well. If we don't do this, then the dynamic
2172 loader might not merge the entries for the real
2173 definition and the weak definition. */
2174 if (h->dynindx != -1
2175 && hlook->dynindx == -1)
2177 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2178 goto error_return;
2181 break;
2186 if (buf != NULL)
2188 free (buf);
2189 buf = NULL;
2192 if (extversym != NULL)
2194 free (extversym);
2195 extversym = NULL;
2198 /* If this object is the same format as the output object, and it is
2199 not a shared library, then let the backend look through the
2200 relocs.
2202 This is required to build global offset table entries and to
2203 arrange for dynamic relocs. It is not required for the
2204 particular common case of linking non PIC code, even when linking
2205 against shared libraries, but unfortunately there is no way of
2206 knowing whether an object file has been compiled PIC or not.
2207 Looking through the relocs is not particularly time consuming.
2208 The problem is that we must either (1) keep the relocs in memory,
2209 which causes the linker to require additional runtime memory or
2210 (2) read the relocs twice from the input file, which wastes time.
2211 This would be a good case for using mmap.
2213 I have no idea how to handle linking PIC code into a file of a
2214 different format. It probably can't be done. */
2215 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2216 if (! dynamic
2217 && abfd->xvec == info->hash->creator
2218 && check_relocs != NULL)
2220 asection *o;
2222 for (o = abfd->sections; o != NULL; o = o->next)
2224 Elf_Internal_Rela *internal_relocs;
2225 boolean ok;
2227 if ((o->flags & SEC_RELOC) == 0
2228 || o->reloc_count == 0
2229 || ((info->strip == strip_all || info->strip == strip_debugger)
2230 && (o->flags & SEC_DEBUGGING) != 0)
2231 || bfd_is_abs_section (o->output_section))
2232 continue;
2234 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2235 (abfd, o, (PTR) NULL,
2236 (Elf_Internal_Rela *) NULL,
2237 info->keep_memory));
2238 if (internal_relocs == NULL)
2239 goto error_return;
2241 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2243 if (! info->keep_memory)
2244 free (internal_relocs);
2246 if (! ok)
2247 goto error_return;
2251 /* If this is a non-traditional, non-relocateable link, try to
2252 optimize the handling of the .stab/.stabstr sections. */
2253 if (! dynamic
2254 && ! info->relocateable
2255 && ! info->traditional_format
2256 && info->hash->creator->flavour == bfd_target_elf_flavour
2257 && is_elf_hash_table (info)
2258 && (info->strip != strip_all && info->strip != strip_debugger))
2260 asection *stab, *stabstr;
2262 stab = bfd_get_section_by_name (abfd, ".stab");
2263 if (stab != NULL && !(stab->flags & SEC_MERGE))
2265 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2267 if (stabstr != NULL)
2269 struct bfd_elf_section_data *secdata;
2271 secdata = elf_section_data (stab);
2272 if (! _bfd_link_section_stabs (abfd,
2273 & hash_table->stab_info,
2274 stab, stabstr,
2275 &secdata->sec_info))
2276 goto error_return;
2277 if (secdata->sec_info)
2278 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2283 if (! info->relocateable && ! dynamic
2284 && is_elf_hash_table (info))
2286 asection *s;
2288 for (s = abfd->sections; s != NULL; s = s->next)
2289 if (s->flags & SEC_MERGE)
2291 struct bfd_elf_section_data *secdata;
2293 secdata = elf_section_data (s);
2294 if (! _bfd_merge_section (abfd,
2295 & hash_table->merge_info,
2296 s, &secdata->sec_info))
2297 goto error_return;
2298 else if (secdata->sec_info)
2299 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2303 return true;
2305 error_return:
2306 if (buf != NULL)
2307 free (buf);
2308 if (dynbuf != NULL)
2309 free (dynbuf);
2310 if (extversym != NULL)
2311 free (extversym);
2312 return false;
2315 /* Create some sections which will be filled in with dynamic linking
2316 information. ABFD is an input file which requires dynamic sections
2317 to be created. The dynamic sections take up virtual memory space
2318 when the final executable is run, so we need to create them before
2319 addresses are assigned to the output sections. We work out the
2320 actual contents and size of these sections later. */
2322 boolean
2323 elf_link_create_dynamic_sections (abfd, info)
2324 bfd *abfd;
2325 struct bfd_link_info *info;
2327 flagword flags;
2328 register asection *s;
2329 struct elf_link_hash_entry *h;
2330 struct elf_backend_data *bed;
2332 if (! is_elf_hash_table (info))
2333 return false;
2335 if (elf_hash_table (info)->dynamic_sections_created)
2336 return true;
2338 /* Make sure that all dynamic sections use the same input BFD. */
2339 if (elf_hash_table (info)->dynobj == NULL)
2340 elf_hash_table (info)->dynobj = abfd;
2341 else
2342 abfd = elf_hash_table (info)->dynobj;
2344 /* Note that we set the SEC_IN_MEMORY flag for all of these
2345 sections. */
2346 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2347 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2349 /* A dynamically linked executable has a .interp section, but a
2350 shared library does not. */
2351 if (! info->shared)
2353 s = bfd_make_section (abfd, ".interp");
2354 if (s == NULL
2355 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2356 return false;
2359 if (! info->traditional_format
2360 && info->hash->creator->flavour == bfd_target_elf_flavour)
2362 s = bfd_make_section (abfd, ".eh_frame_hdr");
2363 if (s == NULL
2364 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2365 || ! bfd_set_section_alignment (abfd, s, 2))
2366 return false;
2369 /* Create sections to hold version informations. These are removed
2370 if they are not needed. */
2371 s = bfd_make_section (abfd, ".gnu.version_d");
2372 if (s == NULL
2373 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2374 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2375 return false;
2377 s = bfd_make_section (abfd, ".gnu.version");
2378 if (s == NULL
2379 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2380 || ! bfd_set_section_alignment (abfd, s, 1))
2381 return false;
2383 s = bfd_make_section (abfd, ".gnu.version_r");
2384 if (s == NULL
2385 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2386 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2387 return false;
2389 s = bfd_make_section (abfd, ".dynsym");
2390 if (s == NULL
2391 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2392 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2393 return false;
2395 s = bfd_make_section (abfd, ".dynstr");
2396 if (s == NULL
2397 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2398 return false;
2400 /* Create a strtab to hold the dynamic symbol names. */
2401 if (elf_hash_table (info)->dynstr == NULL)
2403 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2404 if (elf_hash_table (info)->dynstr == NULL)
2405 return false;
2408 s = bfd_make_section (abfd, ".dynamic");
2409 if (s == NULL
2410 || ! bfd_set_section_flags (abfd, s, flags)
2411 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2412 return false;
2414 /* The special symbol _DYNAMIC is always set to the start of the
2415 .dynamic section. This call occurs before we have processed the
2416 symbols for any dynamic object, so we don't have to worry about
2417 overriding a dynamic definition. We could set _DYNAMIC in a
2418 linker script, but we only want to define it if we are, in fact,
2419 creating a .dynamic section. We don't want to define it if there
2420 is no .dynamic section, since on some ELF platforms the start up
2421 code examines it to decide how to initialize the process. */
2422 h = NULL;
2423 if (! (_bfd_generic_link_add_one_symbol
2424 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2425 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2426 (struct bfd_link_hash_entry **) &h)))
2427 return false;
2428 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2429 h->type = STT_OBJECT;
2431 if (info->shared
2432 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2433 return false;
2435 bed = get_elf_backend_data (abfd);
2437 s = bfd_make_section (abfd, ".hash");
2438 if (s == NULL
2439 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2440 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2441 return false;
2442 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2444 /* Let the backend create the rest of the sections. This lets the
2445 backend set the right flags. The backend will normally create
2446 the .got and .plt sections. */
2447 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2448 return false;
2450 elf_hash_table (info)->dynamic_sections_created = true;
2452 return true;
2455 /* Add an entry to the .dynamic table. */
2457 boolean
2458 elf_add_dynamic_entry (info, tag, val)
2459 struct bfd_link_info *info;
2460 bfd_vma tag;
2461 bfd_vma val;
2463 Elf_Internal_Dyn dyn;
2464 bfd *dynobj;
2465 asection *s;
2466 bfd_size_type newsize;
2467 bfd_byte *newcontents;
2469 if (! is_elf_hash_table (info))
2470 return false;
2472 dynobj = elf_hash_table (info)->dynobj;
2474 s = bfd_get_section_by_name (dynobj, ".dynamic");
2475 BFD_ASSERT (s != NULL);
2477 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2478 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2479 if (newcontents == NULL)
2480 return false;
2482 dyn.d_tag = tag;
2483 dyn.d_un.d_val = val;
2484 elf_swap_dyn_out (dynobj, &dyn,
2485 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2487 s->_raw_size = newsize;
2488 s->contents = newcontents;
2490 return true;
2493 /* Record a new local dynamic symbol. */
2495 boolean
2496 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2497 struct bfd_link_info *info;
2498 bfd *input_bfd;
2499 long input_indx;
2501 struct elf_link_local_dynamic_entry *entry;
2502 struct elf_link_hash_table *eht;
2503 struct elf_strtab_hash *dynstr;
2504 Elf_External_Sym esym;
2505 Elf_External_Sym_Shndx eshndx;
2506 Elf_External_Sym_Shndx *shndx;
2507 unsigned long dynstr_index;
2508 char *name;
2509 file_ptr pos;
2510 bfd_size_type amt;
2512 if (! is_elf_hash_table (info))
2513 return false;
2515 /* See if the entry exists already. */
2516 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2517 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2518 return true;
2520 entry = (struct elf_link_local_dynamic_entry *)
2521 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2522 if (entry == NULL)
2523 return false;
2525 /* Go find the symbol, so that we can find it's name. */
2526 amt = sizeof (Elf_External_Sym);
2527 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2528 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2529 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2530 return false;
2531 shndx = NULL;
2532 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2534 amt = sizeof (Elf_External_Sym_Shndx);
2535 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2536 pos += input_indx * amt;
2537 shndx = &eshndx;
2538 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2539 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2540 return false;
2542 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
2544 name = (bfd_elf_string_from_elf_section
2545 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2546 entry->isym.st_name));
2548 dynstr = elf_hash_table (info)->dynstr;
2549 if (dynstr == NULL)
2551 /* Create a strtab to hold the dynamic symbol names. */
2552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2553 if (dynstr == NULL)
2554 return false;
2557 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2558 if (dynstr_index == (unsigned long) -1)
2559 return false;
2560 entry->isym.st_name = dynstr_index;
2562 eht = elf_hash_table (info);
2564 entry->next = eht->dynlocal;
2565 eht->dynlocal = entry;
2566 entry->input_bfd = input_bfd;
2567 entry->input_indx = input_indx;
2568 eht->dynsymcount++;
2570 /* Whatever binding the symbol had before, it's now local. */
2571 entry->isym.st_info
2572 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2574 /* The dynindx will be set at the end of size_dynamic_sections. */
2576 return true;
2579 /* Read and swap the relocs from the section indicated by SHDR. This
2580 may be either a REL or a RELA section. The relocations are
2581 translated into RELA relocations and stored in INTERNAL_RELOCS,
2582 which should have already been allocated to contain enough space.
2583 The EXTERNAL_RELOCS are a buffer where the external form of the
2584 relocations should be stored.
2586 Returns false if something goes wrong. */
2588 static boolean
2589 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2590 internal_relocs)
2591 bfd *abfd;
2592 Elf_Internal_Shdr *shdr;
2593 PTR external_relocs;
2594 Elf_Internal_Rela *internal_relocs;
2596 struct elf_backend_data *bed;
2597 bfd_size_type amt;
2599 /* If there aren't any relocations, that's OK. */
2600 if (!shdr)
2601 return true;
2603 /* Position ourselves at the start of the section. */
2604 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2605 return false;
2607 /* Read the relocations. */
2608 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2609 return false;
2611 bed = get_elf_backend_data (abfd);
2613 /* Convert the external relocations to the internal format. */
2614 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2616 Elf_External_Rel *erel;
2617 Elf_External_Rel *erelend;
2618 Elf_Internal_Rela *irela;
2619 Elf_Internal_Rel *irel;
2621 erel = (Elf_External_Rel *) external_relocs;
2622 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2623 irela = internal_relocs;
2624 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2625 irel = bfd_alloc (abfd, amt);
2626 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2628 unsigned int i;
2630 if (bed->s->swap_reloc_in)
2631 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2632 else
2633 elf_swap_reloc_in (abfd, erel, irel);
2635 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2637 irela[i].r_offset = irel[i].r_offset;
2638 irela[i].r_info = irel[i].r_info;
2639 irela[i].r_addend = 0;
2643 else
2645 Elf_External_Rela *erela;
2646 Elf_External_Rela *erelaend;
2647 Elf_Internal_Rela *irela;
2649 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2651 erela = (Elf_External_Rela *) external_relocs;
2652 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2653 irela = internal_relocs;
2654 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2656 if (bed->s->swap_reloca_in)
2657 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2658 else
2659 elf_swap_reloca_in (abfd, erela, irela);
2663 return true;
2666 /* Read and swap the relocs for a section O. They may have been
2667 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2668 not NULL, they are used as buffers to read into. They are known to
2669 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2670 the return value is allocated using either malloc or bfd_alloc,
2671 according to the KEEP_MEMORY argument. If O has two relocation
2672 sections (both REL and RELA relocations), then the REL_HDR
2673 relocations will appear first in INTERNAL_RELOCS, followed by the
2674 REL_HDR2 relocations. */
2676 Elf_Internal_Rela *
2677 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2678 keep_memory)
2679 bfd *abfd;
2680 asection *o;
2681 PTR external_relocs;
2682 Elf_Internal_Rela *internal_relocs;
2683 boolean keep_memory;
2685 Elf_Internal_Shdr *rel_hdr;
2686 PTR alloc1 = NULL;
2687 Elf_Internal_Rela *alloc2 = NULL;
2688 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2690 if (elf_section_data (o)->relocs != NULL)
2691 return elf_section_data (o)->relocs;
2693 if (o->reloc_count == 0)
2694 return NULL;
2696 rel_hdr = &elf_section_data (o)->rel_hdr;
2698 if (internal_relocs == NULL)
2700 bfd_size_type size;
2702 size = o->reloc_count;
2703 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2704 if (keep_memory)
2705 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2706 else
2707 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2708 if (internal_relocs == NULL)
2709 goto error_return;
2712 if (external_relocs == NULL)
2714 bfd_size_type size = rel_hdr->sh_size;
2716 if (elf_section_data (o)->rel_hdr2)
2717 size += elf_section_data (o)->rel_hdr2->sh_size;
2718 alloc1 = (PTR) bfd_malloc (size);
2719 if (alloc1 == NULL)
2720 goto error_return;
2721 external_relocs = alloc1;
2724 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2725 external_relocs,
2726 internal_relocs))
2727 goto error_return;
2728 if (!elf_link_read_relocs_from_section
2729 (abfd,
2730 elf_section_data (o)->rel_hdr2,
2731 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2732 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2733 * bed->s->int_rels_per_ext_rel)))
2734 goto error_return;
2736 /* Cache the results for next time, if we can. */
2737 if (keep_memory)
2738 elf_section_data (o)->relocs = internal_relocs;
2740 if (alloc1 != NULL)
2741 free (alloc1);
2743 /* Don't free alloc2, since if it was allocated we are passing it
2744 back (under the name of internal_relocs). */
2746 return internal_relocs;
2748 error_return:
2749 if (alloc1 != NULL)
2750 free (alloc1);
2751 if (alloc2 != NULL)
2752 free (alloc2);
2753 return NULL;
2756 /* Record an assignment to a symbol made by a linker script. We need
2757 this in case some dynamic object refers to this symbol. */
2759 boolean
2760 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2761 bfd *output_bfd ATTRIBUTE_UNUSED;
2762 struct bfd_link_info *info;
2763 const char *name;
2764 boolean provide;
2766 struct elf_link_hash_entry *h;
2768 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2769 return true;
2771 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2772 if (h == NULL)
2773 return false;
2775 if (h->root.type == bfd_link_hash_new)
2776 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2778 /* If this symbol is being provided by the linker script, and it is
2779 currently defined by a dynamic object, but not by a regular
2780 object, then mark it as undefined so that the generic linker will
2781 force the correct value. */
2782 if (provide
2783 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2784 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2785 h->root.type = bfd_link_hash_undefined;
2787 /* If this symbol is not being provided by the linker script, and it is
2788 currently defined by a dynamic object, but not by a regular object,
2789 then clear out any version information because the symbol will not be
2790 associated with the dynamic object any more. */
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->verinfo.verdef = NULL;
2796 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2798 /* When possible, keep the original type of the symbol. */
2799 if (h->type == STT_NOTYPE)
2800 h->type = STT_OBJECT;
2802 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2803 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2804 || info->shared)
2805 && h->dynindx == -1)
2807 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2808 return false;
2810 /* If this is a weak defined symbol, and we know a corresponding
2811 real symbol from the same dynamic object, make sure the real
2812 symbol is also made into a dynamic symbol. */
2813 if (h->weakdef != NULL
2814 && h->weakdef->dynindx == -1)
2816 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2817 return false;
2821 return true;
2824 /* This structure is used to pass information to
2825 elf_link_assign_sym_version. */
2827 struct elf_assign_sym_version_info
2829 /* Output BFD. */
2830 bfd *output_bfd;
2831 /* General link information. */
2832 struct bfd_link_info *info;
2833 /* Version tree. */
2834 struct bfd_elf_version_tree *verdefs;
2835 /* Whether we had a failure. */
2836 boolean failed;
2839 /* This structure is used to pass information to
2840 elf_link_find_version_dependencies. */
2842 struct elf_find_verdep_info
2844 /* Output BFD. */
2845 bfd *output_bfd;
2846 /* General link information. */
2847 struct bfd_link_info *info;
2848 /* The number of dependencies. */
2849 unsigned int vers;
2850 /* Whether we had a failure. */
2851 boolean failed;
2854 /* Array used to determine the number of hash table buckets to use
2855 based on the number of symbols there are. If there are fewer than
2856 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2857 fewer than 37 we use 17 buckets, and so forth. We never use more
2858 than 32771 buckets. */
2860 static const size_t elf_buckets[] =
2862 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2863 16411, 32771, 0
2866 /* Compute bucket count for hashing table. We do not use a static set
2867 of possible tables sizes anymore. Instead we determine for all
2868 possible reasonable sizes of the table the outcome (i.e., the
2869 number of collisions etc) and choose the best solution. The
2870 weighting functions are not too simple to allow the table to grow
2871 without bounds. Instead one of the weighting factors is the size.
2872 Therefore the result is always a good payoff between few collisions
2873 (= short chain lengths) and table size. */
2874 static size_t
2875 compute_bucket_count (info)
2876 struct bfd_link_info *info;
2878 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2879 size_t best_size = 0;
2880 unsigned long int *hashcodes;
2881 unsigned long int *hashcodesp;
2882 unsigned long int i;
2883 bfd_size_type amt;
2885 /* Compute the hash values for all exported symbols. At the same
2886 time store the values in an array so that we could use them for
2887 optimizations. */
2888 amt = dynsymcount;
2889 amt *= sizeof (unsigned long int);
2890 hashcodes = (unsigned long int *) bfd_malloc (amt);
2891 if (hashcodes == NULL)
2892 return 0;
2893 hashcodesp = hashcodes;
2895 /* Put all hash values in HASHCODES. */
2896 elf_link_hash_traverse (elf_hash_table (info),
2897 elf_collect_hash_codes, &hashcodesp);
2899 /* We have a problem here. The following code to optimize the table
2900 size requires an integer type with more the 32 bits. If
2901 BFD_HOST_U_64_BIT is set we know about such a type. */
2902 #ifdef BFD_HOST_U_64_BIT
2903 if (info->optimize == true)
2905 unsigned long int nsyms = hashcodesp - hashcodes;
2906 size_t minsize;
2907 size_t maxsize;
2908 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2909 unsigned long int *counts ;
2911 /* Possible optimization parameters: if we have NSYMS symbols we say
2912 that the hashing table must at least have NSYMS/4 and at most
2913 2*NSYMS buckets. */
2914 minsize = nsyms / 4;
2915 if (minsize == 0)
2916 minsize = 1;
2917 best_size = maxsize = nsyms * 2;
2919 /* Create array where we count the collisions in. We must use bfd_malloc
2920 since the size could be large. */
2921 amt = maxsize;
2922 amt *= sizeof (unsigned long int);
2923 counts = (unsigned long int *) bfd_malloc (amt);
2924 if (counts == NULL)
2926 free (hashcodes);
2927 return 0;
2930 /* Compute the "optimal" size for the hash table. The criteria is a
2931 minimal chain length. The minor criteria is (of course) the size
2932 of the table. */
2933 for (i = minsize; i < maxsize; ++i)
2935 /* Walk through the array of hashcodes and count the collisions. */
2936 BFD_HOST_U_64_BIT max;
2937 unsigned long int j;
2938 unsigned long int fact;
2940 memset (counts, '\0', i * sizeof (unsigned long int));
2942 /* Determine how often each hash bucket is used. */
2943 for (j = 0; j < nsyms; ++j)
2944 ++counts[hashcodes[j] % i];
2946 /* For the weight function we need some information about the
2947 pagesize on the target. This is information need not be 100%
2948 accurate. Since this information is not available (so far) we
2949 define it here to a reasonable default value. If it is crucial
2950 to have a better value some day simply define this value. */
2951 # ifndef BFD_TARGET_PAGESIZE
2952 # define BFD_TARGET_PAGESIZE (4096)
2953 # endif
2955 /* We in any case need 2 + NSYMS entries for the size values and
2956 the chains. */
2957 max = (2 + nsyms) * (ARCH_SIZE / 8);
2959 # if 1
2960 /* Variant 1: optimize for short chains. We add the squares
2961 of all the chain lengths (which favous many small chain
2962 over a few long chains). */
2963 for (j = 0; j < i; ++j)
2964 max += counts[j] * counts[j];
2966 /* This adds penalties for the overall size of the table. */
2967 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2968 max *= fact * fact;
2969 # else
2970 /* Variant 2: Optimize a lot more for small table. Here we
2971 also add squares of the size but we also add penalties for
2972 empty slots (the +1 term). */
2973 for (j = 0; j < i; ++j)
2974 max += (1 + counts[j]) * (1 + counts[j]);
2976 /* The overall size of the table is considered, but not as
2977 strong as in variant 1, where it is squared. */
2978 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2979 max *= fact;
2980 # endif
2982 /* Compare with current best results. */
2983 if (max < best_chlen)
2985 best_chlen = max;
2986 best_size = i;
2990 free (counts);
2992 else
2993 #endif /* defined (BFD_HOST_U_64_BIT) */
2995 /* This is the fallback solution if no 64bit type is available or if we
2996 are not supposed to spend much time on optimizations. We select the
2997 bucket count using a fixed set of numbers. */
2998 for (i = 0; elf_buckets[i] != 0; i++)
3000 best_size = elf_buckets[i];
3001 if (dynsymcount < elf_buckets[i + 1])
3002 break;
3006 /* Free the arrays we needed. */
3007 free (hashcodes);
3009 return best_size;
3012 /* Set up the sizes and contents of the ELF dynamic sections. This is
3013 called by the ELF linker emulation before_allocation routine. We
3014 must set the sizes of the sections before the linker sets the
3015 addresses of the various sections. */
3017 boolean
3018 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3019 filter_shlib,
3020 auxiliary_filters, info, sinterpptr,
3021 verdefs)
3022 bfd *output_bfd;
3023 const char *soname;
3024 const char *rpath;
3025 const char *filter_shlib;
3026 const char * const *auxiliary_filters;
3027 struct bfd_link_info *info;
3028 asection **sinterpptr;
3029 struct bfd_elf_version_tree *verdefs;
3031 bfd_size_type soname_indx;
3032 bfd *dynobj;
3033 struct elf_backend_data *bed;
3034 struct elf_assign_sym_version_info asvinfo;
3036 *sinterpptr = NULL;
3038 soname_indx = (bfd_size_type) -1;
3040 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3041 return true;
3043 if (! is_elf_hash_table (info))
3044 return false;
3046 /* Any syms created from now on start with -1 in
3047 got.refcount/offset and plt.refcount/offset. */
3048 elf_hash_table (info)->init_refcount = -1;
3050 /* The backend may have to create some sections regardless of whether
3051 we're dynamic or not. */
3052 bed = get_elf_backend_data (output_bfd);
3053 if (bed->elf_backend_always_size_sections
3054 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3055 return false;
3057 dynobj = elf_hash_table (info)->dynobj;
3059 /* If there were no dynamic objects in the link, there is nothing to
3060 do here. */
3061 if (dynobj == NULL)
3062 return true;
3064 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3065 return false;
3067 if (elf_hash_table (info)->dynamic_sections_created)
3069 struct elf_info_failed eif;
3070 struct elf_link_hash_entry *h;
3071 asection *dynstr;
3073 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3074 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3076 if (soname != NULL)
3078 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3079 soname, true);
3080 if (soname_indx == (bfd_size_type) -1
3081 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3082 soname_indx))
3083 return false;
3086 if (info->symbolic)
3088 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3089 (bfd_vma) 0))
3090 return false;
3091 info->flags |= DF_SYMBOLIC;
3094 if (rpath != NULL)
3096 bfd_size_type indx;
3098 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3099 true);
3100 if (info->new_dtags)
3101 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3102 if (indx == (bfd_size_type) -1
3103 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3104 || (info->new_dtags
3105 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3106 indx)))
3107 return false;
3110 if (filter_shlib != NULL)
3112 bfd_size_type indx;
3114 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3115 filter_shlib, true);
3116 if (indx == (bfd_size_type) -1
3117 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3118 return false;
3121 if (auxiliary_filters != NULL)
3123 const char * const *p;
3125 for (p = auxiliary_filters; *p != NULL; p++)
3127 bfd_size_type indx;
3129 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3130 *p, true);
3131 if (indx == (bfd_size_type) -1
3132 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3133 indx))
3134 return false;
3138 eif.info = info;
3139 eif.verdefs = verdefs;
3140 eif.failed = false;
3142 /* If we are supposed to export all symbols into the dynamic symbol
3143 table (this is not the normal case), then do so. */
3144 if (info->export_dynamic)
3146 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3147 (PTR) &eif);
3148 if (eif.failed)
3149 return false;
3152 /* Attach all the symbols to their version information. */
3153 asvinfo.output_bfd = output_bfd;
3154 asvinfo.info = info;
3155 asvinfo.verdefs = verdefs;
3156 asvinfo.failed = false;
3158 elf_link_hash_traverse (elf_hash_table (info),
3159 elf_link_assign_sym_version,
3160 (PTR) &asvinfo);
3161 if (asvinfo.failed)
3162 return false;
3164 /* Find all symbols which were defined in a dynamic object and make
3165 the backend pick a reasonable value for them. */
3166 elf_link_hash_traverse (elf_hash_table (info),
3167 elf_adjust_dynamic_symbol,
3168 (PTR) &eif);
3169 if (eif.failed)
3170 return false;
3172 /* Add some entries to the .dynamic section. We fill in some of the
3173 values later, in elf_bfd_final_link, but we must add the entries
3174 now so that we know the final size of the .dynamic section. */
3176 /* If there are initialization and/or finalization functions to
3177 call then add the corresponding DT_INIT/DT_FINI entries. */
3178 h = (info->init_function
3179 ? elf_link_hash_lookup (elf_hash_table (info),
3180 info->init_function, false,
3181 false, false)
3182 : NULL);
3183 if (h != NULL
3184 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3185 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3187 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3188 return false;
3190 h = (info->fini_function
3191 ? elf_link_hash_lookup (elf_hash_table (info),
3192 info->fini_function, false,
3193 false, false)
3194 : NULL);
3195 if (h != NULL
3196 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3197 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3199 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3200 return false;
3203 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3204 /* If .dynstr is excluded from the link, we don't want any of
3205 these tags. Strictly, we should be checking each section
3206 individually; This quick check covers for the case where
3207 someone does a /DISCARD/ : { *(*) }. */
3208 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3210 bfd_size_type strsize;
3212 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3213 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3214 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3215 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3216 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3217 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3218 (bfd_vma) sizeof (Elf_External_Sym)))
3219 return false;
3223 /* The backend must work out the sizes of all the other dynamic
3224 sections. */
3225 if (bed->elf_backend_size_dynamic_sections
3226 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3227 return false;
3229 if (elf_hash_table (info)->dynamic_sections_created)
3231 bfd_size_type dynsymcount;
3232 asection *s;
3233 size_t bucketcount = 0;
3234 size_t hash_entry_size;
3235 unsigned int dtagcount;
3237 /* Set up the version definition section. */
3238 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3239 BFD_ASSERT (s != NULL);
3241 /* We may have created additional version definitions if we are
3242 just linking a regular application. */
3243 verdefs = asvinfo.verdefs;
3245 /* Skip anonymous version tag. */
3246 if (verdefs != NULL && verdefs->vernum == 0)
3247 verdefs = verdefs->next;
3249 if (verdefs == NULL)
3250 _bfd_strip_section_from_output (info, s);
3251 else
3253 unsigned int cdefs;
3254 bfd_size_type size;
3255 struct bfd_elf_version_tree *t;
3256 bfd_byte *p;
3257 Elf_Internal_Verdef def;
3258 Elf_Internal_Verdaux defaux;
3260 cdefs = 0;
3261 size = 0;
3263 /* Make space for the base version. */
3264 size += sizeof (Elf_External_Verdef);
3265 size += sizeof (Elf_External_Verdaux);
3266 ++cdefs;
3268 for (t = verdefs; t != NULL; t = t->next)
3270 struct bfd_elf_version_deps *n;
3272 size += sizeof (Elf_External_Verdef);
3273 size += sizeof (Elf_External_Verdaux);
3274 ++cdefs;
3276 for (n = t->deps; n != NULL; n = n->next)
3277 size += sizeof (Elf_External_Verdaux);
3280 s->_raw_size = size;
3281 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3282 if (s->contents == NULL && s->_raw_size != 0)
3283 return false;
3285 /* Fill in the version definition section. */
3287 p = s->contents;
3289 def.vd_version = VER_DEF_CURRENT;
3290 def.vd_flags = VER_FLG_BASE;
3291 def.vd_ndx = 1;
3292 def.vd_cnt = 1;
3293 def.vd_aux = sizeof (Elf_External_Verdef);
3294 def.vd_next = (sizeof (Elf_External_Verdef)
3295 + sizeof (Elf_External_Verdaux));
3297 if (soname_indx != (bfd_size_type) -1)
3299 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3300 soname_indx);
3301 def.vd_hash = bfd_elf_hash (soname);
3302 defaux.vda_name = soname_indx;
3304 else
3306 const char *name;
3307 bfd_size_type indx;
3309 name = basename (output_bfd->filename);
3310 def.vd_hash = bfd_elf_hash (name);
3311 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3312 name, false);
3313 if (indx == (bfd_size_type) -1)
3314 return false;
3315 defaux.vda_name = indx;
3317 defaux.vda_next = 0;
3319 _bfd_elf_swap_verdef_out (output_bfd, &def,
3320 (Elf_External_Verdef *) p);
3321 p += sizeof (Elf_External_Verdef);
3322 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3323 (Elf_External_Verdaux *) p);
3324 p += sizeof (Elf_External_Verdaux);
3326 for (t = verdefs; t != NULL; t = t->next)
3328 unsigned int cdeps;
3329 struct bfd_elf_version_deps *n;
3330 struct elf_link_hash_entry *h;
3332 cdeps = 0;
3333 for (n = t->deps; n != NULL; n = n->next)
3334 ++cdeps;
3336 /* Add a symbol representing this version. */
3337 h = NULL;
3338 if (! (_bfd_generic_link_add_one_symbol
3339 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3340 (bfd_vma) 0, (const char *) NULL, false,
3341 get_elf_backend_data (dynobj)->collect,
3342 (struct bfd_link_hash_entry **) &h)))
3343 return false;
3344 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3345 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3346 h->type = STT_OBJECT;
3347 h->verinfo.vertree = t;
3349 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3350 return false;
3352 def.vd_version = VER_DEF_CURRENT;
3353 def.vd_flags = 0;
3354 if (t->globals == NULL && t->locals == NULL && ! t->used)
3355 def.vd_flags |= VER_FLG_WEAK;
3356 def.vd_ndx = t->vernum + 1;
3357 def.vd_cnt = cdeps + 1;
3358 def.vd_hash = bfd_elf_hash (t->name);
3359 def.vd_aux = sizeof (Elf_External_Verdef);
3360 if (t->next != NULL)
3361 def.vd_next = (sizeof (Elf_External_Verdef)
3362 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3363 else
3364 def.vd_next = 0;
3366 _bfd_elf_swap_verdef_out (output_bfd, &def,
3367 (Elf_External_Verdef *) p);
3368 p += sizeof (Elf_External_Verdef);
3370 defaux.vda_name = h->dynstr_index;
3371 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3372 h->dynstr_index);
3373 if (t->deps == NULL)
3374 defaux.vda_next = 0;
3375 else
3376 defaux.vda_next = sizeof (Elf_External_Verdaux);
3377 t->name_indx = defaux.vda_name;
3379 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3380 (Elf_External_Verdaux *) p);
3381 p += sizeof (Elf_External_Verdaux);
3383 for (n = t->deps; n != NULL; n = n->next)
3385 if (n->version_needed == NULL)
3387 /* This can happen if there was an error in the
3388 version script. */
3389 defaux.vda_name = 0;
3391 else
3393 defaux.vda_name = n->version_needed->name_indx;
3394 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3395 defaux.vda_name);
3397 if (n->next == NULL)
3398 defaux.vda_next = 0;
3399 else
3400 defaux.vda_next = sizeof (Elf_External_Verdaux);
3402 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3403 (Elf_External_Verdaux *) p);
3404 p += sizeof (Elf_External_Verdaux);
3408 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3409 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3410 (bfd_vma) cdefs))
3411 return false;
3413 elf_tdata (output_bfd)->cverdefs = cdefs;
3416 if (info->new_dtags && info->flags)
3418 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3419 return false;
3422 if (info->flags_1)
3424 if (! info->shared)
3425 info->flags_1 &= ~ (DF_1_INITFIRST
3426 | DF_1_NODELETE
3427 | DF_1_NOOPEN);
3428 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3429 info->flags_1))
3430 return false;
3433 /* Work out the size of the version reference section. */
3435 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3436 BFD_ASSERT (s != NULL);
3438 struct elf_find_verdep_info sinfo;
3440 sinfo.output_bfd = output_bfd;
3441 sinfo.info = info;
3442 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3443 if (sinfo.vers == 0)
3444 sinfo.vers = 1;
3445 sinfo.failed = false;
3447 elf_link_hash_traverse (elf_hash_table (info),
3448 elf_link_find_version_dependencies,
3449 (PTR) &sinfo);
3451 if (elf_tdata (output_bfd)->verref == NULL)
3452 _bfd_strip_section_from_output (info, s);
3453 else
3455 Elf_Internal_Verneed *t;
3456 unsigned int size;
3457 unsigned int crefs;
3458 bfd_byte *p;
3460 /* Build the version definition section. */
3461 size = 0;
3462 crefs = 0;
3463 for (t = elf_tdata (output_bfd)->verref;
3464 t != NULL;
3465 t = t->vn_nextref)
3467 Elf_Internal_Vernaux *a;
3469 size += sizeof (Elf_External_Verneed);
3470 ++crefs;
3471 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3472 size += sizeof (Elf_External_Vernaux);
3475 s->_raw_size = size;
3476 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3477 if (s->contents == NULL)
3478 return false;
3480 p = s->contents;
3481 for (t = elf_tdata (output_bfd)->verref;
3482 t != NULL;
3483 t = t->vn_nextref)
3485 unsigned int caux;
3486 Elf_Internal_Vernaux *a;
3487 bfd_size_type indx;
3489 caux = 0;
3490 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3491 ++caux;
3493 t->vn_version = VER_NEED_CURRENT;
3494 t->vn_cnt = caux;
3495 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3496 elf_dt_name (t->vn_bfd) != NULL
3497 ? elf_dt_name (t->vn_bfd)
3498 : basename (t->vn_bfd->filename),
3499 false);
3500 if (indx == (bfd_size_type) -1)
3501 return false;
3502 t->vn_file = indx;
3503 t->vn_aux = sizeof (Elf_External_Verneed);
3504 if (t->vn_nextref == NULL)
3505 t->vn_next = 0;
3506 else
3507 t->vn_next = (sizeof (Elf_External_Verneed)
3508 + caux * sizeof (Elf_External_Vernaux));
3510 _bfd_elf_swap_verneed_out (output_bfd, t,
3511 (Elf_External_Verneed *) p);
3512 p += sizeof (Elf_External_Verneed);
3514 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3516 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3517 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3518 a->vna_nodename, false);
3519 if (indx == (bfd_size_type) -1)
3520 return false;
3521 a->vna_name = indx;
3522 if (a->vna_nextptr == NULL)
3523 a->vna_next = 0;
3524 else
3525 a->vna_next = sizeof (Elf_External_Vernaux);
3527 _bfd_elf_swap_vernaux_out (output_bfd, a,
3528 (Elf_External_Vernaux *) p);
3529 p += sizeof (Elf_External_Vernaux);
3533 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3534 (bfd_vma) 0)
3535 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3536 (bfd_vma) crefs))
3537 return false;
3539 elf_tdata (output_bfd)->cverrefs = crefs;
3543 /* Assign dynsym indicies. In a shared library we generate a
3544 section symbol for each output section, which come first.
3545 Next come all of the back-end allocated local dynamic syms,
3546 followed by the rest of the global symbols. */
3548 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3550 /* Work out the size of the symbol version section. */
3551 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3552 BFD_ASSERT (s != NULL);
3553 if (dynsymcount == 0
3554 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3556 _bfd_strip_section_from_output (info, s);
3557 /* The DYNSYMCOUNT might have changed if we were going to
3558 output a dynamic symbol table entry for S. */
3559 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3561 else
3563 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3564 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3565 if (s->contents == NULL)
3566 return false;
3568 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3569 return false;
3572 /* Set the size of the .dynsym and .hash sections. We counted
3573 the number of dynamic symbols in elf_link_add_object_symbols.
3574 We will build the contents of .dynsym and .hash when we build
3575 the final symbol table, because until then we do not know the
3576 correct value to give the symbols. We built the .dynstr
3577 section as we went along in elf_link_add_object_symbols. */
3578 s = bfd_get_section_by_name (dynobj, ".dynsym");
3579 BFD_ASSERT (s != NULL);
3580 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3581 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3582 if (s->contents == NULL && s->_raw_size != 0)
3583 return false;
3585 if (dynsymcount != 0)
3587 Elf_Internal_Sym isym;
3589 /* The first entry in .dynsym is a dummy symbol. */
3590 isym.st_value = 0;
3591 isym.st_size = 0;
3592 isym.st_name = 0;
3593 isym.st_info = 0;
3594 isym.st_other = 0;
3595 isym.st_shndx = 0;
3596 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3599 /* Compute the size of the hashing table. As a side effect this
3600 computes the hash values for all the names we export. */
3601 bucketcount = compute_bucket_count (info);
3603 s = bfd_get_section_by_name (dynobj, ".hash");
3604 BFD_ASSERT (s != NULL);
3605 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3606 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3607 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3608 if (s->contents == NULL)
3609 return false;
3610 memset (s->contents, 0, (size_t) s->_raw_size);
3612 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3613 s->contents);
3614 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3615 s->contents + hash_entry_size);
3617 elf_hash_table (info)->bucketcount = bucketcount;
3619 s = bfd_get_section_by_name (dynobj, ".dynstr");
3620 BFD_ASSERT (s != NULL);
3622 elf_finalize_dynstr (output_bfd, info);
3624 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3626 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3627 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3628 return false;
3631 return true;
3634 /* This function is used to adjust offsets into .dynstr for
3635 dynamic symbols. This is called via elf_link_hash_traverse. */
3637 static boolean elf_adjust_dynstr_offsets
3638 PARAMS ((struct elf_link_hash_entry *, PTR));
3640 static boolean
3641 elf_adjust_dynstr_offsets (h, data)
3642 struct elf_link_hash_entry *h;
3643 PTR data;
3645 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3647 if (h->dynindx != -1)
3648 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3649 return true;
3652 /* Assign string offsets in .dynstr, update all structures referencing
3653 them. */
3655 static boolean
3656 elf_finalize_dynstr (output_bfd, info)
3657 bfd *output_bfd;
3658 struct bfd_link_info *info;
3660 struct elf_link_local_dynamic_entry *entry;
3661 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3662 bfd *dynobj = elf_hash_table (info)->dynobj;
3663 asection *sdyn;
3664 bfd_size_type size;
3665 Elf_External_Dyn *dyncon, *dynconend;
3667 _bfd_elf_strtab_finalize (dynstr);
3668 size = _bfd_elf_strtab_size (dynstr);
3670 /* Update all .dynamic entries referencing .dynstr strings. */
3671 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3672 BFD_ASSERT (sdyn != NULL);
3674 dyncon = (Elf_External_Dyn *) sdyn->contents;
3675 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3676 sdyn->_raw_size);
3677 for (; dyncon < dynconend; dyncon++)
3679 Elf_Internal_Dyn dyn;
3681 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3682 switch (dyn.d_tag)
3684 case DT_STRSZ:
3685 dyn.d_un.d_val = size;
3686 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3687 break;
3688 case DT_NEEDED:
3689 case DT_SONAME:
3690 case DT_RPATH:
3691 case DT_RUNPATH:
3692 case DT_FILTER:
3693 case DT_AUXILIARY:
3694 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3695 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3696 break;
3697 default:
3698 break;
3702 /* Now update local dynamic symbols. */
3703 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3704 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3705 entry->isym.st_name);
3707 /* And the rest of dynamic symbols. */
3708 elf_link_hash_traverse (elf_hash_table (info),
3709 elf_adjust_dynstr_offsets, dynstr);
3711 /* Adjust version definitions. */
3712 if (elf_tdata (output_bfd)->cverdefs)
3714 asection *s;
3715 bfd_byte *p;
3716 bfd_size_type i;
3717 Elf_Internal_Verdef def;
3718 Elf_Internal_Verdaux defaux;
3720 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3721 p = (bfd_byte *) s->contents;
3724 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3725 &def);
3726 p += sizeof (Elf_External_Verdef);
3727 for (i = 0; i < def.vd_cnt; ++i)
3729 _bfd_elf_swap_verdaux_in (output_bfd,
3730 (Elf_External_Verdaux *) p, &defaux);
3731 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3732 defaux.vda_name);
3733 _bfd_elf_swap_verdaux_out (output_bfd,
3734 &defaux, (Elf_External_Verdaux *) p);
3735 p += sizeof (Elf_External_Verdaux);
3738 while (def.vd_next);
3741 /* Adjust version references. */
3742 if (elf_tdata (output_bfd)->verref)
3744 asection *s;
3745 bfd_byte *p;
3746 bfd_size_type i;
3747 Elf_Internal_Verneed need;
3748 Elf_Internal_Vernaux needaux;
3750 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3751 p = (bfd_byte *) s->contents;
3754 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3755 &need);
3756 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3757 _bfd_elf_swap_verneed_out (output_bfd, &need,
3758 (Elf_External_Verneed *) p);
3759 p += sizeof (Elf_External_Verneed);
3760 for (i = 0; i < need.vn_cnt; ++i)
3762 _bfd_elf_swap_vernaux_in (output_bfd,
3763 (Elf_External_Vernaux *) p, &needaux);
3764 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3765 needaux.vna_name);
3766 _bfd_elf_swap_vernaux_out (output_bfd,
3767 &needaux,
3768 (Elf_External_Vernaux *) p);
3769 p += sizeof (Elf_External_Vernaux);
3772 while (need.vn_next);
3775 return true;
3778 /* Fix up the flags for a symbol. This handles various cases which
3779 can only be fixed after all the input files are seen. This is
3780 currently called by both adjust_dynamic_symbol and
3781 assign_sym_version, which is unnecessary but perhaps more robust in
3782 the face of future changes. */
3784 static boolean
3785 elf_fix_symbol_flags (h, eif)
3786 struct elf_link_hash_entry *h;
3787 struct elf_info_failed *eif;
3789 /* If this symbol was mentioned in a non-ELF file, try to set
3790 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3791 permit a non-ELF file to correctly refer to a symbol defined in
3792 an ELF dynamic object. */
3793 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3795 while (h->root.type == bfd_link_hash_indirect)
3796 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3798 if (h->root.type != bfd_link_hash_defined
3799 && h->root.type != bfd_link_hash_defweak)
3800 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3801 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3802 else
3804 if (h->root.u.def.section->owner != NULL
3805 && (bfd_get_flavour (h->root.u.def.section->owner)
3806 == bfd_target_elf_flavour))
3807 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3808 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3809 else
3810 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3813 if (h->dynindx == -1
3814 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3815 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3817 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3819 eif->failed = true;
3820 return false;
3824 else
3826 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3827 was first seen in a non-ELF file. Fortunately, if the symbol
3828 was first seen in an ELF file, we're probably OK unless the
3829 symbol was defined in a non-ELF file. Catch that case here.
3830 FIXME: We're still in trouble if the symbol was first seen in
3831 a dynamic object, and then later in a non-ELF regular object. */
3832 if ((h->root.type == bfd_link_hash_defined
3833 || h->root.type == bfd_link_hash_defweak)
3834 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3835 && (h->root.u.def.section->owner != NULL
3836 ? (bfd_get_flavour (h->root.u.def.section->owner)
3837 != bfd_target_elf_flavour)
3838 : (bfd_is_abs_section (h->root.u.def.section)
3839 && (h->elf_link_hash_flags
3840 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3841 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3844 /* If this is a final link, and the symbol was defined as a common
3845 symbol in a regular object file, and there was no definition in
3846 any dynamic object, then the linker will have allocated space for
3847 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3848 flag will not have been set. */
3849 if (h->root.type == bfd_link_hash_defined
3850 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3851 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3852 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3853 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3854 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3856 /* If -Bsymbolic was used (which means to bind references to global
3857 symbols to the definition within the shared object), and this
3858 symbol was defined in a regular object, then it actually doesn't
3859 need a PLT entry, and we can accomplish that by forcing it local.
3860 Likewise, if the symbol has hidden or internal visibility.
3861 FIXME: It might be that we also do not need a PLT for other
3862 non-hidden visibilities, but we would have to tell that to the
3863 backend specifically; we can't just clear PLT-related data here. */
3864 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3865 && eif->info->shared
3866 && is_elf_hash_table (eif->info)
3867 && (eif->info->symbolic
3868 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3869 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3870 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3872 struct elf_backend_data *bed;
3873 boolean force_local;
3875 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3877 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3878 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3879 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3882 /* If this is a weak defined symbol in a dynamic object, and we know
3883 the real definition in the dynamic object, copy interesting flags
3884 over to the real definition. */
3885 if (h->weakdef != NULL)
3887 struct elf_link_hash_entry *weakdef;
3889 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3890 || h->root.type == bfd_link_hash_defweak);
3891 weakdef = h->weakdef;
3892 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3893 || weakdef->root.type == bfd_link_hash_defweak);
3894 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3896 /* If the real definition is defined by a regular object file,
3897 don't do anything special. See the longer description in
3898 elf_adjust_dynamic_symbol, below. */
3899 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3900 h->weakdef = NULL;
3901 else
3903 struct elf_backend_data *bed;
3905 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3906 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3910 return true;
3913 /* Make the backend pick a good value for a dynamic symbol. This is
3914 called via elf_link_hash_traverse, and also calls itself
3915 recursively. */
3917 static boolean
3918 elf_adjust_dynamic_symbol (h, data)
3919 struct elf_link_hash_entry *h;
3920 PTR data;
3922 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3923 bfd *dynobj;
3924 struct elf_backend_data *bed;
3926 /* Ignore indirect symbols. These are added by the versioning code. */
3927 if (h->root.type == bfd_link_hash_indirect)
3928 return true;
3930 if (! is_elf_hash_table (eif->info))
3931 return false;
3933 /* Fix the symbol flags. */
3934 if (! elf_fix_symbol_flags (h, eif))
3935 return false;
3937 /* If this symbol does not require a PLT entry, and it is not
3938 defined by a dynamic object, or is not referenced by a regular
3939 object, ignore it. We do have to handle a weak defined symbol,
3940 even if no regular object refers to it, if we decided to add it
3941 to the dynamic symbol table. FIXME: Do we normally need to worry
3942 about symbols which are defined by one dynamic object and
3943 referenced by another one? */
3944 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3945 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3946 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3947 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3948 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3950 h->plt.offset = (bfd_vma) -1;
3951 return true;
3954 /* If we've already adjusted this symbol, don't do it again. This
3955 can happen via a recursive call. */
3956 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3957 return true;
3959 /* Don't look at this symbol again. Note that we must set this
3960 after checking the above conditions, because we may look at a
3961 symbol once, decide not to do anything, and then get called
3962 recursively later after REF_REGULAR is set below. */
3963 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3965 /* If this is a weak definition, and we know a real definition, and
3966 the real symbol is not itself defined by a regular object file,
3967 then get a good value for the real definition. We handle the
3968 real symbol first, for the convenience of the backend routine.
3970 Note that there is a confusing case here. If the real definition
3971 is defined by a regular object file, we don't get the real symbol
3972 from the dynamic object, but we do get the weak symbol. If the
3973 processor backend uses a COPY reloc, then if some routine in the
3974 dynamic object changes the real symbol, we will not see that
3975 change in the corresponding weak symbol. This is the way other
3976 ELF linkers work as well, and seems to be a result of the shared
3977 library model.
3979 I will clarify this issue. Most SVR4 shared libraries define the
3980 variable _timezone and define timezone as a weak synonym. The
3981 tzset call changes _timezone. If you write
3982 extern int timezone;
3983 int _timezone = 5;
3984 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3985 you might expect that, since timezone is a synonym for _timezone,
3986 the same number will print both times. However, if the processor
3987 backend uses a COPY reloc, then actually timezone will be copied
3988 into your process image, and, since you define _timezone
3989 yourself, _timezone will not. Thus timezone and _timezone will
3990 wind up at different memory locations. The tzset call will set
3991 _timezone, leaving timezone unchanged. */
3993 if (h->weakdef != NULL)
3995 /* If we get to this point, we know there is an implicit
3996 reference by a regular object file via the weak symbol H.
3997 FIXME: Is this really true? What if the traversal finds
3998 H->WEAKDEF before it finds H? */
3999 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4001 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4002 return false;
4005 /* If a symbol has no type and no size and does not require a PLT
4006 entry, then we are probably about to do the wrong thing here: we
4007 are probably going to create a COPY reloc for an empty object.
4008 This case can arise when a shared object is built with assembly
4009 code, and the assembly code fails to set the symbol type. */
4010 if (h->size == 0
4011 && h->type == STT_NOTYPE
4012 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4013 (*_bfd_error_handler)
4014 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4015 h->root.root.string);
4017 dynobj = elf_hash_table (eif->info)->dynobj;
4018 bed = get_elf_backend_data (dynobj);
4019 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4021 eif->failed = true;
4022 return false;
4025 return true;
4028 /* This routine is used to export all defined symbols into the dynamic
4029 symbol table. It is called via elf_link_hash_traverse. */
4031 static boolean
4032 elf_export_symbol (h, data)
4033 struct elf_link_hash_entry *h;
4034 PTR data;
4036 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4038 /* Ignore indirect symbols. These are added by the versioning code. */
4039 if (h->root.type == bfd_link_hash_indirect)
4040 return true;
4042 if (h->dynindx == -1
4043 && (h->elf_link_hash_flags
4044 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4046 struct bfd_elf_version_tree *t;
4047 struct bfd_elf_version_expr *d;
4049 for (t = eif->verdefs; t != NULL; t = t->next)
4051 if (t->globals != NULL)
4053 for (d = t->globals; d != NULL; d = d->next)
4055 if ((*d->match) (d, h->root.root.string))
4056 goto doit;
4060 if (t->locals != NULL)
4062 for (d = t->locals ; d != NULL; d = d->next)
4064 if ((*d->match) (d, h->root.root.string))
4065 return true;
4070 if (!eif->verdefs)
4072 doit:
4073 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4075 eif->failed = true;
4076 return false;
4081 return true;
4084 /* Look through the symbols which are defined in other shared
4085 libraries and referenced here. Update the list of version
4086 dependencies. This will be put into the .gnu.version_r section.
4087 This function is called via elf_link_hash_traverse. */
4089 static boolean
4090 elf_link_find_version_dependencies (h, data)
4091 struct elf_link_hash_entry *h;
4092 PTR data;
4094 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4095 Elf_Internal_Verneed *t;
4096 Elf_Internal_Vernaux *a;
4097 bfd_size_type amt;
4099 /* We only care about symbols defined in shared objects with version
4100 information. */
4101 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4102 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4103 || h->dynindx == -1
4104 || h->verinfo.verdef == NULL)
4105 return true;
4107 /* See if we already know about this version. */
4108 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4110 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4111 continue;
4113 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4114 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4115 return true;
4117 break;
4120 /* This is a new version. Add it to tree we are building. */
4122 if (t == NULL)
4124 amt = sizeof *t;
4125 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4126 if (t == NULL)
4128 rinfo->failed = true;
4129 return false;
4132 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4133 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4134 elf_tdata (rinfo->output_bfd)->verref = t;
4137 amt = sizeof *a;
4138 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4140 /* Note that we are copying a string pointer here, and testing it
4141 above. If bfd_elf_string_from_elf_section is ever changed to
4142 discard the string data when low in memory, this will have to be
4143 fixed. */
4144 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4146 a->vna_flags = h->verinfo.verdef->vd_flags;
4147 a->vna_nextptr = t->vn_auxptr;
4149 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4150 ++rinfo->vers;
4152 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4154 t->vn_auxptr = a;
4156 return true;
4159 /* Figure out appropriate versions for all the symbols. We may not
4160 have the version number script until we have read all of the input
4161 files, so until that point we don't know which symbols should be
4162 local. This function is called via elf_link_hash_traverse. */
4164 static boolean
4165 elf_link_assign_sym_version (h, data)
4166 struct elf_link_hash_entry *h;
4167 PTR data;
4169 struct elf_assign_sym_version_info *sinfo;
4170 struct bfd_link_info *info;
4171 struct elf_backend_data *bed;
4172 struct elf_info_failed eif;
4173 char *p;
4174 bfd_size_type amt;
4176 sinfo = (struct elf_assign_sym_version_info *) data;
4177 info = sinfo->info;
4179 /* Fix the symbol flags. */
4180 eif.failed = false;
4181 eif.info = info;
4182 if (! elf_fix_symbol_flags (h, &eif))
4184 if (eif.failed)
4185 sinfo->failed = true;
4186 return false;
4189 /* We only need version numbers for symbols defined in regular
4190 objects. */
4191 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4192 return true;
4194 bed = get_elf_backend_data (sinfo->output_bfd);
4195 p = strchr (h->root.root.string, ELF_VER_CHR);
4196 if (p != NULL && h->verinfo.vertree == NULL)
4198 struct bfd_elf_version_tree *t;
4199 boolean hidden;
4201 hidden = true;
4203 /* There are two consecutive ELF_VER_CHR characters if this is
4204 not a hidden symbol. */
4205 ++p;
4206 if (*p == ELF_VER_CHR)
4208 hidden = false;
4209 ++p;
4212 /* If there is no version string, we can just return out. */
4213 if (*p == '\0')
4215 if (hidden)
4216 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4217 return true;
4220 /* Look for the version. If we find it, it is no longer weak. */
4221 for (t = sinfo->verdefs; t != NULL; t = t->next)
4223 if (strcmp (t->name, p) == 0)
4225 size_t len;
4226 char *alc;
4227 struct bfd_elf_version_expr *d;
4229 len = p - h->root.root.string;
4230 alc = bfd_malloc ((bfd_size_type) len);
4231 if (alc == NULL)
4232 return false;
4233 strncpy (alc, h->root.root.string, len - 1);
4234 alc[len - 1] = '\0';
4235 if (alc[len - 2] == ELF_VER_CHR)
4236 alc[len - 2] = '\0';
4238 h->verinfo.vertree = t;
4239 t->used = true;
4240 d = NULL;
4242 if (t->globals != NULL)
4244 for (d = t->globals; d != NULL; d = d->next)
4245 if ((*d->match) (d, alc))
4246 break;
4249 /* See if there is anything to force this symbol to
4250 local scope. */
4251 if (d == NULL && t->locals != NULL)
4253 for (d = t->locals; d != NULL; d = d->next)
4255 if ((*d->match) (d, alc))
4257 if (h->dynindx != -1
4258 && info->shared
4259 && ! info->export_dynamic)
4261 (*bed->elf_backend_hide_symbol) (info, h, true);
4264 break;
4269 free (alc);
4270 break;
4274 /* If we are building an application, we need to create a
4275 version node for this version. */
4276 if (t == NULL && ! info->shared)
4278 struct bfd_elf_version_tree **pp;
4279 int version_index;
4281 /* If we aren't going to export this symbol, we don't need
4282 to worry about it. */
4283 if (h->dynindx == -1)
4284 return true;
4286 amt = sizeof *t;
4287 t = ((struct bfd_elf_version_tree *)
4288 bfd_alloc (sinfo->output_bfd, amt));
4289 if (t == NULL)
4291 sinfo->failed = true;
4292 return false;
4295 t->next = NULL;
4296 t->name = p;
4297 t->globals = NULL;
4298 t->locals = NULL;
4299 t->deps = NULL;
4300 t->name_indx = (unsigned int) -1;
4301 t->used = true;
4303 version_index = 1;
4304 /* Don't count anonymous version tag. */
4305 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4306 version_index = 0;
4307 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4308 ++version_index;
4309 t->vernum = version_index;
4311 *pp = t;
4313 h->verinfo.vertree = t;
4315 else if (t == NULL)
4317 /* We could not find the version for a symbol when
4318 generating a shared archive. Return an error. */
4319 (*_bfd_error_handler)
4320 (_("%s: undefined versioned symbol name %s"),
4321 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4322 bfd_set_error (bfd_error_bad_value);
4323 sinfo->failed = true;
4324 return false;
4327 if (hidden)
4328 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4331 /* If we don't have a version for this symbol, see if we can find
4332 something. */
4333 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4335 struct bfd_elf_version_tree *t;
4336 struct bfd_elf_version_tree *deflt;
4337 struct bfd_elf_version_expr *d;
4339 /* See if can find what version this symbol is in. If the
4340 symbol is supposed to be local, then don't actually register
4341 it. */
4342 deflt = NULL;
4343 for (t = sinfo->verdefs; t != NULL; t = t->next)
4345 if (t->globals != NULL)
4347 for (d = t->globals; d != NULL; d = d->next)
4349 if ((*d->match) (d, h->root.root.string))
4351 h->verinfo.vertree = t;
4352 break;
4356 if (d != NULL)
4357 break;
4360 if (t->locals != NULL)
4362 for (d = t->locals; d != NULL; d = d->next)
4364 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4365 deflt = t;
4366 else if ((*d->match) (d, h->root.root.string))
4368 h->verinfo.vertree = t;
4369 if (h->dynindx != -1
4370 && info->shared
4371 && ! info->export_dynamic)
4373 (*bed->elf_backend_hide_symbol) (info, h, true);
4375 break;
4379 if (d != NULL)
4380 break;
4384 if (deflt != NULL && h->verinfo.vertree == NULL)
4386 h->verinfo.vertree = deflt;
4387 if (h->dynindx != -1
4388 && info->shared
4389 && ! info->export_dynamic)
4391 (*bed->elf_backend_hide_symbol) (info, h, true);
4396 return true;
4399 /* Final phase of ELF linker. */
4401 /* A structure we use to avoid passing large numbers of arguments. */
4403 struct elf_final_link_info
4405 /* General link information. */
4406 struct bfd_link_info *info;
4407 /* Output BFD. */
4408 bfd *output_bfd;
4409 /* Symbol string table. */
4410 struct bfd_strtab_hash *symstrtab;
4411 /* .dynsym section. */
4412 asection *dynsym_sec;
4413 /* .hash section. */
4414 asection *hash_sec;
4415 /* symbol version section (.gnu.version). */
4416 asection *symver_sec;
4417 /* Buffer large enough to hold contents of any section. */
4418 bfd_byte *contents;
4419 /* Buffer large enough to hold external relocs of any section. */
4420 PTR external_relocs;
4421 /* Buffer large enough to hold internal relocs of any section. */
4422 Elf_Internal_Rela *internal_relocs;
4423 /* Buffer large enough to hold external local symbols of any input
4424 BFD. */
4425 Elf_External_Sym *external_syms;
4426 /* And a buffer for symbol section indices. */
4427 Elf_External_Sym_Shndx *locsym_shndx;
4428 /* Buffer large enough to hold internal local symbols of any input
4429 BFD. */
4430 Elf_Internal_Sym *internal_syms;
4431 /* Array large enough to hold a symbol index for each local symbol
4432 of any input BFD. */
4433 long *indices;
4434 /* Array large enough to hold a section pointer for each local
4435 symbol of any input BFD. */
4436 asection **sections;
4437 /* Buffer to hold swapped out symbols. */
4438 Elf_External_Sym *symbuf;
4439 /* And one for symbol section indices. */
4440 Elf_External_Sym_Shndx *symshndxbuf;
4441 /* Number of swapped out symbols in buffer. */
4442 size_t symbuf_count;
4443 /* Number of symbols which fit in symbuf. */
4444 size_t symbuf_size;
4447 static boolean elf_link_output_sym
4448 PARAMS ((struct elf_final_link_info *, const char *,
4449 Elf_Internal_Sym *, asection *));
4450 static boolean elf_link_flush_output_syms
4451 PARAMS ((struct elf_final_link_info *));
4452 static boolean elf_link_output_extsym
4453 PARAMS ((struct elf_link_hash_entry *, PTR));
4454 static boolean elf_link_sec_merge_syms
4455 PARAMS ((struct elf_link_hash_entry *, PTR));
4456 static boolean elf_link_input_bfd
4457 PARAMS ((struct elf_final_link_info *, bfd *));
4458 static boolean elf_reloc_link_order
4459 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4460 struct bfd_link_order *));
4462 /* This struct is used to pass information to elf_link_output_extsym. */
4464 struct elf_outext_info
4466 boolean failed;
4467 boolean localsyms;
4468 struct elf_final_link_info *finfo;
4471 /* Compute the size of, and allocate space for, REL_HDR which is the
4472 section header for a section containing relocations for O. */
4474 static boolean
4475 elf_link_size_reloc_section (abfd, rel_hdr, o)
4476 bfd *abfd;
4477 Elf_Internal_Shdr *rel_hdr;
4478 asection *o;
4480 bfd_size_type reloc_count;
4481 bfd_size_type num_rel_hashes;
4483 /* Figure out how many relocations there will be. */
4484 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4485 reloc_count = elf_section_data (o)->rel_count;
4486 else
4487 reloc_count = elf_section_data (o)->rel_count2;
4489 num_rel_hashes = o->reloc_count;
4490 if (num_rel_hashes < reloc_count)
4491 num_rel_hashes = reloc_count;
4493 /* That allows us to calculate the size of the section. */
4494 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4496 /* The contents field must last into write_object_contents, so we
4497 allocate it with bfd_alloc rather than malloc. Also since we
4498 cannot be sure that the contents will actually be filled in,
4499 we zero the allocated space. */
4500 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4501 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4502 return false;
4504 /* We only allocate one set of hash entries, so we only do it the
4505 first time we are called. */
4506 if (elf_section_data (o)->rel_hashes == NULL
4507 && num_rel_hashes)
4509 struct elf_link_hash_entry **p;
4511 p = ((struct elf_link_hash_entry **)
4512 bfd_zmalloc (num_rel_hashes
4513 * sizeof (struct elf_link_hash_entry *)));
4514 if (p == NULL)
4515 return false;
4517 elf_section_data (o)->rel_hashes = p;
4520 return true;
4523 /* When performing a relocateable link, the input relocations are
4524 preserved. But, if they reference global symbols, the indices
4525 referenced must be updated. Update all the relocations in
4526 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4528 static void
4529 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4530 bfd *abfd;
4531 Elf_Internal_Shdr *rel_hdr;
4532 unsigned int count;
4533 struct elf_link_hash_entry **rel_hash;
4535 unsigned int i;
4536 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4537 Elf_Internal_Rel *irel;
4538 Elf_Internal_Rela *irela;
4539 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4541 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4542 if (irel == NULL)
4544 (*_bfd_error_handler) (_("Error: out of memory"));
4545 abort ();
4548 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4549 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4550 if (irela == NULL)
4552 (*_bfd_error_handler) (_("Error: out of memory"));
4553 abort ();
4556 for (i = 0; i < count; i++, rel_hash++)
4558 if (*rel_hash == NULL)
4559 continue;
4561 BFD_ASSERT ((*rel_hash)->indx >= 0);
4563 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4565 Elf_External_Rel *erel;
4566 unsigned int j;
4568 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4569 if (bed->s->swap_reloc_in)
4570 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4571 else
4572 elf_swap_reloc_in (abfd, erel, irel);
4574 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4575 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4576 ELF_R_TYPE (irel[j].r_info));
4578 if (bed->s->swap_reloc_out)
4579 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4580 else
4581 elf_swap_reloc_out (abfd, irel, erel);
4583 else
4585 Elf_External_Rela *erela;
4586 unsigned int j;
4588 BFD_ASSERT (rel_hdr->sh_entsize
4589 == sizeof (Elf_External_Rela));
4591 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4592 if (bed->s->swap_reloca_in)
4593 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4594 else
4595 elf_swap_reloca_in (abfd, erela, irela);
4597 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4598 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4599 ELF_R_TYPE (irela[j].r_info));
4601 if (bed->s->swap_reloca_out)
4602 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4603 else
4604 elf_swap_reloca_out (abfd, irela, erela);
4608 free (irel);
4609 free (irela);
4612 struct elf_link_sort_rela {
4613 bfd_vma offset;
4614 enum elf_reloc_type_class type;
4615 union {
4616 Elf_Internal_Rel rel;
4617 Elf_Internal_Rela rela;
4618 } u;
4621 static int
4622 elf_link_sort_cmp1 (A, B)
4623 const PTR A;
4624 const PTR B;
4626 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4627 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4628 int relativea, relativeb;
4630 relativea = a->type == reloc_class_relative;
4631 relativeb = b->type == reloc_class_relative;
4633 if (relativea < relativeb)
4634 return 1;
4635 if (relativea > relativeb)
4636 return -1;
4637 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4638 return -1;
4639 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4640 return 1;
4641 if (a->u.rel.r_offset < b->u.rel.r_offset)
4642 return -1;
4643 if (a->u.rel.r_offset > b->u.rel.r_offset)
4644 return 1;
4645 return 0;
4648 static int
4649 elf_link_sort_cmp2 (A, B)
4650 const PTR A;
4651 const PTR B;
4653 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4654 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4655 int copya, copyb;
4657 if (a->offset < b->offset)
4658 return -1;
4659 if (a->offset > b->offset)
4660 return 1;
4661 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4662 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4663 if (copya < copyb)
4664 return -1;
4665 if (copya > copyb)
4666 return 1;
4667 if (a->u.rel.r_offset < b->u.rel.r_offset)
4668 return -1;
4669 if (a->u.rel.r_offset > b->u.rel.r_offset)
4670 return 1;
4671 return 0;
4674 static size_t
4675 elf_link_sort_relocs (abfd, info, psec)
4676 bfd *abfd;
4677 struct bfd_link_info *info;
4678 asection **psec;
4680 bfd *dynobj = elf_hash_table (info)->dynobj;
4681 asection *reldyn, *o;
4682 boolean rel = false;
4683 bfd_size_type count, size;
4684 size_t i, j, ret;
4685 struct elf_link_sort_rela *rela;
4686 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4688 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4689 if (reldyn == NULL || reldyn->_raw_size == 0)
4691 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4692 if (reldyn == NULL || reldyn->_raw_size == 0)
4693 return 0;
4694 rel = true;
4695 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4697 else
4698 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4700 size = 0;
4701 for (o = dynobj->sections; o != NULL; o = o->next)
4702 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4703 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4704 && o->output_section == reldyn)
4705 size += o->_raw_size;
4707 if (size != reldyn->_raw_size)
4708 return 0;
4710 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4711 if (rela == NULL)
4713 (*info->callbacks->warning)
4714 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4715 (bfd_vma) 0);
4716 return 0;
4719 for (o = dynobj->sections; o != NULL; o = o->next)
4720 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4721 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4722 && o->output_section == reldyn)
4724 if (rel)
4726 Elf_External_Rel *erel, *erelend;
4727 struct elf_link_sort_rela *s;
4729 erel = (Elf_External_Rel *) o->contents;
4730 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4731 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4732 for (; erel < erelend; erel++, s++)
4734 if (bed->s->swap_reloc_in)
4735 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4736 else
4737 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4739 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4742 else
4744 Elf_External_Rela *erela, *erelaend;
4745 struct elf_link_sort_rela *s;
4747 erela = (Elf_External_Rela *) o->contents;
4748 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4749 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4750 for (; erela < erelaend; erela++, s++)
4752 if (bed->s->swap_reloca_in)
4753 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4754 &s->u.rela);
4755 else
4756 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4758 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4763 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4764 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4766 for (i = ret, j = ret; i < count; i++)
4768 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4769 j = i;
4770 rela[i].offset = rela[j].u.rel.r_offset;
4772 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4774 for (o = dynobj->sections; o != NULL; o = o->next)
4775 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4776 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4777 && o->output_section == reldyn)
4779 if (rel)
4781 Elf_External_Rel *erel, *erelend;
4782 struct elf_link_sort_rela *s;
4784 erel = (Elf_External_Rel *) o->contents;
4785 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4786 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4787 for (; erel < erelend; erel++, s++)
4789 if (bed->s->swap_reloc_out)
4790 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4791 (bfd_byte *) erel);
4792 else
4793 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4796 else
4798 Elf_External_Rela *erela, *erelaend;
4799 struct elf_link_sort_rela *s;
4801 erela = (Elf_External_Rela *) o->contents;
4802 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4803 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4804 for (; erela < erelaend; erela++, s++)
4806 if (bed->s->swap_reloca_out)
4807 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4808 (bfd_byte *) erela);
4809 else
4810 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4815 free (rela);
4816 *psec = reldyn;
4817 return ret;
4820 /* Do the final step of an ELF link. */
4822 boolean
4823 elf_bfd_final_link (abfd, info)
4824 bfd *abfd;
4825 struct bfd_link_info *info;
4827 boolean dynamic;
4828 boolean emit_relocs;
4829 bfd *dynobj;
4830 struct elf_final_link_info finfo;
4831 register asection *o;
4832 register struct bfd_link_order *p;
4833 register bfd *sub;
4834 bfd_size_type max_contents_size;
4835 bfd_size_type max_external_reloc_size;
4836 bfd_size_type max_internal_reloc_count;
4837 bfd_size_type max_sym_count;
4838 bfd_size_type max_sym_shndx_count;
4839 file_ptr off;
4840 Elf_Internal_Sym elfsym;
4841 unsigned int i;
4842 Elf_Internal_Shdr *symtab_hdr;
4843 Elf_Internal_Shdr *symstrtab_hdr;
4844 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4845 struct elf_outext_info eoinfo;
4846 boolean merged;
4847 size_t relativecount = 0;
4848 asection *reldyn = 0;
4849 bfd_size_type amt;
4851 if (! is_elf_hash_table (info))
4852 return false;
4854 if (info->shared)
4855 abfd->flags |= DYNAMIC;
4857 dynamic = elf_hash_table (info)->dynamic_sections_created;
4858 dynobj = elf_hash_table (info)->dynobj;
4860 emit_relocs = (info->relocateable
4861 || info->emitrelocations
4862 || bed->elf_backend_emit_relocs);
4864 finfo.info = info;
4865 finfo.output_bfd = abfd;
4866 finfo.symstrtab = elf_stringtab_init ();
4867 if (finfo.symstrtab == NULL)
4868 return false;
4870 if (! dynamic)
4872 finfo.dynsym_sec = NULL;
4873 finfo.hash_sec = NULL;
4874 finfo.symver_sec = NULL;
4876 else
4878 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4879 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4880 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4881 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4882 /* Note that it is OK if symver_sec is NULL. */
4885 finfo.contents = NULL;
4886 finfo.external_relocs = NULL;
4887 finfo.internal_relocs = NULL;
4888 finfo.external_syms = NULL;
4889 finfo.locsym_shndx = NULL;
4890 finfo.internal_syms = NULL;
4891 finfo.indices = NULL;
4892 finfo.sections = NULL;
4893 finfo.symbuf = NULL;
4894 finfo.symshndxbuf = NULL;
4895 finfo.symbuf_count = 0;
4897 /* Count up the number of relocations we will output for each output
4898 section, so that we know the sizes of the reloc sections. We
4899 also figure out some maximum sizes. */
4900 max_contents_size = 0;
4901 max_external_reloc_size = 0;
4902 max_internal_reloc_count = 0;
4903 max_sym_count = 0;
4904 max_sym_shndx_count = 0;
4905 merged = false;
4906 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4908 o->reloc_count = 0;
4910 for (p = o->link_order_head; p != NULL; p = p->next)
4912 if (p->type == bfd_section_reloc_link_order
4913 || p->type == bfd_symbol_reloc_link_order)
4914 ++o->reloc_count;
4915 else if (p->type == bfd_indirect_link_order)
4917 asection *sec;
4919 sec = p->u.indirect.section;
4921 /* Mark all sections which are to be included in the
4922 link. This will normally be every section. We need
4923 to do this so that we can identify any sections which
4924 the linker has decided to not include. */
4925 sec->linker_mark = true;
4927 if (sec->flags & SEC_MERGE)
4928 merged = true;
4930 if (info->relocateable || info->emitrelocations)
4931 o->reloc_count += sec->reloc_count;
4932 else if (bed->elf_backend_count_relocs)
4934 Elf_Internal_Rela * relocs;
4936 relocs = (NAME(_bfd_elf,link_read_relocs)
4937 (abfd, sec, (PTR) NULL,
4938 (Elf_Internal_Rela *) NULL, info->keep_memory));
4940 o->reloc_count += (*bed->elf_backend_count_relocs)
4941 (sec, relocs);
4943 if (!info->keep_memory)
4944 free (relocs);
4947 if (sec->_raw_size > max_contents_size)
4948 max_contents_size = sec->_raw_size;
4949 if (sec->_cooked_size > max_contents_size)
4950 max_contents_size = sec->_cooked_size;
4952 /* We are interested in just local symbols, not all
4953 symbols. */
4954 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4955 && (sec->owner->flags & DYNAMIC) == 0)
4957 size_t sym_count;
4959 if (elf_bad_symtab (sec->owner))
4960 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4961 / sizeof (Elf_External_Sym));
4962 else
4963 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4965 if (sym_count > max_sym_count)
4966 max_sym_count = sym_count;
4968 if (sym_count > max_sym_shndx_count
4969 && elf_symtab_shndx (sec->owner) != 0)
4970 max_sym_shndx_count = sym_count;
4972 if ((sec->flags & SEC_RELOC) != 0)
4974 size_t ext_size;
4976 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4977 if (ext_size > max_external_reloc_size)
4978 max_external_reloc_size = ext_size;
4979 if (sec->reloc_count > max_internal_reloc_count)
4980 max_internal_reloc_count = sec->reloc_count;
4986 if (o->reloc_count > 0)
4987 o->flags |= SEC_RELOC;
4988 else
4990 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4991 set it (this is probably a bug) and if it is set
4992 assign_section_numbers will create a reloc section. */
4993 o->flags &=~ SEC_RELOC;
4996 /* If the SEC_ALLOC flag is not set, force the section VMA to
4997 zero. This is done in elf_fake_sections as well, but forcing
4998 the VMA to 0 here will ensure that relocs against these
4999 sections are handled correctly. */
5000 if ((o->flags & SEC_ALLOC) == 0
5001 && ! o->user_set_vma)
5002 o->vma = 0;
5005 if (! info->relocateable && merged)
5006 elf_link_hash_traverse (elf_hash_table (info),
5007 elf_link_sec_merge_syms, (PTR) abfd);
5009 /* Figure out the file positions for everything but the symbol table
5010 and the relocs. We set symcount to force assign_section_numbers
5011 to create a symbol table. */
5012 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5013 BFD_ASSERT (! abfd->output_has_begun);
5014 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5015 goto error_return;
5017 /* Figure out how many relocations we will have in each section.
5018 Just using RELOC_COUNT isn't good enough since that doesn't
5019 maintain a separate value for REL vs. RELA relocations. */
5020 if (emit_relocs)
5021 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5022 for (o = sub->sections; o != NULL; o = o->next)
5024 asection *output_section;
5026 if (! o->linker_mark)
5028 /* This section was omitted from the link. */
5029 continue;
5032 output_section = o->output_section;
5034 if (output_section != NULL
5035 && (o->flags & SEC_RELOC) != 0)
5037 struct bfd_elf_section_data *esdi
5038 = elf_section_data (o);
5039 struct bfd_elf_section_data *esdo
5040 = elf_section_data (output_section);
5041 unsigned int *rel_count;
5042 unsigned int *rel_count2;
5044 /* We must be careful to add the relocation froms the
5045 input section to the right output count. */
5046 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
5048 rel_count = &esdo->rel_count;
5049 rel_count2 = &esdo->rel_count2;
5051 else
5053 rel_count = &esdo->rel_count2;
5054 rel_count2 = &esdo->rel_count;
5057 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5058 if (esdi->rel_hdr2)
5059 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5060 output_section->flags |= SEC_RELOC;
5064 /* That created the reloc sections. Set their sizes, and assign
5065 them file positions, and allocate some buffers. */
5066 for (o = abfd->sections; o != NULL; o = o->next)
5068 if ((o->flags & SEC_RELOC) != 0)
5070 if (!elf_link_size_reloc_section (abfd,
5071 &elf_section_data (o)->rel_hdr,
5073 goto error_return;
5075 if (elf_section_data (o)->rel_hdr2
5076 && !elf_link_size_reloc_section (abfd,
5077 elf_section_data (o)->rel_hdr2,
5079 goto error_return;
5082 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5083 to count upwards while actually outputting the relocations. */
5084 elf_section_data (o)->rel_count = 0;
5085 elf_section_data (o)->rel_count2 = 0;
5088 _bfd_elf_assign_file_positions_for_relocs (abfd);
5090 /* We have now assigned file positions for all the sections except
5091 .symtab and .strtab. We start the .symtab section at the current
5092 file position, and write directly to it. We build the .strtab
5093 section in memory. */
5094 bfd_get_symcount (abfd) = 0;
5095 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5096 /* sh_name is set in prep_headers. */
5097 symtab_hdr->sh_type = SHT_SYMTAB;
5098 symtab_hdr->sh_flags = 0;
5099 symtab_hdr->sh_addr = 0;
5100 symtab_hdr->sh_size = 0;
5101 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5102 /* sh_link is set in assign_section_numbers. */
5103 /* sh_info is set below. */
5104 /* sh_offset is set just below. */
5105 symtab_hdr->sh_addralign = bed->s->file_align;
5107 off = elf_tdata (abfd)->next_file_pos;
5108 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5110 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5111 incorrect. We do not yet know the size of the .symtab section.
5112 We correct next_file_pos below, after we do know the size. */
5114 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5115 continuously seeking to the right position in the file. */
5116 if (! info->keep_memory || max_sym_count < 20)
5117 finfo.symbuf_size = 20;
5118 else
5119 finfo.symbuf_size = max_sym_count;
5120 amt = finfo.symbuf_size;
5121 amt *= sizeof (Elf_External_Sym);
5122 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5123 if (finfo.symbuf == NULL)
5124 goto error_return;
5125 if (elf_numsections (abfd) > SHN_LORESERVE)
5127 amt = finfo.symbuf_size;
5128 amt *= sizeof (Elf_External_Sym_Shndx);
5129 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5130 if (finfo.symshndxbuf == NULL)
5131 goto error_return;
5134 /* Start writing out the symbol table. The first symbol is always a
5135 dummy symbol. */
5136 if (info->strip != strip_all
5137 || emit_relocs)
5139 elfsym.st_value = 0;
5140 elfsym.st_size = 0;
5141 elfsym.st_info = 0;
5142 elfsym.st_other = 0;
5143 elfsym.st_shndx = SHN_UNDEF;
5144 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5145 &elfsym, bfd_und_section_ptr))
5146 goto error_return;
5149 #if 0
5150 /* Some standard ELF linkers do this, but we don't because it causes
5151 bootstrap comparison failures. */
5152 /* Output a file symbol for the output file as the second symbol.
5153 We output this even if we are discarding local symbols, although
5154 I'm not sure if this is correct. */
5155 elfsym.st_value = 0;
5156 elfsym.st_size = 0;
5157 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5158 elfsym.st_other = 0;
5159 elfsym.st_shndx = SHN_ABS;
5160 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5161 &elfsym, bfd_abs_section_ptr))
5162 goto error_return;
5163 #endif
5165 /* Output a symbol for each section. We output these even if we are
5166 discarding local symbols, since they are used for relocs. These
5167 symbols have no names. We store the index of each one in the
5168 index field of the section, so that we can find it again when
5169 outputting relocs. */
5170 if (info->strip != strip_all
5171 || emit_relocs)
5173 elfsym.st_size = 0;
5174 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5175 elfsym.st_other = 0;
5176 for (i = 1; i < elf_numsections (abfd); i++)
5178 o = section_from_elf_index (abfd, i);
5179 if (o != NULL)
5180 o->target_index = bfd_get_symcount (abfd);
5181 elfsym.st_shndx = i;
5182 if (info->relocateable || o == NULL)
5183 elfsym.st_value = 0;
5184 else
5185 elfsym.st_value = o->vma;
5186 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5187 &elfsym, o))
5188 goto error_return;
5189 if (i == SHN_LORESERVE)
5190 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5194 /* Allocate some memory to hold information read in from the input
5195 files. */
5196 if (max_contents_size != 0)
5198 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5199 if (finfo.contents == NULL)
5200 goto error_return;
5203 if (max_external_reloc_size != 0)
5205 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5206 if (finfo.external_relocs == NULL)
5207 goto error_return;
5210 if (max_internal_reloc_count != 0)
5212 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5213 amt *= sizeof (Elf_Internal_Rela);
5214 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5215 if (finfo.internal_relocs == NULL)
5216 goto error_return;
5219 if (max_sym_count != 0)
5221 amt = max_sym_count * sizeof (Elf_External_Sym);
5222 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5223 if (finfo.external_syms == NULL)
5224 goto error_return;
5226 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5227 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5228 if (finfo.internal_syms == NULL)
5229 goto error_return;
5231 amt = max_sym_count * sizeof (long);
5232 finfo.indices = (long *) bfd_malloc (amt);
5233 if (finfo.indices == NULL)
5234 goto error_return;
5236 amt = max_sym_count * sizeof (asection *);
5237 finfo.sections = (asection **) bfd_malloc (amt);
5238 if (finfo.sections == NULL)
5239 goto error_return;
5242 if (max_sym_shndx_count != 0)
5244 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5245 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5246 if (finfo.locsym_shndx == NULL)
5247 goto error_return;
5250 /* Since ELF permits relocations to be against local symbols, we
5251 must have the local symbols available when we do the relocations.
5252 Since we would rather only read the local symbols once, and we
5253 would rather not keep them in memory, we handle all the
5254 relocations for a single input file at the same time.
5256 Unfortunately, there is no way to know the total number of local
5257 symbols until we have seen all of them, and the local symbol
5258 indices precede the global symbol indices. This means that when
5259 we are generating relocateable output, and we see a reloc against
5260 a global symbol, we can not know the symbol index until we have
5261 finished examining all the local symbols to see which ones we are
5262 going to output. To deal with this, we keep the relocations in
5263 memory, and don't output them until the end of the link. This is
5264 an unfortunate waste of memory, but I don't see a good way around
5265 it. Fortunately, it only happens when performing a relocateable
5266 link, which is not the common case. FIXME: If keep_memory is set
5267 we could write the relocs out and then read them again; I don't
5268 know how bad the memory loss will be. */
5270 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5271 sub->output_has_begun = false;
5272 for (o = abfd->sections; o != NULL; o = o->next)
5274 for (p = o->link_order_head; p != NULL; p = p->next)
5276 if (p->type == bfd_indirect_link_order
5277 && (bfd_get_flavour (p->u.indirect.section->owner)
5278 == bfd_target_elf_flavour))
5280 sub = p->u.indirect.section->owner;
5281 if (! sub->output_has_begun)
5283 if (! elf_link_input_bfd (&finfo, sub))
5284 goto error_return;
5285 sub->output_has_begun = true;
5288 else if (p->type == bfd_section_reloc_link_order
5289 || p->type == bfd_symbol_reloc_link_order)
5291 if (! elf_reloc_link_order (abfd, info, o, p))
5292 goto error_return;
5294 else
5296 if (! _bfd_default_link_order (abfd, info, o, p))
5297 goto error_return;
5302 /* That wrote out all the local symbols. Finish up the symbol table
5303 with the global symbols. Even if we want to strip everything we
5304 can, we still need to deal with those global symbols that got
5305 converted to local in a version script. */
5307 if (info->shared)
5309 /* Output any global symbols that got converted to local in a
5310 version script. We do this in a separate step since ELF
5311 requires all local symbols to appear prior to any global
5312 symbols. FIXME: We should only do this if some global
5313 symbols were, in fact, converted to become local. FIXME:
5314 Will this work correctly with the Irix 5 linker? */
5315 eoinfo.failed = false;
5316 eoinfo.finfo = &finfo;
5317 eoinfo.localsyms = true;
5318 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5319 (PTR) &eoinfo);
5320 if (eoinfo.failed)
5321 return false;
5324 /* The sh_info field records the index of the first non local symbol. */
5325 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5327 if (dynamic
5328 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5330 Elf_Internal_Sym sym;
5331 Elf_External_Sym *dynsym =
5332 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5333 long last_local = 0;
5335 /* Write out the section symbols for the output sections. */
5336 if (info->shared)
5338 asection *s;
5340 sym.st_size = 0;
5341 sym.st_name = 0;
5342 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5343 sym.st_other = 0;
5345 for (s = abfd->sections; s != NULL; s = s->next)
5347 int indx;
5348 Elf_External_Sym *dest;
5350 indx = elf_section_data (s)->this_idx;
5351 BFD_ASSERT (indx > 0);
5352 sym.st_shndx = indx;
5353 sym.st_value = s->vma;
5354 dest = dynsym + elf_section_data (s)->dynindx;
5355 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5358 last_local = bfd_count_sections (abfd);
5361 /* Write out the local dynsyms. */
5362 if (elf_hash_table (info)->dynlocal)
5364 struct elf_link_local_dynamic_entry *e;
5365 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5367 asection *s;
5368 Elf_External_Sym *dest;
5370 sym.st_size = e->isym.st_size;
5371 sym.st_other = e->isym.st_other;
5373 /* Copy the internal symbol as is.
5374 Note that we saved a word of storage and overwrote
5375 the original st_name with the dynstr_index. */
5376 sym = e->isym;
5378 if (e->isym.st_shndx < SHN_LORESERVE
5379 || e->isym.st_shndx > SHN_HIRESERVE)
5381 s = bfd_section_from_elf_index (e->input_bfd,
5382 e->isym.st_shndx);
5384 sym.st_shndx =
5385 elf_section_data (s->output_section)->this_idx;
5386 sym.st_value = (s->output_section->vma
5387 + s->output_offset
5388 + e->isym.st_value);
5391 if (last_local < e->dynindx)
5392 last_local = e->dynindx;
5394 dest = dynsym + e->dynindx;
5395 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5399 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5400 last_local + 1;
5403 /* We get the global symbols from the hash table. */
5404 eoinfo.failed = false;
5405 eoinfo.localsyms = false;
5406 eoinfo.finfo = &finfo;
5407 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5408 (PTR) &eoinfo);
5409 if (eoinfo.failed)
5410 return false;
5412 /* If backend needs to output some symbols not present in the hash
5413 table, do it now. */
5414 if (bed->elf_backend_output_arch_syms)
5416 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5417 Elf_Internal_Sym *,
5418 asection *));
5420 if (! ((*bed->elf_backend_output_arch_syms)
5421 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5422 return false;
5425 /* Flush all symbols to the file. */
5426 if (! elf_link_flush_output_syms (&finfo))
5427 return false;
5429 /* Now we know the size of the symtab section. */
5430 off += symtab_hdr->sh_size;
5432 /* Finish up and write out the symbol string table (.strtab)
5433 section. */
5434 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5435 /* sh_name was set in prep_headers. */
5436 symstrtab_hdr->sh_type = SHT_STRTAB;
5437 symstrtab_hdr->sh_flags = 0;
5438 symstrtab_hdr->sh_addr = 0;
5439 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5440 symstrtab_hdr->sh_entsize = 0;
5441 symstrtab_hdr->sh_link = 0;
5442 symstrtab_hdr->sh_info = 0;
5443 /* sh_offset is set just below. */
5444 symstrtab_hdr->sh_addralign = 1;
5446 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5447 elf_tdata (abfd)->next_file_pos = off;
5449 if (bfd_get_symcount (abfd) > 0)
5451 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5452 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5453 return false;
5456 /* Adjust the relocs to have the correct symbol indices. */
5457 for (o = abfd->sections; o != NULL; o = o->next)
5459 if ((o->flags & SEC_RELOC) == 0)
5460 continue;
5462 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5463 elf_section_data (o)->rel_count,
5464 elf_section_data (o)->rel_hashes);
5465 if (elf_section_data (o)->rel_hdr2 != NULL)
5466 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5467 elf_section_data (o)->rel_count2,
5468 (elf_section_data (o)->rel_hashes
5469 + elf_section_data (o)->rel_count));
5471 /* Set the reloc_count field to 0 to prevent write_relocs from
5472 trying to swap the relocs out itself. */
5473 o->reloc_count = 0;
5476 if (dynamic && info->combreloc && dynobj != NULL)
5477 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5479 /* If we are linking against a dynamic object, or generating a
5480 shared library, finish up the dynamic linking information. */
5481 if (dynamic)
5483 Elf_External_Dyn *dyncon, *dynconend;
5485 /* Fix up .dynamic entries. */
5486 o = bfd_get_section_by_name (dynobj, ".dynamic");
5487 BFD_ASSERT (o != NULL);
5489 dyncon = (Elf_External_Dyn *) o->contents;
5490 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5491 for (; dyncon < dynconend; dyncon++)
5493 Elf_Internal_Dyn dyn;
5494 const char *name;
5495 unsigned int type;
5497 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5499 switch (dyn.d_tag)
5501 default:
5502 break;
5503 case DT_NULL:
5504 if (relativecount > 0 && dyncon + 1 < dynconend)
5506 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5508 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5509 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5510 default: break;
5512 if (dyn.d_tag != DT_NULL)
5514 dyn.d_un.d_val = relativecount;
5515 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5516 relativecount = 0;
5519 break;
5520 case DT_INIT:
5521 name = info->init_function;
5522 goto get_sym;
5523 case DT_FINI:
5524 name = info->fini_function;
5525 get_sym:
5527 struct elf_link_hash_entry *h;
5529 h = elf_link_hash_lookup (elf_hash_table (info), name,
5530 false, false, true);
5531 if (h != NULL
5532 && (h->root.type == bfd_link_hash_defined
5533 || h->root.type == bfd_link_hash_defweak))
5535 dyn.d_un.d_val = h->root.u.def.value;
5536 o = h->root.u.def.section;
5537 if (o->output_section != NULL)
5538 dyn.d_un.d_val += (o->output_section->vma
5539 + o->output_offset);
5540 else
5542 /* The symbol is imported from another shared
5543 library and does not apply to this one. */
5544 dyn.d_un.d_val = 0;
5547 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5550 break;
5552 case DT_HASH:
5553 name = ".hash";
5554 goto get_vma;
5555 case DT_STRTAB:
5556 name = ".dynstr";
5557 goto get_vma;
5558 case DT_SYMTAB:
5559 name = ".dynsym";
5560 goto get_vma;
5561 case DT_VERDEF:
5562 name = ".gnu.version_d";
5563 goto get_vma;
5564 case DT_VERNEED:
5565 name = ".gnu.version_r";
5566 goto get_vma;
5567 case DT_VERSYM:
5568 name = ".gnu.version";
5569 get_vma:
5570 o = bfd_get_section_by_name (abfd, name);
5571 BFD_ASSERT (o != NULL);
5572 dyn.d_un.d_ptr = o->vma;
5573 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5574 break;
5576 case DT_REL:
5577 case DT_RELA:
5578 case DT_RELSZ:
5579 case DT_RELASZ:
5580 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5581 type = SHT_REL;
5582 else
5583 type = SHT_RELA;
5584 dyn.d_un.d_val = 0;
5585 for (i = 1; i < elf_numsections (abfd); i++)
5587 Elf_Internal_Shdr *hdr;
5589 hdr = elf_elfsections (abfd)[i];
5590 if (hdr->sh_type == type
5591 && (hdr->sh_flags & SHF_ALLOC) != 0)
5593 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5594 dyn.d_un.d_val += hdr->sh_size;
5595 else
5597 if (dyn.d_un.d_val == 0
5598 || hdr->sh_addr < dyn.d_un.d_val)
5599 dyn.d_un.d_val = hdr->sh_addr;
5603 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5604 break;
5609 /* If we have created any dynamic sections, then output them. */
5610 if (dynobj != NULL)
5612 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5613 goto error_return;
5615 for (o = dynobj->sections; o != NULL; o = o->next)
5617 if ((o->flags & SEC_HAS_CONTENTS) == 0
5618 || o->_raw_size == 0
5619 || o->output_section == bfd_abs_section_ptr)
5620 continue;
5621 if ((o->flags & SEC_LINKER_CREATED) == 0)
5623 /* At this point, we are only interested in sections
5624 created by elf_link_create_dynamic_sections. */
5625 continue;
5627 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5628 != SHT_STRTAB)
5629 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5631 if (! bfd_set_section_contents (abfd, o->output_section,
5632 o->contents,
5633 (file_ptr) o->output_offset,
5634 o->_raw_size))
5635 goto error_return;
5637 else
5639 /* The contents of the .dynstr section are actually in a
5640 stringtab. */
5641 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5642 if (bfd_seek (abfd, off, SEEK_SET) != 0
5643 || ! _bfd_elf_strtab_emit (abfd,
5644 elf_hash_table (info)->dynstr))
5645 goto error_return;
5650 /* If we have optimized stabs strings, output them. */
5651 if (elf_hash_table (info)->stab_info != NULL)
5653 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5654 goto error_return;
5657 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5659 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5660 ".eh_frame_hdr");
5661 if (o
5662 && (elf_section_data (o)->sec_info_type
5663 == ELF_INFO_TYPE_EH_FRAME_HDR))
5665 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5666 goto error_return;
5670 if (finfo.symstrtab != NULL)
5671 _bfd_stringtab_free (finfo.symstrtab);
5672 if (finfo.contents != NULL)
5673 free (finfo.contents);
5674 if (finfo.external_relocs != NULL)
5675 free (finfo.external_relocs);
5676 if (finfo.internal_relocs != NULL)
5677 free (finfo.internal_relocs);
5678 if (finfo.external_syms != NULL)
5679 free (finfo.external_syms);
5680 if (finfo.locsym_shndx != NULL)
5681 free (finfo.locsym_shndx);
5682 if (finfo.internal_syms != NULL)
5683 free (finfo.internal_syms);
5684 if (finfo.indices != NULL)
5685 free (finfo.indices);
5686 if (finfo.sections != NULL)
5687 free (finfo.sections);
5688 if (finfo.symbuf != NULL)
5689 free (finfo.symbuf);
5690 if (finfo.symshndxbuf != NULL)
5691 free (finfo.symbuf);
5692 for (o = abfd->sections; o != NULL; o = o->next)
5694 if ((o->flags & SEC_RELOC) != 0
5695 && elf_section_data (o)->rel_hashes != NULL)
5696 free (elf_section_data (o)->rel_hashes);
5699 elf_tdata (abfd)->linker = true;
5701 return true;
5703 error_return:
5704 if (finfo.symstrtab != NULL)
5705 _bfd_stringtab_free (finfo.symstrtab);
5706 if (finfo.contents != NULL)
5707 free (finfo.contents);
5708 if (finfo.external_relocs != NULL)
5709 free (finfo.external_relocs);
5710 if (finfo.internal_relocs != NULL)
5711 free (finfo.internal_relocs);
5712 if (finfo.external_syms != NULL)
5713 free (finfo.external_syms);
5714 if (finfo.locsym_shndx != NULL)
5715 free (finfo.locsym_shndx);
5716 if (finfo.internal_syms != NULL)
5717 free (finfo.internal_syms);
5718 if (finfo.indices != NULL)
5719 free (finfo.indices);
5720 if (finfo.sections != NULL)
5721 free (finfo.sections);
5722 if (finfo.symbuf != NULL)
5723 free (finfo.symbuf);
5724 if (finfo.symshndxbuf != NULL)
5725 free (finfo.symbuf);
5726 for (o = abfd->sections; o != NULL; o = o->next)
5728 if ((o->flags & SEC_RELOC) != 0
5729 && elf_section_data (o)->rel_hashes != NULL)
5730 free (elf_section_data (o)->rel_hashes);
5733 return false;
5736 /* Add a symbol to the output symbol table. */
5738 static boolean
5739 elf_link_output_sym (finfo, name, elfsym, input_sec)
5740 struct elf_final_link_info *finfo;
5741 const char *name;
5742 Elf_Internal_Sym *elfsym;
5743 asection *input_sec;
5745 Elf_External_Sym *dest;
5746 Elf_External_Sym_Shndx *destshndx;
5748 boolean (*output_symbol_hook) PARAMS ((bfd *,
5749 struct bfd_link_info *info,
5750 const char *,
5751 Elf_Internal_Sym *,
5752 asection *));
5754 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5755 elf_backend_link_output_symbol_hook;
5756 if (output_symbol_hook != NULL)
5758 if (! ((*output_symbol_hook)
5759 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5760 return false;
5763 if (name == (const char *) NULL || *name == '\0')
5764 elfsym->st_name = 0;
5765 else if (input_sec->flags & SEC_EXCLUDE)
5766 elfsym->st_name = 0;
5767 else
5769 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5770 name, true, false);
5771 if (elfsym->st_name == (unsigned long) -1)
5772 return false;
5775 if (finfo->symbuf_count >= finfo->symbuf_size)
5777 if (! elf_link_flush_output_syms (finfo))
5778 return false;
5781 dest = finfo->symbuf + finfo->symbuf_count;
5782 destshndx = finfo->symshndxbuf;
5783 if (destshndx != NULL)
5784 destshndx += finfo->symbuf_count;
5785 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5786 ++finfo->symbuf_count;
5788 ++ bfd_get_symcount (finfo->output_bfd);
5790 return true;
5793 /* Flush the output symbols to the file. */
5795 static boolean
5796 elf_link_flush_output_syms (finfo)
5797 struct elf_final_link_info *finfo;
5799 if (finfo->symbuf_count > 0)
5801 Elf_Internal_Shdr *hdr;
5802 file_ptr pos;
5803 bfd_size_type amt;
5805 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5806 pos = hdr->sh_offset + hdr->sh_size;
5807 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5808 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5809 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5810 return false;
5812 hdr->sh_size += amt;
5814 if (finfo->symshndxbuf != NULL)
5816 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5817 pos = hdr->sh_offset + hdr->sh_size;
5818 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5819 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5820 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5821 != amt))
5822 return false;
5824 hdr->sh_size += amt;
5827 finfo->symbuf_count = 0;
5830 return true;
5833 /* Adjust all external symbols pointing into SEC_MERGE sections
5834 to reflect the object merging within the sections. */
5836 static boolean
5837 elf_link_sec_merge_syms (h, data)
5838 struct elf_link_hash_entry *h;
5839 PTR data;
5841 asection *sec;
5843 if ((h->root.type == bfd_link_hash_defined
5844 || h->root.type == bfd_link_hash_defweak)
5845 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5846 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
5848 bfd *output_bfd = (bfd *) data;
5850 h->root.u.def.value =
5851 _bfd_merged_section_offset (output_bfd,
5852 &h->root.u.def.section,
5853 elf_section_data (sec)->sec_info,
5854 h->root.u.def.value, (bfd_vma) 0);
5857 return true;
5860 /* Add an external symbol to the symbol table. This is called from
5861 the hash table traversal routine. When generating a shared object,
5862 we go through the symbol table twice. The first time we output
5863 anything that might have been forced to local scope in a version
5864 script. The second time we output the symbols that are still
5865 global symbols. */
5867 static boolean
5868 elf_link_output_extsym (h, data)
5869 struct elf_link_hash_entry *h;
5870 PTR data;
5872 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5873 struct elf_final_link_info *finfo = eoinfo->finfo;
5874 boolean strip;
5875 Elf_Internal_Sym sym;
5876 asection *input_sec;
5878 /* Decide whether to output this symbol in this pass. */
5879 if (eoinfo->localsyms)
5881 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5882 return true;
5884 else
5886 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5887 return true;
5890 /* If we are not creating a shared library, and this symbol is
5891 referenced by a shared library but is not defined anywhere, then
5892 warn that it is undefined. If we do not do this, the runtime
5893 linker will complain that the symbol is undefined when the
5894 program is run. We don't have to worry about symbols that are
5895 referenced by regular files, because we will already have issued
5896 warnings for them. */
5897 if (! finfo->info->relocateable
5898 && ! finfo->info->allow_shlib_undefined
5899 && ! finfo->info->shared
5900 && h->root.type == bfd_link_hash_undefined
5901 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5902 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5904 if (! ((*finfo->info->callbacks->undefined_symbol)
5905 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5906 (asection *) NULL, (bfd_vma) 0, true)))
5908 eoinfo->failed = true;
5909 return false;
5913 /* We don't want to output symbols that have never been mentioned by
5914 a regular file, or that we have been told to strip. However, if
5915 h->indx is set to -2, the symbol is used by a reloc and we must
5916 output it. */
5917 if (h->indx == -2)
5918 strip = false;
5919 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5920 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5921 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5922 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5923 strip = true;
5924 else if (finfo->info->strip == strip_all
5925 || (finfo->info->strip == strip_some
5926 && bfd_hash_lookup (finfo->info->keep_hash,
5927 h->root.root.string,
5928 false, false) == NULL))
5929 strip = true;
5930 else
5931 strip = false;
5933 /* If we're stripping it, and it's not a dynamic symbol, there's
5934 nothing else to do unless it is a forced local symbol. */
5935 if (strip
5936 && h->dynindx == -1
5937 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5938 return true;
5940 sym.st_value = 0;
5941 sym.st_size = h->size;
5942 sym.st_other = h->other;
5943 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5944 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5945 else if (h->root.type == bfd_link_hash_undefweak
5946 || h->root.type == bfd_link_hash_defweak)
5947 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5948 else
5949 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5951 switch (h->root.type)
5953 default:
5954 case bfd_link_hash_new:
5955 abort ();
5956 return false;
5958 case bfd_link_hash_undefined:
5959 input_sec = bfd_und_section_ptr;
5960 sym.st_shndx = SHN_UNDEF;
5961 break;
5963 case bfd_link_hash_undefweak:
5964 input_sec = bfd_und_section_ptr;
5965 sym.st_shndx = SHN_UNDEF;
5966 break;
5968 case bfd_link_hash_defined:
5969 case bfd_link_hash_defweak:
5971 input_sec = h->root.u.def.section;
5972 if (input_sec->output_section != NULL)
5974 sym.st_shndx =
5975 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5976 input_sec->output_section);
5977 if (sym.st_shndx == SHN_BAD)
5979 (*_bfd_error_handler)
5980 (_("%s: could not find output section %s for input section %s"),
5981 bfd_get_filename (finfo->output_bfd),
5982 input_sec->output_section->name,
5983 input_sec->name);
5984 eoinfo->failed = true;
5985 return false;
5988 /* ELF symbols in relocateable files are section relative,
5989 but in nonrelocateable files they are virtual
5990 addresses. */
5991 sym.st_value = h->root.u.def.value + input_sec->output_offset;
5992 if (! finfo->info->relocateable)
5993 sym.st_value += input_sec->output_section->vma;
5995 else
5997 BFD_ASSERT (input_sec->owner == NULL
5998 || (input_sec->owner->flags & DYNAMIC) != 0);
5999 sym.st_shndx = SHN_UNDEF;
6000 input_sec = bfd_und_section_ptr;
6003 break;
6005 case bfd_link_hash_common:
6006 input_sec = h->root.u.c.p->section;
6007 sym.st_shndx = SHN_COMMON;
6008 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6009 break;
6011 case bfd_link_hash_indirect:
6012 /* These symbols are created by symbol versioning. They point
6013 to the decorated version of the name. For example, if the
6014 symbol foo@@GNU_1.2 is the default, which should be used when
6015 foo is used with no version, then we add an indirect symbol
6016 foo which points to foo@@GNU_1.2. We ignore these symbols,
6017 since the indirected symbol is already in the hash table. */
6018 return true;
6020 case bfd_link_hash_warning:
6021 /* We can't represent these symbols in ELF, although a warning
6022 symbol may have come from a .gnu.warning.SYMBOL section. We
6023 just put the target symbol in the hash table. If the target
6024 symbol does not really exist, don't do anything. */
6025 if (h->root.u.i.link->type == bfd_link_hash_new)
6026 return true;
6027 return (elf_link_output_extsym
6028 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
6031 /* Give the processor backend a chance to tweak the symbol value,
6032 and also to finish up anything that needs to be done for this
6033 symbol. */
6034 if ((h->dynindx != -1
6035 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6036 && elf_hash_table (finfo->info)->dynamic_sections_created)
6038 struct elf_backend_data *bed;
6040 bed = get_elf_backend_data (finfo->output_bfd);
6041 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6042 (finfo->output_bfd, finfo->info, h, &sym)))
6044 eoinfo->failed = true;
6045 return false;
6049 /* If we are marking the symbol as undefined, and there are no
6050 non-weak references to this symbol from a regular object, then
6051 mark the symbol as weak undefined; if there are non-weak
6052 references, mark the symbol as strong. We can't do this earlier,
6053 because it might not be marked as undefined until the
6054 finish_dynamic_symbol routine gets through with it. */
6055 if (sym.st_shndx == SHN_UNDEF
6056 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6057 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6058 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6060 int bindtype;
6062 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6063 bindtype = STB_GLOBAL;
6064 else
6065 bindtype = STB_WEAK;
6066 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6069 /* If a symbol is not defined locally, we clear the visibility
6070 field. */
6071 if (! finfo->info->relocateable
6072 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6073 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6075 /* If this symbol should be put in the .dynsym section, then put it
6076 there now. We have already know the symbol index. We also fill
6077 in the entry in the .hash section. */
6078 if (h->dynindx != -1
6079 && elf_hash_table (finfo->info)->dynamic_sections_created)
6081 size_t bucketcount;
6082 size_t bucket;
6083 size_t hash_entry_size;
6084 bfd_byte *bucketpos;
6085 bfd_vma chain;
6086 Elf_External_Sym *esym;
6088 sym.st_name = h->dynstr_index;
6089 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6090 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6092 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6093 bucket = h->elf_hash_value % bucketcount;
6094 hash_entry_size
6095 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6096 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6097 + (bucket + 2) * hash_entry_size);
6098 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6099 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6100 bucketpos);
6101 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6102 ((bfd_byte *) finfo->hash_sec->contents
6103 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6105 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6107 Elf_Internal_Versym iversym;
6108 Elf_External_Versym *eversym;
6110 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6112 if (h->verinfo.verdef == NULL)
6113 iversym.vs_vers = 0;
6114 else
6115 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6117 else
6119 if (h->verinfo.vertree == NULL)
6120 iversym.vs_vers = 1;
6121 else
6122 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6125 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6126 iversym.vs_vers |= VERSYM_HIDDEN;
6128 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6129 eversym += h->dynindx;
6130 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6134 /* If we're stripping it, then it was just a dynamic symbol, and
6135 there's nothing else to do. */
6136 if (strip)
6137 return true;
6139 h->indx = bfd_get_symcount (finfo->output_bfd);
6141 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6143 eoinfo->failed = true;
6144 return false;
6147 return true;
6150 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6151 originated from the section given by INPUT_REL_HDR) to the
6152 OUTPUT_BFD. */
6154 static void
6155 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6156 internal_relocs)
6157 bfd *output_bfd;
6158 asection *input_section;
6159 Elf_Internal_Shdr *input_rel_hdr;
6160 Elf_Internal_Rela *internal_relocs;
6162 Elf_Internal_Rela *irela;
6163 Elf_Internal_Rela *irelaend;
6164 Elf_Internal_Shdr *output_rel_hdr;
6165 asection *output_section;
6166 unsigned int *rel_countp = NULL;
6167 struct elf_backend_data *bed;
6168 bfd_size_type amt;
6170 output_section = input_section->output_section;
6171 output_rel_hdr = NULL;
6173 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6174 == input_rel_hdr->sh_entsize)
6176 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6177 rel_countp = &elf_section_data (output_section)->rel_count;
6179 else if (elf_section_data (output_section)->rel_hdr2
6180 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6181 == input_rel_hdr->sh_entsize))
6183 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6184 rel_countp = &elf_section_data (output_section)->rel_count2;
6187 BFD_ASSERT (output_rel_hdr != NULL);
6189 bed = get_elf_backend_data (output_bfd);
6190 irela = internal_relocs;
6191 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6192 * bed->s->int_rels_per_ext_rel;
6194 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6196 Elf_External_Rel *erel;
6197 Elf_Internal_Rel *irel;
6199 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6200 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6201 if (irel == NULL)
6203 (*_bfd_error_handler) (_("Error: out of memory"));
6204 abort ();
6207 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6208 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6210 unsigned int i;
6212 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6214 irel[i].r_offset = irela[i].r_offset;
6215 irel[i].r_info = irela[i].r_info;
6216 BFD_ASSERT (irela[i].r_addend == 0);
6219 if (bed->s->swap_reloc_out)
6220 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6221 else
6222 elf_swap_reloc_out (output_bfd, irel, erel);
6225 free (irel);
6227 else
6229 Elf_External_Rela *erela;
6231 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6233 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6234 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6235 if (bed->s->swap_reloca_out)
6236 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6237 else
6238 elf_swap_reloca_out (output_bfd, irela, erela);
6241 /* Bump the counter, so that we know where to add the next set of
6242 relocations. */
6243 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6246 /* Link an input file into the linker output file. This function
6247 handles all the sections and relocations of the input file at once.
6248 This is so that we only have to read the local symbols once, and
6249 don't have to keep them in memory. */
6251 static boolean
6252 elf_link_input_bfd (finfo, input_bfd)
6253 struct elf_final_link_info *finfo;
6254 bfd *input_bfd;
6256 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6257 bfd *, asection *, bfd_byte *,
6258 Elf_Internal_Rela *,
6259 Elf_Internal_Sym *, asection **));
6260 bfd *output_bfd;
6261 Elf_Internal_Shdr *symtab_hdr;
6262 Elf_Internal_Shdr *shndx_hdr;
6263 size_t locsymcount;
6264 size_t extsymoff;
6265 Elf_External_Sym *external_syms;
6266 Elf_External_Sym *esym;
6267 Elf_External_Sym *esymend;
6268 Elf_External_Sym_Shndx *shndx_buf;
6269 Elf_External_Sym_Shndx *shndx;
6270 Elf_Internal_Sym *isym;
6271 long *pindex;
6272 asection **ppsection;
6273 asection *o;
6274 struct elf_backend_data *bed;
6275 boolean emit_relocs;
6276 struct elf_link_hash_entry **sym_hashes;
6278 output_bfd = finfo->output_bfd;
6279 bed = get_elf_backend_data (output_bfd);
6280 relocate_section = bed->elf_backend_relocate_section;
6282 /* If this is a dynamic object, we don't want to do anything here:
6283 we don't want the local symbols, and we don't want the section
6284 contents. */
6285 if ((input_bfd->flags & DYNAMIC) != 0)
6286 return true;
6288 emit_relocs = (finfo->info->relocateable
6289 || finfo->info->emitrelocations
6290 || bed->elf_backend_emit_relocs);
6292 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6293 if (elf_bad_symtab (input_bfd))
6295 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6296 extsymoff = 0;
6298 else
6300 locsymcount = symtab_hdr->sh_info;
6301 extsymoff = symtab_hdr->sh_info;
6304 /* Read the local symbols. */
6305 if (symtab_hdr->contents != NULL)
6306 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6307 else if (locsymcount == 0)
6308 external_syms = NULL;
6309 else
6311 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6312 external_syms = finfo->external_syms;
6313 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6314 || bfd_bread (external_syms, amt, input_bfd) != amt)
6315 return false;
6318 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6319 shndx_buf = NULL;
6320 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6322 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6323 shndx_buf = finfo->locsym_shndx;
6324 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6325 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6326 return false;
6329 /* Swap in the local symbols and write out the ones which we know
6330 are going into the output file. */
6331 for (esym = external_syms, esymend = esym + locsymcount,
6332 isym = finfo->internal_syms, pindex = finfo->indices,
6333 ppsection = finfo->sections, shndx = shndx_buf;
6334 esym < esymend;
6335 esym++, isym++, pindex++, ppsection++,
6336 shndx = (shndx != NULL ? shndx + 1 : NULL))
6338 asection *isec;
6339 const char *name;
6340 Elf_Internal_Sym osym;
6342 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
6343 *pindex = -1;
6345 if (elf_bad_symtab (input_bfd))
6347 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6349 *ppsection = NULL;
6350 continue;
6354 if (isym->st_shndx == SHN_UNDEF)
6355 isec = bfd_und_section_ptr;
6356 else if (isym->st_shndx < SHN_LORESERVE
6357 || isym->st_shndx > SHN_HIRESERVE)
6359 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6360 if (isec
6361 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6362 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6363 isym->st_value =
6364 _bfd_merged_section_offset (output_bfd, &isec,
6365 elf_section_data (isec)->sec_info,
6366 isym->st_value, (bfd_vma) 0);
6368 else if (isym->st_shndx == SHN_ABS)
6369 isec = bfd_abs_section_ptr;
6370 else if (isym->st_shndx == SHN_COMMON)
6371 isec = bfd_com_section_ptr;
6372 else
6374 /* Who knows? */
6375 isec = NULL;
6378 *ppsection = isec;
6380 /* Don't output the first, undefined, symbol. */
6381 if (esym == external_syms)
6382 continue;
6384 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6386 /* We never output section symbols. Instead, we use the
6387 section symbol of the corresponding section in the output
6388 file. */
6389 continue;
6392 /* If we are stripping all symbols, we don't want to output this
6393 one. */
6394 if (finfo->info->strip == strip_all)
6395 continue;
6397 /* If we are discarding all local symbols, we don't want to
6398 output this one. If we are generating a relocateable output
6399 file, then some of the local symbols may be required by
6400 relocs; we output them below as we discover that they are
6401 needed. */
6402 if (finfo->info->discard == discard_all)
6403 continue;
6405 /* If this symbol is defined in a section which we are
6406 discarding, we don't need to keep it, but note that
6407 linker_mark is only reliable for sections that have contents.
6408 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6409 as well as linker_mark. */
6410 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6411 && isec != NULL
6412 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6413 || (! finfo->info->relocateable
6414 && (isec->flags & SEC_EXCLUDE) != 0)))
6415 continue;
6417 /* Get the name of the symbol. */
6418 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6419 isym->st_name);
6420 if (name == NULL)
6421 return false;
6423 /* See if we are discarding symbols with this name. */
6424 if ((finfo->info->strip == strip_some
6425 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6426 == NULL))
6427 || (((finfo->info->discard == discard_sec_merge
6428 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6429 || finfo->info->discard == discard_l)
6430 && bfd_is_local_label_name (input_bfd, name)))
6431 continue;
6433 /* If we get here, we are going to output this symbol. */
6435 osym = *isym;
6437 /* Adjust the section index for the output file. */
6438 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6439 isec->output_section);
6440 if (osym.st_shndx == SHN_BAD)
6441 return false;
6443 *pindex = bfd_get_symcount (output_bfd);
6445 /* ELF symbols in relocateable files are section relative, but
6446 in executable files they are virtual addresses. Note that
6447 this code assumes that all ELF sections have an associated
6448 BFD section with a reasonable value for output_offset; below
6449 we assume that they also have a reasonable value for
6450 output_section. Any special sections must be set up to meet
6451 these requirements. */
6452 osym.st_value += isec->output_offset;
6453 if (! finfo->info->relocateable)
6454 osym.st_value += isec->output_section->vma;
6456 if (! elf_link_output_sym (finfo, name, &osym, isec))
6457 return false;
6460 /* Relocate the contents of each section. */
6461 sym_hashes = elf_sym_hashes (input_bfd);
6462 for (o = input_bfd->sections; o != NULL; o = o->next)
6464 bfd_byte *contents;
6466 if (! o->linker_mark)
6468 /* This section was omitted from the link. */
6469 continue;
6472 if ((o->flags & SEC_HAS_CONTENTS) == 0
6473 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6474 continue;
6476 if ((o->flags & SEC_LINKER_CREATED) != 0)
6478 /* Section was created by elf_link_create_dynamic_sections
6479 or somesuch. */
6480 continue;
6483 /* Get the contents of the section. They have been cached by a
6484 relaxation routine. Note that o is a section in an input
6485 file, so the contents field will not have been set by any of
6486 the routines which work on output files. */
6487 if (elf_section_data (o)->this_hdr.contents != NULL)
6488 contents = elf_section_data (o)->this_hdr.contents;
6489 else
6491 contents = finfo->contents;
6492 if (! bfd_get_section_contents (input_bfd, o, contents,
6493 (file_ptr) 0, o->_raw_size))
6494 return false;
6497 if ((o->flags & SEC_RELOC) != 0)
6499 Elf_Internal_Rela *internal_relocs;
6501 /* Get the swapped relocs. */
6502 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6503 (input_bfd, o, finfo->external_relocs,
6504 finfo->internal_relocs, false));
6505 if (internal_relocs == NULL
6506 && o->reloc_count > 0)
6507 return false;
6509 /* Run through the relocs looking for any against symbols
6510 from discarded sections and section symbols from
6511 removed link-once sections. Complain about relocs
6512 against discarded sections. Zero relocs against removed
6513 link-once sections. We should really complain if
6514 anything in the final link tries to use it, but
6515 DWARF-based exception handling might have an entry in
6516 .eh_frame to describe a routine in the linkonce section,
6517 and it turns out to be hard to remove the .eh_frame
6518 entry too. FIXME. */
6519 if (!finfo->info->relocateable
6520 && !elf_section_ignore_discarded_relocs (o))
6522 Elf_Internal_Rela *rel, *relend;
6524 rel = internal_relocs;
6525 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6526 for ( ; rel < relend; rel++)
6528 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6530 if (r_symndx >= locsymcount
6531 || (elf_bad_symtab (input_bfd)
6532 && finfo->sections[r_symndx] == NULL))
6534 struct elf_link_hash_entry *h;
6536 h = sym_hashes[r_symndx - extsymoff];
6537 while (h->root.type == bfd_link_hash_indirect
6538 || h->root.type == bfd_link_hash_warning)
6539 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6541 /* Complain if the definition comes from a
6542 discarded section. */
6543 if ((h->root.type == bfd_link_hash_defined
6544 || h->root.type == bfd_link_hash_defweak)
6545 && elf_discarded_section (h->root.u.def.section))
6547 #if BFD_VERSION_DATE < 20031005
6548 if ((o->flags & SEC_DEBUGGING) != 0)
6550 #if BFD_VERSION_DATE > 20021005
6551 (*finfo->info->callbacks->warning)
6552 (finfo->info,
6553 _("warning: relocation against removed section; zeroing"),
6554 NULL, input_bfd, o, rel->r_offset);
6555 #endif
6556 BFD_ASSERT (r_symndx != 0);
6557 memset (rel, 0, sizeof (*rel));
6559 else
6560 #endif
6562 if (! ((*finfo->info->callbacks->undefined_symbol)
6563 (finfo->info, h->root.root.string,
6564 input_bfd, o, rel->r_offset,
6565 true)))
6566 return false;
6570 else
6572 asection *sec = finfo->sections[r_symndx];
6574 if (sec != NULL && elf_discarded_section (sec))
6576 #if BFD_VERSION_DATE < 20031005
6577 if ((o->flags & SEC_DEBUGGING) != 0
6578 || (sec->flags & SEC_LINK_ONCE) != 0)
6580 #if BFD_VERSION_DATE > 20021005
6581 (*finfo->info->callbacks->warning)
6582 (finfo->info,
6583 _("warning: relocation against removed section"),
6584 NULL, input_bfd, o, rel->r_offset);
6585 #endif
6586 BFD_ASSERT (r_symndx != 0);
6587 rel->r_info
6588 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6589 rel->r_addend = 0;
6591 else
6592 #endif
6594 boolean ok;
6595 const char *msg
6596 = _("local symbols in discarded section %s");
6597 bfd_size_type amt
6598 = strlen (sec->name) + strlen (msg) - 1;
6599 char *buf = (char *) bfd_malloc (amt);
6601 if (buf != NULL)
6602 sprintf (buf, msg, sec->name);
6603 else
6604 buf = (char *) sec->name;
6605 ok = (*finfo->info->callbacks
6606 ->undefined_symbol) (finfo->info, buf,
6607 input_bfd, o,
6608 rel->r_offset,
6609 true);
6610 if (buf != sec->name)
6611 free (buf);
6612 if (!ok)
6613 return false;
6620 /* Relocate the section by invoking a back end routine.
6622 The back end routine is responsible for adjusting the
6623 section contents as necessary, and (if using Rela relocs
6624 and generating a relocateable output file) adjusting the
6625 reloc addend as necessary.
6627 The back end routine does not have to worry about setting
6628 the reloc address or the reloc symbol index.
6630 The back end routine is given a pointer to the swapped in
6631 internal symbols, and can access the hash table entries
6632 for the external symbols via elf_sym_hashes (input_bfd).
6634 When generating relocateable output, the back end routine
6635 must handle STB_LOCAL/STT_SECTION symbols specially. The
6636 output symbol is going to be a section symbol
6637 corresponding to the output section, which will require
6638 the addend to be adjusted. */
6640 if (! (*relocate_section) (output_bfd, finfo->info,
6641 input_bfd, o, contents,
6642 internal_relocs,
6643 finfo->internal_syms,
6644 finfo->sections))
6645 return false;
6647 if (emit_relocs)
6649 Elf_Internal_Rela *irela;
6650 Elf_Internal_Rela *irelaend;
6651 struct elf_link_hash_entry **rel_hash;
6652 Elf_Internal_Shdr *input_rel_hdr;
6653 unsigned int next_erel;
6654 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6655 Elf_Internal_Shdr *,
6656 Elf_Internal_Rela *));
6658 /* Adjust the reloc addresses and symbol indices. */
6660 irela = internal_relocs;
6661 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6662 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6663 + elf_section_data (o->output_section)->rel_count
6664 + elf_section_data (o->output_section)->rel_count2);
6665 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6667 unsigned long r_symndx;
6668 asection *sec;
6670 if (next_erel == bed->s->int_rels_per_ext_rel)
6672 rel_hash++;
6673 next_erel = 0;
6676 irela->r_offset += o->output_offset;
6678 /* Relocs in an executable have to be virtual addresses. */
6679 if (finfo->info->emitrelocations)
6680 irela->r_offset += o->output_section->vma;
6682 r_symndx = ELF_R_SYM (irela->r_info);
6684 if (r_symndx == 0)
6685 continue;
6687 if (r_symndx >= locsymcount
6688 || (elf_bad_symtab (input_bfd)
6689 && finfo->sections[r_symndx] == NULL))
6691 struct elf_link_hash_entry *rh;
6692 unsigned long indx;
6694 /* This is a reloc against a global symbol. We
6695 have not yet output all the local symbols, so
6696 we do not know the symbol index of any global
6697 symbol. We set the rel_hash entry for this
6698 reloc to point to the global hash table entry
6699 for this symbol. The symbol index is then
6700 set at the end of elf_bfd_final_link. */
6701 indx = r_symndx - extsymoff;
6702 rh = elf_sym_hashes (input_bfd)[indx];
6703 while (rh->root.type == bfd_link_hash_indirect
6704 || rh->root.type == bfd_link_hash_warning)
6705 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6707 /* Setting the index to -2 tells
6708 elf_link_output_extsym that this symbol is
6709 used by a reloc. */
6710 BFD_ASSERT (rh->indx < 0);
6711 rh->indx = -2;
6713 *rel_hash = rh;
6715 continue;
6718 /* This is a reloc against a local symbol. */
6720 *rel_hash = NULL;
6721 isym = finfo->internal_syms + r_symndx;
6722 sec = finfo->sections[r_symndx];
6723 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6725 /* I suppose the backend ought to fill in the
6726 section of any STT_SECTION symbol against a
6727 processor specific section. If we have
6728 discarded a section, the output_section will
6729 be the absolute section. */
6730 if (sec != NULL
6731 && (bfd_is_abs_section (sec)
6732 || (sec->output_section != NULL
6733 && bfd_is_abs_section (sec->output_section))))
6734 r_symndx = 0;
6735 else if (sec == NULL || sec->owner == NULL)
6737 bfd_set_error (bfd_error_bad_value);
6738 return false;
6740 else
6742 r_symndx = sec->output_section->target_index;
6743 BFD_ASSERT (r_symndx != 0);
6746 else
6748 if (finfo->indices[r_symndx] == -1)
6750 unsigned long shlink;
6751 const char *name;
6752 asection *osec;
6754 if (finfo->info->strip == strip_all)
6756 /* You can't do ld -r -s. */
6757 bfd_set_error (bfd_error_invalid_operation);
6758 return false;
6761 /* This symbol was skipped earlier, but
6762 since it is needed by a reloc, we
6763 must output it now. */
6764 shlink = symtab_hdr->sh_link;
6765 name = (bfd_elf_string_from_elf_section
6766 (input_bfd, shlink, isym->st_name));
6767 if (name == NULL)
6768 return false;
6770 osec = sec->output_section;
6771 isym->st_shndx =
6772 _bfd_elf_section_from_bfd_section (output_bfd,
6773 osec);
6774 if (isym->st_shndx == SHN_BAD)
6775 return false;
6777 isym->st_value += sec->output_offset;
6778 if (! finfo->info->relocateable)
6779 isym->st_value += osec->vma;
6781 finfo->indices[r_symndx]
6782 = bfd_get_symcount (output_bfd);
6784 if (! elf_link_output_sym (finfo, name, isym, sec))
6785 return false;
6788 r_symndx = finfo->indices[r_symndx];
6791 irela->r_info = ELF_R_INFO (r_symndx,
6792 ELF_R_TYPE (irela->r_info));
6795 /* Swap out the relocs. */
6796 if (bed->elf_backend_emit_relocs
6797 && !(finfo->info->relocateable
6798 || finfo->info->emitrelocations))
6799 reloc_emitter = bed->elf_backend_emit_relocs;
6800 else
6801 reloc_emitter = elf_link_output_relocs;
6803 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6804 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6806 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6807 if (input_rel_hdr)
6809 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6810 * bed->s->int_rels_per_ext_rel);
6811 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6817 /* Write out the modified section contents. */
6818 if (bed->elf_backend_write_section
6819 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6821 /* Section written out. */
6823 else switch (elf_section_data (o)->sec_info_type)
6825 case ELF_INFO_TYPE_STABS:
6826 if (! (_bfd_write_section_stabs
6827 (output_bfd,
6828 &elf_hash_table (finfo->info)->stab_info,
6829 o, &elf_section_data (o)->sec_info, contents)))
6830 return false;
6831 break;
6832 case ELF_INFO_TYPE_MERGE:
6833 if (! (_bfd_write_merged_section
6834 (output_bfd, o, elf_section_data (o)->sec_info)))
6835 return false;
6836 break;
6837 case ELF_INFO_TYPE_EH_FRAME:
6839 asection *ehdrsec;
6841 ehdrsec
6842 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6843 ".eh_frame_hdr");
6844 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6845 contents)))
6846 return false;
6848 break;
6849 default:
6851 bfd_size_type sec_size;
6853 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6854 if (! (o->flags & SEC_EXCLUDE)
6855 && ! bfd_set_section_contents (output_bfd, o->output_section,
6856 contents,
6857 (file_ptr) o->output_offset,
6858 sec_size))
6859 return false;
6861 break;
6865 return true;
6868 /* Generate a reloc when linking an ELF file. This is a reloc
6869 requested by the linker, and does come from any input file. This
6870 is used to build constructor and destructor tables when linking
6871 with -Ur. */
6873 static boolean
6874 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6875 bfd *output_bfd;
6876 struct bfd_link_info *info;
6877 asection *output_section;
6878 struct bfd_link_order *link_order;
6880 reloc_howto_type *howto;
6881 long indx;
6882 bfd_vma offset;
6883 bfd_vma addend;
6884 struct elf_link_hash_entry **rel_hash_ptr;
6885 Elf_Internal_Shdr *rel_hdr;
6886 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6888 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6889 if (howto == NULL)
6891 bfd_set_error (bfd_error_bad_value);
6892 return false;
6895 addend = link_order->u.reloc.p->addend;
6897 /* Figure out the symbol index. */
6898 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6899 + elf_section_data (output_section)->rel_count
6900 + elf_section_data (output_section)->rel_count2);
6901 if (link_order->type == bfd_section_reloc_link_order)
6903 indx = link_order->u.reloc.p->u.section->target_index;
6904 BFD_ASSERT (indx != 0);
6905 *rel_hash_ptr = NULL;
6907 else
6909 struct elf_link_hash_entry *h;
6911 /* Treat a reloc against a defined symbol as though it were
6912 actually against the section. */
6913 h = ((struct elf_link_hash_entry *)
6914 bfd_wrapped_link_hash_lookup (output_bfd, info,
6915 link_order->u.reloc.p->u.name,
6916 false, false, true));
6917 if (h != NULL
6918 && (h->root.type == bfd_link_hash_defined
6919 || h->root.type == bfd_link_hash_defweak))
6921 asection *section;
6923 section = h->root.u.def.section;
6924 indx = section->output_section->target_index;
6925 *rel_hash_ptr = NULL;
6926 /* It seems that we ought to add the symbol value to the
6927 addend here, but in practice it has already been added
6928 because it was passed to constructor_callback. */
6929 addend += section->output_section->vma + section->output_offset;
6931 else if (h != NULL)
6933 /* Setting the index to -2 tells elf_link_output_extsym that
6934 this symbol is used by a reloc. */
6935 h->indx = -2;
6936 *rel_hash_ptr = h;
6937 indx = 0;
6939 else
6941 if (! ((*info->callbacks->unattached_reloc)
6942 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6943 (asection *) NULL, (bfd_vma) 0)))
6944 return false;
6945 indx = 0;
6949 /* If this is an inplace reloc, we must write the addend into the
6950 object file. */
6951 if (howto->partial_inplace && addend != 0)
6953 bfd_size_type size;
6954 bfd_reloc_status_type rstat;
6955 bfd_byte *buf;
6956 boolean ok;
6957 const char *sym_name;
6959 size = bfd_get_reloc_size (howto);
6960 buf = (bfd_byte *) bfd_zmalloc (size);
6961 if (buf == (bfd_byte *) NULL)
6962 return false;
6963 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6964 switch (rstat)
6966 case bfd_reloc_ok:
6967 break;
6969 default:
6970 case bfd_reloc_outofrange:
6971 abort ();
6973 case bfd_reloc_overflow:
6974 if (link_order->type == bfd_section_reloc_link_order)
6975 sym_name = bfd_section_name (output_bfd,
6976 link_order->u.reloc.p->u.section);
6977 else
6978 sym_name = link_order->u.reloc.p->u.name;
6979 if (! ((*info->callbacks->reloc_overflow)
6980 (info, sym_name, howto->name, addend,
6981 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6983 free (buf);
6984 return false;
6986 break;
6988 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6989 (file_ptr) link_order->offset, size);
6990 free (buf);
6991 if (! ok)
6992 return false;
6995 /* The address of a reloc is relative to the section in a
6996 relocateable file, and is a virtual address in an executable
6997 file. */
6998 offset = link_order->offset;
6999 if (! info->relocateable)
7000 offset += output_section->vma;
7002 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7004 if (rel_hdr->sh_type == SHT_REL)
7006 bfd_size_type size;
7007 Elf_Internal_Rel *irel;
7008 Elf_External_Rel *erel;
7009 unsigned int i;
7011 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7012 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7013 if (irel == NULL)
7014 return false;
7016 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7017 irel[i].r_offset = offset;
7018 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7020 erel = ((Elf_External_Rel *) rel_hdr->contents
7021 + elf_section_data (output_section)->rel_count);
7023 if (bed->s->swap_reloc_out)
7024 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7025 else
7026 elf_swap_reloc_out (output_bfd, irel, erel);
7028 free (irel);
7030 else
7032 bfd_size_type size;
7033 Elf_Internal_Rela *irela;
7034 Elf_External_Rela *erela;
7035 unsigned int i;
7037 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7038 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7039 if (irela == NULL)
7040 return false;
7042 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7043 irela[i].r_offset = offset;
7044 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7045 irela[0].r_addend = addend;
7047 erela = ((Elf_External_Rela *) rel_hdr->contents
7048 + elf_section_data (output_section)->rel_count);
7050 if (bed->s->swap_reloca_out)
7051 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7052 else
7053 elf_swap_reloca_out (output_bfd, irela, erela);
7056 ++elf_section_data (output_section)->rel_count;
7058 return true;
7061 /* Allocate a pointer to live in a linker created section. */
7063 boolean
7064 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7065 bfd *abfd;
7066 struct bfd_link_info *info;
7067 elf_linker_section_t *lsect;
7068 struct elf_link_hash_entry *h;
7069 const Elf_Internal_Rela *rel;
7071 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7072 elf_linker_section_pointers_t *linker_section_ptr;
7073 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7074 bfd_size_type amt;
7076 BFD_ASSERT (lsect != NULL);
7078 /* Is this a global symbol? */
7079 if (h != NULL)
7081 /* Has this symbol already been allocated? If so, our work is done. */
7082 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7083 rel->r_addend,
7084 lsect->which))
7085 return true;
7087 ptr_linker_section_ptr = &h->linker_section_pointer;
7088 /* Make sure this symbol is output as a dynamic symbol. */
7089 if (h->dynindx == -1)
7091 if (! elf_link_record_dynamic_symbol (info, h))
7092 return false;
7095 if (lsect->rel_section)
7096 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7098 else
7100 /* Allocation of a pointer to a local symbol. */
7101 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7103 /* Allocate a table to hold the local symbols if first time. */
7104 if (!ptr)
7106 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7107 register unsigned int i;
7109 amt = num_symbols;
7110 amt *= sizeof (elf_linker_section_pointers_t *);
7111 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7113 if (!ptr)
7114 return false;
7116 elf_local_ptr_offsets (abfd) = ptr;
7117 for (i = 0; i < num_symbols; i++)
7118 ptr[i] = (elf_linker_section_pointers_t *) 0;
7121 /* Has this symbol already been allocated? If so, our work is done. */
7122 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7123 rel->r_addend,
7124 lsect->which))
7125 return true;
7127 ptr_linker_section_ptr = &ptr[r_symndx];
7129 if (info->shared)
7131 /* If we are generating a shared object, we need to
7132 output a R_<xxx>_RELATIVE reloc so that the
7133 dynamic linker can adjust this GOT entry. */
7134 BFD_ASSERT (lsect->rel_section != NULL);
7135 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7139 /* Allocate space for a pointer in the linker section, and allocate
7140 a new pointer record from internal memory. */
7141 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7142 amt = sizeof (elf_linker_section_pointers_t);
7143 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7145 if (!linker_section_ptr)
7146 return false;
7148 linker_section_ptr->next = *ptr_linker_section_ptr;
7149 linker_section_ptr->addend = rel->r_addend;
7150 linker_section_ptr->which = lsect->which;
7151 linker_section_ptr->written_address_p = false;
7152 *ptr_linker_section_ptr = linker_section_ptr;
7154 #if 0
7155 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7157 linker_section_ptr->offset = (lsect->section->_raw_size
7158 - lsect->hole_size + (ARCH_SIZE / 8));
7159 lsect->hole_offset += ARCH_SIZE / 8;
7160 lsect->sym_offset += ARCH_SIZE / 8;
7161 if (lsect->sym_hash)
7163 /* Bump up symbol value if needed. */
7164 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7165 #ifdef DEBUG
7166 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7167 lsect->sym_hash->root.root.string,
7168 (long) ARCH_SIZE / 8,
7169 (long) lsect->sym_hash->root.u.def.value);
7170 #endif
7173 else
7174 #endif
7175 linker_section_ptr->offset = lsect->section->_raw_size;
7177 lsect->section->_raw_size += ARCH_SIZE / 8;
7179 #ifdef DEBUG
7180 fprintf (stderr,
7181 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7182 lsect->name, (long) linker_section_ptr->offset,
7183 (long) lsect->section->_raw_size);
7184 #endif
7186 return true;
7189 #if ARCH_SIZE==64
7190 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7191 #endif
7192 #if ARCH_SIZE==32
7193 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7194 #endif
7196 /* Fill in the address for a pointer generated in a linker section. */
7198 bfd_vma
7199 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7200 relocation, rel, relative_reloc)
7201 bfd *output_bfd;
7202 bfd *input_bfd;
7203 struct bfd_link_info *info;
7204 elf_linker_section_t *lsect;
7205 struct elf_link_hash_entry *h;
7206 bfd_vma relocation;
7207 const Elf_Internal_Rela *rel;
7208 int relative_reloc;
7210 elf_linker_section_pointers_t *linker_section_ptr;
7212 BFD_ASSERT (lsect != NULL);
7214 if (h != NULL)
7216 /* Handle global symbol. */
7217 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7218 (h->linker_section_pointer,
7219 rel->r_addend,
7220 lsect->which));
7222 BFD_ASSERT (linker_section_ptr != NULL);
7224 if (! elf_hash_table (info)->dynamic_sections_created
7225 || (info->shared
7226 && info->symbolic
7227 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7229 /* This is actually a static link, or it is a
7230 -Bsymbolic link and the symbol is defined
7231 locally. We must initialize this entry in the
7232 global section.
7234 When doing a dynamic link, we create a .rela.<xxx>
7235 relocation entry to initialize the value. This
7236 is done in the finish_dynamic_symbol routine. */
7237 if (!linker_section_ptr->written_address_p)
7239 linker_section_ptr->written_address_p = true;
7240 bfd_put_ptr (output_bfd,
7241 relocation + linker_section_ptr->addend,
7242 (lsect->section->contents
7243 + linker_section_ptr->offset));
7247 else
7249 /* Handle local symbol. */
7250 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7251 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7252 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7253 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7254 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7255 rel->r_addend,
7256 lsect->which));
7258 BFD_ASSERT (linker_section_ptr != NULL);
7260 /* Write out pointer if it hasn't been rewritten out before. */
7261 if (!linker_section_ptr->written_address_p)
7263 linker_section_ptr->written_address_p = true;
7264 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7265 lsect->section->contents + linker_section_ptr->offset);
7267 if (info->shared)
7269 asection *srel = lsect->rel_section;
7270 Elf_Internal_Rela *outrel;
7271 Elf_External_Rela *erel;
7272 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7273 unsigned int i;
7274 bfd_size_type amt;
7276 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7277 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7278 if (outrel == NULL)
7280 (*_bfd_error_handler) (_("Error: out of memory"));
7281 return 0;
7284 /* We need to generate a relative reloc for the dynamic
7285 linker. */
7286 if (!srel)
7288 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7289 lsect->rel_name);
7290 lsect->rel_section = srel;
7293 BFD_ASSERT (srel != NULL);
7295 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7296 outrel[i].r_offset = (lsect->section->output_section->vma
7297 + lsect->section->output_offset
7298 + linker_section_ptr->offset);
7299 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7300 outrel[0].r_addend = 0;
7301 erel = (Elf_External_Rela *) lsect->section->contents;
7302 erel += elf_section_data (lsect->section)->rel_count;
7303 elf_swap_reloca_out (output_bfd, outrel, erel);
7304 ++elf_section_data (lsect->section)->rel_count;
7306 free (outrel);
7311 relocation = (lsect->section->output_offset
7312 + linker_section_ptr->offset
7313 - lsect->hole_offset
7314 - lsect->sym_offset);
7316 #ifdef DEBUG
7317 fprintf (stderr,
7318 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7319 lsect->name, (long) relocation, (long) relocation);
7320 #endif
7322 /* Subtract out the addend, because it will get added back in by the normal
7323 processing. */
7324 return relocation - linker_section_ptr->addend;
7327 /* Garbage collect unused sections. */
7329 static boolean elf_gc_mark
7330 PARAMS ((struct bfd_link_info *info, asection *sec,
7331 asection * (*gc_mark_hook)
7332 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7333 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7335 static boolean elf_gc_sweep
7336 PARAMS ((struct bfd_link_info *info,
7337 boolean (*gc_sweep_hook)
7338 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7339 const Elf_Internal_Rela *relocs))));
7341 static boolean elf_gc_sweep_symbol
7342 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7344 static boolean elf_gc_allocate_got_offsets
7345 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7347 static boolean elf_gc_propagate_vtable_entries_used
7348 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7350 static boolean elf_gc_smash_unused_vtentry_relocs
7351 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7353 /* The mark phase of garbage collection. For a given section, mark
7354 it and any sections in this section's group, and all the sections
7355 which define symbols to which it refers. */
7357 static boolean
7358 elf_gc_mark (info, sec, gc_mark_hook)
7359 struct bfd_link_info *info;
7360 asection *sec;
7361 asection * (*gc_mark_hook)
7362 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7363 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7365 boolean ret;
7366 asection *group_sec;
7368 sec->gc_mark = 1;
7370 /* Mark all the sections in the group. */
7371 group_sec = elf_section_data (sec)->next_in_group;
7372 if (group_sec && !group_sec->gc_mark)
7373 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7374 return false;
7376 /* Look through the section relocs. */
7377 ret = true;
7378 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7380 Elf_Internal_Rela *relstart, *rel, *relend;
7381 Elf_Internal_Shdr *symtab_hdr;
7382 Elf_Internal_Shdr *shndx_hdr;
7383 struct elf_link_hash_entry **sym_hashes;
7384 size_t nlocsyms;
7385 size_t extsymoff;
7386 Elf_External_Sym *locsyms, *freesyms = NULL;
7387 Elf_External_Sym_Shndx *locsym_shndx;
7388 bfd *input_bfd = sec->owner;
7389 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7391 /* GCFIXME: how to arrange so that relocs and symbols are not
7392 reread continually? */
7394 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7395 sym_hashes = elf_sym_hashes (input_bfd);
7397 /* Read the local symbols. */
7398 if (elf_bad_symtab (input_bfd))
7400 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7401 extsymoff = 0;
7403 else
7404 extsymoff = nlocsyms = symtab_hdr->sh_info;
7406 if (symtab_hdr->contents)
7407 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7408 else if (nlocsyms == 0)
7409 locsyms = NULL;
7410 else
7412 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7413 locsyms = freesyms = bfd_malloc (amt);
7414 if (freesyms == NULL
7415 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7416 || bfd_bread (locsyms, amt, input_bfd) != amt)
7418 ret = false;
7419 goto out1;
7423 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7424 locsym_shndx = NULL;
7425 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7427 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7428 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7429 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7430 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7431 return false;
7434 /* Read the relocations. */
7435 relstart = (NAME(_bfd_elf,link_read_relocs)
7436 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7437 info->keep_memory));
7438 if (relstart == NULL)
7440 ret = false;
7441 goto out1;
7443 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7445 for (rel = relstart; rel < relend; rel++)
7447 unsigned long r_symndx;
7448 asection *rsec;
7449 struct elf_link_hash_entry *h;
7450 Elf_Internal_Sym s;
7452 r_symndx = ELF_R_SYM (rel->r_info);
7453 if (r_symndx == 0)
7454 continue;
7456 if (elf_bad_symtab (sec->owner))
7458 elf_swap_symbol_in (input_bfd,
7459 locsyms + r_symndx,
7460 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7461 &s);
7462 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7463 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7464 else
7466 h = sym_hashes[r_symndx - extsymoff];
7467 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7470 else if (r_symndx >= nlocsyms)
7472 h = sym_hashes[r_symndx - extsymoff];
7473 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7475 else
7477 elf_swap_symbol_in (input_bfd,
7478 locsyms + r_symndx,
7479 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7480 &s);
7481 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7484 if (rsec && !rsec->gc_mark)
7485 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7487 ret = false;
7488 goto out2;
7492 out2:
7493 if (!info->keep_memory)
7494 free (relstart);
7495 out1:
7496 if (freesyms)
7497 free (freesyms);
7500 return ret;
7503 /* The sweep phase of garbage collection. Remove all garbage sections. */
7505 static boolean
7506 elf_gc_sweep (info, gc_sweep_hook)
7507 struct bfd_link_info *info;
7508 boolean (*gc_sweep_hook)
7509 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7510 const Elf_Internal_Rela *relocs));
7512 bfd *sub;
7514 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7516 asection *o;
7518 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7519 continue;
7521 for (o = sub->sections; o != NULL; o = o->next)
7523 /* Keep special sections. Keep .debug sections. */
7524 if ((o->flags & SEC_LINKER_CREATED)
7525 || (o->flags & SEC_DEBUGGING))
7526 o->gc_mark = 1;
7528 if (o->gc_mark)
7529 continue;
7531 /* Skip sweeping sections already excluded. */
7532 if (o->flags & SEC_EXCLUDE)
7533 continue;
7535 /* Since this is early in the link process, it is simple
7536 to remove a section from the output. */
7537 o->flags |= SEC_EXCLUDE;
7539 /* But we also have to update some of the relocation
7540 info we collected before. */
7541 if (gc_sweep_hook
7542 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7544 Elf_Internal_Rela *internal_relocs;
7545 boolean r;
7547 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7548 (o->owner, o, NULL, NULL, info->keep_memory));
7549 if (internal_relocs == NULL)
7550 return false;
7552 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7554 if (!info->keep_memory)
7555 free (internal_relocs);
7557 if (!r)
7558 return false;
7563 /* Remove the symbols that were in the swept sections from the dynamic
7564 symbol table. GCFIXME: Anyone know how to get them out of the
7565 static symbol table as well? */
7567 int i = 0;
7569 elf_link_hash_traverse (elf_hash_table (info),
7570 elf_gc_sweep_symbol,
7571 (PTR) &i);
7573 elf_hash_table (info)->dynsymcount = i;
7576 return true;
7579 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7581 static boolean
7582 elf_gc_sweep_symbol (h, idxptr)
7583 struct elf_link_hash_entry *h;
7584 PTR idxptr;
7586 int *idx = (int *) idxptr;
7588 if (h->dynindx != -1
7589 && ((h->root.type != bfd_link_hash_defined
7590 && h->root.type != bfd_link_hash_defweak)
7591 || h->root.u.def.section->gc_mark))
7592 h->dynindx = (*idx)++;
7594 return true;
7597 /* Propogate collected vtable information. This is called through
7598 elf_link_hash_traverse. */
7600 static boolean
7601 elf_gc_propagate_vtable_entries_used (h, okp)
7602 struct elf_link_hash_entry *h;
7603 PTR okp;
7605 /* Those that are not vtables. */
7606 if (h->vtable_parent == NULL)
7607 return true;
7609 /* Those vtables that do not have parents, we cannot merge. */
7610 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7611 return true;
7613 /* If we've already been done, exit. */
7614 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7615 return true;
7617 /* Make sure the parent's table is up to date. */
7618 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7620 if (h->vtable_entries_used == NULL)
7622 /* None of this table's entries were referenced. Re-use the
7623 parent's table. */
7624 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7625 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7627 else
7629 size_t n;
7630 boolean *cu, *pu;
7632 /* Or the parent's entries into ours. */
7633 cu = h->vtable_entries_used;
7634 cu[-1] = true;
7635 pu = h->vtable_parent->vtable_entries_used;
7636 if (pu != NULL)
7638 asection *sec = h->root.u.def.section;
7639 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7640 int file_align = bed->s->file_align;
7642 n = h->vtable_parent->vtable_entries_size / file_align;
7643 while (n--)
7645 if (*pu)
7646 *cu = true;
7647 pu++;
7648 cu++;
7653 return true;
7656 static boolean
7657 elf_gc_smash_unused_vtentry_relocs (h, okp)
7658 struct elf_link_hash_entry *h;
7659 PTR okp;
7661 asection *sec;
7662 bfd_vma hstart, hend;
7663 Elf_Internal_Rela *relstart, *relend, *rel;
7664 struct elf_backend_data *bed;
7665 int file_align;
7667 /* Take care of both those symbols that do not describe vtables as
7668 well as those that are not loaded. */
7669 if (h->vtable_parent == NULL)
7670 return true;
7672 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7673 || h->root.type == bfd_link_hash_defweak);
7675 sec = h->root.u.def.section;
7676 hstart = h->root.u.def.value;
7677 hend = hstart + h->size;
7679 relstart = (NAME(_bfd_elf,link_read_relocs)
7680 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7681 if (!relstart)
7682 return *(boolean *) okp = false;
7683 bed = get_elf_backend_data (sec->owner);
7684 file_align = bed->s->file_align;
7686 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7688 for (rel = relstart; rel < relend; ++rel)
7689 if (rel->r_offset >= hstart && rel->r_offset < hend)
7691 /* If the entry is in use, do nothing. */
7692 if (h->vtable_entries_used
7693 && (rel->r_offset - hstart) < h->vtable_entries_size)
7695 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7696 if (h->vtable_entries_used[entry])
7697 continue;
7699 /* Otherwise, kill it. */
7700 rel->r_offset = rel->r_info = rel->r_addend = 0;
7703 return true;
7706 /* Do mark and sweep of unused sections. */
7708 boolean
7709 elf_gc_sections (abfd, info)
7710 bfd *abfd;
7711 struct bfd_link_info *info;
7713 boolean ok = true;
7714 bfd *sub;
7715 asection * (*gc_mark_hook)
7716 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7717 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7719 if (!get_elf_backend_data (abfd)->can_gc_sections
7720 || info->relocateable || info->emitrelocations
7721 || elf_hash_table (info)->dynamic_sections_created)
7722 return true;
7724 /* Apply transitive closure to the vtable entry usage info. */
7725 elf_link_hash_traverse (elf_hash_table (info),
7726 elf_gc_propagate_vtable_entries_used,
7727 (PTR) &ok);
7728 if (!ok)
7729 return false;
7731 /* Kill the vtable relocations that were not used. */
7732 elf_link_hash_traverse (elf_hash_table (info),
7733 elf_gc_smash_unused_vtentry_relocs,
7734 (PTR) &ok);
7735 if (!ok)
7736 return false;
7738 /* Grovel through relocs to find out who stays ... */
7740 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7741 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7743 asection *o;
7745 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7746 continue;
7748 for (o = sub->sections; o != NULL; o = o->next)
7750 if (o->flags & SEC_KEEP)
7751 if (!elf_gc_mark (info, o, gc_mark_hook))
7752 return false;
7756 /* ... and mark SEC_EXCLUDE for those that go. */
7757 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7758 return false;
7760 return true;
7763 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7765 boolean
7766 elf_gc_record_vtinherit (abfd, sec, h, offset)
7767 bfd *abfd;
7768 asection *sec;
7769 struct elf_link_hash_entry *h;
7770 bfd_vma offset;
7772 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7773 struct elf_link_hash_entry **search, *child;
7774 bfd_size_type extsymcount;
7776 /* The sh_info field of the symtab header tells us where the
7777 external symbols start. We don't care about the local symbols at
7778 this point. */
7779 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7780 if (!elf_bad_symtab (abfd))
7781 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7783 sym_hashes = elf_sym_hashes (abfd);
7784 sym_hashes_end = sym_hashes + extsymcount;
7786 /* Hunt down the child symbol, which is in this section at the same
7787 offset as the relocation. */
7788 for (search = sym_hashes; search != sym_hashes_end; ++search)
7790 if ((child = *search) != NULL
7791 && (child->root.type == bfd_link_hash_defined
7792 || child->root.type == bfd_link_hash_defweak)
7793 && child->root.u.def.section == sec
7794 && child->root.u.def.value == offset)
7795 goto win;
7798 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7799 bfd_archive_filename (abfd), sec->name,
7800 (unsigned long) offset);
7801 bfd_set_error (bfd_error_invalid_operation);
7802 return false;
7804 win:
7805 if (!h)
7807 /* This *should* only be the absolute section. It could potentially
7808 be that someone has defined a non-global vtable though, which
7809 would be bad. It isn't worth paging in the local symbols to be
7810 sure though; that case should simply be handled by the assembler. */
7812 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7814 else
7815 child->vtable_parent = h;
7817 return true;
7820 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7822 boolean
7823 elf_gc_record_vtentry (abfd, sec, h, addend)
7824 bfd *abfd ATTRIBUTE_UNUSED;
7825 asection *sec ATTRIBUTE_UNUSED;
7826 struct elf_link_hash_entry *h;
7827 bfd_vma addend;
7829 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7830 int file_align = bed->s->file_align;
7832 if (addend >= h->vtable_entries_size)
7834 size_t size, bytes;
7835 boolean *ptr = h->vtable_entries_used;
7837 /* While the symbol is undefined, we have to be prepared to handle
7838 a zero size. */
7839 if (h->root.type == bfd_link_hash_undefined)
7840 size = addend;
7841 else
7843 size = h->size;
7844 if (size < addend)
7846 /* Oops! We've got a reference past the defined end of
7847 the table. This is probably a bug -- shall we warn? */
7848 size = addend;
7852 /* Allocate one extra entry for use as a "done" flag for the
7853 consolidation pass. */
7854 bytes = (size / file_align + 1) * sizeof (boolean);
7856 if (ptr)
7858 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7860 if (ptr != NULL)
7862 size_t oldbytes;
7864 oldbytes = ((h->vtable_entries_size / file_align + 1)
7865 * sizeof (boolean));
7866 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7869 else
7870 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7872 if (ptr == NULL)
7873 return false;
7875 /* And arrange for that done flag to be at index -1. */
7876 h->vtable_entries_used = ptr + 1;
7877 h->vtable_entries_size = size;
7880 h->vtable_entries_used[addend / file_align] = true;
7882 return true;
7885 /* And an accompanying bit to work out final got entry offsets once
7886 we're done. Should be called from final_link. */
7888 boolean
7889 elf_gc_common_finalize_got_offsets (abfd, info)
7890 bfd *abfd;
7891 struct bfd_link_info *info;
7893 bfd *i;
7894 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7895 bfd_vma gotoff;
7897 /* The GOT offset is relative to the .got section, but the GOT header is
7898 put into the .got.plt section, if the backend uses it. */
7899 if (bed->want_got_plt)
7900 gotoff = 0;
7901 else
7902 gotoff = bed->got_header_size;
7904 /* Do the local .got entries first. */
7905 for (i = info->input_bfds; i; i = i->link_next)
7907 bfd_signed_vma *local_got;
7908 bfd_size_type j, locsymcount;
7909 Elf_Internal_Shdr *symtab_hdr;
7911 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7912 continue;
7914 local_got = elf_local_got_refcounts (i);
7915 if (!local_got)
7916 continue;
7918 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7919 if (elf_bad_symtab (i))
7920 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7921 else
7922 locsymcount = symtab_hdr->sh_info;
7924 for (j = 0; j < locsymcount; ++j)
7926 if (local_got[j] > 0)
7928 local_got[j] = gotoff;
7929 gotoff += ARCH_SIZE / 8;
7931 else
7932 local_got[j] = (bfd_vma) -1;
7936 /* Then the global .got entries. .plt refcounts are handled by
7937 adjust_dynamic_symbol */
7938 elf_link_hash_traverse (elf_hash_table (info),
7939 elf_gc_allocate_got_offsets,
7940 (PTR) &gotoff);
7941 return true;
7944 /* We need a special top-level link routine to convert got reference counts
7945 to real got offsets. */
7947 static boolean
7948 elf_gc_allocate_got_offsets (h, offarg)
7949 struct elf_link_hash_entry *h;
7950 PTR offarg;
7952 bfd_vma *off = (bfd_vma *) offarg;
7954 if (h->got.refcount > 0)
7956 h->got.offset = off[0];
7957 off[0] += ARCH_SIZE / 8;
7959 else
7960 h->got.offset = (bfd_vma) -1;
7962 return true;
7965 /* Many folk need no more in the way of final link than this, once
7966 got entry reference counting is enabled. */
7968 boolean
7969 elf_gc_common_final_link (abfd, info)
7970 bfd *abfd;
7971 struct bfd_link_info *info;
7973 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7974 return false;
7976 /* Invoke the regular ELF backend linker to do all the work. */
7977 return elf_bfd_final_link (abfd, info);
7980 /* This function will be called though elf_link_hash_traverse to store
7981 all hash value of the exported symbols in an array. */
7983 static boolean
7984 elf_collect_hash_codes (h, data)
7985 struct elf_link_hash_entry *h;
7986 PTR data;
7988 unsigned long **valuep = (unsigned long **) data;
7989 const char *name;
7990 char *p;
7991 unsigned long ha;
7992 char *alc = NULL;
7994 /* Ignore indirect symbols. These are added by the versioning code. */
7995 if (h->dynindx == -1)
7996 return true;
7998 name = h->root.root.string;
7999 p = strchr (name, ELF_VER_CHR);
8000 if (p != NULL)
8002 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8003 memcpy (alc, name, (size_t) (p - name));
8004 alc[p - name] = '\0';
8005 name = alc;
8008 /* Compute the hash value. */
8009 ha = bfd_elf_hash (name);
8011 /* Store the found hash value in the array given as the argument. */
8012 *(*valuep)++ = ha;
8014 /* And store it in the struct so that we can put it in the hash table
8015 later. */
8016 h->elf_hash_value = ha;
8018 if (alc != NULL)
8019 free (alc);
8021 return true;
8024 boolean
8025 elf_reloc_symbol_deleted_p (offset, cookie)
8026 bfd_vma offset;
8027 PTR cookie;
8029 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8031 if (rcookie->bad_symtab)
8032 rcookie->rel = rcookie->rels;
8034 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8036 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8037 Elf_Internal_Sym isym;
8039 if (! rcookie->bad_symtab)
8040 if (rcookie->rel->r_offset > offset)
8041 return false;
8042 if (rcookie->rel->r_offset != offset)
8043 continue;
8045 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8047 Elf_External_Sym *lsym;
8048 Elf_External_Sym_Shndx *lshndx;
8050 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8051 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8052 if (lshndx != NULL)
8053 lshndx += r_symndx;
8054 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8057 if (r_symndx >= rcookie->locsymcount
8058 || (rcookie->locsyms
8059 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8061 struct elf_link_hash_entry *h;
8063 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8065 while (h->root.type == bfd_link_hash_indirect
8066 || h->root.type == bfd_link_hash_warning)
8067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8069 if ((h->root.type == bfd_link_hash_defined
8070 || h->root.type == bfd_link_hash_defweak)
8071 && elf_discarded_section (h->root.u.def.section))
8072 return true;
8073 else
8074 return false;
8076 else if (rcookie->locsyms)
8078 /* It's not a relocation against a global symbol,
8079 but it could be a relocation against a local
8080 symbol for a discarded section. */
8081 asection *isec;
8083 /* Need to: get the symbol; get the section. */
8084 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8086 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8087 if (isec != NULL && elf_discarded_section (isec))
8088 return true;
8091 return false;
8093 return false;
8096 /* Discard unneeded references to discarded sections.
8097 Returns true if any section's size was changed. */
8098 /* This function assumes that the relocations are in sorted order,
8099 which is true for all known assemblers. */
8101 boolean
8102 elf_bfd_discard_info (output_bfd, info)
8103 bfd *output_bfd;
8104 struct bfd_link_info *info;
8106 struct elf_reloc_cookie cookie;
8107 asection *stab, *eh, *ehdr;
8108 Elf_Internal_Shdr *symtab_hdr;
8109 Elf_Internal_Shdr *shndx_hdr;
8110 Elf_External_Sym *freesyms;
8111 struct elf_backend_data *bed;
8112 bfd *abfd;
8113 boolean ret = false;
8114 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8116 if (info->relocateable
8117 || info->traditional_format
8118 || info->hash->creator->flavour != bfd_target_elf_flavour
8119 || ! is_elf_hash_table (info))
8120 return false;
8122 ehdr = NULL;
8123 if (elf_hash_table (info)->dynobj != NULL)
8124 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8125 ".eh_frame_hdr");
8127 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8129 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8130 continue;
8132 bed = get_elf_backend_data (abfd);
8134 if ((abfd->flags & DYNAMIC) != 0)
8135 continue;
8137 eh = NULL;
8138 if (ehdr)
8140 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8141 if (eh && eh->_raw_size == 0)
8142 eh = NULL;
8145 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
8146 if ((! stab || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8147 && ! eh
8148 && (strip || ! bed->elf_backend_discard_info))
8149 continue;
8151 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8152 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8154 cookie.abfd = abfd;
8155 cookie.sym_hashes = elf_sym_hashes (abfd);
8156 cookie.bad_symtab = elf_bad_symtab (abfd);
8157 if (cookie.bad_symtab)
8159 cookie.locsymcount =
8160 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8161 cookie.extsymoff = 0;
8163 else
8165 cookie.locsymcount = symtab_hdr->sh_info;
8166 cookie.extsymoff = symtab_hdr->sh_info;
8169 freesyms = NULL;
8170 if (symtab_hdr->contents)
8171 cookie.locsyms = (void *) symtab_hdr->contents;
8172 else if (cookie.locsymcount == 0)
8173 cookie.locsyms = NULL;
8174 else
8176 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8177 cookie.locsyms = bfd_malloc (amt);
8178 if (cookie.locsyms == NULL)
8179 return false;
8180 freesyms = cookie.locsyms;
8181 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8182 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8184 error_ret_free_loc:
8185 free (cookie.locsyms);
8186 return false;
8190 cookie.locsym_shndx = NULL;
8191 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8193 bfd_size_type amt;
8194 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8195 cookie.locsym_shndx = bfd_malloc (amt);
8196 if (cookie.locsym_shndx == NULL)
8197 goto error_ret_free_loc;
8198 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8199 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8201 free (cookie.locsym_shndx);
8202 goto error_ret_free_loc;
8206 if (stab)
8208 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8209 (abfd, stab, (PTR) NULL,
8210 (Elf_Internal_Rela *) NULL,
8211 info->keep_memory));
8212 if (cookie.rels)
8214 cookie.rel = cookie.rels;
8215 cookie.relend =
8216 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8217 if (_bfd_discard_section_stabs (abfd, stab,
8218 elf_section_data (stab)->sec_info,
8219 elf_reloc_symbol_deleted_p,
8220 &cookie))
8221 ret = true;
8222 if (! info->keep_memory)
8223 free (cookie.rels);
8227 if (eh)
8229 cookie.rels = NULL;
8230 cookie.rel = NULL;
8231 cookie.relend = NULL;
8232 if (eh->reloc_count)
8233 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8234 (abfd, eh, (PTR) NULL,
8235 (Elf_Internal_Rela *) NULL,
8236 info->keep_memory));
8237 if (cookie.rels)
8239 cookie.rel = cookie.rels;
8240 cookie.relend =
8241 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8243 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8244 elf_reloc_symbol_deleted_p,
8245 &cookie))
8246 ret = true;
8247 if (! info->keep_memory)
8248 free (cookie.rels);
8251 if (bed->elf_backend_discard_info)
8253 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8254 ret = true;
8257 if (cookie.locsym_shndx != NULL)
8258 free (cookie.locsym_shndx);
8260 if (freesyms != NULL)
8261 free (freesyms);
8264 if (ehdr
8265 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd,
8266 info, ehdr))
8267 ret = true;
8268 return ret;
8271 static boolean
8272 elf_section_ignore_discarded_relocs (sec)
8273 asection *sec;
8275 switch (elf_section_data (sec)->sec_info_type)
8277 case ELF_INFO_TYPE_STABS:
8278 case ELF_INFO_TYPE_EH_FRAME:
8279 return true;
8280 default:
8281 break;
8283 if ((get_elf_backend_data (sec->owner)->elf_backend_ignore_discarded_relocs
8284 != NULL)
8285 && (*get_elf_backend_data (sec->owner)
8286 ->elf_backend_ignore_discarded_relocs) (sec))
8287 return true;
8289 return false;