Fix obvious typo in comment.
[binutils.git] / bfd / elflink.c
blob5c5fb2def8ff2813610ecedda03e938df2df4d01
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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 2 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, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 bfd_boolean
31 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
33 flagword flags;
34 asection *s;
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 int ptralign;
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 return TRUE;
45 switch (bed->s->arch_size)
47 case 32:
48 ptralign = 2;
49 break;
51 case 64:
52 ptralign = 3;
53 break;
55 default:
56 bfd_set_error (bfd_error_bad_value);
57 return FALSE;
60 flags = bed->dynamic_sec_flags;
62 s = bfd_make_section_with_flags (abfd, ".got", flags);
63 if (s == NULL
64 || !bfd_set_section_alignment (abfd, s, ptralign))
65 return FALSE;
67 if (bed->want_got_plt)
69 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
70 if (s == NULL
71 || !bfd_set_section_alignment (abfd, s, ptralign))
72 return FALSE;
75 if (bed->want_got_sym)
77 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
78 (or .got.plt) section. We don't do this in the linker script
79 because we don't want to define the symbol if we are not creating
80 a global offset table. */
81 bh = NULL;
82 if (!(_bfd_generic_link_add_one_symbol
83 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
84 0, NULL, FALSE, bed->collect, &bh)))
85 return FALSE;
86 h = (struct elf_link_hash_entry *) bh;
87 h->def_regular = 1;
88 h->type = STT_OBJECT;
89 h->other = STV_HIDDEN;
91 if (! info->executable
92 && ! bfd_elf_link_record_dynamic_symbol (info, h))
93 return FALSE;
95 elf_hash_table (info)->hgot = h;
98 /* The first bit of the global offset table is the header. */
99 s->size += bed->got_header_size;
101 return TRUE;
104 /* Create a strtab to hold the dynamic symbol names. */
105 static bfd_boolean
106 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
108 struct elf_link_hash_table *hash_table;
110 hash_table = elf_hash_table (info);
111 if (hash_table->dynobj == NULL)
112 hash_table->dynobj = abfd;
114 if (hash_table->dynstr == NULL)
116 hash_table->dynstr = _bfd_elf_strtab_init ();
117 if (hash_table->dynstr == NULL)
118 return FALSE;
120 return TRUE;
123 /* Create some sections which will be filled in with dynamic linking
124 information. ABFD is an input file which requires dynamic sections
125 to be created. The dynamic sections take up virtual memory space
126 when the final executable is run, so we need to create them before
127 addresses are assigned to the output sections. We work out the
128 actual contents and size of these sections later. */
130 bfd_boolean
131 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
133 flagword flags;
134 register asection *s;
135 struct elf_link_hash_entry *h;
136 struct bfd_link_hash_entry *bh;
137 const struct elf_backend_data *bed;
139 if (! is_elf_hash_table (info->hash))
140 return FALSE;
142 if (elf_hash_table (info)->dynamic_sections_created)
143 return TRUE;
145 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
146 return FALSE;
148 abfd = elf_hash_table (info)->dynobj;
149 bed = get_elf_backend_data (abfd);
151 flags = bed->dynamic_sec_flags;
153 /* A dynamically linked executable has a .interp section, but a
154 shared library does not. */
155 if (info->executable)
157 s = bfd_make_section_with_flags (abfd, ".interp",
158 flags | SEC_READONLY);
159 if (s == NULL)
160 return FALSE;
163 if (! info->traditional_format)
165 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
166 flags | SEC_READONLY);
167 if (s == NULL
168 || ! bfd_set_section_alignment (abfd, s, 2))
169 return FALSE;
170 elf_hash_table (info)->eh_info.hdr_sec = s;
173 /* Create sections to hold version informations. These are removed
174 if they are not needed. */
175 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
176 flags | SEC_READONLY);
177 if (s == NULL
178 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
179 return FALSE;
181 s = bfd_make_section_with_flags (abfd, ".gnu.version",
182 flags | SEC_READONLY);
183 if (s == NULL
184 || ! bfd_set_section_alignment (abfd, s, 1))
185 return FALSE;
187 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
188 flags | SEC_READONLY);
189 if (s == NULL
190 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
191 return FALSE;
193 s = bfd_make_section_with_flags (abfd, ".dynsym",
194 flags | SEC_READONLY);
195 if (s == NULL
196 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
197 return FALSE;
199 s = bfd_make_section_with_flags (abfd, ".dynstr",
200 flags | SEC_READONLY);
201 if (s == NULL)
202 return FALSE;
204 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
205 if (s == NULL
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
209 /* The special symbol _DYNAMIC is always set to the start of the
210 .dynamic section. We could set _DYNAMIC in a linker script, but we
211 only want to define it if we are, in fact, creating a .dynamic
212 section. We don't want to define it if there is no .dynamic
213 section, since on some ELF platforms the start up code examines it
214 to decide how to initialize the process. */
215 h = elf_link_hash_lookup (elf_hash_table (info), "_DYNAMIC",
216 FALSE, FALSE, FALSE);
217 if (h != NULL)
219 /* Zap symbol defined in an as-needed lib that wasn't linked.
220 This is a symptom of a larger problem: Absolute symbols
221 defined in shared libraries can't be overridden, because we
222 lose the link to the bfd which is via the symbol section. */
223 h->root.type = bfd_link_hash_new;
225 bh = &h->root;
226 if (! (_bfd_generic_link_add_one_symbol
227 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
228 get_elf_backend_data (abfd)->collect, &bh)))
229 return FALSE;
230 h = (struct elf_link_hash_entry *) bh;
231 h->def_regular = 1;
232 h->type = STT_OBJECT;
234 if (! info->executable
235 && ! bfd_elf_link_record_dynamic_symbol (info, h))
236 return FALSE;
238 s = bfd_make_section_with_flags (abfd, ".hash",
239 flags | SEC_READONLY);
240 if (s == NULL
241 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
242 return FALSE;
243 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
245 /* Let the backend create the rest of the sections. This lets the
246 backend set the right flags. The backend will normally create
247 the .got and .plt sections. */
248 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
249 return FALSE;
251 elf_hash_table (info)->dynamic_sections_created = TRUE;
253 return TRUE;
256 /* Create dynamic sections when linking against a dynamic object. */
258 bfd_boolean
259 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
261 flagword flags, pltflags;
262 asection *s;
263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
265 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
266 .rel[a].bss sections. */
267 flags = bed->dynamic_sec_flags;
269 pltflags = flags;
270 if (bed->plt_not_loaded)
271 /* We do not clear SEC_ALLOC here because we still want the OS to
272 allocate space for the section; it's just that there's nothing
273 to read in from the object file. */
274 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
275 else
276 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
277 if (bed->plt_readonly)
278 pltflags |= SEC_READONLY;
280 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
283 return FALSE;
285 if (bed->want_plt_sym)
287 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
288 .plt section. */
289 struct elf_link_hash_entry *h;
290 struct bfd_link_hash_entry *bh = NULL;
292 if (! (_bfd_generic_link_add_one_symbol
293 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
294 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
295 return FALSE;
296 h = (struct elf_link_hash_entry *) bh;
297 h->def_regular = 1;
298 h->type = STT_OBJECT;
300 if (! info->executable
301 && ! bfd_elf_link_record_dynamic_symbol (info, h))
302 return FALSE;
305 s = bfd_make_section_with_flags (abfd,
306 (bed->default_use_rela_p
307 ? ".rela.plt" : ".rel.plt"),
308 flags | SEC_READONLY);
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
313 if (! _bfd_elf_create_got_section (abfd, info))
314 return FALSE;
316 if (bed->want_dynbss)
318 /* The .dynbss section is a place to put symbols which are defined
319 by dynamic objects, are referenced by regular objects, and are
320 not functions. We must allocate space for them in the process
321 image and use a R_*_COPY reloc to tell the dynamic linker to
322 initialize them at run time. The linker script puts the .dynbss
323 section into the .bss section of the final image. */
324 s = bfd_make_section_with_flags (abfd, ".dynbss",
325 (SEC_ALLOC
326 | SEC_LINKER_CREATED));
327 if (s == NULL)
328 return FALSE;
330 /* The .rel[a].bss section holds copy relocs. This section is not
331 normally needed. We need to create it here, though, so that the
332 linker will map it to an output section. We can't just create it
333 only if we need it, because we will not know whether we need it
334 until we have seen all the input files, and the first time the
335 main linker code calls BFD after examining all the input files
336 (size_dynamic_sections) the input sections have already been
337 mapped to the output sections. If the section turns out not to
338 be needed, we can discard it later. We will never need this
339 section when generating a shared object, since they do not use
340 copy relocs. */
341 if (! info->shared)
343 s = bfd_make_section_with_flags (abfd,
344 (bed->default_use_rela_p
345 ? ".rela.bss" : ".rel.bss"),
346 flags | SEC_READONLY);
347 if (s == NULL
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 return FALSE;
353 return TRUE;
356 /* Record a new dynamic symbol. We record the dynamic symbols as we
357 read the input files, since we need to have a list of all of them
358 before we can determine the final sizes of the output sections.
359 Note that we may actually call this function even though we are not
360 going to output any dynamic symbols; in some cases we know that a
361 symbol should be in the dynamic symbol table, but only if there is
362 one. */
364 bfd_boolean
365 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
366 struct elf_link_hash_entry *h)
368 if (h->dynindx == -1)
370 struct elf_strtab_hash *dynstr;
371 char *p;
372 const char *name;
373 bfd_size_type indx;
375 /* XXX: The ABI draft says the linker must turn hidden and
376 internal symbols into STB_LOCAL symbols when producing the
377 DSO. However, if ld.so honors st_other in the dynamic table,
378 this would not be necessary. */
379 switch (ELF_ST_VISIBILITY (h->other))
381 case STV_INTERNAL:
382 case STV_HIDDEN:
383 if (h->root.type != bfd_link_hash_undefined
384 && h->root.type != bfd_link_hash_undefweak)
386 h->forced_local = 1;
387 if (!elf_hash_table (info)->is_relocatable_executable)
388 return TRUE;
391 default:
392 break;
395 h->dynindx = elf_hash_table (info)->dynsymcount;
396 ++elf_hash_table (info)->dynsymcount;
398 dynstr = elf_hash_table (info)->dynstr;
399 if (dynstr == NULL)
401 /* Create a strtab to hold the dynamic symbol names. */
402 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
403 if (dynstr == NULL)
404 return FALSE;
407 /* We don't put any version information in the dynamic string
408 table. */
409 name = h->root.root.string;
410 p = strchr (name, ELF_VER_CHR);
411 if (p != NULL)
412 /* We know that the p points into writable memory. In fact,
413 there are only a few symbols that have read-only names, being
414 those like _GLOBAL_OFFSET_TABLE_ that are created specially
415 by the backends. Most symbols will have names pointing into
416 an ELF string table read from a file, or to objalloc memory. */
417 *p = 0;
419 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
421 if (p != NULL)
422 *p = ELF_VER_CHR;
424 if (indx == (bfd_size_type) -1)
425 return FALSE;
426 h->dynstr_index = indx;
429 return TRUE;
432 /* Record an assignment to a symbol made by a linker script. We need
433 this in case some dynamic object refers to this symbol. */
435 bfd_boolean
436 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
437 struct bfd_link_info *info,
438 const char *name,
439 bfd_boolean provide)
441 struct elf_link_hash_entry *h;
442 struct elf_link_hash_table *htab;
444 if (!is_elf_hash_table (info->hash))
445 return TRUE;
447 htab = elf_hash_table (info);
448 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
449 if (h == NULL)
450 return provide;
452 /* Since we're defining the symbol, don't let it seem to have not
453 been defined. record_dynamic_symbol and size_dynamic_sections
454 may depend on this. */
455 if (h->root.type == bfd_link_hash_undefweak
456 || h->root.type == bfd_link_hash_undefined)
458 h->root.type = bfd_link_hash_new;
459 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
460 bfd_link_repair_undef_list (&htab->root);
463 if (h->root.type == bfd_link_hash_new)
464 h->non_elf = 0;
466 /* If this symbol is being provided by the linker script, and it is
467 currently defined by a dynamic object, but not by a regular
468 object, then mark it as undefined so that the generic linker will
469 force the correct value. */
470 if (provide
471 && h->def_dynamic
472 && !h->def_regular)
473 h->root.type = bfd_link_hash_undefined;
475 /* If this symbol is not being provided by the linker script, and it is
476 currently defined by a dynamic object, but not by a regular object,
477 then clear out any version information because the symbol will not be
478 associated with the dynamic object any more. */
479 if (!provide
480 && h->def_dynamic
481 && !h->def_regular)
482 h->verinfo.verdef = NULL;
484 h->def_regular = 1;
486 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
487 and executables. */
488 if (!info->relocatable
489 && h->dynindx != -1
490 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
491 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
492 h->forced_local = 1;
494 if ((h->def_dynamic
495 || h->ref_dynamic
496 || info->shared
497 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
498 && h->dynindx == -1)
500 if (! bfd_elf_link_record_dynamic_symbol (info, h))
501 return FALSE;
503 /* If this is a weak defined symbol, and we know a corresponding
504 real symbol from the same dynamic object, make sure the real
505 symbol is also made into a dynamic symbol. */
506 if (h->u.weakdef != NULL
507 && h->u.weakdef->dynindx == -1)
509 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
510 return FALSE;
514 return TRUE;
517 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
518 success, and 2 on a failure caused by attempting to record a symbol
519 in a discarded section, eg. a discarded link-once section symbol. */
522 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
523 bfd *input_bfd,
524 long input_indx)
526 bfd_size_type amt;
527 struct elf_link_local_dynamic_entry *entry;
528 struct elf_link_hash_table *eht;
529 struct elf_strtab_hash *dynstr;
530 unsigned long dynstr_index;
531 char *name;
532 Elf_External_Sym_Shndx eshndx;
533 char esym[sizeof (Elf64_External_Sym)];
535 if (! is_elf_hash_table (info->hash))
536 return 0;
538 /* See if the entry exists already. */
539 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
540 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
541 return 1;
543 amt = sizeof (*entry);
544 entry = bfd_alloc (input_bfd, amt);
545 if (entry == NULL)
546 return 0;
548 /* Go find the symbol, so that we can find it's name. */
549 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
550 1, input_indx, &entry->isym, esym, &eshndx))
552 bfd_release (input_bfd, entry);
553 return 0;
556 if (entry->isym.st_shndx != SHN_UNDEF
557 && (entry->isym.st_shndx < SHN_LORESERVE
558 || entry->isym.st_shndx > SHN_HIRESERVE))
560 asection *s;
562 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
563 if (s == NULL || bfd_is_abs_section (s->output_section))
565 /* We can still bfd_release here as nothing has done another
566 bfd_alloc. We can't do this later in this function. */
567 bfd_release (input_bfd, entry);
568 return 2;
572 name = (bfd_elf_string_from_elf_section
573 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
574 entry->isym.st_name));
576 dynstr = elf_hash_table (info)->dynstr;
577 if (dynstr == NULL)
579 /* Create a strtab to hold the dynamic symbol names. */
580 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
581 if (dynstr == NULL)
582 return 0;
585 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
586 if (dynstr_index == (unsigned long) -1)
587 return 0;
588 entry->isym.st_name = dynstr_index;
590 eht = elf_hash_table (info);
592 entry->next = eht->dynlocal;
593 eht->dynlocal = entry;
594 entry->input_bfd = input_bfd;
595 entry->input_indx = input_indx;
596 eht->dynsymcount++;
598 /* Whatever binding the symbol had before, it's now local. */
599 entry->isym.st_info
600 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
602 /* The dynindx will be set at the end of size_dynamic_sections. */
604 return 1;
607 /* Return the dynindex of a local dynamic symbol. */
609 long
610 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
611 bfd *input_bfd,
612 long input_indx)
614 struct elf_link_local_dynamic_entry *e;
616 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
617 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
618 return e->dynindx;
619 return -1;
622 /* This function is used to renumber the dynamic symbols, if some of
623 them are removed because they are marked as local. This is called
624 via elf_link_hash_traverse. */
626 static bfd_boolean
627 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
628 void *data)
630 size_t *count = data;
632 if (h->root.type == bfd_link_hash_warning)
633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
635 if (h->forced_local)
636 return TRUE;
638 if (h->dynindx != -1)
639 h->dynindx = ++(*count);
641 return TRUE;
645 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
646 STB_LOCAL binding. */
648 static bfd_boolean
649 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
650 void *data)
652 size_t *count = data;
654 if (h->root.type == bfd_link_hash_warning)
655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
657 if (!h->forced_local)
658 return TRUE;
660 if (h->dynindx != -1)
661 h->dynindx = ++(*count);
663 return TRUE;
666 /* Return true if the dynamic symbol for a given section should be
667 omitted when creating a shared library. */
668 bfd_boolean
669 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
670 struct bfd_link_info *info,
671 asection *p)
673 switch (elf_section_data (p)->this_hdr.sh_type)
675 case SHT_PROGBITS:
676 case SHT_NOBITS:
677 /* If sh_type is yet undecided, assume it could be
678 SHT_PROGBITS/SHT_NOBITS. */
679 case SHT_NULL:
680 if (strcmp (p->name, ".got") == 0
681 || strcmp (p->name, ".got.plt") == 0
682 || strcmp (p->name, ".plt") == 0)
684 asection *ip;
685 bfd *dynobj = elf_hash_table (info)->dynobj;
687 if (dynobj != NULL
688 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
689 && (ip->flags & SEC_LINKER_CREATED)
690 && ip->output_section == p)
691 return TRUE;
693 return FALSE;
695 /* There shouldn't be section relative relocations
696 against any other section. */
697 default:
698 return TRUE;
702 /* Assign dynsym indices. In a shared library we generate a section
703 symbol for each output section, which come first. Next come symbols
704 which have been forced to local binding. Then all of the back-end
705 allocated local dynamic syms, followed by the rest of the global
706 symbols. */
708 static unsigned long
709 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
710 struct bfd_link_info *info,
711 unsigned long *section_sym_count)
713 unsigned long dynsymcount = 0;
715 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
717 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
718 asection *p;
719 for (p = output_bfd->sections; p ; p = p->next)
720 if ((p->flags & SEC_EXCLUDE) == 0
721 && (p->flags & SEC_ALLOC) != 0
722 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
723 elf_section_data (p)->dynindx = ++dynsymcount;
725 *section_sym_count = dynsymcount;
727 elf_link_hash_traverse (elf_hash_table (info),
728 elf_link_renumber_local_hash_table_dynsyms,
729 &dynsymcount);
731 if (elf_hash_table (info)->dynlocal)
733 struct elf_link_local_dynamic_entry *p;
734 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
735 p->dynindx = ++dynsymcount;
738 elf_link_hash_traverse (elf_hash_table (info),
739 elf_link_renumber_hash_table_dynsyms,
740 &dynsymcount);
742 /* There is an unused NULL entry at the head of the table which
743 we must account for in our count. Unless there weren't any
744 symbols, which means we'll have no table at all. */
745 if (dynsymcount != 0)
746 ++dynsymcount;
748 return elf_hash_table (info)->dynsymcount = dynsymcount;
751 /* This function is called when we want to define a new symbol. It
752 handles the various cases which arise when we find a definition in
753 a dynamic object, or when there is already a definition in a
754 dynamic object. The new symbol is described by NAME, SYM, PSEC,
755 and PVALUE. We set SYM_HASH to the hash table entry. We set
756 OVERRIDE if the old symbol is overriding a new definition. We set
757 TYPE_CHANGE_OK if it is OK for the type to change. We set
758 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
759 change, we mean that we shouldn't warn if the type or size does
760 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
761 object is overridden by a regular object. */
763 bfd_boolean
764 _bfd_elf_merge_symbol (bfd *abfd,
765 struct bfd_link_info *info,
766 const char *name,
767 Elf_Internal_Sym *sym,
768 asection **psec,
769 bfd_vma *pvalue,
770 unsigned int *pold_alignment,
771 struct elf_link_hash_entry **sym_hash,
772 bfd_boolean *skip,
773 bfd_boolean *override,
774 bfd_boolean *type_change_ok,
775 bfd_boolean *size_change_ok)
777 asection *sec, *oldsec;
778 struct elf_link_hash_entry *h;
779 struct elf_link_hash_entry *flip;
780 int bind;
781 bfd *oldbfd;
782 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
783 bfd_boolean newweak, oldweak;
785 *skip = FALSE;
786 *override = FALSE;
788 sec = *psec;
789 bind = ELF_ST_BIND (sym->st_info);
791 if (! bfd_is_und_section (sec))
792 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
793 else
794 h = ((struct elf_link_hash_entry *)
795 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
796 if (h == NULL)
797 return FALSE;
798 *sym_hash = h;
800 /* This code is for coping with dynamic objects, and is only useful
801 if we are doing an ELF link. */
802 if (info->hash->creator != abfd->xvec)
803 return TRUE;
805 /* For merging, we only care about real symbols. */
807 while (h->root.type == bfd_link_hash_indirect
808 || h->root.type == bfd_link_hash_warning)
809 h = (struct elf_link_hash_entry *) h->root.u.i.link;
811 /* If we just created the symbol, mark it as being an ELF symbol.
812 Other than that, there is nothing to do--there is no merge issue
813 with a newly defined symbol--so we just return. */
815 if (h->root.type == bfd_link_hash_new)
817 h->non_elf = 0;
818 return TRUE;
821 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
822 existing symbol. */
824 switch (h->root.type)
826 default:
827 oldbfd = NULL;
828 oldsec = NULL;
829 break;
831 case bfd_link_hash_undefined:
832 case bfd_link_hash_undefweak:
833 oldbfd = h->root.u.undef.abfd;
834 oldsec = NULL;
835 break;
837 case bfd_link_hash_defined:
838 case bfd_link_hash_defweak:
839 oldbfd = h->root.u.def.section->owner;
840 oldsec = h->root.u.def.section;
841 break;
843 case bfd_link_hash_common:
844 oldbfd = h->root.u.c.p->section->owner;
845 oldsec = h->root.u.c.p->section;
846 break;
849 /* In cases involving weak versioned symbols, we may wind up trying
850 to merge a symbol with itself. Catch that here, to avoid the
851 confusion that results if we try to override a symbol with
852 itself. The additional tests catch cases like
853 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
854 dynamic object, which we do want to handle here. */
855 if (abfd == oldbfd
856 && ((abfd->flags & DYNAMIC) == 0
857 || !h->def_regular))
858 return TRUE;
860 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
861 respectively, is from a dynamic object. */
863 if ((abfd->flags & DYNAMIC) != 0)
864 newdyn = TRUE;
865 else
866 newdyn = FALSE;
868 if (oldbfd != NULL)
869 olddyn = (oldbfd->flags & DYNAMIC) != 0;
870 else
872 asection *hsec;
874 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
875 indices used by MIPS ELF. */
876 switch (h->root.type)
878 default:
879 hsec = NULL;
880 break;
882 case bfd_link_hash_defined:
883 case bfd_link_hash_defweak:
884 hsec = h->root.u.def.section;
885 break;
887 case bfd_link_hash_common:
888 hsec = h->root.u.c.p->section;
889 break;
892 if (hsec == NULL)
893 olddyn = FALSE;
894 else
895 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
898 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
899 respectively, appear to be a definition rather than reference. */
901 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
902 newdef = FALSE;
903 else
904 newdef = TRUE;
906 if (h->root.type == bfd_link_hash_undefined
907 || h->root.type == bfd_link_hash_undefweak
908 || h->root.type == bfd_link_hash_common)
909 olddef = FALSE;
910 else
911 olddef = TRUE;
913 /* Check TLS symbol. */
914 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
915 && ELF_ST_TYPE (sym->st_info) != h->type)
917 bfd *ntbfd, *tbfd;
918 bfd_boolean ntdef, tdef;
919 asection *ntsec, *tsec;
921 if (h->type == STT_TLS)
923 ntbfd = abfd;
924 ntsec = sec;
925 ntdef = newdef;
926 tbfd = oldbfd;
927 tsec = oldsec;
928 tdef = olddef;
930 else
932 ntbfd = oldbfd;
933 ntsec = oldsec;
934 ntdef = olddef;
935 tbfd = abfd;
936 tsec = sec;
937 tdef = newdef;
940 if (tdef && ntdef)
941 (*_bfd_error_handler)
942 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
943 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
944 else if (!tdef && !ntdef)
945 (*_bfd_error_handler)
946 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
947 tbfd, ntbfd, h->root.root.string);
948 else if (tdef)
949 (*_bfd_error_handler)
950 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
951 tbfd, tsec, ntbfd, h->root.root.string);
952 else
953 (*_bfd_error_handler)
954 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
955 tbfd, ntbfd, ntsec, h->root.root.string);
957 bfd_set_error (bfd_error_bad_value);
958 return FALSE;
961 /* We need to remember if a symbol has a definition in a dynamic
962 object or is weak in all dynamic objects. Internal and hidden
963 visibility will make it unavailable to dynamic objects. */
964 if (newdyn && !h->dynamic_def)
966 if (!bfd_is_und_section (sec))
967 h->dynamic_def = 1;
968 else
970 /* Check if this symbol is weak in all dynamic objects. If it
971 is the first time we see it in a dynamic object, we mark
972 if it is weak. Otherwise, we clear it. */
973 if (!h->ref_dynamic)
975 if (bind == STB_WEAK)
976 h->dynamic_weak = 1;
978 else if (bind != STB_WEAK)
979 h->dynamic_weak = 0;
983 /* If the old symbol has non-default visibility, we ignore the new
984 definition from a dynamic object. */
985 if (newdyn
986 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
987 && !bfd_is_und_section (sec))
989 *skip = TRUE;
990 /* Make sure this symbol is dynamic. */
991 h->ref_dynamic = 1;
992 /* A protected symbol has external availability. Make sure it is
993 recorded as dynamic.
995 FIXME: Should we check type and size for protected symbol? */
996 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
997 return bfd_elf_link_record_dynamic_symbol (info, h);
998 else
999 return TRUE;
1001 else if (!newdyn
1002 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1003 && h->def_dynamic)
1005 /* If the new symbol with non-default visibility comes from a
1006 relocatable file and the old definition comes from a dynamic
1007 object, we remove the old definition. */
1008 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1009 h = *sym_hash;
1011 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1012 && bfd_is_und_section (sec))
1014 /* If the new symbol is undefined and the old symbol was
1015 also undefined before, we need to make sure
1016 _bfd_generic_link_add_one_symbol doesn't mess
1017 up the linker hash table undefs list. Since the old
1018 definition came from a dynamic object, it is still on the
1019 undefs list. */
1020 h->root.type = bfd_link_hash_undefined;
1021 h->root.u.undef.abfd = abfd;
1023 else
1025 h->root.type = bfd_link_hash_new;
1026 h->root.u.undef.abfd = NULL;
1029 if (h->def_dynamic)
1031 h->def_dynamic = 0;
1032 h->ref_dynamic = 1;
1033 h->dynamic_def = 1;
1035 /* FIXME: Should we check type and size for protected symbol? */
1036 h->size = 0;
1037 h->type = 0;
1038 return TRUE;
1041 /* Differentiate strong and weak symbols. */
1042 newweak = bind == STB_WEAK;
1043 oldweak = (h->root.type == bfd_link_hash_defweak
1044 || h->root.type == bfd_link_hash_undefweak);
1046 /* If a new weak symbol definition comes from a regular file and the
1047 old symbol comes from a dynamic library, we treat the new one as
1048 strong. Similarly, an old weak symbol definition from a regular
1049 file is treated as strong when the new symbol comes from a dynamic
1050 library. Further, an old weak symbol from a dynamic library is
1051 treated as strong if the new symbol is from a dynamic library.
1052 This reflects the way glibc's ld.so works.
1054 Do this before setting *type_change_ok or *size_change_ok so that
1055 we warn properly when dynamic library symbols are overridden. */
1057 if (newdef && !newdyn && olddyn)
1058 newweak = FALSE;
1059 if (olddef && newdyn)
1060 oldweak = FALSE;
1062 /* It's OK to change the type if either the existing symbol or the
1063 new symbol is weak. A type change is also OK if the old symbol
1064 is undefined and the new symbol is defined. */
1066 if (oldweak
1067 || newweak
1068 || (newdef
1069 && h->root.type == bfd_link_hash_undefined))
1070 *type_change_ok = TRUE;
1072 /* It's OK to change the size if either the existing symbol or the
1073 new symbol is weak, or if the old symbol is undefined. */
1075 if (*type_change_ok
1076 || h->root.type == bfd_link_hash_undefined)
1077 *size_change_ok = TRUE;
1079 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1080 symbol, respectively, appears to be a common symbol in a dynamic
1081 object. If a symbol appears in an uninitialized section, and is
1082 not weak, and is not a function, then it may be a common symbol
1083 which was resolved when the dynamic object was created. We want
1084 to treat such symbols specially, because they raise special
1085 considerations when setting the symbol size: if the symbol
1086 appears as a common symbol in a regular object, and the size in
1087 the regular object is larger, we must make sure that we use the
1088 larger size. This problematic case can always be avoided in C,
1089 but it must be handled correctly when using Fortran shared
1090 libraries.
1092 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1093 likewise for OLDDYNCOMMON and OLDDEF.
1095 Note that this test is just a heuristic, and that it is quite
1096 possible to have an uninitialized symbol in a shared object which
1097 is really a definition, rather than a common symbol. This could
1098 lead to some minor confusion when the symbol really is a common
1099 symbol in some regular object. However, I think it will be
1100 harmless. */
1102 if (newdyn
1103 && newdef
1104 && !newweak
1105 && (sec->flags & SEC_ALLOC) != 0
1106 && (sec->flags & SEC_LOAD) == 0
1107 && sym->st_size > 0
1108 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1109 newdyncommon = TRUE;
1110 else
1111 newdyncommon = FALSE;
1113 if (olddyn
1114 && olddef
1115 && h->root.type == bfd_link_hash_defined
1116 && h->def_dynamic
1117 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1118 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1119 && h->size > 0
1120 && h->type != STT_FUNC)
1121 olddyncommon = TRUE;
1122 else
1123 olddyncommon = FALSE;
1125 /* If both the old and the new symbols look like common symbols in a
1126 dynamic object, set the size of the symbol to the larger of the
1127 two. */
1129 if (olddyncommon
1130 && newdyncommon
1131 && sym->st_size != h->size)
1133 /* Since we think we have two common symbols, issue a multiple
1134 common warning if desired. Note that we only warn if the
1135 size is different. If the size is the same, we simply let
1136 the old symbol override the new one as normally happens with
1137 symbols defined in dynamic objects. */
1139 if (! ((*info->callbacks->multiple_common)
1140 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1141 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1142 return FALSE;
1144 if (sym->st_size > h->size)
1145 h->size = sym->st_size;
1147 *size_change_ok = TRUE;
1150 /* If we are looking at a dynamic object, and we have found a
1151 definition, we need to see if the symbol was already defined by
1152 some other object. If so, we want to use the existing
1153 definition, and we do not want to report a multiple symbol
1154 definition error; we do this by clobbering *PSEC to be
1155 bfd_und_section_ptr.
1157 We treat a common symbol as a definition if the symbol in the
1158 shared library is a function, since common symbols always
1159 represent variables; this can cause confusion in principle, but
1160 any such confusion would seem to indicate an erroneous program or
1161 shared library. We also permit a common symbol in a regular
1162 object to override a weak symbol in a shared object. */
1164 if (newdyn
1165 && newdef
1166 && (olddef
1167 || (h->root.type == bfd_link_hash_common
1168 && (newweak
1169 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1171 *override = TRUE;
1172 newdef = FALSE;
1173 newdyncommon = FALSE;
1175 *psec = sec = bfd_und_section_ptr;
1176 *size_change_ok = TRUE;
1178 /* If we get here when the old symbol is a common symbol, then
1179 we are explicitly letting it override a weak symbol or
1180 function in a dynamic object, and we don't want to warn about
1181 a type change. If the old symbol is a defined symbol, a type
1182 change warning may still be appropriate. */
1184 if (h->root.type == bfd_link_hash_common)
1185 *type_change_ok = TRUE;
1188 /* Handle the special case of an old common symbol merging with a
1189 new symbol which looks like a common symbol in a shared object.
1190 We change *PSEC and *PVALUE to make the new symbol look like a
1191 common symbol, and let _bfd_generic_link_add_one_symbol will do
1192 the right thing. */
1194 if (newdyncommon
1195 && h->root.type == bfd_link_hash_common)
1197 *override = TRUE;
1198 newdef = FALSE;
1199 newdyncommon = FALSE;
1200 *pvalue = sym->st_size;
1201 *psec = sec = bfd_com_section_ptr;
1202 *size_change_ok = TRUE;
1205 /* Skip weak definitions of symbols that are already defined. */
1206 if (newdef && olddef && newweak && !oldweak)
1207 *skip = TRUE;
1209 /* If the old symbol is from a dynamic object, and the new symbol is
1210 a definition which is not from a dynamic object, then the new
1211 symbol overrides the old symbol. Symbols from regular files
1212 always take precedence over symbols from dynamic objects, even if
1213 they are defined after the dynamic object in the link.
1215 As above, we again permit a common symbol in a regular object to
1216 override a definition in a shared object if the shared object
1217 symbol is a function or is weak. */
1219 flip = NULL;
1220 if (!newdyn
1221 && (newdef
1222 || (bfd_is_com_section (sec)
1223 && (oldweak
1224 || h->type == STT_FUNC)))
1225 && olddyn
1226 && olddef
1227 && h->def_dynamic)
1229 /* Change the hash table entry to undefined, and let
1230 _bfd_generic_link_add_one_symbol do the right thing with the
1231 new definition. */
1233 h->root.type = bfd_link_hash_undefined;
1234 h->root.u.undef.abfd = h->root.u.def.section->owner;
1235 *size_change_ok = TRUE;
1237 olddef = FALSE;
1238 olddyncommon = FALSE;
1240 /* We again permit a type change when a common symbol may be
1241 overriding a function. */
1243 if (bfd_is_com_section (sec))
1244 *type_change_ok = TRUE;
1246 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1247 flip = *sym_hash;
1248 else
1249 /* This union may have been set to be non-NULL when this symbol
1250 was seen in a dynamic object. We must force the union to be
1251 NULL, so that it is correct for a regular symbol. */
1252 h->verinfo.vertree = NULL;
1255 /* Handle the special case of a new common symbol merging with an
1256 old symbol that looks like it might be a common symbol defined in
1257 a shared object. Note that we have already handled the case in
1258 which a new common symbol should simply override the definition
1259 in the shared library. */
1261 if (! newdyn
1262 && bfd_is_com_section (sec)
1263 && olddyncommon)
1265 /* It would be best if we could set the hash table entry to a
1266 common symbol, but we don't know what to use for the section
1267 or the alignment. */
1268 if (! ((*info->callbacks->multiple_common)
1269 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1270 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1271 return FALSE;
1273 /* If the presumed common symbol in the dynamic object is
1274 larger, pretend that the new symbol has its size. */
1276 if (h->size > *pvalue)
1277 *pvalue = h->size;
1279 /* We need to remember the alignment required by the symbol
1280 in the dynamic object. */
1281 BFD_ASSERT (pold_alignment);
1282 *pold_alignment = h->root.u.def.section->alignment_power;
1284 olddef = FALSE;
1285 olddyncommon = FALSE;
1287 h->root.type = bfd_link_hash_undefined;
1288 h->root.u.undef.abfd = h->root.u.def.section->owner;
1290 *size_change_ok = TRUE;
1291 *type_change_ok = TRUE;
1293 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1294 flip = *sym_hash;
1295 else
1296 h->verinfo.vertree = NULL;
1299 if (flip != NULL)
1301 /* Handle the case where we had a versioned symbol in a dynamic
1302 library and now find a definition in a normal object. In this
1303 case, we make the versioned symbol point to the normal one. */
1304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1305 flip->root.type = h->root.type;
1306 h->root.type = bfd_link_hash_indirect;
1307 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1308 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1309 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1310 if (h->def_dynamic)
1312 h->def_dynamic = 0;
1313 flip->ref_dynamic = 1;
1317 return TRUE;
1320 /* This function is called to create an indirect symbol from the
1321 default for the symbol with the default version if needed. The
1322 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1323 set DYNSYM if the new indirect symbol is dynamic. */
1325 bfd_boolean
1326 _bfd_elf_add_default_symbol (bfd *abfd,
1327 struct bfd_link_info *info,
1328 struct elf_link_hash_entry *h,
1329 const char *name,
1330 Elf_Internal_Sym *sym,
1331 asection **psec,
1332 bfd_vma *value,
1333 bfd_boolean *dynsym,
1334 bfd_boolean override)
1336 bfd_boolean type_change_ok;
1337 bfd_boolean size_change_ok;
1338 bfd_boolean skip;
1339 char *shortname;
1340 struct elf_link_hash_entry *hi;
1341 struct bfd_link_hash_entry *bh;
1342 const struct elf_backend_data *bed;
1343 bfd_boolean collect;
1344 bfd_boolean dynamic;
1345 char *p;
1346 size_t len, shortlen;
1347 asection *sec;
1349 /* If this symbol has a version, and it is the default version, we
1350 create an indirect symbol from the default name to the fully
1351 decorated name. This will cause external references which do not
1352 specify a version to be bound to this version of the symbol. */
1353 p = strchr (name, ELF_VER_CHR);
1354 if (p == NULL || p[1] != ELF_VER_CHR)
1355 return TRUE;
1357 if (override)
1359 /* We are overridden by an old definition. We need to check if we
1360 need to create the indirect symbol from the default name. */
1361 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1362 FALSE, FALSE);
1363 BFD_ASSERT (hi != NULL);
1364 if (hi == h)
1365 return TRUE;
1366 while (hi->root.type == bfd_link_hash_indirect
1367 || hi->root.type == bfd_link_hash_warning)
1369 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1370 if (hi == h)
1371 return TRUE;
1375 bed = get_elf_backend_data (abfd);
1376 collect = bed->collect;
1377 dynamic = (abfd->flags & DYNAMIC) != 0;
1379 shortlen = p - name;
1380 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1381 if (shortname == NULL)
1382 return FALSE;
1383 memcpy (shortname, name, shortlen);
1384 shortname[shortlen] = '\0';
1386 /* We are going to create a new symbol. Merge it with any existing
1387 symbol with this name. For the purposes of the merge, act as
1388 though we were defining the symbol we just defined, although we
1389 actually going to define an indirect symbol. */
1390 type_change_ok = FALSE;
1391 size_change_ok = FALSE;
1392 sec = *psec;
1393 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1394 NULL, &hi, &skip, &override,
1395 &type_change_ok, &size_change_ok))
1396 return FALSE;
1398 if (skip)
1399 goto nondefault;
1401 if (! override)
1403 bh = &hi->root;
1404 if (! (_bfd_generic_link_add_one_symbol
1405 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1406 0, name, FALSE, collect, &bh)))
1407 return FALSE;
1408 hi = (struct elf_link_hash_entry *) bh;
1410 else
1412 /* In this case the symbol named SHORTNAME is overriding the
1413 indirect symbol we want to add. We were planning on making
1414 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1415 is the name without a version. NAME is the fully versioned
1416 name, and it is the default version.
1418 Overriding means that we already saw a definition for the
1419 symbol SHORTNAME in a regular object, and it is overriding
1420 the symbol defined in the dynamic object.
1422 When this happens, we actually want to change NAME, the
1423 symbol we just added, to refer to SHORTNAME. This will cause
1424 references to NAME in the shared object to become references
1425 to SHORTNAME in the regular object. This is what we expect
1426 when we override a function in a shared object: that the
1427 references in the shared object will be mapped to the
1428 definition in the regular object. */
1430 while (hi->root.type == bfd_link_hash_indirect
1431 || hi->root.type == bfd_link_hash_warning)
1432 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1434 h->root.type = bfd_link_hash_indirect;
1435 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1436 if (h->def_dynamic)
1438 h->def_dynamic = 0;
1439 hi->ref_dynamic = 1;
1440 if (hi->ref_regular
1441 || hi->def_regular)
1443 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1444 return FALSE;
1448 /* Now set HI to H, so that the following code will set the
1449 other fields correctly. */
1450 hi = h;
1453 /* If there is a duplicate definition somewhere, then HI may not
1454 point to an indirect symbol. We will have reported an error to
1455 the user in that case. */
1457 if (hi->root.type == bfd_link_hash_indirect)
1459 struct elf_link_hash_entry *ht;
1461 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1462 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1464 /* See if the new flags lead us to realize that the symbol must
1465 be dynamic. */
1466 if (! *dynsym)
1468 if (! dynamic)
1470 if (info->shared
1471 || hi->ref_dynamic)
1472 *dynsym = TRUE;
1474 else
1476 if (hi->ref_regular)
1477 *dynsym = TRUE;
1482 /* We also need to define an indirection from the nondefault version
1483 of the symbol. */
1485 nondefault:
1486 len = strlen (name);
1487 shortname = bfd_hash_allocate (&info->hash->table, len);
1488 if (shortname == NULL)
1489 return FALSE;
1490 memcpy (shortname, name, shortlen);
1491 memcpy (shortname + shortlen, p + 1, len - shortlen);
1493 /* Once again, merge with any existing symbol. */
1494 type_change_ok = FALSE;
1495 size_change_ok = FALSE;
1496 sec = *psec;
1497 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1498 NULL, &hi, &skip, &override,
1499 &type_change_ok, &size_change_ok))
1500 return FALSE;
1502 if (skip)
1503 return TRUE;
1505 if (override)
1507 /* Here SHORTNAME is a versioned name, so we don't expect to see
1508 the type of override we do in the case above unless it is
1509 overridden by a versioned definition. */
1510 if (hi->root.type != bfd_link_hash_defined
1511 && hi->root.type != bfd_link_hash_defweak)
1512 (*_bfd_error_handler)
1513 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1514 abfd, shortname);
1516 else
1518 bh = &hi->root;
1519 if (! (_bfd_generic_link_add_one_symbol
1520 (info, abfd, shortname, BSF_INDIRECT,
1521 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1522 return FALSE;
1523 hi = (struct elf_link_hash_entry *) bh;
1525 /* If there is a duplicate definition somewhere, then HI may not
1526 point to an indirect symbol. We will have reported an error
1527 to the user in that case. */
1529 if (hi->root.type == bfd_link_hash_indirect)
1531 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1533 /* See if the new flags lead us to realize that the symbol
1534 must be dynamic. */
1535 if (! *dynsym)
1537 if (! dynamic)
1539 if (info->shared
1540 || hi->ref_dynamic)
1541 *dynsym = TRUE;
1543 else
1545 if (hi->ref_regular)
1546 *dynsym = TRUE;
1552 return TRUE;
1555 /* This routine is used to export all defined symbols into the dynamic
1556 symbol table. It is called via elf_link_hash_traverse. */
1558 bfd_boolean
1559 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1561 struct elf_info_failed *eif = data;
1563 /* Ignore indirect symbols. These are added by the versioning code. */
1564 if (h->root.type == bfd_link_hash_indirect)
1565 return TRUE;
1567 if (h->root.type == bfd_link_hash_warning)
1568 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1570 if (h->dynindx == -1
1571 && (h->def_regular
1572 || h->ref_regular))
1574 struct bfd_elf_version_tree *t;
1575 struct bfd_elf_version_expr *d;
1577 for (t = eif->verdefs; t != NULL; t = t->next)
1579 if (t->globals.list != NULL)
1581 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1582 if (d != NULL)
1583 goto doit;
1586 if (t->locals.list != NULL)
1588 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1589 if (d != NULL)
1590 return TRUE;
1594 if (!eif->verdefs)
1596 doit:
1597 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1599 eif->failed = TRUE;
1600 return FALSE;
1605 return TRUE;
1608 /* Look through the symbols which are defined in other shared
1609 libraries and referenced here. Update the list of version
1610 dependencies. This will be put into the .gnu.version_r section.
1611 This function is called via elf_link_hash_traverse. */
1613 bfd_boolean
1614 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1615 void *data)
1617 struct elf_find_verdep_info *rinfo = data;
1618 Elf_Internal_Verneed *t;
1619 Elf_Internal_Vernaux *a;
1620 bfd_size_type amt;
1622 if (h->root.type == bfd_link_hash_warning)
1623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1625 /* We only care about symbols defined in shared objects with version
1626 information. */
1627 if (!h->def_dynamic
1628 || h->def_regular
1629 || h->dynindx == -1
1630 || h->verinfo.verdef == NULL)
1631 return TRUE;
1633 /* See if we already know about this version. */
1634 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1636 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1637 continue;
1639 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1640 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1641 return TRUE;
1643 break;
1646 /* This is a new version. Add it to tree we are building. */
1648 if (t == NULL)
1650 amt = sizeof *t;
1651 t = bfd_zalloc (rinfo->output_bfd, amt);
1652 if (t == NULL)
1654 rinfo->failed = TRUE;
1655 return FALSE;
1658 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1659 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1660 elf_tdata (rinfo->output_bfd)->verref = t;
1663 amt = sizeof *a;
1664 a = bfd_zalloc (rinfo->output_bfd, amt);
1666 /* Note that we are copying a string pointer here, and testing it
1667 above. If bfd_elf_string_from_elf_section is ever changed to
1668 discard the string data when low in memory, this will have to be
1669 fixed. */
1670 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1672 a->vna_flags = h->verinfo.verdef->vd_flags;
1673 a->vna_nextptr = t->vn_auxptr;
1675 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1676 ++rinfo->vers;
1678 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1680 t->vn_auxptr = a;
1682 return TRUE;
1685 /* Figure out appropriate versions for all the symbols. We may not
1686 have the version number script until we have read all of the input
1687 files, so until that point we don't know which symbols should be
1688 local. This function is called via elf_link_hash_traverse. */
1690 bfd_boolean
1691 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1693 struct elf_assign_sym_version_info *sinfo;
1694 struct bfd_link_info *info;
1695 const struct elf_backend_data *bed;
1696 struct elf_info_failed eif;
1697 char *p;
1698 bfd_size_type amt;
1700 sinfo = data;
1701 info = sinfo->info;
1703 if (h->root.type == bfd_link_hash_warning)
1704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1706 /* Fix the symbol flags. */
1707 eif.failed = FALSE;
1708 eif.info = info;
1709 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1711 if (eif.failed)
1712 sinfo->failed = TRUE;
1713 return FALSE;
1716 /* We only need version numbers for symbols defined in regular
1717 objects. */
1718 if (!h->def_regular)
1719 return TRUE;
1721 bed = get_elf_backend_data (sinfo->output_bfd);
1722 p = strchr (h->root.root.string, ELF_VER_CHR);
1723 if (p != NULL && h->verinfo.vertree == NULL)
1725 struct bfd_elf_version_tree *t;
1726 bfd_boolean hidden;
1728 hidden = TRUE;
1730 /* There are two consecutive ELF_VER_CHR characters if this is
1731 not a hidden symbol. */
1732 ++p;
1733 if (*p == ELF_VER_CHR)
1735 hidden = FALSE;
1736 ++p;
1739 /* If there is no version string, we can just return out. */
1740 if (*p == '\0')
1742 if (hidden)
1743 h->hidden = 1;
1744 return TRUE;
1747 /* Look for the version. If we find it, it is no longer weak. */
1748 for (t = sinfo->verdefs; t != NULL; t = t->next)
1750 if (strcmp (t->name, p) == 0)
1752 size_t len;
1753 char *alc;
1754 struct bfd_elf_version_expr *d;
1756 len = p - h->root.root.string;
1757 alc = bfd_malloc (len);
1758 if (alc == NULL)
1759 return FALSE;
1760 memcpy (alc, h->root.root.string, len - 1);
1761 alc[len - 1] = '\0';
1762 if (alc[len - 2] == ELF_VER_CHR)
1763 alc[len - 2] = '\0';
1765 h->verinfo.vertree = t;
1766 t->used = TRUE;
1767 d = NULL;
1769 if (t->globals.list != NULL)
1770 d = (*t->match) (&t->globals, NULL, alc);
1772 /* See if there is anything to force this symbol to
1773 local scope. */
1774 if (d == NULL && t->locals.list != NULL)
1776 d = (*t->match) (&t->locals, NULL, alc);
1777 if (d != NULL
1778 && h->dynindx != -1
1779 && ! info->export_dynamic)
1780 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1783 free (alc);
1784 break;
1788 /* If we are building an application, we need to create a
1789 version node for this version. */
1790 if (t == NULL && info->executable)
1792 struct bfd_elf_version_tree **pp;
1793 int version_index;
1795 /* If we aren't going to export this symbol, we don't need
1796 to worry about it. */
1797 if (h->dynindx == -1)
1798 return TRUE;
1800 amt = sizeof *t;
1801 t = bfd_zalloc (sinfo->output_bfd, amt);
1802 if (t == NULL)
1804 sinfo->failed = TRUE;
1805 return FALSE;
1808 t->name = p;
1809 t->name_indx = (unsigned int) -1;
1810 t->used = TRUE;
1812 version_index = 1;
1813 /* Don't count anonymous version tag. */
1814 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1815 version_index = 0;
1816 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1817 ++version_index;
1818 t->vernum = version_index;
1820 *pp = t;
1822 h->verinfo.vertree = t;
1824 else if (t == NULL)
1826 /* We could not find the version for a symbol when
1827 generating a shared archive. Return an error. */
1828 (*_bfd_error_handler)
1829 (_("%B: undefined versioned symbol name %s"),
1830 sinfo->output_bfd, h->root.root.string);
1831 bfd_set_error (bfd_error_bad_value);
1832 sinfo->failed = TRUE;
1833 return FALSE;
1836 if (hidden)
1837 h->hidden = 1;
1840 /* If we don't have a version for this symbol, see if we can find
1841 something. */
1842 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1844 struct bfd_elf_version_tree *t;
1845 struct bfd_elf_version_tree *local_ver;
1846 struct bfd_elf_version_expr *d;
1848 /* See if can find what version this symbol is in. If the
1849 symbol is supposed to be local, then don't actually register
1850 it. */
1851 local_ver = NULL;
1852 for (t = sinfo->verdefs; t != NULL; t = t->next)
1854 if (t->globals.list != NULL)
1856 bfd_boolean matched;
1858 matched = FALSE;
1859 d = NULL;
1860 while ((d = (*t->match) (&t->globals, d,
1861 h->root.root.string)) != NULL)
1862 if (d->symver)
1863 matched = TRUE;
1864 else
1866 /* There is a version without definition. Make
1867 the symbol the default definition for this
1868 version. */
1869 h->verinfo.vertree = t;
1870 local_ver = NULL;
1871 d->script = 1;
1872 break;
1874 if (d != NULL)
1875 break;
1876 else if (matched)
1877 /* There is no undefined version for this symbol. Hide the
1878 default one. */
1879 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1882 if (t->locals.list != NULL)
1884 d = NULL;
1885 while ((d = (*t->match) (&t->locals, d,
1886 h->root.root.string)) != NULL)
1888 local_ver = t;
1889 /* If the match is "*", keep looking for a more
1890 explicit, perhaps even global, match.
1891 XXX: Shouldn't this be !d->wildcard instead? */
1892 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1893 break;
1896 if (d != NULL)
1897 break;
1901 if (local_ver != NULL)
1903 h->verinfo.vertree = local_ver;
1904 if (h->dynindx != -1
1905 && ! info->export_dynamic)
1907 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1912 return TRUE;
1915 /* Read and swap the relocs from the section indicated by SHDR. This
1916 may be either a REL or a RELA section. The relocations are
1917 translated into RELA relocations and stored in INTERNAL_RELOCS,
1918 which should have already been allocated to contain enough space.
1919 The EXTERNAL_RELOCS are a buffer where the external form of the
1920 relocations should be stored.
1922 Returns FALSE if something goes wrong. */
1924 static bfd_boolean
1925 elf_link_read_relocs_from_section (bfd *abfd,
1926 asection *sec,
1927 Elf_Internal_Shdr *shdr,
1928 void *external_relocs,
1929 Elf_Internal_Rela *internal_relocs)
1931 const struct elf_backend_data *bed;
1932 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1933 const bfd_byte *erela;
1934 const bfd_byte *erelaend;
1935 Elf_Internal_Rela *irela;
1936 Elf_Internal_Shdr *symtab_hdr;
1937 size_t nsyms;
1939 /* Position ourselves at the start of the section. */
1940 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1941 return FALSE;
1943 /* Read the relocations. */
1944 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1945 return FALSE;
1947 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1948 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1950 bed = get_elf_backend_data (abfd);
1952 /* Convert the external relocations to the internal format. */
1953 if (shdr->sh_entsize == bed->s->sizeof_rel)
1954 swap_in = bed->s->swap_reloc_in;
1955 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1956 swap_in = bed->s->swap_reloca_in;
1957 else
1959 bfd_set_error (bfd_error_wrong_format);
1960 return FALSE;
1963 erela = external_relocs;
1964 erelaend = erela + shdr->sh_size;
1965 irela = internal_relocs;
1966 while (erela < erelaend)
1968 bfd_vma r_symndx;
1970 (*swap_in) (abfd, erela, irela);
1971 r_symndx = ELF32_R_SYM (irela->r_info);
1972 if (bed->s->arch_size == 64)
1973 r_symndx >>= 24;
1974 if ((size_t) r_symndx >= nsyms)
1976 (*_bfd_error_handler)
1977 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1978 " for offset 0x%lx in section `%A'"),
1979 abfd, sec,
1980 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1981 bfd_set_error (bfd_error_bad_value);
1982 return FALSE;
1984 irela += bed->s->int_rels_per_ext_rel;
1985 erela += shdr->sh_entsize;
1988 return TRUE;
1991 /* Read and swap the relocs for a section O. They may have been
1992 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1993 not NULL, they are used as buffers to read into. They are known to
1994 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1995 the return value is allocated using either malloc or bfd_alloc,
1996 according to the KEEP_MEMORY argument. If O has two relocation
1997 sections (both REL and RELA relocations), then the REL_HDR
1998 relocations will appear first in INTERNAL_RELOCS, followed by the
1999 REL_HDR2 relocations. */
2001 Elf_Internal_Rela *
2002 _bfd_elf_link_read_relocs (bfd *abfd,
2003 asection *o,
2004 void *external_relocs,
2005 Elf_Internal_Rela *internal_relocs,
2006 bfd_boolean keep_memory)
2008 Elf_Internal_Shdr *rel_hdr;
2009 void *alloc1 = NULL;
2010 Elf_Internal_Rela *alloc2 = NULL;
2011 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2013 if (elf_section_data (o)->relocs != NULL)
2014 return elf_section_data (o)->relocs;
2016 if (o->reloc_count == 0)
2017 return NULL;
2019 rel_hdr = &elf_section_data (o)->rel_hdr;
2021 if (internal_relocs == NULL)
2023 bfd_size_type size;
2025 size = o->reloc_count;
2026 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2027 if (keep_memory)
2028 internal_relocs = bfd_alloc (abfd, size);
2029 else
2030 internal_relocs = alloc2 = bfd_malloc (size);
2031 if (internal_relocs == NULL)
2032 goto error_return;
2035 if (external_relocs == NULL)
2037 bfd_size_type size = rel_hdr->sh_size;
2039 if (elf_section_data (o)->rel_hdr2)
2040 size += elf_section_data (o)->rel_hdr2->sh_size;
2041 alloc1 = bfd_malloc (size);
2042 if (alloc1 == NULL)
2043 goto error_return;
2044 external_relocs = alloc1;
2047 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2048 external_relocs,
2049 internal_relocs))
2050 goto error_return;
2051 if (elf_section_data (o)->rel_hdr2
2052 && (!elf_link_read_relocs_from_section
2053 (abfd, o,
2054 elf_section_data (o)->rel_hdr2,
2055 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2056 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2057 * bed->s->int_rels_per_ext_rel))))
2058 goto error_return;
2060 /* Cache the results for next time, if we can. */
2061 if (keep_memory)
2062 elf_section_data (o)->relocs = internal_relocs;
2064 if (alloc1 != NULL)
2065 free (alloc1);
2067 /* Don't free alloc2, since if it was allocated we are passing it
2068 back (under the name of internal_relocs). */
2070 return internal_relocs;
2072 error_return:
2073 if (alloc1 != NULL)
2074 free (alloc1);
2075 if (alloc2 != NULL)
2076 free (alloc2);
2077 return NULL;
2080 /* Compute the size of, and allocate space for, REL_HDR which is the
2081 section header for a section containing relocations for O. */
2083 bfd_boolean
2084 _bfd_elf_link_size_reloc_section (bfd *abfd,
2085 Elf_Internal_Shdr *rel_hdr,
2086 asection *o)
2088 bfd_size_type reloc_count;
2089 bfd_size_type num_rel_hashes;
2091 /* Figure out how many relocations there will be. */
2092 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2093 reloc_count = elf_section_data (o)->rel_count;
2094 else
2095 reloc_count = elf_section_data (o)->rel_count2;
2097 num_rel_hashes = o->reloc_count;
2098 if (num_rel_hashes < reloc_count)
2099 num_rel_hashes = reloc_count;
2101 /* That allows us to calculate the size of the section. */
2102 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2104 /* The contents field must last into write_object_contents, so we
2105 allocate it with bfd_alloc rather than malloc. Also since we
2106 cannot be sure that the contents will actually be filled in,
2107 we zero the allocated space. */
2108 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2109 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2110 return FALSE;
2112 /* We only allocate one set of hash entries, so we only do it the
2113 first time we are called. */
2114 if (elf_section_data (o)->rel_hashes == NULL
2115 && num_rel_hashes)
2117 struct elf_link_hash_entry **p;
2119 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2120 if (p == NULL)
2121 return FALSE;
2123 elf_section_data (o)->rel_hashes = p;
2126 return TRUE;
2129 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2130 originated from the section given by INPUT_REL_HDR) to the
2131 OUTPUT_BFD. */
2133 bfd_boolean
2134 _bfd_elf_link_output_relocs (bfd *output_bfd,
2135 asection *input_section,
2136 Elf_Internal_Shdr *input_rel_hdr,
2137 Elf_Internal_Rela *internal_relocs,
2138 struct elf_link_hash_entry **rel_hash
2139 ATTRIBUTE_UNUSED)
2141 Elf_Internal_Rela *irela;
2142 Elf_Internal_Rela *irelaend;
2143 bfd_byte *erel;
2144 Elf_Internal_Shdr *output_rel_hdr;
2145 asection *output_section;
2146 unsigned int *rel_countp = NULL;
2147 const struct elf_backend_data *bed;
2148 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2150 output_section = input_section->output_section;
2151 output_rel_hdr = NULL;
2153 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2154 == input_rel_hdr->sh_entsize)
2156 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2157 rel_countp = &elf_section_data (output_section)->rel_count;
2159 else if (elf_section_data (output_section)->rel_hdr2
2160 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2161 == input_rel_hdr->sh_entsize))
2163 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2164 rel_countp = &elf_section_data (output_section)->rel_count2;
2166 else
2168 (*_bfd_error_handler)
2169 (_("%B: relocation size mismatch in %B section %A"),
2170 output_bfd, input_section->owner, input_section);
2171 bfd_set_error (bfd_error_wrong_object_format);
2172 return FALSE;
2175 bed = get_elf_backend_data (output_bfd);
2176 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2177 swap_out = bed->s->swap_reloc_out;
2178 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2179 swap_out = bed->s->swap_reloca_out;
2180 else
2181 abort ();
2183 erel = output_rel_hdr->contents;
2184 erel += *rel_countp * input_rel_hdr->sh_entsize;
2185 irela = internal_relocs;
2186 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2187 * bed->s->int_rels_per_ext_rel);
2188 while (irela < irelaend)
2190 (*swap_out) (output_bfd, irela, erel);
2191 irela += bed->s->int_rels_per_ext_rel;
2192 erel += input_rel_hdr->sh_entsize;
2195 /* Bump the counter, so that we know where to add the next set of
2196 relocations. */
2197 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2199 return TRUE;
2202 /* Fix up the flags for a symbol. This handles various cases which
2203 can only be fixed after all the input files are seen. This is
2204 currently called by both adjust_dynamic_symbol and
2205 assign_sym_version, which is unnecessary but perhaps more robust in
2206 the face of future changes. */
2208 bfd_boolean
2209 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2210 struct elf_info_failed *eif)
2212 /* If this symbol was mentioned in a non-ELF file, try to set
2213 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2214 permit a non-ELF file to correctly refer to a symbol defined in
2215 an ELF dynamic object. */
2216 if (h->non_elf)
2218 while (h->root.type == bfd_link_hash_indirect)
2219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2221 if (h->root.type != bfd_link_hash_defined
2222 && h->root.type != bfd_link_hash_defweak)
2224 h->ref_regular = 1;
2225 h->ref_regular_nonweak = 1;
2227 else
2229 if (h->root.u.def.section->owner != NULL
2230 && (bfd_get_flavour (h->root.u.def.section->owner)
2231 == bfd_target_elf_flavour))
2233 h->ref_regular = 1;
2234 h->ref_regular_nonweak = 1;
2236 else
2237 h->def_regular = 1;
2240 if (h->dynindx == -1
2241 && (h->def_dynamic
2242 || h->ref_dynamic))
2244 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2246 eif->failed = TRUE;
2247 return FALSE;
2251 else
2253 /* Unfortunately, NON_ELF is only correct if the symbol
2254 was first seen in a non-ELF file. Fortunately, if the symbol
2255 was first seen in an ELF file, we're probably OK unless the
2256 symbol was defined in a non-ELF file. Catch that case here.
2257 FIXME: We're still in trouble if the symbol was first seen in
2258 a dynamic object, and then later in a non-ELF regular object. */
2259 if ((h->root.type == bfd_link_hash_defined
2260 || h->root.type == bfd_link_hash_defweak)
2261 && !h->def_regular
2262 && (h->root.u.def.section->owner != NULL
2263 ? (bfd_get_flavour (h->root.u.def.section->owner)
2264 != bfd_target_elf_flavour)
2265 : (bfd_is_abs_section (h->root.u.def.section)
2266 && !h->def_dynamic)))
2267 h->def_regular = 1;
2270 /* If this is a final link, and the symbol was defined as a common
2271 symbol in a regular object file, and there was no definition in
2272 any dynamic object, then the linker will have allocated space for
2273 the symbol in a common section but the DEF_REGULAR
2274 flag will not have been set. */
2275 if (h->root.type == bfd_link_hash_defined
2276 && !h->def_regular
2277 && h->ref_regular
2278 && !h->def_dynamic
2279 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2280 h->def_regular = 1;
2282 /* If -Bsymbolic was used (which means to bind references to global
2283 symbols to the definition within the shared object), and this
2284 symbol was defined in a regular object, then it actually doesn't
2285 need a PLT entry. Likewise, if the symbol has non-default
2286 visibility. If the symbol has hidden or internal visibility, we
2287 will force it local. */
2288 if (h->needs_plt
2289 && eif->info->shared
2290 && is_elf_hash_table (eif->info->hash)
2291 && (eif->info->symbolic
2292 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2293 && h->def_regular)
2295 const struct elf_backend_data *bed;
2296 bfd_boolean force_local;
2298 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2300 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2301 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2302 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2305 /* If a weak undefined symbol has non-default visibility, we also
2306 hide it from the dynamic linker. */
2307 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2308 && h->root.type == bfd_link_hash_undefweak)
2310 const struct elf_backend_data *bed;
2311 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2312 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2315 /* If this is a weak defined symbol in a dynamic object, and we know
2316 the real definition in the dynamic object, copy interesting flags
2317 over to the real definition. */
2318 if (h->u.weakdef != NULL)
2320 struct elf_link_hash_entry *weakdef;
2322 weakdef = h->u.weakdef;
2323 if (h->root.type == bfd_link_hash_indirect)
2324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2326 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2327 || h->root.type == bfd_link_hash_defweak);
2328 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2329 || weakdef->root.type == bfd_link_hash_defweak);
2330 BFD_ASSERT (weakdef->def_dynamic);
2332 /* If the real definition is defined by a regular object file,
2333 don't do anything special. See the longer description in
2334 _bfd_elf_adjust_dynamic_symbol, below. */
2335 if (weakdef->def_regular)
2336 h->u.weakdef = NULL;
2337 else
2339 const struct elf_backend_data *bed;
2341 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2342 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2346 return TRUE;
2349 /* Make the backend pick a good value for a dynamic symbol. This is
2350 called via elf_link_hash_traverse, and also calls itself
2351 recursively. */
2353 bfd_boolean
2354 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2356 struct elf_info_failed *eif = data;
2357 bfd *dynobj;
2358 const struct elf_backend_data *bed;
2360 if (! is_elf_hash_table (eif->info->hash))
2361 return FALSE;
2363 if (h->root.type == bfd_link_hash_warning)
2365 h->plt = elf_hash_table (eif->info)->init_offset;
2366 h->got = elf_hash_table (eif->info)->init_offset;
2368 /* When warning symbols are created, they **replace** the "real"
2369 entry in the hash table, thus we never get to see the real
2370 symbol in a hash traversal. So look at it now. */
2371 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2374 /* Ignore indirect symbols. These are added by the versioning code. */
2375 if (h->root.type == bfd_link_hash_indirect)
2376 return TRUE;
2378 /* Fix the symbol flags. */
2379 if (! _bfd_elf_fix_symbol_flags (h, eif))
2380 return FALSE;
2382 /* If this symbol does not require a PLT entry, and it is not
2383 defined by a dynamic object, or is not referenced by a regular
2384 object, ignore it. We do have to handle a weak defined symbol,
2385 even if no regular object refers to it, if we decided to add it
2386 to the dynamic symbol table. FIXME: Do we normally need to worry
2387 about symbols which are defined by one dynamic object and
2388 referenced by another one? */
2389 if (!h->needs_plt
2390 && (h->def_regular
2391 || !h->def_dynamic
2392 || (!h->ref_regular
2393 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2395 h->plt = elf_hash_table (eif->info)->init_offset;
2396 return TRUE;
2399 /* If we've already adjusted this symbol, don't do it again. This
2400 can happen via a recursive call. */
2401 if (h->dynamic_adjusted)
2402 return TRUE;
2404 /* Don't look at this symbol again. Note that we must set this
2405 after checking the above conditions, because we may look at a
2406 symbol once, decide not to do anything, and then get called
2407 recursively later after REF_REGULAR is set below. */
2408 h->dynamic_adjusted = 1;
2410 /* If this is a weak definition, and we know a real definition, and
2411 the real symbol is not itself defined by a regular object file,
2412 then get a good value for the real definition. We handle the
2413 real symbol first, for the convenience of the backend routine.
2415 Note that there is a confusing case here. If the real definition
2416 is defined by a regular object file, we don't get the real symbol
2417 from the dynamic object, but we do get the weak symbol. If the
2418 processor backend uses a COPY reloc, then if some routine in the
2419 dynamic object changes the real symbol, we will not see that
2420 change in the corresponding weak symbol. This is the way other
2421 ELF linkers work as well, and seems to be a result of the shared
2422 library model.
2424 I will clarify this issue. Most SVR4 shared libraries define the
2425 variable _timezone and define timezone as a weak synonym. The
2426 tzset call changes _timezone. If you write
2427 extern int timezone;
2428 int _timezone = 5;
2429 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2430 you might expect that, since timezone is a synonym for _timezone,
2431 the same number will print both times. However, if the processor
2432 backend uses a COPY reloc, then actually timezone will be copied
2433 into your process image, and, since you define _timezone
2434 yourself, _timezone will not. Thus timezone and _timezone will
2435 wind up at different memory locations. The tzset call will set
2436 _timezone, leaving timezone unchanged. */
2438 if (h->u.weakdef != NULL)
2440 /* If we get to this point, we know there is an implicit
2441 reference by a regular object file via the weak symbol H.
2442 FIXME: Is this really true? What if the traversal finds
2443 H->U.WEAKDEF before it finds H? */
2444 h->u.weakdef->ref_regular = 1;
2446 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2447 return FALSE;
2450 /* If a symbol has no type and no size and does not require a PLT
2451 entry, then we are probably about to do the wrong thing here: we
2452 are probably going to create a COPY reloc for an empty object.
2453 This case can arise when a shared object is built with assembly
2454 code, and the assembly code fails to set the symbol type. */
2455 if (h->size == 0
2456 && h->type == STT_NOTYPE
2457 && !h->needs_plt)
2458 (*_bfd_error_handler)
2459 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2460 h->root.root.string);
2462 dynobj = elf_hash_table (eif->info)->dynobj;
2463 bed = get_elf_backend_data (dynobj);
2464 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2466 eif->failed = TRUE;
2467 return FALSE;
2470 return TRUE;
2473 /* Adjust all external symbols pointing into SEC_MERGE sections
2474 to reflect the object merging within the sections. */
2476 bfd_boolean
2477 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2479 asection *sec;
2481 if (h->root.type == bfd_link_hash_warning)
2482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2484 if ((h->root.type == bfd_link_hash_defined
2485 || h->root.type == bfd_link_hash_defweak)
2486 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2487 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2489 bfd *output_bfd = data;
2491 h->root.u.def.value =
2492 _bfd_merged_section_offset (output_bfd,
2493 &h->root.u.def.section,
2494 elf_section_data (sec)->sec_info,
2495 h->root.u.def.value);
2498 return TRUE;
2501 /* Returns false if the symbol referred to by H should be considered
2502 to resolve local to the current module, and true if it should be
2503 considered to bind dynamically. */
2505 bfd_boolean
2506 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2507 struct bfd_link_info *info,
2508 bfd_boolean ignore_protected)
2510 bfd_boolean binding_stays_local_p;
2512 if (h == NULL)
2513 return FALSE;
2515 while (h->root.type == bfd_link_hash_indirect
2516 || h->root.type == bfd_link_hash_warning)
2517 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2519 /* If it was forced local, then clearly it's not dynamic. */
2520 if (h->dynindx == -1)
2521 return FALSE;
2522 if (h->forced_local)
2523 return FALSE;
2525 /* Identify the cases where name binding rules say that a
2526 visible symbol resolves locally. */
2527 binding_stays_local_p = info->executable || info->symbolic;
2529 switch (ELF_ST_VISIBILITY (h->other))
2531 case STV_INTERNAL:
2532 case STV_HIDDEN:
2533 return FALSE;
2535 case STV_PROTECTED:
2536 /* Proper resolution for function pointer equality may require
2537 that these symbols perhaps be resolved dynamically, even though
2538 we should be resolving them to the current module. */
2539 if (!ignore_protected || h->type != STT_FUNC)
2540 binding_stays_local_p = TRUE;
2541 break;
2543 default:
2544 break;
2547 /* If it isn't defined locally, then clearly it's dynamic. */
2548 if (!h->def_regular)
2549 return TRUE;
2551 /* Otherwise, the symbol is dynamic if binding rules don't tell
2552 us that it remains local. */
2553 return !binding_stays_local_p;
2556 /* Return true if the symbol referred to by H should be considered
2557 to resolve local to the current module, and false otherwise. Differs
2558 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2559 undefined symbols and weak symbols. */
2561 bfd_boolean
2562 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2563 struct bfd_link_info *info,
2564 bfd_boolean local_protected)
2566 /* If it's a local sym, of course we resolve locally. */
2567 if (h == NULL)
2568 return TRUE;
2570 /* Common symbols that become definitions don't get the DEF_REGULAR
2571 flag set, so test it first, and don't bail out. */
2572 if (ELF_COMMON_DEF_P (h))
2573 /* Do nothing. */;
2574 /* If we don't have a definition in a regular file, then we can't
2575 resolve locally. The sym is either undefined or dynamic. */
2576 else if (!h->def_regular)
2577 return FALSE;
2579 /* Forced local symbols resolve locally. */
2580 if (h->forced_local)
2581 return TRUE;
2583 /* As do non-dynamic symbols. */
2584 if (h->dynindx == -1)
2585 return TRUE;
2587 /* At this point, we know the symbol is defined and dynamic. In an
2588 executable it must resolve locally, likewise when building symbolic
2589 shared libraries. */
2590 if (info->executable || info->symbolic)
2591 return TRUE;
2593 /* Now deal with defined dynamic symbols in shared libraries. Ones
2594 with default visibility might not resolve locally. */
2595 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2596 return FALSE;
2598 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2599 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2600 return TRUE;
2602 /* STV_PROTECTED non-function symbols are local. */
2603 if (h->type != STT_FUNC)
2604 return TRUE;
2606 /* Function pointer equality tests may require that STV_PROTECTED
2607 symbols be treated as dynamic symbols, even when we know that the
2608 dynamic linker will resolve them locally. */
2609 return local_protected;
2612 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2613 aligned. Returns the first TLS output section. */
2615 struct bfd_section *
2616 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2618 struct bfd_section *sec, *tls;
2619 unsigned int align = 0;
2621 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2622 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2623 break;
2624 tls = sec;
2626 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2627 if (sec->alignment_power > align)
2628 align = sec->alignment_power;
2630 elf_hash_table (info)->tls_sec = tls;
2632 /* Ensure the alignment of the first section is the largest alignment,
2633 so that the tls segment starts aligned. */
2634 if (tls != NULL)
2635 tls->alignment_power = align;
2637 return tls;
2640 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2641 static bfd_boolean
2642 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2643 Elf_Internal_Sym *sym)
2645 /* Local symbols do not count, but target specific ones might. */
2646 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2647 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2648 return FALSE;
2650 /* Function symbols do not count. */
2651 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2652 return FALSE;
2654 /* If the section is undefined, then so is the symbol. */
2655 if (sym->st_shndx == SHN_UNDEF)
2656 return FALSE;
2658 /* If the symbol is defined in the common section, then
2659 it is a common definition and so does not count. */
2660 if (sym->st_shndx == SHN_COMMON)
2661 return FALSE;
2663 /* If the symbol is in a target specific section then we
2664 must rely upon the backend to tell us what it is. */
2665 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2666 /* FIXME - this function is not coded yet:
2668 return _bfd_is_global_symbol_definition (abfd, sym);
2670 Instead for now assume that the definition is not global,
2671 Even if this is wrong, at least the linker will behave
2672 in the same way that it used to do. */
2673 return FALSE;
2675 return TRUE;
2678 /* Search the symbol table of the archive element of the archive ABFD
2679 whose archive map contains a mention of SYMDEF, and determine if
2680 the symbol is defined in this element. */
2681 static bfd_boolean
2682 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2684 Elf_Internal_Shdr * hdr;
2685 bfd_size_type symcount;
2686 bfd_size_type extsymcount;
2687 bfd_size_type extsymoff;
2688 Elf_Internal_Sym *isymbuf;
2689 Elf_Internal_Sym *isym;
2690 Elf_Internal_Sym *isymend;
2691 bfd_boolean result;
2693 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2694 if (abfd == NULL)
2695 return FALSE;
2697 if (! bfd_check_format (abfd, bfd_object))
2698 return FALSE;
2700 /* If we have already included the element containing this symbol in the
2701 link then we do not need to include it again. Just claim that any symbol
2702 it contains is not a definition, so that our caller will not decide to
2703 (re)include this element. */
2704 if (abfd->archive_pass)
2705 return FALSE;
2707 /* Select the appropriate symbol table. */
2708 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2709 hdr = &elf_tdata (abfd)->symtab_hdr;
2710 else
2711 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2713 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2715 /* The sh_info field of the symtab header tells us where the
2716 external symbols start. We don't care about the local symbols. */
2717 if (elf_bad_symtab (abfd))
2719 extsymcount = symcount;
2720 extsymoff = 0;
2722 else
2724 extsymcount = symcount - hdr->sh_info;
2725 extsymoff = hdr->sh_info;
2728 if (extsymcount == 0)
2729 return FALSE;
2731 /* Read in the symbol table. */
2732 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2733 NULL, NULL, NULL);
2734 if (isymbuf == NULL)
2735 return FALSE;
2737 /* Scan the symbol table looking for SYMDEF. */
2738 result = FALSE;
2739 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2741 const char *name;
2743 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2744 isym->st_name);
2745 if (name == NULL)
2746 break;
2748 if (strcmp (name, symdef->name) == 0)
2750 result = is_global_data_symbol_definition (abfd, isym);
2751 break;
2755 free (isymbuf);
2757 return result;
2760 /* Add an entry to the .dynamic table. */
2762 bfd_boolean
2763 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2764 bfd_vma tag,
2765 bfd_vma val)
2767 struct elf_link_hash_table *hash_table;
2768 const struct elf_backend_data *bed;
2769 asection *s;
2770 bfd_size_type newsize;
2771 bfd_byte *newcontents;
2772 Elf_Internal_Dyn dyn;
2774 hash_table = elf_hash_table (info);
2775 if (! is_elf_hash_table (hash_table))
2776 return FALSE;
2778 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2779 _bfd_error_handler
2780 (_("warning: creating a DT_TEXTREL in a shared object."));
2782 bed = get_elf_backend_data (hash_table->dynobj);
2783 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2784 BFD_ASSERT (s != NULL);
2786 newsize = s->size + bed->s->sizeof_dyn;
2787 newcontents = bfd_realloc (s->contents, newsize);
2788 if (newcontents == NULL)
2789 return FALSE;
2791 dyn.d_tag = tag;
2792 dyn.d_un.d_val = val;
2793 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2795 s->size = newsize;
2796 s->contents = newcontents;
2798 return TRUE;
2801 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2802 otherwise just check whether one already exists. Returns -1 on error,
2803 1 if a DT_NEEDED tag already exists, and 0 on success. */
2805 static int
2806 elf_add_dt_needed_tag (bfd *abfd,
2807 struct bfd_link_info *info,
2808 const char *soname,
2809 bfd_boolean do_it)
2811 struct elf_link_hash_table *hash_table;
2812 bfd_size_type oldsize;
2813 bfd_size_type strindex;
2815 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2816 return -1;
2818 hash_table = elf_hash_table (info);
2819 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2820 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2821 if (strindex == (bfd_size_type) -1)
2822 return -1;
2824 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2826 asection *sdyn;
2827 const struct elf_backend_data *bed;
2828 bfd_byte *extdyn;
2830 bed = get_elf_backend_data (hash_table->dynobj);
2831 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2832 if (sdyn != NULL)
2833 for (extdyn = sdyn->contents;
2834 extdyn < sdyn->contents + sdyn->size;
2835 extdyn += bed->s->sizeof_dyn)
2837 Elf_Internal_Dyn dyn;
2839 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2840 if (dyn.d_tag == DT_NEEDED
2841 && dyn.d_un.d_val == strindex)
2843 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2844 return 1;
2849 if (do_it)
2851 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2852 return -1;
2854 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2855 return -1;
2857 else
2858 /* We were just checking for existence of the tag. */
2859 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2861 return 0;
2864 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2865 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2866 references from regular objects to these symbols.
2868 ??? Should we do something about references from other dynamic
2869 obects? If not, we potentially lose some warnings about undefined
2870 symbols. But how can we recover the initial undefined / undefweak
2871 state? */
2873 struct elf_smash_syms_data
2875 bfd *not_needed;
2876 struct elf_link_hash_table *htab;
2877 bfd_boolean twiddled;
2880 static bfd_boolean
2881 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2883 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2884 struct bfd_link_hash_entry *bh;
2886 switch (h->root.type)
2888 default:
2889 case bfd_link_hash_new:
2890 return TRUE;
2892 case bfd_link_hash_undefined:
2893 if (h->root.u.undef.abfd != inf->not_needed)
2894 return TRUE;
2895 if (h->root.u.undef.weak != NULL
2896 && h->root.u.undef.weak != inf->not_needed)
2898 /* Symbol was undefweak in u.undef.weak bfd, and has become
2899 undefined in as-needed lib. Restore weak. */
2900 h->root.type = bfd_link_hash_undefweak;
2901 h->root.u.undef.abfd = h->root.u.undef.weak;
2902 if (h->root.u.undef.next != NULL
2903 || inf->htab->root.undefs_tail == &h->root)
2904 inf->twiddled = TRUE;
2905 return TRUE;
2907 break;
2909 case bfd_link_hash_undefweak:
2910 if (h->root.u.undef.abfd != inf->not_needed)
2911 return TRUE;
2912 break;
2914 case bfd_link_hash_defined:
2915 case bfd_link_hash_defweak:
2916 if (h->root.u.def.section->owner != inf->not_needed)
2917 return TRUE;
2918 break;
2920 case bfd_link_hash_common:
2921 if (h->root.u.c.p->section->owner != inf->not_needed)
2922 return TRUE;
2923 break;
2925 case bfd_link_hash_warning:
2926 case bfd_link_hash_indirect:
2927 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2928 if (h->root.u.i.link->type != bfd_link_hash_new)
2929 return TRUE;
2930 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2931 return TRUE;
2932 break;
2935 /* There is no way we can undo symbol table state from defined or
2936 defweak back to undefined. */
2937 if (h->ref_regular)
2938 abort ();
2940 /* Set sym back to newly created state, but keep undef.next if it is
2941 being used as a list pointer. */
2942 bh = h->root.u.undef.next;
2943 if (bh == &h->root)
2944 bh = NULL;
2945 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2946 inf->twiddled = TRUE;
2947 (*inf->htab->root.table.newfunc) (&h->root.root,
2948 &inf->htab->root.table,
2949 h->root.root.string);
2950 h->root.u.undef.next = bh;
2951 h->root.u.undef.abfd = inf->not_needed;
2952 h->non_elf = 0;
2953 return TRUE;
2956 /* Sort symbol by value and section. */
2957 static int
2958 elf_sort_symbol (const void *arg1, const void *arg2)
2960 const struct elf_link_hash_entry *h1;
2961 const struct elf_link_hash_entry *h2;
2962 bfd_signed_vma vdiff;
2964 h1 = *(const struct elf_link_hash_entry **) arg1;
2965 h2 = *(const struct elf_link_hash_entry **) arg2;
2966 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2967 if (vdiff != 0)
2968 return vdiff > 0 ? 1 : -1;
2969 else
2971 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2972 if (sdiff != 0)
2973 return sdiff > 0 ? 1 : -1;
2975 return 0;
2978 /* This function is used to adjust offsets into .dynstr for
2979 dynamic symbols. This is called via elf_link_hash_traverse. */
2981 static bfd_boolean
2982 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2984 struct elf_strtab_hash *dynstr = data;
2986 if (h->root.type == bfd_link_hash_warning)
2987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2989 if (h->dynindx != -1)
2990 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2991 return TRUE;
2994 /* Assign string offsets in .dynstr, update all structures referencing
2995 them. */
2997 static bfd_boolean
2998 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3000 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3001 struct elf_link_local_dynamic_entry *entry;
3002 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3003 bfd *dynobj = hash_table->dynobj;
3004 asection *sdyn;
3005 bfd_size_type size;
3006 const struct elf_backend_data *bed;
3007 bfd_byte *extdyn;
3009 _bfd_elf_strtab_finalize (dynstr);
3010 size = _bfd_elf_strtab_size (dynstr);
3012 bed = get_elf_backend_data (dynobj);
3013 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3014 BFD_ASSERT (sdyn != NULL);
3016 /* Update all .dynamic entries referencing .dynstr strings. */
3017 for (extdyn = sdyn->contents;
3018 extdyn < sdyn->contents + sdyn->size;
3019 extdyn += bed->s->sizeof_dyn)
3021 Elf_Internal_Dyn dyn;
3023 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3024 switch (dyn.d_tag)
3026 case DT_STRSZ:
3027 dyn.d_un.d_val = size;
3028 break;
3029 case DT_NEEDED:
3030 case DT_SONAME:
3031 case DT_RPATH:
3032 case DT_RUNPATH:
3033 case DT_FILTER:
3034 case DT_AUXILIARY:
3035 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3036 break;
3037 default:
3038 continue;
3040 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3043 /* Now update local dynamic symbols. */
3044 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3045 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3046 entry->isym.st_name);
3048 /* And the rest of dynamic symbols. */
3049 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3051 /* Adjust version definitions. */
3052 if (elf_tdata (output_bfd)->cverdefs)
3054 asection *s;
3055 bfd_byte *p;
3056 bfd_size_type i;
3057 Elf_Internal_Verdef def;
3058 Elf_Internal_Verdaux defaux;
3060 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3061 p = s->contents;
3064 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3065 &def);
3066 p += sizeof (Elf_External_Verdef);
3067 if (def.vd_aux != sizeof (Elf_External_Verdef))
3068 continue;
3069 for (i = 0; i < def.vd_cnt; ++i)
3071 _bfd_elf_swap_verdaux_in (output_bfd,
3072 (Elf_External_Verdaux *) p, &defaux);
3073 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3074 defaux.vda_name);
3075 _bfd_elf_swap_verdaux_out (output_bfd,
3076 &defaux, (Elf_External_Verdaux *) p);
3077 p += sizeof (Elf_External_Verdaux);
3080 while (def.vd_next);
3083 /* Adjust version references. */
3084 if (elf_tdata (output_bfd)->verref)
3086 asection *s;
3087 bfd_byte *p;
3088 bfd_size_type i;
3089 Elf_Internal_Verneed need;
3090 Elf_Internal_Vernaux needaux;
3092 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3093 p = s->contents;
3096 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3097 &need);
3098 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3099 _bfd_elf_swap_verneed_out (output_bfd, &need,
3100 (Elf_External_Verneed *) p);
3101 p += sizeof (Elf_External_Verneed);
3102 for (i = 0; i < need.vn_cnt; ++i)
3104 _bfd_elf_swap_vernaux_in (output_bfd,
3105 (Elf_External_Vernaux *) p, &needaux);
3106 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3107 needaux.vna_name);
3108 _bfd_elf_swap_vernaux_out (output_bfd,
3109 &needaux,
3110 (Elf_External_Vernaux *) p);
3111 p += sizeof (Elf_External_Vernaux);
3114 while (need.vn_next);
3117 return TRUE;
3120 /* Add symbols from an ELF object file to the linker hash table. */
3122 static bfd_boolean
3123 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3125 bfd_boolean (*add_symbol_hook)
3126 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3127 const char **, flagword *, asection **, bfd_vma *);
3128 bfd_boolean (*check_relocs)
3129 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3130 bfd_boolean (*check_directives)
3131 (bfd *, struct bfd_link_info *);
3132 bfd_boolean collect;
3133 Elf_Internal_Shdr *hdr;
3134 bfd_size_type symcount;
3135 bfd_size_type extsymcount;
3136 bfd_size_type extsymoff;
3137 struct elf_link_hash_entry **sym_hash;
3138 bfd_boolean dynamic;
3139 Elf_External_Versym *extversym = NULL;
3140 Elf_External_Versym *ever;
3141 struct elf_link_hash_entry *weaks;
3142 struct elf_link_hash_entry **nondeflt_vers = NULL;
3143 bfd_size_type nondeflt_vers_cnt = 0;
3144 Elf_Internal_Sym *isymbuf = NULL;
3145 Elf_Internal_Sym *isym;
3146 Elf_Internal_Sym *isymend;
3147 const struct elf_backend_data *bed;
3148 bfd_boolean add_needed;
3149 struct elf_link_hash_table * hash_table;
3150 bfd_size_type amt;
3152 hash_table = elf_hash_table (info);
3154 bed = get_elf_backend_data (abfd);
3155 add_symbol_hook = bed->elf_add_symbol_hook;
3156 collect = bed->collect;
3158 if ((abfd->flags & DYNAMIC) == 0)
3159 dynamic = FALSE;
3160 else
3162 dynamic = TRUE;
3164 /* You can't use -r against a dynamic object. Also, there's no
3165 hope of using a dynamic object which does not exactly match
3166 the format of the output file. */
3167 if (info->relocatable
3168 || !is_elf_hash_table (hash_table)
3169 || hash_table->root.creator != abfd->xvec)
3171 if (info->relocatable)
3172 bfd_set_error (bfd_error_invalid_operation);
3173 else
3174 bfd_set_error (bfd_error_wrong_format);
3175 goto error_return;
3179 /* As a GNU extension, any input sections which are named
3180 .gnu.warning.SYMBOL are treated as warning symbols for the given
3181 symbol. This differs from .gnu.warning sections, which generate
3182 warnings when they are included in an output file. */
3183 if (info->executable)
3185 asection *s;
3187 for (s = abfd->sections; s != NULL; s = s->next)
3189 const char *name;
3191 name = bfd_get_section_name (abfd, s);
3192 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3194 char *msg;
3195 bfd_size_type sz;
3197 name += sizeof ".gnu.warning." - 1;
3199 /* If this is a shared object, then look up the symbol
3200 in the hash table. If it is there, and it is already
3201 been defined, then we will not be using the entry
3202 from this shared object, so we don't need to warn.
3203 FIXME: If we see the definition in a regular object
3204 later on, we will warn, but we shouldn't. The only
3205 fix is to keep track of what warnings we are supposed
3206 to emit, and then handle them all at the end of the
3207 link. */
3208 if (dynamic)
3210 struct elf_link_hash_entry *h;
3212 h = elf_link_hash_lookup (hash_table, name,
3213 FALSE, FALSE, TRUE);
3215 /* FIXME: What about bfd_link_hash_common? */
3216 if (h != NULL
3217 && (h->root.type == bfd_link_hash_defined
3218 || h->root.type == bfd_link_hash_defweak))
3220 /* We don't want to issue this warning. Clobber
3221 the section size so that the warning does not
3222 get copied into the output file. */
3223 s->size = 0;
3224 continue;
3228 sz = s->size;
3229 msg = bfd_alloc (abfd, sz + 1);
3230 if (msg == NULL)
3231 goto error_return;
3233 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3234 goto error_return;
3236 msg[sz] = '\0';
3238 if (! (_bfd_generic_link_add_one_symbol
3239 (info, abfd, name, BSF_WARNING, s, 0, msg,
3240 FALSE, collect, NULL)))
3241 goto error_return;
3243 if (! info->relocatable)
3245 /* Clobber the section size so that the warning does
3246 not get copied into the output file. */
3247 s->size = 0;
3249 /* Also set SEC_EXCLUDE, so that symbols defined in
3250 the warning section don't get copied to the output. */
3251 s->flags |= SEC_EXCLUDE;
3257 add_needed = TRUE;
3258 if (! dynamic)
3260 /* If we are creating a shared library, create all the dynamic
3261 sections immediately. We need to attach them to something,
3262 so we attach them to this BFD, provided it is the right
3263 format. FIXME: If there are no input BFD's of the same
3264 format as the output, we can't make a shared library. */
3265 if (info->shared
3266 && is_elf_hash_table (hash_table)
3267 && hash_table->root.creator == abfd->xvec
3268 && ! hash_table->dynamic_sections_created)
3270 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3271 goto error_return;
3274 else if (!is_elf_hash_table (hash_table))
3275 goto error_return;
3276 else
3278 asection *s;
3279 const char *soname = NULL;
3280 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3281 int ret;
3283 /* ld --just-symbols and dynamic objects don't mix very well.
3284 Test for --just-symbols by looking at info set up by
3285 _bfd_elf_link_just_syms. */
3286 if ((s = abfd->sections) != NULL
3287 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3288 goto error_return;
3290 /* If this dynamic lib was specified on the command line with
3291 --as-needed in effect, then we don't want to add a DT_NEEDED
3292 tag unless the lib is actually used. Similary for libs brought
3293 in by another lib's DT_NEEDED. When --no-add-needed is used
3294 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3295 any dynamic library in DT_NEEDED tags in the dynamic lib at
3296 all. */
3297 add_needed = (elf_dyn_lib_class (abfd)
3298 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3299 | DYN_NO_NEEDED)) == 0;
3301 s = bfd_get_section_by_name (abfd, ".dynamic");
3302 if (s != NULL)
3304 bfd_byte *dynbuf;
3305 bfd_byte *extdyn;
3306 int elfsec;
3307 unsigned long shlink;
3309 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3310 goto error_free_dyn;
3312 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3313 if (elfsec == -1)
3314 goto error_free_dyn;
3315 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3317 for (extdyn = dynbuf;
3318 extdyn < dynbuf + s->size;
3319 extdyn += bed->s->sizeof_dyn)
3321 Elf_Internal_Dyn dyn;
3323 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3324 if (dyn.d_tag == DT_SONAME)
3326 unsigned int tagv = dyn.d_un.d_val;
3327 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3328 if (soname == NULL)
3329 goto error_free_dyn;
3331 if (dyn.d_tag == DT_NEEDED)
3333 struct bfd_link_needed_list *n, **pn;
3334 char *fnm, *anm;
3335 unsigned int tagv = dyn.d_un.d_val;
3337 amt = sizeof (struct bfd_link_needed_list);
3338 n = bfd_alloc (abfd, amt);
3339 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3340 if (n == NULL || fnm == NULL)
3341 goto error_free_dyn;
3342 amt = strlen (fnm) + 1;
3343 anm = bfd_alloc (abfd, amt);
3344 if (anm == NULL)
3345 goto error_free_dyn;
3346 memcpy (anm, fnm, amt);
3347 n->name = anm;
3348 n->by = abfd;
3349 n->next = NULL;
3350 for (pn = & hash_table->needed;
3351 *pn != NULL;
3352 pn = &(*pn)->next)
3354 *pn = n;
3356 if (dyn.d_tag == DT_RUNPATH)
3358 struct bfd_link_needed_list *n, **pn;
3359 char *fnm, *anm;
3360 unsigned int tagv = dyn.d_un.d_val;
3362 amt = sizeof (struct bfd_link_needed_list);
3363 n = bfd_alloc (abfd, amt);
3364 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3365 if (n == NULL || fnm == NULL)
3366 goto error_free_dyn;
3367 amt = strlen (fnm) + 1;
3368 anm = bfd_alloc (abfd, amt);
3369 if (anm == NULL)
3370 goto error_free_dyn;
3371 memcpy (anm, fnm, amt);
3372 n->name = anm;
3373 n->by = abfd;
3374 n->next = NULL;
3375 for (pn = & runpath;
3376 *pn != NULL;
3377 pn = &(*pn)->next)
3379 *pn = n;
3381 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3382 if (!runpath && dyn.d_tag == DT_RPATH)
3384 struct bfd_link_needed_list *n, **pn;
3385 char *fnm, *anm;
3386 unsigned int tagv = dyn.d_un.d_val;
3388 amt = sizeof (struct bfd_link_needed_list);
3389 n = bfd_alloc (abfd, amt);
3390 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3391 if (n == NULL || fnm == NULL)
3392 goto error_free_dyn;
3393 amt = strlen (fnm) + 1;
3394 anm = bfd_alloc (abfd, amt);
3395 if (anm == NULL)
3397 error_free_dyn:
3398 free (dynbuf);
3399 goto error_return;
3401 memcpy (anm, fnm, amt);
3402 n->name = anm;
3403 n->by = abfd;
3404 n->next = NULL;
3405 for (pn = & rpath;
3406 *pn != NULL;
3407 pn = &(*pn)->next)
3409 *pn = n;
3413 free (dynbuf);
3416 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3417 frees all more recently bfd_alloc'd blocks as well. */
3418 if (runpath)
3419 rpath = runpath;
3421 if (rpath)
3423 struct bfd_link_needed_list **pn;
3424 for (pn = & hash_table->runpath;
3425 *pn != NULL;
3426 pn = &(*pn)->next)
3428 *pn = rpath;
3431 /* We do not want to include any of the sections in a dynamic
3432 object in the output file. We hack by simply clobbering the
3433 list of sections in the BFD. This could be handled more
3434 cleanly by, say, a new section flag; the existing
3435 SEC_NEVER_LOAD flag is not the one we want, because that one
3436 still implies that the section takes up space in the output
3437 file. */
3438 bfd_section_list_clear (abfd);
3440 /* Find the name to use in a DT_NEEDED entry that refers to this
3441 object. If the object has a DT_SONAME entry, we use it.
3442 Otherwise, if the generic linker stuck something in
3443 elf_dt_name, we use that. Otherwise, we just use the file
3444 name. */
3445 if (soname == NULL || *soname == '\0')
3447 soname = elf_dt_name (abfd);
3448 if (soname == NULL || *soname == '\0')
3449 soname = bfd_get_filename (abfd);
3452 /* Save the SONAME because sometimes the linker emulation code
3453 will need to know it. */
3454 elf_dt_name (abfd) = soname;
3456 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3457 if (ret < 0)
3458 goto error_return;
3460 /* If we have already included this dynamic object in the
3461 link, just ignore it. There is no reason to include a
3462 particular dynamic object more than once. */
3463 if (ret > 0)
3464 return TRUE;
3467 /* If this is a dynamic object, we always link against the .dynsym
3468 symbol table, not the .symtab symbol table. The dynamic linker
3469 will only see the .dynsym symbol table, so there is no reason to
3470 look at .symtab for a dynamic object. */
3472 if (! dynamic || elf_dynsymtab (abfd) == 0)
3473 hdr = &elf_tdata (abfd)->symtab_hdr;
3474 else
3475 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3477 symcount = hdr->sh_size / bed->s->sizeof_sym;
3479 /* The sh_info field of the symtab header tells us where the
3480 external symbols start. We don't care about the local symbols at
3481 this point. */
3482 if (elf_bad_symtab (abfd))
3484 extsymcount = symcount;
3485 extsymoff = 0;
3487 else
3489 extsymcount = symcount - hdr->sh_info;
3490 extsymoff = hdr->sh_info;
3493 sym_hash = NULL;
3494 if (extsymcount != 0)
3496 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3497 NULL, NULL, NULL);
3498 if (isymbuf == NULL)
3499 goto error_return;
3501 /* We store a pointer to the hash table entry for each external
3502 symbol. */
3503 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3504 sym_hash = bfd_alloc (abfd, amt);
3505 if (sym_hash == NULL)
3506 goto error_free_sym;
3507 elf_sym_hashes (abfd) = sym_hash;
3510 if (dynamic)
3512 /* Read in any version definitions. */
3513 if (!_bfd_elf_slurp_version_tables (abfd,
3514 info->default_imported_symver))
3515 goto error_free_sym;
3517 /* Read in the symbol versions, but don't bother to convert them
3518 to internal format. */
3519 if (elf_dynversym (abfd) != 0)
3521 Elf_Internal_Shdr *versymhdr;
3523 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3524 extversym = bfd_malloc (versymhdr->sh_size);
3525 if (extversym == NULL)
3526 goto error_free_sym;
3527 amt = versymhdr->sh_size;
3528 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3529 || bfd_bread (extversym, amt, abfd) != amt)
3530 goto error_free_vers;
3534 weaks = NULL;
3536 ever = extversym != NULL ? extversym + extsymoff : NULL;
3537 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3538 isym < isymend;
3539 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3541 int bind;
3542 bfd_vma value;
3543 asection *sec, *new_sec;
3544 flagword flags;
3545 const char *name;
3546 struct elf_link_hash_entry *h;
3547 bfd_boolean definition;
3548 bfd_boolean size_change_ok;
3549 bfd_boolean type_change_ok;
3550 bfd_boolean new_weakdef;
3551 bfd_boolean override;
3552 unsigned int old_alignment;
3553 bfd *old_bfd;
3555 override = FALSE;
3557 flags = BSF_NO_FLAGS;
3558 sec = NULL;
3559 value = isym->st_value;
3560 *sym_hash = NULL;
3562 bind = ELF_ST_BIND (isym->st_info);
3563 if (bind == STB_LOCAL)
3565 /* This should be impossible, since ELF requires that all
3566 global symbols follow all local symbols, and that sh_info
3567 point to the first global symbol. Unfortunately, Irix 5
3568 screws this up. */
3569 continue;
3571 else if (bind == STB_GLOBAL)
3573 if (isym->st_shndx != SHN_UNDEF
3574 && isym->st_shndx != SHN_COMMON)
3575 flags = BSF_GLOBAL;
3577 else if (bind == STB_WEAK)
3578 flags = BSF_WEAK;
3579 else
3581 /* Leave it up to the processor backend. */
3584 if (isym->st_shndx == SHN_UNDEF)
3585 sec = bfd_und_section_ptr;
3586 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3588 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3589 if (sec == NULL)
3590 sec = bfd_abs_section_ptr;
3591 else if (sec->kept_section)
3593 /* Symbols from discarded section are undefined, and have
3594 default visibility. */
3595 sec = bfd_und_section_ptr;
3596 isym->st_shndx = SHN_UNDEF;
3597 isym->st_other = STV_DEFAULT
3598 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3600 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3601 value -= sec->vma;
3603 else if (isym->st_shndx == SHN_ABS)
3604 sec = bfd_abs_section_ptr;
3605 else if (isym->st_shndx == SHN_COMMON)
3607 sec = bfd_com_section_ptr;
3608 /* What ELF calls the size we call the value. What ELF
3609 calls the value we call the alignment. */
3610 value = isym->st_size;
3612 else
3614 /* Leave it up to the processor backend. */
3617 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3618 isym->st_name);
3619 if (name == NULL)
3620 goto error_free_vers;
3622 if (isym->st_shndx == SHN_COMMON
3623 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3625 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3627 if (tcomm == NULL)
3629 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3630 (SEC_ALLOC
3631 | SEC_IS_COMMON
3632 | SEC_LINKER_CREATED
3633 | SEC_THREAD_LOCAL));
3634 if (tcomm == NULL)
3635 goto error_free_vers;
3637 sec = tcomm;
3639 else if (add_symbol_hook)
3641 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3642 &value))
3643 goto error_free_vers;
3645 /* The hook function sets the name to NULL if this symbol
3646 should be skipped for some reason. */
3647 if (name == NULL)
3648 continue;
3651 /* Sanity check that all possibilities were handled. */
3652 if (sec == NULL)
3654 bfd_set_error (bfd_error_bad_value);
3655 goto error_free_vers;
3658 if (bfd_is_und_section (sec)
3659 || bfd_is_com_section (sec))
3660 definition = FALSE;
3661 else
3662 definition = TRUE;
3664 size_change_ok = FALSE;
3665 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3666 old_alignment = 0;
3667 old_bfd = NULL;
3668 new_sec = sec;
3670 if (is_elf_hash_table (hash_table))
3672 Elf_Internal_Versym iver;
3673 unsigned int vernum = 0;
3674 bfd_boolean skip;
3676 if (ever == NULL)
3678 if (info->default_imported_symver)
3679 /* Use the default symbol version created earlier. */
3680 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3681 else
3682 iver.vs_vers = 0;
3684 else
3685 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3687 vernum = iver.vs_vers & VERSYM_VERSION;
3689 /* If this is a hidden symbol, or if it is not version
3690 1, we append the version name to the symbol name.
3691 However, we do not modify a non-hidden absolute
3692 symbol, because it might be the version symbol
3693 itself. FIXME: What if it isn't? */
3694 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3695 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3697 const char *verstr;
3698 size_t namelen, verlen, newlen;
3699 char *newname, *p;
3701 if (isym->st_shndx != SHN_UNDEF)
3703 if (vernum > elf_tdata (abfd)->cverdefs)
3704 verstr = NULL;
3705 else if (vernum > 1)
3706 verstr =
3707 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3708 else
3709 verstr = "";
3711 if (verstr == NULL)
3713 (*_bfd_error_handler)
3714 (_("%B: %s: invalid version %u (max %d)"),
3715 abfd, name, vernum,
3716 elf_tdata (abfd)->cverdefs);
3717 bfd_set_error (bfd_error_bad_value);
3718 goto error_free_vers;
3721 else
3723 /* We cannot simply test for the number of
3724 entries in the VERNEED section since the
3725 numbers for the needed versions do not start
3726 at 0. */
3727 Elf_Internal_Verneed *t;
3729 verstr = NULL;
3730 for (t = elf_tdata (abfd)->verref;
3731 t != NULL;
3732 t = t->vn_nextref)
3734 Elf_Internal_Vernaux *a;
3736 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3738 if (a->vna_other == vernum)
3740 verstr = a->vna_nodename;
3741 break;
3744 if (a != NULL)
3745 break;
3747 if (verstr == NULL)
3749 (*_bfd_error_handler)
3750 (_("%B: %s: invalid needed version %d"),
3751 abfd, name, vernum);
3752 bfd_set_error (bfd_error_bad_value);
3753 goto error_free_vers;
3757 namelen = strlen (name);
3758 verlen = strlen (verstr);
3759 newlen = namelen + verlen + 2;
3760 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3761 && isym->st_shndx != SHN_UNDEF)
3762 ++newlen;
3764 newname = bfd_alloc (abfd, newlen);
3765 if (newname == NULL)
3766 goto error_free_vers;
3767 memcpy (newname, name, namelen);
3768 p = newname + namelen;
3769 *p++ = ELF_VER_CHR;
3770 /* If this is a defined non-hidden version symbol,
3771 we add another @ to the name. This indicates the
3772 default version of the symbol. */
3773 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3774 && isym->st_shndx != SHN_UNDEF)
3775 *p++ = ELF_VER_CHR;
3776 memcpy (p, verstr, verlen + 1);
3778 name = newname;
3781 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3782 &value, &old_alignment,
3783 sym_hash, &skip, &override,
3784 &type_change_ok, &size_change_ok))
3785 goto error_free_vers;
3787 if (skip)
3788 continue;
3790 if (override)
3791 definition = FALSE;
3793 h = *sym_hash;
3794 while (h->root.type == bfd_link_hash_indirect
3795 || h->root.type == bfd_link_hash_warning)
3796 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3798 /* Remember the old alignment if this is a common symbol, so
3799 that we don't reduce the alignment later on. We can't
3800 check later, because _bfd_generic_link_add_one_symbol
3801 will set a default for the alignment which we want to
3802 override. We also remember the old bfd where the existing
3803 definition comes from. */
3804 switch (h->root.type)
3806 default:
3807 break;
3809 case bfd_link_hash_defined:
3810 case bfd_link_hash_defweak:
3811 old_bfd = h->root.u.def.section->owner;
3812 break;
3814 case bfd_link_hash_common:
3815 old_bfd = h->root.u.c.p->section->owner;
3816 old_alignment = h->root.u.c.p->alignment_power;
3817 break;
3820 if (elf_tdata (abfd)->verdef != NULL
3821 && ! override
3822 && vernum > 1
3823 && definition)
3824 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3827 if (! (_bfd_generic_link_add_one_symbol
3828 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3829 (struct bfd_link_hash_entry **) sym_hash)))
3830 goto error_free_vers;
3832 h = *sym_hash;
3833 while (h->root.type == bfd_link_hash_indirect
3834 || h->root.type == bfd_link_hash_warning)
3835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3836 *sym_hash = h;
3838 new_weakdef = FALSE;
3839 if (dynamic
3840 && definition
3841 && (flags & BSF_WEAK) != 0
3842 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3843 && is_elf_hash_table (hash_table)
3844 && h->u.weakdef == NULL)
3846 /* Keep a list of all weak defined non function symbols from
3847 a dynamic object, using the weakdef field. Later in this
3848 function we will set the weakdef field to the correct
3849 value. We only put non-function symbols from dynamic
3850 objects on this list, because that happens to be the only
3851 time we need to know the normal symbol corresponding to a
3852 weak symbol, and the information is time consuming to
3853 figure out. If the weakdef field is not already NULL,
3854 then this symbol was already defined by some previous
3855 dynamic object, and we will be using that previous
3856 definition anyhow. */
3858 h->u.weakdef = weaks;
3859 weaks = h;
3860 new_weakdef = TRUE;
3863 /* Set the alignment of a common symbol. */
3864 if ((isym->st_shndx == SHN_COMMON
3865 || bfd_is_com_section (sec))
3866 && h->root.type == bfd_link_hash_common)
3868 unsigned int align;
3870 if (isym->st_shndx == SHN_COMMON)
3871 align = bfd_log2 (isym->st_value);
3872 else
3874 /* The new symbol is a common symbol in a shared object.
3875 We need to get the alignment from the section. */
3876 align = new_sec->alignment_power;
3878 if (align > old_alignment
3879 /* Permit an alignment power of zero if an alignment of one
3880 is specified and no other alignments have been specified. */
3881 || (isym->st_value == 1 && old_alignment == 0))
3882 h->root.u.c.p->alignment_power = align;
3883 else
3884 h->root.u.c.p->alignment_power = old_alignment;
3887 if (is_elf_hash_table (hash_table))
3889 bfd_boolean dynsym;
3891 /* Check the alignment when a common symbol is involved. This
3892 can change when a common symbol is overridden by a normal
3893 definition or a common symbol is ignored due to the old
3894 normal definition. We need to make sure the maximum
3895 alignment is maintained. */
3896 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3897 && h->root.type != bfd_link_hash_common)
3899 unsigned int common_align;
3900 unsigned int normal_align;
3901 unsigned int symbol_align;
3902 bfd *normal_bfd;
3903 bfd *common_bfd;
3905 symbol_align = ffs (h->root.u.def.value) - 1;
3906 if (h->root.u.def.section->owner != NULL
3907 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3909 normal_align = h->root.u.def.section->alignment_power;
3910 if (normal_align > symbol_align)
3911 normal_align = symbol_align;
3913 else
3914 normal_align = symbol_align;
3916 if (old_alignment)
3918 common_align = old_alignment;
3919 common_bfd = old_bfd;
3920 normal_bfd = abfd;
3922 else
3924 common_align = bfd_log2 (isym->st_value);
3925 common_bfd = abfd;
3926 normal_bfd = old_bfd;
3929 if (normal_align < common_align)
3930 (*_bfd_error_handler)
3931 (_("Warning: alignment %u of symbol `%s' in %B"
3932 " is smaller than %u in %B"),
3933 normal_bfd, common_bfd,
3934 1 << normal_align, name, 1 << common_align);
3937 /* Remember the symbol size and type. */
3938 if (isym->st_size != 0
3939 && (definition || h->size == 0))
3941 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3942 (*_bfd_error_handler)
3943 (_("Warning: size of symbol `%s' changed"
3944 " from %lu in %B to %lu in %B"),
3945 old_bfd, abfd,
3946 name, (unsigned long) h->size,
3947 (unsigned long) isym->st_size);
3949 h->size = isym->st_size;
3952 /* If this is a common symbol, then we always want H->SIZE
3953 to be the size of the common symbol. The code just above
3954 won't fix the size if a common symbol becomes larger. We
3955 don't warn about a size change here, because that is
3956 covered by --warn-common. */
3957 if (h->root.type == bfd_link_hash_common)
3958 h->size = h->root.u.c.size;
3960 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3961 && (definition || h->type == STT_NOTYPE))
3963 if (h->type != STT_NOTYPE
3964 && h->type != ELF_ST_TYPE (isym->st_info)
3965 && ! type_change_ok)
3966 (*_bfd_error_handler)
3967 (_("Warning: type of symbol `%s' changed"
3968 " from %d to %d in %B"),
3969 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3971 h->type = ELF_ST_TYPE (isym->st_info);
3974 /* If st_other has a processor-specific meaning, specific
3975 code might be needed here. We never merge the visibility
3976 attribute with the one from a dynamic object. */
3977 if (bed->elf_backend_merge_symbol_attribute)
3978 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3979 dynamic);
3981 /* If this symbol has default visibility and the user has requested
3982 we not re-export it, then mark it as hidden. */
3983 if (definition && !dynamic
3984 && (abfd->no_export
3985 || (abfd->my_archive && abfd->my_archive->no_export))
3986 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3987 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3989 if (isym->st_other != 0 && !dynamic)
3991 unsigned char hvis, symvis, other, nvis;
3993 /* Take the balance of OTHER from the definition. */
3994 other = (definition ? isym->st_other : h->other);
3995 other &= ~ ELF_ST_VISIBILITY (-1);
3997 /* Combine visibilities, using the most constraining one. */
3998 hvis = ELF_ST_VISIBILITY (h->other);
3999 symvis = ELF_ST_VISIBILITY (isym->st_other);
4000 if (! hvis)
4001 nvis = symvis;
4002 else if (! symvis)
4003 nvis = hvis;
4004 else
4005 nvis = hvis < symvis ? hvis : symvis;
4007 h->other = other | nvis;
4010 /* Set a flag in the hash table entry indicating the type of
4011 reference or definition we just found. Keep a count of
4012 the number of dynamic symbols we find. A dynamic symbol
4013 is one which is referenced or defined by both a regular
4014 object and a shared object. */
4015 dynsym = FALSE;
4016 if (! dynamic)
4018 if (! definition)
4020 h->ref_regular = 1;
4021 if (bind != STB_WEAK)
4022 h->ref_regular_nonweak = 1;
4024 else
4025 h->def_regular = 1;
4026 if (! info->executable
4027 || h->def_dynamic
4028 || h->ref_dynamic)
4029 dynsym = TRUE;
4031 else
4033 if (! definition)
4034 h->ref_dynamic = 1;
4035 else
4036 h->def_dynamic = 1;
4037 if (h->def_regular
4038 || h->ref_regular
4039 || (h->u.weakdef != NULL
4040 && ! new_weakdef
4041 && h->u.weakdef->dynindx != -1))
4042 dynsym = TRUE;
4045 /* Check to see if we need to add an indirect symbol for
4046 the default name. */
4047 if (definition || h->root.type == bfd_link_hash_common)
4048 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4049 &sec, &value, &dynsym,
4050 override))
4051 goto error_free_vers;
4053 if (definition && !dynamic)
4055 char *p = strchr (name, ELF_VER_CHR);
4056 if (p != NULL && p[1] != ELF_VER_CHR)
4058 /* Queue non-default versions so that .symver x, x@FOO
4059 aliases can be checked. */
4060 if (! nondeflt_vers)
4062 amt = (isymend - isym + 1)
4063 * sizeof (struct elf_link_hash_entry *);
4064 nondeflt_vers = bfd_malloc (amt);
4066 nondeflt_vers [nondeflt_vers_cnt++] = h;
4070 if (dynsym && h->dynindx == -1)
4072 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4073 goto error_free_vers;
4074 if (h->u.weakdef != NULL
4075 && ! new_weakdef
4076 && h->u.weakdef->dynindx == -1)
4078 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4079 goto error_free_vers;
4082 else if (dynsym && h->dynindx != -1)
4083 /* If the symbol already has a dynamic index, but
4084 visibility says it should not be visible, turn it into
4085 a local symbol. */
4086 switch (ELF_ST_VISIBILITY (h->other))
4088 case STV_INTERNAL:
4089 case STV_HIDDEN:
4090 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4091 dynsym = FALSE;
4092 break;
4095 if (!add_needed
4096 && definition
4097 && dynsym
4098 && h->ref_regular)
4100 int ret;
4101 const char *soname = elf_dt_name (abfd);
4103 /* A symbol from a library loaded via DT_NEEDED of some
4104 other library is referenced by a regular object.
4105 Add a DT_NEEDED entry for it. Issue an error if
4106 --no-add-needed is used. */
4107 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4109 (*_bfd_error_handler)
4110 (_("%s: invalid DSO for symbol `%s' definition"),
4111 abfd, name);
4112 bfd_set_error (bfd_error_bad_value);
4113 goto error_free_vers;
4116 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4118 add_needed = TRUE;
4119 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4120 if (ret < 0)
4121 goto error_free_vers;
4123 BFD_ASSERT (ret == 0);
4128 /* Now that all the symbols from this input file are created, handle
4129 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4130 if (nondeflt_vers != NULL)
4132 bfd_size_type cnt, symidx;
4134 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4136 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4137 char *shortname, *p;
4139 p = strchr (h->root.root.string, ELF_VER_CHR);
4140 if (p == NULL
4141 || (h->root.type != bfd_link_hash_defined
4142 && h->root.type != bfd_link_hash_defweak))
4143 continue;
4145 amt = p - h->root.root.string;
4146 shortname = bfd_malloc (amt + 1);
4147 memcpy (shortname, h->root.root.string, amt);
4148 shortname[amt] = '\0';
4150 hi = (struct elf_link_hash_entry *)
4151 bfd_link_hash_lookup (&hash_table->root, shortname,
4152 FALSE, FALSE, FALSE);
4153 if (hi != NULL
4154 && hi->root.type == h->root.type
4155 && hi->root.u.def.value == h->root.u.def.value
4156 && hi->root.u.def.section == h->root.u.def.section)
4158 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4159 hi->root.type = bfd_link_hash_indirect;
4160 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4161 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4162 sym_hash = elf_sym_hashes (abfd);
4163 if (sym_hash)
4164 for (symidx = 0; symidx < extsymcount; ++symidx)
4165 if (sym_hash[symidx] == hi)
4167 sym_hash[symidx] = h;
4168 break;
4171 free (shortname);
4173 free (nondeflt_vers);
4174 nondeflt_vers = NULL;
4177 if (extversym != NULL)
4179 free (extversym);
4180 extversym = NULL;
4183 if (isymbuf != NULL)
4184 free (isymbuf);
4185 isymbuf = NULL;
4187 if (!add_needed
4188 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4190 /* Remove symbols defined in an as-needed shared lib that wasn't
4191 needed. */
4192 struct elf_smash_syms_data inf;
4193 inf.not_needed = abfd;
4194 inf.htab = hash_table;
4195 inf.twiddled = FALSE;
4196 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4197 if (inf.twiddled)
4198 bfd_link_repair_undef_list (&hash_table->root);
4199 weaks = NULL;
4202 /* Now set the weakdefs field correctly for all the weak defined
4203 symbols we found. The only way to do this is to search all the
4204 symbols. Since we only need the information for non functions in
4205 dynamic objects, that's the only time we actually put anything on
4206 the list WEAKS. We need this information so that if a regular
4207 object refers to a symbol defined weakly in a dynamic object, the
4208 real symbol in the dynamic object is also put in the dynamic
4209 symbols; we also must arrange for both symbols to point to the
4210 same memory location. We could handle the general case of symbol
4211 aliasing, but a general symbol alias can only be generated in
4212 assembler code, handling it correctly would be very time
4213 consuming, and other ELF linkers don't handle general aliasing
4214 either. */
4215 if (weaks != NULL)
4217 struct elf_link_hash_entry **hpp;
4218 struct elf_link_hash_entry **hppend;
4219 struct elf_link_hash_entry **sorted_sym_hash;
4220 struct elf_link_hash_entry *h;
4221 size_t sym_count;
4223 /* Since we have to search the whole symbol list for each weak
4224 defined symbol, search time for N weak defined symbols will be
4225 O(N^2). Binary search will cut it down to O(NlogN). */
4226 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4227 sorted_sym_hash = bfd_malloc (amt);
4228 if (sorted_sym_hash == NULL)
4229 goto error_return;
4230 sym_hash = sorted_sym_hash;
4231 hpp = elf_sym_hashes (abfd);
4232 hppend = hpp + extsymcount;
4233 sym_count = 0;
4234 for (; hpp < hppend; hpp++)
4236 h = *hpp;
4237 if (h != NULL
4238 && h->root.type == bfd_link_hash_defined
4239 && h->type != STT_FUNC)
4241 *sym_hash = h;
4242 sym_hash++;
4243 sym_count++;
4247 qsort (sorted_sym_hash, sym_count,
4248 sizeof (struct elf_link_hash_entry *),
4249 elf_sort_symbol);
4251 while (weaks != NULL)
4253 struct elf_link_hash_entry *hlook;
4254 asection *slook;
4255 bfd_vma vlook;
4256 long ilook;
4257 size_t i, j, idx;
4259 hlook = weaks;
4260 weaks = hlook->u.weakdef;
4261 hlook->u.weakdef = NULL;
4263 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4264 || hlook->root.type == bfd_link_hash_defweak
4265 || hlook->root.type == bfd_link_hash_common
4266 || hlook->root.type == bfd_link_hash_indirect);
4267 slook = hlook->root.u.def.section;
4268 vlook = hlook->root.u.def.value;
4270 ilook = -1;
4271 i = 0;
4272 j = sym_count;
4273 while (i < j)
4275 bfd_signed_vma vdiff;
4276 idx = (i + j) / 2;
4277 h = sorted_sym_hash [idx];
4278 vdiff = vlook - h->root.u.def.value;
4279 if (vdiff < 0)
4280 j = idx;
4281 else if (vdiff > 0)
4282 i = idx + 1;
4283 else
4285 long sdiff = slook->id - h->root.u.def.section->id;
4286 if (sdiff < 0)
4287 j = idx;
4288 else if (sdiff > 0)
4289 i = idx + 1;
4290 else
4292 ilook = idx;
4293 break;
4298 /* We didn't find a value/section match. */
4299 if (ilook == -1)
4300 continue;
4302 for (i = ilook; i < sym_count; i++)
4304 h = sorted_sym_hash [i];
4306 /* Stop if value or section doesn't match. */
4307 if (h->root.u.def.value != vlook
4308 || h->root.u.def.section != slook)
4309 break;
4310 else if (h != hlook)
4312 hlook->u.weakdef = h;
4314 /* If the weak definition is in the list of dynamic
4315 symbols, make sure the real definition is put
4316 there as well. */
4317 if (hlook->dynindx != -1 && h->dynindx == -1)
4319 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4320 goto error_return;
4323 /* If the real definition is in the list of dynamic
4324 symbols, make sure the weak definition is put
4325 there as well. If we don't do this, then the
4326 dynamic loader might not merge the entries for the
4327 real definition and the weak definition. */
4328 if (h->dynindx != -1 && hlook->dynindx == -1)
4330 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4331 goto error_return;
4333 break;
4338 free (sorted_sym_hash);
4341 check_directives = get_elf_backend_data (abfd)->check_directives;
4342 if (check_directives)
4343 check_directives (abfd, info);
4345 /* If this object is the same format as the output object, and it is
4346 not a shared library, then let the backend look through the
4347 relocs.
4349 This is required to build global offset table entries and to
4350 arrange for dynamic relocs. It is not required for the
4351 particular common case of linking non PIC code, even when linking
4352 against shared libraries, but unfortunately there is no way of
4353 knowing whether an object file has been compiled PIC or not.
4354 Looking through the relocs is not particularly time consuming.
4355 The problem is that we must either (1) keep the relocs in memory,
4356 which causes the linker to require additional runtime memory or
4357 (2) read the relocs twice from the input file, which wastes time.
4358 This would be a good case for using mmap.
4360 I have no idea how to handle linking PIC code into a file of a
4361 different format. It probably can't be done. */
4362 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4363 if (! dynamic
4364 && is_elf_hash_table (hash_table)
4365 && hash_table->root.creator == abfd->xvec
4366 && check_relocs != NULL)
4368 asection *o;
4370 for (o = abfd->sections; o != NULL; o = o->next)
4372 Elf_Internal_Rela *internal_relocs;
4373 bfd_boolean ok;
4375 if ((o->flags & SEC_RELOC) == 0
4376 || o->reloc_count == 0
4377 || ((info->strip == strip_all || info->strip == strip_debugger)
4378 && (o->flags & SEC_DEBUGGING) != 0)
4379 || bfd_is_abs_section (o->output_section))
4380 continue;
4382 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4383 info->keep_memory);
4384 if (internal_relocs == NULL)
4385 goto error_return;
4387 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4389 if (elf_section_data (o)->relocs != internal_relocs)
4390 free (internal_relocs);
4392 if (! ok)
4393 goto error_return;
4397 /* If this is a non-traditional link, try to optimize the handling
4398 of the .stab/.stabstr sections. */
4399 if (! dynamic
4400 && ! info->traditional_format
4401 && is_elf_hash_table (hash_table)
4402 && (info->strip != strip_all && info->strip != strip_debugger))
4404 asection *stabstr;
4406 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4407 if (stabstr != NULL)
4409 bfd_size_type string_offset = 0;
4410 asection *stab;
4412 for (stab = abfd->sections; stab; stab = stab->next)
4413 if (strncmp (".stab", stab->name, 5) == 0
4414 && (!stab->name[5] ||
4415 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4416 && (stab->flags & SEC_MERGE) == 0
4417 && !bfd_is_abs_section (stab->output_section))
4419 struct bfd_elf_section_data *secdata;
4421 secdata = elf_section_data (stab);
4422 if (! _bfd_link_section_stabs (abfd,
4423 &hash_table->stab_info,
4424 stab, stabstr,
4425 &secdata->sec_info,
4426 &string_offset))
4427 goto error_return;
4428 if (secdata->sec_info)
4429 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4434 if (is_elf_hash_table (hash_table) && add_needed)
4436 /* Add this bfd to the loaded list. */
4437 struct elf_link_loaded_list *n;
4439 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4440 if (n == NULL)
4441 goto error_return;
4442 n->abfd = abfd;
4443 n->next = hash_table->loaded;
4444 hash_table->loaded = n;
4447 return TRUE;
4449 error_free_vers:
4450 if (nondeflt_vers != NULL)
4451 free (nondeflt_vers);
4452 if (extversym != NULL)
4453 free (extversym);
4454 error_free_sym:
4455 if (isymbuf != NULL)
4456 free (isymbuf);
4457 error_return:
4458 return FALSE;
4461 /* Return the linker hash table entry of a symbol that might be
4462 satisfied by an archive symbol. Return -1 on error. */
4464 struct elf_link_hash_entry *
4465 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4466 struct bfd_link_info *info,
4467 const char *name)
4469 struct elf_link_hash_entry *h;
4470 char *p, *copy;
4471 size_t len, first;
4473 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4474 if (h != NULL)
4475 return h;
4477 /* If this is a default version (the name contains @@), look up the
4478 symbol again with only one `@' as well as without the version.
4479 The effect is that references to the symbol with and without the
4480 version will be matched by the default symbol in the archive. */
4482 p = strchr (name, ELF_VER_CHR);
4483 if (p == NULL || p[1] != ELF_VER_CHR)
4484 return h;
4486 /* First check with only one `@'. */
4487 len = strlen (name);
4488 copy = bfd_alloc (abfd, len);
4489 if (copy == NULL)
4490 return (struct elf_link_hash_entry *) 0 - 1;
4492 first = p - name + 1;
4493 memcpy (copy, name, first);
4494 memcpy (copy + first, name + first + 1, len - first);
4496 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4497 if (h == NULL)
4499 /* We also need to check references to the symbol without the
4500 version. */
4501 copy[first - 1] = '\0';
4502 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4503 FALSE, FALSE, FALSE);
4506 bfd_release (abfd, copy);
4507 return h;
4510 /* Add symbols from an ELF archive file to the linker hash table. We
4511 don't use _bfd_generic_link_add_archive_symbols because of a
4512 problem which arises on UnixWare. The UnixWare libc.so is an
4513 archive which includes an entry libc.so.1 which defines a bunch of
4514 symbols. The libc.so archive also includes a number of other
4515 object files, which also define symbols, some of which are the same
4516 as those defined in libc.so.1. Correct linking requires that we
4517 consider each object file in turn, and include it if it defines any
4518 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4519 this; it looks through the list of undefined symbols, and includes
4520 any object file which defines them. When this algorithm is used on
4521 UnixWare, it winds up pulling in libc.so.1 early and defining a
4522 bunch of symbols. This means that some of the other objects in the
4523 archive are not included in the link, which is incorrect since they
4524 precede libc.so.1 in the archive.
4526 Fortunately, ELF archive handling is simpler than that done by
4527 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4528 oddities. In ELF, if we find a symbol in the archive map, and the
4529 symbol is currently undefined, we know that we must pull in that
4530 object file.
4532 Unfortunately, we do have to make multiple passes over the symbol
4533 table until nothing further is resolved. */
4535 static bfd_boolean
4536 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4538 symindex c;
4539 bfd_boolean *defined = NULL;
4540 bfd_boolean *included = NULL;
4541 carsym *symdefs;
4542 bfd_boolean loop;
4543 bfd_size_type amt;
4544 const struct elf_backend_data *bed;
4545 struct elf_link_hash_entry * (*archive_symbol_lookup)
4546 (bfd *, struct bfd_link_info *, const char *);
4548 if (! bfd_has_map (abfd))
4550 /* An empty archive is a special case. */
4551 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4552 return TRUE;
4553 bfd_set_error (bfd_error_no_armap);
4554 return FALSE;
4557 /* Keep track of all symbols we know to be already defined, and all
4558 files we know to be already included. This is to speed up the
4559 second and subsequent passes. */
4560 c = bfd_ardata (abfd)->symdef_count;
4561 if (c == 0)
4562 return TRUE;
4563 amt = c;
4564 amt *= sizeof (bfd_boolean);
4565 defined = bfd_zmalloc (amt);
4566 included = bfd_zmalloc (amt);
4567 if (defined == NULL || included == NULL)
4568 goto error_return;
4570 symdefs = bfd_ardata (abfd)->symdefs;
4571 bed = get_elf_backend_data (abfd);
4572 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4576 file_ptr last;
4577 symindex i;
4578 carsym *symdef;
4579 carsym *symdefend;
4581 loop = FALSE;
4582 last = -1;
4584 symdef = symdefs;
4585 symdefend = symdef + c;
4586 for (i = 0; symdef < symdefend; symdef++, i++)
4588 struct elf_link_hash_entry *h;
4589 bfd *element;
4590 struct bfd_link_hash_entry *undefs_tail;
4591 symindex mark;
4593 if (defined[i] || included[i])
4594 continue;
4595 if (symdef->file_offset == last)
4597 included[i] = TRUE;
4598 continue;
4601 h = archive_symbol_lookup (abfd, info, symdef->name);
4602 if (h == (struct elf_link_hash_entry *) 0 - 1)
4603 goto error_return;
4605 if (h == NULL)
4606 continue;
4608 if (h->root.type == bfd_link_hash_common)
4610 /* We currently have a common symbol. The archive map contains
4611 a reference to this symbol, so we may want to include it. We
4612 only want to include it however, if this archive element
4613 contains a definition of the symbol, not just another common
4614 declaration of it.
4616 Unfortunately some archivers (including GNU ar) will put
4617 declarations of common symbols into their archive maps, as
4618 well as real definitions, so we cannot just go by the archive
4619 map alone. Instead we must read in the element's symbol
4620 table and check that to see what kind of symbol definition
4621 this is. */
4622 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4623 continue;
4625 else if (h->root.type != bfd_link_hash_undefined)
4627 if (h->root.type != bfd_link_hash_undefweak)
4628 defined[i] = TRUE;
4629 continue;
4632 /* We need to include this archive member. */
4633 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4634 if (element == NULL)
4635 goto error_return;
4637 if (! bfd_check_format (element, bfd_object))
4638 goto error_return;
4640 /* Doublecheck that we have not included this object
4641 already--it should be impossible, but there may be
4642 something wrong with the archive. */
4643 if (element->archive_pass != 0)
4645 bfd_set_error (bfd_error_bad_value);
4646 goto error_return;
4648 element->archive_pass = 1;
4650 undefs_tail = info->hash->undefs_tail;
4652 if (! (*info->callbacks->add_archive_element) (info, element,
4653 symdef->name))
4654 goto error_return;
4655 if (! bfd_link_add_symbols (element, info))
4656 goto error_return;
4658 /* If there are any new undefined symbols, we need to make
4659 another pass through the archive in order to see whether
4660 they can be defined. FIXME: This isn't perfect, because
4661 common symbols wind up on undefs_tail and because an
4662 undefined symbol which is defined later on in this pass
4663 does not require another pass. This isn't a bug, but it
4664 does make the code less efficient than it could be. */
4665 if (undefs_tail != info->hash->undefs_tail)
4666 loop = TRUE;
4668 /* Look backward to mark all symbols from this object file
4669 which we have already seen in this pass. */
4670 mark = i;
4673 included[mark] = TRUE;
4674 if (mark == 0)
4675 break;
4676 --mark;
4678 while (symdefs[mark].file_offset == symdef->file_offset);
4680 /* We mark subsequent symbols from this object file as we go
4681 on through the loop. */
4682 last = symdef->file_offset;
4685 while (loop);
4687 free (defined);
4688 free (included);
4690 return TRUE;
4692 error_return:
4693 if (defined != NULL)
4694 free (defined);
4695 if (included != NULL)
4696 free (included);
4697 return FALSE;
4700 /* Given an ELF BFD, add symbols to the global hash table as
4701 appropriate. */
4703 bfd_boolean
4704 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4706 switch (bfd_get_format (abfd))
4708 case bfd_object:
4709 return elf_link_add_object_symbols (abfd, info);
4710 case bfd_archive:
4711 return elf_link_add_archive_symbols (abfd, info);
4712 default:
4713 bfd_set_error (bfd_error_wrong_format);
4714 return FALSE;
4718 /* This function will be called though elf_link_hash_traverse to store
4719 all hash value of the exported symbols in an array. */
4721 static bfd_boolean
4722 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4724 unsigned long **valuep = data;
4725 const char *name;
4726 char *p;
4727 unsigned long ha;
4728 char *alc = NULL;
4730 if (h->root.type == bfd_link_hash_warning)
4731 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4733 /* Ignore indirect symbols. These are added by the versioning code. */
4734 if (h->dynindx == -1)
4735 return TRUE;
4737 name = h->root.root.string;
4738 p = strchr (name, ELF_VER_CHR);
4739 if (p != NULL)
4741 alc = bfd_malloc (p - name + 1);
4742 memcpy (alc, name, p - name);
4743 alc[p - name] = '\0';
4744 name = alc;
4747 /* Compute the hash value. */
4748 ha = bfd_elf_hash (name);
4750 /* Store the found hash value in the array given as the argument. */
4751 *(*valuep)++ = ha;
4753 /* And store it in the struct so that we can put it in the hash table
4754 later. */
4755 h->u.elf_hash_value = ha;
4757 if (alc != NULL)
4758 free (alc);
4760 return TRUE;
4763 /* Array used to determine the number of hash table buckets to use
4764 based on the number of symbols there are. If there are fewer than
4765 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4766 fewer than 37 we use 17 buckets, and so forth. We never use more
4767 than 32771 buckets. */
4769 static const size_t elf_buckets[] =
4771 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4772 16411, 32771, 0
4775 /* Compute bucket count for hashing table. We do not use a static set
4776 of possible tables sizes anymore. Instead we determine for all
4777 possible reasonable sizes of the table the outcome (i.e., the
4778 number of collisions etc) and choose the best solution. The
4779 weighting functions are not too simple to allow the table to grow
4780 without bounds. Instead one of the weighting factors is the size.
4781 Therefore the result is always a good payoff between few collisions
4782 (= short chain lengths) and table size. */
4783 static size_t
4784 compute_bucket_count (struct bfd_link_info *info)
4786 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4787 size_t best_size = 0;
4788 unsigned long int *hashcodes;
4789 unsigned long int *hashcodesp;
4790 unsigned long int i;
4791 bfd_size_type amt;
4793 /* Compute the hash values for all exported symbols. At the same
4794 time store the values in an array so that we could use them for
4795 optimizations. */
4796 amt = dynsymcount;
4797 amt *= sizeof (unsigned long int);
4798 hashcodes = bfd_malloc (amt);
4799 if (hashcodes == NULL)
4800 return 0;
4801 hashcodesp = hashcodes;
4803 /* Put all hash values in HASHCODES. */
4804 elf_link_hash_traverse (elf_hash_table (info),
4805 elf_collect_hash_codes, &hashcodesp);
4807 /* We have a problem here. The following code to optimize the table
4808 size requires an integer type with more the 32 bits. If
4809 BFD_HOST_U_64_BIT is set we know about such a type. */
4810 #ifdef BFD_HOST_U_64_BIT
4811 if (info->optimize)
4813 unsigned long int nsyms = hashcodesp - hashcodes;
4814 size_t minsize;
4815 size_t maxsize;
4816 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4817 unsigned long int *counts ;
4818 bfd *dynobj = elf_hash_table (info)->dynobj;
4819 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4821 /* Possible optimization parameters: if we have NSYMS symbols we say
4822 that the hashing table must at least have NSYMS/4 and at most
4823 2*NSYMS buckets. */
4824 minsize = nsyms / 4;
4825 if (minsize == 0)
4826 minsize = 1;
4827 best_size = maxsize = nsyms * 2;
4829 /* Create array where we count the collisions in. We must use bfd_malloc
4830 since the size could be large. */
4831 amt = maxsize;
4832 amt *= sizeof (unsigned long int);
4833 counts = bfd_malloc (amt);
4834 if (counts == NULL)
4836 free (hashcodes);
4837 return 0;
4840 /* Compute the "optimal" size for the hash table. The criteria is a
4841 minimal chain length. The minor criteria is (of course) the size
4842 of the table. */
4843 for (i = minsize; i < maxsize; ++i)
4845 /* Walk through the array of hashcodes and count the collisions. */
4846 BFD_HOST_U_64_BIT max;
4847 unsigned long int j;
4848 unsigned long int fact;
4850 memset (counts, '\0', i * sizeof (unsigned long int));
4852 /* Determine how often each hash bucket is used. */
4853 for (j = 0; j < nsyms; ++j)
4854 ++counts[hashcodes[j] % i];
4856 /* For the weight function we need some information about the
4857 pagesize on the target. This is information need not be 100%
4858 accurate. Since this information is not available (so far) we
4859 define it here to a reasonable default value. If it is crucial
4860 to have a better value some day simply define this value. */
4861 # ifndef BFD_TARGET_PAGESIZE
4862 # define BFD_TARGET_PAGESIZE (4096)
4863 # endif
4865 /* We in any case need 2 + NSYMS entries for the size values and
4866 the chains. */
4867 max = (2 + nsyms) * (bed->s->arch_size / 8);
4869 # if 1
4870 /* Variant 1: optimize for short chains. We add the squares
4871 of all the chain lengths (which favors many small chain
4872 over a few long chains). */
4873 for (j = 0; j < i; ++j)
4874 max += counts[j] * counts[j];
4876 /* This adds penalties for the overall size of the table. */
4877 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4878 max *= fact * fact;
4879 # else
4880 /* Variant 2: Optimize a lot more for small table. Here we
4881 also add squares of the size but we also add penalties for
4882 empty slots (the +1 term). */
4883 for (j = 0; j < i; ++j)
4884 max += (1 + counts[j]) * (1 + counts[j]);
4886 /* The overall size of the table is considered, but not as
4887 strong as in variant 1, where it is squared. */
4888 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4889 max *= fact;
4890 # endif
4892 /* Compare with current best results. */
4893 if (max < best_chlen)
4895 best_chlen = max;
4896 best_size = i;
4900 free (counts);
4902 else
4903 #endif /* defined (BFD_HOST_U_64_BIT) */
4905 /* This is the fallback solution if no 64bit type is available or if we
4906 are not supposed to spend much time on optimizations. We select the
4907 bucket count using a fixed set of numbers. */
4908 for (i = 0; elf_buckets[i] != 0; i++)
4910 best_size = elf_buckets[i];
4911 if (dynsymcount < elf_buckets[i + 1])
4912 break;
4916 /* Free the arrays we needed. */
4917 free (hashcodes);
4919 return best_size;
4922 /* Set up the sizes and contents of the ELF dynamic sections. This is
4923 called by the ELF linker emulation before_allocation routine. We
4924 must set the sizes of the sections before the linker sets the
4925 addresses of the various sections. */
4927 bfd_boolean
4928 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4929 const char *soname,
4930 const char *rpath,
4931 const char *filter_shlib,
4932 const char * const *auxiliary_filters,
4933 struct bfd_link_info *info,
4934 asection **sinterpptr,
4935 struct bfd_elf_version_tree *verdefs)
4937 bfd_size_type soname_indx;
4938 bfd *dynobj;
4939 const struct elf_backend_data *bed;
4940 struct elf_assign_sym_version_info asvinfo;
4942 *sinterpptr = NULL;
4944 soname_indx = (bfd_size_type) -1;
4946 if (!is_elf_hash_table (info->hash))
4947 return TRUE;
4949 elf_tdata (output_bfd)->relro = info->relro;
4950 if (info->execstack)
4951 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4952 else if (info->noexecstack)
4953 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4954 else
4956 bfd *inputobj;
4957 asection *notesec = NULL;
4958 int exec = 0;
4960 for (inputobj = info->input_bfds;
4961 inputobj;
4962 inputobj = inputobj->link_next)
4964 asection *s;
4966 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4967 continue;
4968 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4969 if (s)
4971 if (s->flags & SEC_CODE)
4972 exec = PF_X;
4973 notesec = s;
4975 else
4976 exec = PF_X;
4978 if (notesec)
4980 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4981 if (exec && info->relocatable
4982 && notesec->output_section != bfd_abs_section_ptr)
4983 notesec->output_section->flags |= SEC_CODE;
4987 /* Any syms created from now on start with -1 in
4988 got.refcount/offset and plt.refcount/offset. */
4989 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4991 /* The backend may have to create some sections regardless of whether
4992 we're dynamic or not. */
4993 bed = get_elf_backend_data (output_bfd);
4994 if (bed->elf_backend_always_size_sections
4995 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4996 return FALSE;
4998 dynobj = elf_hash_table (info)->dynobj;
5000 /* If there were no dynamic objects in the link, there is nothing to
5001 do here. */
5002 if (dynobj == NULL)
5003 return TRUE;
5005 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5006 return FALSE;
5008 if (elf_hash_table (info)->dynamic_sections_created)
5010 struct elf_info_failed eif;
5011 struct elf_link_hash_entry *h;
5012 asection *dynstr;
5013 struct bfd_elf_version_tree *t;
5014 struct bfd_elf_version_expr *d;
5015 bfd_boolean all_defined;
5017 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5018 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5020 if (soname != NULL)
5022 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5023 soname, TRUE);
5024 if (soname_indx == (bfd_size_type) -1
5025 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5026 return FALSE;
5029 if (info->symbolic)
5031 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5032 return FALSE;
5033 info->flags |= DF_SYMBOLIC;
5036 if (rpath != NULL)
5038 bfd_size_type indx;
5040 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5041 TRUE);
5042 if (indx == (bfd_size_type) -1
5043 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5044 return FALSE;
5046 if (info->new_dtags)
5048 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5049 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5050 return FALSE;
5054 if (filter_shlib != NULL)
5056 bfd_size_type indx;
5058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5059 filter_shlib, TRUE);
5060 if (indx == (bfd_size_type) -1
5061 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5062 return FALSE;
5065 if (auxiliary_filters != NULL)
5067 const char * const *p;
5069 for (p = auxiliary_filters; *p != NULL; p++)
5071 bfd_size_type indx;
5073 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5074 *p, TRUE);
5075 if (indx == (bfd_size_type) -1
5076 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5077 return FALSE;
5081 eif.info = info;
5082 eif.verdefs = verdefs;
5083 eif.failed = FALSE;
5085 /* If we are supposed to export all symbols into the dynamic symbol
5086 table (this is not the normal case), then do so. */
5087 if (info->export_dynamic)
5089 elf_link_hash_traverse (elf_hash_table (info),
5090 _bfd_elf_export_symbol,
5091 &eif);
5092 if (eif.failed)
5093 return FALSE;
5096 /* Make all global versions with definition. */
5097 for (t = verdefs; t != NULL; t = t->next)
5098 for (d = t->globals.list; d != NULL; d = d->next)
5099 if (!d->symver && d->symbol)
5101 const char *verstr, *name;
5102 size_t namelen, verlen, newlen;
5103 char *newname, *p;
5104 struct elf_link_hash_entry *newh;
5106 name = d->symbol;
5107 namelen = strlen (name);
5108 verstr = t->name;
5109 verlen = strlen (verstr);
5110 newlen = namelen + verlen + 3;
5112 newname = bfd_malloc (newlen);
5113 if (newname == NULL)
5114 return FALSE;
5115 memcpy (newname, name, namelen);
5117 /* Check the hidden versioned definition. */
5118 p = newname + namelen;
5119 *p++ = ELF_VER_CHR;
5120 memcpy (p, verstr, verlen + 1);
5121 newh = elf_link_hash_lookup (elf_hash_table (info),
5122 newname, FALSE, FALSE,
5123 FALSE);
5124 if (newh == NULL
5125 || (newh->root.type != bfd_link_hash_defined
5126 && newh->root.type != bfd_link_hash_defweak))
5128 /* Check the default versioned definition. */
5129 *p++ = ELF_VER_CHR;
5130 memcpy (p, verstr, verlen + 1);
5131 newh = elf_link_hash_lookup (elf_hash_table (info),
5132 newname, FALSE, FALSE,
5133 FALSE);
5135 free (newname);
5137 /* Mark this version if there is a definition and it is
5138 not defined in a shared object. */
5139 if (newh != NULL
5140 && !newh->def_dynamic
5141 && (newh->root.type == bfd_link_hash_defined
5142 || newh->root.type == bfd_link_hash_defweak))
5143 d->symver = 1;
5146 /* Attach all the symbols to their version information. */
5147 asvinfo.output_bfd = output_bfd;
5148 asvinfo.info = info;
5149 asvinfo.verdefs = verdefs;
5150 asvinfo.failed = FALSE;
5152 elf_link_hash_traverse (elf_hash_table (info),
5153 _bfd_elf_link_assign_sym_version,
5154 &asvinfo);
5155 if (asvinfo.failed)
5156 return FALSE;
5158 if (!info->allow_undefined_version)
5160 /* Check if all global versions have a definition. */
5161 all_defined = TRUE;
5162 for (t = verdefs; t != NULL; t = t->next)
5163 for (d = t->globals.list; d != NULL; d = d->next)
5164 if (!d->symver && !d->script)
5166 (*_bfd_error_handler)
5167 (_("%s: undefined version: %s"),
5168 d->pattern, t->name);
5169 all_defined = FALSE;
5172 if (!all_defined)
5174 bfd_set_error (bfd_error_bad_value);
5175 return FALSE;
5179 /* Find all symbols which were defined in a dynamic object and make
5180 the backend pick a reasonable value for them. */
5181 elf_link_hash_traverse (elf_hash_table (info),
5182 _bfd_elf_adjust_dynamic_symbol,
5183 &eif);
5184 if (eif.failed)
5185 return FALSE;
5187 /* Add some entries to the .dynamic section. We fill in some of the
5188 values later, in bfd_elf_final_link, but we must add the entries
5189 now so that we know the final size of the .dynamic section. */
5191 /* If there are initialization and/or finalization functions to
5192 call then add the corresponding DT_INIT/DT_FINI entries. */
5193 h = (info->init_function
5194 ? elf_link_hash_lookup (elf_hash_table (info),
5195 info->init_function, FALSE,
5196 FALSE, FALSE)
5197 : NULL);
5198 if (h != NULL
5199 && (h->ref_regular
5200 || h->def_regular))
5202 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5203 return FALSE;
5205 h = (info->fini_function
5206 ? elf_link_hash_lookup (elf_hash_table (info),
5207 info->fini_function, FALSE,
5208 FALSE, FALSE)
5209 : NULL);
5210 if (h != NULL
5211 && (h->ref_regular
5212 || h->def_regular))
5214 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5215 return FALSE;
5218 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5220 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5221 if (! info->executable)
5223 bfd *sub;
5224 asection *o;
5226 for (sub = info->input_bfds; sub != NULL;
5227 sub = sub->link_next)
5228 for (o = sub->sections; o != NULL; o = o->next)
5229 if (elf_section_data (o)->this_hdr.sh_type
5230 == SHT_PREINIT_ARRAY)
5232 (*_bfd_error_handler)
5233 (_("%B: .preinit_array section is not allowed in DSO"),
5234 sub);
5235 break;
5238 bfd_set_error (bfd_error_nonrepresentable_section);
5239 return FALSE;
5242 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5243 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5244 return FALSE;
5246 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5248 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5249 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5250 return FALSE;
5252 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5254 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5255 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5256 return FALSE;
5259 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5260 /* If .dynstr is excluded from the link, we don't want any of
5261 these tags. Strictly, we should be checking each section
5262 individually; This quick check covers for the case where
5263 someone does a /DISCARD/ : { *(*) }. */
5264 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5266 bfd_size_type strsize;
5268 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5269 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5270 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5271 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5272 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5273 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5274 bed->s->sizeof_sym))
5275 return FALSE;
5279 /* The backend must work out the sizes of all the other dynamic
5280 sections. */
5281 if (bed->elf_backend_size_dynamic_sections
5282 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5283 return FALSE;
5285 if (elf_hash_table (info)->dynamic_sections_created)
5287 unsigned long section_sym_count;
5288 asection *s;
5290 /* Set up the version definition section. */
5291 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5292 BFD_ASSERT (s != NULL);
5294 /* We may have created additional version definitions if we are
5295 just linking a regular application. */
5296 verdefs = asvinfo.verdefs;
5298 /* Skip anonymous version tag. */
5299 if (verdefs != NULL && verdefs->vernum == 0)
5300 verdefs = verdefs->next;
5302 if (verdefs == NULL && !info->create_default_symver)
5303 s->flags |= SEC_EXCLUDE;
5304 else
5306 unsigned int cdefs;
5307 bfd_size_type size;
5308 struct bfd_elf_version_tree *t;
5309 bfd_byte *p;
5310 Elf_Internal_Verdef def;
5311 Elf_Internal_Verdaux defaux;
5312 struct bfd_link_hash_entry *bh;
5313 struct elf_link_hash_entry *h;
5314 const char *name;
5316 cdefs = 0;
5317 size = 0;
5319 /* Make space for the base version. */
5320 size += sizeof (Elf_External_Verdef);
5321 size += sizeof (Elf_External_Verdaux);
5322 ++cdefs;
5324 /* Make space for the default version. */
5325 if (info->create_default_symver)
5327 size += sizeof (Elf_External_Verdef);
5328 ++cdefs;
5331 for (t = verdefs; t != NULL; t = t->next)
5333 struct bfd_elf_version_deps *n;
5335 size += sizeof (Elf_External_Verdef);
5336 size += sizeof (Elf_External_Verdaux);
5337 ++cdefs;
5339 for (n = t->deps; n != NULL; n = n->next)
5340 size += sizeof (Elf_External_Verdaux);
5343 s->size = size;
5344 s->contents = bfd_alloc (output_bfd, s->size);
5345 if (s->contents == NULL && s->size != 0)
5346 return FALSE;
5348 /* Fill in the version definition section. */
5350 p = s->contents;
5352 def.vd_version = VER_DEF_CURRENT;
5353 def.vd_flags = VER_FLG_BASE;
5354 def.vd_ndx = 1;
5355 def.vd_cnt = 1;
5356 if (info->create_default_symver)
5358 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5359 def.vd_next = sizeof (Elf_External_Verdef);
5361 else
5363 def.vd_aux = sizeof (Elf_External_Verdef);
5364 def.vd_next = (sizeof (Elf_External_Verdef)
5365 + sizeof (Elf_External_Verdaux));
5368 if (soname_indx != (bfd_size_type) -1)
5370 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5371 soname_indx);
5372 def.vd_hash = bfd_elf_hash (soname);
5373 defaux.vda_name = soname_indx;
5374 name = soname;
5376 else
5378 bfd_size_type indx;
5380 name = basename (output_bfd->filename);
5381 def.vd_hash = bfd_elf_hash (name);
5382 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5383 name, FALSE);
5384 if (indx == (bfd_size_type) -1)
5385 return FALSE;
5386 defaux.vda_name = indx;
5388 defaux.vda_next = 0;
5390 _bfd_elf_swap_verdef_out (output_bfd, &def,
5391 (Elf_External_Verdef *) p);
5392 p += sizeof (Elf_External_Verdef);
5393 if (info->create_default_symver)
5395 /* Add a symbol representing this version. */
5396 bh = NULL;
5397 if (! (_bfd_generic_link_add_one_symbol
5398 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5399 0, NULL, FALSE,
5400 get_elf_backend_data (dynobj)->collect, &bh)))
5401 return FALSE;
5402 h = (struct elf_link_hash_entry *) bh;
5403 h->non_elf = 0;
5404 h->def_regular = 1;
5405 h->type = STT_OBJECT;
5406 h->verinfo.vertree = NULL;
5408 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5409 return FALSE;
5411 /* Create a duplicate of the base version with the same
5412 aux block, but different flags. */
5413 def.vd_flags = 0;
5414 def.vd_ndx = 2;
5415 def.vd_aux = sizeof (Elf_External_Verdef);
5416 if (verdefs)
5417 def.vd_next = (sizeof (Elf_External_Verdef)
5418 + sizeof (Elf_External_Verdaux));
5419 else
5420 def.vd_next = 0;
5421 _bfd_elf_swap_verdef_out (output_bfd, &def,
5422 (Elf_External_Verdef *) p);
5423 p += sizeof (Elf_External_Verdef);
5425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5426 (Elf_External_Verdaux *) p);
5427 p += sizeof (Elf_External_Verdaux);
5429 for (t = verdefs; t != NULL; t = t->next)
5431 unsigned int cdeps;
5432 struct bfd_elf_version_deps *n;
5434 cdeps = 0;
5435 for (n = t->deps; n != NULL; n = n->next)
5436 ++cdeps;
5438 /* Add a symbol representing this version. */
5439 bh = NULL;
5440 if (! (_bfd_generic_link_add_one_symbol
5441 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5442 0, NULL, FALSE,
5443 get_elf_backend_data (dynobj)->collect, &bh)))
5444 return FALSE;
5445 h = (struct elf_link_hash_entry *) bh;
5446 h->non_elf = 0;
5447 h->def_regular = 1;
5448 h->type = STT_OBJECT;
5449 h->verinfo.vertree = t;
5451 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5452 return FALSE;
5454 def.vd_version = VER_DEF_CURRENT;
5455 def.vd_flags = 0;
5456 if (t->globals.list == NULL
5457 && t->locals.list == NULL
5458 && ! t->used)
5459 def.vd_flags |= VER_FLG_WEAK;
5460 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5461 def.vd_cnt = cdeps + 1;
5462 def.vd_hash = bfd_elf_hash (t->name);
5463 def.vd_aux = sizeof (Elf_External_Verdef);
5464 def.vd_next = 0;
5465 if (t->next != NULL)
5466 def.vd_next = (sizeof (Elf_External_Verdef)
5467 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5469 _bfd_elf_swap_verdef_out (output_bfd, &def,
5470 (Elf_External_Verdef *) p);
5471 p += sizeof (Elf_External_Verdef);
5473 defaux.vda_name = h->dynstr_index;
5474 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5475 h->dynstr_index);
5476 defaux.vda_next = 0;
5477 if (t->deps != NULL)
5478 defaux.vda_next = sizeof (Elf_External_Verdaux);
5479 t->name_indx = defaux.vda_name;
5481 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5482 (Elf_External_Verdaux *) p);
5483 p += sizeof (Elf_External_Verdaux);
5485 for (n = t->deps; n != NULL; n = n->next)
5487 if (n->version_needed == NULL)
5489 /* This can happen if there was an error in the
5490 version script. */
5491 defaux.vda_name = 0;
5493 else
5495 defaux.vda_name = n->version_needed->name_indx;
5496 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5497 defaux.vda_name);
5499 if (n->next == NULL)
5500 defaux.vda_next = 0;
5501 else
5502 defaux.vda_next = sizeof (Elf_External_Verdaux);
5504 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5505 (Elf_External_Verdaux *) p);
5506 p += sizeof (Elf_External_Verdaux);
5510 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5511 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5512 return FALSE;
5514 elf_tdata (output_bfd)->cverdefs = cdefs;
5517 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5519 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5520 return FALSE;
5522 else if (info->flags & DF_BIND_NOW)
5524 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5525 return FALSE;
5528 if (info->flags_1)
5530 if (info->executable)
5531 info->flags_1 &= ~ (DF_1_INITFIRST
5532 | DF_1_NODELETE
5533 | DF_1_NOOPEN);
5534 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5535 return FALSE;
5538 /* Work out the size of the version reference section. */
5540 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5541 BFD_ASSERT (s != NULL);
5543 struct elf_find_verdep_info sinfo;
5545 sinfo.output_bfd = output_bfd;
5546 sinfo.info = info;
5547 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5548 if (sinfo.vers == 0)
5549 sinfo.vers = 1;
5550 sinfo.failed = FALSE;
5552 elf_link_hash_traverse (elf_hash_table (info),
5553 _bfd_elf_link_find_version_dependencies,
5554 &sinfo);
5556 if (elf_tdata (output_bfd)->verref == NULL)
5557 s->flags |= SEC_EXCLUDE;
5558 else
5560 Elf_Internal_Verneed *t;
5561 unsigned int size;
5562 unsigned int crefs;
5563 bfd_byte *p;
5565 /* Build the version definition section. */
5566 size = 0;
5567 crefs = 0;
5568 for (t = elf_tdata (output_bfd)->verref;
5569 t != NULL;
5570 t = t->vn_nextref)
5572 Elf_Internal_Vernaux *a;
5574 size += sizeof (Elf_External_Verneed);
5575 ++crefs;
5576 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5577 size += sizeof (Elf_External_Vernaux);
5580 s->size = size;
5581 s->contents = bfd_alloc (output_bfd, s->size);
5582 if (s->contents == NULL)
5583 return FALSE;
5585 p = s->contents;
5586 for (t = elf_tdata (output_bfd)->verref;
5587 t != NULL;
5588 t = t->vn_nextref)
5590 unsigned int caux;
5591 Elf_Internal_Vernaux *a;
5592 bfd_size_type indx;
5594 caux = 0;
5595 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5596 ++caux;
5598 t->vn_version = VER_NEED_CURRENT;
5599 t->vn_cnt = caux;
5600 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5601 elf_dt_name (t->vn_bfd) != NULL
5602 ? elf_dt_name (t->vn_bfd)
5603 : basename (t->vn_bfd->filename),
5604 FALSE);
5605 if (indx == (bfd_size_type) -1)
5606 return FALSE;
5607 t->vn_file = indx;
5608 t->vn_aux = sizeof (Elf_External_Verneed);
5609 if (t->vn_nextref == NULL)
5610 t->vn_next = 0;
5611 else
5612 t->vn_next = (sizeof (Elf_External_Verneed)
5613 + caux * sizeof (Elf_External_Vernaux));
5615 _bfd_elf_swap_verneed_out (output_bfd, t,
5616 (Elf_External_Verneed *) p);
5617 p += sizeof (Elf_External_Verneed);
5619 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5621 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5622 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5623 a->vna_nodename, FALSE);
5624 if (indx == (bfd_size_type) -1)
5625 return FALSE;
5626 a->vna_name = indx;
5627 if (a->vna_nextptr == NULL)
5628 a->vna_next = 0;
5629 else
5630 a->vna_next = sizeof (Elf_External_Vernaux);
5632 _bfd_elf_swap_vernaux_out (output_bfd, a,
5633 (Elf_External_Vernaux *) p);
5634 p += sizeof (Elf_External_Vernaux);
5638 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5639 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5640 return FALSE;
5642 elf_tdata (output_bfd)->cverrefs = crefs;
5646 if ((elf_tdata (output_bfd)->cverrefs == 0
5647 && elf_tdata (output_bfd)->cverdefs == 0)
5648 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5649 &section_sym_count) == 0)
5651 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5652 s->flags |= SEC_EXCLUDE;
5655 return TRUE;
5658 bfd_boolean
5659 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5661 if (!is_elf_hash_table (info->hash))
5662 return TRUE;
5664 if (elf_hash_table (info)->dynamic_sections_created)
5666 bfd *dynobj;
5667 const struct elf_backend_data *bed;
5668 asection *s;
5669 bfd_size_type dynsymcount;
5670 unsigned long section_sym_count;
5671 size_t bucketcount = 0;
5672 size_t hash_entry_size;
5673 unsigned int dtagcount;
5675 dynobj = elf_hash_table (info)->dynobj;
5677 /* Assign dynsym indicies. In a shared library we generate a
5678 section symbol for each output section, which come first.
5679 Next come all of the back-end allocated local dynamic syms,
5680 followed by the rest of the global symbols. */
5682 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5683 &section_sym_count);
5685 /* Work out the size of the symbol version section. */
5686 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5687 BFD_ASSERT (s != NULL);
5688 if (dynsymcount != 0
5689 && (s->flags & SEC_EXCLUDE) == 0)
5691 s->size = dynsymcount * sizeof (Elf_External_Versym);
5692 s->contents = bfd_zalloc (output_bfd, s->size);
5693 if (s->contents == NULL)
5694 return FALSE;
5696 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5697 return FALSE;
5700 /* Set the size of the .dynsym and .hash sections. We counted
5701 the number of dynamic symbols in elf_link_add_object_symbols.
5702 We will build the contents of .dynsym and .hash when we build
5703 the final symbol table, because until then we do not know the
5704 correct value to give the symbols. We built the .dynstr
5705 section as we went along in elf_link_add_object_symbols. */
5706 s = bfd_get_section_by_name (dynobj, ".dynsym");
5707 BFD_ASSERT (s != NULL);
5708 bed = get_elf_backend_data (output_bfd);
5709 s->size = dynsymcount * bed->s->sizeof_sym;
5711 if (dynsymcount != 0)
5713 s->contents = bfd_alloc (output_bfd, s->size);
5714 if (s->contents == NULL)
5715 return FALSE;
5717 /* The first entry in .dynsym is a dummy symbol.
5718 Clear all the section syms, in case we don't output them all. */
5719 ++section_sym_count;
5720 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5723 /* Compute the size of the hashing table. As a side effect this
5724 computes the hash values for all the names we export. */
5725 bucketcount = compute_bucket_count (info);
5727 s = bfd_get_section_by_name (dynobj, ".hash");
5728 BFD_ASSERT (s != NULL);
5729 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5730 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5731 s->contents = bfd_zalloc (output_bfd, s->size);
5732 if (s->contents == NULL)
5733 return FALSE;
5735 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5736 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5737 s->contents + hash_entry_size);
5739 elf_hash_table (info)->bucketcount = bucketcount;
5741 s = bfd_get_section_by_name (dynobj, ".dynstr");
5742 BFD_ASSERT (s != NULL);
5744 elf_finalize_dynstr (output_bfd, info);
5746 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5748 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5749 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5750 return FALSE;
5753 return TRUE;
5756 /* Final phase of ELF linker. */
5758 /* A structure we use to avoid passing large numbers of arguments. */
5760 struct elf_final_link_info
5762 /* General link information. */
5763 struct bfd_link_info *info;
5764 /* Output BFD. */
5765 bfd *output_bfd;
5766 /* Symbol string table. */
5767 struct bfd_strtab_hash *symstrtab;
5768 /* .dynsym section. */
5769 asection *dynsym_sec;
5770 /* .hash section. */
5771 asection *hash_sec;
5772 /* symbol version section (.gnu.version). */
5773 asection *symver_sec;
5774 /* Buffer large enough to hold contents of any section. */
5775 bfd_byte *contents;
5776 /* Buffer large enough to hold external relocs of any section. */
5777 void *external_relocs;
5778 /* Buffer large enough to hold internal relocs of any section. */
5779 Elf_Internal_Rela *internal_relocs;
5780 /* Buffer large enough to hold external local symbols of any input
5781 BFD. */
5782 bfd_byte *external_syms;
5783 /* And a buffer for symbol section indices. */
5784 Elf_External_Sym_Shndx *locsym_shndx;
5785 /* Buffer large enough to hold internal local symbols of any input
5786 BFD. */
5787 Elf_Internal_Sym *internal_syms;
5788 /* Array large enough to hold a symbol index for each local symbol
5789 of any input BFD. */
5790 long *indices;
5791 /* Array large enough to hold a section pointer for each local
5792 symbol of any input BFD. */
5793 asection **sections;
5794 /* Buffer to hold swapped out symbols. */
5795 bfd_byte *symbuf;
5796 /* And one for symbol section indices. */
5797 Elf_External_Sym_Shndx *symshndxbuf;
5798 /* Number of swapped out symbols in buffer. */
5799 size_t symbuf_count;
5800 /* Number of symbols which fit in symbuf. */
5801 size_t symbuf_size;
5802 /* And same for symshndxbuf. */
5803 size_t shndxbuf_size;
5806 /* This struct is used to pass information to elf_link_output_extsym. */
5808 struct elf_outext_info
5810 bfd_boolean failed;
5811 bfd_boolean localsyms;
5812 struct elf_final_link_info *finfo;
5815 /* When performing a relocatable link, the input relocations are
5816 preserved. But, if they reference global symbols, the indices
5817 referenced must be updated. Update all the relocations in
5818 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5820 static void
5821 elf_link_adjust_relocs (bfd *abfd,
5822 Elf_Internal_Shdr *rel_hdr,
5823 unsigned int count,
5824 struct elf_link_hash_entry **rel_hash)
5826 unsigned int i;
5827 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5828 bfd_byte *erela;
5829 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5830 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5831 bfd_vma r_type_mask;
5832 int r_sym_shift;
5834 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5836 swap_in = bed->s->swap_reloc_in;
5837 swap_out = bed->s->swap_reloc_out;
5839 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5841 swap_in = bed->s->swap_reloca_in;
5842 swap_out = bed->s->swap_reloca_out;
5844 else
5845 abort ();
5847 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5848 abort ();
5850 if (bed->s->arch_size == 32)
5852 r_type_mask = 0xff;
5853 r_sym_shift = 8;
5855 else
5857 r_type_mask = 0xffffffff;
5858 r_sym_shift = 32;
5861 erela = rel_hdr->contents;
5862 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5864 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5865 unsigned int j;
5867 if (*rel_hash == NULL)
5868 continue;
5870 BFD_ASSERT ((*rel_hash)->indx >= 0);
5872 (*swap_in) (abfd, erela, irela);
5873 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5874 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5875 | (irela[j].r_info & r_type_mask));
5876 (*swap_out) (abfd, irela, erela);
5880 struct elf_link_sort_rela
5882 union {
5883 bfd_vma offset;
5884 bfd_vma sym_mask;
5885 } u;
5886 enum elf_reloc_type_class type;
5887 /* We use this as an array of size int_rels_per_ext_rel. */
5888 Elf_Internal_Rela rela[1];
5891 static int
5892 elf_link_sort_cmp1 (const void *A, const void *B)
5894 const struct elf_link_sort_rela *a = A;
5895 const struct elf_link_sort_rela *b = B;
5896 int relativea, relativeb;
5898 relativea = a->type == reloc_class_relative;
5899 relativeb = b->type == reloc_class_relative;
5901 if (relativea < relativeb)
5902 return 1;
5903 if (relativea > relativeb)
5904 return -1;
5905 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5906 return -1;
5907 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5908 return 1;
5909 if (a->rela->r_offset < b->rela->r_offset)
5910 return -1;
5911 if (a->rela->r_offset > b->rela->r_offset)
5912 return 1;
5913 return 0;
5916 static int
5917 elf_link_sort_cmp2 (const void *A, const void *B)
5919 const struct elf_link_sort_rela *a = A;
5920 const struct elf_link_sort_rela *b = B;
5921 int copya, copyb;
5923 if (a->u.offset < b->u.offset)
5924 return -1;
5925 if (a->u.offset > b->u.offset)
5926 return 1;
5927 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5928 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5929 if (copya < copyb)
5930 return -1;
5931 if (copya > copyb)
5932 return 1;
5933 if (a->rela->r_offset < b->rela->r_offset)
5934 return -1;
5935 if (a->rela->r_offset > b->rela->r_offset)
5936 return 1;
5937 return 0;
5940 static size_t
5941 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5943 asection *reldyn;
5944 bfd_size_type count, size;
5945 size_t i, ret, sort_elt, ext_size;
5946 bfd_byte *sort, *s_non_relative, *p;
5947 struct elf_link_sort_rela *sq;
5948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5949 int i2e = bed->s->int_rels_per_ext_rel;
5950 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5951 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5952 struct bfd_link_order *lo;
5953 bfd_vma r_sym_mask;
5955 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5956 if (reldyn == NULL || reldyn->size == 0)
5958 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5959 if (reldyn == NULL || reldyn->size == 0)
5960 return 0;
5961 ext_size = bed->s->sizeof_rel;
5962 swap_in = bed->s->swap_reloc_in;
5963 swap_out = bed->s->swap_reloc_out;
5965 else
5967 ext_size = bed->s->sizeof_rela;
5968 swap_in = bed->s->swap_reloca_in;
5969 swap_out = bed->s->swap_reloca_out;
5971 count = reldyn->size / ext_size;
5973 size = 0;
5974 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5975 if (lo->type == bfd_indirect_link_order)
5977 asection *o = lo->u.indirect.section;
5978 size += o->size;
5981 if (size != reldyn->size)
5982 return 0;
5984 sort_elt = (sizeof (struct elf_link_sort_rela)
5985 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5986 sort = bfd_zmalloc (sort_elt * count);
5987 if (sort == NULL)
5989 (*info->callbacks->warning)
5990 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5991 return 0;
5994 if (bed->s->arch_size == 32)
5995 r_sym_mask = ~(bfd_vma) 0xff;
5996 else
5997 r_sym_mask = ~(bfd_vma) 0xffffffff;
5999 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6000 if (lo->type == bfd_indirect_link_order)
6002 bfd_byte *erel, *erelend;
6003 asection *o = lo->u.indirect.section;
6005 if (o->contents == NULL && o->size != 0)
6007 /* This is a reloc section that is being handled as a normal
6008 section. See bfd_section_from_shdr. We can't combine
6009 relocs in this case. */
6010 free (sort);
6011 return 0;
6013 erel = o->contents;
6014 erelend = o->contents + o->size;
6015 p = sort + o->output_offset / ext_size * sort_elt;
6016 while (erel < erelend)
6018 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6019 (*swap_in) (abfd, erel, s->rela);
6020 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6021 s->u.sym_mask = r_sym_mask;
6022 p += sort_elt;
6023 erel += ext_size;
6027 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6029 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6031 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6032 if (s->type != reloc_class_relative)
6033 break;
6035 ret = i;
6036 s_non_relative = p;
6038 sq = (struct elf_link_sort_rela *) s_non_relative;
6039 for (; i < count; i++, p += sort_elt)
6041 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6042 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6043 sq = sp;
6044 sp->u.offset = sq->rela->r_offset;
6047 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6049 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6050 if (lo->type == bfd_indirect_link_order)
6052 bfd_byte *erel, *erelend;
6053 asection *o = lo->u.indirect.section;
6055 erel = o->contents;
6056 erelend = o->contents + o->size;
6057 p = sort + o->output_offset / ext_size * sort_elt;
6058 while (erel < erelend)
6060 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6061 (*swap_out) (abfd, s->rela, erel);
6062 p += sort_elt;
6063 erel += ext_size;
6067 free (sort);
6068 *psec = reldyn;
6069 return ret;
6072 /* Flush the output symbols to the file. */
6074 static bfd_boolean
6075 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6076 const struct elf_backend_data *bed)
6078 if (finfo->symbuf_count > 0)
6080 Elf_Internal_Shdr *hdr;
6081 file_ptr pos;
6082 bfd_size_type amt;
6084 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6085 pos = hdr->sh_offset + hdr->sh_size;
6086 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6087 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6088 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6089 return FALSE;
6091 hdr->sh_size += amt;
6092 finfo->symbuf_count = 0;
6095 return TRUE;
6098 /* Add a symbol to the output symbol table. */
6100 static bfd_boolean
6101 elf_link_output_sym (struct elf_final_link_info *finfo,
6102 const char *name,
6103 Elf_Internal_Sym *elfsym,
6104 asection *input_sec,
6105 struct elf_link_hash_entry *h)
6107 bfd_byte *dest;
6108 Elf_External_Sym_Shndx *destshndx;
6109 bfd_boolean (*output_symbol_hook)
6110 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6111 struct elf_link_hash_entry *);
6112 const struct elf_backend_data *bed;
6114 bed = get_elf_backend_data (finfo->output_bfd);
6115 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6116 if (output_symbol_hook != NULL)
6118 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6119 return FALSE;
6122 if (name == NULL || *name == '\0')
6123 elfsym->st_name = 0;
6124 else if (input_sec->flags & SEC_EXCLUDE)
6125 elfsym->st_name = 0;
6126 else
6128 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6129 name, TRUE, FALSE);
6130 if (elfsym->st_name == (unsigned long) -1)
6131 return FALSE;
6134 if (finfo->symbuf_count >= finfo->symbuf_size)
6136 if (! elf_link_flush_output_syms (finfo, bed))
6137 return FALSE;
6140 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6141 destshndx = finfo->symshndxbuf;
6142 if (destshndx != NULL)
6144 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6146 bfd_size_type amt;
6148 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6149 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6150 if (destshndx == NULL)
6151 return FALSE;
6152 memset ((char *) destshndx + amt, 0, amt);
6153 finfo->shndxbuf_size *= 2;
6155 destshndx += bfd_get_symcount (finfo->output_bfd);
6158 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6159 finfo->symbuf_count += 1;
6160 bfd_get_symcount (finfo->output_bfd) += 1;
6162 return TRUE;
6165 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6166 allowing an unsatisfied unversioned symbol in the DSO to match a
6167 versioned symbol that would normally require an explicit version.
6168 We also handle the case that a DSO references a hidden symbol
6169 which may be satisfied by a versioned symbol in another DSO. */
6171 static bfd_boolean
6172 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6173 const struct elf_backend_data *bed,
6174 struct elf_link_hash_entry *h)
6176 bfd *abfd;
6177 struct elf_link_loaded_list *loaded;
6179 if (!is_elf_hash_table (info->hash))
6180 return FALSE;
6182 switch (h->root.type)
6184 default:
6185 abfd = NULL;
6186 break;
6188 case bfd_link_hash_undefined:
6189 case bfd_link_hash_undefweak:
6190 abfd = h->root.u.undef.abfd;
6191 if ((abfd->flags & DYNAMIC) == 0
6192 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6193 return FALSE;
6194 break;
6196 case bfd_link_hash_defined:
6197 case bfd_link_hash_defweak:
6198 abfd = h->root.u.def.section->owner;
6199 break;
6201 case bfd_link_hash_common:
6202 abfd = h->root.u.c.p->section->owner;
6203 break;
6205 BFD_ASSERT (abfd != NULL);
6207 for (loaded = elf_hash_table (info)->loaded;
6208 loaded != NULL;
6209 loaded = loaded->next)
6211 bfd *input;
6212 Elf_Internal_Shdr *hdr;
6213 bfd_size_type symcount;
6214 bfd_size_type extsymcount;
6215 bfd_size_type extsymoff;
6216 Elf_Internal_Shdr *versymhdr;
6217 Elf_Internal_Sym *isym;
6218 Elf_Internal_Sym *isymend;
6219 Elf_Internal_Sym *isymbuf;
6220 Elf_External_Versym *ever;
6221 Elf_External_Versym *extversym;
6223 input = loaded->abfd;
6225 /* We check each DSO for a possible hidden versioned definition. */
6226 if (input == abfd
6227 || (input->flags & DYNAMIC) == 0
6228 || elf_dynversym (input) == 0)
6229 continue;
6231 hdr = &elf_tdata (input)->dynsymtab_hdr;
6233 symcount = hdr->sh_size / bed->s->sizeof_sym;
6234 if (elf_bad_symtab (input))
6236 extsymcount = symcount;
6237 extsymoff = 0;
6239 else
6241 extsymcount = symcount - hdr->sh_info;
6242 extsymoff = hdr->sh_info;
6245 if (extsymcount == 0)
6246 continue;
6248 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6249 NULL, NULL, NULL);
6250 if (isymbuf == NULL)
6251 return FALSE;
6253 /* Read in any version definitions. */
6254 versymhdr = &elf_tdata (input)->dynversym_hdr;
6255 extversym = bfd_malloc (versymhdr->sh_size);
6256 if (extversym == NULL)
6257 goto error_ret;
6259 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6260 || (bfd_bread (extversym, versymhdr->sh_size, input)
6261 != versymhdr->sh_size))
6263 free (extversym);
6264 error_ret:
6265 free (isymbuf);
6266 return FALSE;
6269 ever = extversym + extsymoff;
6270 isymend = isymbuf + extsymcount;
6271 for (isym = isymbuf; isym < isymend; isym++, ever++)
6273 const char *name;
6274 Elf_Internal_Versym iver;
6275 unsigned short version_index;
6277 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6278 || isym->st_shndx == SHN_UNDEF)
6279 continue;
6281 name = bfd_elf_string_from_elf_section (input,
6282 hdr->sh_link,
6283 isym->st_name);
6284 if (strcmp (name, h->root.root.string) != 0)
6285 continue;
6287 _bfd_elf_swap_versym_in (input, ever, &iver);
6289 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6291 /* If we have a non-hidden versioned sym, then it should
6292 have provided a definition for the undefined sym. */
6293 abort ();
6296 version_index = iver.vs_vers & VERSYM_VERSION;
6297 if (version_index == 1 || version_index == 2)
6299 /* This is the base or first version. We can use it. */
6300 free (extversym);
6301 free (isymbuf);
6302 return TRUE;
6306 free (extversym);
6307 free (isymbuf);
6310 return FALSE;
6313 /* Add an external symbol to the symbol table. This is called from
6314 the hash table traversal routine. When generating a shared object,
6315 we go through the symbol table twice. The first time we output
6316 anything that might have been forced to local scope in a version
6317 script. The second time we output the symbols that are still
6318 global symbols. */
6320 static bfd_boolean
6321 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6323 struct elf_outext_info *eoinfo = data;
6324 struct elf_final_link_info *finfo = eoinfo->finfo;
6325 bfd_boolean strip;
6326 Elf_Internal_Sym sym;
6327 asection *input_sec;
6328 const struct elf_backend_data *bed;
6330 if (h->root.type == bfd_link_hash_warning)
6332 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6333 if (h->root.type == bfd_link_hash_new)
6334 return TRUE;
6337 /* Decide whether to output this symbol in this pass. */
6338 if (eoinfo->localsyms)
6340 if (!h->forced_local)
6341 return TRUE;
6343 else
6345 if (h->forced_local)
6346 return TRUE;
6349 bed = get_elf_backend_data (finfo->output_bfd);
6351 /* If we have an undefined symbol reference here then it must have
6352 come from a shared library that is being linked in. (Undefined
6353 references in regular files have already been handled). If we
6354 are reporting errors for this situation then do so now. */
6355 if (h->root.type == bfd_link_hash_undefined
6356 && h->ref_dynamic
6357 && !h->ref_regular
6358 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6359 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6361 if (! ((*finfo->info->callbacks->undefined_symbol)
6362 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6363 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6365 eoinfo->failed = TRUE;
6366 return FALSE;
6370 /* We should also warn if a forced local symbol is referenced from
6371 shared libraries. */
6372 if (! finfo->info->relocatable
6373 && (! finfo->info->shared)
6374 && h->forced_local
6375 && h->ref_dynamic
6376 && !h->dynamic_def
6377 && !h->dynamic_weak
6378 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6380 (*_bfd_error_handler)
6381 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6382 finfo->output_bfd,
6383 h->root.u.def.section == bfd_abs_section_ptr
6384 ? finfo->output_bfd : h->root.u.def.section->owner,
6385 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6386 ? "internal"
6387 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6388 ? "hidden" : "local",
6389 h->root.root.string);
6390 eoinfo->failed = TRUE;
6391 return FALSE;
6394 /* We don't want to output symbols that have never been mentioned by
6395 a regular file, or that we have been told to strip. However, if
6396 h->indx is set to -2, the symbol is used by a reloc and we must
6397 output it. */
6398 if (h->indx == -2)
6399 strip = FALSE;
6400 else if ((h->def_dynamic
6401 || h->ref_dynamic
6402 || h->root.type == bfd_link_hash_new)
6403 && !h->def_regular
6404 && !h->ref_regular)
6405 strip = TRUE;
6406 else if (finfo->info->strip == strip_all)
6407 strip = TRUE;
6408 else if (finfo->info->strip == strip_some
6409 && bfd_hash_lookup (finfo->info->keep_hash,
6410 h->root.root.string, FALSE, FALSE) == NULL)
6411 strip = TRUE;
6412 else if (finfo->info->strip_discarded
6413 && (h->root.type == bfd_link_hash_defined
6414 || h->root.type == bfd_link_hash_defweak)
6415 && elf_discarded_section (h->root.u.def.section))
6416 strip = TRUE;
6417 else
6418 strip = FALSE;
6420 /* If we're stripping it, and it's not a dynamic symbol, there's
6421 nothing else to do unless it is a forced local symbol. */
6422 if (strip
6423 && h->dynindx == -1
6424 && !h->forced_local)
6425 return TRUE;
6427 sym.st_value = 0;
6428 sym.st_size = h->size;
6429 sym.st_other = h->other;
6430 if (h->forced_local)
6431 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6432 else if (h->root.type == bfd_link_hash_undefweak
6433 || h->root.type == bfd_link_hash_defweak)
6434 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6435 else
6436 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6438 switch (h->root.type)
6440 default:
6441 case bfd_link_hash_new:
6442 case bfd_link_hash_warning:
6443 abort ();
6444 return FALSE;
6446 case bfd_link_hash_undefined:
6447 case bfd_link_hash_undefweak:
6448 input_sec = bfd_und_section_ptr;
6449 sym.st_shndx = SHN_UNDEF;
6450 break;
6452 case bfd_link_hash_defined:
6453 case bfd_link_hash_defweak:
6455 input_sec = h->root.u.def.section;
6456 if (input_sec->output_section != NULL)
6458 sym.st_shndx =
6459 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6460 input_sec->output_section);
6461 if (sym.st_shndx == SHN_BAD)
6463 (*_bfd_error_handler)
6464 (_("%B: could not find output section %A for input section %A"),
6465 finfo->output_bfd, input_sec->output_section, input_sec);
6466 eoinfo->failed = TRUE;
6467 return FALSE;
6470 /* ELF symbols in relocatable files are section relative,
6471 but in nonrelocatable files they are virtual
6472 addresses. */
6473 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6474 if (! finfo->info->relocatable)
6476 sym.st_value += input_sec->output_section->vma;
6477 if (h->type == STT_TLS)
6479 /* STT_TLS symbols are relative to PT_TLS segment
6480 base. */
6481 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6482 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6486 else
6488 BFD_ASSERT (input_sec->owner == NULL
6489 || (input_sec->owner->flags & DYNAMIC) != 0);
6490 sym.st_shndx = SHN_UNDEF;
6491 input_sec = bfd_und_section_ptr;
6494 break;
6496 case bfd_link_hash_common:
6497 input_sec = h->root.u.c.p->section;
6498 sym.st_shndx = SHN_COMMON;
6499 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6500 break;
6502 case bfd_link_hash_indirect:
6503 /* These symbols are created by symbol versioning. They point
6504 to the decorated version of the name. For example, if the
6505 symbol foo@@GNU_1.2 is the default, which should be used when
6506 foo is used with no version, then we add an indirect symbol
6507 foo which points to foo@@GNU_1.2. We ignore these symbols,
6508 since the indirected symbol is already in the hash table. */
6509 return TRUE;
6512 /* Give the processor backend a chance to tweak the symbol value,
6513 and also to finish up anything that needs to be done for this
6514 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6515 forced local syms when non-shared is due to a historical quirk. */
6516 if ((h->dynindx != -1
6517 || h->forced_local)
6518 && ((finfo->info->shared
6519 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6520 || h->root.type != bfd_link_hash_undefweak))
6521 || !h->forced_local)
6522 && elf_hash_table (finfo->info)->dynamic_sections_created)
6524 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6525 (finfo->output_bfd, finfo->info, h, &sym)))
6527 eoinfo->failed = TRUE;
6528 return FALSE;
6532 /* If we are marking the symbol as undefined, and there are no
6533 non-weak references to this symbol from a regular object, then
6534 mark the symbol as weak undefined; if there are non-weak
6535 references, mark the symbol as strong. We can't do this earlier,
6536 because it might not be marked as undefined until the
6537 finish_dynamic_symbol routine gets through with it. */
6538 if (sym.st_shndx == SHN_UNDEF
6539 && h->ref_regular
6540 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6541 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6543 int bindtype;
6545 if (h->ref_regular_nonweak)
6546 bindtype = STB_GLOBAL;
6547 else
6548 bindtype = STB_WEAK;
6549 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6552 /* If a non-weak symbol with non-default visibility is not defined
6553 locally, it is a fatal error. */
6554 if (! finfo->info->relocatable
6555 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6556 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6557 && h->root.type == bfd_link_hash_undefined
6558 && !h->def_regular)
6560 (*_bfd_error_handler)
6561 (_("%B: %s symbol `%s' isn't defined"),
6562 finfo->output_bfd,
6563 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6564 ? "protected"
6565 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6566 ? "internal" : "hidden",
6567 h->root.root.string);
6568 eoinfo->failed = TRUE;
6569 return FALSE;
6572 /* If this symbol should be put in the .dynsym section, then put it
6573 there now. We already know the symbol index. We also fill in
6574 the entry in the .hash section. */
6575 if (h->dynindx != -1
6576 && elf_hash_table (finfo->info)->dynamic_sections_created)
6578 size_t bucketcount;
6579 size_t bucket;
6580 size_t hash_entry_size;
6581 bfd_byte *bucketpos;
6582 bfd_vma chain;
6583 bfd_byte *esym;
6585 sym.st_name = h->dynstr_index;
6586 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6587 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6589 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6590 bucket = h->u.elf_hash_value % bucketcount;
6591 hash_entry_size
6592 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6593 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6594 + (bucket + 2) * hash_entry_size);
6595 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6596 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6597 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6598 ((bfd_byte *) finfo->hash_sec->contents
6599 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6601 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6603 Elf_Internal_Versym iversym;
6604 Elf_External_Versym *eversym;
6606 if (!h->def_regular)
6608 if (h->verinfo.verdef == NULL)
6609 iversym.vs_vers = 0;
6610 else
6611 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6613 else
6615 if (h->verinfo.vertree == NULL)
6616 iversym.vs_vers = 1;
6617 else
6618 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6619 if (finfo->info->create_default_symver)
6620 iversym.vs_vers++;
6623 if (h->hidden)
6624 iversym.vs_vers |= VERSYM_HIDDEN;
6626 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6627 eversym += h->dynindx;
6628 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6632 /* If we're stripping it, then it was just a dynamic symbol, and
6633 there's nothing else to do. */
6634 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6635 return TRUE;
6637 h->indx = bfd_get_symcount (finfo->output_bfd);
6639 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6641 eoinfo->failed = TRUE;
6642 return FALSE;
6645 return TRUE;
6648 /* Return TRUE if special handling is done for relocs in SEC against
6649 symbols defined in discarded sections. */
6651 static bfd_boolean
6652 elf_section_ignore_discarded_relocs (asection *sec)
6654 const struct elf_backend_data *bed;
6656 switch (sec->sec_info_type)
6658 case ELF_INFO_TYPE_STABS:
6659 case ELF_INFO_TYPE_EH_FRAME:
6660 return TRUE;
6661 default:
6662 break;
6665 bed = get_elf_backend_data (sec->owner);
6666 if (bed->elf_backend_ignore_discarded_relocs != NULL
6667 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6668 return TRUE;
6670 return FALSE;
6673 enum action_discarded
6675 COMPLAIN = 1,
6676 PRETEND = 2
6679 /* Return a mask saying how ld should treat relocations in SEC against
6680 symbols defined in discarded sections. If this function returns
6681 COMPLAIN set, ld will issue a warning message. If this function
6682 returns PRETEND set, and the discarded section was link-once and the
6683 same size as the kept link-once section, ld will pretend that the
6684 symbol was actually defined in the kept section. Otherwise ld will
6685 zero the reloc (at least that is the intent, but some cooperation by
6686 the target dependent code is needed, particularly for REL targets). */
6688 static unsigned int
6689 elf_action_discarded (asection *sec)
6691 if (sec->flags & SEC_DEBUGGING)
6692 return PRETEND;
6694 if (strcmp (".eh_frame", sec->name) == 0)
6695 return 0;
6697 if (strcmp (".gcc_except_table", sec->name) == 0)
6698 return 0;
6700 if (strcmp (".PARISC.unwind", sec->name) == 0)
6701 return 0;
6703 if (strcmp (".fixup", sec->name) == 0)
6704 return 0;
6706 return COMPLAIN | PRETEND;
6709 /* Find a match between a section and a member of a section group. */
6711 static asection *
6712 match_group_member (asection *sec, asection *group)
6714 asection *first = elf_next_in_group (group);
6715 asection *s = first;
6717 while (s != NULL)
6719 if (bfd_elf_match_symbols_in_sections (s, sec))
6720 return s;
6722 if (s == first)
6723 break;
6726 return NULL;
6729 /* Check if the kept section of a discarded section SEC can be used
6730 to replace it. Return the replacement if it is OK. Otherwise return
6731 NULL. */
6733 asection *
6734 _bfd_elf_check_kept_section (asection *sec)
6736 asection *kept;
6738 kept = sec->kept_section;
6739 if (kept != NULL)
6741 if (elf_sec_group (sec) != NULL)
6742 kept = match_group_member (sec, kept);
6743 if (kept != NULL && sec->size != kept->size)
6744 kept = NULL;
6746 return kept;
6749 /* Link an input file into the linker output file. This function
6750 handles all the sections and relocations of the input file at once.
6751 This is so that we only have to read the local symbols once, and
6752 don't have to keep them in memory. */
6754 static bfd_boolean
6755 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6757 bfd_boolean (*relocate_section)
6758 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6759 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6760 bfd *output_bfd;
6761 Elf_Internal_Shdr *symtab_hdr;
6762 size_t locsymcount;
6763 size_t extsymoff;
6764 Elf_Internal_Sym *isymbuf;
6765 Elf_Internal_Sym *isym;
6766 Elf_Internal_Sym *isymend;
6767 long *pindex;
6768 asection **ppsection;
6769 asection *o;
6770 const struct elf_backend_data *bed;
6771 bfd_boolean emit_relocs;
6772 struct elf_link_hash_entry **sym_hashes;
6774 output_bfd = finfo->output_bfd;
6775 bed = get_elf_backend_data (output_bfd);
6776 relocate_section = bed->elf_backend_relocate_section;
6778 /* If this is a dynamic object, we don't want to do anything here:
6779 we don't want the local symbols, and we don't want the section
6780 contents. */
6781 if ((input_bfd->flags & DYNAMIC) != 0)
6782 return TRUE;
6784 emit_relocs = (finfo->info->relocatable
6785 || finfo->info->emitrelocations);
6787 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6788 if (elf_bad_symtab (input_bfd))
6790 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6791 extsymoff = 0;
6793 else
6795 locsymcount = symtab_hdr->sh_info;
6796 extsymoff = symtab_hdr->sh_info;
6799 /* Read the local symbols. */
6800 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6801 if (isymbuf == NULL && locsymcount != 0)
6803 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6804 finfo->internal_syms,
6805 finfo->external_syms,
6806 finfo->locsym_shndx);
6807 if (isymbuf == NULL)
6808 return FALSE;
6811 /* Find local symbol sections and adjust values of symbols in
6812 SEC_MERGE sections. Write out those local symbols we know are
6813 going into the output file. */
6814 isymend = isymbuf + locsymcount;
6815 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6816 isym < isymend;
6817 isym++, pindex++, ppsection++)
6819 asection *isec;
6820 const char *name;
6821 Elf_Internal_Sym osym;
6823 *pindex = -1;
6825 if (elf_bad_symtab (input_bfd))
6827 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6829 *ppsection = NULL;
6830 continue;
6834 if (isym->st_shndx == SHN_UNDEF)
6835 isec = bfd_und_section_ptr;
6836 else if (isym->st_shndx < SHN_LORESERVE
6837 || isym->st_shndx > SHN_HIRESERVE)
6839 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6840 if (isec
6841 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6842 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6843 isym->st_value =
6844 _bfd_merged_section_offset (output_bfd, &isec,
6845 elf_section_data (isec)->sec_info,
6846 isym->st_value);
6848 else if (isym->st_shndx == SHN_ABS)
6849 isec = bfd_abs_section_ptr;
6850 else if (isym->st_shndx == SHN_COMMON)
6851 isec = bfd_com_section_ptr;
6852 else
6854 /* Who knows? */
6855 isec = NULL;
6858 *ppsection = isec;
6860 /* Don't output the first, undefined, symbol. */
6861 if (ppsection == finfo->sections)
6862 continue;
6864 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6866 /* We never output section symbols. Instead, we use the
6867 section symbol of the corresponding section in the output
6868 file. */
6869 continue;
6872 /* If we are stripping all symbols, we don't want to output this
6873 one. */
6874 if (finfo->info->strip == strip_all)
6875 continue;
6877 /* If we are discarding all local symbols, we don't want to
6878 output this one. If we are generating a relocatable output
6879 file, then some of the local symbols may be required by
6880 relocs; we output them below as we discover that they are
6881 needed. */
6882 if (finfo->info->discard == discard_all)
6883 continue;
6885 /* If this symbol is defined in a section which we are
6886 discarding, we don't need to keep it, but note that
6887 linker_mark is only reliable for sections that have contents.
6888 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6889 as well as linker_mark. */
6890 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6891 && (isec == NULL
6892 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6893 || (! finfo->info->relocatable
6894 && (isec->flags & SEC_EXCLUDE) != 0)))
6895 continue;
6897 /* If the section is not in the output BFD's section list, it is not
6898 being output. */
6899 if (bfd_section_removed_from_list (output_bfd, isec->output_section))
6900 continue;
6902 /* Get the name of the symbol. */
6903 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6904 isym->st_name);
6905 if (name == NULL)
6906 return FALSE;
6908 /* See if we are discarding symbols with this name. */
6909 if ((finfo->info->strip == strip_some
6910 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6911 == NULL))
6912 || (((finfo->info->discard == discard_sec_merge
6913 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6914 || finfo->info->discard == discard_l)
6915 && bfd_is_local_label_name (input_bfd, name)))
6916 continue;
6918 /* If we get here, we are going to output this symbol. */
6920 osym = *isym;
6922 /* Adjust the section index for the output file. */
6923 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6924 isec->output_section);
6925 if (osym.st_shndx == SHN_BAD)
6926 return FALSE;
6928 *pindex = bfd_get_symcount (output_bfd);
6930 /* ELF symbols in relocatable files are section relative, but
6931 in executable files they are virtual addresses. Note that
6932 this code assumes that all ELF sections have an associated
6933 BFD section with a reasonable value for output_offset; below
6934 we assume that they also have a reasonable value for
6935 output_section. Any special sections must be set up to meet
6936 these requirements. */
6937 osym.st_value += isec->output_offset;
6938 if (! finfo->info->relocatable)
6940 osym.st_value += isec->output_section->vma;
6941 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6943 /* STT_TLS symbols are relative to PT_TLS segment base. */
6944 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6945 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6949 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6950 return FALSE;
6953 /* Relocate the contents of each section. */
6954 sym_hashes = elf_sym_hashes (input_bfd);
6955 for (o = input_bfd->sections; o != NULL; o = o->next)
6957 bfd_byte *contents;
6959 if (! o->linker_mark)
6961 /* This section was omitted from the link. */
6962 continue;
6965 if ((o->flags & SEC_HAS_CONTENTS) == 0
6966 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6967 continue;
6969 if ((o->flags & SEC_LINKER_CREATED) != 0)
6971 /* Section was created by _bfd_elf_link_create_dynamic_sections
6972 or somesuch. */
6973 continue;
6976 /* Get the contents of the section. They have been cached by a
6977 relaxation routine. Note that o is a section in an input
6978 file, so the contents field will not have been set by any of
6979 the routines which work on output files. */
6980 if (elf_section_data (o)->this_hdr.contents != NULL)
6981 contents = elf_section_data (o)->this_hdr.contents;
6982 else
6984 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6986 contents = finfo->contents;
6987 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6988 return FALSE;
6991 if ((o->flags & SEC_RELOC) != 0)
6993 Elf_Internal_Rela *internal_relocs;
6994 bfd_vma r_type_mask;
6995 int r_sym_shift;
6997 /* Get the swapped relocs. */
6998 internal_relocs
6999 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7000 finfo->internal_relocs, FALSE);
7001 if (internal_relocs == NULL
7002 && o->reloc_count > 0)
7003 return FALSE;
7005 if (bed->s->arch_size == 32)
7007 r_type_mask = 0xff;
7008 r_sym_shift = 8;
7010 else
7012 r_type_mask = 0xffffffff;
7013 r_sym_shift = 32;
7016 /* Run through the relocs looking for any against symbols
7017 from discarded sections and section symbols from
7018 removed link-once sections. Complain about relocs
7019 against discarded sections. Zero relocs against removed
7020 link-once sections. Preserve debug information as much
7021 as we can. */
7022 if (!elf_section_ignore_discarded_relocs (o))
7024 Elf_Internal_Rela *rel, *relend;
7025 unsigned int action = elf_action_discarded (o);
7027 rel = internal_relocs;
7028 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7029 for ( ; rel < relend; rel++)
7031 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7032 asection **ps, *sec;
7033 struct elf_link_hash_entry *h = NULL;
7034 const char *sym_name;
7036 if (r_symndx == STN_UNDEF)
7037 continue;
7039 if (r_symndx >= locsymcount
7040 || (elf_bad_symtab (input_bfd)
7041 && finfo->sections[r_symndx] == NULL))
7043 h = sym_hashes[r_symndx - extsymoff];
7045 /* Badly formatted input files can contain relocs that
7046 reference non-existant symbols. Check here so that
7047 we do not seg fault. */
7048 if (h == NULL)
7050 char buffer [32];
7052 sprintf_vma (buffer, rel->r_info);
7053 (*_bfd_error_handler)
7054 (_("error: %B contains a reloc (0x%s) for section %A "
7055 "that references a non-existent global symbol"),
7056 input_bfd, o, buffer);
7057 bfd_set_error (bfd_error_bad_value);
7058 return FALSE;
7061 while (h->root.type == bfd_link_hash_indirect
7062 || h->root.type == bfd_link_hash_warning)
7063 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7065 if (h->root.type != bfd_link_hash_defined
7066 && h->root.type != bfd_link_hash_defweak)
7067 continue;
7069 ps = &h->root.u.def.section;
7070 sym_name = h->root.root.string;
7072 else
7074 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7075 ps = &finfo->sections[r_symndx];
7076 sym_name = bfd_elf_sym_name (input_bfd,
7077 symtab_hdr,
7078 sym, *ps);
7081 /* Complain if the definition comes from a
7082 discarded section. */
7083 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7085 BFD_ASSERT (r_symndx != 0);
7086 if (action & COMPLAIN)
7088 (*_bfd_error_handler)
7089 (_("`%s' referenced in section `%A' of %B: "
7090 "defined in discarded section `%A' of %B"),
7091 o, input_bfd, sec, sec->owner, sym_name);
7092 bfd_set_error (bfd_error_bad_value);
7093 return FALSE;
7096 /* Try to do the best we can to support buggy old
7097 versions of gcc. If we've warned, or this is
7098 debugging info, pretend that the symbol is
7099 really defined in the kept linkonce section.
7100 FIXME: This is quite broken. Modifying the
7101 symbol here means we will be changing all later
7102 uses of the symbol, not just in this section.
7103 The only thing that makes this half reasonable
7104 is that we warn in non-debug sections, and
7105 debug sections tend to come after other
7106 sections. */
7107 if (action & PRETEND)
7109 asection *kept;
7111 kept = _bfd_elf_check_kept_section (sec);
7112 if (kept != NULL)
7114 *ps = kept;
7115 continue;
7119 /* Remove the symbol reference from the reloc, but
7120 don't kill the reloc completely. This is so that
7121 a zero value will be written into the section,
7122 which may have non-zero contents put there by the
7123 assembler. Zero in things like an eh_frame fde
7124 pc_begin allows stack unwinders to recognize the
7125 fde as bogus. */
7126 rel->r_info &= r_type_mask;
7127 rel->r_addend = 0;
7132 /* Relocate the section by invoking a back end routine.
7134 The back end routine is responsible for adjusting the
7135 section contents as necessary, and (if using Rela relocs
7136 and generating a relocatable output file) adjusting the
7137 reloc addend as necessary.
7139 The back end routine does not have to worry about setting
7140 the reloc address or the reloc symbol index.
7142 The back end routine is given a pointer to the swapped in
7143 internal symbols, and can access the hash table entries
7144 for the external symbols via elf_sym_hashes (input_bfd).
7146 When generating relocatable output, the back end routine
7147 must handle STB_LOCAL/STT_SECTION symbols specially. The
7148 output symbol is going to be a section symbol
7149 corresponding to the output section, which will require
7150 the addend to be adjusted. */
7152 if (! (*relocate_section) (output_bfd, finfo->info,
7153 input_bfd, o, contents,
7154 internal_relocs,
7155 isymbuf,
7156 finfo->sections))
7157 return FALSE;
7159 if (emit_relocs)
7161 Elf_Internal_Rela *irela;
7162 Elf_Internal_Rela *irelaend;
7163 bfd_vma last_offset;
7164 struct elf_link_hash_entry **rel_hash;
7165 struct elf_link_hash_entry **rel_hash_list;
7166 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7167 unsigned int next_erel;
7168 bfd_boolean rela_normal;
7170 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7171 rela_normal = (bed->rela_normal
7172 && (input_rel_hdr->sh_entsize
7173 == bed->s->sizeof_rela));
7175 /* Adjust the reloc addresses and symbol indices. */
7177 irela = internal_relocs;
7178 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7179 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7180 + elf_section_data (o->output_section)->rel_count
7181 + elf_section_data (o->output_section)->rel_count2);
7182 rel_hash_list = rel_hash;
7183 last_offset = o->output_offset;
7184 if (!finfo->info->relocatable)
7185 last_offset += o->output_section->vma;
7186 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7188 unsigned long r_symndx;
7189 asection *sec;
7190 Elf_Internal_Sym sym;
7192 if (next_erel == bed->s->int_rels_per_ext_rel)
7194 rel_hash++;
7195 next_erel = 0;
7198 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7199 finfo->info, o,
7200 irela->r_offset);
7201 if (irela->r_offset >= (bfd_vma) -2)
7203 /* This is a reloc for a deleted entry or somesuch.
7204 Turn it into an R_*_NONE reloc, at the same
7205 offset as the last reloc. elf_eh_frame.c and
7206 elf_bfd_discard_info rely on reloc offsets
7207 being ordered. */
7208 irela->r_offset = last_offset;
7209 irela->r_info = 0;
7210 irela->r_addend = 0;
7211 continue;
7214 irela->r_offset += o->output_offset;
7216 /* Relocs in an executable have to be virtual addresses. */
7217 if (!finfo->info->relocatable)
7218 irela->r_offset += o->output_section->vma;
7220 last_offset = irela->r_offset;
7222 r_symndx = irela->r_info >> r_sym_shift;
7223 if (r_symndx == STN_UNDEF)
7224 continue;
7226 if (r_symndx >= locsymcount
7227 || (elf_bad_symtab (input_bfd)
7228 && finfo->sections[r_symndx] == NULL))
7230 struct elf_link_hash_entry *rh;
7231 unsigned long indx;
7233 /* This is a reloc against a global symbol. We
7234 have not yet output all the local symbols, so
7235 we do not know the symbol index of any global
7236 symbol. We set the rel_hash entry for this
7237 reloc to point to the global hash table entry
7238 for this symbol. The symbol index is then
7239 set at the end of bfd_elf_final_link. */
7240 indx = r_symndx - extsymoff;
7241 rh = elf_sym_hashes (input_bfd)[indx];
7242 while (rh->root.type == bfd_link_hash_indirect
7243 || rh->root.type == bfd_link_hash_warning)
7244 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7246 /* Setting the index to -2 tells
7247 elf_link_output_extsym that this symbol is
7248 used by a reloc. */
7249 BFD_ASSERT (rh->indx < 0);
7250 rh->indx = -2;
7252 *rel_hash = rh;
7254 continue;
7257 /* This is a reloc against a local symbol. */
7259 *rel_hash = NULL;
7260 sym = isymbuf[r_symndx];
7261 sec = finfo->sections[r_symndx];
7262 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7264 /* I suppose the backend ought to fill in the
7265 section of any STT_SECTION symbol against a
7266 processor specific section. */
7267 r_symndx = 0;
7268 if (bfd_is_abs_section (sec))
7270 else if (sec == NULL || sec->owner == NULL)
7272 bfd_set_error (bfd_error_bad_value);
7273 return FALSE;
7275 else
7277 asection *osec = sec->output_section;
7279 /* If we have discarded a section, the output
7280 section will be the absolute section. In
7281 case of discarded link-once and discarded
7282 SEC_MERGE sections, use the kept section. */
7283 if (bfd_is_abs_section (osec)
7284 && sec->kept_section != NULL
7285 && sec->kept_section->output_section != NULL)
7287 osec = sec->kept_section->output_section;
7288 irela->r_addend -= osec->vma;
7291 if (!bfd_is_abs_section (osec))
7293 r_symndx = osec->target_index;
7294 BFD_ASSERT (r_symndx != 0);
7298 /* Adjust the addend according to where the
7299 section winds up in the output section. */
7300 if (rela_normal)
7301 irela->r_addend += sec->output_offset;
7303 else
7305 if (finfo->indices[r_symndx] == -1)
7307 unsigned long shlink;
7308 const char *name;
7309 asection *osec;
7311 if (finfo->info->strip == strip_all)
7313 /* You can't do ld -r -s. */
7314 bfd_set_error (bfd_error_invalid_operation);
7315 return FALSE;
7318 /* This symbol was skipped earlier, but
7319 since it is needed by a reloc, we
7320 must output it now. */
7321 shlink = symtab_hdr->sh_link;
7322 name = (bfd_elf_string_from_elf_section
7323 (input_bfd, shlink, sym.st_name));
7324 if (name == NULL)
7325 return FALSE;
7327 osec = sec->output_section;
7328 sym.st_shndx =
7329 _bfd_elf_section_from_bfd_section (output_bfd,
7330 osec);
7331 if (sym.st_shndx == SHN_BAD)
7332 return FALSE;
7334 sym.st_value += sec->output_offset;
7335 if (! finfo->info->relocatable)
7337 sym.st_value += osec->vma;
7338 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7340 /* STT_TLS symbols are relative to PT_TLS
7341 segment base. */
7342 BFD_ASSERT (elf_hash_table (finfo->info)
7343 ->tls_sec != NULL);
7344 sym.st_value -= (elf_hash_table (finfo->info)
7345 ->tls_sec->vma);
7349 finfo->indices[r_symndx]
7350 = bfd_get_symcount (output_bfd);
7352 if (! elf_link_output_sym (finfo, name, &sym, sec,
7353 NULL))
7354 return FALSE;
7357 r_symndx = finfo->indices[r_symndx];
7360 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7361 | (irela->r_info & r_type_mask));
7364 /* Swap out the relocs. */
7365 if (input_rel_hdr->sh_size != 0
7366 && !bed->elf_backend_emit_relocs (output_bfd, o,
7367 input_rel_hdr,
7368 internal_relocs,
7369 rel_hash_list))
7370 return FALSE;
7372 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7373 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7375 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7376 * bed->s->int_rels_per_ext_rel);
7377 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7378 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7379 input_rel_hdr2,
7380 internal_relocs,
7381 rel_hash_list))
7382 return FALSE;
7387 /* Write out the modified section contents. */
7388 if (bed->elf_backend_write_section
7389 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7391 /* Section written out. */
7393 else switch (o->sec_info_type)
7395 case ELF_INFO_TYPE_STABS:
7396 if (! (_bfd_write_section_stabs
7397 (output_bfd,
7398 &elf_hash_table (finfo->info)->stab_info,
7399 o, &elf_section_data (o)->sec_info, contents)))
7400 return FALSE;
7401 break;
7402 case ELF_INFO_TYPE_MERGE:
7403 if (! _bfd_write_merged_section (output_bfd, o,
7404 elf_section_data (o)->sec_info))
7405 return FALSE;
7406 break;
7407 case ELF_INFO_TYPE_EH_FRAME:
7409 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7410 o, contents))
7411 return FALSE;
7413 break;
7414 default:
7416 if (! (o->flags & SEC_EXCLUDE)
7417 && ! bfd_set_section_contents (output_bfd, o->output_section,
7418 contents,
7419 (file_ptr) o->output_offset,
7420 o->size))
7421 return FALSE;
7423 break;
7427 return TRUE;
7430 /* Generate a reloc when linking an ELF file. This is a reloc
7431 requested by the linker, and does come from any input file. This
7432 is used to build constructor and destructor tables when linking
7433 with -Ur. */
7435 static bfd_boolean
7436 elf_reloc_link_order (bfd *output_bfd,
7437 struct bfd_link_info *info,
7438 asection *output_section,
7439 struct bfd_link_order *link_order)
7441 reloc_howto_type *howto;
7442 long indx;
7443 bfd_vma offset;
7444 bfd_vma addend;
7445 struct elf_link_hash_entry **rel_hash_ptr;
7446 Elf_Internal_Shdr *rel_hdr;
7447 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7448 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7449 bfd_byte *erel;
7450 unsigned int i;
7452 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7453 if (howto == NULL)
7455 bfd_set_error (bfd_error_bad_value);
7456 return FALSE;
7459 addend = link_order->u.reloc.p->addend;
7461 /* Figure out the symbol index. */
7462 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7463 + elf_section_data (output_section)->rel_count
7464 + elf_section_data (output_section)->rel_count2);
7465 if (link_order->type == bfd_section_reloc_link_order)
7467 indx = link_order->u.reloc.p->u.section->target_index;
7468 BFD_ASSERT (indx != 0);
7469 *rel_hash_ptr = NULL;
7471 else
7473 struct elf_link_hash_entry *h;
7475 /* Treat a reloc against a defined symbol as though it were
7476 actually against the section. */
7477 h = ((struct elf_link_hash_entry *)
7478 bfd_wrapped_link_hash_lookup (output_bfd, info,
7479 link_order->u.reloc.p->u.name,
7480 FALSE, FALSE, TRUE));
7481 if (h != NULL
7482 && (h->root.type == bfd_link_hash_defined
7483 || h->root.type == bfd_link_hash_defweak))
7485 asection *section;
7487 section = h->root.u.def.section;
7488 indx = section->output_section->target_index;
7489 *rel_hash_ptr = NULL;
7490 /* It seems that we ought to add the symbol value to the
7491 addend here, but in practice it has already been added
7492 because it was passed to constructor_callback. */
7493 addend += section->output_section->vma + section->output_offset;
7495 else if (h != NULL)
7497 /* Setting the index to -2 tells elf_link_output_extsym that
7498 this symbol is used by a reloc. */
7499 h->indx = -2;
7500 *rel_hash_ptr = h;
7501 indx = 0;
7503 else
7505 if (! ((*info->callbacks->unattached_reloc)
7506 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7507 return FALSE;
7508 indx = 0;
7512 /* If this is an inplace reloc, we must write the addend into the
7513 object file. */
7514 if (howto->partial_inplace && addend != 0)
7516 bfd_size_type size;
7517 bfd_reloc_status_type rstat;
7518 bfd_byte *buf;
7519 bfd_boolean ok;
7520 const char *sym_name;
7522 size = bfd_get_reloc_size (howto);
7523 buf = bfd_zmalloc (size);
7524 if (buf == NULL)
7525 return FALSE;
7526 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7527 switch (rstat)
7529 case bfd_reloc_ok:
7530 break;
7532 default:
7533 case bfd_reloc_outofrange:
7534 abort ();
7536 case bfd_reloc_overflow:
7537 if (link_order->type == bfd_section_reloc_link_order)
7538 sym_name = bfd_section_name (output_bfd,
7539 link_order->u.reloc.p->u.section);
7540 else
7541 sym_name = link_order->u.reloc.p->u.name;
7542 if (! ((*info->callbacks->reloc_overflow)
7543 (info, NULL, sym_name, howto->name, addend, NULL,
7544 NULL, (bfd_vma) 0)))
7546 free (buf);
7547 return FALSE;
7549 break;
7551 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7552 link_order->offset, size);
7553 free (buf);
7554 if (! ok)
7555 return FALSE;
7558 /* The address of a reloc is relative to the section in a
7559 relocatable file, and is a virtual address in an executable
7560 file. */
7561 offset = link_order->offset;
7562 if (! info->relocatable)
7563 offset += output_section->vma;
7565 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7567 irel[i].r_offset = offset;
7568 irel[i].r_info = 0;
7569 irel[i].r_addend = 0;
7571 if (bed->s->arch_size == 32)
7572 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7573 else
7574 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7576 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7577 erel = rel_hdr->contents;
7578 if (rel_hdr->sh_type == SHT_REL)
7580 erel += (elf_section_data (output_section)->rel_count
7581 * bed->s->sizeof_rel);
7582 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7584 else
7586 irel[0].r_addend = addend;
7587 erel += (elf_section_data (output_section)->rel_count
7588 * bed->s->sizeof_rela);
7589 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7592 ++elf_section_data (output_section)->rel_count;
7594 return TRUE;
7598 /* Get the output vma of the section pointed to by the sh_link field. */
7600 static bfd_vma
7601 elf_get_linked_section_vma (struct bfd_link_order *p)
7603 Elf_Internal_Shdr **elf_shdrp;
7604 asection *s;
7605 int elfsec;
7607 s = p->u.indirect.section;
7608 elf_shdrp = elf_elfsections (s->owner);
7609 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7610 elfsec = elf_shdrp[elfsec]->sh_link;
7611 /* PR 290:
7612 The Intel C compiler generates SHT_IA_64_UNWIND with
7613 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7614 sh_info fields. Hence we could get the situation
7615 where elfsec is 0. */
7616 if (elfsec == 0)
7618 const struct elf_backend_data *bed
7619 = get_elf_backend_data (s->owner);
7620 if (bed->link_order_error_handler)
7621 bed->link_order_error_handler
7622 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7623 return 0;
7625 else
7627 s = elf_shdrp[elfsec]->bfd_section;
7628 return s->output_section->vma + s->output_offset;
7633 /* Compare two sections based on the locations of the sections they are
7634 linked to. Used by elf_fixup_link_order. */
7636 static int
7637 compare_link_order (const void * a, const void * b)
7639 bfd_vma apos;
7640 bfd_vma bpos;
7642 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7643 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7644 if (apos < bpos)
7645 return -1;
7646 return apos > bpos;
7650 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7651 order as their linked sections. Returns false if this could not be done
7652 because an output section includes both ordered and unordered
7653 sections. Ideally we'd do this in the linker proper. */
7655 static bfd_boolean
7656 elf_fixup_link_order (bfd *abfd, asection *o)
7658 int seen_linkorder;
7659 int seen_other;
7660 int n;
7661 struct bfd_link_order *p;
7662 bfd *sub;
7663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7664 int elfsec;
7665 struct bfd_link_order **sections;
7666 asection *s;
7667 bfd_vma offset;
7669 seen_other = 0;
7670 seen_linkorder = 0;
7671 for (p = o->map_head.link_order; p != NULL; p = p->next)
7673 if (p->type == bfd_indirect_link_order
7674 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7675 == bfd_target_elf_flavour)
7676 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7678 s = p->u.indirect.section;
7679 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7680 if (elfsec != -1
7681 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7682 seen_linkorder++;
7683 else
7684 seen_other++;
7686 else
7687 seen_other++;
7690 if (!seen_linkorder)
7691 return TRUE;
7693 if (seen_other && seen_linkorder)
7695 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7697 bfd_set_error (bfd_error_bad_value);
7698 return FALSE;
7701 sections = (struct bfd_link_order **)
7702 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7703 seen_linkorder = 0;
7705 for (p = o->map_head.link_order; p != NULL; p = p->next)
7707 sections[seen_linkorder++] = p;
7709 /* Sort the input sections in the order of their linked section. */
7710 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7711 compare_link_order);
7713 /* Change the offsets of the sections. */
7714 offset = 0;
7715 for (n = 0; n < seen_linkorder; n++)
7717 s = sections[n]->u.indirect.section;
7718 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7719 s->output_offset = offset;
7720 sections[n]->offset = offset;
7721 offset += sections[n]->size;
7724 return TRUE;
7728 /* Do the final step of an ELF link. */
7730 bfd_boolean
7731 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7733 bfd_boolean dynamic;
7734 bfd_boolean emit_relocs;
7735 bfd *dynobj;
7736 struct elf_final_link_info finfo;
7737 register asection *o;
7738 register struct bfd_link_order *p;
7739 register bfd *sub;
7740 bfd_size_type max_contents_size;
7741 bfd_size_type max_external_reloc_size;
7742 bfd_size_type max_internal_reloc_count;
7743 bfd_size_type max_sym_count;
7744 bfd_size_type max_sym_shndx_count;
7745 file_ptr off;
7746 Elf_Internal_Sym elfsym;
7747 unsigned int i;
7748 Elf_Internal_Shdr *symtab_hdr;
7749 Elf_Internal_Shdr *symtab_shndx_hdr;
7750 Elf_Internal_Shdr *symstrtab_hdr;
7751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7752 struct elf_outext_info eoinfo;
7753 bfd_boolean merged;
7754 size_t relativecount = 0;
7755 asection *reldyn = 0;
7756 bfd_size_type amt;
7758 if (! is_elf_hash_table (info->hash))
7759 return FALSE;
7761 if (info->shared)
7762 abfd->flags |= DYNAMIC;
7764 dynamic = elf_hash_table (info)->dynamic_sections_created;
7765 dynobj = elf_hash_table (info)->dynobj;
7767 emit_relocs = (info->relocatable
7768 || info->emitrelocations
7769 || bed->elf_backend_emit_relocs);
7771 finfo.info = info;
7772 finfo.output_bfd = abfd;
7773 finfo.symstrtab = _bfd_elf_stringtab_init ();
7774 if (finfo.symstrtab == NULL)
7775 return FALSE;
7777 if (! dynamic)
7779 finfo.dynsym_sec = NULL;
7780 finfo.hash_sec = NULL;
7781 finfo.symver_sec = NULL;
7783 else
7785 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7786 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7787 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7788 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7789 /* Note that it is OK if symver_sec is NULL. */
7792 finfo.contents = NULL;
7793 finfo.external_relocs = NULL;
7794 finfo.internal_relocs = NULL;
7795 finfo.external_syms = NULL;
7796 finfo.locsym_shndx = NULL;
7797 finfo.internal_syms = NULL;
7798 finfo.indices = NULL;
7799 finfo.sections = NULL;
7800 finfo.symbuf = NULL;
7801 finfo.symshndxbuf = NULL;
7802 finfo.symbuf_count = 0;
7803 finfo.shndxbuf_size = 0;
7805 /* Count up the number of relocations we will output for each output
7806 section, so that we know the sizes of the reloc sections. We
7807 also figure out some maximum sizes. */
7808 max_contents_size = 0;
7809 max_external_reloc_size = 0;
7810 max_internal_reloc_count = 0;
7811 max_sym_count = 0;
7812 max_sym_shndx_count = 0;
7813 merged = FALSE;
7814 for (o = abfd->sections; o != NULL; o = o->next)
7816 struct bfd_elf_section_data *esdo = elf_section_data (o);
7817 o->reloc_count = 0;
7819 for (p = o->map_head.link_order; p != NULL; p = p->next)
7821 unsigned int reloc_count = 0;
7822 struct bfd_elf_section_data *esdi = NULL;
7823 unsigned int *rel_count1;
7825 if (p->type == bfd_section_reloc_link_order
7826 || p->type == bfd_symbol_reloc_link_order)
7827 reloc_count = 1;
7828 else if (p->type == bfd_indirect_link_order)
7830 asection *sec;
7832 sec = p->u.indirect.section;
7833 esdi = elf_section_data (sec);
7835 /* Mark all sections which are to be included in the
7836 link. This will normally be every section. We need
7837 to do this so that we can identify any sections which
7838 the linker has decided to not include. */
7839 sec->linker_mark = TRUE;
7841 if (sec->flags & SEC_MERGE)
7842 merged = TRUE;
7844 if (info->relocatable || info->emitrelocations)
7845 reloc_count = sec->reloc_count;
7846 else if (bed->elf_backend_count_relocs)
7848 Elf_Internal_Rela * relocs;
7850 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7851 info->keep_memory);
7853 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7855 if (elf_section_data (o)->relocs != relocs)
7856 free (relocs);
7859 if (sec->rawsize > max_contents_size)
7860 max_contents_size = sec->rawsize;
7861 if (sec->size > max_contents_size)
7862 max_contents_size = sec->size;
7864 /* We are interested in just local symbols, not all
7865 symbols. */
7866 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7867 && (sec->owner->flags & DYNAMIC) == 0)
7869 size_t sym_count;
7871 if (elf_bad_symtab (sec->owner))
7872 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7873 / bed->s->sizeof_sym);
7874 else
7875 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7877 if (sym_count > max_sym_count)
7878 max_sym_count = sym_count;
7880 if (sym_count > max_sym_shndx_count
7881 && elf_symtab_shndx (sec->owner) != 0)
7882 max_sym_shndx_count = sym_count;
7884 if ((sec->flags & SEC_RELOC) != 0)
7886 size_t ext_size;
7888 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7889 if (ext_size > max_external_reloc_size)
7890 max_external_reloc_size = ext_size;
7891 if (sec->reloc_count > max_internal_reloc_count)
7892 max_internal_reloc_count = sec->reloc_count;
7897 if (reloc_count == 0)
7898 continue;
7900 o->reloc_count += reloc_count;
7902 /* MIPS may have a mix of REL and RELA relocs on sections.
7903 To support this curious ABI we keep reloc counts in
7904 elf_section_data too. We must be careful to add the
7905 relocations from the input section to the right output
7906 count. FIXME: Get rid of one count. We have
7907 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7908 rel_count1 = &esdo->rel_count;
7909 if (esdi != NULL)
7911 bfd_boolean same_size;
7912 bfd_size_type entsize1;
7914 entsize1 = esdi->rel_hdr.sh_entsize;
7915 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7916 || entsize1 == bed->s->sizeof_rela);
7917 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7919 if (!same_size)
7920 rel_count1 = &esdo->rel_count2;
7922 if (esdi->rel_hdr2 != NULL)
7924 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7925 unsigned int alt_count;
7926 unsigned int *rel_count2;
7928 BFD_ASSERT (entsize2 != entsize1
7929 && (entsize2 == bed->s->sizeof_rel
7930 || entsize2 == bed->s->sizeof_rela));
7932 rel_count2 = &esdo->rel_count2;
7933 if (!same_size)
7934 rel_count2 = &esdo->rel_count;
7936 /* The following is probably too simplistic if the
7937 backend counts output relocs unusually. */
7938 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7939 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7940 *rel_count2 += alt_count;
7941 reloc_count -= alt_count;
7944 *rel_count1 += reloc_count;
7947 if (o->reloc_count > 0)
7948 o->flags |= SEC_RELOC;
7949 else
7951 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7952 set it (this is probably a bug) and if it is set
7953 assign_section_numbers will create a reloc section. */
7954 o->flags &=~ SEC_RELOC;
7957 /* If the SEC_ALLOC flag is not set, force the section VMA to
7958 zero. This is done in elf_fake_sections as well, but forcing
7959 the VMA to 0 here will ensure that relocs against these
7960 sections are handled correctly. */
7961 if ((o->flags & SEC_ALLOC) == 0
7962 && ! o->user_set_vma)
7963 o->vma = 0;
7966 if (! info->relocatable && merged)
7967 elf_link_hash_traverse (elf_hash_table (info),
7968 _bfd_elf_link_sec_merge_syms, abfd);
7970 /* Figure out the file positions for everything but the symbol table
7971 and the relocs. We set symcount to force assign_section_numbers
7972 to create a symbol table. */
7973 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7974 BFD_ASSERT (! abfd->output_has_begun);
7975 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7976 goto error_return;
7978 /* Set sizes, and assign file positions for reloc sections. */
7979 for (o = abfd->sections; o != NULL; o = o->next)
7981 if ((o->flags & SEC_RELOC) != 0)
7983 if (!(_bfd_elf_link_size_reloc_section
7984 (abfd, &elf_section_data (o)->rel_hdr, o)))
7985 goto error_return;
7987 if (elf_section_data (o)->rel_hdr2
7988 && !(_bfd_elf_link_size_reloc_section
7989 (abfd, elf_section_data (o)->rel_hdr2, o)))
7990 goto error_return;
7993 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7994 to count upwards while actually outputting the relocations. */
7995 elf_section_data (o)->rel_count = 0;
7996 elf_section_data (o)->rel_count2 = 0;
7999 _bfd_elf_assign_file_positions_for_relocs (abfd);
8001 /* We have now assigned file positions for all the sections except
8002 .symtab and .strtab. We start the .symtab section at the current
8003 file position, and write directly to it. We build the .strtab
8004 section in memory. */
8005 bfd_get_symcount (abfd) = 0;
8006 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8007 /* sh_name is set in prep_headers. */
8008 symtab_hdr->sh_type = SHT_SYMTAB;
8009 /* sh_flags, sh_addr and sh_size all start off zero. */
8010 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8011 /* sh_link is set in assign_section_numbers. */
8012 /* sh_info is set below. */
8013 /* sh_offset is set just below. */
8014 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8016 off = elf_tdata (abfd)->next_file_pos;
8017 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8019 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8020 incorrect. We do not yet know the size of the .symtab section.
8021 We correct next_file_pos below, after we do know the size. */
8023 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8024 continuously seeking to the right position in the file. */
8025 if (! info->keep_memory || max_sym_count < 20)
8026 finfo.symbuf_size = 20;
8027 else
8028 finfo.symbuf_size = max_sym_count;
8029 amt = finfo.symbuf_size;
8030 amt *= bed->s->sizeof_sym;
8031 finfo.symbuf = bfd_malloc (amt);
8032 if (finfo.symbuf == NULL)
8033 goto error_return;
8034 if (elf_numsections (abfd) > SHN_LORESERVE)
8036 /* Wild guess at number of output symbols. realloc'd as needed. */
8037 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8038 finfo.shndxbuf_size = amt;
8039 amt *= sizeof (Elf_External_Sym_Shndx);
8040 finfo.symshndxbuf = bfd_zmalloc (amt);
8041 if (finfo.symshndxbuf == NULL)
8042 goto error_return;
8045 /* Start writing out the symbol table. The first symbol is always a
8046 dummy symbol. */
8047 if (info->strip != strip_all
8048 || emit_relocs)
8050 elfsym.st_value = 0;
8051 elfsym.st_size = 0;
8052 elfsym.st_info = 0;
8053 elfsym.st_other = 0;
8054 elfsym.st_shndx = SHN_UNDEF;
8055 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8056 NULL))
8057 goto error_return;
8060 /* Output a symbol for each section. We output these even if we are
8061 discarding local symbols, since they are used for relocs. These
8062 symbols have no names. We store the index of each one in the
8063 index field of the section, so that we can find it again when
8064 outputting relocs. */
8065 if (info->strip != strip_all
8066 || emit_relocs)
8068 elfsym.st_size = 0;
8069 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8070 elfsym.st_other = 0;
8071 for (i = 1; i < elf_numsections (abfd); i++)
8073 o = bfd_section_from_elf_index (abfd, i);
8074 if (o != NULL)
8075 o->target_index = bfd_get_symcount (abfd);
8076 elfsym.st_shndx = i;
8077 if (info->relocatable || o == NULL)
8078 elfsym.st_value = 0;
8079 else
8080 elfsym.st_value = o->vma;
8081 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8082 goto error_return;
8083 if (i == SHN_LORESERVE - 1)
8084 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8088 /* Allocate some memory to hold information read in from the input
8089 files. */
8090 if (max_contents_size != 0)
8092 finfo.contents = bfd_malloc (max_contents_size);
8093 if (finfo.contents == NULL)
8094 goto error_return;
8097 if (max_external_reloc_size != 0)
8099 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8100 if (finfo.external_relocs == NULL)
8101 goto error_return;
8104 if (max_internal_reloc_count != 0)
8106 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8107 amt *= sizeof (Elf_Internal_Rela);
8108 finfo.internal_relocs = bfd_malloc (amt);
8109 if (finfo.internal_relocs == NULL)
8110 goto error_return;
8113 if (max_sym_count != 0)
8115 amt = max_sym_count * bed->s->sizeof_sym;
8116 finfo.external_syms = bfd_malloc (amt);
8117 if (finfo.external_syms == NULL)
8118 goto error_return;
8120 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8121 finfo.internal_syms = bfd_malloc (amt);
8122 if (finfo.internal_syms == NULL)
8123 goto error_return;
8125 amt = max_sym_count * sizeof (long);
8126 finfo.indices = bfd_malloc (amt);
8127 if (finfo.indices == NULL)
8128 goto error_return;
8130 amt = max_sym_count * sizeof (asection *);
8131 finfo.sections = bfd_malloc (amt);
8132 if (finfo.sections == NULL)
8133 goto error_return;
8136 if (max_sym_shndx_count != 0)
8138 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8139 finfo.locsym_shndx = bfd_malloc (amt);
8140 if (finfo.locsym_shndx == NULL)
8141 goto error_return;
8144 if (elf_hash_table (info)->tls_sec)
8146 bfd_vma base, end = 0;
8147 asection *sec;
8149 for (sec = elf_hash_table (info)->tls_sec;
8150 sec && (sec->flags & SEC_THREAD_LOCAL);
8151 sec = sec->next)
8153 bfd_vma size = sec->size;
8155 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8157 struct bfd_link_order *o;
8159 for (o = sec->map_head.link_order; o != NULL; o = o->next)
8160 if (size < o->offset + o->size)
8161 size = o->offset + o->size;
8163 end = sec->vma + size;
8165 base = elf_hash_table (info)->tls_sec->vma;
8166 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8167 elf_hash_table (info)->tls_size = end - base;
8170 /* Reorder SHF_LINK_ORDER sections. */
8171 for (o = abfd->sections; o != NULL; o = o->next)
8173 if (!elf_fixup_link_order (abfd, o))
8174 return FALSE;
8177 /* Since ELF permits relocations to be against local symbols, we
8178 must have the local symbols available when we do the relocations.
8179 Since we would rather only read the local symbols once, and we
8180 would rather not keep them in memory, we handle all the
8181 relocations for a single input file at the same time.
8183 Unfortunately, there is no way to know the total number of local
8184 symbols until we have seen all of them, and the local symbol
8185 indices precede the global symbol indices. This means that when
8186 we are generating relocatable output, and we see a reloc against
8187 a global symbol, we can not know the symbol index until we have
8188 finished examining all the local symbols to see which ones we are
8189 going to output. To deal with this, we keep the relocations in
8190 memory, and don't output them until the end of the link. This is
8191 an unfortunate waste of memory, but I don't see a good way around
8192 it. Fortunately, it only happens when performing a relocatable
8193 link, which is not the common case. FIXME: If keep_memory is set
8194 we could write the relocs out and then read them again; I don't
8195 know how bad the memory loss will be. */
8197 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8198 sub->output_has_begun = FALSE;
8199 for (o = abfd->sections; o != NULL; o = o->next)
8201 for (p = o->map_head.link_order; p != NULL; p = p->next)
8203 if (p->type == bfd_indirect_link_order
8204 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8205 == bfd_target_elf_flavour)
8206 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8208 if (! sub->output_has_begun)
8210 if (! elf_link_input_bfd (&finfo, sub))
8211 goto error_return;
8212 sub->output_has_begun = TRUE;
8215 else if (p->type == bfd_section_reloc_link_order
8216 || p->type == bfd_symbol_reloc_link_order)
8218 if (! elf_reloc_link_order (abfd, info, o, p))
8219 goto error_return;
8221 else
8223 if (! _bfd_default_link_order (abfd, info, o, p))
8224 goto error_return;
8229 /* Output any global symbols that got converted to local in a
8230 version script or due to symbol visibility. We do this in a
8231 separate step since ELF requires all local symbols to appear
8232 prior to any global symbols. FIXME: We should only do this if
8233 some global symbols were, in fact, converted to become local.
8234 FIXME: Will this work correctly with the Irix 5 linker? */
8235 eoinfo.failed = FALSE;
8236 eoinfo.finfo = &finfo;
8237 eoinfo.localsyms = TRUE;
8238 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8239 &eoinfo);
8240 if (eoinfo.failed)
8241 return FALSE;
8243 /* That wrote out all the local symbols. Finish up the symbol table
8244 with the global symbols. Even if we want to strip everything we
8245 can, we still need to deal with those global symbols that got
8246 converted to local in a version script. */
8248 /* The sh_info field records the index of the first non local symbol. */
8249 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8251 if (dynamic
8252 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8254 Elf_Internal_Sym sym;
8255 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8256 long last_local = 0;
8258 /* Write out the section symbols for the output sections. */
8259 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8261 asection *s;
8263 sym.st_size = 0;
8264 sym.st_name = 0;
8265 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8266 sym.st_other = 0;
8268 for (s = abfd->sections; s != NULL; s = s->next)
8270 int indx;
8271 bfd_byte *dest;
8272 long dynindx;
8274 dynindx = elf_section_data (s)->dynindx;
8275 if (dynindx <= 0)
8276 continue;
8277 indx = elf_section_data (s)->this_idx;
8278 BFD_ASSERT (indx > 0);
8279 sym.st_shndx = indx;
8280 sym.st_value = s->vma;
8281 dest = dynsym + dynindx * bed->s->sizeof_sym;
8282 if (last_local < dynindx)
8283 last_local = dynindx;
8284 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8288 /* Write out the local dynsyms. */
8289 if (elf_hash_table (info)->dynlocal)
8291 struct elf_link_local_dynamic_entry *e;
8292 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8294 asection *s;
8295 bfd_byte *dest;
8297 sym.st_size = e->isym.st_size;
8298 sym.st_other = e->isym.st_other;
8300 /* Copy the internal symbol as is.
8301 Note that we saved a word of storage and overwrote
8302 the original st_name with the dynstr_index. */
8303 sym = e->isym;
8305 if (e->isym.st_shndx != SHN_UNDEF
8306 && (e->isym.st_shndx < SHN_LORESERVE
8307 || e->isym.st_shndx > SHN_HIRESERVE))
8309 s = bfd_section_from_elf_index (e->input_bfd,
8310 e->isym.st_shndx);
8312 sym.st_shndx =
8313 elf_section_data (s->output_section)->this_idx;
8314 sym.st_value = (s->output_section->vma
8315 + s->output_offset
8316 + e->isym.st_value);
8319 if (last_local < e->dynindx)
8320 last_local = e->dynindx;
8322 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8323 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8327 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8328 last_local + 1;
8331 /* We get the global symbols from the hash table. */
8332 eoinfo.failed = FALSE;
8333 eoinfo.localsyms = FALSE;
8334 eoinfo.finfo = &finfo;
8335 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8336 &eoinfo);
8337 if (eoinfo.failed)
8338 return FALSE;
8340 /* If backend needs to output some symbols not present in the hash
8341 table, do it now. */
8342 if (bed->elf_backend_output_arch_syms)
8344 typedef bfd_boolean (*out_sym_func)
8345 (void *, const char *, Elf_Internal_Sym *, asection *,
8346 struct elf_link_hash_entry *);
8348 if (! ((*bed->elf_backend_output_arch_syms)
8349 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8350 return FALSE;
8353 /* Flush all symbols to the file. */
8354 if (! elf_link_flush_output_syms (&finfo, bed))
8355 return FALSE;
8357 /* Now we know the size of the symtab section. */
8358 off += symtab_hdr->sh_size;
8360 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8361 if (symtab_shndx_hdr->sh_name != 0)
8363 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8364 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8365 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8366 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8367 symtab_shndx_hdr->sh_size = amt;
8369 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8370 off, TRUE);
8372 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8373 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8374 return FALSE;
8378 /* Finish up and write out the symbol string table (.strtab)
8379 section. */
8380 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8381 /* sh_name was set in prep_headers. */
8382 symstrtab_hdr->sh_type = SHT_STRTAB;
8383 symstrtab_hdr->sh_flags = 0;
8384 symstrtab_hdr->sh_addr = 0;
8385 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8386 symstrtab_hdr->sh_entsize = 0;
8387 symstrtab_hdr->sh_link = 0;
8388 symstrtab_hdr->sh_info = 0;
8389 /* sh_offset is set just below. */
8390 symstrtab_hdr->sh_addralign = 1;
8392 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8393 elf_tdata (abfd)->next_file_pos = off;
8395 if (bfd_get_symcount (abfd) > 0)
8397 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8398 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8399 return FALSE;
8402 /* Adjust the relocs to have the correct symbol indices. */
8403 for (o = abfd->sections; o != NULL; o = o->next)
8405 if ((o->flags & SEC_RELOC) == 0)
8406 continue;
8408 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8409 elf_section_data (o)->rel_count,
8410 elf_section_data (o)->rel_hashes);
8411 if (elf_section_data (o)->rel_hdr2 != NULL)
8412 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8413 elf_section_data (o)->rel_count2,
8414 (elf_section_data (o)->rel_hashes
8415 + elf_section_data (o)->rel_count));
8417 /* Set the reloc_count field to 0 to prevent write_relocs from
8418 trying to swap the relocs out itself. */
8419 o->reloc_count = 0;
8422 if (dynamic && info->combreloc && dynobj != NULL)
8423 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8425 /* If we are linking against a dynamic object, or generating a
8426 shared library, finish up the dynamic linking information. */
8427 if (dynamic)
8429 bfd_byte *dyncon, *dynconend;
8431 /* Fix up .dynamic entries. */
8432 o = bfd_get_section_by_name (dynobj, ".dynamic");
8433 BFD_ASSERT (o != NULL);
8435 dyncon = o->contents;
8436 dynconend = o->contents + o->size;
8437 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8439 Elf_Internal_Dyn dyn;
8440 const char *name;
8441 unsigned int type;
8443 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8445 switch (dyn.d_tag)
8447 default:
8448 continue;
8449 case DT_NULL:
8450 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8452 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8454 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8455 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8456 default: continue;
8458 dyn.d_un.d_val = relativecount;
8459 relativecount = 0;
8460 break;
8462 continue;
8464 case DT_INIT:
8465 name = info->init_function;
8466 goto get_sym;
8467 case DT_FINI:
8468 name = info->fini_function;
8469 get_sym:
8471 struct elf_link_hash_entry *h;
8473 h = elf_link_hash_lookup (elf_hash_table (info), name,
8474 FALSE, FALSE, TRUE);
8475 if (h != NULL
8476 && (h->root.type == bfd_link_hash_defined
8477 || h->root.type == bfd_link_hash_defweak))
8479 dyn.d_un.d_val = h->root.u.def.value;
8480 o = h->root.u.def.section;
8481 if (o->output_section != NULL)
8482 dyn.d_un.d_val += (o->output_section->vma
8483 + o->output_offset);
8484 else
8486 /* The symbol is imported from another shared
8487 library and does not apply to this one. */
8488 dyn.d_un.d_val = 0;
8490 break;
8493 continue;
8495 case DT_PREINIT_ARRAYSZ:
8496 name = ".preinit_array";
8497 goto get_size;
8498 case DT_INIT_ARRAYSZ:
8499 name = ".init_array";
8500 goto get_size;
8501 case DT_FINI_ARRAYSZ:
8502 name = ".fini_array";
8503 get_size:
8504 o = bfd_get_section_by_name (abfd, name);
8505 if (o == NULL)
8507 (*_bfd_error_handler)
8508 (_("%B: could not find output section %s"), abfd, name);
8509 goto error_return;
8511 if (o->size == 0)
8512 (*_bfd_error_handler)
8513 (_("warning: %s section has zero size"), name);
8514 dyn.d_un.d_val = o->size;
8515 break;
8517 case DT_PREINIT_ARRAY:
8518 name = ".preinit_array";
8519 goto get_vma;
8520 case DT_INIT_ARRAY:
8521 name = ".init_array";
8522 goto get_vma;
8523 case DT_FINI_ARRAY:
8524 name = ".fini_array";
8525 goto get_vma;
8527 case DT_HASH:
8528 name = ".hash";
8529 goto get_vma;
8530 case DT_STRTAB:
8531 name = ".dynstr";
8532 goto get_vma;
8533 case DT_SYMTAB:
8534 name = ".dynsym";
8535 goto get_vma;
8536 case DT_VERDEF:
8537 name = ".gnu.version_d";
8538 goto get_vma;
8539 case DT_VERNEED:
8540 name = ".gnu.version_r";
8541 goto get_vma;
8542 case DT_VERSYM:
8543 name = ".gnu.version";
8544 get_vma:
8545 o = bfd_get_section_by_name (abfd, name);
8546 if (o == NULL)
8548 (*_bfd_error_handler)
8549 (_("%B: could not find output section %s"), abfd, name);
8550 goto error_return;
8552 dyn.d_un.d_ptr = o->vma;
8553 break;
8555 case DT_REL:
8556 case DT_RELA:
8557 case DT_RELSZ:
8558 case DT_RELASZ:
8559 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8560 type = SHT_REL;
8561 else
8562 type = SHT_RELA;
8563 dyn.d_un.d_val = 0;
8564 for (i = 1; i < elf_numsections (abfd); i++)
8566 Elf_Internal_Shdr *hdr;
8568 hdr = elf_elfsections (abfd)[i];
8569 if (hdr->sh_type == type
8570 && (hdr->sh_flags & SHF_ALLOC) != 0)
8572 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8573 dyn.d_un.d_val += hdr->sh_size;
8574 else
8576 if (dyn.d_un.d_val == 0
8577 || hdr->sh_addr < dyn.d_un.d_val)
8578 dyn.d_un.d_val = hdr->sh_addr;
8582 break;
8584 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8588 /* If we have created any dynamic sections, then output them. */
8589 if (dynobj != NULL)
8591 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8592 goto error_return;
8594 for (o = dynobj->sections; o != NULL; o = o->next)
8596 if ((o->flags & SEC_HAS_CONTENTS) == 0
8597 || o->size == 0
8598 || o->output_section == bfd_abs_section_ptr)
8599 continue;
8600 if ((o->flags & SEC_LINKER_CREATED) == 0)
8602 /* At this point, we are only interested in sections
8603 created by _bfd_elf_link_create_dynamic_sections. */
8604 continue;
8606 if (elf_hash_table (info)->stab_info.stabstr == o)
8607 continue;
8608 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8609 continue;
8610 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8611 != SHT_STRTAB)
8612 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8614 if (! bfd_set_section_contents (abfd, o->output_section,
8615 o->contents,
8616 (file_ptr) o->output_offset,
8617 o->size))
8618 goto error_return;
8620 else
8622 /* The contents of the .dynstr section are actually in a
8623 stringtab. */
8624 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8625 if (bfd_seek (abfd, off, SEEK_SET) != 0
8626 || ! _bfd_elf_strtab_emit (abfd,
8627 elf_hash_table (info)->dynstr))
8628 goto error_return;
8633 if (info->relocatable)
8635 bfd_boolean failed = FALSE;
8637 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8638 if (failed)
8639 goto error_return;
8642 /* If we have optimized stabs strings, output them. */
8643 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8645 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8646 goto error_return;
8649 if (info->eh_frame_hdr)
8651 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8652 goto error_return;
8655 if (finfo.symstrtab != NULL)
8656 _bfd_stringtab_free (finfo.symstrtab);
8657 if (finfo.contents != NULL)
8658 free (finfo.contents);
8659 if (finfo.external_relocs != NULL)
8660 free (finfo.external_relocs);
8661 if (finfo.internal_relocs != NULL)
8662 free (finfo.internal_relocs);
8663 if (finfo.external_syms != NULL)
8664 free (finfo.external_syms);
8665 if (finfo.locsym_shndx != NULL)
8666 free (finfo.locsym_shndx);
8667 if (finfo.internal_syms != NULL)
8668 free (finfo.internal_syms);
8669 if (finfo.indices != NULL)
8670 free (finfo.indices);
8671 if (finfo.sections != NULL)
8672 free (finfo.sections);
8673 if (finfo.symbuf != NULL)
8674 free (finfo.symbuf);
8675 if (finfo.symshndxbuf != NULL)
8676 free (finfo.symshndxbuf);
8677 for (o = abfd->sections; o != NULL; o = o->next)
8679 if ((o->flags & SEC_RELOC) != 0
8680 && elf_section_data (o)->rel_hashes != NULL)
8681 free (elf_section_data (o)->rel_hashes);
8684 elf_tdata (abfd)->linker = TRUE;
8686 return TRUE;
8688 error_return:
8689 if (finfo.symstrtab != NULL)
8690 _bfd_stringtab_free (finfo.symstrtab);
8691 if (finfo.contents != NULL)
8692 free (finfo.contents);
8693 if (finfo.external_relocs != NULL)
8694 free (finfo.external_relocs);
8695 if (finfo.internal_relocs != NULL)
8696 free (finfo.internal_relocs);
8697 if (finfo.external_syms != NULL)
8698 free (finfo.external_syms);
8699 if (finfo.locsym_shndx != NULL)
8700 free (finfo.locsym_shndx);
8701 if (finfo.internal_syms != NULL)
8702 free (finfo.internal_syms);
8703 if (finfo.indices != NULL)
8704 free (finfo.indices);
8705 if (finfo.sections != NULL)
8706 free (finfo.sections);
8707 if (finfo.symbuf != NULL)
8708 free (finfo.symbuf);
8709 if (finfo.symshndxbuf != NULL)
8710 free (finfo.symshndxbuf);
8711 for (o = abfd->sections; o != NULL; o = o->next)
8713 if ((o->flags & SEC_RELOC) != 0
8714 && elf_section_data (o)->rel_hashes != NULL)
8715 free (elf_section_data (o)->rel_hashes);
8718 return FALSE;
8721 /* Garbage collect unused sections. */
8723 /* The mark phase of garbage collection. For a given section, mark
8724 it and any sections in this section's group, and all the sections
8725 which define symbols to which it refers. */
8727 typedef asection * (*gc_mark_hook_fn)
8728 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8729 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8731 bfd_boolean
8732 _bfd_elf_gc_mark (struct bfd_link_info *info,
8733 asection *sec,
8734 gc_mark_hook_fn gc_mark_hook)
8736 bfd_boolean ret;
8737 asection *group_sec;
8739 sec->gc_mark = 1;
8741 /* Mark all the sections in the group. */
8742 group_sec = elf_section_data (sec)->next_in_group;
8743 if (group_sec && !group_sec->gc_mark)
8744 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8745 return FALSE;
8747 /* Look through the section relocs. */
8748 ret = TRUE;
8749 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8751 Elf_Internal_Rela *relstart, *rel, *relend;
8752 Elf_Internal_Shdr *symtab_hdr;
8753 struct elf_link_hash_entry **sym_hashes;
8754 size_t nlocsyms;
8755 size_t extsymoff;
8756 bfd *input_bfd = sec->owner;
8757 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8758 Elf_Internal_Sym *isym = NULL;
8759 int r_sym_shift;
8761 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8762 sym_hashes = elf_sym_hashes (input_bfd);
8764 /* Read the local symbols. */
8765 if (elf_bad_symtab (input_bfd))
8767 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8768 extsymoff = 0;
8770 else
8771 extsymoff = nlocsyms = symtab_hdr->sh_info;
8773 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8774 if (isym == NULL && nlocsyms != 0)
8776 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8777 NULL, NULL, NULL);
8778 if (isym == NULL)
8779 return FALSE;
8782 /* Read the relocations. */
8783 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8784 info->keep_memory);
8785 if (relstart == NULL)
8787 ret = FALSE;
8788 goto out1;
8790 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8792 if (bed->s->arch_size == 32)
8793 r_sym_shift = 8;
8794 else
8795 r_sym_shift = 32;
8797 for (rel = relstart; rel < relend; rel++)
8799 unsigned long r_symndx;
8800 asection *rsec;
8801 struct elf_link_hash_entry *h;
8803 r_symndx = rel->r_info >> r_sym_shift;
8804 if (r_symndx == 0)
8805 continue;
8807 if (r_symndx >= nlocsyms
8808 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8810 h = sym_hashes[r_symndx - extsymoff];
8811 while (h->root.type == bfd_link_hash_indirect
8812 || h->root.type == bfd_link_hash_warning)
8813 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8814 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8816 else
8818 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8821 if (rsec && !rsec->gc_mark)
8823 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8824 rsec->gc_mark = 1;
8825 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8827 ret = FALSE;
8828 goto out2;
8833 out2:
8834 if (elf_section_data (sec)->relocs != relstart)
8835 free (relstart);
8836 out1:
8837 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8839 if (! info->keep_memory)
8840 free (isym);
8841 else
8842 symtab_hdr->contents = (unsigned char *) isym;
8846 return ret;
8849 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8851 static bfd_boolean
8852 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8854 int *idx = idxptr;
8856 if (h->root.type == bfd_link_hash_warning)
8857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8859 if (h->dynindx != -1
8860 && ((h->root.type != bfd_link_hash_defined
8861 && h->root.type != bfd_link_hash_defweak)
8862 || h->root.u.def.section->gc_mark))
8863 h->dynindx = (*idx)++;
8865 return TRUE;
8868 /* The sweep phase of garbage collection. Remove all garbage sections. */
8870 typedef bfd_boolean (*gc_sweep_hook_fn)
8871 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8873 static bfd_boolean
8874 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8876 bfd *sub;
8878 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8880 asection *o;
8882 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8883 continue;
8885 for (o = sub->sections; o != NULL; o = o->next)
8887 /* Keep debug and special sections. */
8888 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8889 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8890 o->gc_mark = 1;
8892 if (o->gc_mark)
8893 continue;
8895 /* Skip sweeping sections already excluded. */
8896 if (o->flags & SEC_EXCLUDE)
8897 continue;
8899 /* Since this is early in the link process, it is simple
8900 to remove a section from the output. */
8901 o->flags |= SEC_EXCLUDE;
8903 /* But we also have to update some of the relocation
8904 info we collected before. */
8905 if (gc_sweep_hook
8906 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8908 Elf_Internal_Rela *internal_relocs;
8909 bfd_boolean r;
8911 internal_relocs
8912 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8913 info->keep_memory);
8914 if (internal_relocs == NULL)
8915 return FALSE;
8917 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8919 if (elf_section_data (o)->relocs != internal_relocs)
8920 free (internal_relocs);
8922 if (!r)
8923 return FALSE;
8928 /* Remove the symbols that were in the swept sections from the dynamic
8929 symbol table. GCFIXME: Anyone know how to get them out of the
8930 static symbol table as well? */
8932 int i = 0;
8934 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8936 elf_hash_table (info)->dynsymcount = i;
8939 return TRUE;
8942 /* Propagate collected vtable information. This is called through
8943 elf_link_hash_traverse. */
8945 static bfd_boolean
8946 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8948 if (h->root.type == bfd_link_hash_warning)
8949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8951 /* Those that are not vtables. */
8952 if (h->vtable == NULL || h->vtable->parent == NULL)
8953 return TRUE;
8955 /* Those vtables that do not have parents, we cannot merge. */
8956 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8957 return TRUE;
8959 /* If we've already been done, exit. */
8960 if (h->vtable->used && h->vtable->used[-1])
8961 return TRUE;
8963 /* Make sure the parent's table is up to date. */
8964 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8966 if (h->vtable->used == NULL)
8968 /* None of this table's entries were referenced. Re-use the
8969 parent's table. */
8970 h->vtable->used = h->vtable->parent->vtable->used;
8971 h->vtable->size = h->vtable->parent->vtable->size;
8973 else
8975 size_t n;
8976 bfd_boolean *cu, *pu;
8978 /* Or the parent's entries into ours. */
8979 cu = h->vtable->used;
8980 cu[-1] = TRUE;
8981 pu = h->vtable->parent->vtable->used;
8982 if (pu != NULL)
8984 const struct elf_backend_data *bed;
8985 unsigned int log_file_align;
8987 bed = get_elf_backend_data (h->root.u.def.section->owner);
8988 log_file_align = bed->s->log_file_align;
8989 n = h->vtable->parent->vtable->size >> log_file_align;
8990 while (n--)
8992 if (*pu)
8993 *cu = TRUE;
8994 pu++;
8995 cu++;
9000 return TRUE;
9003 static bfd_boolean
9004 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9006 asection *sec;
9007 bfd_vma hstart, hend;
9008 Elf_Internal_Rela *relstart, *relend, *rel;
9009 const struct elf_backend_data *bed;
9010 unsigned int log_file_align;
9012 if (h->root.type == bfd_link_hash_warning)
9013 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9015 /* Take care of both those symbols that do not describe vtables as
9016 well as those that are not loaded. */
9017 if (h->vtable == NULL || h->vtable->parent == NULL)
9018 return TRUE;
9020 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9021 || h->root.type == bfd_link_hash_defweak);
9023 sec = h->root.u.def.section;
9024 hstart = h->root.u.def.value;
9025 hend = hstart + h->size;
9027 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9028 if (!relstart)
9029 return *(bfd_boolean *) okp = FALSE;
9030 bed = get_elf_backend_data (sec->owner);
9031 log_file_align = bed->s->log_file_align;
9033 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9035 for (rel = relstart; rel < relend; ++rel)
9036 if (rel->r_offset >= hstart && rel->r_offset < hend)
9038 /* If the entry is in use, do nothing. */
9039 if (h->vtable->used
9040 && (rel->r_offset - hstart) < h->vtable->size)
9042 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9043 if (h->vtable->used[entry])
9044 continue;
9046 /* Otherwise, kill it. */
9047 rel->r_offset = rel->r_info = rel->r_addend = 0;
9050 return TRUE;
9053 /* Mark sections containing dynamically referenced symbols. This is called
9054 through elf_link_hash_traverse. */
9056 static bfd_boolean
9057 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
9058 void *okp ATTRIBUTE_UNUSED)
9060 if (h->root.type == bfd_link_hash_warning)
9061 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9063 if ((h->root.type == bfd_link_hash_defined
9064 || h->root.type == bfd_link_hash_defweak)
9065 && h->ref_dynamic)
9066 h->root.u.def.section->flags |= SEC_KEEP;
9068 return TRUE;
9071 /* Mark sections containing global symbols. This is called through
9072 elf_link_hash_traverse. */
9074 static bfd_boolean
9075 elf_mark_used_section (struct elf_link_hash_entry *h,
9076 void *data ATTRIBUTE_UNUSED)
9078 if (h->root.type == bfd_link_hash_warning)
9079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9081 if (h->root.type == bfd_link_hash_defined
9082 || h->root.type == bfd_link_hash_defweak)
9084 asection *s = h->root.u.def.section;
9085 if (s != NULL && s->output_section != NULL)
9086 s->output_section->flags |= SEC_KEEP;
9089 return TRUE;
9092 /* Do mark and sweep of unused sections. */
9094 bfd_boolean
9095 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9097 bfd_boolean ok = TRUE;
9098 bfd *sub;
9099 asection * (*gc_mark_hook)
9100 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9101 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9103 if (!info->gc_sections)
9105 /* If we are called when info->gc_sections is 0, we will mark
9106 all sections containing global symbols for non-relocatable
9107 link. */
9108 if (!info->relocatable)
9109 elf_link_hash_traverse (elf_hash_table (info),
9110 elf_mark_used_section, NULL);
9111 return TRUE;
9114 if (!get_elf_backend_data (abfd)->can_gc_sections
9115 || info->relocatable
9116 || info->emitrelocations
9117 || info->shared
9118 || !is_elf_hash_table (info->hash))
9120 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9121 return TRUE;
9124 /* Apply transitive closure to the vtable entry usage info. */
9125 elf_link_hash_traverse (elf_hash_table (info),
9126 elf_gc_propagate_vtable_entries_used,
9127 &ok);
9128 if (!ok)
9129 return FALSE;
9131 /* Kill the vtable relocations that were not used. */
9132 elf_link_hash_traverse (elf_hash_table (info),
9133 elf_gc_smash_unused_vtentry_relocs,
9134 &ok);
9135 if (!ok)
9136 return FALSE;
9138 /* Mark dynamically referenced symbols. */
9139 if (elf_hash_table (info)->dynamic_sections_created)
9140 elf_link_hash_traverse (elf_hash_table (info),
9141 elf_gc_mark_dynamic_ref_symbol,
9142 &ok);
9143 if (!ok)
9144 return FALSE;
9146 /* Grovel through relocs to find out who stays ... */
9147 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9148 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9150 asection *o;
9152 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9153 continue;
9155 for (o = sub->sections; o != NULL; o = o->next)
9157 if (o->flags & SEC_KEEP)
9159 /* _bfd_elf_discard_section_eh_frame knows how to discard
9160 orphaned FDEs so don't mark sections referenced by the
9161 EH frame section. */
9162 if (strcmp (o->name, ".eh_frame") == 0)
9163 o->gc_mark = 1;
9164 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9165 return FALSE;
9170 /* ... and mark SEC_EXCLUDE for those that go. */
9171 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9172 return FALSE;
9174 return TRUE;
9177 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9179 bfd_boolean
9180 bfd_elf_gc_record_vtinherit (bfd *abfd,
9181 asection *sec,
9182 struct elf_link_hash_entry *h,
9183 bfd_vma offset)
9185 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9186 struct elf_link_hash_entry **search, *child;
9187 bfd_size_type extsymcount;
9188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9190 /* The sh_info field of the symtab header tells us where the
9191 external symbols start. We don't care about the local symbols at
9192 this point. */
9193 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9194 if (!elf_bad_symtab (abfd))
9195 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9197 sym_hashes = elf_sym_hashes (abfd);
9198 sym_hashes_end = sym_hashes + extsymcount;
9200 /* Hunt down the child symbol, which is in this section at the same
9201 offset as the relocation. */
9202 for (search = sym_hashes; search != sym_hashes_end; ++search)
9204 if ((child = *search) != NULL
9205 && (child->root.type == bfd_link_hash_defined
9206 || child->root.type == bfd_link_hash_defweak)
9207 && child->root.u.def.section == sec
9208 && child->root.u.def.value == offset)
9209 goto win;
9212 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9213 abfd, sec, (unsigned long) offset);
9214 bfd_set_error (bfd_error_invalid_operation);
9215 return FALSE;
9217 win:
9218 if (!child->vtable)
9220 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9221 if (!child->vtable)
9222 return FALSE;
9224 if (!h)
9226 /* This *should* only be the absolute section. It could potentially
9227 be that someone has defined a non-global vtable though, which
9228 would be bad. It isn't worth paging in the local symbols to be
9229 sure though; that case should simply be handled by the assembler. */
9231 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9233 else
9234 child->vtable->parent = h;
9236 return TRUE;
9239 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9241 bfd_boolean
9242 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9243 asection *sec ATTRIBUTE_UNUSED,
9244 struct elf_link_hash_entry *h,
9245 bfd_vma addend)
9247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9248 unsigned int log_file_align = bed->s->log_file_align;
9250 if (!h->vtable)
9252 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9253 if (!h->vtable)
9254 return FALSE;
9257 if (addend >= h->vtable->size)
9259 size_t size, bytes, file_align;
9260 bfd_boolean *ptr = h->vtable->used;
9262 /* While the symbol is undefined, we have to be prepared to handle
9263 a zero size. */
9264 file_align = 1 << log_file_align;
9265 if (h->root.type == bfd_link_hash_undefined)
9266 size = addend + file_align;
9267 else
9269 size = h->size;
9270 if (addend >= size)
9272 /* Oops! We've got a reference past the defined end of
9273 the table. This is probably a bug -- shall we warn? */
9274 size = addend + file_align;
9277 size = (size + file_align - 1) & -file_align;
9279 /* Allocate one extra entry for use as a "done" flag for the
9280 consolidation pass. */
9281 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9283 if (ptr)
9285 ptr = bfd_realloc (ptr - 1, bytes);
9287 if (ptr != NULL)
9289 size_t oldbytes;
9291 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9292 * sizeof (bfd_boolean));
9293 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9296 else
9297 ptr = bfd_zmalloc (bytes);
9299 if (ptr == NULL)
9300 return FALSE;
9302 /* And arrange for that done flag to be at index -1. */
9303 h->vtable->used = ptr + 1;
9304 h->vtable->size = size;
9307 h->vtable->used[addend >> log_file_align] = TRUE;
9309 return TRUE;
9312 struct alloc_got_off_arg {
9313 bfd_vma gotoff;
9314 unsigned int got_elt_size;
9317 /* We need a special top-level link routine to convert got reference counts
9318 to real got offsets. */
9320 static bfd_boolean
9321 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9323 struct alloc_got_off_arg *gofarg = arg;
9325 if (h->root.type == bfd_link_hash_warning)
9326 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9328 if (h->got.refcount > 0)
9330 h->got.offset = gofarg->gotoff;
9331 gofarg->gotoff += gofarg->got_elt_size;
9333 else
9334 h->got.offset = (bfd_vma) -1;
9336 return TRUE;
9339 /* And an accompanying bit to work out final got entry offsets once
9340 we're done. Should be called from final_link. */
9342 bfd_boolean
9343 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9344 struct bfd_link_info *info)
9346 bfd *i;
9347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9348 bfd_vma gotoff;
9349 unsigned int got_elt_size = bed->s->arch_size / 8;
9350 struct alloc_got_off_arg gofarg;
9352 if (! is_elf_hash_table (info->hash))
9353 return FALSE;
9355 /* The GOT offset is relative to the .got section, but the GOT header is
9356 put into the .got.plt section, if the backend uses it. */
9357 if (bed->want_got_plt)
9358 gotoff = 0;
9359 else
9360 gotoff = bed->got_header_size;
9362 /* Do the local .got entries first. */
9363 for (i = info->input_bfds; i; i = i->link_next)
9365 bfd_signed_vma *local_got;
9366 bfd_size_type j, locsymcount;
9367 Elf_Internal_Shdr *symtab_hdr;
9369 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9370 continue;
9372 local_got = elf_local_got_refcounts (i);
9373 if (!local_got)
9374 continue;
9376 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9377 if (elf_bad_symtab (i))
9378 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9379 else
9380 locsymcount = symtab_hdr->sh_info;
9382 for (j = 0; j < locsymcount; ++j)
9384 if (local_got[j] > 0)
9386 local_got[j] = gotoff;
9387 gotoff += got_elt_size;
9389 else
9390 local_got[j] = (bfd_vma) -1;
9394 /* Then the global .got entries. .plt refcounts are handled by
9395 adjust_dynamic_symbol */
9396 gofarg.gotoff = gotoff;
9397 gofarg.got_elt_size = got_elt_size;
9398 elf_link_hash_traverse (elf_hash_table (info),
9399 elf_gc_allocate_got_offsets,
9400 &gofarg);
9401 return TRUE;
9404 /* Many folk need no more in the way of final link than this, once
9405 got entry reference counting is enabled. */
9407 bfd_boolean
9408 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9410 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9411 return FALSE;
9413 /* Invoke the regular ELF backend linker to do all the work. */
9414 return bfd_elf_final_link (abfd, info);
9417 bfd_boolean
9418 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9420 struct elf_reloc_cookie *rcookie = cookie;
9422 if (rcookie->bad_symtab)
9423 rcookie->rel = rcookie->rels;
9425 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9427 unsigned long r_symndx;
9429 if (! rcookie->bad_symtab)
9430 if (rcookie->rel->r_offset > offset)
9431 return FALSE;
9432 if (rcookie->rel->r_offset != offset)
9433 continue;
9435 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9436 if (r_symndx == SHN_UNDEF)
9437 return TRUE;
9439 if (r_symndx >= rcookie->locsymcount
9440 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9442 struct elf_link_hash_entry *h;
9444 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9446 while (h->root.type == bfd_link_hash_indirect
9447 || h->root.type == bfd_link_hash_warning)
9448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9450 if ((h->root.type == bfd_link_hash_defined
9451 || h->root.type == bfd_link_hash_defweak)
9452 && elf_discarded_section (h->root.u.def.section))
9453 return TRUE;
9454 else
9455 return FALSE;
9457 else
9459 /* It's not a relocation against a global symbol,
9460 but it could be a relocation against a local
9461 symbol for a discarded section. */
9462 asection *isec;
9463 Elf_Internal_Sym *isym;
9465 /* Need to: get the symbol; get the section. */
9466 isym = &rcookie->locsyms[r_symndx];
9467 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9469 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9470 if (isec != NULL && elf_discarded_section (isec))
9471 return TRUE;
9474 return FALSE;
9476 return FALSE;
9479 /* Discard unneeded references to discarded sections.
9480 Returns TRUE if any section's size was changed. */
9481 /* This function assumes that the relocations are in sorted order,
9482 which is true for all known assemblers. */
9484 bfd_boolean
9485 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9487 struct elf_reloc_cookie cookie;
9488 asection *stab, *eh;
9489 Elf_Internal_Shdr *symtab_hdr;
9490 const struct elf_backend_data *bed;
9491 bfd *abfd;
9492 unsigned int count;
9493 bfd_boolean ret = FALSE;
9495 if (info->traditional_format
9496 || !is_elf_hash_table (info->hash))
9497 return FALSE;
9499 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9501 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9502 continue;
9504 bed = get_elf_backend_data (abfd);
9506 if ((abfd->flags & DYNAMIC) != 0)
9507 continue;
9509 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9510 if (info->relocatable
9511 || (eh != NULL
9512 && (eh->size == 0
9513 || bfd_is_abs_section (eh->output_section))))
9514 eh = NULL;
9516 stab = bfd_get_section_by_name (abfd, ".stab");
9517 if (stab != NULL
9518 && (stab->size == 0
9519 || bfd_is_abs_section (stab->output_section)
9520 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9521 stab = NULL;
9523 if (stab == NULL
9524 && eh == NULL
9525 && bed->elf_backend_discard_info == NULL)
9526 continue;
9528 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9529 cookie.abfd = abfd;
9530 cookie.sym_hashes = elf_sym_hashes (abfd);
9531 cookie.bad_symtab = elf_bad_symtab (abfd);
9532 if (cookie.bad_symtab)
9534 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9535 cookie.extsymoff = 0;
9537 else
9539 cookie.locsymcount = symtab_hdr->sh_info;
9540 cookie.extsymoff = symtab_hdr->sh_info;
9543 if (bed->s->arch_size == 32)
9544 cookie.r_sym_shift = 8;
9545 else
9546 cookie.r_sym_shift = 32;
9548 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9549 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9551 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9552 cookie.locsymcount, 0,
9553 NULL, NULL, NULL);
9554 if (cookie.locsyms == NULL)
9555 return FALSE;
9558 if (stab != NULL)
9560 cookie.rels = NULL;
9561 count = stab->reloc_count;
9562 if (count != 0)
9563 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9564 info->keep_memory);
9565 if (cookie.rels != NULL)
9567 cookie.rel = cookie.rels;
9568 cookie.relend = cookie.rels;
9569 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9570 if (_bfd_discard_section_stabs (abfd, stab,
9571 elf_section_data (stab)->sec_info,
9572 bfd_elf_reloc_symbol_deleted_p,
9573 &cookie))
9574 ret = TRUE;
9575 if (elf_section_data (stab)->relocs != cookie.rels)
9576 free (cookie.rels);
9580 if (eh != NULL)
9582 cookie.rels = NULL;
9583 count = eh->reloc_count;
9584 if (count != 0)
9585 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9586 info->keep_memory);
9587 cookie.rel = cookie.rels;
9588 cookie.relend = cookie.rels;
9589 if (cookie.rels != NULL)
9590 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9592 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9593 bfd_elf_reloc_symbol_deleted_p,
9594 &cookie))
9595 ret = TRUE;
9597 if (cookie.rels != NULL
9598 && elf_section_data (eh)->relocs != cookie.rels)
9599 free (cookie.rels);
9602 if (bed->elf_backend_discard_info != NULL
9603 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9604 ret = TRUE;
9606 if (cookie.locsyms != NULL
9607 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9609 if (! info->keep_memory)
9610 free (cookie.locsyms);
9611 else
9612 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9616 if (info->eh_frame_hdr
9617 && !info->relocatable
9618 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9619 ret = TRUE;
9621 return ret;
9624 void
9625 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9627 flagword flags;
9628 const char *name, *p;
9629 struct bfd_section_already_linked *l;
9630 struct bfd_section_already_linked_hash_entry *already_linked_list;
9631 asection *group;
9633 /* A single member comdat group section may be discarded by a
9634 linkonce section. See below. */
9635 if (sec->output_section == bfd_abs_section_ptr)
9636 return;
9638 flags = sec->flags;
9640 /* Check if it belongs to a section group. */
9641 group = elf_sec_group (sec);
9643 /* Return if it isn't a linkonce section nor a member of a group. A
9644 comdat group section also has SEC_LINK_ONCE set. */
9645 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9646 return;
9648 if (group)
9650 /* If this is the member of a single member comdat group, check if
9651 the group should be discarded. */
9652 if (elf_next_in_group (sec) == sec
9653 && (group->flags & SEC_LINK_ONCE) != 0)
9654 sec = group;
9655 else
9656 return;
9659 /* FIXME: When doing a relocatable link, we may have trouble
9660 copying relocations in other sections that refer to local symbols
9661 in the section being discarded. Those relocations will have to
9662 be converted somehow; as of this writing I'm not sure that any of
9663 the backends handle that correctly.
9665 It is tempting to instead not discard link once sections when
9666 doing a relocatable link (technically, they should be discarded
9667 whenever we are building constructors). However, that fails,
9668 because the linker winds up combining all the link once sections
9669 into a single large link once section, which defeats the purpose
9670 of having link once sections in the first place.
9672 Also, not merging link once sections in a relocatable link
9673 causes trouble for MIPS ELF, which relies on link once semantics
9674 to handle the .reginfo section correctly. */
9676 name = bfd_get_section_name (abfd, sec);
9678 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9679 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9680 p++;
9681 else
9682 p = name;
9684 already_linked_list = bfd_section_already_linked_table_lookup (p);
9686 for (l = already_linked_list->entry; l != NULL; l = l->next)
9688 /* We may have 3 different sections on the list: group section,
9689 comdat section and linkonce section. SEC may be a linkonce or
9690 group section. We match a group section with a group section,
9691 a linkonce section with a linkonce section, and ignore comdat
9692 section. */
9693 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9694 && strcmp (name, l->sec->name) == 0
9695 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9697 /* The section has already been linked. See if we should
9698 issue a warning. */
9699 switch (flags & SEC_LINK_DUPLICATES)
9701 default:
9702 abort ();
9704 case SEC_LINK_DUPLICATES_DISCARD:
9705 break;
9707 case SEC_LINK_DUPLICATES_ONE_ONLY:
9708 (*_bfd_error_handler)
9709 (_("%B: ignoring duplicate section `%A'"),
9710 abfd, sec);
9711 break;
9713 case SEC_LINK_DUPLICATES_SAME_SIZE:
9714 if (sec->size != l->sec->size)
9715 (*_bfd_error_handler)
9716 (_("%B: duplicate section `%A' has different size"),
9717 abfd, sec);
9718 break;
9720 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9721 if (sec->size != l->sec->size)
9722 (*_bfd_error_handler)
9723 (_("%B: duplicate section `%A' has different size"),
9724 abfd, sec);
9725 else if (sec->size != 0)
9727 bfd_byte *sec_contents, *l_sec_contents;
9729 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9730 (*_bfd_error_handler)
9731 (_("%B: warning: could not read contents of section `%A'"),
9732 abfd, sec);
9733 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9734 &l_sec_contents))
9735 (*_bfd_error_handler)
9736 (_("%B: warning: could not read contents of section `%A'"),
9737 l->sec->owner, l->sec);
9738 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9739 (*_bfd_error_handler)
9740 (_("%B: warning: duplicate section `%A' has different contents"),
9741 abfd, sec);
9743 if (sec_contents)
9744 free (sec_contents);
9745 if (l_sec_contents)
9746 free (l_sec_contents);
9748 break;
9751 /* Set the output_section field so that lang_add_section
9752 does not create a lang_input_section structure for this
9753 section. Since there might be a symbol in the section
9754 being discarded, we must retain a pointer to the section
9755 which we are really going to use. */
9756 sec->output_section = bfd_abs_section_ptr;
9757 sec->kept_section = l->sec;
9759 if (flags & SEC_GROUP)
9761 asection *first = elf_next_in_group (sec);
9762 asection *s = first;
9764 while (s != NULL)
9766 s->output_section = bfd_abs_section_ptr;
9767 /* Record which group discards it. */
9768 s->kept_section = l->sec;
9769 s = elf_next_in_group (s);
9770 /* These lists are circular. */
9771 if (s == first)
9772 break;
9776 return;
9780 if (group)
9782 /* If this is the member of a single member comdat group and the
9783 group hasn't be discarded, we check if it matches a linkonce
9784 section. We only record the discarded comdat group. Otherwise
9785 the undiscarded group will be discarded incorrectly later since
9786 itself has been recorded. */
9787 for (l = already_linked_list->entry; l != NULL; l = l->next)
9788 if ((l->sec->flags & SEC_GROUP) == 0
9789 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9790 && bfd_elf_match_symbols_in_sections (l->sec,
9791 elf_next_in_group (sec)))
9793 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9794 elf_next_in_group (sec)->kept_section = l->sec;
9795 group->output_section = bfd_abs_section_ptr;
9796 break;
9798 if (l == NULL)
9799 return;
9801 else
9802 /* There is no direct match. But for linkonce section, we should
9803 check if there is a match with comdat group member. We always
9804 record the linkonce section, discarded or not. */
9805 for (l = already_linked_list->entry; l != NULL; l = l->next)
9806 if (l->sec->flags & SEC_GROUP)
9808 asection *first = elf_next_in_group (l->sec);
9810 if (first != NULL
9811 && elf_next_in_group (first) == first
9812 && bfd_elf_match_symbols_in_sections (first, sec))
9814 sec->output_section = bfd_abs_section_ptr;
9815 sec->kept_section = l->sec;
9816 break;
9820 /* This is the first section with this name. Record it. */
9821 bfd_section_already_linked_table_insert (already_linked_list, sec);
9824 static void
9825 bfd_elf_set_symbol (struct elf_link_hash_entry *h, bfd_vma val)
9827 h->root.type = bfd_link_hash_defined;
9828 h->root.u.def.section = bfd_abs_section_ptr;
9829 h->root.u.def.value = val;
9830 h->def_regular = 1;
9831 h->type = STT_OBJECT;
9832 h->other = STV_HIDDEN | (h->other & ~ ELF_ST_VISIBILITY (-1));
9833 h->forced_local = 1;
9836 /* Set NAME to VAL if the symbol exists and is undefined. */
9838 void
9839 _bfd_elf_provide_symbol (struct bfd_link_info *info, const char *name,
9840 bfd_vma val)
9842 struct elf_link_hash_entry *h;
9844 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE,
9845 FALSE);
9846 if (h != NULL && (h->root.type == bfd_link_hash_undefined
9847 || h->root.type == bfd_link_hash_undefweak))
9848 bfd_elf_set_symbol (h, val);
9851 /* Set START and END to boundaries of SEC if they exist and are
9852 undefined. */
9854 void
9855 _bfd_elf_provide_section_bound_symbols (struct bfd_link_info *info,
9856 asection *sec,
9857 const char *start,
9858 const char *end)
9860 struct elf_link_hash_entry *hs, *he;
9861 bfd_vma start_val, end_val;
9862 bfd_boolean do_start, do_end;
9864 /* Check if we need them or not first. */
9865 hs = elf_link_hash_lookup (elf_hash_table (info), start, FALSE,
9866 FALSE, FALSE);
9867 do_start = (hs != NULL
9868 && (hs->root.type == bfd_link_hash_undefined
9869 || hs->root.type == bfd_link_hash_undefweak));
9871 he = elf_link_hash_lookup (elf_hash_table (info), end, FALSE,
9872 FALSE, FALSE);
9873 do_end = (he != NULL
9874 && (he->root.type == bfd_link_hash_undefined
9875 || he->root.type == bfd_link_hash_undefweak));
9877 if (!do_start && !do_end)
9878 return;
9880 if (sec != NULL)
9882 start_val = sec->vma;
9883 end_val = start_val + sec->size;
9885 else
9887 /* We have to choose those values very carefully. Some targets,
9888 like alpha, may have relocation overflow with 0. "__bss_start"
9889 should be defined in all cases. */
9890 struct elf_link_hash_entry *h
9891 = elf_link_hash_lookup (elf_hash_table (info), "__bss_start",
9892 FALSE, FALSE, FALSE);
9893 if (h != NULL && h->root.type == bfd_link_hash_defined)
9894 start_val = h->root.u.def.value;
9895 else
9896 start_val = 0;
9897 end_val = start_val;
9900 if (do_start)
9901 bfd_elf_set_symbol (hs, start_val);
9903 if (do_end)
9904 bfd_elf_set_symbol (he, end_val);