2005-02-11 Paul Brook <paul@codesourcery.com>
[binutils.git] / bfd / elflink.c
blob16a368ef37f8c32867b5b5ecc9cef61db70703d1
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 (abfd, ".got");
63 if (s == NULL
64 || !bfd_set_section_flags (abfd, s, flags)
65 || !bfd_set_section_alignment (abfd, s, ptralign))
66 return FALSE;
68 if (bed->want_got_plt)
70 s = bfd_make_section (abfd, ".got.plt");
71 if (s == NULL
72 || !bfd_set_section_flags (abfd, s, flags)
73 || !bfd_set_section_alignment (abfd, s, ptralign))
74 return FALSE;
77 if (bed->want_got_sym)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
83 bh = NULL;
84 if (!(_bfd_generic_link_add_one_symbol
85 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
86 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
87 return FALSE;
88 h = (struct elf_link_hash_entry *) bh;
89 h->def_regular = 1;
90 h->type = STT_OBJECT;
91 h->other = STV_HIDDEN;
93 if (! info->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info, h))
95 return FALSE;
97 elf_hash_table (info)->hgot = h;
100 /* The first bit of the global offset table is the header. */
101 s->size += bed->got_header_size + bed->got_symbol_offset;
103 return TRUE;
106 /* Create a strtab to hold the dynamic symbol names. */
107 static bfd_boolean
108 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
110 struct elf_link_hash_table *hash_table;
112 hash_table = elf_hash_table (info);
113 if (hash_table->dynobj == NULL)
114 hash_table->dynobj = abfd;
116 if (hash_table->dynstr == NULL)
118 hash_table->dynstr = _bfd_elf_strtab_init ();
119 if (hash_table->dynstr == NULL)
120 return FALSE;
122 return TRUE;
125 /* Create some sections which will be filled in with dynamic linking
126 information. ABFD is an input file which requires dynamic sections
127 to be created. The dynamic sections take up virtual memory space
128 when the final executable is run, so we need to create them before
129 addresses are assigned to the output sections. We work out the
130 actual contents and size of these sections later. */
132 bfd_boolean
133 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
135 flagword flags;
136 register asection *s;
137 struct elf_link_hash_entry *h;
138 struct bfd_link_hash_entry *bh;
139 const struct elf_backend_data *bed;
141 if (! is_elf_hash_table (info->hash))
142 return FALSE;
144 if (elf_hash_table (info)->dynamic_sections_created)
145 return TRUE;
147 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
148 return FALSE;
150 abfd = elf_hash_table (info)->dynobj;
151 bed = get_elf_backend_data (abfd);
153 flags = bed->dynamic_sec_flags;
155 /* A dynamically linked executable has a .interp section, but a
156 shared library does not. */
157 if (info->executable)
159 s = bfd_make_section (abfd, ".interp");
160 if (s == NULL
161 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
162 return FALSE;
165 if (! info->traditional_format)
167 s = bfd_make_section (abfd, ".eh_frame_hdr");
168 if (s == NULL
169 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170 || ! bfd_set_section_alignment (abfd, s, 2))
171 return FALSE;
172 elf_hash_table (info)->eh_info.hdr_sec = s;
175 /* Create sections to hold version informations. These are removed
176 if they are not needed. */
177 s = bfd_make_section (abfd, ".gnu.version_d");
178 if (s == NULL
179 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
180 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
181 return FALSE;
183 s = bfd_make_section (abfd, ".gnu.version");
184 if (s == NULL
185 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
186 || ! bfd_set_section_alignment (abfd, s, 1))
187 return FALSE;
189 s = bfd_make_section (abfd, ".gnu.version_r");
190 if (s == NULL
191 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
192 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
193 return FALSE;
195 s = bfd_make_section (abfd, ".dynsym");
196 if (s == NULL
197 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
198 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
199 return FALSE;
201 s = bfd_make_section (abfd, ".dynstr");
202 if (s == NULL
203 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
204 return FALSE;
206 s = bfd_make_section (abfd, ".dynamic");
207 if (s == NULL
208 || ! bfd_set_section_flags (abfd, s, flags)
209 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
210 return FALSE;
212 /* The special symbol _DYNAMIC is always set to the start of the
213 .dynamic section. We could set _DYNAMIC in a linker script, but we
214 only want to define it if we are, in fact, creating a .dynamic
215 section. We don't want to define it if there is no .dynamic
216 section, since on some ELF platforms the start up code examines it
217 to decide how to initialize the process. */
218 h = elf_link_hash_lookup (elf_hash_table (info), "_DYNAMIC",
219 FALSE, FALSE, FALSE);
220 if (h != NULL)
222 /* Zap symbol defined in an as-needed lib that wasn't linked.
223 This is a symptom of a larger problem: Absolute symbols
224 defined in shared libraries can't be overridden, because we
225 lose the link to the bfd which is via the symbol section. */
226 h->root.type = bfd_link_hash_new;
228 bh = &h->root;
229 if (! (_bfd_generic_link_add_one_symbol
230 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
231 get_elf_backend_data (abfd)->collect, &bh)))
232 return FALSE;
233 h = (struct elf_link_hash_entry *) bh;
234 h->def_regular = 1;
235 h->type = STT_OBJECT;
237 if (! info->executable
238 && ! bfd_elf_link_record_dynamic_symbol (info, h))
239 return FALSE;
241 s = bfd_make_section (abfd, ".hash");
242 if (s == NULL
243 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
244 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
245 return FALSE;
246 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
248 /* Let the backend create the rest of the sections. This lets the
249 backend set the right flags. The backend will normally create
250 the .got and .plt sections. */
251 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
252 return FALSE;
254 elf_hash_table (info)->dynamic_sections_created = TRUE;
256 return TRUE;
259 /* Create dynamic sections when linking against a dynamic object. */
261 bfd_boolean
262 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
264 flagword flags, pltflags;
265 asection *s;
266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
268 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
269 .rel[a].bss sections. */
270 flags = bed->dynamic_sec_flags;
272 pltflags = flags;
273 if (bed->plt_not_loaded)
274 /* We do not clear SEC_ALLOC here because we still want the OS to
275 allocate space for the section; it's just that there's nothing
276 to read in from the object file. */
277 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
278 else
279 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
280 if (bed->plt_readonly)
281 pltflags |= SEC_READONLY;
283 s = bfd_make_section (abfd, ".plt");
284 if (s == NULL
285 || ! bfd_set_section_flags (abfd, s, pltflags)
286 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
287 return FALSE;
289 if (bed->want_plt_sym)
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
292 .plt section. */
293 struct elf_link_hash_entry *h;
294 struct bfd_link_hash_entry *bh = NULL;
296 if (! (_bfd_generic_link_add_one_symbol
297 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
298 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
299 return FALSE;
300 h = (struct elf_link_hash_entry *) bh;
301 h->def_regular = 1;
302 h->type = STT_OBJECT;
304 if (! info->executable
305 && ! bfd_elf_link_record_dynamic_symbol (info, h))
306 return FALSE;
309 s = bfd_make_section (abfd,
310 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
311 if (s == NULL
312 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
313 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
314 return FALSE;
316 if (! _bfd_elf_create_got_section (abfd, info))
317 return FALSE;
319 if (bed->want_dynbss)
321 /* The .dynbss section is a place to put symbols which are defined
322 by dynamic objects, are referenced by regular objects, and are
323 not functions. We must allocate space for them in the process
324 image and use a R_*_COPY reloc to tell the dynamic linker to
325 initialize them at run time. The linker script puts the .dynbss
326 section into the .bss section of the final image. */
327 s = bfd_make_section (abfd, ".dynbss");
328 if (s == NULL
329 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
330 return FALSE;
332 /* The .rel[a].bss section holds copy relocs. This section is not
333 normally needed. We need to create it here, though, so that the
334 linker will map it to an output section. We can't just create it
335 only if we need it, because we will not know whether we need it
336 until we have seen all the input files, and the first time the
337 main linker code calls BFD after examining all the input files
338 (size_dynamic_sections) the input sections have already been
339 mapped to the output sections. If the section turns out not to
340 be needed, we can discard it later. We will never need this
341 section when generating a shared object, since they do not use
342 copy relocs. */
343 if (! info->shared)
345 s = bfd_make_section (abfd,
346 (bed->default_use_rela_p
347 ? ".rela.bss" : ".rel.bss"));
348 if (s == NULL
349 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
350 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
351 return FALSE;
355 return TRUE;
358 /* Record a new dynamic symbol. We record the dynamic symbols as we
359 read the input files, since we need to have a list of all of them
360 before we can determine the final sizes of the output sections.
361 Note that we may actually call this function even though we are not
362 going to output any dynamic symbols; in some cases we know that a
363 symbol should be in the dynamic symbol table, but only if there is
364 one. */
366 bfd_boolean
367 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
368 struct elf_link_hash_entry *h)
370 if (h->dynindx == -1)
372 struct elf_strtab_hash *dynstr;
373 char *p;
374 const char *name;
375 bfd_size_type indx;
377 /* XXX: The ABI draft says the linker must turn hidden and
378 internal symbols into STB_LOCAL symbols when producing the
379 DSO. However, if ld.so honors st_other in the dynamic table,
380 this would not be necessary. */
381 switch (ELF_ST_VISIBILITY (h->other))
383 case STV_INTERNAL:
384 case STV_HIDDEN:
385 if (h->root.type != bfd_link_hash_undefined
386 && h->root.type != bfd_link_hash_undefweak)
388 h->forced_local = 1;
389 if (!elf_hash_table (info)->is_relocatable_executable)
390 return TRUE;
393 default:
394 break;
397 h->dynindx = elf_hash_table (info)->dynsymcount;
398 ++elf_hash_table (info)->dynsymcount;
400 dynstr = elf_hash_table (info)->dynstr;
401 if (dynstr == NULL)
403 /* Create a strtab to hold the dynamic symbol names. */
404 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
405 if (dynstr == NULL)
406 return FALSE;
409 /* We don't put any version information in the dynamic string
410 table. */
411 name = h->root.root.string;
412 p = strchr (name, ELF_VER_CHR);
413 if (p != NULL)
414 /* We know that the p points into writable memory. In fact,
415 there are only a few symbols that have read-only names, being
416 those like _GLOBAL_OFFSET_TABLE_ that are created specially
417 by the backends. Most symbols will have names pointing into
418 an ELF string table read from a file, or to objalloc memory. */
419 *p = 0;
421 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
423 if (p != NULL)
424 *p = ELF_VER_CHR;
426 if (indx == (bfd_size_type) -1)
427 return FALSE;
428 h->dynstr_index = indx;
431 return TRUE;
434 /* Record an assignment to a symbol made by a linker script. We need
435 this in case some dynamic object refers to this symbol. */
437 bfd_boolean
438 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
439 struct bfd_link_info *info,
440 const char *name,
441 bfd_boolean provide)
443 struct elf_link_hash_entry *h;
444 struct elf_link_hash_table *htab;
446 if (!is_elf_hash_table (info->hash))
447 return TRUE;
449 htab = elf_hash_table (info);
450 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
451 if (h == NULL)
452 return provide;
454 /* Since we're defining the symbol, don't let it seem to have not
455 been defined. record_dynamic_symbol and size_dynamic_sections
456 may depend on this. */
457 if (h->root.type == bfd_link_hash_undefweak
458 || h->root.type == bfd_link_hash_undefined)
460 h->root.type = bfd_link_hash_new;
461 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
462 bfd_link_repair_undef_list (&htab->root);
465 if (h->root.type == bfd_link_hash_new)
466 h->non_elf = 0;
468 /* If this symbol is being provided by the linker script, and it is
469 currently defined by a dynamic object, but not by a regular
470 object, then mark it as undefined so that the generic linker will
471 force the correct value. */
472 if (provide
473 && h->def_dynamic
474 && !h->def_regular)
475 h->root.type = bfd_link_hash_undefined;
477 /* If this symbol is not being provided by the linker script, and it is
478 currently defined by a dynamic object, but not by a regular object,
479 then clear out any version information because the symbol will not be
480 associated with the dynamic object any more. */
481 if (!provide
482 && h->def_dynamic
483 && !h->def_regular)
484 h->verinfo.verdef = NULL;
486 h->def_regular = 1;
488 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
489 and executables. */
490 if (!info->relocatable
491 && h->dynindx != -1
492 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
493 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
494 h->forced_local = 1;
496 if ((h->def_dynamic
497 || h->ref_dynamic
498 || info->shared
499 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
500 && h->dynindx == -1)
502 if (! bfd_elf_link_record_dynamic_symbol (info, h))
503 return FALSE;
505 /* If this is a weak defined symbol, and we know a corresponding
506 real symbol from the same dynamic object, make sure the real
507 symbol is also made into a dynamic symbol. */
508 if (h->u.weakdef != NULL
509 && h->u.weakdef->dynindx == -1)
511 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
512 return FALSE;
516 return TRUE;
519 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
520 success, and 2 on a failure caused by attempting to record a symbol
521 in a discarded section, eg. a discarded link-once section symbol. */
524 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
525 bfd *input_bfd,
526 long input_indx)
528 bfd_size_type amt;
529 struct elf_link_local_dynamic_entry *entry;
530 struct elf_link_hash_table *eht;
531 struct elf_strtab_hash *dynstr;
532 unsigned long dynstr_index;
533 char *name;
534 Elf_External_Sym_Shndx eshndx;
535 char esym[sizeof (Elf64_External_Sym)];
537 if (! is_elf_hash_table (info->hash))
538 return 0;
540 /* See if the entry exists already. */
541 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
542 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
543 return 1;
545 amt = sizeof (*entry);
546 entry = bfd_alloc (input_bfd, amt);
547 if (entry == NULL)
548 return 0;
550 /* Go find the symbol, so that we can find it's name. */
551 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
552 1, input_indx, &entry->isym, esym, &eshndx))
554 bfd_release (input_bfd, entry);
555 return 0;
558 if (entry->isym.st_shndx != SHN_UNDEF
559 && (entry->isym.st_shndx < SHN_LORESERVE
560 || entry->isym.st_shndx > SHN_HIRESERVE))
562 asection *s;
564 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
565 if (s == NULL || bfd_is_abs_section (s->output_section))
567 /* We can still bfd_release here as nothing has done another
568 bfd_alloc. We can't do this later in this function. */
569 bfd_release (input_bfd, entry);
570 return 2;
574 name = (bfd_elf_string_from_elf_section
575 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
576 entry->isym.st_name));
578 dynstr = elf_hash_table (info)->dynstr;
579 if (dynstr == NULL)
581 /* Create a strtab to hold the dynamic symbol names. */
582 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
583 if (dynstr == NULL)
584 return 0;
587 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
588 if (dynstr_index == (unsigned long) -1)
589 return 0;
590 entry->isym.st_name = dynstr_index;
592 eht = elf_hash_table (info);
594 entry->next = eht->dynlocal;
595 eht->dynlocal = entry;
596 entry->input_bfd = input_bfd;
597 entry->input_indx = input_indx;
598 eht->dynsymcount++;
600 /* Whatever binding the symbol had before, it's now local. */
601 entry->isym.st_info
602 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
604 /* The dynindx will be set at the end of size_dynamic_sections. */
606 return 1;
609 /* Return the dynindex of a local dynamic symbol. */
611 long
612 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
613 bfd *input_bfd,
614 long input_indx)
616 struct elf_link_local_dynamic_entry *e;
618 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
619 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
620 return e->dynindx;
621 return -1;
624 /* This function is used to renumber the dynamic symbols, if some of
625 them are removed because they are marked as local. This is called
626 via elf_link_hash_traverse. */
628 static bfd_boolean
629 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
630 void *data)
632 size_t *count = data;
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
637 if (h->forced_local)
638 return TRUE;
640 if (h->dynindx != -1)
641 h->dynindx = ++(*count);
643 return TRUE;
647 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
648 STB_LOCAL binding. */
650 static bfd_boolean
651 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
652 void *data)
654 size_t *count = data;
656 if (h->root.type == bfd_link_hash_warning)
657 h = (struct elf_link_hash_entry *) h->root.u.i.link;
659 if (!h->forced_local)
660 return TRUE;
662 if (h->dynindx != -1)
663 h->dynindx = ++(*count);
665 return TRUE;
668 /* Return true if the dynamic symbol for a given section should be
669 omitted when creating a shared library. */
670 bfd_boolean
671 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
672 struct bfd_link_info *info,
673 asection *p)
675 switch (elf_section_data (p)->this_hdr.sh_type)
677 case SHT_PROGBITS:
678 case SHT_NOBITS:
679 /* If sh_type is yet undecided, assume it could be
680 SHT_PROGBITS/SHT_NOBITS. */
681 case SHT_NULL:
682 if (strcmp (p->name, ".got") == 0
683 || strcmp (p->name, ".got.plt") == 0
684 || strcmp (p->name, ".plt") == 0)
686 asection *ip;
687 bfd *dynobj = elf_hash_table (info)->dynobj;
689 if (dynobj != NULL
690 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
691 && (ip->flags & SEC_LINKER_CREATED)
692 && ip->output_section == p)
693 return TRUE;
695 return FALSE;
697 /* There shouldn't be section relative relocations
698 against any other section. */
699 default:
700 return TRUE;
704 /* Assign dynsym indices. In a shared library we generate a section
705 symbol for each output section, which come first. Next come symbols
706 which have been forced to local binding. Then all of the back-end
707 allocated local dynamic syms, followed by the rest of the global
708 symbols. */
710 unsigned long
711 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
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;
726 elf_link_hash_traverse (elf_hash_table (info),
727 elf_link_renumber_local_hash_table_dynsyms,
728 &dynsymcount);
730 if (elf_hash_table (info)->dynlocal)
732 struct elf_link_local_dynamic_entry *p;
733 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
734 p->dynindx = ++dynsymcount;
737 elf_link_hash_traverse (elf_hash_table (info),
738 elf_link_renumber_hash_table_dynsyms,
739 &dynsymcount);
741 /* There is an unused NULL entry at the head of the table which
742 we must account for in our count. Unless there weren't any
743 symbols, which means we'll have no table at all. */
744 if (dynsymcount != 0)
745 ++dynsymcount;
747 return elf_hash_table (info)->dynsymcount = dynsymcount;
750 /* This function is called when we want to define a new symbol. It
751 handles the various cases which arise when we find a definition in
752 a dynamic object, or when there is already a definition in a
753 dynamic object. The new symbol is described by NAME, SYM, PSEC,
754 and PVALUE. We set SYM_HASH to the hash table entry. We set
755 OVERRIDE if the old symbol is overriding a new definition. We set
756 TYPE_CHANGE_OK if it is OK for the type to change. We set
757 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
758 change, we mean that we shouldn't warn if the type or size does
759 change. */
761 bfd_boolean
762 _bfd_elf_merge_symbol (bfd *abfd,
763 struct bfd_link_info *info,
764 const char *name,
765 Elf_Internal_Sym *sym,
766 asection **psec,
767 bfd_vma *pvalue,
768 struct elf_link_hash_entry **sym_hash,
769 bfd_boolean *skip,
770 bfd_boolean *override,
771 bfd_boolean *type_change_ok,
772 bfd_boolean *size_change_ok)
774 asection *sec, *oldsec;
775 struct elf_link_hash_entry *h;
776 struct elf_link_hash_entry *flip;
777 int bind;
778 bfd *oldbfd;
779 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
780 bfd_boolean newweak, oldweak;
782 *skip = FALSE;
783 *override = FALSE;
785 sec = *psec;
786 bind = ELF_ST_BIND (sym->st_info);
788 if (! bfd_is_und_section (sec))
789 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
790 else
791 h = ((struct elf_link_hash_entry *)
792 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
793 if (h == NULL)
794 return FALSE;
795 *sym_hash = h;
797 /* This code is for coping with dynamic objects, and is only useful
798 if we are doing an ELF link. */
799 if (info->hash->creator != abfd->xvec)
800 return TRUE;
802 /* For merging, we only care about real symbols. */
804 while (h->root.type == bfd_link_hash_indirect
805 || h->root.type == bfd_link_hash_warning)
806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
808 /* If we just created the symbol, mark it as being an ELF symbol.
809 Other than that, there is nothing to do--there is no merge issue
810 with a newly defined symbol--so we just return. */
812 if (h->root.type == bfd_link_hash_new)
814 h->non_elf = 0;
815 return TRUE;
818 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
819 existing symbol. */
821 switch (h->root.type)
823 default:
824 oldbfd = NULL;
825 oldsec = NULL;
826 break;
828 case bfd_link_hash_undefined:
829 case bfd_link_hash_undefweak:
830 oldbfd = h->root.u.undef.abfd;
831 oldsec = NULL;
832 break;
834 case bfd_link_hash_defined:
835 case bfd_link_hash_defweak:
836 oldbfd = h->root.u.def.section->owner;
837 oldsec = h->root.u.def.section;
838 break;
840 case bfd_link_hash_common:
841 oldbfd = h->root.u.c.p->section->owner;
842 oldsec = h->root.u.c.p->section;
843 break;
846 /* In cases involving weak versioned symbols, we may wind up trying
847 to merge a symbol with itself. Catch that here, to avoid the
848 confusion that results if we try to override a symbol with
849 itself. The additional tests catch cases like
850 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
851 dynamic object, which we do want to handle here. */
852 if (abfd == oldbfd
853 && ((abfd->flags & DYNAMIC) == 0
854 || !h->def_regular))
855 return TRUE;
857 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
858 respectively, is from a dynamic object. */
860 if ((abfd->flags & DYNAMIC) != 0)
861 newdyn = TRUE;
862 else
863 newdyn = FALSE;
865 if (oldbfd != NULL)
866 olddyn = (oldbfd->flags & DYNAMIC) != 0;
867 else
869 asection *hsec;
871 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
872 indices used by MIPS ELF. */
873 switch (h->root.type)
875 default:
876 hsec = NULL;
877 break;
879 case bfd_link_hash_defined:
880 case bfd_link_hash_defweak:
881 hsec = h->root.u.def.section;
882 break;
884 case bfd_link_hash_common:
885 hsec = h->root.u.c.p->section;
886 break;
889 if (hsec == NULL)
890 olddyn = FALSE;
891 else
892 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
895 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
896 respectively, appear to be a definition rather than reference. */
898 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
899 newdef = FALSE;
900 else
901 newdef = TRUE;
903 if (h->root.type == bfd_link_hash_undefined
904 || h->root.type == bfd_link_hash_undefweak
905 || h->root.type == bfd_link_hash_common)
906 olddef = FALSE;
907 else
908 olddef = TRUE;
910 /* Check TLS symbol. */
911 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
912 && ELF_ST_TYPE (sym->st_info) != h->type)
914 bfd *ntbfd, *tbfd;
915 bfd_boolean ntdef, tdef;
916 asection *ntsec, *tsec;
918 if (h->type == STT_TLS)
920 ntbfd = abfd;
921 ntsec = sec;
922 ntdef = newdef;
923 tbfd = oldbfd;
924 tsec = oldsec;
925 tdef = olddef;
927 else
929 ntbfd = oldbfd;
930 ntsec = oldsec;
931 ntdef = olddef;
932 tbfd = abfd;
933 tsec = sec;
934 tdef = newdef;
937 if (tdef && ntdef)
938 (*_bfd_error_handler)
939 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
940 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
941 else if (!tdef && !ntdef)
942 (*_bfd_error_handler)
943 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
944 tbfd, ntbfd, h->root.root.string);
945 else if (tdef)
946 (*_bfd_error_handler)
947 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
948 tbfd, tsec, ntbfd, h->root.root.string);
949 else
950 (*_bfd_error_handler)
951 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
952 tbfd, ntbfd, ntsec, h->root.root.string);
954 bfd_set_error (bfd_error_bad_value);
955 return FALSE;
958 /* We need to remember if a symbol has a definition in a dynamic
959 object or is weak in all dynamic objects. Internal and hidden
960 visibility will make it unavailable to dynamic objects. */
961 if (newdyn && !h->dynamic_def)
963 if (!bfd_is_und_section (sec))
964 h->dynamic_def = 1;
965 else
967 /* Check if this symbol is weak in all dynamic objects. If it
968 is the first time we see it in a dynamic object, we mark
969 if it is weak. Otherwise, we clear it. */
970 if (!h->ref_dynamic)
972 if (bind == STB_WEAK)
973 h->dynamic_weak = 1;
975 else if (bind != STB_WEAK)
976 h->dynamic_weak = 0;
980 /* If the old symbol has non-default visibility, we ignore the new
981 definition from a dynamic object. */
982 if (newdyn
983 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
984 && !bfd_is_und_section (sec))
986 *skip = TRUE;
987 /* Make sure this symbol is dynamic. */
988 h->ref_dynamic = 1;
989 /* A protected symbol has external availability. Make sure it is
990 recorded as dynamic.
992 FIXME: Should we check type and size for protected symbol? */
993 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
994 return bfd_elf_link_record_dynamic_symbol (info, h);
995 else
996 return TRUE;
998 else if (!newdyn
999 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1000 && h->def_dynamic)
1002 /* If the new symbol with non-default visibility comes from a
1003 relocatable file and the old definition comes from a dynamic
1004 object, we remove the old definition. */
1005 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1006 h = *sym_hash;
1008 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1009 && bfd_is_und_section (sec))
1011 /* If the new symbol is undefined and the old symbol was
1012 also undefined before, we need to make sure
1013 _bfd_generic_link_add_one_symbol doesn't mess
1014 up the linker hash table undefs list. Since the old
1015 definition came from a dynamic object, it is still on the
1016 undefs list. */
1017 h->root.type = bfd_link_hash_undefined;
1018 h->root.u.undef.abfd = abfd;
1020 else
1022 h->root.type = bfd_link_hash_new;
1023 h->root.u.undef.abfd = NULL;
1026 if (h->def_dynamic)
1028 h->def_dynamic = 0;
1029 h->ref_dynamic = 1;
1030 h->dynamic_def = 1;
1032 /* FIXME: Should we check type and size for protected symbol? */
1033 h->size = 0;
1034 h->type = 0;
1035 return TRUE;
1038 /* Differentiate strong and weak symbols. */
1039 newweak = bind == STB_WEAK;
1040 oldweak = (h->root.type == bfd_link_hash_defweak
1041 || h->root.type == bfd_link_hash_undefweak);
1043 /* If a new weak symbol definition comes from a regular file and the
1044 old symbol comes from a dynamic library, we treat the new one as
1045 strong. Similarly, an old weak symbol definition from a regular
1046 file is treated as strong when the new symbol comes from a dynamic
1047 library. Further, an old weak symbol from a dynamic library is
1048 treated as strong if the new symbol is from a dynamic library.
1049 This reflects the way glibc's ld.so works.
1051 Do this before setting *type_change_ok or *size_change_ok so that
1052 we warn properly when dynamic library symbols are overridden. */
1054 if (newdef && !newdyn && olddyn)
1055 newweak = FALSE;
1056 if (olddef && newdyn)
1057 oldweak = FALSE;
1059 /* It's OK to change the type if either the existing symbol or the
1060 new symbol is weak. A type change is also OK if the old symbol
1061 is undefined and the new symbol is defined. */
1063 if (oldweak
1064 || newweak
1065 || (newdef
1066 && h->root.type == bfd_link_hash_undefined))
1067 *type_change_ok = TRUE;
1069 /* It's OK to change the size if either the existing symbol or the
1070 new symbol is weak, or if the old symbol is undefined. */
1072 if (*type_change_ok
1073 || h->root.type == bfd_link_hash_undefined)
1074 *size_change_ok = TRUE;
1076 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1077 symbol, respectively, appears to be a common symbol in a dynamic
1078 object. If a symbol appears in an uninitialized section, and is
1079 not weak, and is not a function, then it may be a common symbol
1080 which was resolved when the dynamic object was created. We want
1081 to treat such symbols specially, because they raise special
1082 considerations when setting the symbol size: if the symbol
1083 appears as a common symbol in a regular object, and the size in
1084 the regular object is larger, we must make sure that we use the
1085 larger size. This problematic case can always be avoided in C,
1086 but it must be handled correctly when using Fortran shared
1087 libraries.
1089 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1090 likewise for OLDDYNCOMMON and OLDDEF.
1092 Note that this test is just a heuristic, and that it is quite
1093 possible to have an uninitialized symbol in a shared object which
1094 is really a definition, rather than a common symbol. This could
1095 lead to some minor confusion when the symbol really is a common
1096 symbol in some regular object. However, I think it will be
1097 harmless. */
1099 if (newdyn
1100 && newdef
1101 && !newweak
1102 && (sec->flags & SEC_ALLOC) != 0
1103 && (sec->flags & SEC_LOAD) == 0
1104 && sym->st_size > 0
1105 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1106 newdyncommon = TRUE;
1107 else
1108 newdyncommon = FALSE;
1110 if (olddyn
1111 && olddef
1112 && h->root.type == bfd_link_hash_defined
1113 && h->def_dynamic
1114 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1115 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1116 && h->size > 0
1117 && h->type != STT_FUNC)
1118 olddyncommon = TRUE;
1119 else
1120 olddyncommon = FALSE;
1122 /* If both the old and the new symbols look like common symbols in a
1123 dynamic object, set the size of the symbol to the larger of the
1124 two. */
1126 if (olddyncommon
1127 && newdyncommon
1128 && sym->st_size != h->size)
1130 /* Since we think we have two common symbols, issue a multiple
1131 common warning if desired. Note that we only warn if the
1132 size is different. If the size is the same, we simply let
1133 the old symbol override the new one as normally happens with
1134 symbols defined in dynamic objects. */
1136 if (! ((*info->callbacks->multiple_common)
1137 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1138 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1139 return FALSE;
1141 if (sym->st_size > h->size)
1142 h->size = sym->st_size;
1144 *size_change_ok = TRUE;
1147 /* If we are looking at a dynamic object, and we have found a
1148 definition, we need to see if the symbol was already defined by
1149 some other object. If so, we want to use the existing
1150 definition, and we do not want to report a multiple symbol
1151 definition error; we do this by clobbering *PSEC to be
1152 bfd_und_section_ptr.
1154 We treat a common symbol as a definition if the symbol in the
1155 shared library is a function, since common symbols always
1156 represent variables; this can cause confusion in principle, but
1157 any such confusion would seem to indicate an erroneous program or
1158 shared library. We also permit a common symbol in a regular
1159 object to override a weak symbol in a shared object. */
1161 if (newdyn
1162 && newdef
1163 && (olddef
1164 || (h->root.type == bfd_link_hash_common
1165 && (newweak
1166 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1168 *override = TRUE;
1169 newdef = FALSE;
1170 newdyncommon = FALSE;
1172 *psec = sec = bfd_und_section_ptr;
1173 *size_change_ok = TRUE;
1175 /* If we get here when the old symbol is a common symbol, then
1176 we are explicitly letting it override a weak symbol or
1177 function in a dynamic object, and we don't want to warn about
1178 a type change. If the old symbol is a defined symbol, a type
1179 change warning may still be appropriate. */
1181 if (h->root.type == bfd_link_hash_common)
1182 *type_change_ok = TRUE;
1185 /* Handle the special case of an old common symbol merging with a
1186 new symbol which looks like a common symbol in a shared object.
1187 We change *PSEC and *PVALUE to make the new symbol look like a
1188 common symbol, and let _bfd_generic_link_add_one_symbol will do
1189 the right thing. */
1191 if (newdyncommon
1192 && h->root.type == bfd_link_hash_common)
1194 *override = TRUE;
1195 newdef = FALSE;
1196 newdyncommon = FALSE;
1197 *pvalue = sym->st_size;
1198 *psec = sec = bfd_com_section_ptr;
1199 *size_change_ok = TRUE;
1202 /* If the old symbol is from a dynamic object, and the new symbol is
1203 a definition which is not from a dynamic object, then the new
1204 symbol overrides the old symbol. Symbols from regular files
1205 always take precedence over symbols from dynamic objects, even if
1206 they are defined after the dynamic object in the link.
1208 As above, we again permit a common symbol in a regular object to
1209 override a definition in a shared object if the shared object
1210 symbol is a function or is weak. */
1212 flip = NULL;
1213 if (!newdyn
1214 && (newdef
1215 || (bfd_is_com_section (sec)
1216 && (oldweak
1217 || h->type == STT_FUNC)))
1218 && olddyn
1219 && olddef
1220 && h->def_dynamic)
1222 /* Change the hash table entry to undefined, and let
1223 _bfd_generic_link_add_one_symbol do the right thing with the
1224 new definition. */
1226 h->root.type = bfd_link_hash_undefined;
1227 h->root.u.undef.abfd = h->root.u.def.section->owner;
1228 *size_change_ok = TRUE;
1230 olddef = FALSE;
1231 olddyncommon = FALSE;
1233 /* We again permit a type change when a common symbol may be
1234 overriding a function. */
1236 if (bfd_is_com_section (sec))
1237 *type_change_ok = TRUE;
1239 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1240 flip = *sym_hash;
1241 else
1242 /* This union may have been set to be non-NULL when this symbol
1243 was seen in a dynamic object. We must force the union to be
1244 NULL, so that it is correct for a regular symbol. */
1245 h->verinfo.vertree = NULL;
1248 /* Handle the special case of a new common symbol merging with an
1249 old symbol that looks like it might be a common symbol defined in
1250 a shared object. Note that we have already handled the case in
1251 which a new common symbol should simply override the definition
1252 in the shared library. */
1254 if (! newdyn
1255 && bfd_is_com_section (sec)
1256 && olddyncommon)
1258 /* It would be best if we could set the hash table entry to a
1259 common symbol, but we don't know what to use for the section
1260 or the alignment. */
1261 if (! ((*info->callbacks->multiple_common)
1262 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1263 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1264 return FALSE;
1266 /* If the presumed common symbol in the dynamic object is
1267 larger, pretend that the new symbol has its size. */
1269 if (h->size > *pvalue)
1270 *pvalue = h->size;
1272 /* FIXME: We no longer know the alignment required by the symbol
1273 in the dynamic object, so we just wind up using the one from
1274 the regular object. */
1276 olddef = FALSE;
1277 olddyncommon = FALSE;
1279 h->root.type = bfd_link_hash_undefined;
1280 h->root.u.undef.abfd = h->root.u.def.section->owner;
1282 *size_change_ok = TRUE;
1283 *type_change_ok = TRUE;
1285 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1286 flip = *sym_hash;
1287 else
1288 h->verinfo.vertree = NULL;
1291 if (flip != NULL)
1293 /* Handle the case where we had a versioned symbol in a dynamic
1294 library and now find a definition in a normal object. In this
1295 case, we make the versioned symbol point to the normal one. */
1296 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1297 flip->root.type = h->root.type;
1298 h->root.type = bfd_link_hash_indirect;
1299 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1300 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1301 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1302 if (h->def_dynamic)
1304 h->def_dynamic = 0;
1305 flip->ref_dynamic = 1;
1309 return TRUE;
1312 /* This function is called to create an indirect symbol from the
1313 default for the symbol with the default version if needed. The
1314 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1315 set DYNSYM if the new indirect symbol is dynamic. */
1317 bfd_boolean
1318 _bfd_elf_add_default_symbol (bfd *abfd,
1319 struct bfd_link_info *info,
1320 struct elf_link_hash_entry *h,
1321 const char *name,
1322 Elf_Internal_Sym *sym,
1323 asection **psec,
1324 bfd_vma *value,
1325 bfd_boolean *dynsym,
1326 bfd_boolean override)
1328 bfd_boolean type_change_ok;
1329 bfd_boolean size_change_ok;
1330 bfd_boolean skip;
1331 char *shortname;
1332 struct elf_link_hash_entry *hi;
1333 struct bfd_link_hash_entry *bh;
1334 const struct elf_backend_data *bed;
1335 bfd_boolean collect;
1336 bfd_boolean dynamic;
1337 char *p;
1338 size_t len, shortlen;
1339 asection *sec;
1341 /* If this symbol has a version, and it is the default version, we
1342 create an indirect symbol from the default name to the fully
1343 decorated name. This will cause external references which do not
1344 specify a version to be bound to this version of the symbol. */
1345 p = strchr (name, ELF_VER_CHR);
1346 if (p == NULL || p[1] != ELF_VER_CHR)
1347 return TRUE;
1349 if (override)
1351 /* We are overridden by an old definition. We need to check if we
1352 need to create the indirect symbol from the default name. */
1353 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1354 FALSE, FALSE);
1355 BFD_ASSERT (hi != NULL);
1356 if (hi == h)
1357 return TRUE;
1358 while (hi->root.type == bfd_link_hash_indirect
1359 || hi->root.type == bfd_link_hash_warning)
1361 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1362 if (hi == h)
1363 return TRUE;
1367 bed = get_elf_backend_data (abfd);
1368 collect = bed->collect;
1369 dynamic = (abfd->flags & DYNAMIC) != 0;
1371 shortlen = p - name;
1372 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1373 if (shortname == NULL)
1374 return FALSE;
1375 memcpy (shortname, name, shortlen);
1376 shortname[shortlen] = '\0';
1378 /* We are going to create a new symbol. Merge it with any existing
1379 symbol with this name. For the purposes of the merge, act as
1380 though we were defining the symbol we just defined, although we
1381 actually going to define an indirect symbol. */
1382 type_change_ok = FALSE;
1383 size_change_ok = FALSE;
1384 sec = *psec;
1385 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1386 &hi, &skip, &override, &type_change_ok,
1387 &size_change_ok))
1388 return FALSE;
1390 if (skip)
1391 goto nondefault;
1393 if (! override)
1395 bh = &hi->root;
1396 if (! (_bfd_generic_link_add_one_symbol
1397 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1398 0, name, FALSE, collect, &bh)))
1399 return FALSE;
1400 hi = (struct elf_link_hash_entry *) bh;
1402 else
1404 /* In this case the symbol named SHORTNAME is overriding the
1405 indirect symbol we want to add. We were planning on making
1406 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1407 is the name without a version. NAME is the fully versioned
1408 name, and it is the default version.
1410 Overriding means that we already saw a definition for the
1411 symbol SHORTNAME in a regular object, and it is overriding
1412 the symbol defined in the dynamic object.
1414 When this happens, we actually want to change NAME, the
1415 symbol we just added, to refer to SHORTNAME. This will cause
1416 references to NAME in the shared object to become references
1417 to SHORTNAME in the regular object. This is what we expect
1418 when we override a function in a shared object: that the
1419 references in the shared object will be mapped to the
1420 definition in the regular object. */
1422 while (hi->root.type == bfd_link_hash_indirect
1423 || hi->root.type == bfd_link_hash_warning)
1424 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1426 h->root.type = bfd_link_hash_indirect;
1427 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1428 if (h->def_dynamic)
1430 h->def_dynamic = 0;
1431 hi->ref_dynamic = 1;
1432 if (hi->ref_regular
1433 || hi->def_regular)
1435 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1436 return FALSE;
1440 /* Now set HI to H, so that the following code will set the
1441 other fields correctly. */
1442 hi = h;
1445 /* If there is a duplicate definition somewhere, then HI may not
1446 point to an indirect symbol. We will have reported an error to
1447 the user in that case. */
1449 if (hi->root.type == bfd_link_hash_indirect)
1451 struct elf_link_hash_entry *ht;
1453 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1454 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1456 /* See if the new flags lead us to realize that the symbol must
1457 be dynamic. */
1458 if (! *dynsym)
1460 if (! dynamic)
1462 if (info->shared
1463 || hi->ref_dynamic)
1464 *dynsym = TRUE;
1466 else
1468 if (hi->ref_regular)
1469 *dynsym = TRUE;
1474 /* We also need to define an indirection from the nondefault version
1475 of the symbol. */
1477 nondefault:
1478 len = strlen (name);
1479 shortname = bfd_hash_allocate (&info->hash->table, len);
1480 if (shortname == NULL)
1481 return FALSE;
1482 memcpy (shortname, name, shortlen);
1483 memcpy (shortname + shortlen, p + 1, len - shortlen);
1485 /* Once again, merge with any existing symbol. */
1486 type_change_ok = FALSE;
1487 size_change_ok = FALSE;
1488 sec = *psec;
1489 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1490 &hi, &skip, &override, &type_change_ok,
1491 &size_change_ok))
1492 return FALSE;
1494 if (skip)
1495 return TRUE;
1497 if (override)
1499 /* Here SHORTNAME is a versioned name, so we don't expect to see
1500 the type of override we do in the case above unless it is
1501 overridden by a versioned definition. */
1502 if (hi->root.type != bfd_link_hash_defined
1503 && hi->root.type != bfd_link_hash_defweak)
1504 (*_bfd_error_handler)
1505 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1506 abfd, shortname);
1508 else
1510 bh = &hi->root;
1511 if (! (_bfd_generic_link_add_one_symbol
1512 (info, abfd, shortname, BSF_INDIRECT,
1513 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1514 return FALSE;
1515 hi = (struct elf_link_hash_entry *) bh;
1517 /* If there is a duplicate definition somewhere, then HI may not
1518 point to an indirect symbol. We will have reported an error
1519 to the user in that case. */
1521 if (hi->root.type == bfd_link_hash_indirect)
1523 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1525 /* See if the new flags lead us to realize that the symbol
1526 must be dynamic. */
1527 if (! *dynsym)
1529 if (! dynamic)
1531 if (info->shared
1532 || hi->ref_dynamic)
1533 *dynsym = TRUE;
1535 else
1537 if (hi->ref_regular)
1538 *dynsym = TRUE;
1544 return TRUE;
1547 /* This routine is used to export all defined symbols into the dynamic
1548 symbol table. It is called via elf_link_hash_traverse. */
1550 bfd_boolean
1551 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1553 struct elf_info_failed *eif = data;
1555 /* Ignore indirect symbols. These are added by the versioning code. */
1556 if (h->root.type == bfd_link_hash_indirect)
1557 return TRUE;
1559 if (h->root.type == bfd_link_hash_warning)
1560 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1562 if (h->dynindx == -1
1563 && (h->def_regular
1564 || h->ref_regular))
1566 struct bfd_elf_version_tree *t;
1567 struct bfd_elf_version_expr *d;
1569 for (t = eif->verdefs; t != NULL; t = t->next)
1571 if (t->globals.list != NULL)
1573 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1574 if (d != NULL)
1575 goto doit;
1578 if (t->locals.list != NULL)
1580 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1581 if (d != NULL)
1582 return TRUE;
1586 if (!eif->verdefs)
1588 doit:
1589 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1591 eif->failed = TRUE;
1592 return FALSE;
1597 return TRUE;
1600 /* Look through the symbols which are defined in other shared
1601 libraries and referenced here. Update the list of version
1602 dependencies. This will be put into the .gnu.version_r section.
1603 This function is called via elf_link_hash_traverse. */
1605 bfd_boolean
1606 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1607 void *data)
1609 struct elf_find_verdep_info *rinfo = data;
1610 Elf_Internal_Verneed *t;
1611 Elf_Internal_Vernaux *a;
1612 bfd_size_type amt;
1614 if (h->root.type == bfd_link_hash_warning)
1615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1617 /* We only care about symbols defined in shared objects with version
1618 information. */
1619 if (!h->def_dynamic
1620 || h->def_regular
1621 || h->dynindx == -1
1622 || h->verinfo.verdef == NULL)
1623 return TRUE;
1625 /* See if we already know about this version. */
1626 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1628 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1629 continue;
1631 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1632 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1633 return TRUE;
1635 break;
1638 /* This is a new version. Add it to tree we are building. */
1640 if (t == NULL)
1642 amt = sizeof *t;
1643 t = bfd_zalloc (rinfo->output_bfd, amt);
1644 if (t == NULL)
1646 rinfo->failed = TRUE;
1647 return FALSE;
1650 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1651 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1652 elf_tdata (rinfo->output_bfd)->verref = t;
1655 amt = sizeof *a;
1656 a = bfd_zalloc (rinfo->output_bfd, amt);
1658 /* Note that we are copying a string pointer here, and testing it
1659 above. If bfd_elf_string_from_elf_section is ever changed to
1660 discard the string data when low in memory, this will have to be
1661 fixed. */
1662 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1664 a->vna_flags = h->verinfo.verdef->vd_flags;
1665 a->vna_nextptr = t->vn_auxptr;
1667 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1668 ++rinfo->vers;
1670 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1672 t->vn_auxptr = a;
1674 return TRUE;
1677 /* Figure out appropriate versions for all the symbols. We may not
1678 have the version number script until we have read all of the input
1679 files, so until that point we don't know which symbols should be
1680 local. This function is called via elf_link_hash_traverse. */
1682 bfd_boolean
1683 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1685 struct elf_assign_sym_version_info *sinfo;
1686 struct bfd_link_info *info;
1687 const struct elf_backend_data *bed;
1688 struct elf_info_failed eif;
1689 char *p;
1690 bfd_size_type amt;
1692 sinfo = data;
1693 info = sinfo->info;
1695 if (h->root.type == bfd_link_hash_warning)
1696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1698 /* Fix the symbol flags. */
1699 eif.failed = FALSE;
1700 eif.info = info;
1701 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1703 if (eif.failed)
1704 sinfo->failed = TRUE;
1705 return FALSE;
1708 /* We only need version numbers for symbols defined in regular
1709 objects. */
1710 if (!h->def_regular)
1711 return TRUE;
1713 bed = get_elf_backend_data (sinfo->output_bfd);
1714 p = strchr (h->root.root.string, ELF_VER_CHR);
1715 if (p != NULL && h->verinfo.vertree == NULL)
1717 struct bfd_elf_version_tree *t;
1718 bfd_boolean hidden;
1720 hidden = TRUE;
1722 /* There are two consecutive ELF_VER_CHR characters if this is
1723 not a hidden symbol. */
1724 ++p;
1725 if (*p == ELF_VER_CHR)
1727 hidden = FALSE;
1728 ++p;
1731 /* If there is no version string, we can just return out. */
1732 if (*p == '\0')
1734 if (hidden)
1735 h->hidden = 1;
1736 return TRUE;
1739 /* Look for the version. If we find it, it is no longer weak. */
1740 for (t = sinfo->verdefs; t != NULL; t = t->next)
1742 if (strcmp (t->name, p) == 0)
1744 size_t len;
1745 char *alc;
1746 struct bfd_elf_version_expr *d;
1748 len = p - h->root.root.string;
1749 alc = bfd_malloc (len);
1750 if (alc == NULL)
1751 return FALSE;
1752 memcpy (alc, h->root.root.string, len - 1);
1753 alc[len - 1] = '\0';
1754 if (alc[len - 2] == ELF_VER_CHR)
1755 alc[len - 2] = '\0';
1757 h->verinfo.vertree = t;
1758 t->used = TRUE;
1759 d = NULL;
1761 if (t->globals.list != NULL)
1762 d = (*t->match) (&t->globals, NULL, alc);
1764 /* See if there is anything to force this symbol to
1765 local scope. */
1766 if (d == NULL && t->locals.list != NULL)
1768 d = (*t->match) (&t->locals, NULL, alc);
1769 if (d != NULL
1770 && h->dynindx != -1
1771 && info->shared
1772 && ! info->export_dynamic)
1773 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1776 free (alc);
1777 break;
1781 /* If we are building an application, we need to create a
1782 version node for this version. */
1783 if (t == NULL && info->executable)
1785 struct bfd_elf_version_tree **pp;
1786 int version_index;
1788 /* If we aren't going to export this symbol, we don't need
1789 to worry about it. */
1790 if (h->dynindx == -1)
1791 return TRUE;
1793 amt = sizeof *t;
1794 t = bfd_zalloc (sinfo->output_bfd, amt);
1795 if (t == NULL)
1797 sinfo->failed = TRUE;
1798 return FALSE;
1801 t->name = p;
1802 t->name_indx = (unsigned int) -1;
1803 t->used = TRUE;
1805 version_index = 1;
1806 /* Don't count anonymous version tag. */
1807 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1808 version_index = 0;
1809 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1810 ++version_index;
1811 t->vernum = version_index;
1813 *pp = t;
1815 h->verinfo.vertree = t;
1817 else if (t == NULL)
1819 /* We could not find the version for a symbol when
1820 generating a shared archive. Return an error. */
1821 (*_bfd_error_handler)
1822 (_("%B: undefined versioned symbol name %s"),
1823 sinfo->output_bfd, h->root.root.string);
1824 bfd_set_error (bfd_error_bad_value);
1825 sinfo->failed = TRUE;
1826 return FALSE;
1829 if (hidden)
1830 h->hidden = 1;
1833 /* If we don't have a version for this symbol, see if we can find
1834 something. */
1835 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1837 struct bfd_elf_version_tree *t;
1838 struct bfd_elf_version_tree *local_ver;
1839 struct bfd_elf_version_expr *d;
1841 /* See if can find what version this symbol is in. If the
1842 symbol is supposed to be local, then don't actually register
1843 it. */
1844 local_ver = NULL;
1845 for (t = sinfo->verdefs; t != NULL; t = t->next)
1847 if (t->globals.list != NULL)
1849 bfd_boolean matched;
1851 matched = FALSE;
1852 d = NULL;
1853 while ((d = (*t->match) (&t->globals, d,
1854 h->root.root.string)) != NULL)
1855 if (d->symver)
1856 matched = TRUE;
1857 else
1859 /* There is a version without definition. Make
1860 the symbol the default definition for this
1861 version. */
1862 h->verinfo.vertree = t;
1863 local_ver = NULL;
1864 d->script = 1;
1865 break;
1867 if (d != NULL)
1868 break;
1869 else if (matched)
1870 /* There is no undefined version for this symbol. Hide the
1871 default one. */
1872 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1875 if (t->locals.list != NULL)
1877 d = NULL;
1878 while ((d = (*t->match) (&t->locals, d,
1879 h->root.root.string)) != NULL)
1881 local_ver = t;
1882 /* If the match is "*", keep looking for a more
1883 explicit, perhaps even global, match.
1884 XXX: Shouldn't this be !d->wildcard instead? */
1885 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1886 break;
1889 if (d != NULL)
1890 break;
1894 if (local_ver != NULL)
1896 h->verinfo.vertree = local_ver;
1897 if (h->dynindx != -1
1898 && info->shared
1899 && ! info->export_dynamic)
1901 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1906 return TRUE;
1909 /* Read and swap the relocs from the section indicated by SHDR. This
1910 may be either a REL or a RELA section. The relocations are
1911 translated into RELA relocations and stored in INTERNAL_RELOCS,
1912 which should have already been allocated to contain enough space.
1913 The EXTERNAL_RELOCS are a buffer where the external form of the
1914 relocations should be stored.
1916 Returns FALSE if something goes wrong. */
1918 static bfd_boolean
1919 elf_link_read_relocs_from_section (bfd *abfd,
1920 asection *sec,
1921 Elf_Internal_Shdr *shdr,
1922 void *external_relocs,
1923 Elf_Internal_Rela *internal_relocs)
1925 const struct elf_backend_data *bed;
1926 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1927 const bfd_byte *erela;
1928 const bfd_byte *erelaend;
1929 Elf_Internal_Rela *irela;
1930 Elf_Internal_Shdr *symtab_hdr;
1931 size_t nsyms;
1933 /* Position ourselves at the start of the section. */
1934 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1935 return FALSE;
1937 /* Read the relocations. */
1938 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1939 return FALSE;
1941 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1942 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1944 bed = get_elf_backend_data (abfd);
1946 /* Convert the external relocations to the internal format. */
1947 if (shdr->sh_entsize == bed->s->sizeof_rel)
1948 swap_in = bed->s->swap_reloc_in;
1949 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1950 swap_in = bed->s->swap_reloca_in;
1951 else
1953 bfd_set_error (bfd_error_wrong_format);
1954 return FALSE;
1957 erela = external_relocs;
1958 erelaend = erela + shdr->sh_size;
1959 irela = internal_relocs;
1960 while (erela < erelaend)
1962 bfd_vma r_symndx;
1964 (*swap_in) (abfd, erela, irela);
1965 r_symndx = ELF32_R_SYM (irela->r_info);
1966 if (bed->s->arch_size == 64)
1967 r_symndx >>= 24;
1968 if ((size_t) r_symndx >= nsyms)
1970 (*_bfd_error_handler)
1971 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1972 " for offset 0x%lx in section `%A'"),
1973 abfd, sec,
1974 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1975 bfd_set_error (bfd_error_bad_value);
1976 return FALSE;
1978 irela += bed->s->int_rels_per_ext_rel;
1979 erela += shdr->sh_entsize;
1982 return TRUE;
1985 /* Read and swap the relocs for a section O. They may have been
1986 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1987 not NULL, they are used as buffers to read into. They are known to
1988 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1989 the return value is allocated using either malloc or bfd_alloc,
1990 according to the KEEP_MEMORY argument. If O has two relocation
1991 sections (both REL and RELA relocations), then the REL_HDR
1992 relocations will appear first in INTERNAL_RELOCS, followed by the
1993 REL_HDR2 relocations. */
1995 Elf_Internal_Rela *
1996 _bfd_elf_link_read_relocs (bfd *abfd,
1997 asection *o,
1998 void *external_relocs,
1999 Elf_Internal_Rela *internal_relocs,
2000 bfd_boolean keep_memory)
2002 Elf_Internal_Shdr *rel_hdr;
2003 void *alloc1 = NULL;
2004 Elf_Internal_Rela *alloc2 = NULL;
2005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2007 if (elf_section_data (o)->relocs != NULL)
2008 return elf_section_data (o)->relocs;
2010 if (o->reloc_count == 0)
2011 return NULL;
2013 rel_hdr = &elf_section_data (o)->rel_hdr;
2015 if (internal_relocs == NULL)
2017 bfd_size_type size;
2019 size = o->reloc_count;
2020 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2021 if (keep_memory)
2022 internal_relocs = bfd_alloc (abfd, size);
2023 else
2024 internal_relocs = alloc2 = bfd_malloc (size);
2025 if (internal_relocs == NULL)
2026 goto error_return;
2029 if (external_relocs == NULL)
2031 bfd_size_type size = rel_hdr->sh_size;
2033 if (elf_section_data (o)->rel_hdr2)
2034 size += elf_section_data (o)->rel_hdr2->sh_size;
2035 alloc1 = bfd_malloc (size);
2036 if (alloc1 == NULL)
2037 goto error_return;
2038 external_relocs = alloc1;
2041 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2042 external_relocs,
2043 internal_relocs))
2044 goto error_return;
2045 if (elf_section_data (o)->rel_hdr2
2046 && (!elf_link_read_relocs_from_section
2047 (abfd, o,
2048 elf_section_data (o)->rel_hdr2,
2049 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2050 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2051 * bed->s->int_rels_per_ext_rel))))
2052 goto error_return;
2054 /* Cache the results for next time, if we can. */
2055 if (keep_memory)
2056 elf_section_data (o)->relocs = internal_relocs;
2058 if (alloc1 != NULL)
2059 free (alloc1);
2061 /* Don't free alloc2, since if it was allocated we are passing it
2062 back (under the name of internal_relocs). */
2064 return internal_relocs;
2066 error_return:
2067 if (alloc1 != NULL)
2068 free (alloc1);
2069 if (alloc2 != NULL)
2070 free (alloc2);
2071 return NULL;
2074 /* Compute the size of, and allocate space for, REL_HDR which is the
2075 section header for a section containing relocations for O. */
2077 bfd_boolean
2078 _bfd_elf_link_size_reloc_section (bfd *abfd,
2079 Elf_Internal_Shdr *rel_hdr,
2080 asection *o)
2082 bfd_size_type reloc_count;
2083 bfd_size_type num_rel_hashes;
2085 /* Figure out how many relocations there will be. */
2086 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2087 reloc_count = elf_section_data (o)->rel_count;
2088 else
2089 reloc_count = elf_section_data (o)->rel_count2;
2091 num_rel_hashes = o->reloc_count;
2092 if (num_rel_hashes < reloc_count)
2093 num_rel_hashes = reloc_count;
2095 /* That allows us to calculate the size of the section. */
2096 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2098 /* The contents field must last into write_object_contents, so we
2099 allocate it with bfd_alloc rather than malloc. Also since we
2100 cannot be sure that the contents will actually be filled in,
2101 we zero the allocated space. */
2102 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2103 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2104 return FALSE;
2106 /* We only allocate one set of hash entries, so we only do it the
2107 first time we are called. */
2108 if (elf_section_data (o)->rel_hashes == NULL
2109 && num_rel_hashes)
2111 struct elf_link_hash_entry **p;
2113 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2114 if (p == NULL)
2115 return FALSE;
2117 elf_section_data (o)->rel_hashes = p;
2120 return TRUE;
2123 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2124 originated from the section given by INPUT_REL_HDR) to the
2125 OUTPUT_BFD. */
2127 bfd_boolean
2128 _bfd_elf_link_output_relocs (bfd *output_bfd,
2129 asection *input_section,
2130 Elf_Internal_Shdr *input_rel_hdr,
2131 Elf_Internal_Rela *internal_relocs)
2133 Elf_Internal_Rela *irela;
2134 Elf_Internal_Rela *irelaend;
2135 bfd_byte *erel;
2136 Elf_Internal_Shdr *output_rel_hdr;
2137 asection *output_section;
2138 unsigned int *rel_countp = NULL;
2139 const struct elf_backend_data *bed;
2140 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2142 output_section = input_section->output_section;
2143 output_rel_hdr = NULL;
2145 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2146 == input_rel_hdr->sh_entsize)
2148 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2149 rel_countp = &elf_section_data (output_section)->rel_count;
2151 else if (elf_section_data (output_section)->rel_hdr2
2152 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2153 == input_rel_hdr->sh_entsize))
2155 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2156 rel_countp = &elf_section_data (output_section)->rel_count2;
2158 else
2160 (*_bfd_error_handler)
2161 (_("%B: relocation size mismatch in %B section %A"),
2162 output_bfd, input_section->owner, input_section);
2163 bfd_set_error (bfd_error_wrong_object_format);
2164 return FALSE;
2167 bed = get_elf_backend_data (output_bfd);
2168 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2169 swap_out = bed->s->swap_reloc_out;
2170 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2171 swap_out = bed->s->swap_reloca_out;
2172 else
2173 abort ();
2175 erel = output_rel_hdr->contents;
2176 erel += *rel_countp * input_rel_hdr->sh_entsize;
2177 irela = internal_relocs;
2178 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2179 * bed->s->int_rels_per_ext_rel);
2180 while (irela < irelaend)
2182 (*swap_out) (output_bfd, irela, erel);
2183 irela += bed->s->int_rels_per_ext_rel;
2184 erel += input_rel_hdr->sh_entsize;
2187 /* Bump the counter, so that we know where to add the next set of
2188 relocations. */
2189 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2191 return TRUE;
2194 /* Fix up the flags for a symbol. This handles various cases which
2195 can only be fixed after all the input files are seen. This is
2196 currently called by both adjust_dynamic_symbol and
2197 assign_sym_version, which is unnecessary but perhaps more robust in
2198 the face of future changes. */
2200 bfd_boolean
2201 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2202 struct elf_info_failed *eif)
2204 /* If this symbol was mentioned in a non-ELF file, try to set
2205 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2206 permit a non-ELF file to correctly refer to a symbol defined in
2207 an ELF dynamic object. */
2208 if (h->non_elf)
2210 while (h->root.type == bfd_link_hash_indirect)
2211 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2213 if (h->root.type != bfd_link_hash_defined
2214 && h->root.type != bfd_link_hash_defweak)
2216 h->ref_regular = 1;
2217 h->ref_regular_nonweak = 1;
2219 else
2221 if (h->root.u.def.section->owner != NULL
2222 && (bfd_get_flavour (h->root.u.def.section->owner)
2223 == bfd_target_elf_flavour))
2225 h->ref_regular = 1;
2226 h->ref_regular_nonweak = 1;
2228 else
2229 h->def_regular = 1;
2232 if (h->dynindx == -1
2233 && (h->def_dynamic
2234 || h->ref_dynamic))
2236 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2238 eif->failed = TRUE;
2239 return FALSE;
2243 else
2245 /* Unfortunately, NON_ELF is only correct if the symbol
2246 was first seen in a non-ELF file. Fortunately, if the symbol
2247 was first seen in an ELF file, we're probably OK unless the
2248 symbol was defined in a non-ELF file. Catch that case here.
2249 FIXME: We're still in trouble if the symbol was first seen in
2250 a dynamic object, and then later in a non-ELF regular object. */
2251 if ((h->root.type == bfd_link_hash_defined
2252 || h->root.type == bfd_link_hash_defweak)
2253 && !h->def_regular
2254 && (h->root.u.def.section->owner != NULL
2255 ? (bfd_get_flavour (h->root.u.def.section->owner)
2256 != bfd_target_elf_flavour)
2257 : (bfd_is_abs_section (h->root.u.def.section)
2258 && !h->def_dynamic)))
2259 h->def_regular = 1;
2262 /* If this is a final link, and the symbol was defined as a common
2263 symbol in a regular object file, and there was no definition in
2264 any dynamic object, then the linker will have allocated space for
2265 the symbol in a common section but the DEF_REGULAR
2266 flag will not have been set. */
2267 if (h->root.type == bfd_link_hash_defined
2268 && !h->def_regular
2269 && h->ref_regular
2270 && !h->def_dynamic
2271 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2272 h->def_regular = 1;
2274 /* If -Bsymbolic was used (which means to bind references to global
2275 symbols to the definition within the shared object), and this
2276 symbol was defined in a regular object, then it actually doesn't
2277 need a PLT entry. Likewise, if the symbol has non-default
2278 visibility. If the symbol has hidden or internal visibility, we
2279 will force it local. */
2280 if (h->needs_plt
2281 && eif->info->shared
2282 && is_elf_hash_table (eif->info->hash)
2283 && (eif->info->symbolic
2284 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2285 && h->def_regular)
2287 const struct elf_backend_data *bed;
2288 bfd_boolean force_local;
2290 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2292 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2293 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2294 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2297 /* If a weak undefined symbol has non-default visibility, we also
2298 hide it from the dynamic linker. */
2299 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2300 && h->root.type == bfd_link_hash_undefweak)
2302 const struct elf_backend_data *bed;
2303 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2304 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2307 /* If this is a weak defined symbol in a dynamic object, and we know
2308 the real definition in the dynamic object, copy interesting flags
2309 over to the real definition. */
2310 if (h->u.weakdef != NULL)
2312 struct elf_link_hash_entry *weakdef;
2314 weakdef = h->u.weakdef;
2315 if (h->root.type == bfd_link_hash_indirect)
2316 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2318 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2319 || h->root.type == bfd_link_hash_defweak);
2320 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2321 || weakdef->root.type == bfd_link_hash_defweak);
2322 BFD_ASSERT (weakdef->def_dynamic);
2324 /* If the real definition is defined by a regular object file,
2325 don't do anything special. See the longer description in
2326 _bfd_elf_adjust_dynamic_symbol, below. */
2327 if (weakdef->def_regular)
2328 h->u.weakdef = NULL;
2329 else
2331 const struct elf_backend_data *bed;
2333 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2334 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2338 return TRUE;
2341 /* Make the backend pick a good value for a dynamic symbol. This is
2342 called via elf_link_hash_traverse, and also calls itself
2343 recursively. */
2345 bfd_boolean
2346 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2348 struct elf_info_failed *eif = data;
2349 bfd *dynobj;
2350 const struct elf_backend_data *bed;
2352 if (! is_elf_hash_table (eif->info->hash))
2353 return FALSE;
2355 if (h->root.type == bfd_link_hash_warning)
2357 h->plt = elf_hash_table (eif->info)->init_offset;
2358 h->got = elf_hash_table (eif->info)->init_offset;
2360 /* When warning symbols are created, they **replace** the "real"
2361 entry in the hash table, thus we never get to see the real
2362 symbol in a hash traversal. So look at it now. */
2363 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2366 /* Ignore indirect symbols. These are added by the versioning code. */
2367 if (h->root.type == bfd_link_hash_indirect)
2368 return TRUE;
2370 /* Fix the symbol flags. */
2371 if (! _bfd_elf_fix_symbol_flags (h, eif))
2372 return FALSE;
2374 /* If this symbol does not require a PLT entry, and it is not
2375 defined by a dynamic object, or is not referenced by a regular
2376 object, ignore it. We do have to handle a weak defined symbol,
2377 even if no regular object refers to it, if we decided to add it
2378 to the dynamic symbol table. FIXME: Do we normally need to worry
2379 about symbols which are defined by one dynamic object and
2380 referenced by another one? */
2381 if (!h->needs_plt
2382 && (h->def_regular
2383 || !h->def_dynamic
2384 || (!h->ref_regular
2385 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2387 h->plt = elf_hash_table (eif->info)->init_offset;
2388 return TRUE;
2391 /* If we've already adjusted this symbol, don't do it again. This
2392 can happen via a recursive call. */
2393 if (h->dynamic_adjusted)
2394 return TRUE;
2396 /* Don't look at this symbol again. Note that we must set this
2397 after checking the above conditions, because we may look at a
2398 symbol once, decide not to do anything, and then get called
2399 recursively later after REF_REGULAR is set below. */
2400 h->dynamic_adjusted = 1;
2402 /* If this is a weak definition, and we know a real definition, and
2403 the real symbol is not itself defined by a regular object file,
2404 then get a good value for the real definition. We handle the
2405 real symbol first, for the convenience of the backend routine.
2407 Note that there is a confusing case here. If the real definition
2408 is defined by a regular object file, we don't get the real symbol
2409 from the dynamic object, but we do get the weak symbol. If the
2410 processor backend uses a COPY reloc, then if some routine in the
2411 dynamic object changes the real symbol, we will not see that
2412 change in the corresponding weak symbol. This is the way other
2413 ELF linkers work as well, and seems to be a result of the shared
2414 library model.
2416 I will clarify this issue. Most SVR4 shared libraries define the
2417 variable _timezone and define timezone as a weak synonym. The
2418 tzset call changes _timezone. If you write
2419 extern int timezone;
2420 int _timezone = 5;
2421 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2422 you might expect that, since timezone is a synonym for _timezone,
2423 the same number will print both times. However, if the processor
2424 backend uses a COPY reloc, then actually timezone will be copied
2425 into your process image, and, since you define _timezone
2426 yourself, _timezone will not. Thus timezone and _timezone will
2427 wind up at different memory locations. The tzset call will set
2428 _timezone, leaving timezone unchanged. */
2430 if (h->u.weakdef != NULL)
2432 /* If we get to this point, we know there is an implicit
2433 reference by a regular object file via the weak symbol H.
2434 FIXME: Is this really true? What if the traversal finds
2435 H->U.WEAKDEF before it finds H? */
2436 h->u.weakdef->ref_regular = 1;
2438 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2439 return FALSE;
2442 /* If a symbol has no type and no size and does not require a PLT
2443 entry, then we are probably about to do the wrong thing here: we
2444 are probably going to create a COPY reloc for an empty object.
2445 This case can arise when a shared object is built with assembly
2446 code, and the assembly code fails to set the symbol type. */
2447 if (h->size == 0
2448 && h->type == STT_NOTYPE
2449 && !h->needs_plt)
2450 (*_bfd_error_handler)
2451 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2452 h->root.root.string);
2454 dynobj = elf_hash_table (eif->info)->dynobj;
2455 bed = get_elf_backend_data (dynobj);
2456 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2458 eif->failed = TRUE;
2459 return FALSE;
2462 return TRUE;
2465 /* Adjust all external symbols pointing into SEC_MERGE sections
2466 to reflect the object merging within the sections. */
2468 bfd_boolean
2469 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2471 asection *sec;
2473 if (h->root.type == bfd_link_hash_warning)
2474 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2476 if ((h->root.type == bfd_link_hash_defined
2477 || h->root.type == bfd_link_hash_defweak)
2478 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2479 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2481 bfd *output_bfd = data;
2483 h->root.u.def.value =
2484 _bfd_merged_section_offset (output_bfd,
2485 &h->root.u.def.section,
2486 elf_section_data (sec)->sec_info,
2487 h->root.u.def.value);
2490 return TRUE;
2493 /* Returns false if the symbol referred to by H should be considered
2494 to resolve local to the current module, and true if it should be
2495 considered to bind dynamically. */
2497 bfd_boolean
2498 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2499 struct bfd_link_info *info,
2500 bfd_boolean ignore_protected)
2502 bfd_boolean binding_stays_local_p;
2504 if (h == NULL)
2505 return FALSE;
2507 while (h->root.type == bfd_link_hash_indirect
2508 || h->root.type == bfd_link_hash_warning)
2509 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2511 /* If it was forced local, then clearly it's not dynamic. */
2512 if (h->dynindx == -1)
2513 return FALSE;
2514 if (h->forced_local)
2515 return FALSE;
2517 /* Identify the cases where name binding rules say that a
2518 visible symbol resolves locally. */
2519 binding_stays_local_p = info->executable || info->symbolic;
2521 switch (ELF_ST_VISIBILITY (h->other))
2523 case STV_INTERNAL:
2524 case STV_HIDDEN:
2525 return FALSE;
2527 case STV_PROTECTED:
2528 /* Proper resolution for function pointer equality may require
2529 that these symbols perhaps be resolved dynamically, even though
2530 we should be resolving them to the current module. */
2531 if (!ignore_protected || h->type != STT_FUNC)
2532 binding_stays_local_p = TRUE;
2533 break;
2535 default:
2536 break;
2539 /* If it isn't defined locally, then clearly it's dynamic. */
2540 if (!h->def_regular)
2541 return TRUE;
2543 /* Otherwise, the symbol is dynamic if binding rules don't tell
2544 us that it remains local. */
2545 return !binding_stays_local_p;
2548 /* Return true if the symbol referred to by H should be considered
2549 to resolve local to the current module, and false otherwise. Differs
2550 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2551 undefined symbols and weak symbols. */
2553 bfd_boolean
2554 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2555 struct bfd_link_info *info,
2556 bfd_boolean local_protected)
2558 /* If it's a local sym, of course we resolve locally. */
2559 if (h == NULL)
2560 return TRUE;
2562 /* Common symbols that become definitions don't get the DEF_REGULAR
2563 flag set, so test it first, and don't bail out. */
2564 if (ELF_COMMON_DEF_P (h))
2565 /* Do nothing. */;
2566 /* If we don't have a definition in a regular file, then we can't
2567 resolve locally. The sym is either undefined or dynamic. */
2568 else if (!h->def_regular)
2569 return FALSE;
2571 /* Forced local symbols resolve locally. */
2572 if (h->forced_local)
2573 return TRUE;
2575 /* As do non-dynamic symbols. */
2576 if (h->dynindx == -1)
2577 return TRUE;
2579 /* At this point, we know the symbol is defined and dynamic. In an
2580 executable it must resolve locally, likewise when building symbolic
2581 shared libraries. */
2582 if (info->executable || info->symbolic)
2583 return TRUE;
2585 /* Now deal with defined dynamic symbols in shared libraries. Ones
2586 with default visibility might not resolve locally. */
2587 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2588 return FALSE;
2590 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2591 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2592 return TRUE;
2594 /* STV_PROTECTED non-function symbols are local. */
2595 if (h->type != STT_FUNC)
2596 return TRUE;
2598 /* Function pointer equality tests may require that STV_PROTECTED
2599 symbols be treated as dynamic symbols, even when we know that the
2600 dynamic linker will resolve them locally. */
2601 return local_protected;
2604 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2605 aligned. Returns the first TLS output section. */
2607 struct bfd_section *
2608 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2610 struct bfd_section *sec, *tls;
2611 unsigned int align = 0;
2613 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2614 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2615 break;
2616 tls = sec;
2618 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2619 if (sec->alignment_power > align)
2620 align = sec->alignment_power;
2622 elf_hash_table (info)->tls_sec = tls;
2624 /* Ensure the alignment of the first section is the largest alignment,
2625 so that the tls segment starts aligned. */
2626 if (tls != NULL)
2627 tls->alignment_power = align;
2629 return tls;
2632 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2633 static bfd_boolean
2634 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2635 Elf_Internal_Sym *sym)
2637 /* Local symbols do not count, but target specific ones might. */
2638 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2639 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2640 return FALSE;
2642 /* Function symbols do not count. */
2643 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2644 return FALSE;
2646 /* If the section is undefined, then so is the symbol. */
2647 if (sym->st_shndx == SHN_UNDEF)
2648 return FALSE;
2650 /* If the symbol is defined in the common section, then
2651 it is a common definition and so does not count. */
2652 if (sym->st_shndx == SHN_COMMON)
2653 return FALSE;
2655 /* If the symbol is in a target specific section then we
2656 must rely upon the backend to tell us what it is. */
2657 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2658 /* FIXME - this function is not coded yet:
2660 return _bfd_is_global_symbol_definition (abfd, sym);
2662 Instead for now assume that the definition is not global,
2663 Even if this is wrong, at least the linker will behave
2664 in the same way that it used to do. */
2665 return FALSE;
2667 return TRUE;
2670 /* Search the symbol table of the archive element of the archive ABFD
2671 whose archive map contains a mention of SYMDEF, and determine if
2672 the symbol is defined in this element. */
2673 static bfd_boolean
2674 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2676 Elf_Internal_Shdr * hdr;
2677 bfd_size_type symcount;
2678 bfd_size_type extsymcount;
2679 bfd_size_type extsymoff;
2680 Elf_Internal_Sym *isymbuf;
2681 Elf_Internal_Sym *isym;
2682 Elf_Internal_Sym *isymend;
2683 bfd_boolean result;
2685 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2686 if (abfd == NULL)
2687 return FALSE;
2689 if (! bfd_check_format (abfd, bfd_object))
2690 return FALSE;
2692 /* If we have already included the element containing this symbol in the
2693 link then we do not need to include it again. Just claim that any symbol
2694 it contains is not a definition, so that our caller will not decide to
2695 (re)include this element. */
2696 if (abfd->archive_pass)
2697 return FALSE;
2699 /* Select the appropriate symbol table. */
2700 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2701 hdr = &elf_tdata (abfd)->symtab_hdr;
2702 else
2703 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2705 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2707 /* The sh_info field of the symtab header tells us where the
2708 external symbols start. We don't care about the local symbols. */
2709 if (elf_bad_symtab (abfd))
2711 extsymcount = symcount;
2712 extsymoff = 0;
2714 else
2716 extsymcount = symcount - hdr->sh_info;
2717 extsymoff = hdr->sh_info;
2720 if (extsymcount == 0)
2721 return FALSE;
2723 /* Read in the symbol table. */
2724 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2725 NULL, NULL, NULL);
2726 if (isymbuf == NULL)
2727 return FALSE;
2729 /* Scan the symbol table looking for SYMDEF. */
2730 result = FALSE;
2731 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2733 const char *name;
2735 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2736 isym->st_name);
2737 if (name == NULL)
2738 break;
2740 if (strcmp (name, symdef->name) == 0)
2742 result = is_global_data_symbol_definition (abfd, isym);
2743 break;
2747 free (isymbuf);
2749 return result;
2752 /* Add an entry to the .dynamic table. */
2754 bfd_boolean
2755 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2756 bfd_vma tag,
2757 bfd_vma val)
2759 struct elf_link_hash_table *hash_table;
2760 const struct elf_backend_data *bed;
2761 asection *s;
2762 bfd_size_type newsize;
2763 bfd_byte *newcontents;
2764 Elf_Internal_Dyn dyn;
2766 hash_table = elf_hash_table (info);
2767 if (! is_elf_hash_table (hash_table))
2768 return FALSE;
2770 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2771 _bfd_error_handler
2772 (_("warning: creating a DT_TEXTREL in a shared object."));
2774 bed = get_elf_backend_data (hash_table->dynobj);
2775 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2776 BFD_ASSERT (s != NULL);
2778 newsize = s->size + bed->s->sizeof_dyn;
2779 newcontents = bfd_realloc (s->contents, newsize);
2780 if (newcontents == NULL)
2781 return FALSE;
2783 dyn.d_tag = tag;
2784 dyn.d_un.d_val = val;
2785 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2787 s->size = newsize;
2788 s->contents = newcontents;
2790 return TRUE;
2793 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2794 otherwise just check whether one already exists. Returns -1 on error,
2795 1 if a DT_NEEDED tag already exists, and 0 on success. */
2797 static int
2798 elf_add_dt_needed_tag (bfd *abfd,
2799 struct bfd_link_info *info,
2800 const char *soname,
2801 bfd_boolean do_it)
2803 struct elf_link_hash_table *hash_table;
2804 bfd_size_type oldsize;
2805 bfd_size_type strindex;
2807 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2808 return -1;
2810 hash_table = elf_hash_table (info);
2811 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2812 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2813 if (strindex == (bfd_size_type) -1)
2814 return -1;
2816 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2818 asection *sdyn;
2819 const struct elf_backend_data *bed;
2820 bfd_byte *extdyn;
2822 bed = get_elf_backend_data (hash_table->dynobj);
2823 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2824 if (sdyn != NULL)
2825 for (extdyn = sdyn->contents;
2826 extdyn < sdyn->contents + sdyn->size;
2827 extdyn += bed->s->sizeof_dyn)
2829 Elf_Internal_Dyn dyn;
2831 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2832 if (dyn.d_tag == DT_NEEDED
2833 && dyn.d_un.d_val == strindex)
2835 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2836 return 1;
2841 if (do_it)
2843 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2844 return -1;
2846 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2847 return -1;
2849 else
2850 /* We were just checking for existence of the tag. */
2851 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2853 return 0;
2856 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2857 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2858 references from regular objects to these symbols.
2860 ??? Should we do something about references from other dynamic
2861 obects? If not, we potentially lose some warnings about undefined
2862 symbols. But how can we recover the initial undefined / undefweak
2863 state? */
2865 struct elf_smash_syms_data
2867 bfd *not_needed;
2868 struct elf_link_hash_table *htab;
2869 bfd_boolean twiddled;
2872 static bfd_boolean
2873 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2875 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2876 struct bfd_link_hash_entry *bh;
2878 switch (h->root.type)
2880 default:
2881 case bfd_link_hash_new:
2882 return TRUE;
2884 case bfd_link_hash_undefined:
2885 if (h->root.u.undef.abfd != inf->not_needed)
2886 return TRUE;
2887 if (h->root.u.undef.weak != NULL
2888 && h->root.u.undef.weak != inf->not_needed)
2890 /* Symbol was undefweak in u.undef.weak bfd, and has become
2891 undefined in as-needed lib. Restore weak. */
2892 h->root.type = bfd_link_hash_undefweak;
2893 h->root.u.undef.abfd = h->root.u.undef.weak;
2894 if (h->root.u.undef.next != NULL
2895 || inf->htab->root.undefs_tail == &h->root)
2896 inf->twiddled = TRUE;
2897 return TRUE;
2899 break;
2901 case bfd_link_hash_undefweak:
2902 if (h->root.u.undef.abfd != inf->not_needed)
2903 return TRUE;
2904 break;
2906 case bfd_link_hash_defined:
2907 case bfd_link_hash_defweak:
2908 if (h->root.u.def.section->owner != inf->not_needed)
2909 return TRUE;
2910 break;
2912 case bfd_link_hash_common:
2913 if (h->root.u.c.p->section->owner != inf->not_needed)
2914 return TRUE;
2915 break;
2917 case bfd_link_hash_warning:
2918 case bfd_link_hash_indirect:
2919 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2920 if (h->root.u.i.link->type != bfd_link_hash_new)
2921 return TRUE;
2922 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2923 return TRUE;
2924 break;
2927 /* There is no way we can undo symbol table state from defined or
2928 defweak back to undefined. */
2929 if (h->ref_regular)
2930 abort ();
2932 /* Set sym back to newly created state, but keep undefs list pointer. */
2933 bh = h->root.u.undef.next;
2934 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2935 inf->twiddled = TRUE;
2936 (*inf->htab->root.table.newfunc) (&h->root.root,
2937 &inf->htab->root.table,
2938 h->root.root.string);
2939 h->root.u.undef.next = bh;
2940 h->root.u.undef.abfd = inf->not_needed;
2941 h->non_elf = 0;
2942 return TRUE;
2945 /* Sort symbol by value and section. */
2946 static int
2947 elf_sort_symbol (const void *arg1, const void *arg2)
2949 const struct elf_link_hash_entry *h1;
2950 const struct elf_link_hash_entry *h2;
2951 bfd_signed_vma vdiff;
2953 h1 = *(const struct elf_link_hash_entry **) arg1;
2954 h2 = *(const struct elf_link_hash_entry **) arg2;
2955 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2956 if (vdiff != 0)
2957 return vdiff > 0 ? 1 : -1;
2958 else
2960 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2961 if (sdiff != 0)
2962 return sdiff > 0 ? 1 : -1;
2964 return 0;
2967 /* This function is used to adjust offsets into .dynstr for
2968 dynamic symbols. This is called via elf_link_hash_traverse. */
2970 static bfd_boolean
2971 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2973 struct elf_strtab_hash *dynstr = data;
2975 if (h->root.type == bfd_link_hash_warning)
2976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2978 if (h->dynindx != -1)
2979 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2980 return TRUE;
2983 /* Assign string offsets in .dynstr, update all structures referencing
2984 them. */
2986 static bfd_boolean
2987 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2989 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2990 struct elf_link_local_dynamic_entry *entry;
2991 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2992 bfd *dynobj = hash_table->dynobj;
2993 asection *sdyn;
2994 bfd_size_type size;
2995 const struct elf_backend_data *bed;
2996 bfd_byte *extdyn;
2998 _bfd_elf_strtab_finalize (dynstr);
2999 size = _bfd_elf_strtab_size (dynstr);
3001 bed = get_elf_backend_data (dynobj);
3002 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3003 BFD_ASSERT (sdyn != NULL);
3005 /* Update all .dynamic entries referencing .dynstr strings. */
3006 for (extdyn = sdyn->contents;
3007 extdyn < sdyn->contents + sdyn->size;
3008 extdyn += bed->s->sizeof_dyn)
3010 Elf_Internal_Dyn dyn;
3012 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3013 switch (dyn.d_tag)
3015 case DT_STRSZ:
3016 dyn.d_un.d_val = size;
3017 break;
3018 case DT_NEEDED:
3019 case DT_SONAME:
3020 case DT_RPATH:
3021 case DT_RUNPATH:
3022 case DT_FILTER:
3023 case DT_AUXILIARY:
3024 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3025 break;
3026 default:
3027 continue;
3029 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3032 /* Now update local dynamic symbols. */
3033 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3034 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3035 entry->isym.st_name);
3037 /* And the rest of dynamic symbols. */
3038 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3040 /* Adjust version definitions. */
3041 if (elf_tdata (output_bfd)->cverdefs)
3043 asection *s;
3044 bfd_byte *p;
3045 bfd_size_type i;
3046 Elf_Internal_Verdef def;
3047 Elf_Internal_Verdaux defaux;
3049 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3050 p = s->contents;
3053 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3054 &def);
3055 p += sizeof (Elf_External_Verdef);
3056 if (def.vd_aux != sizeof (Elf_External_Verdef))
3057 continue;
3058 for (i = 0; i < def.vd_cnt; ++i)
3060 _bfd_elf_swap_verdaux_in (output_bfd,
3061 (Elf_External_Verdaux *) p, &defaux);
3062 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3063 defaux.vda_name);
3064 _bfd_elf_swap_verdaux_out (output_bfd,
3065 &defaux, (Elf_External_Verdaux *) p);
3066 p += sizeof (Elf_External_Verdaux);
3069 while (def.vd_next);
3072 /* Adjust version references. */
3073 if (elf_tdata (output_bfd)->verref)
3075 asection *s;
3076 bfd_byte *p;
3077 bfd_size_type i;
3078 Elf_Internal_Verneed need;
3079 Elf_Internal_Vernaux needaux;
3081 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3082 p = s->contents;
3085 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3086 &need);
3087 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3088 _bfd_elf_swap_verneed_out (output_bfd, &need,
3089 (Elf_External_Verneed *) p);
3090 p += sizeof (Elf_External_Verneed);
3091 for (i = 0; i < need.vn_cnt; ++i)
3093 _bfd_elf_swap_vernaux_in (output_bfd,
3094 (Elf_External_Vernaux *) p, &needaux);
3095 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3096 needaux.vna_name);
3097 _bfd_elf_swap_vernaux_out (output_bfd,
3098 &needaux,
3099 (Elf_External_Vernaux *) p);
3100 p += sizeof (Elf_External_Vernaux);
3103 while (need.vn_next);
3106 return TRUE;
3109 /* Add symbols from an ELF object file to the linker hash table. */
3111 static bfd_boolean
3112 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3114 bfd_boolean (*add_symbol_hook)
3115 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3116 const char **, flagword *, asection **, bfd_vma *);
3117 bfd_boolean (*check_relocs)
3118 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3119 bfd_boolean (*check_directives)
3120 (bfd *, struct bfd_link_info *);
3121 bfd_boolean collect;
3122 Elf_Internal_Shdr *hdr;
3123 bfd_size_type symcount;
3124 bfd_size_type extsymcount;
3125 bfd_size_type extsymoff;
3126 struct elf_link_hash_entry **sym_hash;
3127 bfd_boolean dynamic;
3128 Elf_External_Versym *extversym = NULL;
3129 Elf_External_Versym *ever;
3130 struct elf_link_hash_entry *weaks;
3131 struct elf_link_hash_entry **nondeflt_vers = NULL;
3132 bfd_size_type nondeflt_vers_cnt = 0;
3133 Elf_Internal_Sym *isymbuf = NULL;
3134 Elf_Internal_Sym *isym;
3135 Elf_Internal_Sym *isymend;
3136 const struct elf_backend_data *bed;
3137 bfd_boolean add_needed;
3138 struct elf_link_hash_table * hash_table;
3139 bfd_size_type amt;
3141 hash_table = elf_hash_table (info);
3143 bed = get_elf_backend_data (abfd);
3144 add_symbol_hook = bed->elf_add_symbol_hook;
3145 collect = bed->collect;
3147 if ((abfd->flags & DYNAMIC) == 0)
3148 dynamic = FALSE;
3149 else
3151 dynamic = TRUE;
3153 /* You can't use -r against a dynamic object. Also, there's no
3154 hope of using a dynamic object which does not exactly match
3155 the format of the output file. */
3156 if (info->relocatable
3157 || !is_elf_hash_table (hash_table)
3158 || hash_table->root.creator != abfd->xvec)
3160 if (info->relocatable)
3161 bfd_set_error (bfd_error_invalid_operation);
3162 else
3163 bfd_set_error (bfd_error_wrong_format);
3164 goto error_return;
3168 /* As a GNU extension, any input sections which are named
3169 .gnu.warning.SYMBOL are treated as warning symbols for the given
3170 symbol. This differs from .gnu.warning sections, which generate
3171 warnings when they are included in an output file. */
3172 if (info->executable)
3174 asection *s;
3176 for (s = abfd->sections; s != NULL; s = s->next)
3178 const char *name;
3180 name = bfd_get_section_name (abfd, s);
3181 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3183 char *msg;
3184 bfd_size_type sz;
3186 name += sizeof ".gnu.warning." - 1;
3188 /* If this is a shared object, then look up the symbol
3189 in the hash table. If it is there, and it is already
3190 been defined, then we will not be using the entry
3191 from this shared object, so we don't need to warn.
3192 FIXME: If we see the definition in a regular object
3193 later on, we will warn, but we shouldn't. The only
3194 fix is to keep track of what warnings we are supposed
3195 to emit, and then handle them all at the end of the
3196 link. */
3197 if (dynamic)
3199 struct elf_link_hash_entry *h;
3201 h = elf_link_hash_lookup (hash_table, name,
3202 FALSE, FALSE, TRUE);
3204 /* FIXME: What about bfd_link_hash_common? */
3205 if (h != NULL
3206 && (h->root.type == bfd_link_hash_defined
3207 || h->root.type == bfd_link_hash_defweak))
3209 /* We don't want to issue this warning. Clobber
3210 the section size so that the warning does not
3211 get copied into the output file. */
3212 s->size = 0;
3213 continue;
3217 sz = s->size;
3218 msg = bfd_alloc (abfd, sz + 1);
3219 if (msg == NULL)
3220 goto error_return;
3222 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3223 goto error_return;
3225 msg[sz] = '\0';
3227 if (! (_bfd_generic_link_add_one_symbol
3228 (info, abfd, name, BSF_WARNING, s, 0, msg,
3229 FALSE, collect, NULL)))
3230 goto error_return;
3232 if (! info->relocatable)
3234 /* Clobber the section size so that the warning does
3235 not get copied into the output file. */
3236 s->size = 0;
3242 add_needed = TRUE;
3243 if (! dynamic)
3245 /* If we are creating a shared library, create all the dynamic
3246 sections immediately. We need to attach them to something,
3247 so we attach them to this BFD, provided it is the right
3248 format. FIXME: If there are no input BFD's of the same
3249 format as the output, we can't make a shared library. */
3250 if (info->shared
3251 && is_elf_hash_table (hash_table)
3252 && hash_table->root.creator == abfd->xvec
3253 && ! hash_table->dynamic_sections_created)
3255 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3256 goto error_return;
3259 else if (!is_elf_hash_table (hash_table))
3260 goto error_return;
3261 else
3263 asection *s;
3264 const char *soname = NULL;
3265 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3266 int ret;
3268 /* ld --just-symbols and dynamic objects don't mix very well.
3269 Test for --just-symbols by looking at info set up by
3270 _bfd_elf_link_just_syms. */
3271 if ((s = abfd->sections) != NULL
3272 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3273 goto error_return;
3275 /* If this dynamic lib was specified on the command line with
3276 --as-needed in effect, then we don't want to add a DT_NEEDED
3277 tag unless the lib is actually used. Similary for libs brought
3278 in by another lib's DT_NEEDED. When --no-add-needed is used
3279 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3280 any dynamic library in DT_NEEDED tags in the dynamic lib at
3281 all. */
3282 add_needed = (elf_dyn_lib_class (abfd)
3283 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3284 | DYN_NO_NEEDED)) == 0;
3286 s = bfd_get_section_by_name (abfd, ".dynamic");
3287 if (s != NULL)
3289 bfd_byte *dynbuf;
3290 bfd_byte *extdyn;
3291 int elfsec;
3292 unsigned long shlink;
3294 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3295 goto error_free_dyn;
3297 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3298 if (elfsec == -1)
3299 goto error_free_dyn;
3300 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3302 for (extdyn = dynbuf;
3303 extdyn < dynbuf + s->size;
3304 extdyn += bed->s->sizeof_dyn)
3306 Elf_Internal_Dyn dyn;
3308 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3309 if (dyn.d_tag == DT_SONAME)
3311 unsigned int tagv = dyn.d_un.d_val;
3312 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3313 if (soname == NULL)
3314 goto error_free_dyn;
3316 if (dyn.d_tag == DT_NEEDED)
3318 struct bfd_link_needed_list *n, **pn;
3319 char *fnm, *anm;
3320 unsigned int tagv = dyn.d_un.d_val;
3322 amt = sizeof (struct bfd_link_needed_list);
3323 n = bfd_alloc (abfd, amt);
3324 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3325 if (n == NULL || fnm == NULL)
3326 goto error_free_dyn;
3327 amt = strlen (fnm) + 1;
3328 anm = bfd_alloc (abfd, amt);
3329 if (anm == NULL)
3330 goto error_free_dyn;
3331 memcpy (anm, fnm, amt);
3332 n->name = anm;
3333 n->by = abfd;
3334 n->next = NULL;
3335 for (pn = & hash_table->needed;
3336 *pn != NULL;
3337 pn = &(*pn)->next)
3339 *pn = n;
3341 if (dyn.d_tag == DT_RUNPATH)
3343 struct bfd_link_needed_list *n, **pn;
3344 char *fnm, *anm;
3345 unsigned int tagv = dyn.d_un.d_val;
3347 amt = sizeof (struct bfd_link_needed_list);
3348 n = bfd_alloc (abfd, amt);
3349 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3350 if (n == NULL || fnm == NULL)
3351 goto error_free_dyn;
3352 amt = strlen (fnm) + 1;
3353 anm = bfd_alloc (abfd, amt);
3354 if (anm == NULL)
3355 goto error_free_dyn;
3356 memcpy (anm, fnm, amt);
3357 n->name = anm;
3358 n->by = abfd;
3359 n->next = NULL;
3360 for (pn = & runpath;
3361 *pn != NULL;
3362 pn = &(*pn)->next)
3364 *pn = n;
3366 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3367 if (!runpath && dyn.d_tag == DT_RPATH)
3369 struct bfd_link_needed_list *n, **pn;
3370 char *fnm, *anm;
3371 unsigned int tagv = dyn.d_un.d_val;
3373 amt = sizeof (struct bfd_link_needed_list);
3374 n = bfd_alloc (abfd, amt);
3375 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3376 if (n == NULL || fnm == NULL)
3377 goto error_free_dyn;
3378 amt = strlen (fnm) + 1;
3379 anm = bfd_alloc (abfd, amt);
3380 if (anm == NULL)
3382 error_free_dyn:
3383 free (dynbuf);
3384 goto error_return;
3386 memcpy (anm, fnm, amt);
3387 n->name = anm;
3388 n->by = abfd;
3389 n->next = NULL;
3390 for (pn = & rpath;
3391 *pn != NULL;
3392 pn = &(*pn)->next)
3394 *pn = n;
3398 free (dynbuf);
3401 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3402 frees all more recently bfd_alloc'd blocks as well. */
3403 if (runpath)
3404 rpath = runpath;
3406 if (rpath)
3408 struct bfd_link_needed_list **pn;
3409 for (pn = & hash_table->runpath;
3410 *pn != NULL;
3411 pn = &(*pn)->next)
3413 *pn = rpath;
3416 /* We do not want to include any of the sections in a dynamic
3417 object in the output file. We hack by simply clobbering the
3418 list of sections in the BFD. This could be handled more
3419 cleanly by, say, a new section flag; the existing
3420 SEC_NEVER_LOAD flag is not the one we want, because that one
3421 still implies that the section takes up space in the output
3422 file. */
3423 bfd_section_list_clear (abfd);
3425 /* Find the name to use in a DT_NEEDED entry that refers to this
3426 object. If the object has a DT_SONAME entry, we use it.
3427 Otherwise, if the generic linker stuck something in
3428 elf_dt_name, we use that. Otherwise, we just use the file
3429 name. */
3430 if (soname == NULL || *soname == '\0')
3432 soname = elf_dt_name (abfd);
3433 if (soname == NULL || *soname == '\0')
3434 soname = bfd_get_filename (abfd);
3437 /* Save the SONAME because sometimes the linker emulation code
3438 will need to know it. */
3439 elf_dt_name (abfd) = soname;
3441 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3442 if (ret < 0)
3443 goto error_return;
3445 /* If we have already included this dynamic object in the
3446 link, just ignore it. There is no reason to include a
3447 particular dynamic object more than once. */
3448 if (ret > 0)
3449 return TRUE;
3452 /* If this is a dynamic object, we always link against the .dynsym
3453 symbol table, not the .symtab symbol table. The dynamic linker
3454 will only see the .dynsym symbol table, so there is no reason to
3455 look at .symtab for a dynamic object. */
3457 if (! dynamic || elf_dynsymtab (abfd) == 0)
3458 hdr = &elf_tdata (abfd)->symtab_hdr;
3459 else
3460 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3462 symcount = hdr->sh_size / bed->s->sizeof_sym;
3464 /* The sh_info field of the symtab header tells us where the
3465 external symbols start. We don't care about the local symbols at
3466 this point. */
3467 if (elf_bad_symtab (abfd))
3469 extsymcount = symcount;
3470 extsymoff = 0;
3472 else
3474 extsymcount = symcount - hdr->sh_info;
3475 extsymoff = hdr->sh_info;
3478 sym_hash = NULL;
3479 if (extsymcount != 0)
3481 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3482 NULL, NULL, NULL);
3483 if (isymbuf == NULL)
3484 goto error_return;
3486 /* We store a pointer to the hash table entry for each external
3487 symbol. */
3488 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3489 sym_hash = bfd_alloc (abfd, amt);
3490 if (sym_hash == NULL)
3491 goto error_free_sym;
3492 elf_sym_hashes (abfd) = sym_hash;
3495 if (dynamic)
3497 /* Read in any version definitions. */
3498 if (!_bfd_elf_slurp_version_tables (abfd,
3499 info->default_imported_symver))
3500 goto error_free_sym;
3502 /* Read in the symbol versions, but don't bother to convert them
3503 to internal format. */
3504 if (elf_dynversym (abfd) != 0)
3506 Elf_Internal_Shdr *versymhdr;
3508 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3509 extversym = bfd_malloc (versymhdr->sh_size);
3510 if (extversym == NULL)
3511 goto error_free_sym;
3512 amt = versymhdr->sh_size;
3513 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3514 || bfd_bread (extversym, amt, abfd) != amt)
3515 goto error_free_vers;
3519 weaks = NULL;
3521 ever = extversym != NULL ? extversym + extsymoff : NULL;
3522 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3523 isym < isymend;
3524 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3526 int bind;
3527 bfd_vma value;
3528 asection *sec;
3529 flagword flags;
3530 const char *name;
3531 struct elf_link_hash_entry *h;
3532 bfd_boolean definition;
3533 bfd_boolean size_change_ok;
3534 bfd_boolean type_change_ok;
3535 bfd_boolean new_weakdef;
3536 bfd_boolean override;
3537 unsigned int old_alignment;
3538 bfd *old_bfd;
3540 override = FALSE;
3542 flags = BSF_NO_FLAGS;
3543 sec = NULL;
3544 value = isym->st_value;
3545 *sym_hash = NULL;
3547 bind = ELF_ST_BIND (isym->st_info);
3548 if (bind == STB_LOCAL)
3550 /* This should be impossible, since ELF requires that all
3551 global symbols follow all local symbols, and that sh_info
3552 point to the first global symbol. Unfortunately, Irix 5
3553 screws this up. */
3554 continue;
3556 else if (bind == STB_GLOBAL)
3558 if (isym->st_shndx != SHN_UNDEF
3559 && isym->st_shndx != SHN_COMMON)
3560 flags = BSF_GLOBAL;
3562 else if (bind == STB_WEAK)
3563 flags = BSF_WEAK;
3564 else
3566 /* Leave it up to the processor backend. */
3569 if (isym->st_shndx == SHN_UNDEF)
3570 sec = bfd_und_section_ptr;
3571 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3573 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3574 if (sec == NULL)
3575 sec = bfd_abs_section_ptr;
3576 else if (sec->kept_section)
3578 /* Symbols from discarded section are undefined. */
3579 sec = bfd_und_section_ptr;
3580 isym->st_shndx = SHN_UNDEF;
3582 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3583 value -= sec->vma;
3585 else if (isym->st_shndx == SHN_ABS)
3586 sec = bfd_abs_section_ptr;
3587 else if (isym->st_shndx == SHN_COMMON)
3589 sec = bfd_com_section_ptr;
3590 /* What ELF calls the size we call the value. What ELF
3591 calls the value we call the alignment. */
3592 value = isym->st_size;
3594 else
3596 /* Leave it up to the processor backend. */
3599 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3600 isym->st_name);
3601 if (name == NULL)
3602 goto error_free_vers;
3604 if (isym->st_shndx == SHN_COMMON
3605 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3607 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3609 if (tcomm == NULL)
3611 tcomm = bfd_make_section (abfd, ".tcommon");
3612 if (tcomm == NULL
3613 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3614 | SEC_IS_COMMON
3615 | SEC_LINKER_CREATED
3616 | SEC_THREAD_LOCAL)))
3617 goto error_free_vers;
3619 sec = tcomm;
3621 else if (add_symbol_hook)
3623 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3624 &value))
3625 goto error_free_vers;
3627 /* The hook function sets the name to NULL if this symbol
3628 should be skipped for some reason. */
3629 if (name == NULL)
3630 continue;
3633 /* Sanity check that all possibilities were handled. */
3634 if (sec == NULL)
3636 bfd_set_error (bfd_error_bad_value);
3637 goto error_free_vers;
3640 if (bfd_is_und_section (sec)
3641 || bfd_is_com_section (sec))
3642 definition = FALSE;
3643 else
3644 definition = TRUE;
3646 size_change_ok = FALSE;
3647 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3648 old_alignment = 0;
3649 old_bfd = NULL;
3651 if (is_elf_hash_table (hash_table))
3653 Elf_Internal_Versym iver;
3654 unsigned int vernum = 0;
3655 bfd_boolean skip;
3657 if (ever == NULL)
3659 if (info->default_imported_symver)
3660 /* Use the default symbol version created earlier. */
3661 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3662 else
3663 iver.vs_vers = 0;
3665 else
3666 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3668 vernum = iver.vs_vers & VERSYM_VERSION;
3670 /* If this is a hidden symbol, or if it is not version
3671 1, we append the version name to the symbol name.
3672 However, we do not modify a non-hidden absolute
3673 symbol, because it might be the version symbol
3674 itself. FIXME: What if it isn't? */
3675 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3676 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3678 const char *verstr;
3679 size_t namelen, verlen, newlen;
3680 char *newname, *p;
3682 if (isym->st_shndx != SHN_UNDEF)
3684 if (vernum > elf_tdata (abfd)->cverdefs)
3685 verstr = NULL;
3686 else if (vernum > 1)
3687 verstr =
3688 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3689 else
3690 verstr = "";
3692 if (verstr == NULL)
3694 (*_bfd_error_handler)
3695 (_("%B: %s: invalid version %u (max %d)"),
3696 abfd, name, vernum,
3697 elf_tdata (abfd)->cverdefs);
3698 bfd_set_error (bfd_error_bad_value);
3699 goto error_free_vers;
3702 else
3704 /* We cannot simply test for the number of
3705 entries in the VERNEED section since the
3706 numbers for the needed versions do not start
3707 at 0. */
3708 Elf_Internal_Verneed *t;
3710 verstr = NULL;
3711 for (t = elf_tdata (abfd)->verref;
3712 t != NULL;
3713 t = t->vn_nextref)
3715 Elf_Internal_Vernaux *a;
3717 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3719 if (a->vna_other == vernum)
3721 verstr = a->vna_nodename;
3722 break;
3725 if (a != NULL)
3726 break;
3728 if (verstr == NULL)
3730 (*_bfd_error_handler)
3731 (_("%B: %s: invalid needed version %d"),
3732 abfd, name, vernum);
3733 bfd_set_error (bfd_error_bad_value);
3734 goto error_free_vers;
3738 namelen = strlen (name);
3739 verlen = strlen (verstr);
3740 newlen = namelen + verlen + 2;
3741 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3742 && isym->st_shndx != SHN_UNDEF)
3743 ++newlen;
3745 newname = bfd_alloc (abfd, newlen);
3746 if (newname == NULL)
3747 goto error_free_vers;
3748 memcpy (newname, name, namelen);
3749 p = newname + namelen;
3750 *p++ = ELF_VER_CHR;
3751 /* If this is a defined non-hidden version symbol,
3752 we add another @ to the name. This indicates the
3753 default version of the symbol. */
3754 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3755 && isym->st_shndx != SHN_UNDEF)
3756 *p++ = ELF_VER_CHR;
3757 memcpy (p, verstr, verlen + 1);
3759 name = newname;
3762 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3763 sym_hash, &skip, &override,
3764 &type_change_ok, &size_change_ok))
3765 goto error_free_vers;
3767 if (skip)
3768 continue;
3770 if (override)
3771 definition = FALSE;
3773 h = *sym_hash;
3774 while (h->root.type == bfd_link_hash_indirect
3775 || h->root.type == bfd_link_hash_warning)
3776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3778 /* Remember the old alignment if this is a common symbol, so
3779 that we don't reduce the alignment later on. We can't
3780 check later, because _bfd_generic_link_add_one_symbol
3781 will set a default for the alignment which we want to
3782 override. We also remember the old bfd where the existing
3783 definition comes from. */
3784 switch (h->root.type)
3786 default:
3787 break;
3789 case bfd_link_hash_defined:
3790 case bfd_link_hash_defweak:
3791 old_bfd = h->root.u.def.section->owner;
3792 break;
3794 case bfd_link_hash_common:
3795 old_bfd = h->root.u.c.p->section->owner;
3796 old_alignment = h->root.u.c.p->alignment_power;
3797 break;
3800 if (elf_tdata (abfd)->verdef != NULL
3801 && ! override
3802 && vernum > 1
3803 && definition)
3804 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3807 if (! (_bfd_generic_link_add_one_symbol
3808 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3809 (struct bfd_link_hash_entry **) sym_hash)))
3810 goto error_free_vers;
3812 h = *sym_hash;
3813 while (h->root.type == bfd_link_hash_indirect
3814 || h->root.type == bfd_link_hash_warning)
3815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3816 *sym_hash = h;
3818 new_weakdef = FALSE;
3819 if (dynamic
3820 && definition
3821 && (flags & BSF_WEAK) != 0
3822 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3823 && is_elf_hash_table (hash_table)
3824 && h->u.weakdef == NULL)
3826 /* Keep a list of all weak defined non function symbols from
3827 a dynamic object, using the weakdef field. Later in this
3828 function we will set the weakdef field to the correct
3829 value. We only put non-function symbols from dynamic
3830 objects on this list, because that happens to be the only
3831 time we need to know the normal symbol corresponding to a
3832 weak symbol, and the information is time consuming to
3833 figure out. If the weakdef field is not already NULL,
3834 then this symbol was already defined by some previous
3835 dynamic object, and we will be using that previous
3836 definition anyhow. */
3838 h->u.weakdef = weaks;
3839 weaks = h;
3840 new_weakdef = TRUE;
3843 /* Set the alignment of a common symbol. */
3844 if (isym->st_shndx == SHN_COMMON
3845 && h->root.type == bfd_link_hash_common)
3847 unsigned int align;
3849 align = bfd_log2 (isym->st_value);
3850 if (align > old_alignment
3851 /* Permit an alignment power of zero if an alignment of one
3852 is specified and no other alignments have been specified. */
3853 || (isym->st_value == 1 && old_alignment == 0))
3854 h->root.u.c.p->alignment_power = align;
3855 else
3856 h->root.u.c.p->alignment_power = old_alignment;
3859 if (is_elf_hash_table (hash_table))
3861 bfd_boolean dynsym;
3863 /* Check the alignment when a common symbol is involved. This
3864 can change when a common symbol is overridden by a normal
3865 definition or a common symbol is ignored due to the old
3866 normal definition. We need to make sure the maximum
3867 alignment is maintained. */
3868 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3869 && h->root.type != bfd_link_hash_common)
3871 unsigned int common_align;
3872 unsigned int normal_align;
3873 unsigned int symbol_align;
3874 bfd *normal_bfd;
3875 bfd *common_bfd;
3877 symbol_align = ffs (h->root.u.def.value) - 1;
3878 if (h->root.u.def.section->owner != NULL
3879 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3881 normal_align = h->root.u.def.section->alignment_power;
3882 if (normal_align > symbol_align)
3883 normal_align = symbol_align;
3885 else
3886 normal_align = symbol_align;
3888 if (old_alignment)
3890 common_align = old_alignment;
3891 common_bfd = old_bfd;
3892 normal_bfd = abfd;
3894 else
3896 common_align = bfd_log2 (isym->st_value);
3897 common_bfd = abfd;
3898 normal_bfd = old_bfd;
3901 if (normal_align < common_align)
3902 (*_bfd_error_handler)
3903 (_("Warning: alignment %u of symbol `%s' in %B"
3904 " is smaller than %u in %B"),
3905 normal_bfd, common_bfd,
3906 1 << normal_align, name, 1 << common_align);
3909 /* Remember the symbol size and type. */
3910 if (isym->st_size != 0
3911 && (definition || h->size == 0))
3913 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3914 (*_bfd_error_handler)
3915 (_("Warning: size of symbol `%s' changed"
3916 " from %lu in %B to %lu in %B"),
3917 old_bfd, abfd,
3918 name, (unsigned long) h->size,
3919 (unsigned long) isym->st_size);
3921 h->size = isym->st_size;
3924 /* If this is a common symbol, then we always want H->SIZE
3925 to be the size of the common symbol. The code just above
3926 won't fix the size if a common symbol becomes larger. We
3927 don't warn about a size change here, because that is
3928 covered by --warn-common. */
3929 if (h->root.type == bfd_link_hash_common)
3930 h->size = h->root.u.c.size;
3932 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3933 && (definition || h->type == STT_NOTYPE))
3935 if (h->type != STT_NOTYPE
3936 && h->type != ELF_ST_TYPE (isym->st_info)
3937 && ! type_change_ok)
3938 (*_bfd_error_handler)
3939 (_("Warning: type of symbol `%s' changed"
3940 " from %d to %d in %B"),
3941 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3943 h->type = ELF_ST_TYPE (isym->st_info);
3946 /* If st_other has a processor-specific meaning, specific
3947 code might be needed here. We never merge the visibility
3948 attribute with the one from a dynamic object. */
3949 if (bed->elf_backend_merge_symbol_attribute)
3950 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3951 dynamic);
3953 /* If this symbol has default visibility and the user has requested
3954 we not re-export it, then mark it as hidden. */
3955 if (definition && !dynamic
3956 && (abfd->no_export
3957 || (abfd->my_archive && abfd->my_archive->no_export))
3958 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3959 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3961 if (isym->st_other != 0 && !dynamic)
3963 unsigned char hvis, symvis, other, nvis;
3965 /* Take the balance of OTHER from the definition. */
3966 other = (definition ? isym->st_other : h->other);
3967 other &= ~ ELF_ST_VISIBILITY (-1);
3969 /* Combine visibilities, using the most constraining one. */
3970 hvis = ELF_ST_VISIBILITY (h->other);
3971 symvis = ELF_ST_VISIBILITY (isym->st_other);
3972 if (! hvis)
3973 nvis = symvis;
3974 else if (! symvis)
3975 nvis = hvis;
3976 else
3977 nvis = hvis < symvis ? hvis : symvis;
3979 h->other = other | nvis;
3982 /* Set a flag in the hash table entry indicating the type of
3983 reference or definition we just found. Keep a count of
3984 the number of dynamic symbols we find. A dynamic symbol
3985 is one which is referenced or defined by both a regular
3986 object and a shared object. */
3987 dynsym = FALSE;
3988 if (! dynamic)
3990 if (! definition)
3992 h->ref_regular = 1;
3993 if (bind != STB_WEAK)
3994 h->ref_regular_nonweak = 1;
3996 else
3997 h->def_regular = 1;
3998 if (! info->executable
3999 || h->def_dynamic
4000 || h->ref_dynamic)
4001 dynsym = TRUE;
4003 else
4005 if (! definition)
4006 h->ref_dynamic = 1;
4007 else
4008 h->def_dynamic = 1;
4009 if (h->def_regular
4010 || h->ref_regular
4011 || (h->u.weakdef != NULL
4012 && ! new_weakdef
4013 && h->u.weakdef->dynindx != -1))
4014 dynsym = TRUE;
4017 /* Check to see if we need to add an indirect symbol for
4018 the default name. */
4019 if (definition || h->root.type == bfd_link_hash_common)
4020 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4021 &sec, &value, &dynsym,
4022 override))
4023 goto error_free_vers;
4025 if (definition && !dynamic)
4027 char *p = strchr (name, ELF_VER_CHR);
4028 if (p != NULL && p[1] != ELF_VER_CHR)
4030 /* Queue non-default versions so that .symver x, x@FOO
4031 aliases can be checked. */
4032 if (! nondeflt_vers)
4034 amt = (isymend - isym + 1)
4035 * sizeof (struct elf_link_hash_entry *);
4036 nondeflt_vers = bfd_malloc (amt);
4038 nondeflt_vers [nondeflt_vers_cnt++] = h;
4042 if (dynsym && h->dynindx == -1)
4044 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4045 goto error_free_vers;
4046 if (h->u.weakdef != NULL
4047 && ! new_weakdef
4048 && h->u.weakdef->dynindx == -1)
4050 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4051 goto error_free_vers;
4054 else if (dynsym && h->dynindx != -1)
4055 /* If the symbol already has a dynamic index, but
4056 visibility says it should not be visible, turn it into
4057 a local symbol. */
4058 switch (ELF_ST_VISIBILITY (h->other))
4060 case STV_INTERNAL:
4061 case STV_HIDDEN:
4062 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4063 dynsym = FALSE;
4064 break;
4067 if (!add_needed
4068 && definition
4069 && dynsym
4070 && h->ref_regular)
4072 int ret;
4073 const char *soname = elf_dt_name (abfd);
4075 /* A symbol from a library loaded via DT_NEEDED of some
4076 other library is referenced by a regular object.
4077 Add a DT_NEEDED entry for it. Issue an error if
4078 --no-add-needed is used. */
4079 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4081 (*_bfd_error_handler)
4082 (_("%s: invalid DSO for symbol `%s' definition"),
4083 abfd, name);
4084 bfd_set_error (bfd_error_bad_value);
4085 goto error_free_vers;
4088 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4090 add_needed = TRUE;
4091 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4092 if (ret < 0)
4093 goto error_free_vers;
4095 BFD_ASSERT (ret == 0);
4100 /* Now that all the symbols from this input file are created, handle
4101 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4102 if (nondeflt_vers != NULL)
4104 bfd_size_type cnt, symidx;
4106 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4108 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4109 char *shortname, *p;
4111 p = strchr (h->root.root.string, ELF_VER_CHR);
4112 if (p == NULL
4113 || (h->root.type != bfd_link_hash_defined
4114 && h->root.type != bfd_link_hash_defweak))
4115 continue;
4117 amt = p - h->root.root.string;
4118 shortname = bfd_malloc (amt + 1);
4119 memcpy (shortname, h->root.root.string, amt);
4120 shortname[amt] = '\0';
4122 hi = (struct elf_link_hash_entry *)
4123 bfd_link_hash_lookup (&hash_table->root, shortname,
4124 FALSE, FALSE, FALSE);
4125 if (hi != NULL
4126 && hi->root.type == h->root.type
4127 && hi->root.u.def.value == h->root.u.def.value
4128 && hi->root.u.def.section == h->root.u.def.section)
4130 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4131 hi->root.type = bfd_link_hash_indirect;
4132 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4133 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4134 sym_hash = elf_sym_hashes (abfd);
4135 if (sym_hash)
4136 for (symidx = 0; symidx < extsymcount; ++symidx)
4137 if (sym_hash[symidx] == hi)
4139 sym_hash[symidx] = h;
4140 break;
4143 free (shortname);
4145 free (nondeflt_vers);
4146 nondeflt_vers = NULL;
4149 if (extversym != NULL)
4151 free (extversym);
4152 extversym = NULL;
4155 if (isymbuf != NULL)
4156 free (isymbuf);
4157 isymbuf = NULL;
4159 if (!add_needed
4160 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4162 /* Remove symbols defined in an as-needed shared lib that wasn't
4163 needed. */
4164 struct elf_smash_syms_data inf;
4165 inf.not_needed = abfd;
4166 inf.htab = hash_table;
4167 inf.twiddled = FALSE;
4168 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4169 if (inf.twiddled)
4170 bfd_link_repair_undef_list (&hash_table->root);
4171 weaks = NULL;
4174 /* Now set the weakdefs field correctly for all the weak defined
4175 symbols we found. The only way to do this is to search all the
4176 symbols. Since we only need the information for non functions in
4177 dynamic objects, that's the only time we actually put anything on
4178 the list WEAKS. We need this information so that if a regular
4179 object refers to a symbol defined weakly in a dynamic object, the
4180 real symbol in the dynamic object is also put in the dynamic
4181 symbols; we also must arrange for both symbols to point to the
4182 same memory location. We could handle the general case of symbol
4183 aliasing, but a general symbol alias can only be generated in
4184 assembler code, handling it correctly would be very time
4185 consuming, and other ELF linkers don't handle general aliasing
4186 either. */
4187 if (weaks != NULL)
4189 struct elf_link_hash_entry **hpp;
4190 struct elf_link_hash_entry **hppend;
4191 struct elf_link_hash_entry **sorted_sym_hash;
4192 struct elf_link_hash_entry *h;
4193 size_t sym_count;
4195 /* Since we have to search the whole symbol list for each weak
4196 defined symbol, search time for N weak defined symbols will be
4197 O(N^2). Binary search will cut it down to O(NlogN). */
4198 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4199 sorted_sym_hash = bfd_malloc (amt);
4200 if (sorted_sym_hash == NULL)
4201 goto error_return;
4202 sym_hash = sorted_sym_hash;
4203 hpp = elf_sym_hashes (abfd);
4204 hppend = hpp + extsymcount;
4205 sym_count = 0;
4206 for (; hpp < hppend; hpp++)
4208 h = *hpp;
4209 if (h != NULL
4210 && h->root.type == bfd_link_hash_defined
4211 && h->type != STT_FUNC)
4213 *sym_hash = h;
4214 sym_hash++;
4215 sym_count++;
4219 qsort (sorted_sym_hash, sym_count,
4220 sizeof (struct elf_link_hash_entry *),
4221 elf_sort_symbol);
4223 while (weaks != NULL)
4225 struct elf_link_hash_entry *hlook;
4226 asection *slook;
4227 bfd_vma vlook;
4228 long ilook;
4229 size_t i, j, idx;
4231 hlook = weaks;
4232 weaks = hlook->u.weakdef;
4233 hlook->u.weakdef = NULL;
4235 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4236 || hlook->root.type == bfd_link_hash_defweak
4237 || hlook->root.type == bfd_link_hash_common
4238 || hlook->root.type == bfd_link_hash_indirect);
4239 slook = hlook->root.u.def.section;
4240 vlook = hlook->root.u.def.value;
4242 ilook = -1;
4243 i = 0;
4244 j = sym_count;
4245 while (i < j)
4247 bfd_signed_vma vdiff;
4248 idx = (i + j) / 2;
4249 h = sorted_sym_hash [idx];
4250 vdiff = vlook - h->root.u.def.value;
4251 if (vdiff < 0)
4252 j = idx;
4253 else if (vdiff > 0)
4254 i = idx + 1;
4255 else
4257 long sdiff = slook->id - h->root.u.def.section->id;
4258 if (sdiff < 0)
4259 j = idx;
4260 else if (sdiff > 0)
4261 i = idx + 1;
4262 else
4264 ilook = idx;
4265 break;
4270 /* We didn't find a value/section match. */
4271 if (ilook == -1)
4272 continue;
4274 for (i = ilook; i < sym_count; i++)
4276 h = sorted_sym_hash [i];
4278 /* Stop if value or section doesn't match. */
4279 if (h->root.u.def.value != vlook
4280 || h->root.u.def.section != slook)
4281 break;
4282 else if (h != hlook)
4284 hlook->u.weakdef = h;
4286 /* If the weak definition is in the list of dynamic
4287 symbols, make sure the real definition is put
4288 there as well. */
4289 if (hlook->dynindx != -1 && h->dynindx == -1)
4291 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4292 goto error_return;
4295 /* If the real definition is in the list of dynamic
4296 symbols, make sure the weak definition is put
4297 there as well. If we don't do this, then the
4298 dynamic loader might not merge the entries for the
4299 real definition and the weak definition. */
4300 if (h->dynindx != -1 && hlook->dynindx == -1)
4302 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4303 goto error_return;
4305 break;
4310 free (sorted_sym_hash);
4313 check_directives = get_elf_backend_data (abfd)->check_directives;
4314 if (check_directives)
4315 check_directives (abfd, info);
4317 /* If this object is the same format as the output object, and it is
4318 not a shared library, then let the backend look through the
4319 relocs.
4321 This is required to build global offset table entries and to
4322 arrange for dynamic relocs. It is not required for the
4323 particular common case of linking non PIC code, even when linking
4324 against shared libraries, but unfortunately there is no way of
4325 knowing whether an object file has been compiled PIC or not.
4326 Looking through the relocs is not particularly time consuming.
4327 The problem is that we must either (1) keep the relocs in memory,
4328 which causes the linker to require additional runtime memory or
4329 (2) read the relocs twice from the input file, which wastes time.
4330 This would be a good case for using mmap.
4332 I have no idea how to handle linking PIC code into a file of a
4333 different format. It probably can't be done. */
4334 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4335 if (! dynamic
4336 && is_elf_hash_table (hash_table)
4337 && hash_table->root.creator == abfd->xvec
4338 && check_relocs != NULL)
4340 asection *o;
4342 for (o = abfd->sections; o != NULL; o = o->next)
4344 Elf_Internal_Rela *internal_relocs;
4345 bfd_boolean ok;
4347 if ((o->flags & SEC_RELOC) == 0
4348 || o->reloc_count == 0
4349 || ((info->strip == strip_all || info->strip == strip_debugger)
4350 && (o->flags & SEC_DEBUGGING) != 0)
4351 || bfd_is_abs_section (o->output_section))
4352 continue;
4354 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4355 info->keep_memory);
4356 if (internal_relocs == NULL)
4357 goto error_return;
4359 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4361 if (elf_section_data (o)->relocs != internal_relocs)
4362 free (internal_relocs);
4364 if (! ok)
4365 goto error_return;
4369 /* If this is a non-traditional link, try to optimize the handling
4370 of the .stab/.stabstr sections. */
4371 if (! dynamic
4372 && ! info->traditional_format
4373 && is_elf_hash_table (hash_table)
4374 && (info->strip != strip_all && info->strip != strip_debugger))
4376 asection *stabstr;
4378 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4379 if (stabstr != NULL)
4381 bfd_size_type string_offset = 0;
4382 asection *stab;
4384 for (stab = abfd->sections; stab; stab = stab->next)
4385 if (strncmp (".stab", stab->name, 5) == 0
4386 && (!stab->name[5] ||
4387 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4388 && (stab->flags & SEC_MERGE) == 0
4389 && !bfd_is_abs_section (stab->output_section))
4391 struct bfd_elf_section_data *secdata;
4393 secdata = elf_section_data (stab);
4394 if (! _bfd_link_section_stabs (abfd,
4395 &hash_table->stab_info,
4396 stab, stabstr,
4397 &secdata->sec_info,
4398 &string_offset))
4399 goto error_return;
4400 if (secdata->sec_info)
4401 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4406 if (is_elf_hash_table (hash_table) && add_needed)
4408 /* Add this bfd to the loaded list. */
4409 struct elf_link_loaded_list *n;
4411 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4412 if (n == NULL)
4413 goto error_return;
4414 n->abfd = abfd;
4415 n->next = hash_table->loaded;
4416 hash_table->loaded = n;
4419 return TRUE;
4421 error_free_vers:
4422 if (nondeflt_vers != NULL)
4423 free (nondeflt_vers);
4424 if (extversym != NULL)
4425 free (extversym);
4426 error_free_sym:
4427 if (isymbuf != NULL)
4428 free (isymbuf);
4429 error_return:
4430 return FALSE;
4433 /* Return the linker hash table entry of a symbol that might be
4434 satisfied by an archive symbol. Return -1 on error. */
4436 struct elf_link_hash_entry *
4437 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4438 struct bfd_link_info *info,
4439 const char *name)
4441 struct elf_link_hash_entry *h;
4442 char *p, *copy;
4443 size_t len, first;
4445 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4446 if (h != NULL)
4447 return h;
4449 /* If this is a default version (the name contains @@), look up the
4450 symbol again with only one `@' as well as without the version.
4451 The effect is that references to the symbol with and without the
4452 version will be matched by the default symbol in the archive. */
4454 p = strchr (name, ELF_VER_CHR);
4455 if (p == NULL || p[1] != ELF_VER_CHR)
4456 return h;
4458 /* First check with only one `@'. */
4459 len = strlen (name);
4460 copy = bfd_alloc (abfd, len);
4461 if (copy == NULL)
4462 return (struct elf_link_hash_entry *) 0 - 1;
4464 first = p - name + 1;
4465 memcpy (copy, name, first);
4466 memcpy (copy + first, name + first + 1, len - first);
4468 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4469 if (h == NULL)
4471 /* We also need to check references to the symbol without the
4472 version. */
4473 copy[first - 1] = '\0';
4474 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4475 FALSE, FALSE, FALSE);
4478 bfd_release (abfd, copy);
4479 return h;
4482 /* Add symbols from an ELF archive file to the linker hash table. We
4483 don't use _bfd_generic_link_add_archive_symbols because of a
4484 problem which arises on UnixWare. The UnixWare libc.so is an
4485 archive which includes an entry libc.so.1 which defines a bunch of
4486 symbols. The libc.so archive also includes a number of other
4487 object files, which also define symbols, some of which are the same
4488 as those defined in libc.so.1. Correct linking requires that we
4489 consider each object file in turn, and include it if it defines any
4490 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4491 this; it looks through the list of undefined symbols, and includes
4492 any object file which defines them. When this algorithm is used on
4493 UnixWare, it winds up pulling in libc.so.1 early and defining a
4494 bunch of symbols. This means that some of the other objects in the
4495 archive are not included in the link, which is incorrect since they
4496 precede libc.so.1 in the archive.
4498 Fortunately, ELF archive handling is simpler than that done by
4499 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4500 oddities. In ELF, if we find a symbol in the archive map, and the
4501 symbol is currently undefined, we know that we must pull in that
4502 object file.
4504 Unfortunately, we do have to make multiple passes over the symbol
4505 table until nothing further is resolved. */
4507 static bfd_boolean
4508 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4510 symindex c;
4511 bfd_boolean *defined = NULL;
4512 bfd_boolean *included = NULL;
4513 carsym *symdefs;
4514 bfd_boolean loop;
4515 bfd_size_type amt;
4516 const struct elf_backend_data *bed;
4517 struct elf_link_hash_entry * (*archive_symbol_lookup)
4518 (bfd *, struct bfd_link_info *, const char *);
4520 if (! bfd_has_map (abfd))
4522 /* An empty archive is a special case. */
4523 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4524 return TRUE;
4525 bfd_set_error (bfd_error_no_armap);
4526 return FALSE;
4529 /* Keep track of all symbols we know to be already defined, and all
4530 files we know to be already included. This is to speed up the
4531 second and subsequent passes. */
4532 c = bfd_ardata (abfd)->symdef_count;
4533 if (c == 0)
4534 return TRUE;
4535 amt = c;
4536 amt *= sizeof (bfd_boolean);
4537 defined = bfd_zmalloc (amt);
4538 included = bfd_zmalloc (amt);
4539 if (defined == NULL || included == NULL)
4540 goto error_return;
4542 symdefs = bfd_ardata (abfd)->symdefs;
4543 bed = get_elf_backend_data (abfd);
4544 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4548 file_ptr last;
4549 symindex i;
4550 carsym *symdef;
4551 carsym *symdefend;
4553 loop = FALSE;
4554 last = -1;
4556 symdef = symdefs;
4557 symdefend = symdef + c;
4558 for (i = 0; symdef < symdefend; symdef++, i++)
4560 struct elf_link_hash_entry *h;
4561 bfd *element;
4562 struct bfd_link_hash_entry *undefs_tail;
4563 symindex mark;
4565 if (defined[i] || included[i])
4566 continue;
4567 if (symdef->file_offset == last)
4569 included[i] = TRUE;
4570 continue;
4573 h = archive_symbol_lookup (abfd, info, symdef->name);
4574 if (h == (struct elf_link_hash_entry *) 0 - 1)
4575 goto error_return;
4577 if (h == NULL)
4578 continue;
4580 if (h->root.type == bfd_link_hash_common)
4582 /* We currently have a common symbol. The archive map contains
4583 a reference to this symbol, so we may want to include it. We
4584 only want to include it however, if this archive element
4585 contains a definition of the symbol, not just another common
4586 declaration of it.
4588 Unfortunately some archivers (including GNU ar) will put
4589 declarations of common symbols into their archive maps, as
4590 well as real definitions, so we cannot just go by the archive
4591 map alone. Instead we must read in the element's symbol
4592 table and check that to see what kind of symbol definition
4593 this is. */
4594 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4595 continue;
4597 else if (h->root.type != bfd_link_hash_undefined)
4599 if (h->root.type != bfd_link_hash_undefweak)
4600 defined[i] = TRUE;
4601 continue;
4604 /* We need to include this archive member. */
4605 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4606 if (element == NULL)
4607 goto error_return;
4609 if (! bfd_check_format (element, bfd_object))
4610 goto error_return;
4612 /* Doublecheck that we have not included this object
4613 already--it should be impossible, but there may be
4614 something wrong with the archive. */
4615 if (element->archive_pass != 0)
4617 bfd_set_error (bfd_error_bad_value);
4618 goto error_return;
4620 element->archive_pass = 1;
4622 undefs_tail = info->hash->undefs_tail;
4624 if (! (*info->callbacks->add_archive_element) (info, element,
4625 symdef->name))
4626 goto error_return;
4627 if (! bfd_link_add_symbols (element, info))
4628 goto error_return;
4630 /* If there are any new undefined symbols, we need to make
4631 another pass through the archive in order to see whether
4632 they can be defined. FIXME: This isn't perfect, because
4633 common symbols wind up on undefs_tail and because an
4634 undefined symbol which is defined later on in this pass
4635 does not require another pass. This isn't a bug, but it
4636 does make the code less efficient than it could be. */
4637 if (undefs_tail != info->hash->undefs_tail)
4638 loop = TRUE;
4640 /* Look backward to mark all symbols from this object file
4641 which we have already seen in this pass. */
4642 mark = i;
4645 included[mark] = TRUE;
4646 if (mark == 0)
4647 break;
4648 --mark;
4650 while (symdefs[mark].file_offset == symdef->file_offset);
4652 /* We mark subsequent symbols from this object file as we go
4653 on through the loop. */
4654 last = symdef->file_offset;
4657 while (loop);
4659 free (defined);
4660 free (included);
4662 return TRUE;
4664 error_return:
4665 if (defined != NULL)
4666 free (defined);
4667 if (included != NULL)
4668 free (included);
4669 return FALSE;
4672 /* Given an ELF BFD, add symbols to the global hash table as
4673 appropriate. */
4675 bfd_boolean
4676 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4678 switch (bfd_get_format (abfd))
4680 case bfd_object:
4681 return elf_link_add_object_symbols (abfd, info);
4682 case bfd_archive:
4683 return elf_link_add_archive_symbols (abfd, info);
4684 default:
4685 bfd_set_error (bfd_error_wrong_format);
4686 return FALSE;
4690 /* This function will be called though elf_link_hash_traverse to store
4691 all hash value of the exported symbols in an array. */
4693 static bfd_boolean
4694 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4696 unsigned long **valuep = data;
4697 const char *name;
4698 char *p;
4699 unsigned long ha;
4700 char *alc = NULL;
4702 if (h->root.type == bfd_link_hash_warning)
4703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4705 /* Ignore indirect symbols. These are added by the versioning code. */
4706 if (h->dynindx == -1)
4707 return TRUE;
4709 name = h->root.root.string;
4710 p = strchr (name, ELF_VER_CHR);
4711 if (p != NULL)
4713 alc = bfd_malloc (p - name + 1);
4714 memcpy (alc, name, p - name);
4715 alc[p - name] = '\0';
4716 name = alc;
4719 /* Compute the hash value. */
4720 ha = bfd_elf_hash (name);
4722 /* Store the found hash value in the array given as the argument. */
4723 *(*valuep)++ = ha;
4725 /* And store it in the struct so that we can put it in the hash table
4726 later. */
4727 h->u.elf_hash_value = ha;
4729 if (alc != NULL)
4730 free (alc);
4732 return TRUE;
4735 /* Array used to determine the number of hash table buckets to use
4736 based on the number of symbols there are. If there are fewer than
4737 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4738 fewer than 37 we use 17 buckets, and so forth. We never use more
4739 than 32771 buckets. */
4741 static const size_t elf_buckets[] =
4743 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4744 16411, 32771, 0
4747 /* Compute bucket count for hashing table. We do not use a static set
4748 of possible tables sizes anymore. Instead we determine for all
4749 possible reasonable sizes of the table the outcome (i.e., the
4750 number of collisions etc) and choose the best solution. The
4751 weighting functions are not too simple to allow the table to grow
4752 without bounds. Instead one of the weighting factors is the size.
4753 Therefore the result is always a good payoff between few collisions
4754 (= short chain lengths) and table size. */
4755 static size_t
4756 compute_bucket_count (struct bfd_link_info *info)
4758 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4759 size_t best_size = 0;
4760 unsigned long int *hashcodes;
4761 unsigned long int *hashcodesp;
4762 unsigned long int i;
4763 bfd_size_type amt;
4765 /* Compute the hash values for all exported symbols. At the same
4766 time store the values in an array so that we could use them for
4767 optimizations. */
4768 amt = dynsymcount;
4769 amt *= sizeof (unsigned long int);
4770 hashcodes = bfd_malloc (amt);
4771 if (hashcodes == NULL)
4772 return 0;
4773 hashcodesp = hashcodes;
4775 /* Put all hash values in HASHCODES. */
4776 elf_link_hash_traverse (elf_hash_table (info),
4777 elf_collect_hash_codes, &hashcodesp);
4779 /* We have a problem here. The following code to optimize the table
4780 size requires an integer type with more the 32 bits. If
4781 BFD_HOST_U_64_BIT is set we know about such a type. */
4782 #ifdef BFD_HOST_U_64_BIT
4783 if (info->optimize)
4785 unsigned long int nsyms = hashcodesp - hashcodes;
4786 size_t minsize;
4787 size_t maxsize;
4788 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4789 unsigned long int *counts ;
4790 bfd *dynobj = elf_hash_table (info)->dynobj;
4791 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4793 /* Possible optimization parameters: if we have NSYMS symbols we say
4794 that the hashing table must at least have NSYMS/4 and at most
4795 2*NSYMS buckets. */
4796 minsize = nsyms / 4;
4797 if (minsize == 0)
4798 minsize = 1;
4799 best_size = maxsize = nsyms * 2;
4801 /* Create array where we count the collisions in. We must use bfd_malloc
4802 since the size could be large. */
4803 amt = maxsize;
4804 amt *= sizeof (unsigned long int);
4805 counts = bfd_malloc (amt);
4806 if (counts == NULL)
4808 free (hashcodes);
4809 return 0;
4812 /* Compute the "optimal" size for the hash table. The criteria is a
4813 minimal chain length. The minor criteria is (of course) the size
4814 of the table. */
4815 for (i = minsize; i < maxsize; ++i)
4817 /* Walk through the array of hashcodes and count the collisions. */
4818 BFD_HOST_U_64_BIT max;
4819 unsigned long int j;
4820 unsigned long int fact;
4822 memset (counts, '\0', i * sizeof (unsigned long int));
4824 /* Determine how often each hash bucket is used. */
4825 for (j = 0; j < nsyms; ++j)
4826 ++counts[hashcodes[j] % i];
4828 /* For the weight function we need some information about the
4829 pagesize on the target. This is information need not be 100%
4830 accurate. Since this information is not available (so far) we
4831 define it here to a reasonable default value. If it is crucial
4832 to have a better value some day simply define this value. */
4833 # ifndef BFD_TARGET_PAGESIZE
4834 # define BFD_TARGET_PAGESIZE (4096)
4835 # endif
4837 /* We in any case need 2 + NSYMS entries for the size values and
4838 the chains. */
4839 max = (2 + nsyms) * (bed->s->arch_size / 8);
4841 # if 1
4842 /* Variant 1: optimize for short chains. We add the squares
4843 of all the chain lengths (which favors many small chain
4844 over a few long chains). */
4845 for (j = 0; j < i; ++j)
4846 max += counts[j] * counts[j];
4848 /* This adds penalties for the overall size of the table. */
4849 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4850 max *= fact * fact;
4851 # else
4852 /* Variant 2: Optimize a lot more for small table. Here we
4853 also add squares of the size but we also add penalties for
4854 empty slots (the +1 term). */
4855 for (j = 0; j < i; ++j)
4856 max += (1 + counts[j]) * (1 + counts[j]);
4858 /* The overall size of the table is considered, but not as
4859 strong as in variant 1, where it is squared. */
4860 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4861 max *= fact;
4862 # endif
4864 /* Compare with current best results. */
4865 if (max < best_chlen)
4867 best_chlen = max;
4868 best_size = i;
4872 free (counts);
4874 else
4875 #endif /* defined (BFD_HOST_U_64_BIT) */
4877 /* This is the fallback solution if no 64bit type is available or if we
4878 are not supposed to spend much time on optimizations. We select the
4879 bucket count using a fixed set of numbers. */
4880 for (i = 0; elf_buckets[i] != 0; i++)
4882 best_size = elf_buckets[i];
4883 if (dynsymcount < elf_buckets[i + 1])
4884 break;
4888 /* Free the arrays we needed. */
4889 free (hashcodes);
4891 return best_size;
4894 /* Set up the sizes and contents of the ELF dynamic sections. This is
4895 called by the ELF linker emulation before_allocation routine. We
4896 must set the sizes of the sections before the linker sets the
4897 addresses of the various sections. */
4899 bfd_boolean
4900 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4901 const char *soname,
4902 const char *rpath,
4903 const char *filter_shlib,
4904 const char * const *auxiliary_filters,
4905 struct bfd_link_info *info,
4906 asection **sinterpptr,
4907 struct bfd_elf_version_tree *verdefs)
4909 bfd_size_type soname_indx;
4910 bfd *dynobj;
4911 const struct elf_backend_data *bed;
4912 struct elf_assign_sym_version_info asvinfo;
4914 *sinterpptr = NULL;
4916 soname_indx = (bfd_size_type) -1;
4918 if (!is_elf_hash_table (info->hash))
4919 return TRUE;
4921 elf_tdata (output_bfd)->relro = info->relro;
4922 if (info->execstack)
4923 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4924 else if (info->noexecstack)
4925 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4926 else
4928 bfd *inputobj;
4929 asection *notesec = NULL;
4930 int exec = 0;
4932 for (inputobj = info->input_bfds;
4933 inputobj;
4934 inputobj = inputobj->link_next)
4936 asection *s;
4938 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4939 continue;
4940 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4941 if (s)
4943 if (s->flags & SEC_CODE)
4944 exec = PF_X;
4945 notesec = s;
4947 else
4948 exec = PF_X;
4950 if (notesec)
4952 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4953 if (exec && info->relocatable
4954 && notesec->output_section != bfd_abs_section_ptr)
4955 notesec->output_section->flags |= SEC_CODE;
4959 /* Any syms created from now on start with -1 in
4960 got.refcount/offset and plt.refcount/offset. */
4961 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4963 /* The backend may have to create some sections regardless of whether
4964 we're dynamic or not. */
4965 bed = get_elf_backend_data (output_bfd);
4966 if (bed->elf_backend_always_size_sections
4967 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4968 return FALSE;
4970 dynobj = elf_hash_table (info)->dynobj;
4972 /* If there were no dynamic objects in the link, there is nothing to
4973 do here. */
4974 if (dynobj == NULL)
4975 return TRUE;
4977 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4978 return FALSE;
4980 if (elf_hash_table (info)->dynamic_sections_created)
4982 struct elf_info_failed eif;
4983 struct elf_link_hash_entry *h;
4984 asection *dynstr;
4985 struct bfd_elf_version_tree *t;
4986 struct bfd_elf_version_expr *d;
4987 bfd_boolean all_defined;
4989 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4990 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4992 if (soname != NULL)
4994 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4995 soname, TRUE);
4996 if (soname_indx == (bfd_size_type) -1
4997 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4998 return FALSE;
5001 if (info->symbolic)
5003 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5004 return FALSE;
5005 info->flags |= DF_SYMBOLIC;
5008 if (rpath != NULL)
5010 bfd_size_type indx;
5012 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5013 TRUE);
5014 if (indx == (bfd_size_type) -1
5015 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5016 return FALSE;
5018 if (info->new_dtags)
5020 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5021 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5022 return FALSE;
5026 if (filter_shlib != NULL)
5028 bfd_size_type indx;
5030 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5031 filter_shlib, TRUE);
5032 if (indx == (bfd_size_type) -1
5033 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5034 return FALSE;
5037 if (auxiliary_filters != NULL)
5039 const char * const *p;
5041 for (p = auxiliary_filters; *p != NULL; p++)
5043 bfd_size_type indx;
5045 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5046 *p, TRUE);
5047 if (indx == (bfd_size_type) -1
5048 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5049 return FALSE;
5053 eif.info = info;
5054 eif.verdefs = verdefs;
5055 eif.failed = FALSE;
5057 /* If we are supposed to export all symbols into the dynamic symbol
5058 table (this is not the normal case), then do so. */
5059 if (info->export_dynamic)
5061 elf_link_hash_traverse (elf_hash_table (info),
5062 _bfd_elf_export_symbol,
5063 &eif);
5064 if (eif.failed)
5065 return FALSE;
5068 /* Make all global versions with definition. */
5069 for (t = verdefs; t != NULL; t = t->next)
5070 for (d = t->globals.list; d != NULL; d = d->next)
5071 if (!d->symver && d->symbol)
5073 const char *verstr, *name;
5074 size_t namelen, verlen, newlen;
5075 char *newname, *p;
5076 struct elf_link_hash_entry *newh;
5078 name = d->symbol;
5079 namelen = strlen (name);
5080 verstr = t->name;
5081 verlen = strlen (verstr);
5082 newlen = namelen + verlen + 3;
5084 newname = bfd_malloc (newlen);
5085 if (newname == NULL)
5086 return FALSE;
5087 memcpy (newname, name, namelen);
5089 /* Check the hidden versioned definition. */
5090 p = newname + namelen;
5091 *p++ = ELF_VER_CHR;
5092 memcpy (p, verstr, verlen + 1);
5093 newh = elf_link_hash_lookup (elf_hash_table (info),
5094 newname, FALSE, FALSE,
5095 FALSE);
5096 if (newh == NULL
5097 || (newh->root.type != bfd_link_hash_defined
5098 && newh->root.type != bfd_link_hash_defweak))
5100 /* Check the default versioned definition. */
5101 *p++ = ELF_VER_CHR;
5102 memcpy (p, verstr, verlen + 1);
5103 newh = elf_link_hash_lookup (elf_hash_table (info),
5104 newname, FALSE, FALSE,
5105 FALSE);
5107 free (newname);
5109 /* Mark this version if there is a definition and it is
5110 not defined in a shared object. */
5111 if (newh != NULL
5112 && !newh->def_dynamic
5113 && (newh->root.type == bfd_link_hash_defined
5114 || newh->root.type == bfd_link_hash_defweak))
5115 d->symver = 1;
5118 /* Attach all the symbols to their version information. */
5119 asvinfo.output_bfd = output_bfd;
5120 asvinfo.info = info;
5121 asvinfo.verdefs = verdefs;
5122 asvinfo.failed = FALSE;
5124 elf_link_hash_traverse (elf_hash_table (info),
5125 _bfd_elf_link_assign_sym_version,
5126 &asvinfo);
5127 if (asvinfo.failed)
5128 return FALSE;
5130 if (!info->allow_undefined_version)
5132 /* Check if all global versions have a definition. */
5133 all_defined = TRUE;
5134 for (t = verdefs; t != NULL; t = t->next)
5135 for (d = t->globals.list; d != NULL; d = d->next)
5136 if (!d->symver && !d->script)
5138 (*_bfd_error_handler)
5139 (_("%s: undefined version: %s"),
5140 d->pattern, t->name);
5141 all_defined = FALSE;
5144 if (!all_defined)
5146 bfd_set_error (bfd_error_bad_value);
5147 return FALSE;
5151 /* Find all symbols which were defined in a dynamic object and make
5152 the backend pick a reasonable value for them. */
5153 elf_link_hash_traverse (elf_hash_table (info),
5154 _bfd_elf_adjust_dynamic_symbol,
5155 &eif);
5156 if (eif.failed)
5157 return FALSE;
5159 /* Add some entries to the .dynamic section. We fill in some of the
5160 values later, in bfd_elf_final_link, but we must add the entries
5161 now so that we know the final size of the .dynamic section. */
5163 /* If there are initialization and/or finalization functions to
5164 call then add the corresponding DT_INIT/DT_FINI entries. */
5165 h = (info->init_function
5166 ? elf_link_hash_lookup (elf_hash_table (info),
5167 info->init_function, FALSE,
5168 FALSE, FALSE)
5169 : NULL);
5170 if (h != NULL
5171 && (h->ref_regular
5172 || h->def_regular))
5174 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5175 return FALSE;
5177 h = (info->fini_function
5178 ? elf_link_hash_lookup (elf_hash_table (info),
5179 info->fini_function, FALSE,
5180 FALSE, FALSE)
5181 : NULL);
5182 if (h != NULL
5183 && (h->ref_regular
5184 || h->def_regular))
5186 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5187 return FALSE;
5190 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5192 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5193 if (! info->executable)
5195 bfd *sub;
5196 asection *o;
5198 for (sub = info->input_bfds; sub != NULL;
5199 sub = sub->link_next)
5200 for (o = sub->sections; o != NULL; o = o->next)
5201 if (elf_section_data (o)->this_hdr.sh_type
5202 == SHT_PREINIT_ARRAY)
5204 (*_bfd_error_handler)
5205 (_("%B: .preinit_array section is not allowed in DSO"),
5206 sub);
5207 break;
5210 bfd_set_error (bfd_error_nonrepresentable_section);
5211 return FALSE;
5214 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5215 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5216 return FALSE;
5218 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5220 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5221 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5222 return FALSE;
5224 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5226 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5227 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5228 return FALSE;
5231 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5232 /* If .dynstr is excluded from the link, we don't want any of
5233 these tags. Strictly, we should be checking each section
5234 individually; This quick check covers for the case where
5235 someone does a /DISCARD/ : { *(*) }. */
5236 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5238 bfd_size_type strsize;
5240 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5241 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5242 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5243 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5244 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5245 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5246 bed->s->sizeof_sym))
5247 return FALSE;
5251 /* The backend must work out the sizes of all the other dynamic
5252 sections. */
5253 if (bed->elf_backend_size_dynamic_sections
5254 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5255 return FALSE;
5257 if (elf_hash_table (info)->dynamic_sections_created)
5259 bfd_size_type dynsymcount;
5260 asection *s;
5261 size_t bucketcount = 0;
5262 size_t hash_entry_size;
5263 unsigned int dtagcount;
5265 /* Set up the version definition section. */
5266 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5267 BFD_ASSERT (s != NULL);
5269 /* We may have created additional version definitions if we are
5270 just linking a regular application. */
5271 verdefs = asvinfo.verdefs;
5273 /* Skip anonymous version tag. */
5274 if (verdefs != NULL && verdefs->vernum == 0)
5275 verdefs = verdefs->next;
5277 if (verdefs == NULL && !info->create_default_symver)
5278 _bfd_strip_section_from_output (info, s);
5279 else
5281 unsigned int cdefs;
5282 bfd_size_type size;
5283 struct bfd_elf_version_tree *t;
5284 bfd_byte *p;
5285 Elf_Internal_Verdef def;
5286 Elf_Internal_Verdaux defaux;
5287 struct bfd_link_hash_entry *bh;
5288 struct elf_link_hash_entry *h;
5289 const char *name;
5291 cdefs = 0;
5292 size = 0;
5294 /* Make space for the base version. */
5295 size += sizeof (Elf_External_Verdef);
5296 size += sizeof (Elf_External_Verdaux);
5297 ++cdefs;
5299 /* Make space for the default version. */
5300 if (info->create_default_symver)
5302 size += sizeof (Elf_External_Verdef);
5303 ++cdefs;
5306 for (t = verdefs; t != NULL; t = t->next)
5308 struct bfd_elf_version_deps *n;
5310 size += sizeof (Elf_External_Verdef);
5311 size += sizeof (Elf_External_Verdaux);
5312 ++cdefs;
5314 for (n = t->deps; n != NULL; n = n->next)
5315 size += sizeof (Elf_External_Verdaux);
5318 s->size = size;
5319 s->contents = bfd_alloc (output_bfd, s->size);
5320 if (s->contents == NULL && s->size != 0)
5321 return FALSE;
5323 /* Fill in the version definition section. */
5325 p = s->contents;
5327 def.vd_version = VER_DEF_CURRENT;
5328 def.vd_flags = VER_FLG_BASE;
5329 def.vd_ndx = 1;
5330 def.vd_cnt = 1;
5331 if (info->create_default_symver)
5333 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5334 def.vd_next = sizeof (Elf_External_Verdef);
5336 else
5338 def.vd_aux = sizeof (Elf_External_Verdef);
5339 def.vd_next = (sizeof (Elf_External_Verdef)
5340 + sizeof (Elf_External_Verdaux));
5343 if (soname_indx != (bfd_size_type) -1)
5345 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5346 soname_indx);
5347 def.vd_hash = bfd_elf_hash (soname);
5348 defaux.vda_name = soname_indx;
5349 name = soname;
5351 else
5353 bfd_size_type indx;
5355 name = basename (output_bfd->filename);
5356 def.vd_hash = bfd_elf_hash (name);
5357 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5358 name, FALSE);
5359 if (indx == (bfd_size_type) -1)
5360 return FALSE;
5361 defaux.vda_name = indx;
5363 defaux.vda_next = 0;
5365 _bfd_elf_swap_verdef_out (output_bfd, &def,
5366 (Elf_External_Verdef *) p);
5367 p += sizeof (Elf_External_Verdef);
5368 if (info->create_default_symver)
5370 /* Add a symbol representing this version. */
5371 bh = NULL;
5372 if (! (_bfd_generic_link_add_one_symbol
5373 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5374 0, NULL, FALSE,
5375 get_elf_backend_data (dynobj)->collect, &bh)))
5376 return FALSE;
5377 h = (struct elf_link_hash_entry *) bh;
5378 h->non_elf = 0;
5379 h->def_regular = 1;
5380 h->type = STT_OBJECT;
5381 h->verinfo.vertree = NULL;
5383 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5384 return FALSE;
5386 /* Create a duplicate of the base version with the same
5387 aux block, but different flags. */
5388 def.vd_flags = 0;
5389 def.vd_ndx = 2;
5390 def.vd_aux = sizeof (Elf_External_Verdef);
5391 if (verdefs)
5392 def.vd_next = (sizeof (Elf_External_Verdef)
5393 + sizeof (Elf_External_Verdaux));
5394 else
5395 def.vd_next = 0;
5396 _bfd_elf_swap_verdef_out (output_bfd, &def,
5397 (Elf_External_Verdef *) p);
5398 p += sizeof (Elf_External_Verdef);
5400 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5401 (Elf_External_Verdaux *) p);
5402 p += sizeof (Elf_External_Verdaux);
5404 for (t = verdefs; t != NULL; t = t->next)
5406 unsigned int cdeps;
5407 struct bfd_elf_version_deps *n;
5409 cdeps = 0;
5410 for (n = t->deps; n != NULL; n = n->next)
5411 ++cdeps;
5413 /* Add a symbol representing this version. */
5414 bh = NULL;
5415 if (! (_bfd_generic_link_add_one_symbol
5416 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5417 0, NULL, FALSE,
5418 get_elf_backend_data (dynobj)->collect, &bh)))
5419 return FALSE;
5420 h = (struct elf_link_hash_entry *) bh;
5421 h->non_elf = 0;
5422 h->def_regular = 1;
5423 h->type = STT_OBJECT;
5424 h->verinfo.vertree = t;
5426 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5427 return FALSE;
5429 def.vd_version = VER_DEF_CURRENT;
5430 def.vd_flags = 0;
5431 if (t->globals.list == NULL
5432 && t->locals.list == NULL
5433 && ! t->used)
5434 def.vd_flags |= VER_FLG_WEAK;
5435 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5436 def.vd_cnt = cdeps + 1;
5437 def.vd_hash = bfd_elf_hash (t->name);
5438 def.vd_aux = sizeof (Elf_External_Verdef);
5439 def.vd_next = 0;
5440 if (t->next != NULL)
5441 def.vd_next = (sizeof (Elf_External_Verdef)
5442 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5444 _bfd_elf_swap_verdef_out (output_bfd, &def,
5445 (Elf_External_Verdef *) p);
5446 p += sizeof (Elf_External_Verdef);
5448 defaux.vda_name = h->dynstr_index;
5449 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5450 h->dynstr_index);
5451 defaux.vda_next = 0;
5452 if (t->deps != NULL)
5453 defaux.vda_next = sizeof (Elf_External_Verdaux);
5454 t->name_indx = defaux.vda_name;
5456 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5457 (Elf_External_Verdaux *) p);
5458 p += sizeof (Elf_External_Verdaux);
5460 for (n = t->deps; n != NULL; n = n->next)
5462 if (n->version_needed == NULL)
5464 /* This can happen if there was an error in the
5465 version script. */
5466 defaux.vda_name = 0;
5468 else
5470 defaux.vda_name = n->version_needed->name_indx;
5471 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5472 defaux.vda_name);
5474 if (n->next == NULL)
5475 defaux.vda_next = 0;
5476 else
5477 defaux.vda_next = sizeof (Elf_External_Verdaux);
5479 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5480 (Elf_External_Verdaux *) p);
5481 p += sizeof (Elf_External_Verdaux);
5485 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5486 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5487 return FALSE;
5489 elf_tdata (output_bfd)->cverdefs = cdefs;
5492 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5494 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5495 return FALSE;
5497 else if (info->flags & DF_BIND_NOW)
5499 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5500 return FALSE;
5503 if (info->flags_1)
5505 if (info->executable)
5506 info->flags_1 &= ~ (DF_1_INITFIRST
5507 | DF_1_NODELETE
5508 | DF_1_NOOPEN);
5509 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5510 return FALSE;
5513 /* Work out the size of the version reference section. */
5515 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5516 BFD_ASSERT (s != NULL);
5518 struct elf_find_verdep_info sinfo;
5520 sinfo.output_bfd = output_bfd;
5521 sinfo.info = info;
5522 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5523 if (sinfo.vers == 0)
5524 sinfo.vers = 1;
5525 sinfo.failed = FALSE;
5527 elf_link_hash_traverse (elf_hash_table (info),
5528 _bfd_elf_link_find_version_dependencies,
5529 &sinfo);
5531 if (elf_tdata (output_bfd)->verref == NULL)
5532 _bfd_strip_section_from_output (info, s);
5533 else
5535 Elf_Internal_Verneed *t;
5536 unsigned int size;
5537 unsigned int crefs;
5538 bfd_byte *p;
5540 /* Build the version definition section. */
5541 size = 0;
5542 crefs = 0;
5543 for (t = elf_tdata (output_bfd)->verref;
5544 t != NULL;
5545 t = t->vn_nextref)
5547 Elf_Internal_Vernaux *a;
5549 size += sizeof (Elf_External_Verneed);
5550 ++crefs;
5551 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5552 size += sizeof (Elf_External_Vernaux);
5555 s->size = size;
5556 s->contents = bfd_alloc (output_bfd, s->size);
5557 if (s->contents == NULL)
5558 return FALSE;
5560 p = s->contents;
5561 for (t = elf_tdata (output_bfd)->verref;
5562 t != NULL;
5563 t = t->vn_nextref)
5565 unsigned int caux;
5566 Elf_Internal_Vernaux *a;
5567 bfd_size_type indx;
5569 caux = 0;
5570 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5571 ++caux;
5573 t->vn_version = VER_NEED_CURRENT;
5574 t->vn_cnt = caux;
5575 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5576 elf_dt_name (t->vn_bfd) != NULL
5577 ? elf_dt_name (t->vn_bfd)
5578 : basename (t->vn_bfd->filename),
5579 FALSE);
5580 if (indx == (bfd_size_type) -1)
5581 return FALSE;
5582 t->vn_file = indx;
5583 t->vn_aux = sizeof (Elf_External_Verneed);
5584 if (t->vn_nextref == NULL)
5585 t->vn_next = 0;
5586 else
5587 t->vn_next = (sizeof (Elf_External_Verneed)
5588 + caux * sizeof (Elf_External_Vernaux));
5590 _bfd_elf_swap_verneed_out (output_bfd, t,
5591 (Elf_External_Verneed *) p);
5592 p += sizeof (Elf_External_Verneed);
5594 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5596 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5597 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5598 a->vna_nodename, FALSE);
5599 if (indx == (bfd_size_type) -1)
5600 return FALSE;
5601 a->vna_name = indx;
5602 if (a->vna_nextptr == NULL)
5603 a->vna_next = 0;
5604 else
5605 a->vna_next = sizeof (Elf_External_Vernaux);
5607 _bfd_elf_swap_vernaux_out (output_bfd, a,
5608 (Elf_External_Vernaux *) p);
5609 p += sizeof (Elf_External_Vernaux);
5613 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5614 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5615 return FALSE;
5617 elf_tdata (output_bfd)->cverrefs = crefs;
5621 /* Assign dynsym indicies. In a shared library we generate a
5622 section symbol for each output section, which come first.
5623 Next come all of the back-end allocated local dynamic syms,
5624 followed by the rest of the global symbols. */
5626 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5628 /* Work out the size of the symbol version section. */
5629 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5630 BFD_ASSERT (s != NULL);
5631 if (dynsymcount == 0
5632 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5633 && !info->create_default_symver))
5635 _bfd_strip_section_from_output (info, s);
5636 /* The DYNSYMCOUNT might have changed if we were going to
5637 output a dynamic symbol table entry for S. */
5638 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5640 else
5642 s->size = dynsymcount * sizeof (Elf_External_Versym);
5643 s->contents = bfd_zalloc (output_bfd, s->size);
5644 if (s->contents == NULL)
5645 return FALSE;
5647 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5648 return FALSE;
5651 /* Set the size of the .dynsym and .hash sections. We counted
5652 the number of dynamic symbols in elf_link_add_object_symbols.
5653 We will build the contents of .dynsym and .hash when we build
5654 the final symbol table, because until then we do not know the
5655 correct value to give the symbols. We built the .dynstr
5656 section as we went along in elf_link_add_object_symbols. */
5657 s = bfd_get_section_by_name (dynobj, ".dynsym");
5658 BFD_ASSERT (s != NULL);
5659 s->size = dynsymcount * bed->s->sizeof_sym;
5660 s->contents = bfd_alloc (output_bfd, s->size);
5661 if (s->contents == NULL && s->size != 0)
5662 return FALSE;
5664 if (dynsymcount != 0)
5666 Elf_Internal_Sym isym;
5668 /* The first entry in .dynsym is a dummy symbol. */
5669 isym.st_value = 0;
5670 isym.st_size = 0;
5671 isym.st_name = 0;
5672 isym.st_info = 0;
5673 isym.st_other = 0;
5674 isym.st_shndx = 0;
5675 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5678 /* Compute the size of the hashing table. As a side effect this
5679 computes the hash values for all the names we export. */
5680 bucketcount = compute_bucket_count (info);
5682 s = bfd_get_section_by_name (dynobj, ".hash");
5683 BFD_ASSERT (s != NULL);
5684 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5685 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5686 s->contents = bfd_zalloc (output_bfd, s->size);
5687 if (s->contents == NULL)
5688 return FALSE;
5690 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5691 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5692 s->contents + hash_entry_size);
5694 elf_hash_table (info)->bucketcount = bucketcount;
5696 s = bfd_get_section_by_name (dynobj, ".dynstr");
5697 BFD_ASSERT (s != NULL);
5699 elf_finalize_dynstr (output_bfd, info);
5701 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5703 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5704 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5705 return FALSE;
5708 return TRUE;
5711 /* Final phase of ELF linker. */
5713 /* A structure we use to avoid passing large numbers of arguments. */
5715 struct elf_final_link_info
5717 /* General link information. */
5718 struct bfd_link_info *info;
5719 /* Output BFD. */
5720 bfd *output_bfd;
5721 /* Symbol string table. */
5722 struct bfd_strtab_hash *symstrtab;
5723 /* .dynsym section. */
5724 asection *dynsym_sec;
5725 /* .hash section. */
5726 asection *hash_sec;
5727 /* symbol version section (.gnu.version). */
5728 asection *symver_sec;
5729 /* Buffer large enough to hold contents of any section. */
5730 bfd_byte *contents;
5731 /* Buffer large enough to hold external relocs of any section. */
5732 void *external_relocs;
5733 /* Buffer large enough to hold internal relocs of any section. */
5734 Elf_Internal_Rela *internal_relocs;
5735 /* Buffer large enough to hold external local symbols of any input
5736 BFD. */
5737 bfd_byte *external_syms;
5738 /* And a buffer for symbol section indices. */
5739 Elf_External_Sym_Shndx *locsym_shndx;
5740 /* Buffer large enough to hold internal local symbols of any input
5741 BFD. */
5742 Elf_Internal_Sym *internal_syms;
5743 /* Array large enough to hold a symbol index for each local symbol
5744 of any input BFD. */
5745 long *indices;
5746 /* Array large enough to hold a section pointer for each local
5747 symbol of any input BFD. */
5748 asection **sections;
5749 /* Buffer to hold swapped out symbols. */
5750 bfd_byte *symbuf;
5751 /* And one for symbol section indices. */
5752 Elf_External_Sym_Shndx *symshndxbuf;
5753 /* Number of swapped out symbols in buffer. */
5754 size_t symbuf_count;
5755 /* Number of symbols which fit in symbuf. */
5756 size_t symbuf_size;
5757 /* And same for symshndxbuf. */
5758 size_t shndxbuf_size;
5761 /* This struct is used to pass information to elf_link_output_extsym. */
5763 struct elf_outext_info
5765 bfd_boolean failed;
5766 bfd_boolean localsyms;
5767 struct elf_final_link_info *finfo;
5770 /* When performing a relocatable link, the input relocations are
5771 preserved. But, if they reference global symbols, the indices
5772 referenced must be updated. Update all the relocations in
5773 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5775 static void
5776 elf_link_adjust_relocs (bfd *abfd,
5777 Elf_Internal_Shdr *rel_hdr,
5778 unsigned int count,
5779 struct elf_link_hash_entry **rel_hash)
5781 unsigned int i;
5782 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5783 bfd_byte *erela;
5784 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5785 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5786 bfd_vma r_type_mask;
5787 int r_sym_shift;
5789 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5791 swap_in = bed->s->swap_reloc_in;
5792 swap_out = bed->s->swap_reloc_out;
5794 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5796 swap_in = bed->s->swap_reloca_in;
5797 swap_out = bed->s->swap_reloca_out;
5799 else
5800 abort ();
5802 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5803 abort ();
5805 if (bed->s->arch_size == 32)
5807 r_type_mask = 0xff;
5808 r_sym_shift = 8;
5810 else
5812 r_type_mask = 0xffffffff;
5813 r_sym_shift = 32;
5816 erela = rel_hdr->contents;
5817 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5819 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5820 unsigned int j;
5822 if (*rel_hash == NULL)
5823 continue;
5825 BFD_ASSERT ((*rel_hash)->indx >= 0);
5827 (*swap_in) (abfd, erela, irela);
5828 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5829 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5830 | (irela[j].r_info & r_type_mask));
5831 (*swap_out) (abfd, irela, erela);
5835 struct elf_link_sort_rela
5837 union {
5838 bfd_vma offset;
5839 bfd_vma sym_mask;
5840 } u;
5841 enum elf_reloc_type_class type;
5842 /* We use this as an array of size int_rels_per_ext_rel. */
5843 Elf_Internal_Rela rela[1];
5846 static int
5847 elf_link_sort_cmp1 (const void *A, const void *B)
5849 const struct elf_link_sort_rela *a = A;
5850 const struct elf_link_sort_rela *b = B;
5851 int relativea, relativeb;
5853 relativea = a->type == reloc_class_relative;
5854 relativeb = b->type == reloc_class_relative;
5856 if (relativea < relativeb)
5857 return 1;
5858 if (relativea > relativeb)
5859 return -1;
5860 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5861 return -1;
5862 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5863 return 1;
5864 if (a->rela->r_offset < b->rela->r_offset)
5865 return -1;
5866 if (a->rela->r_offset > b->rela->r_offset)
5867 return 1;
5868 return 0;
5871 static int
5872 elf_link_sort_cmp2 (const void *A, const void *B)
5874 const struct elf_link_sort_rela *a = A;
5875 const struct elf_link_sort_rela *b = B;
5876 int copya, copyb;
5878 if (a->u.offset < b->u.offset)
5879 return -1;
5880 if (a->u.offset > b->u.offset)
5881 return 1;
5882 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5883 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5884 if (copya < copyb)
5885 return -1;
5886 if (copya > copyb)
5887 return 1;
5888 if (a->rela->r_offset < b->rela->r_offset)
5889 return -1;
5890 if (a->rela->r_offset > b->rela->r_offset)
5891 return 1;
5892 return 0;
5895 static size_t
5896 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5898 asection *reldyn;
5899 bfd_size_type count, size;
5900 size_t i, ret, sort_elt, ext_size;
5901 bfd_byte *sort, *s_non_relative, *p;
5902 struct elf_link_sort_rela *sq;
5903 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5904 int i2e = bed->s->int_rels_per_ext_rel;
5905 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5906 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5907 struct bfd_link_order *lo;
5908 bfd_vma r_sym_mask;
5910 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5911 if (reldyn == NULL || reldyn->size == 0)
5913 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5914 if (reldyn == NULL || reldyn->size == 0)
5915 return 0;
5916 ext_size = bed->s->sizeof_rel;
5917 swap_in = bed->s->swap_reloc_in;
5918 swap_out = bed->s->swap_reloc_out;
5920 else
5922 ext_size = bed->s->sizeof_rela;
5923 swap_in = bed->s->swap_reloca_in;
5924 swap_out = bed->s->swap_reloca_out;
5926 count = reldyn->size / ext_size;
5928 size = 0;
5929 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5930 if (lo->type == bfd_indirect_link_order)
5932 asection *o = lo->u.indirect.section;
5933 size += o->size;
5936 if (size != reldyn->size)
5937 return 0;
5939 sort_elt = (sizeof (struct elf_link_sort_rela)
5940 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5941 sort = bfd_zmalloc (sort_elt * count);
5942 if (sort == NULL)
5944 (*info->callbacks->warning)
5945 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5946 return 0;
5949 if (bed->s->arch_size == 32)
5950 r_sym_mask = ~(bfd_vma) 0xff;
5951 else
5952 r_sym_mask = ~(bfd_vma) 0xffffffff;
5954 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5955 if (lo->type == bfd_indirect_link_order)
5957 bfd_byte *erel, *erelend;
5958 asection *o = lo->u.indirect.section;
5960 if (o->contents == NULL && o->size != 0)
5962 /* This is a reloc section that is being handled as a normal
5963 section. See bfd_section_from_shdr. We can't combine
5964 relocs in this case. */
5965 free (sort);
5966 return 0;
5968 erel = o->contents;
5969 erelend = o->contents + o->size;
5970 p = sort + o->output_offset / ext_size * sort_elt;
5971 while (erel < erelend)
5973 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5974 (*swap_in) (abfd, erel, s->rela);
5975 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5976 s->u.sym_mask = r_sym_mask;
5977 p += sort_elt;
5978 erel += ext_size;
5982 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5984 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5986 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5987 if (s->type != reloc_class_relative)
5988 break;
5990 ret = i;
5991 s_non_relative = p;
5993 sq = (struct elf_link_sort_rela *) s_non_relative;
5994 for (; i < count; i++, p += sort_elt)
5996 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5997 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5998 sq = sp;
5999 sp->u.offset = sq->rela->r_offset;
6002 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6004 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
6005 if (lo->type == bfd_indirect_link_order)
6007 bfd_byte *erel, *erelend;
6008 asection *o = lo->u.indirect.section;
6010 erel = o->contents;
6011 erelend = o->contents + o->size;
6012 p = sort + o->output_offset / ext_size * sort_elt;
6013 while (erel < erelend)
6015 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6016 (*swap_out) (abfd, s->rela, erel);
6017 p += sort_elt;
6018 erel += ext_size;
6022 free (sort);
6023 *psec = reldyn;
6024 return ret;
6027 /* Flush the output symbols to the file. */
6029 static bfd_boolean
6030 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6031 const struct elf_backend_data *bed)
6033 if (finfo->symbuf_count > 0)
6035 Elf_Internal_Shdr *hdr;
6036 file_ptr pos;
6037 bfd_size_type amt;
6039 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6040 pos = hdr->sh_offset + hdr->sh_size;
6041 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6042 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6043 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6044 return FALSE;
6046 hdr->sh_size += amt;
6047 finfo->symbuf_count = 0;
6050 return TRUE;
6053 /* Add a symbol to the output symbol table. */
6055 static bfd_boolean
6056 elf_link_output_sym (struct elf_final_link_info *finfo,
6057 const char *name,
6058 Elf_Internal_Sym *elfsym,
6059 asection *input_sec,
6060 struct elf_link_hash_entry *h)
6062 bfd_byte *dest;
6063 Elf_External_Sym_Shndx *destshndx;
6064 bfd_boolean (*output_symbol_hook)
6065 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6066 struct elf_link_hash_entry *);
6067 const struct elf_backend_data *bed;
6069 bed = get_elf_backend_data (finfo->output_bfd);
6070 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6071 if (output_symbol_hook != NULL)
6073 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6074 return FALSE;
6077 if (name == NULL || *name == '\0')
6078 elfsym->st_name = 0;
6079 else if (input_sec->flags & SEC_EXCLUDE)
6080 elfsym->st_name = 0;
6081 else
6083 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6084 name, TRUE, FALSE);
6085 if (elfsym->st_name == (unsigned long) -1)
6086 return FALSE;
6089 if (finfo->symbuf_count >= finfo->symbuf_size)
6091 if (! elf_link_flush_output_syms (finfo, bed))
6092 return FALSE;
6095 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6096 destshndx = finfo->symshndxbuf;
6097 if (destshndx != NULL)
6099 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6101 bfd_size_type amt;
6103 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6104 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6105 if (destshndx == NULL)
6106 return FALSE;
6107 memset ((char *) destshndx + amt, 0, amt);
6108 finfo->shndxbuf_size *= 2;
6110 destshndx += bfd_get_symcount (finfo->output_bfd);
6113 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6114 finfo->symbuf_count += 1;
6115 bfd_get_symcount (finfo->output_bfd) += 1;
6117 return TRUE;
6120 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6121 allowing an unsatisfied unversioned symbol in the DSO to match a
6122 versioned symbol that would normally require an explicit version.
6123 We also handle the case that a DSO references a hidden symbol
6124 which may be satisfied by a versioned symbol in another DSO. */
6126 static bfd_boolean
6127 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6128 const struct elf_backend_data *bed,
6129 struct elf_link_hash_entry *h)
6131 bfd *abfd;
6132 struct elf_link_loaded_list *loaded;
6134 if (!is_elf_hash_table (info->hash))
6135 return FALSE;
6137 switch (h->root.type)
6139 default:
6140 abfd = NULL;
6141 break;
6143 case bfd_link_hash_undefined:
6144 case bfd_link_hash_undefweak:
6145 abfd = h->root.u.undef.abfd;
6146 if ((abfd->flags & DYNAMIC) == 0
6147 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6148 return FALSE;
6149 break;
6151 case bfd_link_hash_defined:
6152 case bfd_link_hash_defweak:
6153 abfd = h->root.u.def.section->owner;
6154 break;
6156 case bfd_link_hash_common:
6157 abfd = h->root.u.c.p->section->owner;
6158 break;
6160 BFD_ASSERT (abfd != NULL);
6162 for (loaded = elf_hash_table (info)->loaded;
6163 loaded != NULL;
6164 loaded = loaded->next)
6166 bfd *input;
6167 Elf_Internal_Shdr *hdr;
6168 bfd_size_type symcount;
6169 bfd_size_type extsymcount;
6170 bfd_size_type extsymoff;
6171 Elf_Internal_Shdr *versymhdr;
6172 Elf_Internal_Sym *isym;
6173 Elf_Internal_Sym *isymend;
6174 Elf_Internal_Sym *isymbuf;
6175 Elf_External_Versym *ever;
6176 Elf_External_Versym *extversym;
6178 input = loaded->abfd;
6180 /* We check each DSO for a possible hidden versioned definition. */
6181 if (input == abfd
6182 || (input->flags & DYNAMIC) == 0
6183 || elf_dynversym (input) == 0)
6184 continue;
6186 hdr = &elf_tdata (input)->dynsymtab_hdr;
6188 symcount = hdr->sh_size / bed->s->sizeof_sym;
6189 if (elf_bad_symtab (input))
6191 extsymcount = symcount;
6192 extsymoff = 0;
6194 else
6196 extsymcount = symcount - hdr->sh_info;
6197 extsymoff = hdr->sh_info;
6200 if (extsymcount == 0)
6201 continue;
6203 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6204 NULL, NULL, NULL);
6205 if (isymbuf == NULL)
6206 return FALSE;
6208 /* Read in any version definitions. */
6209 versymhdr = &elf_tdata (input)->dynversym_hdr;
6210 extversym = bfd_malloc (versymhdr->sh_size);
6211 if (extversym == NULL)
6212 goto error_ret;
6214 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6215 || (bfd_bread (extversym, versymhdr->sh_size, input)
6216 != versymhdr->sh_size))
6218 free (extversym);
6219 error_ret:
6220 free (isymbuf);
6221 return FALSE;
6224 ever = extversym + extsymoff;
6225 isymend = isymbuf + extsymcount;
6226 for (isym = isymbuf; isym < isymend; isym++, ever++)
6228 const char *name;
6229 Elf_Internal_Versym iver;
6230 unsigned short version_index;
6232 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6233 || isym->st_shndx == SHN_UNDEF)
6234 continue;
6236 name = bfd_elf_string_from_elf_section (input,
6237 hdr->sh_link,
6238 isym->st_name);
6239 if (strcmp (name, h->root.root.string) != 0)
6240 continue;
6242 _bfd_elf_swap_versym_in (input, ever, &iver);
6244 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6246 /* If we have a non-hidden versioned sym, then it should
6247 have provided a definition for the undefined sym. */
6248 abort ();
6251 version_index = iver.vs_vers & VERSYM_VERSION;
6252 if (version_index == 1 || version_index == 2)
6254 /* This is the base or first version. We can use it. */
6255 free (extversym);
6256 free (isymbuf);
6257 return TRUE;
6261 free (extversym);
6262 free (isymbuf);
6265 return FALSE;
6268 /* Add an external symbol to the symbol table. This is called from
6269 the hash table traversal routine. When generating a shared object,
6270 we go through the symbol table twice. The first time we output
6271 anything that might have been forced to local scope in a version
6272 script. The second time we output the symbols that are still
6273 global symbols. */
6275 static bfd_boolean
6276 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6278 struct elf_outext_info *eoinfo = data;
6279 struct elf_final_link_info *finfo = eoinfo->finfo;
6280 bfd_boolean strip;
6281 Elf_Internal_Sym sym;
6282 asection *input_sec;
6283 const struct elf_backend_data *bed;
6285 if (h->root.type == bfd_link_hash_warning)
6287 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6288 if (h->root.type == bfd_link_hash_new)
6289 return TRUE;
6292 /* Decide whether to output this symbol in this pass. */
6293 if (eoinfo->localsyms)
6295 if (!h->forced_local)
6296 return TRUE;
6298 else
6300 if (h->forced_local)
6301 return TRUE;
6304 bed = get_elf_backend_data (finfo->output_bfd);
6306 /* If we have an undefined symbol reference here then it must have
6307 come from a shared library that is being linked in. (Undefined
6308 references in regular files have already been handled). If we
6309 are reporting errors for this situation then do so now. */
6310 if (h->root.type == bfd_link_hash_undefined
6311 && h->ref_dynamic
6312 && !h->ref_regular
6313 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6314 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6316 if (! ((*finfo->info->callbacks->undefined_symbol)
6317 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6318 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6320 eoinfo->failed = TRUE;
6321 return FALSE;
6325 /* We should also warn if a forced local symbol is referenced from
6326 shared libraries. */
6327 if (! finfo->info->relocatable
6328 && (! finfo->info->shared)
6329 && h->forced_local
6330 && h->ref_dynamic
6331 && !h->dynamic_def
6332 && !h->dynamic_weak
6333 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6335 (*_bfd_error_handler)
6336 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6337 finfo->output_bfd, h->root.u.def.section->owner,
6338 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6339 ? "internal"
6340 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6341 ? "hidden" : "local",
6342 h->root.root.string);
6343 eoinfo->failed = TRUE;
6344 return FALSE;
6347 /* We don't want to output symbols that have never been mentioned by
6348 a regular file, or that we have been told to strip. However, if
6349 h->indx is set to -2, the symbol is used by a reloc and we must
6350 output it. */
6351 if (h->indx == -2)
6352 strip = FALSE;
6353 else if ((h->def_dynamic
6354 || h->ref_dynamic
6355 || h->root.type == bfd_link_hash_new)
6356 && !h->def_regular
6357 && !h->ref_regular)
6358 strip = TRUE;
6359 else if (finfo->info->strip == strip_all)
6360 strip = TRUE;
6361 else if (finfo->info->strip == strip_some
6362 && bfd_hash_lookup (finfo->info->keep_hash,
6363 h->root.root.string, FALSE, FALSE) == NULL)
6364 strip = TRUE;
6365 else if (finfo->info->strip_discarded
6366 && (h->root.type == bfd_link_hash_defined
6367 || h->root.type == bfd_link_hash_defweak)
6368 && elf_discarded_section (h->root.u.def.section))
6369 strip = TRUE;
6370 else
6371 strip = FALSE;
6373 /* If we're stripping it, and it's not a dynamic symbol, there's
6374 nothing else to do unless it is a forced local symbol. */
6375 if (strip
6376 && h->dynindx == -1
6377 && !h->forced_local)
6378 return TRUE;
6380 sym.st_value = 0;
6381 sym.st_size = h->size;
6382 sym.st_other = h->other;
6383 if (h->forced_local)
6384 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6385 else if (h->root.type == bfd_link_hash_undefweak
6386 || h->root.type == bfd_link_hash_defweak)
6387 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6388 else
6389 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6391 switch (h->root.type)
6393 default:
6394 case bfd_link_hash_new:
6395 case bfd_link_hash_warning:
6396 abort ();
6397 return FALSE;
6399 case bfd_link_hash_undefined:
6400 case bfd_link_hash_undefweak:
6401 input_sec = bfd_und_section_ptr;
6402 sym.st_shndx = SHN_UNDEF;
6403 break;
6405 case bfd_link_hash_defined:
6406 case bfd_link_hash_defweak:
6408 input_sec = h->root.u.def.section;
6409 if (input_sec->output_section != NULL)
6411 sym.st_shndx =
6412 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6413 input_sec->output_section);
6414 if (sym.st_shndx == SHN_BAD)
6416 (*_bfd_error_handler)
6417 (_("%B: could not find output section %A for input section %A"),
6418 finfo->output_bfd, input_sec->output_section, input_sec);
6419 eoinfo->failed = TRUE;
6420 return FALSE;
6423 /* ELF symbols in relocatable files are section relative,
6424 but in nonrelocatable files they are virtual
6425 addresses. */
6426 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6427 if (! finfo->info->relocatable)
6429 sym.st_value += input_sec->output_section->vma;
6430 if (h->type == STT_TLS)
6432 /* STT_TLS symbols are relative to PT_TLS segment
6433 base. */
6434 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6435 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6439 else
6441 BFD_ASSERT (input_sec->owner == NULL
6442 || (input_sec->owner->flags & DYNAMIC) != 0);
6443 sym.st_shndx = SHN_UNDEF;
6444 input_sec = bfd_und_section_ptr;
6447 break;
6449 case bfd_link_hash_common:
6450 input_sec = h->root.u.c.p->section;
6451 sym.st_shndx = SHN_COMMON;
6452 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6453 break;
6455 case bfd_link_hash_indirect:
6456 /* These symbols are created by symbol versioning. They point
6457 to the decorated version of the name. For example, if the
6458 symbol foo@@GNU_1.2 is the default, which should be used when
6459 foo is used with no version, then we add an indirect symbol
6460 foo which points to foo@@GNU_1.2. We ignore these symbols,
6461 since the indirected symbol is already in the hash table. */
6462 return TRUE;
6465 /* Give the processor backend a chance to tweak the symbol value,
6466 and also to finish up anything that needs to be done for this
6467 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6468 forced local syms when non-shared is due to a historical quirk. */
6469 if ((h->dynindx != -1
6470 || h->forced_local)
6471 && ((finfo->info->shared
6472 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6473 || h->root.type != bfd_link_hash_undefweak))
6474 || !h->forced_local)
6475 && elf_hash_table (finfo->info)->dynamic_sections_created)
6477 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6478 (finfo->output_bfd, finfo->info, h, &sym)))
6480 eoinfo->failed = TRUE;
6481 return FALSE;
6485 /* If we are marking the symbol as undefined, and there are no
6486 non-weak references to this symbol from a regular object, then
6487 mark the symbol as weak undefined; if there are non-weak
6488 references, mark the symbol as strong. We can't do this earlier,
6489 because it might not be marked as undefined until the
6490 finish_dynamic_symbol routine gets through with it. */
6491 if (sym.st_shndx == SHN_UNDEF
6492 && h->ref_regular
6493 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6494 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6496 int bindtype;
6498 if (h->ref_regular_nonweak)
6499 bindtype = STB_GLOBAL;
6500 else
6501 bindtype = STB_WEAK;
6502 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6505 /* If a non-weak symbol with non-default visibility is not defined
6506 locally, it is a fatal error. */
6507 if (! finfo->info->relocatable
6508 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6509 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6510 && h->root.type == bfd_link_hash_undefined
6511 && !h->def_regular)
6513 (*_bfd_error_handler)
6514 (_("%B: %s symbol `%s' isn't defined"),
6515 finfo->output_bfd,
6516 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6517 ? "protected"
6518 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6519 ? "internal" : "hidden",
6520 h->root.root.string);
6521 eoinfo->failed = TRUE;
6522 return FALSE;
6525 /* If this symbol should be put in the .dynsym section, then put it
6526 there now. We already know the symbol index. We also fill in
6527 the entry in the .hash section. */
6528 if (h->dynindx != -1
6529 && elf_hash_table (finfo->info)->dynamic_sections_created)
6531 size_t bucketcount;
6532 size_t bucket;
6533 size_t hash_entry_size;
6534 bfd_byte *bucketpos;
6535 bfd_vma chain;
6536 bfd_byte *esym;
6538 sym.st_name = h->dynstr_index;
6539 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6540 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6542 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6543 bucket = h->u.elf_hash_value % bucketcount;
6544 hash_entry_size
6545 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6546 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6547 + (bucket + 2) * hash_entry_size);
6548 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6549 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6550 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6551 ((bfd_byte *) finfo->hash_sec->contents
6552 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6554 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6556 Elf_Internal_Versym iversym;
6557 Elf_External_Versym *eversym;
6559 if (!h->def_regular)
6561 if (h->verinfo.verdef == NULL)
6562 iversym.vs_vers = 0;
6563 else
6564 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6566 else
6568 if (h->verinfo.vertree == NULL)
6569 iversym.vs_vers = 1;
6570 else
6571 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6572 if (finfo->info->create_default_symver)
6573 iversym.vs_vers++;
6576 if (h->hidden)
6577 iversym.vs_vers |= VERSYM_HIDDEN;
6579 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6580 eversym += h->dynindx;
6581 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6585 /* If we're stripping it, then it was just a dynamic symbol, and
6586 there's nothing else to do. */
6587 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6588 return TRUE;
6590 h->indx = bfd_get_symcount (finfo->output_bfd);
6592 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6594 eoinfo->failed = TRUE;
6595 return FALSE;
6598 return TRUE;
6601 /* Return TRUE if special handling is done for relocs in SEC against
6602 symbols defined in discarded sections. */
6604 static bfd_boolean
6605 elf_section_ignore_discarded_relocs (asection *sec)
6607 const struct elf_backend_data *bed;
6609 switch (sec->sec_info_type)
6611 case ELF_INFO_TYPE_STABS:
6612 case ELF_INFO_TYPE_EH_FRAME:
6613 return TRUE;
6614 default:
6615 break;
6618 bed = get_elf_backend_data (sec->owner);
6619 if (bed->elf_backend_ignore_discarded_relocs != NULL
6620 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6621 return TRUE;
6623 return FALSE;
6626 enum action_discarded
6628 COMPLAIN = 1,
6629 PRETEND = 2
6632 /* Return a mask saying how ld should treat relocations in SEC against
6633 symbols defined in discarded sections. If this function returns
6634 COMPLAIN set, ld will issue a warning message. If this function
6635 returns PRETEND set, and the discarded section was link-once and the
6636 same size as the kept link-once section, ld will pretend that the
6637 symbol was actually defined in the kept section. Otherwise ld will
6638 zero the reloc (at least that is the intent, but some cooperation by
6639 the target dependent code is needed, particularly for REL targets). */
6641 static unsigned int
6642 elf_action_discarded (asection *sec)
6644 if (sec->flags & SEC_DEBUGGING)
6645 return PRETEND;
6647 if (strcmp (".eh_frame", sec->name) == 0)
6648 return 0;
6650 if (strcmp (".gcc_except_table", sec->name) == 0)
6651 return 0;
6653 if (strcmp (".PARISC.unwind", sec->name) == 0)
6654 return 0;
6656 if (strcmp (".fixup", sec->name) == 0)
6657 return 0;
6659 return COMPLAIN | PRETEND;
6662 /* Find a match between a section and a member of a section group. */
6664 static asection *
6665 match_group_member (asection *sec, asection *group)
6667 asection *first = elf_next_in_group (group);
6668 asection *s = first;
6670 while (s != NULL)
6672 if (bfd_elf_match_symbols_in_sections (s, sec))
6673 return s;
6675 if (s == first)
6676 break;
6679 return NULL;
6682 /* Link an input file into the linker output file. This function
6683 handles all the sections and relocations of the input file at once.
6684 This is so that we only have to read the local symbols once, and
6685 don't have to keep them in memory. */
6687 static bfd_boolean
6688 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6690 bfd_boolean (*relocate_section)
6691 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6692 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6693 bfd *output_bfd;
6694 Elf_Internal_Shdr *symtab_hdr;
6695 size_t locsymcount;
6696 size_t extsymoff;
6697 Elf_Internal_Sym *isymbuf;
6698 Elf_Internal_Sym *isym;
6699 Elf_Internal_Sym *isymend;
6700 long *pindex;
6701 asection **ppsection;
6702 asection *o;
6703 const struct elf_backend_data *bed;
6704 bfd_boolean emit_relocs;
6705 struct elf_link_hash_entry **sym_hashes;
6707 output_bfd = finfo->output_bfd;
6708 bed = get_elf_backend_data (output_bfd);
6709 relocate_section = bed->elf_backend_relocate_section;
6711 /* If this is a dynamic object, we don't want to do anything here:
6712 we don't want the local symbols, and we don't want the section
6713 contents. */
6714 if ((input_bfd->flags & DYNAMIC) != 0)
6715 return TRUE;
6717 emit_relocs = (finfo->info->relocatable
6718 || finfo->info->emitrelocations
6719 || bed->elf_backend_emit_relocs);
6721 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6722 if (elf_bad_symtab (input_bfd))
6724 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6725 extsymoff = 0;
6727 else
6729 locsymcount = symtab_hdr->sh_info;
6730 extsymoff = symtab_hdr->sh_info;
6733 /* Read the local symbols. */
6734 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6735 if (isymbuf == NULL && locsymcount != 0)
6737 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6738 finfo->internal_syms,
6739 finfo->external_syms,
6740 finfo->locsym_shndx);
6741 if (isymbuf == NULL)
6742 return FALSE;
6745 /* Find local symbol sections and adjust values of symbols in
6746 SEC_MERGE sections. Write out those local symbols we know are
6747 going into the output file. */
6748 isymend = isymbuf + locsymcount;
6749 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6750 isym < isymend;
6751 isym++, pindex++, ppsection++)
6753 asection *isec;
6754 const char *name;
6755 Elf_Internal_Sym osym;
6757 *pindex = -1;
6759 if (elf_bad_symtab (input_bfd))
6761 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6763 *ppsection = NULL;
6764 continue;
6768 if (isym->st_shndx == SHN_UNDEF)
6769 isec = bfd_und_section_ptr;
6770 else if (isym->st_shndx < SHN_LORESERVE
6771 || isym->st_shndx > SHN_HIRESERVE)
6773 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6774 if (isec
6775 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6776 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6777 isym->st_value =
6778 _bfd_merged_section_offset (output_bfd, &isec,
6779 elf_section_data (isec)->sec_info,
6780 isym->st_value);
6782 else if (isym->st_shndx == SHN_ABS)
6783 isec = bfd_abs_section_ptr;
6784 else if (isym->st_shndx == SHN_COMMON)
6785 isec = bfd_com_section_ptr;
6786 else
6788 /* Who knows? */
6789 isec = NULL;
6792 *ppsection = isec;
6794 /* Don't output the first, undefined, symbol. */
6795 if (ppsection == finfo->sections)
6796 continue;
6798 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6800 /* We never output section symbols. Instead, we use the
6801 section symbol of the corresponding section in the output
6802 file. */
6803 continue;
6806 /* If we are stripping all symbols, we don't want to output this
6807 one. */
6808 if (finfo->info->strip == strip_all)
6809 continue;
6811 /* If we are discarding all local symbols, we don't want to
6812 output this one. If we are generating a relocatable output
6813 file, then some of the local symbols may be required by
6814 relocs; we output them below as we discover that they are
6815 needed. */
6816 if (finfo->info->discard == discard_all)
6817 continue;
6819 /* If this symbol is defined in a section which we are
6820 discarding, we don't need to keep it, but note that
6821 linker_mark is only reliable for sections that have contents.
6822 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6823 as well as linker_mark. */
6824 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6825 && (isec == NULL
6826 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6827 || (! finfo->info->relocatable
6828 && (isec->flags & SEC_EXCLUDE) != 0)))
6829 continue;
6831 /* Get the name of the symbol. */
6832 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6833 isym->st_name);
6834 if (name == NULL)
6835 return FALSE;
6837 /* See if we are discarding symbols with this name. */
6838 if ((finfo->info->strip == strip_some
6839 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6840 == NULL))
6841 || (((finfo->info->discard == discard_sec_merge
6842 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6843 || finfo->info->discard == discard_l)
6844 && bfd_is_local_label_name (input_bfd, name)))
6845 continue;
6847 /* If we get here, we are going to output this symbol. */
6849 osym = *isym;
6851 /* Adjust the section index for the output file. */
6852 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6853 isec->output_section);
6854 if (osym.st_shndx == SHN_BAD)
6855 return FALSE;
6857 *pindex = bfd_get_symcount (output_bfd);
6859 /* ELF symbols in relocatable files are section relative, but
6860 in executable files they are virtual addresses. Note that
6861 this code assumes that all ELF sections have an associated
6862 BFD section with a reasonable value for output_offset; below
6863 we assume that they also have a reasonable value for
6864 output_section. Any special sections must be set up to meet
6865 these requirements. */
6866 osym.st_value += isec->output_offset;
6867 if (! finfo->info->relocatable)
6869 osym.st_value += isec->output_section->vma;
6870 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6872 /* STT_TLS symbols are relative to PT_TLS segment base. */
6873 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6874 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6878 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6879 return FALSE;
6882 /* Relocate the contents of each section. */
6883 sym_hashes = elf_sym_hashes (input_bfd);
6884 for (o = input_bfd->sections; o != NULL; o = o->next)
6886 bfd_byte *contents;
6888 if (! o->linker_mark)
6890 /* This section was omitted from the link. */
6891 continue;
6894 if ((o->flags & SEC_HAS_CONTENTS) == 0
6895 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6896 continue;
6898 if ((o->flags & SEC_LINKER_CREATED) != 0)
6900 /* Section was created by _bfd_elf_link_create_dynamic_sections
6901 or somesuch. */
6902 continue;
6905 /* Get the contents of the section. They have been cached by a
6906 relaxation routine. Note that o is a section in an input
6907 file, so the contents field will not have been set by any of
6908 the routines which work on output files. */
6909 if (elf_section_data (o)->this_hdr.contents != NULL)
6910 contents = elf_section_data (o)->this_hdr.contents;
6911 else
6913 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6915 contents = finfo->contents;
6916 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6917 return FALSE;
6920 if ((o->flags & SEC_RELOC) != 0)
6922 Elf_Internal_Rela *internal_relocs;
6923 bfd_vma r_type_mask;
6924 int r_sym_shift;
6926 /* Get the swapped relocs. */
6927 internal_relocs
6928 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6929 finfo->internal_relocs, FALSE);
6930 if (internal_relocs == NULL
6931 && o->reloc_count > 0)
6932 return FALSE;
6934 if (bed->s->arch_size == 32)
6936 r_type_mask = 0xff;
6937 r_sym_shift = 8;
6939 else
6941 r_type_mask = 0xffffffff;
6942 r_sym_shift = 32;
6945 /* Run through the relocs looking for any against symbols
6946 from discarded sections and section symbols from
6947 removed link-once sections. Complain about relocs
6948 against discarded sections. Zero relocs against removed
6949 link-once sections. Preserve debug information as much
6950 as we can. */
6951 if (!elf_section_ignore_discarded_relocs (o))
6953 Elf_Internal_Rela *rel, *relend;
6954 unsigned int action = elf_action_discarded (o);
6956 rel = internal_relocs;
6957 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6958 for ( ; rel < relend; rel++)
6960 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6961 asection **ps, *sec;
6962 struct elf_link_hash_entry *h = NULL;
6963 const char *sym_name;
6965 if (r_symndx == STN_UNDEF)
6966 continue;
6968 if (r_symndx >= locsymcount
6969 || (elf_bad_symtab (input_bfd)
6970 && finfo->sections[r_symndx] == NULL))
6972 h = sym_hashes[r_symndx - extsymoff];
6973 while (h->root.type == bfd_link_hash_indirect
6974 || h->root.type == bfd_link_hash_warning)
6975 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6977 if (h->root.type != bfd_link_hash_defined
6978 && h->root.type != bfd_link_hash_defweak)
6979 continue;
6981 ps = &h->root.u.def.section;
6982 sym_name = h->root.root.string;
6984 else
6986 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6987 ps = &finfo->sections[r_symndx];
6988 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
6991 /* Complain if the definition comes from a
6992 discarded section. */
6993 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6995 asection *kept;
6997 BFD_ASSERT (r_symndx != 0);
6998 if (action & COMPLAIN)
7000 (*_bfd_error_handler)
7001 (_("`%s' referenced in section `%A' of %B: "
7002 "defined in discarded section `%A' of %B\n"),
7003 o, input_bfd, sec, sec->owner, sym_name);
7006 /* Try to do the best we can to support buggy old
7007 versions of gcc. If we've warned, or this is
7008 debugging info, pretend that the symbol is
7009 really defined in the kept linkonce section.
7010 FIXME: This is quite broken. Modifying the
7011 symbol here means we will be changing all later
7012 uses of the symbol, not just in this section.
7013 The only thing that makes this half reasonable
7014 is that we warn in non-debug sections, and
7015 debug sections tend to come after other
7016 sections. */
7017 kept = sec->kept_section;
7018 if (kept != NULL && (action & PRETEND))
7020 if (elf_sec_group (sec) != NULL)
7021 kept = match_group_member (sec, kept);
7022 if (kept != NULL
7023 && sec->size == kept->size)
7025 *ps = kept;
7026 continue;
7030 /* Remove the symbol reference from the reloc, but
7031 don't kill the reloc completely. This is so that
7032 a zero value will be written into the section,
7033 which may have non-zero contents put there by the
7034 assembler. Zero in things like an eh_frame fde
7035 pc_begin allows stack unwinders to recognize the
7036 fde as bogus. */
7037 rel->r_info &= r_type_mask;
7038 rel->r_addend = 0;
7043 /* Relocate the section by invoking a back end routine.
7045 The back end routine is responsible for adjusting the
7046 section contents as necessary, and (if using Rela relocs
7047 and generating a relocatable output file) adjusting the
7048 reloc addend as necessary.
7050 The back end routine does not have to worry about setting
7051 the reloc address or the reloc symbol index.
7053 The back end routine is given a pointer to the swapped in
7054 internal symbols, and can access the hash table entries
7055 for the external symbols via elf_sym_hashes (input_bfd).
7057 When generating relocatable output, the back end routine
7058 must handle STB_LOCAL/STT_SECTION symbols specially. The
7059 output symbol is going to be a section symbol
7060 corresponding to the output section, which will require
7061 the addend to be adjusted. */
7063 if (! (*relocate_section) (output_bfd, finfo->info,
7064 input_bfd, o, contents,
7065 internal_relocs,
7066 isymbuf,
7067 finfo->sections))
7068 return FALSE;
7070 if (emit_relocs)
7072 Elf_Internal_Rela *irela;
7073 Elf_Internal_Rela *irelaend;
7074 bfd_vma last_offset;
7075 struct elf_link_hash_entry **rel_hash;
7076 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7077 unsigned int next_erel;
7078 bfd_boolean (*reloc_emitter)
7079 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
7080 bfd_boolean rela_normal;
7082 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7083 rela_normal = (bed->rela_normal
7084 && (input_rel_hdr->sh_entsize
7085 == bed->s->sizeof_rela));
7087 /* Adjust the reloc addresses and symbol indices. */
7089 irela = internal_relocs;
7090 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7091 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7092 + elf_section_data (o->output_section)->rel_count
7093 + elf_section_data (o->output_section)->rel_count2);
7094 last_offset = o->output_offset;
7095 if (!finfo->info->relocatable)
7096 last_offset += o->output_section->vma;
7097 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7099 unsigned long r_symndx;
7100 asection *sec;
7101 Elf_Internal_Sym sym;
7103 if (next_erel == bed->s->int_rels_per_ext_rel)
7105 rel_hash++;
7106 next_erel = 0;
7109 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7110 finfo->info, o,
7111 irela->r_offset);
7112 if (irela->r_offset >= (bfd_vma) -2)
7114 /* This is a reloc for a deleted entry or somesuch.
7115 Turn it into an R_*_NONE reloc, at the same
7116 offset as the last reloc. elf_eh_frame.c and
7117 elf_bfd_discard_info rely on reloc offsets
7118 being ordered. */
7119 irela->r_offset = last_offset;
7120 irela->r_info = 0;
7121 irela->r_addend = 0;
7122 continue;
7125 irela->r_offset += o->output_offset;
7127 /* Relocs in an executable have to be virtual addresses. */
7128 if (!finfo->info->relocatable)
7129 irela->r_offset += o->output_section->vma;
7131 last_offset = irela->r_offset;
7133 r_symndx = irela->r_info >> r_sym_shift;
7134 if (r_symndx == STN_UNDEF)
7135 continue;
7137 if (r_symndx >= locsymcount
7138 || (elf_bad_symtab (input_bfd)
7139 && finfo->sections[r_symndx] == NULL))
7141 struct elf_link_hash_entry *rh;
7142 unsigned long indx;
7144 /* This is a reloc against a global symbol. We
7145 have not yet output all the local symbols, so
7146 we do not know the symbol index of any global
7147 symbol. We set the rel_hash entry for this
7148 reloc to point to the global hash table entry
7149 for this symbol. The symbol index is then
7150 set at the end of bfd_elf_final_link. */
7151 indx = r_symndx - extsymoff;
7152 rh = elf_sym_hashes (input_bfd)[indx];
7153 while (rh->root.type == bfd_link_hash_indirect
7154 || rh->root.type == bfd_link_hash_warning)
7155 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7157 /* Setting the index to -2 tells
7158 elf_link_output_extsym that this symbol is
7159 used by a reloc. */
7160 BFD_ASSERT (rh->indx < 0);
7161 rh->indx = -2;
7163 *rel_hash = rh;
7165 continue;
7168 /* This is a reloc against a local symbol. */
7170 *rel_hash = NULL;
7171 sym = isymbuf[r_symndx];
7172 sec = finfo->sections[r_symndx];
7173 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7175 /* I suppose the backend ought to fill in the
7176 section of any STT_SECTION symbol against a
7177 processor specific section. */
7178 r_symndx = 0;
7179 if (bfd_is_abs_section (sec))
7181 else if (sec == NULL || sec->owner == NULL)
7183 bfd_set_error (bfd_error_bad_value);
7184 return FALSE;
7186 else
7188 asection *osec = sec->output_section;
7190 /* If we have discarded a section, the output
7191 section will be the absolute section. In
7192 case of discarded link-once and discarded
7193 SEC_MERGE sections, use the kept section. */
7194 if (bfd_is_abs_section (osec)
7195 && sec->kept_section != NULL
7196 && sec->kept_section->output_section != NULL)
7198 osec = sec->kept_section->output_section;
7199 irela->r_addend -= osec->vma;
7202 if (!bfd_is_abs_section (osec))
7204 r_symndx = osec->target_index;
7205 BFD_ASSERT (r_symndx != 0);
7209 /* Adjust the addend according to where the
7210 section winds up in the output section. */
7211 if (rela_normal)
7212 irela->r_addend += sec->output_offset;
7214 else
7216 if (finfo->indices[r_symndx] == -1)
7218 unsigned long shlink;
7219 const char *name;
7220 asection *osec;
7222 if (finfo->info->strip == strip_all)
7224 /* You can't do ld -r -s. */
7225 bfd_set_error (bfd_error_invalid_operation);
7226 return FALSE;
7229 /* This symbol was skipped earlier, but
7230 since it is needed by a reloc, we
7231 must output it now. */
7232 shlink = symtab_hdr->sh_link;
7233 name = (bfd_elf_string_from_elf_section
7234 (input_bfd, shlink, sym.st_name));
7235 if (name == NULL)
7236 return FALSE;
7238 osec = sec->output_section;
7239 sym.st_shndx =
7240 _bfd_elf_section_from_bfd_section (output_bfd,
7241 osec);
7242 if (sym.st_shndx == SHN_BAD)
7243 return FALSE;
7245 sym.st_value += sec->output_offset;
7246 if (! finfo->info->relocatable)
7248 sym.st_value += osec->vma;
7249 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7251 /* STT_TLS symbols are relative to PT_TLS
7252 segment base. */
7253 BFD_ASSERT (elf_hash_table (finfo->info)
7254 ->tls_sec != NULL);
7255 sym.st_value -= (elf_hash_table (finfo->info)
7256 ->tls_sec->vma);
7260 finfo->indices[r_symndx]
7261 = bfd_get_symcount (output_bfd);
7263 if (! elf_link_output_sym (finfo, name, &sym, sec,
7264 NULL))
7265 return FALSE;
7268 r_symndx = finfo->indices[r_symndx];
7271 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7272 | (irela->r_info & r_type_mask));
7275 /* Swap out the relocs. */
7276 if (bed->elf_backend_emit_relocs
7277 && !(finfo->info->relocatable
7278 || finfo->info->emitrelocations))
7279 reloc_emitter = bed->elf_backend_emit_relocs;
7280 else
7281 reloc_emitter = _bfd_elf_link_output_relocs;
7283 if (input_rel_hdr->sh_size != 0
7284 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7285 internal_relocs))
7286 return FALSE;
7288 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7289 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7291 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7292 * bed->s->int_rels_per_ext_rel);
7293 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7294 internal_relocs))
7295 return FALSE;
7300 /* Write out the modified section contents. */
7301 if (bed->elf_backend_write_section
7302 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7304 /* Section written out. */
7306 else switch (o->sec_info_type)
7308 case ELF_INFO_TYPE_STABS:
7309 if (! (_bfd_write_section_stabs
7310 (output_bfd,
7311 &elf_hash_table (finfo->info)->stab_info,
7312 o, &elf_section_data (o)->sec_info, contents)))
7313 return FALSE;
7314 break;
7315 case ELF_INFO_TYPE_MERGE:
7316 if (! _bfd_write_merged_section (output_bfd, o,
7317 elf_section_data (o)->sec_info))
7318 return FALSE;
7319 break;
7320 case ELF_INFO_TYPE_EH_FRAME:
7322 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7323 o, contents))
7324 return FALSE;
7326 break;
7327 default:
7329 if (! (o->flags & SEC_EXCLUDE)
7330 && ! bfd_set_section_contents (output_bfd, o->output_section,
7331 contents,
7332 (file_ptr) o->output_offset,
7333 o->size))
7334 return FALSE;
7336 break;
7340 return TRUE;
7343 /* Generate a reloc when linking an ELF file. This is a reloc
7344 requested by the linker, and does come from any input file. This
7345 is used to build constructor and destructor tables when linking
7346 with -Ur. */
7348 static bfd_boolean
7349 elf_reloc_link_order (bfd *output_bfd,
7350 struct bfd_link_info *info,
7351 asection *output_section,
7352 struct bfd_link_order *link_order)
7354 reloc_howto_type *howto;
7355 long indx;
7356 bfd_vma offset;
7357 bfd_vma addend;
7358 struct elf_link_hash_entry **rel_hash_ptr;
7359 Elf_Internal_Shdr *rel_hdr;
7360 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7361 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7362 bfd_byte *erel;
7363 unsigned int i;
7365 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7366 if (howto == NULL)
7368 bfd_set_error (bfd_error_bad_value);
7369 return FALSE;
7372 addend = link_order->u.reloc.p->addend;
7374 /* Figure out the symbol index. */
7375 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7376 + elf_section_data (output_section)->rel_count
7377 + elf_section_data (output_section)->rel_count2);
7378 if (link_order->type == bfd_section_reloc_link_order)
7380 indx = link_order->u.reloc.p->u.section->target_index;
7381 BFD_ASSERT (indx != 0);
7382 *rel_hash_ptr = NULL;
7384 else
7386 struct elf_link_hash_entry *h;
7388 /* Treat a reloc against a defined symbol as though it were
7389 actually against the section. */
7390 h = ((struct elf_link_hash_entry *)
7391 bfd_wrapped_link_hash_lookup (output_bfd, info,
7392 link_order->u.reloc.p->u.name,
7393 FALSE, FALSE, TRUE));
7394 if (h != NULL
7395 && (h->root.type == bfd_link_hash_defined
7396 || h->root.type == bfd_link_hash_defweak))
7398 asection *section;
7400 section = h->root.u.def.section;
7401 indx = section->output_section->target_index;
7402 *rel_hash_ptr = NULL;
7403 /* It seems that we ought to add the symbol value to the
7404 addend here, but in practice it has already been added
7405 because it was passed to constructor_callback. */
7406 addend += section->output_section->vma + section->output_offset;
7408 else if (h != NULL)
7410 /* Setting the index to -2 tells elf_link_output_extsym that
7411 this symbol is used by a reloc. */
7412 h->indx = -2;
7413 *rel_hash_ptr = h;
7414 indx = 0;
7416 else
7418 if (! ((*info->callbacks->unattached_reloc)
7419 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7420 return FALSE;
7421 indx = 0;
7425 /* If this is an inplace reloc, we must write the addend into the
7426 object file. */
7427 if (howto->partial_inplace && addend != 0)
7429 bfd_size_type size;
7430 bfd_reloc_status_type rstat;
7431 bfd_byte *buf;
7432 bfd_boolean ok;
7433 const char *sym_name;
7435 size = bfd_get_reloc_size (howto);
7436 buf = bfd_zmalloc (size);
7437 if (buf == NULL)
7438 return FALSE;
7439 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7440 switch (rstat)
7442 case bfd_reloc_ok:
7443 break;
7445 default:
7446 case bfd_reloc_outofrange:
7447 abort ();
7449 case bfd_reloc_overflow:
7450 if (link_order->type == bfd_section_reloc_link_order)
7451 sym_name = bfd_section_name (output_bfd,
7452 link_order->u.reloc.p->u.section);
7453 else
7454 sym_name = link_order->u.reloc.p->u.name;
7455 if (! ((*info->callbacks->reloc_overflow)
7456 (info, NULL, sym_name, howto->name, addend, NULL,
7457 NULL, (bfd_vma) 0)))
7459 free (buf);
7460 return FALSE;
7462 break;
7464 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7465 link_order->offset, size);
7466 free (buf);
7467 if (! ok)
7468 return FALSE;
7471 /* The address of a reloc is relative to the section in a
7472 relocatable file, and is a virtual address in an executable
7473 file. */
7474 offset = link_order->offset;
7475 if (! info->relocatable)
7476 offset += output_section->vma;
7478 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7480 irel[i].r_offset = offset;
7481 irel[i].r_info = 0;
7482 irel[i].r_addend = 0;
7484 if (bed->s->arch_size == 32)
7485 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7486 else
7487 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7489 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7490 erel = rel_hdr->contents;
7491 if (rel_hdr->sh_type == SHT_REL)
7493 erel += (elf_section_data (output_section)->rel_count
7494 * bed->s->sizeof_rel);
7495 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7497 else
7499 irel[0].r_addend = addend;
7500 erel += (elf_section_data (output_section)->rel_count
7501 * bed->s->sizeof_rela);
7502 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7505 ++elf_section_data (output_section)->rel_count;
7507 return TRUE;
7511 /* Get the output vma of the section pointed to by the sh_link field. */
7513 static bfd_vma
7514 elf_get_linked_section_vma (struct bfd_link_order *p)
7516 Elf_Internal_Shdr **elf_shdrp;
7517 asection *s;
7518 int elfsec;
7520 s = p->u.indirect.section;
7521 elf_shdrp = elf_elfsections (s->owner);
7522 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7523 elfsec = elf_shdrp[elfsec]->sh_link;
7524 /* PR 290:
7525 The Intel C compiler generates SHT_IA_64_UNWIND with
7526 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7527 sh_info fields. Hence we could get the situation
7528 where elfsec is 0. */
7529 if (elfsec == 0)
7531 const struct elf_backend_data *bed
7532 = get_elf_backend_data (s->owner);
7533 if (bed->link_order_error_handler)
7534 bed->link_order_error_handler
7535 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7536 return 0;
7538 else
7540 s = elf_shdrp[elfsec]->bfd_section;
7541 return s->output_section->vma + s->output_offset;
7546 /* Compare two sections based on the locations of the sections they are
7547 linked to. Used by elf_fixup_link_order. */
7549 static int
7550 compare_link_order (const void * a, const void * b)
7552 bfd_vma apos;
7553 bfd_vma bpos;
7555 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7556 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7557 if (apos < bpos)
7558 return -1;
7559 return apos > bpos;
7563 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7564 order as their linked sections. Returns false if this could not be done
7565 because an output section includes both ordered and unordered
7566 sections. Ideally we'd do this in the linker proper. */
7568 static bfd_boolean
7569 elf_fixup_link_order (bfd *abfd, asection *o)
7571 int seen_linkorder;
7572 int seen_other;
7573 int n;
7574 struct bfd_link_order *p;
7575 bfd *sub;
7576 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7577 int elfsec;
7578 struct bfd_link_order **sections;
7579 asection *s;
7580 bfd_vma offset;
7582 seen_other = 0;
7583 seen_linkorder = 0;
7584 for (p = o->link_order_head; p != NULL; p = p->next)
7586 if (p->type == bfd_indirect_link_order
7587 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7588 == bfd_target_elf_flavour)
7589 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7591 s = p->u.indirect.section;
7592 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7593 if (elfsec != -1
7594 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7595 seen_linkorder++;
7596 else
7597 seen_other++;
7599 else
7600 seen_other++;
7603 if (!seen_linkorder)
7604 return TRUE;
7606 if (seen_other && seen_linkorder)
7608 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7610 bfd_set_error (bfd_error_bad_value);
7611 return FALSE;
7614 sections = (struct bfd_link_order **)
7615 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7616 seen_linkorder = 0;
7618 for (p = o->link_order_head; p != NULL; p = p->next)
7620 sections[seen_linkorder++] = p;
7622 /* Sort the input sections in the order of their linked section. */
7623 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7624 compare_link_order);
7626 /* Change the offsets of the sections. */
7627 offset = 0;
7628 for (n = 0; n < seen_linkorder; n++)
7630 s = sections[n]->u.indirect.section;
7631 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7632 s->output_offset = offset;
7633 sections[n]->offset = offset;
7634 offset += sections[n]->size;
7637 return TRUE;
7641 /* Do the final step of an ELF link. */
7643 bfd_boolean
7644 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7646 bfd_boolean dynamic;
7647 bfd_boolean emit_relocs;
7648 bfd *dynobj;
7649 struct elf_final_link_info finfo;
7650 register asection *o;
7651 register struct bfd_link_order *p;
7652 register bfd *sub;
7653 bfd_size_type max_contents_size;
7654 bfd_size_type max_external_reloc_size;
7655 bfd_size_type max_internal_reloc_count;
7656 bfd_size_type max_sym_count;
7657 bfd_size_type max_sym_shndx_count;
7658 file_ptr off;
7659 Elf_Internal_Sym elfsym;
7660 unsigned int i;
7661 Elf_Internal_Shdr *symtab_hdr;
7662 Elf_Internal_Shdr *symtab_shndx_hdr;
7663 Elf_Internal_Shdr *symstrtab_hdr;
7664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7665 struct elf_outext_info eoinfo;
7666 bfd_boolean merged;
7667 size_t relativecount = 0;
7668 asection *reldyn = 0;
7669 bfd_size_type amt;
7671 if (! is_elf_hash_table (info->hash))
7672 return FALSE;
7674 if (info->shared)
7675 abfd->flags |= DYNAMIC;
7677 dynamic = elf_hash_table (info)->dynamic_sections_created;
7678 dynobj = elf_hash_table (info)->dynobj;
7680 emit_relocs = (info->relocatable
7681 || info->emitrelocations
7682 || bed->elf_backend_emit_relocs);
7684 finfo.info = info;
7685 finfo.output_bfd = abfd;
7686 finfo.symstrtab = _bfd_elf_stringtab_init ();
7687 if (finfo.symstrtab == NULL)
7688 return FALSE;
7690 if (! dynamic)
7692 finfo.dynsym_sec = NULL;
7693 finfo.hash_sec = NULL;
7694 finfo.symver_sec = NULL;
7696 else
7698 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7699 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7700 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7701 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7702 /* Note that it is OK if symver_sec is NULL. */
7705 finfo.contents = NULL;
7706 finfo.external_relocs = NULL;
7707 finfo.internal_relocs = NULL;
7708 finfo.external_syms = NULL;
7709 finfo.locsym_shndx = NULL;
7710 finfo.internal_syms = NULL;
7711 finfo.indices = NULL;
7712 finfo.sections = NULL;
7713 finfo.symbuf = NULL;
7714 finfo.symshndxbuf = NULL;
7715 finfo.symbuf_count = 0;
7716 finfo.shndxbuf_size = 0;
7718 /* Count up the number of relocations we will output for each output
7719 section, so that we know the sizes of the reloc sections. We
7720 also figure out some maximum sizes. */
7721 max_contents_size = 0;
7722 max_external_reloc_size = 0;
7723 max_internal_reloc_count = 0;
7724 max_sym_count = 0;
7725 max_sym_shndx_count = 0;
7726 merged = FALSE;
7727 for (o = abfd->sections; o != NULL; o = o->next)
7729 struct bfd_elf_section_data *esdo = elf_section_data (o);
7730 o->reloc_count = 0;
7732 for (p = o->link_order_head; p != NULL; p = p->next)
7734 unsigned int reloc_count = 0;
7735 struct bfd_elf_section_data *esdi = NULL;
7736 unsigned int *rel_count1;
7738 if (p->type == bfd_section_reloc_link_order
7739 || p->type == bfd_symbol_reloc_link_order)
7740 reloc_count = 1;
7741 else if (p->type == bfd_indirect_link_order)
7743 asection *sec;
7745 sec = p->u.indirect.section;
7746 esdi = elf_section_data (sec);
7748 /* Mark all sections which are to be included in the
7749 link. This will normally be every section. We need
7750 to do this so that we can identify any sections which
7751 the linker has decided to not include. */
7752 sec->linker_mark = TRUE;
7754 if (sec->flags & SEC_MERGE)
7755 merged = TRUE;
7757 if (info->relocatable || info->emitrelocations)
7758 reloc_count = sec->reloc_count;
7759 else if (bed->elf_backend_count_relocs)
7761 Elf_Internal_Rela * relocs;
7763 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7764 info->keep_memory);
7766 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7768 if (elf_section_data (o)->relocs != relocs)
7769 free (relocs);
7772 if (sec->rawsize > max_contents_size)
7773 max_contents_size = sec->rawsize;
7774 if (sec->size > max_contents_size)
7775 max_contents_size = sec->size;
7777 /* We are interested in just local symbols, not all
7778 symbols. */
7779 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7780 && (sec->owner->flags & DYNAMIC) == 0)
7782 size_t sym_count;
7784 if (elf_bad_symtab (sec->owner))
7785 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7786 / bed->s->sizeof_sym);
7787 else
7788 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7790 if (sym_count > max_sym_count)
7791 max_sym_count = sym_count;
7793 if (sym_count > max_sym_shndx_count
7794 && elf_symtab_shndx (sec->owner) != 0)
7795 max_sym_shndx_count = sym_count;
7797 if ((sec->flags & SEC_RELOC) != 0)
7799 size_t ext_size;
7801 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7802 if (ext_size > max_external_reloc_size)
7803 max_external_reloc_size = ext_size;
7804 if (sec->reloc_count > max_internal_reloc_count)
7805 max_internal_reloc_count = sec->reloc_count;
7810 if (reloc_count == 0)
7811 continue;
7813 o->reloc_count += reloc_count;
7815 /* MIPS may have a mix of REL and RELA relocs on sections.
7816 To support this curious ABI we keep reloc counts in
7817 elf_section_data too. We must be careful to add the
7818 relocations from the input section to the right output
7819 count. FIXME: Get rid of one count. We have
7820 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7821 rel_count1 = &esdo->rel_count;
7822 if (esdi != NULL)
7824 bfd_boolean same_size;
7825 bfd_size_type entsize1;
7827 entsize1 = esdi->rel_hdr.sh_entsize;
7828 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7829 || entsize1 == bed->s->sizeof_rela);
7830 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7832 if (!same_size)
7833 rel_count1 = &esdo->rel_count2;
7835 if (esdi->rel_hdr2 != NULL)
7837 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7838 unsigned int alt_count;
7839 unsigned int *rel_count2;
7841 BFD_ASSERT (entsize2 != entsize1
7842 && (entsize2 == bed->s->sizeof_rel
7843 || entsize2 == bed->s->sizeof_rela));
7845 rel_count2 = &esdo->rel_count2;
7846 if (!same_size)
7847 rel_count2 = &esdo->rel_count;
7849 /* The following is probably too simplistic if the
7850 backend counts output relocs unusually. */
7851 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7852 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7853 *rel_count2 += alt_count;
7854 reloc_count -= alt_count;
7857 *rel_count1 += reloc_count;
7860 if (o->reloc_count > 0)
7861 o->flags |= SEC_RELOC;
7862 else
7864 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7865 set it (this is probably a bug) and if it is set
7866 assign_section_numbers will create a reloc section. */
7867 o->flags &=~ SEC_RELOC;
7870 /* If the SEC_ALLOC flag is not set, force the section VMA to
7871 zero. This is done in elf_fake_sections as well, but forcing
7872 the VMA to 0 here will ensure that relocs against these
7873 sections are handled correctly. */
7874 if ((o->flags & SEC_ALLOC) == 0
7875 && ! o->user_set_vma)
7876 o->vma = 0;
7879 if (! info->relocatable && merged)
7880 elf_link_hash_traverse (elf_hash_table (info),
7881 _bfd_elf_link_sec_merge_syms, abfd);
7883 /* Figure out the file positions for everything but the symbol table
7884 and the relocs. We set symcount to force assign_section_numbers
7885 to create a symbol table. */
7886 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7887 BFD_ASSERT (! abfd->output_has_begun);
7888 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7889 goto error_return;
7891 /* Set sizes, and assign file positions for reloc sections. */
7892 for (o = abfd->sections; o != NULL; o = o->next)
7894 if ((o->flags & SEC_RELOC) != 0)
7896 if (!(_bfd_elf_link_size_reloc_section
7897 (abfd, &elf_section_data (o)->rel_hdr, o)))
7898 goto error_return;
7900 if (elf_section_data (o)->rel_hdr2
7901 && !(_bfd_elf_link_size_reloc_section
7902 (abfd, elf_section_data (o)->rel_hdr2, o)))
7903 goto error_return;
7906 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7907 to count upwards while actually outputting the relocations. */
7908 elf_section_data (o)->rel_count = 0;
7909 elf_section_data (o)->rel_count2 = 0;
7912 _bfd_elf_assign_file_positions_for_relocs (abfd);
7914 /* We have now assigned file positions for all the sections except
7915 .symtab and .strtab. We start the .symtab section at the current
7916 file position, and write directly to it. We build the .strtab
7917 section in memory. */
7918 bfd_get_symcount (abfd) = 0;
7919 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7920 /* sh_name is set in prep_headers. */
7921 symtab_hdr->sh_type = SHT_SYMTAB;
7922 /* sh_flags, sh_addr and sh_size all start off zero. */
7923 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7924 /* sh_link is set in assign_section_numbers. */
7925 /* sh_info is set below. */
7926 /* sh_offset is set just below. */
7927 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7929 off = elf_tdata (abfd)->next_file_pos;
7930 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7932 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7933 incorrect. We do not yet know the size of the .symtab section.
7934 We correct next_file_pos below, after we do know the size. */
7936 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7937 continuously seeking to the right position in the file. */
7938 if (! info->keep_memory || max_sym_count < 20)
7939 finfo.symbuf_size = 20;
7940 else
7941 finfo.symbuf_size = max_sym_count;
7942 amt = finfo.symbuf_size;
7943 amt *= bed->s->sizeof_sym;
7944 finfo.symbuf = bfd_malloc (amt);
7945 if (finfo.symbuf == NULL)
7946 goto error_return;
7947 if (elf_numsections (abfd) > SHN_LORESERVE)
7949 /* Wild guess at number of output symbols. realloc'd as needed. */
7950 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7951 finfo.shndxbuf_size = amt;
7952 amt *= sizeof (Elf_External_Sym_Shndx);
7953 finfo.symshndxbuf = bfd_zmalloc (amt);
7954 if (finfo.symshndxbuf == NULL)
7955 goto error_return;
7958 /* Start writing out the symbol table. The first symbol is always a
7959 dummy symbol. */
7960 if (info->strip != strip_all
7961 || emit_relocs)
7963 elfsym.st_value = 0;
7964 elfsym.st_size = 0;
7965 elfsym.st_info = 0;
7966 elfsym.st_other = 0;
7967 elfsym.st_shndx = SHN_UNDEF;
7968 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7969 NULL))
7970 goto error_return;
7973 /* Output a symbol for each section. We output these even if we are
7974 discarding local symbols, since they are used for relocs. These
7975 symbols have no names. We store the index of each one in the
7976 index field of the section, so that we can find it again when
7977 outputting relocs. */
7978 if (info->strip != strip_all
7979 || emit_relocs)
7981 elfsym.st_size = 0;
7982 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7983 elfsym.st_other = 0;
7984 for (i = 1; i < elf_numsections (abfd); i++)
7986 o = bfd_section_from_elf_index (abfd, i);
7987 if (o != NULL)
7988 o->target_index = bfd_get_symcount (abfd);
7989 elfsym.st_shndx = i;
7990 if (info->relocatable || o == NULL)
7991 elfsym.st_value = 0;
7992 else
7993 elfsym.st_value = o->vma;
7994 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7995 goto error_return;
7996 if (i == SHN_LORESERVE - 1)
7997 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8001 /* Allocate some memory to hold information read in from the input
8002 files. */
8003 if (max_contents_size != 0)
8005 finfo.contents = bfd_malloc (max_contents_size);
8006 if (finfo.contents == NULL)
8007 goto error_return;
8010 if (max_external_reloc_size != 0)
8012 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8013 if (finfo.external_relocs == NULL)
8014 goto error_return;
8017 if (max_internal_reloc_count != 0)
8019 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8020 amt *= sizeof (Elf_Internal_Rela);
8021 finfo.internal_relocs = bfd_malloc (amt);
8022 if (finfo.internal_relocs == NULL)
8023 goto error_return;
8026 if (max_sym_count != 0)
8028 amt = max_sym_count * bed->s->sizeof_sym;
8029 finfo.external_syms = bfd_malloc (amt);
8030 if (finfo.external_syms == NULL)
8031 goto error_return;
8033 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8034 finfo.internal_syms = bfd_malloc (amt);
8035 if (finfo.internal_syms == NULL)
8036 goto error_return;
8038 amt = max_sym_count * sizeof (long);
8039 finfo.indices = bfd_malloc (amt);
8040 if (finfo.indices == NULL)
8041 goto error_return;
8043 amt = max_sym_count * sizeof (asection *);
8044 finfo.sections = bfd_malloc (amt);
8045 if (finfo.sections == NULL)
8046 goto error_return;
8049 if (max_sym_shndx_count != 0)
8051 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8052 finfo.locsym_shndx = bfd_malloc (amt);
8053 if (finfo.locsym_shndx == NULL)
8054 goto error_return;
8057 if (elf_hash_table (info)->tls_sec)
8059 bfd_vma base, end = 0;
8060 asection *sec;
8062 for (sec = elf_hash_table (info)->tls_sec;
8063 sec && (sec->flags & SEC_THREAD_LOCAL);
8064 sec = sec->next)
8066 bfd_vma size = sec->size;
8068 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8070 struct bfd_link_order *o;
8072 for (o = sec->link_order_head; o != NULL; o = o->next)
8073 if (size < o->offset + o->size)
8074 size = o->offset + o->size;
8076 end = sec->vma + size;
8078 base = elf_hash_table (info)->tls_sec->vma;
8079 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8080 elf_hash_table (info)->tls_size = end - base;
8083 /* Reorder SHF_LINK_ORDER sections. */
8084 for (o = abfd->sections; o != NULL; o = o->next)
8086 if (!elf_fixup_link_order (abfd, o))
8087 return FALSE;
8090 /* Since ELF permits relocations to be against local symbols, we
8091 must have the local symbols available when we do the relocations.
8092 Since we would rather only read the local symbols once, and we
8093 would rather not keep them in memory, we handle all the
8094 relocations for a single input file at the same time.
8096 Unfortunately, there is no way to know the total number of local
8097 symbols until we have seen all of them, and the local symbol
8098 indices precede the global symbol indices. This means that when
8099 we are generating relocatable output, and we see a reloc against
8100 a global symbol, we can not know the symbol index until we have
8101 finished examining all the local symbols to see which ones we are
8102 going to output. To deal with this, we keep the relocations in
8103 memory, and don't output them until the end of the link. This is
8104 an unfortunate waste of memory, but I don't see a good way around
8105 it. Fortunately, it only happens when performing a relocatable
8106 link, which is not the common case. FIXME: If keep_memory is set
8107 we could write the relocs out and then read them again; I don't
8108 know how bad the memory loss will be. */
8110 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8111 sub->output_has_begun = FALSE;
8112 for (o = abfd->sections; o != NULL; o = o->next)
8114 for (p = o->link_order_head; p != NULL; p = p->next)
8116 if (p->type == bfd_indirect_link_order
8117 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8118 == bfd_target_elf_flavour)
8119 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8121 if (! sub->output_has_begun)
8123 if (! elf_link_input_bfd (&finfo, sub))
8124 goto error_return;
8125 sub->output_has_begun = TRUE;
8128 else if (p->type == bfd_section_reloc_link_order
8129 || p->type == bfd_symbol_reloc_link_order)
8131 if (! elf_reloc_link_order (abfd, info, o, p))
8132 goto error_return;
8134 else
8136 if (! _bfd_default_link_order (abfd, info, o, p))
8137 goto error_return;
8142 /* Output any global symbols that got converted to local in a
8143 version script or due to symbol visibility. We do this in a
8144 separate step since ELF requires all local symbols to appear
8145 prior to any global symbols. FIXME: We should only do this if
8146 some global symbols were, in fact, converted to become local.
8147 FIXME: Will this work correctly with the Irix 5 linker? */
8148 eoinfo.failed = FALSE;
8149 eoinfo.finfo = &finfo;
8150 eoinfo.localsyms = TRUE;
8151 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8152 &eoinfo);
8153 if (eoinfo.failed)
8154 return FALSE;
8156 /* That wrote out all the local symbols. Finish up the symbol table
8157 with the global symbols. Even if we want to strip everything we
8158 can, we still need to deal with those global symbols that got
8159 converted to local in a version script. */
8161 /* The sh_info field records the index of the first non local symbol. */
8162 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8164 if (dynamic
8165 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8167 Elf_Internal_Sym sym;
8168 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8169 long last_local = 0;
8171 /* Write out the section symbols for the output sections. */
8172 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8174 asection *s;
8176 sym.st_size = 0;
8177 sym.st_name = 0;
8178 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8179 sym.st_other = 0;
8181 for (s = abfd->sections; s != NULL; s = s->next)
8183 int indx;
8184 bfd_byte *dest;
8185 long dynindx;
8187 dynindx = elf_section_data (s)->dynindx;
8188 if (dynindx <= 0)
8189 continue;
8190 indx = elf_section_data (s)->this_idx;
8191 BFD_ASSERT (indx > 0);
8192 sym.st_shndx = indx;
8193 sym.st_value = s->vma;
8194 dest = dynsym + dynindx * bed->s->sizeof_sym;
8195 if (last_local < dynindx)
8196 last_local = dynindx;
8197 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8201 /* Write out the local dynsyms. */
8202 if (elf_hash_table (info)->dynlocal)
8204 struct elf_link_local_dynamic_entry *e;
8205 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8207 asection *s;
8208 bfd_byte *dest;
8210 sym.st_size = e->isym.st_size;
8211 sym.st_other = e->isym.st_other;
8213 /* Copy the internal symbol as is.
8214 Note that we saved a word of storage and overwrote
8215 the original st_name with the dynstr_index. */
8216 sym = e->isym;
8218 if (e->isym.st_shndx != SHN_UNDEF
8219 && (e->isym.st_shndx < SHN_LORESERVE
8220 || e->isym.st_shndx > SHN_HIRESERVE))
8222 s = bfd_section_from_elf_index (e->input_bfd,
8223 e->isym.st_shndx);
8225 sym.st_shndx =
8226 elf_section_data (s->output_section)->this_idx;
8227 sym.st_value = (s->output_section->vma
8228 + s->output_offset
8229 + e->isym.st_value);
8232 if (last_local < e->dynindx)
8233 last_local = e->dynindx;
8235 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8236 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8240 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8241 last_local + 1;
8244 /* We get the global symbols from the hash table. */
8245 eoinfo.failed = FALSE;
8246 eoinfo.localsyms = FALSE;
8247 eoinfo.finfo = &finfo;
8248 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8249 &eoinfo);
8250 if (eoinfo.failed)
8251 return FALSE;
8253 /* If backend needs to output some symbols not present in the hash
8254 table, do it now. */
8255 if (bed->elf_backend_output_arch_syms)
8257 typedef bfd_boolean (*out_sym_func)
8258 (void *, const char *, Elf_Internal_Sym *, asection *,
8259 struct elf_link_hash_entry *);
8261 if (! ((*bed->elf_backend_output_arch_syms)
8262 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8263 return FALSE;
8266 /* Flush all symbols to the file. */
8267 if (! elf_link_flush_output_syms (&finfo, bed))
8268 return FALSE;
8270 /* Now we know the size of the symtab section. */
8271 off += symtab_hdr->sh_size;
8273 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8274 if (symtab_shndx_hdr->sh_name != 0)
8276 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8277 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8278 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8279 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8280 symtab_shndx_hdr->sh_size = amt;
8282 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8283 off, TRUE);
8285 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8286 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8287 return FALSE;
8291 /* Finish up and write out the symbol string table (.strtab)
8292 section. */
8293 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8294 /* sh_name was set in prep_headers. */
8295 symstrtab_hdr->sh_type = SHT_STRTAB;
8296 symstrtab_hdr->sh_flags = 0;
8297 symstrtab_hdr->sh_addr = 0;
8298 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8299 symstrtab_hdr->sh_entsize = 0;
8300 symstrtab_hdr->sh_link = 0;
8301 symstrtab_hdr->sh_info = 0;
8302 /* sh_offset is set just below. */
8303 symstrtab_hdr->sh_addralign = 1;
8305 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8306 elf_tdata (abfd)->next_file_pos = off;
8308 if (bfd_get_symcount (abfd) > 0)
8310 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8311 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8312 return FALSE;
8315 /* Adjust the relocs to have the correct symbol indices. */
8316 for (o = abfd->sections; o != NULL; o = o->next)
8318 if ((o->flags & SEC_RELOC) == 0)
8319 continue;
8321 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8322 elf_section_data (o)->rel_count,
8323 elf_section_data (o)->rel_hashes);
8324 if (elf_section_data (o)->rel_hdr2 != NULL)
8325 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8326 elf_section_data (o)->rel_count2,
8327 (elf_section_data (o)->rel_hashes
8328 + elf_section_data (o)->rel_count));
8330 /* Set the reloc_count field to 0 to prevent write_relocs from
8331 trying to swap the relocs out itself. */
8332 o->reloc_count = 0;
8335 if (dynamic && info->combreloc && dynobj != NULL)
8336 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8338 /* If we are linking against a dynamic object, or generating a
8339 shared library, finish up the dynamic linking information. */
8340 if (dynamic)
8342 bfd_byte *dyncon, *dynconend;
8344 /* Fix up .dynamic entries. */
8345 o = bfd_get_section_by_name (dynobj, ".dynamic");
8346 BFD_ASSERT (o != NULL);
8348 dyncon = o->contents;
8349 dynconend = o->contents + o->size;
8350 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8352 Elf_Internal_Dyn dyn;
8353 const char *name;
8354 unsigned int type;
8356 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8358 switch (dyn.d_tag)
8360 default:
8361 continue;
8362 case DT_NULL:
8363 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8365 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8367 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8368 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8369 default: continue;
8371 dyn.d_un.d_val = relativecount;
8372 relativecount = 0;
8373 break;
8375 continue;
8377 case DT_INIT:
8378 name = info->init_function;
8379 goto get_sym;
8380 case DT_FINI:
8381 name = info->fini_function;
8382 get_sym:
8384 struct elf_link_hash_entry *h;
8386 h = elf_link_hash_lookup (elf_hash_table (info), name,
8387 FALSE, FALSE, TRUE);
8388 if (h != NULL
8389 && (h->root.type == bfd_link_hash_defined
8390 || h->root.type == bfd_link_hash_defweak))
8392 dyn.d_un.d_val = h->root.u.def.value;
8393 o = h->root.u.def.section;
8394 if (o->output_section != NULL)
8395 dyn.d_un.d_val += (o->output_section->vma
8396 + o->output_offset);
8397 else
8399 /* The symbol is imported from another shared
8400 library and does not apply to this one. */
8401 dyn.d_un.d_val = 0;
8403 break;
8406 continue;
8408 case DT_PREINIT_ARRAYSZ:
8409 name = ".preinit_array";
8410 goto get_size;
8411 case DT_INIT_ARRAYSZ:
8412 name = ".init_array";
8413 goto get_size;
8414 case DT_FINI_ARRAYSZ:
8415 name = ".fini_array";
8416 get_size:
8417 o = bfd_get_section_by_name (abfd, name);
8418 if (o == NULL)
8420 (*_bfd_error_handler)
8421 (_("%B: could not find output section %s"), abfd, name);
8422 goto error_return;
8424 if (o->size == 0)
8425 (*_bfd_error_handler)
8426 (_("warning: %s section has zero size"), name);
8427 dyn.d_un.d_val = o->size;
8428 break;
8430 case DT_PREINIT_ARRAY:
8431 name = ".preinit_array";
8432 goto get_vma;
8433 case DT_INIT_ARRAY:
8434 name = ".init_array";
8435 goto get_vma;
8436 case DT_FINI_ARRAY:
8437 name = ".fini_array";
8438 goto get_vma;
8440 case DT_HASH:
8441 name = ".hash";
8442 goto get_vma;
8443 case DT_STRTAB:
8444 name = ".dynstr";
8445 goto get_vma;
8446 case DT_SYMTAB:
8447 name = ".dynsym";
8448 goto get_vma;
8449 case DT_VERDEF:
8450 name = ".gnu.version_d";
8451 goto get_vma;
8452 case DT_VERNEED:
8453 name = ".gnu.version_r";
8454 goto get_vma;
8455 case DT_VERSYM:
8456 name = ".gnu.version";
8457 get_vma:
8458 o = bfd_get_section_by_name (abfd, name);
8459 if (o == NULL)
8461 (*_bfd_error_handler)
8462 (_("%B: could not find output section %s"), abfd, name);
8463 goto error_return;
8465 dyn.d_un.d_ptr = o->vma;
8466 break;
8468 case DT_REL:
8469 case DT_RELA:
8470 case DT_RELSZ:
8471 case DT_RELASZ:
8472 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8473 type = SHT_REL;
8474 else
8475 type = SHT_RELA;
8476 dyn.d_un.d_val = 0;
8477 for (i = 1; i < elf_numsections (abfd); i++)
8479 Elf_Internal_Shdr *hdr;
8481 hdr = elf_elfsections (abfd)[i];
8482 if (hdr->sh_type == type
8483 && (hdr->sh_flags & SHF_ALLOC) != 0)
8485 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8486 dyn.d_un.d_val += hdr->sh_size;
8487 else
8489 if (dyn.d_un.d_val == 0
8490 || hdr->sh_addr < dyn.d_un.d_val)
8491 dyn.d_un.d_val = hdr->sh_addr;
8495 break;
8497 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8501 /* If we have created any dynamic sections, then output them. */
8502 if (dynobj != NULL)
8504 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8505 goto error_return;
8507 for (o = dynobj->sections; o != NULL; o = o->next)
8509 if ((o->flags & SEC_HAS_CONTENTS) == 0
8510 || o->size == 0
8511 || o->output_section == bfd_abs_section_ptr)
8512 continue;
8513 if ((o->flags & SEC_LINKER_CREATED) == 0)
8515 /* At this point, we are only interested in sections
8516 created by _bfd_elf_link_create_dynamic_sections. */
8517 continue;
8519 if (elf_hash_table (info)->stab_info.stabstr == o)
8520 continue;
8521 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8522 continue;
8523 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8524 != SHT_STRTAB)
8525 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8527 if (! bfd_set_section_contents (abfd, o->output_section,
8528 o->contents,
8529 (file_ptr) o->output_offset,
8530 o->size))
8531 goto error_return;
8533 else
8535 /* The contents of the .dynstr section are actually in a
8536 stringtab. */
8537 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8538 if (bfd_seek (abfd, off, SEEK_SET) != 0
8539 || ! _bfd_elf_strtab_emit (abfd,
8540 elf_hash_table (info)->dynstr))
8541 goto error_return;
8546 if (info->relocatable)
8548 bfd_boolean failed = FALSE;
8550 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8551 if (failed)
8552 goto error_return;
8555 /* If we have optimized stabs strings, output them. */
8556 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8558 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8559 goto error_return;
8562 if (info->eh_frame_hdr)
8564 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8565 goto error_return;
8568 if (finfo.symstrtab != NULL)
8569 _bfd_stringtab_free (finfo.symstrtab);
8570 if (finfo.contents != NULL)
8571 free (finfo.contents);
8572 if (finfo.external_relocs != NULL)
8573 free (finfo.external_relocs);
8574 if (finfo.internal_relocs != NULL)
8575 free (finfo.internal_relocs);
8576 if (finfo.external_syms != NULL)
8577 free (finfo.external_syms);
8578 if (finfo.locsym_shndx != NULL)
8579 free (finfo.locsym_shndx);
8580 if (finfo.internal_syms != NULL)
8581 free (finfo.internal_syms);
8582 if (finfo.indices != NULL)
8583 free (finfo.indices);
8584 if (finfo.sections != NULL)
8585 free (finfo.sections);
8586 if (finfo.symbuf != NULL)
8587 free (finfo.symbuf);
8588 if (finfo.symshndxbuf != NULL)
8589 free (finfo.symshndxbuf);
8590 for (o = abfd->sections; o != NULL; o = o->next)
8592 if ((o->flags & SEC_RELOC) != 0
8593 && elf_section_data (o)->rel_hashes != NULL)
8594 free (elf_section_data (o)->rel_hashes);
8597 elf_tdata (abfd)->linker = TRUE;
8599 return TRUE;
8601 error_return:
8602 if (finfo.symstrtab != NULL)
8603 _bfd_stringtab_free (finfo.symstrtab);
8604 if (finfo.contents != NULL)
8605 free (finfo.contents);
8606 if (finfo.external_relocs != NULL)
8607 free (finfo.external_relocs);
8608 if (finfo.internal_relocs != NULL)
8609 free (finfo.internal_relocs);
8610 if (finfo.external_syms != NULL)
8611 free (finfo.external_syms);
8612 if (finfo.locsym_shndx != NULL)
8613 free (finfo.locsym_shndx);
8614 if (finfo.internal_syms != NULL)
8615 free (finfo.internal_syms);
8616 if (finfo.indices != NULL)
8617 free (finfo.indices);
8618 if (finfo.sections != NULL)
8619 free (finfo.sections);
8620 if (finfo.symbuf != NULL)
8621 free (finfo.symbuf);
8622 if (finfo.symshndxbuf != NULL)
8623 free (finfo.symshndxbuf);
8624 for (o = abfd->sections; o != NULL; o = o->next)
8626 if ((o->flags & SEC_RELOC) != 0
8627 && elf_section_data (o)->rel_hashes != NULL)
8628 free (elf_section_data (o)->rel_hashes);
8631 return FALSE;
8634 /* Garbage collect unused sections. */
8636 /* The mark phase of garbage collection. For a given section, mark
8637 it and any sections in this section's group, and all the sections
8638 which define symbols to which it refers. */
8640 typedef asection * (*gc_mark_hook_fn)
8641 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8642 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8644 bfd_boolean
8645 _bfd_elf_gc_mark (struct bfd_link_info *info,
8646 asection *sec,
8647 gc_mark_hook_fn gc_mark_hook)
8649 bfd_boolean ret;
8650 asection *group_sec;
8652 sec->gc_mark = 1;
8654 /* Mark all the sections in the group. */
8655 group_sec = elf_section_data (sec)->next_in_group;
8656 if (group_sec && !group_sec->gc_mark)
8657 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8658 return FALSE;
8660 /* Look through the section relocs. */
8661 ret = TRUE;
8662 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8664 Elf_Internal_Rela *relstart, *rel, *relend;
8665 Elf_Internal_Shdr *symtab_hdr;
8666 struct elf_link_hash_entry **sym_hashes;
8667 size_t nlocsyms;
8668 size_t extsymoff;
8669 bfd *input_bfd = sec->owner;
8670 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8671 Elf_Internal_Sym *isym = NULL;
8672 int r_sym_shift;
8674 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8675 sym_hashes = elf_sym_hashes (input_bfd);
8677 /* Read the local symbols. */
8678 if (elf_bad_symtab (input_bfd))
8680 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8681 extsymoff = 0;
8683 else
8684 extsymoff = nlocsyms = symtab_hdr->sh_info;
8686 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8687 if (isym == NULL && nlocsyms != 0)
8689 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8690 NULL, NULL, NULL);
8691 if (isym == NULL)
8692 return FALSE;
8695 /* Read the relocations. */
8696 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8697 info->keep_memory);
8698 if (relstart == NULL)
8700 ret = FALSE;
8701 goto out1;
8703 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8705 if (bed->s->arch_size == 32)
8706 r_sym_shift = 8;
8707 else
8708 r_sym_shift = 32;
8710 for (rel = relstart; rel < relend; rel++)
8712 unsigned long r_symndx;
8713 asection *rsec;
8714 struct elf_link_hash_entry *h;
8716 r_symndx = rel->r_info >> r_sym_shift;
8717 if (r_symndx == 0)
8718 continue;
8720 if (r_symndx >= nlocsyms
8721 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8723 h = sym_hashes[r_symndx - extsymoff];
8724 while (h->root.type == bfd_link_hash_indirect
8725 || h->root.type == bfd_link_hash_warning)
8726 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8727 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8729 else
8731 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8734 if (rsec && !rsec->gc_mark)
8736 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8737 rsec->gc_mark = 1;
8738 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8740 ret = FALSE;
8741 goto out2;
8746 out2:
8747 if (elf_section_data (sec)->relocs != relstart)
8748 free (relstart);
8749 out1:
8750 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8752 if (! info->keep_memory)
8753 free (isym);
8754 else
8755 symtab_hdr->contents = (unsigned char *) isym;
8759 return ret;
8762 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8764 static bfd_boolean
8765 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8767 int *idx = idxptr;
8769 if (h->root.type == bfd_link_hash_warning)
8770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8772 if (h->dynindx != -1
8773 && ((h->root.type != bfd_link_hash_defined
8774 && h->root.type != bfd_link_hash_defweak)
8775 || h->root.u.def.section->gc_mark))
8776 h->dynindx = (*idx)++;
8778 return TRUE;
8781 /* The sweep phase of garbage collection. Remove all garbage sections. */
8783 typedef bfd_boolean (*gc_sweep_hook_fn)
8784 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8786 static bfd_boolean
8787 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8789 bfd *sub;
8791 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8793 asection *o;
8795 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8796 continue;
8798 for (o = sub->sections; o != NULL; o = o->next)
8800 /* Keep debug and special sections. */
8801 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8802 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8803 o->gc_mark = 1;
8805 if (o->gc_mark)
8806 continue;
8808 /* Skip sweeping sections already excluded. */
8809 if (o->flags & SEC_EXCLUDE)
8810 continue;
8812 /* Since this is early in the link process, it is simple
8813 to remove a section from the output. */
8814 o->flags |= SEC_EXCLUDE;
8816 /* But we also have to update some of the relocation
8817 info we collected before. */
8818 if (gc_sweep_hook
8819 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8821 Elf_Internal_Rela *internal_relocs;
8822 bfd_boolean r;
8824 internal_relocs
8825 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8826 info->keep_memory);
8827 if (internal_relocs == NULL)
8828 return FALSE;
8830 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8832 if (elf_section_data (o)->relocs != internal_relocs)
8833 free (internal_relocs);
8835 if (!r)
8836 return FALSE;
8841 /* Remove the symbols that were in the swept sections from the dynamic
8842 symbol table. GCFIXME: Anyone know how to get them out of the
8843 static symbol table as well? */
8845 int i = 0;
8847 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8849 elf_hash_table (info)->dynsymcount = i;
8852 return TRUE;
8855 /* Propagate collected vtable information. This is called through
8856 elf_link_hash_traverse. */
8858 static bfd_boolean
8859 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8861 if (h->root.type == bfd_link_hash_warning)
8862 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8864 /* Those that are not vtables. */
8865 if (h->vtable == NULL || h->vtable->parent == NULL)
8866 return TRUE;
8868 /* Those vtables that do not have parents, we cannot merge. */
8869 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8870 return TRUE;
8872 /* If we've already been done, exit. */
8873 if (h->vtable->used && h->vtable->used[-1])
8874 return TRUE;
8876 /* Make sure the parent's table is up to date. */
8877 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8879 if (h->vtable->used == NULL)
8881 /* None of this table's entries were referenced. Re-use the
8882 parent's table. */
8883 h->vtable->used = h->vtable->parent->vtable->used;
8884 h->vtable->size = h->vtable->parent->vtable->size;
8886 else
8888 size_t n;
8889 bfd_boolean *cu, *pu;
8891 /* Or the parent's entries into ours. */
8892 cu = h->vtable->used;
8893 cu[-1] = TRUE;
8894 pu = h->vtable->parent->vtable->used;
8895 if (pu != NULL)
8897 const struct elf_backend_data *bed;
8898 unsigned int log_file_align;
8900 bed = get_elf_backend_data (h->root.u.def.section->owner);
8901 log_file_align = bed->s->log_file_align;
8902 n = h->vtable->parent->vtable->size >> log_file_align;
8903 while (n--)
8905 if (*pu)
8906 *cu = TRUE;
8907 pu++;
8908 cu++;
8913 return TRUE;
8916 static bfd_boolean
8917 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8919 asection *sec;
8920 bfd_vma hstart, hend;
8921 Elf_Internal_Rela *relstart, *relend, *rel;
8922 const struct elf_backend_data *bed;
8923 unsigned int log_file_align;
8925 if (h->root.type == bfd_link_hash_warning)
8926 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8928 /* Take care of both those symbols that do not describe vtables as
8929 well as those that are not loaded. */
8930 if (h->vtable == NULL || h->vtable->parent == NULL)
8931 return TRUE;
8933 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8934 || h->root.type == bfd_link_hash_defweak);
8936 sec = h->root.u.def.section;
8937 hstart = h->root.u.def.value;
8938 hend = hstart + h->size;
8940 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8941 if (!relstart)
8942 return *(bfd_boolean *) okp = FALSE;
8943 bed = get_elf_backend_data (sec->owner);
8944 log_file_align = bed->s->log_file_align;
8946 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8948 for (rel = relstart; rel < relend; ++rel)
8949 if (rel->r_offset >= hstart && rel->r_offset < hend)
8951 /* If the entry is in use, do nothing. */
8952 if (h->vtable->used
8953 && (rel->r_offset - hstart) < h->vtable->size)
8955 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8956 if (h->vtable->used[entry])
8957 continue;
8959 /* Otherwise, kill it. */
8960 rel->r_offset = rel->r_info = rel->r_addend = 0;
8963 return TRUE;
8966 /* Mark sections containing dynamically referenced symbols. This is called
8967 through elf_link_hash_traverse. */
8969 static bfd_boolean
8970 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8971 void *okp ATTRIBUTE_UNUSED)
8973 if (h->root.type == bfd_link_hash_warning)
8974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8976 if ((h->root.type == bfd_link_hash_defined
8977 || h->root.type == bfd_link_hash_defweak)
8978 && h->ref_dynamic)
8979 h->root.u.def.section->flags |= SEC_KEEP;
8981 return TRUE;
8984 /* Do mark and sweep of unused sections. */
8986 bfd_boolean
8987 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8989 bfd_boolean ok = TRUE;
8990 bfd *sub;
8991 asection * (*gc_mark_hook)
8992 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8993 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8995 if (!get_elf_backend_data (abfd)->can_gc_sections
8996 || info->relocatable
8997 || info->emitrelocations
8998 || info->shared
8999 || !is_elf_hash_table (info->hash))
9001 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9002 return TRUE;
9005 /* Apply transitive closure to the vtable entry usage info. */
9006 elf_link_hash_traverse (elf_hash_table (info),
9007 elf_gc_propagate_vtable_entries_used,
9008 &ok);
9009 if (!ok)
9010 return FALSE;
9012 /* Kill the vtable relocations that were not used. */
9013 elf_link_hash_traverse (elf_hash_table (info),
9014 elf_gc_smash_unused_vtentry_relocs,
9015 &ok);
9016 if (!ok)
9017 return FALSE;
9019 /* Mark dynamically referenced symbols. */
9020 if (elf_hash_table (info)->dynamic_sections_created)
9021 elf_link_hash_traverse (elf_hash_table (info),
9022 elf_gc_mark_dynamic_ref_symbol,
9023 &ok);
9024 if (!ok)
9025 return FALSE;
9027 /* Grovel through relocs to find out who stays ... */
9028 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9029 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9031 asection *o;
9033 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9034 continue;
9036 for (o = sub->sections; o != NULL; o = o->next)
9038 if (o->flags & SEC_KEEP)
9040 /* _bfd_elf_discard_section_eh_frame knows how to discard
9041 orphaned FDEs so don't mark sections referenced by the
9042 EH frame section. */
9043 if (strcmp (o->name, ".eh_frame") == 0)
9044 o->gc_mark = 1;
9045 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9046 return FALSE;
9051 /* ... and mark SEC_EXCLUDE for those that go. */
9052 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9053 return FALSE;
9055 return TRUE;
9058 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9060 bfd_boolean
9061 bfd_elf_gc_record_vtinherit (bfd *abfd,
9062 asection *sec,
9063 struct elf_link_hash_entry *h,
9064 bfd_vma offset)
9066 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9067 struct elf_link_hash_entry **search, *child;
9068 bfd_size_type extsymcount;
9069 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9071 /* The sh_info field of the symtab header tells us where the
9072 external symbols start. We don't care about the local symbols at
9073 this point. */
9074 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9075 if (!elf_bad_symtab (abfd))
9076 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9078 sym_hashes = elf_sym_hashes (abfd);
9079 sym_hashes_end = sym_hashes + extsymcount;
9081 /* Hunt down the child symbol, which is in this section at the same
9082 offset as the relocation. */
9083 for (search = sym_hashes; search != sym_hashes_end; ++search)
9085 if ((child = *search) != NULL
9086 && (child->root.type == bfd_link_hash_defined
9087 || child->root.type == bfd_link_hash_defweak)
9088 && child->root.u.def.section == sec
9089 && child->root.u.def.value == offset)
9090 goto win;
9093 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9094 abfd, sec, (unsigned long) offset);
9095 bfd_set_error (bfd_error_invalid_operation);
9096 return FALSE;
9098 win:
9099 if (!child->vtable)
9101 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9102 if (!child->vtable)
9103 return FALSE;
9105 if (!h)
9107 /* This *should* only be the absolute section. It could potentially
9108 be that someone has defined a non-global vtable though, which
9109 would be bad. It isn't worth paging in the local symbols to be
9110 sure though; that case should simply be handled by the assembler. */
9112 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9114 else
9115 child->vtable->parent = h;
9117 return TRUE;
9120 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9122 bfd_boolean
9123 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9124 asection *sec ATTRIBUTE_UNUSED,
9125 struct elf_link_hash_entry *h,
9126 bfd_vma addend)
9128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9129 unsigned int log_file_align = bed->s->log_file_align;
9131 if (!h->vtable)
9133 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9134 if (!h->vtable)
9135 return FALSE;
9138 if (addend >= h->vtable->size)
9140 size_t size, bytes, file_align;
9141 bfd_boolean *ptr = h->vtable->used;
9143 /* While the symbol is undefined, we have to be prepared to handle
9144 a zero size. */
9145 file_align = 1 << log_file_align;
9146 if (h->root.type == bfd_link_hash_undefined)
9147 size = addend + file_align;
9148 else
9150 size = h->size;
9151 if (addend >= size)
9153 /* Oops! We've got a reference past the defined end of
9154 the table. This is probably a bug -- shall we warn? */
9155 size = addend + file_align;
9158 size = (size + file_align - 1) & -file_align;
9160 /* Allocate one extra entry for use as a "done" flag for the
9161 consolidation pass. */
9162 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9164 if (ptr)
9166 ptr = bfd_realloc (ptr - 1, bytes);
9168 if (ptr != NULL)
9170 size_t oldbytes;
9172 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9173 * sizeof (bfd_boolean));
9174 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9177 else
9178 ptr = bfd_zmalloc (bytes);
9180 if (ptr == NULL)
9181 return FALSE;
9183 /* And arrange for that done flag to be at index -1. */
9184 h->vtable->used = ptr + 1;
9185 h->vtable->size = size;
9188 h->vtable->used[addend >> log_file_align] = TRUE;
9190 return TRUE;
9193 struct alloc_got_off_arg {
9194 bfd_vma gotoff;
9195 unsigned int got_elt_size;
9198 /* We need a special top-level link routine to convert got reference counts
9199 to real got offsets. */
9201 static bfd_boolean
9202 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9204 struct alloc_got_off_arg *gofarg = arg;
9206 if (h->root.type == bfd_link_hash_warning)
9207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9209 if (h->got.refcount > 0)
9211 h->got.offset = gofarg->gotoff;
9212 gofarg->gotoff += gofarg->got_elt_size;
9214 else
9215 h->got.offset = (bfd_vma) -1;
9217 return TRUE;
9220 /* And an accompanying bit to work out final got entry offsets once
9221 we're done. Should be called from final_link. */
9223 bfd_boolean
9224 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9225 struct bfd_link_info *info)
9227 bfd *i;
9228 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9229 bfd_vma gotoff;
9230 unsigned int got_elt_size = bed->s->arch_size / 8;
9231 struct alloc_got_off_arg gofarg;
9233 if (! is_elf_hash_table (info->hash))
9234 return FALSE;
9236 /* The GOT offset is relative to the .got section, but the GOT header is
9237 put into the .got.plt section, if the backend uses it. */
9238 if (bed->want_got_plt)
9239 gotoff = 0;
9240 else
9241 gotoff = bed->got_header_size;
9243 /* Do the local .got entries first. */
9244 for (i = info->input_bfds; i; i = i->link_next)
9246 bfd_signed_vma *local_got;
9247 bfd_size_type j, locsymcount;
9248 Elf_Internal_Shdr *symtab_hdr;
9250 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9251 continue;
9253 local_got = elf_local_got_refcounts (i);
9254 if (!local_got)
9255 continue;
9257 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9258 if (elf_bad_symtab (i))
9259 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9260 else
9261 locsymcount = symtab_hdr->sh_info;
9263 for (j = 0; j < locsymcount; ++j)
9265 if (local_got[j] > 0)
9267 local_got[j] = gotoff;
9268 gotoff += got_elt_size;
9270 else
9271 local_got[j] = (bfd_vma) -1;
9275 /* Then the global .got entries. .plt refcounts are handled by
9276 adjust_dynamic_symbol */
9277 gofarg.gotoff = gotoff;
9278 gofarg.got_elt_size = got_elt_size;
9279 elf_link_hash_traverse (elf_hash_table (info),
9280 elf_gc_allocate_got_offsets,
9281 &gofarg);
9282 return TRUE;
9285 /* Many folk need no more in the way of final link than this, once
9286 got entry reference counting is enabled. */
9288 bfd_boolean
9289 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9291 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9292 return FALSE;
9294 /* Invoke the regular ELF backend linker to do all the work. */
9295 return bfd_elf_final_link (abfd, info);
9298 bfd_boolean
9299 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9301 struct elf_reloc_cookie *rcookie = cookie;
9303 if (rcookie->bad_symtab)
9304 rcookie->rel = rcookie->rels;
9306 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9308 unsigned long r_symndx;
9310 if (! rcookie->bad_symtab)
9311 if (rcookie->rel->r_offset > offset)
9312 return FALSE;
9313 if (rcookie->rel->r_offset != offset)
9314 continue;
9316 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9317 if (r_symndx == SHN_UNDEF)
9318 return TRUE;
9320 if (r_symndx >= rcookie->locsymcount
9321 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9323 struct elf_link_hash_entry *h;
9325 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9327 while (h->root.type == bfd_link_hash_indirect
9328 || h->root.type == bfd_link_hash_warning)
9329 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9331 if ((h->root.type == bfd_link_hash_defined
9332 || h->root.type == bfd_link_hash_defweak)
9333 && elf_discarded_section (h->root.u.def.section))
9334 return TRUE;
9335 else
9336 return FALSE;
9338 else
9340 /* It's not a relocation against a global symbol,
9341 but it could be a relocation against a local
9342 symbol for a discarded section. */
9343 asection *isec;
9344 Elf_Internal_Sym *isym;
9346 /* Need to: get the symbol; get the section. */
9347 isym = &rcookie->locsyms[r_symndx];
9348 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9350 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9351 if (isec != NULL && elf_discarded_section (isec))
9352 return TRUE;
9355 return FALSE;
9357 return FALSE;
9360 /* Discard unneeded references to discarded sections.
9361 Returns TRUE if any section's size was changed. */
9362 /* This function assumes that the relocations are in sorted order,
9363 which is true for all known assemblers. */
9365 bfd_boolean
9366 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9368 struct elf_reloc_cookie cookie;
9369 asection *stab, *eh;
9370 Elf_Internal_Shdr *symtab_hdr;
9371 const struct elf_backend_data *bed;
9372 bfd *abfd;
9373 unsigned int count;
9374 bfd_boolean ret = FALSE;
9376 if (info->traditional_format
9377 || !is_elf_hash_table (info->hash))
9378 return FALSE;
9380 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9382 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9383 continue;
9385 bed = get_elf_backend_data (abfd);
9387 if ((abfd->flags & DYNAMIC) != 0)
9388 continue;
9390 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9391 if (info->relocatable
9392 || (eh != NULL
9393 && (eh->size == 0
9394 || bfd_is_abs_section (eh->output_section))))
9395 eh = NULL;
9397 stab = bfd_get_section_by_name (abfd, ".stab");
9398 if (stab != NULL
9399 && (stab->size == 0
9400 || bfd_is_abs_section (stab->output_section)
9401 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9402 stab = NULL;
9404 if (stab == NULL
9405 && eh == NULL
9406 && bed->elf_backend_discard_info == NULL)
9407 continue;
9409 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9410 cookie.abfd = abfd;
9411 cookie.sym_hashes = elf_sym_hashes (abfd);
9412 cookie.bad_symtab = elf_bad_symtab (abfd);
9413 if (cookie.bad_symtab)
9415 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9416 cookie.extsymoff = 0;
9418 else
9420 cookie.locsymcount = symtab_hdr->sh_info;
9421 cookie.extsymoff = symtab_hdr->sh_info;
9424 if (bed->s->arch_size == 32)
9425 cookie.r_sym_shift = 8;
9426 else
9427 cookie.r_sym_shift = 32;
9429 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9430 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9432 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9433 cookie.locsymcount, 0,
9434 NULL, NULL, NULL);
9435 if (cookie.locsyms == NULL)
9436 return FALSE;
9439 if (stab != NULL)
9441 cookie.rels = NULL;
9442 count = stab->reloc_count;
9443 if (count != 0)
9444 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9445 info->keep_memory);
9446 if (cookie.rels != NULL)
9448 cookie.rel = cookie.rels;
9449 cookie.relend = cookie.rels;
9450 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9451 if (_bfd_discard_section_stabs (abfd, stab,
9452 elf_section_data (stab)->sec_info,
9453 bfd_elf_reloc_symbol_deleted_p,
9454 &cookie))
9455 ret = TRUE;
9456 if (elf_section_data (stab)->relocs != cookie.rels)
9457 free (cookie.rels);
9461 if (eh != NULL)
9463 cookie.rels = NULL;
9464 count = eh->reloc_count;
9465 if (count != 0)
9466 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9467 info->keep_memory);
9468 cookie.rel = cookie.rels;
9469 cookie.relend = cookie.rels;
9470 if (cookie.rels != NULL)
9471 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9473 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9474 bfd_elf_reloc_symbol_deleted_p,
9475 &cookie))
9476 ret = TRUE;
9478 if (cookie.rels != NULL
9479 && elf_section_data (eh)->relocs != cookie.rels)
9480 free (cookie.rels);
9483 if (bed->elf_backend_discard_info != NULL
9484 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9485 ret = TRUE;
9487 if (cookie.locsyms != NULL
9488 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9490 if (! info->keep_memory)
9491 free (cookie.locsyms);
9492 else
9493 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9497 if (info->eh_frame_hdr
9498 && !info->relocatable
9499 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9500 ret = TRUE;
9502 return ret;
9505 void
9506 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9508 flagword flags;
9509 const char *name, *p;
9510 struct bfd_section_already_linked *l;
9511 struct bfd_section_already_linked_hash_entry *already_linked_list;
9512 asection *group;
9514 /* A single member comdat group section may be discarded by a
9515 linkonce section. See below. */
9516 if (sec->output_section == bfd_abs_section_ptr)
9517 return;
9519 flags = sec->flags;
9521 /* Check if it belongs to a section group. */
9522 group = elf_sec_group (sec);
9524 /* Return if it isn't a linkonce section nor a member of a group. A
9525 comdat group section also has SEC_LINK_ONCE set. */
9526 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9527 return;
9529 if (group)
9531 /* If this is the member of a single member comdat group, check if
9532 the group should be discarded. */
9533 if (elf_next_in_group (sec) == sec
9534 && (group->flags & SEC_LINK_ONCE) != 0)
9535 sec = group;
9536 else
9537 return;
9540 /* FIXME: When doing a relocatable link, we may have trouble
9541 copying relocations in other sections that refer to local symbols
9542 in the section being discarded. Those relocations will have to
9543 be converted somehow; as of this writing I'm not sure that any of
9544 the backends handle that correctly.
9546 It is tempting to instead not discard link once sections when
9547 doing a relocatable link (technically, they should be discarded
9548 whenever we are building constructors). However, that fails,
9549 because the linker winds up combining all the link once sections
9550 into a single large link once section, which defeats the purpose
9551 of having link once sections in the first place.
9553 Also, not merging link once sections in a relocatable link
9554 causes trouble for MIPS ELF, which relies on link once semantics
9555 to handle the .reginfo section correctly. */
9557 name = bfd_get_section_name (abfd, sec);
9559 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9560 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9561 p++;
9562 else
9563 p = name;
9565 already_linked_list = bfd_section_already_linked_table_lookup (p);
9567 for (l = already_linked_list->entry; l != NULL; l = l->next)
9569 /* We may have 3 different sections on the list: group section,
9570 comdat section and linkonce section. SEC may be a linkonce or
9571 group section. We match a group section with a group section,
9572 a linkonce section with a linkonce section, and ignore comdat
9573 section. */
9574 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9575 && strcmp (name, l->sec->name) == 0
9576 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9578 /* The section has already been linked. See if we should
9579 issue a warning. */
9580 switch (flags & SEC_LINK_DUPLICATES)
9582 default:
9583 abort ();
9585 case SEC_LINK_DUPLICATES_DISCARD:
9586 break;
9588 case SEC_LINK_DUPLICATES_ONE_ONLY:
9589 (*_bfd_error_handler)
9590 (_("%B: ignoring duplicate section `%A'\n"),
9591 abfd, sec);
9592 break;
9594 case SEC_LINK_DUPLICATES_SAME_SIZE:
9595 if (sec->size != l->sec->size)
9596 (*_bfd_error_handler)
9597 (_("%B: duplicate section `%A' has different size\n"),
9598 abfd, sec);
9599 break;
9601 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9602 if (sec->size != l->sec->size)
9603 (*_bfd_error_handler)
9604 (_("%B: duplicate section `%A' has different size\n"),
9605 abfd, sec);
9606 else if (sec->size != 0)
9608 bfd_byte *sec_contents, *l_sec_contents;
9610 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9611 (*_bfd_error_handler)
9612 (_("%B: warning: could not read contents of section `%A'\n"),
9613 abfd, sec);
9614 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9615 &l_sec_contents))
9616 (*_bfd_error_handler)
9617 (_("%B: warning: could not read contents of section `%A'\n"),
9618 l->sec->owner, l->sec);
9619 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9620 (*_bfd_error_handler)
9621 (_("%B: warning: duplicate section `%A' has different contents\n"),
9622 abfd, sec);
9624 if (sec_contents)
9625 free (sec_contents);
9626 if (l_sec_contents)
9627 free (l_sec_contents);
9629 break;
9632 /* Set the output_section field so that lang_add_section
9633 does not create a lang_input_section structure for this
9634 section. Since there might be a symbol in the section
9635 being discarded, we must retain a pointer to the section
9636 which we are really going to use. */
9637 sec->output_section = bfd_abs_section_ptr;
9638 sec->kept_section = l->sec;
9640 if (flags & SEC_GROUP)
9642 asection *first = elf_next_in_group (sec);
9643 asection *s = first;
9645 while (s != NULL)
9647 s->output_section = bfd_abs_section_ptr;
9648 /* Record which group discards it. */
9649 s->kept_section = l->sec;
9650 s = elf_next_in_group (s);
9651 /* These lists are circular. */
9652 if (s == first)
9653 break;
9657 return;
9661 if (group)
9663 /* If this is the member of a single member comdat group and the
9664 group hasn't be discarded, we check if it matches a linkonce
9665 section. We only record the discarded comdat group. Otherwise
9666 the undiscarded group will be discarded incorrectly later since
9667 itself has been recorded. */
9668 for (l = already_linked_list->entry; l != NULL; l = l->next)
9669 if ((l->sec->flags & SEC_GROUP) == 0
9670 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9671 && bfd_elf_match_symbols_in_sections (l->sec,
9672 elf_next_in_group (sec)))
9674 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9675 elf_next_in_group (sec)->kept_section = l->sec;
9676 group->output_section = bfd_abs_section_ptr;
9677 break;
9679 if (l == NULL)
9680 return;
9682 else
9683 /* There is no direct match. But for linkonce section, we should
9684 check if there is a match with comdat group member. We always
9685 record the linkonce section, discarded or not. */
9686 for (l = already_linked_list->entry; l != NULL; l = l->next)
9687 if (l->sec->flags & SEC_GROUP)
9689 asection *first = elf_next_in_group (l->sec);
9691 if (first != NULL
9692 && elf_next_in_group (first) == first
9693 && bfd_elf_match_symbols_in_sections (first, sec))
9695 sec->output_section = bfd_abs_section_ptr;
9696 sec->kept_section = l->sec;
9697 break;
9701 /* This is the first section with this name. Record it. */
9702 bfd_section_already_linked_table_insert (already_linked_list, sec);