* bfd.c (bfd_archive_filename): New function.
[binutils.git] / bfd / elflink.h
blob393f932b17c2333cb01339f5f595601eb8a507c4
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 *, Elf_Internal_Sym *,
43 asection **, bfd_vma *, struct elf_link_hash_entry **,
44 boolean *, boolean *, boolean *, boolean));
45 static boolean elf_export_symbol
46 PARAMS ((struct elf_link_hash_entry *, PTR));
47 static boolean elf_fix_symbol_flags
48 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
49 static boolean elf_adjust_dynamic_symbol
50 PARAMS ((struct elf_link_hash_entry *, PTR));
51 static boolean elf_link_find_version_dependencies
52 PARAMS ((struct elf_link_hash_entry *, PTR));
53 static boolean elf_link_find_version_dependencies
54 PARAMS ((struct elf_link_hash_entry *, PTR));
55 static boolean elf_link_assign_sym_version
56 PARAMS ((struct elf_link_hash_entry *, PTR));
57 static boolean elf_collect_hash_codes
58 PARAMS ((struct elf_link_hash_entry *, PTR));
59 static boolean elf_link_read_relocs_from_section
60 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
61 static size_t compute_bucket_count
62 PARAMS ((struct bfd_link_info *));
63 static void elf_link_output_relocs
64 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
65 static boolean elf_link_size_reloc_section
66 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
67 static void elf_link_adjust_relocs
68 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
69 struct elf_link_hash_entry **));
70 static int elf_link_sort_cmp1
71 PARAMS ((const void *, const void *));
72 static int elf_link_sort_cmp2
73 PARAMS ((const void *, const void *));
74 static size_t elf_link_sort_relocs
75 PARAMS ((bfd *, struct bfd_link_info *, asection **));
77 /* Given an ELF BFD, add symbols to the global hash table as
78 appropriate. */
80 boolean
81 elf_bfd_link_add_symbols (abfd, info)
82 bfd *abfd;
83 struct bfd_link_info *info;
85 switch (bfd_get_format (abfd))
87 case bfd_object:
88 return elf_link_add_object_symbols (abfd, info);
89 case bfd_archive:
90 return elf_link_add_archive_symbols (abfd, info);
91 default:
92 bfd_set_error (bfd_error_wrong_format);
93 return false;
97 /* Return true iff this is a non-common, definition of a non-function symbol. */
98 static boolean
99 is_global_data_symbol_definition (abfd, sym)
100 bfd * abfd ATTRIBUTE_UNUSED;
101 Elf_Internal_Sym * sym;
103 /* Local symbols do not count, but target specific ones might. */
104 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
105 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
106 return false;
108 /* Function symbols do not count. */
109 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
110 return false;
112 /* If the section is undefined, then so is the symbol. */
113 if (sym->st_shndx == SHN_UNDEF)
114 return false;
116 /* If the symbol is defined in the common section, then
117 it is a common definition and so does not count. */
118 if (sym->st_shndx == SHN_COMMON)
119 return false;
121 /* If the symbol is in a target specific section then we
122 must rely upon the backend to tell us what it is. */
123 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
124 /* FIXME - this function is not coded yet:
126 return _bfd_is_global_symbol_definition (abfd, sym);
128 Instead for now assume that the definition is not global,
129 Even if this is wrong, at least the linker will behave
130 in the same way that it used to do. */
131 return false;
133 return true;
136 /* Search the symbol table of the archive element of the archive ABFD
137 whose archive map contains a mention of SYMDEF, and determine if
138 the symbol is defined in this element. */
139 static boolean
140 elf_link_is_defined_archive_symbol (abfd, symdef)
141 bfd * abfd;
142 carsym * symdef;
144 Elf_Internal_Shdr * hdr;
145 Elf_External_Sym * esym;
146 Elf_External_Sym * esymend;
147 Elf_External_Sym * buf = NULL;
148 bfd_size_type symcount;
149 bfd_size_type extsymcount;
150 bfd_size_type extsymoff;
151 boolean result = false;
152 file_ptr pos;
153 bfd_size_type amt;
155 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
156 if (abfd == (bfd *) NULL)
157 return false;
159 if (! bfd_check_format (abfd, bfd_object))
160 return false;
162 /* If we have already included the element containing this symbol in the
163 link then we do not need to include it again. Just claim that any symbol
164 it contains is not a definition, so that our caller will not decide to
165 (re)include this element. */
166 if (abfd->archive_pass)
167 return false;
169 /* Select the appropriate symbol table. */
170 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
171 hdr = &elf_tdata (abfd)->symtab_hdr;
172 else
173 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
175 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
177 /* The sh_info field of the symtab header tells us where the
178 external symbols start. We don't care about the local symbols. */
179 if (elf_bad_symtab (abfd))
181 extsymcount = symcount;
182 extsymoff = 0;
184 else
186 extsymcount = symcount - hdr->sh_info;
187 extsymoff = hdr->sh_info;
190 amt = extsymcount * sizeof (Elf_External_Sym);
191 buf = (Elf_External_Sym *) bfd_malloc (amt);
192 if (buf == NULL && extsymcount != 0)
193 return false;
195 /* Read in the symbol table.
196 FIXME: This ought to be cached somewhere. */
197 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
198 if (bfd_seek (abfd, pos, SEEK_SET) != 0
199 || bfd_bread ((PTR) buf, amt, abfd) != amt)
201 free (buf);
202 return false;
205 /* Scan the symbol table looking for SYMDEF. */
206 esymend = buf + extsymcount;
207 for (esym = buf;
208 esym < esymend;
209 esym++)
211 Elf_Internal_Sym sym;
212 const char * name;
214 elf_swap_symbol_in (abfd, esym, & sym);
216 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
217 if (name == (const char *) NULL)
218 break;
220 if (strcmp (name, symdef->name) == 0)
222 result = is_global_data_symbol_definition (abfd, & sym);
223 break;
227 free (buf);
229 return result;
232 /* Add symbols from an ELF archive file to the linker hash table. We
233 don't use _bfd_generic_link_add_archive_symbols because of a
234 problem which arises on UnixWare. The UnixWare libc.so is an
235 archive which includes an entry libc.so.1 which defines a bunch of
236 symbols. The libc.so archive also includes a number of other
237 object files, which also define symbols, some of which are the same
238 as those defined in libc.so.1. Correct linking requires that we
239 consider each object file in turn, and include it if it defines any
240 symbols we need. _bfd_generic_link_add_archive_symbols does not do
241 this; it looks through the list of undefined symbols, and includes
242 any object file which defines them. When this algorithm is used on
243 UnixWare, it winds up pulling in libc.so.1 early and defining a
244 bunch of symbols. This means that some of the other objects in the
245 archive are not included in the link, which is incorrect since they
246 precede libc.so.1 in the archive.
248 Fortunately, ELF archive handling is simpler than that done by
249 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
250 oddities. In ELF, if we find a symbol in the archive map, and the
251 symbol is currently undefined, we know that we must pull in that
252 object file.
254 Unfortunately, we do have to make multiple passes over the symbol
255 table until nothing further is resolved. */
257 static boolean
258 elf_link_add_archive_symbols (abfd, info)
259 bfd *abfd;
260 struct bfd_link_info *info;
262 symindex c;
263 boolean *defined = NULL;
264 boolean *included = NULL;
265 carsym *symdefs;
266 boolean loop;
267 bfd_size_type amt;
269 if (! bfd_has_map (abfd))
271 /* An empty archive is a special case. */
272 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
273 return true;
274 bfd_set_error (bfd_error_no_armap);
275 return false;
278 /* Keep track of all symbols we know to be already defined, and all
279 files we know to be already included. This is to speed up the
280 second and subsequent passes. */
281 c = bfd_ardata (abfd)->symdef_count;
282 if (c == 0)
283 return true;
284 amt = c;
285 amt *= sizeof (boolean);
286 defined = (boolean *) bfd_malloc (amt);
287 included = (boolean *) bfd_malloc (amt);
288 if (defined == (boolean *) NULL || included == (boolean *) NULL)
289 goto error_return;
290 memset (defined, 0, (size_t) amt);
291 memset (included, 0, (size_t) amt);
293 symdefs = bfd_ardata (abfd)->symdefs;
297 file_ptr last;
298 symindex i;
299 carsym *symdef;
300 carsym *symdefend;
302 loop = false;
303 last = -1;
305 symdef = symdefs;
306 symdefend = symdef + c;
307 for (i = 0; symdef < symdefend; symdef++, i++)
309 struct elf_link_hash_entry *h;
310 bfd *element;
311 struct bfd_link_hash_entry *undefs_tail;
312 symindex mark;
314 if (defined[i] || included[i])
315 continue;
316 if (symdef->file_offset == last)
318 included[i] = true;
319 continue;
322 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
323 false, false, false);
325 if (h == NULL)
327 char *p, *copy;
329 /* If this is a default version (the name contains @@),
330 look up the symbol again without the version. The
331 effect is that references to the symbol without the
332 version will be matched by the default symbol in the
333 archive. */
335 p = strchr (symdef->name, ELF_VER_CHR);
336 if (p == NULL || p[1] != ELF_VER_CHR)
337 continue;
339 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
340 if (copy == NULL)
341 goto error_return;
342 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
343 copy[p - symdef->name] = '\0';
345 h = elf_link_hash_lookup (elf_hash_table (info), copy,
346 false, false, false);
348 bfd_release (abfd, copy);
351 if (h == NULL)
352 continue;
354 if (h->root.type == bfd_link_hash_common)
356 /* We currently have a common symbol. The archive map contains
357 a reference to this symbol, so we may want to include it. We
358 only want to include it however, if this archive element
359 contains a definition of the symbol, not just another common
360 declaration of it.
362 Unfortunately some archivers (including GNU ar) will put
363 declarations of common symbols into their archive maps, as
364 well as real definitions, so we cannot just go by the archive
365 map alone. Instead we must read in the element's symbol
366 table and check that to see what kind of symbol definition
367 this is. */
368 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
369 continue;
371 else if (h->root.type != bfd_link_hash_undefined)
373 if (h->root.type != bfd_link_hash_undefweak)
374 defined[i] = true;
375 continue;
378 /* We need to include this archive member. */
379 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
380 if (element == (bfd *) NULL)
381 goto error_return;
383 if (! bfd_check_format (element, bfd_object))
384 goto error_return;
386 /* Doublecheck that we have not included this object
387 already--it should be impossible, but there may be
388 something wrong with the archive. */
389 if (element->archive_pass != 0)
391 bfd_set_error (bfd_error_bad_value);
392 goto error_return;
394 element->archive_pass = 1;
396 undefs_tail = info->hash->undefs_tail;
398 if (! (*info->callbacks->add_archive_element) (info, element,
399 symdef->name))
400 goto error_return;
401 if (! elf_link_add_object_symbols (element, info))
402 goto error_return;
404 /* If there are any new undefined symbols, we need to make
405 another pass through the archive in order to see whether
406 they can be defined. FIXME: This isn't perfect, because
407 common symbols wind up on undefs_tail and because an
408 undefined symbol which is defined later on in this pass
409 does not require another pass. This isn't a bug, but it
410 does make the code less efficient than it could be. */
411 if (undefs_tail != info->hash->undefs_tail)
412 loop = true;
414 /* Look backward to mark all symbols from this object file
415 which we have already seen in this pass. */
416 mark = i;
419 included[mark] = true;
420 if (mark == 0)
421 break;
422 --mark;
424 while (symdefs[mark].file_offset == symdef->file_offset);
426 /* We mark subsequent symbols from this object file as we go
427 on through the loop. */
428 last = symdef->file_offset;
431 while (loop);
433 free (defined);
434 free (included);
436 return true;
438 error_return:
439 if (defined != (boolean *) NULL)
440 free (defined);
441 if (included != (boolean *) NULL)
442 free (included);
443 return false;
446 /* This function is called when we want to define a new symbol. It
447 handles the various cases which arise when we find a definition in
448 a dynamic object, or when there is already a definition in a
449 dynamic object. The new symbol is described by NAME, SYM, PSEC,
450 and PVALUE. We set SYM_HASH to the hash table entry. We set
451 OVERRIDE if the old symbol is overriding a new definition. We set
452 TYPE_CHANGE_OK if it is OK for the type to change. We set
453 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
454 change, we mean that we shouldn't warn if the type or size does
455 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
456 a shared object. */
458 static boolean
459 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
460 override, type_change_ok, size_change_ok, dt_needed)
461 bfd *abfd;
462 struct bfd_link_info *info;
463 const char *name;
464 Elf_Internal_Sym *sym;
465 asection **psec;
466 bfd_vma *pvalue;
467 struct elf_link_hash_entry **sym_hash;
468 boolean *override;
469 boolean *type_change_ok;
470 boolean *size_change_ok;
471 boolean dt_needed;
473 asection *sec;
474 struct elf_link_hash_entry *h;
475 int bind;
476 bfd *oldbfd;
477 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
479 *override = false;
481 sec = *psec;
482 bind = ELF_ST_BIND (sym->st_info);
484 if (! bfd_is_und_section (sec))
485 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
486 else
487 h = ((struct elf_link_hash_entry *)
488 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
489 if (h == NULL)
490 return false;
491 *sym_hash = h;
493 /* This code is for coping with dynamic objects, and is only useful
494 if we are doing an ELF link. */
495 if (info->hash->creator != abfd->xvec)
496 return true;
498 /* For merging, we only care about real symbols. */
500 while (h->root.type == bfd_link_hash_indirect
501 || h->root.type == bfd_link_hash_warning)
502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
504 /* If we just created the symbol, mark it as being an ELF symbol.
505 Other than that, there is nothing to do--there is no merge issue
506 with a newly defined symbol--so we just return. */
508 if (h->root.type == bfd_link_hash_new)
510 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
511 return true;
514 /* OLDBFD is a BFD associated with the existing symbol. */
516 switch (h->root.type)
518 default:
519 oldbfd = NULL;
520 break;
522 case bfd_link_hash_undefined:
523 case bfd_link_hash_undefweak:
524 oldbfd = h->root.u.undef.abfd;
525 break;
527 case bfd_link_hash_defined:
528 case bfd_link_hash_defweak:
529 oldbfd = h->root.u.def.section->owner;
530 break;
532 case bfd_link_hash_common:
533 oldbfd = h->root.u.c.p->section->owner;
534 break;
537 /* In cases involving weak versioned symbols, we may wind up trying
538 to merge a symbol with itself. Catch that here, to avoid the
539 confusion that results if we try to override a symbol with
540 itself. The additional tests catch cases like
541 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
542 dynamic object, which we do want to handle here. */
543 if (abfd == oldbfd
544 && ((abfd->flags & DYNAMIC) == 0
545 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
546 return true;
548 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
549 respectively, is from a dynamic object. */
551 if ((abfd->flags & DYNAMIC) != 0)
552 newdyn = true;
553 else
554 newdyn = false;
556 if (oldbfd != NULL)
557 olddyn = (oldbfd->flags & DYNAMIC) != 0;
558 else
560 asection *hsec;
562 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
563 indices used by MIPS ELF. */
564 switch (h->root.type)
566 default:
567 hsec = NULL;
568 break;
570 case bfd_link_hash_defined:
571 case bfd_link_hash_defweak:
572 hsec = h->root.u.def.section;
573 break;
575 case bfd_link_hash_common:
576 hsec = h->root.u.c.p->section;
577 break;
580 if (hsec == NULL)
581 olddyn = false;
582 else
583 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
586 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
587 respectively, appear to be a definition rather than reference. */
589 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
590 newdef = false;
591 else
592 newdef = true;
594 if (h->root.type == bfd_link_hash_undefined
595 || h->root.type == bfd_link_hash_undefweak
596 || h->root.type == bfd_link_hash_common)
597 olddef = false;
598 else
599 olddef = true;
601 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
602 symbol, respectively, appears to be a common symbol in a dynamic
603 object. If a symbol appears in an uninitialized section, and is
604 not weak, and is not a function, then it may be a common symbol
605 which was resolved when the dynamic object was created. We want
606 to treat such symbols specially, because they raise special
607 considerations when setting the symbol size: if the symbol
608 appears as a common symbol in a regular object, and the size in
609 the regular object is larger, we must make sure that we use the
610 larger size. This problematic case can always be avoided in C,
611 but it must be handled correctly when using Fortran shared
612 libraries.
614 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
615 likewise for OLDDYNCOMMON and OLDDEF.
617 Note that this test is just a heuristic, and that it is quite
618 possible to have an uninitialized symbol in a shared object which
619 is really a definition, rather than a common symbol. This could
620 lead to some minor confusion when the symbol really is a common
621 symbol in some regular object. However, I think it will be
622 harmless. */
624 if (newdyn
625 && newdef
626 && (sec->flags & SEC_ALLOC) != 0
627 && (sec->flags & SEC_LOAD) == 0
628 && sym->st_size > 0
629 && bind != STB_WEAK
630 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
631 newdyncommon = true;
632 else
633 newdyncommon = false;
635 if (olddyn
636 && olddef
637 && h->root.type == bfd_link_hash_defined
638 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
639 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
640 && (h->root.u.def.section->flags & SEC_LOAD) == 0
641 && h->size > 0
642 && h->type != STT_FUNC)
643 olddyncommon = true;
644 else
645 olddyncommon = false;
647 /* It's OK to change the type if either the existing symbol or the
648 new symbol is weak unless it comes from a DT_NEEDED entry of
649 a shared object, in which case, the DT_NEEDED entry may not be
650 required at the run time. */
652 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
653 || h->root.type == bfd_link_hash_undefweak
654 || bind == STB_WEAK)
655 *type_change_ok = true;
657 /* It's OK to change the size if either the existing symbol or the
658 new symbol is weak, or if the old symbol is undefined. */
660 if (*type_change_ok
661 || h->root.type == bfd_link_hash_undefined)
662 *size_change_ok = true;
664 /* If both the old and the new symbols look like common symbols in a
665 dynamic object, set the size of the symbol to the larger of the
666 two. */
668 if (olddyncommon
669 && newdyncommon
670 && sym->st_size != h->size)
672 /* Since we think we have two common symbols, issue a multiple
673 common warning if desired. Note that we only warn if the
674 size is different. If the size is the same, we simply let
675 the old symbol override the new one as normally happens with
676 symbols defined in dynamic objects. */
678 if (! ((*info->callbacks->multiple_common)
679 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
680 h->size, abfd, bfd_link_hash_common, sym->st_size)))
681 return false;
683 if (sym->st_size > h->size)
684 h->size = sym->st_size;
686 *size_change_ok = true;
689 /* If we are looking at a dynamic object, and we have found a
690 definition, we need to see if the symbol was already defined by
691 some other object. If so, we want to use the existing
692 definition, and we do not want to report a multiple symbol
693 definition error; we do this by clobbering *PSEC to be
694 bfd_und_section_ptr.
696 We treat a common symbol as a definition if the symbol in the
697 shared library is a function, since common symbols always
698 represent variables; this can cause confusion in principle, but
699 any such confusion would seem to indicate an erroneous program or
700 shared library. We also permit a common symbol in a regular
701 object to override a weak symbol in a shared object.
703 We prefer a non-weak definition in a shared library to a weak
704 definition in the executable unless it comes from a DT_NEEDED
705 entry of a shared object, in which case, the DT_NEEDED entry
706 may not be required at the run time. */
708 if (newdyn
709 && newdef
710 && (olddef
711 || (h->root.type == bfd_link_hash_common
712 && (bind == STB_WEAK
713 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
714 && (h->root.type != bfd_link_hash_defweak
715 || dt_needed
716 || bind == STB_WEAK))
718 *override = true;
719 newdef = false;
720 newdyncommon = false;
722 *psec = sec = bfd_und_section_ptr;
723 *size_change_ok = true;
725 /* If we get here when the old symbol is a common symbol, then
726 we are explicitly letting it override a weak symbol or
727 function in a dynamic object, and we don't want to warn about
728 a type change. If the old symbol is a defined symbol, a type
729 change warning may still be appropriate. */
731 if (h->root.type == bfd_link_hash_common)
732 *type_change_ok = true;
735 /* Handle the special case of an old common symbol merging with a
736 new symbol which looks like a common symbol in a shared object.
737 We change *PSEC and *PVALUE to make the new symbol look like a
738 common symbol, and let _bfd_generic_link_add_one_symbol will do
739 the right thing. */
741 if (newdyncommon
742 && h->root.type == bfd_link_hash_common)
744 *override = true;
745 newdef = false;
746 newdyncommon = false;
747 *pvalue = sym->st_size;
748 *psec = sec = bfd_com_section_ptr;
749 *size_change_ok = true;
752 /* If the old symbol is from a dynamic object, and the new symbol is
753 a definition which is not from a dynamic object, then the new
754 symbol overrides the old symbol. Symbols from regular files
755 always take precedence over symbols from dynamic objects, even if
756 they are defined after the dynamic object in the link.
758 As above, we again permit a common symbol in a regular object to
759 override a definition in a shared object if the shared object
760 symbol is a function or is weak.
762 As above, we permit a non-weak definition in a shared object to
763 override a weak definition in a regular object. */
765 if (! newdyn
766 && (newdef
767 || (bfd_is_com_section (sec)
768 && (h->root.type == bfd_link_hash_defweak
769 || h->type == STT_FUNC)))
770 && olddyn
771 && olddef
772 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
773 && (bind != STB_WEAK
774 || h->root.type == bfd_link_hash_defweak))
776 /* Change the hash table entry to undefined, and let
777 _bfd_generic_link_add_one_symbol do the right thing with the
778 new definition. */
780 h->root.type = bfd_link_hash_undefined;
781 h->root.u.undef.abfd = h->root.u.def.section->owner;
782 *size_change_ok = true;
784 olddef = false;
785 olddyncommon = false;
787 /* We again permit a type change when a common symbol may be
788 overriding a function. */
790 if (bfd_is_com_section (sec))
791 *type_change_ok = true;
793 /* This union may have been set to be non-NULL when this symbol
794 was seen in a dynamic object. We must force the union to be
795 NULL, so that it is correct for a regular symbol. */
797 h->verinfo.vertree = NULL;
799 /* In this special case, if H is the target of an indirection,
800 we want the caller to frob with H rather than with the
801 indirect symbol. That will permit the caller to redefine the
802 target of the indirection, rather than the indirect symbol
803 itself. FIXME: This will break the -y option if we store a
804 symbol with a different name. */
805 *sym_hash = h;
808 /* Handle the special case of a new common symbol merging with an
809 old symbol that looks like it might be a common symbol defined in
810 a shared object. Note that we have already handled the case in
811 which a new common symbol should simply override the definition
812 in the shared library. */
814 if (! newdyn
815 && bfd_is_com_section (sec)
816 && olddyncommon)
818 /* It would be best if we could set the hash table entry to a
819 common symbol, but we don't know what to use for the section
820 or the alignment. */
821 if (! ((*info->callbacks->multiple_common)
822 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
823 h->size, abfd, bfd_link_hash_common, sym->st_size)))
824 return false;
826 /* If the predumed common symbol in the dynamic object is
827 larger, pretend that the new symbol has its size. */
829 if (h->size > *pvalue)
830 *pvalue = h->size;
832 /* FIXME: We no longer know the alignment required by the symbol
833 in the dynamic object, so we just wind up using the one from
834 the regular object. */
836 olddef = false;
837 olddyncommon = false;
839 h->root.type = bfd_link_hash_undefined;
840 h->root.u.undef.abfd = h->root.u.def.section->owner;
842 *size_change_ok = true;
843 *type_change_ok = true;
845 h->verinfo.vertree = NULL;
848 /* Handle the special case of a weak definition in a regular object
849 followed by a non-weak definition in a shared object. In this
850 case, we prefer the definition in the shared object unless it
851 comes from a DT_NEEDED entry of a shared object, in which case,
852 the DT_NEEDED entry may not be required at the run time. */
853 if (olddef
854 && ! dt_needed
855 && h->root.type == bfd_link_hash_defweak
856 && newdef
857 && newdyn
858 && bind != STB_WEAK)
860 /* To make this work we have to frob the flags so that the rest
861 of the code does not think we are using the regular
862 definition. */
863 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
864 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
865 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
866 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
867 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
868 | ELF_LINK_HASH_DEF_DYNAMIC);
870 /* If H is the target of an indirection, we want the caller to
871 use H rather than the indirect symbol. Otherwise if we are
872 defining a new indirect symbol we will wind up attaching it
873 to the entry we are overriding. */
874 *sym_hash = h;
877 /* Handle the special case of a non-weak definition in a shared
878 object followed by a weak definition in a regular object. In
879 this case we prefer to definition in the shared object. To make
880 this work we have to tell the caller to not treat the new symbol
881 as a definition. */
882 if (olddef
883 && olddyn
884 && h->root.type != bfd_link_hash_defweak
885 && newdef
886 && ! newdyn
887 && bind == STB_WEAK)
888 *override = true;
890 return true;
893 /* Add symbols from an ELF object file to the linker hash table. */
895 static boolean
896 elf_link_add_object_symbols (abfd, info)
897 bfd *abfd;
898 struct bfd_link_info *info;
900 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
901 const Elf_Internal_Sym *,
902 const char **, flagword *,
903 asection **, bfd_vma *));
904 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
905 asection *, const Elf_Internal_Rela *));
906 boolean collect;
907 Elf_Internal_Shdr *hdr;
908 bfd_size_type symcount;
909 bfd_size_type extsymcount;
910 bfd_size_type extsymoff;
911 Elf_External_Sym *buf = NULL;
912 struct elf_link_hash_entry **sym_hash;
913 boolean dynamic;
914 Elf_External_Versym *extversym = NULL;
915 Elf_External_Versym *ever;
916 Elf_External_Dyn *dynbuf = NULL;
917 struct elf_link_hash_entry *weaks;
918 Elf_External_Sym *esym;
919 Elf_External_Sym *esymend;
920 struct elf_backend_data *bed;
921 boolean dt_needed;
922 struct elf_link_hash_table * hash_table;
923 file_ptr pos;
924 bfd_size_type amt;
926 hash_table = elf_hash_table (info);
928 bed = get_elf_backend_data (abfd);
929 add_symbol_hook = bed->elf_add_symbol_hook;
930 collect = bed->collect;
932 if ((abfd->flags & DYNAMIC) == 0)
933 dynamic = false;
934 else
936 dynamic = true;
938 /* You can't use -r against a dynamic object. Also, there's no
939 hope of using a dynamic object which does not exactly match
940 the format of the output file. */
941 if (info->relocateable || info->hash->creator != abfd->xvec)
943 bfd_set_error (bfd_error_invalid_operation);
944 goto error_return;
948 /* As a GNU extension, any input sections which are named
949 .gnu.warning.SYMBOL are treated as warning symbols for the given
950 symbol. This differs from .gnu.warning sections, which generate
951 warnings when they are included in an output file. */
952 if (! info->shared)
954 asection *s;
956 for (s = abfd->sections; s != NULL; s = s->next)
958 const char *name;
960 name = bfd_get_section_name (abfd, s);
961 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
963 char *msg;
964 bfd_size_type sz;
966 name += sizeof ".gnu.warning." - 1;
968 /* If this is a shared object, then look up the symbol
969 in the hash table. If it is there, and it is already
970 been defined, then we will not be using the entry
971 from this shared object, so we don't need to warn.
972 FIXME: If we see the definition in a regular object
973 later on, we will warn, but we shouldn't. The only
974 fix is to keep track of what warnings we are supposed
975 to emit, and then handle them all at the end of the
976 link. */
977 if (dynamic && abfd->xvec == info->hash->creator)
979 struct elf_link_hash_entry *h;
981 h = elf_link_hash_lookup (hash_table, name,
982 false, false, true);
984 /* FIXME: What about bfd_link_hash_common? */
985 if (h != NULL
986 && (h->root.type == bfd_link_hash_defined
987 || h->root.type == bfd_link_hash_defweak))
989 /* We don't want to issue this warning. Clobber
990 the section size so that the warning does not
991 get copied into the output file. */
992 s->_raw_size = 0;
993 continue;
997 sz = bfd_section_size (abfd, s);
998 msg = (char *) bfd_alloc (abfd, sz + 1);
999 if (msg == NULL)
1000 goto error_return;
1002 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1003 goto error_return;
1005 msg[sz] = '\0';
1007 if (! (_bfd_generic_link_add_one_symbol
1008 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1009 false, collect, (struct bfd_link_hash_entry **) NULL)))
1010 goto error_return;
1012 if (! info->relocateable)
1014 /* Clobber the section size so that the warning does
1015 not get copied into the output file. */
1016 s->_raw_size = 0;
1022 /* If this is a dynamic object, we always link against the .dynsym
1023 symbol table, not the .symtab symbol table. The dynamic linker
1024 will only see the .dynsym symbol table, so there is no reason to
1025 look at .symtab for a dynamic object. */
1027 if (! dynamic || elf_dynsymtab (abfd) == 0)
1028 hdr = &elf_tdata (abfd)->symtab_hdr;
1029 else
1030 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1032 if (dynamic)
1034 /* Read in any version definitions. */
1036 if (! _bfd_elf_slurp_version_tables (abfd))
1037 goto error_return;
1039 /* Read in the symbol versions, but don't bother to convert them
1040 to internal format. */
1041 if (elf_dynversym (abfd) != 0)
1043 Elf_Internal_Shdr *versymhdr;
1045 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1046 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1047 if (extversym == NULL)
1048 goto error_return;
1049 amt = versymhdr->sh_size;
1050 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1051 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1052 goto error_return;
1056 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1058 /* The sh_info field of the symtab header tells us where the
1059 external symbols start. We don't care about the local symbols at
1060 this point. */
1061 if (elf_bad_symtab (abfd))
1063 extsymcount = symcount;
1064 extsymoff = 0;
1066 else
1068 extsymcount = symcount - hdr->sh_info;
1069 extsymoff = hdr->sh_info;
1072 amt = extsymcount * sizeof (Elf_External_Sym);
1073 buf = (Elf_External_Sym *) bfd_malloc (amt);
1074 if (buf == NULL && extsymcount != 0)
1075 goto error_return;
1077 /* We store a pointer to the hash table entry for each external
1078 symbol. */
1079 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1080 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1081 if (sym_hash == NULL)
1082 goto error_return;
1083 elf_sym_hashes (abfd) = sym_hash;
1085 dt_needed = false;
1087 if (! dynamic)
1089 /* If we are creating a shared library, create all the dynamic
1090 sections immediately. We need to attach them to something,
1091 so we attach them to this BFD, provided it is the right
1092 format. FIXME: If there are no input BFD's of the same
1093 format as the output, we can't make a shared library. */
1094 if (info->shared
1095 && is_elf_hash_table (info)
1096 && ! hash_table->dynamic_sections_created
1097 && abfd->xvec == info->hash->creator)
1099 if (! elf_link_create_dynamic_sections (abfd, info))
1100 goto error_return;
1103 else if (! is_elf_hash_table (info))
1104 goto error_return;
1105 else
1107 asection *s;
1108 boolean add_needed;
1109 const char *name;
1110 bfd_size_type oldsize;
1111 bfd_size_type strindex;
1113 /* Find the name to use in a DT_NEEDED entry that refers to this
1114 object. If the object has a DT_SONAME entry, we use it.
1115 Otherwise, if the generic linker stuck something in
1116 elf_dt_name, we use that. Otherwise, we just use the file
1117 name. If the generic linker put a null string into
1118 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1119 there is a DT_SONAME entry. */
1120 add_needed = true;
1121 name = bfd_get_filename (abfd);
1122 if (elf_dt_name (abfd) != NULL)
1124 name = elf_dt_name (abfd);
1125 if (*name == '\0')
1127 if (elf_dt_soname (abfd) != NULL)
1128 dt_needed = true;
1130 add_needed = false;
1133 s = bfd_get_section_by_name (abfd, ".dynamic");
1134 if (s != NULL)
1136 Elf_External_Dyn *extdyn;
1137 Elf_External_Dyn *extdynend;
1138 int elfsec;
1139 unsigned long shlink;
1140 int rpath;
1141 int runpath;
1143 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1144 if (dynbuf == NULL)
1145 goto error_return;
1147 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1148 (file_ptr) 0, s->_raw_size))
1149 goto error_return;
1151 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1152 if (elfsec == -1)
1153 goto error_return;
1154 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1157 /* The shared libraries distributed with hpux11 have a bogus
1158 sh_link field for the ".dynamic" section. This code detects
1159 when SHLINK refers to a section that is not a string table
1160 and tries to find the string table for the ".dynsym" section
1161 instead. */
1162 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1163 if (shdr->sh_type != SHT_STRTAB)
1165 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1166 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1167 if (elfdsec == -1)
1168 goto error_return;
1169 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1173 extdyn = dynbuf;
1174 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1175 rpath = 0;
1176 runpath = 0;
1177 for (; extdyn < extdynend; extdyn++)
1179 Elf_Internal_Dyn dyn;
1181 elf_swap_dyn_in (abfd, extdyn, &dyn);
1182 if (dyn.d_tag == DT_SONAME)
1184 unsigned int tagv = dyn.d_un.d_val;
1185 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1186 if (name == NULL)
1187 goto error_return;
1189 if (dyn.d_tag == DT_NEEDED)
1191 struct bfd_link_needed_list *n, **pn;
1192 char *fnm, *anm;
1193 unsigned int tagv = dyn.d_un.d_val;
1195 amt = sizeof (struct bfd_link_needed_list);
1196 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1197 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1198 if (n == NULL || fnm == NULL)
1199 goto error_return;
1200 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1201 if (anm == NULL)
1202 goto error_return;
1203 strcpy (anm, fnm);
1204 n->name = anm;
1205 n->by = abfd;
1206 n->next = NULL;
1207 for (pn = & hash_table->needed;
1208 *pn != NULL;
1209 pn = &(*pn)->next)
1211 *pn = n;
1213 if (dyn.d_tag == DT_RUNPATH)
1215 struct bfd_link_needed_list *n, **pn;
1216 char *fnm, *anm;
1217 unsigned int tagv = dyn.d_un.d_val;
1219 /* When we see DT_RPATH before DT_RUNPATH, we have
1220 to clear runpath. Do _NOT_ bfd_release, as that
1221 frees all more recently bfd_alloc'd blocks as
1222 well. */
1223 if (rpath && hash_table->runpath)
1224 hash_table->runpath = NULL;
1226 amt = sizeof (struct bfd_link_needed_list);
1227 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1228 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1229 if (n == NULL || fnm == NULL)
1230 goto error_return;
1231 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1232 if (anm == NULL)
1233 goto error_return;
1234 strcpy (anm, fnm);
1235 n->name = anm;
1236 n->by = abfd;
1237 n->next = NULL;
1238 for (pn = & hash_table->runpath;
1239 *pn != NULL;
1240 pn = &(*pn)->next)
1242 *pn = n;
1243 runpath = 1;
1244 rpath = 0;
1246 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1247 if (!runpath && dyn.d_tag == DT_RPATH)
1249 struct bfd_link_needed_list *n, **pn;
1250 char *fnm, *anm;
1251 unsigned int tagv = dyn.d_un.d_val;
1253 amt = sizeof (struct bfd_link_needed_list);
1254 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1255 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1256 if (n == NULL || fnm == NULL)
1257 goto error_return;
1258 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1259 if (anm == NULL)
1260 goto error_return;
1261 strcpy (anm, fnm);
1262 n->name = anm;
1263 n->by = abfd;
1264 n->next = NULL;
1265 for (pn = & hash_table->runpath;
1266 *pn != NULL;
1267 pn = &(*pn)->next)
1269 *pn = n;
1270 rpath = 1;
1274 free (dynbuf);
1275 dynbuf = NULL;
1278 /* We do not want to include any of the sections in a dynamic
1279 object in the output file. We hack by simply clobbering the
1280 list of sections in the BFD. This could be handled more
1281 cleanly by, say, a new section flag; the existing
1282 SEC_NEVER_LOAD flag is not the one we want, because that one
1283 still implies that the section takes up space in the output
1284 file. */
1285 abfd->sections = NULL;
1286 abfd->section_count = 0;
1288 /* If this is the first dynamic object found in the link, create
1289 the special sections required for dynamic linking. */
1290 if (! hash_table->dynamic_sections_created)
1291 if (! elf_link_create_dynamic_sections (abfd, info))
1292 goto error_return;
1294 if (add_needed)
1296 /* Add a DT_NEEDED entry for this dynamic object. */
1297 oldsize = _bfd_stringtab_size (hash_table->dynstr);
1298 strindex = _bfd_stringtab_add (hash_table->dynstr, name,
1299 true, false);
1300 if (strindex == (bfd_size_type) -1)
1301 goto error_return;
1303 if (oldsize == _bfd_stringtab_size (hash_table->dynstr))
1305 asection *sdyn;
1306 Elf_External_Dyn *dyncon, *dynconend;
1308 /* The hash table size did not change, which means that
1309 the dynamic object name was already entered. If we
1310 have already included this dynamic object in the
1311 link, just ignore it. There is no reason to include
1312 a particular dynamic object more than once. */
1313 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1314 BFD_ASSERT (sdyn != NULL);
1316 dyncon = (Elf_External_Dyn *) sdyn->contents;
1317 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1318 sdyn->_raw_size);
1319 for (; dyncon < dynconend; dyncon++)
1321 Elf_Internal_Dyn dyn;
1323 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1324 if (dyn.d_tag == DT_NEEDED
1325 && dyn.d_un.d_val == strindex)
1327 if (buf != NULL)
1328 free (buf);
1329 if (extversym != NULL)
1330 free (extversym);
1331 return true;
1336 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1337 goto error_return;
1340 /* Save the SONAME, if there is one, because sometimes the
1341 linker emulation code will need to know it. */
1342 if (*name == '\0')
1343 name = basename (bfd_get_filename (abfd));
1344 elf_dt_name (abfd) = name;
1347 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1348 amt = extsymcount * sizeof (Elf_External_Sym);
1349 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1350 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1351 goto error_return;
1353 weaks = NULL;
1355 ever = extversym != NULL ? extversym + extsymoff : NULL;
1356 esymend = buf + extsymcount;
1357 for (esym = buf;
1358 esym < esymend;
1359 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1361 Elf_Internal_Sym sym;
1362 int bind;
1363 bfd_vma value;
1364 asection *sec;
1365 flagword flags;
1366 const char *name;
1367 struct elf_link_hash_entry *h;
1368 boolean definition;
1369 boolean size_change_ok, type_change_ok;
1370 boolean new_weakdef;
1371 unsigned int old_alignment;
1373 elf_swap_symbol_in (abfd, esym, &sym);
1375 flags = BSF_NO_FLAGS;
1376 sec = NULL;
1377 value = sym.st_value;
1378 *sym_hash = NULL;
1380 bind = ELF_ST_BIND (sym.st_info);
1381 if (bind == STB_LOCAL)
1383 /* This should be impossible, since ELF requires that all
1384 global symbols follow all local symbols, and that sh_info
1385 point to the first global symbol. Unfortunatealy, Irix 5
1386 screws this up. */
1387 continue;
1389 else if (bind == STB_GLOBAL)
1391 if (sym.st_shndx != SHN_UNDEF
1392 && sym.st_shndx != SHN_COMMON)
1393 flags = BSF_GLOBAL;
1395 else if (bind == STB_WEAK)
1396 flags = BSF_WEAK;
1397 else
1399 /* Leave it up to the processor backend. */
1402 if (sym.st_shndx == SHN_UNDEF)
1403 sec = bfd_und_section_ptr;
1404 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
1406 sec = section_from_elf_index (abfd, sym.st_shndx);
1407 if (sec == NULL)
1408 sec = bfd_abs_section_ptr;
1409 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1410 value -= sec->vma;
1412 else if (sym.st_shndx == SHN_ABS)
1413 sec = bfd_abs_section_ptr;
1414 else if (sym.st_shndx == SHN_COMMON)
1416 sec = bfd_com_section_ptr;
1417 /* What ELF calls the size we call the value. What ELF
1418 calls the value we call the alignment. */
1419 value = sym.st_size;
1421 else
1423 /* Leave it up to the processor backend. */
1426 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1427 if (name == (const char *) NULL)
1428 goto error_return;
1430 if (add_symbol_hook)
1432 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1433 &value))
1434 goto error_return;
1436 /* The hook function sets the name to NULL if this symbol
1437 should be skipped for some reason. */
1438 if (name == (const char *) NULL)
1439 continue;
1442 /* Sanity check that all possibilities were handled. */
1443 if (sec == (asection *) NULL)
1445 bfd_set_error (bfd_error_bad_value);
1446 goto error_return;
1449 if (bfd_is_und_section (sec)
1450 || bfd_is_com_section (sec))
1451 definition = false;
1452 else
1453 definition = true;
1455 size_change_ok = false;
1456 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1457 old_alignment = 0;
1458 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1460 Elf_Internal_Versym iver;
1461 unsigned int vernum = 0;
1462 boolean override;
1464 if (ever != NULL)
1466 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1467 vernum = iver.vs_vers & VERSYM_VERSION;
1469 /* If this is a hidden symbol, or if it is not version
1470 1, we append the version name to the symbol name.
1471 However, we do not modify a non-hidden absolute
1472 symbol, because it might be the version symbol
1473 itself. FIXME: What if it isn't? */
1474 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1475 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1477 const char *verstr;
1478 unsigned int namelen;
1479 bfd_size_type newlen;
1480 char *newname, *p;
1482 if (sym.st_shndx != SHN_UNDEF)
1484 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1486 (*_bfd_error_handler)
1487 (_("%s: %s: invalid version %u (max %d)"),
1488 bfd_archive_filename (abfd), name, vernum,
1489 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1490 bfd_set_error (bfd_error_bad_value);
1491 goto error_return;
1493 else if (vernum > 1)
1494 verstr =
1495 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1496 else
1497 verstr = "";
1499 else
1501 /* We cannot simply test for the number of
1502 entries in the VERNEED section since the
1503 numbers for the needed versions do not start
1504 at 0. */
1505 Elf_Internal_Verneed *t;
1507 verstr = NULL;
1508 for (t = elf_tdata (abfd)->verref;
1509 t != NULL;
1510 t = t->vn_nextref)
1512 Elf_Internal_Vernaux *a;
1514 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1516 if (a->vna_other == vernum)
1518 verstr = a->vna_nodename;
1519 break;
1522 if (a != NULL)
1523 break;
1525 if (verstr == NULL)
1527 (*_bfd_error_handler)
1528 (_("%s: %s: invalid needed version %d"),
1529 bfd_archive_filename (abfd), name, vernum);
1530 bfd_set_error (bfd_error_bad_value);
1531 goto error_return;
1535 namelen = strlen (name);
1536 newlen = namelen + strlen (verstr) + 2;
1537 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1538 ++newlen;
1540 newname = (char *) bfd_alloc (abfd, newlen);
1541 if (newname == NULL)
1542 goto error_return;
1543 strcpy (newname, name);
1544 p = newname + namelen;
1545 *p++ = ELF_VER_CHR;
1546 /* If this is a defined non-hidden version symbol,
1547 we add another @ to the name. This indicates the
1548 default version of the symbol. */
1549 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1550 && sym.st_shndx != SHN_UNDEF)
1551 *p++ = ELF_VER_CHR;
1552 strcpy (p, verstr);
1554 name = newname;
1558 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1559 sym_hash, &override, &type_change_ok,
1560 &size_change_ok, dt_needed))
1561 goto error_return;
1563 if (override)
1564 definition = false;
1566 h = *sym_hash;
1567 while (h->root.type == bfd_link_hash_indirect
1568 || h->root.type == bfd_link_hash_warning)
1569 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1571 /* Remember the old alignment if this is a common symbol, so
1572 that we don't reduce the alignment later on. We can't
1573 check later, because _bfd_generic_link_add_one_symbol
1574 will set a default for the alignment which we want to
1575 override. */
1576 if (h->root.type == bfd_link_hash_common)
1577 old_alignment = h->root.u.c.p->alignment_power;
1579 if (elf_tdata (abfd)->verdef != NULL
1580 && ! override
1581 && vernum > 1
1582 && definition)
1583 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1586 if (! (_bfd_generic_link_add_one_symbol
1587 (info, abfd, name, flags, sec, value, (const char *) NULL,
1588 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1589 goto error_return;
1591 h = *sym_hash;
1592 while (h->root.type == bfd_link_hash_indirect
1593 || h->root.type == bfd_link_hash_warning)
1594 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1595 *sym_hash = h;
1597 new_weakdef = false;
1598 if (dynamic
1599 && definition
1600 && (flags & BSF_WEAK) != 0
1601 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1602 && info->hash->creator->flavour == bfd_target_elf_flavour
1603 && h->weakdef == NULL)
1605 /* Keep a list of all weak defined non function symbols from
1606 a dynamic object, using the weakdef field. Later in this
1607 function we will set the weakdef field to the correct
1608 value. We only put non-function symbols from dynamic
1609 objects on this list, because that happens to be the only
1610 time we need to know the normal symbol corresponding to a
1611 weak symbol, and the information is time consuming to
1612 figure out. If the weakdef field is not already NULL,
1613 then this symbol was already defined by some previous
1614 dynamic object, and we will be using that previous
1615 definition anyhow. */
1617 h->weakdef = weaks;
1618 weaks = h;
1619 new_weakdef = true;
1622 /* Set the alignment of a common symbol. */
1623 if (sym.st_shndx == SHN_COMMON
1624 && h->root.type == bfd_link_hash_common)
1626 unsigned int align;
1628 align = bfd_log2 (sym.st_value);
1629 if (align > old_alignment
1630 /* Permit an alignment power of zero if an alignment of one
1631 is specified and no other alignments have been specified. */
1632 || (sym.st_value == 1 && old_alignment == 0))
1633 h->root.u.c.p->alignment_power = align;
1636 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1638 int old_flags;
1639 boolean dynsym;
1640 int new_flag;
1642 /* Remember the symbol size and type. */
1643 if (sym.st_size != 0
1644 && (definition || h->size == 0))
1646 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1647 (*_bfd_error_handler)
1648 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1649 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1650 bfd_archive_filename (abfd));
1652 h->size = sym.st_size;
1655 /* If this is a common symbol, then we always want H->SIZE
1656 to be the size of the common symbol. The code just above
1657 won't fix the size if a common symbol becomes larger. We
1658 don't warn about a size change here, because that is
1659 covered by --warn-common. */
1660 if (h->root.type == bfd_link_hash_common)
1661 h->size = h->root.u.c.size;
1663 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1664 && (definition || h->type == STT_NOTYPE))
1666 if (h->type != STT_NOTYPE
1667 && h->type != ELF_ST_TYPE (sym.st_info)
1668 && ! type_change_ok)
1669 (*_bfd_error_handler)
1670 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1671 name, h->type, ELF_ST_TYPE (sym.st_info),
1672 bfd_archive_filename (abfd));
1674 h->type = ELF_ST_TYPE (sym.st_info);
1677 /* If st_other has a processor-specific meaning, specific code
1678 might be needed here. */
1679 if (sym.st_other != 0)
1681 /* Combine visibilities, using the most constraining one. */
1682 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1683 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1685 if (symvis && (hvis > symvis || hvis == 0))
1686 h->other = sym.st_other;
1688 /* If neither has visibility, use the st_other of the
1689 definition. This is an arbitrary choice, since the
1690 other bits have no general meaning. */
1691 if (!symvis && !hvis
1692 && (definition || h->other == 0))
1693 h->other = sym.st_other;
1696 /* Set a flag in the hash table entry indicating the type of
1697 reference or definition we just found. Keep a count of
1698 the number of dynamic symbols we find. A dynamic symbol
1699 is one which is referenced or defined by both a regular
1700 object and a shared object. */
1701 old_flags = h->elf_link_hash_flags;
1702 dynsym = false;
1703 if (! dynamic)
1705 if (! definition)
1707 new_flag = ELF_LINK_HASH_REF_REGULAR;
1708 if (bind != STB_WEAK)
1709 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1711 else
1712 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1713 if (info->shared
1714 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1715 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1716 dynsym = true;
1718 else
1720 if (! definition)
1721 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1722 else
1723 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1724 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1725 | ELF_LINK_HASH_REF_REGULAR)) != 0
1726 || (h->weakdef != NULL
1727 && ! new_weakdef
1728 && h->weakdef->dynindx != -1))
1729 dynsym = true;
1732 h->elf_link_hash_flags |= new_flag;
1734 /* If this symbol has a version, and it is the default
1735 version, we create an indirect symbol from the default
1736 name to the fully decorated name. This will cause
1737 external references which do not specify a version to be
1738 bound to this version of the symbol. */
1739 if (definition || h->root.type == bfd_link_hash_common)
1741 char *p;
1743 p = strchr (name, ELF_VER_CHR);
1744 if (p != NULL && p[1] == ELF_VER_CHR)
1746 char *shortname;
1747 struct elf_link_hash_entry *hi;
1748 boolean override;
1750 shortname = bfd_hash_allocate (&info->hash->table,
1751 (size_t) (p - name + 1));
1752 if (shortname == NULL)
1753 goto error_return;
1754 strncpy (shortname, name, (size_t) (p - name));
1755 shortname[p - name] = '\0';
1757 /* We are going to create a new symbol. Merge it
1758 with any existing symbol with this name. For the
1759 purposes of the merge, act as though we were
1760 defining the symbol we just defined, although we
1761 actually going to define an indirect symbol. */
1762 type_change_ok = false;
1763 size_change_ok = false;
1764 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1765 &value, &hi, &override,
1766 &type_change_ok,
1767 &size_change_ok, dt_needed))
1768 goto error_return;
1770 if (! override)
1772 if (! (_bfd_generic_link_add_one_symbol
1773 (info, abfd, shortname, BSF_INDIRECT,
1774 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1775 collect, (struct bfd_link_hash_entry **) &hi)))
1776 goto error_return;
1778 else
1780 /* In this case the symbol named SHORTNAME is
1781 overriding the indirect symbol we want to
1782 add. We were planning on making SHORTNAME an
1783 indirect symbol referring to NAME. SHORTNAME
1784 is the name without a version. NAME is the
1785 fully versioned name, and it is the default
1786 version.
1788 Overriding means that we already saw a
1789 definition for the symbol SHORTNAME in a
1790 regular object, and it is overriding the
1791 symbol defined in the dynamic object.
1793 When this happens, we actually want to change
1794 NAME, the symbol we just added, to refer to
1795 SHORTNAME. This will cause references to
1796 NAME in the shared object to become
1797 references to SHORTNAME in the regular
1798 object. This is what we expect when we
1799 override a function in a shared object: that
1800 the references in the shared object will be
1801 mapped to the definition in the regular
1802 object. */
1804 while (hi->root.type == bfd_link_hash_indirect
1805 || hi->root.type == bfd_link_hash_warning)
1806 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1810 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1812 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1813 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1814 if (hi->elf_link_hash_flags
1815 & (ELF_LINK_HASH_REF_REGULAR
1816 | ELF_LINK_HASH_DEF_REGULAR))
1818 if (! _bfd_elf_link_record_dynamic_symbol (info,
1819 hi))
1820 goto error_return;
1824 /* Now set HI to H, so that the following code
1825 will set the other fields correctly. */
1826 hi = h;
1829 /* If there is a duplicate definition somewhere,
1830 then HI may not point to an indirect symbol. We
1831 will have reported an error to the user in that
1832 case. */
1834 if (hi->root.type == bfd_link_hash_indirect)
1836 struct elf_link_hash_entry *ht;
1838 /* If the symbol became indirect, then we assume
1839 that we have not seen a definition before. */
1840 BFD_ASSERT ((hi->elf_link_hash_flags
1841 & (ELF_LINK_HASH_DEF_DYNAMIC
1842 | ELF_LINK_HASH_DEF_REGULAR))
1843 == 0);
1845 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1846 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1848 /* See if the new flags lead us to realize that
1849 the symbol must be dynamic. */
1850 if (! dynsym)
1852 if (! dynamic)
1854 if (info->shared
1855 || ((hi->elf_link_hash_flags
1856 & ELF_LINK_HASH_REF_DYNAMIC)
1857 != 0))
1858 dynsym = true;
1860 else
1862 if ((hi->elf_link_hash_flags
1863 & ELF_LINK_HASH_REF_REGULAR) != 0)
1864 dynsym = true;
1869 /* We also need to define an indirection from the
1870 nondefault version of the symbol. */
1872 shortname = bfd_hash_allocate (&info->hash->table,
1873 strlen (name));
1874 if (shortname == NULL)
1875 goto error_return;
1876 strncpy (shortname, name, (size_t) (p - name));
1877 strcpy (shortname + (p - name), p + 1);
1879 /* Once again, merge with any existing symbol. */
1880 type_change_ok = false;
1881 size_change_ok = false;
1882 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1883 &value, &hi, &override,
1884 &type_change_ok,
1885 &size_change_ok, dt_needed))
1886 goto error_return;
1888 if (override)
1890 /* Here SHORTNAME is a versioned name, so we
1891 don't expect to see the type of override we
1892 do in the case above. */
1893 (*_bfd_error_handler)
1894 (_("%s: warning: unexpected redefinition of `%s'"),
1895 bfd_archive_filename (abfd), shortname);
1897 else
1899 if (! (_bfd_generic_link_add_one_symbol
1900 (info, abfd, shortname, BSF_INDIRECT,
1901 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1902 collect, (struct bfd_link_hash_entry **) &hi)))
1903 goto error_return;
1905 /* If there is a duplicate definition somewhere,
1906 then HI may not point to an indirect symbol.
1907 We will have reported an error to the user in
1908 that case. */
1910 if (hi->root.type == bfd_link_hash_indirect)
1912 /* If the symbol became indirect, then we
1913 assume that we have not seen a definition
1914 before. */
1915 BFD_ASSERT ((hi->elf_link_hash_flags
1916 & (ELF_LINK_HASH_DEF_DYNAMIC
1917 | ELF_LINK_HASH_DEF_REGULAR))
1918 == 0);
1920 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1922 /* See if the new flags lead us to realize
1923 that the symbol must be dynamic. */
1924 if (! dynsym)
1926 if (! dynamic)
1928 if (info->shared
1929 || ((hi->elf_link_hash_flags
1930 & ELF_LINK_HASH_REF_DYNAMIC)
1931 != 0))
1932 dynsym = true;
1934 else
1936 if ((hi->elf_link_hash_flags
1937 & ELF_LINK_HASH_REF_REGULAR) != 0)
1938 dynsym = true;
1946 if (dynsym && h->dynindx == -1)
1948 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1949 goto error_return;
1950 if (h->weakdef != NULL
1951 && ! new_weakdef
1952 && h->weakdef->dynindx == -1)
1954 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1955 goto error_return;
1958 else if (dynsym && h->dynindx != -1)
1959 /* If the symbol already has a dynamic index, but
1960 visibility says it should not be visible, turn it into
1961 a local symbol. */
1962 switch (ELF_ST_VISIBILITY (h->other))
1964 case STV_INTERNAL:
1965 case STV_HIDDEN:
1966 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1967 (*bed->elf_backend_hide_symbol) (info, h);
1968 break;
1971 if (dt_needed && definition
1972 && (h->elf_link_hash_flags
1973 & ELF_LINK_HASH_REF_REGULAR) != 0)
1975 bfd_size_type oldsize;
1976 bfd_size_type strindex;
1978 if (! is_elf_hash_table (info))
1979 goto error_return;
1981 /* The symbol from a DT_NEEDED object is referenced from
1982 the regular object to create a dynamic executable. We
1983 have to make sure there is a DT_NEEDED entry for it. */
1985 dt_needed = false;
1986 oldsize = _bfd_stringtab_size (hash_table->dynstr);
1987 strindex = _bfd_stringtab_add (hash_table->dynstr,
1988 elf_dt_soname (abfd),
1989 true, false);
1990 if (strindex == (bfd_size_type) -1)
1991 goto error_return;
1993 if (oldsize
1994 == _bfd_stringtab_size (hash_table->dynstr))
1996 asection *sdyn;
1997 Elf_External_Dyn *dyncon, *dynconend;
1999 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2000 ".dynamic");
2001 BFD_ASSERT (sdyn != NULL);
2003 dyncon = (Elf_External_Dyn *) sdyn->contents;
2004 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2005 sdyn->_raw_size);
2006 for (; dyncon < dynconend; dyncon++)
2008 Elf_Internal_Dyn dyn;
2010 elf_swap_dyn_in (hash_table->dynobj,
2011 dyncon, &dyn);
2012 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2013 dyn.d_un.d_val != strindex);
2017 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2018 goto error_return;
2023 /* Now set the weakdefs field correctly for all the weak defined
2024 symbols we found. The only way to do this is to search all the
2025 symbols. Since we only need the information for non functions in
2026 dynamic objects, that's the only time we actually put anything on
2027 the list WEAKS. We need this information so that if a regular
2028 object refers to a symbol defined weakly in a dynamic object, the
2029 real symbol in the dynamic object is also put in the dynamic
2030 symbols; we also must arrange for both symbols to point to the
2031 same memory location. We could handle the general case of symbol
2032 aliasing, but a general symbol alias can only be generated in
2033 assembler code, handling it correctly would be very time
2034 consuming, and other ELF linkers don't handle general aliasing
2035 either. */
2036 while (weaks != NULL)
2038 struct elf_link_hash_entry *hlook;
2039 asection *slook;
2040 bfd_vma vlook;
2041 struct elf_link_hash_entry **hpp;
2042 struct elf_link_hash_entry **hppend;
2044 hlook = weaks;
2045 weaks = hlook->weakdef;
2046 hlook->weakdef = NULL;
2048 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2049 || hlook->root.type == bfd_link_hash_defweak
2050 || hlook->root.type == bfd_link_hash_common
2051 || hlook->root.type == bfd_link_hash_indirect);
2052 slook = hlook->root.u.def.section;
2053 vlook = hlook->root.u.def.value;
2055 hpp = elf_sym_hashes (abfd);
2056 hppend = hpp + extsymcount;
2057 for (; hpp < hppend; hpp++)
2059 struct elf_link_hash_entry *h;
2061 h = *hpp;
2062 if (h != NULL && h != hlook
2063 && h->root.type == bfd_link_hash_defined
2064 && h->root.u.def.section == slook
2065 && h->root.u.def.value == vlook)
2067 hlook->weakdef = h;
2069 /* If the weak definition is in the list of dynamic
2070 symbols, make sure the real definition is put there
2071 as well. */
2072 if (hlook->dynindx != -1
2073 && h->dynindx == -1)
2075 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2076 goto error_return;
2079 /* If the real definition is in the list of dynamic
2080 symbols, make sure the weak definition is put there
2081 as well. If we don't do this, then the dynamic
2082 loader might not merge the entries for the real
2083 definition and the weak definition. */
2084 if (h->dynindx != -1
2085 && hlook->dynindx == -1)
2087 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2088 goto error_return;
2091 break;
2096 if (buf != NULL)
2098 free (buf);
2099 buf = NULL;
2102 if (extversym != NULL)
2104 free (extversym);
2105 extversym = NULL;
2108 /* If this object is the same format as the output object, and it is
2109 not a shared library, then let the backend look through the
2110 relocs.
2112 This is required to build global offset table entries and to
2113 arrange for dynamic relocs. It is not required for the
2114 particular common case of linking non PIC code, even when linking
2115 against shared libraries, but unfortunately there is no way of
2116 knowing whether an object file has been compiled PIC or not.
2117 Looking through the relocs is not particularly time consuming.
2118 The problem is that we must either (1) keep the relocs in memory,
2119 which causes the linker to require additional runtime memory or
2120 (2) read the relocs twice from the input file, which wastes time.
2121 This would be a good case for using mmap.
2123 I have no idea how to handle linking PIC code into a file of a
2124 different format. It probably can't be done. */
2125 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2126 if (! dynamic
2127 && abfd->xvec == info->hash->creator
2128 && check_relocs != NULL)
2130 asection *o;
2132 for (o = abfd->sections; o != NULL; o = o->next)
2134 Elf_Internal_Rela *internal_relocs;
2135 boolean ok;
2137 if ((o->flags & SEC_RELOC) == 0
2138 || o->reloc_count == 0
2139 || ((info->strip == strip_all || info->strip == strip_debugger)
2140 && (o->flags & SEC_DEBUGGING) != 0)
2141 || bfd_is_abs_section (o->output_section))
2142 continue;
2144 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2145 (abfd, o, (PTR) NULL,
2146 (Elf_Internal_Rela *) NULL,
2147 info->keep_memory));
2148 if (internal_relocs == NULL)
2149 goto error_return;
2151 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2153 if (! info->keep_memory)
2154 free (internal_relocs);
2156 if (! ok)
2157 goto error_return;
2161 /* If this is a non-traditional, non-relocateable link, try to
2162 optimize the handling of the .stab/.stabstr sections. */
2163 if (! dynamic
2164 && ! info->relocateable
2165 && ! info->traditional_format
2166 && info->hash->creator->flavour == bfd_target_elf_flavour
2167 && is_elf_hash_table (info)
2168 && (info->strip != strip_all && info->strip != strip_debugger))
2170 asection *stab, *stabstr;
2172 stab = bfd_get_section_by_name (abfd, ".stab");
2173 if (stab != NULL)
2175 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2177 if (stabstr != NULL)
2179 struct bfd_elf_section_data *secdata;
2181 secdata = elf_section_data (stab);
2182 if (! _bfd_link_section_stabs (abfd,
2183 & hash_table->stab_info,
2184 stab, stabstr,
2185 &secdata->stab_info))
2186 goto error_return;
2191 if (! info->relocateable && ! dynamic
2192 && is_elf_hash_table (info))
2194 asection *s;
2196 for (s = abfd->sections; s != NULL; s = s->next)
2197 if ((s->flags & SEC_MERGE)
2198 && ! _bfd_merge_section (abfd, & hash_table->merge_info, s,
2199 & elf_section_data (s)->merge_info))
2200 goto error_return;
2203 return true;
2205 error_return:
2206 if (buf != NULL)
2207 free (buf);
2208 if (dynbuf != NULL)
2209 free (dynbuf);
2210 if (extversym != NULL)
2211 free (extversym);
2212 return false;
2215 /* Create some sections which will be filled in with dynamic linking
2216 information. ABFD is an input file which requires dynamic sections
2217 to be created. The dynamic sections take up virtual memory space
2218 when the final executable is run, so we need to create them before
2219 addresses are assigned to the output sections. We work out the
2220 actual contents and size of these sections later. */
2222 boolean
2223 elf_link_create_dynamic_sections (abfd, info)
2224 bfd *abfd;
2225 struct bfd_link_info *info;
2227 flagword flags;
2228 register asection *s;
2229 struct elf_link_hash_entry *h;
2230 struct elf_backend_data *bed;
2232 if (! is_elf_hash_table (info))
2233 return false;
2235 if (elf_hash_table (info)->dynamic_sections_created)
2236 return true;
2238 /* Make sure that all dynamic sections use the same input BFD. */
2239 if (elf_hash_table (info)->dynobj == NULL)
2240 elf_hash_table (info)->dynobj = abfd;
2241 else
2242 abfd = elf_hash_table (info)->dynobj;
2244 /* Note that we set the SEC_IN_MEMORY flag for all of these
2245 sections. */
2246 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2247 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2249 /* A dynamically linked executable has a .interp section, but a
2250 shared library does not. */
2251 if (! info->shared)
2253 s = bfd_make_section (abfd, ".interp");
2254 if (s == NULL
2255 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2256 return false;
2259 /* Create sections to hold version informations. These are removed
2260 if they are not needed. */
2261 s = bfd_make_section (abfd, ".gnu.version_d");
2262 if (s == NULL
2263 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2264 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2265 return false;
2267 s = bfd_make_section (abfd, ".gnu.version");
2268 if (s == NULL
2269 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2270 || ! bfd_set_section_alignment (abfd, s, 1))
2271 return false;
2273 s = bfd_make_section (abfd, ".gnu.version_r");
2274 if (s == NULL
2275 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2276 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2277 return false;
2279 s = bfd_make_section (abfd, ".dynsym");
2280 if (s == NULL
2281 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2282 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2283 return false;
2285 s = bfd_make_section (abfd, ".dynstr");
2286 if (s == NULL
2287 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2288 return false;
2290 /* Create a strtab to hold the dynamic symbol names. */
2291 if (elf_hash_table (info)->dynstr == NULL)
2293 elf_hash_table (info)->dynstr = elf_stringtab_init ();
2294 if (elf_hash_table (info)->dynstr == NULL)
2295 return false;
2298 s = bfd_make_section (abfd, ".dynamic");
2299 if (s == NULL
2300 || ! bfd_set_section_flags (abfd, s, flags)
2301 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2302 return false;
2304 /* The special symbol _DYNAMIC is always set to the start of the
2305 .dynamic section. This call occurs before we have processed the
2306 symbols for any dynamic object, so we don't have to worry about
2307 overriding a dynamic definition. We could set _DYNAMIC in a
2308 linker script, but we only want to define it if we are, in fact,
2309 creating a .dynamic section. We don't want to define it if there
2310 is no .dynamic section, since on some ELF platforms the start up
2311 code examines it to decide how to initialize the process. */
2312 h = NULL;
2313 if (! (_bfd_generic_link_add_one_symbol
2314 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2315 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2316 (struct bfd_link_hash_entry **) &h)))
2317 return false;
2318 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2319 h->type = STT_OBJECT;
2321 if (info->shared
2322 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2323 return false;
2325 bed = get_elf_backend_data (abfd);
2327 s = bfd_make_section (abfd, ".hash");
2328 if (s == NULL
2329 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2330 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2331 return false;
2332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2334 /* Let the backend create the rest of the sections. This lets the
2335 backend set the right flags. The backend will normally create
2336 the .got and .plt sections. */
2337 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2338 return false;
2340 elf_hash_table (info)->dynamic_sections_created = true;
2342 return true;
2345 /* Add an entry to the .dynamic table. */
2347 boolean
2348 elf_add_dynamic_entry (info, tag, val)
2349 struct bfd_link_info *info;
2350 bfd_vma tag;
2351 bfd_vma val;
2353 Elf_Internal_Dyn dyn;
2354 bfd *dynobj;
2355 asection *s;
2356 bfd_size_type newsize;
2357 bfd_byte *newcontents;
2359 if (! is_elf_hash_table (info))
2360 return false;
2362 dynobj = elf_hash_table (info)->dynobj;
2364 s = bfd_get_section_by_name (dynobj, ".dynamic");
2365 BFD_ASSERT (s != NULL);
2367 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2368 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2369 if (newcontents == NULL)
2370 return false;
2372 dyn.d_tag = tag;
2373 dyn.d_un.d_val = val;
2374 elf_swap_dyn_out (dynobj, &dyn,
2375 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2377 s->_raw_size = newsize;
2378 s->contents = newcontents;
2380 return true;
2383 /* Record a new local dynamic symbol. */
2385 boolean
2386 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2387 struct bfd_link_info *info;
2388 bfd *input_bfd;
2389 long input_indx;
2391 struct elf_link_local_dynamic_entry *entry;
2392 struct elf_link_hash_table *eht;
2393 struct bfd_strtab_hash *dynstr;
2394 Elf_External_Sym esym;
2395 unsigned long dynstr_index;
2396 char *name;
2397 file_ptr pos;
2398 bfd_size_type amt;
2400 if (! is_elf_hash_table (info))
2401 return false;
2403 /* See if the entry exists already. */
2404 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2405 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2406 return true;
2408 entry = (struct elf_link_local_dynamic_entry *)
2409 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2410 if (entry == NULL)
2411 return false;
2413 /* Go find the symbol, so that we can find it's name. */
2414 amt = sizeof (Elf_External_Sym);
2415 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2416 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2417 || bfd_bread (&esym, amt, input_bfd) != amt)
2418 return false;
2419 elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
2421 name = (bfd_elf_string_from_elf_section
2422 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2423 entry->isym.st_name));
2425 dynstr = elf_hash_table (info)->dynstr;
2426 if (dynstr == NULL)
2428 /* Create a strtab to hold the dynamic symbol names. */
2429 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
2430 if (dynstr == NULL)
2431 return false;
2434 dynstr_index = _bfd_stringtab_add (dynstr, name, true, false);
2435 if (dynstr_index == (unsigned long) -1)
2436 return false;
2437 entry->isym.st_name = dynstr_index;
2439 eht = elf_hash_table (info);
2441 entry->next = eht->dynlocal;
2442 eht->dynlocal = entry;
2443 entry->input_bfd = input_bfd;
2444 entry->input_indx = input_indx;
2445 eht->dynsymcount++;
2447 /* Whatever binding the symbol had before, it's now local. */
2448 entry->isym.st_info
2449 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2451 /* The dynindx will be set at the end of size_dynamic_sections. */
2453 return true;
2456 /* Read and swap the relocs from the section indicated by SHDR. This
2457 may be either a REL or a RELA section. The relocations are
2458 translated into RELA relocations and stored in INTERNAL_RELOCS,
2459 which should have already been allocated to contain enough space.
2460 The EXTERNAL_RELOCS are a buffer where the external form of the
2461 relocations should be stored.
2463 Returns false if something goes wrong. */
2465 static boolean
2466 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2467 internal_relocs)
2468 bfd *abfd;
2469 Elf_Internal_Shdr *shdr;
2470 PTR external_relocs;
2471 Elf_Internal_Rela *internal_relocs;
2473 struct elf_backend_data *bed;
2474 bfd_size_type amt;
2476 /* If there aren't any relocations, that's OK. */
2477 if (!shdr)
2478 return true;
2480 /* Position ourselves at the start of the section. */
2481 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2482 return false;
2484 /* Read the relocations. */
2485 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2486 return false;
2488 bed = get_elf_backend_data (abfd);
2490 /* Convert the external relocations to the internal format. */
2491 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2493 Elf_External_Rel *erel;
2494 Elf_External_Rel *erelend;
2495 Elf_Internal_Rela *irela;
2496 Elf_Internal_Rel *irel;
2498 erel = (Elf_External_Rel *) external_relocs;
2499 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2500 irela = internal_relocs;
2501 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2502 irel = bfd_alloc (abfd, amt);
2503 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2505 unsigned int i;
2507 if (bed->s->swap_reloc_in)
2508 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2509 else
2510 elf_swap_reloc_in (abfd, erel, irel);
2512 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2514 irela[i].r_offset = irel[i].r_offset;
2515 irela[i].r_info = irel[i].r_info;
2516 irela[i].r_addend = 0;
2520 else
2522 Elf_External_Rela *erela;
2523 Elf_External_Rela *erelaend;
2524 Elf_Internal_Rela *irela;
2526 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2528 erela = (Elf_External_Rela *) external_relocs;
2529 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2530 irela = internal_relocs;
2531 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2533 if (bed->s->swap_reloca_in)
2534 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2535 else
2536 elf_swap_reloca_in (abfd, erela, irela);
2540 return true;
2543 /* Read and swap the relocs for a section O. They may have been
2544 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2545 not NULL, they are used as buffers to read into. They are known to
2546 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2547 the return value is allocated using either malloc or bfd_alloc,
2548 according to the KEEP_MEMORY argument. If O has two relocation
2549 sections (both REL and RELA relocations), then the REL_HDR
2550 relocations will appear first in INTERNAL_RELOCS, followed by the
2551 REL_HDR2 relocations. */
2553 Elf_Internal_Rela *
2554 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2555 keep_memory)
2556 bfd *abfd;
2557 asection *o;
2558 PTR external_relocs;
2559 Elf_Internal_Rela *internal_relocs;
2560 boolean keep_memory;
2562 Elf_Internal_Shdr *rel_hdr;
2563 PTR alloc1 = NULL;
2564 Elf_Internal_Rela *alloc2 = NULL;
2565 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2567 if (elf_section_data (o)->relocs != NULL)
2568 return elf_section_data (o)->relocs;
2570 if (o->reloc_count == 0)
2571 return NULL;
2573 rel_hdr = &elf_section_data (o)->rel_hdr;
2575 if (internal_relocs == NULL)
2577 bfd_size_type size;
2579 size = o->reloc_count;
2580 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2581 if (keep_memory)
2582 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2583 else
2584 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2585 if (internal_relocs == NULL)
2586 goto error_return;
2589 if (external_relocs == NULL)
2591 bfd_size_type size = rel_hdr->sh_size;
2593 if (elf_section_data (o)->rel_hdr2)
2594 size += elf_section_data (o)->rel_hdr2->sh_size;
2595 alloc1 = (PTR) bfd_malloc (size);
2596 if (alloc1 == NULL)
2597 goto error_return;
2598 external_relocs = alloc1;
2601 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2602 external_relocs,
2603 internal_relocs))
2604 goto error_return;
2605 if (!elf_link_read_relocs_from_section
2606 (abfd,
2607 elf_section_data (o)->rel_hdr2,
2608 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2609 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2610 * bed->s->int_rels_per_ext_rel)))
2611 goto error_return;
2613 /* Cache the results for next time, if we can. */
2614 if (keep_memory)
2615 elf_section_data (o)->relocs = internal_relocs;
2617 if (alloc1 != NULL)
2618 free (alloc1);
2620 /* Don't free alloc2, since if it was allocated we are passing it
2621 back (under the name of internal_relocs). */
2623 return internal_relocs;
2625 error_return:
2626 if (alloc1 != NULL)
2627 free (alloc1);
2628 if (alloc2 != NULL)
2629 free (alloc2);
2630 return NULL;
2633 /* Record an assignment to a symbol made by a linker script. We need
2634 this in case some dynamic object refers to this symbol. */
2636 boolean
2637 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2638 bfd *output_bfd ATTRIBUTE_UNUSED;
2639 struct bfd_link_info *info;
2640 const char *name;
2641 boolean provide;
2643 struct elf_link_hash_entry *h;
2645 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2646 return true;
2648 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2649 if (h == NULL)
2650 return false;
2652 if (h->root.type == bfd_link_hash_new)
2653 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2655 /* If this symbol is being provided by the linker script, and it is
2656 currently defined by a dynamic object, but not by a regular
2657 object, then mark it as undefined so that the generic linker will
2658 force the correct value. */
2659 if (provide
2660 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2661 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2662 h->root.type = bfd_link_hash_undefined;
2664 /* If this symbol is not being provided by the linker script, and it is
2665 currently defined by a dynamic object, but not by a regular object,
2666 then clear out any version information because the symbol will not be
2667 associated with the dynamic object any more. */
2668 if (!provide
2669 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2670 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2671 h->verinfo.verdef = NULL;
2673 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2675 /* When possible, keep the original type of the symbol. */
2676 if (h->type == STT_NOTYPE)
2677 h->type = STT_OBJECT;
2679 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2680 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2681 || info->shared)
2682 && h->dynindx == -1)
2684 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2685 return false;
2687 /* If this is a weak defined symbol, and we know a corresponding
2688 real symbol from the same dynamic object, make sure the real
2689 symbol is also made into a dynamic symbol. */
2690 if (h->weakdef != NULL
2691 && h->weakdef->dynindx == -1)
2693 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2694 return false;
2698 return true;
2701 /* This structure is used to pass information to
2702 elf_link_assign_sym_version. */
2704 struct elf_assign_sym_version_info
2706 /* Output BFD. */
2707 bfd *output_bfd;
2708 /* General link information. */
2709 struct bfd_link_info *info;
2710 /* Version tree. */
2711 struct bfd_elf_version_tree *verdefs;
2712 /* Whether we had a failure. */
2713 boolean failed;
2716 /* This structure is used to pass information to
2717 elf_link_find_version_dependencies. */
2719 struct elf_find_verdep_info
2721 /* Output BFD. */
2722 bfd *output_bfd;
2723 /* General link information. */
2724 struct bfd_link_info *info;
2725 /* The number of dependencies. */
2726 unsigned int vers;
2727 /* Whether we had a failure. */
2728 boolean failed;
2731 /* Array used to determine the number of hash table buckets to use
2732 based on the number of symbols there are. If there are fewer than
2733 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2734 fewer than 37 we use 17 buckets, and so forth. We never use more
2735 than 32771 buckets. */
2737 static const size_t elf_buckets[] =
2739 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2740 16411, 32771, 0
2743 /* Compute bucket count for hashing table. We do not use a static set
2744 of possible tables sizes anymore. Instead we determine for all
2745 possible reasonable sizes of the table the outcome (i.e., the
2746 number of collisions etc) and choose the best solution. The
2747 weighting functions are not too simple to allow the table to grow
2748 without bounds. Instead one of the weighting factors is the size.
2749 Therefore the result is always a good payoff between few collisions
2750 (= short chain lengths) and table size. */
2751 static size_t
2752 compute_bucket_count (info)
2753 struct bfd_link_info *info;
2755 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2756 size_t best_size = 0;
2757 unsigned long int *hashcodes;
2758 unsigned long int *hashcodesp;
2759 unsigned long int i;
2760 bfd_size_type amt;
2762 /* Compute the hash values for all exported symbols. At the same
2763 time store the values in an array so that we could use them for
2764 optimizations. */
2765 amt = dynsymcount;
2766 amt *= sizeof (unsigned long int);
2767 hashcodes = (unsigned long int *) bfd_malloc (amt);
2768 if (hashcodes == NULL)
2769 return 0;
2770 hashcodesp = hashcodes;
2772 /* Put all hash values in HASHCODES. */
2773 elf_link_hash_traverse (elf_hash_table (info),
2774 elf_collect_hash_codes, &hashcodesp);
2776 /* We have a problem here. The following code to optimize the table
2777 size requires an integer type with more the 32 bits. If
2778 BFD_HOST_U_64_BIT is set we know about such a type. */
2779 #ifdef BFD_HOST_U_64_BIT
2780 if (info->optimize == true)
2782 unsigned long int nsyms = hashcodesp - hashcodes;
2783 size_t minsize;
2784 size_t maxsize;
2785 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2786 unsigned long int *counts ;
2788 /* Possible optimization parameters: if we have NSYMS symbols we say
2789 that the hashing table must at least have NSYMS/4 and at most
2790 2*NSYMS buckets. */
2791 minsize = nsyms / 4;
2792 if (minsize == 0)
2793 minsize = 1;
2794 best_size = maxsize = nsyms * 2;
2796 /* Create array where we count the collisions in. We must use bfd_malloc
2797 since the size could be large. */
2798 amt = maxsize;
2799 amt *= sizeof (unsigned long int);
2800 counts = (unsigned long int *) bfd_malloc (amt);
2801 if (counts == NULL)
2803 free (hashcodes);
2804 return 0;
2807 /* Compute the "optimal" size for the hash table. The criteria is a
2808 minimal chain length. The minor criteria is (of course) the size
2809 of the table. */
2810 for (i = minsize; i < maxsize; ++i)
2812 /* Walk through the array of hashcodes and count the collisions. */
2813 BFD_HOST_U_64_BIT max;
2814 unsigned long int j;
2815 unsigned long int fact;
2817 memset (counts, '\0', i * sizeof (unsigned long int));
2819 /* Determine how often each hash bucket is used. */
2820 for (j = 0; j < nsyms; ++j)
2821 ++counts[hashcodes[j] % i];
2823 /* For the weight function we need some information about the
2824 pagesize on the target. This is information need not be 100%
2825 accurate. Since this information is not available (so far) we
2826 define it here to a reasonable default value. If it is crucial
2827 to have a better value some day simply define this value. */
2828 # ifndef BFD_TARGET_PAGESIZE
2829 # define BFD_TARGET_PAGESIZE (4096)
2830 # endif
2832 /* We in any case need 2 + NSYMS entries for the size values and
2833 the chains. */
2834 max = (2 + nsyms) * (ARCH_SIZE / 8);
2836 # if 1
2837 /* Variant 1: optimize for short chains. We add the squares
2838 of all the chain lengths (which favous many small chain
2839 over a few long chains). */
2840 for (j = 0; j < i; ++j)
2841 max += counts[j] * counts[j];
2843 /* This adds penalties for the overall size of the table. */
2844 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2845 max *= fact * fact;
2846 # else
2847 /* Variant 2: Optimize a lot more for small table. Here we
2848 also add squares of the size but we also add penalties for
2849 empty slots (the +1 term). */
2850 for (j = 0; j < i; ++j)
2851 max += (1 + counts[j]) * (1 + counts[j]);
2853 /* The overall size of the table is considered, but not as
2854 strong as in variant 1, where it is squared. */
2855 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2856 max *= fact;
2857 # endif
2859 /* Compare with current best results. */
2860 if (max < best_chlen)
2862 best_chlen = max;
2863 best_size = i;
2867 free (counts);
2869 else
2870 #endif /* defined (BFD_HOST_U_64_BIT) */
2872 /* This is the fallback solution if no 64bit type is available or if we
2873 are not supposed to spend much time on optimizations. We select the
2874 bucket count using a fixed set of numbers. */
2875 for (i = 0; elf_buckets[i] != 0; i++)
2877 best_size = elf_buckets[i];
2878 if (dynsymcount < elf_buckets[i + 1])
2879 break;
2883 /* Free the arrays we needed. */
2884 free (hashcodes);
2886 return best_size;
2889 /* Set up the sizes and contents of the ELF dynamic sections. This is
2890 called by the ELF linker emulation before_allocation routine. We
2891 must set the sizes of the sections before the linker sets the
2892 addresses of the various sections. */
2894 boolean
2895 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2896 filter_shlib,
2897 auxiliary_filters, info, sinterpptr,
2898 verdefs)
2899 bfd *output_bfd;
2900 const char *soname;
2901 const char *rpath;
2902 const char *filter_shlib;
2903 const char * const *auxiliary_filters;
2904 struct bfd_link_info *info;
2905 asection **sinterpptr;
2906 struct bfd_elf_version_tree *verdefs;
2908 bfd_size_type soname_indx;
2909 bfd *dynobj;
2910 struct elf_backend_data *bed;
2911 struct elf_assign_sym_version_info asvinfo;
2913 *sinterpptr = NULL;
2915 soname_indx = (bfd_size_type) -1;
2917 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2918 return true;
2920 if (! is_elf_hash_table (info))
2921 return false;
2923 /* The backend may have to create some sections regardless of whether
2924 we're dynamic or not. */
2925 bed = get_elf_backend_data (output_bfd);
2926 if (bed->elf_backend_always_size_sections
2927 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2928 return false;
2930 dynobj = elf_hash_table (info)->dynobj;
2932 /* If there were no dynamic objects in the link, there is nothing to
2933 do here. */
2934 if (dynobj == NULL)
2935 return true;
2937 if (elf_hash_table (info)->dynamic_sections_created)
2939 struct elf_info_failed eif;
2940 struct elf_link_hash_entry *h;
2941 asection *dynstr;
2943 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2944 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2946 if (soname != NULL)
2948 soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2949 soname, true, true);
2950 if (soname_indx == (bfd_size_type) -1
2951 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
2952 soname_indx))
2953 return false;
2956 if (info->symbolic)
2958 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
2959 (bfd_vma) 0))
2960 return false;
2961 info->flags |= DF_SYMBOLIC;
2964 if (rpath != NULL)
2966 bfd_size_type indx;
2968 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
2969 true, true);
2970 if (indx == (bfd_size_type) -1
2971 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
2972 || (info->new_dtags
2973 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
2974 indx)))
2975 return false;
2978 if (filter_shlib != NULL)
2980 bfd_size_type indx;
2982 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2983 filter_shlib, true, true);
2984 if (indx == (bfd_size_type) -1
2985 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
2986 return false;
2989 if (auxiliary_filters != NULL)
2991 const char * const *p;
2993 for (p = auxiliary_filters; *p != NULL; p++)
2995 bfd_size_type indx;
2997 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2998 *p, true, true);
2999 if (indx == (bfd_size_type) -1
3000 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3001 indx))
3002 return false;
3006 eif.info = info;
3007 eif.verdefs = verdefs;
3008 eif.failed = false;
3010 /* If we are supposed to export all symbols into the dynamic symbol
3011 table (this is not the normal case), then do so. */
3012 if (info->export_dynamic)
3014 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3015 (PTR) &eif);
3016 if (eif.failed)
3017 return false;
3020 /* Attach all the symbols to their version information. */
3021 asvinfo.output_bfd = output_bfd;
3022 asvinfo.info = info;
3023 asvinfo.verdefs = verdefs;
3024 asvinfo.failed = false;
3026 elf_link_hash_traverse (elf_hash_table (info),
3027 elf_link_assign_sym_version,
3028 (PTR) &asvinfo);
3029 if (asvinfo.failed)
3030 return false;
3032 /* Find all symbols which were defined in a dynamic object and make
3033 the backend pick a reasonable value for them. */
3034 elf_link_hash_traverse (elf_hash_table (info),
3035 elf_adjust_dynamic_symbol,
3036 (PTR) &eif);
3037 if (eif.failed)
3038 return false;
3040 /* Add some entries to the .dynamic section. We fill in some of the
3041 values later, in elf_bfd_final_link, but we must add the entries
3042 now so that we know the final size of the .dynamic section. */
3044 /* If there are initialization and/or finalization functions to
3045 call then add the corresponding DT_INIT/DT_FINI entries. */
3046 h = (info->init_function
3047 ? elf_link_hash_lookup (elf_hash_table (info),
3048 info->init_function, false,
3049 false, false)
3050 : NULL);
3051 if (h != NULL
3052 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3053 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3055 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3056 return false;
3058 h = (info->fini_function
3059 ? elf_link_hash_lookup (elf_hash_table (info),
3060 info->fini_function, false,
3061 false, false)
3062 : NULL);
3063 if (h != NULL
3064 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3065 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3067 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3068 return false;
3071 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3072 /* If .dynstr is excluded from the link, we don't want any of
3073 these tags. Strictly, we should be checking each section
3074 individually; This quick check covers for the case where
3075 someone does a /DISCARD/ : { *(*) }. */
3076 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3078 bfd_size_type strsize;
3080 strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
3081 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3082 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3083 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3084 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3085 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3086 (bfd_vma) sizeof (Elf_External_Sym)))
3087 return false;
3091 /* The backend must work out the sizes of all the other dynamic
3092 sections. */
3093 if (bed->elf_backend_size_dynamic_sections
3094 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3095 return false;
3097 if (elf_hash_table (info)->dynamic_sections_created)
3099 bfd_size_type dynsymcount;
3100 asection *s;
3101 size_t bucketcount = 0;
3102 size_t hash_entry_size;
3103 unsigned int dtagcount;
3105 /* Set up the version definition section. */
3106 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3107 BFD_ASSERT (s != NULL);
3109 /* We may have created additional version definitions if we are
3110 just linking a regular application. */
3111 verdefs = asvinfo.verdefs;
3113 if (verdefs == NULL)
3114 _bfd_strip_section_from_output (info, s);
3115 else
3117 unsigned int cdefs;
3118 bfd_size_type size;
3119 struct bfd_elf_version_tree *t;
3120 bfd_byte *p;
3121 Elf_Internal_Verdef def;
3122 Elf_Internal_Verdaux defaux;
3124 cdefs = 0;
3125 size = 0;
3127 /* Make space for the base version. */
3128 size += sizeof (Elf_External_Verdef);
3129 size += sizeof (Elf_External_Verdaux);
3130 ++cdefs;
3132 for (t = verdefs; t != NULL; t = t->next)
3134 struct bfd_elf_version_deps *n;
3136 size += sizeof (Elf_External_Verdef);
3137 size += sizeof (Elf_External_Verdaux);
3138 ++cdefs;
3140 for (n = t->deps; n != NULL; n = n->next)
3141 size += sizeof (Elf_External_Verdaux);
3144 s->_raw_size = size;
3145 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3146 if (s->contents == NULL && s->_raw_size != 0)
3147 return false;
3149 /* Fill in the version definition section. */
3151 p = s->contents;
3153 def.vd_version = VER_DEF_CURRENT;
3154 def.vd_flags = VER_FLG_BASE;
3155 def.vd_ndx = 1;
3156 def.vd_cnt = 1;
3157 def.vd_aux = sizeof (Elf_External_Verdef);
3158 def.vd_next = (sizeof (Elf_External_Verdef)
3159 + sizeof (Elf_External_Verdaux));
3161 if (soname_indx != (bfd_size_type) -1)
3163 def.vd_hash = bfd_elf_hash (soname);
3164 defaux.vda_name = soname_indx;
3166 else
3168 const char *name;
3169 bfd_size_type indx;
3171 name = basename (output_bfd->filename);
3172 def.vd_hash = bfd_elf_hash (name);
3173 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3174 name, true, false);
3175 if (indx == (bfd_size_type) -1)
3176 return false;
3177 defaux.vda_name = indx;
3179 defaux.vda_next = 0;
3181 _bfd_elf_swap_verdef_out (output_bfd, &def,
3182 (Elf_External_Verdef *) p);
3183 p += sizeof (Elf_External_Verdef);
3184 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3185 (Elf_External_Verdaux *) p);
3186 p += sizeof (Elf_External_Verdaux);
3188 for (t = verdefs; t != NULL; t = t->next)
3190 unsigned int cdeps;
3191 struct bfd_elf_version_deps *n;
3192 struct elf_link_hash_entry *h;
3194 cdeps = 0;
3195 for (n = t->deps; n != NULL; n = n->next)
3196 ++cdeps;
3198 /* Add a symbol representing this version. */
3199 h = NULL;
3200 if (! (_bfd_generic_link_add_one_symbol
3201 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3202 (bfd_vma) 0, (const char *) NULL, false,
3203 get_elf_backend_data (dynobj)->collect,
3204 (struct bfd_link_hash_entry **) &h)))
3205 return false;
3206 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3207 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3208 h->type = STT_OBJECT;
3209 h->verinfo.vertree = t;
3211 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3212 return false;
3214 def.vd_version = VER_DEF_CURRENT;
3215 def.vd_flags = 0;
3216 if (t->globals == NULL && t->locals == NULL && ! t->used)
3217 def.vd_flags |= VER_FLG_WEAK;
3218 def.vd_ndx = t->vernum + 1;
3219 def.vd_cnt = cdeps + 1;
3220 def.vd_hash = bfd_elf_hash (t->name);
3221 def.vd_aux = sizeof (Elf_External_Verdef);
3222 if (t->next != NULL)
3223 def.vd_next = (sizeof (Elf_External_Verdef)
3224 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3225 else
3226 def.vd_next = 0;
3228 _bfd_elf_swap_verdef_out (output_bfd, &def,
3229 (Elf_External_Verdef *) p);
3230 p += sizeof (Elf_External_Verdef);
3232 defaux.vda_name = h->dynstr_index;
3233 if (t->deps == NULL)
3234 defaux.vda_next = 0;
3235 else
3236 defaux.vda_next = sizeof (Elf_External_Verdaux);
3237 t->name_indx = defaux.vda_name;
3239 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3240 (Elf_External_Verdaux *) p);
3241 p += sizeof (Elf_External_Verdaux);
3243 for (n = t->deps; n != NULL; n = n->next)
3245 if (n->version_needed == NULL)
3247 /* This can happen if there was an error in the
3248 version script. */
3249 defaux.vda_name = 0;
3251 else
3252 defaux.vda_name = n->version_needed->name_indx;
3253 if (n->next == NULL)
3254 defaux.vda_next = 0;
3255 else
3256 defaux.vda_next = sizeof (Elf_External_Verdaux);
3258 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3259 (Elf_External_Verdaux *) p);
3260 p += sizeof (Elf_External_Verdaux);
3264 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3265 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3266 (bfd_vma) cdefs))
3267 return false;
3269 elf_tdata (output_bfd)->cverdefs = cdefs;
3272 if (info->new_dtags && info->flags)
3274 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3275 return false;
3278 if (info->flags_1)
3280 if (! info->shared)
3281 info->flags_1 &= ~ (DF_1_INITFIRST
3282 | DF_1_NODELETE
3283 | DF_1_NOOPEN);
3284 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3285 info->flags_1))
3286 return false;
3289 /* Work out the size of the version reference section. */
3291 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3292 BFD_ASSERT (s != NULL);
3294 struct elf_find_verdep_info sinfo;
3296 sinfo.output_bfd = output_bfd;
3297 sinfo.info = info;
3298 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3299 if (sinfo.vers == 0)
3300 sinfo.vers = 1;
3301 sinfo.failed = false;
3303 elf_link_hash_traverse (elf_hash_table (info),
3304 elf_link_find_version_dependencies,
3305 (PTR) &sinfo);
3307 if (elf_tdata (output_bfd)->verref == NULL)
3308 _bfd_strip_section_from_output (info, s);
3309 else
3311 Elf_Internal_Verneed *t;
3312 unsigned int size;
3313 unsigned int crefs;
3314 bfd_byte *p;
3316 /* Build the version definition section. */
3317 size = 0;
3318 crefs = 0;
3319 for (t = elf_tdata (output_bfd)->verref;
3320 t != NULL;
3321 t = t->vn_nextref)
3323 Elf_Internal_Vernaux *a;
3325 size += sizeof (Elf_External_Verneed);
3326 ++crefs;
3327 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3328 size += sizeof (Elf_External_Vernaux);
3331 s->_raw_size = size;
3332 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3333 if (s->contents == NULL)
3334 return false;
3336 p = s->contents;
3337 for (t = elf_tdata (output_bfd)->verref;
3338 t != NULL;
3339 t = t->vn_nextref)
3341 unsigned int caux;
3342 Elf_Internal_Vernaux *a;
3343 bfd_size_type indx;
3345 caux = 0;
3346 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3347 ++caux;
3349 t->vn_version = VER_NEED_CURRENT;
3350 t->vn_cnt = caux;
3351 if (elf_dt_name (t->vn_bfd) != NULL)
3352 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3353 elf_dt_name (t->vn_bfd),
3354 true, false);
3355 else
3356 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3357 basename (t->vn_bfd->filename),
3358 true, false);
3359 if (indx == (bfd_size_type) -1)
3360 return false;
3361 t->vn_file = indx;
3362 t->vn_aux = sizeof (Elf_External_Verneed);
3363 if (t->vn_nextref == NULL)
3364 t->vn_next = 0;
3365 else
3366 t->vn_next = (sizeof (Elf_External_Verneed)
3367 + caux * sizeof (Elf_External_Vernaux));
3369 _bfd_elf_swap_verneed_out (output_bfd, t,
3370 (Elf_External_Verneed *) p);
3371 p += sizeof (Elf_External_Verneed);
3373 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3375 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3376 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3377 a->vna_nodename, true, false);
3378 if (indx == (bfd_size_type) -1)
3379 return false;
3380 a->vna_name = indx;
3381 if (a->vna_nextptr == NULL)
3382 a->vna_next = 0;
3383 else
3384 a->vna_next = sizeof (Elf_External_Vernaux);
3386 _bfd_elf_swap_vernaux_out (output_bfd, a,
3387 (Elf_External_Vernaux *) p);
3388 p += sizeof (Elf_External_Vernaux);
3392 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3393 (bfd_vma) 0)
3394 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3395 (bfd_vma) crefs))
3396 return false;
3398 elf_tdata (output_bfd)->cverrefs = crefs;
3402 /* Assign dynsym indicies. In a shared library we generate a
3403 section symbol for each output section, which come first.
3404 Next come all of the back-end allocated local dynamic syms,
3405 followed by the rest of the global symbols. */
3407 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3409 /* Work out the size of the symbol version section. */
3410 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3411 BFD_ASSERT (s != NULL);
3412 if (dynsymcount == 0
3413 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3415 _bfd_strip_section_from_output (info, s);
3416 /* The DYNSYMCOUNT might have changed if we were going to
3417 output a dynamic symbol table entry for S. */
3418 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3420 else
3422 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3423 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3424 if (s->contents == NULL)
3425 return false;
3427 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3428 return false;
3431 /* Set the size of the .dynsym and .hash sections. We counted
3432 the number of dynamic symbols in elf_link_add_object_symbols.
3433 We will build the contents of .dynsym and .hash when we build
3434 the final symbol table, because until then we do not know the
3435 correct value to give the symbols. We built the .dynstr
3436 section as we went along in elf_link_add_object_symbols. */
3437 s = bfd_get_section_by_name (dynobj, ".dynsym");
3438 BFD_ASSERT (s != NULL);
3439 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3440 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3441 if (s->contents == NULL && s->_raw_size != 0)
3442 return false;
3444 if (dynsymcount != 0)
3446 Elf_Internal_Sym isym;
3448 /* The first entry in .dynsym is a dummy symbol. */
3449 isym.st_value = 0;
3450 isym.st_size = 0;
3451 isym.st_name = 0;
3452 isym.st_info = 0;
3453 isym.st_other = 0;
3454 isym.st_shndx = 0;
3455 elf_swap_symbol_out (output_bfd, &isym,
3456 (PTR) (Elf_External_Sym *) s->contents);
3459 /* Compute the size of the hashing table. As a side effect this
3460 computes the hash values for all the names we export. */
3461 bucketcount = compute_bucket_count (info);
3463 s = bfd_get_section_by_name (dynobj, ".hash");
3464 BFD_ASSERT (s != NULL);
3465 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3466 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3467 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3468 if (s->contents == NULL)
3469 return false;
3470 memset (s->contents, 0, (size_t) s->_raw_size);
3472 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3473 s->contents);
3474 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3475 s->contents + hash_entry_size);
3477 elf_hash_table (info)->bucketcount = bucketcount;
3479 s = bfd_get_section_by_name (dynobj, ".dynstr");
3480 BFD_ASSERT (s != NULL);
3481 s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
3483 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3484 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3485 return false;
3488 return true;
3491 /* Fix up the flags for a symbol. This handles various cases which
3492 can only be fixed after all the input files are seen. This is
3493 currently called by both adjust_dynamic_symbol and
3494 assign_sym_version, which is unnecessary but perhaps more robust in
3495 the face of future changes. */
3497 static boolean
3498 elf_fix_symbol_flags (h, eif)
3499 struct elf_link_hash_entry *h;
3500 struct elf_info_failed *eif;
3502 /* If this symbol was mentioned in a non-ELF file, try to set
3503 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3504 permit a non-ELF file to correctly refer to a symbol defined in
3505 an ELF dynamic object. */
3506 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3508 while (h->root.type == bfd_link_hash_indirect)
3509 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3511 if (h->root.type != bfd_link_hash_defined
3512 && h->root.type != bfd_link_hash_defweak)
3513 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3514 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3515 else
3517 if (h->root.u.def.section->owner != NULL
3518 && (bfd_get_flavour (h->root.u.def.section->owner)
3519 == bfd_target_elf_flavour))
3520 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3521 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3522 else
3523 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3526 if (h->dynindx == -1
3527 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3528 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3530 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3532 eif->failed = true;
3533 return false;
3537 else
3539 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3540 was first seen in a non-ELF file. Fortunately, if the symbol
3541 was first seen in an ELF file, we're probably OK unless the
3542 symbol was defined in a non-ELF file. Catch that case here.
3543 FIXME: We're still in trouble if the symbol was first seen in
3544 a dynamic object, and then later in a non-ELF regular object. */
3545 if ((h->root.type == bfd_link_hash_defined
3546 || h->root.type == bfd_link_hash_defweak)
3547 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3548 && (h->root.u.def.section->owner != NULL
3549 ? (bfd_get_flavour (h->root.u.def.section->owner)
3550 != bfd_target_elf_flavour)
3551 : (bfd_is_abs_section (h->root.u.def.section)
3552 && (h->elf_link_hash_flags
3553 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3554 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3557 /* If this is a final link, and the symbol was defined as a common
3558 symbol in a regular object file, and there was no definition in
3559 any dynamic object, then the linker will have allocated space for
3560 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3561 flag will not have been set. */
3562 if (h->root.type == bfd_link_hash_defined
3563 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3564 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3565 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3566 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3567 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3569 /* If -Bsymbolic was used (which means to bind references to global
3570 symbols to the definition within the shared object), and this
3571 symbol was defined in a regular object, then it actually doesn't
3572 need a PLT entry, and we can accomplish that by forcing it local.
3573 Likewise, if the symbol has hidden or internal visibility.
3574 FIXME: It might be that we also do not need a PLT for other
3575 non-hidden visibilities, but we would have to tell that to the
3576 backend specifically; we can't just clear PLT-related data here. */
3577 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3578 && eif->info->shared
3579 && is_elf_hash_table (eif->info)
3580 && (eif->info->symbolic
3581 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3582 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3583 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3585 struct elf_backend_data *bed;
3587 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3588 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3589 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3590 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3591 (*bed->elf_backend_hide_symbol) (eif->info, h);
3594 /* If this is a weak defined symbol in a dynamic object, and we know
3595 the real definition in the dynamic object, copy interesting flags
3596 over to the real definition. */
3597 if (h->weakdef != NULL)
3599 struct elf_link_hash_entry *weakdef;
3601 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3602 || h->root.type == bfd_link_hash_defweak);
3603 weakdef = h->weakdef;
3604 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3605 || weakdef->root.type == bfd_link_hash_defweak);
3606 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3608 /* If the real definition is defined by a regular object file,
3609 don't do anything special. See the longer description in
3610 elf_adjust_dynamic_symbol, below. */
3611 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3612 h->weakdef = NULL;
3613 else
3614 weakdef->elf_link_hash_flags |=
3615 (h->elf_link_hash_flags
3616 & (ELF_LINK_HASH_REF_REGULAR
3617 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3618 | ELF_LINK_NON_GOT_REF));
3621 return true;
3624 /* Make the backend pick a good value for a dynamic symbol. This is
3625 called via elf_link_hash_traverse, and also calls itself
3626 recursively. */
3628 static boolean
3629 elf_adjust_dynamic_symbol (h, data)
3630 struct elf_link_hash_entry *h;
3631 PTR data;
3633 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3634 bfd *dynobj;
3635 struct elf_backend_data *bed;
3637 /* Ignore indirect symbols. These are added by the versioning code. */
3638 if (h->root.type == bfd_link_hash_indirect)
3639 return true;
3641 if (! is_elf_hash_table (eif->info))
3642 return false;
3644 /* Fix the symbol flags. */
3645 if (! elf_fix_symbol_flags (h, eif))
3646 return false;
3648 /* If this symbol does not require a PLT entry, and it is not
3649 defined by a dynamic object, or is not referenced by a regular
3650 object, ignore it. We do have to handle a weak defined symbol,
3651 even if no regular object refers to it, if we decided to add it
3652 to the dynamic symbol table. FIXME: Do we normally need to worry
3653 about symbols which are defined by one dynamic object and
3654 referenced by another one? */
3655 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3656 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3657 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3658 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3659 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3661 h->plt.offset = (bfd_vma) -1;
3662 return true;
3665 /* If we've already adjusted this symbol, don't do it again. This
3666 can happen via a recursive call. */
3667 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3668 return true;
3670 /* Don't look at this symbol again. Note that we must set this
3671 after checking the above conditions, because we may look at a
3672 symbol once, decide not to do anything, and then get called
3673 recursively later after REF_REGULAR is set below. */
3674 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3676 /* If this is a weak definition, and we know a real definition, and
3677 the real symbol is not itself defined by a regular object file,
3678 then get a good value for the real definition. We handle the
3679 real symbol first, for the convenience of the backend routine.
3681 Note that there is a confusing case here. If the real definition
3682 is defined by a regular object file, we don't get the real symbol
3683 from the dynamic object, but we do get the weak symbol. If the
3684 processor backend uses a COPY reloc, then if some routine in the
3685 dynamic object changes the real symbol, we will not see that
3686 change in the corresponding weak symbol. This is the way other
3687 ELF linkers work as well, and seems to be a result of the shared
3688 library model.
3690 I will clarify this issue. Most SVR4 shared libraries define the
3691 variable _timezone and define timezone as a weak synonym. The
3692 tzset call changes _timezone. If you write
3693 extern int timezone;
3694 int _timezone = 5;
3695 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3696 you might expect that, since timezone is a synonym for _timezone,
3697 the same number will print both times. However, if the processor
3698 backend uses a COPY reloc, then actually timezone will be copied
3699 into your process image, and, since you define _timezone
3700 yourself, _timezone will not. Thus timezone and _timezone will
3701 wind up at different memory locations. The tzset call will set
3702 _timezone, leaving timezone unchanged. */
3704 if (h->weakdef != NULL)
3706 /* If we get to this point, we know there is an implicit
3707 reference by a regular object file via the weak symbol H.
3708 FIXME: Is this really true? What if the traversal finds
3709 H->WEAKDEF before it finds H? */
3710 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3712 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
3713 return false;
3716 /* If a symbol has no type and no size and does not require a PLT
3717 entry, then we are probably about to do the wrong thing here: we
3718 are probably going to create a COPY reloc for an empty object.
3719 This case can arise when a shared object is built with assembly
3720 code, and the assembly code fails to set the symbol type. */
3721 if (h->size == 0
3722 && h->type == STT_NOTYPE
3723 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3724 (*_bfd_error_handler)
3725 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3726 h->root.root.string);
3728 dynobj = elf_hash_table (eif->info)->dynobj;
3729 bed = get_elf_backend_data (dynobj);
3730 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3732 eif->failed = true;
3733 return false;
3736 return true;
3739 /* This routine is used to export all defined symbols into the dynamic
3740 symbol table. It is called via elf_link_hash_traverse. */
3742 static boolean
3743 elf_export_symbol (h, data)
3744 struct elf_link_hash_entry *h;
3745 PTR data;
3747 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3749 /* Ignore indirect symbols. These are added by the versioning code. */
3750 if (h->root.type == bfd_link_hash_indirect)
3751 return true;
3753 if (h->dynindx == -1
3754 && (h->elf_link_hash_flags
3755 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
3757 struct bfd_elf_version_tree *t;
3758 struct bfd_elf_version_expr *d;
3760 for (t = eif->verdefs; t != NULL; t = t->next)
3762 if (t->globals != NULL)
3764 for (d = t->globals; d != NULL; d = d->next)
3766 if ((*d->match) (d, h->root.root.string))
3767 goto doit;
3771 if (t->locals != NULL)
3773 for (d = t->locals ; d != NULL; d = d->next)
3775 if ((*d->match) (d, h->root.root.string))
3776 return true;
3781 if (!eif->verdefs)
3783 doit:
3784 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3786 eif->failed = true;
3787 return false;
3792 return true;
3795 /* Look through the symbols which are defined in other shared
3796 libraries and referenced here. Update the list of version
3797 dependencies. This will be put into the .gnu.version_r section.
3798 This function is called via elf_link_hash_traverse. */
3800 static boolean
3801 elf_link_find_version_dependencies (h, data)
3802 struct elf_link_hash_entry *h;
3803 PTR data;
3805 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
3806 Elf_Internal_Verneed *t;
3807 Elf_Internal_Vernaux *a;
3808 bfd_size_type amt;
3810 /* We only care about symbols defined in shared objects with version
3811 information. */
3812 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3813 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3814 || h->dynindx == -1
3815 || h->verinfo.verdef == NULL)
3816 return true;
3818 /* See if we already know about this version. */
3819 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
3821 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
3822 continue;
3824 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3825 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
3826 return true;
3828 break;
3831 /* This is a new version. Add it to tree we are building. */
3833 if (t == NULL)
3835 amt = sizeof *t;
3836 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
3837 if (t == NULL)
3839 rinfo->failed = true;
3840 return false;
3843 t->vn_bfd = h->verinfo.verdef->vd_bfd;
3844 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
3845 elf_tdata (rinfo->output_bfd)->verref = t;
3848 amt = sizeof *a;
3849 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
3851 /* Note that we are copying a string pointer here, and testing it
3852 above. If bfd_elf_string_from_elf_section is ever changed to
3853 discard the string data when low in memory, this will have to be
3854 fixed. */
3855 a->vna_nodename = h->verinfo.verdef->vd_nodename;
3857 a->vna_flags = h->verinfo.verdef->vd_flags;
3858 a->vna_nextptr = t->vn_auxptr;
3860 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
3861 ++rinfo->vers;
3863 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
3865 t->vn_auxptr = a;
3867 return true;
3870 /* Figure out appropriate versions for all the symbols. We may not
3871 have the version number script until we have read all of the input
3872 files, so until that point we don't know which symbols should be
3873 local. This function is called via elf_link_hash_traverse. */
3875 static boolean
3876 elf_link_assign_sym_version (h, data)
3877 struct elf_link_hash_entry *h;
3878 PTR data;
3880 struct elf_assign_sym_version_info *sinfo;
3881 struct bfd_link_info *info;
3882 struct elf_backend_data *bed;
3883 struct elf_info_failed eif;
3884 char *p;
3885 bfd_size_type amt;
3887 sinfo = (struct elf_assign_sym_version_info *) data;
3888 info = sinfo->info;
3890 /* Fix the symbol flags. */
3891 eif.failed = false;
3892 eif.info = info;
3893 if (! elf_fix_symbol_flags (h, &eif))
3895 if (eif.failed)
3896 sinfo->failed = true;
3897 return false;
3900 /* We only need version numbers for symbols defined in regular
3901 objects. */
3902 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3903 return true;
3905 bed = get_elf_backend_data (sinfo->output_bfd);
3906 p = strchr (h->root.root.string, ELF_VER_CHR);
3907 if (p != NULL && h->verinfo.vertree == NULL)
3909 struct bfd_elf_version_tree *t;
3910 boolean hidden;
3912 hidden = true;
3914 /* There are two consecutive ELF_VER_CHR characters if this is
3915 not a hidden symbol. */
3916 ++p;
3917 if (*p == ELF_VER_CHR)
3919 hidden = false;
3920 ++p;
3923 /* If there is no version string, we can just return out. */
3924 if (*p == '\0')
3926 if (hidden)
3927 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3928 return true;
3931 /* Look for the version. If we find it, it is no longer weak. */
3932 for (t = sinfo->verdefs; t != NULL; t = t->next)
3934 if (strcmp (t->name, p) == 0)
3936 size_t len;
3937 char *alc;
3938 struct bfd_elf_version_expr *d;
3940 len = p - h->root.root.string;
3941 alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
3942 if (alc == NULL)
3943 return false;
3944 strncpy (alc, h->root.root.string, len - 1);
3945 alc[len - 1] = '\0';
3946 if (alc[len - 2] == ELF_VER_CHR)
3947 alc[len - 2] = '\0';
3949 h->verinfo.vertree = t;
3950 t->used = true;
3951 d = NULL;
3953 if (t->globals != NULL)
3955 for (d = t->globals; d != NULL; d = d->next)
3956 if ((*d->match) (d, alc))
3957 break;
3960 /* See if there is anything to force this symbol to
3961 local scope. */
3962 if (d == NULL && t->locals != NULL)
3964 for (d = t->locals; d != NULL; d = d->next)
3966 if ((*d->match) (d, alc))
3968 if (h->dynindx != -1
3969 && info->shared
3970 && ! info->export_dynamic)
3972 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3973 (*bed->elf_backend_hide_symbol) (info, h);
3974 /* FIXME: The name of the symbol has
3975 already been recorded in the dynamic
3976 string table section. */
3979 break;
3984 bfd_release (sinfo->output_bfd, alc);
3985 break;
3989 /* If we are building an application, we need to create a
3990 version node for this version. */
3991 if (t == NULL && ! info->shared)
3993 struct bfd_elf_version_tree **pp;
3994 int version_index;
3996 /* If we aren't going to export this symbol, we don't need
3997 to worry about it. */
3998 if (h->dynindx == -1)
3999 return true;
4001 amt = sizeof *t;
4002 t = ((struct bfd_elf_version_tree *)
4003 bfd_alloc (sinfo->output_bfd, amt));
4004 if (t == NULL)
4006 sinfo->failed = true;
4007 return false;
4010 t->next = NULL;
4011 t->name = p;
4012 t->globals = NULL;
4013 t->locals = NULL;
4014 t->deps = NULL;
4015 t->name_indx = (unsigned int) -1;
4016 t->used = true;
4018 version_index = 1;
4019 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4020 ++version_index;
4021 t->vernum = version_index;
4023 *pp = t;
4025 h->verinfo.vertree = t;
4027 else if (t == NULL)
4029 /* We could not find the version for a symbol when
4030 generating a shared archive. Return an error. */
4031 (*_bfd_error_handler)
4032 (_("%s: undefined versioned symbol name %s"),
4033 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4034 bfd_set_error (bfd_error_bad_value);
4035 sinfo->failed = true;
4036 return false;
4039 if (hidden)
4040 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4043 /* If we don't have a version for this symbol, see if we can find
4044 something. */
4045 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4047 struct bfd_elf_version_tree *t;
4048 struct bfd_elf_version_tree *deflt;
4049 struct bfd_elf_version_expr *d;
4051 /* See if can find what version this symbol is in. If the
4052 symbol is supposed to be local, then don't actually register
4053 it. */
4054 deflt = NULL;
4055 for (t = sinfo->verdefs; t != NULL; t = t->next)
4057 if (t->globals != NULL)
4059 for (d = t->globals; d != NULL; d = d->next)
4061 if ((*d->match) (d, h->root.root.string))
4063 h->verinfo.vertree = t;
4064 break;
4068 if (d != NULL)
4069 break;
4072 if (t->locals != NULL)
4074 for (d = t->locals; d != NULL; d = d->next)
4076 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4077 deflt = t;
4078 else if ((*d->match) (d, h->root.root.string))
4080 h->verinfo.vertree = t;
4081 if (h->dynindx != -1
4082 && info->shared
4083 && ! info->export_dynamic)
4085 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4086 (*bed->elf_backend_hide_symbol) (info, h);
4087 /* FIXME: The name of the symbol has already
4088 been recorded in the dynamic string table
4089 section. */
4091 break;
4095 if (d != NULL)
4096 break;
4100 if (deflt != NULL && h->verinfo.vertree == NULL)
4102 h->verinfo.vertree = deflt;
4103 if (h->dynindx != -1
4104 && info->shared
4105 && ! info->export_dynamic)
4107 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4108 (*bed->elf_backend_hide_symbol) (info, h);
4109 /* FIXME: The name of the symbol has already been
4110 recorded in the dynamic string table section. */
4115 return true;
4118 /* Final phase of ELF linker. */
4120 /* A structure we use to avoid passing large numbers of arguments. */
4122 struct elf_final_link_info
4124 /* General link information. */
4125 struct bfd_link_info *info;
4126 /* Output BFD. */
4127 bfd *output_bfd;
4128 /* Symbol string table. */
4129 struct bfd_strtab_hash *symstrtab;
4130 /* .dynsym section. */
4131 asection *dynsym_sec;
4132 /* .hash section. */
4133 asection *hash_sec;
4134 /* symbol version section (.gnu.version). */
4135 asection *symver_sec;
4136 /* Buffer large enough to hold contents of any section. */
4137 bfd_byte *contents;
4138 /* Buffer large enough to hold external relocs of any section. */
4139 PTR external_relocs;
4140 /* Buffer large enough to hold internal relocs of any section. */
4141 Elf_Internal_Rela *internal_relocs;
4142 /* Buffer large enough to hold external local symbols of any input
4143 BFD. */
4144 Elf_External_Sym *external_syms;
4145 /* Buffer large enough to hold internal local symbols of any input
4146 BFD. */
4147 Elf_Internal_Sym *internal_syms;
4148 /* Array large enough to hold a symbol index for each local symbol
4149 of any input BFD. */
4150 long *indices;
4151 /* Array large enough to hold a section pointer for each local
4152 symbol of any input BFD. */
4153 asection **sections;
4154 /* Buffer to hold swapped out symbols. */
4155 Elf_External_Sym *symbuf;
4156 /* Number of swapped out symbols in buffer. */
4157 size_t symbuf_count;
4158 /* Number of symbols which fit in symbuf. */
4159 size_t symbuf_size;
4162 static boolean elf_link_output_sym
4163 PARAMS ((struct elf_final_link_info *, const char *,
4164 Elf_Internal_Sym *, asection *));
4165 static boolean elf_link_flush_output_syms
4166 PARAMS ((struct elf_final_link_info *));
4167 static boolean elf_link_output_extsym
4168 PARAMS ((struct elf_link_hash_entry *, PTR));
4169 static boolean elf_link_sec_merge_syms
4170 PARAMS ((struct elf_link_hash_entry *, PTR));
4171 static boolean elf_link_input_bfd
4172 PARAMS ((struct elf_final_link_info *, bfd *));
4173 static boolean elf_reloc_link_order
4174 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4175 struct bfd_link_order *));
4177 /* This struct is used to pass information to elf_link_output_extsym. */
4179 struct elf_outext_info
4181 boolean failed;
4182 boolean localsyms;
4183 struct elf_final_link_info *finfo;
4186 /* Compute the size of, and allocate space for, REL_HDR which is the
4187 section header for a section containing relocations for O. */
4189 static boolean
4190 elf_link_size_reloc_section (abfd, rel_hdr, o)
4191 bfd *abfd;
4192 Elf_Internal_Shdr *rel_hdr;
4193 asection *o;
4195 bfd_size_type reloc_count;
4196 bfd_size_type num_rel_hashes;
4198 /* Figure out how many relocations there will be. */
4199 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4200 reloc_count = elf_section_data (o)->rel_count;
4201 else
4202 reloc_count = elf_section_data (o)->rel_count2;
4204 num_rel_hashes = o->reloc_count;
4205 if (num_rel_hashes < reloc_count)
4206 num_rel_hashes = reloc_count;
4208 /* That allows us to calculate the size of the section. */
4209 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4211 /* The contents field must last into write_object_contents, so we
4212 allocate it with bfd_alloc rather than malloc. Also since we
4213 cannot be sure that the contents will actually be filled in,
4214 we zero the allocated space. */
4215 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4216 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4217 return false;
4219 /* We only allocate one set of hash entries, so we only do it the
4220 first time we are called. */
4221 if (elf_section_data (o)->rel_hashes == NULL
4222 && num_rel_hashes)
4224 struct elf_link_hash_entry **p;
4226 p = ((struct elf_link_hash_entry **)
4227 bfd_zmalloc (num_rel_hashes
4228 * sizeof (struct elf_link_hash_entry *)));
4229 if (p == NULL)
4230 return false;
4232 elf_section_data (o)->rel_hashes = p;
4235 return true;
4238 /* When performing a relocateable link, the input relocations are
4239 preserved. But, if they reference global symbols, the indices
4240 referenced must be updated. Update all the relocations in
4241 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4243 static void
4244 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4245 bfd *abfd;
4246 Elf_Internal_Shdr *rel_hdr;
4247 unsigned int count;
4248 struct elf_link_hash_entry **rel_hash;
4250 unsigned int i;
4251 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4252 Elf_Internal_Rel *irel;
4253 Elf_Internal_Rela *irela;
4254 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4256 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4257 if (irel == NULL)
4259 (*_bfd_error_handler) (_("Error: out of memory"));
4260 abort ();
4263 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4264 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4265 if (irela == NULL)
4267 (*_bfd_error_handler) (_("Error: out of memory"));
4268 abort ();
4271 for (i = 0; i < count; i++, rel_hash++)
4273 if (*rel_hash == NULL)
4274 continue;
4276 BFD_ASSERT ((*rel_hash)->indx >= 0);
4278 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4280 Elf_External_Rel *erel;
4281 unsigned int j;
4283 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4284 if (bed->s->swap_reloc_in)
4285 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4286 else
4287 elf_swap_reloc_in (abfd, erel, irel);
4289 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4290 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4291 ELF_R_TYPE (irel[j].r_info));
4293 if (bed->s->swap_reloc_out)
4294 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4295 else
4296 elf_swap_reloc_out (abfd, irel, erel);
4298 else
4300 Elf_External_Rela *erela;
4301 unsigned int j;
4303 BFD_ASSERT (rel_hdr->sh_entsize
4304 == sizeof (Elf_External_Rela));
4306 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4307 if (bed->s->swap_reloca_in)
4308 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4309 else
4310 elf_swap_reloca_in (abfd, erela, irela);
4312 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4313 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4314 ELF_R_TYPE (irela[j].r_info));
4316 if (bed->s->swap_reloca_out)
4317 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4318 else
4319 elf_swap_reloca_out (abfd, irela, erela);
4323 free (irel);
4324 free (irela);
4327 struct elf_link_sort_rela {
4328 bfd_vma offset;
4329 enum elf_reloc_type_class type;
4330 union {
4331 Elf_Internal_Rel rel;
4332 Elf_Internal_Rela rela;
4333 } u;
4336 static int
4337 elf_link_sort_cmp1 (A, B)
4338 const PTR A;
4339 const PTR B;
4341 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *)A;
4342 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *)B;
4343 int relativea, relativeb;
4345 relativea = a->type == reloc_class_relative;
4346 relativeb = b->type == reloc_class_relative;
4348 if (relativea < relativeb)
4349 return 1;
4350 if (relativea > relativeb)
4351 return -1;
4352 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4353 return -1;
4354 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4355 return 1;
4356 if (a->u.rel.r_offset < b->u.rel.r_offset)
4357 return -1;
4358 if (a->u.rel.r_offset > b->u.rel.r_offset)
4359 return 1;
4360 return 0;
4363 static int
4364 elf_link_sort_cmp2 (A, B)
4365 const PTR A;
4366 const PTR B;
4368 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *)A;
4369 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *)B;
4370 int copya, copyb;
4372 if (a->offset < b->offset)
4373 return -1;
4374 if (a->offset > b->offset)
4375 return 1;
4376 copya = a->type == reloc_class_copy;
4377 copyb = b->type == reloc_class_copy;
4378 if (copya < copyb)
4379 return -1;
4380 if (copya > copyb)
4381 return 1;
4382 if (a->u.rel.r_offset < b->u.rel.r_offset)
4383 return -1;
4384 if (a->u.rel.r_offset > b->u.rel.r_offset)
4385 return 1;
4386 return 0;
4389 static size_t
4390 elf_link_sort_relocs (abfd, info, psec)
4391 bfd *abfd;
4392 struct bfd_link_info *info;
4393 asection **psec;
4395 bfd *dynobj = elf_hash_table (info)->dynobj;
4396 asection *reldyn, *o;
4397 boolean rel = false;
4398 size_t count, size, i, j, ret;
4399 struct elf_link_sort_rela *rela;
4400 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4402 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4403 if (reldyn == NULL || reldyn->_raw_size == 0)
4405 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4406 if (reldyn == NULL || reldyn->_raw_size == 0)
4407 return 0;
4408 rel = true;
4409 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4411 else
4412 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4414 size = 0;
4415 for (o = dynobj->sections; o != NULL; o = o->next)
4416 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4417 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4418 && o->output_section == reldyn)
4419 size += o->_raw_size;
4421 if (size != reldyn->_raw_size)
4422 return 0;
4424 rela = (struct elf_link_sort_rela *) calloc (sizeof (*rela), count);
4425 if (rela == NULL)
4427 (*info->callbacks->warning)
4428 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4429 (bfd_vma) 0);
4430 return 0;
4433 for (o = dynobj->sections; o != NULL; o = o->next)
4434 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4435 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4436 && o->output_section == reldyn)
4438 if (rel)
4440 Elf_External_Rel *erel, *erelend;
4441 struct elf_link_sort_rela *s;
4443 erel = (Elf_External_Rel *) o->contents;
4444 erelend = (Elf_External_Rel *) ((PTR) o->contents + o->_raw_size);
4445 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4446 for (; erel < erelend; erel++, s++)
4448 if (bed->s->swap_reloc_in)
4449 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4450 else
4451 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4453 s->type = ((*bed->elf_backend_reloc_type_class)
4454 ((int) ELF_R_TYPE (s->u.rel.r_info)));
4457 else
4459 Elf_External_Rela *erela, *erelaend;
4460 struct elf_link_sort_rela *s;
4462 erela = (Elf_External_Rela *) o->contents;
4463 erelaend = (Elf_External_Rela *) ((PTR) o->contents + o->_raw_size);
4464 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4465 for (; erela < erelaend; erela++, s++)
4467 if (bed->s->swap_reloca_in)
4468 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4469 &s->u.rela);
4470 else
4471 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4473 s->type = ((*bed->elf_backend_reloc_type_class)
4474 ((int) ELF_R_TYPE (s->u.rel.r_info)));
4479 qsort (rela, count, sizeof (*rela), elf_link_sort_cmp1);
4480 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4482 for (i = ret, j = ret; i < count; i++)
4484 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4485 j = i;
4486 rela[i].offset = rela[j].u.rel.r_offset;
4488 qsort (rela + ret, count - ret, sizeof (*rela), elf_link_sort_cmp2);
4490 for (o = dynobj->sections; o != NULL; o = o->next)
4491 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4492 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4493 && o->output_section == reldyn)
4495 if (rel)
4497 Elf_External_Rel *erel, *erelend;
4498 struct elf_link_sort_rela *s;
4500 erel = (Elf_External_Rel *) o->contents;
4501 erelend = (Elf_External_Rel *) ((PTR) o->contents + o->_raw_size);
4502 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4503 for (; erel < erelend; erel++, s++)
4505 if (bed->s->swap_reloc_out)
4506 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4507 (bfd_byte *) erel);
4508 else
4509 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4512 else
4514 Elf_External_Rela *erela, *erelaend;
4515 struct elf_link_sort_rela *s;
4517 erela = (Elf_External_Rela *) o->contents;
4518 erelaend = (Elf_External_Rela *) ((PTR) o->contents + o->_raw_size);
4519 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4520 for (; erela < erelaend; erela++, s++)
4522 if (bed->s->swap_reloca_out)
4523 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4524 (bfd_byte *) erela);
4525 else
4526 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4531 free (rela);
4532 *psec = reldyn;
4533 return ret;
4536 /* Do the final step of an ELF link. */
4538 boolean
4539 elf_bfd_final_link (abfd, info)
4540 bfd *abfd;
4541 struct bfd_link_info *info;
4543 boolean dynamic;
4544 boolean emit_relocs;
4545 bfd *dynobj;
4546 struct elf_final_link_info finfo;
4547 register asection *o;
4548 register struct bfd_link_order *p;
4549 register bfd *sub;
4550 bfd_size_type max_contents_size;
4551 bfd_size_type max_external_reloc_size;
4552 bfd_size_type max_internal_reloc_count;
4553 bfd_size_type max_sym_count;
4554 file_ptr off;
4555 Elf_Internal_Sym elfsym;
4556 unsigned int i;
4557 Elf_Internal_Shdr *symtab_hdr;
4558 Elf_Internal_Shdr *symstrtab_hdr;
4559 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4560 struct elf_outext_info eoinfo;
4561 boolean merged;
4562 size_t relativecount = 0;
4563 asection *reldyn = 0;
4564 bfd_size_type amt;
4566 if (! is_elf_hash_table (info))
4567 return false;
4569 if (info->shared)
4570 abfd->flags |= DYNAMIC;
4572 dynamic = elf_hash_table (info)->dynamic_sections_created;
4573 dynobj = elf_hash_table (info)->dynobj;
4575 emit_relocs = (info->relocateable
4576 || info->emitrelocations
4577 || bed->elf_backend_emit_relocs);
4579 finfo.info = info;
4580 finfo.output_bfd = abfd;
4581 finfo.symstrtab = elf_stringtab_init ();
4582 if (finfo.symstrtab == NULL)
4583 return false;
4585 if (! dynamic)
4587 finfo.dynsym_sec = NULL;
4588 finfo.hash_sec = NULL;
4589 finfo.symver_sec = NULL;
4591 else
4593 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4594 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4595 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4596 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4597 /* Note that it is OK if symver_sec is NULL. */
4600 finfo.contents = NULL;
4601 finfo.external_relocs = NULL;
4602 finfo.internal_relocs = NULL;
4603 finfo.external_syms = NULL;
4604 finfo.internal_syms = NULL;
4605 finfo.indices = NULL;
4606 finfo.sections = NULL;
4607 finfo.symbuf = NULL;
4608 finfo.symbuf_count = 0;
4610 /* Count up the number of relocations we will output for each output
4611 section, so that we know the sizes of the reloc sections. We
4612 also figure out some maximum sizes. */
4613 max_contents_size = 0;
4614 max_external_reloc_size = 0;
4615 max_internal_reloc_count = 0;
4616 max_sym_count = 0;
4617 merged = false;
4618 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4620 o->reloc_count = 0;
4622 for (p = o->link_order_head; p != NULL; p = p->next)
4624 if (p->type == bfd_section_reloc_link_order
4625 || p->type == bfd_symbol_reloc_link_order)
4626 ++o->reloc_count;
4627 else if (p->type == bfd_indirect_link_order)
4629 asection *sec;
4631 sec = p->u.indirect.section;
4633 /* Mark all sections which are to be included in the
4634 link. This will normally be every section. We need
4635 to do this so that we can identify any sections which
4636 the linker has decided to not include. */
4637 sec->linker_mark = true;
4639 if (sec->flags & SEC_MERGE)
4640 merged = true;
4642 if (info->relocateable || info->emitrelocations)
4643 o->reloc_count += sec->reloc_count;
4644 else if (bed->elf_backend_count_relocs)
4646 Elf_Internal_Rela * relocs;
4648 relocs = (NAME(_bfd_elf,link_read_relocs)
4649 (abfd, sec, (PTR) NULL,
4650 (Elf_Internal_Rela *) NULL, info->keep_memory));
4652 o->reloc_count += (*bed->elf_backend_count_relocs)
4653 (sec, relocs);
4655 if (!info->keep_memory)
4656 free (relocs);
4659 if (sec->_raw_size > max_contents_size)
4660 max_contents_size = sec->_raw_size;
4661 if (sec->_cooked_size > max_contents_size)
4662 max_contents_size = sec->_cooked_size;
4664 /* We are interested in just local symbols, not all
4665 symbols. */
4666 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4667 && (sec->owner->flags & DYNAMIC) == 0)
4669 size_t sym_count;
4671 if (elf_bad_symtab (sec->owner))
4672 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4673 / sizeof (Elf_External_Sym));
4674 else
4675 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4677 if (sym_count > max_sym_count)
4678 max_sym_count = sym_count;
4680 if ((sec->flags & SEC_RELOC) != 0)
4682 size_t ext_size;
4684 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4685 if (ext_size > max_external_reloc_size)
4686 max_external_reloc_size = ext_size;
4687 if (sec->reloc_count > max_internal_reloc_count)
4688 max_internal_reloc_count = sec->reloc_count;
4694 if (o->reloc_count > 0)
4695 o->flags |= SEC_RELOC;
4696 else
4698 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4699 set it (this is probably a bug) and if it is set
4700 assign_section_numbers will create a reloc section. */
4701 o->flags &=~ SEC_RELOC;
4704 /* If the SEC_ALLOC flag is not set, force the section VMA to
4705 zero. This is done in elf_fake_sections as well, but forcing
4706 the VMA to 0 here will ensure that relocs against these
4707 sections are handled correctly. */
4708 if ((o->flags & SEC_ALLOC) == 0
4709 && ! o->user_set_vma)
4710 o->vma = 0;
4713 if (! info->relocateable && merged)
4714 elf_link_hash_traverse (elf_hash_table (info),
4715 elf_link_sec_merge_syms, (PTR) abfd);
4717 /* Figure out the file positions for everything but the symbol table
4718 and the relocs. We set symcount to force assign_section_numbers
4719 to create a symbol table. */
4720 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
4721 BFD_ASSERT (! abfd->output_has_begun);
4722 if (! _bfd_elf_compute_section_file_positions (abfd, info))
4723 goto error_return;
4725 /* Figure out how many relocations we will have in each section.
4726 Just using RELOC_COUNT isn't good enough since that doesn't
4727 maintain a separate value for REL vs. RELA relocations. */
4728 if (emit_relocs)
4729 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4730 for (o = sub->sections; o != NULL; o = o->next)
4732 asection *output_section;
4734 if (! o->linker_mark)
4736 /* This section was omitted from the link. */
4737 continue;
4740 output_section = o->output_section;
4742 if (output_section != NULL
4743 && (o->flags & SEC_RELOC) != 0)
4745 struct bfd_elf_section_data *esdi
4746 = elf_section_data (o);
4747 struct bfd_elf_section_data *esdo
4748 = elf_section_data (output_section);
4749 unsigned int *rel_count;
4750 unsigned int *rel_count2;
4752 /* We must be careful to add the relocation froms the
4753 input section to the right output count. */
4754 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
4756 rel_count = &esdo->rel_count;
4757 rel_count2 = &esdo->rel_count2;
4759 else
4761 rel_count = &esdo->rel_count2;
4762 rel_count2 = &esdo->rel_count;
4765 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
4766 if (esdi->rel_hdr2)
4767 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
4768 output_section->flags |= SEC_RELOC;
4772 /* That created the reloc sections. Set their sizes, and assign
4773 them file positions, and allocate some buffers. */
4774 for (o = abfd->sections; o != NULL; o = o->next)
4776 if ((o->flags & SEC_RELOC) != 0)
4778 if (!elf_link_size_reloc_section (abfd,
4779 &elf_section_data (o)->rel_hdr,
4781 goto error_return;
4783 if (elf_section_data (o)->rel_hdr2
4784 && !elf_link_size_reloc_section (abfd,
4785 elf_section_data (o)->rel_hdr2,
4787 goto error_return;
4790 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4791 to count upwards while actually outputting the relocations. */
4792 elf_section_data (o)->rel_count = 0;
4793 elf_section_data (o)->rel_count2 = 0;
4796 _bfd_elf_assign_file_positions_for_relocs (abfd);
4798 /* We have now assigned file positions for all the sections except
4799 .symtab and .strtab. We start the .symtab section at the current
4800 file position, and write directly to it. We build the .strtab
4801 section in memory. */
4802 bfd_get_symcount (abfd) = 0;
4803 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4804 /* sh_name is set in prep_headers. */
4805 symtab_hdr->sh_type = SHT_SYMTAB;
4806 symtab_hdr->sh_flags = 0;
4807 symtab_hdr->sh_addr = 0;
4808 symtab_hdr->sh_size = 0;
4809 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
4810 /* sh_link is set in assign_section_numbers. */
4811 /* sh_info is set below. */
4812 /* sh_offset is set just below. */
4813 symtab_hdr->sh_addralign = bed->s->file_align;
4815 off = elf_tdata (abfd)->next_file_pos;
4816 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
4818 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4819 incorrect. We do not yet know the size of the .symtab section.
4820 We correct next_file_pos below, after we do know the size. */
4822 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4823 continuously seeking to the right position in the file. */
4824 if (! info->keep_memory || max_sym_count < 20)
4825 finfo.symbuf_size = 20;
4826 else
4827 finfo.symbuf_size = max_sym_count;
4828 amt = finfo.symbuf_size;
4829 amt *= sizeof (Elf_External_Sym);
4830 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
4831 if (finfo.symbuf == NULL)
4832 goto error_return;
4834 /* Start writing out the symbol table. The first symbol is always a
4835 dummy symbol. */
4836 if (info->strip != strip_all
4837 || emit_relocs)
4839 elfsym.st_value = 0;
4840 elfsym.st_size = 0;
4841 elfsym.st_info = 0;
4842 elfsym.st_other = 0;
4843 elfsym.st_shndx = SHN_UNDEF;
4844 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4845 &elfsym, bfd_und_section_ptr))
4846 goto error_return;
4849 #if 0
4850 /* Some standard ELF linkers do this, but we don't because it causes
4851 bootstrap comparison failures. */
4852 /* Output a file symbol for the output file as the second symbol.
4853 We output this even if we are discarding local symbols, although
4854 I'm not sure if this is correct. */
4855 elfsym.st_value = 0;
4856 elfsym.st_size = 0;
4857 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4858 elfsym.st_other = 0;
4859 elfsym.st_shndx = SHN_ABS;
4860 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
4861 &elfsym, bfd_abs_section_ptr))
4862 goto error_return;
4863 #endif
4865 /* Output a symbol for each section. We output these even if we are
4866 discarding local symbols, since they are used for relocs. These
4867 symbols have no names. We store the index of each one in the
4868 index field of the section, so that we can find it again when
4869 outputting relocs. */
4870 if (info->strip != strip_all
4871 || emit_relocs)
4873 elfsym.st_size = 0;
4874 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4875 elfsym.st_other = 0;
4876 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
4878 o = section_from_elf_index (abfd, i);
4879 if (o != NULL)
4880 o->target_index = bfd_get_symcount (abfd);
4881 elfsym.st_shndx = i;
4882 if (info->relocateable || o == NULL)
4883 elfsym.st_value = 0;
4884 else
4885 elfsym.st_value = o->vma;
4886 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4887 &elfsym, o))
4888 goto error_return;
4892 /* Allocate some memory to hold information read in from the input
4893 files. */
4894 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4895 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
4896 finfo.internal_relocs = ((Elf_Internal_Rela *)
4897 bfd_malloc (max_internal_reloc_count
4898 * sizeof (Elf_Internal_Rela)
4899 * bed->s->int_rels_per_ext_rel));
4900 finfo.external_syms = ((Elf_External_Sym *)
4901 bfd_malloc (max_sym_count
4902 * sizeof (Elf_External_Sym)));
4903 finfo.internal_syms = ((Elf_Internal_Sym *)
4904 bfd_malloc (max_sym_count
4905 * sizeof (Elf_Internal_Sym)));
4906 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4907 finfo.sections = ((asection **)
4908 bfd_malloc (max_sym_count * sizeof (asection *)));
4909 if ((finfo.contents == NULL && max_contents_size != 0)
4910 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
4911 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
4912 || (finfo.external_syms == NULL && max_sym_count != 0)
4913 || (finfo.internal_syms == NULL && max_sym_count != 0)
4914 || (finfo.indices == NULL && max_sym_count != 0)
4915 || (finfo.sections == NULL && max_sym_count != 0))
4916 goto error_return;
4918 /* Since ELF permits relocations to be against local symbols, we
4919 must have the local symbols available when we do the relocations.
4920 Since we would rather only read the local symbols once, and we
4921 would rather not keep them in memory, we handle all the
4922 relocations for a single input file at the same time.
4924 Unfortunately, there is no way to know the total number of local
4925 symbols until we have seen all of them, and the local symbol
4926 indices precede the global symbol indices. This means that when
4927 we are generating relocateable output, and we see a reloc against
4928 a global symbol, we can not know the symbol index until we have
4929 finished examining all the local symbols to see which ones we are
4930 going to output. To deal with this, we keep the relocations in
4931 memory, and don't output them until the end of the link. This is
4932 an unfortunate waste of memory, but I don't see a good way around
4933 it. Fortunately, it only happens when performing a relocateable
4934 link, which is not the common case. FIXME: If keep_memory is set
4935 we could write the relocs out and then read them again; I don't
4936 know how bad the memory loss will be. */
4938 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4939 sub->output_has_begun = false;
4940 for (o = abfd->sections; o != NULL; o = o->next)
4942 for (p = o->link_order_head; p != NULL; p = p->next)
4944 if (p->type == bfd_indirect_link_order
4945 && (bfd_get_flavour (p->u.indirect.section->owner)
4946 == bfd_target_elf_flavour))
4948 sub = p->u.indirect.section->owner;
4949 if (! sub->output_has_begun)
4951 if (! elf_link_input_bfd (&finfo, sub))
4952 goto error_return;
4953 sub->output_has_begun = true;
4956 else if (p->type == bfd_section_reloc_link_order
4957 || p->type == bfd_symbol_reloc_link_order)
4959 if (! elf_reloc_link_order (abfd, info, o, p))
4960 goto error_return;
4962 else
4964 if (! _bfd_default_link_order (abfd, info, o, p))
4965 goto error_return;
4970 /* That wrote out all the local symbols. Finish up the symbol table
4971 with the global symbols. Even if we want to strip everything we
4972 can, we still need to deal with those global symbols that got
4973 converted to local in a version script. */
4975 if (info->shared)
4977 /* Output any global symbols that got converted to local in a
4978 version script. We do this in a separate step since ELF
4979 requires all local symbols to appear prior to any global
4980 symbols. FIXME: We should only do this if some global
4981 symbols were, in fact, converted to become local. FIXME:
4982 Will this work correctly with the Irix 5 linker? */
4983 eoinfo.failed = false;
4984 eoinfo.finfo = &finfo;
4985 eoinfo.localsyms = true;
4986 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
4987 (PTR) &eoinfo);
4988 if (eoinfo.failed)
4989 return false;
4992 /* The sh_info field records the index of the first non local symbol. */
4993 symtab_hdr->sh_info = bfd_get_symcount (abfd);
4995 if (dynamic
4996 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
4998 Elf_Internal_Sym sym;
4999 Elf_External_Sym *dynsym =
5000 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5001 long last_local = 0;
5003 /* Write out the section symbols for the output sections. */
5004 if (info->shared)
5006 asection *s;
5008 sym.st_size = 0;
5009 sym.st_name = 0;
5010 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5011 sym.st_other = 0;
5013 for (s = abfd->sections; s != NULL; s = s->next)
5015 int indx;
5016 indx = elf_section_data (s)->this_idx;
5017 BFD_ASSERT (indx > 0);
5018 sym.st_shndx = indx;
5019 sym.st_value = s->vma;
5021 elf_swap_symbol_out (abfd, &sym,
5022 dynsym + elf_section_data (s)->dynindx);
5025 last_local = bfd_count_sections (abfd);
5028 /* Write out the local dynsyms. */
5029 if (elf_hash_table (info)->dynlocal)
5031 struct elf_link_local_dynamic_entry *e;
5032 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5034 asection *s;
5036 sym.st_size = e->isym.st_size;
5037 sym.st_other = e->isym.st_other;
5039 /* Copy the internal symbol as is.
5040 Note that we saved a word of storage and overwrote
5041 the original st_name with the dynstr_index. */
5042 sym = e->isym;
5044 if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE)
5046 s = bfd_section_from_elf_index (e->input_bfd,
5047 e->isym.st_shndx);
5049 sym.st_shndx =
5050 elf_section_data (s->output_section)->this_idx;
5051 sym.st_value = (s->output_section->vma
5052 + s->output_offset
5053 + e->isym.st_value);
5056 if (last_local < e->dynindx)
5057 last_local = e->dynindx;
5059 elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx);
5063 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5064 last_local + 1;
5067 /* We get the global symbols from the hash table. */
5068 eoinfo.failed = false;
5069 eoinfo.localsyms = false;
5070 eoinfo.finfo = &finfo;
5071 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5072 (PTR) &eoinfo);
5073 if (eoinfo.failed)
5074 return false;
5076 /* If backend needs to output some symbols not present in the hash
5077 table, do it now. */
5078 if (bed->elf_backend_output_arch_syms)
5080 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5081 Elf_Internal_Sym *,
5082 asection *));
5084 if (! ((*bed->elf_backend_output_arch_syms)
5085 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5086 return false;
5089 /* Flush all symbols to the file. */
5090 if (! elf_link_flush_output_syms (&finfo))
5091 return false;
5093 /* Now we know the size of the symtab section. */
5094 off += symtab_hdr->sh_size;
5096 /* Finish up and write out the symbol string table (.strtab)
5097 section. */
5098 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5099 /* sh_name was set in prep_headers. */
5100 symstrtab_hdr->sh_type = SHT_STRTAB;
5101 symstrtab_hdr->sh_flags = 0;
5102 symstrtab_hdr->sh_addr = 0;
5103 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5104 symstrtab_hdr->sh_entsize = 0;
5105 symstrtab_hdr->sh_link = 0;
5106 symstrtab_hdr->sh_info = 0;
5107 /* sh_offset is set just below. */
5108 symstrtab_hdr->sh_addralign = 1;
5110 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5111 elf_tdata (abfd)->next_file_pos = off;
5113 if (bfd_get_symcount (abfd) > 0)
5115 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5116 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5117 return false;
5120 /* Adjust the relocs to have the correct symbol indices. */
5121 for (o = abfd->sections; o != NULL; o = o->next)
5123 if ((o->flags & SEC_RELOC) == 0)
5124 continue;
5126 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5127 elf_section_data (o)->rel_count,
5128 elf_section_data (o)->rel_hashes);
5129 if (elf_section_data (o)->rel_hdr2 != NULL)
5130 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5131 elf_section_data (o)->rel_count2,
5132 (elf_section_data (o)->rel_hashes
5133 + elf_section_data (o)->rel_count));
5135 /* Set the reloc_count field to 0 to prevent write_relocs from
5136 trying to swap the relocs out itself. */
5137 o->reloc_count = 0;
5140 if (dynamic && info->combreloc && dynobj != NULL)
5141 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5143 /* If we are linking against a dynamic object, or generating a
5144 shared library, finish up the dynamic linking information. */
5145 if (dynamic)
5147 Elf_External_Dyn *dyncon, *dynconend;
5149 /* Fix up .dynamic entries. */
5150 o = bfd_get_section_by_name (dynobj, ".dynamic");
5151 BFD_ASSERT (o != NULL);
5153 dyncon = (Elf_External_Dyn *) o->contents;
5154 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5155 for (; dyncon < dynconend; dyncon++)
5157 Elf_Internal_Dyn dyn;
5158 const char *name;
5159 unsigned int type;
5161 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5163 switch (dyn.d_tag)
5165 default:
5166 break;
5167 case DT_NULL:
5168 if (relativecount > 0 && dyncon + 1 < dynconend)
5170 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5172 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5173 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5174 default: break;
5176 if (dyn.d_tag != DT_NULL)
5178 dyn.d_un.d_val = relativecount;
5179 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5180 relativecount = 0;
5183 break;
5184 case DT_INIT:
5185 name = info->init_function;
5186 goto get_sym;
5187 case DT_FINI:
5188 name = info->fini_function;
5189 get_sym:
5191 struct elf_link_hash_entry *h;
5193 h = elf_link_hash_lookup (elf_hash_table (info), name,
5194 false, false, true);
5195 if (h != NULL
5196 && (h->root.type == bfd_link_hash_defined
5197 || h->root.type == bfd_link_hash_defweak))
5199 dyn.d_un.d_val = h->root.u.def.value;
5200 o = h->root.u.def.section;
5201 if (o->output_section != NULL)
5202 dyn.d_un.d_val += (o->output_section->vma
5203 + o->output_offset);
5204 else
5206 /* The symbol is imported from another shared
5207 library and does not apply to this one. */
5208 dyn.d_un.d_val = 0;
5211 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5214 break;
5216 case DT_HASH:
5217 name = ".hash";
5218 goto get_vma;
5219 case DT_STRTAB:
5220 name = ".dynstr";
5221 goto get_vma;
5222 case DT_SYMTAB:
5223 name = ".dynsym";
5224 goto get_vma;
5225 case DT_VERDEF:
5226 name = ".gnu.version_d";
5227 goto get_vma;
5228 case DT_VERNEED:
5229 name = ".gnu.version_r";
5230 goto get_vma;
5231 case DT_VERSYM:
5232 name = ".gnu.version";
5233 get_vma:
5234 o = bfd_get_section_by_name (abfd, name);
5235 BFD_ASSERT (o != NULL);
5236 dyn.d_un.d_ptr = o->vma;
5237 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5238 break;
5240 case DT_REL:
5241 case DT_RELA:
5242 case DT_RELSZ:
5243 case DT_RELASZ:
5244 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5245 type = SHT_REL;
5246 else
5247 type = SHT_RELA;
5248 dyn.d_un.d_val = 0;
5249 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5251 Elf_Internal_Shdr *hdr;
5253 hdr = elf_elfsections (abfd)[i];
5254 if (hdr->sh_type == type
5255 && (hdr->sh_flags & SHF_ALLOC) != 0)
5257 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5258 dyn.d_un.d_val += hdr->sh_size;
5259 else
5261 if (dyn.d_un.d_val == 0
5262 || hdr->sh_addr < dyn.d_un.d_val)
5263 dyn.d_un.d_val = hdr->sh_addr;
5267 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5268 break;
5273 /* If we have created any dynamic sections, then output them. */
5274 if (dynobj != NULL)
5276 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5277 goto error_return;
5279 for (o = dynobj->sections; o != NULL; o = o->next)
5281 if ((o->flags & SEC_HAS_CONTENTS) == 0
5282 || o->_raw_size == 0
5283 || o->output_section == bfd_abs_section_ptr)
5284 continue;
5285 if ((o->flags & SEC_LINKER_CREATED) == 0)
5287 /* At this point, we are only interested in sections
5288 created by elf_link_create_dynamic_sections. */
5289 continue;
5291 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5292 != SHT_STRTAB)
5293 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5295 if (! bfd_set_section_contents (abfd, o->output_section,
5296 o->contents,
5297 (file_ptr) o->output_offset,
5298 o->_raw_size))
5299 goto error_return;
5301 else
5303 /* The contents of the .dynstr section are actually in a
5304 stringtab. */
5305 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5306 if (bfd_seek (abfd, off, SEEK_SET) != 0
5307 || ! _bfd_stringtab_emit (abfd,
5308 elf_hash_table (info)->dynstr))
5309 goto error_return;
5314 /* If we have optimized stabs strings, output them. */
5315 if (elf_hash_table (info)->stab_info != NULL)
5317 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5318 goto error_return;
5321 if (finfo.symstrtab != NULL)
5322 _bfd_stringtab_free (finfo.symstrtab);
5323 if (finfo.contents != NULL)
5324 free (finfo.contents);
5325 if (finfo.external_relocs != NULL)
5326 free (finfo.external_relocs);
5327 if (finfo.internal_relocs != NULL)
5328 free (finfo.internal_relocs);
5329 if (finfo.external_syms != NULL)
5330 free (finfo.external_syms);
5331 if (finfo.internal_syms != NULL)
5332 free (finfo.internal_syms);
5333 if (finfo.indices != NULL)
5334 free (finfo.indices);
5335 if (finfo.sections != NULL)
5336 free (finfo.sections);
5337 if (finfo.symbuf != NULL)
5338 free (finfo.symbuf);
5339 for (o = abfd->sections; o != NULL; o = o->next)
5341 if ((o->flags & SEC_RELOC) != 0
5342 && elf_section_data (o)->rel_hashes != NULL)
5343 free (elf_section_data (o)->rel_hashes);
5346 elf_tdata (abfd)->linker = true;
5348 return true;
5350 error_return:
5351 if (finfo.symstrtab != NULL)
5352 _bfd_stringtab_free (finfo.symstrtab);
5353 if (finfo.contents != NULL)
5354 free (finfo.contents);
5355 if (finfo.external_relocs != NULL)
5356 free (finfo.external_relocs);
5357 if (finfo.internal_relocs != NULL)
5358 free (finfo.internal_relocs);
5359 if (finfo.external_syms != NULL)
5360 free (finfo.external_syms);
5361 if (finfo.internal_syms != NULL)
5362 free (finfo.internal_syms);
5363 if (finfo.indices != NULL)
5364 free (finfo.indices);
5365 if (finfo.sections != NULL)
5366 free (finfo.sections);
5367 if (finfo.symbuf != NULL)
5368 free (finfo.symbuf);
5369 for (o = abfd->sections; o != NULL; o = o->next)
5371 if ((o->flags & SEC_RELOC) != 0
5372 && elf_section_data (o)->rel_hashes != NULL)
5373 free (elf_section_data (o)->rel_hashes);
5376 return false;
5379 /* Add a symbol to the output symbol table. */
5381 static boolean
5382 elf_link_output_sym (finfo, name, elfsym, input_sec)
5383 struct elf_final_link_info *finfo;
5384 const char *name;
5385 Elf_Internal_Sym *elfsym;
5386 asection *input_sec;
5388 boolean (*output_symbol_hook) PARAMS ((bfd *,
5389 struct bfd_link_info *info,
5390 const char *,
5391 Elf_Internal_Sym *,
5392 asection *));
5394 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5395 elf_backend_link_output_symbol_hook;
5396 if (output_symbol_hook != NULL)
5398 if (! ((*output_symbol_hook)
5399 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5400 return false;
5403 if (name == (const char *) NULL || *name == '\0')
5404 elfsym->st_name = 0;
5405 else if (input_sec->flags & SEC_EXCLUDE)
5406 elfsym->st_name = 0;
5407 else
5409 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5410 name, true, false);
5411 if (elfsym->st_name == (unsigned long) -1)
5412 return false;
5415 if (finfo->symbuf_count >= finfo->symbuf_size)
5417 if (! elf_link_flush_output_syms (finfo))
5418 return false;
5421 elf_swap_symbol_out (finfo->output_bfd, elfsym,
5422 (PTR) (finfo->symbuf + finfo->symbuf_count));
5423 ++finfo->symbuf_count;
5425 ++ bfd_get_symcount (finfo->output_bfd);
5427 return true;
5430 /* Flush the output symbols to the file. */
5432 static boolean
5433 elf_link_flush_output_syms (finfo)
5434 struct elf_final_link_info *finfo;
5436 if (finfo->symbuf_count > 0)
5438 Elf_Internal_Shdr *symtab;
5439 file_ptr pos;
5440 bfd_size_type amt;
5442 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5443 pos = symtab->sh_offset + symtab->sh_size;
5444 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5445 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5446 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5447 return false;
5449 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5451 finfo->symbuf_count = 0;
5454 return true;
5457 /* Adjust all external symbols pointing into SEC_MERGE sections
5458 to reflect the object merging within the sections. */
5460 static boolean
5461 elf_link_sec_merge_syms (h, data)
5462 struct elf_link_hash_entry *h;
5463 PTR data;
5465 asection *sec;
5467 if ((h->root.type == bfd_link_hash_defined
5468 || h->root.type == bfd_link_hash_defweak)
5469 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5470 && elf_section_data (sec)->merge_info)
5472 bfd *output_bfd = (bfd *) data;
5474 h->root.u.def.value =
5475 _bfd_merged_section_offset (output_bfd,
5476 &h->root.u.def.section,
5477 elf_section_data (sec)->merge_info,
5478 h->root.u.def.value, (bfd_vma) 0);
5481 return true;
5484 /* Add an external symbol to the symbol table. This is called from
5485 the hash table traversal routine. When generating a shared object,
5486 we go through the symbol table twice. The first time we output
5487 anything that might have been forced to local scope in a version
5488 script. The second time we output the symbols that are still
5489 global symbols. */
5491 static boolean
5492 elf_link_output_extsym (h, data)
5493 struct elf_link_hash_entry *h;
5494 PTR data;
5496 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5497 struct elf_final_link_info *finfo = eoinfo->finfo;
5498 boolean strip;
5499 Elf_Internal_Sym sym;
5500 asection *input_sec;
5502 /* Decide whether to output this symbol in this pass. */
5503 if (eoinfo->localsyms)
5505 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5506 return true;
5508 else
5510 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5511 return true;
5514 /* If we are not creating a shared library, and this symbol is
5515 referenced by a shared library but is not defined anywhere, then
5516 warn that it is undefined. If we do not do this, the runtime
5517 linker will complain that the symbol is undefined when the
5518 program is run. We don't have to worry about symbols that are
5519 referenced by regular files, because we will already have issued
5520 warnings for them. */
5521 if (! finfo->info->relocateable
5522 && ! finfo->info->allow_shlib_undefined
5523 && ! finfo->info->shared
5524 && h->root.type == bfd_link_hash_undefined
5525 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5526 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5528 if (! ((*finfo->info->callbacks->undefined_symbol)
5529 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5530 (asection *) NULL, (bfd_vma) 0, true)))
5532 eoinfo->failed = true;
5533 return false;
5537 /* We don't want to output symbols that have never been mentioned by
5538 a regular file, or that we have been told to strip. However, if
5539 h->indx is set to -2, the symbol is used by a reloc and we must
5540 output it. */
5541 if (h->indx == -2)
5542 strip = false;
5543 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5544 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5545 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5546 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5547 strip = true;
5548 else if (finfo->info->strip == strip_all
5549 || (finfo->info->strip == strip_some
5550 && bfd_hash_lookup (finfo->info->keep_hash,
5551 h->root.root.string,
5552 false, false) == NULL))
5553 strip = true;
5554 else
5555 strip = false;
5557 /* If we're stripping it, and it's not a dynamic symbol, there's
5558 nothing else to do unless it is a forced local symbol. */
5559 if (strip
5560 && h->dynindx == -1
5561 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5562 return true;
5564 sym.st_value = 0;
5565 sym.st_size = h->size;
5566 sym.st_other = h->other;
5567 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5568 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5569 else if (h->root.type == bfd_link_hash_undefweak
5570 || h->root.type == bfd_link_hash_defweak)
5571 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5572 else
5573 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5575 switch (h->root.type)
5577 default:
5578 case bfd_link_hash_new:
5579 abort ();
5580 return false;
5582 case bfd_link_hash_undefined:
5583 input_sec = bfd_und_section_ptr;
5584 sym.st_shndx = SHN_UNDEF;
5585 break;
5587 case bfd_link_hash_undefweak:
5588 input_sec = bfd_und_section_ptr;
5589 sym.st_shndx = SHN_UNDEF;
5590 break;
5592 case bfd_link_hash_defined:
5593 case bfd_link_hash_defweak:
5595 input_sec = h->root.u.def.section;
5596 if (input_sec->output_section != NULL)
5598 sym.st_shndx =
5599 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5600 input_sec->output_section);
5601 if (sym.st_shndx == (unsigned short) -1)
5603 (*_bfd_error_handler)
5604 (_("%s: could not find output section %s for input section %s"),
5605 bfd_get_filename (finfo->output_bfd),
5606 input_sec->output_section->name,
5607 input_sec->name);
5608 eoinfo->failed = true;
5609 return false;
5612 /* ELF symbols in relocateable files are section relative,
5613 but in nonrelocateable files they are virtual
5614 addresses. */
5615 sym.st_value = h->root.u.def.value + input_sec->output_offset;
5616 if (! finfo->info->relocateable)
5617 sym.st_value += input_sec->output_section->vma;
5619 else
5621 BFD_ASSERT (input_sec->owner == NULL
5622 || (input_sec->owner->flags & DYNAMIC) != 0);
5623 sym.st_shndx = SHN_UNDEF;
5624 input_sec = bfd_und_section_ptr;
5627 break;
5629 case bfd_link_hash_common:
5630 input_sec = h->root.u.c.p->section;
5631 sym.st_shndx = SHN_COMMON;
5632 sym.st_value = 1 << h->root.u.c.p->alignment_power;
5633 break;
5635 case bfd_link_hash_indirect:
5636 /* These symbols are created by symbol versioning. They point
5637 to the decorated version of the name. For example, if the
5638 symbol foo@@GNU_1.2 is the default, which should be used when
5639 foo is used with no version, then we add an indirect symbol
5640 foo which points to foo@@GNU_1.2. We ignore these symbols,
5641 since the indirected symbol is already in the hash table. */
5642 return true;
5644 case bfd_link_hash_warning:
5645 /* We can't represent these symbols in ELF, although a warning
5646 symbol may have come from a .gnu.warning.SYMBOL section. We
5647 just put the target symbol in the hash table. If the target
5648 symbol does not really exist, don't do anything. */
5649 if (h->root.u.i.link->type == bfd_link_hash_new)
5650 return true;
5651 return (elf_link_output_extsym
5652 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
5655 /* Give the processor backend a chance to tweak the symbol value,
5656 and also to finish up anything that needs to be done for this
5657 symbol. */
5658 if ((h->dynindx != -1
5659 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5660 && elf_hash_table (finfo->info)->dynamic_sections_created)
5662 struct elf_backend_data *bed;
5664 bed = get_elf_backend_data (finfo->output_bfd);
5665 if (! ((*bed->elf_backend_finish_dynamic_symbol)
5666 (finfo->output_bfd, finfo->info, h, &sym)))
5668 eoinfo->failed = true;
5669 return false;
5673 /* If we are marking the symbol as undefined, and there are no
5674 non-weak references to this symbol from a regular object, then
5675 mark the symbol as weak undefined; if there are non-weak
5676 references, mark the symbol as strong. We can't do this earlier,
5677 because it might not be marked as undefined until the
5678 finish_dynamic_symbol routine gets through with it. */
5679 if (sym.st_shndx == SHN_UNDEF
5680 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
5681 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
5682 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
5684 int bindtype;
5686 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
5687 bindtype = STB_GLOBAL;
5688 else
5689 bindtype = STB_WEAK;
5690 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
5693 /* If a symbol is not defined locally, we clear the visibility
5694 field. */
5695 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5696 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
5698 /* If this symbol should be put in the .dynsym section, then put it
5699 there now. We have already know the symbol index. We also fill
5700 in the entry in the .hash section. */
5701 if (h->dynindx != -1
5702 && elf_hash_table (finfo->info)->dynamic_sections_created)
5704 size_t bucketcount;
5705 size_t bucket;
5706 size_t hash_entry_size;
5707 bfd_byte *bucketpos;
5708 bfd_vma chain;
5709 Elf_External_Sym *esym;
5711 sym.st_name = h->dynstr_index;
5712 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
5713 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym);
5715 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5716 bucket = h->elf_hash_value % bucketcount;
5717 hash_entry_size
5718 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
5719 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5720 + (bucket + 2) * hash_entry_size);
5721 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
5722 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
5723 bucketpos);
5724 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
5725 ((bfd_byte *) finfo->hash_sec->contents
5726 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
5728 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
5730 Elf_Internal_Versym iversym;
5731 Elf_External_Versym *eversym;
5733 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5735 if (h->verinfo.verdef == NULL)
5736 iversym.vs_vers = 0;
5737 else
5738 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
5740 else
5742 if (h->verinfo.vertree == NULL)
5743 iversym.vs_vers = 1;
5744 else
5745 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
5748 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
5749 iversym.vs_vers |= VERSYM_HIDDEN;
5751 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
5752 eversym += h->dynindx;
5753 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
5757 /* If we're stripping it, then it was just a dynamic symbol, and
5758 there's nothing else to do. */
5759 if (strip)
5760 return true;
5762 h->indx = bfd_get_symcount (finfo->output_bfd);
5764 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
5766 eoinfo->failed = true;
5767 return false;
5770 return true;
5773 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5774 originated from the section given by INPUT_REL_HDR) to the
5775 OUTPUT_BFD. */
5777 static void
5778 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
5779 internal_relocs)
5780 bfd *output_bfd;
5781 asection *input_section;
5782 Elf_Internal_Shdr *input_rel_hdr;
5783 Elf_Internal_Rela *internal_relocs;
5785 Elf_Internal_Rela *irela;
5786 Elf_Internal_Rela *irelaend;
5787 Elf_Internal_Shdr *output_rel_hdr;
5788 asection *output_section;
5789 unsigned int *rel_countp = NULL;
5790 struct elf_backend_data *bed;
5791 bfd_size_type amt;
5793 output_section = input_section->output_section;
5794 output_rel_hdr = NULL;
5796 if (elf_section_data (output_section)->rel_hdr.sh_entsize
5797 == input_rel_hdr->sh_entsize)
5799 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
5800 rel_countp = &elf_section_data (output_section)->rel_count;
5802 else if (elf_section_data (output_section)->rel_hdr2
5803 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
5804 == input_rel_hdr->sh_entsize))
5806 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
5807 rel_countp = &elf_section_data (output_section)->rel_count2;
5810 BFD_ASSERT (output_rel_hdr != NULL);
5812 bed = get_elf_backend_data (output_bfd);
5813 irela = internal_relocs;
5814 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
5815 * bed->s->int_rels_per_ext_rel;
5817 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5819 Elf_External_Rel *erel;
5820 Elf_Internal_Rel *irel;
5822 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
5823 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
5824 if (irel == NULL)
5826 (*_bfd_error_handler) (_("Error: out of memory"));
5827 abort ();
5830 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
5831 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
5833 unsigned int i;
5835 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5837 irel[i].r_offset = irela[i].r_offset;
5838 irel[i].r_info = irela[i].r_info;
5839 BFD_ASSERT (irela[i].r_addend == 0);
5842 if (bed->s->swap_reloc_out)
5843 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
5844 else
5845 elf_swap_reloc_out (output_bfd, irel, erel);
5848 free (irel);
5850 else
5852 Elf_External_Rela *erela;
5854 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
5856 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
5857 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
5858 if (bed->s->swap_reloca_out)
5859 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
5860 else
5861 elf_swap_reloca_out (output_bfd, irela, erela);
5864 /* Bump the counter, so that we know where to add the next set of
5865 relocations. */
5866 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
5869 /* Link an input file into the linker output file. This function
5870 handles all the sections and relocations of the input file at once.
5871 This is so that we only have to read the local symbols once, and
5872 don't have to keep them in memory. */
5874 static boolean
5875 elf_link_input_bfd (finfo, input_bfd)
5876 struct elf_final_link_info *finfo;
5877 bfd *input_bfd;
5879 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
5880 bfd *, asection *, bfd_byte *,
5881 Elf_Internal_Rela *,
5882 Elf_Internal_Sym *, asection **));
5883 bfd *output_bfd;
5884 Elf_Internal_Shdr *symtab_hdr;
5885 size_t locsymcount;
5886 size_t extsymoff;
5887 Elf_External_Sym *external_syms;
5888 Elf_External_Sym *esym;
5889 Elf_External_Sym *esymend;
5890 Elf_Internal_Sym *isym;
5891 long *pindex;
5892 asection **ppsection;
5893 asection *o;
5894 struct elf_backend_data *bed;
5895 boolean emit_relocs;
5897 output_bfd = finfo->output_bfd;
5898 bed = get_elf_backend_data (output_bfd);
5899 relocate_section = bed->elf_backend_relocate_section;
5901 /* If this is a dynamic object, we don't want to do anything here:
5902 we don't want the local symbols, and we don't want the section
5903 contents. */
5904 if ((input_bfd->flags & DYNAMIC) != 0)
5905 return true;
5907 emit_relocs = (finfo->info->relocateable
5908 || finfo->info->emitrelocations
5909 || bed->elf_backend_emit_relocs);
5911 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5912 if (elf_bad_symtab (input_bfd))
5914 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5915 extsymoff = 0;
5917 else
5919 locsymcount = symtab_hdr->sh_info;
5920 extsymoff = symtab_hdr->sh_info;
5923 /* Read the local symbols. */
5924 if (symtab_hdr->contents != NULL)
5925 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
5926 else if (locsymcount == 0)
5927 external_syms = NULL;
5928 else
5930 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
5931 external_syms = finfo->external_syms;
5932 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
5933 || bfd_bread (external_syms, amt, input_bfd) != amt)
5934 return false;
5937 /* Swap in the local symbols and write out the ones which we know
5938 are going into the output file. */
5939 esym = external_syms;
5940 esymend = esym + locsymcount;
5941 isym = finfo->internal_syms;
5942 pindex = finfo->indices;
5943 ppsection = finfo->sections;
5944 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
5946 asection *isec;
5947 const char *name;
5948 Elf_Internal_Sym osym;
5950 elf_swap_symbol_in (input_bfd, esym, isym);
5951 *pindex = -1;
5953 if (elf_bad_symtab (input_bfd))
5955 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
5957 *ppsection = NULL;
5958 continue;
5962 name = NULL;
5963 if (isym->st_shndx == SHN_UNDEF)
5965 isec = bfd_und_section_ptr;
5966 name = isec->name;
5968 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
5970 isec = section_from_elf_index (input_bfd, isym->st_shndx);
5971 if (isec && elf_section_data (isec)->merge_info
5972 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
5973 isym->st_value =
5974 _bfd_merged_section_offset (output_bfd, &isec,
5975 elf_section_data (isec)->merge_info,
5976 isym->st_value, (bfd_vma) 0);
5978 else if (isym->st_shndx == SHN_ABS)
5980 isec = bfd_abs_section_ptr;
5981 name = isec->name;
5983 else if (isym->st_shndx == SHN_COMMON)
5985 isec = bfd_com_section_ptr;
5986 name = isec->name;
5988 else
5990 /* Who knows? */
5991 isec = NULL;
5994 *ppsection = isec;
5996 /* Don't output the first, undefined, symbol. */
5997 if (esym == external_syms)
5998 continue;
6000 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6002 asection *ksec;
6004 /* Save away all section symbol values. */
6005 if (isec != NULL)
6007 if (name)
6009 if (isec->symbol->value != isym->st_value)
6010 (*_bfd_error_handler)
6011 (_("%s: invalid section symbol index 0x%x (%s) ignored"),
6012 bfd_archive_filename (input_bfd), isym->st_shndx,
6013 name);
6014 continue;
6016 isec->symbol->value = isym->st_value;
6019 /* If this is a discarded link-once section symbol, update
6020 it's value to that of the kept section symbol. The
6021 linker will keep the first of any matching link-once
6022 sections, so we should have already seen it's section
6023 symbol. I trust no-one will have the bright idea of
6024 re-ordering the bfd list... */
6025 if (isec != NULL
6026 && (bfd_get_section_flags (input_bfd, isec) & SEC_LINK_ONCE) != 0
6027 && (ksec = isec->kept_section) != NULL)
6029 isym->st_value = ksec->symbol->value;
6031 /* That put the value right, but the section info is all
6032 wrong. I hope this works. */
6033 isec->output_offset = ksec->output_offset;
6034 isec->output_section = ksec->output_section;
6037 /* We never output section symbols. Instead, we use the
6038 section symbol of the corresponding section in the output
6039 file. */
6040 continue;
6043 /* If we are stripping all symbols, we don't want to output this
6044 one. */
6045 if (finfo->info->strip == strip_all)
6046 continue;
6048 /* If we are discarding all local symbols, we don't want to
6049 output this one. If we are generating a relocateable output
6050 file, then some of the local symbols may be required by
6051 relocs; we output them below as we discover that they are
6052 needed. */
6053 if (finfo->info->discard == discard_all)
6054 continue;
6056 /* If this symbol is defined in a section which we are
6057 discarding, we don't need to keep it, but note that
6058 linker_mark is only reliable for sections that have contents.
6059 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6060 as well as linker_mark. */
6061 if (isym->st_shndx > 0
6062 && isym->st_shndx < SHN_LORESERVE
6063 && isec != NULL
6064 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6065 || (! finfo->info->relocateable
6066 && (isec->flags & SEC_EXCLUDE) != 0)))
6067 continue;
6069 /* Get the name of the symbol. */
6070 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6071 isym->st_name);
6072 if (name == NULL)
6073 return false;
6075 /* See if we are discarding symbols with this name. */
6076 if ((finfo->info->strip == strip_some
6077 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6078 == NULL))
6079 || (((finfo->info->discard == discard_sec_merge
6080 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6081 || finfo->info->discard == discard_l)
6082 && bfd_is_local_label_name (input_bfd, name)))
6083 continue;
6085 /* If we get here, we are going to output this symbol. */
6087 osym = *isym;
6089 /* Adjust the section index for the output file. */
6090 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6091 isec->output_section);
6092 if (osym.st_shndx == (unsigned short) -1)
6093 return false;
6095 *pindex = bfd_get_symcount (output_bfd);
6097 /* ELF symbols in relocateable files are section relative, but
6098 in executable files they are virtual addresses. Note that
6099 this code assumes that all ELF sections have an associated
6100 BFD section with a reasonable value for output_offset; below
6101 we assume that they also have a reasonable value for
6102 output_section. Any special sections must be set up to meet
6103 these requirements. */
6104 osym.st_value += isec->output_offset;
6105 if (! finfo->info->relocateable)
6106 osym.st_value += isec->output_section->vma;
6108 if (! elf_link_output_sym (finfo, name, &osym, isec))
6109 return false;
6112 /* Relocate the contents of each section. */
6113 for (o = input_bfd->sections; o != NULL; o = o->next)
6115 bfd_byte *contents;
6117 if (! o->linker_mark)
6119 /* This section was omitted from the link. */
6120 continue;
6123 if ((o->flags & SEC_HAS_CONTENTS) == 0
6124 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6125 continue;
6127 if ((o->flags & SEC_LINKER_CREATED) != 0)
6129 /* Section was created by elf_link_create_dynamic_sections
6130 or somesuch. */
6131 continue;
6134 /* Get the contents of the section. They have been cached by a
6135 relaxation routine. Note that o is a section in an input
6136 file, so the contents field will not have been set by any of
6137 the routines which work on output files. */
6138 if (elf_section_data (o)->this_hdr.contents != NULL)
6139 contents = elf_section_data (o)->this_hdr.contents;
6140 else
6142 contents = finfo->contents;
6143 if (! bfd_get_section_contents (input_bfd, o, contents,
6144 (file_ptr) 0, o->_raw_size))
6145 return false;
6148 if ((o->flags & SEC_RELOC) != 0)
6150 Elf_Internal_Rela *internal_relocs;
6152 /* Get the swapped relocs. */
6153 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6154 (input_bfd, o, finfo->external_relocs,
6155 finfo->internal_relocs, false));
6156 if (internal_relocs == NULL
6157 && o->reloc_count > 0)
6158 return false;
6160 /* Relocate the section by invoking a back end routine.
6162 The back end routine is responsible for adjusting the
6163 section contents as necessary, and (if using Rela relocs
6164 and generating a relocateable output file) adjusting the
6165 reloc addend as necessary.
6167 The back end routine does not have to worry about setting
6168 the reloc address or the reloc symbol index.
6170 The back end routine is given a pointer to the swapped in
6171 internal symbols, and can access the hash table entries
6172 for the external symbols via elf_sym_hashes (input_bfd).
6174 When generating relocateable output, the back end routine
6175 must handle STB_LOCAL/STT_SECTION symbols specially. The
6176 output symbol is going to be a section symbol
6177 corresponding to the output section, which will require
6178 the addend to be adjusted. */
6180 if (! (*relocate_section) (output_bfd, finfo->info,
6181 input_bfd, o, contents,
6182 internal_relocs,
6183 finfo->internal_syms,
6184 finfo->sections))
6185 return false;
6187 if (emit_relocs)
6189 Elf_Internal_Rela *irela;
6190 Elf_Internal_Rela *irelaend;
6191 struct elf_link_hash_entry **rel_hash;
6192 Elf_Internal_Shdr *input_rel_hdr;
6193 unsigned int next_erel;
6194 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6195 Elf_Internal_Shdr *,
6196 Elf_Internal_Rela *));
6198 /* Adjust the reloc addresses and symbol indices. */
6200 irela = internal_relocs;
6201 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6202 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6203 + elf_section_data (o->output_section)->rel_count
6204 + elf_section_data (o->output_section)->rel_count2);
6205 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6207 unsigned long r_symndx;
6208 asection *sec;
6210 if (next_erel == bed->s->int_rels_per_ext_rel)
6212 rel_hash++;
6213 next_erel = 0;
6216 irela->r_offset += o->output_offset;
6218 /* Relocs in an executable have to be virtual addresses. */
6219 if (finfo->info->emitrelocations)
6220 irela->r_offset += o->output_section->vma;
6222 r_symndx = ELF_R_SYM (irela->r_info);
6224 if (r_symndx == 0)
6225 continue;
6227 if (r_symndx >= locsymcount
6228 || (elf_bad_symtab (input_bfd)
6229 && finfo->sections[r_symndx] == NULL))
6231 struct elf_link_hash_entry *rh;
6232 unsigned long indx;
6234 /* This is a reloc against a global symbol. We
6235 have not yet output all the local symbols, so
6236 we do not know the symbol index of any global
6237 symbol. We set the rel_hash entry for this
6238 reloc to point to the global hash table entry
6239 for this symbol. The symbol index is then
6240 set at the end of elf_bfd_final_link. */
6241 indx = r_symndx - extsymoff;
6242 rh = elf_sym_hashes (input_bfd)[indx];
6243 while (rh->root.type == bfd_link_hash_indirect
6244 || rh->root.type == bfd_link_hash_warning)
6245 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6247 /* Setting the index to -2 tells
6248 elf_link_output_extsym that this symbol is
6249 used by a reloc. */
6250 BFD_ASSERT (rh->indx < 0);
6251 rh->indx = -2;
6253 *rel_hash = rh;
6255 continue;
6258 /* This is a reloc against a local symbol. */
6260 *rel_hash = NULL;
6261 isym = finfo->internal_syms + r_symndx;
6262 sec = finfo->sections[r_symndx];
6263 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6265 /* I suppose the backend ought to fill in the
6266 section of any STT_SECTION symbol against a
6267 processor specific section. If we have
6268 discarded a section, the output_section will
6269 be the absolute section. */
6270 if (sec != NULL
6271 && (bfd_is_abs_section (sec)
6272 || (sec->output_section != NULL
6273 && bfd_is_abs_section (sec->output_section))))
6274 r_symndx = 0;
6275 else if (sec == NULL || sec->owner == NULL)
6277 bfd_set_error (bfd_error_bad_value);
6278 return false;
6280 else
6282 r_symndx = sec->output_section->target_index;
6283 BFD_ASSERT (r_symndx != 0);
6286 else
6288 if (finfo->indices[r_symndx] == -1)
6290 unsigned long shlink;
6291 const char *name;
6292 asection *osec;
6294 if (finfo->info->strip == strip_all)
6296 /* You can't do ld -r -s. */
6297 bfd_set_error (bfd_error_invalid_operation);
6298 return false;
6301 /* This symbol was skipped earlier, but
6302 since it is needed by a reloc, we
6303 must output it now. */
6304 shlink = symtab_hdr->sh_link;
6305 name = (bfd_elf_string_from_elf_section
6306 (input_bfd, shlink, isym->st_name));
6307 if (name == NULL)
6308 return false;
6310 osec = sec->output_section;
6311 isym->st_shndx =
6312 _bfd_elf_section_from_bfd_section (output_bfd,
6313 osec);
6314 if (isym->st_shndx == (unsigned short) -1)
6315 return false;
6317 isym->st_value += sec->output_offset;
6318 if (! finfo->info->relocateable)
6319 isym->st_value += osec->vma;
6321 finfo->indices[r_symndx]
6322 = bfd_get_symcount (output_bfd);
6324 if (! elf_link_output_sym (finfo, name, isym, sec))
6325 return false;
6328 r_symndx = finfo->indices[r_symndx];
6331 irela->r_info = ELF_R_INFO (r_symndx,
6332 ELF_R_TYPE (irela->r_info));
6335 /* Swap out the relocs. */
6336 if (bed->elf_backend_emit_relocs
6337 && !(finfo->info->relocateable
6338 || finfo->info->emitrelocations))
6339 reloc_emitter = bed->elf_backend_emit_relocs;
6340 else
6341 reloc_emitter = elf_link_output_relocs;
6343 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6344 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6346 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6347 if (input_rel_hdr)
6349 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6350 * bed->s->int_rels_per_ext_rel);
6351 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6357 /* Write out the modified section contents. */
6358 if (elf_section_data (o)->stab_info)
6360 if (! (_bfd_write_section_stabs
6361 (output_bfd, &elf_hash_table (finfo->info)->stab_info,
6362 o, &elf_section_data (o)->stab_info, contents)))
6363 return false;
6365 else if (elf_section_data (o)->merge_info)
6367 if (! (_bfd_write_merged_section
6368 (output_bfd, o, elf_section_data (o)->merge_info)))
6369 return false;
6371 else
6373 bfd_size_type sec_size;
6375 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6376 if (! (o->flags & SEC_EXCLUDE)
6377 && ! bfd_set_section_contents (output_bfd, o->output_section,
6378 contents,
6379 (file_ptr) o->output_offset,
6380 sec_size))
6381 return false;
6385 return true;
6388 /* Generate a reloc when linking an ELF file. This is a reloc
6389 requested by the linker, and does come from any input file. This
6390 is used to build constructor and destructor tables when linking
6391 with -Ur. */
6393 static boolean
6394 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6395 bfd *output_bfd;
6396 struct bfd_link_info *info;
6397 asection *output_section;
6398 struct bfd_link_order *link_order;
6400 reloc_howto_type *howto;
6401 long indx;
6402 bfd_vma offset;
6403 bfd_vma addend;
6404 struct elf_link_hash_entry **rel_hash_ptr;
6405 Elf_Internal_Shdr *rel_hdr;
6406 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6408 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6409 if (howto == NULL)
6411 bfd_set_error (bfd_error_bad_value);
6412 return false;
6415 addend = link_order->u.reloc.p->addend;
6417 /* Figure out the symbol index. */
6418 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6419 + elf_section_data (output_section)->rel_count
6420 + elf_section_data (output_section)->rel_count2);
6421 if (link_order->type == bfd_section_reloc_link_order)
6423 indx = link_order->u.reloc.p->u.section->target_index;
6424 BFD_ASSERT (indx != 0);
6425 *rel_hash_ptr = NULL;
6427 else
6429 struct elf_link_hash_entry *h;
6431 /* Treat a reloc against a defined symbol as though it were
6432 actually against the section. */
6433 h = ((struct elf_link_hash_entry *)
6434 bfd_wrapped_link_hash_lookup (output_bfd, info,
6435 link_order->u.reloc.p->u.name,
6436 false, false, true));
6437 if (h != NULL
6438 && (h->root.type == bfd_link_hash_defined
6439 || h->root.type == bfd_link_hash_defweak))
6441 asection *section;
6443 section = h->root.u.def.section;
6444 indx = section->output_section->target_index;
6445 *rel_hash_ptr = NULL;
6446 /* It seems that we ought to add the symbol value to the
6447 addend here, but in practice it has already been added
6448 because it was passed to constructor_callback. */
6449 addend += section->output_section->vma + section->output_offset;
6451 else if (h != NULL)
6453 /* Setting the index to -2 tells elf_link_output_extsym that
6454 this symbol is used by a reloc. */
6455 h->indx = -2;
6456 *rel_hash_ptr = h;
6457 indx = 0;
6459 else
6461 if (! ((*info->callbacks->unattached_reloc)
6462 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6463 (asection *) NULL, (bfd_vma) 0)))
6464 return false;
6465 indx = 0;
6469 /* If this is an inplace reloc, we must write the addend into the
6470 object file. */
6471 if (howto->partial_inplace && addend != 0)
6473 bfd_size_type size;
6474 bfd_reloc_status_type rstat;
6475 bfd_byte *buf;
6476 boolean ok;
6477 const char *sym_name;
6479 size = bfd_get_reloc_size (howto);
6480 buf = (bfd_byte *) bfd_zmalloc (size);
6481 if (buf == (bfd_byte *) NULL)
6482 return false;
6483 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6484 switch (rstat)
6486 case bfd_reloc_ok:
6487 break;
6489 default:
6490 case bfd_reloc_outofrange:
6491 abort ();
6493 case bfd_reloc_overflow:
6494 if (link_order->type == bfd_section_reloc_link_order)
6495 sym_name = bfd_section_name (output_bfd,
6496 link_order->u.reloc.p->u.section);
6497 else
6498 sym_name = link_order->u.reloc.p->u.name;
6499 if (! ((*info->callbacks->reloc_overflow)
6500 (info, sym_name, howto->name, addend,
6501 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6503 free (buf);
6504 return false;
6506 break;
6508 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6509 (file_ptr) link_order->offset, size);
6510 free (buf);
6511 if (! ok)
6512 return false;
6515 /* The address of a reloc is relative to the section in a
6516 relocateable file, and is a virtual address in an executable
6517 file. */
6518 offset = link_order->offset;
6519 if (! info->relocateable)
6520 offset += output_section->vma;
6522 rel_hdr = &elf_section_data (output_section)->rel_hdr;
6524 if (rel_hdr->sh_type == SHT_REL)
6526 bfd_size_type size;
6527 Elf_Internal_Rel *irel;
6528 Elf_External_Rel *erel;
6529 unsigned int i;
6531 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6532 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
6533 if (irel == NULL)
6534 return false;
6536 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6537 irel[i].r_offset = offset;
6538 irel[0].r_info = ELF_R_INFO (indx, howto->type);
6540 erel = ((Elf_External_Rel *) rel_hdr->contents
6541 + elf_section_data (output_section)->rel_count);
6543 if (bed->s->swap_reloc_out)
6544 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
6545 else
6546 elf_swap_reloc_out (output_bfd, irel, erel);
6548 free (irel);
6550 else
6552 bfd_size_type size;
6553 Elf_Internal_Rela *irela;
6554 Elf_External_Rela *erela;
6555 unsigned int i;
6557 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
6558 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
6559 if (irela == NULL)
6560 return false;
6562 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6563 irela[i].r_offset = offset;
6564 irela[0].r_info = ELF_R_INFO (indx, howto->type);
6565 irela[0].r_addend = addend;
6567 erela = ((Elf_External_Rela *) rel_hdr->contents
6568 + elf_section_data (output_section)->rel_count);
6570 if (bed->s->swap_reloca_out)
6571 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
6572 else
6573 elf_swap_reloca_out (output_bfd, irela, erela);
6576 ++elf_section_data (output_section)->rel_count;
6578 return true;
6581 /* Allocate a pointer to live in a linker created section. */
6583 boolean
6584 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
6585 bfd *abfd;
6586 struct bfd_link_info *info;
6587 elf_linker_section_t *lsect;
6588 struct elf_link_hash_entry *h;
6589 const Elf_Internal_Rela *rel;
6591 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
6592 elf_linker_section_pointers_t *linker_section_ptr;
6593 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6594 bfd_size_type amt;
6596 BFD_ASSERT (lsect != NULL);
6598 /* Is this a global symbol? */
6599 if (h != NULL)
6601 /* Has this symbol already been allocated? If so, our work is done. */
6602 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
6603 rel->r_addend,
6604 lsect->which))
6605 return true;
6607 ptr_linker_section_ptr = &h->linker_section_pointer;
6608 /* Make sure this symbol is output as a dynamic symbol. */
6609 if (h->dynindx == -1)
6611 if (! elf_link_record_dynamic_symbol (info, h))
6612 return false;
6615 if (lsect->rel_section)
6616 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6618 else
6620 /* Allocation of a pointer to a local symbol. */
6621 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
6623 /* Allocate a table to hold the local symbols if first time. */
6624 if (!ptr)
6626 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
6627 register unsigned int i;
6629 amt = num_symbols;
6630 amt *= sizeof (elf_linker_section_pointers_t *);
6631 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
6633 if (!ptr)
6634 return false;
6636 elf_local_ptr_offsets (abfd) = ptr;
6637 for (i = 0; i < num_symbols; i++)
6638 ptr[i] = (elf_linker_section_pointers_t *) 0;
6641 /* Has this symbol already been allocated? If so, our work is done. */
6642 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
6643 rel->r_addend,
6644 lsect->which))
6645 return true;
6647 ptr_linker_section_ptr = &ptr[r_symndx];
6649 if (info->shared)
6651 /* If we are generating a shared object, we need to
6652 output a R_<xxx>_RELATIVE reloc so that the
6653 dynamic linker can adjust this GOT entry. */
6654 BFD_ASSERT (lsect->rel_section != NULL);
6655 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6659 /* Allocate space for a pointer in the linker section, and allocate
6660 a new pointer record from internal memory. */
6661 BFD_ASSERT (ptr_linker_section_ptr != NULL);
6662 amt = sizeof (elf_linker_section_pointers_t);
6663 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
6665 if (!linker_section_ptr)
6666 return false;
6668 linker_section_ptr->next = *ptr_linker_section_ptr;
6669 linker_section_ptr->addend = rel->r_addend;
6670 linker_section_ptr->which = lsect->which;
6671 linker_section_ptr->written_address_p = false;
6672 *ptr_linker_section_ptr = linker_section_ptr;
6674 #if 0
6675 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
6677 linker_section_ptr->offset = (lsect->section->_raw_size
6678 - lsect->hole_size + (ARCH_SIZE / 8));
6679 lsect->hole_offset += ARCH_SIZE / 8;
6680 lsect->sym_offset += ARCH_SIZE / 8;
6681 if (lsect->sym_hash)
6683 /* Bump up symbol value if needed. */
6684 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
6685 #ifdef DEBUG
6686 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
6687 lsect->sym_hash->root.root.string,
6688 (long) ARCH_SIZE / 8,
6689 (long) lsect->sym_hash->root.u.def.value);
6690 #endif
6693 else
6694 #endif
6695 linker_section_ptr->offset = lsect->section->_raw_size;
6697 lsect->section->_raw_size += ARCH_SIZE / 8;
6699 #ifdef DEBUG
6700 fprintf (stderr,
6701 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6702 lsect->name, (long) linker_section_ptr->offset,
6703 (long) lsect->section->_raw_size);
6704 #endif
6706 return true;
6709 #if ARCH_SIZE==64
6710 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6711 #endif
6712 #if ARCH_SIZE==32
6713 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6714 #endif
6716 /* Fill in the address for a pointer generated in a linker section. */
6718 bfd_vma
6719 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
6720 relocation, rel, relative_reloc)
6721 bfd *output_bfd;
6722 bfd *input_bfd;
6723 struct bfd_link_info *info;
6724 elf_linker_section_t *lsect;
6725 struct elf_link_hash_entry *h;
6726 bfd_vma relocation;
6727 const Elf_Internal_Rela *rel;
6728 int relative_reloc;
6730 elf_linker_section_pointers_t *linker_section_ptr;
6732 BFD_ASSERT (lsect != NULL);
6734 if (h != NULL)
6736 /* Handle global symbol. */
6737 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
6738 (h->linker_section_pointer,
6739 rel->r_addend,
6740 lsect->which));
6742 BFD_ASSERT (linker_section_ptr != NULL);
6744 if (! elf_hash_table (info)->dynamic_sections_created
6745 || (info->shared
6746 && info->symbolic
6747 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6749 /* This is actually a static link, or it is a
6750 -Bsymbolic link and the symbol is defined
6751 locally. We must initialize this entry in the
6752 global section.
6754 When doing a dynamic link, we create a .rela.<xxx>
6755 relocation entry to initialize the value. This
6756 is done in the finish_dynamic_symbol routine. */
6757 if (!linker_section_ptr->written_address_p)
6759 linker_section_ptr->written_address_p = true;
6760 bfd_put_ptr (output_bfd,
6761 relocation + linker_section_ptr->addend,
6762 (lsect->section->contents
6763 + linker_section_ptr->offset));
6767 else
6769 /* Handle local symbol. */
6770 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6771 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6772 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
6773 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
6774 (elf_local_ptr_offsets (input_bfd)[r_symndx],
6775 rel->r_addend,
6776 lsect->which));
6778 BFD_ASSERT (linker_section_ptr != NULL);
6780 /* Write out pointer if it hasn't been rewritten out before. */
6781 if (!linker_section_ptr->written_address_p)
6783 linker_section_ptr->written_address_p = true;
6784 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
6785 lsect->section->contents + linker_section_ptr->offset);
6787 if (info->shared)
6789 asection *srel = lsect->rel_section;
6790 Elf_Internal_Rela *outrel;
6791 Elf_External_Rela *erel;
6792 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6793 unsigned int i;
6794 bfd_size_type amt;
6796 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
6797 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
6798 if (outrel == NULL)
6800 (*_bfd_error_handler) (_("Error: out of memory"));
6801 return 0;
6804 /* We need to generate a relative reloc for the dynamic
6805 linker. */
6806 if (!srel)
6808 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
6809 lsect->rel_name);
6810 lsect->rel_section = srel;
6813 BFD_ASSERT (srel != NULL);
6815 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6816 outrel[i].r_offset = (lsect->section->output_section->vma
6817 + lsect->section->output_offset
6818 + linker_section_ptr->offset);
6819 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
6820 outrel[0].r_addend = 0;
6821 erel = (Elf_External_Rela *) lsect->section->contents;
6822 erel += elf_section_data (lsect->section)->rel_count;
6823 elf_swap_reloca_out (output_bfd, outrel, erel);
6824 ++elf_section_data (lsect->section)->rel_count;
6826 free (outrel);
6831 relocation = (lsect->section->output_offset
6832 + linker_section_ptr->offset
6833 - lsect->hole_offset
6834 - lsect->sym_offset);
6836 #ifdef DEBUG
6837 fprintf (stderr,
6838 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6839 lsect->name, (long) relocation, (long) relocation);
6840 #endif
6842 /* Subtract out the addend, because it will get added back in by the normal
6843 processing. */
6844 return relocation - linker_section_ptr->addend;
6847 /* Garbage collect unused sections. */
6849 static boolean elf_gc_mark
6850 PARAMS ((struct bfd_link_info *info, asection *sec,
6851 asection * (*gc_mark_hook)
6852 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
6853 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
6855 static boolean elf_gc_sweep
6856 PARAMS ((struct bfd_link_info *info,
6857 boolean (*gc_sweep_hook)
6858 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
6859 const Elf_Internal_Rela *relocs))));
6861 static boolean elf_gc_sweep_symbol
6862 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
6864 static boolean elf_gc_allocate_got_offsets
6865 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
6867 static boolean elf_gc_propagate_vtable_entries_used
6868 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
6870 static boolean elf_gc_smash_unused_vtentry_relocs
6871 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
6873 /* The mark phase of garbage collection. For a given section, mark
6874 it, and all the sections which define symbols to which it refers. */
6876 static boolean
6877 elf_gc_mark (info, sec, gc_mark_hook)
6878 struct bfd_link_info *info;
6879 asection *sec;
6880 asection * (*gc_mark_hook)
6881 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
6882 struct elf_link_hash_entry *, Elf_Internal_Sym *));
6884 boolean ret = true;
6886 sec->gc_mark = 1;
6888 /* Look through the section relocs. */
6890 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
6892 Elf_Internal_Rela *relstart, *rel, *relend;
6893 Elf_Internal_Shdr *symtab_hdr;
6894 struct elf_link_hash_entry **sym_hashes;
6895 size_t nlocsyms;
6896 size_t extsymoff;
6897 Elf_External_Sym *locsyms, *freesyms = NULL;
6898 bfd *input_bfd = sec->owner;
6899 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6901 /* GCFIXME: how to arrange so that relocs and symbols are not
6902 reread continually? */
6904 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6905 sym_hashes = elf_sym_hashes (input_bfd);
6907 /* Read the local symbols. */
6908 if (elf_bad_symtab (input_bfd))
6910 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6911 extsymoff = 0;
6913 else
6914 extsymoff = nlocsyms = symtab_hdr->sh_info;
6915 if (symtab_hdr->contents)
6916 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
6917 else if (nlocsyms == 0)
6918 locsyms = NULL;
6919 else
6921 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
6922 locsyms = freesyms = bfd_malloc (amt);
6923 if (freesyms == NULL
6924 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6925 || bfd_bread (locsyms, amt, input_bfd) != amt)
6927 ret = false;
6928 goto out1;
6932 /* Read the relocations. */
6933 relstart = (NAME(_bfd_elf,link_read_relocs)
6934 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
6935 info->keep_memory));
6936 if (relstart == NULL)
6938 ret = false;
6939 goto out1;
6941 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6943 for (rel = relstart; rel < relend; rel++)
6945 unsigned long r_symndx;
6946 asection *rsec;
6947 struct elf_link_hash_entry *h;
6948 Elf_Internal_Sym s;
6950 r_symndx = ELF_R_SYM (rel->r_info);
6951 if (r_symndx == 0)
6952 continue;
6954 if (elf_bad_symtab (sec->owner))
6956 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6957 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
6958 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
6959 else
6961 h = sym_hashes[r_symndx - extsymoff];
6962 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
6965 else if (r_symndx >= nlocsyms)
6967 h = sym_hashes[r_symndx - extsymoff];
6968 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
6970 else
6972 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6973 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
6976 if (rsec && !rsec->gc_mark)
6977 if (!elf_gc_mark (info, rsec, gc_mark_hook))
6979 ret = false;
6980 goto out2;
6984 out2:
6985 if (!info->keep_memory)
6986 free (relstart);
6987 out1:
6988 if (freesyms)
6989 free (freesyms);
6992 return ret;
6995 /* The sweep phase of garbage collection. Remove all garbage sections. */
6997 static boolean
6998 elf_gc_sweep (info, gc_sweep_hook)
6999 struct bfd_link_info *info;
7000 boolean (*gc_sweep_hook)
7001 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7002 const Elf_Internal_Rela *relocs));
7004 bfd *sub;
7006 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7008 asection *o;
7010 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7011 continue;
7013 for (o = sub->sections; o != NULL; o = o->next)
7015 /* Keep special sections. Keep .debug sections. */
7016 if ((o->flags & SEC_LINKER_CREATED)
7017 || (o->flags & SEC_DEBUGGING))
7018 o->gc_mark = 1;
7020 if (o->gc_mark)
7021 continue;
7023 /* Skip sweeping sections already excluded. */
7024 if (o->flags & SEC_EXCLUDE)
7025 continue;
7027 /* Since this is early in the link process, it is simple
7028 to remove a section from the output. */
7029 o->flags |= SEC_EXCLUDE;
7031 /* But we also have to update some of the relocation
7032 info we collected before. */
7033 if (gc_sweep_hook
7034 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7036 Elf_Internal_Rela *internal_relocs;
7037 boolean r;
7039 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7040 (o->owner, o, NULL, NULL, info->keep_memory));
7041 if (internal_relocs == NULL)
7042 return false;
7044 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7046 if (!info->keep_memory)
7047 free (internal_relocs);
7049 if (!r)
7050 return false;
7055 /* Remove the symbols that were in the swept sections from the dynamic
7056 symbol table. GCFIXME: Anyone know how to get them out of the
7057 static symbol table as well? */
7059 int i = 0;
7061 elf_link_hash_traverse (elf_hash_table (info),
7062 elf_gc_sweep_symbol,
7063 (PTR) &i);
7065 elf_hash_table (info)->dynsymcount = i;
7068 return true;
7071 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7073 static boolean
7074 elf_gc_sweep_symbol (h, idxptr)
7075 struct elf_link_hash_entry *h;
7076 PTR idxptr;
7078 int *idx = (int *) idxptr;
7080 if (h->dynindx != -1
7081 && ((h->root.type != bfd_link_hash_defined
7082 && h->root.type != bfd_link_hash_defweak)
7083 || h->root.u.def.section->gc_mark))
7084 h->dynindx = (*idx)++;
7086 return true;
7089 /* Propogate collected vtable information. This is called through
7090 elf_link_hash_traverse. */
7092 static boolean
7093 elf_gc_propagate_vtable_entries_used (h, okp)
7094 struct elf_link_hash_entry *h;
7095 PTR okp;
7097 /* Those that are not vtables. */
7098 if (h->vtable_parent == NULL)
7099 return true;
7101 /* Those vtables that do not have parents, we cannot merge. */
7102 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7103 return true;
7105 /* If we've already been done, exit. */
7106 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7107 return true;
7109 /* Make sure the parent's table is up to date. */
7110 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7112 if (h->vtable_entries_used == NULL)
7114 /* None of this table's entries were referenced. Re-use the
7115 parent's table. */
7116 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7117 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7119 else
7121 size_t n;
7122 boolean *cu, *pu;
7124 /* Or the parent's entries into ours. */
7125 cu = h->vtable_entries_used;
7126 cu[-1] = true;
7127 pu = h->vtable_parent->vtable_entries_used;
7128 if (pu != NULL)
7130 asection *sec = h->root.u.def.section;
7131 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7132 int file_align = bed->s->file_align;
7134 n = h->vtable_parent->vtable_entries_size / file_align;
7135 while (n--)
7137 if (*pu)
7138 *cu = true;
7139 pu++;
7140 cu++;
7145 return true;
7148 static boolean
7149 elf_gc_smash_unused_vtentry_relocs (h, okp)
7150 struct elf_link_hash_entry *h;
7151 PTR okp;
7153 asection *sec;
7154 bfd_vma hstart, hend;
7155 Elf_Internal_Rela *relstart, *relend, *rel;
7156 struct elf_backend_data *bed;
7157 int file_align;
7159 /* Take care of both those symbols that do not describe vtables as
7160 well as those that are not loaded. */
7161 if (h->vtable_parent == NULL)
7162 return true;
7164 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7165 || h->root.type == bfd_link_hash_defweak);
7167 sec = h->root.u.def.section;
7168 hstart = h->root.u.def.value;
7169 hend = hstart + h->size;
7171 relstart = (NAME(_bfd_elf,link_read_relocs)
7172 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7173 if (!relstart)
7174 return *(boolean *) okp = false;
7175 bed = get_elf_backend_data (sec->owner);
7176 file_align = bed->s->file_align;
7178 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7180 for (rel = relstart; rel < relend; ++rel)
7181 if (rel->r_offset >= hstart && rel->r_offset < hend)
7183 /* If the entry is in use, do nothing. */
7184 if (h->vtable_entries_used
7185 && (rel->r_offset - hstart) < h->vtable_entries_size)
7187 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7188 if (h->vtable_entries_used[entry])
7189 continue;
7191 /* Otherwise, kill it. */
7192 rel->r_offset = rel->r_info = rel->r_addend = 0;
7195 return true;
7198 /* Do mark and sweep of unused sections. */
7200 boolean
7201 elf_gc_sections (abfd, info)
7202 bfd *abfd;
7203 struct bfd_link_info *info;
7205 boolean ok = true;
7206 bfd *sub;
7207 asection * (*gc_mark_hook)
7208 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7209 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7211 if (!get_elf_backend_data (abfd)->can_gc_sections
7212 || info->relocateable || info->emitrelocations
7213 || elf_hash_table (info)->dynamic_sections_created)
7214 return true;
7216 /* Apply transitive closure to the vtable entry usage info. */
7217 elf_link_hash_traverse (elf_hash_table (info),
7218 elf_gc_propagate_vtable_entries_used,
7219 (PTR) &ok);
7220 if (!ok)
7221 return false;
7223 /* Kill the vtable relocations that were not used. */
7224 elf_link_hash_traverse (elf_hash_table (info),
7225 elf_gc_smash_unused_vtentry_relocs,
7226 (PTR) &ok);
7227 if (!ok)
7228 return false;
7230 /* Grovel through relocs to find out who stays ... */
7232 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7233 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7235 asection *o;
7237 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7238 continue;
7240 for (o = sub->sections; o != NULL; o = o->next)
7242 if (o->flags & SEC_KEEP)
7243 if (!elf_gc_mark (info, o, gc_mark_hook))
7244 return false;
7248 /* ... and mark SEC_EXCLUDE for those that go. */
7249 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7250 return false;
7252 return true;
7255 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7257 boolean
7258 elf_gc_record_vtinherit (abfd, sec, h, offset)
7259 bfd *abfd;
7260 asection *sec;
7261 struct elf_link_hash_entry *h;
7262 bfd_vma offset;
7264 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7265 struct elf_link_hash_entry **search, *child;
7266 bfd_size_type extsymcount;
7268 /* The sh_info field of the symtab header tells us where the
7269 external symbols start. We don't care about the local symbols at
7270 this point. */
7271 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7272 if (!elf_bad_symtab (abfd))
7273 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7275 sym_hashes = elf_sym_hashes (abfd);
7276 sym_hashes_end = sym_hashes + extsymcount;
7278 /* Hunt down the child symbol, which is in this section at the same
7279 offset as the relocation. */
7280 for (search = sym_hashes; search != sym_hashes_end; ++search)
7282 if ((child = *search) != NULL
7283 && (child->root.type == bfd_link_hash_defined
7284 || child->root.type == bfd_link_hash_defweak)
7285 && child->root.u.def.section == sec
7286 && child->root.u.def.value == offset)
7287 goto win;
7290 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7291 bfd_archive_filename (abfd), sec->name,
7292 (unsigned long) offset);
7293 bfd_set_error (bfd_error_invalid_operation);
7294 return false;
7296 win:
7297 if (!h)
7299 /* This *should* only be the absolute section. It could potentially
7300 be that someone has defined a non-global vtable though, which
7301 would be bad. It isn't worth paging in the local symbols to be
7302 sure though; that case should simply be handled by the assembler. */
7304 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7306 else
7307 child->vtable_parent = h;
7309 return true;
7312 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7314 boolean
7315 elf_gc_record_vtentry (abfd, sec, h, addend)
7316 bfd *abfd ATTRIBUTE_UNUSED;
7317 asection *sec ATTRIBUTE_UNUSED;
7318 struct elf_link_hash_entry *h;
7319 bfd_vma addend;
7321 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7322 int file_align = bed->s->file_align;
7324 if (addend >= h->vtable_entries_size)
7326 size_t size, bytes;
7327 boolean *ptr = h->vtable_entries_used;
7329 /* While the symbol is undefined, we have to be prepared to handle
7330 a zero size. */
7331 if (h->root.type == bfd_link_hash_undefined)
7332 size = addend;
7333 else
7335 size = h->size;
7336 if (size < addend)
7338 /* Oops! We've got a reference past the defined end of
7339 the table. This is probably a bug -- shall we warn? */
7340 size = addend;
7344 /* Allocate one extra entry for use as a "done" flag for the
7345 consolidation pass. */
7346 bytes = (size / file_align + 1) * sizeof (boolean);
7348 if (ptr)
7350 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7352 if (ptr != NULL)
7354 size_t oldbytes;
7356 oldbytes = ((h->vtable_entries_size / file_align + 1)
7357 * sizeof (boolean));
7358 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7361 else
7362 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7364 if (ptr == NULL)
7365 return false;
7367 /* And arrange for that done flag to be at index -1. */
7368 h->vtable_entries_used = ptr + 1;
7369 h->vtable_entries_size = size;
7372 h->vtable_entries_used[addend / file_align] = true;
7374 return true;
7377 /* And an accompanying bit to work out final got entry offsets once
7378 we're done. Should be called from final_link. */
7380 boolean
7381 elf_gc_common_finalize_got_offsets (abfd, info)
7382 bfd *abfd;
7383 struct bfd_link_info *info;
7385 bfd *i;
7386 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7387 bfd_vma gotoff;
7389 /* The GOT offset is relative to the .got section, but the GOT header is
7390 put into the .got.plt section, if the backend uses it. */
7391 if (bed->want_got_plt)
7392 gotoff = 0;
7393 else
7394 gotoff = bed->got_header_size;
7396 /* Do the local .got entries first. */
7397 for (i = info->input_bfds; i; i = i->link_next)
7399 bfd_signed_vma *local_got;
7400 bfd_size_type j, locsymcount;
7401 Elf_Internal_Shdr *symtab_hdr;
7403 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7404 continue;
7406 local_got = elf_local_got_refcounts (i);
7407 if (!local_got)
7408 continue;
7410 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7411 if (elf_bad_symtab (i))
7412 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7413 else
7414 locsymcount = symtab_hdr->sh_info;
7416 for (j = 0; j < locsymcount; ++j)
7418 if (local_got[j] > 0)
7420 local_got[j] = gotoff;
7421 gotoff += ARCH_SIZE / 8;
7423 else
7424 local_got[j] = (bfd_vma) -1;
7428 /* Then the global .got entries. .plt refcounts are handled by
7429 adjust_dynamic_symbol */
7430 elf_link_hash_traverse (elf_hash_table (info),
7431 elf_gc_allocate_got_offsets,
7432 (PTR) &gotoff);
7433 return true;
7436 /* We need a special top-level link routine to convert got reference counts
7437 to real got offsets. */
7439 static boolean
7440 elf_gc_allocate_got_offsets (h, offarg)
7441 struct elf_link_hash_entry *h;
7442 PTR offarg;
7444 bfd_vma *off = (bfd_vma *) offarg;
7446 if (h->got.refcount > 0)
7448 h->got.offset = off[0];
7449 off[0] += ARCH_SIZE / 8;
7451 else
7452 h->got.offset = (bfd_vma) -1;
7454 return true;
7457 /* Many folk need no more in the way of final link than this, once
7458 got entry reference counting is enabled. */
7460 boolean
7461 elf_gc_common_final_link (abfd, info)
7462 bfd *abfd;
7463 struct bfd_link_info *info;
7465 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7466 return false;
7468 /* Invoke the regular ELF backend linker to do all the work. */
7469 return elf_bfd_final_link (abfd, info);
7472 /* This function will be called though elf_link_hash_traverse to store
7473 all hash value of the exported symbols in an array. */
7475 static boolean
7476 elf_collect_hash_codes (h, data)
7477 struct elf_link_hash_entry *h;
7478 PTR data;
7480 unsigned long **valuep = (unsigned long **) data;
7481 const char *name;
7482 char *p;
7483 unsigned long ha;
7484 char *alc = NULL;
7486 /* Ignore indirect symbols. These are added by the versioning code. */
7487 if (h->dynindx == -1)
7488 return true;
7490 name = h->root.root.string;
7491 p = strchr (name, ELF_VER_CHR);
7492 if (p != NULL)
7494 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
7495 memcpy (alc, name, (size_t) (p - name));
7496 alc[p - name] = '\0';
7497 name = alc;
7500 /* Compute the hash value. */
7501 ha = bfd_elf_hash (name);
7503 /* Store the found hash value in the array given as the argument. */
7504 *(*valuep)++ = ha;
7506 /* And store it in the struct so that we can put it in the hash table
7507 later. */
7508 h->elf_hash_value = ha;
7510 if (alc != NULL)
7511 free (alc);
7513 return true;