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