source: trunk/src/binutils/bfd/elflink.h@ 524

Last change on this file since 524 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 211.4 KB
Line 
1/* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* ELF linker code. */
22
23/* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
25
26struct elf_info_failed
27{
28 boolean failed;
29 struct bfd_link_info *info;
30};
31
32static boolean elf_link_add_object_symbols
33 PARAMS ((bfd *, struct bfd_link_info *));
34static boolean elf_link_add_archive_symbols
35 PARAMS ((bfd *, struct bfd_link_info *));
36static boolean elf_merge_symbol
37 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
38 asection **, bfd_vma *, struct elf_link_hash_entry **,
39 boolean *, boolean *, boolean *, boolean));
40static boolean elf_export_symbol
41 PARAMS ((struct elf_link_hash_entry *, PTR));
42static boolean elf_fix_symbol_flags
43 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
44static boolean elf_adjust_dynamic_symbol
45 PARAMS ((struct elf_link_hash_entry *, PTR));
46static boolean elf_link_find_version_dependencies
47 PARAMS ((struct elf_link_hash_entry *, PTR));
48static boolean elf_link_find_version_dependencies
49 PARAMS ((struct elf_link_hash_entry *, PTR));
50static boolean elf_link_assign_sym_version
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52static boolean elf_collect_hash_codes
53 PARAMS ((struct elf_link_hash_entry *, PTR));
54static boolean elf_link_read_relocs_from_section
55 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
56static void elf_link_output_relocs
57 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
58static boolean elf_link_size_reloc_section
59 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
60static void elf_link_adjust_relocs
61 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
62 struct elf_link_hash_entry **));
63
64/* Given an ELF BFD, add symbols to the global hash table as
65 appropriate. */
66
67boolean
68elf_bfd_link_add_symbols (abfd, info)
69 bfd *abfd;
70 struct bfd_link_info *info;
71{
72 switch (bfd_get_format (abfd))
73 {
74 case bfd_object:
75 return elf_link_add_object_symbols (abfd, info);
76 case bfd_archive:
77 return elf_link_add_archive_symbols (abfd, info);
78 default:
79 bfd_set_error (bfd_error_wrong_format);
80 return false;
81 }
82}
83
84
85/* Return true iff this is a non-common, definition of a non-function symbol. */
86static boolean
87is_global_data_symbol_definition (abfd, sym)
88 bfd * abfd ATTRIBUTE_UNUSED;
89 Elf_Internal_Sym * sym;
90{
91 /* Local symbols do not count, but target specific ones might. */
92 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
93 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
94 return false;
95
96 /* Function symbols do not count. */
97 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
98 return false;
99
100 /* If the section is undefined, then so is the symbol. */
101 if (sym->st_shndx == SHN_UNDEF)
102 return false;
103
104 /* If the symbol is defined in the common section, then
105 it is a common definition and so does not count. */
106 if (sym->st_shndx == SHN_COMMON)
107 return false;
108
109 /* If the symbol is in a target specific section then we
110 must rely upon the backend to tell us what it is. */
111 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
112 /* FIXME - this function is not coded yet:
113
114 return _bfd_is_global_symbol_definition (abfd, sym);
115
116 Instead for now assume that the definition is not global,
117 Even if this is wrong, at least the linker will behave
118 in the same way that it used to do. */
119 return false;
120
121 return true;
122}
123
124/* Search the symbol table of the archive element of the archive ABFD
125 whose archive map contains a mention of SYMDEF, and determine if
126 the symbol is defined in this element. */
127static boolean
128elf_link_is_defined_archive_symbol (abfd, symdef)
129 bfd * abfd;
130 carsym * symdef;
131{
132 Elf_Internal_Shdr * hdr;
133 Elf_External_Sym * esym;
134 Elf_External_Sym * esymend;
135 Elf_External_Sym * buf = NULL;
136 size_t symcount;
137 size_t extsymcount;
138 size_t extsymoff;
139 boolean result = false;
140
141 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
142 if (abfd == (bfd *) NULL)
143 return false;
144
145 if (! bfd_check_format (abfd, bfd_object))
146 return false;
147
148 /* If we have already included the element containing this symbol in the
149 link then we do not need to include it again. Just claim that any symbol
150 it contains is not a definition, so that our caller will not decide to
151 (re)include this element. */
152 if (abfd->archive_pass)
153 return false;
154
155 /* Select the appropriate symbol table. */
156 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
157 hdr = &elf_tdata (abfd)->symtab_hdr;
158 else
159 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
160
161 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
162
163 /* The sh_info field of the symtab header tells us where the
164 external symbols start. We don't care about the local symbols. */
165 if (elf_bad_symtab (abfd))
166 {
167 extsymcount = symcount;
168 extsymoff = 0;
169 }
170 else
171 {
172 extsymcount = symcount - hdr->sh_info;
173 extsymoff = hdr->sh_info;
174 }
175
176 buf = ((Elf_External_Sym *)
177 bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
178 if (buf == NULL && extsymcount != 0)
179 return false;
180
181 /* Read in the symbol table.
182 FIXME: This ought to be cached somewhere. */
183 if (bfd_seek (abfd,
184 hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
185 SEEK_SET) != 0
186 || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
187 != extsymcount * sizeof (Elf_External_Sym)))
188 {
189 free (buf);
190 return false;
191 }
192
193 /* Scan the symbol table looking for SYMDEF. */
194 esymend = buf + extsymcount;
195 for (esym = buf;
196 esym < esymend;
197 esym++)
198 {
199 Elf_Internal_Sym sym;
200 const char * name;
201
202 elf_swap_symbol_in (abfd, esym, & sym);
203
204 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
205 if (name == (const char *) NULL)
206 break;
207
208 if (strcmp (name, symdef->name) == 0)
209 {
210 result = is_global_data_symbol_definition (abfd, & sym);
211 break;
212 }
213 }
214
215 free (buf);
216
217 return result;
218}
219
220
221/* Add symbols from an ELF archive file to the linker hash table. We
222 don't use _bfd_generic_link_add_archive_symbols because of a
223 problem which arises on UnixWare. The UnixWare libc.so is an
224 archive which includes an entry libc.so.1 which defines a bunch of
225 symbols. The libc.so archive also includes a number of other
226 object files, which also define symbols, some of which are the same
227 as those defined in libc.so.1. Correct linking requires that we
228 consider each object file in turn, and include it if it defines any
229 symbols we need. _bfd_generic_link_add_archive_symbols does not do
230 this; it looks through the list of undefined symbols, and includes
231 any object file which defines them. When this algorithm is used on
232 UnixWare, it winds up pulling in libc.so.1 early and defining a
233 bunch of symbols. This means that some of the other objects in the
234 archive are not included in the link, which is incorrect since they
235 precede libc.so.1 in the archive.
236
237 Fortunately, ELF archive handling is simpler than that done by
238 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
239 oddities. In ELF, if we find a symbol in the archive map, and the
240 symbol is currently undefined, we know that we must pull in that
241 object file.
242
243 Unfortunately, we do have to make multiple passes over the symbol
244 table until nothing further is resolved. */
245
246static boolean
247elf_link_add_archive_symbols (abfd, info)
248 bfd *abfd;
249 struct bfd_link_info *info;
250{
251 symindex c;
252 boolean *defined = NULL;
253 boolean *included = NULL;
254 carsym *symdefs;
255 boolean loop;
256
257 if (! bfd_has_map (abfd))
258 {
259 /* An empty archive is a special case. */
260 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
261 return true;
262 bfd_set_error (bfd_error_no_armap);
263 return false;
264 }
265
266 /* Keep track of all symbols we know to be already defined, and all
267 files we know to be already included. This is to speed up the
268 second and subsequent passes. */
269 c = bfd_ardata (abfd)->symdef_count;
270 if (c == 0)
271 return true;
272 defined = (boolean *) bfd_malloc (c * sizeof (boolean));
273 included = (boolean *) bfd_malloc (c * sizeof (boolean));
274 if (defined == (boolean *) NULL || included == (boolean *) NULL)
275 goto error_return;
276 memset (defined, 0, c * sizeof (boolean));
277 memset (included, 0, c * sizeof (boolean));
278
279 symdefs = bfd_ardata (abfd)->symdefs;
280
281 do
282 {
283 file_ptr last;
284 symindex i;
285 carsym *symdef;
286 carsym *symdefend;
287
288 loop = false;
289 last = -1;
290
291 symdef = symdefs;
292 symdefend = symdef + c;
293 for (i = 0; symdef < symdefend; symdef++, i++)
294 {
295 struct elf_link_hash_entry *h;
296 bfd *element;
297 struct bfd_link_hash_entry *undefs_tail;
298 symindex mark;
299
300 if (defined[i] || included[i])
301 continue;
302 if (symdef->file_offset == last)
303 {
304 included[i] = true;
305 continue;
306 }
307
308 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
309 false, false, false);
310
311 if (h == NULL)
312 {
313 char *p, *copy;
314
315 /* If this is a default version (the name contains @@),
316 look up the symbol again without the version. The
317 effect is that references to the symbol without the
318 version will be matched by the default symbol in the
319 archive. */
320
321 p = strchr (symdef->name, ELF_VER_CHR);
322 if (p == NULL || p[1] != ELF_VER_CHR)
323 continue;
324
325 copy = bfd_alloc (abfd, p - symdef->name + 1);
326 if (copy == NULL)
327 goto error_return;
328 memcpy (copy, symdef->name, p - symdef->name);
329 copy[p - symdef->name] = '\0';
330
331 h = elf_link_hash_lookup (elf_hash_table (info), copy,
332 false, false, false);
333
334 bfd_release (abfd, copy);
335 }
336
337 if (h == NULL)
338 continue;
339
340 if (h->root.type == bfd_link_hash_common)
341 {
342 /* We currently have a common symbol. The archive map contains
343 a reference to this symbol, so we may want to include it. We
344 only want to include it however, if this archive element
345 contains a definition of the symbol, not just another common
346 declaration of it.
347
348 Unfortunately some archivers (including GNU ar) will put
349 declarations of common symbols into their archive maps, as
350 well as real definitions, so we cannot just go by the archive
351 map alone. Instead we must read in the element's symbol
352 table and check that to see what kind of symbol definition
353 this is. */
354 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
355 continue;
356 }
357 else if (h->root.type != bfd_link_hash_undefined)
358 {
359 if (h->root.type != bfd_link_hash_undefweak)
360 defined[i] = true;
361 continue;
362 }
363
364 /* We need to include this archive member. */
365 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
366 if (element == (bfd *) NULL)
367 goto error_return;
368
369 if (! bfd_check_format (element, bfd_object))
370 goto error_return;
371
372 /* Doublecheck that we have not included this object
373 already--it should be impossible, but there may be
374 something wrong with the archive. */
375 if (element->archive_pass != 0)
376 {
377 bfd_set_error (bfd_error_bad_value);
378 goto error_return;
379 }
380 element->archive_pass = 1;
381
382 undefs_tail = info->hash->undefs_tail;
383
384 if (! (*info->callbacks->add_archive_element) (info, element,
385 symdef->name))
386 goto error_return;
387 if (! elf_link_add_object_symbols (element, info))
388 goto error_return;
389
390 /* If there are any new undefined symbols, we need to make
391 another pass through the archive in order to see whether
392 they can be defined. FIXME: This isn't perfect, because
393 common symbols wind up on undefs_tail and because an
394 undefined symbol which is defined later on in this pass
395 does not require another pass. This isn't a bug, but it
396 does make the code less efficient than it could be. */
397 if (undefs_tail != info->hash->undefs_tail)
398 loop = true;
399
400 /* Look backward to mark all symbols from this object file
401 which we have already seen in this pass. */
402 mark = i;
403 do
404 {
405 included[mark] = true;
406 if (mark == 0)
407 break;
408 --mark;
409 }
410 while (symdefs[mark].file_offset == symdef->file_offset);
411
412 /* We mark subsequent symbols from this object file as we go
413 on through the loop. */
414 last = symdef->file_offset;
415 }
416 }
417 while (loop);
418
419 free (defined);
420 free (included);
421
422 return true;
423
424 error_return:
425 if (defined != (boolean *) NULL)
426 free (defined);
427 if (included != (boolean *) NULL)
428 free (included);
429 return false;
430}
431
432/* This function is called when we want to define a new symbol. It
433 handles the various cases which arise when we find a definition in
434 a dynamic object, or when there is already a definition in a
435 dynamic object. The new symbol is described by NAME, SYM, PSEC,
436 and PVALUE. We set SYM_HASH to the hash table entry. We set
437 OVERRIDE if the old symbol is overriding a new definition. We set
438 TYPE_CHANGE_OK if it is OK for the type to change. We set
439 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
440 change, we mean that we shouldn't warn if the type or size does
441 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
442 a shared object. */
443
444static boolean
445elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
446 override, type_change_ok, size_change_ok, dt_needed)
447 bfd *abfd;
448 struct bfd_link_info *info;
449 const char *name;
450 Elf_Internal_Sym *sym;
451 asection **psec;
452 bfd_vma *pvalue;
453 struct elf_link_hash_entry **sym_hash;
454 boolean *override;
455 boolean *type_change_ok;
456 boolean *size_change_ok;
457 boolean dt_needed;
458{
459 asection *sec;
460 struct elf_link_hash_entry *h;
461 int bind;
462 bfd *oldbfd;
463 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
464
465 *override = false;
466
467 sec = *psec;
468 bind = ELF_ST_BIND (sym->st_info);
469
470 if (! bfd_is_und_section (sec))
471 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
472 else
473 h = ((struct elf_link_hash_entry *)
474 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
475 if (h == NULL)
476 return false;
477 *sym_hash = h;
478
479 /* This code is for coping with dynamic objects, and is only useful
480 if we are doing an ELF link. */
481 if (info->hash->creator != abfd->xvec)
482 return true;
483
484 /* For merging, we only care about real symbols. */
485
486 while (h->root.type == bfd_link_hash_indirect
487 || h->root.type == bfd_link_hash_warning)
488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
489
490 /* If we just created the symbol, mark it as being an ELF symbol.
491 Other than that, there is nothing to do--there is no merge issue
492 with a newly defined symbol--so we just return. */
493
494 if (h->root.type == bfd_link_hash_new)
495 {
496 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
497 return true;
498 }
499
500 /* OLDBFD is a BFD associated with the existing symbol. */
501
502 switch (h->root.type)
503 {
504 default:
505 oldbfd = NULL;
506 break;
507
508 case bfd_link_hash_undefined:
509 case bfd_link_hash_undefweak:
510 oldbfd = h->root.u.undef.abfd;
511 break;
512
513 case bfd_link_hash_defined:
514 case bfd_link_hash_defweak:
515 oldbfd = h->root.u.def.section->owner;
516 break;
517
518 case bfd_link_hash_common:
519 oldbfd = h->root.u.c.p->section->owner;
520 break;
521 }
522
523 /* In cases involving weak versioned symbols, we may wind up trying
524 to merge a symbol with itself. Catch that here, to avoid the
525 confusion that results if we try to override a symbol with
526 itself. The additional tests catch cases like
527 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
528 dynamic object, which we do want to handle here. */
529 if (abfd == oldbfd
530 && ((abfd->flags & DYNAMIC) == 0
531 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
532 return true;
533
534 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
535 respectively, is from a dynamic object. */
536
537 if ((abfd->flags & DYNAMIC) != 0)
538 newdyn = true;
539 else
540 newdyn = false;
541
542 if (oldbfd != NULL)
543 olddyn = (oldbfd->flags & DYNAMIC) != 0;
544 else
545 {
546 asection *hsec;
547
548 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
549 indices used by MIPS ELF. */
550 switch (h->root.type)
551 {
552 default:
553 hsec = NULL;
554 break;
555
556 case bfd_link_hash_defined:
557 case bfd_link_hash_defweak:
558 hsec = h->root.u.def.section;
559 break;
560
561 case bfd_link_hash_common:
562 hsec = h->root.u.c.p->section;
563 break;
564 }
565
566 if (hsec == NULL)
567 olddyn = false;
568 else
569 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
570 }
571
572 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
573 respectively, appear to be a definition rather than reference. */
574
575 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
576 newdef = false;
577 else
578 newdef = true;
579
580 if (h->root.type == bfd_link_hash_undefined
581 || h->root.type == bfd_link_hash_undefweak
582 || h->root.type == bfd_link_hash_common)
583 olddef = false;
584 else
585 olddef = true;
586
587 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
588 symbol, respectively, appears to be a common symbol in a dynamic
589 object. If a symbol appears in an uninitialized section, and is
590 not weak, and is not a function, then it may be a common symbol
591 which was resolved when the dynamic object was created. We want
592 to treat such symbols specially, because they raise special
593 considerations when setting the symbol size: if the symbol
594 appears as a common symbol in a regular object, and the size in
595 the regular object is larger, we must make sure that we use the
596 larger size. This problematic case can always be avoided in C,
597 but it must be handled correctly when using Fortran shared
598 libraries.
599
600 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
601 likewise for OLDDYNCOMMON and OLDDEF.
602
603 Note that this test is just a heuristic, and that it is quite
604 possible to have an uninitialized symbol in a shared object which
605 is really a definition, rather than a common symbol. This could
606 lead to some minor confusion when the symbol really is a common
607 symbol in some regular object. However, I think it will be
608 harmless. */
609
610 if (newdyn
611 && newdef
612 && (sec->flags & SEC_ALLOC) != 0
613 && (sec->flags & SEC_LOAD) == 0
614 && sym->st_size > 0
615 && bind != STB_WEAK
616 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
617 newdyncommon = true;
618 else
619 newdyncommon = false;
620
621 if (olddyn
622 && olddef
623 && h->root.type == bfd_link_hash_defined
624 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
625 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
626 && (h->root.u.def.section->flags & SEC_LOAD) == 0
627 && h->size > 0
628 && h->type != STT_FUNC)
629 olddyncommon = true;
630 else
631 olddyncommon = false;
632
633 /* It's OK to change the type if either the existing symbol or the
634 new symbol is weak unless it comes from a DT_NEEDED entry of
635 a shared object, in which case, the DT_NEEDED entry may not be
636 required at the run time. */
637
638 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
639 || h->root.type == bfd_link_hash_undefweak
640 || bind == STB_WEAK)
641 *type_change_ok = true;
642
643 /* It's OK to change the size if either the existing symbol or the
644 new symbol is weak, or if the old symbol is undefined. */
645
646 if (*type_change_ok
647 || h->root.type == bfd_link_hash_undefined)
648 *size_change_ok = true;
649
650 /* If both the old and the new symbols look like common symbols in a
651 dynamic object, set the size of the symbol to the larger of the
652 two. */
653
654 if (olddyncommon
655 && newdyncommon
656 && sym->st_size != h->size)
657 {
658 /* Since we think we have two common symbols, issue a multiple
659 common warning if desired. Note that we only warn if the
660 size is different. If the size is the same, we simply let
661 the old symbol override the new one as normally happens with
662 symbols defined in dynamic objects. */
663
664 if (! ((*info->callbacks->multiple_common)
665 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
666 h->size, abfd, bfd_link_hash_common, sym->st_size)))
667 return false;
668
669 if (sym->st_size > h->size)
670 h->size = sym->st_size;
671
672 *size_change_ok = true;
673 }
674
675 /* If we are looking at a dynamic object, and we have found a
676 definition, we need to see if the symbol was already defined by
677 some other object. If so, we want to use the existing
678 definition, and we do not want to report a multiple symbol
679 definition error; we do this by clobbering *PSEC to be
680 bfd_und_section_ptr.
681
682 We treat a common symbol as a definition if the symbol in the
683 shared library is a function, since common symbols always
684 represent variables; this can cause confusion in principle, but
685 any such confusion would seem to indicate an erroneous program or
686 shared library. We also permit a common symbol in a regular
687 object to override a weak symbol in a shared object.
688
689 We prefer a non-weak definition in a shared library to a weak
690 definition in the executable unless it comes from a DT_NEEDED
691 entry of a shared object, in which case, the DT_NEEDED entry
692 may not be required at the run time. */
693
694 if (newdyn
695 && newdef
696 && (olddef
697 || (h->root.type == bfd_link_hash_common
698 && (bind == STB_WEAK
699 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
700 && (h->root.type != bfd_link_hash_defweak
701 || dt_needed
702 || bind == STB_WEAK))
703 {
704 *override = true;
705 newdef = false;
706 newdyncommon = false;
707
708 *psec = sec = bfd_und_section_ptr;
709 *size_change_ok = true;
710
711 /* If we get here when the old symbol is a common symbol, then
712 we are explicitly letting it override a weak symbol or
713 function in a dynamic object, and we don't want to warn about
714 a type change. If the old symbol is a defined symbol, a type
715 change warning may still be appropriate. */
716
717 if (h->root.type == bfd_link_hash_common)
718 *type_change_ok = true;
719 }
720
721 /* Handle the special case of an old common symbol merging with a
722 new symbol which looks like a common symbol in a shared object.
723 We change *PSEC and *PVALUE to make the new symbol look like a
724 common symbol, and let _bfd_generic_link_add_one_symbol will do
725 the right thing. */
726
727 if (newdyncommon
728 && h->root.type == bfd_link_hash_common)
729 {
730 *override = true;
731 newdef = false;
732 newdyncommon = false;
733 *pvalue = sym->st_size;
734 *psec = sec = bfd_com_section_ptr;
735 *size_change_ok = true;
736 }
737
738 /* If the old symbol is from a dynamic object, and the new symbol is
739 a definition which is not from a dynamic object, then the new
740 symbol overrides the old symbol. Symbols from regular files
741 always take precedence over symbols from dynamic objects, even if
742 they are defined after the dynamic object in the link.
743
744 As above, we again permit a common symbol in a regular object to
745 override a definition in a shared object if the shared object
746 symbol is a function or is weak.
747
748 As above, we permit a non-weak definition in a shared object to
749 override a weak definition in a regular object. */
750
751 if (! newdyn
752 && (newdef
753 || (bfd_is_com_section (sec)
754 && (h->root.type == bfd_link_hash_defweak
755 || h->type == STT_FUNC)))
756 && olddyn
757 && olddef
758 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
759 && (bind != STB_WEAK
760 || h->root.type == bfd_link_hash_defweak))
761 {
762 /* Change the hash table entry to undefined, and let
763 _bfd_generic_link_add_one_symbol do the right thing with the
764 new definition. */
765
766 h->root.type = bfd_link_hash_undefined;
767 h->root.u.undef.abfd = h->root.u.def.section->owner;
768 *size_change_ok = true;
769
770 olddef = false;
771 olddyncommon = false;
772
773 /* We again permit a type change when a common symbol may be
774 overriding a function. */
775
776 if (bfd_is_com_section (sec))
777 *type_change_ok = true;
778
779 /* This union may have been set to be non-NULL when this symbol
780 was seen in a dynamic object. We must force the union to be
781 NULL, so that it is correct for a regular symbol. */
782
783 h->verinfo.vertree = NULL;
784
785 /* In this special case, if H is the target of an indirection,
786 we want the caller to frob with H rather than with the
787 indirect symbol. That will permit the caller to redefine the
788 target of the indirection, rather than the indirect symbol
789 itself. FIXME: This will break the -y option if we store a
790 symbol with a different name. */
791 *sym_hash = h;
792 }
793
794 /* Handle the special case of a new common symbol merging with an
795 old symbol that looks like it might be a common symbol defined in
796 a shared object. Note that we have already handled the case in
797 which a new common symbol should simply override the definition
798 in the shared library. */
799
800 if (! newdyn
801 && bfd_is_com_section (sec)
802 && olddyncommon)
803 {
804 /* It would be best if we could set the hash table entry to a
805 common symbol, but we don't know what to use for the section
806 or the alignment. */
807 if (! ((*info->callbacks->multiple_common)
808 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
809 h->size, abfd, bfd_link_hash_common, sym->st_size)))
810 return false;
811
812 /* If the predumed common symbol in the dynamic object is
813 larger, pretend that the new symbol has its size. */
814
815 if (h->size > *pvalue)
816 *pvalue = h->size;
817
818 /* FIXME: We no longer know the alignment required by the symbol
819 in the dynamic object, so we just wind up using the one from
820 the regular object. */
821
822 olddef = false;
823 olddyncommon = false;
824
825 h->root.type = bfd_link_hash_undefined;
826 h->root.u.undef.abfd = h->root.u.def.section->owner;
827
828 *size_change_ok = true;
829 *type_change_ok = true;
830
831 h->verinfo.vertree = NULL;
832 }
833
834 /* Handle the special case of a weak definition in a regular object
835 followed by a non-weak definition in a shared object. In this
836 case, we prefer the definition in the shared object unless it
837 comes from a DT_NEEDED entry of a shared object, in which case,
838 the DT_NEEDED entry may not be required at the run time. */
839 if (olddef
840 && ! dt_needed
841 && h->root.type == bfd_link_hash_defweak
842 && newdef
843 && newdyn
844 && bind != STB_WEAK)
845 {
846 /* To make this work we have to frob the flags so that the rest
847 of the code does not think we are using the regular
848 definition. */
849 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
850 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
851 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
852 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
853 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
854 | ELF_LINK_HASH_DEF_DYNAMIC);
855
856 /* If H is the target of an indirection, we want the caller to
857 use H rather than the indirect symbol. Otherwise if we are
858 defining a new indirect symbol we will wind up attaching it
859 to the entry we are overriding. */
860 *sym_hash = h;
861 }
862
863 /* Handle the special case of a non-weak definition in a shared
864 object followed by a weak definition in a regular object. In
865 this case we prefer to definition in the shared object. To make
866 this work we have to tell the caller to not treat the new symbol
867 as a definition. */
868 if (olddef
869 && olddyn
870 && h->root.type != bfd_link_hash_defweak
871 && newdef
872 && ! newdyn
873 && bind == STB_WEAK)
874 *override = true;
875
876 return true;
877}
878
879/* Add symbols from an ELF object file to the linker hash table. */
880
881static boolean
882elf_link_add_object_symbols (abfd, info)
883 bfd *abfd;
884 struct bfd_link_info *info;
885{
886 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
887 const Elf_Internal_Sym *,
888 const char **, flagword *,
889 asection **, bfd_vma *));
890 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
891 asection *, const Elf_Internal_Rela *));
892 boolean collect;
893 Elf_Internal_Shdr *hdr;
894 size_t symcount;
895 size_t extsymcount;
896 size_t extsymoff;
897 Elf_External_Sym *buf = NULL;
898 struct elf_link_hash_entry **sym_hash;
899 boolean dynamic;
900 Elf_External_Versym *extversym = NULL;
901 Elf_External_Versym *ever;
902 Elf_External_Dyn *dynbuf = NULL;
903 struct elf_link_hash_entry *weaks;
904 Elf_External_Sym *esym;
905 Elf_External_Sym *esymend;
906 struct elf_backend_data *bed;
907 boolean dt_needed;
908
909 bed = get_elf_backend_data (abfd);
910 add_symbol_hook = bed->elf_add_symbol_hook;
911 collect = bed->collect;
912
913 if ((abfd->flags & DYNAMIC) == 0)
914 dynamic = false;
915 else
916 {
917 dynamic = true;
918
919 /* You can't use -r against a dynamic object. Also, there's no
920 hope of using a dynamic object which does not exactly match
921 the format of the output file. */
922 if (info->relocateable || info->hash->creator != abfd->xvec)
923 {
924 bfd_set_error (bfd_error_invalid_operation);
925 goto error_return;
926 }
927 }
928
929 /* As a GNU extension, any input sections which are named
930 .gnu.warning.SYMBOL are treated as warning symbols for the given
931 symbol. This differs from .gnu.warning sections, which generate
932 warnings when they are included in an output file. */
933 if (! info->shared)
934 {
935 asection *s;
936
937 for (s = abfd->sections; s != NULL; s = s->next)
938 {
939 const char *name;
940
941 name = bfd_get_section_name (abfd, s);
942 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
943 {
944 char *msg;
945 bfd_size_type sz;
946
947 name += sizeof ".gnu.warning." - 1;
948
949 /* If this is a shared object, then look up the symbol
950 in the hash table. If it is there, and it is already
951 been defined, then we will not be using the entry
952 from this shared object, so we don't need to warn.
953 FIXME: If we see the definition in a regular object
954 later on, we will warn, but we shouldn't. The only
955 fix is to keep track of what warnings we are supposed
956 to emit, and then handle them all at the end of the
957 link. */
958 if (dynamic && abfd->xvec == info->hash->creator)
959 {
960 struct elf_link_hash_entry *h;
961
962 h = elf_link_hash_lookup (elf_hash_table (info), name,
963 false, false, true);
964
965 /* FIXME: What about bfd_link_hash_common? */
966 if (h != NULL
967 && (h->root.type == bfd_link_hash_defined
968 || h->root.type == bfd_link_hash_defweak))
969 {
970 /* We don't want to issue this warning. Clobber
971 the section size so that the warning does not
972 get copied into the output file. */
973 s->_raw_size = 0;
974 continue;
975 }
976 }
977
978 sz = bfd_section_size (abfd, s);
979 msg = (char *) bfd_alloc (abfd, sz + 1);
980 if (msg == NULL)
981 goto error_return;
982
983 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
984 goto error_return;
985
986 msg[sz] = '\0';
987
988 if (! (_bfd_generic_link_add_one_symbol
989 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
990 false, collect, (struct bfd_link_hash_entry **) NULL)))
991 goto error_return;
992
993 if (! info->relocateable)
994 {
995 /* Clobber the section size so that the warning does
996 not get copied into the output file. */
997 s->_raw_size = 0;
998 }
999 }
1000 }
1001 }
1002
1003 /* If this is a dynamic object, we always link against the .dynsym
1004 symbol table, not the .symtab symbol table. The dynamic linker
1005 will only see the .dynsym symbol table, so there is no reason to
1006 look at .symtab for a dynamic object. */
1007
1008 if (! dynamic || elf_dynsymtab (abfd) == 0)
1009 hdr = &elf_tdata (abfd)->symtab_hdr;
1010 else
1011 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1012
1013 if (dynamic)
1014 {
1015 /* Read in any version definitions. */
1016
1017 if (! _bfd_elf_slurp_version_tables (abfd))
1018 goto error_return;
1019
1020 /* Read in the symbol versions, but don't bother to convert them
1021 to internal format. */
1022 if (elf_dynversym (abfd) != 0)
1023 {
1024 Elf_Internal_Shdr *versymhdr;
1025
1026 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1027 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1028 if (extversym == NULL)
1029 goto error_return;
1030 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1031 || (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd)
1032 != versymhdr->sh_size))
1033 goto error_return;
1034 }
1035 }
1036
1037 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1038
1039 /* The sh_info field of the symtab header tells us where the
1040 external symbols start. We don't care about the local symbols at
1041 this point. */
1042 if (elf_bad_symtab (abfd))
1043 {
1044 extsymcount = symcount;
1045 extsymoff = 0;
1046 }
1047 else
1048 {
1049 extsymcount = symcount - hdr->sh_info;
1050 extsymoff = hdr->sh_info;
1051 }
1052
1053 buf = ((Elf_External_Sym *)
1054 bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
1055 if (buf == NULL && extsymcount != 0)
1056 goto error_return;
1057
1058 /* We store a pointer to the hash table entry for each external
1059 symbol. */
1060 sym_hash = ((struct elf_link_hash_entry **)
1061 bfd_alloc (abfd,
1062 extsymcount * sizeof (struct elf_link_hash_entry *)));
1063 if (sym_hash == NULL)
1064 goto error_return;
1065 elf_sym_hashes (abfd) = sym_hash;
1066
1067 dt_needed = false;
1068
1069 if (! dynamic)
1070 {
1071 /* If we are creating a shared library, create all the dynamic
1072 sections immediately. We need to attach them to something,
1073 so we attach them to this BFD, provided it is the right
1074 format. FIXME: If there are no input BFD's of the same
1075 format as the output, we can't make a shared library. */
1076 if (info->shared
1077 && ! elf_hash_table (info)->dynamic_sections_created
1078 && abfd->xvec == info->hash->creator)
1079 {
1080 if (! elf_link_create_dynamic_sections (abfd, info))
1081 goto error_return;
1082 }
1083 }
1084 else
1085 {
1086 asection *s;
1087 boolean add_needed;
1088 const char *name;
1089 bfd_size_type oldsize;
1090 bfd_size_type strindex;
1091
1092 /* Find the name to use in a DT_NEEDED entry that refers to this
1093 object. If the object has a DT_SONAME entry, we use it.
1094 Otherwise, if the generic linker stuck something in
1095 elf_dt_name, we use that. Otherwise, we just use the file
1096 name. If the generic linker put a null string into
1097 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1098 there is a DT_SONAME entry. */
1099 add_needed = true;
1100 name = bfd_get_filename (abfd);
1101 if (elf_dt_name (abfd) != NULL)
1102 {
1103 name = elf_dt_name (abfd);
1104 if (*name == '\0')
1105 {
1106 if (elf_dt_soname (abfd) != NULL)
1107 dt_needed = true;
1108
1109 add_needed = false;
1110 }
1111 }
1112 s = bfd_get_section_by_name (abfd, ".dynamic");
1113 if (s != NULL)
1114 {
1115 Elf_External_Dyn *extdyn;
1116 Elf_External_Dyn *extdynend;
1117 int elfsec;
1118 unsigned long link;
1119 int rpath;
1120 int runpath;
1121
1122 dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
1123 if (dynbuf == NULL)
1124 goto error_return;
1125
1126 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1127 (file_ptr) 0, s->_raw_size))
1128 goto error_return;
1129
1130 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1131 if (elfsec == -1)
1132 goto error_return;
1133 link = elf_elfsections (abfd)[elfsec]->sh_link;
1134
1135 {
1136 /* The shared libraries distributed with hpux11 have a bogus
1137 sh_link field for the ".dynamic" section. This code detects
1138 when LINK refers to a section that is not a string table and
1139 tries to find the string table for the ".dynsym" section
1140 instead. */
1141 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[link];
1142 if (hdr->sh_type != SHT_STRTAB)
1143 {
1144 asection *s = bfd_get_section_by_name (abfd, ".dynsym");
1145 int elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1146 if (elfsec == -1)
1147 goto error_return;
1148 link = elf_elfsections (abfd)[elfsec]->sh_link;
1149 }
1150 }
1151
1152 extdyn = dynbuf;
1153 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1154 rpath = 0;
1155 runpath = 0;
1156 for (; extdyn < extdynend; extdyn++)
1157 {
1158 Elf_Internal_Dyn dyn;
1159
1160 elf_swap_dyn_in (abfd, extdyn, &dyn);
1161 if (dyn.d_tag == DT_SONAME)
1162 {
1163 name = bfd_elf_string_from_elf_section (abfd, link,
1164 dyn.d_un.d_val);
1165 if (name == NULL)
1166 goto error_return;
1167 }
1168 if (dyn.d_tag == DT_NEEDED)
1169 {
1170 struct bfd_link_needed_list *n, **pn;
1171 char *fnm, *anm;
1172
1173 n = ((struct bfd_link_needed_list *)
1174 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
1175 fnm = bfd_elf_string_from_elf_section (abfd, link,
1176 dyn.d_un.d_val);
1177 if (n == NULL || fnm == NULL)
1178 goto error_return;
1179 anm = bfd_alloc (abfd, strlen (fnm) + 1);
1180 if (anm == NULL)
1181 goto error_return;
1182 strcpy (anm, fnm);
1183 n->name = anm;
1184 n->by = abfd;
1185 n->next = NULL;
1186 for (pn = &elf_hash_table (info)->needed;
1187 *pn != NULL;
1188 pn = &(*pn)->next)
1189 ;
1190 *pn = n;
1191 }
1192 if (dyn.d_tag == DT_RUNPATH)
1193 {
1194 struct bfd_link_needed_list *n, **pn;
1195 char *fnm, *anm;
1196
1197 /* When we see DT_RPATH before DT_RUNPATH, we have
1198 to clear runpath. Do _NOT_ bfd_release, as that
1199 frees all more recently bfd_alloc'd blocks as
1200 well. */
1201 if (rpath && elf_hash_table (info)->runpath)
1202 elf_hash_table (info)->runpath = NULL;
1203
1204 n = ((struct bfd_link_needed_list *)
1205 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
1206 fnm = bfd_elf_string_from_elf_section (abfd, link,
1207 dyn.d_un.d_val);
1208 if (n == NULL || fnm == NULL)
1209 goto error_return;
1210 anm = bfd_alloc (abfd, strlen (fnm) + 1);
1211 if (anm == NULL)
1212 goto error_return;
1213 strcpy (anm, fnm);
1214 n->name = anm;
1215 n->by = abfd;
1216 n->next = NULL;
1217 for (pn = &elf_hash_table (info)->runpath;
1218 *pn != NULL;
1219 pn = &(*pn)->next)
1220 ;
1221 *pn = n;
1222 runpath = 1;
1223 rpath = 0;
1224 }
1225 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1226 if (!runpath && dyn.d_tag == DT_RPATH)
1227 {
1228 struct bfd_link_needed_list *n, **pn;
1229 char *fnm, *anm;
1230
1231 n = ((struct bfd_link_needed_list *)
1232 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
1233 fnm = bfd_elf_string_from_elf_section (abfd, link,
1234 dyn.d_un.d_val);
1235 if (n == NULL || fnm == NULL)
1236 goto error_return;
1237 anm = bfd_alloc (abfd, strlen (fnm) + 1);
1238 if (anm == NULL)
1239 goto error_return;
1240 strcpy (anm, fnm);
1241 n->name = anm;
1242 n->by = abfd;
1243 n->next = NULL;
1244 for (pn = &elf_hash_table (info)->runpath;
1245 *pn != NULL;
1246 pn = &(*pn)->next)
1247 ;
1248 *pn = n;
1249 rpath = 1;
1250 }
1251 }
1252
1253 free (dynbuf);
1254 dynbuf = NULL;
1255 }
1256
1257 /* We do not want to include any of the sections in a dynamic
1258 object in the output file. We hack by simply clobbering the
1259 list of sections in the BFD. This could be handled more
1260 cleanly by, say, a new section flag; the existing
1261 SEC_NEVER_LOAD flag is not the one we want, because that one
1262 still implies that the section takes up space in the output
1263 file. */
1264 abfd->sections = NULL;
1265 abfd->section_count = 0;
1266
1267 /* If this is the first dynamic object found in the link, create
1268 the special sections required for dynamic linking. */
1269 if (! elf_hash_table (info)->dynamic_sections_created)
1270 {
1271 if (! elf_link_create_dynamic_sections (abfd, info))
1272 goto error_return;
1273 }
1274
1275 if (add_needed)
1276 {
1277 /* Add a DT_NEEDED entry for this dynamic object. */
1278 oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1279 strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
1280 true, false);
1281 if (strindex == (bfd_size_type) -1)
1282 goto error_return;
1283
1284 if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
1285 {
1286 asection *sdyn;
1287 Elf_External_Dyn *dyncon, *dynconend;
1288
1289 /* The hash table size did not change, which means that
1290 the dynamic object name was already entered. If we
1291 have already included this dynamic object in the
1292 link, just ignore it. There is no reason to include
1293 a particular dynamic object more than once. */
1294 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
1295 ".dynamic");
1296 BFD_ASSERT (sdyn != NULL);
1297
1298 dyncon = (Elf_External_Dyn *) sdyn->contents;
1299 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1300 sdyn->_raw_size);
1301 for (; dyncon < dynconend; dyncon++)
1302 {
1303 Elf_Internal_Dyn dyn;
1304
1305 elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
1306 &dyn);
1307 if (dyn.d_tag == DT_NEEDED
1308 && dyn.d_un.d_val == strindex)
1309 {
1310 if (buf != NULL)
1311 free (buf);
1312 if (extversym != NULL)
1313 free (extversym);
1314 return true;
1315 }
1316 }
1317 }
1318
1319 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1320 goto error_return;
1321 }
1322
1323 /* Save the SONAME, if there is one, because sometimes the
1324 linker emulation code will need to know it. */
1325 if (*name == '\0')
1326 name = bfd_get_filename (abfd);
1327 elf_dt_name (abfd) = name;
1328 }
1329
1330 if (bfd_seek (abfd,
1331 hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
1332 SEEK_SET) != 0
1333 || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
1334 != extsymcount * sizeof (Elf_External_Sym)))
1335 goto error_return;
1336
1337 weaks = NULL;
1338
1339 ever = extversym != NULL ? extversym + extsymoff : NULL;
1340 esymend = buf + extsymcount;
1341 for (esym = buf;
1342 esym < esymend;
1343 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1344 {
1345 Elf_Internal_Sym sym;
1346 int bind;
1347 bfd_vma value;
1348 asection *sec;
1349 flagword flags;
1350 const char *name;
1351 struct elf_link_hash_entry *h;
1352 boolean definition;
1353 boolean size_change_ok, type_change_ok;
1354 boolean new_weakdef;
1355 unsigned int old_alignment;
1356
1357 elf_swap_symbol_in (abfd, esym, &sym);
1358
1359 flags = BSF_NO_FLAGS;
1360 sec = NULL;
1361 value = sym.st_value;
1362 *sym_hash = NULL;
1363
1364 bind = ELF_ST_BIND (sym.st_info);
1365 if (bind == STB_LOCAL)
1366 {
1367 /* This should be impossible, since ELF requires that all
1368 global symbols follow all local symbols, and that sh_info
1369 point to the first global symbol. Unfortunatealy, Irix 5
1370 screws this up. */
1371 continue;
1372 }
1373 else if (bind == STB_GLOBAL)
1374 {
1375 if (sym.st_shndx != SHN_UNDEF
1376 && sym.st_shndx != SHN_COMMON)
1377 flags = BSF_GLOBAL;
1378 }
1379 else if (bind == STB_WEAK)
1380 flags = BSF_WEAK;
1381 else
1382 {
1383 /* Leave it up to the processor backend. */
1384 }
1385
1386 if (sym.st_shndx == SHN_UNDEF)
1387 sec = bfd_und_section_ptr;
1388 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
1389 {
1390 sec = section_from_elf_index (abfd, sym.st_shndx);
1391 if (sec == NULL)
1392 sec = bfd_abs_section_ptr;
1393 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1394 value -= sec->vma;
1395 }
1396 else if (sym.st_shndx == SHN_ABS)
1397 sec = bfd_abs_section_ptr;
1398 else if (sym.st_shndx == SHN_COMMON)
1399 {
1400 sec = bfd_com_section_ptr;
1401 /* What ELF calls the size we call the value. What ELF
1402 calls the value we call the alignment. */
1403 value = sym.st_size;
1404 }
1405 else
1406 {
1407 /* Leave it up to the processor backend. */
1408 }
1409
1410 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1411 if (name == (const char *) NULL)
1412 goto error_return;
1413
1414 if (add_symbol_hook)
1415 {
1416 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1417 &value))
1418 goto error_return;
1419
1420 /* The hook function sets the name to NULL if this symbol
1421 should be skipped for some reason. */
1422 if (name == (const char *) NULL)
1423 continue;
1424 }
1425
1426 /* Sanity check that all possibilities were handled. */
1427 if (sec == (asection *) NULL)
1428 {
1429 bfd_set_error (bfd_error_bad_value);
1430 goto error_return;
1431 }
1432
1433 if (bfd_is_und_section (sec)
1434 || bfd_is_com_section (sec))
1435 definition = false;
1436 else
1437 definition = true;
1438
1439 size_change_ok = false;
1440 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1441 old_alignment = 0;
1442 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1443 {
1444 Elf_Internal_Versym iver;
1445 unsigned int vernum = 0;
1446 boolean override;
1447
1448 if (ever != NULL)
1449 {
1450 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1451 vernum = iver.vs_vers & VERSYM_VERSION;
1452
1453 /* If this is a hidden symbol, or if it is not version
1454 1, we append the version name to the symbol name.
1455 However, we do not modify a non-hidden absolute
1456 symbol, because it might be the version symbol
1457 itself. FIXME: What if it isn't? */
1458 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1459 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1460 {
1461 const char *verstr;
1462 int namelen, newlen;
1463 char *newname, *p;
1464
1465 if (sym.st_shndx != SHN_UNDEF)
1466 {
1467 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1468 {
1469 (*_bfd_error_handler)
1470 (_("%s: %s: invalid version %u (max %d)"),
1471 bfd_get_filename (abfd), name, vernum,
1472 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1473 bfd_set_error (bfd_error_bad_value);
1474 goto error_return;
1475 }
1476 else if (vernum > 1)
1477 verstr =
1478 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1479 else
1480 verstr = "";
1481 }
1482 else
1483 {
1484 /* We cannot simply test for the number of
1485 entries in the VERNEED section since the
1486 numbers for the needed versions do not start
1487 at 0. */
1488 Elf_Internal_Verneed *t;
1489
1490 verstr = NULL;
1491 for (t = elf_tdata (abfd)->verref;
1492 t != NULL;
1493 t = t->vn_nextref)
1494 {
1495 Elf_Internal_Vernaux *a;
1496
1497 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1498 {
1499 if (a->vna_other == vernum)
1500 {
1501 verstr = a->vna_nodename;
1502 break;
1503 }
1504 }
1505 if (a != NULL)
1506 break;
1507 }
1508 if (verstr == NULL)
1509 {
1510 (*_bfd_error_handler)
1511 (_("%s: %s: invalid needed version %d"),
1512 bfd_get_filename (abfd), name, vernum);
1513 bfd_set_error (bfd_error_bad_value);
1514 goto error_return;
1515 }
1516 }
1517
1518 namelen = strlen (name);
1519 newlen = namelen + strlen (verstr) + 2;
1520 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1521 ++newlen;
1522
1523 newname = (char *) bfd_alloc (abfd, newlen);
1524 if (newname == NULL)
1525 goto error_return;
1526 strcpy (newname, name);
1527 p = newname + namelen;
1528 *p++ = ELF_VER_CHR;
1529 /* If this is a defined non-hidden version symbol,
1530 we add another @ to the name. This indicates the
1531 default version of the symbol. */
1532 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1533 && sym.st_shndx != SHN_UNDEF)
1534 *p++ = ELF_VER_CHR;
1535 strcpy (p, verstr);
1536
1537 name = newname;
1538 }
1539 }
1540
1541 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1542 sym_hash, &override, &type_change_ok,
1543 &size_change_ok, dt_needed))
1544 goto error_return;
1545
1546 if (override)
1547 definition = false;
1548
1549 h = *sym_hash;
1550 while (h->root.type == bfd_link_hash_indirect
1551 || h->root.type == bfd_link_hash_warning)
1552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1553
1554 /* Remember the old alignment if this is a common symbol, so
1555 that we don't reduce the alignment later on. We can't
1556 check later, because _bfd_generic_link_add_one_symbol
1557 will set a default for the alignment which we want to
1558 override. */
1559 if (h->root.type == bfd_link_hash_common)
1560 old_alignment = h->root.u.c.p->alignment_power;
1561
1562 if (elf_tdata (abfd)->verdef != NULL
1563 && ! override
1564 && vernum > 1
1565 && definition)
1566 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1567 }
1568
1569 if (! (_bfd_generic_link_add_one_symbol
1570 (info, abfd, name, flags, sec, value, (const char *) NULL,
1571 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1572 goto error_return;
1573
1574 h = *sym_hash;
1575 while (h->root.type == bfd_link_hash_indirect
1576 || h->root.type == bfd_link_hash_warning)
1577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1578 *sym_hash = h;
1579
1580 new_weakdef = false;
1581 if (dynamic
1582 && definition
1583 && (flags & BSF_WEAK) != 0
1584 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1585 && info->hash->creator->flavour == bfd_target_elf_flavour
1586 && h->weakdef == NULL)
1587 {
1588 /* Keep a list of all weak defined non function symbols from
1589 a dynamic object, using the weakdef field. Later in this
1590 function we will set the weakdef field to the correct
1591 value. We only put non-function symbols from dynamic
1592 objects on this list, because that happens to be the only
1593 time we need to know the normal symbol corresponding to a
1594 weak symbol, and the information is time consuming to
1595 figure out. If the weakdef field is not already NULL,
1596 then this symbol was already defined by some previous
1597 dynamic object, and we will be using that previous
1598 definition anyhow. */
1599
1600 h->weakdef = weaks;
1601 weaks = h;
1602 new_weakdef = true;
1603 }
1604
1605 /* Set the alignment of a common symbol. */
1606 if (sym.st_shndx == SHN_COMMON
1607 && h->root.type == bfd_link_hash_common)
1608 {
1609 unsigned int align;
1610
1611 align = bfd_log2 (sym.st_value);
1612 if (align > old_alignment
1613 /* Permit an alignment power of zero if an alignment of one
1614 is specified and no other alignments have been specified. */
1615 || (sym.st_value == 1 && old_alignment == 0))
1616 h->root.u.c.p->alignment_power = align;
1617 }
1618
1619 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1620 {
1621 int old_flags;
1622 boolean dynsym;
1623 int new_flag;
1624
1625 /* Remember the symbol size and type. */
1626 if (sym.st_size != 0
1627 && (definition || h->size == 0))
1628 {
1629 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1630 (*_bfd_error_handler)
1631 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1632 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1633 bfd_get_filename (abfd));
1634
1635 h->size = sym.st_size;
1636 }
1637
1638 /* If this is a common symbol, then we always want H->SIZE
1639 to be the size of the common symbol. The code just above
1640 won't fix the size if a common symbol becomes larger. We
1641 don't warn about a size change here, because that is
1642 covered by --warn-common. */
1643 if (h->root.type == bfd_link_hash_common)
1644 h->size = h->root.u.c.size;
1645
1646 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1647 && (definition || h->type == STT_NOTYPE))
1648 {
1649 if (h->type != STT_NOTYPE
1650 && h->type != ELF_ST_TYPE (sym.st_info)
1651 && ! type_change_ok)
1652 (*_bfd_error_handler)
1653 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1654 name, h->type, ELF_ST_TYPE (sym.st_info),
1655 bfd_get_filename (abfd));
1656
1657 h->type = ELF_ST_TYPE (sym.st_info);
1658 }
1659
1660 /* If st_other has a processor-specific meaning, specific code
1661 might be needed here. */
1662 if (sym.st_other != 0)
1663 {
1664 /* Combine visibilities, using the most constraining one. */
1665 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1666 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1667
1668 if (symvis && (hvis > symvis || hvis == 0))
1669 h->other = sym.st_other;
1670
1671 /* If neither has visibility, use the st_other of the
1672 definition. This is an arbitrary choice, since the
1673 other bits have no general meaning. */
1674 if (!symvis && !hvis
1675 && (definition || h->other == 0))
1676 h->other = sym.st_other;
1677 }
1678
1679 /* Set a flag in the hash table entry indicating the type of
1680 reference or definition we just found. Keep a count of
1681 the number of dynamic symbols we find. A dynamic symbol
1682 is one which is referenced or defined by both a regular
1683 object and a shared object. */
1684 old_flags = h->elf_link_hash_flags;
1685 dynsym = false;
1686 if (! dynamic)
1687 {
1688 if (! definition)
1689 {
1690 new_flag = ELF_LINK_HASH_REF_REGULAR;
1691 if (bind != STB_WEAK)
1692 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1693 }
1694 else
1695 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1696 if (info->shared
1697 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1698 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1699 dynsym = true;
1700 }
1701 else
1702 {
1703 if (! definition)
1704 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1705 else
1706 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1707 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1708 | ELF_LINK_HASH_REF_REGULAR)) != 0
1709 || (h->weakdef != NULL
1710 && ! new_weakdef
1711 && h->weakdef->dynindx != -1))
1712 dynsym = true;
1713 }
1714
1715 h->elf_link_hash_flags |= new_flag;
1716
1717 /* If this symbol has a version, and it is the default
1718 version, we create an indirect symbol from the default
1719 name to the fully decorated name. This will cause
1720 external references which do not specify a version to be
1721 bound to this version of the symbol. */
1722 if (definition || h->root.type == bfd_link_hash_common)
1723 {
1724 char *p;
1725
1726 p = strchr (name, ELF_VER_CHR);
1727 if (p != NULL && p[1] == ELF_VER_CHR)
1728 {
1729 char *shortname;
1730 struct elf_link_hash_entry *hi;
1731 boolean override;
1732
1733 shortname = bfd_hash_allocate (&info->hash->table,
1734 p - name + 1);
1735 if (shortname == NULL)
1736 goto error_return;
1737 strncpy (shortname, name, p - name);
1738 shortname[p - name] = '\0';
1739
1740 /* We are going to create a new symbol. Merge it
1741 with any existing symbol with this name. For the
1742 purposes of the merge, act as though we were
1743 defining the symbol we just defined, although we
1744 actually going to define an indirect symbol. */
1745 type_change_ok = false;
1746 size_change_ok = false;
1747 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1748 &value, &hi, &override,
1749 &type_change_ok,
1750 &size_change_ok, dt_needed))
1751 goto error_return;
1752
1753 if (! override)
1754 {
1755 if (! (_bfd_generic_link_add_one_symbol
1756 (info, abfd, shortname, BSF_INDIRECT,
1757 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1758 collect, (struct bfd_link_hash_entry **) &hi)))
1759 goto error_return;
1760 }
1761 else
1762 {
1763 /* In this case the symbol named SHORTNAME is
1764 overriding the indirect symbol we want to
1765 add. We were planning on making SHORTNAME an
1766 indirect symbol referring to NAME. SHORTNAME
1767 is the name without a version. NAME is the
1768 fully versioned name, and it is the default
1769 version.
1770
1771 Overriding means that we already saw a
1772 definition for the symbol SHORTNAME in a
1773 regular object, and it is overriding the
1774 symbol defined in the dynamic object.
1775
1776 When this happens, we actually want to change
1777 NAME, the symbol we just added, to refer to
1778 SHORTNAME. This will cause references to
1779 NAME in the shared object to become
1780 references to SHORTNAME in the regular
1781 object. This is what we expect when we
1782 override a function in a shared object: that
1783 the references in the shared object will be
1784 mapped to the definition in the regular
1785 object. */
1786
1787 while (hi->root.type == bfd_link_hash_indirect
1788 || hi->root.type == bfd_link_hash_warning)
1789 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1790
1791 h->root.type = bfd_link_hash_indirect;
1792 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1793 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1794 {
1795 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1796 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1797 if (hi->elf_link_hash_flags
1798 & (ELF_LINK_HASH_REF_REGULAR
1799 | ELF_LINK_HASH_DEF_REGULAR))
1800 {
1801 if (! _bfd_elf_link_record_dynamic_symbol (info,
1802 hi))
1803 goto error_return;
1804 }
1805 }
1806
1807 /* Now set HI to H, so that the following code
1808 will set the other fields correctly. */
1809 hi = h;
1810 }
1811
1812 /* If there is a duplicate definition somewhere,
1813 then HI may not point to an indirect symbol. We
1814 will have reported an error to the user in that
1815 case. */
1816
1817 if (hi->root.type == bfd_link_hash_indirect)
1818 {
1819 struct elf_link_hash_entry *ht;
1820
1821 /* If the symbol became indirect, then we assume
1822 that we have not seen a definition before. */
1823 BFD_ASSERT ((hi->elf_link_hash_flags
1824 & (ELF_LINK_HASH_DEF_DYNAMIC
1825 | ELF_LINK_HASH_DEF_REGULAR))
1826 == 0);
1827
1828 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1829 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1830
1831 /* See if the new flags lead us to realize that
1832 the symbol must be dynamic. */
1833 if (! dynsym)
1834 {
1835 if (! dynamic)
1836 {
1837 if (info->shared
1838 || ((hi->elf_link_hash_flags
1839 & ELF_LINK_HASH_REF_DYNAMIC)
1840 != 0))
1841 dynsym = true;
1842 }
1843 else
1844 {
1845 if ((hi->elf_link_hash_flags
1846 & ELF_LINK_HASH_REF_REGULAR) != 0)
1847 dynsym = true;
1848 }
1849 }
1850 }
1851
1852 /* We also need to define an indirection from the
1853 nondefault version of the symbol. */
1854
1855 shortname = bfd_hash_allocate (&info->hash->table,
1856 strlen (name));
1857 if (shortname == NULL)
1858 goto error_return;
1859 strncpy (shortname, name, p - name);
1860 strcpy (shortname + (p - name), p + 1);
1861
1862 /* Once again, merge with any existing symbol. */
1863 type_change_ok = false;
1864 size_change_ok = false;
1865 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1866 &value, &hi, &override,
1867 &type_change_ok,
1868 &size_change_ok, dt_needed))
1869 goto error_return;
1870
1871 if (override)
1872 {
1873 /* Here SHORTNAME is a versioned name, so we
1874 don't expect to see the type of override we
1875 do in the case above. */
1876 (*_bfd_error_handler)
1877 (_("%s: warning: unexpected redefinition of `%s'"),
1878 bfd_get_filename (abfd), shortname);
1879 }
1880 else
1881 {
1882 if (! (_bfd_generic_link_add_one_symbol
1883 (info, abfd, shortname, BSF_INDIRECT,
1884 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1885 collect, (struct bfd_link_hash_entry **) &hi)))
1886 goto error_return;
1887
1888 /* If there is a duplicate definition somewhere,
1889 then HI may not point to an indirect symbol.
1890 We will have reported an error to the user in
1891 that case. */
1892
1893 if (hi->root.type == bfd_link_hash_indirect)
1894 {
1895 /* If the symbol became indirect, then we
1896 assume that we have not seen a definition
1897 before. */
1898 BFD_ASSERT ((hi->elf_link_hash_flags
1899 & (ELF_LINK_HASH_DEF_DYNAMIC
1900 | ELF_LINK_HASH_DEF_REGULAR))
1901 == 0);
1902
1903 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1904
1905 /* See if the new flags lead us to realize
1906 that the symbol must be dynamic. */
1907 if (! dynsym)
1908 {
1909 if (! dynamic)
1910 {
1911 if (info->shared
1912 || ((hi->elf_link_hash_flags
1913 & ELF_LINK_HASH_REF_DYNAMIC)
1914 != 0))
1915 dynsym = true;
1916 }
1917 else
1918 {
1919 if ((hi->elf_link_hash_flags
1920 & ELF_LINK_HASH_REF_REGULAR) != 0)
1921 dynsym = true;
1922 }
1923 }
1924 }
1925 }
1926 }
1927 }
1928
1929 if (dynsym && h->dynindx == -1)
1930 {
1931 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1932 goto error_return;
1933 if (h->weakdef != NULL
1934 && ! new_weakdef
1935 && h->weakdef->dynindx == -1)
1936 {
1937 if (! _bfd_elf_link_record_dynamic_symbol (info,
1938 h->weakdef))
1939 goto error_return;
1940 }
1941 }
1942 else if (dynsym && h->dynindx != -1)
1943 /* If the symbol already has a dynamic index, but
1944 visibility says it should not be visible, turn it into
1945 a local symbol. */
1946 switch (ELF_ST_VISIBILITY (h->other))
1947 {
1948 case STV_INTERNAL:
1949 case STV_HIDDEN:
1950 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1951 (*bed->elf_backend_hide_symbol) (info, h);
1952 break;
1953 }
1954
1955 if (dt_needed && definition
1956 && (h->elf_link_hash_flags
1957 & ELF_LINK_HASH_REF_REGULAR) != 0)
1958 {
1959 bfd_size_type oldsize;
1960 bfd_size_type strindex;
1961
1962 /* The symbol from a DT_NEEDED object is referenced from
1963 the regular object to create a dynamic executable. We
1964 have to make sure there is a DT_NEEDED entry for it. */
1965
1966 dt_needed = false;
1967 oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1968 strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
1969 elf_dt_soname (abfd),
1970 true, false);
1971 if (strindex == (bfd_size_type) -1)
1972 goto error_return;
1973
1974 if (oldsize
1975 == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
1976 {
1977 asection *sdyn;
1978 Elf_External_Dyn *dyncon, *dynconend;
1979
1980 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
1981 ".dynamic");
1982 BFD_ASSERT (sdyn != NULL);
1983
1984 dyncon = (Elf_External_Dyn *) sdyn->contents;
1985 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1986 sdyn->_raw_size);
1987 for (; dyncon < dynconend; dyncon++)
1988 {
1989 Elf_Internal_Dyn dyn;
1990
1991 elf_swap_dyn_in (elf_hash_table (info)->dynobj,
1992 dyncon, &dyn);
1993 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1994 dyn.d_un.d_val != strindex);
1995 }
1996 }
1997
1998 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
1999 goto error_return;
2000 }
2001 }
2002 }
2003
2004 /* Now set the weakdefs field correctly for all the weak defined
2005 symbols we found. The only way to do this is to search all the
2006 symbols. Since we only need the information for non functions in
2007 dynamic objects, that's the only time we actually put anything on
2008 the list WEAKS. We need this information so that if a regular
2009 object refers to a symbol defined weakly in a dynamic object, the
2010 real symbol in the dynamic object is also put in the dynamic
2011 symbols; we also must arrange for both symbols to point to the
2012 same memory location. We could handle the general case of symbol
2013 aliasing, but a general symbol alias can only be generated in
2014 assembler code, handling it correctly would be very time
2015 consuming, and other ELF linkers don't handle general aliasing
2016 either. */
2017 while (weaks != NULL)
2018 {
2019 struct elf_link_hash_entry *hlook;
2020 asection *slook;
2021 bfd_vma vlook;
2022 struct elf_link_hash_entry **hpp;
2023 struct elf_link_hash_entry **hppend;
2024
2025 hlook = weaks;
2026 weaks = hlook->weakdef;
2027 hlook->weakdef = NULL;
2028
2029 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2030 || hlook->root.type == bfd_link_hash_defweak
2031 || hlook->root.type == bfd_link_hash_common
2032 || hlook->root.type == bfd_link_hash_indirect);
2033 slook = hlook->root.u.def.section;
2034 vlook = hlook->root.u.def.value;
2035
2036 hpp = elf_sym_hashes (abfd);
2037 hppend = hpp + extsymcount;
2038 for (; hpp < hppend; hpp++)
2039 {
2040 struct elf_link_hash_entry *h;
2041
2042 h = *hpp;
2043 if (h != NULL && h != hlook
2044 && h->root.type == bfd_link_hash_defined
2045 && h->root.u.def.section == slook
2046 && h->root.u.def.value == vlook)
2047 {
2048 hlook->weakdef = h;
2049
2050 /* If the weak definition is in the list of dynamic
2051 symbols, make sure the real definition is put there
2052 as well. */
2053 if (hlook->dynindx != -1
2054 && h->dynindx == -1)
2055 {
2056 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2057 goto error_return;
2058 }
2059
2060 /* If the real definition is in the list of dynamic
2061 symbols, make sure the weak definition is put there
2062 as well. If we don't do this, then the dynamic
2063 loader might not merge the entries for the real
2064 definition and the weak definition. */
2065 if (h->dynindx != -1
2066 && hlook->dynindx == -1)
2067 {
2068 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2069 goto error_return;
2070 }
2071
2072 break;
2073 }
2074 }
2075 }
2076
2077 if (buf != NULL)
2078 {
2079 free (buf);
2080 buf = NULL;
2081 }
2082
2083 if (extversym != NULL)
2084 {
2085 free (extversym);
2086 extversym = NULL;
2087 }
2088
2089 /* If this object is the same format as the output object, and it is
2090 not a shared library, then let the backend look through the
2091 relocs.
2092
2093 This is required to build global offset table entries and to
2094 arrange for dynamic relocs. It is not required for the
2095 particular common case of linking non PIC code, even when linking
2096 against shared libraries, but unfortunately there is no way of
2097 knowing whether an object file has been compiled PIC or not.
2098 Looking through the relocs is not particularly time consuming.
2099 The problem is that we must either (1) keep the relocs in memory,
2100 which causes the linker to require additional runtime memory or
2101 (2) read the relocs twice from the input file, which wastes time.
2102 This would be a good case for using mmap.
2103
2104 I have no idea how to handle linking PIC code into a file of a
2105 different format. It probably can't be done. */
2106 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2107 if (! dynamic
2108 && abfd->xvec == info->hash->creator
2109 && check_relocs != NULL)
2110 {
2111 asection *o;
2112
2113 for (o = abfd->sections; o != NULL; o = o->next)
2114 {
2115 Elf_Internal_Rela *internal_relocs;
2116 boolean ok;
2117
2118 if ((o->flags & SEC_RELOC) == 0
2119 || o->reloc_count == 0
2120 || ((info->strip == strip_all || info->strip == strip_debugger)
2121 && (o->flags & SEC_DEBUGGING) != 0)
2122 || bfd_is_abs_section (o->output_section))
2123 continue;
2124
2125 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2126 (abfd, o, (PTR) NULL,
2127 (Elf_Internal_Rela *) NULL,
2128 info->keep_memory));
2129 if (internal_relocs == NULL)
2130 goto error_return;
2131
2132 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2133
2134 if (! info->keep_memory)
2135 free (internal_relocs);
2136
2137 if (! ok)
2138 goto error_return;
2139 }
2140 }
2141
2142 /* If this is a non-traditional, non-relocateable link, try to
2143 optimize the handling of the .stab/.stabstr sections. */
2144 if (! dynamic
2145 && ! info->relocateable
2146 && ! info->traditional_format
2147 && info->hash->creator->flavour == bfd_target_elf_flavour
2148 && (info->strip != strip_all && info->strip != strip_debugger))
2149 {
2150 asection *stab, *stabstr;
2151
2152 stab = bfd_get_section_by_name (abfd, ".stab");
2153 if (stab != NULL)
2154 {
2155 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2156
2157 if (stabstr != NULL)
2158 {
2159 struct bfd_elf_section_data *secdata;
2160
2161 secdata = elf_section_data (stab);
2162 if (! _bfd_link_section_stabs (abfd,
2163 &elf_hash_table (info)->stab_info,
2164 stab, stabstr,
2165 &secdata->stab_info))
2166 goto error_return;
2167 }
2168 }
2169 }
2170
2171 return true;
2172
2173 error_return:
2174 if (buf != NULL)
2175 free (buf);
2176 if (dynbuf != NULL)
2177 free (dynbuf);
2178 if (extversym != NULL)
2179 free (extversym);
2180 return false;
2181}
2182
2183/* Create some sections which will be filled in with dynamic linking
2184 information. ABFD is an input file which requires dynamic sections
2185 to be created. The dynamic sections take up virtual memory space
2186 when the final executable is run, so we need to create them before
2187 addresses are assigned to the output sections. We work out the
2188 actual contents and size of these sections later. */
2189
2190boolean
2191elf_link_create_dynamic_sections (abfd, info)
2192 bfd *abfd;
2193 struct bfd_link_info *info;
2194{
2195 flagword flags;
2196 register asection *s;
2197 struct elf_link_hash_entry *h;
2198 struct elf_backend_data *bed;
2199
2200 if (elf_hash_table (info)->dynamic_sections_created)
2201 return true;
2202
2203 /* Make sure that all dynamic sections use the same input BFD. */
2204 if (elf_hash_table (info)->dynobj == NULL)
2205 elf_hash_table (info)->dynobj = abfd;
2206 else
2207 abfd = elf_hash_table (info)->dynobj;
2208
2209 /* Note that we set the SEC_IN_MEMORY flag for all of these
2210 sections. */
2211 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2212 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2213
2214 /* A dynamically linked executable has a .interp section, but a
2215 shared library does not. */
2216 if (! info->shared)
2217 {
2218 s = bfd_make_section (abfd, ".interp");
2219 if (s == NULL
2220 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2221 return false;
2222 }
2223
2224 /* Create sections to hold version informations. These are removed
2225 if they are not needed. */
2226 s = bfd_make_section (abfd, ".gnu.version_d");
2227 if (s == NULL
2228 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2229 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2230 return false;
2231
2232 s = bfd_make_section (abfd, ".gnu.version");
2233 if (s == NULL
2234 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2235 || ! bfd_set_section_alignment (abfd, s, 1))
2236 return false;
2237
2238 s = bfd_make_section (abfd, ".gnu.version_r");
2239 if (s == NULL
2240 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2241 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2242 return false;
2243
2244 s = bfd_make_section (abfd, ".dynsym");
2245 if (s == NULL
2246 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2247 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2248 return false;
2249
2250 s = bfd_make_section (abfd, ".dynstr");
2251 if (s == NULL
2252 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2253 return false;
2254
2255 /* Create a strtab to hold the dynamic symbol names. */
2256 if (elf_hash_table (info)->dynstr == NULL)
2257 {
2258 elf_hash_table (info)->dynstr = elf_stringtab_init ();
2259 if (elf_hash_table (info)->dynstr == NULL)
2260 return false;
2261 }
2262
2263 s = bfd_make_section (abfd, ".dynamic");
2264 if (s == NULL
2265 || ! bfd_set_section_flags (abfd, s, flags)
2266 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2267 return false;
2268
2269 /* The special symbol _DYNAMIC is always set to the start of the
2270 .dynamic section. This call occurs before we have processed the
2271 symbols for any dynamic object, so we don't have to worry about
2272 overriding a dynamic definition. We could set _DYNAMIC in a
2273 linker script, but we only want to define it if we are, in fact,
2274 creating a .dynamic section. We don't want to define it if there
2275 is no .dynamic section, since on some ELF platforms the start up
2276 code examines it to decide how to initialize the process. */
2277 h = NULL;
2278 if (! (_bfd_generic_link_add_one_symbol
2279 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2280 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2281 (struct bfd_link_hash_entry **) &h)))
2282 return false;
2283 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2284 h->type = STT_OBJECT;
2285
2286 if (info->shared
2287 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2288 return false;
2289
2290 bed = get_elf_backend_data (abfd);
2291
2292 s = bfd_make_section (abfd, ".hash");
2293 if (s == NULL
2294 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2295 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2296 return false;
2297 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2298
2299 /* Let the backend create the rest of the sections. This lets the
2300 backend set the right flags. The backend will normally create
2301 the .got and .plt sections. */
2302 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2303 return false;
2304
2305 elf_hash_table (info)->dynamic_sections_created = true;
2306
2307 return true;
2308}
2309
2310/* Add an entry to the .dynamic table. */
2311
2312boolean
2313elf_add_dynamic_entry (info, tag, val)
2314 struct bfd_link_info *info;
2315 bfd_vma tag;
2316 bfd_vma val;
2317{
2318 Elf_Internal_Dyn dyn;
2319 bfd *dynobj;
2320 asection *s;
2321 size_t newsize;
2322 bfd_byte *newcontents;
2323
2324 dynobj = elf_hash_table (info)->dynobj;
2325
2326 s = bfd_get_section_by_name (dynobj, ".dynamic");
2327 BFD_ASSERT (s != NULL);
2328
2329 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2330 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2331 if (newcontents == NULL)
2332 return false;
2333
2334 dyn.d_tag = tag;
2335 dyn.d_un.d_val = val;
2336 elf_swap_dyn_out (dynobj, &dyn,
2337 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2338
2339 s->_raw_size = newsize;
2340 s->contents = newcontents;
2341
2342 return true;
2343}
2344
2345/* Record a new local dynamic symbol. */
2346
2347boolean
2348elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2349 struct bfd_link_info *info;
2350 bfd *input_bfd;
2351 long input_indx;
2352{
2353 struct elf_link_local_dynamic_entry *entry;
2354 struct elf_link_hash_table *eht;
2355 struct bfd_strtab_hash *dynstr;
2356 Elf_External_Sym esym;
2357 unsigned long dynstr_index;
2358 char *name;
2359
2360 /* See if the entry exists already. */
2361 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2362 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2363 return true;
2364
2365 entry = (struct elf_link_local_dynamic_entry *)
2366 bfd_alloc (input_bfd, sizeof (*entry));
2367 if (entry == NULL)
2368 return false;
2369
2370 /* Go find the symbol, so that we can find it's name. */
2371 if (bfd_seek (input_bfd,
2372 (elf_tdata (input_bfd)->symtab_hdr.sh_offset
2373 + input_indx * sizeof (Elf_External_Sym)),
2374 SEEK_SET) != 0
2375 || (bfd_read (&esym, sizeof (Elf_External_Sym), 1, input_bfd)
2376 != sizeof (Elf_External_Sym)))
2377 return false;
2378 elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
2379
2380 name = (bfd_elf_string_from_elf_section
2381 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2382 entry->isym.st_name));
2383
2384 dynstr = elf_hash_table (info)->dynstr;
2385 if (dynstr == NULL)
2386 {
2387 /* Create a strtab to hold the dynamic symbol names. */
2388 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
2389 if (dynstr == NULL)
2390 return false;
2391 }
2392
2393 dynstr_index = _bfd_stringtab_add (dynstr, name, true, false);
2394 if (dynstr_index == (unsigned long) -1)
2395 return false;
2396 entry->isym.st_name = dynstr_index;
2397
2398 eht = elf_hash_table (info);
2399
2400 entry->next = eht->dynlocal;
2401 eht->dynlocal = entry;
2402 entry->input_bfd = input_bfd;
2403 entry->input_indx = input_indx;
2404 eht->dynsymcount++;
2405
2406 /* Whatever binding the symbol had before, it's now local. */
2407 entry->isym.st_info
2408 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2409
2410 /* The dynindx will be set at the end of size_dynamic_sections. */
2411
2412 return true;
2413}
2414
2415
2416/* Read and swap the relocs from the section indicated by SHDR. This
2417 may be either a REL or a RELA section. The relocations are
2418 translated into RELA relocations and stored in INTERNAL_RELOCS,
2419 which should have already been allocated to contain enough space.
2420 The EXTERNAL_RELOCS are a buffer where the external form of the
2421 relocations should be stored.
2422
2423 Returns false if something goes wrong. */
2424
2425static boolean
2426elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2427 internal_relocs)
2428 bfd *abfd;
2429 Elf_Internal_Shdr *shdr;
2430 PTR external_relocs;
2431 Elf_Internal_Rela *internal_relocs;
2432{
2433 struct elf_backend_data *bed;
2434
2435 /* If there aren't any relocations, that's OK. */
2436 if (!shdr)
2437 return true;
2438
2439 /* Position ourselves at the start of the section. */
2440 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2441 return false;
2442
2443 /* Read the relocations. */
2444 if (bfd_read (external_relocs, 1, shdr->sh_size, abfd)
2445 != shdr->sh_size)
2446 return false;
2447
2448 bed = get_elf_backend_data (abfd);
2449
2450 /* Convert the external relocations to the internal format. */
2451 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2452 {
2453 Elf_External_Rel *erel;
2454 Elf_External_Rel *erelend;
2455 Elf_Internal_Rela *irela;
2456 Elf_Internal_Rel *irel;
2457
2458 erel = (Elf_External_Rel *) external_relocs;
2459 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2460 irela = internal_relocs;
2461 irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
2462 * sizeof (Elf_Internal_Rel)));
2463 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2464 {
2465 unsigned int i;
2466
2467 if (bed->s->swap_reloc_in)
2468 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2469 else
2470 elf_swap_reloc_in (abfd, erel, irel);
2471
2472 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2473 {
2474 irela[i].r_offset = irel[i].r_offset;
2475 irela[i].r_info = irel[i].r_info;
2476 irela[i].r_addend = 0;
2477 }
2478 }
2479 }
2480 else
2481 {
2482 Elf_External_Rela *erela;
2483 Elf_External_Rela *erelaend;
2484 Elf_Internal_Rela *irela;
2485
2486 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2487
2488 erela = (Elf_External_Rela *) external_relocs;
2489 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2490 irela = internal_relocs;
2491 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2492 {
2493 if (bed->s->swap_reloca_in)
2494 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2495 else
2496 elf_swap_reloca_in (abfd, erela, irela);
2497 }
2498 }
2499
2500 return true;
2501}
2502
2503/* Read and swap the relocs for a section O. They may have been
2504 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2505 not NULL, they are used as buffers to read into. They are known to
2506 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2507 the return value is allocated using either malloc or bfd_alloc,
2508 according to the KEEP_MEMORY argument. If O has two relocation
2509 sections (both REL and RELA relocations), then the REL_HDR
2510 relocations will appear first in INTERNAL_RELOCS, followed by the
2511 REL_HDR2 relocations. */
2512
2513Elf_Internal_Rela *
2514NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2515 keep_memory)
2516 bfd *abfd;
2517 asection *o;
2518 PTR external_relocs;
2519 Elf_Internal_Rela *internal_relocs;
2520 boolean keep_memory;
2521{
2522 Elf_Internal_Shdr *rel_hdr;
2523 PTR alloc1 = NULL;
2524 Elf_Internal_Rela *alloc2 = NULL;
2525 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2526
2527 if (elf_section_data (o)->relocs != NULL)
2528 return elf_section_data (o)->relocs;
2529
2530 if (o->reloc_count == 0)
2531 return NULL;
2532
2533 rel_hdr = &elf_section_data (o)->rel_hdr;
2534
2535 if (internal_relocs == NULL)
2536 {
2537 size_t size;
2538
2539 size = (o->reloc_count * bed->s->int_rels_per_ext_rel
2540 * sizeof (Elf_Internal_Rela));
2541 if (keep_memory)
2542 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2543 else
2544 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2545 if (internal_relocs == NULL)
2546 goto error_return;
2547 }
2548
2549 if (external_relocs == NULL)
2550 {
2551 size_t size = (size_t) rel_hdr->sh_size;
2552
2553 if (elf_section_data (o)->rel_hdr2)
2554 size += (size_t) elf_section_data (o)->rel_hdr2->sh_size;
2555 alloc1 = (PTR) bfd_malloc (size);
2556 if (alloc1 == NULL)
2557 goto error_return;
2558 external_relocs = alloc1;
2559 }
2560
2561 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2562 external_relocs,
2563 internal_relocs))
2564 goto error_return;
2565 if (!elf_link_read_relocs_from_section
2566 (abfd,
2567 elf_section_data (o)->rel_hdr2,
2568 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2569 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2570 * bed->s->int_rels_per_ext_rel)))
2571 goto error_return;
2572
2573 /* Cache the results for next time, if we can. */
2574 if (keep_memory)
2575 elf_section_data (o)->relocs = internal_relocs;
2576
2577 if (alloc1 != NULL)
2578 free (alloc1);
2579
2580 /* Don't free alloc2, since if it was allocated we are passing it
2581 back (under the name of internal_relocs). */
2582
2583 return internal_relocs;
2584
2585 error_return:
2586 if (alloc1 != NULL)
2587 free (alloc1);
2588 if (alloc2 != NULL)
2589 free (alloc2);
2590 return NULL;
2591}
2592
2593
2594/* Record an assignment to a symbol made by a linker script. We need
2595 this in case some dynamic object refers to this symbol. */
2596
2597/*ARGSUSED*/
2598boolean
2599NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2600 bfd *output_bfd ATTRIBUTE_UNUSED;
2601 struct bfd_link_info *info;
2602 const char *name;
2603 boolean provide;
2604{
2605 struct elf_link_hash_entry *h;
2606
2607 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2608 return true;
2609
2610 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2611 if (h == NULL)
2612 return false;
2613
2614 if (h->root.type == bfd_link_hash_new)
2615 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
2616
2617 /* If this symbol is being provided by the linker script, and it is
2618 currently defined by a dynamic object, but not by a regular
2619 object, then mark it as undefined so that the generic linker will
2620 force the correct value. */
2621 if (provide
2622 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2623 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2624 h->root.type = bfd_link_hash_undefined;
2625
2626 /* If this symbol is not being provided by the linker script, and it is
2627 currently defined by a dynamic object, but not by a regular object,
2628 then clear out any version information because the symbol will not be
2629 associated with the dynamic object any more. */
2630 if (!provide
2631 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2632 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2633 h->verinfo.verdef = NULL;
2634
2635 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2636
2637 /* When possible, keep the original type of the symbol */
2638 if (h->type == STT_NOTYPE)
2639 h->type = STT_OBJECT;
2640
2641 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2642 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2643 || info->shared)
2644 && h->dynindx == -1)
2645 {
2646 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2647 return false;
2648
2649 /* If this is a weak defined symbol, and we know a corresponding
2650 real symbol from the same dynamic object, make sure the real
2651 symbol is also made into a dynamic symbol. */
2652 if (h->weakdef != NULL
2653 && h->weakdef->dynindx == -1)
2654 {
2655 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2656 return false;
2657 }
2658 }
2659
2660 return true;
2661}
2662
2663
2664/* This structure is used to pass information to
2665 elf_link_assign_sym_version. */
2666
2667struct elf_assign_sym_version_info
2668{
2669 /* Output BFD. */
2670 bfd *output_bfd;
2671 /* General link information. */
2672 struct bfd_link_info *info;
2673 /* Version tree. */
2674 struct bfd_elf_version_tree *verdefs;
2675 /* Whether we are exporting all dynamic symbols. */
2676 boolean export_dynamic;
2677 /* Whether we had a failure. */
2678 boolean failed;
2679};
2680
2681/* This structure is used to pass information to
2682 elf_link_find_version_dependencies. */
2683
2684struct elf_find_verdep_info
2685{
2686 /* Output BFD. */
2687 bfd *output_bfd;
2688 /* General link information. */
2689 struct bfd_link_info *info;
2690 /* The number of dependencies. */
2691 unsigned int vers;
2692 /* Whether we had a failure. */
2693 boolean failed;
2694};
2695
2696/* Array used to determine the number of hash table buckets to use
2697 based on the number of symbols there are. If there are fewer than
2698 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2699 fewer than 37 we use 17 buckets, and so forth. We never use more
2700 than 32771 buckets. */
2701
2702static const size_t elf_buckets[] =
2703{
2704 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2705 16411, 32771, 0
2706};
2707
2708/* Compute bucket count for hashing table. We do not use a static set
2709 of possible tables sizes anymore. Instead we determine for all
2710 possible reasonable sizes of the table the outcome (i.e., the
2711 number of collisions etc) and choose the best solution. The
2712 weighting functions are not too simple to allow the table to grow
2713 without bounds. Instead one of the weighting factors is the size.
2714 Therefore the result is always a good payoff between few collisions
2715 (= short chain lengths) and table size. */
2716static size_t
2717compute_bucket_count (info)
2718 struct bfd_link_info *info;
2719{
2720 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2721 size_t best_size = 0;
2722 unsigned long int *hashcodes;
2723 unsigned long int *hashcodesp;
2724 unsigned long int i;
2725
2726 /* Compute the hash values for all exported symbols. At the same
2727 time store the values in an array so that we could use them for
2728 optimizations. */
2729 hashcodes = (unsigned long int *) bfd_malloc (dynsymcount
2730 * sizeof (unsigned long int));
2731 if (hashcodes == NULL)
2732 return 0;
2733 hashcodesp = hashcodes;
2734
2735 /* Put all hash values in HASHCODES. */
2736 elf_link_hash_traverse (elf_hash_table (info),
2737 elf_collect_hash_codes, &hashcodesp);
2738
2739/* We have a problem here. The following code to optimize the table
2740 size requires an integer type with more the 32 bits. If
2741 BFD_HOST_U_64_BIT is set we know about such a type. */
2742#ifdef BFD_HOST_U_64_BIT
2743 if (info->optimize == true)
2744 {
2745 unsigned long int nsyms = hashcodesp - hashcodes;
2746 size_t minsize;
2747 size_t maxsize;
2748 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2749 unsigned long int *counts ;
2750
2751 /* Possible optimization parameters: if we have NSYMS symbols we say
2752 that the hashing table must at least have NSYMS/4 and at most
2753 2*NSYMS buckets. */
2754 minsize = nsyms / 4;
2755 if (minsize == 0)
2756 minsize = 1;
2757 best_size = maxsize = nsyms * 2;
2758
2759 /* Create array where we count the collisions in. We must use bfd_malloc
2760 since the size could be large. */
2761 counts = (unsigned long int *) bfd_malloc (maxsize
2762 * sizeof (unsigned long int));
2763 if (counts == NULL)
2764 {
2765 free (hashcodes);
2766 return 0;
2767 }
2768
2769 /* Compute the "optimal" size for the hash table. The criteria is a
2770 minimal chain length. The minor criteria is (of course) the size
2771 of the table. */
2772 for (i = minsize; i < maxsize; ++i)
2773 {
2774 /* Walk through the array of hashcodes and count the collisions. */
2775 BFD_HOST_U_64_BIT max;
2776 unsigned long int j;
2777 unsigned long int fact;
2778
2779 memset (counts, '\0', i * sizeof (unsigned long int));
2780
2781 /* Determine how often each hash bucket is used. */
2782 for (j = 0; j < nsyms; ++j)
2783 ++counts[hashcodes[j] % i];
2784
2785 /* For the weight function we need some information about the
2786 pagesize on the target. This is information need not be 100%
2787 accurate. Since this information is not available (so far) we
2788 define it here to a reasonable default value. If it is crucial
2789 to have a better value some day simply define this value. */
2790# ifndef BFD_TARGET_PAGESIZE
2791# define BFD_TARGET_PAGESIZE (4096)
2792# endif
2793
2794 /* We in any case need 2 + NSYMS entries for the size values and
2795 the chains. */
2796 max = (2 + nsyms) * (ARCH_SIZE / 8);
2797
2798# if 1
2799 /* Variant 1: optimize for short chains. We add the squares
2800 of all the chain lengths (which favous many small chain
2801 over a few long chains). */
2802 for (j = 0; j < i; ++j)
2803 max += counts[j] * counts[j];
2804
2805 /* This adds penalties for the overall size of the table. */
2806 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2807 max *= fact * fact;
2808# else
2809 /* Variant 2: Optimize a lot more for small table. Here we
2810 also add squares of the size but we also add penalties for
2811 empty slots (the +1 term). */
2812 for (j = 0; j < i; ++j)
2813 max += (1 + counts[j]) * (1 + counts[j]);
2814
2815 /* The overall size of the table is considered, but not as
2816 strong as in variant 1, where it is squared. */
2817 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2818 max *= fact;
2819# endif
2820
2821 /* Compare with current best results. */
2822 if (max < best_chlen)
2823 {
2824 best_chlen = max;
2825 best_size = i;
2826 }
2827 }
2828
2829 free (counts);
2830 }
2831 else
2832#endif /* defined (BFD_HOST_U_64_BIT) */
2833 {
2834 /* This is the fallback solution if no 64bit type is available or if we
2835 are not supposed to spend much time on optimizations. We select the
2836 bucket count using a fixed set of numbers. */
2837 for (i = 0; elf_buckets[i] != 0; i++)
2838 {
2839 best_size = elf_buckets[i];
2840 if (dynsymcount < elf_buckets[i + 1])
2841 break;
2842 }
2843 }
2844
2845 /* Free the arrays we needed. */
2846 free (hashcodes);
2847
2848 return best_size;
2849}
2850
2851/* Set up the sizes and contents of the ELF dynamic sections. This is
2852 called by the ELF linker emulation before_allocation routine. We
2853 must set the sizes of the sections before the linker sets the
2854 addresses of the various sections. */
2855
2856boolean
2857NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2858 export_dynamic, filter_shlib,
2859 auxiliary_filters, info, sinterpptr,
2860 verdefs)
2861 bfd *output_bfd;
2862 const char *soname;
2863 const char *rpath;
2864 boolean export_dynamic;
2865 const char *filter_shlib;
2866 const char * const *auxiliary_filters;
2867 struct bfd_link_info *info;
2868 asection **sinterpptr;
2869 struct bfd_elf_version_tree *verdefs;
2870{
2871 bfd_size_type soname_indx;
2872 bfd *dynobj;
2873 struct elf_backend_data *bed;
2874 struct elf_assign_sym_version_info asvinfo;
2875
2876 *sinterpptr = NULL;
2877
2878 soname_indx = (bfd_size_type) -1;
2879
2880 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2881 return true;
2882
2883 /* The backend may have to create some sections regardless of whether
2884 we're dynamic or not. */
2885 bed = get_elf_backend_data (output_bfd);
2886 if (bed->elf_backend_always_size_sections
2887 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2888 return false;
2889
2890 dynobj = elf_hash_table (info)->dynobj;
2891
2892 /* If there were no dynamic objects in the link, there is nothing to
2893 do here. */
2894 if (dynobj == NULL)
2895 return true;
2896
2897 if (elf_hash_table (info)->dynamic_sections_created)
2898 {
2899 struct elf_info_failed eif;
2900 struct elf_link_hash_entry *h;
2901 asection *dynstr;
2902
2903 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2904 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2905
2906 if (soname != NULL)
2907 {
2908 soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2909 soname, true, true);
2910 if (soname_indx == (bfd_size_type) -1
2911 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
2912 return false;
2913 }
2914
2915 if (info->symbolic)
2916 {
2917 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
2918 return false;
2919 info->flags |= DF_SYMBOLIC;
2920 }
2921
2922 if (rpath != NULL)
2923 {
2924 bfd_size_type indx;
2925
2926 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
2927 true, true);
2928 if (indx == (bfd_size_type) -1
2929 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
2930 || (info->new_dtags
2931 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
2932 return false;
2933 }
2934
2935 if (filter_shlib != NULL)
2936 {
2937 bfd_size_type indx;
2938
2939 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2940 filter_shlib, true, true);
2941 if (indx == (bfd_size_type) -1
2942 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
2943 return false;
2944 }
2945
2946 if (auxiliary_filters != NULL)
2947 {
2948 const char * const *p;
2949
2950 for (p = auxiliary_filters; *p != NULL; p++)
2951 {
2952 bfd_size_type indx;
2953
2954 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2955 *p, true, true);
2956 if (indx == (bfd_size_type) -1
2957 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
2958 return false;
2959 }
2960 }
2961
2962 eif.info = info;
2963 eif.failed = false;
2964
2965 /* If we are supposed to export all symbols into the dynamic symbol
2966 table (this is not the normal case), then do so. */
2967 if (export_dynamic)
2968 {
2969 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
2970 (PTR) &eif);
2971 if (eif.failed)
2972 return false;
2973 }
2974
2975 /* Attach all the symbols to their version information. */
2976 asvinfo.output_bfd = output_bfd;
2977 asvinfo.info = info;
2978 asvinfo.verdefs = verdefs;
2979 asvinfo.export_dynamic = export_dynamic;
2980 asvinfo.failed = false;
2981
2982 elf_link_hash_traverse (elf_hash_table (info),
2983 elf_link_assign_sym_version,
2984 (PTR) &asvinfo);
2985 if (asvinfo.failed)
2986 return false;
2987
2988 /* Find all symbols which were defined in a dynamic object and make
2989 the backend pick a reasonable value for them. */
2990 elf_link_hash_traverse (elf_hash_table (info),
2991 elf_adjust_dynamic_symbol,
2992 (PTR) &eif);
2993 if (eif.failed)
2994 return false;
2995
2996 /* Add some entries to the .dynamic section. We fill in some of the
2997 values later, in elf_bfd_final_link, but we must add the entries
2998 now so that we know the final size of the .dynamic section. */
2999
3000 /* If there are initialization and/or finalization functions to
3001 call then add the corresponding DT_INIT/DT_FINI entries. */
3002 h = (info->init_function
3003 ? elf_link_hash_lookup (elf_hash_table (info),
3004 info->init_function, false,
3005 false, false)
3006 : NULL);
3007 if (h != NULL
3008 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3009 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3010 {
3011 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
3012 return false;
3013 }
3014 h = (info->fini_function
3015 ? elf_link_hash_lookup (elf_hash_table (info),
3016 info->fini_function, false,
3017 false, false)
3018 : NULL);
3019 if (h != NULL
3020 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3021 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3022 {
3023 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
3024 return false;
3025 }
3026
3027 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3028 /* If .dynstr is excluded from the link, we don't want any of
3029 these tags. Strictly, we should be checking each section
3030 individually; This quick check covers for the case where
3031 someone does a /DISCARD/ : { *(*) }. */
3032 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3033 {
3034 bfd_size_type strsize;
3035
3036 strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
3037 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
3038 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
3039 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
3040 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
3041 || ! elf_add_dynamic_entry (info, DT_SYMENT,
3042 sizeof (Elf_External_Sym)))
3043 return false;
3044 }
3045 }
3046
3047 /* The backend must work out the sizes of all the other dynamic
3048 sections. */
3049 if (bed->elf_backend_size_dynamic_sections
3050 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3051 return false;
3052
3053 if (elf_hash_table (info)->dynamic_sections_created)
3054 {
3055 size_t dynsymcount;
3056 asection *s;
3057 size_t bucketcount = 0;
3058 size_t hash_entry_size;
3059
3060 /* Set up the version definition section. */
3061 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3062 BFD_ASSERT (s != NULL);
3063
3064 /* We may have created additional version definitions if we are
3065 just linking a regular application. */
3066 verdefs = asvinfo.verdefs;
3067
3068 if (verdefs == NULL)
3069 _bfd_strip_section_from_output (info, s);
3070 else
3071 {
3072 unsigned int cdefs;
3073 bfd_size_type size;
3074 struct bfd_elf_version_tree *t;
3075 bfd_byte *p;
3076 Elf_Internal_Verdef def;
3077 Elf_Internal_Verdaux defaux;
3078
3079 cdefs = 0;
3080 size = 0;
3081
3082 /* Make space for the base version. */
3083 size += sizeof (Elf_External_Verdef);
3084 size += sizeof (Elf_External_Verdaux);
3085 ++cdefs;
3086
3087 for (t = verdefs; t != NULL; t = t->next)
3088 {
3089 struct bfd_elf_version_deps *n;
3090
3091 size += sizeof (Elf_External_Verdef);
3092 size += sizeof (Elf_External_Verdaux);
3093 ++cdefs;
3094
3095 for (n = t->deps; n != NULL; n = n->next)
3096 size += sizeof (Elf_External_Verdaux);
3097 }
3098
3099 s->_raw_size = size;
3100 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3101 if (s->contents == NULL && s->_raw_size != 0)
3102 return false;
3103
3104 /* Fill in the version definition section. */
3105
3106 p = s->contents;
3107
3108 def.vd_version = VER_DEF_CURRENT;
3109 def.vd_flags = VER_FLG_BASE;
3110 def.vd_ndx = 1;
3111 def.vd_cnt = 1;
3112 def.vd_aux = sizeof (Elf_External_Verdef);
3113 def.vd_next = (sizeof (Elf_External_Verdef)
3114 + sizeof (Elf_External_Verdaux));
3115
3116 if (soname_indx != (bfd_size_type) -1)
3117 {
3118 def.vd_hash = bfd_elf_hash (soname);
3119 defaux.vda_name = soname_indx;
3120 }
3121 else
3122 {
3123 const char *name;
3124 bfd_size_type indx;
3125
3126 name = output_bfd->filename;
3127 def.vd_hash = bfd_elf_hash (name);
3128 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3129 name, true, false);
3130 if (indx == (bfd_size_type) -1)
3131 return false;
3132 defaux.vda_name = indx;
3133 }
3134 defaux.vda_next = 0;
3135
3136 _bfd_elf_swap_verdef_out (output_bfd, &def,
3137 (Elf_External_Verdef *)p);
3138 p += sizeof (Elf_External_Verdef);
3139 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3140 (Elf_External_Verdaux *) p);
3141 p += sizeof (Elf_External_Verdaux);
3142
3143 for (t = verdefs; t != NULL; t = t->next)
3144 {
3145 unsigned int cdeps;
3146 struct bfd_elf_version_deps *n;
3147 struct elf_link_hash_entry *h;
3148
3149 cdeps = 0;
3150 for (n = t->deps; n != NULL; n = n->next)
3151 ++cdeps;
3152
3153 /* Add a symbol representing this version. */
3154 h = NULL;
3155 if (! (_bfd_generic_link_add_one_symbol
3156 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3157 (bfd_vma) 0, (const char *) NULL, false,
3158 get_elf_backend_data (dynobj)->collect,
3159 (struct bfd_link_hash_entry **) &h)))
3160 return false;
3161 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3162 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3163 h->type = STT_OBJECT;
3164 h->verinfo.vertree = t;
3165
3166 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3167 return false;
3168
3169 def.vd_version = VER_DEF_CURRENT;
3170 def.vd_flags = 0;
3171 if (t->globals == NULL && t->locals == NULL && ! t->used)
3172 def.vd_flags |= VER_FLG_WEAK;
3173 def.vd_ndx = t->vernum + 1;
3174 def.vd_cnt = cdeps + 1;
3175 def.vd_hash = bfd_elf_hash (t->name);
3176 def.vd_aux = sizeof (Elf_External_Verdef);
3177 if (t->next != NULL)
3178 def.vd_next = (sizeof (Elf_External_Verdef)
3179 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3180 else
3181 def.vd_next = 0;
3182
3183 _bfd_elf_swap_verdef_out (output_bfd, &def,
3184 (Elf_External_Verdef *) p);
3185 p += sizeof (Elf_External_Verdef);
3186
3187 defaux.vda_name = h->dynstr_index;
3188 if (t->deps == NULL)
3189 defaux.vda_next = 0;
3190 else
3191 defaux.vda_next = sizeof (Elf_External_Verdaux);
3192 t->name_indx = defaux.vda_name;
3193
3194 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3195 (Elf_External_Verdaux *) p);
3196 p += sizeof (Elf_External_Verdaux);
3197
3198 for (n = t->deps; n != NULL; n = n->next)
3199 {
3200 if (n->version_needed == NULL)
3201 {
3202 /* This can happen if there was an error in the
3203 version script. */
3204 defaux.vda_name = 0;
3205 }
3206 else
3207 defaux.vda_name = n->version_needed->name_indx;
3208 if (n->next == NULL)
3209 defaux.vda_next = 0;
3210 else
3211 defaux.vda_next = sizeof (Elf_External_Verdaux);
3212
3213 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3214 (Elf_External_Verdaux *) p);
3215 p += sizeof (Elf_External_Verdaux);
3216 }
3217 }
3218
3219 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
3220 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
3221 return false;
3222
3223 elf_tdata (output_bfd)->cverdefs = cdefs;
3224 }
3225
3226 if (info->new_dtags && info->flags)
3227 {
3228 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
3229 return false;
3230 }
3231
3232 if (info->flags_1)
3233 {
3234 if (! info->shared)
3235 info->flags_1 &= ~ (DF_1_INITFIRST
3236 | DF_1_NODELETE
3237 | DF_1_NOOPEN);
3238 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
3239 return false;
3240 }
3241
3242 /* Work out the size of the version reference section. */
3243
3244 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3245 BFD_ASSERT (s != NULL);
3246 {
3247 struct elf_find_verdep_info sinfo;
3248
3249 sinfo.output_bfd = output_bfd;
3250 sinfo.info = info;
3251 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3252 if (sinfo.vers == 0)
3253 sinfo.vers = 1;
3254 sinfo.failed = false;
3255
3256 elf_link_hash_traverse (elf_hash_table (info),
3257 elf_link_find_version_dependencies,
3258 (PTR) &sinfo);
3259
3260 if (elf_tdata (output_bfd)->verref == NULL)
3261 _bfd_strip_section_from_output (info, s);
3262 else
3263 {
3264 Elf_Internal_Verneed *t;
3265 unsigned int size;
3266 unsigned int crefs;
3267 bfd_byte *p;
3268
3269 /* Build the version definition section. */
3270 size = 0;
3271 crefs = 0;
3272 for (t = elf_tdata (output_bfd)->verref;
3273 t != NULL;
3274 t = t->vn_nextref)
3275 {
3276 Elf_Internal_Vernaux *a;
3277
3278 size += sizeof (Elf_External_Verneed);
3279 ++crefs;
3280 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3281 size += sizeof (Elf_External_Vernaux);
3282 }
3283
3284 s->_raw_size = size;
3285 s->contents = (bfd_byte *) bfd_alloc (output_bfd, size);
3286 if (s->contents == NULL)
3287 return false;
3288
3289 p = s->contents;
3290 for (t = elf_tdata (output_bfd)->verref;
3291 t != NULL;
3292 t = t->vn_nextref)
3293 {
3294 unsigned int caux;
3295 Elf_Internal_Vernaux *a;
3296 bfd_size_type indx;
3297
3298 caux = 0;
3299 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3300 ++caux;
3301
3302 t->vn_version = VER_NEED_CURRENT;
3303 t->vn_cnt = caux;
3304 if (elf_dt_name (t->vn_bfd) != NULL)
3305 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3306 elf_dt_name (t->vn_bfd),
3307 true, false);
3308 else
3309 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3310 t->vn_bfd->filename, true, false);
3311 if (indx == (bfd_size_type) -1)
3312 return false;
3313 t->vn_file = indx;
3314 t->vn_aux = sizeof (Elf_External_Verneed);
3315 if (t->vn_nextref == NULL)
3316 t->vn_next = 0;
3317 else
3318 t->vn_next = (sizeof (Elf_External_Verneed)
3319 + caux * sizeof (Elf_External_Vernaux));
3320
3321 _bfd_elf_swap_verneed_out (output_bfd, t,
3322 (Elf_External_Verneed *) p);
3323 p += sizeof (Elf_External_Verneed);
3324
3325 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3326 {
3327 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3328 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
3329 a->vna_nodename, true, false);
3330 if (indx == (bfd_size_type) -1)
3331 return false;
3332 a->vna_name = indx;
3333 if (a->vna_nextptr == NULL)
3334 a->vna_next = 0;
3335 else
3336 a->vna_next = sizeof (Elf_External_Vernaux);
3337
3338 _bfd_elf_swap_vernaux_out (output_bfd, a,
3339 (Elf_External_Vernaux *) p);
3340 p += sizeof (Elf_External_Vernaux);
3341 }
3342 }
3343
3344 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
3345 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
3346 return false;
3347
3348 elf_tdata (output_bfd)->cverrefs = crefs;
3349 }
3350 }
3351
3352 /* Assign dynsym indicies. In a shared library we generate a
3353 section symbol for each output section, which come first.
3354 Next come all of the back-end allocated local dynamic syms,
3355 followed by the rest of the global symbols. */
3356
3357 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3358
3359 /* Work out the size of the symbol version section. */
3360 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3361 BFD_ASSERT (s != NULL);
3362 if (dynsymcount == 0
3363 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3364 {
3365 _bfd_strip_section_from_output (info, s);
3366 /* The DYNSYMCOUNT might have changed if we were going to
3367 output a dynamic symbol table entry for S. */
3368 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3369 }
3370 else
3371 {
3372 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3373 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3374 if (s->contents == NULL)
3375 return false;
3376
3377 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
3378 return false;
3379 }
3380
3381 /* Set the size of the .dynsym and .hash sections. We counted
3382 the number of dynamic symbols in elf_link_add_object_symbols.
3383 We will build the contents of .dynsym and .hash when we build
3384 the final symbol table, because until then we do not know the
3385 correct value to give the symbols. We built the .dynstr
3386 section as we went along in elf_link_add_object_symbols. */
3387 s = bfd_get_section_by_name (dynobj, ".dynsym");
3388 BFD_ASSERT (s != NULL);
3389 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3390 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3391 if (s->contents == NULL && s->_raw_size != 0)
3392 return false;
3393
3394 if (dynsymcount != 0)
3395 {
3396 Elf_Internal_Sym isym;
3397
3398 /* The first entry in .dynsym is a dummy symbol. */
3399 isym.st_value = 0;
3400 isym.st_size = 0;
3401 isym.st_name = 0;
3402 isym.st_info = 0;
3403 isym.st_other = 0;
3404 isym.st_shndx = 0;
3405 elf_swap_symbol_out (output_bfd, &isym,
3406 (PTR) (Elf_External_Sym *) s->contents);
3407 }
3408
3409 /* Compute the size of the hashing table. As a side effect this
3410 computes the hash values for all the names we export. */
3411 bucketcount = compute_bucket_count (info);
3412
3413 s = bfd_get_section_by_name (dynobj, ".hash");
3414 BFD_ASSERT (s != NULL);
3415 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3416 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3417 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3418 if (s->contents == NULL)
3419 return false;
3420 memset (s->contents, 0, (size_t) s->_raw_size);
3421
3422 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
3423 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
3424 s->contents + hash_entry_size);
3425
3426 elf_hash_table (info)->bucketcount = bucketcount;
3427
3428 s = bfd_get_section_by_name (dynobj, ".dynstr");
3429 BFD_ASSERT (s != NULL);
3430 s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
3431
3432 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
3433 return false;
3434 }
3435
3436 return true;
3437}
3438
3439
3440/* Fix up the flags for a symbol. This handles various cases which
3441 can only be fixed after all the input files are seen. This is
3442 currently called by both adjust_dynamic_symbol and
3443 assign_sym_version, which is unnecessary but perhaps more robust in
3444 the face of future changes. */
3445
3446static boolean
3447elf_fix_symbol_flags (h, eif)
3448 struct elf_link_hash_entry *h;
3449 struct elf_info_failed *eif;
3450{
3451 /* If this symbol was mentioned in a non-ELF file, try to set
3452 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3453 permit a non-ELF file to correctly refer to a symbol defined in
3454 an ELF dynamic object. */
3455 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3456 {
3457 while (h->root.type == bfd_link_hash_indirect)
3458 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3459
3460 if (h->root.type != bfd_link_hash_defined
3461 && h->root.type != bfd_link_hash_defweak)
3462 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3463 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3464 else
3465 {
3466 if (h->root.u.def.section->owner != NULL
3467 && (bfd_get_flavour (h->root.u.def.section->owner)
3468 == bfd_target_elf_flavour))
3469 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3470 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3471 else
3472 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3473 }
3474
3475 if (h->dynindx == -1
3476 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3477 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3478 {
3479 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3480 {
3481 eif->failed = true;
3482 return false;
3483 }
3484 }
3485 }
3486 else
3487 {
3488 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3489 was first seen in a non-ELF file. Fortunately, if the symbol
3490 was first seen in an ELF file, we're probably OK unless the
3491 symbol was defined in a non-ELF file. Catch that case here.
3492 FIXME: We're still in trouble if the symbol was first seen in
3493 a dynamic object, and then later in a non-ELF regular object. */
3494 if ((h->root.type == bfd_link_hash_defined
3495 || h->root.type == bfd_link_hash_defweak)
3496 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3497 && (h->root.u.def.section->owner != NULL
3498 ? (bfd_get_flavour (h->root.u.def.section->owner)
3499 != bfd_target_elf_flavour)
3500 : (bfd_is_abs_section (h->root.u.def.section)
3501 && (h->elf_link_hash_flags
3502 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3503 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3504 }
3505
3506 /* If this is a final link, and the symbol was defined as a common
3507 symbol in a regular object file, and there was no definition in
3508 any dynamic object, then the linker will have allocated space for
3509 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3510 flag will not have been set. */
3511 if (h->root.type == bfd_link_hash_defined
3512 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3513 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3514 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3515 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3516 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3517
3518 /* If -Bsymbolic was used (which means to bind references to global
3519 symbols to the definition within the shared object), and this
3520 symbol was defined in a regular object, then it actually doesn't
3521 need a PLT entry, and we can accomplish that by forcing it local.
3522 Likewise, if the symbol has hidden or internal visibility.
3523 FIXME: It might be that we also do not need a PLT for other
3524 non-hidden visibilities, but we would have to tell that to the
3525 backend specifically; we can't just clear PLT-related data here. */
3526 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3527 && eif->info->shared
3528 && (eif->info->symbolic
3529 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3530 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3531 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3532 {
3533 struct elf_backend_data *bed;
3534 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3535 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3536 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3537 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3538 (*bed->elf_backend_hide_symbol) (eif->info, h);
3539 }
3540
3541 /* If this is a weak defined symbol in a dynamic object, and we know
3542 the real definition in the dynamic object, copy interesting flags
3543 over to the real definition. */
3544 if (h->weakdef != NULL)
3545 {
3546 struct elf_link_hash_entry *weakdef;
3547
3548 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3549 || h->root.type == bfd_link_hash_defweak);
3550 weakdef = h->weakdef;
3551 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3552 || weakdef->root.type == bfd_link_hash_defweak);
3553 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3554
3555 /* If the real definition is defined by a regular object file,
3556 don't do anything special. See the longer description in
3557 elf_adjust_dynamic_symbol, below. */
3558 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3559 h->weakdef = NULL;
3560 else
3561 weakdef->elf_link_hash_flags |=
3562 (h->elf_link_hash_flags
3563 & (ELF_LINK_HASH_REF_REGULAR
3564 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3565 | ELF_LINK_NON_GOT_REF));
3566 }
3567
3568 return true;
3569}
3570
3571/* Make the backend pick a good value for a dynamic symbol. This is
3572 called via elf_link_hash_traverse, and also calls itself
3573 recursively. */
3574
3575static boolean
3576elf_adjust_dynamic_symbol (h, data)
3577 struct elf_link_hash_entry *h;
3578 PTR data;
3579{
3580 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3581 bfd *dynobj;
3582 struct elf_backend_data *bed;
3583
3584 /* Ignore indirect symbols. These are added by the versioning code. */
3585 if (h->root.type == bfd_link_hash_indirect)
3586 return true;
3587
3588 /* Fix the symbol flags. */
3589 if (! elf_fix_symbol_flags (h, eif))
3590 return false;
3591
3592 /* If this symbol does not require a PLT entry, and it is not
3593 defined by a dynamic object, or is not referenced by a regular
3594 object, ignore it. We do have to handle a weak defined symbol,
3595 even if no regular object refers to it, if we decided to add it
3596 to the dynamic symbol table. FIXME: Do we normally need to worry
3597 about symbols which are defined by one dynamic object and
3598 referenced by another one? */
3599 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3600 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3601 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3602 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3603 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3604 {
3605 h->plt.offset = (bfd_vma) -1;
3606 return true;
3607 }
3608
3609 /* If we've already adjusted this symbol, don't do it again. This
3610 can happen via a recursive call. */
3611 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3612 return true;
3613
3614 /* Don't look at this symbol again. Note that we must set this
3615 after checking the above conditions, because we may look at a
3616 symbol once, decide not to do anything, and then get called
3617 recursively later after REF_REGULAR is set below. */
3618 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3619
3620 /* If this is a weak definition, and we know a real definition, and
3621 the real symbol is not itself defined by a regular object file,
3622 then get a good value for the real definition. We handle the
3623 real symbol first, for the convenience of the backend routine.
3624
3625 Note that there is a confusing case here. If the real definition
3626 is defined by a regular object file, we don't get the real symbol
3627 from the dynamic object, but we do get the weak symbol. If the
3628 processor backend uses a COPY reloc, then if some routine in the
3629 dynamic object changes the real symbol, we will not see that
3630 change in the corresponding weak symbol. This is the way other
3631 ELF linkers work as well, and seems to be a result of the shared
3632 library model.
3633
3634 I will clarify this issue. Most SVR4 shared libraries define the
3635 variable _timezone and define timezone as a weak synonym. The
3636 tzset call changes _timezone. If you write
3637 extern int timezone;
3638 int _timezone = 5;
3639 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3640 you might expect that, since timezone is a synonym for _timezone,
3641 the same number will print both times. However, if the processor
3642 backend uses a COPY reloc, then actually timezone will be copied
3643 into your process image, and, since you define _timezone
3644 yourself, _timezone will not. Thus timezone and _timezone will
3645 wind up at different memory locations. The tzset call will set
3646 _timezone, leaving timezone unchanged. */
3647
3648 if (h->weakdef != NULL)
3649 {
3650 /* If we get to this point, we know there is an implicit
3651 reference by a regular object file via the weak symbol H.
3652 FIXME: Is this really true? What if the traversal finds
3653 H->WEAKDEF before it finds H? */
3654 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3655
3656 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
3657 return false;
3658 }
3659
3660 /* If a symbol has no type and no size and does not require a PLT
3661 entry, then we are probably about to do the wrong thing here: we
3662 are probably going to create a COPY reloc for an empty object.
3663 This case can arise when a shared object is built with assembly
3664 code, and the assembly code fails to set the symbol type. */
3665 if (h->size == 0
3666 && h->type == STT_NOTYPE
3667 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3668 (*_bfd_error_handler)
3669 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3670 h->root.root.string);
3671
3672 dynobj = elf_hash_table (eif->info)->dynobj;
3673 bed = get_elf_backend_data (dynobj);
3674 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3675 {
3676 eif->failed = true;
3677 return false;
3678 }
3679
3680 return true;
3681}
3682
3683
3684/* This routine is used to export all defined symbols into the dynamic
3685 symbol table. It is called via elf_link_hash_traverse. */
3686
3687static boolean
3688elf_export_symbol (h, data)
3689 struct elf_link_hash_entry *h;
3690 PTR data;
3691{
3692 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3693
3694 /* Ignore indirect symbols. These are added by the versioning code. */
3695 if (h->root.type == bfd_link_hash_indirect)
3696 return true;
3697
3698 if (h->dynindx == -1
3699 && (h->elf_link_hash_flags
3700 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
3701 {
3702 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3703 {
3704 eif->failed = true;
3705 return false;
3706 }
3707 }
3708
3709 return true;
3710}
3711
3712
3713/* Look through the symbols which are defined in other shared
3714 libraries and referenced here. Update the list of version
3715 dependencies. This will be put into the .gnu.version_r section.
3716 This function is called via elf_link_hash_traverse. */
3717
3718static boolean
3719elf_link_find_version_dependencies (h, data)
3720 struct elf_link_hash_entry *h;
3721 PTR data;
3722{
3723 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
3724 Elf_Internal_Verneed *t;
3725 Elf_Internal_Vernaux *a;
3726
3727 /* We only care about symbols defined in shared objects with version
3728 information. */
3729 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3730 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3731 || h->dynindx == -1
3732 || h->verinfo.verdef == NULL)
3733 return true;
3734
3735 /* See if we already know about this version. */
3736 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
3737 {
3738 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
3739 continue;
3740
3741 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3742 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
3743 return true;
3744
3745 break;
3746 }
3747
3748 /* This is a new version. Add it to tree we are building. */
3749
3750 if (t == NULL)
3751 {
3752 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t);
3753 if (t == NULL)
3754 {
3755 rinfo->failed = true;
3756 return false;
3757 }
3758
3759 t->vn_bfd = h->verinfo.verdef->vd_bfd;
3760 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
3761 elf_tdata (rinfo->output_bfd)->verref = t;
3762 }
3763
3764 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a);
3765
3766 /* Note that we are copying a string pointer here, and testing it
3767 above. If bfd_elf_string_from_elf_section is ever changed to
3768 discard the string data when low in memory, this will have to be
3769 fixed. */
3770 a->vna_nodename = h->verinfo.verdef->vd_nodename;
3771
3772 a->vna_flags = h->verinfo.verdef->vd_flags;
3773 a->vna_nextptr = t->vn_auxptr;
3774
3775 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
3776 ++rinfo->vers;
3777
3778 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
3779
3780 t->vn_auxptr = a;
3781
3782 return true;
3783}
3784
3785/* Figure out appropriate versions for all the symbols. We may not
3786 have the version number script until we have read all of the input
3787 files, so until that point we don't know which symbols should be
3788 local. This function is called via elf_link_hash_traverse. */
3789
3790static boolean
3791elf_link_assign_sym_version (h, data)
3792 struct elf_link_hash_entry *h;
3793 PTR data;
3794{
3795 struct elf_assign_sym_version_info *sinfo =
3796 (struct elf_assign_sym_version_info *) data;
3797 struct bfd_link_info *info = sinfo->info;
3798 struct elf_backend_data *bed;
3799 struct elf_info_failed eif;
3800 char *p;
3801
3802 /* Fix the symbol flags. */
3803 eif.failed = false;
3804 eif.info = info;
3805 if (! elf_fix_symbol_flags (h, &eif))
3806 {
3807 if (eif.failed)
3808 sinfo->failed = true;
3809 return false;
3810 }
3811
3812 /* We only need version numbers for symbols defined in regular
3813 objects. */
3814 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3815 return true;
3816
3817 bed = get_elf_backend_data (sinfo->output_bfd);
3818 p = strchr (h->root.root.string, ELF_VER_CHR);
3819 if (p != NULL && h->verinfo.vertree == NULL)
3820 {
3821 struct bfd_elf_version_tree *t;
3822 boolean hidden;
3823
3824 hidden = true;
3825
3826 /* There are two consecutive ELF_VER_CHR characters if this is
3827 not a hidden symbol. */
3828 ++p;
3829 if (*p == ELF_VER_CHR)
3830 {
3831 hidden = false;
3832 ++p;
3833 }
3834
3835 /* If there is no version string, we can just return out. */
3836 if (*p == '\0')
3837 {
3838 if (hidden)
3839 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3840 return true;
3841 }
3842
3843 /* Look for the version. If we find it, it is no longer weak. */
3844 for (t = sinfo->verdefs; t != NULL; t = t->next)
3845 {
3846 if (strcmp (t->name, p) == 0)
3847 {
3848 int len;
3849 char *alc;
3850 struct bfd_elf_version_expr *d;
3851
3852 len = p - h->root.root.string;
3853 alc = bfd_alloc (sinfo->output_bfd, len);
3854 if (alc == NULL)
3855 return false;
3856 strncpy (alc, h->root.root.string, len - 1);
3857 alc[len - 1] = '\0';
3858 if (alc[len - 2] == ELF_VER_CHR)
3859 alc[len - 2] = '\0';
3860
3861 h->verinfo.vertree = t;
3862 t->used = true;
3863 d = NULL;
3864
3865 if (t->globals != NULL)
3866 {
3867 for (d = t->globals; d != NULL; d = d->next)
3868 if ((*d->match) (d, alc))
3869 break;
3870 }
3871
3872 /* See if there is anything to force this symbol to
3873 local scope. */
3874 if (d == NULL && t->locals != NULL)
3875 {
3876 for (d = t->locals; d != NULL; d = d->next)
3877 {
3878 if ((*d->match) (d, alc))
3879 {
3880 if (h->dynindx != -1
3881 && info->shared
3882 && ! sinfo->export_dynamic)
3883 {
3884 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3885 (*bed->elf_backend_hide_symbol) (info, h);
3886 /* FIXME: The name of the symbol has
3887 already been recorded in the dynamic
3888 string table section. */
3889 }
3890
3891 break;
3892 }
3893 }
3894 }
3895
3896 bfd_release (sinfo->output_bfd, alc);
3897 break;
3898 }
3899 }
3900
3901 /* If we are building an application, we need to create a
3902 version node for this version. */
3903 if (t == NULL && ! info->shared)
3904 {
3905 struct bfd_elf_version_tree **pp;
3906 int version_index;
3907
3908 /* If we aren't going to export this symbol, we don't need
3909 to worry about it. */
3910 if (h->dynindx == -1)
3911 return true;
3912
3913 t = ((struct bfd_elf_version_tree *)
3914 bfd_alloc (sinfo->output_bfd, sizeof *t));
3915 if (t == NULL)
3916 {
3917 sinfo->failed = true;
3918 return false;
3919 }
3920
3921 t->next = NULL;
3922 t->name = p;
3923 t->globals = NULL;
3924 t->locals = NULL;
3925 t->deps = NULL;
3926 t->name_indx = (unsigned int) -1;
3927 t->used = true;
3928
3929 version_index = 1;
3930 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
3931 ++version_index;
3932 t->vernum = version_index;
3933
3934 *pp = t;
3935
3936 h->verinfo.vertree = t;
3937 }
3938 else if (t == NULL)
3939 {
3940 /* We could not find the version for a symbol when
3941 generating a shared archive. Return an error. */
3942 (*_bfd_error_handler)
3943 (_("%s: undefined versioned symbol name %s"),
3944 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
3945 bfd_set_error (bfd_error_bad_value);
3946 sinfo->failed = true;
3947 return false;
3948 }
3949
3950 if (hidden)
3951 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3952 }
3953
3954 /* If we don't have a version for this symbol, see if we can find
3955 something. */
3956 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
3957 {
3958 struct bfd_elf_version_tree *t;
3959 struct bfd_elf_version_tree *deflt;
3960 struct bfd_elf_version_expr *d;
3961
3962 /* See if can find what version this symbol is in. If the
3963 symbol is supposed to be local, then don't actually register
3964 it. */
3965 deflt = NULL;
3966 for (t = sinfo->verdefs; t != NULL; t = t->next)
3967 {
3968 if (t->globals != NULL)
3969 {
3970 for (d = t->globals; d != NULL; d = d->next)
3971 {
3972 if ((*d->match) (d, h->root.root.string))
3973 {
3974 h->verinfo.vertree = t;
3975 break;
3976 }
3977 }
3978
3979 if (d != NULL)
3980 break;
3981 }
3982
3983 if (t->locals != NULL)
3984 {
3985 for (d = t->locals; d != NULL; d = d->next)
3986 {
3987 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
3988 deflt = t;
3989 else if ((*d->match) (d, h->root.root.string))
3990 {
3991 h->verinfo.vertree = t;
3992 if (h->dynindx != -1
3993 && info->shared
3994 && ! sinfo->export_dynamic)
3995 {
3996 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3997 (*bed->elf_backend_hide_symbol) (info, h);
3998 /* FIXME: The name of the symbol has already
3999 been recorded in the dynamic string table
4000 section. */
4001 }
4002 break;
4003 }
4004 }
4005
4006 if (d != NULL)
4007 break;
4008 }
4009 }
4010
4011 if (deflt != NULL && h->verinfo.vertree == NULL)
4012 {
4013 h->verinfo.vertree = deflt;
4014 if (h->dynindx != -1
4015 && info->shared
4016 && ! sinfo->export_dynamic)
4017 {
4018 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4019 (*bed->elf_backend_hide_symbol) (info, h);
4020 /* FIXME: The name of the symbol has already been
4021 recorded in the dynamic string table section. */
4022 }
4023 }
4024 }
4025
4026 return true;
4027}
4028
4029
4030/* Final phase of ELF linker. */
4031
4032/* A structure we use to avoid passing large numbers of arguments. */
4033
4034struct elf_final_link_info
4035{
4036 /* General link information. */
4037 struct bfd_link_info *info;
4038 /* Output BFD. */
4039 bfd *output_bfd;
4040 /* Symbol string table. */
4041 struct bfd_strtab_hash *symstrtab;
4042 /* .dynsym section. */
4043 asection *dynsym_sec;
4044 /* .hash section. */
4045 asection *hash_sec;
4046 /* symbol version section (.gnu.version). */
4047 asection *symver_sec;
4048 /* Buffer large enough to hold contents of any section. */
4049 bfd_byte *contents;
4050 /* Buffer large enough to hold external relocs of any section. */
4051 PTR external_relocs;
4052 /* Buffer large enough to hold internal relocs of any section. */
4053 Elf_Internal_Rela *internal_relocs;
4054 /* Buffer large enough to hold external local symbols of any input
4055 BFD. */
4056 Elf_External_Sym *external_syms;
4057 /* Buffer large enough to hold internal local symbols of any input
4058 BFD. */
4059 Elf_Internal_Sym *internal_syms;
4060 /* Array large enough to hold a symbol index for each local symbol
4061 of any input BFD. */
4062 long *indices;
4063 /* Array large enough to hold a section pointer for each local
4064 symbol of any input BFD. */
4065 asection **sections;
4066 /* Buffer to hold swapped out symbols. */
4067 Elf_External_Sym *symbuf;
4068 /* Number of swapped out symbols in buffer. */
4069 size_t symbuf_count;
4070 /* Number of symbols which fit in symbuf. */
4071 size_t symbuf_size;
4072};
4073
4074static boolean elf_link_output_sym
4075 PARAMS ((struct elf_final_link_info *, const char *,
4076 Elf_Internal_Sym *, asection *));
4077static boolean elf_link_flush_output_syms
4078 PARAMS ((struct elf_final_link_info *));
4079static boolean elf_link_output_extsym
4080 PARAMS ((struct elf_link_hash_entry *, PTR));
4081static boolean elf_link_input_bfd
4082 PARAMS ((struct elf_final_link_info *, bfd *));
4083static boolean elf_reloc_link_order
4084 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4085 struct bfd_link_order *));
4086
4087/* This struct is used to pass information to elf_link_output_extsym. */
4088
4089struct elf_outext_info
4090{
4091 boolean failed;
4092 boolean localsyms;
4093 struct elf_final_link_info *finfo;
4094};
4095
4096/* Compute the size of, and allocate space for, REL_HDR which is the
4097 section header for a section containing relocations for O. */
4098
4099static boolean
4100elf_link_size_reloc_section (abfd, rel_hdr, o)
4101 bfd *abfd;
4102 Elf_Internal_Shdr *rel_hdr;
4103 asection *o;
4104{
4105 unsigned reloc_count;
4106
4107 /* Figure out how many relocations there will be. */
4108 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4109 reloc_count = elf_section_data (o)->rel_count;
4110 else
4111 reloc_count = elf_section_data (o)->rel_count2;
4112
4113 /* That allows us to calculate the size of the section. */
4114 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4115
4116 /* The contents field must last into write_object_contents, so we
4117 allocate it with bfd_alloc rather than malloc. Also since we
4118 cannot be sure that the contents will actually be filled in,
4119 we zero the allocated space. */
4120 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4121 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4122 return false;
4123
4124 /* We only allocate one set of hash entries, so we only do it the
4125 first time we are called. */
4126 if (elf_section_data (o)->rel_hashes == NULL)
4127 {
4128 struct elf_link_hash_entry **p;
4129
4130 p = ((struct elf_link_hash_entry **)
4131 bfd_zmalloc (o->reloc_count
4132 * sizeof (struct elf_link_hash_entry *)));
4133 if (p == NULL && o->reloc_count != 0)
4134 return false;
4135
4136 elf_section_data (o)->rel_hashes = p;
4137 }
4138
4139 return true;
4140}
4141
4142/* When performing a relocateable link, the input relocations are
4143 preserved. But, if they reference global symbols, the indices
4144 referenced must be updated. Update all the relocations in
4145 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4146
4147static void
4148elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4149 bfd *abfd;
4150 Elf_Internal_Shdr *rel_hdr;
4151 unsigned int count;
4152 struct elf_link_hash_entry **rel_hash;
4153{
4154 unsigned int i;
4155 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4156 Elf_Internal_Rel *irel;
4157 Elf_Internal_Rela *irela;
4158
4159 irel = (Elf_Internal_Rel *) bfd_zmalloc (sizeof (Elf_Internal_Rel)
4160 * bed->s->int_rels_per_ext_rel);
4161 if (irel == NULL)
4162 {
4163 (*_bfd_error_handler) (_("Error: out of memory"));
4164 abort ();
4165 }
4166
4167 irela = (Elf_Internal_Rela *) bfd_zmalloc (sizeof (Elf_Internal_Rela)
4168 * bed->s->int_rels_per_ext_rel);
4169 if (irela == NULL)
4170 {
4171 (*_bfd_error_handler) (_("Error: out of memory"));
4172 abort ();
4173 }
4174
4175 for (i = 0; i < count; i++, rel_hash++)
4176 {
4177 if (*rel_hash == NULL)
4178 continue;
4179
4180 BFD_ASSERT ((*rel_hash)->indx >= 0);
4181
4182 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4183 {
4184 Elf_External_Rel *erel;
4185 unsigned int j;
4186
4187 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4188 if (bed->s->swap_reloc_in)
4189 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4190 else
4191 elf_swap_reloc_in (abfd, erel, irel);
4192
4193 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4194 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4195 ELF_R_TYPE (irel[j].r_info));
4196
4197 if (bed->s->swap_reloc_out)
4198 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4199 else
4200 elf_swap_reloc_out (abfd, irel, erel);
4201 }
4202 else
4203 {
4204 Elf_External_Rela *erela;
4205 unsigned int j;
4206
4207 BFD_ASSERT (rel_hdr->sh_entsize
4208 == sizeof (Elf_External_Rela));
4209
4210 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4211 if (bed->s->swap_reloca_in)
4212 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4213 else
4214 elf_swap_reloca_in (abfd, erela, irela);
4215
4216 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4217 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4218 ELF_R_TYPE (irela[j].r_info));
4219
4220 if (bed->s->swap_reloca_out)
4221 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4222 else
4223 elf_swap_reloca_out (abfd, irela, erela);
4224 }
4225 }
4226
4227 free (irel);
4228 free (irela);
4229}
4230
4231/* Do the final step of an ELF link. */
4232
4233boolean
4234elf_bfd_final_link (abfd, info)
4235 bfd *abfd;
4236 struct bfd_link_info *info;
4237{
4238 boolean dynamic;
4239 bfd *dynobj;
4240 struct elf_final_link_info finfo;
4241 register asection *o;
4242 register struct bfd_link_order *p;
4243 register bfd *sub;
4244 size_t max_contents_size;
4245 size_t max_external_reloc_size;
4246 size_t max_internal_reloc_count;
4247 size_t max_sym_count;
4248 file_ptr off;
4249 Elf_Internal_Sym elfsym;
4250 unsigned int i;
4251 Elf_Internal_Shdr *symtab_hdr;
4252 Elf_Internal_Shdr *symstrtab_hdr;
4253 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4254 struct elf_outext_info eoinfo;
4255
4256 if (info->shared)
4257 abfd->flags |= DYNAMIC;
4258
4259 dynamic = elf_hash_table (info)->dynamic_sections_created;
4260 dynobj = elf_hash_table (info)->dynobj;
4261
4262 finfo.info = info;
4263 finfo.output_bfd = abfd;
4264 finfo.symstrtab = elf_stringtab_init ();
4265 if (finfo.symstrtab == NULL)
4266 return false;
4267
4268 if (! dynamic)
4269 {
4270 finfo.dynsym_sec = NULL;
4271 finfo.hash_sec = NULL;
4272 finfo.symver_sec = NULL;
4273 }
4274 else
4275 {
4276 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4277 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4278 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4279 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4280 /* Note that it is OK if symver_sec is NULL. */
4281 }
4282
4283 finfo.contents = NULL;
4284 finfo.external_relocs = NULL;
4285 finfo.internal_relocs = NULL;
4286 finfo.external_syms = NULL;
4287 finfo.internal_syms = NULL;
4288 finfo.indices = NULL;
4289 finfo.sections = NULL;
4290 finfo.symbuf = NULL;
4291 finfo.symbuf_count = 0;
4292
4293 /* Count up the number of relocations we will output for each output
4294 section, so that we know the sizes of the reloc sections. We
4295 also figure out some maximum sizes. */
4296 max_contents_size = 0;
4297 max_external_reloc_size = 0;
4298 max_internal_reloc_count = 0;
4299 max_sym_count = 0;
4300 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4301 {
4302 o->reloc_count = 0;
4303
4304 for (p = o->link_order_head; p != NULL; p = p->next)
4305 {
4306 if (p->type == bfd_section_reloc_link_order
4307 || p->type == bfd_symbol_reloc_link_order)
4308 ++o->reloc_count;
4309 else if (p->type == bfd_indirect_link_order)
4310 {
4311 asection *sec;
4312
4313 sec = p->u.indirect.section;
4314
4315 /* Mark all sections which are to be included in the
4316 link. This will normally be every section. We need
4317 to do this so that we can identify any sections which
4318 the linker has decided to not include. */
4319 sec->linker_mark = true;
4320
4321 if (info->relocateable || info->emitrelocations)
4322 o->reloc_count += sec->reloc_count;
4323
4324 if (sec->_raw_size > max_contents_size)
4325 max_contents_size = sec->_raw_size;
4326 if (sec->_cooked_size > max_contents_size)
4327 max_contents_size = sec->_cooked_size;
4328
4329 /* We are interested in just local symbols, not all
4330 symbols. */
4331 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4332 && (sec->owner->flags & DYNAMIC) == 0)
4333 {
4334 size_t sym_count;
4335
4336 if (elf_bad_symtab (sec->owner))
4337 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4338 / sizeof (Elf_External_Sym));
4339 else
4340 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4341
4342 if (sym_count > max_sym_count)
4343 max_sym_count = sym_count;
4344
4345 if ((sec->flags & SEC_RELOC) != 0)
4346 {
4347 size_t ext_size;
4348
4349 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4350 if (ext_size > max_external_reloc_size)
4351 max_external_reloc_size = ext_size;
4352 if (sec->reloc_count > max_internal_reloc_count)
4353 max_internal_reloc_count = sec->reloc_count;
4354 }
4355 }
4356 }
4357 }
4358
4359 if (o->reloc_count > 0)
4360 o->flags |= SEC_RELOC;
4361 else
4362 {
4363 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4364 set it (this is probably a bug) and if it is set
4365 assign_section_numbers will create a reloc section. */
4366 o->flags &=~ SEC_RELOC;
4367 }
4368
4369 /* If the SEC_ALLOC flag is not set, force the section VMA to
4370 zero. This is done in elf_fake_sections as well, but forcing
4371 the VMA to 0 here will ensure that relocs against these
4372 sections are handled correctly. */
4373 if ((o->flags & SEC_ALLOC) == 0
4374 && ! o->user_set_vma)
4375 o->vma = 0;
4376 }
4377
4378 /* Figure out the file positions for everything but the symbol table
4379 and the relocs. We set symcount to force assign_section_numbers
4380 to create a symbol table. */
4381 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
4382 BFD_ASSERT (! abfd->output_has_begun);
4383 if (! _bfd_elf_compute_section_file_positions (abfd, info))
4384 goto error_return;
4385
4386 /* Figure out how many relocations we will have in each section.
4387 Just using RELOC_COUNT isn't good enough since that doesn't
4388 maintain a separate value for REL vs. RELA relocations. */
4389 if (info->relocateable || info->emitrelocations)
4390 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4391 for (o = sub->sections; o != NULL; o = o->next)
4392 {
4393 asection *output_section;
4394
4395 if (! o->linker_mark)
4396 {
4397 /* This section was omitted from the link. */
4398 continue;
4399 }
4400
4401 output_section = o->output_section;
4402
4403 if (output_section != NULL
4404 && (o->flags & SEC_RELOC) != 0)
4405 {
4406 struct bfd_elf_section_data *esdi
4407 = elf_section_data (o);
4408 struct bfd_elf_section_data *esdo
4409 = elf_section_data (output_section);
4410 unsigned int *rel_count;
4411 unsigned int *rel_count2;
4412
4413 /* We must be careful to add the relocation froms the
4414 input section to the right output count. */
4415 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
4416 {
4417 rel_count = &esdo->rel_count;
4418 rel_count2 = &esdo->rel_count2;
4419 }
4420 else
4421 {
4422 rel_count = &esdo->rel_count2;
4423 rel_count2 = &esdo->rel_count;
4424 }
4425
4426 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
4427 if (esdi->rel_hdr2)
4428 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
4429 }
4430 }
4431
4432 /* That created the reloc sections. Set their sizes, and assign
4433 them file positions, and allocate some buffers. */
4434 for (o = abfd->sections; o != NULL; o = o->next)
4435 {
4436 if ((o->flags & SEC_RELOC) != 0)
4437 {
4438 if (!elf_link_size_reloc_section (abfd,
4439 &elf_section_data (o)->rel_hdr,
4440 o))
4441 goto error_return;
4442
4443 if (elf_section_data (o)->rel_hdr2
4444 && !elf_link_size_reloc_section (abfd,
4445 elf_section_data (o)->rel_hdr2,
4446 o))
4447 goto error_return;
4448 }
4449
4450 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4451 to count upwards while actually outputting the relocations. */
4452 elf_section_data (o)->rel_count = 0;
4453 elf_section_data (o)->rel_count2 = 0;
4454 }
4455
4456 _bfd_elf_assign_file_positions_for_relocs (abfd);
4457
4458 /* We have now assigned file positions for all the sections except
4459 .symtab and .strtab. We start the .symtab section at the current
4460 file position, and write directly to it. We build the .strtab
4461 section in memory. */
4462 bfd_get_symcount (abfd) = 0;
4463 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4464 /* sh_name is set in prep_headers. */
4465 symtab_hdr->sh_type = SHT_SYMTAB;
4466 symtab_hdr->sh_flags = 0;
4467 symtab_hdr->sh_addr = 0;
4468 symtab_hdr->sh_size = 0;
4469 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
4470 /* sh_link is set in assign_section_numbers. */
4471 /* sh_info is set below. */
4472 /* sh_offset is set just below. */
4473 symtab_hdr->sh_addralign = bed->s->file_align;
4474
4475 off = elf_tdata (abfd)->next_file_pos;
4476 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
4477
4478 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4479 incorrect. We do not yet know the size of the .symtab section.
4480 We correct next_file_pos below, after we do know the size. */
4481
4482 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4483 continuously seeking to the right position in the file. */
4484 if (! info->keep_memory || max_sym_count < 20)
4485 finfo.symbuf_size = 20;
4486 else
4487 finfo.symbuf_size = max_sym_count;
4488 finfo.symbuf = ((Elf_External_Sym *)
4489 bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
4490 if (finfo.symbuf == NULL)
4491 goto error_return;
4492
4493 /* Start writing out the symbol table. The first symbol is always a
4494 dummy symbol. */
4495 if (info->strip != strip_all || info->relocateable || info->emitrelocations)
4496 {
4497 elfsym.st_value = 0;
4498 elfsym.st_size = 0;
4499 elfsym.st_info = 0;
4500 elfsym.st_other = 0;
4501 elfsym.st_shndx = SHN_UNDEF;
4502 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4503 &elfsym, bfd_und_section_ptr))
4504 goto error_return;
4505 }
4506
4507#if 0
4508 /* Some standard ELF linkers do this, but we don't because it causes
4509 bootstrap comparison failures. */
4510 /* Output a file symbol for the output file as the second symbol.
4511 We output this even if we are discarding local symbols, although
4512 I'm not sure if this is correct. */
4513 elfsym.st_value = 0;
4514 elfsym.st_size = 0;
4515 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4516 elfsym.st_other = 0;
4517 elfsym.st_shndx = SHN_ABS;
4518 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
4519 &elfsym, bfd_abs_section_ptr))
4520 goto error_return;
4521#endif
4522
4523 /* Output a symbol for each section. We output these even if we are
4524 discarding local symbols, since they are used for relocs. These
4525 symbols have no names. We store the index of each one in the
4526 index field of the section, so that we can find it again when
4527 outputting relocs. */
4528 if (info->strip != strip_all || info->relocateable || info->emitrelocations)
4529 {
4530 elfsym.st_size = 0;
4531 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4532 elfsym.st_other = 0;
4533 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
4534 {
4535 o = section_from_elf_index (abfd, i);
4536 if (o != NULL)
4537 o->target_index = bfd_get_symcount (abfd);
4538 elfsym.st_shndx = i;
4539 if (info->relocateable || o == NULL)
4540 elfsym.st_value = 0;
4541 else
4542 elfsym.st_value = o->vma;
4543 if (! elf_link_output_sym (&finfo, (const char *) NULL,
4544 &elfsym, o))
4545 goto error_return;
4546 }
4547 }
4548
4549 /* Allocate some memory to hold information read in from the input
4550 files. */
4551 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4552 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
4553 finfo.internal_relocs = ((Elf_Internal_Rela *)
4554 bfd_malloc (max_internal_reloc_count
4555 * sizeof (Elf_Internal_Rela)
4556 * bed->s->int_rels_per_ext_rel));
4557 finfo.external_syms = ((Elf_External_Sym *)
4558 bfd_malloc (max_sym_count
4559 * sizeof (Elf_External_Sym)));
4560 finfo.internal_syms = ((Elf_Internal_Sym *)
4561 bfd_malloc (max_sym_count
4562 * sizeof (Elf_Internal_Sym)));
4563 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4564 finfo.sections = ((asection **)
4565 bfd_malloc (max_sym_count * sizeof (asection *)));
4566 if ((finfo.contents == NULL && max_contents_size != 0)
4567 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
4568 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
4569 || (finfo.external_syms == NULL && max_sym_count != 0)
4570 || (finfo.internal_syms == NULL && max_sym_count != 0)
4571 || (finfo.indices == NULL && max_sym_count != 0)
4572 || (finfo.sections == NULL && max_sym_count != 0))
4573 goto error_return;
4574
4575 /* Since ELF permits relocations to be against local symbols, we
4576 must have the local symbols available when we do the relocations.
4577 Since we would rather only read the local symbols once, and we
4578 would rather not keep them in memory, we handle all the
4579 relocations for a single input file at the same time.
4580
4581 Unfortunately, there is no way to know the total number of local
4582 symbols until we have seen all of them, and the local symbol
4583 indices precede the global symbol indices. This means that when
4584 we are generating relocateable output, and we see a reloc against
4585 a global symbol, we can not know the symbol index until we have
4586 finished examining all the local symbols to see which ones we are
4587 going to output. To deal with this, we keep the relocations in
4588 memory, and don't output them until the end of the link. This is
4589 an unfortunate waste of memory, but I don't see a good way around
4590 it. Fortunately, it only happens when performing a relocateable
4591 link, which is not the common case. FIXME: If keep_memory is set
4592 we could write the relocs out and then read them again; I don't
4593 know how bad the memory loss will be. */
4594
4595 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4596 sub->output_has_begun = false;
4597 for (o = abfd->sections; o != NULL; o = o->next)
4598 {
4599 for (p = o->link_order_head; p != NULL; p = p->next)
4600 {
4601 if (p->type == bfd_indirect_link_order
4602 && (bfd_get_flavour (p->u.indirect.section->owner)
4603 == bfd_target_elf_flavour))
4604 {
4605 sub = p->u.indirect.section->owner;
4606 if (! sub->output_has_begun)
4607 {
4608 if (! elf_link_input_bfd (&finfo, sub))
4609 goto error_return;
4610 sub->output_has_begun = true;
4611 }
4612 }
4613 else if (p->type == bfd_section_reloc_link_order
4614 || p->type == bfd_symbol_reloc_link_order)
4615 {
4616 if (! elf_reloc_link_order (abfd, info, o, p))
4617 goto error_return;
4618 }
4619 else
4620 {
4621 if (! _bfd_default_link_order (abfd, info, o, p))
4622 goto error_return;
4623 }
4624 }
4625 }
4626
4627 /* That wrote out all the local symbols. Finish up the symbol table
4628 with the global symbols. Even if we want to strip everything we
4629 can, we still need to deal with those global symbols that got
4630 converted to local in a version script. */
4631
4632 if (info->shared)
4633 {
4634 /* Output any global symbols that got converted to local in a
4635 version script. We do this in a separate step since ELF
4636 requires all local symbols to appear prior to any global
4637 symbols. FIXME: We should only do this if some global
4638 symbols were, in fact, converted to become local. FIXME:
4639 Will this work correctly with the Irix 5 linker? */
4640 eoinfo.failed = false;
4641 eoinfo.finfo = &finfo;
4642 eoinfo.localsyms = true;
4643 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
4644 (PTR) &eoinfo);
4645 if (eoinfo.failed)
4646 return false;
4647 }
4648
4649 /* The sh_info field records the index of the first non local symbol. */
4650 symtab_hdr->sh_info = bfd_get_symcount (abfd);
4651
4652 if (dynamic
4653 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
4654 {
4655 Elf_Internal_Sym sym;
4656 Elf_External_Sym *dynsym =
4657 (Elf_External_Sym *)finfo.dynsym_sec->contents;
4658 long last_local = 0;
4659
4660 /* Write out the section symbols for the output sections. */
4661 if (info->shared)
4662 {
4663 asection *s;
4664
4665 sym.st_size = 0;
4666 sym.st_name = 0;
4667 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4668 sym.st_other = 0;
4669
4670 for (s = abfd->sections; s != NULL; s = s->next)
4671 {
4672 int indx;
4673 indx = elf_section_data (s)->this_idx;
4674 BFD_ASSERT (indx > 0);
4675 sym.st_shndx = indx;
4676 sym.st_value = s->vma;
4677
4678 elf_swap_symbol_out (abfd, &sym,
4679 dynsym + elf_section_data (s)->dynindx);
4680 }
4681
4682 last_local = bfd_count_sections (abfd);
4683 }
4684
4685 /* Write out the local dynsyms. */
4686 if (elf_hash_table (info)->dynlocal)
4687 {
4688 struct elf_link_local_dynamic_entry *e;
4689 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
4690 {
4691 asection *s;
4692
4693 sym.st_size = e->isym.st_size;
4694 sym.st_other = e->isym.st_other;
4695
4696 /* Copy the internal symbol as is.
4697 Note that we saved a word of storage and overwrote
4698 the original st_name with the dynstr_index. */
4699 sym = e->isym;
4700
4701 if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE)
4702 {
4703 s = bfd_section_from_elf_index (e->input_bfd,
4704 e->isym.st_shndx);
4705
4706 sym.st_shndx =
4707 elf_section_data (s->output_section)->this_idx;
4708 sym.st_value = (s->output_section->vma
4709 + s->output_offset
4710 + e->isym.st_value);
4711 }
4712
4713 if (last_local < e->dynindx)
4714 last_local = e->dynindx;
4715
4716 elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx);
4717 }
4718 }
4719
4720 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
4721 last_local + 1;
4722 }
4723
4724 /* We get the global symbols from the hash table. */
4725 eoinfo.failed = false;
4726 eoinfo.localsyms = false;
4727 eoinfo.finfo = &finfo;
4728 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
4729 (PTR) &eoinfo);
4730 if (eoinfo.failed)
4731 return false;
4732
4733 /* If backend needs to output some symbols not present in the hash
4734 table, do it now. */
4735 if (bed->elf_backend_output_arch_syms)
4736 {
4737 if (! (*bed->elf_backend_output_arch_syms)
4738 (abfd, info, (PTR) &finfo,
4739 (boolean (*) PARAMS ((PTR, const char *,
4740 Elf_Internal_Sym *, asection *)))
4741 elf_link_output_sym))
4742 return false;
4743 }
4744
4745 /* Flush all symbols to the file. */
4746 if (! elf_link_flush_output_syms (&finfo))
4747 return false;
4748
4749 /* Now we know the size of the symtab section. */
4750 off += symtab_hdr->sh_size;
4751
4752 /* Finish up and write out the symbol string table (.strtab)
4753 section. */
4754 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4755 /* sh_name was set in prep_headers. */
4756 symstrtab_hdr->sh_type = SHT_STRTAB;
4757 symstrtab_hdr->sh_flags = 0;
4758 symstrtab_hdr->sh_addr = 0;
4759 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
4760 symstrtab_hdr->sh_entsize = 0;
4761 symstrtab_hdr->sh_link = 0;
4762 symstrtab_hdr->sh_info = 0;
4763 /* sh_offset is set just below. */
4764 symstrtab_hdr->sh_addralign = 1;
4765
4766 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
4767 elf_tdata (abfd)->next_file_pos = off;
4768
4769 if (bfd_get_symcount (abfd) > 0)
4770 {
4771 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
4772 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
4773 return false;
4774 }
4775
4776 /* Adjust the relocs to have the correct symbol indices. */
4777 for (o = abfd->sections; o != NULL; o = o->next)
4778 {
4779 if ((o->flags & SEC_RELOC) == 0)
4780 continue;
4781
4782 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
4783 elf_section_data (o)->rel_count,
4784 elf_section_data (o)->rel_hashes);
4785 if (elf_section_data (o)->rel_hdr2 != NULL)
4786 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
4787 elf_section_data (o)->rel_count2,
4788 (elf_section_data (o)->rel_hashes
4789 + elf_section_data (o)->rel_count));
4790
4791 /* Set the reloc_count field to 0 to prevent write_relocs from
4792 trying to swap the relocs out itself. */
4793 o->reloc_count = 0;
4794 }
4795
4796 /* If we are linking against a dynamic object, or generating a
4797 shared library, finish up the dynamic linking information. */
4798 if (dynamic)
4799 {
4800 Elf_External_Dyn *dyncon, *dynconend;
4801
4802 /* Fix up .dynamic entries. */
4803 o = bfd_get_section_by_name (dynobj, ".dynamic");
4804 BFD_ASSERT (o != NULL);
4805
4806 dyncon = (Elf_External_Dyn *) o->contents;
4807 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
4808 for (; dyncon < dynconend; dyncon++)
4809 {
4810 Elf_Internal_Dyn dyn;
4811 const char *name;
4812 unsigned int type;
4813
4814 elf_swap_dyn_in (dynobj, dyncon, &dyn);
4815
4816 switch (dyn.d_tag)
4817 {
4818 default:
4819 break;
4820 case DT_INIT:
4821 name = info->init_function;
4822 goto get_sym;
4823 case DT_FINI:
4824 name = info->fini_function;
4825 get_sym:
4826 {
4827 struct elf_link_hash_entry *h;
4828
4829 h = elf_link_hash_lookup (elf_hash_table (info), name,
4830 false, false, true);
4831 if (h != NULL
4832 && (h->root.type == bfd_link_hash_defined
4833 || h->root.type == bfd_link_hash_defweak))
4834 {
4835 dyn.d_un.d_val = h->root.u.def.value;
4836 o = h->root.u.def.section;
4837 if (o->output_section != NULL)
4838 dyn.d_un.d_val += (o->output_section->vma
4839 + o->output_offset);
4840 else
4841 {
4842 /* The symbol is imported from another shared
4843 library and does not apply to this one. */
4844 dyn.d_un.d_val = 0;
4845 }
4846
4847 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4848 }
4849 }
4850 break;
4851
4852 case DT_HASH:
4853 name = ".hash";
4854 goto get_vma;
4855 case DT_STRTAB:
4856 name = ".dynstr";
4857 goto get_vma;
4858 case DT_SYMTAB:
4859 name = ".dynsym";
4860 goto get_vma;
4861 case DT_VERDEF:
4862 name = ".gnu.version_d";
4863 goto get_vma;
4864 case DT_VERNEED:
4865 name = ".gnu.version_r";
4866 goto get_vma;
4867 case DT_VERSYM:
4868 name = ".gnu.version";
4869 get_vma:
4870 o = bfd_get_section_by_name (abfd, name);
4871 BFD_ASSERT (o != NULL);
4872 dyn.d_un.d_ptr = o->vma;
4873 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4874 break;
4875
4876 case DT_REL:
4877 case DT_RELA:
4878 case DT_RELSZ:
4879 case DT_RELASZ:
4880 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
4881 type = SHT_REL;
4882 else
4883 type = SHT_RELA;
4884 dyn.d_un.d_val = 0;
4885 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
4886 {
4887 Elf_Internal_Shdr *hdr;
4888
4889 hdr = elf_elfsections (abfd)[i];
4890 if (hdr->sh_type == type
4891 && (hdr->sh_flags & SHF_ALLOC) != 0)
4892 {
4893 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
4894 dyn.d_un.d_val += hdr->sh_size;
4895 else
4896 {
4897 if (dyn.d_un.d_val == 0
4898 || hdr->sh_addr < dyn.d_un.d_val)
4899 dyn.d_un.d_val = hdr->sh_addr;
4900 }
4901 }
4902 }
4903 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4904 break;
4905 }
4906 }
4907 }
4908
4909 /* If we have created any dynamic sections, then output them. */
4910 if (dynobj != NULL)
4911 {
4912 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
4913 goto error_return;
4914
4915 for (o = dynobj->sections; o != NULL; o = o->next)
4916 {
4917 if ((o->flags & SEC_HAS_CONTENTS) == 0
4918 || o->_raw_size == 0
4919 || o->output_section == bfd_abs_section_ptr)
4920 continue;
4921 if ((o->flags & SEC_LINKER_CREATED) == 0)
4922 {
4923 /* At this point, we are only interested in sections
4924 created by elf_link_create_dynamic_sections. */
4925 continue;
4926 }
4927 if ((elf_section_data (o->output_section)->this_hdr.sh_type
4928 != SHT_STRTAB)
4929 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
4930 {
4931 if (! bfd_set_section_contents (abfd, o->output_section,
4932 o->contents, o->output_offset,
4933 o->_raw_size))
4934 goto error_return;
4935 }
4936 else
4937 {
4938 file_ptr off;
4939
4940 /* The contents of the .dynstr section are actually in a
4941 stringtab. */
4942 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
4943 if (bfd_seek (abfd, off, SEEK_SET) != 0
4944 || ! _bfd_stringtab_emit (abfd,
4945 elf_hash_table (info)->dynstr))
4946 goto error_return;
4947 }
4948 }
4949 }
4950
4951 /* If we have optimized stabs strings, output them. */
4952 if (elf_hash_table (info)->stab_info != NULL)
4953 {
4954 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4955 goto error_return;
4956 }
4957
4958 if (finfo.symstrtab != NULL)
4959 _bfd_stringtab_free (finfo.symstrtab);
4960 if (finfo.contents != NULL)
4961 free (finfo.contents);
4962 if (finfo.external_relocs != NULL)
4963 free (finfo.external_relocs);
4964 if (finfo.internal_relocs != NULL)
4965 free (finfo.internal_relocs);
4966 if (finfo.external_syms != NULL)
4967 free (finfo.external_syms);
4968 if (finfo.internal_syms != NULL)
4969 free (finfo.internal_syms);
4970 if (finfo.indices != NULL)
4971 free (finfo.indices);
4972 if (finfo.sections != NULL)
4973 free (finfo.sections);
4974 if (finfo.symbuf != NULL)
4975 free (finfo.symbuf);
4976 for (o = abfd->sections; o != NULL; o = o->next)
4977 {
4978 if ((o->flags & SEC_RELOC) != 0
4979 && elf_section_data (o)->rel_hashes != NULL)
4980 free (elf_section_data (o)->rel_hashes);
4981 }
4982
4983 elf_tdata (abfd)->linker = true;
4984
4985 return true;
4986
4987 error_return:
4988 if (finfo.symstrtab != NULL)
4989 _bfd_stringtab_free (finfo.symstrtab);
4990 if (finfo.contents != NULL)
4991 free (finfo.contents);
4992 if (finfo.external_relocs != NULL)
4993 free (finfo.external_relocs);
4994 if (finfo.internal_relocs != NULL)
4995 free (finfo.internal_relocs);
4996 if (finfo.external_syms != NULL)
4997 free (finfo.external_syms);
4998 if (finfo.internal_syms != NULL)
4999 free (finfo.internal_syms);
5000 if (finfo.indices != NULL)
5001 free (finfo.indices);
5002 if (finfo.sections != NULL)
5003 free (finfo.sections);
5004 if (finfo.symbuf != NULL)
5005 free (finfo.symbuf);
5006 for (o = abfd->sections; o != NULL; o = o->next)
5007 {
5008 if ((o->flags & SEC_RELOC) != 0
5009 && elf_section_data (o)->rel_hashes != NULL)
5010 free (elf_section_data (o)->rel_hashes);
5011 }
5012
5013 return false;
5014}
5015
5016/* Add a symbol to the output symbol table. */
5017
5018static boolean
5019elf_link_output_sym (finfo, name, elfsym, input_sec)
5020 struct elf_final_link_info *finfo;
5021 const char *name;
5022 Elf_Internal_Sym *elfsym;
5023 asection *input_sec;
5024{
5025 boolean (*output_symbol_hook) PARAMS ((bfd *,
5026 struct bfd_link_info *info,
5027 const char *,
5028 Elf_Internal_Sym *,
5029 asection *));
5030
5031 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5032 elf_backend_link_output_symbol_hook;
5033 if (output_symbol_hook != NULL)
5034 {
5035 if (! ((*output_symbol_hook)
5036 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5037 return false;
5038 }
5039
5040 if (name == (const char *) NULL || *name == '\0')
5041 elfsym->st_name = 0;
5042 else if (input_sec->flags & SEC_EXCLUDE)
5043 elfsym->st_name = 0;
5044 else
5045 {
5046 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5047 name, true,
5048 false);
5049 if (elfsym->st_name == (unsigned long) -1)
5050 return false;
5051 }
5052
5053 if (finfo->symbuf_count >= finfo->symbuf_size)
5054 {
5055 if (! elf_link_flush_output_syms (finfo))
5056 return false;
5057 }
5058
5059 elf_swap_symbol_out (finfo->output_bfd, elfsym,
5060 (PTR) (finfo->symbuf + finfo->symbuf_count));
5061 ++finfo->symbuf_count;
5062
5063 ++ bfd_get_symcount (finfo->output_bfd);
5064
5065 return true;
5066}
5067
5068/* Flush the output symbols to the file. */
5069
5070static boolean
5071elf_link_flush_output_syms (finfo)
5072 struct elf_final_link_info *finfo;
5073{
5074 if (finfo->symbuf_count > 0)
5075 {
5076 Elf_Internal_Shdr *symtab;
5077
5078 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5079
5080 if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
5081 SEEK_SET) != 0
5082 || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
5083 sizeof (Elf_External_Sym), finfo->output_bfd)
5084 != finfo->symbuf_count * sizeof (Elf_External_Sym)))
5085 return false;
5086
5087 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5088
5089 finfo->symbuf_count = 0;
5090 }
5091
5092 return true;
5093}
5094
5095/* Add an external symbol to the symbol table. This is called from
5096 the hash table traversal routine. When generating a shared object,
5097 we go through the symbol table twice. The first time we output
5098 anything that might have been forced to local scope in a version
5099 script. The second time we output the symbols that are still
5100 global symbols. */
5101
5102static boolean
5103elf_link_output_extsym (h, data)
5104 struct elf_link_hash_entry *h;
5105 PTR data;
5106{
5107 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5108 struct elf_final_link_info *finfo = eoinfo->finfo;
5109 boolean strip;
5110 Elf_Internal_Sym sym;
5111 asection *input_sec;
5112
5113 /* Decide whether to output this symbol in this pass. */
5114 if (eoinfo->localsyms)
5115 {
5116 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5117 return true;
5118 }
5119 else
5120 {
5121 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5122 return true;
5123 }
5124
5125 /* If we are not creating a shared library, and this symbol is
5126 referenced by a shared library but is not defined anywhere, then
5127 warn that it is undefined. If we do not do this, the runtime
5128 linker will complain that the symbol is undefined when the
5129 program is run. We don't have to worry about symbols that are
5130 referenced by regular files, because we will already have issued
5131 warnings for them. */
5132 if (! finfo->info->relocateable
5133 && ! finfo->info->allow_shlib_undefined
5134 && ! (finfo->info->shared
5135 && !finfo->info->no_undefined)
5136 && h->root.type == bfd_link_hash_undefined
5137 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5138 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5139 {
5140 if (! ((*finfo->info->callbacks->undefined_symbol)
5141 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5142 (asection *) NULL, 0, true)))
5143 {
5144 eoinfo->failed = true;
5145 return false;
5146 }
5147 }
5148
5149 /* We don't want to output symbols that have never been mentioned by
5150 a regular file, or that we have been told to strip. However, if
5151 h->indx is set to -2, the symbol is used by a reloc and we must
5152 output it. */
5153 if (h->indx == -2)
5154 strip = false;
5155 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5156 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5157 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5158 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5159 strip = true;
5160 else if (finfo->info->strip == strip_all
5161 || (finfo->info->strip == strip_some
5162 && bfd_hash_lookup (finfo->info->keep_hash,
5163 h->root.root.string,
5164 false, false) == NULL))
5165 strip = true;
5166 else
5167 strip = false;
5168
5169 /* If we're stripping it, and it's not a dynamic symbol, there's
5170 nothing else to do unless it is a forced local symbol. */
5171 if (strip
5172 && h->dynindx == -1
5173 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5174 return true;
5175
5176 sym.st_value = 0;
5177 sym.st_size = h->size;
5178 sym.st_other = h->other;
5179 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5180 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5181 else if (h->root.type == bfd_link_hash_undefweak
5182 || h->root.type == bfd_link_hash_defweak)
5183 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5184 else
5185 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5186
5187 switch (h->root.type)
5188 {
5189 default:
5190 case bfd_link_hash_new:
5191 abort ();
5192 return false;
5193
5194 case bfd_link_hash_undefined:
5195 input_sec = bfd_und_section_ptr;
5196 sym.st_shndx = SHN_UNDEF;
5197 break;
5198
5199 case bfd_link_hash_undefweak:
5200 input_sec = bfd_und_section_ptr;
5201 sym.st_shndx = SHN_UNDEF;
5202 break;
5203
5204 case bfd_link_hash_defined:
5205 case bfd_link_hash_defweak:
5206 {
5207 input_sec = h->root.u.def.section;
5208 if (input_sec->output_section != NULL)
5209 {
5210 sym.st_shndx =
5211 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5212 input_sec->output_section);
5213 if (sym.st_shndx == (unsigned short) -1)
5214 {
5215 (*_bfd_error_handler)
5216 (_("%s: could not find output section %s for input section %s"),
5217 bfd_get_filename (finfo->output_bfd),
5218 input_sec->output_section->name,
5219 input_sec->name);
5220 eoinfo->failed = true;
5221 return false;
5222 }
5223
5224 /* ELF symbols in relocateable files are section relative,
5225 but in nonrelocateable files they are virtual
5226 addresses. */
5227 sym.st_value = h->root.u.def.value + input_sec->output_offset;
5228 if (! finfo->info->relocateable)
5229 sym.st_value += input_sec->output_section->vma;
5230 }
5231 else
5232 {
5233 BFD_ASSERT (input_sec->owner == NULL
5234 || (input_sec->owner->flags & DYNAMIC) != 0);
5235 sym.st_shndx = SHN_UNDEF;
5236 input_sec = bfd_und_section_ptr;
5237 }
5238 }
5239 break;
5240
5241 case bfd_link_hash_common:
5242 input_sec = h->root.u.c.p->section;
5243 sym.st_shndx = SHN_COMMON;
5244 sym.st_value = 1 << h->root.u.c.p->alignment_power;
5245 break;
5246
5247 case bfd_link_hash_indirect:
5248 /* These symbols are created by symbol versioning. They point
5249 to the decorated version of the name. For example, if the
5250 symbol foo@@GNU_1.2 is the default, which should be used when
5251 foo is used with no version, then we add an indirect symbol
5252 foo which points to foo@@GNU_1.2. We ignore these symbols,
5253 since the indirected symbol is already in the hash table. */
5254 return true;
5255
5256 case bfd_link_hash_warning:
5257 /* We can't represent these symbols in ELF, although a warning
5258 symbol may have come from a .gnu.warning.SYMBOL section. We
5259 just put the target symbol in the hash table. If the target
5260 symbol does not really exist, don't do anything. */
5261 if (h->root.u.i.link->type == bfd_link_hash_new)
5262 return true;
5263 return (elf_link_output_extsym
5264 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
5265 }
5266
5267 /* Give the processor backend a chance to tweak the symbol value,
5268 and also to finish up anything that needs to be done for this
5269 symbol. */
5270 if ((h->dynindx != -1
5271 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5272 && elf_hash_table (finfo->info)->dynamic_sections_created)
5273 {
5274 struct elf_backend_data *bed;
5275
5276 bed = get_elf_backend_data (finfo->output_bfd);
5277 if (! ((*bed->elf_backend_finish_dynamic_symbol)
5278 (finfo->output_bfd, finfo->info, h, &sym)))
5279 {
5280 eoinfo->failed = true;
5281 return false;
5282 }
5283 }
5284
5285 /* If we are marking the symbol as undefined, and there are no
5286 non-weak references to this symbol from a regular object, then
5287 mark the symbol as weak undefined; if there are non-weak
5288 references, mark the symbol as strong. We can't do this earlier,
5289 because it might not be marked as undefined until the
5290 finish_dynamic_symbol routine gets through with it. */
5291 if (sym.st_shndx == SHN_UNDEF
5292 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
5293 && (ELF_ST_BIND(sym.st_info) == STB_GLOBAL
5294 || ELF_ST_BIND(sym.st_info) == STB_WEAK))
5295 {
5296 int bindtype;
5297
5298 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
5299 bindtype = STB_GLOBAL;
5300 else
5301 bindtype = STB_WEAK;
5302 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
5303 }
5304
5305 /* If a symbol is not defined locally, we clear the visibility
5306 field. */
5307 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5308 sym.st_other ^= ELF_ST_VISIBILITY(sym.st_other);
5309
5310 /* If this symbol should be put in the .dynsym section, then put it
5311 there now. We have already know the symbol index. We also fill
5312 in the entry in the .hash section. */
5313 if (h->dynindx != -1
5314 && elf_hash_table (finfo->info)->dynamic_sections_created)
5315 {
5316 size_t bucketcount;
5317 size_t bucket;
5318 size_t hash_entry_size;
5319 bfd_byte *bucketpos;
5320 bfd_vma chain;
5321
5322 sym.st_name = h->dynstr_index;
5323
5324 elf_swap_symbol_out (finfo->output_bfd, &sym,
5325 (PTR) (((Elf_External_Sym *)
5326 finfo->dynsym_sec->contents)
5327 + h->dynindx));
5328
5329 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5330 bucket = h->elf_hash_value % bucketcount;
5331 hash_entry_size
5332 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
5333 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5334 + (bucket + 2) * hash_entry_size);
5335 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
5336 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
5337 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
5338 ((bfd_byte *) finfo->hash_sec->contents
5339 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
5340
5341 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
5342 {
5343 Elf_Internal_Versym iversym;
5344
5345 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5346 {
5347 if (h->verinfo.verdef == NULL)
5348 iversym.vs_vers = 0;
5349 else
5350 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
5351 }
5352 else
5353 {
5354 if (h->verinfo.vertree == NULL)
5355 iversym.vs_vers = 1;
5356 else
5357 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
5358 }
5359
5360 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
5361 iversym.vs_vers |= VERSYM_HIDDEN;
5362
5363 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym,
5364 (((Elf_External_Versym *)
5365 finfo->symver_sec->contents)
5366 + h->dynindx));
5367 }
5368 }
5369
5370 /* If we're stripping it, then it was just a dynamic symbol, and
5371 there's nothing else to do. */
5372 if (strip)
5373 return true;
5374
5375 h->indx = bfd_get_symcount (finfo->output_bfd);
5376
5377 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
5378 {
5379 eoinfo->failed = true;
5380 return false;
5381 }
5382
5383 return true;
5384}
5385
5386/* Copy the relocations indicated by the INTERNAL_RELOCS (which
5387 originated from the section given by INPUT_REL_HDR) to the
5388 OUTPUT_BFD. */
5389
5390static void
5391elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
5392 internal_relocs)
5393 bfd *output_bfd;
5394 asection *input_section;
5395 Elf_Internal_Shdr *input_rel_hdr;
5396 Elf_Internal_Rela *internal_relocs;
5397{
5398 Elf_Internal_Rela *irela;
5399 Elf_Internal_Rela *irelaend;
5400 Elf_Internal_Shdr *output_rel_hdr;
5401 asection *output_section;
5402 unsigned int *rel_countp = NULL;
5403 struct elf_backend_data *bed;
5404
5405 output_section = input_section->output_section;
5406 output_rel_hdr = NULL;
5407
5408 if (elf_section_data (output_section)->rel_hdr.sh_entsize
5409 == input_rel_hdr->sh_entsize)
5410 {
5411 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
5412 rel_countp = &elf_section_data (output_section)->rel_count;
5413 }
5414 else if (elf_section_data (output_section)->rel_hdr2
5415 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
5416 == input_rel_hdr->sh_entsize))
5417 {
5418 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
5419 rel_countp = &elf_section_data (output_section)->rel_count2;
5420 }
5421
5422 BFD_ASSERT (output_rel_hdr != NULL);
5423
5424 bed = get_elf_backend_data (output_bfd);
5425 irela = internal_relocs;
5426 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
5427 * bed->s->int_rels_per_ext_rel;
5428
5429 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5430 {
5431 Elf_External_Rel *erel;
5432 Elf_Internal_Rel *irel;
5433
5434 irel = (Elf_Internal_Rel *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
5435 * sizeof (Elf_Internal_Rel));
5436 if (irel == NULL)
5437 {
5438 (*_bfd_error_handler) (_("Error: out of memory"));
5439 abort ();
5440 }
5441
5442 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
5443 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
5444 {
5445 unsigned int i;
5446
5447 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5448 {
5449 irel[i].r_offset = irela[i].r_offset;
5450 irel[i].r_info = irela[i].r_info;
5451 BFD_ASSERT (irela[i].r_addend == 0);
5452 }
5453
5454 if (bed->s->swap_reloc_out)
5455 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
5456 else
5457 elf_swap_reloc_out (output_bfd, irel, erel);
5458 }
5459
5460 free (irel);
5461 }
5462 else
5463 {
5464 Elf_External_Rela *erela;
5465
5466 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
5467
5468 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
5469 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
5470 if (bed->s->swap_reloca_out)
5471 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
5472 else
5473 elf_swap_reloca_out (output_bfd, irela, erela);
5474 }
5475
5476 /* Bump the counter, so that we know where to add the next set of
5477 relocations. */
5478 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
5479}
5480
5481/* Link an input file into the linker output file. This function
5482 handles all the sections and relocations of the input file at once.
5483 This is so that we only have to read the local symbols once, and
5484 don't have to keep them in memory. */
5485
5486static boolean
5487elf_link_input_bfd (finfo, input_bfd)
5488 struct elf_final_link_info *finfo;
5489 bfd *input_bfd;
5490{
5491 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
5492 bfd *, asection *, bfd_byte *,
5493 Elf_Internal_Rela *,
5494 Elf_Internal_Sym *, asection **));
5495 bfd *output_bfd;
5496 Elf_Internal_Shdr *symtab_hdr;
5497 size_t locsymcount;
5498 size_t extsymoff;
5499 Elf_External_Sym *external_syms;
5500 Elf_External_Sym *esym;
5501 Elf_External_Sym *esymend;
5502 Elf_Internal_Sym *isym;
5503 long *pindex;
5504 asection **ppsection;
5505 asection *o;
5506 struct elf_backend_data *bed;
5507
5508 output_bfd = finfo->output_bfd;
5509 bed = get_elf_backend_data (output_bfd);
5510 relocate_section = bed->elf_backend_relocate_section;
5511
5512 /* If this is a dynamic object, we don't want to do anything here:
5513 we don't want the local symbols, and we don't want the section
5514 contents. */
5515 if ((input_bfd->flags & DYNAMIC) != 0)
5516 return true;
5517
5518 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5519 if (elf_bad_symtab (input_bfd))
5520 {
5521 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5522 extsymoff = 0;
5523 }
5524 else
5525 {
5526 locsymcount = symtab_hdr->sh_info;
5527 extsymoff = symtab_hdr->sh_info;
5528 }
5529
5530 /* Read the local symbols. */
5531 if (symtab_hdr->contents != NULL)
5532 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
5533 else if (locsymcount == 0)
5534 external_syms = NULL;
5535 else
5536 {
5537 external_syms = finfo->external_syms;
5538 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
5539 || (bfd_read (external_syms, sizeof (Elf_External_Sym),
5540 locsymcount, input_bfd)
5541 != locsymcount * sizeof (Elf_External_Sym)))
5542 return false;
5543 }
5544
5545 /* Swap in the local symbols and write out the ones which we know
5546 are going into the output file. */
5547 esym = external_syms;
5548 esymend = esym + locsymcount;
5549 isym = finfo->internal_syms;
5550 pindex = finfo->indices;
5551 ppsection = finfo->sections;
5552 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
5553 {
5554 asection *isec;
5555 const char *name;
5556 Elf_Internal_Sym osym;
5557
5558 elf_swap_symbol_in (input_bfd, esym, isym);
5559 *pindex = -1;
5560
5561 if (elf_bad_symtab (input_bfd))
5562 {
5563 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
5564 {
5565 *ppsection = NULL;
5566 continue;
5567 }
5568 }
5569
5570 name = NULL;
5571 if (isym->st_shndx == SHN_UNDEF)
5572 {
5573 isec = bfd_und_section_ptr;
5574 name = isec->name;
5575 }
5576 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
5577 isec = section_from_elf_index (input_bfd, isym->st_shndx);
5578 else if (isym->st_shndx == SHN_ABS)
5579 {
5580 isec = bfd_abs_section_ptr;
5581 name = isec->name;
5582 }
5583 else if (isym->st_shndx == SHN_COMMON)
5584 {
5585 isec = bfd_com_section_ptr;
5586 name = isec->name;
5587 }
5588 else
5589 {
5590 /* Who knows? */
5591 isec = NULL;
5592 }
5593
5594 *ppsection = isec;
5595
5596 /* Don't output the first, undefined, symbol. */
5597 if (esym == external_syms)
5598 continue;
5599
5600 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5601 {
5602 asection *ksec;
5603
5604 /* Save away all section symbol values. */
5605 if (isec != NULL)
5606 {
5607 if (name)
5608 {
5609 if (isec->symbol->value != isym->st_value)
5610 (*_bfd_error_handler)
5611 (_("%s: invalid section symbol index 0x%x (%s) ingored"),
5612 bfd_get_filename (input_bfd), isym->st_shndx,
5613 name);
5614 continue;
5615 }
5616 isec->symbol->value = isym->st_value;
5617 }
5618
5619 /* If this is a discarded link-once section symbol, update
5620 it's value to that of the kept section symbol. The
5621 linker will keep the first of any matching link-once
5622 sections, so we should have already seen it's section
5623 symbol. I trust no-one will have the bright idea of
5624 re-ordering the bfd list... */
5625 if (isec != NULL
5626 && (bfd_get_section_flags (input_bfd, isec) & SEC_LINK_ONCE) != 0
5627 && (ksec = isec->kept_section) != NULL)
5628 {
5629 isym->st_value = ksec->symbol->value;
5630
5631 /* That put the value right, but the section info is all
5632 wrong. I hope this works. */
5633 isec->output_offset = ksec->output_offset;
5634 isec->output_section = ksec->output_section;
5635 }
5636
5637 /* We never output section symbols. Instead, we use the
5638 section symbol of the corresponding section in the output
5639 file. */
5640 continue;
5641 }
5642
5643 /* If we are stripping all symbols, we don't want to output this
5644 one. */
5645 if (finfo->info->strip == strip_all)
5646 continue;
5647
5648 /* If we are discarding all local symbols, we don't want to
5649 output this one. If we are generating a relocateable output
5650 file, then some of the local symbols may be required by
5651 relocs; we output them below as we discover that they are
5652 needed. */
5653 if (finfo->info->discard == discard_all)
5654 continue;
5655
5656 /* If this symbol is defined in a section which we are
5657 discarding, we don't need to keep it, but note that
5658 linker_mark is only reliable for sections that have contents.
5659 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
5660 as well as linker_mark. */
5661 if (isym->st_shndx > 0
5662 && isym->st_shndx < SHN_LORESERVE
5663 && isec != NULL
5664 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
5665 || (! finfo->info->relocateable
5666 && (isec->flags & SEC_EXCLUDE) != 0)))
5667 continue;
5668
5669 /* Get the name of the symbol. */
5670 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
5671 isym->st_name);
5672 if (name == NULL)
5673 return false;
5674
5675 /* See if we are discarding symbols with this name. */
5676 if ((finfo->info->strip == strip_some
5677 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
5678 == NULL))
5679 || (finfo->info->discard == discard_l
5680 && bfd_is_local_label_name (input_bfd, name)))
5681 continue;
5682
5683 /* If we get here, we are going to output this symbol. */
5684
5685 osym = *isym;
5686
5687 /* Adjust the section index for the output file. */
5688 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
5689 isec->output_section);
5690 if (osym.st_shndx == (unsigned short) -1)
5691 return false;
5692
5693 *pindex = bfd_get_symcount (output_bfd);
5694
5695 /* ELF symbols in relocateable files are section relative, but
5696 in executable files they are virtual addresses. Note that
5697 this code assumes that all ELF sections have an associated
5698 BFD section with a reasonable value for output_offset; below
5699 we assume that they also have a reasonable value for
5700 output_section. Any special sections must be set up to meet
5701 these requirements. */
5702 osym.st_value += isec->output_offset;
5703 if (! finfo->info->relocateable)
5704 osym.st_value += isec->output_section->vma;
5705
5706 if (! elf_link_output_sym (finfo, name, &osym, isec))
5707 return false;
5708 }
5709
5710 /* Relocate the contents of each section. */
5711 for (o = input_bfd->sections; o != NULL; o = o->next)
5712 {
5713 bfd_byte *contents;
5714
5715 if (! o->linker_mark)
5716 {
5717 /* This section was omitted from the link. */
5718 continue;
5719 }
5720
5721 if ((o->flags & SEC_HAS_CONTENTS) == 0
5722 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
5723 continue;
5724
5725 if ((o->flags & SEC_LINKER_CREATED) != 0)
5726 {
5727 /* Section was created by elf_link_create_dynamic_sections
5728 or somesuch. */
5729 continue;
5730 }
5731
5732 /* Get the contents of the section. They have been cached by a
5733 relaxation routine. Note that o is a section in an input
5734 file, so the contents field will not have been set by any of
5735 the routines which work on output files. */
5736 if (elf_section_data (o)->this_hdr.contents != NULL)
5737 contents = elf_section_data (o)->this_hdr.contents;
5738 else
5739 {
5740 contents = finfo->contents;
5741 if (! bfd_get_section_contents (input_bfd, o, contents,
5742 (file_ptr) 0, o->_raw_size))
5743 return false;
5744 }
5745
5746 if ((o->flags & SEC_RELOC) != 0)
5747 {
5748 Elf_Internal_Rela *internal_relocs;
5749
5750 /* Get the swapped relocs. */
5751 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
5752 (input_bfd, o, finfo->external_relocs,
5753 finfo->internal_relocs, false));
5754 if (internal_relocs == NULL
5755 && o->reloc_count > 0)
5756 return false;
5757
5758 /* Relocate the section by invoking a back end routine.
5759
5760 The back end routine is responsible for adjusting the
5761 section contents as necessary, and (if using Rela relocs
5762 and generating a relocateable output file) adjusting the
5763 reloc addend as necessary.
5764
5765 The back end routine does not have to worry about setting
5766 the reloc address or the reloc symbol index.
5767
5768 The back end routine is given a pointer to the swapped in
5769 internal symbols, and can access the hash table entries
5770 for the external symbols via elf_sym_hashes (input_bfd).
5771
5772 When generating relocateable output, the back end routine
5773 must handle STB_LOCAL/STT_SECTION symbols specially. The
5774 output symbol is going to be a section symbol
5775 corresponding to the output section, which will require
5776 the addend to be adjusted. */
5777
5778 if (! (*relocate_section) (output_bfd, finfo->info,
5779 input_bfd, o, contents,
5780 internal_relocs,
5781 finfo->internal_syms,
5782 finfo->sections))
5783 return false;
5784
5785 if (finfo->info->relocateable || finfo->info->emitrelocations)
5786 {
5787 Elf_Internal_Rela *irela;
5788 Elf_Internal_Rela *irelaend;
5789 struct elf_link_hash_entry **rel_hash;
5790 Elf_Internal_Shdr *input_rel_hdr;
5791 unsigned int next_erel;
5792
5793 /* Adjust the reloc addresses and symbol indices. */
5794
5795 irela = internal_relocs;
5796 irelaend = irela
5797 + o->reloc_count * bed->s->int_rels_per_ext_rel;
5798 rel_hash = (elf_section_data (o->output_section)->rel_hashes
5799 + elf_section_data (o->output_section)->rel_count
5800 + elf_section_data (o->output_section)->rel_count2);
5801 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
5802 {
5803 unsigned long r_symndx;
5804 Elf_Internal_Sym *isym;
5805 asection *sec;
5806
5807 if (next_erel == bed->s->int_rels_per_ext_rel)
5808 {
5809 rel_hash++;
5810 next_erel = 0;
5811 }
5812
5813 irela->r_offset += o->output_offset;
5814
5815 /* Relocs in an executable have to be virtual addresses. */
5816 if (finfo->info->emitrelocations)
5817 irela->r_offset += o->output_section->vma;
5818
5819 r_symndx = ELF_R_SYM (irela->r_info);
5820
5821 if (r_symndx == 0)
5822 continue;
5823
5824 if (r_symndx >= locsymcount
5825 || (elf_bad_symtab (input_bfd)
5826 && finfo->sections[r_symndx] == NULL))
5827 {
5828 struct elf_link_hash_entry *rh;
5829 unsigned long indx;
5830
5831 /* This is a reloc against a global symbol. We
5832 have not yet output all the local symbols, so
5833 we do not know the symbol index of any global
5834 symbol. We set the rel_hash entry for this
5835 reloc to point to the global hash table entry
5836 for this symbol. The symbol index is then
5837 set at the end of elf_bfd_final_link. */
5838 indx = r_symndx - extsymoff;
5839 rh = elf_sym_hashes (input_bfd)[indx];
5840 while (rh->root.type == bfd_link_hash_indirect
5841 || rh->root.type == bfd_link_hash_warning)
5842 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5843
5844 /* Setting the index to -2 tells
5845 elf_link_output_extsym that this symbol is
5846 used by a reloc. */
5847 BFD_ASSERT (rh->indx < 0);
5848 rh->indx = -2;
5849
5850 *rel_hash = rh;
5851
5852 continue;
5853 }
5854
5855 /* This is a reloc against a local symbol. */
5856
5857 *rel_hash = NULL;
5858 isym = finfo->internal_syms + r_symndx;
5859 sec = finfo->sections[r_symndx];
5860 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5861 {
5862 /* I suppose the backend ought to fill in the
5863 section of any STT_SECTION symbol against a
5864 processor specific section. If we have
5865 discarded a section, the output_section will
5866 be the absolute section. */
5867 if (sec != NULL
5868 && (bfd_is_abs_section (sec)
5869 || (sec->output_section != NULL
5870 && bfd_is_abs_section (sec->output_section))))
5871 r_symndx = 0;
5872 else if (sec == NULL || sec->owner == NULL)
5873 {
5874 bfd_set_error (bfd_error_bad_value);
5875 return false;
5876 }
5877 else
5878 {
5879 r_symndx = sec->output_section->target_index;
5880 BFD_ASSERT (r_symndx != 0);
5881 }
5882 }
5883 else
5884 {
5885 if (finfo->indices[r_symndx] == -1)
5886 {
5887 unsigned long link;
5888 const char *name;
5889 asection *osec;
5890
5891 if (finfo->info->strip == strip_all)
5892 {
5893 /* You can't do ld -r -s. */
5894 bfd_set_error (bfd_error_invalid_operation);
5895 return false;
5896 }
5897
5898 /* This symbol was skipped earlier, but
5899 since it is needed by a reloc, we
5900 must output it now. */
5901 link = symtab_hdr->sh_link;
5902 name = bfd_elf_string_from_elf_section (input_bfd,
5903 link,
5904 isym->st_name);
5905 if (name == NULL)
5906 return false;
5907
5908 osec = sec->output_section;
5909 isym->st_shndx =
5910 _bfd_elf_section_from_bfd_section (output_bfd,
5911 osec);
5912 if (isym->st_shndx == (unsigned short) -1)
5913 return false;
5914
5915 isym->st_value += sec->output_offset;
5916 if (! finfo->info->relocateable)
5917 isym->st_value += osec->vma;
5918
5919 finfo->indices[r_symndx] = bfd_get_symcount (output_bfd);
5920
5921 if (! elf_link_output_sym (finfo, name, isym, sec))
5922 return false;
5923 }
5924
5925 r_symndx = finfo->indices[r_symndx];
5926 }
5927
5928 irela->r_info = ELF_R_INFO (r_symndx,
5929 ELF_R_TYPE (irela->r_info));
5930 }
5931
5932 /* Swap out the relocs. */
5933 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5934 elf_link_output_relocs (output_bfd, o,
5935 input_rel_hdr,
5936 internal_relocs);
5937 internal_relocs += NUM_SHDR_ENTRIES (input_rel_hdr)
5938 * bed->s->int_rels_per_ext_rel;
5939 input_rel_hdr = elf_section_data (o)->rel_hdr2;
5940 if (input_rel_hdr)
5941 elf_link_output_relocs (output_bfd, o,
5942 input_rel_hdr,
5943 internal_relocs);
5944 }
5945 }
5946
5947 /* Write out the modified section contents. */
5948 if (elf_section_data (o)->stab_info == NULL)
5949 {
5950 if (! (o->flags & SEC_EXCLUDE) &&
5951 ! bfd_set_section_contents (output_bfd, o->output_section,
5952 contents, o->output_offset,
5953 (o->_cooked_size != 0
5954 ? o->_cooked_size
5955 : o->_raw_size)))
5956 return false;
5957 }
5958 else
5959 {
5960 if (! (_bfd_write_section_stabs
5961 (output_bfd, &elf_hash_table (finfo->info)->stab_info,
5962 o, &elf_section_data (o)->stab_info, contents)))
5963 return false;
5964 }
5965 }
5966
5967 return true;
5968}
5969
5970/* Generate a reloc when linking an ELF file. This is a reloc
5971 requested by the linker, and does come from any input file. This
5972 is used to build constructor and destructor tables when linking
5973 with -Ur. */
5974
5975static boolean
5976elf_reloc_link_order (output_bfd, info, output_section, link_order)
5977 bfd *output_bfd;
5978 struct bfd_link_info *info;
5979 asection *output_section;
5980 struct bfd_link_order *link_order;
5981{
5982 reloc_howto_type *howto;
5983 long indx;
5984 bfd_vma offset;
5985 bfd_vma addend;
5986 struct elf_link_hash_entry **rel_hash_ptr;
5987 Elf_Internal_Shdr *rel_hdr;
5988 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5989
5990 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5991 if (howto == NULL)
5992 {
5993 bfd_set_error (bfd_error_bad_value);
5994 return false;
5995 }
5996
5997 addend = link_order->u.reloc.p->addend;
5998
5999 /* Figure out the symbol index. */
6000 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6001 + elf_section_data (output_section)->rel_count
6002 + elf_section_data (output_section)->rel_count2);
6003 if (link_order->type == bfd_section_reloc_link_order)
6004 {
6005 indx = link_order->u.reloc.p->u.section->target_index;
6006 BFD_ASSERT (indx != 0);
6007 *rel_hash_ptr = NULL;
6008 }
6009 else
6010 {
6011 struct elf_link_hash_entry *h;
6012
6013 /* Treat a reloc against a defined symbol as though it were
6014 actually against the section. */
6015 h = ((struct elf_link_hash_entry *)
6016 bfd_wrapped_link_hash_lookup (output_bfd, info,
6017 link_order->u.reloc.p->u.name,
6018 false, false, true));
6019 if (h != NULL
6020 && (h->root.type == bfd_link_hash_defined
6021 || h->root.type == bfd_link_hash_defweak))
6022 {
6023 asection *section;
6024
6025 section = h->root.u.def.section;
6026 indx = section->output_section->target_index;
6027 *rel_hash_ptr = NULL;
6028 /* It seems that we ought to add the symbol value to the
6029 addend here, but in practice it has already been added
6030 because it was passed to constructor_callback. */
6031 addend += section->output_section->vma + section->output_offset;
6032 }
6033 else if (h != NULL)
6034 {
6035 /* Setting the index to -2 tells elf_link_output_extsym that
6036 this symbol is used by a reloc. */
6037 h->indx = -2;
6038 *rel_hash_ptr = h;
6039 indx = 0;
6040 }
6041 else
6042 {
6043 if (! ((*info->callbacks->unattached_reloc)
6044 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6045 (asection *) NULL, (bfd_vma) 0)))
6046 return false;
6047 indx = 0;
6048 }
6049 }
6050
6051 /* If this is an inplace reloc, we must write the addend into the
6052 object file. */
6053 if (howto->partial_inplace && addend != 0)
6054 {
6055 bfd_size_type size;
6056 bfd_reloc_status_type rstat;
6057 bfd_byte *buf;
6058 boolean ok;
6059
6060 size = bfd_get_reloc_size (howto);
6061 buf = (bfd_byte *) bfd_zmalloc (size);
6062 if (buf == (bfd_byte *) NULL)
6063 return false;
6064 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6065 switch (rstat)
6066 {
6067 case bfd_reloc_ok:
6068 break;
6069 default:
6070 case bfd_reloc_outofrange:
6071 abort ();
6072 case bfd_reloc_overflow:
6073 if (! ((*info->callbacks->reloc_overflow)
6074 (info,
6075 (link_order->type == bfd_section_reloc_link_order
6076 ? bfd_section_name (output_bfd,
6077 link_order->u.reloc.p->u.section)
6078 : link_order->u.reloc.p->u.name),
6079 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6080 (bfd_vma) 0)))
6081 {
6082 free (buf);
6083 return false;
6084 }
6085 break;
6086 }
6087 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6088 (file_ptr) link_order->offset, size);
6089 free (buf);
6090 if (! ok)
6091 return false;
6092 }
6093
6094 /* The address of a reloc is relative to the section in a
6095 relocateable file, and is a virtual address in an executable
6096 file. */
6097 offset = link_order->offset;
6098 if (! info->relocateable)
6099 offset += output_section->vma;
6100
6101 rel_hdr = &elf_section_data (output_section)->rel_hdr;
6102
6103 if (rel_hdr->sh_type == SHT_REL)
6104 {
6105 Elf_Internal_Rel *irel;
6106 Elf_External_Rel *erel;
6107 unsigned int i;
6108
6109 irel = (Elf_Internal_Rel *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
6110 * sizeof (Elf_Internal_Rel));
6111 if (irel == NULL)
6112 return false;
6113
6114 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6115 irel[i].r_offset = offset;
6116 irel[0].r_info = ELF_R_INFO (indx, howto->type);
6117
6118 erel = ((Elf_External_Rel *) rel_hdr->contents
6119 + elf_section_data (output_section)->rel_count);
6120
6121 if (bed->s->swap_reloc_out)
6122 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
6123 else
6124 elf_swap_reloc_out (output_bfd, irel, erel);
6125
6126 free (irel);
6127 }
6128 else
6129 {
6130 Elf_Internal_Rela *irela;
6131 Elf_External_Rela *erela;
6132 unsigned int i;
6133
6134 irela = (Elf_Internal_Rela *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
6135 * sizeof (Elf_Internal_Rela));
6136 if (irela == NULL)
6137 return false;
6138
6139 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6140 irela[i].r_offset = offset;
6141 irela[0].r_info = ELF_R_INFO (indx, howto->type);
6142 irela[0].r_addend = addend;
6143
6144 erela = ((Elf_External_Rela *) rel_hdr->contents
6145 + elf_section_data (output_section)->rel_count);
6146
6147 if (bed->s->swap_reloca_out)
6148 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
6149 else
6150 elf_swap_reloca_out (output_bfd, irela, erela);
6151 }
6152
6153 ++elf_section_data (output_section)->rel_count;
6154
6155 return true;
6156}
6157
6158
6159/* Allocate a pointer to live in a linker created section. */
6160
6161boolean
6162elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
6163 bfd *abfd;
6164 struct bfd_link_info *info;
6165 elf_linker_section_t *lsect;
6166 struct elf_link_hash_entry *h;
6167 const Elf_Internal_Rela *rel;
6168{
6169 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
6170 elf_linker_section_pointers_t *linker_section_ptr;
6171 unsigned long r_symndx = ELF_R_SYM (rel->r_info);;
6172
6173 BFD_ASSERT (lsect != NULL);
6174
6175 /* Is this a global symbol? */
6176 if (h != NULL)
6177 {
6178 /* Has this symbol already been allocated, if so, our work is done */
6179 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
6180 rel->r_addend,
6181 lsect->which))
6182 return true;
6183
6184 ptr_linker_section_ptr = &h->linker_section_pointer;
6185 /* Make sure this symbol is output as a dynamic symbol. */
6186 if (h->dynindx == -1)
6187 {
6188 if (! elf_link_record_dynamic_symbol (info, h))
6189 return false;
6190 }
6191
6192 if (lsect->rel_section)
6193 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6194 }
6195
6196 else /* Allocation of a pointer to a local symbol */
6197 {
6198 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
6199
6200 /* Allocate a table to hold the local symbols if first time */
6201 if (!ptr)
6202 {
6203 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
6204 register unsigned int i;
6205
6206 ptr = (elf_linker_section_pointers_t **)
6207 bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *));
6208
6209 if (!ptr)
6210 return false;
6211
6212 elf_local_ptr_offsets (abfd) = ptr;
6213 for (i = 0; i < num_symbols; i++)
6214 ptr[i] = (elf_linker_section_pointers_t *)0;
6215 }
6216
6217 /* Has this symbol already been allocated, if so, our work is done */
6218 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
6219 rel->r_addend,
6220 lsect->which))
6221 return true;
6222
6223 ptr_linker_section_ptr = &ptr[r_symndx];
6224
6225 if (info->shared)
6226 {
6227 /* If we are generating a shared object, we need to
6228 output a R_<xxx>_RELATIVE reloc so that the
6229 dynamic linker can adjust this GOT entry. */
6230 BFD_ASSERT (lsect->rel_section != NULL);
6231 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6232 }
6233 }
6234
6235 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
6236 from internal memory. */
6237 BFD_ASSERT (ptr_linker_section_ptr != NULL);
6238 linker_section_ptr = (elf_linker_section_pointers_t *)
6239 bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t));
6240
6241 if (!linker_section_ptr)
6242 return false;
6243
6244 linker_section_ptr->next = *ptr_linker_section_ptr;
6245 linker_section_ptr->addend = rel->r_addend;
6246 linker_section_ptr->which = lsect->which;
6247 linker_section_ptr->written_address_p = false;
6248 *ptr_linker_section_ptr = linker_section_ptr;
6249
6250#if 0
6251 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
6252 {
6253 linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8);
6254 lsect->hole_offset += ARCH_SIZE / 8;
6255 lsect->sym_offset += ARCH_SIZE / 8;
6256 if (lsect->sym_hash) /* Bump up symbol value if needed */
6257 {
6258 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
6259#ifdef DEBUG
6260 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
6261 lsect->sym_hash->root.root.string,
6262 (long)ARCH_SIZE / 8,
6263 (long)lsect->sym_hash->root.u.def.value);
6264#endif
6265 }
6266 }
6267 else
6268#endif
6269 linker_section_ptr->offset = lsect->section->_raw_size;
6270
6271 lsect->section->_raw_size += ARCH_SIZE / 8;
6272
6273#ifdef DEBUG
6274 fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6275 lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size);
6276#endif
6277
6278 return true;
6279}
6280
6281
6282#if ARCH_SIZE==64
6283#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6284#endif
6285#if ARCH_SIZE==32
6286#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6287#endif
6288
6289/* Fill in the address for a pointer generated in a linker section. */
6290
6291bfd_vma
6292elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc)
6293 bfd *output_bfd;
6294 bfd *input_bfd;
6295 struct bfd_link_info *info;
6296 elf_linker_section_t *lsect;
6297 struct elf_link_hash_entry *h;
6298 bfd_vma relocation;
6299 const Elf_Internal_Rela *rel;
6300 int relative_reloc;
6301{
6302 elf_linker_section_pointers_t *linker_section_ptr;
6303
6304 BFD_ASSERT (lsect != NULL);
6305
6306 if (h != NULL) /* global symbol */
6307 {
6308 linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
6309 rel->r_addend,
6310 lsect->which);
6311
6312 BFD_ASSERT (linker_section_ptr != NULL);
6313
6314 if (! elf_hash_table (info)->dynamic_sections_created
6315 || (info->shared
6316 && info->symbolic
6317 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6318 {
6319 /* This is actually a static link, or it is a
6320 -Bsymbolic link and the symbol is defined
6321 locally. We must initialize this entry in the
6322 global section.
6323
6324 When doing a dynamic link, we create a .rela.<xxx>
6325 relocation entry to initialize the value. This
6326 is done in the finish_dynamic_symbol routine. */
6327 if (!linker_section_ptr->written_address_p)
6328 {
6329 linker_section_ptr->written_address_p = true;
6330 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
6331 lsect->section->contents + linker_section_ptr->offset);
6332 }
6333 }
6334 }
6335 else /* local symbol */
6336 {
6337 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6338 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6339 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
6340 linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx],
6341 rel->r_addend,
6342 lsect->which);
6343
6344 BFD_ASSERT (linker_section_ptr != NULL);
6345
6346 /* Write out pointer if it hasn't been rewritten out before */
6347 if (!linker_section_ptr->written_address_p)
6348 {
6349 linker_section_ptr->written_address_p = true;
6350 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
6351 lsect->section->contents + linker_section_ptr->offset);
6352
6353 if (info->shared)
6354 {
6355 asection *srel = lsect->rel_section;
6356 Elf_Internal_Rela *outrel;
6357 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6358 unsigned int i;
6359
6360 outrel = (Elf_Internal_Rela *) bfd_zmalloc (sizeof (Elf_Internal_Rela)
6361 * bed->s->int_rels_per_ext_rel);
6362 if (outrel == NULL)
6363 {
6364 (*_bfd_error_handler) (_("Error: out of memory"));
6365 return 0;
6366 }
6367
6368 /* We need to generate a relative reloc for the dynamic linker. */
6369 if (!srel)
6370 lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
6371 lsect->rel_name);
6372
6373 BFD_ASSERT (srel != NULL);
6374
6375 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6376 outrel[i].r_offset = (lsect->section->output_section->vma
6377 + lsect->section->output_offset
6378 + linker_section_ptr->offset);
6379 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
6380 outrel[0].r_addend = 0;
6381 elf_swap_reloca_out (output_bfd, outrel,
6382 (((Elf_External_Rela *)
6383 lsect->section->contents)
6384 + elf_section_data (lsect->section)->rel_count));
6385 ++elf_section_data (lsect->section)->rel_count;
6386
6387 free (outrel);
6388 }
6389 }
6390 }
6391
6392 relocation = (lsect->section->output_offset
6393 + linker_section_ptr->offset
6394 - lsect->hole_offset
6395 - lsect->sym_offset);
6396
6397#ifdef DEBUG
6398 fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6399 lsect->name, (long)relocation, (long)relocation);
6400#endif
6401
6402 /* Subtract out the addend, because it will get added back in by the normal
6403 processing. */
6404 return relocation - linker_section_ptr->addend;
6405}
6406
6407
6408/* Garbage collect unused sections. */
6409
6410static boolean elf_gc_mark
6411 PARAMS ((struct bfd_link_info *info, asection *sec,
6412 asection * (*gc_mark_hook)
6413 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
6414 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
6415
6416static boolean elf_gc_sweep
6417 PARAMS ((struct bfd_link_info *info,
6418 boolean (*gc_sweep_hook)
6419 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
6420 const Elf_Internal_Rela *relocs))));
6421
6422static boolean elf_gc_sweep_symbol
6423 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
6424
6425static boolean elf_gc_allocate_got_offsets
6426 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
6427
6428static boolean elf_gc_propagate_vtable_entries_used
6429 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
6430
6431static boolean elf_gc_smash_unused_vtentry_relocs
6432 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
6433
6434/* The mark phase of garbage collection. For a given section, mark
6435 it, and all the sections which define symbols to which it refers. */
6436
6437static boolean
6438elf_gc_mark (info, sec, gc_mark_hook)
6439 struct bfd_link_info *info;
6440 asection *sec;
6441 asection * (*gc_mark_hook)
6442 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
6443 struct elf_link_hash_entry *, Elf_Internal_Sym *));
6444{
6445 boolean ret = true;
6446
6447 sec->gc_mark = 1;
6448
6449 /* Look through the section relocs. */
6450
6451 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
6452 {
6453 Elf_Internal_Rela *relstart, *rel, *relend;
6454 Elf_Internal_Shdr *symtab_hdr;
6455 struct elf_link_hash_entry **sym_hashes;
6456 size_t nlocsyms;
6457 size_t extsymoff;
6458 Elf_External_Sym *locsyms, *freesyms = NULL;
6459 bfd *input_bfd = sec->owner;
6460 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6461
6462 /* GCFIXME: how to arrange so that relocs and symbols are not
6463 reread continually? */
6464
6465 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6466 sym_hashes = elf_sym_hashes (input_bfd);
6467
6468 /* Read the local symbols. */
6469 if (elf_bad_symtab (input_bfd))
6470 {
6471 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6472 extsymoff = 0;
6473 }
6474 else
6475 extsymoff = nlocsyms = symtab_hdr->sh_info;
6476 if (symtab_hdr->contents)
6477 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
6478 else if (nlocsyms == 0)
6479 locsyms = NULL;
6480 else
6481 {
6482 locsyms = freesyms =
6483 bfd_malloc (nlocsyms * sizeof (Elf_External_Sym));
6484 if (freesyms == NULL
6485 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6486 || (bfd_read (locsyms, sizeof (Elf_External_Sym),
6487 nlocsyms, input_bfd)
6488 != nlocsyms * sizeof (Elf_External_Sym)))
6489 {
6490 ret = false;
6491 goto out1;
6492 }
6493 }
6494
6495 /* Read the relocations. */
6496 relstart = (NAME(_bfd_elf,link_read_relocs)
6497 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
6498 info->keep_memory));
6499 if (relstart == NULL)
6500 {
6501 ret = false;
6502 goto out1;
6503 }
6504 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6505
6506 for (rel = relstart; rel < relend; rel++)
6507 {
6508 unsigned long r_symndx;
6509 asection *rsec;
6510 struct elf_link_hash_entry *h;
6511 Elf_Internal_Sym s;
6512
6513 r_symndx = ELF_R_SYM (rel->r_info);
6514 if (r_symndx == 0)
6515 continue;
6516
6517 if (elf_bad_symtab (sec->owner))
6518 {
6519 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6520 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
6521 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
6522 else
6523 {
6524 h = sym_hashes[r_symndx - extsymoff];
6525 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
6526 }
6527 }
6528 else if (r_symndx >= nlocsyms)
6529 {
6530 h = sym_hashes[r_symndx - extsymoff];
6531 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
6532 }
6533 else
6534 {
6535 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
6536 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
6537 }
6538
6539 if (rsec && !rsec->gc_mark)
6540 if (!elf_gc_mark (info, rsec, gc_mark_hook))
6541 {
6542 ret = false;
6543 goto out2;
6544 }
6545 }
6546
6547 out2:
6548 if (!info->keep_memory)
6549 free (relstart);
6550 out1:
6551 if (freesyms)
6552 free (freesyms);
6553 }
6554
6555 return ret;
6556}
6557
6558/* The sweep phase of garbage collection. Remove all garbage sections. */
6559
6560static boolean
6561elf_gc_sweep (info, gc_sweep_hook)
6562 struct bfd_link_info *info;
6563 boolean (*gc_sweep_hook)
6564 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
6565 const Elf_Internal_Rela *relocs));
6566{
6567 bfd *sub;
6568
6569 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6570 {
6571 asection *o;
6572
6573 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
6574 continue;
6575
6576 for (o = sub->sections; o != NULL; o = o->next)
6577 {
6578 /* Keep special sections. Keep .debug sections. */
6579 if ((o->flags & SEC_LINKER_CREATED)
6580 || (o->flags & SEC_DEBUGGING))
6581 o->gc_mark = 1;
6582
6583 if (o->gc_mark)
6584 continue;
6585
6586 /* Skip sweeping sections already excluded. */
6587 if (o->flags & SEC_EXCLUDE)
6588 continue;
6589
6590 /* Since this is early in the link process, it is simple
6591 to remove a section from the output. */
6592 o->flags |= SEC_EXCLUDE;
6593
6594 /* But we also have to update some of the relocation
6595 info we collected before. */
6596 if (gc_sweep_hook
6597 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
6598 {
6599 Elf_Internal_Rela *internal_relocs;
6600 boolean r;
6601
6602 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6603 (o->owner, o, NULL, NULL, info->keep_memory));
6604 if (internal_relocs == NULL)
6605 return false;
6606
6607 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
6608
6609 if (!info->keep_memory)
6610 free (internal_relocs);
6611
6612 if (!r)
6613 return false;
6614 }
6615 }
6616 }
6617
6618 /* Remove the symbols that were in the swept sections from the dynamic
6619 symbol table. GCFIXME: Anyone know how to get them out of the
6620 static symbol table as well? */
6621 {
6622 int i = 0;
6623
6624 elf_link_hash_traverse (elf_hash_table (info),
6625 elf_gc_sweep_symbol,
6626 (PTR) &i);
6627
6628 elf_hash_table (info)->dynsymcount = i;
6629 }
6630
6631 return true;
6632}
6633
6634/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6635
6636static boolean
6637elf_gc_sweep_symbol (h, idxptr)
6638 struct elf_link_hash_entry *h;
6639 PTR idxptr;
6640{
6641 int *idx = (int *) idxptr;
6642
6643 if (h->dynindx != -1
6644 && ((h->root.type != bfd_link_hash_defined
6645 && h->root.type != bfd_link_hash_defweak)
6646 || h->root.u.def.section->gc_mark))
6647 h->dynindx = (*idx)++;
6648
6649 return true;
6650}
6651
6652/* Propogate collected vtable information. This is called through
6653 elf_link_hash_traverse. */
6654
6655static boolean
6656elf_gc_propagate_vtable_entries_used (h, okp)
6657 struct elf_link_hash_entry *h;
6658 PTR okp;
6659{
6660 /* Those that are not vtables. */
6661 if (h->vtable_parent == NULL)
6662 return true;
6663
6664 /* Those vtables that do not have parents, we cannot merge. */
6665 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
6666 return true;
6667
6668 /* If we've already been done, exit. */
6669 if (h->vtable_entries_used && h->vtable_entries_used[-1])
6670 return true;
6671
6672 /* Make sure the parent's table is up to date. */
6673 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
6674
6675 if (h->vtable_entries_used == NULL)
6676 {
6677 /* None of this table's entries were referenced. Re-use the
6678 parent's table. */
6679 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
6680 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
6681 }
6682 else
6683 {
6684 size_t n;
6685 boolean *cu, *pu;
6686
6687 /* Or the parent's entries into ours. */
6688 cu = h->vtable_entries_used;
6689 cu[-1] = true;
6690 pu = h->vtable_parent->vtable_entries_used;
6691 if (pu != NULL)
6692 {
6693 n = h->vtable_parent->vtable_entries_size / FILE_ALIGN;
6694 while (--n != 0)
6695 {
6696 if (*pu) *cu = true;
6697 pu++, cu++;
6698 }
6699 }
6700 }
6701
6702 return true;
6703}
6704
6705static boolean
6706elf_gc_smash_unused_vtentry_relocs (h, okp)
6707 struct elf_link_hash_entry *h;
6708 PTR okp;
6709{
6710 asection *sec;
6711 bfd_vma hstart, hend;
6712 Elf_Internal_Rela *relstart, *relend, *rel;
6713 struct elf_backend_data *bed;
6714
6715 /* Take care of both those symbols that do not describe vtables as
6716 well as those that are not loaded. */
6717 if (h->vtable_parent == NULL)
6718 return true;
6719
6720 BFD_ASSERT (h->root.type == bfd_link_hash_defined
6721 || h->root.type == bfd_link_hash_defweak);
6722
6723 sec = h->root.u.def.section;
6724 hstart = h->root.u.def.value;
6725 hend = hstart + h->size;
6726
6727 relstart = (NAME(_bfd_elf,link_read_relocs)
6728 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
6729 if (!relstart)
6730 return *(boolean *)okp = false;
6731 bed = get_elf_backend_data (sec->owner);
6732 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6733
6734 for (rel = relstart; rel < relend; ++rel)
6735 if (rel->r_offset >= hstart && rel->r_offset < hend)
6736 {
6737 /* If the entry is in use, do nothing. */
6738 if (h->vtable_entries_used
6739 && (rel->r_offset - hstart) < h->vtable_entries_size)
6740 {
6741 bfd_vma entry = (rel->r_offset - hstart) / FILE_ALIGN;
6742 if (h->vtable_entries_used[entry])
6743 continue;
6744 }
6745 /* Otherwise, kill it. */
6746 rel->r_offset = rel->r_info = rel->r_addend = 0;
6747 }
6748
6749 return true;
6750}
6751
6752/* Do mark and sweep of unused sections. */
6753
6754boolean
6755elf_gc_sections (abfd, info)
6756 bfd *abfd;
6757 struct bfd_link_info *info;
6758{
6759 boolean ok = true;
6760 bfd *sub;
6761 asection * (*gc_mark_hook)
6762 PARAMS ((bfd *abfd, struct bfd_link_info *, Elf_Internal_Rela *,
6763 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
6764
6765 if (!get_elf_backend_data (abfd)->can_gc_sections
6766 || info->relocateable || info->emitrelocations
6767 || elf_hash_table (info)->dynamic_sections_created)
6768 return true;
6769
6770 /* Apply transitive closure to the vtable entry usage info. */
6771 elf_link_hash_traverse (elf_hash_table (info),
6772 elf_gc_propagate_vtable_entries_used,
6773 (PTR) &ok);
6774 if (!ok)
6775 return false;
6776
6777 /* Kill the vtable relocations that were not used. */
6778 elf_link_hash_traverse (elf_hash_table (info),
6779 elf_gc_smash_unused_vtentry_relocs,
6780 (PTR) &ok);
6781 if (!ok)
6782 return false;
6783
6784 /* Grovel through relocs to find out who stays ... */
6785
6786 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
6787 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6788 {
6789 asection *o;
6790
6791 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
6792 continue;
6793
6794 for (o = sub->sections; o != NULL; o = o->next)
6795 {
6796 if (o->flags & SEC_KEEP)
6797 if (!elf_gc_mark (info, o, gc_mark_hook))
6798 return false;
6799 }
6800 }
6801
6802 /* ... and mark SEC_EXCLUDE for those that go. */
6803 if (!elf_gc_sweep(info, get_elf_backend_data (abfd)->gc_sweep_hook))
6804 return false;
6805
6806 return true;
6807}
6808
6809
6810/* Called from check_relocs to record the existance of a VTINHERIT reloc. */
6811
6812boolean
6813elf_gc_record_vtinherit (abfd, sec, h, offset)
6814 bfd *abfd;
6815 asection *sec;
6816 struct elf_link_hash_entry *h;
6817 bfd_vma offset;
6818{
6819 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6820 struct elf_link_hash_entry **search, *child;
6821 bfd_size_type extsymcount;
6822
6823 /* The sh_info field of the symtab header tells us where the
6824 external symbols start. We don't care about the local symbols at
6825 this point. */
6826 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
6827 if (!elf_bad_symtab (abfd))
6828 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
6829
6830 sym_hashes = elf_sym_hashes (abfd);
6831 sym_hashes_end = sym_hashes + extsymcount;
6832
6833 /* Hunt down the child symbol, which is in this section at the same
6834 offset as the relocation. */
6835 for (search = sym_hashes; search != sym_hashes_end; ++search)
6836 {
6837 if ((child = *search) != NULL
6838 && (child->root.type == bfd_link_hash_defined
6839 || child->root.type == bfd_link_hash_defweak)
6840 && child->root.u.def.section == sec
6841 && child->root.u.def.value == offset)
6842 goto win;
6843 }
6844
6845 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
6846 bfd_get_filename (abfd), sec->name,
6847 (unsigned long)offset);
6848 bfd_set_error (bfd_error_invalid_operation);
6849 return false;
6850
6851win:
6852 if (!h)
6853 {
6854 /* This *should* only be the absolute section. It could potentially
6855 be that someone has defined a non-global vtable though, which
6856 would be bad. It isn't worth paging in the local symbols to be
6857 sure though; that case should simply be handled by the assembler. */
6858
6859 child->vtable_parent = (struct elf_link_hash_entry *) -1;
6860 }
6861 else
6862 child->vtable_parent = h;
6863
6864 return true;
6865}
6866
6867/* Called from check_relocs to record the existance of a VTENTRY reloc. */
6868
6869boolean
6870elf_gc_record_vtentry (abfd, sec, h, addend)
6871 bfd *abfd ATTRIBUTE_UNUSED;
6872 asection *sec ATTRIBUTE_UNUSED;
6873 struct elf_link_hash_entry *h;
6874 bfd_vma addend;
6875{
6876 if (addend >= h->vtable_entries_size)
6877 {
6878 size_t size, bytes;
6879 boolean *ptr = h->vtable_entries_used;
6880
6881 /* While the symbol is undefined, we have to be prepared to handle
6882 a zero size. */
6883 if (h->root.type == bfd_link_hash_undefined)
6884 size = addend;
6885 else
6886 {
6887 size = h->size;
6888 if (size < addend)
6889 {
6890 /* Oops! We've got a reference past the defined end of
6891 the table. This is probably a bug -- shall we warn? */
6892 size = addend;
6893 }
6894 }
6895
6896 /* Allocate one extra entry for use as a "done" flag for the
6897 consolidation pass. */
6898 bytes = (size / FILE_ALIGN + 1) * sizeof (boolean);
6899
6900 if (ptr)
6901 {
6902 ptr = bfd_realloc (ptr - 1, bytes);
6903
6904 if (ptr != NULL)
6905 {
6906 size_t oldbytes;
6907
6908 oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof (boolean);
6909 memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes);
6910 }
6911 }
6912 else
6913 ptr = bfd_zmalloc (bytes);
6914
6915 if (ptr == NULL)
6916 return false;
6917
6918 /* And arrange for that done flag to be at index -1. */
6919 h->vtable_entries_used = ptr + 1;
6920 h->vtable_entries_size = size;
6921 }
6922
6923 h->vtable_entries_used[addend / FILE_ALIGN] = true;
6924
6925 return true;
6926}
6927
6928/* And an accompanying bit to work out final got entry offsets once
6929 we're done. Should be called from final_link. */
6930
6931boolean
6932elf_gc_common_finalize_got_offsets (abfd, info)
6933 bfd *abfd;
6934 struct bfd_link_info *info;
6935{
6936 bfd *i;
6937 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6938 bfd_vma gotoff;
6939
6940 /* The GOT offset is relative to the .got section, but the GOT header is
6941 put into the .got.plt section, if the backend uses it. */
6942 if (bed->want_got_plt)
6943 gotoff = 0;
6944 else
6945 gotoff = bed->got_header_size;
6946
6947 /* Do the local .got entries first. */
6948 for (i = info->input_bfds; i; i = i->link_next)
6949 {
6950 bfd_signed_vma *local_got;
6951 bfd_size_type j, locsymcount;
6952 Elf_Internal_Shdr *symtab_hdr;
6953
6954 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
6955 continue;
6956
6957 local_got = elf_local_got_refcounts (i);
6958 if (!local_got)
6959 continue;
6960
6961 symtab_hdr = &elf_tdata (i)->symtab_hdr;
6962 if (elf_bad_symtab (i))
6963 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6964 else
6965 locsymcount = symtab_hdr->sh_info;
6966
6967 for (j = 0; j < locsymcount; ++j)
6968 {
6969 if (local_got[j] > 0)
6970 {
6971 local_got[j] = gotoff;
6972 gotoff += ARCH_SIZE / 8;
6973 }
6974 else
6975 local_got[j] = (bfd_vma) -1;
6976 }
6977 }
6978
6979 /* Then the global .got entries. .plt refcounts are handled by
6980 adjust_dynamic_symbol */
6981 elf_link_hash_traverse (elf_hash_table (info),
6982 elf_gc_allocate_got_offsets,
6983 (PTR) &gotoff);
6984 return true;
6985}
6986
6987/* We need a special top-level link routine to convert got reference counts
6988 to real got offsets. */
6989
6990static boolean
6991elf_gc_allocate_got_offsets (h, offarg)
6992 struct elf_link_hash_entry *h;
6993 PTR offarg;
6994{
6995 bfd_vma *off = (bfd_vma *) offarg;
6996
6997 if (h->got.refcount > 0)
6998 {
6999 h->got.offset = off[0];
7000 off[0] += ARCH_SIZE / 8;
7001 }
7002 else
7003 h->got.offset = (bfd_vma) -1;
7004
7005 return true;
7006}
7007
7008/* Many folk need no more in the way of final link than this, once
7009 got entry reference counting is enabled. */
7010
7011boolean
7012elf_gc_common_final_link (abfd, info)
7013 bfd *abfd;
7014 struct bfd_link_info *info;
7015{
7016 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7017 return false;
7018
7019 /* Invoke the regular ELF backend linker to do all the work. */
7020 return elf_bfd_final_link (abfd, info);
7021}
7022
7023/* This function will be called though elf_link_hash_traverse to store
7024 all hash value of the exported symbols in an array. */
7025
7026static boolean
7027elf_collect_hash_codes (h, data)
7028 struct elf_link_hash_entry *h;
7029 PTR data;
7030{
7031 unsigned long **valuep = (unsigned long **) data;
7032 const char *name;
7033 char *p;
7034 unsigned long ha;
7035 char *alc = NULL;
7036
7037 /* Ignore indirect symbols. These are added by the versioning code. */
7038 if (h->dynindx == -1)
7039 return true;
7040
7041 name = h->root.root.string;
7042 p = strchr (name, ELF_VER_CHR);
7043 if (p != NULL)
7044 {
7045 alc = bfd_malloc (p - name + 1);
7046 memcpy (alc, name, p - name);
7047 alc[p - name] = '\0';
7048 name = alc;
7049 }
7050
7051 /* Compute the hash value. */
7052 ha = bfd_elf_hash (name);
7053
7054 /* Store the found hash value in the array given as the argument. */
7055 *(*valuep)++ = ha;
7056
7057 /* And store it in the struct so that we can put it in the hash table
7058 later. */
7059 h->elf_hash_value = ha;
7060
7061 if (alc != NULL)
7062 free (alc);
7063
7064 return true;
7065}
Note: See TracBrowser for help on using the repository browser.