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