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