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