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