* ieee.c (ieee_slurp_debug): Get the length of the debug
[binutils.git] / bfd / elflink.h
blobab679a70d0d3cd6da375a0998dc81f48628e15c3
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* ELF linker code. */
22 /* This struct is used to pass information to routines called via
23 elf_link_hash_traverse which must return failure. */
25 struct elf_info_failed
27 boolean failed;
28 struct bfd_link_info *info;
31 static boolean elf_link_add_object_symbols
32 PARAMS ((bfd *, struct bfd_link_info *));
33 static boolean elf_link_add_archive_symbols
34 PARAMS ((bfd *, struct bfd_link_info *));
35 static boolean elf_merge_symbol
36 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
37 asection **, bfd_vma *, struct elf_link_hash_entry **,
38 boolean *, boolean *, boolean *));
39 static boolean elf_export_symbol
40 PARAMS ((struct elf_link_hash_entry *, PTR));
41 static boolean elf_fix_symbol_flags
42 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
43 static boolean elf_adjust_dynamic_symbol
44 PARAMS ((struct elf_link_hash_entry *, PTR));
45 static boolean elf_link_find_version_dependencies
46 PARAMS ((struct elf_link_hash_entry *, PTR));
47 static boolean elf_link_find_version_dependencies
48 PARAMS ((struct elf_link_hash_entry *, PTR));
49 static boolean elf_link_assign_sym_version
50 PARAMS ((struct elf_link_hash_entry *, PTR));
51 static boolean elf_collect_hash_codes
52 PARAMS ((struct elf_link_hash_entry *, PTR));
53 static boolean elf_link_read_relocs_from_section
54 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
55 static void elf_link_output_relocs
56 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
57 static boolean elf_link_size_reloc_section
58 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
59 static void elf_link_adjust_relocs
60 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
61 struct elf_link_hash_entry **));
63 /* Given an ELF BFD, add symbols to the global hash table as
64 appropriate. */
66 boolean
67 elf_bfd_link_add_symbols (abfd, info)
68 bfd *abfd;
69 struct bfd_link_info *info;
71 switch (bfd_get_format (abfd))
73 case bfd_object:
74 return elf_link_add_object_symbols (abfd, info);
75 case bfd_archive:
76 return elf_link_add_archive_symbols (abfd, info);
77 default:
78 bfd_set_error (bfd_error_wrong_format);
79 return false;
84 /* Add symbols from an ELF archive file to the linker hash table. We
85 don't use _bfd_generic_link_add_archive_symbols because of a
86 problem which arises on UnixWare. The UnixWare libc.so is an
87 archive which includes an entry libc.so.1 which defines a bunch of
88 symbols. The libc.so archive also includes a number of other
89 object files, which also define symbols, some of which are the same
90 as those defined in libc.so.1. Correct linking requires that we
91 consider each object file in turn, and include it if it defines any
92 symbols we need. _bfd_generic_link_add_archive_symbols does not do
93 this; it looks through the list of undefined symbols, and includes
94 any object file which defines them. When this algorithm is used on
95 UnixWare, it winds up pulling in libc.so.1 early and defining a
96 bunch of symbols. This means that some of the other objects in the
97 archive are not included in the link, which is incorrect since they
98 precede libc.so.1 in the archive.
100 Fortunately, ELF archive handling is simpler than that done by
101 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
102 oddities. In ELF, if we find a symbol in the archive map, and the
103 symbol is currently undefined, we know that we must pull in that
104 object file.
106 Unfortunately, we do have to make multiple passes over the symbol
107 table until nothing further is resolved. */
109 static boolean
110 elf_link_add_archive_symbols (abfd, info)
111 bfd *abfd;
112 struct bfd_link_info *info;
114 symindex c;
115 boolean *defined = NULL;
116 boolean *included = NULL;
117 carsym *symdefs;
118 boolean loop;
120 if (! bfd_has_map (abfd))
122 /* An empty archive is a special case. */
123 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
124 return true;
125 bfd_set_error (bfd_error_no_armap);
126 return false;
129 /* Keep track of all symbols we know to be already defined, and all
130 files we know to be already included. This is to speed up the
131 second and subsequent passes. */
132 c = bfd_ardata (abfd)->symdef_count;
133 if (c == 0)
134 return true;
135 defined = (boolean *) bfd_malloc (c * sizeof (boolean));
136 included = (boolean *) bfd_malloc (c * sizeof (boolean));
137 if (defined == (boolean *) NULL || included == (boolean *) NULL)
138 goto error_return;
139 memset (defined, 0, c * sizeof (boolean));
140 memset (included, 0, c * sizeof (boolean));
142 symdefs = bfd_ardata (abfd)->symdefs;
146 file_ptr last;
147 symindex i;
148 carsym *symdef;
149 carsym *symdefend;
151 loop = false;
152 last = -1;
154 symdef = symdefs;
155 symdefend = symdef + c;
156 for (i = 0; symdef < symdefend; symdef++, i++)
158 struct elf_link_hash_entry *h;
159 bfd *element;
160 struct bfd_link_hash_entry *undefs_tail;
161 symindex mark;
163 if (defined[i] || included[i])
164 continue;
165 if (symdef->file_offset == last)
167 included[i] = true;
168 continue;
171 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
172 false, false, false);
174 if (h == NULL)
176 char *p, *copy;
178 /* If this is a default version (the name contains @@),
179 look up the symbol again without the version. The
180 effect is that references to the symbol without the
181 version will be matched by the default symbol in the
182 archive. */
184 p = strchr (symdef->name, ELF_VER_CHR);
185 if (p == NULL || p[1] != ELF_VER_CHR)
186 continue;
188 copy = bfd_alloc (abfd, p - symdef->name + 1);
189 if (copy == NULL)
190 goto error_return;
191 memcpy (copy, symdef->name, p - symdef->name);
192 copy[p - symdef->name] = '\0';
194 h = elf_link_hash_lookup (elf_hash_table (info), copy,
195 false, false, false);
197 bfd_release (abfd, copy);
200 if (h == NULL)
201 continue;
203 if (h->root.type != bfd_link_hash_undefined)
205 if (h->root.type != bfd_link_hash_undefweak)
206 defined[i] = true;
207 continue;
210 /* We need to include this archive member. */
212 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
213 if (element == (bfd *) NULL)
214 goto error_return;
216 if (! bfd_check_format (element, bfd_object))
217 goto error_return;
219 /* Doublecheck that we have not included this object
220 already--it should be impossible, but there may be
221 something wrong with the archive. */
222 if (element->archive_pass != 0)
224 bfd_set_error (bfd_error_bad_value);
225 goto error_return;
227 element->archive_pass = 1;
229 undefs_tail = info->hash->undefs_tail;
231 if (! (*info->callbacks->add_archive_element) (info, element,
232 symdef->name))
233 goto error_return;
234 if (! elf_link_add_object_symbols (element, info))
235 goto error_return;
237 /* If there are any new undefined symbols, we need to make
238 another pass through the archive in order to see whether
239 they can be defined. FIXME: This isn't perfect, because
240 common symbols wind up on undefs_tail and because an
241 undefined symbol which is defined later on in this pass
242 does not require another pass. This isn't a bug, but it
243 does make the code less efficient than it could be. */
244 if (undefs_tail != info->hash->undefs_tail)
245 loop = true;
247 /* Look backward to mark all symbols from this object file
248 which we have already seen in this pass. */
249 mark = i;
252 included[mark] = true;
253 if (mark == 0)
254 break;
255 --mark;
257 while (symdefs[mark].file_offset == symdef->file_offset);
259 /* We mark subsequent symbols from this object file as we go
260 on through the loop. */
261 last = symdef->file_offset;
264 while (loop);
266 free (defined);
267 free (included);
269 return true;
271 error_return:
272 if (defined != (boolean *) NULL)
273 free (defined);
274 if (included != (boolean *) NULL)
275 free (included);
276 return false;
279 /* This function is called when we want to define a new symbol. It
280 handles the various cases which arise when we find a definition in
281 a dynamic object, or when there is already a definition in a
282 dynamic object. The new symbol is described by NAME, SYM, PSEC,
283 and PVALUE. We set SYM_HASH to the hash table entry. We set
284 OVERRIDE if the old symbol is overriding a new definition. We set
285 TYPE_CHANGE_OK if it is OK for the type to change. We set
286 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
287 change, we mean that we shouldn't warn if the type or size does
288 change. */
290 static boolean
291 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
292 override, type_change_ok, size_change_ok)
293 bfd *abfd;
294 struct bfd_link_info *info;
295 const char *name;
296 Elf_Internal_Sym *sym;
297 asection **psec;
298 bfd_vma *pvalue;
299 struct elf_link_hash_entry **sym_hash;
300 boolean *override;
301 boolean *type_change_ok;
302 boolean *size_change_ok;
304 asection *sec;
305 struct elf_link_hash_entry *h;
306 int bind;
307 bfd *oldbfd;
308 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
310 *override = false;
312 sec = *psec;
313 bind = ELF_ST_BIND (sym->st_info);
315 if (! bfd_is_und_section (sec))
316 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
317 else
318 h = ((struct elf_link_hash_entry *)
319 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
320 if (h == NULL)
321 return false;
322 *sym_hash = h;
324 /* This code is for coping with dynamic objects, and is only useful
325 if we are doing an ELF link. */
326 if (info->hash->creator != abfd->xvec)
327 return true;
329 /* For merging, we only care about real symbols. */
331 while (h->root.type == bfd_link_hash_indirect
332 || h->root.type == bfd_link_hash_warning)
333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
335 /* If we just created the symbol, mark it as being an ELF symbol.
336 Other than that, there is nothing to do--there is no merge issue
337 with a newly defined symbol--so we just return. */
339 if (h->root.type == bfd_link_hash_new)
341 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
342 return true;
345 /* OLDBFD is a BFD associated with the existing symbol. */
347 switch (h->root.type)
349 default:
350 oldbfd = NULL;
351 break;
353 case bfd_link_hash_undefined:
354 case bfd_link_hash_undefweak:
355 oldbfd = h->root.u.undef.abfd;
356 break;
358 case bfd_link_hash_defined:
359 case bfd_link_hash_defweak:
360 oldbfd = h->root.u.def.section->owner;
361 break;
363 case bfd_link_hash_common:
364 oldbfd = h->root.u.c.p->section->owner;
365 break;
368 /* In cases involving weak versioned symbols, we may wind up trying
369 to merge a symbol with itself. Catch that here, to avoid the
370 confusion that results if we try to override a symbol with
371 itself. The additional tests catch cases like
372 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
373 dynamic object, which we do want to handle here. */
374 if (abfd == oldbfd
375 && ((abfd->flags & DYNAMIC) == 0
376 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
377 return true;
379 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
380 respectively, is from a dynamic object. */
382 if ((abfd->flags & DYNAMIC) != 0)
383 newdyn = true;
384 else
385 newdyn = false;
387 if (oldbfd != NULL)
388 olddyn = (oldbfd->flags & DYNAMIC) != 0;
389 else
391 asection *hsec;
393 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
394 indices used by MIPS ELF. */
395 switch (h->root.type)
397 default:
398 hsec = NULL;
399 break;
401 case bfd_link_hash_defined:
402 case bfd_link_hash_defweak:
403 hsec = h->root.u.def.section;
404 break;
406 case bfd_link_hash_common:
407 hsec = h->root.u.c.p->section;
408 break;
411 if (hsec == NULL)
412 olddyn = false;
413 else
414 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
417 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
418 respectively, appear to be a definition rather than reference. */
420 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
421 newdef = false;
422 else
423 newdef = true;
425 if (h->root.type == bfd_link_hash_undefined
426 || h->root.type == bfd_link_hash_undefweak
427 || h->root.type == bfd_link_hash_common)
428 olddef = false;
429 else
430 olddef = true;
432 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
433 symbol, respectively, appears to be a common symbol in a dynamic
434 object. If a symbol appears in an uninitialized section, and is
435 not weak, and is not a function, then it may be a common symbol
436 which was resolved when the dynamic object was created. We want
437 to treat such symbols specially, because they raise special
438 considerations when setting the symbol size: if the symbol
439 appears as a common symbol in a regular object, and the size in
440 the regular object is larger, we must make sure that we use the
441 larger size. This problematic case can always be avoided in C,
442 but it must be handled correctly when using Fortran shared
443 libraries.
445 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
446 likewise for OLDDYNCOMMON and OLDDEF.
448 Note that this test is just a heuristic, and that it is quite
449 possible to have an uninitialized symbol in a shared object which
450 is really a definition, rather than a common symbol. This could
451 lead to some minor confusion when the symbol really is a common
452 symbol in some regular object. However, I think it will be
453 harmless. */
455 if (newdyn
456 && newdef
457 && (sec->flags & SEC_ALLOC) != 0
458 && (sec->flags & SEC_LOAD) == 0
459 && sym->st_size > 0
460 && bind != STB_WEAK
461 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
462 newdyncommon = true;
463 else
464 newdyncommon = false;
466 if (olddyn
467 && olddef
468 && h->root.type == bfd_link_hash_defined
469 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
470 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
471 && (h->root.u.def.section->flags & SEC_LOAD) == 0
472 && h->size > 0
473 && h->type != STT_FUNC)
474 olddyncommon = true;
475 else
476 olddyncommon = false;
478 /* It's OK to change the type if either the existing symbol or the
479 new symbol is weak. */
481 if (h->root.type == bfd_link_hash_defweak
482 || h->root.type == bfd_link_hash_undefweak
483 || bind == STB_WEAK)
484 *type_change_ok = true;
486 /* It's OK to change the size if either the existing symbol or the
487 new symbol is weak, or if the old symbol is undefined. */
489 if (*type_change_ok
490 || h->root.type == bfd_link_hash_undefined)
491 *size_change_ok = true;
493 /* If both the old and the new symbols look like common symbols in a
494 dynamic object, set the size of the symbol to the larger of the
495 two. */
497 if (olddyncommon
498 && newdyncommon
499 && sym->st_size != h->size)
501 /* Since we think we have two common symbols, issue a multiple
502 common warning if desired. Note that we only warn if the
503 size is different. If the size is the same, we simply let
504 the old symbol override the new one as normally happens with
505 symbols defined in dynamic objects. */
507 if (! ((*info->callbacks->multiple_common)
508 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
509 h->size, abfd, bfd_link_hash_common, sym->st_size)))
510 return false;
512 if (sym->st_size > h->size)
513 h->size = sym->st_size;
515 *size_change_ok = true;
518 /* If we are looking at a dynamic object, and we have found a
519 definition, we need to see if the symbol was already defined by
520 some other object. If so, we want to use the existing
521 definition, and we do not want to report a multiple symbol
522 definition error; we do this by clobbering *PSEC to be
523 bfd_und_section_ptr.
525 We treat a common symbol as a definition if the symbol in the
526 shared library is a function, since common symbols always
527 represent variables; this can cause confusion in principle, but
528 any such confusion would seem to indicate an erroneous program or
529 shared library. We also permit a common symbol in a regular
530 object to override a weak symbol in a shared object.
532 We prefer a non-weak definition in a shared library to a weak
533 definition in the executable. */
535 if (newdyn
536 && newdef
537 && (olddef
538 || (h->root.type == bfd_link_hash_common
539 && (bind == STB_WEAK
540 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
541 && (h->root.type != bfd_link_hash_defweak
542 || bind == STB_WEAK))
544 *override = true;
545 newdef = false;
546 newdyncommon = false;
548 *psec = sec = bfd_und_section_ptr;
549 *size_change_ok = true;
551 /* If we get here when the old symbol is a common symbol, then
552 we are explicitly letting it override a weak symbol or
553 function in a dynamic object, and we don't want to warn about
554 a type change. If the old symbol is a defined symbol, a type
555 change warning may still be appropriate. */
557 if (h->root.type == bfd_link_hash_common)
558 *type_change_ok = true;
561 /* Handle the special case of an old common symbol merging with a
562 new symbol which looks like a common symbol in a shared object.
563 We change *PSEC and *PVALUE to make the new symbol look like a
564 common symbol, and let _bfd_generic_link_add_one_symbol will do
565 the right thing. */
567 if (newdyncommon
568 && h->root.type == bfd_link_hash_common)
570 *override = true;
571 newdef = false;
572 newdyncommon = false;
573 *pvalue = sym->st_size;
574 *psec = sec = bfd_com_section_ptr;
575 *size_change_ok = true;
578 /* If the old symbol is from a dynamic object, and the new symbol is
579 a definition which is not from a dynamic object, then the new
580 symbol overrides the old symbol. Symbols from regular files
581 always take precedence over symbols from dynamic objects, even if
582 they are defined after the dynamic object in the link.
584 As above, we again permit a common symbol in a regular object to
585 override a definition in a shared object if the shared object
586 symbol is a function or is weak.
588 As above, we permit a non-weak definition in a shared object to
589 override a weak definition in a regular object. */
591 if (! newdyn
592 && (newdef
593 || (bfd_is_com_section (sec)
594 && (h->root.type == bfd_link_hash_defweak
595 || h->type == STT_FUNC)))
596 && olddyn
597 && olddef
598 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
599 && (bind != STB_WEAK
600 || h->root.type == bfd_link_hash_defweak))
602 /* Change the hash table entry to undefined, and let
603 _bfd_generic_link_add_one_symbol do the right thing with the
604 new definition. */
606 h->root.type = bfd_link_hash_undefined;
607 h->root.u.undef.abfd = h->root.u.def.section->owner;
608 *size_change_ok = true;
610 olddef = false;
611 olddyncommon = false;
613 /* We again permit a type change when a common symbol may be
614 overriding a function. */
616 if (bfd_is_com_section (sec))
617 *type_change_ok = true;
619 /* This union may have been set to be non-NULL when this symbol
620 was seen in a dynamic object. We must force the union to be
621 NULL, so that it is correct for a regular symbol. */
623 h->verinfo.vertree = NULL;
625 /* In this special case, if H is the target of an indirection,
626 we want the caller to frob with H rather than with the
627 indirect symbol. That will permit the caller to redefine the
628 target of the indirection, rather than the indirect symbol
629 itself. FIXME: This will break the -y option if we store a
630 symbol with a different name. */
631 *sym_hash = h;
634 /* Handle the special case of a new common symbol merging with an
635 old symbol that looks like it might be a common symbol defined in
636 a shared object. Note that we have already handled the case in
637 which a new common symbol should simply override the definition
638 in the shared library. */
640 if (! newdyn
641 && bfd_is_com_section (sec)
642 && olddyncommon)
644 /* It would be best if we could set the hash table entry to a
645 common symbol, but we don't know what to use for the section
646 or the alignment. */
647 if (! ((*info->callbacks->multiple_common)
648 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
649 h->size, abfd, bfd_link_hash_common, sym->st_size)))
650 return false;
652 /* If the predumed common symbol in the dynamic object is
653 larger, pretend that the new symbol has its size. */
655 if (h->size > *pvalue)
656 *pvalue = h->size;
658 /* FIXME: We no longer know the alignment required by the symbol
659 in the dynamic object, so we just wind up using the one from
660 the regular object. */
662 olddef = false;
663 olddyncommon = false;
665 h->root.type = bfd_link_hash_undefined;
666 h->root.u.undef.abfd = h->root.u.def.section->owner;
668 *size_change_ok = true;
669 *type_change_ok = true;
671 h->verinfo.vertree = NULL;
674 /* Handle the special case of a weak definition in a regular object
675 followed by a non-weak definition in a shared object. In this
676 case, we prefer the definition in the shared object. */
677 if (olddef
678 && h->root.type == bfd_link_hash_defweak
679 && newdef
680 && newdyn
681 && bind != STB_WEAK)
683 /* To make this work we have to frob the flags so that the rest
684 of the code does not think we are using the regular
685 definition. */
686 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
687 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
688 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
689 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
690 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
691 | ELF_LINK_HASH_DEF_DYNAMIC);
693 /* If H is the target of an indirection, we want the caller to
694 use H rather than the indirect symbol. Otherwise if we are
695 defining a new indirect symbol we will wind up attaching it
696 to the entry we are overriding. */
697 *sym_hash = h;
700 /* Handle the special case of a non-weak definition in a shared
701 object followed by a weak definition in a regular object. In
702 this case we prefer to definition in the shared object. To make
703 this work we have to tell the caller to not treat the new symbol
704 as a definition. */
705 if (olddef
706 && olddyn
707 && h->root.type != bfd_link_hash_defweak
708 && newdef
709 && ! newdyn
710 && bind == STB_WEAK)
711 *override = true;
713 return true;
716 /* Add symbols from an ELF object file to the linker hash table. */
718 static boolean
719 elf_link_add_object_symbols (abfd, info)
720 bfd *abfd;
721 struct bfd_link_info *info;
723 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
724 const Elf_Internal_Sym *,
725 const char **, flagword *,
726 asection **, bfd_vma *));
727 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
728 asection *, const Elf_Internal_Rela *));
729 boolean collect;
730 Elf_Internal_Shdr *hdr;
731 size_t symcount;
732 size_t extsymcount;
733 size_t extsymoff;
734 Elf_External_Sym *buf = NULL;
735 struct elf_link_hash_entry **sym_hash;
736 boolean dynamic;
737 bfd_byte *dynver = NULL;
738 Elf_External_Versym *extversym = NULL;
739 Elf_External_Versym *ever;
740 Elf_External_Dyn *dynbuf = NULL;
741 struct elf_link_hash_entry *weaks;
742 Elf_External_Sym *esym;
743 Elf_External_Sym *esymend;
745 add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
746 collect = get_elf_backend_data (abfd)->collect;
748 if ((abfd->flags & DYNAMIC) == 0)
749 dynamic = false;
750 else
752 dynamic = true;
754 /* You can't use -r against a dynamic object. Also, there's no
755 hope of using a dynamic object which does not exactly match
756 the format of the output file. */
757 if (info->relocateable || info->hash->creator != abfd->xvec)
759 bfd_set_error (bfd_error_invalid_operation);
760 goto error_return;
764 /* As a GNU extension, any input sections which are named
765 .gnu.warning.SYMBOL are treated as warning symbols for the given
766 symbol. This differs from .gnu.warning sections, which generate
767 warnings when they are included in an output file. */
768 if (! info->shared)
770 asection *s;
772 for (s = abfd->sections; s != NULL; s = s->next)
774 const char *name;
776 name = bfd_get_section_name (abfd, s);
777 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
779 char *msg;
780 bfd_size_type sz;
782 name += sizeof ".gnu.warning." - 1;
784 /* If this is a shared object, then look up the symbol
785 in the hash table. If it is there, and it is already
786 been defined, then we will not be using the entry
787 from this shared object, so we don't need to warn.
788 FIXME: If we see the definition in a regular object
789 later on, we will warn, but we shouldn't. The only
790 fix is to keep track of what warnings we are supposed
791 to emit, and then handle them all at the end of the
792 link. */
793 if (dynamic && abfd->xvec == info->hash->creator)
795 struct elf_link_hash_entry *h;
797 h = elf_link_hash_lookup (elf_hash_table (info), name,
798 false, false, true);
800 /* FIXME: What about bfd_link_hash_common? */
801 if (h != NULL
802 && (h->root.type == bfd_link_hash_defined
803 || h->root.type == bfd_link_hash_defweak))
805 /* We don't want to issue this warning. Clobber
806 the section size so that the warning does not
807 get copied into the output file. */
808 s->_raw_size = 0;
809 continue;
813 sz = bfd_section_size (abfd, s);
814 msg = (char *) bfd_alloc (abfd, sz + 1);
815 if (msg == NULL)
816 goto error_return;
818 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
819 goto error_return;
821 msg[sz] = '\0';
823 if (! (_bfd_generic_link_add_one_symbol
824 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
825 false, collect, (struct bfd_link_hash_entry **) NULL)))
826 goto error_return;
828 if (! info->relocateable)
830 /* Clobber the section size so that the warning does
831 not get copied into the output file. */
832 s->_raw_size = 0;
838 /* If this is a dynamic object, we always link against the .dynsym
839 symbol table, not the .symtab symbol table. The dynamic linker
840 will only see the .dynsym symbol table, so there is no reason to
841 look at .symtab for a dynamic object. */
843 if (! dynamic || elf_dynsymtab (abfd) == 0)
844 hdr = &elf_tdata (abfd)->symtab_hdr;
845 else
846 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
848 if (dynamic)
850 /* Read in any version definitions. */
852 if (! _bfd_elf_slurp_version_tables (abfd))
853 goto error_return;
855 /* Read in the symbol versions, but don't bother to convert them
856 to internal format. */
857 if (elf_dynversym (abfd) != 0)
859 Elf_Internal_Shdr *versymhdr;
861 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
862 extversym = (Elf_External_Versym *) bfd_malloc (hdr->sh_size);
863 if (extversym == NULL)
864 goto error_return;
865 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
866 || (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd)
867 != versymhdr->sh_size))
868 goto error_return;
872 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
874 /* The sh_info field of the symtab header tells us where the
875 external symbols start. We don't care about the local symbols at
876 this point. */
877 if (elf_bad_symtab (abfd))
879 extsymcount = symcount;
880 extsymoff = 0;
882 else
884 extsymcount = symcount - hdr->sh_info;
885 extsymoff = hdr->sh_info;
888 buf = ((Elf_External_Sym *)
889 bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
890 if (buf == NULL && extsymcount != 0)
891 goto error_return;
893 /* We store a pointer to the hash table entry for each external
894 symbol. */
895 sym_hash = ((struct elf_link_hash_entry **)
896 bfd_alloc (abfd,
897 extsymcount * sizeof (struct elf_link_hash_entry *)));
898 if (sym_hash == NULL)
899 goto error_return;
900 elf_sym_hashes (abfd) = sym_hash;
902 if (! dynamic)
904 /* If we are creating a shared library, create all the dynamic
905 sections immediately. We need to attach them to something,
906 so we attach them to this BFD, provided it is the right
907 format. FIXME: If there are no input BFD's of the same
908 format as the output, we can't make a shared library. */
909 if (info->shared
910 && ! elf_hash_table (info)->dynamic_sections_created
911 && abfd->xvec == info->hash->creator)
913 if (! elf_link_create_dynamic_sections (abfd, info))
914 goto error_return;
917 else
919 asection *s;
920 boolean add_needed;
921 const char *name;
922 bfd_size_type oldsize;
923 bfd_size_type strindex;
925 /* Find the name to use in a DT_NEEDED entry that refers to this
926 object. If the object has a DT_SONAME entry, we use it.
927 Otherwise, if the generic linker stuck something in
928 elf_dt_name, we use that. Otherwise, we just use the file
929 name. If the generic linker put a null string into
930 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
931 there is a DT_SONAME entry. */
932 add_needed = true;
933 name = bfd_get_filename (abfd);
934 if (elf_dt_name (abfd) != NULL)
936 name = elf_dt_name (abfd);
937 if (*name == '\0')
938 add_needed = false;
940 s = bfd_get_section_by_name (abfd, ".dynamic");
941 if (s != NULL)
943 Elf_External_Dyn *extdyn;
944 Elf_External_Dyn *extdynend;
945 int elfsec;
946 unsigned long link;
948 dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
949 if (dynbuf == NULL)
950 goto error_return;
952 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
953 (file_ptr) 0, s->_raw_size))
954 goto error_return;
956 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
957 if (elfsec == -1)
958 goto error_return;
959 link = elf_elfsections (abfd)[elfsec]->sh_link;
962 /* The shared libraries distributed with hpux11 have a bogus
963 sh_link field for the ".dynamic" section. This code detects
964 when LINK refers to a section that is not a string table and
965 tries to find the string table for the ".dynsym" section
966 instead. */
967 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[link];
968 if (hdr->sh_type != SHT_STRTAB)
970 asection *s = bfd_get_section_by_name (abfd, ".dynsym");
971 int elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
972 if (elfsec == -1)
973 goto error_return;
974 link = elf_elfsections (abfd)[elfsec]->sh_link;
978 extdyn = dynbuf;
979 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
980 for (; extdyn < extdynend; extdyn++)
982 Elf_Internal_Dyn dyn;
984 elf_swap_dyn_in (abfd, extdyn, &dyn);
985 if (dyn.d_tag == DT_SONAME)
987 name = bfd_elf_string_from_elf_section (abfd, link,
988 dyn.d_un.d_val);
989 if (name == NULL)
990 goto error_return;
992 if (dyn.d_tag == DT_NEEDED)
994 struct bfd_link_needed_list *n, **pn;
995 char *fnm, *anm;
997 n = ((struct bfd_link_needed_list *)
998 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
999 fnm = bfd_elf_string_from_elf_section (abfd, link,
1000 dyn.d_un.d_val);
1001 if (n == NULL || fnm == NULL)
1002 goto error_return;
1003 anm = bfd_alloc (abfd, strlen (fnm) + 1);
1004 if (anm == NULL)
1005 goto error_return;
1006 strcpy (anm, fnm);
1007 n->name = anm;
1008 n->by = abfd;
1009 n->next = NULL;
1010 for (pn = &elf_hash_table (info)->needed;
1011 *pn != NULL;
1012 pn = &(*pn)->next)
1014 *pn = n;
1018 free (dynbuf);
1019 dynbuf = NULL;
1022 /* We do not want to include any of the sections in a dynamic
1023 object in the output file. We hack by simply clobbering the
1024 list of sections in the BFD. This could be handled more
1025 cleanly by, say, a new section flag; the existing
1026 SEC_NEVER_LOAD flag is not the one we want, because that one
1027 still implies that the section takes up space in the output
1028 file. */
1029 abfd->sections = NULL;
1030 abfd->section_count = 0;
1032 /* If this is the first dynamic object found in the link, create
1033 the special sections required for dynamic linking. */
1034 if (! elf_hash_table (info)->dynamic_sections_created)
1036 if (! elf_link_create_dynamic_sections (abfd, info))
1037 goto error_return;
1040 if (add_needed)
1042 /* Add a DT_NEEDED entry for this dynamic object. */
1043 oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1044 strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
1045 true, false);
1046 if (strindex == (bfd_size_type) -1)
1047 goto error_return;
1049 if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
1051 asection *sdyn;
1052 Elf_External_Dyn *dyncon, *dynconend;
1054 /* The hash table size did not change, which means that
1055 the dynamic object name was already entered. If we
1056 have already included this dynamic object in the
1057 link, just ignore it. There is no reason to include
1058 a particular dynamic object more than once. */
1059 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
1060 ".dynamic");
1061 BFD_ASSERT (sdyn != NULL);
1063 dyncon = (Elf_External_Dyn *) sdyn->contents;
1064 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1065 sdyn->_raw_size);
1066 for (; dyncon < dynconend; dyncon++)
1068 Elf_Internal_Dyn dyn;
1070 elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
1071 &dyn);
1072 if (dyn.d_tag == DT_NEEDED
1073 && dyn.d_un.d_val == strindex)
1075 if (buf != NULL)
1076 free (buf);
1077 if (extversym != NULL)
1078 free (extversym);
1079 return true;
1084 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1085 goto error_return;
1088 /* Save the SONAME, if there is one, because sometimes the
1089 linker emulation code will need to know it. */
1090 if (*name == '\0')
1091 name = bfd_get_filename (abfd);
1092 elf_dt_name (abfd) = name;
1095 if (bfd_seek (abfd,
1096 hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
1097 SEEK_SET) != 0
1098 || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
1099 != extsymcount * sizeof (Elf_External_Sym)))
1100 goto error_return;
1102 weaks = NULL;
1104 ever = extversym != NULL ? extversym + extsymoff : NULL;
1105 esymend = buf + extsymcount;
1106 for (esym = buf;
1107 esym < esymend;
1108 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1110 Elf_Internal_Sym sym;
1111 int bind;
1112 bfd_vma value;
1113 asection *sec;
1114 flagword flags;
1115 const char *name;
1116 struct elf_link_hash_entry *h;
1117 boolean definition;
1118 boolean size_change_ok, type_change_ok;
1119 boolean new_weakdef;
1120 unsigned int old_alignment;
1122 elf_swap_symbol_in (abfd, esym, &sym);
1124 flags = BSF_NO_FLAGS;
1125 sec = NULL;
1126 value = sym.st_value;
1127 *sym_hash = NULL;
1129 bind = ELF_ST_BIND (sym.st_info);
1130 if (bind == STB_LOCAL)
1132 /* This should be impossible, since ELF requires that all
1133 global symbols follow all local symbols, and that sh_info
1134 point to the first global symbol. Unfortunatealy, Irix 5
1135 screws this up. */
1136 continue;
1138 else if (bind == STB_GLOBAL)
1140 if (sym.st_shndx != SHN_UNDEF
1141 && sym.st_shndx != SHN_COMMON)
1142 flags = BSF_GLOBAL;
1143 else
1144 flags = 0;
1146 else if (bind == STB_WEAK)
1147 flags = BSF_WEAK;
1148 else
1150 /* Leave it up to the processor backend. */
1153 if (sym.st_shndx == SHN_UNDEF)
1154 sec = bfd_und_section_ptr;
1155 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
1157 sec = section_from_elf_index (abfd, sym.st_shndx);
1158 if (sec == NULL)
1159 sec = bfd_abs_section_ptr;
1160 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1161 value -= sec->vma;
1163 else if (sym.st_shndx == SHN_ABS)
1164 sec = bfd_abs_section_ptr;
1165 else if (sym.st_shndx == SHN_COMMON)
1167 sec = bfd_com_section_ptr;
1168 /* What ELF calls the size we call the value. What ELF
1169 calls the value we call the alignment. */
1170 value = sym.st_size;
1172 else
1174 /* Leave it up to the processor backend. */
1177 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1178 if (name == (const char *) NULL)
1179 goto error_return;
1181 if (add_symbol_hook)
1183 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1184 &value))
1185 goto error_return;
1187 /* The hook function sets the name to NULL if this symbol
1188 should be skipped for some reason. */
1189 if (name == (const char *) NULL)
1190 continue;
1193 /* Sanity check that all possibilities were handled. */
1194 if (sec == (asection *) NULL)
1196 bfd_set_error (bfd_error_bad_value);
1197 goto error_return;
1200 if (bfd_is_und_section (sec)
1201 || bfd_is_com_section (sec))
1202 definition = false;
1203 else
1204 definition = true;
1206 size_change_ok = false;
1207 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1208 old_alignment = 0;
1209 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1211 Elf_Internal_Versym iver;
1212 unsigned int vernum = 0;
1213 boolean override;
1215 if (ever != NULL)
1217 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1218 vernum = iver.vs_vers & VERSYM_VERSION;
1220 /* If this is a hidden symbol, or if it is not version
1221 1, we append the version name to the symbol name.
1222 However, we do not modify a non-hidden absolute
1223 symbol, because it might be the version symbol
1224 itself. FIXME: What if it isn't? */
1225 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1226 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1228 const char *verstr;
1229 int namelen, newlen;
1230 char *newname, *p;
1232 if (sym.st_shndx != SHN_UNDEF)
1234 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1236 (*_bfd_error_handler)
1237 (_("%s: %s: invalid version %u (max %d)"),
1238 bfd_get_filename (abfd), name, vernum,
1239 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1240 bfd_set_error (bfd_error_bad_value);
1241 goto error_return;
1243 else if (vernum > 1)
1244 verstr =
1245 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1246 else
1247 verstr = "";
1249 else
1251 /* We cannot simply test for the number of
1252 entries in the VERNEED section since the
1253 numbers for the needed versions do not start
1254 at 0. */
1255 Elf_Internal_Verneed *t;
1257 verstr = NULL;
1258 for (t = elf_tdata (abfd)->verref;
1259 t != NULL;
1260 t = t->vn_nextref)
1262 Elf_Internal_Vernaux *a;
1264 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1266 if (a->vna_other == vernum)
1268 verstr = a->vna_nodename;
1269 break;
1272 if (a != NULL)
1273 break;
1275 if (verstr == NULL)
1277 (*_bfd_error_handler)
1278 (_("%s: %s: invalid needed version %d"),
1279 bfd_get_filename (abfd), name, vernum);
1280 bfd_set_error (bfd_error_bad_value);
1281 goto error_return;
1285 namelen = strlen (name);
1286 newlen = namelen + strlen (verstr) + 2;
1287 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1288 ++newlen;
1290 newname = (char *) bfd_alloc (abfd, newlen);
1291 if (newname == NULL)
1292 goto error_return;
1293 strcpy (newname, name);
1294 p = newname + namelen;
1295 *p++ = ELF_VER_CHR;
1296 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1297 *p++ = ELF_VER_CHR;
1298 strcpy (p, verstr);
1300 name = newname;
1304 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1305 sym_hash, &override, &type_change_ok,
1306 &size_change_ok))
1307 goto error_return;
1309 if (override)
1310 definition = false;
1312 h = *sym_hash;
1313 while (h->root.type == bfd_link_hash_indirect
1314 || h->root.type == bfd_link_hash_warning)
1315 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1317 /* Remember the old alignment if this is a common symbol, so
1318 that we don't reduce the alignment later on. We can't
1319 check later, because _bfd_generic_link_add_one_symbol
1320 will set a default for the alignment which we want to
1321 override. */
1322 if (h->root.type == bfd_link_hash_common)
1323 old_alignment = h->root.u.c.p->alignment_power;
1325 if (elf_tdata (abfd)->verdef != NULL
1326 && ! override
1327 && vernum > 1
1328 && definition)
1329 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1332 if (! (_bfd_generic_link_add_one_symbol
1333 (info, abfd, name, flags, sec, value, (const char *) NULL,
1334 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1335 goto error_return;
1337 h = *sym_hash;
1338 while (h->root.type == bfd_link_hash_indirect
1339 || h->root.type == bfd_link_hash_warning)
1340 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1341 *sym_hash = h;
1343 new_weakdef = false;
1344 if (dynamic
1345 && definition
1346 && (flags & BSF_WEAK) != 0
1347 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1348 && info->hash->creator->flavour == bfd_target_elf_flavour
1349 && h->weakdef == NULL)
1351 /* Keep a list of all weak defined non function symbols from
1352 a dynamic object, using the weakdef field. Later in this
1353 function we will set the weakdef field to the correct
1354 value. We only put non-function symbols from dynamic
1355 objects on this list, because that happens to be the only
1356 time we need to know the normal symbol corresponding to a
1357 weak symbol, and the information is time consuming to
1358 figure out. If the weakdef field is not already NULL,
1359 then this symbol was already defined by some previous
1360 dynamic object, and we will be using that previous
1361 definition anyhow. */
1363 h->weakdef = weaks;
1364 weaks = h;
1365 new_weakdef = true;
1368 /* Set the alignment of a common symbol. */
1369 if (sym.st_shndx == SHN_COMMON
1370 && h->root.type == bfd_link_hash_common)
1372 unsigned int align;
1374 align = bfd_log2 (sym.st_value);
1375 if (align > old_alignment)
1376 h->root.u.c.p->alignment_power = align;
1379 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1381 int old_flags;
1382 boolean dynsym;
1383 int new_flag;
1385 /* Remember the symbol size and type. */
1386 if (sym.st_size != 0
1387 && (definition || h->size == 0))
1389 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1390 (*_bfd_error_handler)
1391 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1392 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1393 bfd_get_filename (abfd));
1395 h->size = sym.st_size;
1398 /* If this is a common symbol, then we always want H->SIZE
1399 to be the size of the common symbol. The code just above
1400 won't fix the size if a common symbol becomes larger. We
1401 don't warn about a size change here, because that is
1402 covered by --warn-common. */
1403 if (h->root.type == bfd_link_hash_common)
1404 h->size = h->root.u.c.size;
1406 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1407 && (definition || h->type == STT_NOTYPE))
1409 if (h->type != STT_NOTYPE
1410 && h->type != ELF_ST_TYPE (sym.st_info)
1411 && ! type_change_ok)
1412 (*_bfd_error_handler)
1413 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1414 name, h->type, ELF_ST_TYPE (sym.st_info),
1415 bfd_get_filename (abfd));
1417 h->type = ELF_ST_TYPE (sym.st_info);
1420 if (sym.st_other != 0
1421 && (definition || h->other == 0))
1422 h->other = sym.st_other;
1424 /* Set a flag in the hash table entry indicating the type of
1425 reference or definition we just found. Keep a count of
1426 the number of dynamic symbols we find. A dynamic symbol
1427 is one which is referenced or defined by both a regular
1428 object and a shared object. */
1429 old_flags = h->elf_link_hash_flags;
1430 dynsym = false;
1431 if (! dynamic)
1433 if (! definition)
1435 new_flag = ELF_LINK_HASH_REF_REGULAR;
1436 if (bind != STB_WEAK)
1437 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1439 else
1440 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1441 if (info->shared
1442 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1443 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1444 dynsym = true;
1446 else
1448 if (! definition)
1449 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1450 else
1451 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1452 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1453 | ELF_LINK_HASH_REF_REGULAR)) != 0
1454 || (h->weakdef != NULL
1455 && ! new_weakdef
1456 && h->weakdef->dynindx != -1))
1457 dynsym = true;
1460 h->elf_link_hash_flags |= new_flag;
1462 /* If this symbol has a version, and it is the default
1463 version, we create an indirect symbol from the default
1464 name to the fully decorated name. This will cause
1465 external references which do not specify a version to be
1466 bound to this version of the symbol. */
1467 if (definition)
1469 char *p;
1471 p = strchr (name, ELF_VER_CHR);
1472 if (p != NULL && p[1] == ELF_VER_CHR)
1474 char *shortname;
1475 struct elf_link_hash_entry *hi;
1476 boolean override;
1478 shortname = bfd_hash_allocate (&info->hash->table,
1479 p - name + 1);
1480 if (shortname == NULL)
1481 goto error_return;
1482 strncpy (shortname, name, p - name);
1483 shortname[p - name] = '\0';
1485 /* We are going to create a new symbol. Merge it
1486 with any existing symbol with this name. For the
1487 purposes of the merge, act as though we were
1488 defining the symbol we just defined, although we
1489 actually going to define an indirect symbol. */
1490 type_change_ok = false;
1491 size_change_ok = false;
1492 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1493 &value, &hi, &override,
1494 &type_change_ok, &size_change_ok))
1495 goto error_return;
1497 if (! override)
1499 if (! (_bfd_generic_link_add_one_symbol
1500 (info, abfd, shortname, BSF_INDIRECT,
1501 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1502 collect, (struct bfd_link_hash_entry **) &hi)))
1503 goto error_return;
1505 else
1507 /* In this case the symbol named SHORTNAME is
1508 overriding the indirect symbol we want to
1509 add. We were planning on making SHORTNAME an
1510 indirect symbol referring to NAME. SHORTNAME
1511 is the name without a version. NAME is the
1512 fully versioned name, and it is the default
1513 version.
1515 Overriding means that we already saw a
1516 definition for the symbol SHORTNAME in a
1517 regular object, and it is overriding the
1518 symbol defined in the dynamic object.
1520 When this happens, we actually want to change
1521 NAME, the symbol we just added, to refer to
1522 SHORTNAME. This will cause references to
1523 NAME in the shared object to become
1524 references to SHORTNAME in the regular
1525 object. This is what we expect when we
1526 override a function in a shared object: that
1527 the references in the shared object will be
1528 mapped to the definition in the regular
1529 object. */
1531 while (hi->root.type == bfd_link_hash_indirect
1532 || hi->root.type == bfd_link_hash_warning)
1533 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1535 h->root.type = bfd_link_hash_indirect;
1536 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1537 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1539 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1540 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1541 if (hi->elf_link_hash_flags
1542 & (ELF_LINK_HASH_REF_REGULAR
1543 | ELF_LINK_HASH_DEF_REGULAR))
1545 if (! _bfd_elf_link_record_dynamic_symbol (info,
1546 hi))
1547 goto error_return;
1551 /* Now set HI to H, so that the following code
1552 will set the other fields correctly. */
1553 hi = h;
1556 /* If there is a duplicate definition somewhere,
1557 then HI may not point to an indirect symbol. We
1558 will have reported an error to the user in that
1559 case. */
1561 if (hi->root.type == bfd_link_hash_indirect)
1563 struct elf_link_hash_entry *ht;
1565 /* If the symbol became indirect, then we assume
1566 that we have not seen a definition before. */
1567 BFD_ASSERT ((hi->elf_link_hash_flags
1568 & (ELF_LINK_HASH_DEF_DYNAMIC
1569 | ELF_LINK_HASH_DEF_REGULAR))
1570 == 0);
1572 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1574 /* Copy down any references that we may have
1575 already seen to the symbol which just became
1576 indirect. */
1577 ht->elf_link_hash_flags |=
1578 (hi->elf_link_hash_flags
1579 & (ELF_LINK_HASH_REF_DYNAMIC
1580 | ELF_LINK_HASH_REF_REGULAR
1581 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1582 | ELF_LINK_NON_GOT_REF));
1584 /* Copy over the global and procedure linkage table
1585 offset entries. These may have been already set
1586 up by a check_relocs routine. */
1587 if (ht->got.offset == (bfd_vma) -1)
1589 ht->got.offset = hi->got.offset;
1590 hi->got.offset = (bfd_vma) -1;
1592 BFD_ASSERT (hi->got.offset == (bfd_vma) -1);
1594 if (ht->plt.offset == (bfd_vma) -1)
1596 ht->plt.offset = hi->plt.offset;
1597 hi->plt.offset = (bfd_vma) -1;
1599 BFD_ASSERT (hi->plt.offset == (bfd_vma) -1);
1601 if (ht->dynindx == -1)
1603 ht->dynindx = hi->dynindx;
1604 ht->dynstr_index = hi->dynstr_index;
1605 hi->dynindx = -1;
1606 hi->dynstr_index = 0;
1608 BFD_ASSERT (hi->dynindx == -1);
1610 /* FIXME: There may be other information to copy
1611 over for particular targets. */
1613 /* See if the new flags lead us to realize that
1614 the symbol must be dynamic. */
1615 if (! dynsym)
1617 if (! dynamic)
1619 if (info->shared
1620 || ((hi->elf_link_hash_flags
1621 & ELF_LINK_HASH_REF_DYNAMIC)
1622 != 0))
1623 dynsym = true;
1625 else
1627 if ((hi->elf_link_hash_flags
1628 & ELF_LINK_HASH_REF_REGULAR) != 0)
1629 dynsym = true;
1634 /* We also need to define an indirection from the
1635 nondefault version of the symbol. */
1637 shortname = bfd_hash_allocate (&info->hash->table,
1638 strlen (name));
1639 if (shortname == NULL)
1640 goto error_return;
1641 strncpy (shortname, name, p - name);
1642 strcpy (shortname + (p - name), p + 1);
1644 /* Once again, merge with any existing symbol. */
1645 type_change_ok = false;
1646 size_change_ok = false;
1647 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1648 &value, &hi, &override,
1649 &type_change_ok, &size_change_ok))
1650 goto error_return;
1652 if (override)
1654 /* Here SHORTNAME is a versioned name, so we
1655 don't expect to see the type of override we
1656 do in the case above. */
1657 (*_bfd_error_handler)
1658 (_("%s: warning: unexpected redefinition of `%s'"),
1659 bfd_get_filename (abfd), shortname);
1661 else
1663 if (! (_bfd_generic_link_add_one_symbol
1664 (info, abfd, shortname, BSF_INDIRECT,
1665 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1666 collect, (struct bfd_link_hash_entry **) &hi)))
1667 goto error_return;
1669 /* If there is a duplicate definition somewhere,
1670 then HI may not point to an indirect symbol.
1671 We will have reported an error to the user in
1672 that case. */
1674 if (hi->root.type == bfd_link_hash_indirect)
1676 /* If the symbol became indirect, then we
1677 assume that we have not seen a definition
1678 before. */
1679 BFD_ASSERT ((hi->elf_link_hash_flags
1680 & (ELF_LINK_HASH_DEF_DYNAMIC
1681 | ELF_LINK_HASH_DEF_REGULAR))
1682 == 0);
1684 /* Copy down any references that we may have
1685 already seen to the symbol which just
1686 became indirect. */
1687 h->elf_link_hash_flags |=
1688 (hi->elf_link_hash_flags
1689 & (ELF_LINK_HASH_REF_DYNAMIC
1690 | ELF_LINK_HASH_REF_REGULAR
1691 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1692 | ELF_LINK_NON_GOT_REF));
1694 /* Copy over the global and procedure linkage
1695 table offset entries. These may have been
1696 already set up by a check_relocs routine. */
1697 if (h->got.offset == (bfd_vma) -1)
1699 h->got.offset = hi->got.offset;
1700 hi->got.offset = (bfd_vma) -1;
1702 BFD_ASSERT (hi->got.offset == (bfd_vma) -1);
1704 if (h->plt.offset == (bfd_vma) -1)
1706 h->plt.offset = hi->plt.offset;
1707 hi->plt.offset = (bfd_vma) -1;
1709 BFD_ASSERT (hi->got.offset == (bfd_vma) -1);
1711 if (h->dynindx == -1)
1713 h->dynindx = hi->dynindx;
1714 h->dynstr_index = hi->dynstr_index;
1715 hi->dynindx = -1;
1716 hi->dynstr_index = 0;
1718 BFD_ASSERT (hi->dynindx == -1);
1720 /* FIXME: There may be other information to
1721 copy over for particular targets. */
1723 /* See if the new flags lead us to realize
1724 that the symbol must be dynamic. */
1725 if (! dynsym)
1727 if (! dynamic)
1729 if (info->shared
1730 || ((hi->elf_link_hash_flags
1731 & ELF_LINK_HASH_REF_DYNAMIC)
1732 != 0))
1733 dynsym = true;
1735 else
1737 if ((hi->elf_link_hash_flags
1738 & ELF_LINK_HASH_REF_REGULAR) != 0)
1739 dynsym = true;
1747 if (dynsym && h->dynindx == -1)
1749 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1750 goto error_return;
1751 if (h->weakdef != NULL
1752 && ! new_weakdef
1753 && h->weakdef->dynindx == -1)
1755 if (! _bfd_elf_link_record_dynamic_symbol (info,
1756 h->weakdef))
1757 goto error_return;
1763 /* Now set the weakdefs field correctly for all the weak defined
1764 symbols we found. The only way to do this is to search all the
1765 symbols. Since we only need the information for non functions in
1766 dynamic objects, that's the only time we actually put anything on
1767 the list WEAKS. We need this information so that if a regular
1768 object refers to a symbol defined weakly in a dynamic object, the
1769 real symbol in the dynamic object is also put in the dynamic
1770 symbols; we also must arrange for both symbols to point to the
1771 same memory location. We could handle the general case of symbol
1772 aliasing, but a general symbol alias can only be generated in
1773 assembler code, handling it correctly would be very time
1774 consuming, and other ELF linkers don't handle general aliasing
1775 either. */
1776 while (weaks != NULL)
1778 struct elf_link_hash_entry *hlook;
1779 asection *slook;
1780 bfd_vma vlook;
1781 struct elf_link_hash_entry **hpp;
1782 struct elf_link_hash_entry **hppend;
1784 hlook = weaks;
1785 weaks = hlook->weakdef;
1786 hlook->weakdef = NULL;
1788 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1789 || hlook->root.type == bfd_link_hash_defweak
1790 || hlook->root.type == bfd_link_hash_common
1791 || hlook->root.type == bfd_link_hash_indirect);
1792 slook = hlook->root.u.def.section;
1793 vlook = hlook->root.u.def.value;
1795 hpp = elf_sym_hashes (abfd);
1796 hppend = hpp + extsymcount;
1797 for (; hpp < hppend; hpp++)
1799 struct elf_link_hash_entry *h;
1801 h = *hpp;
1802 if (h != NULL && h != hlook
1803 && h->root.type == bfd_link_hash_defined
1804 && h->root.u.def.section == slook
1805 && h->root.u.def.value == vlook)
1807 hlook->weakdef = h;
1809 /* If the weak definition is in the list of dynamic
1810 symbols, make sure the real definition is put there
1811 as well. */
1812 if (hlook->dynindx != -1
1813 && h->dynindx == -1)
1815 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1816 goto error_return;
1819 /* If the real definition is in the list of dynamic
1820 symbols, make sure the weak definition is put there
1821 as well. If we don't do this, then the dynamic
1822 loader might not merge the entries for the real
1823 definition and the weak definition. */
1824 if (h->dynindx != -1
1825 && hlook->dynindx == -1)
1827 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1828 goto error_return;
1831 break;
1836 if (buf != NULL)
1838 free (buf);
1839 buf = NULL;
1842 if (extversym != NULL)
1844 free (extversym);
1845 extversym = NULL;
1848 /* If this object is the same format as the output object, and it is
1849 not a shared library, then let the backend look through the
1850 relocs.
1852 This is required to build global offset table entries and to
1853 arrange for dynamic relocs. It is not required for the
1854 particular common case of linking non PIC code, even when linking
1855 against shared libraries, but unfortunately there is no way of
1856 knowing whether an object file has been compiled PIC or not.
1857 Looking through the relocs is not particularly time consuming.
1858 The problem is that we must either (1) keep the relocs in memory,
1859 which causes the linker to require additional runtime memory or
1860 (2) read the relocs twice from the input file, which wastes time.
1861 This would be a good case for using mmap.
1863 I have no idea how to handle linking PIC code into a file of a
1864 different format. It probably can't be done. */
1865 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1866 if (! dynamic
1867 && abfd->xvec == info->hash->creator
1868 && check_relocs != NULL)
1870 asection *o;
1872 for (o = abfd->sections; o != NULL; o = o->next)
1874 Elf_Internal_Rela *internal_relocs;
1875 boolean ok;
1877 if ((o->flags & SEC_RELOC) == 0
1878 || o->reloc_count == 0
1879 || ((info->strip == strip_all || info->strip == strip_debugger)
1880 && (o->flags & SEC_DEBUGGING) != 0)
1881 || bfd_is_abs_section (o->output_section))
1882 continue;
1884 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
1885 (abfd, o, (PTR) NULL,
1886 (Elf_Internal_Rela *) NULL,
1887 info->keep_memory));
1888 if (internal_relocs == NULL)
1889 goto error_return;
1891 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1893 if (! info->keep_memory)
1894 free (internal_relocs);
1896 if (! ok)
1897 goto error_return;
1901 /* If this is a non-traditional, non-relocateable link, try to
1902 optimize the handling of the .stab/.stabstr sections. */
1903 if (! dynamic
1904 && ! info->relocateable
1905 && ! info->traditional_format
1906 && info->hash->creator->flavour == bfd_target_elf_flavour
1907 && (info->strip != strip_all && info->strip != strip_debugger))
1909 asection *stab, *stabstr;
1911 stab = bfd_get_section_by_name (abfd, ".stab");
1912 if (stab != NULL)
1914 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1916 if (stabstr != NULL)
1918 struct bfd_elf_section_data *secdata;
1920 secdata = elf_section_data (stab);
1921 if (! _bfd_link_section_stabs (abfd,
1922 &elf_hash_table (info)->stab_info,
1923 stab, stabstr,
1924 &secdata->stab_info))
1925 goto error_return;
1930 return true;
1932 error_return:
1933 if (buf != NULL)
1934 free (buf);
1935 if (dynbuf != NULL)
1936 free (dynbuf);
1937 if (dynver != NULL)
1938 free (dynver);
1939 if (extversym != NULL)
1940 free (extversym);
1941 return false;
1944 /* Create some sections which will be filled in with dynamic linking
1945 information. ABFD is an input file which requires dynamic sections
1946 to be created. The dynamic sections take up virtual memory space
1947 when the final executable is run, so we need to create them before
1948 addresses are assigned to the output sections. We work out the
1949 actual contents and size of these sections later. */
1951 boolean
1952 elf_link_create_dynamic_sections (abfd, info)
1953 bfd *abfd;
1954 struct bfd_link_info *info;
1956 flagword flags;
1957 register asection *s;
1958 struct elf_link_hash_entry *h;
1959 struct elf_backend_data *bed;
1961 if (elf_hash_table (info)->dynamic_sections_created)
1962 return true;
1964 /* Make sure that all dynamic sections use the same input BFD. */
1965 if (elf_hash_table (info)->dynobj == NULL)
1966 elf_hash_table (info)->dynobj = abfd;
1967 else
1968 abfd = elf_hash_table (info)->dynobj;
1970 /* Note that we set the SEC_IN_MEMORY flag for all of these
1971 sections. */
1972 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1973 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1975 /* A dynamically linked executable has a .interp section, but a
1976 shared library does not. */
1977 if (! info->shared)
1979 s = bfd_make_section (abfd, ".interp");
1980 if (s == NULL
1981 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1982 return false;
1985 /* Create sections to hold version informations. These are removed
1986 if they are not needed. */
1987 s = bfd_make_section (abfd, ".gnu.version_d");
1988 if (s == NULL
1989 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1990 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1991 return false;
1993 s = bfd_make_section (abfd, ".gnu.version");
1994 if (s == NULL
1995 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1996 || ! bfd_set_section_alignment (abfd, s, 1))
1997 return false;
1999 s = bfd_make_section (abfd, ".gnu.version_r");
2000 if (s == NULL
2001 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2002 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2003 return false;
2005 s = bfd_make_section (abfd, ".dynsym");
2006 if (s == NULL
2007 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2008 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2009 return false;
2011 s = bfd_make_section (abfd, ".dynstr");
2012 if (s == NULL
2013 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2014 return false;
2016 /* Create a strtab to hold the dynamic symbol names. */
2017 if (elf_hash_table (info)->dynstr == NULL)
2019 elf_hash_table (info)->dynstr = elf_stringtab_init ();
2020 if (elf_hash_table (info)->dynstr == NULL)
2021 return false;
2024 s = bfd_make_section (abfd, ".dynamic");
2025 if (s == NULL
2026 || ! bfd_set_section_flags (abfd, s, flags)
2027 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2028 return false;
2030 /* The special symbol _DYNAMIC is always set to the start of the
2031 .dynamic section. This call occurs before we have processed the
2032 symbols for any dynamic object, so we don't have to worry about
2033 overriding a dynamic definition. We could set _DYNAMIC in a
2034 linker script, but we only want to define it if we are, in fact,
2035 creating a .dynamic section. We don't want to define it if there
2036 is no .dynamic section, since on some ELF platforms the start up
2037 code examines it to decide how to initialize the process. */
2038 h = NULL;
2039 if (! (_bfd_generic_link_add_one_symbol
2040 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2041 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2042 (struct bfd_link_hash_entry **) &h)))
2043 return false;
2044 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2045 h->type = STT_OBJECT;
2047 if (info->shared
2048 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2049 return false;
2051 bed = get_elf_backend_data (abfd);
2053 s = bfd_make_section (abfd, ".hash");
2054 if (s == NULL
2055 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2056 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2057 return false;
2058 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2060 /* Let the backend create the rest of the sections. This lets the
2061 backend set the right flags. The backend will normally create
2062 the .got and .plt sections. */
2063 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2064 return false;
2066 elf_hash_table (info)->dynamic_sections_created = true;
2068 return true;
2071 /* Add an entry to the .dynamic table. */
2073 boolean
2074 elf_add_dynamic_entry (info, tag, val)
2075 struct bfd_link_info *info;
2076 bfd_vma tag;
2077 bfd_vma val;
2079 Elf_Internal_Dyn dyn;
2080 bfd *dynobj;
2081 asection *s;
2082 size_t newsize;
2083 bfd_byte *newcontents;
2085 dynobj = elf_hash_table (info)->dynobj;
2087 s = bfd_get_section_by_name (dynobj, ".dynamic");
2088 BFD_ASSERT (s != NULL);
2090 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2091 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2092 if (newcontents == NULL)
2093 return false;
2095 dyn.d_tag = tag;
2096 dyn.d_un.d_val = val;
2097 elf_swap_dyn_out (dynobj, &dyn,
2098 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2100 s->_raw_size = newsize;
2101 s->contents = newcontents;
2103 return true;
2106 /* Record a new local dynamic symbol. */
2108 boolean
2109 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2110 struct bfd_link_info *info;
2111 bfd *input_bfd;
2112 long input_indx;
2114 struct elf_link_local_dynamic_entry *entry;
2115 struct elf_link_hash_table *eht;
2116 struct bfd_strtab_hash *dynstr;
2117 Elf_External_Sym esym;
2118 unsigned long dynstr_index;
2119 char *name;
2121 /* See if the entry exists already. */
2122 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2123 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2124 return true;
2126 entry = (struct elf_link_local_dynamic_entry *)
2127 bfd_alloc (input_bfd, sizeof (*entry));
2128 if (entry == NULL)
2129 return false;
2131 /* Go find the symbol, so that we can find it's name. */
2132 if (bfd_seek (input_bfd,
2133 (elf_tdata (input_bfd)->symtab_hdr.sh_offset
2134 + input_indx * sizeof (Elf_External_Sym)),
2135 SEEK_SET) != 0
2136 || (bfd_read (&esym, sizeof (Elf_External_Sym), 1, input_bfd)
2137 != sizeof (Elf_External_Sym)))
2138 return false;
2139 elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
2141 name = (bfd_elf_string_from_elf_section
2142 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2143 entry->isym.st_name));
2145 dynstr = elf_hash_table (info)->dynstr;
2146 if (dynstr == NULL)
2148 /* Create a strtab to hold the dynamic symbol names. */
2149 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
2150 if (dynstr == NULL)
2151 return false;
2154 dynstr_index = _bfd_stringtab_add (dynstr, name, true, false);
2155 if (dynstr_index == (unsigned long) -1)
2156 return false;
2157 entry->isym.st_name = dynstr_index;
2159 eht = elf_hash_table (info);
2161 entry->next = eht->dynlocal;
2162 eht->dynlocal = entry;
2163 entry->input_bfd = input_bfd;
2164 entry->input_indx = input_indx;
2165 eht->dynsymcount++;
2167 /* Whatever binding the symbol had before, it's now local. */
2168 entry->isym.st_info
2169 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2171 /* The dynindx will be set at the end of size_dynamic_sections. */
2173 return true;
2177 /* Read and swap the relocs from the section indicated by SHDR. This
2178 may be either a REL or a RELA section. The relocations are
2179 translated into RELA relocations and stored in INTERNAL_RELOCS,
2180 which should have already been allocated to contain enough space.
2181 The EXTERNAL_RELOCS are a buffer where the external form of the
2182 relocations should be stored.
2184 Returns false if something goes wrong. */
2186 static boolean
2187 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2188 internal_relocs)
2189 bfd *abfd;
2190 Elf_Internal_Shdr *shdr;
2191 PTR external_relocs;
2192 Elf_Internal_Rela *internal_relocs;
2194 struct elf_backend_data *bed;
2196 /* If there aren't any relocations, that's OK. */
2197 if (!shdr)
2198 return true;
2200 /* Position ourselves at the start of the section. */
2201 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2202 return false;
2204 /* Read the relocations. */
2205 if (bfd_read (external_relocs, 1, shdr->sh_size, abfd)
2206 != shdr->sh_size)
2207 return false;
2209 bed = get_elf_backend_data (abfd);
2211 /* Convert the external relocations to the internal format. */
2212 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2214 Elf_External_Rel *erel;
2215 Elf_External_Rel *erelend;
2216 Elf_Internal_Rela *irela;
2217 Elf_Internal_Rel *irel;
2219 erel = (Elf_External_Rel *) external_relocs;
2220 erelend = erel + shdr->sh_size / shdr->sh_entsize;
2221 irela = internal_relocs;
2222 irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
2223 * sizeof (Elf_Internal_Rel)));
2224 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2226 unsigned char i;
2228 if (bed->s->swap_reloc_in)
2229 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2230 else
2231 elf_swap_reloc_in (abfd, erel, irel);
2233 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2235 irela[i].r_offset = irel[i].r_offset;
2236 irela[i].r_info = irel[i].r_info;
2237 irela[i].r_addend = 0;
2241 else
2243 Elf_External_Rela *erela;
2244 Elf_External_Rela *erelaend;
2245 Elf_Internal_Rela *irela;
2247 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2249 erela = (Elf_External_Rela *) external_relocs;
2250 erelaend = erela + shdr->sh_size / shdr->sh_entsize;
2251 irela = internal_relocs;
2252 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2254 if (bed->s->swap_reloca_in)
2255 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2256 else
2257 elf_swap_reloca_in (abfd, erela, irela);
2261 return true;
2264 /* Read and swap the relocs for a section O. They may have been
2265 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2266 not NULL, they are used as buffers to read into. They are known to
2267 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2268 the return value is allocated using either malloc or bfd_alloc,
2269 according to the KEEP_MEMORY argument. If O has two relocation
2270 sections (both REL and RELA relocations), then the REL_HDR
2271 relocations will appear first in INTERNAL_RELOCS, followed by the
2272 REL_HDR2 relocations. */
2274 Elf_Internal_Rela *
2275 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2276 keep_memory)
2277 bfd *abfd;
2278 asection *o;
2279 PTR external_relocs;
2280 Elf_Internal_Rela *internal_relocs;
2281 boolean keep_memory;
2283 Elf_Internal_Shdr *rel_hdr;
2284 PTR alloc1 = NULL;
2285 Elf_Internal_Rela *alloc2 = NULL;
2286 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2288 if (elf_section_data (o)->relocs != NULL)
2289 return elf_section_data (o)->relocs;
2291 if (o->reloc_count == 0)
2292 return NULL;
2294 rel_hdr = &elf_section_data (o)->rel_hdr;
2296 if (internal_relocs == NULL)
2298 size_t size;
2300 size = (o->reloc_count * bed->s->int_rels_per_ext_rel
2301 * sizeof (Elf_Internal_Rela));
2302 if (keep_memory)
2303 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2304 else
2305 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2306 if (internal_relocs == NULL)
2307 goto error_return;
2310 if (external_relocs == NULL)
2312 size_t size = (size_t) rel_hdr->sh_size;
2314 if (elf_section_data (o)->rel_hdr2)
2315 size += (size_t) elf_section_data (o)->rel_hdr2->sh_size;
2316 alloc1 = (PTR) bfd_malloc (size);
2317 if (alloc1 == NULL)
2318 goto error_return;
2319 external_relocs = alloc1;
2322 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2323 external_relocs,
2324 internal_relocs))
2325 goto error_return;
2326 if (!elf_link_read_relocs_from_section
2327 (abfd,
2328 elf_section_data (o)->rel_hdr2,
2329 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2330 internal_relocs + (rel_hdr->sh_size / rel_hdr->sh_entsize
2331 * bed->s->int_rels_per_ext_rel)))
2332 goto error_return;
2334 /* Cache the results for next time, if we can. */
2335 if (keep_memory)
2336 elf_section_data (o)->relocs = internal_relocs;
2338 if (alloc1 != NULL)
2339 free (alloc1);
2341 /* Don't free alloc2, since if it was allocated we are passing it
2342 back (under the name of internal_relocs). */
2344 return internal_relocs;
2346 error_return:
2347 if (alloc1 != NULL)
2348 free (alloc1);
2349 if (alloc2 != NULL)
2350 free (alloc2);
2351 return NULL;
2355 /* Record an assignment to a symbol made by a linker script. We need
2356 this in case some dynamic object refers to this symbol. */
2358 /*ARGSUSED*/
2359 boolean
2360 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2361 bfd *output_bfd ATTRIBUTE_UNUSED;
2362 struct bfd_link_info *info;
2363 const char *name;
2364 boolean provide;
2366 struct elf_link_hash_entry *h;
2368 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2369 return true;
2371 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2372 if (h == NULL)
2373 return false;
2375 if (h->root.type == bfd_link_hash_new)
2376 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
2378 /* If this symbol is being provided by the linker script, and it is
2379 currently defined by a dynamic object, but not by a regular
2380 object, then mark it as undefined so that the generic linker will
2381 force the correct value. */
2382 if (provide
2383 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2384 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2385 h->root.type = bfd_link_hash_undefined;
2387 /* If this symbol is not being provided by the linker script, and it is
2388 currently defined by a dynamic object, but not by a regular object,
2389 then clear out any version information because the symbol will not be
2390 associated with the dynamic object any more. */
2391 if (!provide
2392 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2393 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2394 h->verinfo.verdef = NULL;
2396 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2398 /* When possible, keep the original type of the symbol */
2399 if (h->type == STT_NOTYPE)
2400 h->type = STT_OBJECT;
2402 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2403 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2404 || info->shared)
2405 && h->dynindx == -1)
2407 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2408 return false;
2410 /* If this is a weak defined symbol, and we know a corresponding
2411 real symbol from the same dynamic object, make sure the real
2412 symbol is also made into a dynamic symbol. */
2413 if (h->weakdef != NULL
2414 && h->weakdef->dynindx == -1)
2416 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2417 return false;
2421 return true;
2424 /* This structure is used to pass information to
2425 elf_link_assign_sym_version. */
2427 struct elf_assign_sym_version_info
2429 /* Output BFD. */
2430 bfd *output_bfd;
2431 /* General link information. */
2432 struct bfd_link_info *info;
2433 /* Version tree. */
2434 struct bfd_elf_version_tree *verdefs;
2435 /* Whether we are exporting all dynamic symbols. */
2436 boolean export_dynamic;
2437 /* Whether we had a failure. */
2438 boolean failed;
2441 /* This structure is used to pass information to
2442 elf_link_find_version_dependencies. */
2444 struct elf_find_verdep_info
2446 /* Output BFD. */
2447 bfd *output_bfd;
2448 /* General link information. */
2449 struct bfd_link_info *info;
2450 /* The number of dependencies. */
2451 unsigned int vers;
2452 /* Whether we had a failure. */
2453 boolean failed;
2456 /* Array used to determine the number of hash table buckets to use
2457 based on the number of symbols there are. If there are fewer than
2458 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2459 fewer than 37 we use 17 buckets, and so forth. We never use more
2460 than 32771 buckets. */
2462 static const size_t elf_buckets[] =
2464 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2465 16411, 32771, 0
2468 /* Compute bucket count for hashing table. We do not use a static set
2469 of possible tables sizes anymore. Instead we determine for all
2470 possible reasonable sizes of the table the outcome (i.e., the
2471 number of collisions etc) and choose the best solution. The
2472 weighting functions are not too simple to allow the table to grow
2473 without bounds. Instead one of the weighting factors is the size.
2474 Therefore the result is always a good payoff between few collisions
2475 (= short chain lengths) and table size. */
2476 static size_t
2477 compute_bucket_count (info)
2478 struct bfd_link_info *info;
2480 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2481 size_t best_size = 0;
2482 unsigned long int *hashcodes;
2483 unsigned long int *hashcodesp;
2484 unsigned long int i;
2486 /* Compute the hash values for all exported symbols. At the same
2487 time store the values in an array so that we could use them for
2488 optimizations. */
2489 hashcodes = (unsigned long int *) bfd_malloc (dynsymcount
2490 * sizeof (unsigned long int));
2491 if (hashcodes == NULL)
2492 return 0;
2493 hashcodesp = hashcodes;
2495 /* Put all hash values in HASHCODES. */
2496 elf_link_hash_traverse (elf_hash_table (info),
2497 elf_collect_hash_codes, &hashcodesp);
2499 /* We have a problem here. The following code to optimize the table
2500 size requires an integer type with more the 32 bits. If
2501 BFD_HOST_U_64_BIT is set we know about such a type. */
2502 #ifdef BFD_HOST_U_64_BIT
2503 if (info->optimize == true)
2505 unsigned long int nsyms = hashcodesp - hashcodes;
2506 size_t minsize;
2507 size_t maxsize;
2508 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2509 unsigned long int *counts ;
2511 /* Possible optimization parameters: if we have NSYMS symbols we say
2512 that the hashing table must at least have NSYMS/4 and at most
2513 2*NSYMS buckets. */
2514 minsize = nsyms / 4;
2515 if (minsize == 0)
2516 minsize = 1;
2517 best_size = maxsize = nsyms * 2;
2519 /* Create array where we count the collisions in. We must use bfd_malloc
2520 since the size could be large. */
2521 counts = (unsigned long int *) bfd_malloc (maxsize
2522 * sizeof (unsigned long int));
2523 if (counts == NULL)
2525 free (hashcodes);
2526 return 0;
2529 /* Compute the "optimal" size for the hash table. The criteria is a
2530 minimal chain length. The minor criteria is (of course) the size
2531 of the table. */
2532 for (i = minsize; i < maxsize; ++i)
2534 /* Walk through the array of hashcodes and count the collisions. */
2535 BFD_HOST_U_64_BIT max;
2536 unsigned long int j;
2537 unsigned long int fact;
2539 memset (counts, '\0', i * sizeof (unsigned long int));
2541 /* Determine how often each hash bucket is used. */
2542 for (j = 0; j < nsyms; ++j)
2543 ++counts[hashcodes[j] % i];
2545 /* For the weight function we need some information about the
2546 pagesize on the target. This is information need not be 100%
2547 accurate. Since this information is not available (so far) we
2548 define it here to a reasonable default value. If it is crucial
2549 to have a better value some day simply define this value. */
2550 # ifndef BFD_TARGET_PAGESIZE
2551 # define BFD_TARGET_PAGESIZE (4096)
2552 # endif
2554 /* We in any case need 2 + NSYMS entries for the size values and
2555 the chains. */
2556 max = (2 + nsyms) * (ARCH_SIZE / 8);
2558 # if 1
2559 /* Variant 1: optimize for short chains. We add the squares
2560 of all the chain lengths (which favous many small chain
2561 over a few long chains). */
2562 for (j = 0; j < i; ++j)
2563 max += counts[j] * counts[j];
2565 /* This adds penalties for the overall size of the table. */
2566 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2567 max *= fact * fact;
2568 # else
2569 /* Variant 2: Optimize a lot more for small table. Here we
2570 also add squares of the size but we also add penalties for
2571 empty slots (the +1 term). */
2572 for (j = 0; j < i; ++j)
2573 max += (1 + counts[j]) * (1 + counts[j]);
2575 /* The overall size of the table is considered, but not as
2576 strong as in variant 1, where it is squared. */
2577 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2578 max *= fact;
2579 # endif
2581 /* Compare with current best results. */
2582 if (max < best_chlen)
2584 best_chlen = max;
2585 best_size = i;
2589 free (counts);
2591 else
2592 #endif /* defined (BFD_HOST_U_64_BIT) */
2594 /* This is the fallback solution if no 64bit type is available or if we
2595 are not supposed to spend much time on optimizations. We select the
2596 bucket count using a fixed set of numbers. */
2597 for (i = 0; elf_buckets[i] != 0; i++)
2599 best_size = elf_buckets[i];
2600 if (dynsymcount < elf_buckets[i + 1])
2601 break;
2605 /* Free the arrays we needed. */
2606 free (hashcodes);
2608 return best_size;
2611 /* Set up the sizes and contents of the ELF dynamic sections. This is
2612 called by the ELF linker emulation before_allocation routine. We
2613 must set the sizes of the sections before the linker sets the
2614 addresses of the various sections. */
2616 boolean
2617 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2618 export_dynamic, filter_shlib,
2619 auxiliary_filters, info, sinterpptr,
2620 verdefs)
2621 bfd *output_bfd;
2622 const char *soname;
2623 const char *rpath;
2624 boolean export_dynamic;
2625 const char *filter_shlib;
2626 const char * const *auxiliary_filters;
2627 struct bfd_link_info *info;
2628 asection **sinterpptr;
2629 struct bfd_elf_version_tree *verdefs;
2631 bfd_size_type soname_indx;
2632 bfd *dynobj;
2633 struct elf_backend_data *bed;
2634 struct elf_assign_sym_version_info asvinfo;
2636 *sinterpptr = NULL;
2638 soname_indx = (bfd_size_type) -1;
2640 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2641 return true;
2643 /* The backend may have to create some sections regardless of whether
2644 we're dynamic or not. */
2645 bed = get_elf_backend_data (output_bfd);
2646 if (bed->elf_backend_always_size_sections
2647 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2648 return false;
2650 dynobj = elf_hash_table (info)->dynobj;
2652 /* If there were no dynamic objects in the link, there is nothing to
2653 do here. */
2654 if (dynobj == NULL)
2655 return true;
2657 /* If we are supposed to export all symbols into the dynamic symbol
2658 table (this is not the normal case), then do so. */
2659 if (export_dynamic)
2661 struct elf_info_failed eif;
2663 eif.failed = false;
2664 eif.info = info;
2665 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
2666 (PTR) &eif);
2667 if (eif.failed)
2668 return false;
2671 if (elf_hash_table (info)->dynamic_sections_created)
2673 struct elf_info_failed eif;
2674 struct elf_link_hash_entry *h;
2675 bfd_size_type strsize;
2677 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2678 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2680 if (soname != NULL)
2682 soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2683 soname, true, true);
2684 if (soname_indx == (bfd_size_type) -1
2685 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
2686 return false;
2689 if (info->symbolic)
2691 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
2692 return false;
2695 if (rpath != NULL)
2697 bfd_size_type indx;
2699 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
2700 true, true);
2701 if (indx == (bfd_size_type) -1
2702 || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
2703 return false;
2706 if (filter_shlib != NULL)
2708 bfd_size_type indx;
2710 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2711 filter_shlib, true, true);
2712 if (indx == (bfd_size_type) -1
2713 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
2714 return false;
2717 if (auxiliary_filters != NULL)
2719 const char * const *p;
2721 for (p = auxiliary_filters; *p != NULL; p++)
2723 bfd_size_type indx;
2725 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2726 *p, true, true);
2727 if (indx == (bfd_size_type) -1
2728 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
2729 return false;
2733 /* Attach all the symbols to their version information. */
2734 asvinfo.output_bfd = output_bfd;
2735 asvinfo.info = info;
2736 asvinfo.verdefs = verdefs;
2737 asvinfo.export_dynamic = export_dynamic;
2738 asvinfo.failed = false;
2740 elf_link_hash_traverse (elf_hash_table (info),
2741 elf_link_assign_sym_version,
2742 (PTR) &asvinfo);
2743 if (asvinfo.failed)
2744 return false;
2746 /* Find all symbols which were defined in a dynamic object and make
2747 the backend pick a reasonable value for them. */
2748 eif.failed = false;
2749 eif.info = info;
2750 elf_link_hash_traverse (elf_hash_table (info),
2751 elf_adjust_dynamic_symbol,
2752 (PTR) &eif);
2753 if (eif.failed)
2754 return false;
2756 /* Add some entries to the .dynamic section. We fill in some of the
2757 values later, in elf_bfd_final_link, but we must add the entries
2758 now so that we know the final size of the .dynamic section. */
2760 /* If there are initialization and/or finalization functions to
2761 call then add the corresponding DT_INIT/DT_FINI entries. */
2762 h = (info->init_function
2763 ? elf_link_hash_lookup (elf_hash_table (info),
2764 info->init_function, false,
2765 false, false)
2766 : NULL);
2767 if (h != NULL
2768 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2769 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2771 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
2772 return false;
2774 h = (info->fini_function
2775 ? elf_link_hash_lookup (elf_hash_table (info),
2776 info->fini_function, false,
2777 false, false)
2778 : NULL);
2779 if (h != NULL
2780 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2781 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2783 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
2784 return false;
2787 strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
2788 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
2789 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
2790 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
2791 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
2792 || ! elf_add_dynamic_entry (info, DT_SYMENT,
2793 sizeof (Elf_External_Sym)))
2794 return false;
2797 /* The backend must work out the sizes of all the other dynamic
2798 sections. */
2799 if (bed->elf_backend_size_dynamic_sections
2800 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
2801 return false;
2803 if (elf_hash_table (info)->dynamic_sections_created)
2805 size_t dynsymcount;
2806 asection *s;
2807 size_t bucketcount = 0;
2808 Elf_Internal_Sym isym;
2809 size_t hash_entry_size;
2811 /* Set up the version definition section. */
2812 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2813 BFD_ASSERT (s != NULL);
2815 /* We may have created additional version definitions if we are
2816 just linking a regular application. */
2817 verdefs = asvinfo.verdefs;
2819 if (verdefs == NULL)
2820 _bfd_strip_section_from_output (s);
2821 else
2823 unsigned int cdefs;
2824 bfd_size_type size;
2825 struct bfd_elf_version_tree *t;
2826 bfd_byte *p;
2827 Elf_Internal_Verdef def;
2828 Elf_Internal_Verdaux defaux;
2830 cdefs = 0;
2831 size = 0;
2833 /* Make space for the base version. */
2834 size += sizeof (Elf_External_Verdef);
2835 size += sizeof (Elf_External_Verdaux);
2836 ++cdefs;
2838 for (t = verdefs; t != NULL; t = t->next)
2840 struct bfd_elf_version_deps *n;
2842 size += sizeof (Elf_External_Verdef);
2843 size += sizeof (Elf_External_Verdaux);
2844 ++cdefs;
2846 for (n = t->deps; n != NULL; n = n->next)
2847 size += sizeof (Elf_External_Verdaux);
2850 s->_raw_size = size;
2851 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2852 if (s->contents == NULL && s->_raw_size != 0)
2853 return false;
2855 /* Fill in the version definition section. */
2857 p = s->contents;
2859 def.vd_version = VER_DEF_CURRENT;
2860 def.vd_flags = VER_FLG_BASE;
2861 def.vd_ndx = 1;
2862 def.vd_cnt = 1;
2863 def.vd_aux = sizeof (Elf_External_Verdef);
2864 def.vd_next = (sizeof (Elf_External_Verdef)
2865 + sizeof (Elf_External_Verdaux));
2867 if (soname_indx != (bfd_size_type) -1)
2869 def.vd_hash = bfd_elf_hash (soname);
2870 defaux.vda_name = soname_indx;
2872 else
2874 const char *name;
2875 bfd_size_type indx;
2877 name = output_bfd->filename;
2878 def.vd_hash = bfd_elf_hash (name);
2879 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2880 name, true, false);
2881 if (indx == (bfd_size_type) -1)
2882 return false;
2883 defaux.vda_name = indx;
2885 defaux.vda_next = 0;
2887 _bfd_elf_swap_verdef_out (output_bfd, &def,
2888 (Elf_External_Verdef *)p);
2889 p += sizeof (Elf_External_Verdef);
2890 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2891 (Elf_External_Verdaux *) p);
2892 p += sizeof (Elf_External_Verdaux);
2894 for (t = verdefs; t != NULL; t = t->next)
2896 unsigned int cdeps;
2897 struct bfd_elf_version_deps *n;
2898 struct elf_link_hash_entry *h;
2900 cdeps = 0;
2901 for (n = t->deps; n != NULL; n = n->next)
2902 ++cdeps;
2904 /* Add a symbol representing this version. */
2905 h = NULL;
2906 if (! (_bfd_generic_link_add_one_symbol
2907 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2908 (bfd_vma) 0, (const char *) NULL, false,
2909 get_elf_backend_data (dynobj)->collect,
2910 (struct bfd_link_hash_entry **) &h)))
2911 return false;
2912 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2913 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2914 h->type = STT_OBJECT;
2915 h->verinfo.vertree = t;
2917 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2918 return false;
2920 def.vd_version = VER_DEF_CURRENT;
2921 def.vd_flags = 0;
2922 if (t->globals == NULL && t->locals == NULL && ! t->used)
2923 def.vd_flags |= VER_FLG_WEAK;
2924 def.vd_ndx = t->vernum + 1;
2925 def.vd_cnt = cdeps + 1;
2926 def.vd_hash = bfd_elf_hash (t->name);
2927 def.vd_aux = sizeof (Elf_External_Verdef);
2928 if (t->next != NULL)
2929 def.vd_next = (sizeof (Elf_External_Verdef)
2930 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2931 else
2932 def.vd_next = 0;
2934 _bfd_elf_swap_verdef_out (output_bfd, &def,
2935 (Elf_External_Verdef *) p);
2936 p += sizeof (Elf_External_Verdef);
2938 defaux.vda_name = h->dynstr_index;
2939 if (t->deps == NULL)
2940 defaux.vda_next = 0;
2941 else
2942 defaux.vda_next = sizeof (Elf_External_Verdaux);
2943 t->name_indx = defaux.vda_name;
2945 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2946 (Elf_External_Verdaux *) p);
2947 p += sizeof (Elf_External_Verdaux);
2949 for (n = t->deps; n != NULL; n = n->next)
2951 if (n->version_needed == NULL)
2953 /* This can happen if there was an error in the
2954 version script. */
2955 defaux.vda_name = 0;
2957 else
2958 defaux.vda_name = n->version_needed->name_indx;
2959 if (n->next == NULL)
2960 defaux.vda_next = 0;
2961 else
2962 defaux.vda_next = sizeof (Elf_External_Verdaux);
2964 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2965 (Elf_External_Verdaux *) p);
2966 p += sizeof (Elf_External_Verdaux);
2970 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2971 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2972 return false;
2974 elf_tdata (output_bfd)->cverdefs = cdefs;
2977 /* Work out the size of the version reference section. */
2979 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2980 BFD_ASSERT (s != NULL);
2982 struct elf_find_verdep_info sinfo;
2984 sinfo.output_bfd = output_bfd;
2985 sinfo.info = info;
2986 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2987 if (sinfo.vers == 0)
2988 sinfo.vers = 1;
2989 sinfo.failed = false;
2991 elf_link_hash_traverse (elf_hash_table (info),
2992 elf_link_find_version_dependencies,
2993 (PTR) &sinfo);
2995 if (elf_tdata (output_bfd)->verref == NULL)
2996 _bfd_strip_section_from_output (s);
2997 else
2999 Elf_Internal_Verneed *t;
3000 unsigned int size;
3001 unsigned int crefs;
3002 bfd_byte *p;
3004 /* Build the version definition section. */
3005 size = 0;
3006 crefs = 0;
3007 for (t = elf_tdata (output_bfd)->verref;
3008 t != NULL;
3009 t = t->vn_nextref)
3011 Elf_Internal_Vernaux *a;
3013 size += sizeof (Elf_External_Verneed);
3014 ++crefs;
3015 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3016 size += sizeof (Elf_External_Vernaux);
3019 s->_raw_size = size;
3020 s->contents = (bfd_byte *) bfd_alloc (output_bfd, size);
3021 if (s->contents == NULL)
3022 return false;
3024 p = s->contents;
3025 for (t = elf_tdata (output_bfd)->verref;
3026 t != NULL;
3027 t = t->vn_nextref)
3029 unsigned int caux;
3030 Elf_Internal_Vernaux *a;
3031 bfd_size_type indx;
3033 caux = 0;
3034 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3035 ++caux;
3037 t->vn_version = VER_NEED_CURRENT;
3038 t->vn_cnt = caux;
3039 if (elf_dt_name (t->vn_bfd) != NULL)
3040 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3041 elf_dt_name (t->vn_bfd),
3042 true, false);
3043 else
3044 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3045 t->vn_bfd->filename, true, false);
3046 if (indx == (bfd_size_type) -1)
3047 return false;
3048 t->vn_file = indx;
3049 t->vn_aux = sizeof (Elf_External_Verneed);
3050 if (t->vn_nextref == NULL)
3051 t->vn_next = 0;
3052 else
3053 t->vn_next = (sizeof (Elf_External_Verneed)
3054 + caux * sizeof (Elf_External_Vernaux));
3056 _bfd_elf_swap_verneed_out (output_bfd, t,
3057 (Elf_External_Verneed *) p);
3058 p += sizeof (Elf_External_Verneed);
3060 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3062 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3063 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3064 a->vna_nodename, true, false);
3065 if (indx == (bfd_size_type) -1)
3066 return false;
3067 a->vna_name = indx;
3068 if (a->vna_nextptr == NULL)
3069 a->vna_next = 0;
3070 else
3071 a->vna_next = sizeof (Elf_External_Vernaux);
3073 _bfd_elf_swap_vernaux_out (output_bfd, a,
3074 (Elf_External_Vernaux *) p);
3075 p += sizeof (Elf_External_Vernaux);
3079 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
3080 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
3081 return false;
3083 elf_tdata (output_bfd)->cverrefs = crefs;
3087 /* Assign dynsym indicies. In a shared library we generate a
3088 section symbol for each output section, which come first.
3089 Next come all of the back-end allocated local dynamic syms,
3090 followed by the rest of the global symbols. */
3092 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3094 /* Work out the size of the symbol version section. */
3095 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3096 BFD_ASSERT (s != NULL);
3097 if (dynsymcount == 0
3098 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3100 _bfd_strip_section_from_output (s);
3101 /* The DYNSYMCOUNT might have changed if we were going to
3102 output a dynamic symbol table entry for S. */
3103 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3105 else
3107 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3108 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3109 if (s->contents == NULL)
3110 return false;
3112 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
3113 return false;
3116 /* Set the size of the .dynsym and .hash sections. We counted
3117 the number of dynamic symbols in elf_link_add_object_symbols.
3118 We will build the contents of .dynsym and .hash when we build
3119 the final symbol table, because until then we do not know the
3120 correct value to give the symbols. We built the .dynstr
3121 section as we went along in elf_link_add_object_symbols. */
3122 s = bfd_get_section_by_name (dynobj, ".dynsym");
3123 BFD_ASSERT (s != NULL);
3124 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3125 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3126 if (s->contents == NULL && s->_raw_size != 0)
3127 return false;
3129 /* The first entry in .dynsym is a dummy symbol. */
3130 isym.st_value = 0;
3131 isym.st_size = 0;
3132 isym.st_name = 0;
3133 isym.st_info = 0;
3134 isym.st_other = 0;
3135 isym.st_shndx = 0;
3136 elf_swap_symbol_out (output_bfd, &isym,
3137 (PTR) (Elf_External_Sym *) s->contents);
3139 /* Compute the size of the hashing table. As a side effect this
3140 computes the hash values for all the names we export. */
3141 bucketcount = compute_bucket_count (info);
3143 s = bfd_get_section_by_name (dynobj, ".hash");
3144 BFD_ASSERT (s != NULL);
3145 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3146 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3147 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3148 if (s->contents == NULL)
3149 return false;
3150 memset (s->contents, 0, (size_t) s->_raw_size);
3152 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
3153 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
3154 s->contents + hash_entry_size);
3156 elf_hash_table (info)->bucketcount = bucketcount;
3158 s = bfd_get_section_by_name (dynobj, ".dynstr");
3159 BFD_ASSERT (s != NULL);
3160 s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
3162 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
3163 return false;
3166 return true;
3169 /* Fix up the flags for a symbol. This handles various cases which
3170 can only be fixed after all the input files are seen. This is
3171 currently called by both adjust_dynamic_symbol and
3172 assign_sym_version, which is unnecessary but perhaps more robust in
3173 the face of future changes. */
3175 static boolean
3176 elf_fix_symbol_flags (h, eif)
3177 struct elf_link_hash_entry *h;
3178 struct elf_info_failed *eif;
3180 /* If this symbol was mentioned in a non-ELF file, try to set
3181 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3182 permit a non-ELF file to correctly refer to a symbol defined in
3183 an ELF dynamic object. */
3184 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3186 if (h->root.type != bfd_link_hash_defined
3187 && h->root.type != bfd_link_hash_defweak)
3188 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3189 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3190 else
3192 if (h->root.u.def.section->owner != NULL
3193 && (bfd_get_flavour (h->root.u.def.section->owner)
3194 == bfd_target_elf_flavour))
3195 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3196 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3197 else
3198 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3201 if (h->dynindx == -1
3202 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3203 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3205 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3207 eif->failed = true;
3208 return false;
3212 else
3214 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3215 was first seen in a non-ELF file. Fortunately, if the symbol
3216 was first seen in an ELF file, we're probably OK unless the
3217 symbol was defined in a non-ELF file. Catch that case here.
3218 FIXME: We're still in trouble if the symbol was first seen in
3219 a dynamic object, and then later in a non-ELF regular object. */
3220 if ((h->root.type == bfd_link_hash_defined
3221 || h->root.type == bfd_link_hash_defweak)
3222 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3223 && (h->root.u.def.section->owner != NULL
3224 ? (bfd_get_flavour (h->root.u.def.section->owner)
3225 != bfd_target_elf_flavour)
3226 : (bfd_is_abs_section (h->root.u.def.section)
3227 && (h->elf_link_hash_flags
3228 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3229 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3232 /* If this is a final link, and the symbol was defined as a common
3233 symbol in a regular object file, and there was no definition in
3234 any dynamic object, then the linker will have allocated space for
3235 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3236 flag will not have been set. */
3237 if (h->root.type == bfd_link_hash_defined
3238 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3239 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3240 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3241 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3242 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3244 /* If -Bsymbolic was used (which means to bind references to global
3245 symbols to the definition within the shared object), and this
3246 symbol was defined in a regular object, then it actually doesn't
3247 need a PLT entry. */
3248 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3249 && eif->info->shared
3250 && eif->info->symbolic
3251 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3253 h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
3254 h->plt.offset = (bfd_vma) -1;
3257 /* If this is a weak defined symbol in a dynamic object, and we know
3258 the real definition in the dynamic object, copy interesting flags
3259 over to the real definition. */
3260 if (h->weakdef != NULL)
3262 struct elf_link_hash_entry *weakdef;
3264 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3265 || h->root.type == bfd_link_hash_defweak);
3266 weakdef = h->weakdef;
3267 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3268 || weakdef->root.type == bfd_link_hash_defweak);
3269 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3271 /* If the real definition is defined by a regular object file,
3272 don't do anything special. See the longer description in
3273 elf_adjust_dynamic_symbol, below. */
3274 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3275 h->weakdef = NULL;
3276 else
3277 weakdef->elf_link_hash_flags |=
3278 (h->elf_link_hash_flags
3279 & (ELF_LINK_HASH_REF_REGULAR
3280 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3281 | ELF_LINK_NON_GOT_REF));
3284 return true;
3287 /* Make the backend pick a good value for a dynamic symbol. This is
3288 called via elf_link_hash_traverse, and also calls itself
3289 recursively. */
3291 static boolean
3292 elf_adjust_dynamic_symbol (h, data)
3293 struct elf_link_hash_entry *h;
3294 PTR data;
3296 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3297 bfd *dynobj;
3298 struct elf_backend_data *bed;
3300 /* Ignore indirect symbols. These are added by the versioning code. */
3301 if (h->root.type == bfd_link_hash_indirect)
3302 return true;
3304 /* Fix the symbol flags. */
3305 if (! elf_fix_symbol_flags (h, eif))
3306 return false;
3308 /* If this symbol does not require a PLT entry, and it is not
3309 defined by a dynamic object, or is not referenced by a regular
3310 object, ignore it. We do have to handle a weak defined symbol,
3311 even if no regular object refers to it, if we decided to add it
3312 to the dynamic symbol table. FIXME: Do we normally need to worry
3313 about symbols which are defined by one dynamic object and
3314 referenced by another one? */
3315 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3316 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3317 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3318 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3319 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3321 h->plt.offset = (bfd_vma) -1;
3322 return true;
3325 /* If we've already adjusted this symbol, don't do it again. This
3326 can happen via a recursive call. */
3327 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3328 return true;
3330 /* Don't look at this symbol again. Note that we must set this
3331 after checking the above conditions, because we may look at a
3332 symbol once, decide not to do anything, and then get called
3333 recursively later after REF_REGULAR is set below. */
3334 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3336 /* If this is a weak definition, and we know a real definition, and
3337 the real symbol is not itself defined by a regular object file,
3338 then get a good value for the real definition. We handle the
3339 real symbol first, for the convenience of the backend routine.
3341 Note that there is a confusing case here. If the real definition
3342 is defined by a regular object file, we don't get the real symbol
3343 from the dynamic object, but we do get the weak symbol. If the
3344 processor backend uses a COPY reloc, then if some routine in the
3345 dynamic object changes the real symbol, we will not see that
3346 change in the corresponding weak symbol. This is the way other
3347 ELF linkers work as well, and seems to be a result of the shared
3348 library model.
3350 I will clarify this issue. Most SVR4 shared libraries define the
3351 variable _timezone and define timezone as a weak synonym. The
3352 tzset call changes _timezone. If you write
3353 extern int timezone;
3354 int _timezone = 5;
3355 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3356 you might expect that, since timezone is a synonym for _timezone,
3357 the same number will print both times. However, if the processor
3358 backend uses a COPY reloc, then actually timezone will be copied
3359 into your process image, and, since you define _timezone
3360 yourself, _timezone will not. Thus timezone and _timezone will
3361 wind up at different memory locations. The tzset call will set
3362 _timezone, leaving timezone unchanged. */
3364 if (h->weakdef != NULL)
3366 /* If we get to this point, we know there is an implicit
3367 reference by a regular object file via the weak symbol H.
3368 FIXME: Is this really true? What if the traversal finds
3369 H->WEAKDEF before it finds H? */
3370 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3372 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
3373 return false;
3376 /* If a symbol has no type and no size and does not require a PLT
3377 entry, then we are probably about to do the wrong thing here: we
3378 are probably going to create a COPY reloc for an empty object.
3379 This case can arise when a shared object is built with assembly
3380 code, and the assembly code fails to set the symbol type. */
3381 if (h->size == 0
3382 && h->type == STT_NOTYPE
3383 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3384 (*_bfd_error_handler)
3385 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3386 h->root.root.string);
3388 dynobj = elf_hash_table (eif->info)->dynobj;
3389 bed = get_elf_backend_data (dynobj);
3390 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3392 eif->failed = true;
3393 return false;
3396 return true;
3399 /* This routine is used to export all defined symbols into the dynamic
3400 symbol table. It is called via elf_link_hash_traverse. */
3402 static boolean
3403 elf_export_symbol (h, data)
3404 struct elf_link_hash_entry *h;
3405 PTR data;
3407 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3409 /* Ignore indirect symbols. These are added by the versioning code. */
3410 if (h->root.type == bfd_link_hash_indirect)
3411 return true;
3413 if (h->dynindx == -1
3414 && (h->elf_link_hash_flags
3415 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
3417 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3419 eif->failed = true;
3420 return false;
3424 return true;
3427 /* Look through the symbols which are defined in other shared
3428 libraries and referenced here. Update the list of version
3429 dependencies. This will be put into the .gnu.version_r section.
3430 This function is called via elf_link_hash_traverse. */
3432 static boolean
3433 elf_link_find_version_dependencies (h, data)
3434 struct elf_link_hash_entry *h;
3435 PTR data;
3437 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
3438 Elf_Internal_Verneed *t;
3439 Elf_Internal_Vernaux *a;
3441 /* We only care about symbols defined in shared objects with version
3442 information. */
3443 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3444 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3445 || h->dynindx == -1
3446 || h->verinfo.verdef == NULL)
3447 return true;
3449 /* See if we already know about this version. */
3450 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
3452 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
3453 continue;
3455 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3456 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
3457 return true;
3459 break;
3462 /* This is a new version. Add it to tree we are building. */
3464 if (t == NULL)
3466 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t);
3467 if (t == NULL)
3469 rinfo->failed = true;
3470 return false;
3473 t->vn_bfd = h->verinfo.verdef->vd_bfd;
3474 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
3475 elf_tdata (rinfo->output_bfd)->verref = t;
3478 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a);
3480 /* Note that we are copying a string pointer here, and testing it
3481 above. If bfd_elf_string_from_elf_section is ever changed to
3482 discard the string data when low in memory, this will have to be
3483 fixed. */
3484 a->vna_nodename = h->verinfo.verdef->vd_nodename;
3486 a->vna_flags = h->verinfo.verdef->vd_flags;
3487 a->vna_nextptr = t->vn_auxptr;
3489 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
3490 ++rinfo->vers;
3492 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
3494 t->vn_auxptr = a;
3496 return true;
3499 /* Figure out appropriate versions for all the symbols. We may not
3500 have the version number script until we have read all of the input
3501 files, so until that point we don't know which symbols should be
3502 local. This function is called via elf_link_hash_traverse. */
3504 static boolean
3505 elf_link_assign_sym_version (h, data)
3506 struct elf_link_hash_entry *h;
3507 PTR data;
3509 struct elf_assign_sym_version_info *sinfo =
3510 (struct elf_assign_sym_version_info *) data;
3511 struct bfd_link_info *info = sinfo->info;
3512 struct elf_info_failed eif;
3513 char *p;
3515 /* Fix the symbol flags. */
3516 eif.failed = false;
3517 eif.info = info;
3518 if (! elf_fix_symbol_flags (h, &eif))
3520 if (eif.failed)
3521 sinfo->failed = true;
3522 return false;
3525 /* We only need version numbers for symbols defined in regular
3526 objects. */
3527 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3528 return true;
3530 p = strchr (h->root.root.string, ELF_VER_CHR);
3531 if (p != NULL && h->verinfo.vertree == NULL)
3533 struct bfd_elf_version_tree *t;
3534 boolean hidden;
3536 hidden = true;
3538 /* There are two consecutive ELF_VER_CHR characters if this is
3539 not a hidden symbol. */
3540 ++p;
3541 if (*p == ELF_VER_CHR)
3543 hidden = false;
3544 ++p;
3547 /* If there is no version string, we can just return out. */
3548 if (*p == '\0')
3550 if (hidden)
3551 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3552 return true;
3555 /* Look for the version. If we find it, it is no longer weak. */
3556 for (t = sinfo->verdefs; t != NULL; t = t->next)
3558 if (strcmp (t->name, p) == 0)
3560 int len;
3561 char *alc;
3562 struct bfd_elf_version_expr *d;
3564 len = p - h->root.root.string;
3565 alc = bfd_alloc (sinfo->output_bfd, len);
3566 if (alc == NULL)
3567 return false;
3568 strncpy (alc, h->root.root.string, len - 1);
3569 alc[len - 1] = '\0';
3570 if (alc[len - 2] == ELF_VER_CHR)
3571 alc[len - 2] = '\0';
3573 h->verinfo.vertree = t;
3574 t->used = true;
3575 d = NULL;
3577 if (t->globals != NULL)
3579 for (d = t->globals; d != NULL; d = d->next)
3580 if ((*d->match) (d, alc))
3581 break;
3584 /* See if there is anything to force this symbol to
3585 local scope. */
3586 if (d == NULL && t->locals != NULL)
3588 for (d = t->locals; d != NULL; d = d->next)
3590 if ((*d->match) (d, alc))
3592 if (h->dynindx != -1
3593 && info->shared
3594 && ! sinfo->export_dynamic)
3596 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3597 h->elf_link_hash_flags &=~
3598 ELF_LINK_HASH_NEEDS_PLT;
3599 h->dynindx = -1;
3600 h->plt.offset = (bfd_vma) -1;
3601 /* FIXME: The name of the symbol has
3602 already been recorded in the dynamic
3603 string table section. */
3606 break;
3611 bfd_release (sinfo->output_bfd, alc);
3612 break;
3616 /* If we are building an application, we need to create a
3617 version node for this version. */
3618 if (t == NULL && ! info->shared)
3620 struct bfd_elf_version_tree **pp;
3621 int version_index;
3623 /* If we aren't going to export this symbol, we don't need
3624 to worry about it. */
3625 if (h->dynindx == -1)
3626 return true;
3628 t = ((struct bfd_elf_version_tree *)
3629 bfd_alloc (sinfo->output_bfd, sizeof *t));
3630 if (t == NULL)
3632 sinfo->failed = true;
3633 return false;
3636 t->next = NULL;
3637 t->name = p;
3638 t->globals = NULL;
3639 t->locals = NULL;
3640 t->deps = NULL;
3641 t->name_indx = (unsigned int) -1;
3642 t->used = true;
3644 version_index = 1;
3645 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
3646 ++version_index;
3647 t->vernum = version_index;
3649 *pp = t;
3651 h->verinfo.vertree = t;
3653 else if (t == NULL)
3655 /* We could not find the version for a symbol when
3656 generating a shared archive. Return an error. */
3657 (*_bfd_error_handler)
3658 (_("%s: undefined versioned symbol name %s"),
3659 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
3660 bfd_set_error (bfd_error_bad_value);
3661 sinfo->failed = true;
3662 return false;
3665 if (hidden)
3666 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3669 /* If we don't have a version for this symbol, see if we can find
3670 something. */
3671 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
3673 struct bfd_elf_version_tree *t;
3674 struct bfd_elf_version_tree *deflt;
3675 struct bfd_elf_version_expr *d;
3677 /* See if can find what version this symbol is in. If the
3678 symbol is supposed to be local, then don't actually register
3679 it. */
3680 deflt = NULL;
3681 for (t = sinfo->verdefs; t != NULL; t = t->next)
3683 if (t->globals != NULL)
3685 for (d = t->globals; d != NULL; d = d->next)
3687 if ((*d->match) (d, h->root.root.string))
3689 h->verinfo.vertree = t;
3690 break;
3694 if (d != NULL)
3695 break;
3698 if (t->locals != NULL)
3700 for (d = t->locals; d != NULL; d = d->next)
3702 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
3703 deflt = t;
3704 else if ((*d->match) (d, h->root.root.string))
3706 h->verinfo.vertree = t;
3707 if (h->dynindx != -1
3708 && info->shared
3709 && ! sinfo->export_dynamic)
3711 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3712 h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
3713 h->dynindx = -1;
3714 h->plt.offset = (bfd_vma) -1;
3715 /* FIXME: The name of the symbol has already
3716 been recorded in the dynamic string table
3717 section. */
3719 break;
3723 if (d != NULL)
3724 break;
3728 if (deflt != NULL && h->verinfo.vertree == NULL)
3730 h->verinfo.vertree = deflt;
3731 if (h->dynindx != -1
3732 && info->shared
3733 && ! sinfo->export_dynamic)
3735 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3736 h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
3737 h->dynindx = -1;
3738 h->plt.offset = (bfd_vma) -1;
3739 /* FIXME: The name of the symbol has already been
3740 recorded in the dynamic string table section. */
3745 return true;
3748 /* Final phase of ELF linker. */
3750 /* A structure we use to avoid passing large numbers of arguments. */
3752 struct elf_final_link_info
3754 /* General link information. */
3755 struct bfd_link_info *info;
3756 /* Output BFD. */
3757 bfd *output_bfd;
3758 /* Symbol string table. */
3759 struct bfd_strtab_hash *symstrtab;
3760 /* .dynsym section. */
3761 asection *dynsym_sec;
3762 /* .hash section. */
3763 asection *hash_sec;
3764 /* symbol version section (.gnu.version). */
3765 asection *symver_sec;
3766 /* Buffer large enough to hold contents of any section. */
3767 bfd_byte *contents;
3768 /* Buffer large enough to hold external relocs of any section. */
3769 PTR external_relocs;
3770 /* Buffer large enough to hold internal relocs of any section. */
3771 Elf_Internal_Rela *internal_relocs;
3772 /* Buffer large enough to hold external local symbols of any input
3773 BFD. */
3774 Elf_External_Sym *external_syms;
3775 /* Buffer large enough to hold internal local symbols of any input
3776 BFD. */
3777 Elf_Internal_Sym *internal_syms;
3778 /* Array large enough to hold a symbol index for each local symbol
3779 of any input BFD. */
3780 long *indices;
3781 /* Array large enough to hold a section pointer for each local
3782 symbol of any input BFD. */
3783 asection **sections;
3784 /* Buffer to hold swapped out symbols. */
3785 Elf_External_Sym *symbuf;
3786 /* Number of swapped out symbols in buffer. */
3787 size_t symbuf_count;
3788 /* Number of symbols which fit in symbuf. */
3789 size_t symbuf_size;
3792 static boolean elf_link_output_sym
3793 PARAMS ((struct elf_final_link_info *, const char *,
3794 Elf_Internal_Sym *, asection *));
3795 static boolean elf_link_flush_output_syms
3796 PARAMS ((struct elf_final_link_info *));
3797 static boolean elf_link_output_extsym
3798 PARAMS ((struct elf_link_hash_entry *, PTR));
3799 static boolean elf_link_input_bfd
3800 PARAMS ((struct elf_final_link_info *, bfd *));
3801 static boolean elf_reloc_link_order
3802 PARAMS ((bfd *, struct bfd_link_info *, asection *,
3803 struct bfd_link_order *));
3805 /* This struct is used to pass information to elf_link_output_extsym. */
3807 struct elf_outext_info
3809 boolean failed;
3810 boolean localsyms;
3811 struct elf_final_link_info *finfo;
3814 /* Compute the size of, and allocate space for, REL_HDR which is the
3815 section header for a section containing relocations for O. */
3817 static boolean
3818 elf_link_size_reloc_section (abfd, rel_hdr, o)
3819 bfd *abfd;
3820 Elf_Internal_Shdr *rel_hdr;
3821 asection *o;
3823 register struct elf_link_hash_entry **p, **pend;
3824 unsigned reloc_count;
3826 /* Figure out how many relocations there will be. */
3827 if (rel_hdr == &elf_section_data (o)->rel_hdr)
3828 reloc_count = elf_section_data (o)->rel_count;
3829 else
3830 reloc_count = elf_section_data (o)->rel_count2;
3832 /* That allows us to calculate the size of the section. */
3833 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
3835 /* The contents field must last into write_object_contents, so we
3836 allocate it with bfd_alloc rather than malloc. */
3837 rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
3838 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3839 return false;
3841 /* We only allocate one set of hash entries, so we only do it the
3842 first time we are called. */
3843 if (elf_section_data (o)->rel_hashes == NULL)
3845 p = ((struct elf_link_hash_entry **)
3846 bfd_malloc (o->reloc_count
3847 * sizeof (struct elf_link_hash_entry *)));
3848 if (p == NULL && o->reloc_count != 0)
3849 return false;
3851 elf_section_data (o)->rel_hashes = p;
3852 pend = p + o->reloc_count;
3853 for (; p < pend; p++)
3854 *p = NULL;
3857 return true;
3860 /* When performing a relocateable link, the input relocations are
3861 preserved. But, if they reference global symbols, the indices
3862 referenced must be updated. Update all the relocations in
3863 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
3865 static void
3866 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
3867 bfd *abfd;
3868 Elf_Internal_Shdr *rel_hdr;
3869 unsigned int count;
3870 struct elf_link_hash_entry **rel_hash;
3872 unsigned int i;
3874 for (i = 0; i < count; i++, rel_hash++)
3876 if (*rel_hash == NULL)
3877 continue;
3879 BFD_ASSERT ((*rel_hash)->indx >= 0);
3881 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
3883 Elf_External_Rel *erel;
3884 Elf_Internal_Rel irel;
3886 erel = (Elf_External_Rel *) rel_hdr->contents + i;
3887 elf_swap_reloc_in (abfd, erel, &irel);
3888 irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
3889 ELF_R_TYPE (irel.r_info));
3890 elf_swap_reloc_out (abfd, &irel, erel);
3892 else
3894 Elf_External_Rela *erela;
3895 Elf_Internal_Rela irela;
3897 BFD_ASSERT (rel_hdr->sh_entsize
3898 == sizeof (Elf_External_Rela));
3900 erela = (Elf_External_Rela *) rel_hdr->contents + i;
3901 elf_swap_reloca_in (abfd, erela, &irela);
3902 irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
3903 ELF_R_TYPE (irela.r_info));
3904 elf_swap_reloca_out (abfd, &irela, erela);
3909 /* Do the final step of an ELF link. */
3911 boolean
3912 elf_bfd_final_link (abfd, info)
3913 bfd *abfd;
3914 struct bfd_link_info *info;
3916 boolean dynamic;
3917 bfd *dynobj;
3918 struct elf_final_link_info finfo;
3919 register asection *o;
3920 register struct bfd_link_order *p;
3921 register bfd *sub;
3922 size_t max_contents_size;
3923 size_t max_external_reloc_size;
3924 size_t max_internal_reloc_count;
3925 size_t max_sym_count;
3926 file_ptr off;
3927 Elf_Internal_Sym elfsym;
3928 unsigned int i;
3929 Elf_Internal_Shdr *symtab_hdr;
3930 Elf_Internal_Shdr *symstrtab_hdr;
3931 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3932 struct elf_outext_info eoinfo;
3934 if (info->shared)
3935 abfd->flags |= DYNAMIC;
3937 dynamic = elf_hash_table (info)->dynamic_sections_created;
3938 dynobj = elf_hash_table (info)->dynobj;
3940 finfo.info = info;
3941 finfo.output_bfd = abfd;
3942 finfo.symstrtab = elf_stringtab_init ();
3943 if (finfo.symstrtab == NULL)
3944 return false;
3946 if (! dynamic)
3948 finfo.dynsym_sec = NULL;
3949 finfo.hash_sec = NULL;
3950 finfo.symver_sec = NULL;
3952 else
3954 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3955 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3956 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3957 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3958 /* Note that it is OK if symver_sec is NULL. */
3961 finfo.contents = NULL;
3962 finfo.external_relocs = NULL;
3963 finfo.internal_relocs = NULL;
3964 finfo.external_syms = NULL;
3965 finfo.internal_syms = NULL;
3966 finfo.indices = NULL;
3967 finfo.sections = NULL;
3968 finfo.symbuf = NULL;
3969 finfo.symbuf_count = 0;
3971 /* Count up the number of relocations we will output for each output
3972 section, so that we know the sizes of the reloc sections. We
3973 also figure out some maximum sizes. */
3974 max_contents_size = 0;
3975 max_external_reloc_size = 0;
3976 max_internal_reloc_count = 0;
3977 max_sym_count = 0;
3978 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3980 o->reloc_count = 0;
3982 for (p = o->link_order_head; p != NULL; p = p->next)
3984 if (p->type == bfd_section_reloc_link_order
3985 || p->type == bfd_symbol_reloc_link_order)
3986 ++o->reloc_count;
3987 else if (p->type == bfd_indirect_link_order)
3989 asection *sec;
3991 sec = p->u.indirect.section;
3993 /* Mark all sections which are to be included in the
3994 link. This will normally be every section. We need
3995 to do this so that we can identify any sections which
3996 the linker has decided to not include. */
3997 sec->linker_mark = true;
3999 if (info->relocateable)
4000 o->reloc_count += sec->reloc_count;
4002 if (sec->_raw_size > max_contents_size)
4003 max_contents_size = sec->_raw_size;
4004 if (sec->_cooked_size > max_contents_size)
4005 max_contents_size = sec->_cooked_size;
4007 /* We are interested in just local symbols, not all
4008 symbols. */
4009 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4010 && (sec->owner->flags & DYNAMIC) == 0)
4012 size_t sym_count;
4014 if (elf_bad_symtab (sec->owner))
4015 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4016 / sizeof (Elf_External_Sym));
4017 else
4018 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4020 if (sym_count > max_sym_count)
4021 max_sym_count = sym_count;
4023 if ((sec->flags & SEC_RELOC) != 0)
4025 size_t ext_size;
4027 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4028 if (ext_size > max_external_reloc_size)
4029 max_external_reloc_size = ext_size;
4030 if (sec->reloc_count > max_internal_reloc_count)
4031 max_internal_reloc_count = sec->reloc_count;
4037 if (o->reloc_count > 0)
4038 o->flags |= SEC_RELOC;
4039 else
4041 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4042 set it (this is probably a bug) and if it is set
4043 assign_section_numbers will create a reloc section. */
4044 o->flags &=~ SEC_RELOC;
4047 /* If the SEC_ALLOC flag is not set, force the section VMA to
4048 zero. This is done in elf_fake_sections as well, but forcing
4049 the VMA to 0 here will ensure that relocs against these
4050 sections are handled correctly. */
4051 if ((o->flags & SEC_ALLOC) == 0
4052 && ! o->user_set_vma)
4053 o->vma = 0;
4056 /* Figure out the file positions for everything but the symbol table
4057 and the relocs. We set symcount to force assign_section_numbers
4058 to create a symbol table. */
4059 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
4060 BFD_ASSERT (! abfd->output_has_begun);
4061 if (! _bfd_elf_compute_section_file_positions (abfd, info))
4062 goto error_return;
4064 /* Figure out how many relocations we will have in each section.
4065 Just using RELOC_COUNT isn't good enough since that doesn't
4066 maintain a separate value for REL vs. RELA relocations. */
4067 if (info->relocateable)
4068 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4069 for (o = sub->sections; o != NULL; o = o->next)
4071 asection *output_section;
4073 if (! o->linker_mark)
4075 /* This section was omitted from the link. */
4076 continue;
4079 output_section = o->output_section;
4081 if (output_section != NULL
4082 && (o->flags & SEC_RELOC) != 0)
4084 struct bfd_elf_section_data *esdi
4085 = elf_section_data (o);
4086 struct bfd_elf_section_data *esdo
4087 = elf_section_data (output_section);
4088 unsigned int *rel_count;
4089 unsigned int *rel_count2;
4091 /* We must be careful to add the relocation froms the
4092 input section to the right output count. */
4093 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
4095 rel_count = &esdo->rel_count;
4096 rel_count2 = &esdo->rel_count2;
4098 else
4100 rel_count = &esdo->rel_count2;
4101 rel_count2 = &esdo->rel_count;
4104 *rel_count += (esdi->rel_hdr.sh_size
4105 / esdi->rel_hdr.sh_entsize);
4106 if (esdi->rel_hdr2)
4107 *rel_count2 += (esdi->rel_hdr2->sh_size
4108 / esdi->rel_hdr2->sh_entsize);
4112 /* That created the reloc sections. Set their sizes, and assign
4113 them file positions, and allocate some buffers. */
4114 for (o = abfd->sections; o != NULL; o = o->next)
4116 if ((o->flags & SEC_RELOC) != 0)
4118 if (!elf_link_size_reloc_section (abfd,
4119 &elf_section_data (o)->rel_hdr,
4121 goto error_return;
4123 if (elf_section_data (o)->rel_hdr2
4124 && !elf_link_size_reloc_section (abfd,
4125 elf_section_data (o)->rel_hdr2,
4127 goto error_return;
4130 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4131 to count upwards while actually outputting the relocations. */
4132 elf_section_data (o)->rel_count = 0;
4133 elf_section_data (o)->rel_count2 = 0;
4136 _bfd_elf_assign_file_positions_for_relocs (abfd);
4138 /* We have now assigned file positions for all the sections except
4139 .symtab and .strtab. We start the .symtab section at the current
4140 file position, and write directly to it. We build the .strtab
4141 section in memory. */
4142 bfd_get_symcount (abfd) = 0;
4143 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4144 /* sh_name is set in prep_headers. */
4145 symtab_hdr->sh_type = SHT_SYMTAB;
4146 symtab_hdr->sh_flags = 0;
4147 symtab_hdr->sh_addr = 0;
4148 symtab_hdr->sh_size = 0;
4149 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
4150 /* sh_link is set in assign_section_numbers. */
4151 /* sh_info is set below. */
4152 /* sh_offset is set just below. */
4153 symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */
4155 off = elf_tdata (abfd)->next_file_pos;
4156 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
4158 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4159 incorrect. We do not yet know the size of the .symtab section.
4160 We correct next_file_pos below, after we do know the size. */
4162 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4163 continuously seeking to the right position in the file. */
4164 if (! info->keep_memory || max_sym_count < 20)
4165 finfo.symbuf_size = 20;
4166 else
4167 finfo.symbuf_size = max_sym_count;
4168 finfo.symbuf = ((Elf_External_Sym *)
4169 bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
4170 if (finfo.symbuf == NULL)
4171 goto error_return;
4173 /* Start writing out the symbol table. The first symbol is always a
4174 dummy symbol. */
4175 if (info->strip != strip_all || info->relocateable)
4177 elfsym.st_value = 0;
4178 elfsym.st_size = 0;
4179 elfsym.st_info = 0;
4180 elfsym.st_other = 0;
4181 elfsym.st_shndx = SHN_UNDEF;
4182 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4183 &elfsym, bfd_und_section_ptr))
4184 goto error_return;
4187 #if 0
4188 /* Some standard ELF linkers do this, but we don't because it causes
4189 bootstrap comparison failures. */
4190 /* Output a file symbol for the output file as the second symbol.
4191 We output this even if we are discarding local symbols, although
4192 I'm not sure if this is correct. */
4193 elfsym.st_value = 0;
4194 elfsym.st_size = 0;
4195 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4196 elfsym.st_other = 0;
4197 elfsym.st_shndx = SHN_ABS;
4198 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
4199 &elfsym, bfd_abs_section_ptr))
4200 goto error_return;
4201 #endif
4203 /* Output a symbol for each section. We output these even if we are
4204 discarding local symbols, since they are used for relocs. These
4205 symbols have no names. We store the index of each one in the
4206 index field of the section, so that we can find it again when
4207 outputting relocs. */
4208 if (info->strip != strip_all || info->relocateable)
4210 elfsym.st_size = 0;
4211 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4212 elfsym.st_other = 0;
4213 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
4215 o = section_from_elf_index (abfd, i);
4216 if (o != NULL)
4217 o->target_index = bfd_get_symcount (abfd);
4218 elfsym.st_shndx = i;
4219 if (info->relocateable || o == NULL)
4220 elfsym.st_value = 0;
4221 else
4222 elfsym.st_value = o->vma;
4223 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4224 &elfsym, o))
4225 goto error_return;
4229 /* Allocate some memory to hold information read in from the input
4230 files. */
4231 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4232 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
4233 finfo.internal_relocs = ((Elf_Internal_Rela *)
4234 bfd_malloc (max_internal_reloc_count
4235 * sizeof (Elf_Internal_Rela)
4236 * bed->s->int_rels_per_ext_rel));
4237 finfo.external_syms = ((Elf_External_Sym *)
4238 bfd_malloc (max_sym_count
4239 * sizeof (Elf_External_Sym)));
4240 finfo.internal_syms = ((Elf_Internal_Sym *)
4241 bfd_malloc (max_sym_count
4242 * sizeof (Elf_Internal_Sym)));
4243 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4244 finfo.sections = ((asection **)
4245 bfd_malloc (max_sym_count * sizeof (asection *)));
4246 if ((finfo.contents == NULL && max_contents_size != 0)
4247 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
4248 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
4249 || (finfo.external_syms == NULL && max_sym_count != 0)
4250 || (finfo.internal_syms == NULL && max_sym_count != 0)
4251 || (finfo.indices == NULL && max_sym_count != 0)
4252 || (finfo.sections == NULL && max_sym_count != 0))
4253 goto error_return;
4255 /* Since ELF permits relocations to be against local symbols, we
4256 must have the local symbols available when we do the relocations.
4257 Since we would rather only read the local symbols once, and we
4258 would rather not keep them in memory, we handle all the
4259 relocations for a single input file at the same time.
4261 Unfortunately, there is no way to know the total number of local
4262 symbols until we have seen all of them, and the local symbol
4263 indices precede the global symbol indices. This means that when
4264 we are generating relocateable output, and we see a reloc against
4265 a global symbol, we can not know the symbol index until we have
4266 finished examining all the local symbols to see which ones we are
4267 going to output. To deal with this, we keep the relocations in
4268 memory, and don't output them until the end of the link. This is
4269 an unfortunate waste of memory, but I don't see a good way around
4270 it. Fortunately, it only happens when performing a relocateable
4271 link, which is not the common case. FIXME: If keep_memory is set
4272 we could write the relocs out and then read them again; I don't
4273 know how bad the memory loss will be. */
4275 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4276 sub->output_has_begun = false;
4277 for (o = abfd->sections; o != NULL; o = o->next)
4279 for (p = o->link_order_head; p != NULL; p = p->next)
4281 if (p->type == bfd_indirect_link_order
4282 && (bfd_get_flavour (p->u.indirect.section->owner)
4283 == bfd_target_elf_flavour))
4285 sub = p->u.indirect.section->owner;
4286 if (! sub->output_has_begun)
4288 if (! elf_link_input_bfd (&finfo, sub))
4289 goto error_return;
4290 sub->output_has_begun = true;
4293 else if (p->type == bfd_section_reloc_link_order
4294 || p->type == bfd_symbol_reloc_link_order)
4296 if (! elf_reloc_link_order (abfd, info, o, p))
4297 goto error_return;
4299 else
4301 if (! _bfd_default_link_order (abfd, info, o, p))
4302 goto error_return;
4307 /* That wrote out all the local symbols. Finish up the symbol table
4308 with the global symbols. */
4310 if (info->strip != strip_all && info->shared)
4312 /* Output any global symbols that got converted to local in a
4313 version script. We do this in a separate step since ELF
4314 requires all local symbols to appear prior to any global
4315 symbols. FIXME: We should only do this if some global
4316 symbols were, in fact, converted to become local. FIXME:
4317 Will this work correctly with the Irix 5 linker? */
4318 eoinfo.failed = false;
4319 eoinfo.finfo = &finfo;
4320 eoinfo.localsyms = true;
4321 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
4322 (PTR) &eoinfo);
4323 if (eoinfo.failed)
4324 return false;
4327 /* The sh_info field records the index of the first non local symbol. */
4328 symtab_hdr->sh_info = bfd_get_symcount (abfd);
4330 if (dynamic)
4332 Elf_Internal_Sym sym;
4333 Elf_External_Sym *dynsym =
4334 (Elf_External_Sym *)finfo.dynsym_sec->contents;
4335 long last_local = 0;
4337 /* Write out the section symbols for the output sections. */
4338 if (info->shared)
4340 asection *s;
4342 sym.st_size = 0;
4343 sym.st_name = 0;
4344 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4345 sym.st_other = 0;
4347 for (s = abfd->sections; s != NULL; s = s->next)
4349 int indx;
4350 indx = elf_section_data (s)->this_idx;
4351 BFD_ASSERT (indx > 0);
4352 sym.st_shndx = indx;
4353 sym.st_value = s->vma;
4355 elf_swap_symbol_out (abfd, &sym,
4356 dynsym + elf_section_data (s)->dynindx);
4359 last_local = bfd_count_sections (abfd);
4362 /* Write out the local dynsyms. */
4363 if (elf_hash_table (info)->dynlocal)
4365 struct elf_link_local_dynamic_entry *e;
4366 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
4368 asection *s;
4370 sym.st_size = e->isym.st_size;
4371 sym.st_other = e->isym.st_other;
4373 /* Copy the internal symbol as is.
4374 Note that we saved a word of storage and overwrote
4375 the original st_name with the dynstr_index. */
4376 sym = e->isym;
4378 if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE)
4380 s = bfd_section_from_elf_index (e->input_bfd,
4381 e->isym.st_shndx);
4383 sym.st_shndx =
4384 elf_section_data (s->output_section)->this_idx;
4385 sym.st_value = (s->output_section->vma
4386 + s->output_offset
4387 + e->isym.st_value);
4390 if (last_local < e->dynindx)
4391 last_local = e->dynindx;
4393 elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx);
4397 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
4398 last_local + 1;
4401 /* We get the global symbols from the hash table. */
4402 eoinfo.failed = false;
4403 eoinfo.localsyms = false;
4404 eoinfo.finfo = &finfo;
4405 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
4406 (PTR) &eoinfo);
4407 if (eoinfo.failed)
4408 return false;
4410 /* If backend needs to output some symbols not present in the hash
4411 table, do it now. */
4412 if (bed->elf_backend_output_arch_syms)
4414 if (! (*bed->elf_backend_output_arch_syms)
4415 (abfd, info, (PTR) &finfo,
4416 (boolean (*) PARAMS ((PTR, const char *,
4417 Elf_Internal_Sym *, asection *)))
4418 elf_link_output_sym))
4419 return false;
4422 /* Flush all symbols to the file. */
4423 if (! elf_link_flush_output_syms (&finfo))
4424 return false;
4426 /* Now we know the size of the symtab section. */
4427 off += symtab_hdr->sh_size;
4429 /* Finish up and write out the symbol string table (.strtab)
4430 section. */
4431 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4432 /* sh_name was set in prep_headers. */
4433 symstrtab_hdr->sh_type = SHT_STRTAB;
4434 symstrtab_hdr->sh_flags = 0;
4435 symstrtab_hdr->sh_addr = 0;
4436 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
4437 symstrtab_hdr->sh_entsize = 0;
4438 symstrtab_hdr->sh_link = 0;
4439 symstrtab_hdr->sh_info = 0;
4440 /* sh_offset is set just below. */
4441 symstrtab_hdr->sh_addralign = 1;
4443 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
4444 elf_tdata (abfd)->next_file_pos = off;
4446 if (bfd_get_symcount (abfd) > 0)
4448 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
4449 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
4450 return false;
4453 /* Adjust the relocs to have the correct symbol indices. */
4454 for (o = abfd->sections; o != NULL; o = o->next)
4456 if ((o->flags & SEC_RELOC) == 0)
4457 continue;
4459 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
4460 elf_section_data (o)->rel_count,
4461 elf_section_data (o)->rel_hashes);
4462 if (elf_section_data (o)->rel_hdr2 != NULL)
4463 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
4464 elf_section_data (o)->rel_count2,
4465 (elf_section_data (o)->rel_hashes
4466 + elf_section_data (o)->rel_count));
4468 /* Set the reloc_count field to 0 to prevent write_relocs from
4469 trying to swap the relocs out itself. */
4470 o->reloc_count = 0;
4473 /* If we are linking against a dynamic object, or generating a
4474 shared library, finish up the dynamic linking information. */
4475 if (dynamic)
4477 Elf_External_Dyn *dyncon, *dynconend;
4479 /* Fix up .dynamic entries. */
4480 o = bfd_get_section_by_name (dynobj, ".dynamic");
4481 BFD_ASSERT (o != NULL);
4483 dyncon = (Elf_External_Dyn *) o->contents;
4484 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
4485 for (; dyncon < dynconend; dyncon++)
4487 Elf_Internal_Dyn dyn;
4488 const char *name;
4489 unsigned int type;
4491 elf_swap_dyn_in (dynobj, dyncon, &dyn);
4493 switch (dyn.d_tag)
4495 default:
4496 break;
4497 case DT_INIT:
4498 name = info->init_function;
4499 goto get_sym;
4500 case DT_FINI:
4501 name = info->fini_function;
4502 get_sym:
4504 struct elf_link_hash_entry *h;
4506 h = elf_link_hash_lookup (elf_hash_table (info), name,
4507 false, false, true);
4508 if (h != NULL
4509 && (h->root.type == bfd_link_hash_defined
4510 || h->root.type == bfd_link_hash_defweak))
4512 dyn.d_un.d_val = h->root.u.def.value;
4513 o = h->root.u.def.section;
4514 if (o->output_section != NULL)
4515 dyn.d_un.d_val += (o->output_section->vma
4516 + o->output_offset);
4517 else
4519 /* The symbol is imported from another shared
4520 library and does not apply to this one. */
4521 dyn.d_un.d_val = 0;
4524 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4527 break;
4529 case DT_HASH:
4530 name = ".hash";
4531 goto get_vma;
4532 case DT_STRTAB:
4533 name = ".dynstr";
4534 goto get_vma;
4535 case DT_SYMTAB:
4536 name = ".dynsym";
4537 goto get_vma;
4538 case DT_VERDEF:
4539 name = ".gnu.version_d";
4540 goto get_vma;
4541 case DT_VERNEED:
4542 name = ".gnu.version_r";
4543 goto get_vma;
4544 case DT_VERSYM:
4545 name = ".gnu.version";
4546 get_vma:
4547 o = bfd_get_section_by_name (abfd, name);
4548 BFD_ASSERT (o != NULL);
4549 dyn.d_un.d_ptr = o->vma;
4550 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4551 break;
4553 case DT_REL:
4554 case DT_RELA:
4555 case DT_RELSZ:
4556 case DT_RELASZ:
4557 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
4558 type = SHT_REL;
4559 else
4560 type = SHT_RELA;
4561 dyn.d_un.d_val = 0;
4562 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
4564 Elf_Internal_Shdr *hdr;
4566 hdr = elf_elfsections (abfd)[i];
4567 if (hdr->sh_type == type
4568 && (hdr->sh_flags & SHF_ALLOC) != 0)
4570 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
4571 dyn.d_un.d_val += hdr->sh_size;
4572 else
4574 if (dyn.d_un.d_val == 0
4575 || hdr->sh_addr < dyn.d_un.d_val)
4576 dyn.d_un.d_val = hdr->sh_addr;
4580 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4581 break;
4586 /* If we have created any dynamic sections, then output them. */
4587 if (dynobj != NULL)
4589 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
4590 goto error_return;
4592 for (o = dynobj->sections; o != NULL; o = o->next)
4594 if ((o->flags & SEC_HAS_CONTENTS) == 0
4595 || o->_raw_size == 0)
4596 continue;
4597 if ((o->flags & SEC_LINKER_CREATED) == 0)
4599 /* At this point, we are only interested in sections
4600 created by elf_link_create_dynamic_sections. */
4601 continue;
4603 if ((elf_section_data (o->output_section)->this_hdr.sh_type
4604 != SHT_STRTAB)
4605 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
4607 if (! bfd_set_section_contents (abfd, o->output_section,
4608 o->contents, o->output_offset,
4609 o->_raw_size))
4610 goto error_return;
4612 else
4614 file_ptr off;
4616 /* The contents of the .dynstr section are actually in a
4617 stringtab. */
4618 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
4619 if (bfd_seek (abfd, off, SEEK_SET) != 0
4620 || ! _bfd_stringtab_emit (abfd,
4621 elf_hash_table (info)->dynstr))
4622 goto error_return;
4627 /* If we have optimized stabs strings, output them. */
4628 if (elf_hash_table (info)->stab_info != NULL)
4630 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4631 goto error_return;
4634 if (finfo.symstrtab != NULL)
4635 _bfd_stringtab_free (finfo.symstrtab);
4636 if (finfo.contents != NULL)
4637 free (finfo.contents);
4638 if (finfo.external_relocs != NULL)
4639 free (finfo.external_relocs);
4640 if (finfo.internal_relocs != NULL)
4641 free (finfo.internal_relocs);
4642 if (finfo.external_syms != NULL)
4643 free (finfo.external_syms);
4644 if (finfo.internal_syms != NULL)
4645 free (finfo.internal_syms);
4646 if (finfo.indices != NULL)
4647 free (finfo.indices);
4648 if (finfo.sections != NULL)
4649 free (finfo.sections);
4650 if (finfo.symbuf != NULL)
4651 free (finfo.symbuf);
4652 for (o = abfd->sections; o != NULL; o = o->next)
4654 if ((o->flags & SEC_RELOC) != 0
4655 && elf_section_data (o)->rel_hashes != NULL)
4656 free (elf_section_data (o)->rel_hashes);
4659 elf_tdata (abfd)->linker = true;
4661 return true;
4663 error_return:
4664 if (finfo.symstrtab != NULL)
4665 _bfd_stringtab_free (finfo.symstrtab);
4666 if (finfo.contents != NULL)
4667 free (finfo.contents);
4668 if (finfo.external_relocs != NULL)
4669 free (finfo.external_relocs);
4670 if (finfo.internal_relocs != NULL)
4671 free (finfo.internal_relocs);
4672 if (finfo.external_syms != NULL)
4673 free (finfo.external_syms);
4674 if (finfo.internal_syms != NULL)
4675 free (finfo.internal_syms);
4676 if (finfo.indices != NULL)
4677 free (finfo.indices);
4678 if (finfo.sections != NULL)
4679 free (finfo.sections);
4680 if (finfo.symbuf != NULL)
4681 free (finfo.symbuf);
4682 for (o = abfd->sections; o != NULL; o = o->next)
4684 if ((o->flags & SEC_RELOC) != 0
4685 && elf_section_data (o)->rel_hashes != NULL)
4686 free (elf_section_data (o)->rel_hashes);
4689 return false;
4692 /* Add a symbol to the output symbol table. */
4694 static boolean
4695 elf_link_output_sym (finfo, name, elfsym, input_sec)
4696 struct elf_final_link_info *finfo;
4697 const char *name;
4698 Elf_Internal_Sym *elfsym;
4699 asection *input_sec;
4701 boolean (*output_symbol_hook) PARAMS ((bfd *,
4702 struct bfd_link_info *info,
4703 const char *,
4704 Elf_Internal_Sym *,
4705 asection *));
4707 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4708 elf_backend_link_output_symbol_hook;
4709 if (output_symbol_hook != NULL)
4711 if (! ((*output_symbol_hook)
4712 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
4713 return false;
4716 if (name == (const char *) NULL || *name == '\0')
4717 elfsym->st_name = 0;
4718 else if (input_sec->flags & SEC_EXCLUDE)
4719 elfsym->st_name = 0;
4720 else
4722 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
4723 name, true,
4724 false);
4725 if (elfsym->st_name == (unsigned long) -1)
4726 return false;
4729 if (finfo->symbuf_count >= finfo->symbuf_size)
4731 if (! elf_link_flush_output_syms (finfo))
4732 return false;
4735 elf_swap_symbol_out (finfo->output_bfd, elfsym,
4736 (PTR) (finfo->symbuf + finfo->symbuf_count));
4737 ++finfo->symbuf_count;
4739 ++ bfd_get_symcount (finfo->output_bfd);
4741 return true;
4744 /* Flush the output symbols to the file. */
4746 static boolean
4747 elf_link_flush_output_syms (finfo)
4748 struct elf_final_link_info *finfo;
4750 if (finfo->symbuf_count > 0)
4752 Elf_Internal_Shdr *symtab;
4754 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4756 if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
4757 SEEK_SET) != 0
4758 || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
4759 sizeof (Elf_External_Sym), finfo->output_bfd)
4760 != finfo->symbuf_count * sizeof (Elf_External_Sym)))
4761 return false;
4763 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
4765 finfo->symbuf_count = 0;
4768 return true;
4771 /* Add an external symbol to the symbol table. This is called from
4772 the hash table traversal routine. When generating a shared object,
4773 we go through the symbol table twice. The first time we output
4774 anything that might have been forced to local scope in a version
4775 script. The second time we output the symbols that are still
4776 global symbols. */
4778 static boolean
4779 elf_link_output_extsym (h, data)
4780 struct elf_link_hash_entry *h;
4781 PTR data;
4783 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
4784 struct elf_final_link_info *finfo = eoinfo->finfo;
4785 boolean strip;
4786 Elf_Internal_Sym sym;
4787 asection *input_sec;
4789 /* Decide whether to output this symbol in this pass. */
4790 if (eoinfo->localsyms)
4792 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4793 return true;
4795 else
4797 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4798 return true;
4801 /* If we are not creating a shared library, and this symbol is
4802 referenced by a shared library but is not defined anywhere, then
4803 warn that it is undefined. If we do not do this, the runtime
4804 linker will complain that the symbol is undefined when the
4805 program is run. We don't have to worry about symbols that are
4806 referenced by regular files, because we will already have issued
4807 warnings for them. */
4808 if (! finfo->info->relocateable
4809 && ! (finfo->info->shared
4810 && !finfo->info->no_undefined)
4811 && h->root.type == bfd_link_hash_undefined
4812 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4813 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4815 if (! ((*finfo->info->callbacks->undefined_symbol)
4816 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4817 (asection *) NULL, 0)))
4819 eoinfo->failed = true;
4820 return false;
4824 /* We don't want to output symbols that have never been mentioned by
4825 a regular file, or that we have been told to strip. However, if
4826 h->indx is set to -2, the symbol is used by a reloc and we must
4827 output it. */
4828 if (h->indx == -2)
4829 strip = false;
4830 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4831 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4832 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4833 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4834 strip = true;
4835 else if (finfo->info->strip == strip_all
4836 || (finfo->info->strip == strip_some
4837 && bfd_hash_lookup (finfo->info->keep_hash,
4838 h->root.root.string,
4839 false, false) == NULL))
4840 strip = true;
4841 else
4842 strip = false;
4844 /* If we're stripping it, and it's not a dynamic symbol, there's
4845 nothing else to do. */
4846 if (strip && h->dynindx == -1)
4847 return true;
4849 sym.st_value = 0;
4850 sym.st_size = h->size;
4851 sym.st_other = h->other;
4852 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4853 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4854 else if (h->root.type == bfd_link_hash_undefweak
4855 || h->root.type == bfd_link_hash_defweak)
4856 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4857 else
4858 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4860 switch (h->root.type)
4862 default:
4863 case bfd_link_hash_new:
4864 abort ();
4865 return false;
4867 case bfd_link_hash_undefined:
4868 input_sec = bfd_und_section_ptr;
4869 sym.st_shndx = SHN_UNDEF;
4870 break;
4872 case bfd_link_hash_undefweak:
4873 input_sec = bfd_und_section_ptr;
4874 sym.st_shndx = SHN_UNDEF;
4875 break;
4877 case bfd_link_hash_defined:
4878 case bfd_link_hash_defweak:
4880 input_sec = h->root.u.def.section;
4881 if (input_sec->output_section != NULL)
4883 sym.st_shndx =
4884 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4885 input_sec->output_section);
4886 if (sym.st_shndx == (unsigned short) -1)
4888 (*_bfd_error_handler)
4889 (_("%s: could not find output section %s for input section %s"),
4890 bfd_get_filename (finfo->output_bfd),
4891 input_sec->output_section->name,
4892 input_sec->name);
4893 eoinfo->failed = true;
4894 return false;
4897 /* ELF symbols in relocateable files are section relative,
4898 but in nonrelocateable files they are virtual
4899 addresses. */
4900 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4901 if (! finfo->info->relocateable)
4902 sym.st_value += input_sec->output_section->vma;
4904 else
4906 BFD_ASSERT (input_sec->owner == NULL
4907 || (input_sec->owner->flags & DYNAMIC) != 0);
4908 sym.st_shndx = SHN_UNDEF;
4909 input_sec = bfd_und_section_ptr;
4912 break;
4914 case bfd_link_hash_common:
4915 input_sec = h->root.u.c.p->section;
4916 sym.st_shndx = SHN_COMMON;
4917 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4918 break;
4920 case bfd_link_hash_indirect:
4921 /* These symbols are created by symbol versioning. They point
4922 to the decorated version of the name. For example, if the
4923 symbol foo@@GNU_1.2 is the default, which should be used when
4924 foo is used with no version, then we add an indirect symbol
4925 foo which points to foo@@GNU_1.2. We ignore these symbols,
4926 since the indirected symbol is already in the hash table. If
4927 the indirect symbol is non-ELF, fall through and output it. */
4928 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) == 0)
4929 return true;
4931 /* Fall through. */
4932 case bfd_link_hash_warning:
4933 /* We can't represent these symbols in ELF, although a warning
4934 symbol may have come from a .gnu.warning.SYMBOL section. We
4935 just put the target symbol in the hash table. If the target
4936 symbol does not really exist, don't do anything. */
4937 if (h->root.u.i.link->type == bfd_link_hash_new)
4938 return true;
4939 return (elf_link_output_extsym
4940 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
4943 /* Give the processor backend a chance to tweak the symbol value,
4944 and also to finish up anything that needs to be done for this
4945 symbol. */
4946 if ((h->dynindx != -1
4947 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4948 && elf_hash_table (finfo->info)->dynamic_sections_created)
4950 struct elf_backend_data *bed;
4952 bed = get_elf_backend_data (finfo->output_bfd);
4953 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4954 (finfo->output_bfd, finfo->info, h, &sym)))
4956 eoinfo->failed = true;
4957 return false;
4961 /* If we are marking the symbol as undefined, and there are no
4962 non-weak references to this symbol from a regular object, then
4963 mark the symbol as weak undefined; if there are non-weak
4964 references, mark the symbol as strong. We can't do this earlier,
4965 because it might not be marked as undefined until the
4966 finish_dynamic_symbol routine gets through with it. */
4967 if (sym.st_shndx == SHN_UNDEF
4968 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4969 && (ELF_ST_BIND(sym.st_info) == STB_GLOBAL
4970 || ELF_ST_BIND(sym.st_info) == STB_WEAK))
4972 int bindtype;
4974 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4975 bindtype = STB_GLOBAL;
4976 else
4977 bindtype = STB_WEAK;
4978 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4981 /* If this symbol should be put in the .dynsym section, then put it
4982 there now. We have already know the symbol index. We also fill
4983 in the entry in the .hash section. */
4984 if (h->dynindx != -1
4985 && elf_hash_table (finfo->info)->dynamic_sections_created)
4987 size_t bucketcount;
4988 size_t bucket;
4989 size_t hash_entry_size;
4990 bfd_byte *bucketpos;
4991 bfd_vma chain;
4993 sym.st_name = h->dynstr_index;
4995 elf_swap_symbol_out (finfo->output_bfd, &sym,
4996 (PTR) (((Elf_External_Sym *)
4997 finfo->dynsym_sec->contents)
4998 + h->dynindx));
5000 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5001 bucket = h->elf_hash_value % bucketcount;
5002 hash_entry_size
5003 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
5004 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5005 + (bucket + 2) * hash_entry_size);
5006 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
5007 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
5008 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
5009 ((bfd_byte *) finfo->hash_sec->contents
5010 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
5012 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
5014 Elf_Internal_Versym iversym;
5016 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5018 if (h->verinfo.verdef == NULL)
5019 iversym.vs_vers = 0;
5020 else
5021 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
5023 else
5025 if (h->verinfo.vertree == NULL)
5026 iversym.vs_vers = 1;
5027 else
5028 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
5031 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
5032 iversym.vs_vers |= VERSYM_HIDDEN;
5034 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym,
5035 (((Elf_External_Versym *)
5036 finfo->symver_sec->contents)
5037 + h->dynindx));
5041 /* If we're stripping it, then it was just a dynamic symbol, and
5042 there's nothing else to do. */
5043 if (strip)
5044 return true;
5046 h->indx = bfd_get_symcount (finfo->output_bfd);
5048 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
5050 eoinfo->failed = true;
5051 return false;
5054 return true;
5057 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5058 originated from the section given by INPUT_REL_HDR) to the
5059 OUTPUT_BFD. */
5061 static void
5062 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
5063 internal_relocs)
5064 bfd *output_bfd;
5065 asection *input_section;
5066 Elf_Internal_Shdr *input_rel_hdr;
5067 Elf_Internal_Rela *internal_relocs;
5069 Elf_Internal_Rela *irela;
5070 Elf_Internal_Rela *irelaend;
5071 Elf_Internal_Shdr *output_rel_hdr;
5072 asection *output_section;
5073 unsigned int *rel_countp = NULL;
5075 output_section = input_section->output_section;
5076 output_rel_hdr = NULL;
5078 if (elf_section_data (output_section)->rel_hdr.sh_entsize
5079 == input_rel_hdr->sh_entsize)
5081 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
5082 rel_countp = &elf_section_data (output_section)->rel_count;
5084 else if (elf_section_data (output_section)->rel_hdr2
5085 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
5086 == input_rel_hdr->sh_entsize))
5088 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
5089 rel_countp = &elf_section_data (output_section)->rel_count2;
5092 BFD_ASSERT (output_rel_hdr != NULL);
5094 irela = internal_relocs;
5095 irelaend = irela + input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
5096 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5098 Elf_External_Rel *erel;
5100 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
5101 for (; irela < irelaend; irela++, erel++)
5103 Elf_Internal_Rel irel;
5105 irel.r_offset = irela->r_offset;
5106 irel.r_info = irela->r_info;
5107 BFD_ASSERT (irela->r_addend == 0);
5108 elf_swap_reloc_out (output_bfd, &irel, erel);
5111 else
5113 Elf_External_Rela *erela;
5115 BFD_ASSERT (input_rel_hdr->sh_entsize
5116 == sizeof (Elf_External_Rela));
5117 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
5118 for (; irela < irelaend; irela++, erela++)
5119 elf_swap_reloca_out (output_bfd, irela, erela);
5122 /* Bump the counter, so that we know where to add the next set of
5123 relocations. */
5124 *rel_countp += input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
5127 /* Link an input file into the linker output file. This function
5128 handles all the sections and relocations of the input file at once.
5129 This is so that we only have to read the local symbols once, and
5130 don't have to keep them in memory. */
5132 static boolean
5133 elf_link_input_bfd (finfo, input_bfd)
5134 struct elf_final_link_info *finfo;
5135 bfd *input_bfd;
5137 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
5138 bfd *, asection *, bfd_byte *,
5139 Elf_Internal_Rela *,
5140 Elf_Internal_Sym *, asection **));
5141 bfd *output_bfd;
5142 Elf_Internal_Shdr *symtab_hdr;
5143 size_t locsymcount;
5144 size_t extsymoff;
5145 Elf_External_Sym *external_syms;
5146 Elf_External_Sym *esym;
5147 Elf_External_Sym *esymend;
5148 Elf_Internal_Sym *isym;
5149 long *pindex;
5150 asection **ppsection;
5151 asection *o;
5152 struct elf_backend_data *bed;
5154 output_bfd = finfo->output_bfd;
5155 bed = get_elf_backend_data (output_bfd);
5156 relocate_section = bed->elf_backend_relocate_section;
5158 /* If this is a dynamic object, we don't want to do anything here:
5159 we don't want the local symbols, and we don't want the section
5160 contents. */
5161 if ((input_bfd->flags & DYNAMIC) != 0)
5162 return true;
5164 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5165 if (elf_bad_symtab (input_bfd))
5167 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5168 extsymoff = 0;
5170 else
5172 locsymcount = symtab_hdr->sh_info;
5173 extsymoff = symtab_hdr->sh_info;
5176 /* Read the local symbols. */
5177 if (symtab_hdr->contents != NULL)
5178 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
5179 else if (locsymcount == 0)
5180 external_syms = NULL;
5181 else
5183 external_syms = finfo->external_syms;
5184 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
5185 || (bfd_read (external_syms, sizeof (Elf_External_Sym),
5186 locsymcount, input_bfd)
5187 != locsymcount * sizeof (Elf_External_Sym)))
5188 return false;
5191 /* Swap in the local symbols and write out the ones which we know
5192 are going into the output file. */
5193 esym = external_syms;
5194 esymend = esym + locsymcount;
5195 isym = finfo->internal_syms;
5196 pindex = finfo->indices;
5197 ppsection = finfo->sections;
5198 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
5200 asection *isec;
5201 const char *name;
5202 Elf_Internal_Sym osym;
5204 elf_swap_symbol_in (input_bfd, esym, isym);
5205 *pindex = -1;
5207 if (elf_bad_symtab (input_bfd))
5209 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
5211 *ppsection = NULL;
5212 continue;
5216 if (isym->st_shndx == SHN_UNDEF)
5217 isec = bfd_und_section_ptr;
5218 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
5219 isec = section_from_elf_index (input_bfd, isym->st_shndx);
5220 else if (isym->st_shndx == SHN_ABS)
5221 isec = bfd_abs_section_ptr;
5222 else if (isym->st_shndx == SHN_COMMON)
5223 isec = bfd_com_section_ptr;
5224 else
5226 /* Who knows? */
5227 isec = NULL;
5230 *ppsection = isec;
5232 /* Don't output the first, undefined, symbol. */
5233 if (esym == external_syms)
5234 continue;
5236 /* If we are stripping all symbols, we don't want to output this
5237 one. */
5238 if (finfo->info->strip == strip_all)
5239 continue;
5241 /* We never output section symbols. Instead, we use the section
5242 symbol of the corresponding section in the output file. */
5243 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5244 continue;
5246 /* If we are discarding all local symbols, we don't want to
5247 output this one. If we are generating a relocateable output
5248 file, then some of the local symbols may be required by
5249 relocs; we output them below as we discover that they are
5250 needed. */
5251 if (finfo->info->discard == discard_all)
5252 continue;
5254 /* If this symbol is defined in a section which we are
5255 discarding, we don't need to keep it, but note that
5256 linker_mark is only reliable for sections that have contents.
5257 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
5258 as well as linker_mark. */
5259 if (isym->st_shndx > 0
5260 && isym->st_shndx < SHN_LORESERVE
5261 && isec != NULL
5262 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
5263 || (! finfo->info->relocateable
5264 && (isec->flags & SEC_EXCLUDE) != 0)))
5265 continue;
5267 /* Get the name of the symbol. */
5268 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
5269 isym->st_name);
5270 if (name == NULL)
5271 return false;
5273 /* See if we are discarding symbols with this name. */
5274 if ((finfo->info->strip == strip_some
5275 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
5276 == NULL))
5277 || (finfo->info->discard == discard_l
5278 && bfd_is_local_label_name (input_bfd, name)))
5279 continue;
5281 /* If we get here, we are going to output this symbol. */
5283 osym = *isym;
5285 /* Adjust the section index for the output file. */
5286 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
5287 isec->output_section);
5288 if (osym.st_shndx == (unsigned short) -1)
5289 return false;
5291 *pindex = bfd_get_symcount (output_bfd);
5293 /* ELF symbols in relocateable files are section relative, but
5294 in executable files they are virtual addresses. Note that
5295 this code assumes that all ELF sections have an associated
5296 BFD section with a reasonable value for output_offset; below
5297 we assume that they also have a reasonable value for
5298 output_section. Any special sections must be set up to meet
5299 these requirements. */
5300 osym.st_value += isec->output_offset;
5301 if (! finfo->info->relocateable)
5302 osym.st_value += isec->output_section->vma;
5304 if (! elf_link_output_sym (finfo, name, &osym, isec))
5305 return false;
5308 /* Relocate the contents of each section. */
5309 for (o = input_bfd->sections; o != NULL; o = o->next)
5311 bfd_byte *contents;
5313 if (! o->linker_mark)
5315 /* This section was omitted from the link. */
5316 continue;
5319 if ((o->flags & SEC_HAS_CONTENTS) == 0
5320 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
5321 continue;
5323 if ((o->flags & SEC_LINKER_CREATED) != 0)
5325 /* Section was created by elf_link_create_dynamic_sections
5326 or somesuch. */
5327 continue;
5330 /* Get the contents of the section. They have been cached by a
5331 relaxation routine. Note that o is a section in an input
5332 file, so the contents field will not have been set by any of
5333 the routines which work on output files. */
5334 if (elf_section_data (o)->this_hdr.contents != NULL)
5335 contents = elf_section_data (o)->this_hdr.contents;
5336 else
5338 contents = finfo->contents;
5339 if (! bfd_get_section_contents (input_bfd, o, contents,
5340 (file_ptr) 0, o->_raw_size))
5341 return false;
5344 if ((o->flags & SEC_RELOC) != 0)
5346 Elf_Internal_Rela *internal_relocs;
5348 /* Get the swapped relocs. */
5349 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
5350 (input_bfd, o, finfo->external_relocs,
5351 finfo->internal_relocs, false));
5352 if (internal_relocs == NULL
5353 && o->reloc_count > 0)
5354 return false;
5356 /* Relocate the section by invoking a back end routine.
5358 The back end routine is responsible for adjusting the
5359 section contents as necessary, and (if using Rela relocs
5360 and generating a relocateable output file) adjusting the
5361 reloc addend as necessary.
5363 The back end routine does not have to worry about setting
5364 the reloc address or the reloc symbol index.
5366 The back end routine is given a pointer to the swapped in
5367 internal symbols, and can access the hash table entries
5368 for the external symbols via elf_sym_hashes (input_bfd).
5370 When generating relocateable output, the back end routine
5371 must handle STB_LOCAL/STT_SECTION symbols specially. The
5372 output symbol is going to be a section symbol
5373 corresponding to the output section, which will require
5374 the addend to be adjusted. */
5376 if (! (*relocate_section) (output_bfd, finfo->info,
5377 input_bfd, o, contents,
5378 internal_relocs,
5379 finfo->internal_syms,
5380 finfo->sections))
5381 return false;
5383 if (finfo->info->relocateable)
5385 Elf_Internal_Rela *irela;
5386 Elf_Internal_Rela *irelaend;
5387 struct elf_link_hash_entry **rel_hash;
5388 Elf_Internal_Shdr *input_rel_hdr;
5390 /* Adjust the reloc addresses and symbol indices. */
5392 irela = internal_relocs;
5393 irelaend =
5394 irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
5395 rel_hash = (elf_section_data (o->output_section)->rel_hashes
5396 + elf_section_data (o->output_section)->rel_count
5397 + elf_section_data (o->output_section)->rel_count2);
5398 for (; irela < irelaend; irela++, rel_hash++)
5400 unsigned long r_symndx;
5401 Elf_Internal_Sym *isym;
5402 asection *sec;
5404 irela->r_offset += o->output_offset;
5406 r_symndx = ELF_R_SYM (irela->r_info);
5408 if (r_symndx == 0)
5409 continue;
5411 if (r_symndx >= locsymcount
5412 || (elf_bad_symtab (input_bfd)
5413 && finfo->sections[r_symndx] == NULL))
5415 struct elf_link_hash_entry *rh;
5416 long indx;
5418 /* This is a reloc against a global symbol. We
5419 have not yet output all the local symbols, so
5420 we do not know the symbol index of any global
5421 symbol. We set the rel_hash entry for this
5422 reloc to point to the global hash table entry
5423 for this symbol. The symbol index is then
5424 set at the end of elf_bfd_final_link. */
5425 indx = r_symndx - extsymoff;
5426 rh = elf_sym_hashes (input_bfd)[indx];
5427 while (rh->root.type == bfd_link_hash_indirect
5428 || rh->root.type == bfd_link_hash_warning)
5429 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5431 /* Setting the index to -2 tells
5432 elf_link_output_extsym that this symbol is
5433 used by a reloc. */
5434 BFD_ASSERT (rh->indx < 0);
5435 rh->indx = -2;
5437 *rel_hash = rh;
5439 continue;
5442 /* This is a reloc against a local symbol. */
5444 *rel_hash = NULL;
5445 isym = finfo->internal_syms + r_symndx;
5446 sec = finfo->sections[r_symndx];
5447 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5449 /* I suppose the backend ought to fill in the
5450 section of any STT_SECTION symbol against a
5451 processor specific section. If we have
5452 discarded a section, the output_section will
5453 be the absolute section. */
5454 if (sec != NULL
5455 && (bfd_is_abs_section (sec)
5456 || (sec->output_section != NULL
5457 && bfd_is_abs_section (sec->output_section))))
5458 r_symndx = 0;
5459 else if (sec == NULL || sec->owner == NULL)
5461 bfd_set_error (bfd_error_bad_value);
5462 return false;
5464 else
5466 r_symndx = sec->output_section->target_index;
5467 BFD_ASSERT (r_symndx != 0);
5470 else
5472 if (finfo->indices[r_symndx] == -1)
5474 unsigned long link;
5475 const char *name;
5476 asection *osec;
5478 if (finfo->info->strip == strip_all)
5480 /* You can't do ld -r -s. */
5481 bfd_set_error (bfd_error_invalid_operation);
5482 return false;
5485 /* This symbol was skipped earlier, but
5486 since it is needed by a reloc, we
5487 must output it now. */
5488 link = symtab_hdr->sh_link;
5489 name = bfd_elf_string_from_elf_section (input_bfd,
5490 link,
5491 isym->st_name);
5492 if (name == NULL)
5493 return false;
5495 osec = sec->output_section;
5496 isym->st_shndx =
5497 _bfd_elf_section_from_bfd_section (output_bfd,
5498 osec);
5499 if (isym->st_shndx == (unsigned short) -1)
5500 return false;
5502 isym->st_value += sec->output_offset;
5503 if (! finfo->info->relocateable)
5504 isym->st_value += osec->vma;
5506 finfo->indices[r_symndx] = bfd_get_symcount (output_bfd);
5508 if (! elf_link_output_sym (finfo, name, isym, sec))
5509 return false;
5512 r_symndx = finfo->indices[r_symndx];
5515 irela->r_info = ELF_R_INFO (r_symndx,
5516 ELF_R_TYPE (irela->r_info));
5519 /* Swap out the relocs. */
5520 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5521 elf_link_output_relocs (output_bfd, o,
5522 input_rel_hdr,
5523 internal_relocs);
5524 internal_relocs
5525 += input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
5526 input_rel_hdr = elf_section_data (o)->rel_hdr2;
5527 if (input_rel_hdr)
5528 elf_link_output_relocs (output_bfd, o,
5529 input_rel_hdr,
5530 internal_relocs);
5534 /* Write out the modified section contents. */
5535 if (elf_section_data (o)->stab_info == NULL)
5537 if (! (o->flags & SEC_EXCLUDE) &&
5538 ! bfd_set_section_contents (output_bfd, o->output_section,
5539 contents, o->output_offset,
5540 (o->_cooked_size != 0
5541 ? o->_cooked_size
5542 : o->_raw_size)))
5543 return false;
5545 else
5547 if (! (_bfd_write_section_stabs
5548 (output_bfd, &elf_hash_table (finfo->info)->stab_info,
5549 o, &elf_section_data (o)->stab_info, contents)))
5550 return false;
5554 return true;
5557 /* Generate a reloc when linking an ELF file. This is a reloc
5558 requested by the linker, and does come from any input file. This
5559 is used to build constructor and destructor tables when linking
5560 with -Ur. */
5562 static boolean
5563 elf_reloc_link_order (output_bfd, info, output_section, link_order)
5564 bfd *output_bfd;
5565 struct bfd_link_info *info;
5566 asection *output_section;
5567 struct bfd_link_order *link_order;
5569 reloc_howto_type *howto;
5570 long indx;
5571 bfd_vma offset;
5572 bfd_vma addend;
5573 struct elf_link_hash_entry **rel_hash_ptr;
5574 Elf_Internal_Shdr *rel_hdr;
5576 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5577 if (howto == NULL)
5579 bfd_set_error (bfd_error_bad_value);
5580 return false;
5583 addend = link_order->u.reloc.p->addend;
5585 /* Figure out the symbol index. */
5586 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5587 + elf_section_data (output_section)->rel_count
5588 + elf_section_data (output_section)->rel_count2);
5589 if (link_order->type == bfd_section_reloc_link_order)
5591 indx = link_order->u.reloc.p->u.section->target_index;
5592 BFD_ASSERT (indx != 0);
5593 *rel_hash_ptr = NULL;
5595 else
5597 struct elf_link_hash_entry *h;
5599 /* Treat a reloc against a defined symbol as though it were
5600 actually against the section. */
5601 h = ((struct elf_link_hash_entry *)
5602 bfd_wrapped_link_hash_lookup (output_bfd, info,
5603 link_order->u.reloc.p->u.name,
5604 false, false, true));
5605 if (h != NULL
5606 && (h->root.type == bfd_link_hash_defined
5607 || h->root.type == bfd_link_hash_defweak))
5609 asection *section;
5611 section = h->root.u.def.section;
5612 indx = section->output_section->target_index;
5613 *rel_hash_ptr = NULL;
5614 /* It seems that we ought to add the symbol value to the
5615 addend here, but in practice it has already been added
5616 because it was passed to constructor_callback. */
5617 addend += section->output_section->vma + section->output_offset;
5619 else if (h != NULL)
5621 /* Setting the index to -2 tells elf_link_output_extsym that
5622 this symbol is used by a reloc. */
5623 h->indx = -2;
5624 *rel_hash_ptr = h;
5625 indx = 0;
5627 else
5629 if (! ((*info->callbacks->unattached_reloc)
5630 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5631 (asection *) NULL, (bfd_vma) 0)))
5632 return false;
5633 indx = 0;
5637 /* If this is an inplace reloc, we must write the addend into the
5638 object file. */
5639 if (howto->partial_inplace && addend != 0)
5641 bfd_size_type size;
5642 bfd_reloc_status_type rstat;
5643 bfd_byte *buf;
5644 boolean ok;
5646 size = bfd_get_reloc_size (howto);
5647 buf = (bfd_byte *) bfd_zmalloc (size);
5648 if (buf == (bfd_byte *) NULL)
5649 return false;
5650 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5651 switch (rstat)
5653 case bfd_reloc_ok:
5654 break;
5655 default:
5656 case bfd_reloc_outofrange:
5657 abort ();
5658 case bfd_reloc_overflow:
5659 if (! ((*info->callbacks->reloc_overflow)
5660 (info,
5661 (link_order->type == bfd_section_reloc_link_order
5662 ? bfd_section_name (output_bfd,
5663 link_order->u.reloc.p->u.section)
5664 : link_order->u.reloc.p->u.name),
5665 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5666 (bfd_vma) 0)))
5668 free (buf);
5669 return false;
5671 break;
5673 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5674 (file_ptr) link_order->offset, size);
5675 free (buf);
5676 if (! ok)
5677 return false;
5680 /* The address of a reloc is relative to the section in a
5681 relocateable file, and is a virtual address in an executable
5682 file. */
5683 offset = link_order->offset;
5684 if (! info->relocateable)
5685 offset += output_section->vma;
5687 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5689 if (rel_hdr->sh_type == SHT_REL)
5691 Elf_Internal_Rel irel;
5692 Elf_External_Rel *erel;
5694 irel.r_offset = offset;
5695 irel.r_info = ELF_R_INFO (indx, howto->type);
5696 erel = ((Elf_External_Rel *) rel_hdr->contents
5697 + elf_section_data (output_section)->rel_count);
5698 elf_swap_reloc_out (output_bfd, &irel, erel);
5700 else
5702 Elf_Internal_Rela irela;
5703 Elf_External_Rela *erela;
5705 irela.r_offset = offset;
5706 irela.r_info = ELF_R_INFO (indx, howto->type);
5707 irela.r_addend = addend;
5708 erela = ((Elf_External_Rela *) rel_hdr->contents
5709 + elf_section_data (output_section)->rel_count);
5710 elf_swap_reloca_out (output_bfd, &irela, erela);
5713 ++elf_section_data (output_section)->rel_count;
5715 return true;
5719 /* Allocate a pointer to live in a linker created section. */
5721 boolean
5722 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
5723 bfd *abfd;
5724 struct bfd_link_info *info;
5725 elf_linker_section_t *lsect;
5726 struct elf_link_hash_entry *h;
5727 const Elf_Internal_Rela *rel;
5729 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
5730 elf_linker_section_pointers_t *linker_section_ptr;
5731 unsigned long r_symndx = ELF_R_SYM (rel->r_info);;
5733 BFD_ASSERT (lsect != NULL);
5735 /* Is this a global symbol? */
5736 if (h != NULL)
5738 /* Has this symbol already been allocated, if so, our work is done */
5739 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
5740 rel->r_addend,
5741 lsect->which))
5742 return true;
5744 ptr_linker_section_ptr = &h->linker_section_pointer;
5745 /* Make sure this symbol is output as a dynamic symbol. */
5746 if (h->dynindx == -1)
5748 if (! elf_link_record_dynamic_symbol (info, h))
5749 return false;
5752 if (lsect->rel_section)
5753 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
5756 else /* Allocation of a pointer to a local symbol */
5758 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
5760 /* Allocate a table to hold the local symbols if first time */
5761 if (!ptr)
5763 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
5764 register unsigned int i;
5766 ptr = (elf_linker_section_pointers_t **)
5767 bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *));
5769 if (!ptr)
5770 return false;
5772 elf_local_ptr_offsets (abfd) = ptr;
5773 for (i = 0; i < num_symbols; i++)
5774 ptr[i] = (elf_linker_section_pointers_t *)0;
5777 /* Has this symbol already been allocated, if so, our work is done */
5778 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
5779 rel->r_addend,
5780 lsect->which))
5781 return true;
5783 ptr_linker_section_ptr = &ptr[r_symndx];
5785 if (info->shared)
5787 /* If we are generating a shared object, we need to
5788 output a R_<xxx>_RELATIVE reloc so that the
5789 dynamic linker can adjust this GOT entry. */
5790 BFD_ASSERT (lsect->rel_section != NULL);
5791 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
5795 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5796 from internal memory. */
5797 BFD_ASSERT (ptr_linker_section_ptr != NULL);
5798 linker_section_ptr = (elf_linker_section_pointers_t *)
5799 bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t));
5801 if (!linker_section_ptr)
5802 return false;
5804 linker_section_ptr->next = *ptr_linker_section_ptr;
5805 linker_section_ptr->addend = rel->r_addend;
5806 linker_section_ptr->which = lsect->which;
5807 linker_section_ptr->written_address_p = false;
5808 *ptr_linker_section_ptr = linker_section_ptr;
5810 #if 0
5811 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
5813 linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8);
5814 lsect->hole_offset += ARCH_SIZE / 8;
5815 lsect->sym_offset += ARCH_SIZE / 8;
5816 if (lsect->sym_hash) /* Bump up symbol value if needed */
5818 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
5819 #ifdef DEBUG
5820 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
5821 lsect->sym_hash->root.root.string,
5822 (long)ARCH_SIZE / 8,
5823 (long)lsect->sym_hash->root.u.def.value);
5824 #endif
5827 else
5828 #endif
5829 linker_section_ptr->offset = lsect->section->_raw_size;
5831 lsect->section->_raw_size += ARCH_SIZE / 8;
5833 #ifdef DEBUG
5834 fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5835 lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size);
5836 #endif
5838 return true;
5842 #if ARCH_SIZE==64
5843 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5844 #endif
5845 #if ARCH_SIZE==32
5846 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5847 #endif
5849 /* Fill in the address for a pointer generated in alinker section. */
5851 bfd_vma
5852 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc)
5853 bfd *output_bfd;
5854 bfd *input_bfd;
5855 struct bfd_link_info *info;
5856 elf_linker_section_t *lsect;
5857 struct elf_link_hash_entry *h;
5858 bfd_vma relocation;
5859 const Elf_Internal_Rela *rel;
5860 int relative_reloc;
5862 elf_linker_section_pointers_t *linker_section_ptr;
5864 BFD_ASSERT (lsect != NULL);
5866 if (h != NULL) /* global symbol */
5868 linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
5869 rel->r_addend,
5870 lsect->which);
5872 BFD_ASSERT (linker_section_ptr != NULL);
5874 if (! elf_hash_table (info)->dynamic_sections_created
5875 || (info->shared
5876 && info->symbolic
5877 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5879 /* This is actually a static link, or it is a
5880 -Bsymbolic link and the symbol is defined
5881 locally. We must initialize this entry in the
5882 global section.
5884 When doing a dynamic link, we create a .rela.<xxx>
5885 relocation entry to initialize the value. This
5886 is done in the finish_dynamic_symbol routine. */
5887 if (!linker_section_ptr->written_address_p)
5889 linker_section_ptr->written_address_p = true;
5890 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
5891 lsect->section->contents + linker_section_ptr->offset);
5895 else /* local symbol */
5897 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
5898 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
5899 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
5900 linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx],
5901 rel->r_addend,
5902 lsect->which);
5904 BFD_ASSERT (linker_section_ptr != NULL);
5906 /* Write out pointer if it hasn't been rewritten out before */
5907 if (!linker_section_ptr->written_address_p)
5909 linker_section_ptr->written_address_p = true;
5910 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
5911 lsect->section->contents + linker_section_ptr->offset);
5913 if (info->shared)
5915 asection *srel = lsect->rel_section;
5916 Elf_Internal_Rela outrel;
5918 /* We need to generate a relative reloc for the dynamic linker. */
5919 if (!srel)
5920 lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5921 lsect->rel_name);
5923 BFD_ASSERT (srel != NULL);
5925 outrel.r_offset = (lsect->section->output_section->vma
5926 + lsect->section->output_offset
5927 + linker_section_ptr->offset);
5928 outrel.r_info = ELF_R_INFO (0, relative_reloc);
5929 outrel.r_addend = 0;
5930 elf_swap_reloca_out (output_bfd, &outrel,
5931 (((Elf_External_Rela *)
5932 lsect->section->contents)
5933 + elf_section_data (lsect->section)->rel_count));
5934 ++elf_section_data (lsect->section)->rel_count;
5939 relocation = (lsect->section->output_offset
5940 + linker_section_ptr->offset
5941 - lsect->hole_offset
5942 - lsect->sym_offset);
5944 #ifdef DEBUG
5945 fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5946 lsect->name, (long)relocation, (long)relocation);
5947 #endif
5949 /* Subtract out the addend, because it will get added back in by the normal
5950 processing. */
5951 return relocation - linker_section_ptr->addend;
5954 /* Garbage collect unused sections. */
5956 static boolean elf_gc_mark
5957 PARAMS ((struct bfd_link_info *info, asection *sec,
5958 asection * (*gc_mark_hook)
5959 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
5960 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
5962 static boolean elf_gc_sweep
5963 PARAMS ((struct bfd_link_info *info,
5964 boolean (*gc_sweep_hook)
5965 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
5966 const Elf_Internal_Rela *relocs))));
5968 static boolean elf_gc_sweep_symbol
5969 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
5971 static boolean elf_gc_allocate_got_offsets
5972 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
5974 static boolean elf_gc_propagate_vtable_entries_used
5975 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
5977 static boolean elf_gc_smash_unused_vtentry_relocs
5978 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
5980 /* The mark phase of garbage collection. For a given section, mark
5981 it, and all the sections which define symbols to which it refers. */
5983 static boolean
5984 elf_gc_mark (info, sec, gc_mark_hook)
5985 struct bfd_link_info *info;
5986 asection *sec;
5987 asection * (*gc_mark_hook)
5988 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
5989 struct elf_link_hash_entry *, Elf_Internal_Sym *));
5991 boolean ret = true;
5993 sec->gc_mark = 1;
5995 /* Look through the section relocs. */
5997 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5999 Elf_Internal_Rela *relstart, *rel, *relend;
6000 Elf_Internal_Shdr *symtab_hdr;
6001 struct elf_link_hash_entry **sym_hashes;
6002 size_t nlocsyms;
6003 size_t extsymoff;
6004 Elf_External_Sym *locsyms, *freesyms = NULL;
6005 bfd *input_bfd = sec->owner;
6006 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6008 /* GCFIXME: how to arrange so that relocs and symbols are not
6009 reread continually? */
6011 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6012 sym_hashes = elf_sym_hashes (input_bfd);
6014 /* Read the local symbols. */
6015 if (elf_bad_symtab (input_bfd))
6017 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6018 extsymoff = 0;
6020 else
6021 extsymoff = nlocsyms = symtab_hdr->sh_info;
6022 if (symtab_hdr->contents)
6023 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
6024 else if (nlocsyms == 0)
6025 locsyms = NULL;
6026 else
6028 locsyms = freesyms =
6029 bfd_malloc (nlocsyms * sizeof (Elf_External_Sym));
6030 if (freesyms == NULL
6031 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6032 || (bfd_read (locsyms, sizeof (Elf_External_Sym),
6033 nlocsyms, input_bfd)
6034 != nlocsyms * sizeof (Elf_External_Sym)))
6036 ret = false;
6037 goto out1;
6041 /* Read the relocations. */
6042 relstart = (NAME(_bfd_elf,link_read_relocs)
6043 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
6044 info->keep_memory));
6045 if (relstart == NULL)
6047 ret = false;
6048 goto out1;
6050 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6052 for (rel = relstart; rel < relend; rel++)
6054 unsigned long r_symndx;
6055 asection *rsec;
6056 struct elf_link_hash_entry *h;
6057 Elf_Internal_Sym s;
6059 r_symndx = ELF_R_SYM (rel->r_info);
6060 if (r_symndx == 0)
6061 continue;
6063 if (elf_bad_symtab (sec->owner))
6065 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6066 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
6067 rsec = (*gc_mark_hook)(sec->owner, info, rel, NULL, &s);
6068 else
6070 h = sym_hashes[r_symndx - extsymoff];
6071 rsec = (*gc_mark_hook)(sec->owner, info, rel, h, NULL);
6074 else if (r_symndx >= nlocsyms)
6076 h = sym_hashes[r_symndx - extsymoff];
6077 rsec = (*gc_mark_hook)(sec->owner, info, rel, h, NULL);
6079 else
6081 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6082 rsec = (*gc_mark_hook)(sec->owner, info, rel, NULL, &s);
6085 if (rsec && !rsec->gc_mark)
6086 if (!elf_gc_mark (info, rsec, gc_mark_hook))
6088 ret = false;
6089 goto out2;
6093 out2:
6094 if (!info->keep_memory)
6095 free (relstart);
6096 out1:
6097 if (freesyms)
6098 free (freesyms);
6101 return ret;
6104 /* The sweep phase of garbage collection. Remove all garbage sections. */
6106 static boolean
6107 elf_gc_sweep (info, gc_sweep_hook)
6108 struct bfd_link_info *info;
6109 boolean (*gc_sweep_hook)
6110 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
6111 const Elf_Internal_Rela *relocs));
6113 bfd *sub;
6115 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6117 asection *o;
6119 for (o = sub->sections; o != NULL; o = o->next)
6121 /* Keep special sections. Keep .debug sections. */
6122 if ((o->flags & SEC_LINKER_CREATED)
6123 || (o->flags & SEC_DEBUGGING))
6124 o->gc_mark = 1;
6126 if (o->gc_mark)
6127 continue;
6129 /* Skip sweeping sections already excluded. */
6130 if (o->flags & SEC_EXCLUDE)
6131 continue;
6133 /* Since this is early in the link process, it is simple
6134 to remove a section from the output. */
6135 o->flags |= SEC_EXCLUDE;
6137 /* But we also have to update some of the relocation
6138 info we collected before. */
6139 if (gc_sweep_hook
6140 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
6142 Elf_Internal_Rela *internal_relocs;
6143 boolean r;
6145 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6146 (o->owner, o, NULL, NULL, info->keep_memory));
6147 if (internal_relocs == NULL)
6148 return false;
6150 r = (*gc_sweep_hook)(o->owner, info, o, internal_relocs);
6152 if (!info->keep_memory)
6153 free (internal_relocs);
6155 if (!r)
6156 return false;
6161 /* Remove the symbols that were in the swept sections from the dynamic
6162 symbol table. GCFIXME: Anyone know how to get them out of the
6163 static symbol table as well? */
6165 int i = 0;
6167 elf_link_hash_traverse (elf_hash_table (info),
6168 elf_gc_sweep_symbol,
6169 (PTR) &i);
6171 elf_hash_table (info)->dynsymcount = i;
6174 return true;
6177 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6179 static boolean
6180 elf_gc_sweep_symbol (h, idxptr)
6181 struct elf_link_hash_entry *h;
6182 PTR idxptr;
6184 int *idx = (int *) idxptr;
6186 if (h->dynindx != -1
6187 && ((h->root.type != bfd_link_hash_defined
6188 && h->root.type != bfd_link_hash_defweak)
6189 || h->root.u.def.section->gc_mark))
6190 h->dynindx = (*idx)++;
6192 return true;
6195 /* Propogate collected vtable information. This is called through
6196 elf_link_hash_traverse. */
6198 static boolean
6199 elf_gc_propagate_vtable_entries_used (h, okp)
6200 struct elf_link_hash_entry *h;
6201 PTR okp;
6203 /* Those that are not vtables. */
6204 if (h->vtable_parent == NULL)
6205 return true;
6207 /* Those vtables that do not have parents, we cannot merge. */
6208 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
6209 return true;
6211 /* If we've already been done, exit. */
6212 if (h->vtable_entries_used && h->vtable_entries_used[-1])
6213 return true;
6215 /* Make sure the parent's table is up to date. */
6216 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
6218 if (h->vtable_entries_used == NULL)
6220 /* None of this table's entries were referenced. Re-use the
6221 parent's table. */
6222 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
6223 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
6225 else
6227 size_t n;
6228 boolean *cu, *pu;
6230 /* Or the parent's entries into ours. */
6231 cu = h->vtable_entries_used;
6232 cu[-1] = true;
6233 pu = h->vtable_parent->vtable_entries_used;
6234 if (pu != NULL)
6236 n = h->vtable_parent->vtable_entries_size / FILE_ALIGN;
6237 while (--n != 0)
6239 if (*pu) *cu = true;
6240 pu++, cu++;
6245 return true;
6248 static boolean
6249 elf_gc_smash_unused_vtentry_relocs (h, okp)
6250 struct elf_link_hash_entry *h;
6251 PTR okp;
6253 asection *sec;
6254 bfd_vma hstart, hend;
6255 Elf_Internal_Rela *relstart, *relend, *rel;
6256 struct elf_backend_data *bed;
6258 /* Take care of both those symbols that do not describe vtables as
6259 well as those that are not loaded. */
6260 if (h->vtable_parent == NULL)
6261 return true;
6263 BFD_ASSERT (h->root.type == bfd_link_hash_defined
6264 || h->root.type == bfd_link_hash_defweak);
6266 sec = h->root.u.def.section;
6267 hstart = h->root.u.def.value;
6268 hend = hstart + h->size;
6270 relstart = (NAME(_bfd_elf,link_read_relocs)
6271 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
6272 if (!relstart)
6273 return *(boolean *)okp = false;
6274 bed = get_elf_backend_data (sec->owner);
6275 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6277 for (rel = relstart; rel < relend; ++rel)
6278 if (rel->r_offset >= hstart && rel->r_offset < hend)
6280 /* If the entry is in use, do nothing. */
6281 if (h->vtable_entries_used
6282 && (rel->r_offset - hstart) < h->vtable_entries_size)
6284 bfd_vma entry = (rel->r_offset - hstart) / FILE_ALIGN;
6285 if (h->vtable_entries_used[entry])
6286 continue;
6288 /* Otherwise, kill it. */
6289 rel->r_offset = rel->r_info = rel->r_addend = 0;
6292 return true;
6295 /* Do mark and sweep of unused sections. */
6297 boolean
6298 elf_gc_sections (abfd, info)
6299 bfd *abfd;
6300 struct bfd_link_info *info;
6302 boolean ok = true;
6303 bfd *sub;
6304 asection * (*gc_mark_hook)
6305 PARAMS ((bfd *abfd, struct bfd_link_info *, Elf_Internal_Rela *,
6306 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
6308 if (!get_elf_backend_data (abfd)->can_gc_sections
6309 || info->relocateable
6310 || elf_hash_table (info)->dynamic_sections_created)
6311 return true;
6313 /* Apply transitive closure to the vtable entry usage info. */
6314 elf_link_hash_traverse (elf_hash_table (info),
6315 elf_gc_propagate_vtable_entries_used,
6316 (PTR) &ok);
6317 if (!ok)
6318 return false;
6320 /* Kill the vtable relocations that were not used. */
6321 elf_link_hash_traverse (elf_hash_table (info),
6322 elf_gc_smash_unused_vtentry_relocs,
6323 (PTR) &ok);
6324 if (!ok)
6325 return false;
6327 /* Grovel through relocs to find out who stays ... */
6329 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
6330 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6332 asection *o;
6333 for (o = sub->sections; o != NULL; o = o->next)
6335 if (o->flags & SEC_KEEP)
6336 if (!elf_gc_mark (info, o, gc_mark_hook))
6337 return false;
6341 /* ... and mark SEC_EXCLUDE for those that go. */
6342 if (!elf_gc_sweep(info, get_elf_backend_data (abfd)->gc_sweep_hook))
6343 return false;
6345 return true;
6348 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
6350 boolean
6351 elf_gc_record_vtinherit (abfd, sec, h, offset)
6352 bfd *abfd;
6353 asection *sec;
6354 struct elf_link_hash_entry *h;
6355 bfd_vma offset;
6357 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6358 struct elf_link_hash_entry **search, *child;
6359 bfd_size_type extsymcount;
6361 /* The sh_info field of the symtab header tells us where the
6362 external symbols start. We don't care about the local symbols at
6363 this point. */
6364 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
6365 if (!elf_bad_symtab (abfd))
6366 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
6368 sym_hashes = elf_sym_hashes (abfd);
6369 sym_hashes_end = sym_hashes + extsymcount;
6371 /* Hunt down the child symbol, which is in this section at the same
6372 offset as the relocation. */
6373 for (search = sym_hashes; search != sym_hashes_end; ++search)
6375 if ((child = *search) != NULL
6376 && (child->root.type == bfd_link_hash_defined
6377 || child->root.type == bfd_link_hash_defweak)
6378 && child->root.u.def.section == sec
6379 && child->root.u.def.value == offset)
6380 goto win;
6383 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
6384 bfd_get_filename (abfd), sec->name,
6385 (unsigned long)offset);
6386 bfd_set_error (bfd_error_invalid_operation);
6387 return false;
6389 win:
6390 if (!h)
6392 /* This *should* only be the absolute section. It could potentially
6393 be that someone has defined a non-global vtable though, which
6394 would be bad. It isn't worth paging in the local symbols to be
6395 sure though; that case should simply be handled by the assembler. */
6397 child->vtable_parent = (struct elf_link_hash_entry *) -1;
6399 else
6400 child->vtable_parent = h;
6402 return true;
6405 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
6407 boolean
6408 elf_gc_record_vtentry (abfd, sec, h, addend)
6409 bfd *abfd ATTRIBUTE_UNUSED;
6410 asection *sec ATTRIBUTE_UNUSED;
6411 struct elf_link_hash_entry *h;
6412 bfd_vma addend;
6414 if (addend >= h->vtable_entries_size)
6416 size_t size, bytes;
6417 boolean *ptr = h->vtable_entries_used;
6419 /* While the symbol is undefined, we have to be prepared to handle
6420 a zero size. */
6421 if (h->root.type == bfd_link_hash_undefined)
6422 size = addend;
6423 else
6425 size = h->size;
6426 if (size < addend)
6428 /* Oops! We've got a reference past the defined end of
6429 the table. This is probably a bug -- shall we warn? */
6430 size = addend;
6434 /* Allocate one extra entry for use as a "done" flag for the
6435 consolidation pass. */
6436 bytes = (size / FILE_ALIGN + 1) * sizeof (boolean);
6438 if (ptr)
6440 ptr = bfd_realloc (ptr - 1, bytes);
6442 if (ptr != NULL)
6444 size_t oldbytes;
6446 oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof (boolean);
6447 memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes);
6450 else
6451 ptr = bfd_zmalloc (bytes);
6453 if (ptr == NULL)
6454 return false;
6456 /* And arrange for that done flag to be at index -1. */
6457 h->vtable_entries_used = ptr + 1;
6458 h->vtable_entries_size = size;
6461 h->vtable_entries_used[addend / FILE_ALIGN] = true;
6463 return true;
6466 /* And an accompanying bit to work out final got entry offsets once
6467 we're done. Should be called from final_link. */
6469 boolean
6470 elf_gc_common_finalize_got_offsets (abfd, info)
6471 bfd *abfd;
6472 struct bfd_link_info *info;
6474 bfd *i;
6475 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6476 bfd_vma gotoff;
6478 /* The GOT offset is relative to the .got section, but the GOT header is
6479 put into the .got.plt section, if the backend uses it. */
6480 if (bed->want_got_plt)
6481 gotoff = 0;
6482 else
6483 gotoff = bed->got_header_size;
6485 /* Do the local .got entries first. */
6486 for (i = info->input_bfds; i; i = i->link_next)
6488 bfd_signed_vma *local_got = elf_local_got_refcounts (i);
6489 bfd_size_type j, locsymcount;
6490 Elf_Internal_Shdr *symtab_hdr;
6492 if (!local_got)
6493 continue;
6495 symtab_hdr = &elf_tdata (i)->symtab_hdr;
6496 if (elf_bad_symtab (i))
6497 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6498 else
6499 locsymcount = symtab_hdr->sh_info;
6501 for (j = 0; j < locsymcount; ++j)
6503 if (local_got[j] > 0)
6505 local_got[j] = gotoff;
6506 gotoff += ARCH_SIZE / 8;
6508 else
6509 local_got[j] = (bfd_vma) -1;
6513 /* Then the global .got and .plt entries. */
6514 elf_link_hash_traverse (elf_hash_table (info),
6515 elf_gc_allocate_got_offsets,
6516 (PTR) &gotoff);
6517 return true;
6520 /* We need a special top-level link routine to convert got reference counts
6521 to real got offsets. */
6523 static boolean
6524 elf_gc_allocate_got_offsets (h, offarg)
6525 struct elf_link_hash_entry *h;
6526 PTR offarg;
6528 bfd_vma *off = (bfd_vma *) offarg;
6530 if (h->got.refcount > 0)
6532 h->got.offset = off[0];
6533 off[0] += ARCH_SIZE / 8;
6535 else
6536 h->got.offset = (bfd_vma) -1;
6538 return true;
6541 /* Many folk need no more in the way of final link than this, once
6542 got entry reference counting is enabled. */
6544 boolean
6545 elf_gc_common_final_link (abfd, info)
6546 bfd *abfd;
6547 struct bfd_link_info *info;
6549 if (!elf_gc_common_finalize_got_offsets (abfd, info))
6550 return false;
6552 /* Invoke the regular ELF backend linker to do all the work. */
6553 return elf_bfd_final_link (abfd, info);
6556 /* This function will be called though elf_link_hash_traverse to store
6557 all hash value of the exported symbols in an array. */
6559 static boolean
6560 elf_collect_hash_codes (h, data)
6561 struct elf_link_hash_entry *h;
6562 PTR data;
6564 unsigned long **valuep = (unsigned long **) data;
6565 const char *name;
6566 char *p;
6567 unsigned long ha;
6568 char *alc = NULL;
6570 /* Ignore indirect symbols. These are added by the versioning code. */
6571 if (h->dynindx == -1)
6572 return true;
6574 name = h->root.root.string;
6575 p = strchr (name, ELF_VER_CHR);
6576 if (p != NULL)
6578 alc = bfd_malloc (p - name + 1);
6579 memcpy (alc, name, p - name);
6580 alc[p - name] = '\0';
6581 name = alc;
6584 /* Compute the hash value. */
6585 ha = bfd_elf_hash (name);
6587 /* Store the found hash value in the array given as the argument. */
6588 *(*valuep)++ = ha;
6590 /* And store it in the struct so that we can put it in the hash table
6591 later. */
6592 h->elf_hash_value = ha;
6594 if (alc != NULL)
6595 free (alc);
6597 return true;