include/
[binutils.git] / bfd / elflink.c
blob43379c71a6e6a1c7beb9676e0c15e28090b627d4
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
37 asection *sec,
38 const char *name)
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 if (h != NULL)
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
54 bh = &h->root;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 sec, 0, NULL, FALSE,
57 get_elf_backend_data (abfd)->collect,
58 &bh))
59 return NULL;
60 h = (struct elf_link_hash_entry *) bh;
61 h->def_regular = 1;
62 h->type = STT_OBJECT;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
67 return h;
70 bfd_boolean
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
73 flagword flags;
74 asection *s;
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
77 int ptralign;
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
82 return TRUE;
84 switch (bed->s->arch_size)
86 case 32:
87 ptralign = 2;
88 break;
90 case 64:
91 ptralign = 3;
92 break;
94 default:
95 bfd_set_error (bfd_error_bad_value);
96 return FALSE;
99 flags = bed->dynamic_sec_flags;
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
102 if (s == NULL
103 || !bfd_set_section_alignment (abfd, s, ptralign))
104 return FALSE;
106 if (bed->want_got_plt)
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
109 if (s == NULL
110 || !bfd_set_section_alignment (abfd, s, ptralign))
111 return FALSE;
114 if (bed->want_got_sym)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
122 if (h == NULL)
123 return FALSE;
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
129 return TRUE;
132 /* Create a strtab to hold the dynamic symbol names. */
133 static bfd_boolean
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
136 struct elf_link_hash_table *hash_table;
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
142 if (hash_table->dynstr == NULL)
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
146 return FALSE;
148 return TRUE;
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
158 bfd_boolean
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
161 flagword flags;
162 register asection *s;
163 const struct elf_backend_data *bed;
165 if (! is_elf_hash_table (info->hash))
166 return FALSE;
168 if (elf_hash_table (info)->dynamic_sections_created)
169 return TRUE;
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 return FALSE;
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
177 flags = bed->dynamic_sec_flags;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
185 if (s == NULL)
186 return FALSE;
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
193 if (s == NULL
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 return FALSE;
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
199 if (s == NULL
200 || ! bfd_set_section_alignment (abfd, s, 1))
201 return FALSE;
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
205 if (s == NULL
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
211 if (s == NULL
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
213 return FALSE;
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
217 if (s == NULL)
218 return FALSE;
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
221 if (s == NULL
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 return FALSE;
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
232 return FALSE;
234 if (info->emit_hash)
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
243 if (info->emit_gnu_hash)
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 else
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
263 return FALSE;
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
267 return TRUE;
270 /* Create dynamic sections when linking against a dynamic object. */
272 bfd_boolean
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
277 asection *s;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
284 pltflags = flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
290 else
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
296 if (s == NULL
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
298 return FALSE;
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 .plt section. */
302 if (bed->want_plt_sym)
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
307 if (h == NULL)
308 return FALSE;
311 s = bfd_make_section_with_flags (abfd,
312 (bed->rela_plts_and_copies_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
315 if (s == NULL
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
317 return FALSE;
319 if (! _bfd_elf_create_got_section (abfd, info))
320 return FALSE;
322 if (bed->want_dynbss)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 (SEC_ALLOC
332 | SEC_LINKER_CREATED));
333 if (s == NULL)
334 return FALSE;
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
346 copy relocs. */
347 if (! info->shared)
349 s = bfd_make_section_with_flags (abfd,
350 (bed->rela_plts_and_copies_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
353 if (s == NULL
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
355 return FALSE;
359 return TRUE;
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
368 one. */
370 bfd_boolean
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
374 if (h->dynindx == -1)
376 struct elf_strtab_hash *dynstr;
377 char *p;
378 const char *name;
379 bfd_size_type indx;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
387 case STV_INTERNAL:
388 case STV_HIDDEN:
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
392 h->forced_local = 1;
393 if (!elf_hash_table (info)->is_relocatable_executable)
394 return TRUE;
397 default:
398 break;
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
404 dynstr = elf_hash_table (info)->dynstr;
405 if (dynstr == NULL)
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
409 if (dynstr == NULL)
410 return FALSE;
413 /* We don't put any version information in the dynamic string
414 table. */
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
417 if (p != NULL)
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
423 *p = 0;
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
427 if (p != NULL)
428 *p = ELF_VER_CHR;
430 if (indx == (bfd_size_type) -1)
431 return FALSE;
432 h->dynstr_index = indx;
435 return TRUE;
438 /* Mark a symbol dynamic. */
440 void
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
449 return;
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
453 || (sym != NULL
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 || (d != NULL
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
458 h->dynamic = 1;
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
464 bfd_boolean
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
467 const char *name,
468 bfd_boolean provide,
469 bfd_boolean hidden)
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
475 if (!is_elf_hash_table (info->hash))
476 return TRUE;
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
480 if (h == NULL)
481 return provide;
483 switch (h->root.type)
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
488 break;
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
497 break;
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
500 h->non_elf = 0;
501 break;
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
506 hv = h;
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
511 later. */
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
516 break;
517 case bfd_link_hash_warning:
518 abort ();
519 break;
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
526 if (provide
527 && h->def_dynamic
528 && !h->def_regular)
529 h->root.type = bfd_link_hash_undefined;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
535 if (!provide
536 && h->def_dynamic
537 && !h->def_regular)
538 h->verinfo.verdef = NULL;
540 h->def_regular = 1;
542 if (provide && hidden)
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
551 and executables. */
552 if (!info->relocatable
553 && h->dynindx != -1
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
556 h->forced_local = 1;
558 if ((h->def_dynamic
559 || h->ref_dynamic
560 || info->shared
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
562 && h->dynindx == -1)
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
565 return FALSE;
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
574 return FALSE;
578 return TRUE;
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
587 bfd *input_bfd,
588 long input_indx)
590 bfd_size_type amt;
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
595 char *name;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
599 if (! is_elf_hash_table (info->hash))
600 return 0;
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
605 return 1;
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
609 if (entry == NULL)
610 return 0;
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
616 bfd_release (input_bfd, entry);
617 return 0;
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && entry->isym.st_shndx < SHN_LORESERVE)
623 asection *s;
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
631 return 2;
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
639 dynstr = elf_hash_table (info)->dynstr;
640 if (dynstr == NULL)
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
644 if (dynstr == NULL)
645 return 0;
648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
649 if (dynstr_index == (unsigned long) -1)
650 return 0;
651 entry->isym.st_name = dynstr_index;
653 eht = elf_hash_table (info);
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
659 eht->dynsymcount++;
661 /* Whatever binding the symbol had before, it's now local. */
662 entry->isym.st_info
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
665 /* The dynindx will be set at the end of size_dynamic_sections. */
667 return 1;
670 /* Return the dynindex of a local dynamic symbol. */
672 long
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
674 bfd *input_bfd,
675 long input_indx)
677 struct elf_link_local_dynamic_entry *e;
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
681 return e->dynindx;
682 return -1;
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
689 static bfd_boolean
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
691 void *data)
693 size_t *count = data;
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
698 if (h->forced_local)
699 return TRUE;
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
704 return TRUE;
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
711 static bfd_boolean
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
713 void *data)
715 size_t *count = data;
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720 if (!h->forced_local)
721 return TRUE;
723 if (h->dynindx != -1)
724 h->dynindx = ++(*count);
726 return TRUE;
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
731 bfd_boolean
732 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
734 asection *p)
736 struct elf_link_hash_table *htab;
738 switch (elf_section_data (p)->this_hdr.sh_type)
740 case SHT_PROGBITS:
741 case SHT_NOBITS:
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
744 case SHT_NULL:
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
747 return FALSE;
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
756 asection *ip;
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
762 return TRUE;
764 return FALSE;
766 /* There shouldn't be section relative relocations
767 against any other section. */
768 default:
769 return TRUE;
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
777 symbols. */
779 static unsigned long
780 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
784 unsigned long dynsymcount = 0;
786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
789 asection *p;
790 for (p = output_bfd->sections; p ; p = p->next)
791 if ((p->flags & SEC_EXCLUDE) == 0
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
795 else
796 elf_section_data (p)->dynindx = 0;
798 *section_sym_count = dynsymcount;
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
802 &dynsymcount);
804 if (elf_hash_table (info)->dynlocal)
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
813 &dynsymcount);
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
819 ++dynsymcount;
821 elf_hash_table (info)->dynsymcount = dynsymcount;
822 return dynsymcount;
825 /* This function is called when we want to define a new symbol. It
826 handles the various cases which arise when we find a definition in
827 a dynamic object, or when there is already a definition in a
828 dynamic object. The new symbol is described by NAME, SYM, PSEC,
829 and PVALUE. We set SYM_HASH to the hash table entry. We set
830 OVERRIDE if the old symbol is overriding a new definition. We set
831 TYPE_CHANGE_OK if it is OK for the type to change. We set
832 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
833 change, we mean that we shouldn't warn if the type or size does
834 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
835 object is overridden by a regular object. */
837 bfd_boolean
838 _bfd_elf_merge_symbol (bfd *abfd,
839 struct bfd_link_info *info,
840 const char *name,
841 Elf_Internal_Sym *sym,
842 asection **psec,
843 bfd_vma *pvalue,
844 unsigned int *pold_alignment,
845 struct elf_link_hash_entry **sym_hash,
846 bfd_boolean *skip,
847 bfd_boolean *override,
848 bfd_boolean *type_change_ok,
849 bfd_boolean *size_change_ok)
851 asection *sec, *oldsec;
852 struct elf_link_hash_entry *h;
853 struct elf_link_hash_entry *flip;
854 int bind;
855 bfd *oldbfd;
856 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
857 bfd_boolean newweak, oldweak, newfunc, oldfunc;
858 const struct elf_backend_data *bed;
860 *skip = FALSE;
861 *override = FALSE;
863 sec = *psec;
864 bind = ELF_ST_BIND (sym->st_info);
866 /* Silently discard TLS symbols from --just-syms. There's no way to
867 combine a static TLS block with a new TLS block for this executable. */
868 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
869 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
871 *skip = TRUE;
872 return TRUE;
875 if (! bfd_is_und_section (sec))
876 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
877 else
878 h = ((struct elf_link_hash_entry *)
879 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
880 if (h == NULL)
881 return FALSE;
882 *sym_hash = h;
884 bed = get_elf_backend_data (abfd);
886 /* This code is for coping with dynamic objects, and is only useful
887 if we are doing an ELF link. */
888 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
889 return TRUE;
891 /* For merging, we only care about real symbols. */
893 while (h->root.type == bfd_link_hash_indirect
894 || h->root.type == bfd_link_hash_warning)
895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
897 /* We have to check it for every instance since the first few may be
898 refereences and not all compilers emit symbol type for undefined
899 symbols. */
900 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
902 /* If we just created the symbol, mark it as being an ELF symbol.
903 Other than that, there is nothing to do--there is no merge issue
904 with a newly defined symbol--so we just return. */
906 if (h->root.type == bfd_link_hash_new)
908 h->non_elf = 0;
909 return TRUE;
912 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
913 existing symbol. */
915 switch (h->root.type)
917 default:
918 oldbfd = NULL;
919 oldsec = NULL;
920 break;
922 case bfd_link_hash_undefined:
923 case bfd_link_hash_undefweak:
924 oldbfd = h->root.u.undef.abfd;
925 oldsec = NULL;
926 break;
928 case bfd_link_hash_defined:
929 case bfd_link_hash_defweak:
930 oldbfd = h->root.u.def.section->owner;
931 oldsec = h->root.u.def.section;
932 break;
934 case bfd_link_hash_common:
935 oldbfd = h->root.u.c.p->section->owner;
936 oldsec = h->root.u.c.p->section;
937 break;
940 /* In cases involving weak versioned symbols, we may wind up trying
941 to merge a symbol with itself. Catch that here, to avoid the
942 confusion that results if we try to override a symbol with
943 itself. The additional tests catch cases like
944 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
945 dynamic object, which we do want to handle here. */
946 if (abfd == oldbfd
947 && ((abfd->flags & DYNAMIC) == 0
948 || !h->def_regular))
949 return TRUE;
951 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
952 respectively, is from a dynamic object. */
954 newdyn = (abfd->flags & DYNAMIC) != 0;
956 olddyn = FALSE;
957 if (oldbfd != NULL)
958 olddyn = (oldbfd->flags & DYNAMIC) != 0;
959 else if (oldsec != NULL)
961 /* This handles the special SHN_MIPS_{TEXT,DATA} section
962 indices used by MIPS ELF. */
963 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
966 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
967 respectively, appear to be a definition rather than reference. */
969 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
971 olddef = (h->root.type != bfd_link_hash_undefined
972 && h->root.type != bfd_link_hash_undefweak
973 && h->root.type != bfd_link_hash_common);
975 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
976 respectively, appear to be a function. */
978 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
979 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
981 oldfunc = (h->type != STT_NOTYPE
982 && bed->is_function_type (h->type));
984 /* When we try to create a default indirect symbol from the dynamic
985 definition with the default version, we skip it if its type and
986 the type of existing regular definition mismatch. We only do it
987 if the existing regular definition won't be dynamic. */
988 if (pold_alignment == NULL
989 && !info->shared
990 && !info->export_dynamic
991 && !h->ref_dynamic
992 && newdyn
993 && newdef
994 && !olddyn
995 && (olddef || h->root.type == bfd_link_hash_common)
996 && ELF_ST_TYPE (sym->st_info) != h->type
997 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
998 && h->type != STT_NOTYPE
999 && !(newfunc && oldfunc))
1001 *skip = TRUE;
1002 return TRUE;
1005 /* Check TLS symbol. We don't check undefined symbol introduced by
1006 "ld -u". */
1007 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1008 && ELF_ST_TYPE (sym->st_info) != h->type
1009 && oldbfd != NULL)
1011 bfd *ntbfd, *tbfd;
1012 bfd_boolean ntdef, tdef;
1013 asection *ntsec, *tsec;
1015 if (h->type == STT_TLS)
1017 ntbfd = abfd;
1018 ntsec = sec;
1019 ntdef = newdef;
1020 tbfd = oldbfd;
1021 tsec = oldsec;
1022 tdef = olddef;
1024 else
1026 ntbfd = oldbfd;
1027 ntsec = oldsec;
1028 ntdef = olddef;
1029 tbfd = abfd;
1030 tsec = sec;
1031 tdef = newdef;
1034 if (tdef && ntdef)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1037 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1038 else if (!tdef && !ntdef)
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1041 tbfd, ntbfd, h->root.root.string);
1042 else if (tdef)
1043 (*_bfd_error_handler)
1044 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1045 tbfd, tsec, ntbfd, h->root.root.string);
1046 else
1047 (*_bfd_error_handler)
1048 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1049 tbfd, ntbfd, ntsec, h->root.root.string);
1051 bfd_set_error (bfd_error_bad_value);
1052 return FALSE;
1055 /* We need to remember if a symbol has a definition in a dynamic
1056 object or is weak in all dynamic objects. Internal and hidden
1057 visibility will make it unavailable to dynamic objects. */
1058 if (newdyn && !h->dynamic_def)
1060 if (!bfd_is_und_section (sec))
1061 h->dynamic_def = 1;
1062 else
1064 /* Check if this symbol is weak in all dynamic objects. If it
1065 is the first time we see it in a dynamic object, we mark
1066 if it is weak. Otherwise, we clear it. */
1067 if (!h->ref_dynamic)
1069 if (bind == STB_WEAK)
1070 h->dynamic_weak = 1;
1072 else if (bind != STB_WEAK)
1073 h->dynamic_weak = 0;
1077 /* If the old symbol has non-default visibility, we ignore the new
1078 definition from a dynamic object. */
1079 if (newdyn
1080 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1081 && !bfd_is_und_section (sec))
1083 *skip = TRUE;
1084 /* Make sure this symbol is dynamic. */
1085 h->ref_dynamic = 1;
1086 /* A protected symbol has external availability. Make sure it is
1087 recorded as dynamic.
1089 FIXME: Should we check type and size for protected symbol? */
1090 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1091 return bfd_elf_link_record_dynamic_symbol (info, h);
1092 else
1093 return TRUE;
1095 else if (!newdyn
1096 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1097 && h->def_dynamic)
1099 /* If the new symbol with non-default visibility comes from a
1100 relocatable file and the old definition comes from a dynamic
1101 object, we remove the old definition. */
1102 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1104 /* Handle the case where the old dynamic definition is
1105 default versioned. We need to copy the symbol info from
1106 the symbol with default version to the normal one if it
1107 was referenced before. */
1108 if (h->ref_regular)
1110 const struct elf_backend_data *bed
1111 = get_elf_backend_data (abfd);
1112 struct elf_link_hash_entry *vh = *sym_hash;
1113 vh->root.type = h->root.type;
1114 h->root.type = bfd_link_hash_indirect;
1115 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1116 /* Protected symbols will override the dynamic definition
1117 with default version. */
1118 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1120 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1121 vh->dynamic_def = 1;
1122 vh->ref_dynamic = 1;
1124 else
1126 h->root.type = vh->root.type;
1127 vh->ref_dynamic = 0;
1128 /* We have to hide it here since it was made dynamic
1129 global with extra bits when the symbol info was
1130 copied from the old dynamic definition. */
1131 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1133 h = vh;
1135 else
1136 h = *sym_hash;
1139 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1140 && bfd_is_und_section (sec))
1142 /* If the new symbol is undefined and the old symbol was
1143 also undefined before, we need to make sure
1144 _bfd_generic_link_add_one_symbol doesn't mess
1145 up the linker hash table undefs list. Since the old
1146 definition came from a dynamic object, it is still on the
1147 undefs list. */
1148 h->root.type = bfd_link_hash_undefined;
1149 h->root.u.undef.abfd = abfd;
1151 else
1153 h->root.type = bfd_link_hash_new;
1154 h->root.u.undef.abfd = NULL;
1157 if (h->def_dynamic)
1159 h->def_dynamic = 0;
1160 h->ref_dynamic = 1;
1161 h->dynamic_def = 1;
1163 /* FIXME: Should we check type and size for protected symbol? */
1164 h->size = 0;
1165 h->type = 0;
1166 return TRUE;
1169 /* Differentiate strong and weak symbols. */
1170 newweak = bind == STB_WEAK;
1171 oldweak = (h->root.type == bfd_link_hash_defweak
1172 || h->root.type == bfd_link_hash_undefweak);
1174 /* If a new weak symbol definition comes from a regular file and the
1175 old symbol comes from a dynamic library, we treat the new one as
1176 strong. Similarly, an old weak symbol definition from a regular
1177 file is treated as strong when the new symbol comes from a dynamic
1178 library. Further, an old weak symbol from a dynamic library is
1179 treated as strong if the new symbol is from a dynamic library.
1180 This reflects the way glibc's ld.so works.
1182 Do this before setting *type_change_ok or *size_change_ok so that
1183 we warn properly when dynamic library symbols are overridden. */
1185 if (newdef && !newdyn && olddyn)
1186 newweak = FALSE;
1187 if (olddef && newdyn)
1188 oldweak = FALSE;
1190 /* Allow changes between different types of funciton symbol. */
1191 if (newfunc && oldfunc)
1192 *type_change_ok = TRUE;
1194 /* It's OK to change the type if either the existing symbol or the
1195 new symbol is weak. A type change is also OK if the old symbol
1196 is undefined and the new symbol is defined. */
1198 if (oldweak
1199 || newweak
1200 || (newdef
1201 && h->root.type == bfd_link_hash_undefined))
1202 *type_change_ok = TRUE;
1204 /* It's OK to change the size if either the existing symbol or the
1205 new symbol is weak, or if the old symbol is undefined. */
1207 if (*type_change_ok
1208 || h->root.type == bfd_link_hash_undefined)
1209 *size_change_ok = TRUE;
1211 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1212 symbol, respectively, appears to be a common symbol in a dynamic
1213 object. If a symbol appears in an uninitialized section, and is
1214 not weak, and is not a function, then it may be a common symbol
1215 which was resolved when the dynamic object was created. We want
1216 to treat such symbols specially, because they raise special
1217 considerations when setting the symbol size: if the symbol
1218 appears as a common symbol in a regular object, and the size in
1219 the regular object is larger, we must make sure that we use the
1220 larger size. This problematic case can always be avoided in C,
1221 but it must be handled correctly when using Fortran shared
1222 libraries.
1224 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1225 likewise for OLDDYNCOMMON and OLDDEF.
1227 Note that this test is just a heuristic, and that it is quite
1228 possible to have an uninitialized symbol in a shared object which
1229 is really a definition, rather than a common symbol. This could
1230 lead to some minor confusion when the symbol really is a common
1231 symbol in some regular object. However, I think it will be
1232 harmless. */
1234 if (newdyn
1235 && newdef
1236 && !newweak
1237 && (sec->flags & SEC_ALLOC) != 0
1238 && (sec->flags & SEC_LOAD) == 0
1239 && sym->st_size > 0
1240 && !newfunc)
1241 newdyncommon = TRUE;
1242 else
1243 newdyncommon = FALSE;
1245 if (olddyn
1246 && olddef
1247 && h->root.type == bfd_link_hash_defined
1248 && h->def_dynamic
1249 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1250 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1251 && h->size > 0
1252 && !oldfunc)
1253 olddyncommon = TRUE;
1254 else
1255 olddyncommon = FALSE;
1257 /* We now know everything about the old and new symbols. We ask the
1258 backend to check if we can merge them. */
1259 if (bed->merge_symbol
1260 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1261 pold_alignment, skip, override,
1262 type_change_ok, size_change_ok,
1263 &newdyn, &newdef, &newdyncommon, &newweak,
1264 abfd, &sec,
1265 &olddyn, &olddef, &olddyncommon, &oldweak,
1266 oldbfd, &oldsec))
1267 return FALSE;
1269 /* If both the old and the new symbols look like common symbols in a
1270 dynamic object, set the size of the symbol to the larger of the
1271 two. */
1273 if (olddyncommon
1274 && newdyncommon
1275 && sym->st_size != h->size)
1277 /* Since we think we have two common symbols, issue a multiple
1278 common warning if desired. Note that we only warn if the
1279 size is different. If the size is the same, we simply let
1280 the old symbol override the new one as normally happens with
1281 symbols defined in dynamic objects. */
1283 if (! ((*info->callbacks->multiple_common)
1284 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1285 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1286 return FALSE;
1288 if (sym->st_size > h->size)
1289 h->size = sym->st_size;
1291 *size_change_ok = TRUE;
1294 /* If we are looking at a dynamic object, and we have found a
1295 definition, we need to see if the symbol was already defined by
1296 some other object. If so, we want to use the existing
1297 definition, and we do not want to report a multiple symbol
1298 definition error; we do this by clobbering *PSEC to be
1299 bfd_und_section_ptr.
1301 We treat a common symbol as a definition if the symbol in the
1302 shared library is a function, since common symbols always
1303 represent variables; this can cause confusion in principle, but
1304 any such confusion would seem to indicate an erroneous program or
1305 shared library. We also permit a common symbol in a regular
1306 object to override a weak symbol in a shared object. */
1308 if (newdyn
1309 && newdef
1310 && (olddef
1311 || (h->root.type == bfd_link_hash_common
1312 && (newweak || newfunc))))
1314 *override = TRUE;
1315 newdef = FALSE;
1316 newdyncommon = FALSE;
1318 *psec = sec = bfd_und_section_ptr;
1319 *size_change_ok = TRUE;
1321 /* If we get here when the old symbol is a common symbol, then
1322 we are explicitly letting it override a weak symbol or
1323 function in a dynamic object, and we don't want to warn about
1324 a type change. If the old symbol is a defined symbol, a type
1325 change warning may still be appropriate. */
1327 if (h->root.type == bfd_link_hash_common)
1328 *type_change_ok = TRUE;
1331 /* Handle the special case of an old common symbol merging with a
1332 new symbol which looks like a common symbol in a shared object.
1333 We change *PSEC and *PVALUE to make the new symbol look like a
1334 common symbol, and let _bfd_generic_link_add_one_symbol do the
1335 right thing. */
1337 if (newdyncommon
1338 && h->root.type == bfd_link_hash_common)
1340 *override = TRUE;
1341 newdef = FALSE;
1342 newdyncommon = FALSE;
1343 *pvalue = sym->st_size;
1344 *psec = sec = bed->common_section (oldsec);
1345 *size_change_ok = TRUE;
1348 /* Skip weak definitions of symbols that are already defined. */
1349 if (newdef && olddef && newweak)
1350 *skip = TRUE;
1352 /* If the old symbol is from a dynamic object, and the new symbol is
1353 a definition which is not from a dynamic object, then the new
1354 symbol overrides the old symbol. Symbols from regular files
1355 always take precedence over symbols from dynamic objects, even if
1356 they are defined after the dynamic object in the link.
1358 As above, we again permit a common symbol in a regular object to
1359 override a definition in a shared object if the shared object
1360 symbol is a function or is weak. */
1362 flip = NULL;
1363 if (!newdyn
1364 && (newdef
1365 || (bfd_is_com_section (sec)
1366 && (oldweak || oldfunc)))
1367 && olddyn
1368 && olddef
1369 && h->def_dynamic)
1371 /* Change the hash table entry to undefined, and let
1372 _bfd_generic_link_add_one_symbol do the right thing with the
1373 new definition. */
1375 h->root.type = bfd_link_hash_undefined;
1376 h->root.u.undef.abfd = h->root.u.def.section->owner;
1377 *size_change_ok = TRUE;
1379 olddef = FALSE;
1380 olddyncommon = FALSE;
1382 /* We again permit a type change when a common symbol may be
1383 overriding a function. */
1385 if (bfd_is_com_section (sec))
1387 if (oldfunc)
1389 /* If a common symbol overrides a function, make sure
1390 that it isn't defined dynamically nor has type
1391 function. */
1392 h->def_dynamic = 0;
1393 h->type = STT_NOTYPE;
1395 *type_change_ok = TRUE;
1398 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1399 flip = *sym_hash;
1400 else
1401 /* This union may have been set to be non-NULL when this symbol
1402 was seen in a dynamic object. We must force the union to be
1403 NULL, so that it is correct for a regular symbol. */
1404 h->verinfo.vertree = NULL;
1407 /* Handle the special case of a new common symbol merging with an
1408 old symbol that looks like it might be a common symbol defined in
1409 a shared object. Note that we have already handled the case in
1410 which a new common symbol should simply override the definition
1411 in the shared library. */
1413 if (! newdyn
1414 && bfd_is_com_section (sec)
1415 && olddyncommon)
1417 /* It would be best if we could set the hash table entry to a
1418 common symbol, but we don't know what to use for the section
1419 or the alignment. */
1420 if (! ((*info->callbacks->multiple_common)
1421 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1422 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1423 return FALSE;
1425 /* If the presumed common symbol in the dynamic object is
1426 larger, pretend that the new symbol has its size. */
1428 if (h->size > *pvalue)
1429 *pvalue = h->size;
1431 /* We need to remember the alignment required by the symbol
1432 in the dynamic object. */
1433 BFD_ASSERT (pold_alignment);
1434 *pold_alignment = h->root.u.def.section->alignment_power;
1436 olddef = FALSE;
1437 olddyncommon = FALSE;
1439 h->root.type = bfd_link_hash_undefined;
1440 h->root.u.undef.abfd = h->root.u.def.section->owner;
1442 *size_change_ok = TRUE;
1443 *type_change_ok = TRUE;
1445 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1446 flip = *sym_hash;
1447 else
1448 h->verinfo.vertree = NULL;
1451 if (flip != NULL)
1453 /* Handle the case where we had a versioned symbol in a dynamic
1454 library and now find a definition in a normal object. In this
1455 case, we make the versioned symbol point to the normal one. */
1456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1457 flip->root.type = h->root.type;
1458 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1459 h->root.type = bfd_link_hash_indirect;
1460 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1461 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1462 if (h->def_dynamic)
1464 h->def_dynamic = 0;
1465 flip->ref_dynamic = 1;
1469 return TRUE;
1472 /* This function is called to create an indirect symbol from the
1473 default for the symbol with the default version if needed. The
1474 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1475 set DYNSYM if the new indirect symbol is dynamic. */
1477 bfd_boolean
1478 _bfd_elf_add_default_symbol (bfd *abfd,
1479 struct bfd_link_info *info,
1480 struct elf_link_hash_entry *h,
1481 const char *name,
1482 Elf_Internal_Sym *sym,
1483 asection **psec,
1484 bfd_vma *value,
1485 bfd_boolean *dynsym,
1486 bfd_boolean override)
1488 bfd_boolean type_change_ok;
1489 bfd_boolean size_change_ok;
1490 bfd_boolean skip;
1491 char *shortname;
1492 struct elf_link_hash_entry *hi;
1493 struct bfd_link_hash_entry *bh;
1494 const struct elf_backend_data *bed;
1495 bfd_boolean collect;
1496 bfd_boolean dynamic;
1497 char *p;
1498 size_t len, shortlen;
1499 asection *sec;
1501 /* If this symbol has a version, and it is the default version, we
1502 create an indirect symbol from the default name to the fully
1503 decorated name. This will cause external references which do not
1504 specify a version to be bound to this version of the symbol. */
1505 p = strchr (name, ELF_VER_CHR);
1506 if (p == NULL || p[1] != ELF_VER_CHR)
1507 return TRUE;
1509 if (override)
1511 /* We are overridden by an old definition. We need to check if we
1512 need to create the indirect symbol from the default name. */
1513 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1514 FALSE, FALSE);
1515 BFD_ASSERT (hi != NULL);
1516 if (hi == h)
1517 return TRUE;
1518 while (hi->root.type == bfd_link_hash_indirect
1519 || hi->root.type == bfd_link_hash_warning)
1521 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1522 if (hi == h)
1523 return TRUE;
1527 bed = get_elf_backend_data (abfd);
1528 collect = bed->collect;
1529 dynamic = (abfd->flags & DYNAMIC) != 0;
1531 shortlen = p - name;
1532 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1533 if (shortname == NULL)
1534 return FALSE;
1535 memcpy (shortname, name, shortlen);
1536 shortname[shortlen] = '\0';
1538 /* We are going to create a new symbol. Merge it with any existing
1539 symbol with this name. For the purposes of the merge, act as
1540 though we were defining the symbol we just defined, although we
1541 actually going to define an indirect symbol. */
1542 type_change_ok = FALSE;
1543 size_change_ok = FALSE;
1544 sec = *psec;
1545 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1546 NULL, &hi, &skip, &override,
1547 &type_change_ok, &size_change_ok))
1548 return FALSE;
1550 if (skip)
1551 goto nondefault;
1553 if (! override)
1555 bh = &hi->root;
1556 if (! (_bfd_generic_link_add_one_symbol
1557 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1558 0, name, FALSE, collect, &bh)))
1559 return FALSE;
1560 hi = (struct elf_link_hash_entry *) bh;
1562 else
1564 /* In this case the symbol named SHORTNAME is overriding the
1565 indirect symbol we want to add. We were planning on making
1566 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1567 is the name without a version. NAME is the fully versioned
1568 name, and it is the default version.
1570 Overriding means that we already saw a definition for the
1571 symbol SHORTNAME in a regular object, and it is overriding
1572 the symbol defined in the dynamic object.
1574 When this happens, we actually want to change NAME, the
1575 symbol we just added, to refer to SHORTNAME. This will cause
1576 references to NAME in the shared object to become references
1577 to SHORTNAME in the regular object. This is what we expect
1578 when we override a function in a shared object: that the
1579 references in the shared object will be mapped to the
1580 definition in the regular object. */
1582 while (hi->root.type == bfd_link_hash_indirect
1583 || hi->root.type == bfd_link_hash_warning)
1584 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1586 h->root.type = bfd_link_hash_indirect;
1587 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1588 if (h->def_dynamic)
1590 h->def_dynamic = 0;
1591 hi->ref_dynamic = 1;
1592 if (hi->ref_regular
1593 || hi->def_regular)
1595 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1596 return FALSE;
1600 /* Now set HI to H, so that the following code will set the
1601 other fields correctly. */
1602 hi = h;
1605 /* Check if HI is a warning symbol. */
1606 if (hi->root.type == bfd_link_hash_warning)
1607 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1609 /* If there is a duplicate definition somewhere, then HI may not
1610 point to an indirect symbol. We will have reported an error to
1611 the user in that case. */
1613 if (hi->root.type == bfd_link_hash_indirect)
1615 struct elf_link_hash_entry *ht;
1617 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1618 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1620 /* See if the new flags lead us to realize that the symbol must
1621 be dynamic. */
1622 if (! *dynsym)
1624 if (! dynamic)
1626 if (info->shared
1627 || hi->ref_dynamic)
1628 *dynsym = TRUE;
1630 else
1632 if (hi->ref_regular)
1633 *dynsym = TRUE;
1638 /* We also need to define an indirection from the nondefault version
1639 of the symbol. */
1641 nondefault:
1642 len = strlen (name);
1643 shortname = bfd_hash_allocate (&info->hash->table, len);
1644 if (shortname == NULL)
1645 return FALSE;
1646 memcpy (shortname, name, shortlen);
1647 memcpy (shortname + shortlen, p + 1, len - shortlen);
1649 /* Once again, merge with any existing symbol. */
1650 type_change_ok = FALSE;
1651 size_change_ok = FALSE;
1652 sec = *psec;
1653 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1654 NULL, &hi, &skip, &override,
1655 &type_change_ok, &size_change_ok))
1656 return FALSE;
1658 if (skip)
1659 return TRUE;
1661 if (override)
1663 /* Here SHORTNAME is a versioned name, so we don't expect to see
1664 the type of override we do in the case above unless it is
1665 overridden by a versioned definition. */
1666 if (hi->root.type != bfd_link_hash_defined
1667 && hi->root.type != bfd_link_hash_defweak)
1668 (*_bfd_error_handler)
1669 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1670 abfd, shortname);
1672 else
1674 bh = &hi->root;
1675 if (! (_bfd_generic_link_add_one_symbol
1676 (info, abfd, shortname, BSF_INDIRECT,
1677 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1678 return FALSE;
1679 hi = (struct elf_link_hash_entry *) bh;
1681 /* If there is a duplicate definition somewhere, then HI may not
1682 point to an indirect symbol. We will have reported an error
1683 to the user in that case. */
1685 if (hi->root.type == bfd_link_hash_indirect)
1687 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1689 /* See if the new flags lead us to realize that the symbol
1690 must be dynamic. */
1691 if (! *dynsym)
1693 if (! dynamic)
1695 if (info->shared
1696 || hi->ref_dynamic)
1697 *dynsym = TRUE;
1699 else
1701 if (hi->ref_regular)
1702 *dynsym = TRUE;
1708 return TRUE;
1711 /* This routine is used to export all defined symbols into the dynamic
1712 symbol table. It is called via elf_link_hash_traverse. */
1714 bfd_boolean
1715 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1717 struct elf_info_failed *eif = data;
1719 /* Ignore this if we won't export it. */
1720 if (!eif->info->export_dynamic && !h->dynamic)
1721 return TRUE;
1723 /* Ignore indirect symbols. These are added by the versioning code. */
1724 if (h->root.type == bfd_link_hash_indirect)
1725 return TRUE;
1727 if (h->root.type == bfd_link_hash_warning)
1728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1730 if (h->dynindx == -1
1731 && (h->def_regular
1732 || h->ref_regular))
1734 struct bfd_elf_version_tree *t;
1735 struct bfd_elf_version_expr *d;
1737 for (t = eif->verdefs; t != NULL; t = t->next)
1739 if (t->globals.list != NULL)
1741 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1742 if (d != NULL)
1743 goto doit;
1746 if (t->locals.list != NULL)
1748 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1749 if (d != NULL)
1750 return TRUE;
1754 if (!eif->verdefs)
1756 doit:
1757 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1759 eif->failed = TRUE;
1760 return FALSE;
1765 return TRUE;
1768 /* Look through the symbols which are defined in other shared
1769 libraries and referenced here. Update the list of version
1770 dependencies. This will be put into the .gnu.version_r section.
1771 This function is called via elf_link_hash_traverse. */
1773 bfd_boolean
1774 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1775 void *data)
1777 struct elf_find_verdep_info *rinfo = data;
1778 Elf_Internal_Verneed *t;
1779 Elf_Internal_Vernaux *a;
1780 bfd_size_type amt;
1782 if (h->root.type == bfd_link_hash_warning)
1783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1785 /* We only care about symbols defined in shared objects with version
1786 information. */
1787 if (!h->def_dynamic
1788 || h->def_regular
1789 || h->dynindx == -1
1790 || h->verinfo.verdef == NULL)
1791 return TRUE;
1793 /* See if we already know about this version. */
1794 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1796 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1797 continue;
1799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1800 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1801 return TRUE;
1803 break;
1806 /* This is a new version. Add it to tree we are building. */
1808 if (t == NULL)
1810 amt = sizeof *t;
1811 t = bfd_zalloc (rinfo->output_bfd, amt);
1812 if (t == NULL)
1814 rinfo->failed = TRUE;
1815 return FALSE;
1818 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1819 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1820 elf_tdata (rinfo->output_bfd)->verref = t;
1823 amt = sizeof *a;
1824 a = bfd_zalloc (rinfo->output_bfd, amt);
1825 if (a == NULL)
1827 rinfo->failed = TRUE;
1828 return FALSE;
1831 /* Note that we are copying a string pointer here, and testing it
1832 above. If bfd_elf_string_from_elf_section is ever changed to
1833 discard the string data when low in memory, this will have to be
1834 fixed. */
1835 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1837 a->vna_flags = h->verinfo.verdef->vd_flags;
1838 a->vna_nextptr = t->vn_auxptr;
1840 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1841 ++rinfo->vers;
1843 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1845 t->vn_auxptr = a;
1847 return TRUE;
1850 /* Figure out appropriate versions for all the symbols. We may not
1851 have the version number script until we have read all of the input
1852 files, so until that point we don't know which symbols should be
1853 local. This function is called via elf_link_hash_traverse. */
1855 bfd_boolean
1856 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1858 struct elf_assign_sym_version_info *sinfo;
1859 struct bfd_link_info *info;
1860 const struct elf_backend_data *bed;
1861 struct elf_info_failed eif;
1862 char *p;
1863 bfd_size_type amt;
1865 sinfo = data;
1866 info = sinfo->info;
1868 if (h->root.type == bfd_link_hash_warning)
1869 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1871 /* Fix the symbol flags. */
1872 eif.failed = FALSE;
1873 eif.info = info;
1874 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1876 if (eif.failed)
1877 sinfo->failed = TRUE;
1878 return FALSE;
1881 /* We only need version numbers for symbols defined in regular
1882 objects. */
1883 if (!h->def_regular)
1884 return TRUE;
1886 bed = get_elf_backend_data (sinfo->output_bfd);
1887 p = strchr (h->root.root.string, ELF_VER_CHR);
1888 if (p != NULL && h->verinfo.vertree == NULL)
1890 struct bfd_elf_version_tree *t;
1891 bfd_boolean hidden;
1893 hidden = TRUE;
1895 /* There are two consecutive ELF_VER_CHR characters if this is
1896 not a hidden symbol. */
1897 ++p;
1898 if (*p == ELF_VER_CHR)
1900 hidden = FALSE;
1901 ++p;
1904 /* If there is no version string, we can just return out. */
1905 if (*p == '\0')
1907 if (hidden)
1908 h->hidden = 1;
1909 return TRUE;
1912 /* Look for the version. If we find it, it is no longer weak. */
1913 for (t = sinfo->verdefs; t != NULL; t = t->next)
1915 if (strcmp (t->name, p) == 0)
1917 size_t len;
1918 char *alc;
1919 struct bfd_elf_version_expr *d;
1921 len = p - h->root.root.string;
1922 alc = bfd_malloc (len);
1923 if (alc == NULL)
1925 sinfo->failed = TRUE;
1926 return FALSE;
1928 memcpy (alc, h->root.root.string, len - 1);
1929 alc[len - 1] = '\0';
1930 if (alc[len - 2] == ELF_VER_CHR)
1931 alc[len - 2] = '\0';
1933 h->verinfo.vertree = t;
1934 t->used = TRUE;
1935 d = NULL;
1937 if (t->globals.list != NULL)
1938 d = (*t->match) (&t->globals, NULL, alc);
1940 /* See if there is anything to force this symbol to
1941 local scope. */
1942 if (d == NULL && t->locals.list != NULL)
1944 d = (*t->match) (&t->locals, NULL, alc);
1945 if (d != NULL
1946 && h->dynindx != -1
1947 && ! info->export_dynamic)
1948 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1951 free (alc);
1952 break;
1956 /* If we are building an application, we need to create a
1957 version node for this version. */
1958 if (t == NULL && info->executable)
1960 struct bfd_elf_version_tree **pp;
1961 int version_index;
1963 /* If we aren't going to export this symbol, we don't need
1964 to worry about it. */
1965 if (h->dynindx == -1)
1966 return TRUE;
1968 amt = sizeof *t;
1969 t = bfd_zalloc (sinfo->output_bfd, amt);
1970 if (t == NULL)
1972 sinfo->failed = TRUE;
1973 return FALSE;
1976 t->name = p;
1977 t->name_indx = (unsigned int) -1;
1978 t->used = TRUE;
1980 version_index = 1;
1981 /* Don't count anonymous version tag. */
1982 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1983 version_index = 0;
1984 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1985 ++version_index;
1986 t->vernum = version_index;
1988 *pp = t;
1990 h->verinfo.vertree = t;
1992 else if (t == NULL)
1994 /* We could not find the version for a symbol when
1995 generating a shared archive. Return an error. */
1996 (*_bfd_error_handler)
1997 (_("%B: version node not found for symbol %s"),
1998 sinfo->output_bfd, h->root.root.string);
1999 bfd_set_error (bfd_error_bad_value);
2000 sinfo->failed = TRUE;
2001 return FALSE;
2004 if (hidden)
2005 h->hidden = 1;
2008 /* If we don't have a version for this symbol, see if we can find
2009 something. */
2010 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2012 struct bfd_elf_version_tree *t;
2013 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
2014 struct bfd_elf_version_expr *d;
2016 /* See if can find what version this symbol is in. If the
2017 symbol is supposed to be local, then don't actually register
2018 it. */
2019 local_ver = NULL;
2020 global_ver = NULL;
2021 exist_ver = NULL;
2022 for (t = sinfo->verdefs; t != NULL; t = t->next)
2024 if (t->globals.list != NULL)
2026 d = NULL;
2027 while ((d = (*t->match) (&t->globals, d,
2028 h->root.root.string)) != NULL)
2030 global_ver = t;
2031 local_ver = NULL;
2032 if (d->symver)
2033 exist_ver = t;
2034 d->script = 1;
2035 /* If the match is a wildcard pattern, keep looking for
2036 a more explicit, perhaps even local, match. */
2037 if (d->literal)
2038 break;
2041 if (d != NULL)
2042 break;
2045 if (t->locals.list != NULL)
2047 d = NULL;
2048 while ((d = (*t->match) (&t->locals, d,
2049 h->root.root.string)) != NULL)
2051 local_ver = t;
2052 /* If the match is a wildcard pattern, keep looking for
2053 a more explicit, perhaps even global, match. */
2054 if (d->literal)
2056 /* An exact match overrides a global wildcard. */
2057 global_ver = NULL;
2058 break;
2062 if (d != NULL)
2063 break;
2067 if (global_ver != NULL)
2069 h->verinfo.vertree = global_ver;
2070 /* If we already have a versioned symbol that matches the
2071 node for this symbol, then we don't want to create a
2072 duplicate from the unversioned symbol. Instead hide the
2073 unversioned symbol. */
2074 if (exist_ver == global_ver)
2075 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2077 else if (local_ver != NULL)
2079 h->verinfo.vertree = local_ver;
2080 if (!info->export_dynamic
2081 || exist_ver == local_ver)
2082 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2086 return TRUE;
2089 /* Read and swap the relocs from the section indicated by SHDR. This
2090 may be either a REL or a RELA section. The relocations are
2091 translated into RELA relocations and stored in INTERNAL_RELOCS,
2092 which should have already been allocated to contain enough space.
2093 The EXTERNAL_RELOCS are a buffer where the external form of the
2094 relocations should be stored.
2096 Returns FALSE if something goes wrong. */
2098 static bfd_boolean
2099 elf_link_read_relocs_from_section (bfd *abfd,
2100 asection *sec,
2101 Elf_Internal_Shdr *shdr,
2102 void *external_relocs,
2103 Elf_Internal_Rela *internal_relocs)
2105 const struct elf_backend_data *bed;
2106 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2107 const bfd_byte *erela;
2108 const bfd_byte *erelaend;
2109 Elf_Internal_Rela *irela;
2110 Elf_Internal_Shdr *symtab_hdr;
2111 size_t nsyms;
2113 /* Position ourselves at the start of the section. */
2114 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2115 return FALSE;
2117 /* Read the relocations. */
2118 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2119 return FALSE;
2121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2122 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2124 bed = get_elf_backend_data (abfd);
2126 /* Convert the external relocations to the internal format. */
2127 if (shdr->sh_entsize == bed->s->sizeof_rel)
2128 swap_in = bed->s->swap_reloc_in;
2129 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2130 swap_in = bed->s->swap_reloca_in;
2131 else
2133 bfd_set_error (bfd_error_wrong_format);
2134 return FALSE;
2137 erela = external_relocs;
2138 erelaend = erela + shdr->sh_size;
2139 irela = internal_relocs;
2140 while (erela < erelaend)
2142 bfd_vma r_symndx;
2144 (*swap_in) (abfd, erela, irela);
2145 r_symndx = ELF32_R_SYM (irela->r_info);
2146 if (bed->s->arch_size == 64)
2147 r_symndx >>= 24;
2148 if ((size_t) r_symndx >= nsyms)
2150 (*_bfd_error_handler)
2151 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2152 " for offset 0x%lx in section `%A'"),
2153 abfd, sec,
2154 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2155 bfd_set_error (bfd_error_bad_value);
2156 return FALSE;
2158 irela += bed->s->int_rels_per_ext_rel;
2159 erela += shdr->sh_entsize;
2162 return TRUE;
2165 /* Read and swap the relocs for a section O. They may have been
2166 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2167 not NULL, they are used as buffers to read into. They are known to
2168 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2169 the return value is allocated using either malloc or bfd_alloc,
2170 according to the KEEP_MEMORY argument. If O has two relocation
2171 sections (both REL and RELA relocations), then the REL_HDR
2172 relocations will appear first in INTERNAL_RELOCS, followed by the
2173 REL_HDR2 relocations. */
2175 Elf_Internal_Rela *
2176 _bfd_elf_link_read_relocs (bfd *abfd,
2177 asection *o,
2178 void *external_relocs,
2179 Elf_Internal_Rela *internal_relocs,
2180 bfd_boolean keep_memory)
2182 Elf_Internal_Shdr *rel_hdr;
2183 void *alloc1 = NULL;
2184 Elf_Internal_Rela *alloc2 = NULL;
2185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2187 if (elf_section_data (o)->relocs != NULL)
2188 return elf_section_data (o)->relocs;
2190 if (o->reloc_count == 0)
2191 return NULL;
2193 rel_hdr = &elf_section_data (o)->rel_hdr;
2195 if (internal_relocs == NULL)
2197 bfd_size_type size;
2199 size = o->reloc_count;
2200 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2201 if (keep_memory)
2202 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2203 else
2204 internal_relocs = alloc2 = bfd_malloc (size);
2205 if (internal_relocs == NULL)
2206 goto error_return;
2209 if (external_relocs == NULL)
2211 bfd_size_type size = rel_hdr->sh_size;
2213 if (elf_section_data (o)->rel_hdr2)
2214 size += elf_section_data (o)->rel_hdr2->sh_size;
2215 alloc1 = bfd_malloc (size);
2216 if (alloc1 == NULL)
2217 goto error_return;
2218 external_relocs = alloc1;
2221 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2222 external_relocs,
2223 internal_relocs))
2224 goto error_return;
2225 if (elf_section_data (o)->rel_hdr2
2226 && (!elf_link_read_relocs_from_section
2227 (abfd, o,
2228 elf_section_data (o)->rel_hdr2,
2229 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2230 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2231 * bed->s->int_rels_per_ext_rel))))
2232 goto error_return;
2234 /* Cache the results for next time, if we can. */
2235 if (keep_memory)
2236 elf_section_data (o)->relocs = internal_relocs;
2238 if (alloc1 != NULL)
2239 free (alloc1);
2241 /* Don't free alloc2, since if it was allocated we are passing it
2242 back (under the name of internal_relocs). */
2244 return internal_relocs;
2246 error_return:
2247 if (alloc1 != NULL)
2248 free (alloc1);
2249 if (alloc2 != NULL)
2251 if (keep_memory)
2252 bfd_release (abfd, alloc2);
2253 else
2254 free (alloc2);
2256 return NULL;
2259 /* Compute the size of, and allocate space for, REL_HDR which is the
2260 section header for a section containing relocations for O. */
2262 bfd_boolean
2263 _bfd_elf_link_size_reloc_section (bfd *abfd,
2264 Elf_Internal_Shdr *rel_hdr,
2265 asection *o)
2267 bfd_size_type reloc_count;
2268 bfd_size_type num_rel_hashes;
2270 /* Figure out how many relocations there will be. */
2271 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2272 reloc_count = elf_section_data (o)->rel_count;
2273 else
2274 reloc_count = elf_section_data (o)->rel_count2;
2276 num_rel_hashes = o->reloc_count;
2277 if (num_rel_hashes < reloc_count)
2278 num_rel_hashes = reloc_count;
2280 /* That allows us to calculate the size of the section. */
2281 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2283 /* The contents field must last into write_object_contents, so we
2284 allocate it with bfd_alloc rather than malloc. Also since we
2285 cannot be sure that the contents will actually be filled in,
2286 we zero the allocated space. */
2287 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2288 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2289 return FALSE;
2291 /* We only allocate one set of hash entries, so we only do it the
2292 first time we are called. */
2293 if (elf_section_data (o)->rel_hashes == NULL
2294 && num_rel_hashes)
2296 struct elf_link_hash_entry **p;
2298 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2299 if (p == NULL)
2300 return FALSE;
2302 elf_section_data (o)->rel_hashes = p;
2305 return TRUE;
2308 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2309 originated from the section given by INPUT_REL_HDR) to the
2310 OUTPUT_BFD. */
2312 bfd_boolean
2313 _bfd_elf_link_output_relocs (bfd *output_bfd,
2314 asection *input_section,
2315 Elf_Internal_Shdr *input_rel_hdr,
2316 Elf_Internal_Rela *internal_relocs,
2317 struct elf_link_hash_entry **rel_hash
2318 ATTRIBUTE_UNUSED)
2320 Elf_Internal_Rela *irela;
2321 Elf_Internal_Rela *irelaend;
2322 bfd_byte *erel;
2323 Elf_Internal_Shdr *output_rel_hdr;
2324 asection *output_section;
2325 unsigned int *rel_countp = NULL;
2326 const struct elf_backend_data *bed;
2327 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2329 output_section = input_section->output_section;
2330 output_rel_hdr = NULL;
2332 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2333 == input_rel_hdr->sh_entsize)
2335 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2336 rel_countp = &elf_section_data (output_section)->rel_count;
2338 else if (elf_section_data (output_section)->rel_hdr2
2339 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2340 == input_rel_hdr->sh_entsize))
2342 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2343 rel_countp = &elf_section_data (output_section)->rel_count2;
2345 else
2347 (*_bfd_error_handler)
2348 (_("%B: relocation size mismatch in %B section %A"),
2349 output_bfd, input_section->owner, input_section);
2350 bfd_set_error (bfd_error_wrong_format);
2351 return FALSE;
2354 bed = get_elf_backend_data (output_bfd);
2355 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2356 swap_out = bed->s->swap_reloc_out;
2357 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2358 swap_out = bed->s->swap_reloca_out;
2359 else
2360 abort ();
2362 erel = output_rel_hdr->contents;
2363 erel += *rel_countp * input_rel_hdr->sh_entsize;
2364 irela = internal_relocs;
2365 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2366 * bed->s->int_rels_per_ext_rel);
2367 while (irela < irelaend)
2369 (*swap_out) (output_bfd, irela, erel);
2370 irela += bed->s->int_rels_per_ext_rel;
2371 erel += input_rel_hdr->sh_entsize;
2374 /* Bump the counter, so that we know where to add the next set of
2375 relocations. */
2376 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2378 return TRUE;
2381 /* Make weak undefined symbols in PIE dynamic. */
2383 bfd_boolean
2384 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2385 struct elf_link_hash_entry *h)
2387 if (info->pie
2388 && h->dynindx == -1
2389 && h->root.type == bfd_link_hash_undefweak)
2390 return bfd_elf_link_record_dynamic_symbol (info, h);
2392 return TRUE;
2395 /* Fix up the flags for a symbol. This handles various cases which
2396 can only be fixed after all the input files are seen. This is
2397 currently called by both adjust_dynamic_symbol and
2398 assign_sym_version, which is unnecessary but perhaps more robust in
2399 the face of future changes. */
2401 bfd_boolean
2402 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2403 struct elf_info_failed *eif)
2405 const struct elf_backend_data *bed;
2407 /* If this symbol was mentioned in a non-ELF file, try to set
2408 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2409 permit a non-ELF file to correctly refer to a symbol defined in
2410 an ELF dynamic object. */
2411 if (h->non_elf)
2413 while (h->root.type == bfd_link_hash_indirect)
2414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2416 if (h->root.type != bfd_link_hash_defined
2417 && h->root.type != bfd_link_hash_defweak)
2419 h->ref_regular = 1;
2420 h->ref_regular_nonweak = 1;
2422 else
2424 if (h->root.u.def.section->owner != NULL
2425 && (bfd_get_flavour (h->root.u.def.section->owner)
2426 == bfd_target_elf_flavour))
2428 h->ref_regular = 1;
2429 h->ref_regular_nonweak = 1;
2431 else
2432 h->def_regular = 1;
2435 if (h->dynindx == -1
2436 && (h->def_dynamic
2437 || h->ref_dynamic))
2439 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2441 eif->failed = TRUE;
2442 return FALSE;
2446 else
2448 /* Unfortunately, NON_ELF is only correct if the symbol
2449 was first seen in a non-ELF file. Fortunately, if the symbol
2450 was first seen in an ELF file, we're probably OK unless the
2451 symbol was defined in a non-ELF file. Catch that case here.
2452 FIXME: We're still in trouble if the symbol was first seen in
2453 a dynamic object, and then later in a non-ELF regular object. */
2454 if ((h->root.type == bfd_link_hash_defined
2455 || h->root.type == bfd_link_hash_defweak)
2456 && !h->def_regular
2457 && (h->root.u.def.section->owner != NULL
2458 ? (bfd_get_flavour (h->root.u.def.section->owner)
2459 != bfd_target_elf_flavour)
2460 : (bfd_is_abs_section (h->root.u.def.section)
2461 && !h->def_dynamic)))
2462 h->def_regular = 1;
2465 /* Backend specific symbol fixup. */
2466 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2467 if (bed->elf_backend_fixup_symbol
2468 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2469 return FALSE;
2471 /* If this is a final link, and the symbol was defined as a common
2472 symbol in a regular object file, and there was no definition in
2473 any dynamic object, then the linker will have allocated space for
2474 the symbol in a common section but the DEF_REGULAR
2475 flag will not have been set. */
2476 if (h->root.type == bfd_link_hash_defined
2477 && !h->def_regular
2478 && h->ref_regular
2479 && !h->def_dynamic
2480 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2481 h->def_regular = 1;
2483 /* If -Bsymbolic was used (which means to bind references to global
2484 symbols to the definition within the shared object), and this
2485 symbol was defined in a regular object, then it actually doesn't
2486 need a PLT entry. Likewise, if the symbol has non-default
2487 visibility. If the symbol has hidden or internal visibility, we
2488 will force it local. */
2489 if (h->needs_plt
2490 && eif->info->shared
2491 && is_elf_hash_table (eif->info->hash)
2492 && (SYMBOLIC_BIND (eif->info, h)
2493 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2494 && h->def_regular)
2496 bfd_boolean force_local;
2498 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2499 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2500 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2503 /* If a weak undefined symbol has non-default visibility, we also
2504 hide it from the dynamic linker. */
2505 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2506 && h->root.type == bfd_link_hash_undefweak)
2507 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2509 /* If this is a weak defined symbol in a dynamic object, and we know
2510 the real definition in the dynamic object, copy interesting flags
2511 over to the real definition. */
2512 if (h->u.weakdef != NULL)
2514 struct elf_link_hash_entry *weakdef;
2516 weakdef = h->u.weakdef;
2517 if (h->root.type == bfd_link_hash_indirect)
2518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2520 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2521 || h->root.type == bfd_link_hash_defweak);
2522 BFD_ASSERT (weakdef->def_dynamic);
2524 /* If the real definition is defined by a regular object file,
2525 don't do anything special. See the longer description in
2526 _bfd_elf_adjust_dynamic_symbol, below. */
2527 if (weakdef->def_regular)
2528 h->u.weakdef = NULL;
2529 else
2531 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2532 || weakdef->root.type == bfd_link_hash_defweak);
2533 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2537 return TRUE;
2540 /* Make the backend pick a good value for a dynamic symbol. This is
2541 called via elf_link_hash_traverse, and also calls itself
2542 recursively. */
2544 bfd_boolean
2545 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2547 struct elf_info_failed *eif = data;
2548 bfd *dynobj;
2549 const struct elf_backend_data *bed;
2551 if (! is_elf_hash_table (eif->info->hash))
2552 return FALSE;
2554 if (h->root.type == bfd_link_hash_warning)
2556 h->got = elf_hash_table (eif->info)->init_got_offset;
2557 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2559 /* When warning symbols are created, they **replace** the "real"
2560 entry in the hash table, thus we never get to see the real
2561 symbol in a hash traversal. So look at it now. */
2562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2565 /* Ignore indirect symbols. These are added by the versioning code. */
2566 if (h->root.type == bfd_link_hash_indirect)
2567 return TRUE;
2569 /* Fix the symbol flags. */
2570 if (! _bfd_elf_fix_symbol_flags (h, eif))
2571 return FALSE;
2573 /* If this symbol does not require a PLT entry, and it is not
2574 defined by a dynamic object, or is not referenced by a regular
2575 object, ignore it. We do have to handle a weak defined symbol,
2576 even if no regular object refers to it, if we decided to add it
2577 to the dynamic symbol table. FIXME: Do we normally need to worry
2578 about symbols which are defined by one dynamic object and
2579 referenced by another one? */
2580 if (!h->needs_plt
2581 && (h->def_regular
2582 || !h->def_dynamic
2583 || (!h->ref_regular
2584 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2586 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2587 return TRUE;
2590 /* If we've already adjusted this symbol, don't do it again. This
2591 can happen via a recursive call. */
2592 if (h->dynamic_adjusted)
2593 return TRUE;
2595 /* Don't look at this symbol again. Note that we must set this
2596 after checking the above conditions, because we may look at a
2597 symbol once, decide not to do anything, and then get called
2598 recursively later after REF_REGULAR is set below. */
2599 h->dynamic_adjusted = 1;
2601 /* If this is a weak definition, and we know a real definition, and
2602 the real symbol is not itself defined by a regular object file,
2603 then get a good value for the real definition. We handle the
2604 real symbol first, for the convenience of the backend routine.
2606 Note that there is a confusing case here. If the real definition
2607 is defined by a regular object file, we don't get the real symbol
2608 from the dynamic object, but we do get the weak symbol. If the
2609 processor backend uses a COPY reloc, then if some routine in the
2610 dynamic object changes the real symbol, we will not see that
2611 change in the corresponding weak symbol. This is the way other
2612 ELF linkers work as well, and seems to be a result of the shared
2613 library model.
2615 I will clarify this issue. Most SVR4 shared libraries define the
2616 variable _timezone and define timezone as a weak synonym. The
2617 tzset call changes _timezone. If you write
2618 extern int timezone;
2619 int _timezone = 5;
2620 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2621 you might expect that, since timezone is a synonym for _timezone,
2622 the same number will print both times. However, if the processor
2623 backend uses a COPY reloc, then actually timezone will be copied
2624 into your process image, and, since you define _timezone
2625 yourself, _timezone will not. Thus timezone and _timezone will
2626 wind up at different memory locations. The tzset call will set
2627 _timezone, leaving timezone unchanged. */
2629 if (h->u.weakdef != NULL)
2631 /* If we get to this point, we know there is an implicit
2632 reference by a regular object file via the weak symbol H.
2633 FIXME: Is this really true? What if the traversal finds
2634 H->U.WEAKDEF before it finds H? */
2635 h->u.weakdef->ref_regular = 1;
2637 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2638 return FALSE;
2641 /* If a symbol has no type and no size and does not require a PLT
2642 entry, then we are probably about to do the wrong thing here: we
2643 are probably going to create a COPY reloc for an empty object.
2644 This case can arise when a shared object is built with assembly
2645 code, and the assembly code fails to set the symbol type. */
2646 if (h->size == 0
2647 && h->type == STT_NOTYPE
2648 && !h->needs_plt)
2649 (*_bfd_error_handler)
2650 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2651 h->root.root.string);
2653 dynobj = elf_hash_table (eif->info)->dynobj;
2654 bed = get_elf_backend_data (dynobj);
2655 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2657 eif->failed = TRUE;
2658 return FALSE;
2661 return TRUE;
2664 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2665 DYNBSS. */
2667 bfd_boolean
2668 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2669 asection *dynbss)
2671 unsigned int power_of_two;
2672 bfd_vma mask;
2673 asection *sec = h->root.u.def.section;
2675 /* The section aligment of definition is the maximum alignment
2676 requirement of symbols defined in the section. Since we don't
2677 know the symbol alignment requirement, we start with the
2678 maximum alignment and check low bits of the symbol address
2679 for the minimum alignment. */
2680 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2681 mask = ((bfd_vma) 1 << power_of_two) - 1;
2682 while ((h->root.u.def.value & mask) != 0)
2684 mask >>= 1;
2685 --power_of_two;
2688 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2689 dynbss))
2691 /* Adjust the section alignment if needed. */
2692 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2693 power_of_two))
2694 return FALSE;
2697 /* We make sure that the symbol will be aligned properly. */
2698 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2700 /* Define the symbol as being at this point in DYNBSS. */
2701 h->root.u.def.section = dynbss;
2702 h->root.u.def.value = dynbss->size;
2704 /* Increment the size of DYNBSS to make room for the symbol. */
2705 dynbss->size += h->size;
2707 return TRUE;
2710 /* Adjust all external symbols pointing into SEC_MERGE sections
2711 to reflect the object merging within the sections. */
2713 bfd_boolean
2714 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2716 asection *sec;
2718 if (h->root.type == bfd_link_hash_warning)
2719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2721 if ((h->root.type == bfd_link_hash_defined
2722 || h->root.type == bfd_link_hash_defweak)
2723 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2724 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2726 bfd *output_bfd = data;
2728 h->root.u.def.value =
2729 _bfd_merged_section_offset (output_bfd,
2730 &h->root.u.def.section,
2731 elf_section_data (sec)->sec_info,
2732 h->root.u.def.value);
2735 return TRUE;
2738 /* Returns false if the symbol referred to by H should be considered
2739 to resolve local to the current module, and true if it should be
2740 considered to bind dynamically. */
2742 bfd_boolean
2743 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2744 struct bfd_link_info *info,
2745 bfd_boolean ignore_protected)
2747 bfd_boolean binding_stays_local_p;
2748 const struct elf_backend_data *bed;
2749 struct elf_link_hash_table *hash_table;
2751 if (h == NULL)
2752 return FALSE;
2754 while (h->root.type == bfd_link_hash_indirect
2755 || h->root.type == bfd_link_hash_warning)
2756 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2758 /* If it was forced local, then clearly it's not dynamic. */
2759 if (h->dynindx == -1)
2760 return FALSE;
2761 if (h->forced_local)
2762 return FALSE;
2764 /* Identify the cases where name binding rules say that a
2765 visible symbol resolves locally. */
2766 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2768 switch (ELF_ST_VISIBILITY (h->other))
2770 case STV_INTERNAL:
2771 case STV_HIDDEN:
2772 return FALSE;
2774 case STV_PROTECTED:
2775 hash_table = elf_hash_table (info);
2776 if (!is_elf_hash_table (hash_table))
2777 return FALSE;
2779 bed = get_elf_backend_data (hash_table->dynobj);
2781 /* Proper resolution for function pointer equality may require
2782 that these symbols perhaps be resolved dynamically, even though
2783 we should be resolving them to the current module. */
2784 if (!ignore_protected || !bed->is_function_type (h->type))
2785 binding_stays_local_p = TRUE;
2786 break;
2788 default:
2789 break;
2792 /* If it isn't defined locally, then clearly it's dynamic. */
2793 if (!h->def_regular)
2794 return TRUE;
2796 /* Otherwise, the symbol is dynamic if binding rules don't tell
2797 us that it remains local. */
2798 return !binding_stays_local_p;
2801 /* Return true if the symbol referred to by H should be considered
2802 to resolve local to the current module, and false otherwise. Differs
2803 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2804 undefined symbols and weak symbols. */
2806 bfd_boolean
2807 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2808 struct bfd_link_info *info,
2809 bfd_boolean local_protected)
2811 const struct elf_backend_data *bed;
2812 struct elf_link_hash_table *hash_table;
2814 /* If it's a local sym, of course we resolve locally. */
2815 if (h == NULL)
2816 return TRUE;
2818 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2819 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2820 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2821 return TRUE;
2823 /* Common symbols that become definitions don't get the DEF_REGULAR
2824 flag set, so test it first, and don't bail out. */
2825 if (ELF_COMMON_DEF_P (h))
2826 /* Do nothing. */;
2827 /* If we don't have a definition in a regular file, then we can't
2828 resolve locally. The sym is either undefined or dynamic. */
2829 else if (!h->def_regular)
2830 return FALSE;
2832 /* Forced local symbols resolve locally. */
2833 if (h->forced_local)
2834 return TRUE;
2836 /* As do non-dynamic symbols. */
2837 if (h->dynindx == -1)
2838 return TRUE;
2840 /* At this point, we know the symbol is defined and dynamic. In an
2841 executable it must resolve locally, likewise when building symbolic
2842 shared libraries. */
2843 if (info->executable || SYMBOLIC_BIND (info, h))
2844 return TRUE;
2846 /* Now deal with defined dynamic symbols in shared libraries. Ones
2847 with default visibility might not resolve locally. */
2848 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2849 return FALSE;
2851 hash_table = elf_hash_table (info);
2852 if (!is_elf_hash_table (hash_table))
2853 return TRUE;
2855 bed = get_elf_backend_data (hash_table->dynobj);
2857 /* STV_PROTECTED non-function symbols are local. */
2858 if (!bed->is_function_type (h->type))
2859 return TRUE;
2861 /* Function pointer equality tests may require that STV_PROTECTED
2862 symbols be treated as dynamic symbols, even when we know that the
2863 dynamic linker will resolve them locally. */
2864 return local_protected;
2867 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2868 aligned. Returns the first TLS output section. */
2870 struct bfd_section *
2871 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2873 struct bfd_section *sec, *tls;
2874 unsigned int align = 0;
2876 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2877 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2878 break;
2879 tls = sec;
2881 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2882 if (sec->alignment_power > align)
2883 align = sec->alignment_power;
2885 elf_hash_table (info)->tls_sec = tls;
2887 /* Ensure the alignment of the first section is the largest alignment,
2888 so that the tls segment starts aligned. */
2889 if (tls != NULL)
2890 tls->alignment_power = align;
2892 return tls;
2895 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2896 static bfd_boolean
2897 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2898 Elf_Internal_Sym *sym)
2900 const struct elf_backend_data *bed;
2902 /* Local symbols do not count, but target specific ones might. */
2903 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2904 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2905 return FALSE;
2907 bed = get_elf_backend_data (abfd);
2908 /* Function symbols do not count. */
2909 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2910 return FALSE;
2912 /* If the section is undefined, then so is the symbol. */
2913 if (sym->st_shndx == SHN_UNDEF)
2914 return FALSE;
2916 /* If the symbol is defined in the common section, then
2917 it is a common definition and so does not count. */
2918 if (bed->common_definition (sym))
2919 return FALSE;
2921 /* If the symbol is in a target specific section then we
2922 must rely upon the backend to tell us what it is. */
2923 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2924 /* FIXME - this function is not coded yet:
2926 return _bfd_is_global_symbol_definition (abfd, sym);
2928 Instead for now assume that the definition is not global,
2929 Even if this is wrong, at least the linker will behave
2930 in the same way that it used to do. */
2931 return FALSE;
2933 return TRUE;
2936 /* Search the symbol table of the archive element of the archive ABFD
2937 whose archive map contains a mention of SYMDEF, and determine if
2938 the symbol is defined in this element. */
2939 static bfd_boolean
2940 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2942 Elf_Internal_Shdr * hdr;
2943 bfd_size_type symcount;
2944 bfd_size_type extsymcount;
2945 bfd_size_type extsymoff;
2946 Elf_Internal_Sym *isymbuf;
2947 Elf_Internal_Sym *isym;
2948 Elf_Internal_Sym *isymend;
2949 bfd_boolean result;
2951 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2952 if (abfd == NULL)
2953 return FALSE;
2955 if (! bfd_check_format (abfd, bfd_object))
2956 return FALSE;
2958 /* If we have already included the element containing this symbol in the
2959 link then we do not need to include it again. Just claim that any symbol
2960 it contains is not a definition, so that our caller will not decide to
2961 (re)include this element. */
2962 if (abfd->archive_pass)
2963 return FALSE;
2965 /* Select the appropriate symbol table. */
2966 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2967 hdr = &elf_tdata (abfd)->symtab_hdr;
2968 else
2969 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2971 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2973 /* The sh_info field of the symtab header tells us where the
2974 external symbols start. We don't care about the local symbols. */
2975 if (elf_bad_symtab (abfd))
2977 extsymcount = symcount;
2978 extsymoff = 0;
2980 else
2982 extsymcount = symcount - hdr->sh_info;
2983 extsymoff = hdr->sh_info;
2986 if (extsymcount == 0)
2987 return FALSE;
2989 /* Read in the symbol table. */
2990 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2991 NULL, NULL, NULL);
2992 if (isymbuf == NULL)
2993 return FALSE;
2995 /* Scan the symbol table looking for SYMDEF. */
2996 result = FALSE;
2997 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2999 const char *name;
3001 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3002 isym->st_name);
3003 if (name == NULL)
3004 break;
3006 if (strcmp (name, symdef->name) == 0)
3008 result = is_global_data_symbol_definition (abfd, isym);
3009 break;
3013 free (isymbuf);
3015 return result;
3018 /* Add an entry to the .dynamic table. */
3020 bfd_boolean
3021 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3022 bfd_vma tag,
3023 bfd_vma val)
3025 struct elf_link_hash_table *hash_table;
3026 const struct elf_backend_data *bed;
3027 asection *s;
3028 bfd_size_type newsize;
3029 bfd_byte *newcontents;
3030 Elf_Internal_Dyn dyn;
3032 hash_table = elf_hash_table (info);
3033 if (! is_elf_hash_table (hash_table))
3034 return FALSE;
3036 bed = get_elf_backend_data (hash_table->dynobj);
3037 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3038 BFD_ASSERT (s != NULL);
3040 newsize = s->size + bed->s->sizeof_dyn;
3041 newcontents = bfd_realloc (s->contents, newsize);
3042 if (newcontents == NULL)
3043 return FALSE;
3045 dyn.d_tag = tag;
3046 dyn.d_un.d_val = val;
3047 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3049 s->size = newsize;
3050 s->contents = newcontents;
3052 return TRUE;
3055 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3056 otherwise just check whether one already exists. Returns -1 on error,
3057 1 if a DT_NEEDED tag already exists, and 0 on success. */
3059 static int
3060 elf_add_dt_needed_tag (bfd *abfd,
3061 struct bfd_link_info *info,
3062 const char *soname,
3063 bfd_boolean do_it)
3065 struct elf_link_hash_table *hash_table;
3066 bfd_size_type oldsize;
3067 bfd_size_type strindex;
3069 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3070 return -1;
3072 hash_table = elf_hash_table (info);
3073 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3074 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3075 if (strindex == (bfd_size_type) -1)
3076 return -1;
3078 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3080 asection *sdyn;
3081 const struct elf_backend_data *bed;
3082 bfd_byte *extdyn;
3084 bed = get_elf_backend_data (hash_table->dynobj);
3085 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3086 if (sdyn != NULL)
3087 for (extdyn = sdyn->contents;
3088 extdyn < sdyn->contents + sdyn->size;
3089 extdyn += bed->s->sizeof_dyn)
3091 Elf_Internal_Dyn dyn;
3093 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3094 if (dyn.d_tag == DT_NEEDED
3095 && dyn.d_un.d_val == strindex)
3097 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3098 return 1;
3103 if (do_it)
3105 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3106 return -1;
3108 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3109 return -1;
3111 else
3112 /* We were just checking for existence of the tag. */
3113 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3115 return 0;
3118 /* Sort symbol by value and section. */
3119 static int
3120 elf_sort_symbol (const void *arg1, const void *arg2)
3122 const struct elf_link_hash_entry *h1;
3123 const struct elf_link_hash_entry *h2;
3124 bfd_signed_vma vdiff;
3126 h1 = *(const struct elf_link_hash_entry **) arg1;
3127 h2 = *(const struct elf_link_hash_entry **) arg2;
3128 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3129 if (vdiff != 0)
3130 return vdiff > 0 ? 1 : -1;
3131 else
3133 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3134 if (sdiff != 0)
3135 return sdiff > 0 ? 1 : -1;
3137 return 0;
3140 /* This function is used to adjust offsets into .dynstr for
3141 dynamic symbols. This is called via elf_link_hash_traverse. */
3143 static bfd_boolean
3144 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3146 struct elf_strtab_hash *dynstr = data;
3148 if (h->root.type == bfd_link_hash_warning)
3149 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3151 if (h->dynindx != -1)
3152 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3153 return TRUE;
3156 /* Assign string offsets in .dynstr, update all structures referencing
3157 them. */
3159 static bfd_boolean
3160 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3162 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3163 struct elf_link_local_dynamic_entry *entry;
3164 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3165 bfd *dynobj = hash_table->dynobj;
3166 asection *sdyn;
3167 bfd_size_type size;
3168 const struct elf_backend_data *bed;
3169 bfd_byte *extdyn;
3171 _bfd_elf_strtab_finalize (dynstr);
3172 size = _bfd_elf_strtab_size (dynstr);
3174 bed = get_elf_backend_data (dynobj);
3175 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3176 BFD_ASSERT (sdyn != NULL);
3178 /* Update all .dynamic entries referencing .dynstr strings. */
3179 for (extdyn = sdyn->contents;
3180 extdyn < sdyn->contents + sdyn->size;
3181 extdyn += bed->s->sizeof_dyn)
3183 Elf_Internal_Dyn dyn;
3185 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3186 switch (dyn.d_tag)
3188 case DT_STRSZ:
3189 dyn.d_un.d_val = size;
3190 break;
3191 case DT_NEEDED:
3192 case DT_SONAME:
3193 case DT_RPATH:
3194 case DT_RUNPATH:
3195 case DT_FILTER:
3196 case DT_AUXILIARY:
3197 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3198 break;
3199 default:
3200 continue;
3202 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3205 /* Now update local dynamic symbols. */
3206 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3207 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3208 entry->isym.st_name);
3210 /* And the rest of dynamic symbols. */
3211 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3213 /* Adjust version definitions. */
3214 if (elf_tdata (output_bfd)->cverdefs)
3216 asection *s;
3217 bfd_byte *p;
3218 bfd_size_type i;
3219 Elf_Internal_Verdef def;
3220 Elf_Internal_Verdaux defaux;
3222 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3223 p = s->contents;
3226 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3227 &def);
3228 p += sizeof (Elf_External_Verdef);
3229 if (def.vd_aux != sizeof (Elf_External_Verdef))
3230 continue;
3231 for (i = 0; i < def.vd_cnt; ++i)
3233 _bfd_elf_swap_verdaux_in (output_bfd,
3234 (Elf_External_Verdaux *) p, &defaux);
3235 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3236 defaux.vda_name);
3237 _bfd_elf_swap_verdaux_out (output_bfd,
3238 &defaux, (Elf_External_Verdaux *) p);
3239 p += sizeof (Elf_External_Verdaux);
3242 while (def.vd_next);
3245 /* Adjust version references. */
3246 if (elf_tdata (output_bfd)->verref)
3248 asection *s;
3249 bfd_byte *p;
3250 bfd_size_type i;
3251 Elf_Internal_Verneed need;
3252 Elf_Internal_Vernaux needaux;
3254 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3255 p = s->contents;
3258 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3259 &need);
3260 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3261 _bfd_elf_swap_verneed_out (output_bfd, &need,
3262 (Elf_External_Verneed *) p);
3263 p += sizeof (Elf_External_Verneed);
3264 for (i = 0; i < need.vn_cnt; ++i)
3266 _bfd_elf_swap_vernaux_in (output_bfd,
3267 (Elf_External_Vernaux *) p, &needaux);
3268 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3269 needaux.vna_name);
3270 _bfd_elf_swap_vernaux_out (output_bfd,
3271 &needaux,
3272 (Elf_External_Vernaux *) p);
3273 p += sizeof (Elf_External_Vernaux);
3276 while (need.vn_next);
3279 return TRUE;
3282 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3283 The default is to only match when the INPUT and OUTPUT are exactly
3284 the same target. */
3286 bfd_boolean
3287 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3288 const bfd_target *output)
3290 return input == output;
3293 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3294 This version is used when different targets for the same architecture
3295 are virtually identical. */
3297 bfd_boolean
3298 _bfd_elf_relocs_compatible (const bfd_target *input,
3299 const bfd_target *output)
3301 const struct elf_backend_data *obed, *ibed;
3303 if (input == output)
3304 return TRUE;
3306 ibed = xvec_get_elf_backend_data (input);
3307 obed = xvec_get_elf_backend_data (output);
3309 if (ibed->arch != obed->arch)
3310 return FALSE;
3312 /* If both backends are using this function, deem them compatible. */
3313 return ibed->relocs_compatible == obed->relocs_compatible;
3316 /* Add symbols from an ELF object file to the linker hash table. */
3318 static bfd_boolean
3319 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3321 Elf_Internal_Shdr *hdr;
3322 bfd_size_type symcount;
3323 bfd_size_type extsymcount;
3324 bfd_size_type extsymoff;
3325 struct elf_link_hash_entry **sym_hash;
3326 bfd_boolean dynamic;
3327 Elf_External_Versym *extversym = NULL;
3328 Elf_External_Versym *ever;
3329 struct elf_link_hash_entry *weaks;
3330 struct elf_link_hash_entry **nondeflt_vers = NULL;
3331 bfd_size_type nondeflt_vers_cnt = 0;
3332 Elf_Internal_Sym *isymbuf = NULL;
3333 Elf_Internal_Sym *isym;
3334 Elf_Internal_Sym *isymend;
3335 const struct elf_backend_data *bed;
3336 bfd_boolean add_needed;
3337 struct elf_link_hash_table *htab;
3338 bfd_size_type amt;
3339 void *alloc_mark = NULL;
3340 struct bfd_hash_entry **old_table = NULL;
3341 unsigned int old_size = 0;
3342 unsigned int old_count = 0;
3343 void *old_tab = NULL;
3344 void *old_hash;
3345 void *old_ent;
3346 struct bfd_link_hash_entry *old_undefs = NULL;
3347 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3348 long old_dynsymcount = 0;
3349 size_t tabsize = 0;
3350 size_t hashsize = 0;
3352 htab = elf_hash_table (info);
3353 bed = get_elf_backend_data (abfd);
3355 if ((abfd->flags & DYNAMIC) == 0)
3356 dynamic = FALSE;
3357 else
3359 dynamic = TRUE;
3361 /* You can't use -r against a dynamic object. Also, there's no
3362 hope of using a dynamic object which does not exactly match
3363 the format of the output file. */
3364 if (info->relocatable
3365 || !is_elf_hash_table (htab)
3366 || info->output_bfd->xvec != abfd->xvec)
3368 if (info->relocatable)
3369 bfd_set_error (bfd_error_invalid_operation);
3370 else
3371 bfd_set_error (bfd_error_wrong_format);
3372 goto error_return;
3376 /* As a GNU extension, any input sections which are named
3377 .gnu.warning.SYMBOL are treated as warning symbols for the given
3378 symbol. This differs from .gnu.warning sections, which generate
3379 warnings when they are included in an output file. */
3380 if (info->executable)
3382 asection *s;
3384 for (s = abfd->sections; s != NULL; s = s->next)
3386 const char *name;
3388 name = bfd_get_section_name (abfd, s);
3389 if (CONST_STRNEQ (name, ".gnu.warning."))
3391 char *msg;
3392 bfd_size_type sz;
3394 name += sizeof ".gnu.warning." - 1;
3396 /* If this is a shared object, then look up the symbol
3397 in the hash table. If it is there, and it is already
3398 been defined, then we will not be using the entry
3399 from this shared object, so we don't need to warn.
3400 FIXME: If we see the definition in a regular object
3401 later on, we will warn, but we shouldn't. The only
3402 fix is to keep track of what warnings we are supposed
3403 to emit, and then handle them all at the end of the
3404 link. */
3405 if (dynamic)
3407 struct elf_link_hash_entry *h;
3409 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3411 /* FIXME: What about bfd_link_hash_common? */
3412 if (h != NULL
3413 && (h->root.type == bfd_link_hash_defined
3414 || h->root.type == bfd_link_hash_defweak))
3416 /* We don't want to issue this warning. Clobber
3417 the section size so that the warning does not
3418 get copied into the output file. */
3419 s->size = 0;
3420 continue;
3424 sz = s->size;
3425 msg = bfd_alloc (abfd, sz + 1);
3426 if (msg == NULL)
3427 goto error_return;
3429 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3430 goto error_return;
3432 msg[sz] = '\0';
3434 if (! (_bfd_generic_link_add_one_symbol
3435 (info, abfd, name, BSF_WARNING, s, 0, msg,
3436 FALSE, bed->collect, NULL)))
3437 goto error_return;
3439 if (! info->relocatable)
3441 /* Clobber the section size so that the warning does
3442 not get copied into the output file. */
3443 s->size = 0;
3445 /* Also set SEC_EXCLUDE, so that symbols defined in
3446 the warning section don't get copied to the output. */
3447 s->flags |= SEC_EXCLUDE;
3453 add_needed = TRUE;
3454 if (! dynamic)
3456 /* If we are creating a shared library, create all the dynamic
3457 sections immediately. We need to attach them to something,
3458 so we attach them to this BFD, provided it is the right
3459 format. FIXME: If there are no input BFD's of the same
3460 format as the output, we can't make a shared library. */
3461 if (info->shared
3462 && is_elf_hash_table (htab)
3463 && info->output_bfd->xvec == abfd->xvec
3464 && !htab->dynamic_sections_created)
3466 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3467 goto error_return;
3470 else if (!is_elf_hash_table (htab))
3471 goto error_return;
3472 else
3474 asection *s;
3475 const char *soname = NULL;
3476 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3477 int ret;
3479 /* ld --just-symbols and dynamic objects don't mix very well.
3480 ld shouldn't allow it. */
3481 if ((s = abfd->sections) != NULL
3482 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3483 abort ();
3485 /* If this dynamic lib was specified on the command line with
3486 --as-needed in effect, then we don't want to add a DT_NEEDED
3487 tag unless the lib is actually used. Similary for libs brought
3488 in by another lib's DT_NEEDED. When --no-add-needed is used
3489 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3490 any dynamic library in DT_NEEDED tags in the dynamic lib at
3491 all. */
3492 add_needed = (elf_dyn_lib_class (abfd)
3493 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3494 | DYN_NO_NEEDED)) == 0;
3496 s = bfd_get_section_by_name (abfd, ".dynamic");
3497 if (s != NULL)
3499 bfd_byte *dynbuf;
3500 bfd_byte *extdyn;
3501 unsigned int elfsec;
3502 unsigned long shlink;
3504 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3505 goto error_free_dyn;
3507 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3508 if (elfsec == SHN_BAD)
3509 goto error_free_dyn;
3510 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3512 for (extdyn = dynbuf;
3513 extdyn < dynbuf + s->size;
3514 extdyn += bed->s->sizeof_dyn)
3516 Elf_Internal_Dyn dyn;
3518 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3519 if (dyn.d_tag == DT_SONAME)
3521 unsigned int tagv = dyn.d_un.d_val;
3522 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3523 if (soname == NULL)
3524 goto error_free_dyn;
3526 if (dyn.d_tag == DT_NEEDED)
3528 struct bfd_link_needed_list *n, **pn;
3529 char *fnm, *anm;
3530 unsigned int tagv = dyn.d_un.d_val;
3532 amt = sizeof (struct bfd_link_needed_list);
3533 n = bfd_alloc (abfd, amt);
3534 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3535 if (n == NULL || fnm == NULL)
3536 goto error_free_dyn;
3537 amt = strlen (fnm) + 1;
3538 anm = bfd_alloc (abfd, amt);
3539 if (anm == NULL)
3540 goto error_free_dyn;
3541 memcpy (anm, fnm, amt);
3542 n->name = anm;
3543 n->by = abfd;
3544 n->next = NULL;
3545 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3547 *pn = n;
3549 if (dyn.d_tag == DT_RUNPATH)
3551 struct bfd_link_needed_list *n, **pn;
3552 char *fnm, *anm;
3553 unsigned int tagv = dyn.d_un.d_val;
3555 amt = sizeof (struct bfd_link_needed_list);
3556 n = bfd_alloc (abfd, amt);
3557 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3558 if (n == NULL || fnm == NULL)
3559 goto error_free_dyn;
3560 amt = strlen (fnm) + 1;
3561 anm = bfd_alloc (abfd, amt);
3562 if (anm == NULL)
3563 goto error_free_dyn;
3564 memcpy (anm, fnm, amt);
3565 n->name = anm;
3566 n->by = abfd;
3567 n->next = NULL;
3568 for (pn = & runpath;
3569 *pn != NULL;
3570 pn = &(*pn)->next)
3572 *pn = n;
3574 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3575 if (!runpath && dyn.d_tag == DT_RPATH)
3577 struct bfd_link_needed_list *n, **pn;
3578 char *fnm, *anm;
3579 unsigned int tagv = dyn.d_un.d_val;
3581 amt = sizeof (struct bfd_link_needed_list);
3582 n = bfd_alloc (abfd, amt);
3583 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3584 if (n == NULL || fnm == NULL)
3585 goto error_free_dyn;
3586 amt = strlen (fnm) + 1;
3587 anm = bfd_alloc (abfd, amt);
3588 if (anm == NULL)
3590 error_free_dyn:
3591 free (dynbuf);
3592 goto error_return;
3594 memcpy (anm, fnm, amt);
3595 n->name = anm;
3596 n->by = abfd;
3597 n->next = NULL;
3598 for (pn = & rpath;
3599 *pn != NULL;
3600 pn = &(*pn)->next)
3602 *pn = n;
3606 free (dynbuf);
3609 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3610 frees all more recently bfd_alloc'd blocks as well. */
3611 if (runpath)
3612 rpath = runpath;
3614 if (rpath)
3616 struct bfd_link_needed_list **pn;
3617 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3619 *pn = rpath;
3622 /* We do not want to include any of the sections in a dynamic
3623 object in the output file. We hack by simply clobbering the
3624 list of sections in the BFD. This could be handled more
3625 cleanly by, say, a new section flag; the existing
3626 SEC_NEVER_LOAD flag is not the one we want, because that one
3627 still implies that the section takes up space in the output
3628 file. */
3629 bfd_section_list_clear (abfd);
3631 /* Find the name to use in a DT_NEEDED entry that refers to this
3632 object. If the object has a DT_SONAME entry, we use it.
3633 Otherwise, if the generic linker stuck something in
3634 elf_dt_name, we use that. Otherwise, we just use the file
3635 name. */
3636 if (soname == NULL || *soname == '\0')
3638 soname = elf_dt_name (abfd);
3639 if (soname == NULL || *soname == '\0')
3640 soname = bfd_get_filename (abfd);
3643 /* Save the SONAME because sometimes the linker emulation code
3644 will need to know it. */
3645 elf_dt_name (abfd) = soname;
3647 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3648 if (ret < 0)
3649 goto error_return;
3651 /* If we have already included this dynamic object in the
3652 link, just ignore it. There is no reason to include a
3653 particular dynamic object more than once. */
3654 if (ret > 0)
3655 return TRUE;
3658 /* If this is a dynamic object, we always link against the .dynsym
3659 symbol table, not the .symtab symbol table. The dynamic linker
3660 will only see the .dynsym symbol table, so there is no reason to
3661 look at .symtab for a dynamic object. */
3663 if (! dynamic || elf_dynsymtab (abfd) == 0)
3664 hdr = &elf_tdata (abfd)->symtab_hdr;
3665 else
3666 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3668 symcount = hdr->sh_size / bed->s->sizeof_sym;
3670 /* The sh_info field of the symtab header tells us where the
3671 external symbols start. We don't care about the local symbols at
3672 this point. */
3673 if (elf_bad_symtab (abfd))
3675 extsymcount = symcount;
3676 extsymoff = 0;
3678 else
3680 extsymcount = symcount - hdr->sh_info;
3681 extsymoff = hdr->sh_info;
3684 sym_hash = NULL;
3685 if (extsymcount != 0)
3687 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3688 NULL, NULL, NULL);
3689 if (isymbuf == NULL)
3690 goto error_return;
3692 /* We store a pointer to the hash table entry for each external
3693 symbol. */
3694 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3695 sym_hash = bfd_alloc (abfd, amt);
3696 if (sym_hash == NULL)
3697 goto error_free_sym;
3698 elf_sym_hashes (abfd) = sym_hash;
3701 if (dynamic)
3703 /* Read in any version definitions. */
3704 if (!_bfd_elf_slurp_version_tables (abfd,
3705 info->default_imported_symver))
3706 goto error_free_sym;
3708 /* Read in the symbol versions, but don't bother to convert them
3709 to internal format. */
3710 if (elf_dynversym (abfd) != 0)
3712 Elf_Internal_Shdr *versymhdr;
3714 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3715 extversym = bfd_malloc (versymhdr->sh_size);
3716 if (extversym == NULL)
3717 goto error_free_sym;
3718 amt = versymhdr->sh_size;
3719 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3720 || bfd_bread (extversym, amt, abfd) != amt)
3721 goto error_free_vers;
3725 /* If we are loading an as-needed shared lib, save the symbol table
3726 state before we start adding symbols. If the lib turns out
3727 to be unneeded, restore the state. */
3728 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3730 unsigned int i;
3731 size_t entsize;
3733 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3735 struct bfd_hash_entry *p;
3736 struct elf_link_hash_entry *h;
3738 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3740 h = (struct elf_link_hash_entry *) p;
3741 entsize += htab->root.table.entsize;
3742 if (h->root.type == bfd_link_hash_warning)
3743 entsize += htab->root.table.entsize;
3747 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3748 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3749 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3750 if (old_tab == NULL)
3751 goto error_free_vers;
3753 /* Remember the current objalloc pointer, so that all mem for
3754 symbols added can later be reclaimed. */
3755 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3756 if (alloc_mark == NULL)
3757 goto error_free_vers;
3759 /* Make a special call to the linker "notice" function to
3760 tell it that we are about to handle an as-needed lib. */
3761 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3762 notice_as_needed))
3763 goto error_free_vers;
3765 /* Clone the symbol table and sym hashes. Remember some
3766 pointers into the symbol table, and dynamic symbol count. */
3767 old_hash = (char *) old_tab + tabsize;
3768 old_ent = (char *) old_hash + hashsize;
3769 memcpy (old_tab, htab->root.table.table, tabsize);
3770 memcpy (old_hash, sym_hash, hashsize);
3771 old_undefs = htab->root.undefs;
3772 old_undefs_tail = htab->root.undefs_tail;
3773 old_table = htab->root.table.table;
3774 old_size = htab->root.table.size;
3775 old_count = htab->root.table.count;
3776 old_dynsymcount = htab->dynsymcount;
3778 for (i = 0; i < htab->root.table.size; i++)
3780 struct bfd_hash_entry *p;
3781 struct elf_link_hash_entry *h;
3783 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3785 memcpy (old_ent, p, htab->root.table.entsize);
3786 old_ent = (char *) old_ent + htab->root.table.entsize;
3787 h = (struct elf_link_hash_entry *) p;
3788 if (h->root.type == bfd_link_hash_warning)
3790 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3791 old_ent = (char *) old_ent + htab->root.table.entsize;
3797 weaks = NULL;
3798 ever = extversym != NULL ? extversym + extsymoff : NULL;
3799 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3800 isym < isymend;
3801 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3803 int bind;
3804 bfd_vma value;
3805 asection *sec, *new_sec;
3806 flagword flags;
3807 const char *name;
3808 struct elf_link_hash_entry *h;
3809 bfd_boolean definition;
3810 bfd_boolean size_change_ok;
3811 bfd_boolean type_change_ok;
3812 bfd_boolean new_weakdef;
3813 bfd_boolean override;
3814 bfd_boolean common;
3815 unsigned int old_alignment;
3816 bfd *old_bfd;
3818 override = FALSE;
3820 flags = BSF_NO_FLAGS;
3821 sec = NULL;
3822 value = isym->st_value;
3823 *sym_hash = NULL;
3824 common = bed->common_definition (isym);
3826 bind = ELF_ST_BIND (isym->st_info);
3827 if (bind == STB_LOCAL)
3829 /* This should be impossible, since ELF requires that all
3830 global symbols follow all local symbols, and that sh_info
3831 point to the first global symbol. Unfortunately, Irix 5
3832 screws this up. */
3833 continue;
3835 else if (bind == STB_GLOBAL)
3837 if (isym->st_shndx != SHN_UNDEF && !common)
3838 flags = BSF_GLOBAL;
3840 else if (bind == STB_WEAK)
3841 flags = BSF_WEAK;
3842 else
3844 /* Leave it up to the processor backend. */
3847 if (isym->st_shndx == SHN_UNDEF)
3848 sec = bfd_und_section_ptr;
3849 else if (isym->st_shndx == SHN_ABS)
3850 sec = bfd_abs_section_ptr;
3851 else if (isym->st_shndx == SHN_COMMON)
3853 sec = bfd_com_section_ptr;
3854 /* What ELF calls the size we call the value. What ELF
3855 calls the value we call the alignment. */
3856 value = isym->st_size;
3858 else
3860 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3861 if (sec == NULL)
3862 sec = bfd_abs_section_ptr;
3863 else if (sec->kept_section)
3865 /* Symbols from discarded section are undefined. We keep
3866 its visibility. */
3867 sec = bfd_und_section_ptr;
3868 isym->st_shndx = SHN_UNDEF;
3870 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3871 value -= sec->vma;
3874 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3875 isym->st_name);
3876 if (name == NULL)
3877 goto error_free_vers;
3879 if (isym->st_shndx == SHN_COMMON
3880 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3881 && !info->relocatable)
3883 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3885 if (tcomm == NULL)
3887 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3888 (SEC_ALLOC
3889 | SEC_IS_COMMON
3890 | SEC_LINKER_CREATED
3891 | SEC_THREAD_LOCAL));
3892 if (tcomm == NULL)
3893 goto error_free_vers;
3895 sec = tcomm;
3897 else if (bed->elf_add_symbol_hook)
3899 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3900 &sec, &value))
3901 goto error_free_vers;
3903 /* The hook function sets the name to NULL if this symbol
3904 should be skipped for some reason. */
3905 if (name == NULL)
3906 continue;
3909 /* Sanity check that all possibilities were handled. */
3910 if (sec == NULL)
3912 bfd_set_error (bfd_error_bad_value);
3913 goto error_free_vers;
3916 if (bfd_is_und_section (sec)
3917 || bfd_is_com_section (sec))
3918 definition = FALSE;
3919 else
3920 definition = TRUE;
3922 size_change_ok = FALSE;
3923 type_change_ok = bed->type_change_ok;
3924 old_alignment = 0;
3925 old_bfd = NULL;
3926 new_sec = sec;
3928 if (is_elf_hash_table (htab))
3930 Elf_Internal_Versym iver;
3931 unsigned int vernum = 0;
3932 bfd_boolean skip;
3934 if (ever == NULL)
3936 if (info->default_imported_symver)
3937 /* Use the default symbol version created earlier. */
3938 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3939 else
3940 iver.vs_vers = 0;
3942 else
3943 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3945 vernum = iver.vs_vers & VERSYM_VERSION;
3947 /* If this is a hidden symbol, or if it is not version
3948 1, we append the version name to the symbol name.
3949 However, we do not modify a non-hidden absolute symbol
3950 if it is not a function, because it might be the version
3951 symbol itself. FIXME: What if it isn't? */
3952 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3953 || (vernum > 1
3954 && (!bfd_is_abs_section (sec)
3955 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3957 const char *verstr;
3958 size_t namelen, verlen, newlen;
3959 char *newname, *p;
3961 if (isym->st_shndx != SHN_UNDEF)
3963 if (vernum > elf_tdata (abfd)->cverdefs)
3964 verstr = NULL;
3965 else if (vernum > 1)
3966 verstr =
3967 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3968 else
3969 verstr = "";
3971 if (verstr == NULL)
3973 (*_bfd_error_handler)
3974 (_("%B: %s: invalid version %u (max %d)"),
3975 abfd, name, vernum,
3976 elf_tdata (abfd)->cverdefs);
3977 bfd_set_error (bfd_error_bad_value);
3978 goto error_free_vers;
3981 else
3983 /* We cannot simply test for the number of
3984 entries in the VERNEED section since the
3985 numbers for the needed versions do not start
3986 at 0. */
3987 Elf_Internal_Verneed *t;
3989 verstr = NULL;
3990 for (t = elf_tdata (abfd)->verref;
3991 t != NULL;
3992 t = t->vn_nextref)
3994 Elf_Internal_Vernaux *a;
3996 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3998 if (a->vna_other == vernum)
4000 verstr = a->vna_nodename;
4001 break;
4004 if (a != NULL)
4005 break;
4007 if (verstr == NULL)
4009 (*_bfd_error_handler)
4010 (_("%B: %s: invalid needed version %d"),
4011 abfd, name, vernum);
4012 bfd_set_error (bfd_error_bad_value);
4013 goto error_free_vers;
4017 namelen = strlen (name);
4018 verlen = strlen (verstr);
4019 newlen = namelen + verlen + 2;
4020 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4021 && isym->st_shndx != SHN_UNDEF)
4022 ++newlen;
4024 newname = bfd_hash_allocate (&htab->root.table, newlen);
4025 if (newname == NULL)
4026 goto error_free_vers;
4027 memcpy (newname, name, namelen);
4028 p = newname + namelen;
4029 *p++ = ELF_VER_CHR;
4030 /* If this is a defined non-hidden version symbol,
4031 we add another @ to the name. This indicates the
4032 default version of the symbol. */
4033 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4034 && isym->st_shndx != SHN_UNDEF)
4035 *p++ = ELF_VER_CHR;
4036 memcpy (p, verstr, verlen + 1);
4038 name = newname;
4041 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4042 &value, &old_alignment,
4043 sym_hash, &skip, &override,
4044 &type_change_ok, &size_change_ok))
4045 goto error_free_vers;
4047 if (skip)
4048 continue;
4050 if (override)
4051 definition = FALSE;
4053 h = *sym_hash;
4054 while (h->root.type == bfd_link_hash_indirect
4055 || h->root.type == bfd_link_hash_warning)
4056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4058 /* Remember the old alignment if this is a common symbol, so
4059 that we don't reduce the alignment later on. We can't
4060 check later, because _bfd_generic_link_add_one_symbol
4061 will set a default for the alignment which we want to
4062 override. We also remember the old bfd where the existing
4063 definition comes from. */
4064 switch (h->root.type)
4066 default:
4067 break;
4069 case bfd_link_hash_defined:
4070 case bfd_link_hash_defweak:
4071 old_bfd = h->root.u.def.section->owner;
4072 break;
4074 case bfd_link_hash_common:
4075 old_bfd = h->root.u.c.p->section->owner;
4076 old_alignment = h->root.u.c.p->alignment_power;
4077 break;
4080 if (elf_tdata (abfd)->verdef != NULL
4081 && ! override
4082 && vernum > 1
4083 && definition)
4084 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4087 if (! (_bfd_generic_link_add_one_symbol
4088 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4089 (struct bfd_link_hash_entry **) sym_hash)))
4090 goto error_free_vers;
4092 h = *sym_hash;
4093 while (h->root.type == bfd_link_hash_indirect
4094 || h->root.type == bfd_link_hash_warning)
4095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4096 *sym_hash = h;
4098 new_weakdef = FALSE;
4099 if (dynamic
4100 && definition
4101 && (flags & BSF_WEAK) != 0
4102 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4103 && is_elf_hash_table (htab)
4104 && h->u.weakdef == NULL)
4106 /* Keep a list of all weak defined non function symbols from
4107 a dynamic object, using the weakdef field. Later in this
4108 function we will set the weakdef field to the correct
4109 value. We only put non-function symbols from dynamic
4110 objects on this list, because that happens to be the only
4111 time we need to know the normal symbol corresponding to a
4112 weak symbol, and the information is time consuming to
4113 figure out. If the weakdef field is not already NULL,
4114 then this symbol was already defined by some previous
4115 dynamic object, and we will be using that previous
4116 definition anyhow. */
4118 h->u.weakdef = weaks;
4119 weaks = h;
4120 new_weakdef = TRUE;
4123 /* Set the alignment of a common symbol. */
4124 if ((common || bfd_is_com_section (sec))
4125 && h->root.type == bfd_link_hash_common)
4127 unsigned int align;
4129 if (common)
4130 align = bfd_log2 (isym->st_value);
4131 else
4133 /* The new symbol is a common symbol in a shared object.
4134 We need to get the alignment from the section. */
4135 align = new_sec->alignment_power;
4137 if (align > old_alignment
4138 /* Permit an alignment power of zero if an alignment of one
4139 is specified and no other alignments have been specified. */
4140 || (isym->st_value == 1 && old_alignment == 0))
4141 h->root.u.c.p->alignment_power = align;
4142 else
4143 h->root.u.c.p->alignment_power = old_alignment;
4146 if (is_elf_hash_table (htab))
4148 bfd_boolean dynsym;
4150 /* Check the alignment when a common symbol is involved. This
4151 can change when a common symbol is overridden by a normal
4152 definition or a common symbol is ignored due to the old
4153 normal definition. We need to make sure the maximum
4154 alignment is maintained. */
4155 if ((old_alignment || common)
4156 && h->root.type != bfd_link_hash_common)
4158 unsigned int common_align;
4159 unsigned int normal_align;
4160 unsigned int symbol_align;
4161 bfd *normal_bfd;
4162 bfd *common_bfd;
4164 symbol_align = ffs (h->root.u.def.value) - 1;
4165 if (h->root.u.def.section->owner != NULL
4166 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4168 normal_align = h->root.u.def.section->alignment_power;
4169 if (normal_align > symbol_align)
4170 normal_align = symbol_align;
4172 else
4173 normal_align = symbol_align;
4175 if (old_alignment)
4177 common_align = old_alignment;
4178 common_bfd = old_bfd;
4179 normal_bfd = abfd;
4181 else
4183 common_align = bfd_log2 (isym->st_value);
4184 common_bfd = abfd;
4185 normal_bfd = old_bfd;
4188 if (normal_align < common_align)
4190 /* PR binutils/2735 */
4191 if (normal_bfd == NULL)
4192 (*_bfd_error_handler)
4193 (_("Warning: alignment %u of common symbol `%s' in %B"
4194 " is greater than the alignment (%u) of its section %A"),
4195 common_bfd, h->root.u.def.section,
4196 1 << common_align, name, 1 << normal_align);
4197 else
4198 (*_bfd_error_handler)
4199 (_("Warning: alignment %u of symbol `%s' in %B"
4200 " is smaller than %u in %B"),
4201 normal_bfd, common_bfd,
4202 1 << normal_align, name, 1 << common_align);
4206 /* Remember the symbol size if it isn't undefined. */
4207 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4208 && (definition || h->size == 0))
4210 if (h->size != 0
4211 && h->size != isym->st_size
4212 && ! size_change_ok)
4213 (*_bfd_error_handler)
4214 (_("Warning: size of symbol `%s' changed"
4215 " from %lu in %B to %lu in %B"),
4216 old_bfd, abfd,
4217 name, (unsigned long) h->size,
4218 (unsigned long) isym->st_size);
4220 h->size = isym->st_size;
4223 /* If this is a common symbol, then we always want H->SIZE
4224 to be the size of the common symbol. The code just above
4225 won't fix the size if a common symbol becomes larger. We
4226 don't warn about a size change here, because that is
4227 covered by --warn-common. Allow changed between different
4228 function types. */
4229 if (h->root.type == bfd_link_hash_common)
4230 h->size = h->root.u.c.size;
4232 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4233 && (definition || h->type == STT_NOTYPE))
4235 if (h->type != STT_NOTYPE
4236 && h->type != ELF_ST_TYPE (isym->st_info)
4237 && ! type_change_ok)
4238 (*_bfd_error_handler)
4239 (_("Warning: type of symbol `%s' changed"
4240 " from %d to %d in %B"),
4241 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4243 h->type = ELF_ST_TYPE (isym->st_info);
4246 /* If st_other has a processor-specific meaning, specific
4247 code might be needed here. We never merge the visibility
4248 attribute with the one from a dynamic object. */
4249 if (bed->elf_backend_merge_symbol_attribute)
4250 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4251 dynamic);
4253 /* If this symbol has default visibility and the user has requested
4254 we not re-export it, then mark it as hidden. */
4255 if (definition && !dynamic
4256 && (abfd->no_export
4257 || (abfd->my_archive && abfd->my_archive->no_export))
4258 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4259 isym->st_other = (STV_HIDDEN
4260 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4262 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4264 unsigned char hvis, symvis, other, nvis;
4266 /* Only merge the visibility. Leave the remainder of the
4267 st_other field to elf_backend_merge_symbol_attribute. */
4268 other = h->other & ~ELF_ST_VISIBILITY (-1);
4270 /* Combine visibilities, using the most constraining one. */
4271 hvis = ELF_ST_VISIBILITY (h->other);
4272 symvis = ELF_ST_VISIBILITY (isym->st_other);
4273 if (! hvis)
4274 nvis = symvis;
4275 else if (! symvis)
4276 nvis = hvis;
4277 else
4278 nvis = hvis < symvis ? hvis : symvis;
4280 h->other = other | nvis;
4283 /* Set a flag in the hash table entry indicating the type of
4284 reference or definition we just found. Keep a count of
4285 the number of dynamic symbols we find. A dynamic symbol
4286 is one which is referenced or defined by both a regular
4287 object and a shared object. */
4288 dynsym = FALSE;
4289 if (! dynamic)
4291 if (! definition)
4293 h->ref_regular = 1;
4294 if (bind != STB_WEAK)
4295 h->ref_regular_nonweak = 1;
4297 else
4298 h->def_regular = 1;
4299 if (! info->executable
4300 || h->def_dynamic
4301 || h->ref_dynamic)
4302 dynsym = TRUE;
4304 else
4306 if (! definition)
4307 h->ref_dynamic = 1;
4308 else
4309 h->def_dynamic = 1;
4310 if (h->def_regular
4311 || h->ref_regular
4312 || (h->u.weakdef != NULL
4313 && ! new_weakdef
4314 && h->u.weakdef->dynindx != -1))
4315 dynsym = TRUE;
4318 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4320 /* We don't want to make debug symbol dynamic. */
4321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4322 dynsym = FALSE;
4325 /* Check to see if we need to add an indirect symbol for
4326 the default name. */
4327 if (definition || h->root.type == bfd_link_hash_common)
4328 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4329 &sec, &value, &dynsym,
4330 override))
4331 goto error_free_vers;
4333 if (definition && !dynamic)
4335 char *p = strchr (name, ELF_VER_CHR);
4336 if (p != NULL && p[1] != ELF_VER_CHR)
4338 /* Queue non-default versions so that .symver x, x@FOO
4339 aliases can be checked. */
4340 if (!nondeflt_vers)
4342 amt = ((isymend - isym + 1)
4343 * sizeof (struct elf_link_hash_entry *));
4344 nondeflt_vers = bfd_malloc (amt);
4345 if (!nondeflt_vers)
4346 goto error_free_vers;
4348 nondeflt_vers[nondeflt_vers_cnt++] = h;
4352 if (dynsym && h->dynindx == -1)
4354 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4355 goto error_free_vers;
4356 if (h->u.weakdef != NULL
4357 && ! new_weakdef
4358 && h->u.weakdef->dynindx == -1)
4360 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4361 goto error_free_vers;
4364 else if (dynsym && h->dynindx != -1)
4365 /* If the symbol already has a dynamic index, but
4366 visibility says it should not be visible, turn it into
4367 a local symbol. */
4368 switch (ELF_ST_VISIBILITY (h->other))
4370 case STV_INTERNAL:
4371 case STV_HIDDEN:
4372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4373 dynsym = FALSE;
4374 break;
4377 if (!add_needed
4378 && definition
4379 && dynsym
4380 && h->ref_regular)
4382 int ret;
4383 const char *soname = elf_dt_name (abfd);
4385 /* A symbol from a library loaded via DT_NEEDED of some
4386 other library is referenced by a regular object.
4387 Add a DT_NEEDED entry for it. Issue an error if
4388 --no-add-needed is used. */
4389 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4391 (*_bfd_error_handler)
4392 (_("%s: invalid DSO for symbol `%s' definition"),
4393 abfd, name);
4394 bfd_set_error (bfd_error_bad_value);
4395 goto error_free_vers;
4398 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4400 add_needed = TRUE;
4401 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4402 if (ret < 0)
4403 goto error_free_vers;
4405 BFD_ASSERT (ret == 0);
4410 if (extversym != NULL)
4412 free (extversym);
4413 extversym = NULL;
4416 if (isymbuf != NULL)
4418 free (isymbuf);
4419 isymbuf = NULL;
4422 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4424 unsigned int i;
4426 /* Restore the symbol table. */
4427 if (bed->as_needed_cleanup)
4428 (*bed->as_needed_cleanup) (abfd, info);
4429 old_hash = (char *) old_tab + tabsize;
4430 old_ent = (char *) old_hash + hashsize;
4431 sym_hash = elf_sym_hashes (abfd);
4432 htab->root.table.table = old_table;
4433 htab->root.table.size = old_size;
4434 htab->root.table.count = old_count;
4435 memcpy (htab->root.table.table, old_tab, tabsize);
4436 memcpy (sym_hash, old_hash, hashsize);
4437 htab->root.undefs = old_undefs;
4438 htab->root.undefs_tail = old_undefs_tail;
4439 for (i = 0; i < htab->root.table.size; i++)
4441 struct bfd_hash_entry *p;
4442 struct elf_link_hash_entry *h;
4444 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4446 h = (struct elf_link_hash_entry *) p;
4447 if (h->root.type == bfd_link_hash_warning)
4448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4449 if (h->dynindx >= old_dynsymcount)
4450 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4452 memcpy (p, old_ent, htab->root.table.entsize);
4453 old_ent = (char *) old_ent + htab->root.table.entsize;
4454 h = (struct elf_link_hash_entry *) p;
4455 if (h->root.type == bfd_link_hash_warning)
4457 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4458 old_ent = (char *) old_ent + htab->root.table.entsize;
4463 /* Make a special call to the linker "notice" function to
4464 tell it that symbols added for crefs may need to be removed. */
4465 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4466 notice_not_needed))
4467 goto error_free_vers;
4469 free (old_tab);
4470 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4471 alloc_mark);
4472 if (nondeflt_vers != NULL)
4473 free (nondeflt_vers);
4474 return TRUE;
4477 if (old_tab != NULL)
4479 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4480 notice_needed))
4481 goto error_free_vers;
4482 free (old_tab);
4483 old_tab = NULL;
4486 /* Now that all the symbols from this input file are created, handle
4487 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4488 if (nondeflt_vers != NULL)
4490 bfd_size_type cnt, symidx;
4492 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4494 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4495 char *shortname, *p;
4497 p = strchr (h->root.root.string, ELF_VER_CHR);
4498 if (p == NULL
4499 || (h->root.type != bfd_link_hash_defined
4500 && h->root.type != bfd_link_hash_defweak))
4501 continue;
4503 amt = p - h->root.root.string;
4504 shortname = bfd_malloc (amt + 1);
4505 if (!shortname)
4506 goto error_free_vers;
4507 memcpy (shortname, h->root.root.string, amt);
4508 shortname[amt] = '\0';
4510 hi = (struct elf_link_hash_entry *)
4511 bfd_link_hash_lookup (&htab->root, shortname,
4512 FALSE, FALSE, FALSE);
4513 if (hi != NULL
4514 && hi->root.type == h->root.type
4515 && hi->root.u.def.value == h->root.u.def.value
4516 && hi->root.u.def.section == h->root.u.def.section)
4518 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4519 hi->root.type = bfd_link_hash_indirect;
4520 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4521 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4522 sym_hash = elf_sym_hashes (abfd);
4523 if (sym_hash)
4524 for (symidx = 0; symidx < extsymcount; ++symidx)
4525 if (sym_hash[symidx] == hi)
4527 sym_hash[symidx] = h;
4528 break;
4531 free (shortname);
4533 free (nondeflt_vers);
4534 nondeflt_vers = NULL;
4537 /* Now set the weakdefs field correctly for all the weak defined
4538 symbols we found. The only way to do this is to search all the
4539 symbols. Since we only need the information for non functions in
4540 dynamic objects, that's the only time we actually put anything on
4541 the list WEAKS. We need this information so that if a regular
4542 object refers to a symbol defined weakly in a dynamic object, the
4543 real symbol in the dynamic object is also put in the dynamic
4544 symbols; we also must arrange for both symbols to point to the
4545 same memory location. We could handle the general case of symbol
4546 aliasing, but a general symbol alias can only be generated in
4547 assembler code, handling it correctly would be very time
4548 consuming, and other ELF linkers don't handle general aliasing
4549 either. */
4550 if (weaks != NULL)
4552 struct elf_link_hash_entry **hpp;
4553 struct elf_link_hash_entry **hppend;
4554 struct elf_link_hash_entry **sorted_sym_hash;
4555 struct elf_link_hash_entry *h;
4556 size_t sym_count;
4558 /* Since we have to search the whole symbol list for each weak
4559 defined symbol, search time for N weak defined symbols will be
4560 O(N^2). Binary search will cut it down to O(NlogN). */
4561 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4562 sorted_sym_hash = bfd_malloc (amt);
4563 if (sorted_sym_hash == NULL)
4564 goto error_return;
4565 sym_hash = sorted_sym_hash;
4566 hpp = elf_sym_hashes (abfd);
4567 hppend = hpp + extsymcount;
4568 sym_count = 0;
4569 for (; hpp < hppend; hpp++)
4571 h = *hpp;
4572 if (h != NULL
4573 && h->root.type == bfd_link_hash_defined
4574 && !bed->is_function_type (h->type))
4576 *sym_hash = h;
4577 sym_hash++;
4578 sym_count++;
4582 qsort (sorted_sym_hash, sym_count,
4583 sizeof (struct elf_link_hash_entry *),
4584 elf_sort_symbol);
4586 while (weaks != NULL)
4588 struct elf_link_hash_entry *hlook;
4589 asection *slook;
4590 bfd_vma vlook;
4591 long ilook;
4592 size_t i, j, idx;
4594 hlook = weaks;
4595 weaks = hlook->u.weakdef;
4596 hlook->u.weakdef = NULL;
4598 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4599 || hlook->root.type == bfd_link_hash_defweak
4600 || hlook->root.type == bfd_link_hash_common
4601 || hlook->root.type == bfd_link_hash_indirect);
4602 slook = hlook->root.u.def.section;
4603 vlook = hlook->root.u.def.value;
4605 ilook = -1;
4606 i = 0;
4607 j = sym_count;
4608 while (i < j)
4610 bfd_signed_vma vdiff;
4611 idx = (i + j) / 2;
4612 h = sorted_sym_hash [idx];
4613 vdiff = vlook - h->root.u.def.value;
4614 if (vdiff < 0)
4615 j = idx;
4616 else if (vdiff > 0)
4617 i = idx + 1;
4618 else
4620 long sdiff = slook->id - h->root.u.def.section->id;
4621 if (sdiff < 0)
4622 j = idx;
4623 else if (sdiff > 0)
4624 i = idx + 1;
4625 else
4627 ilook = idx;
4628 break;
4633 /* We didn't find a value/section match. */
4634 if (ilook == -1)
4635 continue;
4637 for (i = ilook; i < sym_count; i++)
4639 h = sorted_sym_hash [i];
4641 /* Stop if value or section doesn't match. */
4642 if (h->root.u.def.value != vlook
4643 || h->root.u.def.section != slook)
4644 break;
4645 else if (h != hlook)
4647 hlook->u.weakdef = h;
4649 /* If the weak definition is in the list of dynamic
4650 symbols, make sure the real definition is put
4651 there as well. */
4652 if (hlook->dynindx != -1 && h->dynindx == -1)
4654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4656 err_free_sym_hash:
4657 free (sorted_sym_hash);
4658 goto error_return;
4662 /* If the real definition is in the list of dynamic
4663 symbols, make sure the weak definition is put
4664 there as well. If we don't do this, then the
4665 dynamic loader might not merge the entries for the
4666 real definition and the weak definition. */
4667 if (h->dynindx != -1 && hlook->dynindx == -1)
4669 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4670 goto err_free_sym_hash;
4672 break;
4677 free (sorted_sym_hash);
4680 if (bed->check_directives
4681 && !(*bed->check_directives) (abfd, info))
4682 return FALSE;
4684 /* If this object is the same format as the output object, and it is
4685 not a shared library, then let the backend look through the
4686 relocs.
4688 This is required to build global offset table entries and to
4689 arrange for dynamic relocs. It is not required for the
4690 particular common case of linking non PIC code, even when linking
4691 against shared libraries, but unfortunately there is no way of
4692 knowing whether an object file has been compiled PIC or not.
4693 Looking through the relocs is not particularly time consuming.
4694 The problem is that we must either (1) keep the relocs in memory,
4695 which causes the linker to require additional runtime memory or
4696 (2) read the relocs twice from the input file, which wastes time.
4697 This would be a good case for using mmap.
4699 I have no idea how to handle linking PIC code into a file of a
4700 different format. It probably can't be done. */
4701 if (! dynamic
4702 && is_elf_hash_table (htab)
4703 && bed->check_relocs != NULL
4704 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4706 asection *o;
4708 for (o = abfd->sections; o != NULL; o = o->next)
4710 Elf_Internal_Rela *internal_relocs;
4711 bfd_boolean ok;
4713 if ((o->flags & SEC_RELOC) == 0
4714 || o->reloc_count == 0
4715 || ((info->strip == strip_all || info->strip == strip_debugger)
4716 && (o->flags & SEC_DEBUGGING) != 0)
4717 || bfd_is_abs_section (o->output_section))
4718 continue;
4720 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4721 info->keep_memory);
4722 if (internal_relocs == NULL)
4723 goto error_return;
4725 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4727 if (elf_section_data (o)->relocs != internal_relocs)
4728 free (internal_relocs);
4730 if (! ok)
4731 goto error_return;
4735 /* If this is a non-traditional link, try to optimize the handling
4736 of the .stab/.stabstr sections. */
4737 if (! dynamic
4738 && ! info->traditional_format
4739 && is_elf_hash_table (htab)
4740 && (info->strip != strip_all && info->strip != strip_debugger))
4742 asection *stabstr;
4744 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4745 if (stabstr != NULL)
4747 bfd_size_type string_offset = 0;
4748 asection *stab;
4750 for (stab = abfd->sections; stab; stab = stab->next)
4751 if (CONST_STRNEQ (stab->name, ".stab")
4752 && (!stab->name[5] ||
4753 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4754 && (stab->flags & SEC_MERGE) == 0
4755 && !bfd_is_abs_section (stab->output_section))
4757 struct bfd_elf_section_data *secdata;
4759 secdata = elf_section_data (stab);
4760 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4761 stabstr, &secdata->sec_info,
4762 &string_offset))
4763 goto error_return;
4764 if (secdata->sec_info)
4765 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4770 if (is_elf_hash_table (htab) && add_needed)
4772 /* Add this bfd to the loaded list. */
4773 struct elf_link_loaded_list *n;
4775 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4776 if (n == NULL)
4777 goto error_return;
4778 n->abfd = abfd;
4779 n->next = htab->loaded;
4780 htab->loaded = n;
4783 return TRUE;
4785 error_free_vers:
4786 if (old_tab != NULL)
4787 free (old_tab);
4788 if (nondeflt_vers != NULL)
4789 free (nondeflt_vers);
4790 if (extversym != NULL)
4791 free (extversym);
4792 error_free_sym:
4793 if (isymbuf != NULL)
4794 free (isymbuf);
4795 error_return:
4796 return FALSE;
4799 /* Return the linker hash table entry of a symbol that might be
4800 satisfied by an archive symbol. Return -1 on error. */
4802 struct elf_link_hash_entry *
4803 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4804 struct bfd_link_info *info,
4805 const char *name)
4807 struct elf_link_hash_entry *h;
4808 char *p, *copy;
4809 size_t len, first;
4811 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4812 if (h != NULL)
4813 return h;
4815 /* If this is a default version (the name contains @@), look up the
4816 symbol again with only one `@' as well as without the version.
4817 The effect is that references to the symbol with and without the
4818 version will be matched by the default symbol in the archive. */
4820 p = strchr (name, ELF_VER_CHR);
4821 if (p == NULL || p[1] != ELF_VER_CHR)
4822 return h;
4824 /* First check with only one `@'. */
4825 len = strlen (name);
4826 copy = bfd_alloc (abfd, len);
4827 if (copy == NULL)
4828 return (struct elf_link_hash_entry *) 0 - 1;
4830 first = p - name + 1;
4831 memcpy (copy, name, first);
4832 memcpy (copy + first, name + first + 1, len - first);
4834 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4835 if (h == NULL)
4837 /* We also need to check references to the symbol without the
4838 version. */
4839 copy[first - 1] = '\0';
4840 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4841 FALSE, FALSE, FALSE);
4844 bfd_release (abfd, copy);
4845 return h;
4848 /* Add symbols from an ELF archive file to the linker hash table. We
4849 don't use _bfd_generic_link_add_archive_symbols because of a
4850 problem which arises on UnixWare. The UnixWare libc.so is an
4851 archive which includes an entry libc.so.1 which defines a bunch of
4852 symbols. The libc.so archive also includes a number of other
4853 object files, which also define symbols, some of which are the same
4854 as those defined in libc.so.1. Correct linking requires that we
4855 consider each object file in turn, and include it if it defines any
4856 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4857 this; it looks through the list of undefined symbols, and includes
4858 any object file which defines them. When this algorithm is used on
4859 UnixWare, it winds up pulling in libc.so.1 early and defining a
4860 bunch of symbols. This means that some of the other objects in the
4861 archive are not included in the link, which is incorrect since they
4862 precede libc.so.1 in the archive.
4864 Fortunately, ELF archive handling is simpler than that done by
4865 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4866 oddities. In ELF, if we find a symbol in the archive map, and the
4867 symbol is currently undefined, we know that we must pull in that
4868 object file.
4870 Unfortunately, we do have to make multiple passes over the symbol
4871 table until nothing further is resolved. */
4873 static bfd_boolean
4874 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4876 symindex c;
4877 bfd_boolean *defined = NULL;
4878 bfd_boolean *included = NULL;
4879 carsym *symdefs;
4880 bfd_boolean loop;
4881 bfd_size_type amt;
4882 const struct elf_backend_data *bed;
4883 struct elf_link_hash_entry * (*archive_symbol_lookup)
4884 (bfd *, struct bfd_link_info *, const char *);
4886 if (! bfd_has_map (abfd))
4888 /* An empty archive is a special case. */
4889 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4890 return TRUE;
4891 bfd_set_error (bfd_error_no_armap);
4892 return FALSE;
4895 /* Keep track of all symbols we know to be already defined, and all
4896 files we know to be already included. This is to speed up the
4897 second and subsequent passes. */
4898 c = bfd_ardata (abfd)->symdef_count;
4899 if (c == 0)
4900 return TRUE;
4901 amt = c;
4902 amt *= sizeof (bfd_boolean);
4903 defined = bfd_zmalloc (amt);
4904 included = bfd_zmalloc (amt);
4905 if (defined == NULL || included == NULL)
4906 goto error_return;
4908 symdefs = bfd_ardata (abfd)->symdefs;
4909 bed = get_elf_backend_data (abfd);
4910 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4914 file_ptr last;
4915 symindex i;
4916 carsym *symdef;
4917 carsym *symdefend;
4919 loop = FALSE;
4920 last = -1;
4922 symdef = symdefs;
4923 symdefend = symdef + c;
4924 for (i = 0; symdef < symdefend; symdef++, i++)
4926 struct elf_link_hash_entry *h;
4927 bfd *element;
4928 struct bfd_link_hash_entry *undefs_tail;
4929 symindex mark;
4931 if (defined[i] || included[i])
4932 continue;
4933 if (symdef->file_offset == last)
4935 included[i] = TRUE;
4936 continue;
4939 h = archive_symbol_lookup (abfd, info, symdef->name);
4940 if (h == (struct elf_link_hash_entry *) 0 - 1)
4941 goto error_return;
4943 if (h == NULL)
4944 continue;
4946 if (h->root.type == bfd_link_hash_common)
4948 /* We currently have a common symbol. The archive map contains
4949 a reference to this symbol, so we may want to include it. We
4950 only want to include it however, if this archive element
4951 contains a definition of the symbol, not just another common
4952 declaration of it.
4954 Unfortunately some archivers (including GNU ar) will put
4955 declarations of common symbols into their archive maps, as
4956 well as real definitions, so we cannot just go by the archive
4957 map alone. Instead we must read in the element's symbol
4958 table and check that to see what kind of symbol definition
4959 this is. */
4960 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4961 continue;
4963 else if (h->root.type != bfd_link_hash_undefined)
4965 if (h->root.type != bfd_link_hash_undefweak)
4966 defined[i] = TRUE;
4967 continue;
4970 /* We need to include this archive member. */
4971 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4972 if (element == NULL)
4973 goto error_return;
4975 if (! bfd_check_format (element, bfd_object))
4976 goto error_return;
4978 /* Doublecheck that we have not included this object
4979 already--it should be impossible, but there may be
4980 something wrong with the archive. */
4981 if (element->archive_pass != 0)
4983 bfd_set_error (bfd_error_bad_value);
4984 goto error_return;
4986 element->archive_pass = 1;
4988 undefs_tail = info->hash->undefs_tail;
4990 if (! (*info->callbacks->add_archive_element) (info, element,
4991 symdef->name))
4992 goto error_return;
4993 if (! bfd_link_add_symbols (element, info))
4994 goto error_return;
4996 /* If there are any new undefined symbols, we need to make
4997 another pass through the archive in order to see whether
4998 they can be defined. FIXME: This isn't perfect, because
4999 common symbols wind up on undefs_tail and because an
5000 undefined symbol which is defined later on in this pass
5001 does not require another pass. This isn't a bug, but it
5002 does make the code less efficient than it could be. */
5003 if (undefs_tail != info->hash->undefs_tail)
5004 loop = TRUE;
5006 /* Look backward to mark all symbols from this object file
5007 which we have already seen in this pass. */
5008 mark = i;
5011 included[mark] = TRUE;
5012 if (mark == 0)
5013 break;
5014 --mark;
5016 while (symdefs[mark].file_offset == symdef->file_offset);
5018 /* We mark subsequent symbols from this object file as we go
5019 on through the loop. */
5020 last = symdef->file_offset;
5023 while (loop);
5025 free (defined);
5026 free (included);
5028 return TRUE;
5030 error_return:
5031 if (defined != NULL)
5032 free (defined);
5033 if (included != NULL)
5034 free (included);
5035 return FALSE;
5038 /* Given an ELF BFD, add symbols to the global hash table as
5039 appropriate. */
5041 bfd_boolean
5042 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5044 switch (bfd_get_format (abfd))
5046 case bfd_object:
5047 return elf_link_add_object_symbols (abfd, info);
5048 case bfd_archive:
5049 return elf_link_add_archive_symbols (abfd, info);
5050 default:
5051 bfd_set_error (bfd_error_wrong_format);
5052 return FALSE;
5056 struct hash_codes_info
5058 unsigned long *hashcodes;
5059 bfd_boolean error;
5062 /* This function will be called though elf_link_hash_traverse to store
5063 all hash value of the exported symbols in an array. */
5065 static bfd_boolean
5066 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5068 struct hash_codes_info *inf = data;
5069 const char *name;
5070 char *p;
5071 unsigned long ha;
5072 char *alc = NULL;
5074 if (h->root.type == bfd_link_hash_warning)
5075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5077 /* Ignore indirect symbols. These are added by the versioning code. */
5078 if (h->dynindx == -1)
5079 return TRUE;
5081 name = h->root.root.string;
5082 p = strchr (name, ELF_VER_CHR);
5083 if (p != NULL)
5085 alc = bfd_malloc (p - name + 1);
5086 if (alc == NULL)
5088 inf->error = TRUE;
5089 return FALSE;
5091 memcpy (alc, name, p - name);
5092 alc[p - name] = '\0';
5093 name = alc;
5096 /* Compute the hash value. */
5097 ha = bfd_elf_hash (name);
5099 /* Store the found hash value in the array given as the argument. */
5100 *(inf->hashcodes)++ = ha;
5102 /* And store it in the struct so that we can put it in the hash table
5103 later. */
5104 h->u.elf_hash_value = ha;
5106 if (alc != NULL)
5107 free (alc);
5109 return TRUE;
5112 struct collect_gnu_hash_codes
5114 bfd *output_bfd;
5115 const struct elf_backend_data *bed;
5116 unsigned long int nsyms;
5117 unsigned long int maskbits;
5118 unsigned long int *hashcodes;
5119 unsigned long int *hashval;
5120 unsigned long int *indx;
5121 unsigned long int *counts;
5122 bfd_vma *bitmask;
5123 bfd_byte *contents;
5124 long int min_dynindx;
5125 unsigned long int bucketcount;
5126 unsigned long int symindx;
5127 long int local_indx;
5128 long int shift1, shift2;
5129 unsigned long int mask;
5130 bfd_boolean error;
5133 /* This function will be called though elf_link_hash_traverse to store
5134 all hash value of the exported symbols in an array. */
5136 static bfd_boolean
5137 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5139 struct collect_gnu_hash_codes *s = data;
5140 const char *name;
5141 char *p;
5142 unsigned long ha;
5143 char *alc = NULL;
5145 if (h->root.type == bfd_link_hash_warning)
5146 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5148 /* Ignore indirect symbols. These are added by the versioning code. */
5149 if (h->dynindx == -1)
5150 return TRUE;
5152 /* Ignore also local symbols and undefined symbols. */
5153 if (! (*s->bed->elf_hash_symbol) (h))
5154 return TRUE;
5156 name = h->root.root.string;
5157 p = strchr (name, ELF_VER_CHR);
5158 if (p != NULL)
5160 alc = bfd_malloc (p - name + 1);
5161 if (alc == NULL)
5163 s->error = TRUE;
5164 return FALSE;
5166 memcpy (alc, name, p - name);
5167 alc[p - name] = '\0';
5168 name = alc;
5171 /* Compute the hash value. */
5172 ha = bfd_elf_gnu_hash (name);
5174 /* Store the found hash value in the array for compute_bucket_count,
5175 and also for .dynsym reordering purposes. */
5176 s->hashcodes[s->nsyms] = ha;
5177 s->hashval[h->dynindx] = ha;
5178 ++s->nsyms;
5179 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5180 s->min_dynindx = h->dynindx;
5182 if (alc != NULL)
5183 free (alc);
5185 return TRUE;
5188 /* This function will be called though elf_link_hash_traverse to do
5189 final dynaminc symbol renumbering. */
5191 static bfd_boolean
5192 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5194 struct collect_gnu_hash_codes *s = data;
5195 unsigned long int bucket;
5196 unsigned long int val;
5198 if (h->root.type == bfd_link_hash_warning)
5199 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5201 /* Ignore indirect symbols. */
5202 if (h->dynindx == -1)
5203 return TRUE;
5205 /* Ignore also local symbols and undefined symbols. */
5206 if (! (*s->bed->elf_hash_symbol) (h))
5208 if (h->dynindx >= s->min_dynindx)
5209 h->dynindx = s->local_indx++;
5210 return TRUE;
5213 bucket = s->hashval[h->dynindx] % s->bucketcount;
5214 val = (s->hashval[h->dynindx] >> s->shift1)
5215 & ((s->maskbits >> s->shift1) - 1);
5216 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5217 s->bitmask[val]
5218 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5219 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5220 if (s->counts[bucket] == 1)
5221 /* Last element terminates the chain. */
5222 val |= 1;
5223 bfd_put_32 (s->output_bfd, val,
5224 s->contents + (s->indx[bucket] - s->symindx) * 4);
5225 --s->counts[bucket];
5226 h->dynindx = s->indx[bucket]++;
5227 return TRUE;
5230 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5232 bfd_boolean
5233 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5235 return !(h->forced_local
5236 || h->root.type == bfd_link_hash_undefined
5237 || h->root.type == bfd_link_hash_undefweak
5238 || ((h->root.type == bfd_link_hash_defined
5239 || h->root.type == bfd_link_hash_defweak)
5240 && h->root.u.def.section->output_section == NULL));
5243 /* Array used to determine the number of hash table buckets to use
5244 based on the number of symbols there are. If there are fewer than
5245 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5246 fewer than 37 we use 17 buckets, and so forth. We never use more
5247 than 32771 buckets. */
5249 static const size_t elf_buckets[] =
5251 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5252 16411, 32771, 0
5255 /* Compute bucket count for hashing table. We do not use a static set
5256 of possible tables sizes anymore. Instead we determine for all
5257 possible reasonable sizes of the table the outcome (i.e., the
5258 number of collisions etc) and choose the best solution. The
5259 weighting functions are not too simple to allow the table to grow
5260 without bounds. Instead one of the weighting factors is the size.
5261 Therefore the result is always a good payoff between few collisions
5262 (= short chain lengths) and table size. */
5263 static size_t
5264 compute_bucket_count (struct bfd_link_info *info,
5265 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5266 unsigned long int nsyms,
5267 int gnu_hash)
5269 size_t best_size = 0;
5270 unsigned long int i;
5272 /* We have a problem here. The following code to optimize the table
5273 size requires an integer type with more the 32 bits. If
5274 BFD_HOST_U_64_BIT is set we know about such a type. */
5275 #ifdef BFD_HOST_U_64_BIT
5276 if (info->optimize)
5278 size_t minsize;
5279 size_t maxsize;
5280 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5281 bfd *dynobj = elf_hash_table (info)->dynobj;
5282 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5283 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5284 unsigned long int *counts;
5285 bfd_size_type amt;
5287 /* Possible optimization parameters: if we have NSYMS symbols we say
5288 that the hashing table must at least have NSYMS/4 and at most
5289 2*NSYMS buckets. */
5290 minsize = nsyms / 4;
5291 if (minsize == 0)
5292 minsize = 1;
5293 best_size = maxsize = nsyms * 2;
5294 if (gnu_hash)
5296 if (minsize < 2)
5297 minsize = 2;
5298 if ((best_size & 31) == 0)
5299 ++best_size;
5302 /* Create array where we count the collisions in. We must use bfd_malloc
5303 since the size could be large. */
5304 amt = maxsize;
5305 amt *= sizeof (unsigned long int);
5306 counts = bfd_malloc (amt);
5307 if (counts == NULL)
5308 return 0;
5310 /* Compute the "optimal" size for the hash table. The criteria is a
5311 minimal chain length. The minor criteria is (of course) the size
5312 of the table. */
5313 for (i = minsize; i < maxsize; ++i)
5315 /* Walk through the array of hashcodes and count the collisions. */
5316 BFD_HOST_U_64_BIT max;
5317 unsigned long int j;
5318 unsigned long int fact;
5320 if (gnu_hash && (i & 31) == 0)
5321 continue;
5323 memset (counts, '\0', i * sizeof (unsigned long int));
5325 /* Determine how often each hash bucket is used. */
5326 for (j = 0; j < nsyms; ++j)
5327 ++counts[hashcodes[j] % i];
5329 /* For the weight function we need some information about the
5330 pagesize on the target. This is information need not be 100%
5331 accurate. Since this information is not available (so far) we
5332 define it here to a reasonable default value. If it is crucial
5333 to have a better value some day simply define this value. */
5334 # ifndef BFD_TARGET_PAGESIZE
5335 # define BFD_TARGET_PAGESIZE (4096)
5336 # endif
5338 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5339 and the chains. */
5340 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5342 # if 1
5343 /* Variant 1: optimize for short chains. We add the squares
5344 of all the chain lengths (which favors many small chain
5345 over a few long chains). */
5346 for (j = 0; j < i; ++j)
5347 max += counts[j] * counts[j];
5349 /* This adds penalties for the overall size of the table. */
5350 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5351 max *= fact * fact;
5352 # else
5353 /* Variant 2: Optimize a lot more for small table. Here we
5354 also add squares of the size but we also add penalties for
5355 empty slots (the +1 term). */
5356 for (j = 0; j < i; ++j)
5357 max += (1 + counts[j]) * (1 + counts[j]);
5359 /* The overall size of the table is considered, but not as
5360 strong as in variant 1, where it is squared. */
5361 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5362 max *= fact;
5363 # endif
5365 /* Compare with current best results. */
5366 if (max < best_chlen)
5368 best_chlen = max;
5369 best_size = i;
5373 free (counts);
5375 else
5376 #endif /* defined (BFD_HOST_U_64_BIT) */
5378 /* This is the fallback solution if no 64bit type is available or if we
5379 are not supposed to spend much time on optimizations. We select the
5380 bucket count using a fixed set of numbers. */
5381 for (i = 0; elf_buckets[i] != 0; i++)
5383 best_size = elf_buckets[i];
5384 if (nsyms < elf_buckets[i + 1])
5385 break;
5387 if (gnu_hash && best_size < 2)
5388 best_size = 2;
5391 return best_size;
5394 /* Set up the sizes and contents of the ELF dynamic sections. This is
5395 called by the ELF linker emulation before_allocation routine. We
5396 must set the sizes of the sections before the linker sets the
5397 addresses of the various sections. */
5399 bfd_boolean
5400 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5401 const char *soname,
5402 const char *rpath,
5403 const char *filter_shlib,
5404 const char * const *auxiliary_filters,
5405 struct bfd_link_info *info,
5406 asection **sinterpptr,
5407 struct bfd_elf_version_tree *verdefs)
5409 bfd_size_type soname_indx;
5410 bfd *dynobj;
5411 const struct elf_backend_data *bed;
5412 struct elf_assign_sym_version_info asvinfo;
5414 *sinterpptr = NULL;
5416 soname_indx = (bfd_size_type) -1;
5418 if (!is_elf_hash_table (info->hash))
5419 return TRUE;
5421 bed = get_elf_backend_data (output_bfd);
5422 if (info->execstack)
5423 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5424 else if (info->noexecstack)
5425 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5426 else
5428 bfd *inputobj;
5429 asection *notesec = NULL;
5430 int exec = 0;
5432 for (inputobj = info->input_bfds;
5433 inputobj;
5434 inputobj = inputobj->link_next)
5436 asection *s;
5438 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5439 continue;
5440 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5441 if (s)
5443 if (s->flags & SEC_CODE)
5444 exec = PF_X;
5445 notesec = s;
5447 else if (bed->default_execstack)
5448 exec = PF_X;
5450 if (notesec)
5452 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5453 if (exec && info->relocatable
5454 && notesec->output_section != bfd_abs_section_ptr)
5455 notesec->output_section->flags |= SEC_CODE;
5459 /* Any syms created from now on start with -1 in
5460 got.refcount/offset and plt.refcount/offset. */
5461 elf_hash_table (info)->init_got_refcount
5462 = elf_hash_table (info)->init_got_offset;
5463 elf_hash_table (info)->init_plt_refcount
5464 = elf_hash_table (info)->init_plt_offset;
5466 /* The backend may have to create some sections regardless of whether
5467 we're dynamic or not. */
5468 if (bed->elf_backend_always_size_sections
5469 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5470 return FALSE;
5472 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5473 return FALSE;
5475 dynobj = elf_hash_table (info)->dynobj;
5477 /* If there were no dynamic objects in the link, there is nothing to
5478 do here. */
5479 if (dynobj == NULL)
5480 return TRUE;
5482 if (elf_hash_table (info)->dynamic_sections_created)
5484 struct elf_info_failed eif;
5485 struct elf_link_hash_entry *h;
5486 asection *dynstr;
5487 struct bfd_elf_version_tree *t;
5488 struct bfd_elf_version_expr *d;
5489 asection *s;
5490 bfd_boolean all_defined;
5492 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5493 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5495 if (soname != NULL)
5497 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5498 soname, TRUE);
5499 if (soname_indx == (bfd_size_type) -1
5500 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5501 return FALSE;
5504 if (info->symbolic)
5506 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5507 return FALSE;
5508 info->flags |= DF_SYMBOLIC;
5511 if (rpath != NULL)
5513 bfd_size_type indx;
5515 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5516 TRUE);
5517 if (indx == (bfd_size_type) -1
5518 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5519 return FALSE;
5521 if (info->new_dtags)
5523 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5524 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5525 return FALSE;
5529 if (filter_shlib != NULL)
5531 bfd_size_type indx;
5533 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5534 filter_shlib, TRUE);
5535 if (indx == (bfd_size_type) -1
5536 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5537 return FALSE;
5540 if (auxiliary_filters != NULL)
5542 const char * const *p;
5544 for (p = auxiliary_filters; *p != NULL; p++)
5546 bfd_size_type indx;
5548 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5549 *p, TRUE);
5550 if (indx == (bfd_size_type) -1
5551 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5552 return FALSE;
5556 eif.info = info;
5557 eif.verdefs = verdefs;
5558 eif.failed = FALSE;
5560 /* If we are supposed to export all symbols into the dynamic symbol
5561 table (this is not the normal case), then do so. */
5562 if (info->export_dynamic
5563 || (info->executable && info->dynamic))
5565 elf_link_hash_traverse (elf_hash_table (info),
5566 _bfd_elf_export_symbol,
5567 &eif);
5568 if (eif.failed)
5569 return FALSE;
5572 /* Make all global versions with definition. */
5573 for (t = verdefs; t != NULL; t = t->next)
5574 for (d = t->globals.list; d != NULL; d = d->next)
5575 if (!d->symver && d->literal)
5577 const char *verstr, *name;
5578 size_t namelen, verlen, newlen;
5579 char *newname, *p;
5580 struct elf_link_hash_entry *newh;
5582 name = d->pattern;
5583 namelen = strlen (name);
5584 verstr = t->name;
5585 verlen = strlen (verstr);
5586 newlen = namelen + verlen + 3;
5588 newname = bfd_malloc (newlen);
5589 if (newname == NULL)
5590 return FALSE;
5591 memcpy (newname, name, namelen);
5593 /* Check the hidden versioned definition. */
5594 p = newname + namelen;
5595 *p++ = ELF_VER_CHR;
5596 memcpy (p, verstr, verlen + 1);
5597 newh = elf_link_hash_lookup (elf_hash_table (info),
5598 newname, FALSE, FALSE,
5599 FALSE);
5600 if (newh == NULL
5601 || (newh->root.type != bfd_link_hash_defined
5602 && newh->root.type != bfd_link_hash_defweak))
5604 /* Check the default versioned definition. */
5605 *p++ = ELF_VER_CHR;
5606 memcpy (p, verstr, verlen + 1);
5607 newh = elf_link_hash_lookup (elf_hash_table (info),
5608 newname, FALSE, FALSE,
5609 FALSE);
5611 free (newname);
5613 /* Mark this version if there is a definition and it is
5614 not defined in a shared object. */
5615 if (newh != NULL
5616 && !newh->def_dynamic
5617 && (newh->root.type == bfd_link_hash_defined
5618 || newh->root.type == bfd_link_hash_defweak))
5619 d->symver = 1;
5622 /* Attach all the symbols to their version information. */
5623 asvinfo.output_bfd = output_bfd;
5624 asvinfo.info = info;
5625 asvinfo.verdefs = verdefs;
5626 asvinfo.failed = FALSE;
5628 elf_link_hash_traverse (elf_hash_table (info),
5629 _bfd_elf_link_assign_sym_version,
5630 &asvinfo);
5631 if (asvinfo.failed)
5632 return FALSE;
5634 if (!info->allow_undefined_version)
5636 /* Check if all global versions have a definition. */
5637 all_defined = TRUE;
5638 for (t = verdefs; t != NULL; t = t->next)
5639 for (d = t->globals.list; d != NULL; d = d->next)
5640 if (d->literal && !d->symver && !d->script)
5642 (*_bfd_error_handler)
5643 (_("%s: undefined version: %s"),
5644 d->pattern, t->name);
5645 all_defined = FALSE;
5648 if (!all_defined)
5650 bfd_set_error (bfd_error_bad_value);
5651 return FALSE;
5655 /* Find all symbols which were defined in a dynamic object and make
5656 the backend pick a reasonable value for them. */
5657 elf_link_hash_traverse (elf_hash_table (info),
5658 _bfd_elf_adjust_dynamic_symbol,
5659 &eif);
5660 if (eif.failed)
5661 return FALSE;
5663 /* Add some entries to the .dynamic section. We fill in some of the
5664 values later, in bfd_elf_final_link, but we must add the entries
5665 now so that we know the final size of the .dynamic section. */
5667 /* If there are initialization and/or finalization functions to
5668 call then add the corresponding DT_INIT/DT_FINI entries. */
5669 h = (info->init_function
5670 ? elf_link_hash_lookup (elf_hash_table (info),
5671 info->init_function, FALSE,
5672 FALSE, FALSE)
5673 : NULL);
5674 if (h != NULL
5675 && (h->ref_regular
5676 || h->def_regular))
5678 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5679 return FALSE;
5681 h = (info->fini_function
5682 ? elf_link_hash_lookup (elf_hash_table (info),
5683 info->fini_function, FALSE,
5684 FALSE, FALSE)
5685 : NULL);
5686 if (h != NULL
5687 && (h->ref_regular
5688 || h->def_regular))
5690 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5691 return FALSE;
5694 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5695 if (s != NULL && s->linker_has_input)
5697 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5698 if (! info->executable)
5700 bfd *sub;
5701 asection *o;
5703 for (sub = info->input_bfds; sub != NULL;
5704 sub = sub->link_next)
5705 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5706 for (o = sub->sections; o != NULL; o = o->next)
5707 if (elf_section_data (o)->this_hdr.sh_type
5708 == SHT_PREINIT_ARRAY)
5710 (*_bfd_error_handler)
5711 (_("%B: .preinit_array section is not allowed in DSO"),
5712 sub);
5713 break;
5716 bfd_set_error (bfd_error_nonrepresentable_section);
5717 return FALSE;
5720 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5721 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5722 return FALSE;
5724 s = bfd_get_section_by_name (output_bfd, ".init_array");
5725 if (s != NULL && s->linker_has_input)
5727 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5728 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5729 return FALSE;
5731 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5732 if (s != NULL && s->linker_has_input)
5734 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5735 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5736 return FALSE;
5739 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5740 /* If .dynstr is excluded from the link, we don't want any of
5741 these tags. Strictly, we should be checking each section
5742 individually; This quick check covers for the case where
5743 someone does a /DISCARD/ : { *(*) }. */
5744 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5746 bfd_size_type strsize;
5748 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5749 if ((info->emit_hash
5750 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5751 || (info->emit_gnu_hash
5752 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5753 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5754 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5755 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5756 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5757 bed->s->sizeof_sym))
5758 return FALSE;
5762 /* The backend must work out the sizes of all the other dynamic
5763 sections. */
5764 if (bed->elf_backend_size_dynamic_sections
5765 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5766 return FALSE;
5768 if (elf_hash_table (info)->dynamic_sections_created)
5770 unsigned long section_sym_count;
5771 asection *s;
5773 /* Set up the version definition section. */
5774 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5775 BFD_ASSERT (s != NULL);
5777 /* We may have created additional version definitions if we are
5778 just linking a regular application. */
5779 verdefs = asvinfo.verdefs;
5781 /* Skip anonymous version tag. */
5782 if (verdefs != NULL && verdefs->vernum == 0)
5783 verdefs = verdefs->next;
5785 if (verdefs == NULL && !info->create_default_symver)
5786 s->flags |= SEC_EXCLUDE;
5787 else
5789 unsigned int cdefs;
5790 bfd_size_type size;
5791 struct bfd_elf_version_tree *t;
5792 bfd_byte *p;
5793 Elf_Internal_Verdef def;
5794 Elf_Internal_Verdaux defaux;
5795 struct bfd_link_hash_entry *bh;
5796 struct elf_link_hash_entry *h;
5797 const char *name;
5799 cdefs = 0;
5800 size = 0;
5802 /* Make space for the base version. */
5803 size += sizeof (Elf_External_Verdef);
5804 size += sizeof (Elf_External_Verdaux);
5805 ++cdefs;
5807 /* Make space for the default version. */
5808 if (info->create_default_symver)
5810 size += sizeof (Elf_External_Verdef);
5811 ++cdefs;
5814 for (t = verdefs; t != NULL; t = t->next)
5816 struct bfd_elf_version_deps *n;
5818 size += sizeof (Elf_External_Verdef);
5819 size += sizeof (Elf_External_Verdaux);
5820 ++cdefs;
5822 for (n = t->deps; n != NULL; n = n->next)
5823 size += sizeof (Elf_External_Verdaux);
5826 s->size = size;
5827 s->contents = bfd_alloc (output_bfd, s->size);
5828 if (s->contents == NULL && s->size != 0)
5829 return FALSE;
5831 /* Fill in the version definition section. */
5833 p = s->contents;
5835 def.vd_version = VER_DEF_CURRENT;
5836 def.vd_flags = VER_FLG_BASE;
5837 def.vd_ndx = 1;
5838 def.vd_cnt = 1;
5839 if (info->create_default_symver)
5841 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5842 def.vd_next = sizeof (Elf_External_Verdef);
5844 else
5846 def.vd_aux = sizeof (Elf_External_Verdef);
5847 def.vd_next = (sizeof (Elf_External_Verdef)
5848 + sizeof (Elf_External_Verdaux));
5851 if (soname_indx != (bfd_size_type) -1)
5853 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5854 soname_indx);
5855 def.vd_hash = bfd_elf_hash (soname);
5856 defaux.vda_name = soname_indx;
5857 name = soname;
5859 else
5861 bfd_size_type indx;
5863 name = lbasename (output_bfd->filename);
5864 def.vd_hash = bfd_elf_hash (name);
5865 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5866 name, FALSE);
5867 if (indx == (bfd_size_type) -1)
5868 return FALSE;
5869 defaux.vda_name = indx;
5871 defaux.vda_next = 0;
5873 _bfd_elf_swap_verdef_out (output_bfd, &def,
5874 (Elf_External_Verdef *) p);
5875 p += sizeof (Elf_External_Verdef);
5876 if (info->create_default_symver)
5878 /* Add a symbol representing this version. */
5879 bh = NULL;
5880 if (! (_bfd_generic_link_add_one_symbol
5881 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5882 0, NULL, FALSE,
5883 get_elf_backend_data (dynobj)->collect, &bh)))
5884 return FALSE;
5885 h = (struct elf_link_hash_entry *) bh;
5886 h->non_elf = 0;
5887 h->def_regular = 1;
5888 h->type = STT_OBJECT;
5889 h->verinfo.vertree = NULL;
5891 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5892 return FALSE;
5894 /* Create a duplicate of the base version with the same
5895 aux block, but different flags. */
5896 def.vd_flags = 0;
5897 def.vd_ndx = 2;
5898 def.vd_aux = sizeof (Elf_External_Verdef);
5899 if (verdefs)
5900 def.vd_next = (sizeof (Elf_External_Verdef)
5901 + sizeof (Elf_External_Verdaux));
5902 else
5903 def.vd_next = 0;
5904 _bfd_elf_swap_verdef_out (output_bfd, &def,
5905 (Elf_External_Verdef *) p);
5906 p += sizeof (Elf_External_Verdef);
5908 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5909 (Elf_External_Verdaux *) p);
5910 p += sizeof (Elf_External_Verdaux);
5912 for (t = verdefs; t != NULL; t = t->next)
5914 unsigned int cdeps;
5915 struct bfd_elf_version_deps *n;
5917 cdeps = 0;
5918 for (n = t->deps; n != NULL; n = n->next)
5919 ++cdeps;
5921 /* Add a symbol representing this version. */
5922 bh = NULL;
5923 if (! (_bfd_generic_link_add_one_symbol
5924 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5925 0, NULL, FALSE,
5926 get_elf_backend_data (dynobj)->collect, &bh)))
5927 return FALSE;
5928 h = (struct elf_link_hash_entry *) bh;
5929 h->non_elf = 0;
5930 h->def_regular = 1;
5931 h->type = STT_OBJECT;
5932 h->verinfo.vertree = t;
5934 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5935 return FALSE;
5937 def.vd_version = VER_DEF_CURRENT;
5938 def.vd_flags = 0;
5939 if (t->globals.list == NULL
5940 && t->locals.list == NULL
5941 && ! t->used)
5942 def.vd_flags |= VER_FLG_WEAK;
5943 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5944 def.vd_cnt = cdeps + 1;
5945 def.vd_hash = bfd_elf_hash (t->name);
5946 def.vd_aux = sizeof (Elf_External_Verdef);
5947 def.vd_next = 0;
5948 if (t->next != NULL)
5949 def.vd_next = (sizeof (Elf_External_Verdef)
5950 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5952 _bfd_elf_swap_verdef_out (output_bfd, &def,
5953 (Elf_External_Verdef *) p);
5954 p += sizeof (Elf_External_Verdef);
5956 defaux.vda_name = h->dynstr_index;
5957 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5958 h->dynstr_index);
5959 defaux.vda_next = 0;
5960 if (t->deps != NULL)
5961 defaux.vda_next = sizeof (Elf_External_Verdaux);
5962 t->name_indx = defaux.vda_name;
5964 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5965 (Elf_External_Verdaux *) p);
5966 p += sizeof (Elf_External_Verdaux);
5968 for (n = t->deps; n != NULL; n = n->next)
5970 if (n->version_needed == NULL)
5972 /* This can happen if there was an error in the
5973 version script. */
5974 defaux.vda_name = 0;
5976 else
5978 defaux.vda_name = n->version_needed->name_indx;
5979 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5980 defaux.vda_name);
5982 if (n->next == NULL)
5983 defaux.vda_next = 0;
5984 else
5985 defaux.vda_next = sizeof (Elf_External_Verdaux);
5987 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5988 (Elf_External_Verdaux *) p);
5989 p += sizeof (Elf_External_Verdaux);
5993 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5994 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5995 return FALSE;
5997 elf_tdata (output_bfd)->cverdefs = cdefs;
6000 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6002 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6003 return FALSE;
6005 else if (info->flags & DF_BIND_NOW)
6007 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6008 return FALSE;
6011 if (info->flags_1)
6013 if (info->executable)
6014 info->flags_1 &= ~ (DF_1_INITFIRST
6015 | DF_1_NODELETE
6016 | DF_1_NOOPEN);
6017 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6018 return FALSE;
6021 /* Work out the size of the version reference section. */
6023 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6024 BFD_ASSERT (s != NULL);
6026 struct elf_find_verdep_info sinfo;
6028 sinfo.output_bfd = output_bfd;
6029 sinfo.info = info;
6030 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6031 if (sinfo.vers == 0)
6032 sinfo.vers = 1;
6033 sinfo.failed = FALSE;
6035 elf_link_hash_traverse (elf_hash_table (info),
6036 _bfd_elf_link_find_version_dependencies,
6037 &sinfo);
6038 if (sinfo.failed)
6039 return FALSE;
6041 if (elf_tdata (output_bfd)->verref == NULL)
6042 s->flags |= SEC_EXCLUDE;
6043 else
6045 Elf_Internal_Verneed *t;
6046 unsigned int size;
6047 unsigned int crefs;
6048 bfd_byte *p;
6050 /* Build the version definition section. */
6051 size = 0;
6052 crefs = 0;
6053 for (t = elf_tdata (output_bfd)->verref;
6054 t != NULL;
6055 t = t->vn_nextref)
6057 Elf_Internal_Vernaux *a;
6059 size += sizeof (Elf_External_Verneed);
6060 ++crefs;
6061 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6062 size += sizeof (Elf_External_Vernaux);
6065 s->size = size;
6066 s->contents = bfd_alloc (output_bfd, s->size);
6067 if (s->contents == NULL)
6068 return FALSE;
6070 p = s->contents;
6071 for (t = elf_tdata (output_bfd)->verref;
6072 t != NULL;
6073 t = t->vn_nextref)
6075 unsigned int caux;
6076 Elf_Internal_Vernaux *a;
6077 bfd_size_type indx;
6079 caux = 0;
6080 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6081 ++caux;
6083 t->vn_version = VER_NEED_CURRENT;
6084 t->vn_cnt = caux;
6085 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6086 elf_dt_name (t->vn_bfd) != NULL
6087 ? elf_dt_name (t->vn_bfd)
6088 : lbasename (t->vn_bfd->filename),
6089 FALSE);
6090 if (indx == (bfd_size_type) -1)
6091 return FALSE;
6092 t->vn_file = indx;
6093 t->vn_aux = sizeof (Elf_External_Verneed);
6094 if (t->vn_nextref == NULL)
6095 t->vn_next = 0;
6096 else
6097 t->vn_next = (sizeof (Elf_External_Verneed)
6098 + caux * sizeof (Elf_External_Vernaux));
6100 _bfd_elf_swap_verneed_out (output_bfd, t,
6101 (Elf_External_Verneed *) p);
6102 p += sizeof (Elf_External_Verneed);
6104 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6106 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6107 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6108 a->vna_nodename, FALSE);
6109 if (indx == (bfd_size_type) -1)
6110 return FALSE;
6111 a->vna_name = indx;
6112 if (a->vna_nextptr == NULL)
6113 a->vna_next = 0;
6114 else
6115 a->vna_next = sizeof (Elf_External_Vernaux);
6117 _bfd_elf_swap_vernaux_out (output_bfd, a,
6118 (Elf_External_Vernaux *) p);
6119 p += sizeof (Elf_External_Vernaux);
6123 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6124 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6125 return FALSE;
6127 elf_tdata (output_bfd)->cverrefs = crefs;
6131 if ((elf_tdata (output_bfd)->cverrefs == 0
6132 && elf_tdata (output_bfd)->cverdefs == 0)
6133 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6134 &section_sym_count) == 0)
6136 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6137 s->flags |= SEC_EXCLUDE;
6140 return TRUE;
6143 /* Find the first non-excluded output section. We'll use its
6144 section symbol for some emitted relocs. */
6145 void
6146 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6148 asection *s;
6150 for (s = output_bfd->sections; s != NULL; s = s->next)
6151 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6152 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6154 elf_hash_table (info)->text_index_section = s;
6155 break;
6159 /* Find two non-excluded output sections, one for code, one for data.
6160 We'll use their section symbols for some emitted relocs. */
6161 void
6162 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6164 asection *s;
6166 /* Data first, since setting text_index_section changes
6167 _bfd_elf_link_omit_section_dynsym. */
6168 for (s = output_bfd->sections; s != NULL; s = s->next)
6169 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6170 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6172 elf_hash_table (info)->data_index_section = s;
6173 break;
6176 for (s = output_bfd->sections; s != NULL; s = s->next)
6177 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6178 == (SEC_ALLOC | SEC_READONLY))
6179 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6181 elf_hash_table (info)->text_index_section = s;
6182 break;
6185 if (elf_hash_table (info)->text_index_section == NULL)
6186 elf_hash_table (info)->text_index_section
6187 = elf_hash_table (info)->data_index_section;
6190 bfd_boolean
6191 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6193 const struct elf_backend_data *bed;
6195 if (!is_elf_hash_table (info->hash))
6196 return TRUE;
6198 bed = get_elf_backend_data (output_bfd);
6199 (*bed->elf_backend_init_index_section) (output_bfd, info);
6201 if (elf_hash_table (info)->dynamic_sections_created)
6203 bfd *dynobj;
6204 asection *s;
6205 bfd_size_type dynsymcount;
6206 unsigned long section_sym_count;
6207 unsigned int dtagcount;
6209 dynobj = elf_hash_table (info)->dynobj;
6211 /* Assign dynsym indicies. In a shared library we generate a
6212 section symbol for each output section, which come first.
6213 Next come all of the back-end allocated local dynamic syms,
6214 followed by the rest of the global symbols. */
6216 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6217 &section_sym_count);
6219 /* Work out the size of the symbol version section. */
6220 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6221 BFD_ASSERT (s != NULL);
6222 if (dynsymcount != 0
6223 && (s->flags & SEC_EXCLUDE) == 0)
6225 s->size = dynsymcount * sizeof (Elf_External_Versym);
6226 s->contents = bfd_zalloc (output_bfd, s->size);
6227 if (s->contents == NULL)
6228 return FALSE;
6230 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6231 return FALSE;
6234 /* Set the size of the .dynsym and .hash sections. We counted
6235 the number of dynamic symbols in elf_link_add_object_symbols.
6236 We will build the contents of .dynsym and .hash when we build
6237 the final symbol table, because until then we do not know the
6238 correct value to give the symbols. We built the .dynstr
6239 section as we went along in elf_link_add_object_symbols. */
6240 s = bfd_get_section_by_name (dynobj, ".dynsym");
6241 BFD_ASSERT (s != NULL);
6242 s->size = dynsymcount * bed->s->sizeof_sym;
6244 if (dynsymcount != 0)
6246 s->contents = bfd_alloc (output_bfd, s->size);
6247 if (s->contents == NULL)
6248 return FALSE;
6250 /* The first entry in .dynsym is a dummy symbol.
6251 Clear all the section syms, in case we don't output them all. */
6252 ++section_sym_count;
6253 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6256 elf_hash_table (info)->bucketcount = 0;
6258 /* Compute the size of the hashing table. As a side effect this
6259 computes the hash values for all the names we export. */
6260 if (info->emit_hash)
6262 unsigned long int *hashcodes;
6263 struct hash_codes_info hashinf;
6264 bfd_size_type amt;
6265 unsigned long int nsyms;
6266 size_t bucketcount;
6267 size_t hash_entry_size;
6269 /* Compute the hash values for all exported symbols. At the same
6270 time store the values in an array so that we could use them for
6271 optimizations. */
6272 amt = dynsymcount * sizeof (unsigned long int);
6273 hashcodes = bfd_malloc (amt);
6274 if (hashcodes == NULL)
6275 return FALSE;
6276 hashinf.hashcodes = hashcodes;
6277 hashinf.error = FALSE;
6279 /* Put all hash values in HASHCODES. */
6280 elf_link_hash_traverse (elf_hash_table (info),
6281 elf_collect_hash_codes, &hashinf);
6282 if (hashinf.error)
6284 free (hashcodes);
6285 return FALSE;
6288 nsyms = hashinf.hashcodes - hashcodes;
6289 bucketcount
6290 = compute_bucket_count (info, hashcodes, nsyms, 0);
6291 free (hashcodes);
6293 if (bucketcount == 0)
6294 return FALSE;
6296 elf_hash_table (info)->bucketcount = bucketcount;
6298 s = bfd_get_section_by_name (dynobj, ".hash");
6299 BFD_ASSERT (s != NULL);
6300 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6301 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6302 s->contents = bfd_zalloc (output_bfd, s->size);
6303 if (s->contents == NULL)
6304 return FALSE;
6306 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6307 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6308 s->contents + hash_entry_size);
6311 if (info->emit_gnu_hash)
6313 size_t i, cnt;
6314 unsigned char *contents;
6315 struct collect_gnu_hash_codes cinfo;
6316 bfd_size_type amt;
6317 size_t bucketcount;
6319 memset (&cinfo, 0, sizeof (cinfo));
6321 /* Compute the hash values for all exported symbols. At the same
6322 time store the values in an array so that we could use them for
6323 optimizations. */
6324 amt = dynsymcount * 2 * sizeof (unsigned long int);
6325 cinfo.hashcodes = bfd_malloc (amt);
6326 if (cinfo.hashcodes == NULL)
6327 return FALSE;
6329 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6330 cinfo.min_dynindx = -1;
6331 cinfo.output_bfd = output_bfd;
6332 cinfo.bed = bed;
6334 /* Put all hash values in HASHCODES. */
6335 elf_link_hash_traverse (elf_hash_table (info),
6336 elf_collect_gnu_hash_codes, &cinfo);
6337 if (cinfo.error)
6339 free (cinfo.hashcodes);
6340 return FALSE;
6343 bucketcount
6344 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6346 if (bucketcount == 0)
6348 free (cinfo.hashcodes);
6349 return FALSE;
6352 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6353 BFD_ASSERT (s != NULL);
6355 if (cinfo.nsyms == 0)
6357 /* Empty .gnu.hash section is special. */
6358 BFD_ASSERT (cinfo.min_dynindx == -1);
6359 free (cinfo.hashcodes);
6360 s->size = 5 * 4 + bed->s->arch_size / 8;
6361 contents = bfd_zalloc (output_bfd, s->size);
6362 if (contents == NULL)
6363 return FALSE;
6364 s->contents = contents;
6365 /* 1 empty bucket. */
6366 bfd_put_32 (output_bfd, 1, contents);
6367 /* SYMIDX above the special symbol 0. */
6368 bfd_put_32 (output_bfd, 1, contents + 4);
6369 /* Just one word for bitmask. */
6370 bfd_put_32 (output_bfd, 1, contents + 8);
6371 /* Only hash fn bloom filter. */
6372 bfd_put_32 (output_bfd, 0, contents + 12);
6373 /* No hashes are valid - empty bitmask. */
6374 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6375 /* No hashes in the only bucket. */
6376 bfd_put_32 (output_bfd, 0,
6377 contents + 16 + bed->s->arch_size / 8);
6379 else
6381 unsigned long int maskwords, maskbitslog2;
6382 BFD_ASSERT (cinfo.min_dynindx != -1);
6384 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6385 if (maskbitslog2 < 3)
6386 maskbitslog2 = 5;
6387 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6388 maskbitslog2 = maskbitslog2 + 3;
6389 else
6390 maskbitslog2 = maskbitslog2 + 2;
6391 if (bed->s->arch_size == 64)
6393 if (maskbitslog2 == 5)
6394 maskbitslog2 = 6;
6395 cinfo.shift1 = 6;
6397 else
6398 cinfo.shift1 = 5;
6399 cinfo.mask = (1 << cinfo.shift1) - 1;
6400 cinfo.shift2 = maskbitslog2;
6401 cinfo.maskbits = 1 << maskbitslog2;
6402 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6403 amt = bucketcount * sizeof (unsigned long int) * 2;
6404 amt += maskwords * sizeof (bfd_vma);
6405 cinfo.bitmask = bfd_malloc (amt);
6406 if (cinfo.bitmask == NULL)
6408 free (cinfo.hashcodes);
6409 return FALSE;
6412 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6413 cinfo.indx = cinfo.counts + bucketcount;
6414 cinfo.symindx = dynsymcount - cinfo.nsyms;
6415 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6417 /* Determine how often each hash bucket is used. */
6418 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6419 for (i = 0; i < cinfo.nsyms; ++i)
6420 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6422 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6423 if (cinfo.counts[i] != 0)
6425 cinfo.indx[i] = cnt;
6426 cnt += cinfo.counts[i];
6428 BFD_ASSERT (cnt == dynsymcount);
6429 cinfo.bucketcount = bucketcount;
6430 cinfo.local_indx = cinfo.min_dynindx;
6432 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6433 s->size += cinfo.maskbits / 8;
6434 contents = bfd_zalloc (output_bfd, s->size);
6435 if (contents == NULL)
6437 free (cinfo.bitmask);
6438 free (cinfo.hashcodes);
6439 return FALSE;
6442 s->contents = contents;
6443 bfd_put_32 (output_bfd, bucketcount, contents);
6444 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6445 bfd_put_32 (output_bfd, maskwords, contents + 8);
6446 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6447 contents += 16 + cinfo.maskbits / 8;
6449 for (i = 0; i < bucketcount; ++i)
6451 if (cinfo.counts[i] == 0)
6452 bfd_put_32 (output_bfd, 0, contents);
6453 else
6454 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6455 contents += 4;
6458 cinfo.contents = contents;
6460 /* Renumber dynamic symbols, populate .gnu.hash section. */
6461 elf_link_hash_traverse (elf_hash_table (info),
6462 elf_renumber_gnu_hash_syms, &cinfo);
6464 contents = s->contents + 16;
6465 for (i = 0; i < maskwords; ++i)
6467 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6468 contents);
6469 contents += bed->s->arch_size / 8;
6472 free (cinfo.bitmask);
6473 free (cinfo.hashcodes);
6477 s = bfd_get_section_by_name (dynobj, ".dynstr");
6478 BFD_ASSERT (s != NULL);
6480 elf_finalize_dynstr (output_bfd, info);
6482 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6484 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6485 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6486 return FALSE;
6489 return TRUE;
6492 /* Indicate that we are only retrieving symbol values from this
6493 section. */
6495 void
6496 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6498 if (is_elf_hash_table (info->hash))
6499 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6500 _bfd_generic_link_just_syms (sec, info);
6503 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6505 static void
6506 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6507 asection *sec)
6509 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6510 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6513 /* Finish SHF_MERGE section merging. */
6515 bfd_boolean
6516 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6518 bfd *ibfd;
6519 asection *sec;
6521 if (!is_elf_hash_table (info->hash))
6522 return FALSE;
6524 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6525 if ((ibfd->flags & DYNAMIC) == 0)
6526 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6527 if ((sec->flags & SEC_MERGE) != 0
6528 && !bfd_is_abs_section (sec->output_section))
6530 struct bfd_elf_section_data *secdata;
6532 secdata = elf_section_data (sec);
6533 if (! _bfd_add_merge_section (abfd,
6534 &elf_hash_table (info)->merge_info,
6535 sec, &secdata->sec_info))
6536 return FALSE;
6537 else if (secdata->sec_info)
6538 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6541 if (elf_hash_table (info)->merge_info != NULL)
6542 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6543 merge_sections_remove_hook);
6544 return TRUE;
6547 /* Create an entry in an ELF linker hash table. */
6549 struct bfd_hash_entry *
6550 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6551 struct bfd_hash_table *table,
6552 const char *string)
6554 /* Allocate the structure if it has not already been allocated by a
6555 subclass. */
6556 if (entry == NULL)
6558 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6559 if (entry == NULL)
6560 return entry;
6563 /* Call the allocation method of the superclass. */
6564 entry = _bfd_link_hash_newfunc (entry, table, string);
6565 if (entry != NULL)
6567 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6568 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6570 /* Set local fields. */
6571 ret->indx = -1;
6572 ret->dynindx = -1;
6573 ret->got = htab->init_got_refcount;
6574 ret->plt = htab->init_plt_refcount;
6575 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6576 - offsetof (struct elf_link_hash_entry, size)));
6577 /* Assume that we have been called by a non-ELF symbol reader.
6578 This flag is then reset by the code which reads an ELF input
6579 file. This ensures that a symbol created by a non-ELF symbol
6580 reader will have the flag set correctly. */
6581 ret->non_elf = 1;
6584 return entry;
6587 /* Copy data from an indirect symbol to its direct symbol, hiding the
6588 old indirect symbol. Also used for copying flags to a weakdef. */
6590 void
6591 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6592 struct elf_link_hash_entry *dir,
6593 struct elf_link_hash_entry *ind)
6595 struct elf_link_hash_table *htab;
6597 /* Copy down any references that we may have already seen to the
6598 symbol which just became indirect. */
6600 dir->ref_dynamic |= ind->ref_dynamic;
6601 dir->ref_regular |= ind->ref_regular;
6602 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6603 dir->non_got_ref |= ind->non_got_ref;
6604 dir->needs_plt |= ind->needs_plt;
6605 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6607 if (ind->root.type != bfd_link_hash_indirect)
6608 return;
6610 /* Copy over the global and procedure linkage table refcount entries.
6611 These may have been already set up by a check_relocs routine. */
6612 htab = elf_hash_table (info);
6613 if (ind->got.refcount > htab->init_got_refcount.refcount)
6615 if (dir->got.refcount < 0)
6616 dir->got.refcount = 0;
6617 dir->got.refcount += ind->got.refcount;
6618 ind->got.refcount = htab->init_got_refcount.refcount;
6621 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6623 if (dir->plt.refcount < 0)
6624 dir->plt.refcount = 0;
6625 dir->plt.refcount += ind->plt.refcount;
6626 ind->plt.refcount = htab->init_plt_refcount.refcount;
6629 if (ind->dynindx != -1)
6631 if (dir->dynindx != -1)
6632 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6633 dir->dynindx = ind->dynindx;
6634 dir->dynstr_index = ind->dynstr_index;
6635 ind->dynindx = -1;
6636 ind->dynstr_index = 0;
6640 void
6641 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6642 struct elf_link_hash_entry *h,
6643 bfd_boolean force_local)
6645 h->plt = elf_hash_table (info)->init_plt_offset;
6646 h->needs_plt = 0;
6647 if (force_local)
6649 h->forced_local = 1;
6650 if (h->dynindx != -1)
6652 h->dynindx = -1;
6653 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6654 h->dynstr_index);
6659 /* Initialize an ELF linker hash table. */
6661 bfd_boolean
6662 _bfd_elf_link_hash_table_init
6663 (struct elf_link_hash_table *table,
6664 bfd *abfd,
6665 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6666 struct bfd_hash_table *,
6667 const char *),
6668 unsigned int entsize)
6670 bfd_boolean ret;
6671 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6673 memset (table, 0, sizeof * table);
6674 table->init_got_refcount.refcount = can_refcount - 1;
6675 table->init_plt_refcount.refcount = can_refcount - 1;
6676 table->init_got_offset.offset = -(bfd_vma) 1;
6677 table->init_plt_offset.offset = -(bfd_vma) 1;
6678 /* The first dynamic symbol is a dummy. */
6679 table->dynsymcount = 1;
6681 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6682 table->root.type = bfd_link_elf_hash_table;
6684 return ret;
6687 /* Create an ELF linker hash table. */
6689 struct bfd_link_hash_table *
6690 _bfd_elf_link_hash_table_create (bfd *abfd)
6692 struct elf_link_hash_table *ret;
6693 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6695 ret = bfd_malloc (amt);
6696 if (ret == NULL)
6697 return NULL;
6699 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6700 sizeof (struct elf_link_hash_entry)))
6702 free (ret);
6703 return NULL;
6706 return &ret->root;
6709 /* This is a hook for the ELF emulation code in the generic linker to
6710 tell the backend linker what file name to use for the DT_NEEDED
6711 entry for a dynamic object. */
6713 void
6714 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6716 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6717 && bfd_get_format (abfd) == bfd_object)
6718 elf_dt_name (abfd) = name;
6722 bfd_elf_get_dyn_lib_class (bfd *abfd)
6724 int lib_class;
6725 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6726 && bfd_get_format (abfd) == bfd_object)
6727 lib_class = elf_dyn_lib_class (abfd);
6728 else
6729 lib_class = 0;
6730 return lib_class;
6733 void
6734 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6736 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6737 && bfd_get_format (abfd) == bfd_object)
6738 elf_dyn_lib_class (abfd) = lib_class;
6741 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6742 the linker ELF emulation code. */
6744 struct bfd_link_needed_list *
6745 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6746 struct bfd_link_info *info)
6748 if (! is_elf_hash_table (info->hash))
6749 return NULL;
6750 return elf_hash_table (info)->needed;
6753 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6754 hook for the linker ELF emulation code. */
6756 struct bfd_link_needed_list *
6757 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6758 struct bfd_link_info *info)
6760 if (! is_elf_hash_table (info->hash))
6761 return NULL;
6762 return elf_hash_table (info)->runpath;
6765 /* Get the name actually used for a dynamic object for a link. This
6766 is the SONAME entry if there is one. Otherwise, it is the string
6767 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6769 const char *
6770 bfd_elf_get_dt_soname (bfd *abfd)
6772 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6773 && bfd_get_format (abfd) == bfd_object)
6774 return elf_dt_name (abfd);
6775 return NULL;
6778 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6779 the ELF linker emulation code. */
6781 bfd_boolean
6782 bfd_elf_get_bfd_needed_list (bfd *abfd,
6783 struct bfd_link_needed_list **pneeded)
6785 asection *s;
6786 bfd_byte *dynbuf = NULL;
6787 unsigned int elfsec;
6788 unsigned long shlink;
6789 bfd_byte *extdyn, *extdynend;
6790 size_t extdynsize;
6791 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6793 *pneeded = NULL;
6795 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6796 || bfd_get_format (abfd) != bfd_object)
6797 return TRUE;
6799 s = bfd_get_section_by_name (abfd, ".dynamic");
6800 if (s == NULL || s->size == 0)
6801 return TRUE;
6803 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6804 goto error_return;
6806 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6807 if (elfsec == SHN_BAD)
6808 goto error_return;
6810 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6812 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6813 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6815 extdyn = dynbuf;
6816 extdynend = extdyn + s->size;
6817 for (; extdyn < extdynend; extdyn += extdynsize)
6819 Elf_Internal_Dyn dyn;
6821 (*swap_dyn_in) (abfd, extdyn, &dyn);
6823 if (dyn.d_tag == DT_NULL)
6824 break;
6826 if (dyn.d_tag == DT_NEEDED)
6828 const char *string;
6829 struct bfd_link_needed_list *l;
6830 unsigned int tagv = dyn.d_un.d_val;
6831 bfd_size_type amt;
6833 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6834 if (string == NULL)
6835 goto error_return;
6837 amt = sizeof *l;
6838 l = bfd_alloc (abfd, amt);
6839 if (l == NULL)
6840 goto error_return;
6842 l->by = abfd;
6843 l->name = string;
6844 l->next = *pneeded;
6845 *pneeded = l;
6849 free (dynbuf);
6851 return TRUE;
6853 error_return:
6854 if (dynbuf != NULL)
6855 free (dynbuf);
6856 return FALSE;
6859 struct elf_symbuf_symbol
6861 unsigned long st_name; /* Symbol name, index in string tbl */
6862 unsigned char st_info; /* Type and binding attributes */
6863 unsigned char st_other; /* Visibilty, and target specific */
6866 struct elf_symbuf_head
6868 struct elf_symbuf_symbol *ssym;
6869 bfd_size_type count;
6870 unsigned int st_shndx;
6873 struct elf_symbol
6875 union
6877 Elf_Internal_Sym *isym;
6878 struct elf_symbuf_symbol *ssym;
6879 } u;
6880 const char *name;
6883 /* Sort references to symbols by ascending section number. */
6885 static int
6886 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6888 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6889 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6891 return s1->st_shndx - s2->st_shndx;
6894 static int
6895 elf_sym_name_compare (const void *arg1, const void *arg2)
6897 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6898 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6899 return strcmp (s1->name, s2->name);
6902 static struct elf_symbuf_head *
6903 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6905 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6906 struct elf_symbuf_symbol *ssym;
6907 struct elf_symbuf_head *ssymbuf, *ssymhead;
6908 bfd_size_type i, shndx_count, total_size;
6910 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6911 if (indbuf == NULL)
6912 return NULL;
6914 for (ind = indbuf, i = 0; i < symcount; i++)
6915 if (isymbuf[i].st_shndx != SHN_UNDEF)
6916 *ind++ = &isymbuf[i];
6917 indbufend = ind;
6919 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6920 elf_sort_elf_symbol);
6922 shndx_count = 0;
6923 if (indbufend > indbuf)
6924 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6925 if (ind[0]->st_shndx != ind[1]->st_shndx)
6926 shndx_count++;
6928 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6929 + (indbufend - indbuf) * sizeof (*ssym));
6930 ssymbuf = bfd_malloc (total_size);
6931 if (ssymbuf == NULL)
6933 free (indbuf);
6934 return NULL;
6937 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6938 ssymbuf->ssym = NULL;
6939 ssymbuf->count = shndx_count;
6940 ssymbuf->st_shndx = 0;
6941 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6943 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6945 ssymhead++;
6946 ssymhead->ssym = ssym;
6947 ssymhead->count = 0;
6948 ssymhead->st_shndx = (*ind)->st_shndx;
6950 ssym->st_name = (*ind)->st_name;
6951 ssym->st_info = (*ind)->st_info;
6952 ssym->st_other = (*ind)->st_other;
6953 ssymhead->count++;
6955 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6956 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6957 == total_size));
6959 free (indbuf);
6960 return ssymbuf;
6963 /* Check if 2 sections define the same set of local and global
6964 symbols. */
6966 static bfd_boolean
6967 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6968 struct bfd_link_info *info)
6970 bfd *bfd1, *bfd2;
6971 const struct elf_backend_data *bed1, *bed2;
6972 Elf_Internal_Shdr *hdr1, *hdr2;
6973 bfd_size_type symcount1, symcount2;
6974 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6975 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6976 Elf_Internal_Sym *isym, *isymend;
6977 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6978 bfd_size_type count1, count2, i;
6979 unsigned int shndx1, shndx2;
6980 bfd_boolean result;
6982 bfd1 = sec1->owner;
6983 bfd2 = sec2->owner;
6985 /* Both sections have to be in ELF. */
6986 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6987 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6988 return FALSE;
6990 if (elf_section_type (sec1) != elf_section_type (sec2))
6991 return FALSE;
6993 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6994 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
6995 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
6996 return FALSE;
6998 bed1 = get_elf_backend_data (bfd1);
6999 bed2 = get_elf_backend_data (bfd2);
7000 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7001 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7002 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7003 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7005 if (symcount1 == 0 || symcount2 == 0)
7006 return FALSE;
7008 result = FALSE;
7009 isymbuf1 = NULL;
7010 isymbuf2 = NULL;
7011 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7012 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7014 if (ssymbuf1 == NULL)
7016 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7017 NULL, NULL, NULL);
7018 if (isymbuf1 == NULL)
7019 goto done;
7021 if (!info->reduce_memory_overheads)
7022 elf_tdata (bfd1)->symbuf = ssymbuf1
7023 = elf_create_symbuf (symcount1, isymbuf1);
7026 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7028 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7029 NULL, NULL, NULL);
7030 if (isymbuf2 == NULL)
7031 goto done;
7033 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7034 elf_tdata (bfd2)->symbuf = ssymbuf2
7035 = elf_create_symbuf (symcount2, isymbuf2);
7038 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7040 /* Optimized faster version. */
7041 bfd_size_type lo, hi, mid;
7042 struct elf_symbol *symp;
7043 struct elf_symbuf_symbol *ssym, *ssymend;
7045 lo = 0;
7046 hi = ssymbuf1->count;
7047 ssymbuf1++;
7048 count1 = 0;
7049 while (lo < hi)
7051 mid = (lo + hi) / 2;
7052 if (shndx1 < ssymbuf1[mid].st_shndx)
7053 hi = mid;
7054 else if (shndx1 > ssymbuf1[mid].st_shndx)
7055 lo = mid + 1;
7056 else
7058 count1 = ssymbuf1[mid].count;
7059 ssymbuf1 += mid;
7060 break;
7064 lo = 0;
7065 hi = ssymbuf2->count;
7066 ssymbuf2++;
7067 count2 = 0;
7068 while (lo < hi)
7070 mid = (lo + hi) / 2;
7071 if (shndx2 < ssymbuf2[mid].st_shndx)
7072 hi = mid;
7073 else if (shndx2 > ssymbuf2[mid].st_shndx)
7074 lo = mid + 1;
7075 else
7077 count2 = ssymbuf2[mid].count;
7078 ssymbuf2 += mid;
7079 break;
7083 if (count1 == 0 || count2 == 0 || count1 != count2)
7084 goto done;
7086 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7087 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7088 if (symtable1 == NULL || symtable2 == NULL)
7089 goto done;
7091 symp = symtable1;
7092 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7093 ssym < ssymend; ssym++, symp++)
7095 symp->u.ssym = ssym;
7096 symp->name = bfd_elf_string_from_elf_section (bfd1,
7097 hdr1->sh_link,
7098 ssym->st_name);
7101 symp = symtable2;
7102 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7103 ssym < ssymend; ssym++, symp++)
7105 symp->u.ssym = ssym;
7106 symp->name = bfd_elf_string_from_elf_section (bfd2,
7107 hdr2->sh_link,
7108 ssym->st_name);
7111 /* Sort symbol by name. */
7112 qsort (symtable1, count1, sizeof (struct elf_symbol),
7113 elf_sym_name_compare);
7114 qsort (symtable2, count1, sizeof (struct elf_symbol),
7115 elf_sym_name_compare);
7117 for (i = 0; i < count1; i++)
7118 /* Two symbols must have the same binding, type and name. */
7119 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7120 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7121 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7122 goto done;
7124 result = TRUE;
7125 goto done;
7128 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7129 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7130 if (symtable1 == NULL || symtable2 == NULL)
7131 goto done;
7133 /* Count definitions in the section. */
7134 count1 = 0;
7135 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7136 if (isym->st_shndx == shndx1)
7137 symtable1[count1++].u.isym = isym;
7139 count2 = 0;
7140 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7141 if (isym->st_shndx == shndx2)
7142 symtable2[count2++].u.isym = isym;
7144 if (count1 == 0 || count2 == 0 || count1 != count2)
7145 goto done;
7147 for (i = 0; i < count1; i++)
7148 symtable1[i].name
7149 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7150 symtable1[i].u.isym->st_name);
7152 for (i = 0; i < count2; i++)
7153 symtable2[i].name
7154 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7155 symtable2[i].u.isym->st_name);
7157 /* Sort symbol by name. */
7158 qsort (symtable1, count1, sizeof (struct elf_symbol),
7159 elf_sym_name_compare);
7160 qsort (symtable2, count1, sizeof (struct elf_symbol),
7161 elf_sym_name_compare);
7163 for (i = 0; i < count1; i++)
7164 /* Two symbols must have the same binding, type and name. */
7165 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7166 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7167 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7168 goto done;
7170 result = TRUE;
7172 done:
7173 if (symtable1)
7174 free (symtable1);
7175 if (symtable2)
7176 free (symtable2);
7177 if (isymbuf1)
7178 free (isymbuf1);
7179 if (isymbuf2)
7180 free (isymbuf2);
7182 return result;
7185 /* Return TRUE if 2 section types are compatible. */
7187 bfd_boolean
7188 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7189 bfd *bbfd, const asection *bsec)
7191 if (asec == NULL
7192 || bsec == NULL
7193 || abfd->xvec->flavour != bfd_target_elf_flavour
7194 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7195 return TRUE;
7197 return elf_section_type (asec) == elf_section_type (bsec);
7200 /* Final phase of ELF linker. */
7202 /* A structure we use to avoid passing large numbers of arguments. */
7204 struct elf_final_link_info
7206 /* General link information. */
7207 struct bfd_link_info *info;
7208 /* Output BFD. */
7209 bfd *output_bfd;
7210 /* Symbol string table. */
7211 struct bfd_strtab_hash *symstrtab;
7212 /* .dynsym section. */
7213 asection *dynsym_sec;
7214 /* .hash section. */
7215 asection *hash_sec;
7216 /* symbol version section (.gnu.version). */
7217 asection *symver_sec;
7218 /* Buffer large enough to hold contents of any section. */
7219 bfd_byte *contents;
7220 /* Buffer large enough to hold external relocs of any section. */
7221 void *external_relocs;
7222 /* Buffer large enough to hold internal relocs of any section. */
7223 Elf_Internal_Rela *internal_relocs;
7224 /* Buffer large enough to hold external local symbols of any input
7225 BFD. */
7226 bfd_byte *external_syms;
7227 /* And a buffer for symbol section indices. */
7228 Elf_External_Sym_Shndx *locsym_shndx;
7229 /* Buffer large enough to hold internal local symbols of any input
7230 BFD. */
7231 Elf_Internal_Sym *internal_syms;
7232 /* Array large enough to hold a symbol index for each local symbol
7233 of any input BFD. */
7234 long *indices;
7235 /* Array large enough to hold a section pointer for each local
7236 symbol of any input BFD. */
7237 asection **sections;
7238 /* Buffer to hold swapped out symbols. */
7239 bfd_byte *symbuf;
7240 /* And one for symbol section indices. */
7241 Elf_External_Sym_Shndx *symshndxbuf;
7242 /* Number of swapped out symbols in buffer. */
7243 size_t symbuf_count;
7244 /* Number of symbols which fit in symbuf. */
7245 size_t symbuf_size;
7246 /* And same for symshndxbuf. */
7247 size_t shndxbuf_size;
7250 /* This struct is used to pass information to elf_link_output_extsym. */
7252 struct elf_outext_info
7254 bfd_boolean failed;
7255 bfd_boolean localsyms;
7256 struct elf_final_link_info *finfo;
7260 /* Support for evaluating a complex relocation.
7262 Complex relocations are generalized, self-describing relocations. The
7263 implementation of them consists of two parts: complex symbols, and the
7264 relocations themselves.
7266 The relocations are use a reserved elf-wide relocation type code (R_RELC
7267 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7268 information (start bit, end bit, word width, etc) into the addend. This
7269 information is extracted from CGEN-generated operand tables within gas.
7271 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7272 internal) representing prefix-notation expressions, including but not
7273 limited to those sorts of expressions normally encoded as addends in the
7274 addend field. The symbol mangling format is:
7276 <node> := <literal>
7277 | <unary-operator> ':' <node>
7278 | <binary-operator> ':' <node> ':' <node>
7281 <literal> := 's' <digits=N> ':' <N character symbol name>
7282 | 'S' <digits=N> ':' <N character section name>
7283 | '#' <hexdigits>
7286 <binary-operator> := as in C
7287 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7289 static void
7290 set_symbol_value (bfd *bfd_with_globals,
7291 Elf_Internal_Sym *isymbuf,
7292 size_t locsymcount,
7293 size_t symidx,
7294 bfd_vma val)
7296 struct elf_link_hash_entry **sym_hashes;
7297 struct elf_link_hash_entry *h;
7298 size_t extsymoff = locsymcount;
7300 if (symidx < locsymcount)
7302 Elf_Internal_Sym *sym;
7304 sym = isymbuf + symidx;
7305 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7307 /* It is a local symbol: move it to the
7308 "absolute" section and give it a value. */
7309 sym->st_shndx = SHN_ABS;
7310 sym->st_value = val;
7311 return;
7313 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7314 extsymoff = 0;
7317 /* It is a global symbol: set its link type
7318 to "defined" and give it a value. */
7320 sym_hashes = elf_sym_hashes (bfd_with_globals);
7321 h = sym_hashes [symidx - extsymoff];
7322 while (h->root.type == bfd_link_hash_indirect
7323 || h->root.type == bfd_link_hash_warning)
7324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7325 h->root.type = bfd_link_hash_defined;
7326 h->root.u.def.value = val;
7327 h->root.u.def.section = bfd_abs_section_ptr;
7330 static bfd_boolean
7331 resolve_symbol (const char *name,
7332 bfd *input_bfd,
7333 struct elf_final_link_info *finfo,
7334 bfd_vma *result,
7335 Elf_Internal_Sym *isymbuf,
7336 size_t locsymcount)
7338 Elf_Internal_Sym *sym;
7339 struct bfd_link_hash_entry *global_entry;
7340 const char *candidate = NULL;
7341 Elf_Internal_Shdr *symtab_hdr;
7342 size_t i;
7344 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7346 for (i = 0; i < locsymcount; ++ i)
7348 sym = isymbuf + i;
7350 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7351 continue;
7353 candidate = bfd_elf_string_from_elf_section (input_bfd,
7354 symtab_hdr->sh_link,
7355 sym->st_name);
7356 #ifdef DEBUG
7357 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7358 name, candidate, (unsigned long) sym->st_value);
7359 #endif
7360 if (candidate && strcmp (candidate, name) == 0)
7362 asection *sec = finfo->sections [i];
7364 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7365 *result += sec->output_offset + sec->output_section->vma;
7366 #ifdef DEBUG
7367 printf ("Found symbol with value %8.8lx\n",
7368 (unsigned long) *result);
7369 #endif
7370 return TRUE;
7374 /* Hmm, haven't found it yet. perhaps it is a global. */
7375 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7376 FALSE, FALSE, TRUE);
7377 if (!global_entry)
7378 return FALSE;
7380 if (global_entry->type == bfd_link_hash_defined
7381 || global_entry->type == bfd_link_hash_defweak)
7383 *result = (global_entry->u.def.value
7384 + global_entry->u.def.section->output_section->vma
7385 + global_entry->u.def.section->output_offset);
7386 #ifdef DEBUG
7387 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7388 global_entry->root.string, (unsigned long) *result);
7389 #endif
7390 return TRUE;
7393 return FALSE;
7396 static bfd_boolean
7397 resolve_section (const char *name,
7398 asection *sections,
7399 bfd_vma *result)
7401 asection *curr;
7402 unsigned int len;
7404 for (curr = sections; curr; curr = curr->next)
7405 if (strcmp (curr->name, name) == 0)
7407 *result = curr->vma;
7408 return TRUE;
7411 /* Hmm. still haven't found it. try pseudo-section names. */
7412 for (curr = sections; curr; curr = curr->next)
7414 len = strlen (curr->name);
7415 if (len > strlen (name))
7416 continue;
7418 if (strncmp (curr->name, name, len) == 0)
7420 if (strncmp (".end", name + len, 4) == 0)
7422 *result = curr->vma + curr->size;
7423 return TRUE;
7426 /* Insert more pseudo-section names here, if you like. */
7430 return FALSE;
7433 static void
7434 undefined_reference (const char *reftype, const char *name)
7436 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7437 reftype, name);
7440 static bfd_boolean
7441 eval_symbol (bfd_vma *result,
7442 const char **symp,
7443 bfd *input_bfd,
7444 struct elf_final_link_info *finfo,
7445 bfd_vma dot,
7446 Elf_Internal_Sym *isymbuf,
7447 size_t locsymcount,
7448 int signed_p)
7450 size_t len;
7451 size_t symlen;
7452 bfd_vma a;
7453 bfd_vma b;
7454 char symbuf[4096];
7455 const char *sym = *symp;
7456 const char *symend;
7457 bfd_boolean symbol_is_section = FALSE;
7459 len = strlen (sym);
7460 symend = sym + len;
7462 if (len < 1 || len > sizeof (symbuf))
7464 bfd_set_error (bfd_error_invalid_operation);
7465 return FALSE;
7468 switch (* sym)
7470 case '.':
7471 *result = dot;
7472 *symp = sym + 1;
7473 return TRUE;
7475 case '#':
7476 ++sym;
7477 *result = strtoul (sym, (char **) symp, 16);
7478 return TRUE;
7480 case 'S':
7481 symbol_is_section = TRUE;
7482 case 's':
7483 ++sym;
7484 symlen = strtol (sym, (char **) symp, 10);
7485 sym = *symp + 1; /* Skip the trailing ':'. */
7487 if (symend < sym || symlen + 1 > sizeof (symbuf))
7489 bfd_set_error (bfd_error_invalid_operation);
7490 return FALSE;
7493 memcpy (symbuf, sym, symlen);
7494 symbuf[symlen] = '\0';
7495 *symp = sym + symlen;
7497 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7498 the symbol as a section, or vice-versa. so we're pretty liberal in our
7499 interpretation here; section means "try section first", not "must be a
7500 section", and likewise with symbol. */
7502 if (symbol_is_section)
7504 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7505 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7506 isymbuf, locsymcount))
7508 undefined_reference ("section", symbuf);
7509 return FALSE;
7512 else
7514 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7515 isymbuf, locsymcount)
7516 && !resolve_section (symbuf, finfo->output_bfd->sections,
7517 result))
7519 undefined_reference ("symbol", symbuf);
7520 return FALSE;
7524 return TRUE;
7526 /* All that remains are operators. */
7528 #define UNARY_OP(op) \
7529 if (strncmp (sym, #op, strlen (#op)) == 0) \
7531 sym += strlen (#op); \
7532 if (*sym == ':') \
7533 ++sym; \
7534 *symp = sym; \
7535 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7536 isymbuf, locsymcount, signed_p)) \
7537 return FALSE; \
7538 if (signed_p) \
7539 *result = op ((bfd_signed_vma) a); \
7540 else \
7541 *result = op a; \
7542 return TRUE; \
7545 #define BINARY_OP(op) \
7546 if (strncmp (sym, #op, strlen (#op)) == 0) \
7548 sym += strlen (#op); \
7549 if (*sym == ':') \
7550 ++sym; \
7551 *symp = sym; \
7552 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7553 isymbuf, locsymcount, signed_p)) \
7554 return FALSE; \
7555 ++*symp; \
7556 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7557 isymbuf, locsymcount, signed_p)) \
7558 return FALSE; \
7559 if (signed_p) \
7560 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7561 else \
7562 *result = a op b; \
7563 return TRUE; \
7566 default:
7567 UNARY_OP (0-);
7568 BINARY_OP (<<);
7569 BINARY_OP (>>);
7570 BINARY_OP (==);
7571 BINARY_OP (!=);
7572 BINARY_OP (<=);
7573 BINARY_OP (>=);
7574 BINARY_OP (&&);
7575 BINARY_OP (||);
7576 UNARY_OP (~);
7577 UNARY_OP (!);
7578 BINARY_OP (*);
7579 BINARY_OP (/);
7580 BINARY_OP (%);
7581 BINARY_OP (^);
7582 BINARY_OP (|);
7583 BINARY_OP (&);
7584 BINARY_OP (+);
7585 BINARY_OP (-);
7586 BINARY_OP (<);
7587 BINARY_OP (>);
7588 #undef UNARY_OP
7589 #undef BINARY_OP
7590 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7591 bfd_set_error (bfd_error_invalid_operation);
7592 return FALSE;
7596 static void
7597 put_value (bfd_vma size,
7598 unsigned long chunksz,
7599 bfd *input_bfd,
7600 bfd_vma x,
7601 bfd_byte *location)
7603 location += (size - chunksz);
7605 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7607 switch (chunksz)
7609 default:
7610 case 0:
7611 abort ();
7612 case 1:
7613 bfd_put_8 (input_bfd, x, location);
7614 break;
7615 case 2:
7616 bfd_put_16 (input_bfd, x, location);
7617 break;
7618 case 4:
7619 bfd_put_32 (input_bfd, x, location);
7620 break;
7621 case 8:
7622 #ifdef BFD64
7623 bfd_put_64 (input_bfd, x, location);
7624 #else
7625 abort ();
7626 #endif
7627 break;
7632 static bfd_vma
7633 get_value (bfd_vma size,
7634 unsigned long chunksz,
7635 bfd *input_bfd,
7636 bfd_byte *location)
7638 bfd_vma x = 0;
7640 for (; size; size -= chunksz, location += chunksz)
7642 switch (chunksz)
7644 default:
7645 case 0:
7646 abort ();
7647 case 1:
7648 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7649 break;
7650 case 2:
7651 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7652 break;
7653 case 4:
7654 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7655 break;
7656 case 8:
7657 #ifdef BFD64
7658 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7659 #else
7660 abort ();
7661 #endif
7662 break;
7665 return x;
7668 static void
7669 decode_complex_addend (unsigned long *start, /* in bits */
7670 unsigned long *oplen, /* in bits */
7671 unsigned long *len, /* in bits */
7672 unsigned long *wordsz, /* in bytes */
7673 unsigned long *chunksz, /* in bytes */
7674 unsigned long *lsb0_p,
7675 unsigned long *signed_p,
7676 unsigned long *trunc_p,
7677 unsigned long encoded)
7679 * start = encoded & 0x3F;
7680 * len = (encoded >> 6) & 0x3F;
7681 * oplen = (encoded >> 12) & 0x3F;
7682 * wordsz = (encoded >> 18) & 0xF;
7683 * chunksz = (encoded >> 22) & 0xF;
7684 * lsb0_p = (encoded >> 27) & 1;
7685 * signed_p = (encoded >> 28) & 1;
7686 * trunc_p = (encoded >> 29) & 1;
7689 bfd_reloc_status_type
7690 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7691 asection *input_section ATTRIBUTE_UNUSED,
7692 bfd_byte *contents,
7693 Elf_Internal_Rela *rel,
7694 bfd_vma relocation)
7696 bfd_vma shift, x, mask;
7697 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7698 bfd_reloc_status_type r;
7700 /* Perform this reloc, since it is complex.
7701 (this is not to say that it necessarily refers to a complex
7702 symbol; merely that it is a self-describing CGEN based reloc.
7703 i.e. the addend has the complete reloc information (bit start, end,
7704 word size, etc) encoded within it.). */
7706 decode_complex_addend (&start, &oplen, &len, &wordsz,
7707 &chunksz, &lsb0_p, &signed_p,
7708 &trunc_p, rel->r_addend);
7710 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7712 if (lsb0_p)
7713 shift = (start + 1) - len;
7714 else
7715 shift = (8 * wordsz) - (start + len);
7717 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7719 #ifdef DEBUG
7720 printf ("Doing complex reloc: "
7721 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7722 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7723 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7724 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7725 oplen, x, mask, relocation);
7726 #endif
7728 r = bfd_reloc_ok;
7729 if (! trunc_p)
7730 /* Now do an overflow check. */
7731 r = bfd_check_overflow ((signed_p
7732 ? complain_overflow_signed
7733 : complain_overflow_unsigned),
7734 len, 0, (8 * wordsz),
7735 relocation);
7737 /* Do the deed. */
7738 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7740 #ifdef DEBUG
7741 printf (" relocation: %8.8lx\n"
7742 " shifted mask: %8.8lx\n"
7743 " shifted/masked reloc: %8.8lx\n"
7744 " result: %8.8lx\n",
7745 relocation, (mask << shift),
7746 ((relocation & mask) << shift), x);
7747 #endif
7748 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7749 return r;
7752 /* When performing a relocatable link, the input relocations are
7753 preserved. But, if they reference global symbols, the indices
7754 referenced must be updated. Update all the relocations in
7755 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7757 static void
7758 elf_link_adjust_relocs (bfd *abfd,
7759 Elf_Internal_Shdr *rel_hdr,
7760 unsigned int count,
7761 struct elf_link_hash_entry **rel_hash)
7763 unsigned int i;
7764 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7765 bfd_byte *erela;
7766 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7767 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7768 bfd_vma r_type_mask;
7769 int r_sym_shift;
7771 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7773 swap_in = bed->s->swap_reloc_in;
7774 swap_out = bed->s->swap_reloc_out;
7776 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7778 swap_in = bed->s->swap_reloca_in;
7779 swap_out = bed->s->swap_reloca_out;
7781 else
7782 abort ();
7784 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7785 abort ();
7787 if (bed->s->arch_size == 32)
7789 r_type_mask = 0xff;
7790 r_sym_shift = 8;
7792 else
7794 r_type_mask = 0xffffffff;
7795 r_sym_shift = 32;
7798 erela = rel_hdr->contents;
7799 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7801 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7802 unsigned int j;
7804 if (*rel_hash == NULL)
7805 continue;
7807 BFD_ASSERT ((*rel_hash)->indx >= 0);
7809 (*swap_in) (abfd, erela, irela);
7810 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7811 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7812 | (irela[j].r_info & r_type_mask));
7813 (*swap_out) (abfd, irela, erela);
7817 struct elf_link_sort_rela
7819 union {
7820 bfd_vma offset;
7821 bfd_vma sym_mask;
7822 } u;
7823 enum elf_reloc_type_class type;
7824 /* We use this as an array of size int_rels_per_ext_rel. */
7825 Elf_Internal_Rela rela[1];
7828 static int
7829 elf_link_sort_cmp1 (const void *A, const void *B)
7831 const struct elf_link_sort_rela *a = A;
7832 const struct elf_link_sort_rela *b = B;
7833 int relativea, relativeb;
7835 relativea = a->type == reloc_class_relative;
7836 relativeb = b->type == reloc_class_relative;
7838 if (relativea < relativeb)
7839 return 1;
7840 if (relativea > relativeb)
7841 return -1;
7842 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7843 return -1;
7844 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7845 return 1;
7846 if (a->rela->r_offset < b->rela->r_offset)
7847 return -1;
7848 if (a->rela->r_offset > b->rela->r_offset)
7849 return 1;
7850 return 0;
7853 static int
7854 elf_link_sort_cmp2 (const void *A, const void *B)
7856 const struct elf_link_sort_rela *a = A;
7857 const struct elf_link_sort_rela *b = B;
7858 int copya, copyb;
7860 if (a->u.offset < b->u.offset)
7861 return -1;
7862 if (a->u.offset > b->u.offset)
7863 return 1;
7864 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7865 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7866 if (copya < copyb)
7867 return -1;
7868 if (copya > copyb)
7869 return 1;
7870 if (a->rela->r_offset < b->rela->r_offset)
7871 return -1;
7872 if (a->rela->r_offset > b->rela->r_offset)
7873 return 1;
7874 return 0;
7877 static size_t
7878 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7880 asection *dynamic_relocs;
7881 asection *rela_dyn;
7882 asection *rel_dyn;
7883 bfd_size_type count, size;
7884 size_t i, ret, sort_elt, ext_size;
7885 bfd_byte *sort, *s_non_relative, *p;
7886 struct elf_link_sort_rela *sq;
7887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7888 int i2e = bed->s->int_rels_per_ext_rel;
7889 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7890 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7891 struct bfd_link_order *lo;
7892 bfd_vma r_sym_mask;
7893 bfd_boolean use_rela;
7895 /* Find a dynamic reloc section. */
7896 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7897 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7898 if (rela_dyn != NULL && rela_dyn->size > 0
7899 && rel_dyn != NULL && rel_dyn->size > 0)
7901 bfd_boolean use_rela_initialised = FALSE;
7903 /* This is just here to stop gcc from complaining.
7904 It's initialization checking code is not perfect. */
7905 use_rela = TRUE;
7907 /* Both sections are present. Examine the sizes
7908 of the indirect sections to help us choose. */
7909 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7910 if (lo->type == bfd_indirect_link_order)
7912 asection *o = lo->u.indirect.section;
7914 if ((o->size % bed->s->sizeof_rela) == 0)
7916 if ((o->size % bed->s->sizeof_rel) == 0)
7917 /* Section size is divisible by both rel and rela sizes.
7918 It is of no help to us. */
7920 else
7922 /* Section size is only divisible by rela. */
7923 if (use_rela_initialised && (use_rela == FALSE))
7925 _bfd_error_handler
7926 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7927 bfd_set_error (bfd_error_invalid_operation);
7928 return 0;
7930 else
7932 use_rela = TRUE;
7933 use_rela_initialised = TRUE;
7937 else if ((o->size % bed->s->sizeof_rel) == 0)
7939 /* Section size is only divisible by rel. */
7940 if (use_rela_initialised && (use_rela == TRUE))
7942 _bfd_error_handler
7943 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7944 bfd_set_error (bfd_error_invalid_operation);
7945 return 0;
7947 else
7949 use_rela = FALSE;
7950 use_rela_initialised = TRUE;
7953 else
7955 /* The section size is not divisible by either - something is wrong. */
7956 _bfd_error_handler
7957 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7958 bfd_set_error (bfd_error_invalid_operation);
7959 return 0;
7963 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7964 if (lo->type == bfd_indirect_link_order)
7966 asection *o = lo->u.indirect.section;
7968 if ((o->size % bed->s->sizeof_rela) == 0)
7970 if ((o->size % bed->s->sizeof_rel) == 0)
7971 /* Section size is divisible by both rel and rela sizes.
7972 It is of no help to us. */
7974 else
7976 /* Section size is only divisible by rela. */
7977 if (use_rela_initialised && (use_rela == FALSE))
7979 _bfd_error_handler
7980 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7981 bfd_set_error (bfd_error_invalid_operation);
7982 return 0;
7984 else
7986 use_rela = TRUE;
7987 use_rela_initialised = TRUE;
7991 else if ((o->size % bed->s->sizeof_rel) == 0)
7993 /* Section size is only divisible by rel. */
7994 if (use_rela_initialised && (use_rela == TRUE))
7996 _bfd_error_handler
7997 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7998 bfd_set_error (bfd_error_invalid_operation);
7999 return 0;
8001 else
8003 use_rela = FALSE;
8004 use_rela_initialised = TRUE;
8007 else
8009 /* The section size is not divisible by either - something is wrong. */
8010 _bfd_error_handler
8011 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8012 bfd_set_error (bfd_error_invalid_operation);
8013 return 0;
8017 if (! use_rela_initialised)
8018 /* Make a guess. */
8019 use_rela = TRUE;
8021 else if (rela_dyn != NULL && rela_dyn->size > 0)
8022 use_rela = TRUE;
8023 else if (rel_dyn != NULL && rel_dyn->size > 0)
8024 use_rela = FALSE;
8025 else
8026 return 0;
8028 if (use_rela)
8030 dynamic_relocs = rela_dyn;
8031 ext_size = bed->s->sizeof_rela;
8032 swap_in = bed->s->swap_reloca_in;
8033 swap_out = bed->s->swap_reloca_out;
8035 else
8037 dynamic_relocs = rel_dyn;
8038 ext_size = bed->s->sizeof_rel;
8039 swap_in = bed->s->swap_reloc_in;
8040 swap_out = bed->s->swap_reloc_out;
8043 size = 0;
8044 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8045 if (lo->type == bfd_indirect_link_order)
8046 size += lo->u.indirect.section->size;
8048 if (size != dynamic_relocs->size)
8049 return 0;
8051 sort_elt = (sizeof (struct elf_link_sort_rela)
8052 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8054 count = dynamic_relocs->size / ext_size;
8055 sort = bfd_zmalloc (sort_elt * count);
8057 if (sort == NULL)
8059 (*info->callbacks->warning)
8060 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8061 return 0;
8064 if (bed->s->arch_size == 32)
8065 r_sym_mask = ~(bfd_vma) 0xff;
8066 else
8067 r_sym_mask = ~(bfd_vma) 0xffffffff;
8069 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8070 if (lo->type == bfd_indirect_link_order)
8072 bfd_byte *erel, *erelend;
8073 asection *o = lo->u.indirect.section;
8075 if (o->contents == NULL && o->size != 0)
8077 /* This is a reloc section that is being handled as a normal
8078 section. See bfd_section_from_shdr. We can't combine
8079 relocs in this case. */
8080 free (sort);
8081 return 0;
8083 erel = o->contents;
8084 erelend = o->contents + o->size;
8085 p = sort + o->output_offset / ext_size * sort_elt;
8087 while (erel < erelend)
8089 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8091 (*swap_in) (abfd, erel, s->rela);
8092 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8093 s->u.sym_mask = r_sym_mask;
8094 p += sort_elt;
8095 erel += ext_size;
8099 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8101 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8103 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8104 if (s->type != reloc_class_relative)
8105 break;
8107 ret = i;
8108 s_non_relative = p;
8110 sq = (struct elf_link_sort_rela *) s_non_relative;
8111 for (; i < count; i++, p += sort_elt)
8113 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8114 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8115 sq = sp;
8116 sp->u.offset = sq->rela->r_offset;
8119 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8121 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8122 if (lo->type == bfd_indirect_link_order)
8124 bfd_byte *erel, *erelend;
8125 asection *o = lo->u.indirect.section;
8127 erel = o->contents;
8128 erelend = o->contents + o->size;
8129 p = sort + o->output_offset / ext_size * sort_elt;
8130 while (erel < erelend)
8132 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8133 (*swap_out) (abfd, s->rela, erel);
8134 p += sort_elt;
8135 erel += ext_size;
8139 free (sort);
8140 *psec = dynamic_relocs;
8141 return ret;
8144 /* Flush the output symbols to the file. */
8146 static bfd_boolean
8147 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8148 const struct elf_backend_data *bed)
8150 if (finfo->symbuf_count > 0)
8152 Elf_Internal_Shdr *hdr;
8153 file_ptr pos;
8154 bfd_size_type amt;
8156 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8157 pos = hdr->sh_offset + hdr->sh_size;
8158 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8159 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8160 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8161 return FALSE;
8163 hdr->sh_size += amt;
8164 finfo->symbuf_count = 0;
8167 return TRUE;
8170 /* Add a symbol to the output symbol table. */
8172 static bfd_boolean
8173 elf_link_output_sym (struct elf_final_link_info *finfo,
8174 const char *name,
8175 Elf_Internal_Sym *elfsym,
8176 asection *input_sec,
8177 struct elf_link_hash_entry *h)
8179 bfd_byte *dest;
8180 Elf_External_Sym_Shndx *destshndx;
8181 bfd_boolean (*output_symbol_hook)
8182 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8183 struct elf_link_hash_entry *);
8184 const struct elf_backend_data *bed;
8186 bed = get_elf_backend_data (finfo->output_bfd);
8187 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8188 if (output_symbol_hook != NULL)
8190 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8191 return FALSE;
8194 if (name == NULL || *name == '\0')
8195 elfsym->st_name = 0;
8196 else if (input_sec->flags & SEC_EXCLUDE)
8197 elfsym->st_name = 0;
8198 else
8200 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8201 name, TRUE, FALSE);
8202 if (elfsym->st_name == (unsigned long) -1)
8203 return FALSE;
8206 if (finfo->symbuf_count >= finfo->symbuf_size)
8208 if (! elf_link_flush_output_syms (finfo, bed))
8209 return FALSE;
8212 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8213 destshndx = finfo->symshndxbuf;
8214 if (destshndx != NULL)
8216 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8218 bfd_size_type amt;
8220 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8221 destshndx = bfd_realloc (destshndx, amt * 2);
8222 if (destshndx == NULL)
8223 return FALSE;
8224 finfo->symshndxbuf = destshndx;
8225 memset ((char *) destshndx + amt, 0, amt);
8226 finfo->shndxbuf_size *= 2;
8228 destshndx += bfd_get_symcount (finfo->output_bfd);
8231 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8232 finfo->symbuf_count += 1;
8233 bfd_get_symcount (finfo->output_bfd) += 1;
8235 return TRUE;
8238 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8240 static bfd_boolean
8241 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8243 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8244 && sym->st_shndx < SHN_LORESERVE)
8246 /* The gABI doesn't support dynamic symbols in output sections
8247 beyond 64k. */
8248 (*_bfd_error_handler)
8249 (_("%B: Too many sections: %d (>= %d)"),
8250 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8251 bfd_set_error (bfd_error_nonrepresentable_section);
8252 return FALSE;
8254 return TRUE;
8257 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8258 allowing an unsatisfied unversioned symbol in the DSO to match a
8259 versioned symbol that would normally require an explicit version.
8260 We also handle the case that a DSO references a hidden symbol
8261 which may be satisfied by a versioned symbol in another DSO. */
8263 static bfd_boolean
8264 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8265 const struct elf_backend_data *bed,
8266 struct elf_link_hash_entry *h)
8268 bfd *abfd;
8269 struct elf_link_loaded_list *loaded;
8271 if (!is_elf_hash_table (info->hash))
8272 return FALSE;
8274 switch (h->root.type)
8276 default:
8277 abfd = NULL;
8278 break;
8280 case bfd_link_hash_undefined:
8281 case bfd_link_hash_undefweak:
8282 abfd = h->root.u.undef.abfd;
8283 if ((abfd->flags & DYNAMIC) == 0
8284 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8285 return FALSE;
8286 break;
8288 case bfd_link_hash_defined:
8289 case bfd_link_hash_defweak:
8290 abfd = h->root.u.def.section->owner;
8291 break;
8293 case bfd_link_hash_common:
8294 abfd = h->root.u.c.p->section->owner;
8295 break;
8297 BFD_ASSERT (abfd != NULL);
8299 for (loaded = elf_hash_table (info)->loaded;
8300 loaded != NULL;
8301 loaded = loaded->next)
8303 bfd *input;
8304 Elf_Internal_Shdr *hdr;
8305 bfd_size_type symcount;
8306 bfd_size_type extsymcount;
8307 bfd_size_type extsymoff;
8308 Elf_Internal_Shdr *versymhdr;
8309 Elf_Internal_Sym *isym;
8310 Elf_Internal_Sym *isymend;
8311 Elf_Internal_Sym *isymbuf;
8312 Elf_External_Versym *ever;
8313 Elf_External_Versym *extversym;
8315 input = loaded->abfd;
8317 /* We check each DSO for a possible hidden versioned definition. */
8318 if (input == abfd
8319 || (input->flags & DYNAMIC) == 0
8320 || elf_dynversym (input) == 0)
8321 continue;
8323 hdr = &elf_tdata (input)->dynsymtab_hdr;
8325 symcount = hdr->sh_size / bed->s->sizeof_sym;
8326 if (elf_bad_symtab (input))
8328 extsymcount = symcount;
8329 extsymoff = 0;
8331 else
8333 extsymcount = symcount - hdr->sh_info;
8334 extsymoff = hdr->sh_info;
8337 if (extsymcount == 0)
8338 continue;
8340 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8341 NULL, NULL, NULL);
8342 if (isymbuf == NULL)
8343 return FALSE;
8345 /* Read in any version definitions. */
8346 versymhdr = &elf_tdata (input)->dynversym_hdr;
8347 extversym = bfd_malloc (versymhdr->sh_size);
8348 if (extversym == NULL)
8349 goto error_ret;
8351 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8352 || (bfd_bread (extversym, versymhdr->sh_size, input)
8353 != versymhdr->sh_size))
8355 free (extversym);
8356 error_ret:
8357 free (isymbuf);
8358 return FALSE;
8361 ever = extversym + extsymoff;
8362 isymend = isymbuf + extsymcount;
8363 for (isym = isymbuf; isym < isymend; isym++, ever++)
8365 const char *name;
8366 Elf_Internal_Versym iver;
8367 unsigned short version_index;
8369 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8370 || isym->st_shndx == SHN_UNDEF)
8371 continue;
8373 name = bfd_elf_string_from_elf_section (input,
8374 hdr->sh_link,
8375 isym->st_name);
8376 if (strcmp (name, h->root.root.string) != 0)
8377 continue;
8379 _bfd_elf_swap_versym_in (input, ever, &iver);
8381 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8383 /* If we have a non-hidden versioned sym, then it should
8384 have provided a definition for the undefined sym. */
8385 abort ();
8388 version_index = iver.vs_vers & VERSYM_VERSION;
8389 if (version_index == 1 || version_index == 2)
8391 /* This is the base or first version. We can use it. */
8392 free (extversym);
8393 free (isymbuf);
8394 return TRUE;
8398 free (extversym);
8399 free (isymbuf);
8402 return FALSE;
8405 /* Add an external symbol to the symbol table. This is called from
8406 the hash table traversal routine. When generating a shared object,
8407 we go through the symbol table twice. The first time we output
8408 anything that might have been forced to local scope in a version
8409 script. The second time we output the symbols that are still
8410 global symbols. */
8412 static bfd_boolean
8413 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8415 struct elf_outext_info *eoinfo = data;
8416 struct elf_final_link_info *finfo = eoinfo->finfo;
8417 bfd_boolean strip;
8418 Elf_Internal_Sym sym;
8419 asection *input_sec;
8420 const struct elf_backend_data *bed;
8422 if (h->root.type == bfd_link_hash_warning)
8424 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8425 if (h->root.type == bfd_link_hash_new)
8426 return TRUE;
8429 /* Decide whether to output this symbol in this pass. */
8430 if (eoinfo->localsyms)
8432 if (!h->forced_local)
8433 return TRUE;
8435 else
8437 if (h->forced_local)
8438 return TRUE;
8441 bed = get_elf_backend_data (finfo->output_bfd);
8443 if (h->root.type == bfd_link_hash_undefined)
8445 /* If we have an undefined symbol reference here then it must have
8446 come from a shared library that is being linked in. (Undefined
8447 references in regular files have already been handled). */
8448 bfd_boolean ignore_undef = FALSE;
8450 /* Some symbols may be special in that the fact that they're
8451 undefined can be safely ignored - let backend determine that. */
8452 if (bed->elf_backend_ignore_undef_symbol)
8453 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8455 /* If we are reporting errors for this situation then do so now. */
8456 if (ignore_undef == FALSE
8457 && h->ref_dynamic
8458 && ! h->ref_regular
8459 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8460 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8462 if (! (finfo->info->callbacks->undefined_symbol
8463 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8464 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8466 eoinfo->failed = TRUE;
8467 return FALSE;
8472 /* We should also warn if a forced local symbol is referenced from
8473 shared libraries. */
8474 if (! finfo->info->relocatable
8475 && (! finfo->info->shared)
8476 && h->forced_local
8477 && h->ref_dynamic
8478 && !h->dynamic_def
8479 && !h->dynamic_weak
8480 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8482 (*_bfd_error_handler)
8483 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8484 finfo->output_bfd,
8485 h->root.u.def.section == bfd_abs_section_ptr
8486 ? finfo->output_bfd : h->root.u.def.section->owner,
8487 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8488 ? "internal"
8489 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8490 ? "hidden" : "local",
8491 h->root.root.string);
8492 eoinfo->failed = TRUE;
8493 return FALSE;
8496 /* We don't want to output symbols that have never been mentioned by
8497 a regular file, or that we have been told to strip. However, if
8498 h->indx is set to -2, the symbol is used by a reloc and we must
8499 output it. */
8500 if (h->indx == -2)
8501 strip = FALSE;
8502 else if ((h->def_dynamic
8503 || h->ref_dynamic
8504 || h->root.type == bfd_link_hash_new)
8505 && !h->def_regular
8506 && !h->ref_regular)
8507 strip = TRUE;
8508 else if (finfo->info->strip == strip_all)
8509 strip = TRUE;
8510 else if (finfo->info->strip == strip_some
8511 && bfd_hash_lookup (finfo->info->keep_hash,
8512 h->root.root.string, FALSE, FALSE) == NULL)
8513 strip = TRUE;
8514 else if (finfo->info->strip_discarded
8515 && (h->root.type == bfd_link_hash_defined
8516 || h->root.type == bfd_link_hash_defweak)
8517 && elf_discarded_section (h->root.u.def.section))
8518 strip = TRUE;
8519 else
8520 strip = FALSE;
8522 /* If we're stripping it, and it's not a dynamic symbol, there's
8523 nothing else to do unless it is a forced local symbol. */
8524 if (strip
8525 && h->dynindx == -1
8526 && !h->forced_local)
8527 return TRUE;
8529 sym.st_value = 0;
8530 sym.st_size = h->size;
8531 sym.st_other = h->other;
8532 if (h->forced_local)
8533 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8534 else if (h->root.type == bfd_link_hash_undefweak
8535 || h->root.type == bfd_link_hash_defweak)
8536 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8537 else
8538 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8540 switch (h->root.type)
8542 default:
8543 case bfd_link_hash_new:
8544 case bfd_link_hash_warning:
8545 abort ();
8546 return FALSE;
8548 case bfd_link_hash_undefined:
8549 case bfd_link_hash_undefweak:
8550 input_sec = bfd_und_section_ptr;
8551 sym.st_shndx = SHN_UNDEF;
8552 break;
8554 case bfd_link_hash_defined:
8555 case bfd_link_hash_defweak:
8557 input_sec = h->root.u.def.section;
8558 if (input_sec->output_section != NULL)
8560 sym.st_shndx =
8561 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8562 input_sec->output_section);
8563 if (sym.st_shndx == SHN_BAD)
8565 (*_bfd_error_handler)
8566 (_("%B: could not find output section %A for input section %A"),
8567 finfo->output_bfd, input_sec->output_section, input_sec);
8568 eoinfo->failed = TRUE;
8569 return FALSE;
8572 /* ELF symbols in relocatable files are section relative,
8573 but in nonrelocatable files they are virtual
8574 addresses. */
8575 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8576 if (! finfo->info->relocatable)
8578 sym.st_value += input_sec->output_section->vma;
8579 if (h->type == STT_TLS)
8581 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8582 if (tls_sec != NULL)
8583 sym.st_value -= tls_sec->vma;
8584 else
8586 /* The TLS section may have been garbage collected. */
8587 BFD_ASSERT (finfo->info->gc_sections
8588 && !input_sec->gc_mark);
8593 else
8595 BFD_ASSERT (input_sec->owner == NULL
8596 || (input_sec->owner->flags & DYNAMIC) != 0);
8597 sym.st_shndx = SHN_UNDEF;
8598 input_sec = bfd_und_section_ptr;
8601 break;
8603 case bfd_link_hash_common:
8604 input_sec = h->root.u.c.p->section;
8605 sym.st_shndx = bed->common_section_index (input_sec);
8606 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8607 break;
8609 case bfd_link_hash_indirect:
8610 /* These symbols are created by symbol versioning. They point
8611 to the decorated version of the name. For example, if the
8612 symbol foo@@GNU_1.2 is the default, which should be used when
8613 foo is used with no version, then we add an indirect symbol
8614 foo which points to foo@@GNU_1.2. We ignore these symbols,
8615 since the indirected symbol is already in the hash table. */
8616 return TRUE;
8619 /* Give the processor backend a chance to tweak the symbol value,
8620 and also to finish up anything that needs to be done for this
8621 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8622 forced local syms when non-shared is due to a historical quirk. */
8623 if ((h->dynindx != -1
8624 || h->forced_local)
8625 && ((finfo->info->shared
8626 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8627 || h->root.type != bfd_link_hash_undefweak))
8628 || !h->forced_local)
8629 && elf_hash_table (finfo->info)->dynamic_sections_created)
8631 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8632 (finfo->output_bfd, finfo->info, h, &sym)))
8634 eoinfo->failed = TRUE;
8635 return FALSE;
8639 /* If we are marking the symbol as undefined, and there are no
8640 non-weak references to this symbol from a regular object, then
8641 mark the symbol as weak undefined; if there are non-weak
8642 references, mark the symbol as strong. We can't do this earlier,
8643 because it might not be marked as undefined until the
8644 finish_dynamic_symbol routine gets through with it. */
8645 if (sym.st_shndx == SHN_UNDEF
8646 && h->ref_regular
8647 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8648 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8650 int bindtype;
8652 if (h->ref_regular_nonweak)
8653 bindtype = STB_GLOBAL;
8654 else
8655 bindtype = STB_WEAK;
8656 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8659 /* If this is a symbol defined in a dynamic library, don't use the
8660 symbol size from the dynamic library. Relinking an executable
8661 against a new library may introduce gratuitous changes in the
8662 executable's symbols if we keep the size. */
8663 if (sym.st_shndx == SHN_UNDEF
8664 && !h->def_regular
8665 && h->def_dynamic)
8666 sym.st_size = 0;
8668 /* If a non-weak symbol with non-default visibility is not defined
8669 locally, it is a fatal error. */
8670 if (! finfo->info->relocatable
8671 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8672 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8673 && h->root.type == bfd_link_hash_undefined
8674 && !h->def_regular)
8676 (*_bfd_error_handler)
8677 (_("%B: %s symbol `%s' isn't defined"),
8678 finfo->output_bfd,
8679 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8680 ? "protected"
8681 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8682 ? "internal" : "hidden",
8683 h->root.root.string);
8684 eoinfo->failed = TRUE;
8685 return FALSE;
8688 /* If this symbol should be put in the .dynsym section, then put it
8689 there now. We already know the symbol index. We also fill in
8690 the entry in the .hash section. */
8691 if (h->dynindx != -1
8692 && elf_hash_table (finfo->info)->dynamic_sections_created)
8694 bfd_byte *esym;
8696 sym.st_name = h->dynstr_index;
8697 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8698 if (! check_dynsym (finfo->output_bfd, &sym))
8700 eoinfo->failed = TRUE;
8701 return FALSE;
8703 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8705 if (finfo->hash_sec != NULL)
8707 size_t hash_entry_size;
8708 bfd_byte *bucketpos;
8709 bfd_vma chain;
8710 size_t bucketcount;
8711 size_t bucket;
8713 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8714 bucket = h->u.elf_hash_value % bucketcount;
8716 hash_entry_size
8717 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8718 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8719 + (bucket + 2) * hash_entry_size);
8720 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8721 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8722 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8723 ((bfd_byte *) finfo->hash_sec->contents
8724 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8727 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8729 Elf_Internal_Versym iversym;
8730 Elf_External_Versym *eversym;
8732 if (!h->def_regular)
8734 if (h->verinfo.verdef == NULL)
8735 iversym.vs_vers = 0;
8736 else
8737 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8739 else
8741 if (h->verinfo.vertree == NULL)
8742 iversym.vs_vers = 1;
8743 else
8744 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8745 if (finfo->info->create_default_symver)
8746 iversym.vs_vers++;
8749 if (h->hidden)
8750 iversym.vs_vers |= VERSYM_HIDDEN;
8752 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8753 eversym += h->dynindx;
8754 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8758 /* If we're stripping it, then it was just a dynamic symbol, and
8759 there's nothing else to do. */
8760 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8761 return TRUE;
8763 h->indx = bfd_get_symcount (finfo->output_bfd);
8765 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8767 eoinfo->failed = TRUE;
8768 return FALSE;
8771 return TRUE;
8774 /* Return TRUE if special handling is done for relocs in SEC against
8775 symbols defined in discarded sections. */
8777 static bfd_boolean
8778 elf_section_ignore_discarded_relocs (asection *sec)
8780 const struct elf_backend_data *bed;
8782 switch (sec->sec_info_type)
8784 case ELF_INFO_TYPE_STABS:
8785 case ELF_INFO_TYPE_EH_FRAME:
8786 return TRUE;
8787 default:
8788 break;
8791 bed = get_elf_backend_data (sec->owner);
8792 if (bed->elf_backend_ignore_discarded_relocs != NULL
8793 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8794 return TRUE;
8796 return FALSE;
8799 /* Return a mask saying how ld should treat relocations in SEC against
8800 symbols defined in discarded sections. If this function returns
8801 COMPLAIN set, ld will issue a warning message. If this function
8802 returns PRETEND set, and the discarded section was link-once and the
8803 same size as the kept link-once section, ld will pretend that the
8804 symbol was actually defined in the kept section. Otherwise ld will
8805 zero the reloc (at least that is the intent, but some cooperation by
8806 the target dependent code is needed, particularly for REL targets). */
8808 unsigned int
8809 _bfd_elf_default_action_discarded (asection *sec)
8811 if (sec->flags & SEC_DEBUGGING)
8812 return PRETEND;
8814 if (strcmp (".eh_frame", sec->name) == 0)
8815 return 0;
8817 if (strcmp (".gcc_except_table", sec->name) == 0)
8818 return 0;
8820 return COMPLAIN | PRETEND;
8823 /* Find a match between a section and a member of a section group. */
8825 static asection *
8826 match_group_member (asection *sec, asection *group,
8827 struct bfd_link_info *info)
8829 asection *first = elf_next_in_group (group);
8830 asection *s = first;
8832 while (s != NULL)
8834 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8835 return s;
8837 s = elf_next_in_group (s);
8838 if (s == first)
8839 break;
8842 return NULL;
8845 /* Check if the kept section of a discarded section SEC can be used
8846 to replace it. Return the replacement if it is OK. Otherwise return
8847 NULL. */
8849 asection *
8850 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8852 asection *kept;
8854 kept = sec->kept_section;
8855 if (kept != NULL)
8857 if ((kept->flags & SEC_GROUP) != 0)
8858 kept = match_group_member (sec, kept, info);
8859 if (kept != NULL
8860 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8861 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8862 kept = NULL;
8863 sec->kept_section = kept;
8865 return kept;
8868 /* Link an input file into the linker output file. This function
8869 handles all the sections and relocations of the input file at once.
8870 This is so that we only have to read the local symbols once, and
8871 don't have to keep them in memory. */
8873 static bfd_boolean
8874 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8876 int (*relocate_section)
8877 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8878 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8879 bfd *output_bfd;
8880 Elf_Internal_Shdr *symtab_hdr;
8881 size_t locsymcount;
8882 size_t extsymoff;
8883 Elf_Internal_Sym *isymbuf;
8884 Elf_Internal_Sym *isym;
8885 Elf_Internal_Sym *isymend;
8886 long *pindex;
8887 asection **ppsection;
8888 asection *o;
8889 const struct elf_backend_data *bed;
8890 struct elf_link_hash_entry **sym_hashes;
8892 output_bfd = finfo->output_bfd;
8893 bed = get_elf_backend_data (output_bfd);
8894 relocate_section = bed->elf_backend_relocate_section;
8896 /* If this is a dynamic object, we don't want to do anything here:
8897 we don't want the local symbols, and we don't want the section
8898 contents. */
8899 if ((input_bfd->flags & DYNAMIC) != 0)
8900 return TRUE;
8902 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8903 if (elf_bad_symtab (input_bfd))
8905 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8906 extsymoff = 0;
8908 else
8910 locsymcount = symtab_hdr->sh_info;
8911 extsymoff = symtab_hdr->sh_info;
8914 /* Read the local symbols. */
8915 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8916 if (isymbuf == NULL && locsymcount != 0)
8918 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8919 finfo->internal_syms,
8920 finfo->external_syms,
8921 finfo->locsym_shndx);
8922 if (isymbuf == NULL)
8923 return FALSE;
8926 /* Find local symbol sections and adjust values of symbols in
8927 SEC_MERGE sections. Write out those local symbols we know are
8928 going into the output file. */
8929 isymend = isymbuf + locsymcount;
8930 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8931 isym < isymend;
8932 isym++, pindex++, ppsection++)
8934 asection *isec;
8935 const char *name;
8936 Elf_Internal_Sym osym;
8938 *pindex = -1;
8940 if (elf_bad_symtab (input_bfd))
8942 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8944 *ppsection = NULL;
8945 continue;
8949 if (isym->st_shndx == SHN_UNDEF)
8950 isec = bfd_und_section_ptr;
8951 else if (isym->st_shndx == SHN_ABS)
8952 isec = bfd_abs_section_ptr;
8953 else if (isym->st_shndx == SHN_COMMON)
8954 isec = bfd_com_section_ptr;
8955 else
8957 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8958 if (isec == NULL)
8960 /* Don't attempt to output symbols with st_shnx in the
8961 reserved range other than SHN_ABS and SHN_COMMON. */
8962 *ppsection = NULL;
8963 continue;
8965 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8966 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8967 isym->st_value =
8968 _bfd_merged_section_offset (output_bfd, &isec,
8969 elf_section_data (isec)->sec_info,
8970 isym->st_value);
8973 *ppsection = isec;
8975 /* Don't output the first, undefined, symbol. */
8976 if (ppsection == finfo->sections)
8977 continue;
8979 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8981 /* We never output section symbols. Instead, we use the
8982 section symbol of the corresponding section in the output
8983 file. */
8984 continue;
8987 /* If we are stripping all symbols, we don't want to output this
8988 one. */
8989 if (finfo->info->strip == strip_all)
8990 continue;
8992 /* If we are discarding all local symbols, we don't want to
8993 output this one. If we are generating a relocatable output
8994 file, then some of the local symbols may be required by
8995 relocs; we output them below as we discover that they are
8996 needed. */
8997 if (finfo->info->discard == discard_all)
8998 continue;
9000 /* If this symbol is defined in a section which we are
9001 discarding, we don't need to keep it. */
9002 if (isym->st_shndx != SHN_UNDEF
9003 && isym->st_shndx < SHN_LORESERVE
9004 && bfd_section_removed_from_list (output_bfd,
9005 isec->output_section))
9006 continue;
9008 /* Get the name of the symbol. */
9009 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9010 isym->st_name);
9011 if (name == NULL)
9012 return FALSE;
9014 /* See if we are discarding symbols with this name. */
9015 if ((finfo->info->strip == strip_some
9016 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9017 == NULL))
9018 || (((finfo->info->discard == discard_sec_merge
9019 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9020 || finfo->info->discard == discard_l)
9021 && bfd_is_local_label_name (input_bfd, name)))
9022 continue;
9024 /* If we get here, we are going to output this symbol. */
9026 osym = *isym;
9028 /* Adjust the section index for the output file. */
9029 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9030 isec->output_section);
9031 if (osym.st_shndx == SHN_BAD)
9032 return FALSE;
9034 *pindex = bfd_get_symcount (output_bfd);
9036 /* ELF symbols in relocatable files are section relative, but
9037 in executable files they are virtual addresses. Note that
9038 this code assumes that all ELF sections have an associated
9039 BFD section with a reasonable value for output_offset; below
9040 we assume that they also have a reasonable value for
9041 output_section. Any special sections must be set up to meet
9042 these requirements. */
9043 osym.st_value += isec->output_offset;
9044 if (! finfo->info->relocatable)
9046 osym.st_value += isec->output_section->vma;
9047 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9049 /* STT_TLS symbols are relative to PT_TLS segment base. */
9050 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9051 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9055 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9056 return FALSE;
9059 /* Relocate the contents of each section. */
9060 sym_hashes = elf_sym_hashes (input_bfd);
9061 for (o = input_bfd->sections; o != NULL; o = o->next)
9063 bfd_byte *contents;
9065 if (! o->linker_mark)
9067 /* This section was omitted from the link. */
9068 continue;
9071 if (finfo->info->relocatable
9072 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9074 /* Deal with the group signature symbol. */
9075 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9076 unsigned long symndx = sec_data->this_hdr.sh_info;
9077 asection *osec = o->output_section;
9079 if (symndx >= locsymcount
9080 || (elf_bad_symtab (input_bfd)
9081 && finfo->sections[symndx] == NULL))
9083 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9084 while (h->root.type == bfd_link_hash_indirect
9085 || h->root.type == bfd_link_hash_warning)
9086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9087 /* Arrange for symbol to be output. */
9088 h->indx = -2;
9089 elf_section_data (osec)->this_hdr.sh_info = -2;
9091 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9093 /* We'll use the output section target_index. */
9094 asection *sec = finfo->sections[symndx]->output_section;
9095 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9097 else
9099 if (finfo->indices[symndx] == -1)
9101 /* Otherwise output the local symbol now. */
9102 Elf_Internal_Sym sym = isymbuf[symndx];
9103 asection *sec = finfo->sections[symndx]->output_section;
9104 const char *name;
9106 name = bfd_elf_string_from_elf_section (input_bfd,
9107 symtab_hdr->sh_link,
9108 sym.st_name);
9109 if (name == NULL)
9110 return FALSE;
9112 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9113 sec);
9114 if (sym.st_shndx == SHN_BAD)
9115 return FALSE;
9117 sym.st_value += o->output_offset;
9119 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9120 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9121 return FALSE;
9123 elf_section_data (osec)->this_hdr.sh_info
9124 = finfo->indices[symndx];
9128 if ((o->flags & SEC_HAS_CONTENTS) == 0
9129 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9130 continue;
9132 if ((o->flags & SEC_LINKER_CREATED) != 0)
9134 /* Section was created by _bfd_elf_link_create_dynamic_sections
9135 or somesuch. */
9136 continue;
9139 /* Get the contents of the section. They have been cached by a
9140 relaxation routine. Note that o is a section in an input
9141 file, so the contents field will not have been set by any of
9142 the routines which work on output files. */
9143 if (elf_section_data (o)->this_hdr.contents != NULL)
9144 contents = elf_section_data (o)->this_hdr.contents;
9145 else
9147 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9149 contents = finfo->contents;
9150 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9151 return FALSE;
9154 if ((o->flags & SEC_RELOC) != 0)
9156 Elf_Internal_Rela *internal_relocs;
9157 Elf_Internal_Rela *rel, *relend;
9158 bfd_vma r_type_mask;
9159 int r_sym_shift;
9160 int action_discarded;
9161 int ret;
9163 /* Get the swapped relocs. */
9164 internal_relocs
9165 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9166 finfo->internal_relocs, FALSE);
9167 if (internal_relocs == NULL
9168 && o->reloc_count > 0)
9169 return FALSE;
9171 if (bed->s->arch_size == 32)
9173 r_type_mask = 0xff;
9174 r_sym_shift = 8;
9176 else
9178 r_type_mask = 0xffffffff;
9179 r_sym_shift = 32;
9182 action_discarded = -1;
9183 if (!elf_section_ignore_discarded_relocs (o))
9184 action_discarded = (*bed->action_discarded) (o);
9186 /* Run through the relocs evaluating complex reloc symbols and
9187 looking for relocs against symbols from discarded sections
9188 or section symbols from removed link-once sections.
9189 Complain about relocs against discarded sections. Zero
9190 relocs against removed link-once sections. */
9192 rel = internal_relocs;
9193 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9194 for ( ; rel < relend; rel++)
9196 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9197 unsigned int s_type;
9198 asection **ps, *sec;
9199 struct elf_link_hash_entry *h = NULL;
9200 const char *sym_name;
9202 if (r_symndx == STN_UNDEF)
9203 continue;
9205 if (r_symndx >= locsymcount
9206 || (elf_bad_symtab (input_bfd)
9207 && finfo->sections[r_symndx] == NULL))
9209 h = sym_hashes[r_symndx - extsymoff];
9211 /* Badly formatted input files can contain relocs that
9212 reference non-existant symbols. Check here so that
9213 we do not seg fault. */
9214 if (h == NULL)
9216 char buffer [32];
9218 sprintf_vma (buffer, rel->r_info);
9219 (*_bfd_error_handler)
9220 (_("error: %B contains a reloc (0x%s) for section %A "
9221 "that references a non-existent global symbol"),
9222 input_bfd, o, buffer);
9223 bfd_set_error (bfd_error_bad_value);
9224 return FALSE;
9227 while (h->root.type == bfd_link_hash_indirect
9228 || h->root.type == bfd_link_hash_warning)
9229 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9231 s_type = h->type;
9233 ps = NULL;
9234 if (h->root.type == bfd_link_hash_defined
9235 || h->root.type == bfd_link_hash_defweak)
9236 ps = &h->root.u.def.section;
9238 sym_name = h->root.root.string;
9240 else
9242 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9244 s_type = ELF_ST_TYPE (sym->st_info);
9245 ps = &finfo->sections[r_symndx];
9246 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9247 sym, *ps);
9250 if (s_type == STT_RELC || s_type == STT_SRELC)
9252 bfd_vma val;
9253 bfd_vma dot = (rel->r_offset
9254 + o->output_offset + o->output_section->vma);
9255 #ifdef DEBUG
9256 printf ("Encountered a complex symbol!");
9257 printf (" (input_bfd %s, section %s, reloc %ld\n",
9258 input_bfd->filename, o->name, rel - internal_relocs);
9259 printf (" symbol: idx %8.8lx, name %s\n",
9260 r_symndx, sym_name);
9261 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9262 (unsigned long) rel->r_info,
9263 (unsigned long) rel->r_offset);
9264 #endif
9265 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9266 isymbuf, locsymcount, s_type == STT_SRELC))
9267 return FALSE;
9269 /* Symbol evaluated OK. Update to absolute value. */
9270 set_symbol_value (input_bfd, isymbuf, locsymcount,
9271 r_symndx, val);
9272 continue;
9275 if (action_discarded != -1 && ps != NULL)
9277 /* Complain if the definition comes from a
9278 discarded section. */
9279 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9281 BFD_ASSERT (r_symndx != 0);
9282 if (action_discarded & COMPLAIN)
9283 (*finfo->info->callbacks->einfo)
9284 (_("%X`%s' referenced in section `%A' of %B: "
9285 "defined in discarded section `%A' of %B\n"),
9286 sym_name, o, input_bfd, sec, sec->owner);
9288 /* Try to do the best we can to support buggy old
9289 versions of gcc. Pretend that the symbol is
9290 really defined in the kept linkonce section.
9291 FIXME: This is quite broken. Modifying the
9292 symbol here means we will be changing all later
9293 uses of the symbol, not just in this section. */
9294 if (action_discarded & PRETEND)
9296 asection *kept;
9298 kept = _bfd_elf_check_kept_section (sec,
9299 finfo->info);
9300 if (kept != NULL)
9302 *ps = kept;
9303 continue;
9310 /* Relocate the section by invoking a back end routine.
9312 The back end routine is responsible for adjusting the
9313 section contents as necessary, and (if using Rela relocs
9314 and generating a relocatable output file) adjusting the
9315 reloc addend as necessary.
9317 The back end routine does not have to worry about setting
9318 the reloc address or the reloc symbol index.
9320 The back end routine is given a pointer to the swapped in
9321 internal symbols, and can access the hash table entries
9322 for the external symbols via elf_sym_hashes (input_bfd).
9324 When generating relocatable output, the back end routine
9325 must handle STB_LOCAL/STT_SECTION symbols specially. The
9326 output symbol is going to be a section symbol
9327 corresponding to the output section, which will require
9328 the addend to be adjusted. */
9330 ret = (*relocate_section) (output_bfd, finfo->info,
9331 input_bfd, o, contents,
9332 internal_relocs,
9333 isymbuf,
9334 finfo->sections);
9335 if (!ret)
9336 return FALSE;
9338 if (ret == 2
9339 || finfo->info->relocatable
9340 || finfo->info->emitrelocations)
9342 Elf_Internal_Rela *irela;
9343 Elf_Internal_Rela *irelaend;
9344 bfd_vma last_offset;
9345 struct elf_link_hash_entry **rel_hash;
9346 struct elf_link_hash_entry **rel_hash_list;
9347 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9348 unsigned int next_erel;
9349 bfd_boolean rela_normal;
9351 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9352 rela_normal = (bed->rela_normal
9353 && (input_rel_hdr->sh_entsize
9354 == bed->s->sizeof_rela));
9356 /* Adjust the reloc addresses and symbol indices. */
9358 irela = internal_relocs;
9359 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9360 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9361 + elf_section_data (o->output_section)->rel_count
9362 + elf_section_data (o->output_section)->rel_count2);
9363 rel_hash_list = rel_hash;
9364 last_offset = o->output_offset;
9365 if (!finfo->info->relocatable)
9366 last_offset += o->output_section->vma;
9367 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9369 unsigned long r_symndx;
9370 asection *sec;
9371 Elf_Internal_Sym sym;
9373 if (next_erel == bed->s->int_rels_per_ext_rel)
9375 rel_hash++;
9376 next_erel = 0;
9379 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9380 finfo->info, o,
9381 irela->r_offset);
9382 if (irela->r_offset >= (bfd_vma) -2)
9384 /* This is a reloc for a deleted entry or somesuch.
9385 Turn it into an R_*_NONE reloc, at the same
9386 offset as the last reloc. elf_eh_frame.c and
9387 bfd_elf_discard_info rely on reloc offsets
9388 being ordered. */
9389 irela->r_offset = last_offset;
9390 irela->r_info = 0;
9391 irela->r_addend = 0;
9392 continue;
9395 irela->r_offset += o->output_offset;
9397 /* Relocs in an executable have to be virtual addresses. */
9398 if (!finfo->info->relocatable)
9399 irela->r_offset += o->output_section->vma;
9401 last_offset = irela->r_offset;
9403 r_symndx = irela->r_info >> r_sym_shift;
9404 if (r_symndx == STN_UNDEF)
9405 continue;
9407 if (r_symndx >= locsymcount
9408 || (elf_bad_symtab (input_bfd)
9409 && finfo->sections[r_symndx] == NULL))
9411 struct elf_link_hash_entry *rh;
9412 unsigned long indx;
9414 /* This is a reloc against a global symbol. We
9415 have not yet output all the local symbols, so
9416 we do not know the symbol index of any global
9417 symbol. We set the rel_hash entry for this
9418 reloc to point to the global hash table entry
9419 for this symbol. The symbol index is then
9420 set at the end of bfd_elf_final_link. */
9421 indx = r_symndx - extsymoff;
9422 rh = elf_sym_hashes (input_bfd)[indx];
9423 while (rh->root.type == bfd_link_hash_indirect
9424 || rh->root.type == bfd_link_hash_warning)
9425 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9427 /* Setting the index to -2 tells
9428 elf_link_output_extsym that this symbol is
9429 used by a reloc. */
9430 BFD_ASSERT (rh->indx < 0);
9431 rh->indx = -2;
9433 *rel_hash = rh;
9435 continue;
9438 /* This is a reloc against a local symbol. */
9440 *rel_hash = NULL;
9441 sym = isymbuf[r_symndx];
9442 sec = finfo->sections[r_symndx];
9443 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9445 /* I suppose the backend ought to fill in the
9446 section of any STT_SECTION symbol against a
9447 processor specific section. */
9448 r_symndx = 0;
9449 if (bfd_is_abs_section (sec))
9451 else if (sec == NULL || sec->owner == NULL)
9453 bfd_set_error (bfd_error_bad_value);
9454 return FALSE;
9456 else
9458 asection *osec = sec->output_section;
9460 /* If we have discarded a section, the output
9461 section will be the absolute section. In
9462 case of discarded SEC_MERGE sections, use
9463 the kept section. relocate_section should
9464 have already handled discarded linkonce
9465 sections. */
9466 if (bfd_is_abs_section (osec)
9467 && sec->kept_section != NULL
9468 && sec->kept_section->output_section != NULL)
9470 osec = sec->kept_section->output_section;
9471 irela->r_addend -= osec->vma;
9474 if (!bfd_is_abs_section (osec))
9476 r_symndx = osec->target_index;
9477 if (r_symndx == 0)
9479 struct elf_link_hash_table *htab;
9480 asection *oi;
9482 htab = elf_hash_table (finfo->info);
9483 oi = htab->text_index_section;
9484 if ((osec->flags & SEC_READONLY) == 0
9485 && htab->data_index_section != NULL)
9486 oi = htab->data_index_section;
9488 if (oi != NULL)
9490 irela->r_addend += osec->vma - oi->vma;
9491 r_symndx = oi->target_index;
9495 BFD_ASSERT (r_symndx != 0);
9499 /* Adjust the addend according to where the
9500 section winds up in the output section. */
9501 if (rela_normal)
9502 irela->r_addend += sec->output_offset;
9504 else
9506 if (finfo->indices[r_symndx] == -1)
9508 unsigned long shlink;
9509 const char *name;
9510 asection *osec;
9512 if (finfo->info->strip == strip_all)
9514 /* You can't do ld -r -s. */
9515 bfd_set_error (bfd_error_invalid_operation);
9516 return FALSE;
9519 /* This symbol was skipped earlier, but
9520 since it is needed by a reloc, we
9521 must output it now. */
9522 shlink = symtab_hdr->sh_link;
9523 name = (bfd_elf_string_from_elf_section
9524 (input_bfd, shlink, sym.st_name));
9525 if (name == NULL)
9526 return FALSE;
9528 osec = sec->output_section;
9529 sym.st_shndx =
9530 _bfd_elf_section_from_bfd_section (output_bfd,
9531 osec);
9532 if (sym.st_shndx == SHN_BAD)
9533 return FALSE;
9535 sym.st_value += sec->output_offset;
9536 if (! finfo->info->relocatable)
9538 sym.st_value += osec->vma;
9539 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9541 /* STT_TLS symbols are relative to PT_TLS
9542 segment base. */
9543 BFD_ASSERT (elf_hash_table (finfo->info)
9544 ->tls_sec != NULL);
9545 sym.st_value -= (elf_hash_table (finfo->info)
9546 ->tls_sec->vma);
9550 finfo->indices[r_symndx]
9551 = bfd_get_symcount (output_bfd);
9553 if (! elf_link_output_sym (finfo, name, &sym, sec,
9554 NULL))
9555 return FALSE;
9558 r_symndx = finfo->indices[r_symndx];
9561 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9562 | (irela->r_info & r_type_mask));
9565 /* Swap out the relocs. */
9566 if (input_rel_hdr->sh_size != 0
9567 && !bed->elf_backend_emit_relocs (output_bfd, o,
9568 input_rel_hdr,
9569 internal_relocs,
9570 rel_hash_list))
9571 return FALSE;
9573 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9574 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9576 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9577 * bed->s->int_rels_per_ext_rel);
9578 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9579 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9580 input_rel_hdr2,
9581 internal_relocs,
9582 rel_hash_list))
9583 return FALSE;
9588 /* Write out the modified section contents. */
9589 if (bed->elf_backend_write_section
9590 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9591 contents))
9593 /* Section written out. */
9595 else switch (o->sec_info_type)
9597 case ELF_INFO_TYPE_STABS:
9598 if (! (_bfd_write_section_stabs
9599 (output_bfd,
9600 &elf_hash_table (finfo->info)->stab_info,
9601 o, &elf_section_data (o)->sec_info, contents)))
9602 return FALSE;
9603 break;
9604 case ELF_INFO_TYPE_MERGE:
9605 if (! _bfd_write_merged_section (output_bfd, o,
9606 elf_section_data (o)->sec_info))
9607 return FALSE;
9608 break;
9609 case ELF_INFO_TYPE_EH_FRAME:
9611 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9612 o, contents))
9613 return FALSE;
9615 break;
9616 default:
9618 if (! (o->flags & SEC_EXCLUDE)
9619 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9620 && ! bfd_set_section_contents (output_bfd, o->output_section,
9621 contents,
9622 (file_ptr) o->output_offset,
9623 o->size))
9624 return FALSE;
9626 break;
9630 return TRUE;
9633 /* Generate a reloc when linking an ELF file. This is a reloc
9634 requested by the linker, and does not come from any input file. This
9635 is used to build constructor and destructor tables when linking
9636 with -Ur. */
9638 static bfd_boolean
9639 elf_reloc_link_order (bfd *output_bfd,
9640 struct bfd_link_info *info,
9641 asection *output_section,
9642 struct bfd_link_order *link_order)
9644 reloc_howto_type *howto;
9645 long indx;
9646 bfd_vma offset;
9647 bfd_vma addend;
9648 struct elf_link_hash_entry **rel_hash_ptr;
9649 Elf_Internal_Shdr *rel_hdr;
9650 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9651 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9652 bfd_byte *erel;
9653 unsigned int i;
9655 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9656 if (howto == NULL)
9658 bfd_set_error (bfd_error_bad_value);
9659 return FALSE;
9662 addend = link_order->u.reloc.p->addend;
9664 /* Figure out the symbol index. */
9665 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9666 + elf_section_data (output_section)->rel_count
9667 + elf_section_data (output_section)->rel_count2);
9668 if (link_order->type == bfd_section_reloc_link_order)
9670 indx = link_order->u.reloc.p->u.section->target_index;
9671 BFD_ASSERT (indx != 0);
9672 *rel_hash_ptr = NULL;
9674 else
9676 struct elf_link_hash_entry *h;
9678 /* Treat a reloc against a defined symbol as though it were
9679 actually against the section. */
9680 h = ((struct elf_link_hash_entry *)
9681 bfd_wrapped_link_hash_lookup (output_bfd, info,
9682 link_order->u.reloc.p->u.name,
9683 FALSE, FALSE, TRUE));
9684 if (h != NULL
9685 && (h->root.type == bfd_link_hash_defined
9686 || h->root.type == bfd_link_hash_defweak))
9688 asection *section;
9690 section = h->root.u.def.section;
9691 indx = section->output_section->target_index;
9692 *rel_hash_ptr = NULL;
9693 /* It seems that we ought to add the symbol value to the
9694 addend here, but in practice it has already been added
9695 because it was passed to constructor_callback. */
9696 addend += section->output_section->vma + section->output_offset;
9698 else if (h != NULL)
9700 /* Setting the index to -2 tells elf_link_output_extsym that
9701 this symbol is used by a reloc. */
9702 h->indx = -2;
9703 *rel_hash_ptr = h;
9704 indx = 0;
9706 else
9708 if (! ((*info->callbacks->unattached_reloc)
9709 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9710 return FALSE;
9711 indx = 0;
9715 /* If this is an inplace reloc, we must write the addend into the
9716 object file. */
9717 if (howto->partial_inplace && addend != 0)
9719 bfd_size_type size;
9720 bfd_reloc_status_type rstat;
9721 bfd_byte *buf;
9722 bfd_boolean ok;
9723 const char *sym_name;
9725 size = bfd_get_reloc_size (howto);
9726 buf = bfd_zmalloc (size);
9727 if (buf == NULL)
9728 return FALSE;
9729 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9730 switch (rstat)
9732 case bfd_reloc_ok:
9733 break;
9735 default:
9736 case bfd_reloc_outofrange:
9737 abort ();
9739 case bfd_reloc_overflow:
9740 if (link_order->type == bfd_section_reloc_link_order)
9741 sym_name = bfd_section_name (output_bfd,
9742 link_order->u.reloc.p->u.section);
9743 else
9744 sym_name = link_order->u.reloc.p->u.name;
9745 if (! ((*info->callbacks->reloc_overflow)
9746 (info, NULL, sym_name, howto->name, addend, NULL,
9747 NULL, (bfd_vma) 0)))
9749 free (buf);
9750 return FALSE;
9752 break;
9754 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9755 link_order->offset, size);
9756 free (buf);
9757 if (! ok)
9758 return FALSE;
9761 /* The address of a reloc is relative to the section in a
9762 relocatable file, and is a virtual address in an executable
9763 file. */
9764 offset = link_order->offset;
9765 if (! info->relocatable)
9766 offset += output_section->vma;
9768 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9770 irel[i].r_offset = offset;
9771 irel[i].r_info = 0;
9772 irel[i].r_addend = 0;
9774 if (bed->s->arch_size == 32)
9775 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9776 else
9777 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9779 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9780 erel = rel_hdr->contents;
9781 if (rel_hdr->sh_type == SHT_REL)
9783 erel += (elf_section_data (output_section)->rel_count
9784 * bed->s->sizeof_rel);
9785 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9787 else
9789 irel[0].r_addend = addend;
9790 erel += (elf_section_data (output_section)->rel_count
9791 * bed->s->sizeof_rela);
9792 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9795 ++elf_section_data (output_section)->rel_count;
9797 return TRUE;
9801 /* Get the output vma of the section pointed to by the sh_link field. */
9803 static bfd_vma
9804 elf_get_linked_section_vma (struct bfd_link_order *p)
9806 Elf_Internal_Shdr **elf_shdrp;
9807 asection *s;
9808 int elfsec;
9810 s = p->u.indirect.section;
9811 elf_shdrp = elf_elfsections (s->owner);
9812 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9813 elfsec = elf_shdrp[elfsec]->sh_link;
9814 /* PR 290:
9815 The Intel C compiler generates SHT_IA_64_UNWIND with
9816 SHF_LINK_ORDER. But it doesn't set the sh_link or
9817 sh_info fields. Hence we could get the situation
9818 where elfsec is 0. */
9819 if (elfsec == 0)
9821 const struct elf_backend_data *bed
9822 = get_elf_backend_data (s->owner);
9823 if (bed->link_order_error_handler)
9824 bed->link_order_error_handler
9825 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9826 return 0;
9828 else
9830 s = elf_shdrp[elfsec]->bfd_section;
9831 return s->output_section->vma + s->output_offset;
9836 /* Compare two sections based on the locations of the sections they are
9837 linked to. Used by elf_fixup_link_order. */
9839 static int
9840 compare_link_order (const void * a, const void * b)
9842 bfd_vma apos;
9843 bfd_vma bpos;
9845 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9846 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9847 if (apos < bpos)
9848 return -1;
9849 return apos > bpos;
9853 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9854 order as their linked sections. Returns false if this could not be done
9855 because an output section includes both ordered and unordered
9856 sections. Ideally we'd do this in the linker proper. */
9858 static bfd_boolean
9859 elf_fixup_link_order (bfd *abfd, asection *o)
9861 int seen_linkorder;
9862 int seen_other;
9863 int n;
9864 struct bfd_link_order *p;
9865 bfd *sub;
9866 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9867 unsigned elfsec;
9868 struct bfd_link_order **sections;
9869 asection *s, *other_sec, *linkorder_sec;
9870 bfd_vma offset;
9872 other_sec = NULL;
9873 linkorder_sec = NULL;
9874 seen_other = 0;
9875 seen_linkorder = 0;
9876 for (p = o->map_head.link_order; p != NULL; p = p->next)
9878 if (p->type == bfd_indirect_link_order)
9880 s = p->u.indirect.section;
9881 sub = s->owner;
9882 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9883 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9884 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9885 && elfsec < elf_numsections (sub)
9886 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9887 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9889 seen_linkorder++;
9890 linkorder_sec = s;
9892 else
9894 seen_other++;
9895 other_sec = s;
9898 else
9899 seen_other++;
9901 if (seen_other && seen_linkorder)
9903 if (other_sec && linkorder_sec)
9904 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9905 o, linkorder_sec,
9906 linkorder_sec->owner, other_sec,
9907 other_sec->owner);
9908 else
9909 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9911 bfd_set_error (bfd_error_bad_value);
9912 return FALSE;
9916 if (!seen_linkorder)
9917 return TRUE;
9919 sections = (struct bfd_link_order **)
9920 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9921 if (sections == NULL)
9922 return FALSE;
9923 seen_linkorder = 0;
9925 for (p = o->map_head.link_order; p != NULL; p = p->next)
9927 sections[seen_linkorder++] = p;
9929 /* Sort the input sections in the order of their linked section. */
9930 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9931 compare_link_order);
9933 /* Change the offsets of the sections. */
9934 offset = 0;
9935 for (n = 0; n < seen_linkorder; n++)
9937 s = sections[n]->u.indirect.section;
9938 offset &= ~(bfd_vma) 0 << s->alignment_power;
9939 s->output_offset = offset;
9940 sections[n]->offset = offset;
9941 offset += sections[n]->size;
9944 free (sections);
9945 return TRUE;
9949 /* Do the final step of an ELF link. */
9951 bfd_boolean
9952 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9954 bfd_boolean dynamic;
9955 bfd_boolean emit_relocs;
9956 bfd *dynobj;
9957 struct elf_final_link_info finfo;
9958 register asection *o;
9959 register struct bfd_link_order *p;
9960 register bfd *sub;
9961 bfd_size_type max_contents_size;
9962 bfd_size_type max_external_reloc_size;
9963 bfd_size_type max_internal_reloc_count;
9964 bfd_size_type max_sym_count;
9965 bfd_size_type max_sym_shndx_count;
9966 file_ptr off;
9967 Elf_Internal_Sym elfsym;
9968 unsigned int i;
9969 Elf_Internal_Shdr *symtab_hdr;
9970 Elf_Internal_Shdr *symtab_shndx_hdr;
9971 Elf_Internal_Shdr *symstrtab_hdr;
9972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9973 struct elf_outext_info eoinfo;
9974 bfd_boolean merged;
9975 size_t relativecount = 0;
9976 asection *reldyn = 0;
9977 bfd_size_type amt;
9978 asection *attr_section = NULL;
9979 bfd_vma attr_size = 0;
9980 const char *std_attrs_section;
9982 if (! is_elf_hash_table (info->hash))
9983 return FALSE;
9985 if (info->shared)
9986 abfd->flags |= DYNAMIC;
9988 dynamic = elf_hash_table (info)->dynamic_sections_created;
9989 dynobj = elf_hash_table (info)->dynobj;
9991 emit_relocs = (info->relocatable
9992 || info->emitrelocations);
9994 finfo.info = info;
9995 finfo.output_bfd = abfd;
9996 finfo.symstrtab = _bfd_elf_stringtab_init ();
9997 if (finfo.symstrtab == NULL)
9998 return FALSE;
10000 if (! dynamic)
10002 finfo.dynsym_sec = NULL;
10003 finfo.hash_sec = NULL;
10004 finfo.symver_sec = NULL;
10006 else
10008 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10009 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10010 BFD_ASSERT (finfo.dynsym_sec != NULL);
10011 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10012 /* Note that it is OK if symver_sec is NULL. */
10015 finfo.contents = NULL;
10016 finfo.external_relocs = NULL;
10017 finfo.internal_relocs = NULL;
10018 finfo.external_syms = NULL;
10019 finfo.locsym_shndx = NULL;
10020 finfo.internal_syms = NULL;
10021 finfo.indices = NULL;
10022 finfo.sections = NULL;
10023 finfo.symbuf = NULL;
10024 finfo.symshndxbuf = NULL;
10025 finfo.symbuf_count = 0;
10026 finfo.shndxbuf_size = 0;
10028 /* The object attributes have been merged. Remove the input
10029 sections from the link, and set the contents of the output
10030 secton. */
10031 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10032 for (o = abfd->sections; o != NULL; o = o->next)
10034 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10035 || strcmp (o->name, ".gnu.attributes") == 0)
10037 for (p = o->map_head.link_order; p != NULL; p = p->next)
10039 asection *input_section;
10041 if (p->type != bfd_indirect_link_order)
10042 continue;
10043 input_section = p->u.indirect.section;
10044 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10045 elf_link_input_bfd ignores this section. */
10046 input_section->flags &= ~SEC_HAS_CONTENTS;
10049 attr_size = bfd_elf_obj_attr_size (abfd);
10050 if (attr_size)
10052 bfd_set_section_size (abfd, o, attr_size);
10053 attr_section = o;
10054 /* Skip this section later on. */
10055 o->map_head.link_order = NULL;
10057 else
10058 o->flags |= SEC_EXCLUDE;
10062 /* Count up the number of relocations we will output for each output
10063 section, so that we know the sizes of the reloc sections. We
10064 also figure out some maximum sizes. */
10065 max_contents_size = 0;
10066 max_external_reloc_size = 0;
10067 max_internal_reloc_count = 0;
10068 max_sym_count = 0;
10069 max_sym_shndx_count = 0;
10070 merged = FALSE;
10071 for (o = abfd->sections; o != NULL; o = o->next)
10073 struct bfd_elf_section_data *esdo = elf_section_data (o);
10074 o->reloc_count = 0;
10076 for (p = o->map_head.link_order; p != NULL; p = p->next)
10078 unsigned int reloc_count = 0;
10079 struct bfd_elf_section_data *esdi = NULL;
10080 unsigned int *rel_count1;
10082 if (p->type == bfd_section_reloc_link_order
10083 || p->type == bfd_symbol_reloc_link_order)
10084 reloc_count = 1;
10085 else if (p->type == bfd_indirect_link_order)
10087 asection *sec;
10089 sec = p->u.indirect.section;
10090 esdi = elf_section_data (sec);
10092 /* Mark all sections which are to be included in the
10093 link. This will normally be every section. We need
10094 to do this so that we can identify any sections which
10095 the linker has decided to not include. */
10096 sec->linker_mark = TRUE;
10098 if (sec->flags & SEC_MERGE)
10099 merged = TRUE;
10101 if (info->relocatable || info->emitrelocations)
10102 reloc_count = sec->reloc_count;
10103 else if (bed->elf_backend_count_relocs)
10104 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10106 if (sec->rawsize > max_contents_size)
10107 max_contents_size = sec->rawsize;
10108 if (sec->size > max_contents_size)
10109 max_contents_size = sec->size;
10111 /* We are interested in just local symbols, not all
10112 symbols. */
10113 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10114 && (sec->owner->flags & DYNAMIC) == 0)
10116 size_t sym_count;
10118 if (elf_bad_symtab (sec->owner))
10119 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10120 / bed->s->sizeof_sym);
10121 else
10122 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10124 if (sym_count > max_sym_count)
10125 max_sym_count = sym_count;
10127 if (sym_count > max_sym_shndx_count
10128 && elf_symtab_shndx (sec->owner) != 0)
10129 max_sym_shndx_count = sym_count;
10131 if ((sec->flags & SEC_RELOC) != 0)
10133 size_t ext_size;
10135 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10136 if (ext_size > max_external_reloc_size)
10137 max_external_reloc_size = ext_size;
10138 if (sec->reloc_count > max_internal_reloc_count)
10139 max_internal_reloc_count = sec->reloc_count;
10144 if (reloc_count == 0)
10145 continue;
10147 o->reloc_count += reloc_count;
10149 /* MIPS may have a mix of REL and RELA relocs on sections.
10150 To support this curious ABI we keep reloc counts in
10151 elf_section_data too. We must be careful to add the
10152 relocations from the input section to the right output
10153 count. FIXME: Get rid of one count. We have
10154 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10155 rel_count1 = &esdo->rel_count;
10156 if (esdi != NULL)
10158 bfd_boolean same_size;
10159 bfd_size_type entsize1;
10161 entsize1 = esdi->rel_hdr.sh_entsize;
10162 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10163 || entsize1 == bed->s->sizeof_rela);
10164 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10166 if (!same_size)
10167 rel_count1 = &esdo->rel_count2;
10169 if (esdi->rel_hdr2 != NULL)
10171 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10172 unsigned int alt_count;
10173 unsigned int *rel_count2;
10175 BFD_ASSERT (entsize2 != entsize1
10176 && (entsize2 == bed->s->sizeof_rel
10177 || entsize2 == bed->s->sizeof_rela));
10179 rel_count2 = &esdo->rel_count2;
10180 if (!same_size)
10181 rel_count2 = &esdo->rel_count;
10183 /* The following is probably too simplistic if the
10184 backend counts output relocs unusually. */
10185 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10186 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10187 *rel_count2 += alt_count;
10188 reloc_count -= alt_count;
10191 *rel_count1 += reloc_count;
10194 if (o->reloc_count > 0)
10195 o->flags |= SEC_RELOC;
10196 else
10198 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10199 set it (this is probably a bug) and if it is set
10200 assign_section_numbers will create a reloc section. */
10201 o->flags &=~ SEC_RELOC;
10204 /* If the SEC_ALLOC flag is not set, force the section VMA to
10205 zero. This is done in elf_fake_sections as well, but forcing
10206 the VMA to 0 here will ensure that relocs against these
10207 sections are handled correctly. */
10208 if ((o->flags & SEC_ALLOC) == 0
10209 && ! o->user_set_vma)
10210 o->vma = 0;
10213 if (! info->relocatable && merged)
10214 elf_link_hash_traverse (elf_hash_table (info),
10215 _bfd_elf_link_sec_merge_syms, abfd);
10217 /* Figure out the file positions for everything but the symbol table
10218 and the relocs. We set symcount to force assign_section_numbers
10219 to create a symbol table. */
10220 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10221 BFD_ASSERT (! abfd->output_has_begun);
10222 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10223 goto error_return;
10225 /* Set sizes, and assign file positions for reloc sections. */
10226 for (o = abfd->sections; o != NULL; o = o->next)
10228 if ((o->flags & SEC_RELOC) != 0)
10230 if (!(_bfd_elf_link_size_reloc_section
10231 (abfd, &elf_section_data (o)->rel_hdr, o)))
10232 goto error_return;
10234 if (elf_section_data (o)->rel_hdr2
10235 && !(_bfd_elf_link_size_reloc_section
10236 (abfd, elf_section_data (o)->rel_hdr2, o)))
10237 goto error_return;
10240 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10241 to count upwards while actually outputting the relocations. */
10242 elf_section_data (o)->rel_count = 0;
10243 elf_section_data (o)->rel_count2 = 0;
10246 _bfd_elf_assign_file_positions_for_relocs (abfd);
10248 /* We have now assigned file positions for all the sections except
10249 .symtab and .strtab. We start the .symtab section at the current
10250 file position, and write directly to it. We build the .strtab
10251 section in memory. */
10252 bfd_get_symcount (abfd) = 0;
10253 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10254 /* sh_name is set in prep_headers. */
10255 symtab_hdr->sh_type = SHT_SYMTAB;
10256 /* sh_flags, sh_addr and sh_size all start off zero. */
10257 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10258 /* sh_link is set in assign_section_numbers. */
10259 /* sh_info is set below. */
10260 /* sh_offset is set just below. */
10261 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10263 off = elf_tdata (abfd)->next_file_pos;
10264 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10266 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10267 incorrect. We do not yet know the size of the .symtab section.
10268 We correct next_file_pos below, after we do know the size. */
10270 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10271 continuously seeking to the right position in the file. */
10272 if (! info->keep_memory || max_sym_count < 20)
10273 finfo.symbuf_size = 20;
10274 else
10275 finfo.symbuf_size = max_sym_count;
10276 amt = finfo.symbuf_size;
10277 amt *= bed->s->sizeof_sym;
10278 finfo.symbuf = bfd_malloc (amt);
10279 if (finfo.symbuf == NULL)
10280 goto error_return;
10281 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10283 /* Wild guess at number of output symbols. realloc'd as needed. */
10284 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10285 finfo.shndxbuf_size = amt;
10286 amt *= sizeof (Elf_External_Sym_Shndx);
10287 finfo.symshndxbuf = bfd_zmalloc (amt);
10288 if (finfo.symshndxbuf == NULL)
10289 goto error_return;
10292 /* Start writing out the symbol table. The first symbol is always a
10293 dummy symbol. */
10294 if (info->strip != strip_all
10295 || emit_relocs)
10297 elfsym.st_value = 0;
10298 elfsym.st_size = 0;
10299 elfsym.st_info = 0;
10300 elfsym.st_other = 0;
10301 elfsym.st_shndx = SHN_UNDEF;
10302 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10303 NULL))
10304 goto error_return;
10307 /* Output a symbol for each section. We output these even if we are
10308 discarding local symbols, since they are used for relocs. These
10309 symbols have no names. We store the index of each one in the
10310 index field of the section, so that we can find it again when
10311 outputting relocs. */
10312 if (info->strip != strip_all
10313 || emit_relocs)
10315 elfsym.st_size = 0;
10316 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10317 elfsym.st_other = 0;
10318 elfsym.st_value = 0;
10319 for (i = 1; i < elf_numsections (abfd); i++)
10321 o = bfd_section_from_elf_index (abfd, i);
10322 if (o != NULL)
10324 o->target_index = bfd_get_symcount (abfd);
10325 elfsym.st_shndx = i;
10326 if (!info->relocatable)
10327 elfsym.st_value = o->vma;
10328 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10329 goto error_return;
10334 /* Allocate some memory to hold information read in from the input
10335 files. */
10336 if (max_contents_size != 0)
10338 finfo.contents = bfd_malloc (max_contents_size);
10339 if (finfo.contents == NULL)
10340 goto error_return;
10343 if (max_external_reloc_size != 0)
10345 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10346 if (finfo.external_relocs == NULL)
10347 goto error_return;
10350 if (max_internal_reloc_count != 0)
10352 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10353 amt *= sizeof (Elf_Internal_Rela);
10354 finfo.internal_relocs = bfd_malloc (amt);
10355 if (finfo.internal_relocs == NULL)
10356 goto error_return;
10359 if (max_sym_count != 0)
10361 amt = max_sym_count * bed->s->sizeof_sym;
10362 finfo.external_syms = bfd_malloc (amt);
10363 if (finfo.external_syms == NULL)
10364 goto error_return;
10366 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10367 finfo.internal_syms = bfd_malloc (amt);
10368 if (finfo.internal_syms == NULL)
10369 goto error_return;
10371 amt = max_sym_count * sizeof (long);
10372 finfo.indices = bfd_malloc (amt);
10373 if (finfo.indices == NULL)
10374 goto error_return;
10376 amt = max_sym_count * sizeof (asection *);
10377 finfo.sections = bfd_malloc (amt);
10378 if (finfo.sections == NULL)
10379 goto error_return;
10382 if (max_sym_shndx_count != 0)
10384 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10385 finfo.locsym_shndx = bfd_malloc (amt);
10386 if (finfo.locsym_shndx == NULL)
10387 goto error_return;
10390 if (elf_hash_table (info)->tls_sec)
10392 bfd_vma base, end = 0;
10393 asection *sec;
10395 for (sec = elf_hash_table (info)->tls_sec;
10396 sec && (sec->flags & SEC_THREAD_LOCAL);
10397 sec = sec->next)
10399 bfd_size_type size = sec->size;
10401 if (size == 0
10402 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10404 struct bfd_link_order *o = sec->map_tail.link_order;
10405 if (o != NULL)
10406 size = o->offset + o->size;
10408 end = sec->vma + size;
10410 base = elf_hash_table (info)->tls_sec->vma;
10411 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10412 elf_hash_table (info)->tls_size = end - base;
10415 /* Reorder SHF_LINK_ORDER sections. */
10416 for (o = abfd->sections; o != NULL; o = o->next)
10418 if (!elf_fixup_link_order (abfd, o))
10419 return FALSE;
10422 /* Since ELF permits relocations to be against local symbols, we
10423 must have the local symbols available when we do the relocations.
10424 Since we would rather only read the local symbols once, and we
10425 would rather not keep them in memory, we handle all the
10426 relocations for a single input file at the same time.
10428 Unfortunately, there is no way to know the total number of local
10429 symbols until we have seen all of them, and the local symbol
10430 indices precede the global symbol indices. This means that when
10431 we are generating relocatable output, and we see a reloc against
10432 a global symbol, we can not know the symbol index until we have
10433 finished examining all the local symbols to see which ones we are
10434 going to output. To deal with this, we keep the relocations in
10435 memory, and don't output them until the end of the link. This is
10436 an unfortunate waste of memory, but I don't see a good way around
10437 it. Fortunately, it only happens when performing a relocatable
10438 link, which is not the common case. FIXME: If keep_memory is set
10439 we could write the relocs out and then read them again; I don't
10440 know how bad the memory loss will be. */
10442 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10443 sub->output_has_begun = FALSE;
10444 for (o = abfd->sections; o != NULL; o = o->next)
10446 for (p = o->map_head.link_order; p != NULL; p = p->next)
10448 if (p->type == bfd_indirect_link_order
10449 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10450 == bfd_target_elf_flavour)
10451 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10453 if (! sub->output_has_begun)
10455 if (! elf_link_input_bfd (&finfo, sub))
10456 goto error_return;
10457 sub->output_has_begun = TRUE;
10460 else if (p->type == bfd_section_reloc_link_order
10461 || p->type == bfd_symbol_reloc_link_order)
10463 if (! elf_reloc_link_order (abfd, info, o, p))
10464 goto error_return;
10466 else
10468 if (! _bfd_default_link_order (abfd, info, o, p))
10469 goto error_return;
10474 /* Free symbol buffer if needed. */
10475 if (!info->reduce_memory_overheads)
10477 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10478 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10479 && elf_tdata (sub)->symbuf)
10481 free (elf_tdata (sub)->symbuf);
10482 elf_tdata (sub)->symbuf = NULL;
10486 /* Output any global symbols that got converted to local in a
10487 version script or due to symbol visibility. We do this in a
10488 separate step since ELF requires all local symbols to appear
10489 prior to any global symbols. FIXME: We should only do this if
10490 some global symbols were, in fact, converted to become local.
10491 FIXME: Will this work correctly with the Irix 5 linker? */
10492 eoinfo.failed = FALSE;
10493 eoinfo.finfo = &finfo;
10494 eoinfo.localsyms = TRUE;
10495 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10496 &eoinfo);
10497 if (eoinfo.failed)
10498 return FALSE;
10500 /* If backend needs to output some local symbols not present in the hash
10501 table, do it now. */
10502 if (bed->elf_backend_output_arch_local_syms)
10504 typedef bfd_boolean (*out_sym_func)
10505 (void *, const char *, Elf_Internal_Sym *, asection *,
10506 struct elf_link_hash_entry *);
10508 if (! ((*bed->elf_backend_output_arch_local_syms)
10509 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10510 return FALSE;
10513 /* That wrote out all the local symbols. Finish up the symbol table
10514 with the global symbols. Even if we want to strip everything we
10515 can, we still need to deal with those global symbols that got
10516 converted to local in a version script. */
10518 /* The sh_info field records the index of the first non local symbol. */
10519 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10521 if (dynamic
10522 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10524 Elf_Internal_Sym sym;
10525 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10526 long last_local = 0;
10528 /* Write out the section symbols for the output sections. */
10529 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10531 asection *s;
10533 sym.st_size = 0;
10534 sym.st_name = 0;
10535 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10536 sym.st_other = 0;
10538 for (s = abfd->sections; s != NULL; s = s->next)
10540 int indx;
10541 bfd_byte *dest;
10542 long dynindx;
10544 dynindx = elf_section_data (s)->dynindx;
10545 if (dynindx <= 0)
10546 continue;
10547 indx = elf_section_data (s)->this_idx;
10548 BFD_ASSERT (indx > 0);
10549 sym.st_shndx = indx;
10550 if (! check_dynsym (abfd, &sym))
10551 return FALSE;
10552 sym.st_value = s->vma;
10553 dest = dynsym + dynindx * bed->s->sizeof_sym;
10554 if (last_local < dynindx)
10555 last_local = dynindx;
10556 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10560 /* Write out the local dynsyms. */
10561 if (elf_hash_table (info)->dynlocal)
10563 struct elf_link_local_dynamic_entry *e;
10564 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10566 asection *s;
10567 bfd_byte *dest;
10569 sym.st_size = e->isym.st_size;
10570 sym.st_other = e->isym.st_other;
10572 /* Copy the internal symbol as is.
10573 Note that we saved a word of storage and overwrote
10574 the original st_name with the dynstr_index. */
10575 sym = e->isym;
10577 s = bfd_section_from_elf_index (e->input_bfd,
10578 e->isym.st_shndx);
10579 if (s != NULL)
10581 sym.st_shndx =
10582 elf_section_data (s->output_section)->this_idx;
10583 if (! check_dynsym (abfd, &sym))
10584 return FALSE;
10585 sym.st_value = (s->output_section->vma
10586 + s->output_offset
10587 + e->isym.st_value);
10590 if (last_local < e->dynindx)
10591 last_local = e->dynindx;
10593 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10594 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10598 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10599 last_local + 1;
10602 /* We get the global symbols from the hash table. */
10603 eoinfo.failed = FALSE;
10604 eoinfo.localsyms = FALSE;
10605 eoinfo.finfo = &finfo;
10606 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10607 &eoinfo);
10608 if (eoinfo.failed)
10609 return FALSE;
10611 /* If backend needs to output some symbols not present in the hash
10612 table, do it now. */
10613 if (bed->elf_backend_output_arch_syms)
10615 typedef bfd_boolean (*out_sym_func)
10616 (void *, const char *, Elf_Internal_Sym *, asection *,
10617 struct elf_link_hash_entry *);
10619 if (! ((*bed->elf_backend_output_arch_syms)
10620 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10621 return FALSE;
10624 /* Flush all symbols to the file. */
10625 if (! elf_link_flush_output_syms (&finfo, bed))
10626 return FALSE;
10628 /* Now we know the size of the symtab section. */
10629 off += symtab_hdr->sh_size;
10631 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10632 if (symtab_shndx_hdr->sh_name != 0)
10634 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10635 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10636 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10637 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10638 symtab_shndx_hdr->sh_size = amt;
10640 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10641 off, TRUE);
10643 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10644 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10645 return FALSE;
10649 /* Finish up and write out the symbol string table (.strtab)
10650 section. */
10651 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10652 /* sh_name was set in prep_headers. */
10653 symstrtab_hdr->sh_type = SHT_STRTAB;
10654 symstrtab_hdr->sh_flags = 0;
10655 symstrtab_hdr->sh_addr = 0;
10656 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10657 symstrtab_hdr->sh_entsize = 0;
10658 symstrtab_hdr->sh_link = 0;
10659 symstrtab_hdr->sh_info = 0;
10660 /* sh_offset is set just below. */
10661 symstrtab_hdr->sh_addralign = 1;
10663 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10664 elf_tdata (abfd)->next_file_pos = off;
10666 if (bfd_get_symcount (abfd) > 0)
10668 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10669 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10670 return FALSE;
10673 /* Adjust the relocs to have the correct symbol indices. */
10674 for (o = abfd->sections; o != NULL; o = o->next)
10676 if ((o->flags & SEC_RELOC) == 0)
10677 continue;
10679 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10680 elf_section_data (o)->rel_count,
10681 elf_section_data (o)->rel_hashes);
10682 if (elf_section_data (o)->rel_hdr2 != NULL)
10683 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10684 elf_section_data (o)->rel_count2,
10685 (elf_section_data (o)->rel_hashes
10686 + elf_section_data (o)->rel_count));
10688 /* Set the reloc_count field to 0 to prevent write_relocs from
10689 trying to swap the relocs out itself. */
10690 o->reloc_count = 0;
10693 if (dynamic && info->combreloc && dynobj != NULL)
10694 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10696 /* If we are linking against a dynamic object, or generating a
10697 shared library, finish up the dynamic linking information. */
10698 if (dynamic)
10700 bfd_byte *dyncon, *dynconend;
10702 /* Fix up .dynamic entries. */
10703 o = bfd_get_section_by_name (dynobj, ".dynamic");
10704 BFD_ASSERT (o != NULL);
10706 dyncon = o->contents;
10707 dynconend = o->contents + o->size;
10708 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10710 Elf_Internal_Dyn dyn;
10711 const char *name;
10712 unsigned int type;
10714 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10716 switch (dyn.d_tag)
10718 default:
10719 continue;
10720 case DT_NULL:
10721 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10723 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10725 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10726 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10727 default: continue;
10729 dyn.d_un.d_val = relativecount;
10730 relativecount = 0;
10731 break;
10733 continue;
10735 case DT_INIT:
10736 name = info->init_function;
10737 goto get_sym;
10738 case DT_FINI:
10739 name = info->fini_function;
10740 get_sym:
10742 struct elf_link_hash_entry *h;
10744 h = elf_link_hash_lookup (elf_hash_table (info), name,
10745 FALSE, FALSE, TRUE);
10746 if (h != NULL
10747 && (h->root.type == bfd_link_hash_defined
10748 || h->root.type == bfd_link_hash_defweak))
10750 dyn.d_un.d_ptr = h->root.u.def.value;
10751 o = h->root.u.def.section;
10752 if (o->output_section != NULL)
10753 dyn.d_un.d_ptr += (o->output_section->vma
10754 + o->output_offset);
10755 else
10757 /* The symbol is imported from another shared
10758 library and does not apply to this one. */
10759 dyn.d_un.d_ptr = 0;
10761 break;
10764 continue;
10766 case DT_PREINIT_ARRAYSZ:
10767 name = ".preinit_array";
10768 goto get_size;
10769 case DT_INIT_ARRAYSZ:
10770 name = ".init_array";
10771 goto get_size;
10772 case DT_FINI_ARRAYSZ:
10773 name = ".fini_array";
10774 get_size:
10775 o = bfd_get_section_by_name (abfd, name);
10776 if (o == NULL)
10778 (*_bfd_error_handler)
10779 (_("%B: could not find output section %s"), abfd, name);
10780 goto error_return;
10782 if (o->size == 0)
10783 (*_bfd_error_handler)
10784 (_("warning: %s section has zero size"), name);
10785 dyn.d_un.d_val = o->size;
10786 break;
10788 case DT_PREINIT_ARRAY:
10789 name = ".preinit_array";
10790 goto get_vma;
10791 case DT_INIT_ARRAY:
10792 name = ".init_array";
10793 goto get_vma;
10794 case DT_FINI_ARRAY:
10795 name = ".fini_array";
10796 goto get_vma;
10798 case DT_HASH:
10799 name = ".hash";
10800 goto get_vma;
10801 case DT_GNU_HASH:
10802 name = ".gnu.hash";
10803 goto get_vma;
10804 case DT_STRTAB:
10805 name = ".dynstr";
10806 goto get_vma;
10807 case DT_SYMTAB:
10808 name = ".dynsym";
10809 goto get_vma;
10810 case DT_VERDEF:
10811 name = ".gnu.version_d";
10812 goto get_vma;
10813 case DT_VERNEED:
10814 name = ".gnu.version_r";
10815 goto get_vma;
10816 case DT_VERSYM:
10817 name = ".gnu.version";
10818 get_vma:
10819 o = bfd_get_section_by_name (abfd, name);
10820 if (o == NULL)
10822 (*_bfd_error_handler)
10823 (_("%B: could not find output section %s"), abfd, name);
10824 goto error_return;
10826 dyn.d_un.d_ptr = o->vma;
10827 break;
10829 case DT_REL:
10830 case DT_RELA:
10831 case DT_RELSZ:
10832 case DT_RELASZ:
10833 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10834 type = SHT_REL;
10835 else
10836 type = SHT_RELA;
10837 dyn.d_un.d_val = 0;
10838 dyn.d_un.d_ptr = 0;
10839 for (i = 1; i < elf_numsections (abfd); i++)
10841 Elf_Internal_Shdr *hdr;
10843 hdr = elf_elfsections (abfd)[i];
10844 if (hdr->sh_type == type
10845 && (hdr->sh_flags & SHF_ALLOC) != 0)
10847 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10848 dyn.d_un.d_val += hdr->sh_size;
10849 else
10851 if (dyn.d_un.d_ptr == 0
10852 || hdr->sh_addr < dyn.d_un.d_ptr)
10853 dyn.d_un.d_ptr = hdr->sh_addr;
10857 break;
10859 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10863 /* If we have created any dynamic sections, then output them. */
10864 if (dynobj != NULL)
10866 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10867 goto error_return;
10869 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10870 if (info->warn_shared_textrel && info->shared)
10872 bfd_byte *dyncon, *dynconend;
10874 /* Fix up .dynamic entries. */
10875 o = bfd_get_section_by_name (dynobj, ".dynamic");
10876 BFD_ASSERT (o != NULL);
10878 dyncon = o->contents;
10879 dynconend = o->contents + o->size;
10880 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10882 Elf_Internal_Dyn dyn;
10884 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10886 if (dyn.d_tag == DT_TEXTREL)
10888 info->callbacks->einfo
10889 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10890 break;
10895 for (o = dynobj->sections; o != NULL; o = o->next)
10897 if ((o->flags & SEC_HAS_CONTENTS) == 0
10898 || o->size == 0
10899 || o->output_section == bfd_abs_section_ptr)
10900 continue;
10901 if ((o->flags & SEC_LINKER_CREATED) == 0)
10903 /* At this point, we are only interested in sections
10904 created by _bfd_elf_link_create_dynamic_sections. */
10905 continue;
10907 if (elf_hash_table (info)->stab_info.stabstr == o)
10908 continue;
10909 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10910 continue;
10911 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10912 != SHT_STRTAB)
10913 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10915 if (! bfd_set_section_contents (abfd, o->output_section,
10916 o->contents,
10917 (file_ptr) o->output_offset,
10918 o->size))
10919 goto error_return;
10921 else
10923 /* The contents of the .dynstr section are actually in a
10924 stringtab. */
10925 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10926 if (bfd_seek (abfd, off, SEEK_SET) != 0
10927 || ! _bfd_elf_strtab_emit (abfd,
10928 elf_hash_table (info)->dynstr))
10929 goto error_return;
10934 if (info->relocatable)
10936 bfd_boolean failed = FALSE;
10938 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10939 if (failed)
10940 goto error_return;
10943 /* If we have optimized stabs strings, output them. */
10944 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10946 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10947 goto error_return;
10950 if (info->eh_frame_hdr)
10952 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10953 goto error_return;
10956 if (finfo.symstrtab != NULL)
10957 _bfd_stringtab_free (finfo.symstrtab);
10958 if (finfo.contents != NULL)
10959 free (finfo.contents);
10960 if (finfo.external_relocs != NULL)
10961 free (finfo.external_relocs);
10962 if (finfo.internal_relocs != NULL)
10963 free (finfo.internal_relocs);
10964 if (finfo.external_syms != NULL)
10965 free (finfo.external_syms);
10966 if (finfo.locsym_shndx != NULL)
10967 free (finfo.locsym_shndx);
10968 if (finfo.internal_syms != NULL)
10969 free (finfo.internal_syms);
10970 if (finfo.indices != NULL)
10971 free (finfo.indices);
10972 if (finfo.sections != NULL)
10973 free (finfo.sections);
10974 if (finfo.symbuf != NULL)
10975 free (finfo.symbuf);
10976 if (finfo.symshndxbuf != NULL)
10977 free (finfo.symshndxbuf);
10978 for (o = abfd->sections; o != NULL; o = o->next)
10980 if ((o->flags & SEC_RELOC) != 0
10981 && elf_section_data (o)->rel_hashes != NULL)
10982 free (elf_section_data (o)->rel_hashes);
10985 elf_tdata (abfd)->linker = TRUE;
10987 if (attr_section)
10989 bfd_byte *contents = bfd_malloc (attr_size);
10990 if (contents == NULL)
10991 return FALSE; /* Bail out and fail. */
10992 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10993 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10994 free (contents);
10997 return TRUE;
10999 error_return:
11000 if (finfo.symstrtab != NULL)
11001 _bfd_stringtab_free (finfo.symstrtab);
11002 if (finfo.contents != NULL)
11003 free (finfo.contents);
11004 if (finfo.external_relocs != NULL)
11005 free (finfo.external_relocs);
11006 if (finfo.internal_relocs != NULL)
11007 free (finfo.internal_relocs);
11008 if (finfo.external_syms != NULL)
11009 free (finfo.external_syms);
11010 if (finfo.locsym_shndx != NULL)
11011 free (finfo.locsym_shndx);
11012 if (finfo.internal_syms != NULL)
11013 free (finfo.internal_syms);
11014 if (finfo.indices != NULL)
11015 free (finfo.indices);
11016 if (finfo.sections != NULL)
11017 free (finfo.sections);
11018 if (finfo.symbuf != NULL)
11019 free (finfo.symbuf);
11020 if (finfo.symshndxbuf != NULL)
11021 free (finfo.symshndxbuf);
11022 for (o = abfd->sections; o != NULL; o = o->next)
11024 if ((o->flags & SEC_RELOC) != 0
11025 && elf_section_data (o)->rel_hashes != NULL)
11026 free (elf_section_data (o)->rel_hashes);
11029 return FALSE;
11032 /* Initialize COOKIE for input bfd ABFD. */
11034 static bfd_boolean
11035 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11036 struct bfd_link_info *info, bfd *abfd)
11038 Elf_Internal_Shdr *symtab_hdr;
11039 const struct elf_backend_data *bed;
11041 bed = get_elf_backend_data (abfd);
11042 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11044 cookie->abfd = abfd;
11045 cookie->sym_hashes = elf_sym_hashes (abfd);
11046 cookie->bad_symtab = elf_bad_symtab (abfd);
11047 if (cookie->bad_symtab)
11049 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11050 cookie->extsymoff = 0;
11052 else
11054 cookie->locsymcount = symtab_hdr->sh_info;
11055 cookie->extsymoff = symtab_hdr->sh_info;
11058 if (bed->s->arch_size == 32)
11059 cookie->r_sym_shift = 8;
11060 else
11061 cookie->r_sym_shift = 32;
11063 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11064 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11066 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11067 cookie->locsymcount, 0,
11068 NULL, NULL, NULL);
11069 if (cookie->locsyms == NULL)
11071 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11072 return FALSE;
11074 if (info->keep_memory)
11075 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11077 return TRUE;
11080 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11082 static void
11083 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11085 Elf_Internal_Shdr *symtab_hdr;
11087 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11088 if (cookie->locsyms != NULL
11089 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11090 free (cookie->locsyms);
11093 /* Initialize the relocation information in COOKIE for input section SEC
11094 of input bfd ABFD. */
11096 static bfd_boolean
11097 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11098 struct bfd_link_info *info, bfd *abfd,
11099 asection *sec)
11101 const struct elf_backend_data *bed;
11103 if (sec->reloc_count == 0)
11105 cookie->rels = NULL;
11106 cookie->relend = NULL;
11108 else
11110 bed = get_elf_backend_data (abfd);
11112 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11113 info->keep_memory);
11114 if (cookie->rels == NULL)
11115 return FALSE;
11116 cookie->rel = cookie->rels;
11117 cookie->relend = (cookie->rels
11118 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11120 cookie->rel = cookie->rels;
11121 return TRUE;
11124 /* Free the memory allocated by init_reloc_cookie_rels,
11125 if appropriate. */
11127 static void
11128 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11129 asection *sec)
11131 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11132 free (cookie->rels);
11135 /* Initialize the whole of COOKIE for input section SEC. */
11137 static bfd_boolean
11138 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11139 struct bfd_link_info *info,
11140 asection *sec)
11142 if (!init_reloc_cookie (cookie, info, sec->owner))
11143 goto error1;
11144 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11145 goto error2;
11146 return TRUE;
11148 error2:
11149 fini_reloc_cookie (cookie, sec->owner);
11150 error1:
11151 return FALSE;
11154 /* Free the memory allocated by init_reloc_cookie_for_section,
11155 if appropriate. */
11157 static void
11158 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11159 asection *sec)
11161 fini_reloc_cookie_rels (cookie, sec);
11162 fini_reloc_cookie (cookie, sec->owner);
11165 /* Garbage collect unused sections. */
11167 /* Default gc_mark_hook. */
11169 asection *
11170 _bfd_elf_gc_mark_hook (asection *sec,
11171 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11172 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11173 struct elf_link_hash_entry *h,
11174 Elf_Internal_Sym *sym)
11176 if (h != NULL)
11178 switch (h->root.type)
11180 case bfd_link_hash_defined:
11181 case bfd_link_hash_defweak:
11182 return h->root.u.def.section;
11184 case bfd_link_hash_common:
11185 return h->root.u.c.p->section;
11187 default:
11188 break;
11191 else
11192 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11194 return NULL;
11197 /* COOKIE->rel describes a relocation against section SEC, which is
11198 a section we've decided to keep. Return the section that contains
11199 the relocation symbol, or NULL if no section contains it. */
11201 asection *
11202 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11203 elf_gc_mark_hook_fn gc_mark_hook,
11204 struct elf_reloc_cookie *cookie)
11206 unsigned long r_symndx;
11207 struct elf_link_hash_entry *h;
11209 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11210 if (r_symndx == 0)
11211 return NULL;
11213 if (r_symndx >= cookie->locsymcount
11214 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11216 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11217 while (h->root.type == bfd_link_hash_indirect
11218 || h->root.type == bfd_link_hash_warning)
11219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11220 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11223 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11224 &cookie->locsyms[r_symndx]);
11227 /* COOKIE->rel describes a relocation against section SEC, which is
11228 a section we've decided to keep. Mark the section that contains
11229 the relocation symbol. */
11231 bfd_boolean
11232 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11233 asection *sec,
11234 elf_gc_mark_hook_fn gc_mark_hook,
11235 struct elf_reloc_cookie *cookie)
11237 asection *rsec;
11239 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11240 if (rsec && !rsec->gc_mark)
11242 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11243 rsec->gc_mark = 1;
11244 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11245 return FALSE;
11247 return TRUE;
11250 /* The mark phase of garbage collection. For a given section, mark
11251 it and any sections in this section's group, and all the sections
11252 which define symbols to which it refers. */
11254 bfd_boolean
11255 _bfd_elf_gc_mark (struct bfd_link_info *info,
11256 asection *sec,
11257 elf_gc_mark_hook_fn gc_mark_hook)
11259 bfd_boolean ret;
11260 asection *group_sec, *eh_frame;
11262 sec->gc_mark = 1;
11264 /* Mark all the sections in the group. */
11265 group_sec = elf_section_data (sec)->next_in_group;
11266 if (group_sec && !group_sec->gc_mark)
11267 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11268 return FALSE;
11270 /* Look through the section relocs. */
11271 ret = TRUE;
11272 eh_frame = elf_eh_frame_section (sec->owner);
11273 if ((sec->flags & SEC_RELOC) != 0
11274 && sec->reloc_count > 0
11275 && sec != eh_frame)
11277 struct elf_reloc_cookie cookie;
11279 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11280 ret = FALSE;
11281 else
11283 for (; cookie.rel < cookie.relend; cookie.rel++)
11284 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11286 ret = FALSE;
11287 break;
11289 fini_reloc_cookie_for_section (&cookie, sec);
11293 if (ret && eh_frame && elf_fde_list (sec))
11295 struct elf_reloc_cookie cookie;
11297 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11298 ret = FALSE;
11299 else
11301 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11302 gc_mark_hook, &cookie))
11303 ret = FALSE;
11304 fini_reloc_cookie_for_section (&cookie, eh_frame);
11308 return ret;
11311 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11313 struct elf_gc_sweep_symbol_info
11315 struct bfd_link_info *info;
11316 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11317 bfd_boolean);
11320 static bfd_boolean
11321 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11323 if (h->root.type == bfd_link_hash_warning)
11324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11326 if ((h->root.type == bfd_link_hash_defined
11327 || h->root.type == bfd_link_hash_defweak)
11328 && !h->root.u.def.section->gc_mark
11329 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11331 struct elf_gc_sweep_symbol_info *inf = data;
11332 (*inf->hide_symbol) (inf->info, h, TRUE);
11335 return TRUE;
11338 /* The sweep phase of garbage collection. Remove all garbage sections. */
11340 typedef bfd_boolean (*gc_sweep_hook_fn)
11341 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11343 static bfd_boolean
11344 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11346 bfd *sub;
11347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11348 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11349 unsigned long section_sym_count;
11350 struct elf_gc_sweep_symbol_info sweep_info;
11352 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11354 asection *o;
11356 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11357 continue;
11359 for (o = sub->sections; o != NULL; o = o->next)
11361 /* Keep debug and special sections. */
11362 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11363 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11364 o->gc_mark = 1;
11366 if (o->gc_mark)
11367 continue;
11369 /* Skip sweeping sections already excluded. */
11370 if (o->flags & SEC_EXCLUDE)
11371 continue;
11373 /* Since this is early in the link process, it is simple
11374 to remove a section from the output. */
11375 o->flags |= SEC_EXCLUDE;
11377 if (info->print_gc_sections && o->size != 0)
11378 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11380 /* But we also have to update some of the relocation
11381 info we collected before. */
11382 if (gc_sweep_hook
11383 && (o->flags & SEC_RELOC) != 0
11384 && o->reloc_count > 0
11385 && !bfd_is_abs_section (o->output_section))
11387 Elf_Internal_Rela *internal_relocs;
11388 bfd_boolean r;
11390 internal_relocs
11391 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11392 info->keep_memory);
11393 if (internal_relocs == NULL)
11394 return FALSE;
11396 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11398 if (elf_section_data (o)->relocs != internal_relocs)
11399 free (internal_relocs);
11401 if (!r)
11402 return FALSE;
11407 /* Remove the symbols that were in the swept sections from the dynamic
11408 symbol table. GCFIXME: Anyone know how to get them out of the
11409 static symbol table as well? */
11410 sweep_info.info = info;
11411 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11412 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11413 &sweep_info);
11415 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11416 return TRUE;
11419 /* Propagate collected vtable information. This is called through
11420 elf_link_hash_traverse. */
11422 static bfd_boolean
11423 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11425 if (h->root.type == bfd_link_hash_warning)
11426 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11428 /* Those that are not vtables. */
11429 if (h->vtable == NULL || h->vtable->parent == NULL)
11430 return TRUE;
11432 /* Those vtables that do not have parents, we cannot merge. */
11433 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11434 return TRUE;
11436 /* If we've already been done, exit. */
11437 if (h->vtable->used && h->vtable->used[-1])
11438 return TRUE;
11440 /* Make sure the parent's table is up to date. */
11441 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11443 if (h->vtable->used == NULL)
11445 /* None of this table's entries were referenced. Re-use the
11446 parent's table. */
11447 h->vtable->used = h->vtable->parent->vtable->used;
11448 h->vtable->size = h->vtable->parent->vtable->size;
11450 else
11452 size_t n;
11453 bfd_boolean *cu, *pu;
11455 /* Or the parent's entries into ours. */
11456 cu = h->vtable->used;
11457 cu[-1] = TRUE;
11458 pu = h->vtable->parent->vtable->used;
11459 if (pu != NULL)
11461 const struct elf_backend_data *bed;
11462 unsigned int log_file_align;
11464 bed = get_elf_backend_data (h->root.u.def.section->owner);
11465 log_file_align = bed->s->log_file_align;
11466 n = h->vtable->parent->vtable->size >> log_file_align;
11467 while (n--)
11469 if (*pu)
11470 *cu = TRUE;
11471 pu++;
11472 cu++;
11477 return TRUE;
11480 static bfd_boolean
11481 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11483 asection *sec;
11484 bfd_vma hstart, hend;
11485 Elf_Internal_Rela *relstart, *relend, *rel;
11486 const struct elf_backend_data *bed;
11487 unsigned int log_file_align;
11489 if (h->root.type == bfd_link_hash_warning)
11490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11492 /* Take care of both those symbols that do not describe vtables as
11493 well as those that are not loaded. */
11494 if (h->vtable == NULL || h->vtable->parent == NULL)
11495 return TRUE;
11497 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11498 || h->root.type == bfd_link_hash_defweak);
11500 sec = h->root.u.def.section;
11501 hstart = h->root.u.def.value;
11502 hend = hstart + h->size;
11504 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11505 if (!relstart)
11506 return *(bfd_boolean *) okp = FALSE;
11507 bed = get_elf_backend_data (sec->owner);
11508 log_file_align = bed->s->log_file_align;
11510 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11512 for (rel = relstart; rel < relend; ++rel)
11513 if (rel->r_offset >= hstart && rel->r_offset < hend)
11515 /* If the entry is in use, do nothing. */
11516 if (h->vtable->used
11517 && (rel->r_offset - hstart) < h->vtable->size)
11519 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11520 if (h->vtable->used[entry])
11521 continue;
11523 /* Otherwise, kill it. */
11524 rel->r_offset = rel->r_info = rel->r_addend = 0;
11527 return TRUE;
11530 /* Mark sections containing dynamically referenced symbols. When
11531 building shared libraries, we must assume that any visible symbol is
11532 referenced. */
11534 bfd_boolean
11535 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11537 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11539 if (h->root.type == bfd_link_hash_warning)
11540 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11542 if ((h->root.type == bfd_link_hash_defined
11543 || h->root.type == bfd_link_hash_defweak)
11544 && (h->ref_dynamic
11545 || (!info->executable
11546 && h->def_regular
11547 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11548 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11549 h->root.u.def.section->flags |= SEC_KEEP;
11551 return TRUE;
11554 /* Keep all sections containing symbols undefined on the command-line,
11555 and the section containing the entry symbol. */
11557 void
11558 _bfd_elf_gc_keep (struct bfd_link_info *info)
11560 struct bfd_sym_chain *sym;
11562 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11564 struct elf_link_hash_entry *h;
11566 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11567 FALSE, FALSE, FALSE);
11569 if (h != NULL
11570 && (h->root.type == bfd_link_hash_defined
11571 || h->root.type == bfd_link_hash_defweak)
11572 && !bfd_is_abs_section (h->root.u.def.section))
11573 h->root.u.def.section->flags |= SEC_KEEP;
11577 /* Do mark and sweep of unused sections. */
11579 bfd_boolean
11580 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11582 bfd_boolean ok = TRUE;
11583 bfd *sub;
11584 elf_gc_mark_hook_fn gc_mark_hook;
11585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11587 if (!bed->can_gc_sections
11588 || !is_elf_hash_table (info->hash))
11590 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11591 return TRUE;
11594 bed->gc_keep (info);
11596 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11597 at the .eh_frame section if we can mark the FDEs individually. */
11598 _bfd_elf_begin_eh_frame_parsing (info);
11599 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11601 asection *sec;
11602 struct elf_reloc_cookie cookie;
11604 sec = bfd_get_section_by_name (sub, ".eh_frame");
11605 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11607 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11608 if (elf_section_data (sec)->sec_info)
11609 elf_eh_frame_section (sub) = sec;
11610 fini_reloc_cookie_for_section (&cookie, sec);
11613 _bfd_elf_end_eh_frame_parsing (info);
11615 /* Apply transitive closure to the vtable entry usage info. */
11616 elf_link_hash_traverse (elf_hash_table (info),
11617 elf_gc_propagate_vtable_entries_used,
11618 &ok);
11619 if (!ok)
11620 return FALSE;
11622 /* Kill the vtable relocations that were not used. */
11623 elf_link_hash_traverse (elf_hash_table (info),
11624 elf_gc_smash_unused_vtentry_relocs,
11625 &ok);
11626 if (!ok)
11627 return FALSE;
11629 /* Mark dynamically referenced symbols. */
11630 if (elf_hash_table (info)->dynamic_sections_created)
11631 elf_link_hash_traverse (elf_hash_table (info),
11632 bed->gc_mark_dynamic_ref,
11633 info);
11635 /* Grovel through relocs to find out who stays ... */
11636 gc_mark_hook = bed->gc_mark_hook;
11637 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11639 asection *o;
11641 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11642 continue;
11644 for (o = sub->sections; o != NULL; o = o->next)
11645 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11646 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11647 return FALSE;
11650 /* Allow the backend to mark additional target specific sections. */
11651 if (bed->gc_mark_extra_sections)
11652 bed->gc_mark_extra_sections (info, gc_mark_hook);
11654 /* ... and mark SEC_EXCLUDE for those that go. */
11655 return elf_gc_sweep (abfd, info);
11658 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11660 bfd_boolean
11661 bfd_elf_gc_record_vtinherit (bfd *abfd,
11662 asection *sec,
11663 struct elf_link_hash_entry *h,
11664 bfd_vma offset)
11666 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11667 struct elf_link_hash_entry **search, *child;
11668 bfd_size_type extsymcount;
11669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11671 /* The sh_info field of the symtab header tells us where the
11672 external symbols start. We don't care about the local symbols at
11673 this point. */
11674 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11675 if (!elf_bad_symtab (abfd))
11676 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11678 sym_hashes = elf_sym_hashes (abfd);
11679 sym_hashes_end = sym_hashes + extsymcount;
11681 /* Hunt down the child symbol, which is in this section at the same
11682 offset as the relocation. */
11683 for (search = sym_hashes; search != sym_hashes_end; ++search)
11685 if ((child = *search) != NULL
11686 && (child->root.type == bfd_link_hash_defined
11687 || child->root.type == bfd_link_hash_defweak)
11688 && child->root.u.def.section == sec
11689 && child->root.u.def.value == offset)
11690 goto win;
11693 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11694 abfd, sec, (unsigned long) offset);
11695 bfd_set_error (bfd_error_invalid_operation);
11696 return FALSE;
11698 win:
11699 if (!child->vtable)
11701 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11702 if (!child->vtable)
11703 return FALSE;
11705 if (!h)
11707 /* This *should* only be the absolute section. It could potentially
11708 be that someone has defined a non-global vtable though, which
11709 would be bad. It isn't worth paging in the local symbols to be
11710 sure though; that case should simply be handled by the assembler. */
11712 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11714 else
11715 child->vtable->parent = h;
11717 return TRUE;
11720 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11722 bfd_boolean
11723 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11724 asection *sec ATTRIBUTE_UNUSED,
11725 struct elf_link_hash_entry *h,
11726 bfd_vma addend)
11728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11729 unsigned int log_file_align = bed->s->log_file_align;
11731 if (!h->vtable)
11733 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11734 if (!h->vtable)
11735 return FALSE;
11738 if (addend >= h->vtable->size)
11740 size_t size, bytes, file_align;
11741 bfd_boolean *ptr = h->vtable->used;
11743 /* While the symbol is undefined, we have to be prepared to handle
11744 a zero size. */
11745 file_align = 1 << log_file_align;
11746 if (h->root.type == bfd_link_hash_undefined)
11747 size = addend + file_align;
11748 else
11750 size = h->size;
11751 if (addend >= size)
11753 /* Oops! We've got a reference past the defined end of
11754 the table. This is probably a bug -- shall we warn? */
11755 size = addend + file_align;
11758 size = (size + file_align - 1) & -file_align;
11760 /* Allocate one extra entry for use as a "done" flag for the
11761 consolidation pass. */
11762 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11764 if (ptr)
11766 ptr = bfd_realloc (ptr - 1, bytes);
11768 if (ptr != NULL)
11770 size_t oldbytes;
11772 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11773 * sizeof (bfd_boolean));
11774 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11777 else
11778 ptr = bfd_zmalloc (bytes);
11780 if (ptr == NULL)
11781 return FALSE;
11783 /* And arrange for that done flag to be at index -1. */
11784 h->vtable->used = ptr + 1;
11785 h->vtable->size = size;
11788 h->vtable->used[addend >> log_file_align] = TRUE;
11790 return TRUE;
11793 struct alloc_got_off_arg {
11794 bfd_vma gotoff;
11795 struct bfd_link_info *info;
11798 /* We need a special top-level link routine to convert got reference counts
11799 to real got offsets. */
11801 static bfd_boolean
11802 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11804 struct alloc_got_off_arg *gofarg = arg;
11805 bfd *obfd = gofarg->info->output_bfd;
11806 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11808 if (h->root.type == bfd_link_hash_warning)
11809 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11811 if (h->got.refcount > 0)
11813 h->got.offset = gofarg->gotoff;
11814 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
11816 else
11817 h->got.offset = (bfd_vma) -1;
11819 return TRUE;
11822 /* And an accompanying bit to work out final got entry offsets once
11823 we're done. Should be called from final_link. */
11825 bfd_boolean
11826 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11827 struct bfd_link_info *info)
11829 bfd *i;
11830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11831 bfd_vma gotoff;
11832 struct alloc_got_off_arg gofarg;
11834 BFD_ASSERT (abfd == info->output_bfd);
11836 if (! is_elf_hash_table (info->hash))
11837 return FALSE;
11839 /* The GOT offset is relative to the .got section, but the GOT header is
11840 put into the .got.plt section, if the backend uses it. */
11841 if (bed->want_got_plt)
11842 gotoff = 0;
11843 else
11844 gotoff = bed->got_header_size;
11846 /* Do the local .got entries first. */
11847 for (i = info->input_bfds; i; i = i->link_next)
11849 bfd_signed_vma *local_got;
11850 bfd_size_type j, locsymcount;
11851 Elf_Internal_Shdr *symtab_hdr;
11853 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11854 continue;
11856 local_got = elf_local_got_refcounts (i);
11857 if (!local_got)
11858 continue;
11860 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11861 if (elf_bad_symtab (i))
11862 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11863 else
11864 locsymcount = symtab_hdr->sh_info;
11866 for (j = 0; j < locsymcount; ++j)
11868 if (local_got[j] > 0)
11870 local_got[j] = gotoff;
11871 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
11873 else
11874 local_got[j] = (bfd_vma) -1;
11878 /* Then the global .got entries. .plt refcounts are handled by
11879 adjust_dynamic_symbol */
11880 gofarg.gotoff = gotoff;
11881 gofarg.info = info;
11882 elf_link_hash_traverse (elf_hash_table (info),
11883 elf_gc_allocate_got_offsets,
11884 &gofarg);
11885 return TRUE;
11888 /* Many folk need no more in the way of final link than this, once
11889 got entry reference counting is enabled. */
11891 bfd_boolean
11892 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11894 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11895 return FALSE;
11897 /* Invoke the regular ELF backend linker to do all the work. */
11898 return bfd_elf_final_link (abfd, info);
11901 bfd_boolean
11902 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11904 struct elf_reloc_cookie *rcookie = cookie;
11906 if (rcookie->bad_symtab)
11907 rcookie->rel = rcookie->rels;
11909 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11911 unsigned long r_symndx;
11913 if (! rcookie->bad_symtab)
11914 if (rcookie->rel->r_offset > offset)
11915 return FALSE;
11916 if (rcookie->rel->r_offset != offset)
11917 continue;
11919 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11920 if (r_symndx == SHN_UNDEF)
11921 return TRUE;
11923 if (r_symndx >= rcookie->locsymcount
11924 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11926 struct elf_link_hash_entry *h;
11928 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11930 while (h->root.type == bfd_link_hash_indirect
11931 || h->root.type == bfd_link_hash_warning)
11932 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11934 if ((h->root.type == bfd_link_hash_defined
11935 || h->root.type == bfd_link_hash_defweak)
11936 && elf_discarded_section (h->root.u.def.section))
11937 return TRUE;
11938 else
11939 return FALSE;
11941 else
11943 /* It's not a relocation against a global symbol,
11944 but it could be a relocation against a local
11945 symbol for a discarded section. */
11946 asection *isec;
11947 Elf_Internal_Sym *isym;
11949 /* Need to: get the symbol; get the section. */
11950 isym = &rcookie->locsyms[r_symndx];
11951 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11952 if (isec != NULL && elf_discarded_section (isec))
11953 return TRUE;
11955 return FALSE;
11957 return FALSE;
11960 /* Discard unneeded references to discarded sections.
11961 Returns TRUE if any section's size was changed. */
11962 /* This function assumes that the relocations are in sorted order,
11963 which is true for all known assemblers. */
11965 bfd_boolean
11966 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11968 struct elf_reloc_cookie cookie;
11969 asection *stab, *eh;
11970 const struct elf_backend_data *bed;
11971 bfd *abfd;
11972 bfd_boolean ret = FALSE;
11974 if (info->traditional_format
11975 || !is_elf_hash_table (info->hash))
11976 return FALSE;
11978 _bfd_elf_begin_eh_frame_parsing (info);
11979 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11981 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11982 continue;
11984 bed = get_elf_backend_data (abfd);
11986 if ((abfd->flags & DYNAMIC) != 0)
11987 continue;
11989 eh = NULL;
11990 if (!info->relocatable)
11992 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11993 if (eh != NULL
11994 && (eh->size == 0
11995 || bfd_is_abs_section (eh->output_section)))
11996 eh = NULL;
11999 stab = bfd_get_section_by_name (abfd, ".stab");
12000 if (stab != NULL
12001 && (stab->size == 0
12002 || bfd_is_abs_section (stab->output_section)
12003 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12004 stab = NULL;
12006 if (stab == NULL
12007 && eh == NULL
12008 && bed->elf_backend_discard_info == NULL)
12009 continue;
12011 if (!init_reloc_cookie (&cookie, info, abfd))
12012 return FALSE;
12014 if (stab != NULL
12015 && stab->reloc_count > 0
12016 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12018 if (_bfd_discard_section_stabs (abfd, stab,
12019 elf_section_data (stab)->sec_info,
12020 bfd_elf_reloc_symbol_deleted_p,
12021 &cookie))
12022 ret = TRUE;
12023 fini_reloc_cookie_rels (&cookie, stab);
12026 if (eh != NULL
12027 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12029 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12030 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12031 bfd_elf_reloc_symbol_deleted_p,
12032 &cookie))
12033 ret = TRUE;
12034 fini_reloc_cookie_rels (&cookie, eh);
12037 if (bed->elf_backend_discard_info != NULL
12038 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12039 ret = TRUE;
12041 fini_reloc_cookie (&cookie, abfd);
12043 _bfd_elf_end_eh_frame_parsing (info);
12045 if (info->eh_frame_hdr
12046 && !info->relocatable
12047 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12048 ret = TRUE;
12050 return ret;
12053 /* For a SHT_GROUP section, return the group signature. For other
12054 sections, return the normal section name. */
12056 static const char *
12057 section_signature (asection *sec)
12059 if ((sec->flags & SEC_GROUP) != 0
12060 && elf_next_in_group (sec) != NULL
12061 && elf_group_name (elf_next_in_group (sec)) != NULL)
12062 return elf_group_name (elf_next_in_group (sec));
12063 return sec->name;
12066 void
12067 _bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12068 struct bfd_link_info *info)
12070 flagword flags;
12071 const char *name, *p;
12072 struct bfd_section_already_linked *l;
12073 struct bfd_section_already_linked_hash_entry *already_linked_list;
12075 if (sec->output_section == bfd_abs_section_ptr)
12076 return;
12078 flags = sec->flags;
12080 /* Return if it isn't a linkonce section. A comdat group section
12081 also has SEC_LINK_ONCE set. */
12082 if ((flags & SEC_LINK_ONCE) == 0)
12083 return;
12085 /* Don't put group member sections on our list of already linked
12086 sections. They are handled as a group via their group section. */
12087 if (elf_sec_group (sec) != NULL)
12088 return;
12090 /* FIXME: When doing a relocatable link, we may have trouble
12091 copying relocations in other sections that refer to local symbols
12092 in the section being discarded. Those relocations will have to
12093 be converted somehow; as of this writing I'm not sure that any of
12094 the backends handle that correctly.
12096 It is tempting to instead not discard link once sections when
12097 doing a relocatable link (technically, they should be discarded
12098 whenever we are building constructors). However, that fails,
12099 because the linker winds up combining all the link once sections
12100 into a single large link once section, which defeats the purpose
12101 of having link once sections in the first place.
12103 Also, not merging link once sections in a relocatable link
12104 causes trouble for MIPS ELF, which relies on link once semantics
12105 to handle the .reginfo section correctly. */
12107 name = section_signature (sec);
12109 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12110 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12111 p++;
12112 else
12113 p = name;
12115 already_linked_list = bfd_section_already_linked_table_lookup (p);
12117 for (l = already_linked_list->entry; l != NULL; l = l->next)
12119 /* We may have 2 different types of sections on the list: group
12120 sections and linkonce sections. Match like sections. */
12121 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12122 && strcmp (name, section_signature (l->sec)) == 0
12123 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12125 /* The section has already been linked. See if we should
12126 issue a warning. */
12127 switch (flags & SEC_LINK_DUPLICATES)
12129 default:
12130 abort ();
12132 case SEC_LINK_DUPLICATES_DISCARD:
12133 break;
12135 case SEC_LINK_DUPLICATES_ONE_ONLY:
12136 (*_bfd_error_handler)
12137 (_("%B: ignoring duplicate section `%A'"),
12138 abfd, sec);
12139 break;
12141 case SEC_LINK_DUPLICATES_SAME_SIZE:
12142 if (sec->size != l->sec->size)
12143 (*_bfd_error_handler)
12144 (_("%B: duplicate section `%A' has different size"),
12145 abfd, sec);
12146 break;
12148 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12149 if (sec->size != l->sec->size)
12150 (*_bfd_error_handler)
12151 (_("%B: duplicate section `%A' has different size"),
12152 abfd, sec);
12153 else if (sec->size != 0)
12155 bfd_byte *sec_contents, *l_sec_contents;
12157 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12158 (*_bfd_error_handler)
12159 (_("%B: warning: could not read contents of section `%A'"),
12160 abfd, sec);
12161 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12162 &l_sec_contents))
12163 (*_bfd_error_handler)
12164 (_("%B: warning: could not read contents of section `%A'"),
12165 l->sec->owner, l->sec);
12166 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12167 (*_bfd_error_handler)
12168 (_("%B: warning: duplicate section `%A' has different contents"),
12169 abfd, sec);
12171 if (sec_contents)
12172 free (sec_contents);
12173 if (l_sec_contents)
12174 free (l_sec_contents);
12176 break;
12179 /* Set the output_section field so that lang_add_section
12180 does not create a lang_input_section structure for this
12181 section. Since there might be a symbol in the section
12182 being discarded, we must retain a pointer to the section
12183 which we are really going to use. */
12184 sec->output_section = bfd_abs_section_ptr;
12185 sec->kept_section = l->sec;
12187 if (flags & SEC_GROUP)
12189 asection *first = elf_next_in_group (sec);
12190 asection *s = first;
12192 while (s != NULL)
12194 s->output_section = bfd_abs_section_ptr;
12195 /* Record which group discards it. */
12196 s->kept_section = l->sec;
12197 s = elf_next_in_group (s);
12198 /* These lists are circular. */
12199 if (s == first)
12200 break;
12204 return;
12208 /* A single member comdat group section may be discarded by a
12209 linkonce section and vice versa. */
12211 if ((flags & SEC_GROUP) != 0)
12213 asection *first = elf_next_in_group (sec);
12215 if (first != NULL && elf_next_in_group (first) == first)
12216 /* Check this single member group against linkonce sections. */
12217 for (l = already_linked_list->entry; l != NULL; l = l->next)
12218 if ((l->sec->flags & SEC_GROUP) == 0
12219 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12220 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12222 first->output_section = bfd_abs_section_ptr;
12223 first->kept_section = l->sec;
12224 sec->output_section = bfd_abs_section_ptr;
12225 break;
12228 else
12229 /* Check this linkonce section against single member groups. */
12230 for (l = already_linked_list->entry; l != NULL; l = l->next)
12231 if (l->sec->flags & SEC_GROUP)
12233 asection *first = elf_next_in_group (l->sec);
12235 if (first != NULL
12236 && elf_next_in_group (first) == first
12237 && bfd_elf_match_symbols_in_sections (first, sec, info))
12239 sec->output_section = bfd_abs_section_ptr;
12240 sec->kept_section = first;
12241 break;
12245 /* This is the first section with this name. Record it. */
12246 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12247 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12250 bfd_boolean
12251 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12253 return sym->st_shndx == SHN_COMMON;
12256 unsigned int
12257 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12259 return SHN_COMMON;
12262 asection *
12263 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12265 return bfd_com_section_ptr;
12268 bfd_vma
12269 _bfd_elf_default_got_elt_size (bfd *abfd,
12270 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12271 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12272 bfd *ibfd ATTRIBUTE_UNUSED,
12273 unsigned long symndx ATTRIBUTE_UNUSED)
12275 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12276 return bed->s->arch_size / 8;
12279 /* Routines to support the creation of dynamic relocs. */
12281 /* Return true if NAME is a name of a relocation
12282 section associated with section S. */
12284 static bfd_boolean
12285 is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12287 if (rela)
12288 return CONST_STRNEQ (name, ".rela")
12289 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12291 return CONST_STRNEQ (name, ".rel")
12292 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12295 /* Returns the name of the dynamic reloc section associated with SEC. */
12297 static const char *
12298 get_dynamic_reloc_section_name (bfd * abfd,
12299 asection * sec,
12300 bfd_boolean is_rela)
12302 const char * name;
12303 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12304 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12306 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12307 if (name == NULL)
12308 return NULL;
12310 if (! is_reloc_section (is_rela, name, sec))
12312 static bfd_boolean complained = FALSE;
12314 if (! complained)
12316 (*_bfd_error_handler)
12317 (_("%B: bad relocation section name `%s\'"), abfd, name);
12318 complained = TRUE;
12320 name = NULL;
12323 return name;
12326 /* Returns the dynamic reloc section associated with SEC.
12327 If necessary compute the name of the dynamic reloc section based
12328 on SEC's name (looked up in ABFD's string table) and the setting
12329 of IS_RELA. */
12331 asection *
12332 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12333 asection * sec,
12334 bfd_boolean is_rela)
12336 asection * reloc_sec = elf_section_data (sec)->sreloc;
12338 if (reloc_sec == NULL)
12340 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12342 if (name != NULL)
12344 reloc_sec = bfd_get_section_by_name (abfd, name);
12346 if (reloc_sec != NULL)
12347 elf_section_data (sec)->sreloc = reloc_sec;
12351 return reloc_sec;
12354 /* Returns the dynamic reloc section associated with SEC. If the
12355 section does not exist it is created and attached to the DYNOBJ
12356 bfd and stored in the SRELOC field of SEC's elf_section_data
12357 structure.
12359 ALIGNMENT is the alignment for the newly created section and
12360 IS_RELA defines whether the name should be .rela.<SEC's name>
12361 or .rel.<SEC's name>. The section name is looked up in the
12362 string table associated with ABFD. */
12364 asection *
12365 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12366 bfd * dynobj,
12367 unsigned int alignment,
12368 bfd * abfd,
12369 bfd_boolean is_rela)
12371 asection * reloc_sec = elf_section_data (sec)->sreloc;
12373 if (reloc_sec == NULL)
12375 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12377 if (name == NULL)
12378 return NULL;
12380 reloc_sec = bfd_get_section_by_name (dynobj, name);
12382 if (reloc_sec == NULL)
12384 flagword flags;
12386 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12387 if ((sec->flags & SEC_ALLOC) != 0)
12388 flags |= SEC_ALLOC | SEC_LOAD;
12390 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12391 if (reloc_sec != NULL)
12393 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12394 reloc_sec = NULL;
12398 elf_section_data (sec)->sreloc = reloc_sec;
12401 return reloc_sec;